mndStb.c 53.6 KB
Newer Older
H
refact  
Hongze Cheng 已提交
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
Shengliang Guan 已提交
16
#include "mndStb.h"
S
Shengliang Guan 已提交
17
#include "mndAuth.h"
S
Shengliang Guan 已提交
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndDnode.h"
S
Shengliang Guan 已提交
20
#include "mndInfoSchema.h"
S
Shengliang Guan 已提交
21
#include "mndMnode.h"
H
Hongze Cheng 已提交
22
#include "mndPerfSchema.h"
C
Cary Xu 已提交
23
#include "mndScheduler.h"
S
Shengliang Guan 已提交
24 25 26
#include "mndShow.h"
#include "mndTrans.h"
#include "mndUser.h"
27
#include "mndVgroup.h"
S
Shengliang Guan 已提交
28
#include "tname.h"
S
Shengliang Guan 已提交
29

S
Shengliang Guan 已提交
30
#define TSDB_STB_VER_NUMBER   1
S
Shengliang Guan 已提交
31 32 33 34 35
#define TSDB_STB_RESERVE_SIZE 64

static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw);
static int32_t  mndStbActionInsert(SSdb *pSdb, SStbObj *pStb);
static int32_t  mndStbActionDelete(SSdb *pSdb, SStbObj *pStb);
S
Shengliang Guan 已提交
36
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
S
Shengliang Guan 已提交
37 38 39 40 41 42 43
static int32_t  mndProcessMCreateStbReq(SNodeMsg *pReq);
static int32_t  mndProcessMAlterStbReq(SNodeMsg *pReq);
static int32_t  mndProcessMDropStbReq(SNodeMsg *pReq);
static int32_t  mndProcessVCreateStbRsp(SNodeMsg *pRsp);
static int32_t  mndProcessVAlterStbRsp(SNodeMsg *pRsp);
static int32_t  mndProcessVDropStbRsp(SNodeMsg *pRsp);
static int32_t  mndProcessTableMetaReq(SNodeMsg *pReq);
H
Hongze Cheng 已提交
44
static int32_t  mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
45 46 47 48
static void     mndCancelGetNextStb(SMnode *pMnode, void *pIter);

int32_t mndInitStb(SMnode *pMnode) {
  SSdbTable table = {.sdbType = SDB_STB,
S
Shengliang Guan 已提交
49
                     .keyType = SDB_KEY_BINARY,
S
Shengliang Guan 已提交
50 51 52 53 54 55
                     .encodeFp = (SdbEncodeFp)mndStbActionEncode,
                     .decodeFp = (SdbDecodeFp)mndStbActionDecode,
                     .insertFp = (SdbInsertFp)mndStbActionInsert,
                     .updateFp = (SdbUpdateFp)mndStbActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndStbActionDelete};

S
Shengliang Guan 已提交
56
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcessMCreateStbReq);
S
Shengliang Guan 已提交
57
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcessMAlterStbReq);
S
Shengliang Guan 已提交
58 59
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcessMDropStbReq);
  mndSetMsgHandle(pMnode, TDMT_VND_CREATE_STB_RSP, mndProcessVCreateStbRsp);
S
Shengliang Guan 已提交
60
  mndSetMsgHandle(pMnode, TDMT_VND_ALTER_STB_RSP, mndProcessVAlterStbRsp);
S
Shengliang Guan 已提交
61
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_STB_RSP, mndProcessVDropStbRsp);
D
dapan1121 已提交
62
  mndSetMsgHandle(pMnode, TDMT_MND_TABLE_META, mndProcessTableMetaReq);
S
Shengliang Guan 已提交
63 64 65

  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STB, mndRetrieveStb);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STB, mndCancelGetNextStb);
S
Shengliang Guan 已提交
66 67

  return sdbSetTable(pMnode->pSdb, table);
S
Shengliang Guan 已提交
68 69
}

S
Shengliang Guan 已提交
70
void mndCleanupStb(SMnode *pMnode) {}
S
Shengliang Guan 已提交
71

72
SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
73 74
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
sma  
Shengliang Guan 已提交
75
  int32_t size = sizeof(SStbObj) + (pStb->numOfColumns + pStb->numOfTags + pStb->numOfSmas) * sizeof(SSchema) +
76
                 + pStb->commentLen + pStb->ast1Len + pStb->ast2Len + TSDB_STB_RESERVE_SIZE;
S
Shengliang Guan 已提交
77
  SSdbRaw *pRaw = sdbAllocRaw(SDB_STB, TSDB_STB_VER_NUMBER, size);
78
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
79 80

  int32_t dataPos = 0;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  SDB_SET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->updateTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->uid, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->dbUid, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->version, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, _OVER)
  SDB_SET_INT32(pRaw, dataPos, (int32_t)(pStb->xFilesFactor * 10000), _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->delay, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ttl, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfSmas, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->commentLen, _OVER)
96 97
  SDB_SET_INT32(pRaw, dataPos, pStb->ast1Len, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
98

S
Shengliang Guan 已提交
99 100
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
101 102 103 104
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
105 106 107 108
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
109 110 111 112
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
113 114
  }

S
sma  
Shengliang Guan 已提交
115
  for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
S
sma  
Shengliang Guan 已提交
116
    SSchema *pSchema = &pStb->pSmas[i];
117 118 119 120
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
sma  
Shengliang Guan 已提交
121 122 123
  }

  if (pStb->commentLen > 0) {
124
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, _OVER)
S
sma  
Shengliang Guan 已提交
125
  }
126 127 128 129 130 131
  if (pStb->ast1Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
  }
  if (pStb->ast2Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
  }
132 133
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
134 135 136

  terrno = 0;

137
_OVER:
138 139 140 141 142
  if (terrno != 0) {
    mError("stb:%s, failed to encode to raw:%p since %s", pStb->name, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
143

144
  mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
145 146 147
  return pRaw;
}

S
Shengliang Guan 已提交
148
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
149 150
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
151
  int8_t sver = 0;
152
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
153

S
Shengliang Guan 已提交
154
  if (sver != TSDB_STB_VER_NUMBER) {
S
Shengliang Guan 已提交
155
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
156
    goto _OVER;
S
Shengliang Guan 已提交
157 158
  }

S
Shengliang 已提交
159
  SSdbRow *pRow = sdbAllocRow(sizeof(SStbObj));
160
  if (pRow == NULL) goto _OVER;
161

S
Shengliang Guan 已提交
162
  SStbObj *pStb = sdbGetRowObj(pRow);
163
  if (pStb == NULL) goto _OVER;
S
Shengliang Guan 已提交
164 165

  int32_t dataPos = 0;
166 167 168 169 170 171 172 173
  SDB_GET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->updateTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->uid, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->dbUid, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->version, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, _OVER)
S
sma  
Shengliang Guan 已提交
174
  int32_t xFilesFactor = 0;
175
  SDB_GET_INT32(pRaw, dataPos, &xFilesFactor, _OVER)
S
sma  
Shengliang Guan 已提交
176
  pStb->xFilesFactor = xFilesFactor / 10000.0f;
177 178 179 180 181 182
  SDB_GET_INT32(pRaw, dataPos, &pStb->delay, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ttl, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfSmas, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->commentLen, _OVER)
183 184
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
185

wafwerar's avatar
wafwerar 已提交
186 187 188
  pStb->pColumns = taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchema));
  pStb->pTags = taosMemoryCalloc(pStb->numOfTags, sizeof(SSchema));
  pStb->pSmas = taosMemoryCalloc(pStb->numOfSmas, sizeof(SSchema));
S
sma  
Shengliang Guan 已提交
189
  if (pStb->pColumns == NULL || pStb->pTags == NULL || pStb->pSmas == NULL) {
190
    goto _OVER;
S
Shengliang 已提交
191
  }
S
Shengliang Guan 已提交
192

S
Shengliang Guan 已提交
193 194
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
195 196 197 198
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
199 200 201 202
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
203 204 205 206
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
207 208
  }

S
sma  
Shengliang Guan 已提交
209 210
  for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
    SSchema *pSchema = &pStb->pSmas[i];
211 212 213 214
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
sma  
Shengliang Guan 已提交
215 216
  }

S
sma  
Shengliang Guan 已提交
217
  if (pStb->commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
218
    pStb->comment = taosMemoryCalloc(pStb->commentLen, 1);
219 220
    if (pStb->comment == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, _OVER)
S
sma  
Shengliang Guan 已提交
221
  }
222 223 224 225 226 227 228 229 230 231
  if (pStb->ast1Len > 0) {
    pStb->pAst1 = taosMemoryCalloc(pStb->ast1Len, 1);
    if (pStb->pAst1 == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
  }
  if (pStb->ast2Len > 0) {
    pStb->pAst2 = taosMemoryCalloc(pStb->ast2Len, 1);
    if (pStb->pAst2 == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
  }
232
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, _OVER)
233 234 235

  terrno = 0;

236
_OVER:
237 238
  if (terrno != 0) {
    mError("stb:%s, failed to decode from raw:%p since %s", pStb->name, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
239 240 241 242
    taosMemoryFreeClear(pStb->pColumns);
    taosMemoryFreeClear(pStb->pTags);
    taosMemoryFreeClear(pStb->comment);
    taosMemoryFreeClear(pRow);
243 244
    return NULL;
  }
S
Shengliang Guan 已提交
245

246
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
247 248 249
  return pRow;
}

S
Shengliang Guan 已提交
250
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
251
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
252 253 254
  return 0;
}

S
Shengliang Guan 已提交
255
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
256
  mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
wafwerar's avatar
wafwerar 已提交
257 258 259
  taosMemoryFreeClear(pStb->pColumns);
  taosMemoryFreeClear(pStb->pTags);
  taosMemoryFreeClear(pStb->comment);
260 261
  taosMemoryFreeClear(pStb->pAst1);
  taosMemoryFreeClear(pStb->pAst2);
S
Shengliang Guan 已提交
262 263 264
  return 0;
}

S
Shengliang Guan 已提交
265 266
static int32_t mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew) {
  mTrace("stb:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
S
Shengliang Guan 已提交
267

S
Shengliang Guan 已提交
268
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
269 270

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
271
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
272
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
273
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
274
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
275 276 277 278 279 280 281 282
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

  if (pOld->numOfTags < pNew->numOfTags) {
wafwerar's avatar
wafwerar 已提交
283
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
284
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
285
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
286
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
287 288 289 290
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
291
    }
S
Shengliang Guan 已提交
292 293
  }

S
Shengliang Guan 已提交
294
  if (pOld->numOfSmas < pNew->numOfSmas) {
wafwerar's avatar
wafwerar 已提交
295
    void *pSmas = taosMemoryMalloc(pNew->numOfSmas * sizeof(SSchema));
S
Shengliang Guan 已提交
296
    if (pSmas != NULL) {
wafwerar's avatar
wafwerar 已提交
297
      taosMemoryFree(pOld->pSmas);
S
Shengliang Guan 已提交
298 299 300 301 302 303 304 305 306
      pOld->pSmas = pSmas;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

  if (pOld->commentLen < pNew->commentLen) {
wafwerar's avatar
wafwerar 已提交
307
    void *comment = taosMemoryMalloc(pNew->commentLen);
S
Shengliang Guan 已提交
308
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
309
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
310 311 312 313 314 315 316 317
      pOld->comment = comment;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  if (pOld->ast1Len < pNew->ast1Len) {
    void *pAst1 = taosMemoryMalloc(pNew->ast1Len);
    if (pAst1 != NULL) {
      taosMemoryFree(pOld->pAst1);
      pOld->pAst1 = pAst1;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

  if (pOld->ast2Len < pNew->ast2Len) {
    void *pAst2 = taosMemoryMalloc(pNew->ast2Len);
    if (pAst2 != NULL) {
      taosMemoryFree(pOld->pAst2);
      pOld->pAst2 = pAst2;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

S
Shengliang Guan 已提交
342 343
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
S
Shengliang Guan 已提交
344
  pOld->nextColId = pNew->nextColId;
S
Shengliang Guan 已提交
345 346
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
347 348
  memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
349
  if (pNew->commentLen != 0) {
S
Shengliang Guan 已提交
350
    memcpy(pOld->comment, pNew->comment, pNew->commentLen);
S
Shengliang Guan 已提交
351
  }
352 353 354 355 356 357
  if (pNew->ast1Len != 0) {
    memcpy(pOld->pAst1, pNew->pAst1, pNew->ast1Len);
  }
  if (pNew->ast2Len != 0) {
    memcpy(pOld->pAst2, pNew->pAst2, pNew->ast2Len);
  }
S
Shengliang Guan 已提交
358
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
359 360 361
  return 0;
}

S
Shengliang Guan 已提交
362
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
363
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
364
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
365
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
366 367 368
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
369 370
}

S
Shengliang Guan 已提交
371
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
372 373 374 375
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

S
Shengliang Guan 已提交
376
static SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
377 378
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
379

S
Shengliang Guan 已提交
380 381
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
382

S
Shengliang Guan 已提交
383 384
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
385

C
Cary Xu 已提交
386
static FORCE_INLINE int schemaExColIdCompare(const void *colId, const void *pSchema) {
H
Hongze Cheng 已提交
387
  if (*(col_id_t *)colId < ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
388
    return -1;
H
Hongze Cheng 已提交
389
  } else if (*(col_id_t *)colId > ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
390 391 392 393 394
    return 1;
  }
  return 0;
}

S
Shengliang Guan 已提交
395
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
396
  SName name = {0};
S
Shengliang Guan 已提交
397
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
X
Xiaoyu Wang 已提交
398 399
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, dbFName);
400

S
Shengliang Guan 已提交
401
  SVCreateTbReq req = {0};
S
Shengliang Guan 已提交
402
  req.name = (char *)tNameGetTableName(&name);
H
more  
Hongze Cheng 已提交
403 404
  req.ttl = 0;
  req.keep = 0;
405
  req.rollup = pStb->pAst1 > 0 ? 1 : 0;
H
more  
Hongze Cheng 已提交
406 407 408 409
  req.type = TD_SUPER_TABLE;
  req.stbCfg.suid = pStb->uid;
  req.stbCfg.nCols = pStb->numOfColumns;
  req.stbCfg.nTagCols = pStb->numOfTags;
S
Shengliang Guan 已提交
410
  req.stbCfg.pTagSchema = pStb->pTags;
C
Cary Xu 已提交
411
  req.stbCfg.nBSmaCols = pStb->numOfSmas;
H
Hongze Cheng 已提交
412
  req.stbCfg.pSchema = (SSchema *)taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchema));
C
Cary Xu 已提交
413 414 415 416
  if (req.stbCfg.pSchema == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
C
Cary Xu 已提交
417

H
Hongze Cheng 已提交
418 419 420
  memcpy(req.stbCfg.pSchema, pStb->pColumns, sizeof(SSchema) * pStb->numOfColumns);
  for (int i = 0; i < pStb->numOfColumns; i++) {
    req.stbCfg.pSchema[i].flags = SCHEMA_SMA_ON;
C
Cary Xu 已提交
421
  }
H
more  
Hongze Cheng 已提交
422

C
Cary Xu 已提交
423 424 425 426 427 428 429 430 431 432 433
  SRSmaParam *pRSmaParam = NULL;
  if (req.rollup) {
    pRSmaParam = (SRSmaParam *)taosMemoryCalloc(1, sizeof(SRSmaParam));
    if (pRSmaParam == NULL) {
      taosMemoryFreeClear(req.stbCfg.pSchema);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return NULL;
    }

    pRSmaParam->xFilesFactor = pStb->xFilesFactor;
    pRSmaParam->delay = pStb->delay;
C
Cary Xu 已提交
434
    if (pStb->ast1Len > 0) {
C
Cary Xu 已提交
435 436 437 438 439
      if (mndConvertRSmaTask(pStb->pAst1, 0, 0, &pRSmaParam->qmsg1, &pRSmaParam->qmsg1Len) != TSDB_CODE_SUCCESS) {
        taosMemoryFreeClear(req.stbCfg.pRSmaParam);
        taosMemoryFreeClear(req.stbCfg.pSchema);
        return NULL;
      }
C
Cary Xu 已提交
440 441
    }
    if (pStb->ast2Len > 0) {
C
Cary Xu 已提交
442 443 444 445 446 447
      if (mndConvertRSmaTask(pStb->pAst2, 0, 0, &pRSmaParam->qmsg2, &pRSmaParam->qmsg2Len) != TSDB_CODE_SUCCESS) {
        taosMemoryFreeClear(pRSmaParam->qmsg1);
        taosMemoryFreeClear(req.stbCfg.pRSmaParam);
        taosMemoryFreeClear(req.stbCfg.pSchema);
        return NULL;
      }
C
Cary Xu 已提交
448 449
    }

C
Cary Xu 已提交
450
    req.stbCfg.pRSmaParam = pRSmaParam;
H
Hongze Cheng 已提交
451
  }
C
Cary Xu 已提交
452

H
Hongze Cheng 已提交
453
  int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
wafwerar's avatar
wafwerar 已提交
454
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
455
  if (pHead == NULL) {
C
Cary Xu 已提交
456
    if (pRSmaParam) {
C
Cary Xu 已提交
457 458
      taosMemoryFreeClear(pRSmaParam->qmsg1);
      taosMemoryFreeClear(pRSmaParam->qmsg2);
C
Cary Xu 已提交
459 460
      taosMemoryFreeClear(pRSmaParam);
    }
C
Cary Xu 已提交
461
    taosMemoryFreeClear(req.stbCfg.pSchema);
H
more  
Hongze Cheng 已提交
462 463 464 465
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
466 467
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
468

S
Shengliang Guan 已提交
469
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
more  
Hongze Cheng 已提交
470 471
  tSerializeSVCreateTbReq(&pBuf, &req);

S
Shengliang Guan 已提交
472
  *pContLen = contLen;
C
Cary Xu 已提交
473
  if (pRSmaParam) {
C
Cary Xu 已提交
474 475
    taosMemoryFreeClear(pRSmaParam->qmsg1);
    taosMemoryFreeClear(pRSmaParam->qmsg2);
C
Cary Xu 已提交
476 477
    taosMemoryFreeClear(pRSmaParam);
  }
C
Cary Xu 已提交
478
  taosMemoryFreeClear(req.stbCfg.pSchema);
S
Shengliang Guan 已提交
479
  return pHead;
480 481
}

S
Shengliang Guan 已提交
482
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
S
Shengliang Guan 已提交
483 484 485 486 487 488 489 490
  SName name = {0};
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  SVDropTbReq req = {0};
  req.ver = 0;
  req.name = (char *)tNameGetTableName(&name);
  req.type = TD_SUPER_TABLE;
  req.suid = pStb->uid;
491

S
Shengliang Guan 已提交
492
  int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
wafwerar's avatar
wafwerar 已提交
493
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
494
  if (pHead == NULL) {
495 496 497 498
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
499 500 501 502 503
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
  tSerializeSVDropTbReq(&pBuf, &req);
504

S
Shengliang Guan 已提交
505 506
  *pContLen = contLen;
  return pHead;
507 508
}

S
Shengliang Guan 已提交
509
static int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
510
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
511
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
512 513
    return -1;
  }
S
Shengliang Guan 已提交
514

S
Shengliang Guan 已提交
515
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
516
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
517 518
    return -1;
  }
S
Shengliang Guan 已提交
519

S
Shengliang Guan 已提交
520
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
521
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
522 523
    return -1;
  }
S
Shengliang Guan 已提交
524

S
Shengliang Guan 已提交
525
  SField *pField = taosArrayGet(pCreate->pColumns, 0);
S
Shengliang Guan 已提交
526
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
S
Shengliang Guan 已提交
527 528 529 530
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
531
  for (int32_t i = 0; i < pCreate->numOfColumns; ++i) {
532
    SField *pField1 = taosArrayGet(pCreate->pColumns, i);
S
Shengliang Guan 已提交
533 534 535 536
    if (pField->type < 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
537
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
538 539 540
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
541
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
542 543 544 545 546 547
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
  }

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
548 549
    SField *pField1 = taosArrayGet(pCreate->pTags, i);
    if (pField1->type < 0) {
S
Shengliang Guan 已提交
550
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
551 552
      return -1;
    }
553
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
554
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
555 556
      return -1;
    }
557
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
558
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
559 560 561
      return -1;
    }
  }
S
Shengliang Guan 已提交
562

S
Shengliang Guan 已提交
563 564 565
  return 0;
}

566
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
567 568 569 570 571 572 573 574
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) return -1;

  return 0;
}

575
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
576 577 578 579 580 581 582 583
  SSdbRaw *pUndoRaw = mndStbActionEncode(pStb);
  if (pUndoRaw == NULL) return -1;
  if (mndTransAppendUndolog(pTrans, pUndoRaw) != 0) return -1;
  if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

584
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
585 586 587 588 589 590 591 592
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

593
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
594
  SSdb   *pSdb = pMnode->pSdb;
595
  SVgObj *pVgroup = NULL;
596
  void   *pIter = NULL;
S
Shengliang Guan 已提交
597
  int32_t contLen;
598 599 600 601

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
602 603 604 605
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
606

S
Shengliang Guan 已提交
607
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
608
    if (pReq == NULL) {
609 610 611 612
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
S
Shengliang Guan 已提交
613

614 615
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
616
    action.pCont = pReq;
S
Shengliang Guan 已提交
617
    action.contLen = contLen;
H
Hongze Cheng 已提交
618
    action.msgType = TDMT_VND_CREATE_STB;
619
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
620
      taosMemoryFree(pReq);
621 622 623 624 625 626
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
627 628 629 630

  return 0;
}

631
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
632
  SSdb   *pSdb = pMnode->pSdb;
633
  SVgObj *pVgroup = NULL;
634
  void   *pIter = NULL;
635 636 637 638

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
639 640 641 642
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
643

S
Shengliang Guan 已提交
644
    int32_t contLen = 0;
S
Shengliang Guan 已提交
645
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
646
    if (pReq == NULL) {
647 648 649 650 651
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
652

653 654
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
655
    action.pCont = pReq;
S
Shengliang Guan 已提交
656
    action.contLen = contLen;
H
Hongze Cheng 已提交
657
    action.msgType = TDMT_VND_DROP_STB;
658
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
659
      taosMemoryFree(pReq);
660 661 662 663 664 665
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
666 667 668 669

  return 0;
}

S
sma  
Shengliang Guan 已提交
670
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
C
Cary Xu 已提交
671
  for (int32_t col = 0; col < pStb->numOfColumns; ++col) {
S
sma  
Shengliang Guan 已提交
672
    SSchema *pSchema = &pStb->pColumns[col];
C
Cary Xu 已提交
673
    if (strncasecmp(pSchema->name, colName, TSDB_COL_NAME_LEN) == 0) {
S
sma  
Shengliang Guan 已提交
674 675 676 677 678 679
      return pSchema;
    }
  }
  return NULL;
}

S
Shengliang Guan 已提交
680
static int32_t mndCreateStb(SMnode *pMnode, SNodeMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
S
Shengliang Guan 已提交
681
  SStbObj stbObj = {0};
S
Shengliang Guan 已提交
682 683
  memcpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
684 685
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
686
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
687
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
688
  stbObj.version = 1;
S
Shengliang Guan 已提交
689
  stbObj.nextColId = 1;
C
Cary Xu 已提交
690 691
  stbObj.xFilesFactor = pCreate->xFilesFactor;
  stbObj.delay = pCreate->delay;
S
sma  
Shengliang Guan 已提交
692
  stbObj.ttl = pCreate->ttl;
S
Shengliang Guan 已提交
693 694
  stbObj.numOfColumns = pCreate->numOfColumns;
  stbObj.numOfTags = pCreate->numOfTags;
S
sma  
Shengliang Guan 已提交
695
  stbObj.numOfSmas = pCreate->numOfSmas;
S
sma  
Shengliang Guan 已提交
696
  stbObj.commentLen = pCreate->commentLen;
S
sma  
Shengliang Guan 已提交
697
  if (stbObj.commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
698
    stbObj.comment = taosMemoryCalloc(stbObj.commentLen, 1);
S
sma  
Shengliang Guan 已提交
699 700 701 702 703
    if (stbObj.comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    memcpy(stbObj.comment, pCreate->comment, stbObj.commentLen);
S
sma  
Shengliang Guan 已提交
704
  }
S
Shengliang Guan 已提交
705

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
  stbObj.ast1Len = pCreate->ast1Len;
  if (stbObj.ast1Len > 0) {
    stbObj.pAst1 = taosMemoryCalloc(stbObj.ast1Len, 1);
    if (stbObj.pAst1 == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    memcpy(stbObj.pAst1, pCreate->pAst1, stbObj.ast1Len);
  }

  stbObj.ast2Len = pCreate->ast2Len;
  if (stbObj.ast2Len > 0) {
    stbObj.pAst2 = taosMemoryCalloc(stbObj.ast2Len, 1);
    if (stbObj.pAst2 == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    memcpy(stbObj.pAst2, pCreate->pAst2, stbObj.ast2Len);
  }

wafwerar's avatar
wafwerar 已提交
726 727 728
  stbObj.pColumns = taosMemoryMalloc(stbObj.numOfColumns * sizeof(SSchema));
  stbObj.pTags = taosMemoryMalloc(stbObj.numOfTags * sizeof(SSchema));
  stbObj.pSmas = taosMemoryMalloc(stbObj.numOfSmas * sizeof(SSchema));
S
sma  
Shengliang Guan 已提交
729
  if (stbObj.pColumns == NULL || stbObj.pTags == NULL || stbObj.pSmas == NULL) {
S
Shengliang Guan 已提交
730 731 732
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
733

S
Shengliang Guan 已提交
734
  for (int32_t i = 0; i < stbObj.numOfColumns; ++i) {
S
Shengliang Guan 已提交
735 736 737 738 739 740
    SField  *pField = taosArrayGet(pCreate->pColumns, i);
    SSchema *pSchema = &stbObj.pColumns[i];
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
    pSchema->colId = stbObj.nextColId;
S
Shengliang Guan 已提交
741 742 743 744
    stbObj.nextColId++;
  }

  for (int32_t i = 0; i < stbObj.numOfTags; ++i) {
S
Shengliang Guan 已提交
745 746 747 748 749 750
    SField  *pField = taosArrayGet(pCreate->pTags, i);
    SSchema *pSchema = &stbObj.pTags[i];
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
    pSchema->colId = stbObj.nextColId;
S
Shengliang Guan 已提交
751
    stbObj.nextColId++;
S
Shengliang Guan 已提交
752 753
  }

S
sma  
Shengliang Guan 已提交
754 755 756 757 758
  for (int32_t i = 0; i < stbObj.numOfSmas; ++i) {
    SField  *pField = taosArrayGet(pCreate->pSmas, i);
    SSchema *pSchema = &stbObj.pSmas[i];
    SSchema *pColSchema = mndFindStbColumns(&stbObj, pField->name);
    if (pColSchema == NULL) {
C
Cary Xu 已提交
759
      mError("stb:%s, sma:%s not found in columns", stbObj.name, pField->name);
S
sma  
Shengliang Guan 已提交
760 761 762 763 764 765
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    memcpy(pSchema, pColSchema, sizeof(SSchema));
  }

S
Shengliang 已提交
766
  int32_t code = -1;
S
Shengliang Guan 已提交
767
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_STB, &pReq->rpcMsg);
768
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
769

S
Shengliang Guan 已提交
770
  mDebug("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
S
Shengliang Guan 已提交
771
  mndTransSetDbInfo(pTrans, pDb);
S
Shengliang Guan 已提交
772

773 774 775 776 777 778
  if (mndSetCreateStbRedoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndSetCreateStbUndoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndSetCreateStbCommitLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndSetCreateStbRedoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndSetCreateStbUndoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
779

S
Shengliang Guan 已提交
780 781
  code = 0;

782
_OVER:
S
Shengliang Guan 已提交
783
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
784
  return code;
S
Shengliang Guan 已提交
785 786
}

S
Shengliang Guan 已提交
787 788
static int32_t mndProcessMCreateStbReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
789 790 791 792
  int32_t        code = -1;
  SStbObj       *pTopicStb = NULL;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
S
Shengliang Guan 已提交
793
  SUserObj      *pUser = NULL;
S
Shengliang Guan 已提交
794
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
795

S
Shengliang Guan 已提交
796 797
  if (tDeserializeSMCreateStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
798
    goto _OVER;
S
Shengliang Guan 已提交
799
  }
S
Shengliang Guan 已提交
800

S
Shengliang Guan 已提交
801
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
802 803
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
804
    goto _OVER;
S
Shengliang Guan 已提交
805
  }
S
Shengliang Guan 已提交
806

S
Shengliang Guan 已提交
807
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
808
  if (pStb != NULL) {
S
Shengliang Guan 已提交
809 810 811
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
812
      goto _OVER;
S
Shengliang Guan 已提交
813 814
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
815
      goto _OVER;
S
Shengliang Guan 已提交
816
    }
S
Shengliang Guan 已提交
817
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
818
    goto _OVER;
S
Shengliang Guan 已提交
819 820
  }

S
Shengliang Guan 已提交
821
  pTopicStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
822
  if (pTopicStb != NULL) {
L
Liu Jicong 已提交
823
    terrno = TSDB_CODE_MND_NAME_CONFLICT_WITH_TOPIC;
824
    goto _OVER;
L
Liu Jicong 已提交
825 826
  }

S
Shengliang Guan 已提交
827
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
828 829
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
830
    goto _OVER;
S
Shengliang Guan 已提交
831 832
  }

S
Shengliang Guan 已提交
833 834
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
835
    goto _OVER;
S
Shengliang Guan 已提交
836 837 838
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
839
    goto _OVER;
S
Shengliang Guan 已提交
840 841
  }

S
Shengliang Guan 已提交
842
  code = mndCreateStb(pMnode, pReq, &createReq, pDb);
843
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
844

845
_OVER:
846
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
847
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
848 849
  }

S
Shengliang Guan 已提交
850 851 852
  mndReleaseStb(pMnode, pStb);
  mndReleaseStb(pMnode, pTopicStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
853 854
  mndReleaseUser(pMnode, pUser);
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
855 856

  return code;
S
Shengliang Guan 已提交
857 858
}

S
Shengliang Guan 已提交
859
static int32_t mndProcessVCreateStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
860
  mndTransProcessRsp(pRsp);
861 862
  return 0;
}
S
Shengliang Guan 已提交
863

S
Shengliang Guan 已提交
864
static int32_t mndCheckAlterStbReq(SMAltertbReq *pAlter) {
S
Shengliang Guan 已提交
865 866 867 868
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
869

S
Shengliang Guan 已提交
870 871
  for (int32_t i = 0; i < pAlter->numOfFields; ++i) {
    SField *pField = taosArrayGet(pAlter->pFields, i);
S
Shengliang Guan 已提交
872

S
Shengliang Guan 已提交
873
    if (pField->type <= 0) {
S
Shengliang Guan 已提交
874 875 876
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
877
    if (pField->bytes <= 0) {
S
Shengliang Guan 已提交
878 879 880
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
881
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
882 883 884
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
885 886 887 888 889
  }

  return 0;
}

S
Shengliang Guan 已提交
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) {
  for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
    if (strcasecmp(pStb->pTags[tag].name, tagName) == 0) {
      return tag;
    }
  }

  return -1;
}

static int32_t mndFindSuperTableColumnIndex(const SStbObj *pStb, const char *colName) {
  for (int32_t col = 0; col < pStb->numOfColumns; col++) {
    if (strcasecmp(pStb->pColumns[col].name, colName) == 0) {
      return col;
    }
  }

  return -1;
}

static int32_t mndAllocStbSchemas(const SStbObj *pOld, SStbObj *pNew) {
wafwerar's avatar
wafwerar 已提交
911 912
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
913 914 915 916 917 918 919 920 921 922
  if (pNew->pTags == NULL || pNew->pColumns == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  memcpy(pNew->pColumns, pOld->pColumns, sizeof(SSchema) * pOld->numOfColumns);
  memcpy(pNew->pTags, pOld->pTags, sizeof(SSchema) * pOld->numOfTags);
  return 0;
}

S
Shengliang Guan 已提交
923
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
924 925 926 927 928 929 930 931 932 933
  if (pOld->numOfTags + ntags > TSDB_MAX_TAGS) {
    terrno = TSDB_CODE_MND_TOO_MANY_TAGS;
    return -1;
  }

  if (pOld->numOfColumns + ntags + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
934 935 936 937 938
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
939
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
940 941
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
942
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
943 944 945
      return -1;
    }

S
Shengliang Guan 已提交
946
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
947
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
948 949 950
      return -1;
    }

S
Shengliang Guan 已提交
951 952 953 954
    SSchema *pSchema = &pNew->pTags[pOld->numOfTags + i];
    pSchema->bytes = pField->bytes;
    pSchema->type = pField->type;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
S
Shengliang Guan 已提交
955 956
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
957 958

    mDebug("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
  }

  pNew->version++;
  return 0;
}

static int32_t mndDropSuperTableTag(const SStbObj *pOld, SStbObj *pNew, const char *tagName) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, tagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pTags + tag, pNew->pTags + tag + 1, sizeof(SSchema) * (pNew->numOfTags - tag - 1));
S
Shengliang Guan 已提交
977
  pNew->numOfTags--;
S
Shengliang Guan 已提交
978 979 980 981 982 983

  pNew->version++;
  mDebug("stb:%s, start to drop tag %s", pNew->name, tagName);
  return 0;
}

S
Shengliang Guan 已提交
984 985 986 987 988 989 990 991 992 993 994 995
static int32_t mndAlterStbTagName(const SStbObj *pOld, SStbObj *pNew, SArray *pFields) {
  if ((int32_t)taosArrayGetSize(pFields) != 2) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

  SField *pField0 = taosArrayGet(pFields, 0);
  SField *pField1 = taosArrayGet(pFields, 1);

  const char *oldTagName = pField0->name;
  const char *newTagName = pField1->name;

S
Shengliang Guan 已提交
996 997 998 999 1000 1001 1002
  int32_t tag = mndFindSuperTableTagIndex(pOld, oldTagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

  if (mndFindSuperTableTagIndex(pOld, newTagName) >= 0) {
S
Shengliang Guan 已提交
1003
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1004 1005 1006
    return -1;
  }

S
Shengliang Guan 已提交
1007 1008
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    return -1;
  }

  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pSchema = (SSchema *)(pNew->pTags + tag);
  memcpy(pSchema->name, newTagName, TSDB_COL_NAME_LEN);

  pNew->version++;
  mDebug("stb:%s, start to modify tag %s to %s", pNew->name, oldTagName, newTagName);
  return 0;
}

S
Shengliang Guan 已提交
1024 1025
static int32_t mndAlterStbTagBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pTag = pNew->pTags + tag;

S
Shengliang Guan 已提交
1037 1038 1039 1040 1041
  if (!(pTag->type == TSDB_DATA_TYPE_BINARY || pTag->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
1042
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1043 1044 1045 1046
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1047
  pTag->bytes = pField->bytes;
S
Shengliang Guan 已提交
1048 1049
  pNew->version++;

S
Shengliang Guan 已提交
1050
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1051 1052 1053
  return 0;
}

S
Shengliang Guan 已提交
1054
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1055 1056 1057 1058 1059
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1060 1061 1062 1063 1064
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1065
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1066 1067
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
1068
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1069 1070 1071
      return -1;
    }

S
Shengliang Guan 已提交
1072
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
1073
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1074 1075 1076
      return -1;
    }

S
Shengliang Guan 已提交
1077 1078 1079 1080
    SSchema *pSchema = &pNew->pColumns[pOld->numOfColumns + i];
    pSchema->bytes = pField->bytes;
    pSchema->type = pField->type;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
S
Shengliang Guan 已提交
1081 1082
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1083 1084

    mDebug("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1085 1086 1087 1088 1089 1090 1091 1092
  }

  pNew->version++;
  return 0;
}

static int32_t mndDropSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, const char *colName) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
S
Shengliang Guan 已提交
1093
  if (col < 0) {
S
Shengliang Guan 已提交
1094 1095 1096 1097
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
  if (col == 0) {
    terrno = TSDB_CODE_MND_INVALID_STB_ALTER_OPTION;
    return -1;
  }

  if (pOld->numOfColumns == 2) {
    terrno = TSDB_CODE_MND_INVALID_STB_ALTER_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
1108 1109 1110 1111 1112
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
S
Shengliang Guan 已提交
1113
  pNew->numOfColumns--;
S
Shengliang Guan 已提交
1114 1115 1116 1117 1118 1119

  pNew->version++;
  mDebug("stb:%s, start to drop col %s", pNew->name, colName);
  return 0;
}

S
Shengliang Guan 已提交
1120 1121
static int32_t mndAlterStbColumnBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1122 1123 1124 1125 1126 1127 1128
  if (col < 0) {
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

  uint32_t nLen = 0;
  for (int32_t i = 0; i < pOld->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1129
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  }

  if (nLen > TSDB_MAX_BYTES_PER_ROW) {
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pCol = pNew->pColumns + col;
S
Shengliang Guan 已提交
1142 1143
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1144 1145 1146
    return -1;
  }

S
Shengliang Guan 已提交
1147
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1148
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1149 1150 1151
    return -1;
  }

S
Shengliang Guan 已提交
1152
  pCol->bytes = pField->bytes;
S
Shengliang Guan 已提交
1153 1154
  pNew->version++;

S
Shengliang Guan 已提交
1155
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1156 1157 1158
  return 0;
}

S
Shengliang Guan 已提交
1159
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1160 1161 1162
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
S
Shengliang Guan 已提交
1163
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_UPDATING) != 0) return -1;
S
Shengliang Guan 已提交
1164 1165 1166 1167

  return 0;
}

S
Shengliang Guan 已提交
1168
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1169 1170 1171 1172 1173 1174 1175 1176
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
1177
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1178 1179 1180 1181 1182 1183 1184 1185
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
  int32_t contLen;

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1186 1187 1188 1189
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1190

S
Shengliang Guan 已提交
1191
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    if (pReq == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }

    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pReq;
    action.contLen = contLen;
S
Shengliang Guan 已提交
1202
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1203
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1204
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1215
static int32_t mndAlterStb(SMnode *pMnode, SNodeMsg *pReq, const SMAltertbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
S
Shengliang Guan 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224
  SStbObj stbObj = {0};
  taosRLockLatch(&pOld->lock);
  memcpy(&stbObj, pOld, sizeof(SStbObj));
  stbObj.pColumns = NULL;
  stbObj.pTags = NULL;
  stbObj.updateTime = taosGetTimestampMs();
  taosRUnLockLatch(&pOld->lock);

  int32_t code = -1;
S
Shengliang Guan 已提交
1225
  STrans *pTrans = NULL;
S
Shengliang Guan 已提交
1226
  SField *pField0 = taosArrayGet(pAlter->pFields, 0);
S
Shengliang Guan 已提交
1227

S
Shengliang Guan 已提交
1228
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
1229
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
1230
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1231
      break;
S
Shengliang Guan 已提交
1232
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang Guan 已提交
1233
      code = mndDropSuperTableTag(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1234
      break;
S
Shengliang Guan 已提交
1235
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
S
Shengliang Guan 已提交
1236
      code = mndAlterStbTagName(pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
1237
      break;
S
Shengliang Guan 已提交
1238
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang Guan 已提交
1239
      code = mndAlterStbTagBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1240 1241
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
1242
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1243 1244
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang Guan 已提交
1245
      code = mndDropSuperTableColumn(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1246
      break;
S
Shengliang Guan 已提交
1247
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang Guan 已提交
1248
      code = mndAlterStbColumnBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1249 1250 1251 1252 1253 1254
      break;
    default:
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      break;
  }

1255
  if (code != 0) goto _OVER;
S
Shengliang Guan 已提交
1256 1257

  code = -1;
S
Shengliang Guan 已提交
1258
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_ALTER_STB, &pReq->rpcMsg);
1259
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1260

S
Shengliang Guan 已提交
1261
  mDebug("trans:%d, used to alter stb:%s", pTrans->id, pAlter->name);
S
Shengliang Guan 已提交
1262
  mndTransSetDbInfo(pTrans, pDb);
S
Shengliang Guan 已提交
1263

1264 1265 1266 1267
  if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
1268 1269

  code = 0;
S
Shengliang Guan 已提交
1270

1271
_OVER:
S
Shengliang Guan 已提交
1272
  mndTransDrop(pTrans);
wafwerar's avatar
wafwerar 已提交
1273 1274
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
S
Shengliang Guan 已提交
1275 1276
  return code;
}
S
Shengliang Guan 已提交
1277

S
Shengliang Guan 已提交
1278 1279
static int32_t mndProcessMAlterStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1280 1281 1282
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1283
  SUserObj    *pUser = NULL;
S
Shengliang Guan 已提交
1284
  SMAltertbReq alterReq = {0};
S
Shengliang Guan 已提交
1285

S
Shengliang Guan 已提交
1286
  if (tDeserializeSMAlterStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1287
    terrno = TSDB_CODE_INVALID_MSG;
1288
    goto _OVER;
S
Shengliang Guan 已提交
1289
  }
S
Shengliang Guan 已提交
1290

S
Shengliang Guan 已提交
1291
  mDebug("stb:%s, start to alter", alterReq.name);
1292
  if (mndCheckAlterStbReq(&alterReq) != 0) goto _OVER;
S
Shengliang Guan 已提交
1293

S
Shengliang Guan 已提交
1294
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1295 1296
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
1297
    goto _OVER;
S
Shengliang Guan 已提交
1298 1299
  }

S
Shengliang Guan 已提交
1300
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1301 1302
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1303
    goto _OVER;
S
Shengliang Guan 已提交
1304
  }
S
Shengliang Guan 已提交
1305

S
Shengliang Guan 已提交
1306 1307
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
1308
    goto _OVER;
S
Shengliang Guan 已提交
1309 1310 1311
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1312
    goto _OVER;
S
Shengliang Guan 已提交
1313 1314
  }

S
Shengliang Guan 已提交
1315
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
1316
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1317

1318
_OVER:
1319
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1320
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1321 1322
  }

S
Shengliang Guan 已提交
1323 1324
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1325
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
1326
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1327 1328

  return code;
S
Shengliang Guan 已提交
1329
}
S
Shengliang Guan 已提交
1330

S
Shengliang Guan 已提交
1331
static int32_t mndProcessVAlterStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1332
  mndTransProcessRsp(pRsp);
1333 1334
  return 0;
}
S
Shengliang Guan 已提交
1335

S
Shengliang Guan 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
static int32_t mndSetDropStbRedoLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING) != 0) return -1;

  return 0;
}

static int32_t mndSetDropStbCommitLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
1354 1355 1356 1357
static int32_t mndSetDropStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1358

S
Shengliang Guan 已提交
1359 1360 1361
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1362 1363 1364 1365
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1366

S
Shengliang Guan 已提交
1367
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1368
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
    if (pReq == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pReq;
    action.contLen = contLen;
    action.msgType = TDMT_VND_DROP_STB;
    action.acceptableCode = TSDB_CODE_VND_TB_NOT_EXIST;
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1383
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1394
static int32_t mndDropStb(SMnode *pMnode, SNodeMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1395
  int32_t code = -1;
S
Shengliang Guan 已提交
1396
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_STB, &pReq->rpcMsg);
1397
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1398

S
Shengliang Guan 已提交
1399
  mDebug("trans:%d, used to drop stb:%s", pTrans->id, pStb->name);
S
Shengliang Guan 已提交
1400
  mndTransSetDbInfo(pTrans, pDb);
S
Shengliang Guan 已提交
1401

1402 1403 1404 1405
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
  if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
1406

S
Shengliang Guan 已提交
1407 1408
  code = 0;

1409
_OVER:
S
Shengliang Guan 已提交
1410
  mndTransDrop(pTrans);
S
Shengliang 已提交
1411
  return code;
S
Shengliang Guan 已提交
1412 1413
}

S
Shengliang Guan 已提交
1414 1415
static int32_t mndProcessMDropStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1416 1417 1418 1419
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1420
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
1421

S
Shengliang Guan 已提交
1422
  if (tDeserializeSMDropStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1423
    terrno = TSDB_CODE_INVALID_MSG;
1424
    goto _OVER;
S
Shengliang Guan 已提交
1425
  }
S
Shengliang Guan 已提交
1426

S
Shengliang Guan 已提交
1427
  mDebug("stb:%s, start to drop", dropReq.name);
S
Shengliang Guan 已提交
1428

S
Shengliang Guan 已提交
1429
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1430
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1431 1432
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1433
      code = 0;
1434
      goto _OVER;
S
Shengliang Guan 已提交
1435 1436
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1437
      goto _OVER;
S
Shengliang Guan 已提交
1438 1439 1440
    }
  }

S
Shengliang Guan 已提交
1441
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1442 1443
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1444
    goto _OVER;
S
Shengliang Guan 已提交
1445 1446
  }

S
Shengliang Guan 已提交
1447 1448
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
1449
    goto _OVER;
S
Shengliang Guan 已提交
1450
  }
S
Shengliang Guan 已提交
1451

S
Shengliang Guan 已提交
1452
  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1453
    goto _OVER;
S
Shengliang Guan 已提交
1454 1455 1456 1457 1458
  }

  code = mndDropStb(pMnode, pReq, pDb, pStb);
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;

1459
_OVER:
S
Shengliang Guan 已提交
1460
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1461
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1462 1463
  }

S
Shengliang Guan 已提交
1464 1465 1466 1467 1468
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
1469
}
S
Shengliang Guan 已提交
1470

S
Shengliang Guan 已提交
1471
static int32_t mndProcessVDropStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1472
  mndTransProcessRsp(pRsp);
1473 1474
  return 0;
}
S
Shengliang Guan 已提交
1475

S
Shengliang Guan 已提交
1476
static int32_t mndBuildStbSchemaImp(SDbObj *pDb, SStbObj *pStb, const char *tbName, STableMetaRsp *pRsp) {
S
Shengliang Guan 已提交
1477 1478
  taosRLockLatch(&pStb->lock);

S
Shengliang Guan 已提交
1479
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
wafwerar's avatar
wafwerar 已提交
1480
  pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
1481
  if (pRsp->pSchemas == NULL) {
S
Shengliang Guan 已提交
1482
    taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1483 1484 1485 1486
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
  strcpy(pRsp->dbFName, pStb->db);
  strcpy(pRsp->tbName, tbName);
  strcpy(pRsp->stbName, tbName);
  pRsp->dbId = pDb->uid;
  pRsp->numOfTags = pStb->numOfTags;
  pRsp->numOfColumns = pStb->numOfColumns;
  pRsp->precision = pDb->cfg.precision;
  pRsp->tableType = TSDB_SUPER_TABLE;
  pRsp->update = pDb->cfg.update;
  pRsp->sversion = pStb->version;
  pRsp->suid = pStb->uid;
  pRsp->tuid = pStb->uid;
S
Shengliang Guan 已提交
1499

S
Shengliang Guan 已提交
1500
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1501
    SSchema *pSchema = &pRsp->pSchemas[i];
S
Shengliang Guan 已提交
1502 1503 1504
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1505 1506
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1507 1508 1509
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
S
Shengliang Guan 已提交
1510
    SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
S
Shengliang Guan 已提交
1511
    SSchema *pSrcSchema = &pStb->pTags[i];
S
Shengliang Guan 已提交
1512 1513
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1514 1515
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1516
  }
S
Shengliang Guan 已提交
1517

S
Shengliang Guan 已提交
1518
  taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1519
  return 0;
S
Shengliang Guan 已提交
1520
}
S
Shengliang Guan 已提交
1521

S
Shengliang Guan 已提交
1522 1523 1524
static int32_t mndBuildStbSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp) {
  char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
  snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, tbName);
S
Shengliang Guan 已提交
1525

S
Shengliang Guan 已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
  SDbObj *pDb = mndAcquireDb(pMnode, dbFName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

  SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
    terrno = TSDB_CODE_MND_INVALID_STB;
    return -1;
  }

  int32_t code = mndBuildStbSchemaImp(pDb, pStb, tbName, pRsp);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
S
Shengliang Guan 已提交
1543
}
S
Shengliang Guan 已提交
1544

S
Shengliang Guan 已提交
1545 1546
static int32_t mndProcessTableMetaReq(SNodeMsg *pReq) {
  SMnode       *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1547 1548 1549
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
1550

S
Shengliang Guan 已提交
1551
  if (tDeserializeSTableInfoReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
1552
    terrno = TSDB_CODE_INVALID_MSG;
1553
    goto _OVER;
S
Shengliang Guan 已提交
1554
  }
D
dapan 已提交
1555

D
dapan1121 已提交
1556 1557 1558
  if (0 == strcmp(infoReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
    mDebug("information_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildInsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
1559
      goto _OVER;
D
dapan1121 已提交
1560
    }
D
dapan1121 已提交
1561 1562 1563
  } else if (0 == strcmp(infoReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
    mDebug("performance_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildPerfsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
1564
      goto _OVER;
D
dapan1121 已提交
1565
    }
D
dapan1121 已提交
1566 1567 1568
  } else {
    mDebug("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
1569
      goto _OVER;
D
dapan1121 已提交
1570
    }
S
Shengliang Guan 已提交
1571
  }
S
Shengliang Guan 已提交
1572

S
Shengliang Guan 已提交
1573 1574 1575
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
1576
    goto _OVER;
S
Shengliang Guan 已提交
1577
  }
S
Shengliang Guan 已提交
1578

S
Shengliang Guan 已提交
1579
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1580 1581
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1582
    goto _OVER;
S
Shengliang Guan 已提交
1583
  }
D
dapan 已提交
1584

S
Shengliang Guan 已提交
1585
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1586 1587
  pReq->pRsp = pRsp;
  pReq->rspLen = rspLen;
S
Shengliang Guan 已提交
1588
  code = 0;
S
Shengliang Guan 已提交
1589

S
Shengliang Guan 已提交
1590
  mDebug("stb:%s.%s, meta is retrieved", infoReq.dbFName, infoReq.tbName);
D
dapan 已提交
1591

1592
_OVER:
S
Shengliang Guan 已提交
1593 1594 1595
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1596

S
Shengliang Guan 已提交
1597 1598 1599
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1600

S
Shengliang Guan 已提交
1601 1602 1603 1604 1605 1606 1607 1608
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableMetaVersion *pStbVersions, int32_t numOfStbs, void **ppRsp,
                           int32_t *pRspLen) {
  STableMetaBatchRsp batchMetaRsp = {0};
  batchMetaRsp.pArray = taosArrayInit(numOfStbs, sizeof(STableMetaRsp));
  if (batchMetaRsp.pArray == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
1609

S
Shengliang Guan 已提交
1610 1611 1612 1613 1614
  for (int32_t i = 0; i < numOfStbs; ++i) {
    SSTableMetaVersion *pStbVersion = &pStbVersions[i];
    pStbVersion->suid = be64toh(pStbVersion->suid);
    pStbVersion->sversion = ntohs(pStbVersion->sversion);
    pStbVersion->tversion = ntohs(pStbVersion->tversion);
S
Shengliang Guan 已提交
1615

S
Shengliang Guan 已提交
1616 1617 1618 1619 1620
    STableMetaRsp metaRsp = {0};
    mDebug("stb:%s.%s, start to retrieve meta", pStbVersion->dbFName, pStbVersion->stbName);
    if (mndBuildStbSchema(pMnode, pStbVersion->dbFName, pStbVersion->stbName, &metaRsp) != 0) {
      metaRsp.numOfColumns = -1;
      metaRsp.suid = pStbVersion->suid;
D
dapan 已提交
1621
    }
S
Shengliang Guan 已提交
1622

S
Shengliang Guan 已提交
1623 1624
    if (pStbVersion->sversion != metaRsp.sversion) {
      taosArrayPush(batchMetaRsp.pArray, &metaRsp);
S
Shengliang Guan 已提交
1625
    }
S
Shengliang Guan 已提交
1626
  }
S
Shengliang Guan 已提交
1627

S
Shengliang Guan 已提交
1628 1629 1630 1631 1632
  int32_t rspLen = tSerializeSTableMetaBatchRsp(NULL, 0, &batchMetaRsp);
  if (rspLen < 0) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
1633 1634
  }

wafwerar's avatar
wafwerar 已提交
1635
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
1636 1637 1638 1639
  if (pRsp == NULL) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
1640 1641
  }

S
Shengliang Guan 已提交
1642 1643 1644
  tSerializeSTableMetaBatchRsp(pRsp, rspLen, &batchMetaRsp);
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1645 1646 1647
  return 0;
}

S
Shengliang Guan 已提交
1648
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1649
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1650 1651 1652 1653 1654 1655
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1656
  int32_t numOfStbs = 0;
1657
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1658
  while (1) {
S
Shengliang Guan 已提交
1659
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1660
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1661 1662
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1663
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1664
      numOfStbs++;
S
Shengliang Guan 已提交
1665 1666
    }

S
Shengliang Guan 已提交
1667
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1668 1669
  }

S
Shengliang Guan 已提交
1670
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1671
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1672 1673 1674
  return 0;
}

S
Shengliang Guan 已提交
1675
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
1676 1677
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
1678
  for (pos = 0; tableId[pos] != 0; ++pos) {
H
Haojun Liao 已提交
1679
    if (tableId[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
1680 1681 1682 1683 1684 1685 1686 1687
    if (num == 2) break;
  }

  if (num == 2) {
    strcpy(name, tableId + pos + 1);
  }
}

H
Hongze Cheng 已提交
1688
static int32_t mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
S
Shengliang Guan 已提交
1689
  SMnode  *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1690
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1691 1692 1693
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1694

H
Hongze Cheng 已提交
1695
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
1696 1697
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
1698
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
1699
  }
S
Shengliang Guan 已提交
1700

S
Shengliang Guan 已提交
1701
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
1702
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1703 1704
    if (pShow->pIter == NULL) break;

H
Haojun Liao 已提交
1705
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
1706
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1707 1708 1709 1710 1711
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
1712
    SName name = {0};
H
Hongze Cheng 已提交
1713
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1714 1715
    mndExtractTableName(pStb->name, &stbName[VARSTR_HEADER_SIZE]);
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
1716

H
Hongze Cheng 已提交
1717 1718
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)stbName, false);
1719

H
Hongze Cheng 已提交
1720 1721
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
1722 1723 1724
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

1725
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
H
Hongze Cheng 已提交
1726
    colDataAppend(pColInfo, numOfRows, (const char *)db, false);
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->createdTime, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->numOfColumns, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->numOfTags, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->updateTime, false);  // number of tables

H
Hongze Cheng 已提交
1740
    char *p = taosMemoryMalloc(pStb->commentLen + VARSTR_HEADER_SIZE);  // check malloc failures
1741 1742 1743 1744 1745 1746 1747
    if (p != NULL) {
      if (pStb->commentLen != 0) {
        STR_TO_VARSTR(p, pStb->comment);
      } else {
        STR_TO_VARSTR(p, "");
      }

1748
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1749 1750
      colDataAppend(pColInfo, numOfRows, (const char *)p, false);
      taosMemoryFree(p);
S
Shengliang Guan 已提交
1751
    }
H
Haojun Liao 已提交
1752

S
Shengliang Guan 已提交
1753
    numOfRows++;
S
Shengliang Guan 已提交
1754
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1755 1756
  }

H
Haojun Liao 已提交
1757 1758 1759 1760
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

1761
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1762 1763 1764
  return numOfRows;
}

S
Shengliang Guan 已提交
1765
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1766 1767
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
1768
}