mndStb.c 54.5 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 96
  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->aggregationMethod, _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)
97 98
  SDB_SET_INT32(pRaw, dataPos, pStb->ast1Len, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
99

S
Shengliang Guan 已提交
100 101
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
102 103 104 105
    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 已提交
106 107 108 109
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
110 111 112 113
    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 已提交
114 115
  }

S
sma  
Shengliang Guan 已提交
116
  for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
S
sma  
Shengliang Guan 已提交
117
    SSchema *pSchema = &pStb->pSmas[i];
118 119 120 121
    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 已提交
122 123 124
  }

  if (pStb->commentLen > 0) {
125
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, _OVER)
S
sma  
Shengliang Guan 已提交
126
  }
127 128 129 130 131 132
  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)
  }
133 134
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
135 136 137

  terrno = 0;

138
_OVER:
139 140 141 142 143
  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 已提交
144

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

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

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

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

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

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

  int32_t dataPos = 0;
167 168 169 170 171 172 173 174
  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 已提交
175
  int32_t xFilesFactor = 0;
176
  SDB_GET_INT32(pRaw, dataPos, &xFilesFactor, _OVER)
S
sma  
Shengliang Guan 已提交
177
  pStb->xFilesFactor = xFilesFactor / 10000.0f;
178 179 180 181 182 183 184
  SDB_GET_INT32(pRaw, dataPos, &pStb->aggregationMethod, _OVER)
  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)
185 186
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
187

wafwerar's avatar
wafwerar 已提交
188 189 190
  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 已提交
191
  if (pStb->pColumns == NULL || pStb->pTags == NULL || pStb->pSmas == NULL) {
192
    goto _OVER;
S
Shengliang 已提交
193
  }
S
Shengliang Guan 已提交
194

S
Shengliang Guan 已提交
195 196
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
197 198 199 200
    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 已提交
201 202 203 204
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
205 206 207 208
    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 已提交
209 210
  }

S
sma  
Shengliang Guan 已提交
211 212
  for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
    SSchema *pSchema = &pStb->pSmas[i];
213 214 215 216
    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 已提交
217 218
  }

S
sma  
Shengliang Guan 已提交
219
  if (pStb->commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
220
    pStb->comment = taosMemoryCalloc(pStb->commentLen, 1);
221 222
    if (pStb->comment == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, _OVER)
S
sma  
Shengliang Guan 已提交
223
  }
224 225 226 227 228 229 230 231 232 233
  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)
  }
234
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, _OVER)
235 236 237

  terrno = 0;

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

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

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

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

S
Shengliang Guan 已提交
267 268
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 已提交
269

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

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
273
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
274
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
275
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
276
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
277 278 279 280 281 282 283 284
    } 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 已提交
285
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
286
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
287
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
288
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
289 290 291 292
    } 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 已提交
293
    }
S
Shengliang Guan 已提交
294 295
  }

S
Shengliang Guan 已提交
296
  if (pOld->numOfSmas < pNew->numOfSmas) {
wafwerar's avatar
wafwerar 已提交
297
    void *pSmas = taosMemoryMalloc(pNew->numOfSmas * sizeof(SSchema));
S
Shengliang Guan 已提交
298
    if (pSmas != NULL) {
wafwerar's avatar
wafwerar 已提交
299
      taosMemoryFree(pOld->pSmas);
S
Shengliang Guan 已提交
300 301 302 303 304 305 306 307 308
      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 已提交
309
    void *comment = taosMemoryMalloc(pNew->commentLen);
S
Shengliang Guan 已提交
310
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
311
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
312 313 314 315 316 317 318 319
      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);
    }
  }

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  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 已提交
344 345
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
S
Shengliang Guan 已提交
346
  pOld->nextColId = pNew->nextColId;
S
Shengliang Guan 已提交
347 348
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
349 350
  memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
351
  if (pNew->commentLen != 0) {
S
Shengliang Guan 已提交
352
    memcpy(pOld->comment, pNew->comment, pNew->commentLen);
S
Shengliang Guan 已提交
353
  }
354 355 356 357 358 359
  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 已提交
360
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
361 362 363
  return 0;
}

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
420 421 422
  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 已提交
423
  }
H
more  
Hongze Cheng 已提交
424

C
Cary Xu 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
  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;
    pRSmaParam->nFuncIds = 1;  // only 1 aggregation method supported currently
    pRSmaParam->pFuncIds = (func_id_t *)taosMemoryCalloc(pRSmaParam->nFuncIds, sizeof(func_id_t));
    if (pRSmaParam->pFuncIds == NULL) {
      taosMemoryFreeClear(req.stbCfg.pRSmaParam);
      taosMemoryFreeClear(req.stbCfg.pSchema);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return NULL;
    }
    for (int32_t f = 0; f < pRSmaParam->nFuncIds; ++f) {
      *(pRSmaParam->pFuncIds + f) = pStb->aggregationMethod;
    }
C
Cary Xu 已提交
447
    if (pStb->ast1Len > 0) {
C
Cary Xu 已提交
448 449 450 451 452 453
      if (mndConvertRSmaTask(pStb->pAst1, 0, 0, &pRSmaParam->qmsg1, &pRSmaParam->qmsg1Len) != TSDB_CODE_SUCCESS) {
        taosMemoryFreeClear(pRSmaParam->pFuncIds);
        taosMemoryFreeClear(req.stbCfg.pRSmaParam);
        taosMemoryFreeClear(req.stbCfg.pSchema);
        return NULL;
      }
C
Cary Xu 已提交
454 455
    }
    if (pStb->ast2Len > 0) {
C
Cary Xu 已提交
456 457 458 459 460 461 462 463
      int32_t qmsgLen2 = 0;
      if (mndConvertRSmaTask(pStb->pAst2, 0, 0, &pRSmaParam->qmsg2, &pRSmaParam->qmsg2Len) != TSDB_CODE_SUCCESS) {
        taosMemoryFreeClear(pRSmaParam->pFuncIds);
        taosMemoryFreeClear(pRSmaParam->qmsg1);
        taosMemoryFreeClear(req.stbCfg.pRSmaParam);
        taosMemoryFreeClear(req.stbCfg.pSchema);
        return NULL;
      }
C
Cary Xu 已提交
464 465
    }

C
Cary Xu 已提交
466
    req.stbCfg.pRSmaParam = pRSmaParam;
H
Hongze Cheng 已提交
467
  }
C
Cary Xu 已提交
468

H
Hongze Cheng 已提交
469
  int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
wafwerar's avatar
wafwerar 已提交
470
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
471
  if (pHead == NULL) {
C
Cary Xu 已提交
472 473
    if (pRSmaParam) {
      taosMemoryFreeClear(pRSmaParam->pFuncIds);
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);
H
more  
Hongze Cheng 已提交
479 480 481 482
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
483 484
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
485

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

S
Shengliang Guan 已提交
489
  *pContLen = contLen;
C
Cary Xu 已提交
490 491
  if (pRSmaParam) {
    taosMemoryFreeClear(pRSmaParam->pFuncIds);
C
Cary Xu 已提交
492 493
    taosMemoryFreeClear(pRSmaParam->qmsg1);
    taosMemoryFreeClear(pRSmaParam->qmsg2);
C
Cary Xu 已提交
494 495
    taosMemoryFreeClear(pRSmaParam);
  }
C
Cary Xu 已提交
496
  taosMemoryFreeClear(req.stbCfg.pSchema);
S
Shengliang Guan 已提交
497
  return pHead;
498 499
}

S
Shengliang Guan 已提交
500
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
S
Shengliang Guan 已提交
501 502 503 504 505 506 507 508
  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;
509

S
Shengliang Guan 已提交
510
  int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
wafwerar's avatar
wafwerar 已提交
511
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
512
  if (pHead == NULL) {
513 514 515 516
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
517 518 519 520 521
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

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

S
Shengliang Guan 已提交
523 524
  *pContLen = contLen;
  return pHead;
525 526
}

S
Shengliang Guan 已提交
527
static int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
528
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
529
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
530 531
    return -1;
  }
S
Shengliang Guan 已提交
532

S
Shengliang Guan 已提交
533
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
534
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
535 536
    return -1;
  }
S
Shengliang Guan 已提交
537

S
Shengliang Guan 已提交
538
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
539
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
540 541
    return -1;
  }
S
Shengliang Guan 已提交
542

S
Shengliang Guan 已提交
543
  SField *pField = taosArrayGet(pCreate->pColumns, 0);
S
Shengliang Guan 已提交
544
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
S
Shengliang Guan 已提交
545 546 547 548
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

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

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
566 567
    SField *pField1 = taosArrayGet(pCreate->pTags, i);
    if (pField1->type < 0) {
S
Shengliang Guan 已提交
568
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
569 570
      return -1;
    }
571
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
572
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
573 574
      return -1;
    }
575
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
576
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
577 578 579
      return -1;
    }
  }
S
Shengliang Guan 已提交
580

S
Shengliang Guan 已提交
581 582 583
  return 0;
}

584
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
585 586 587 588 589 590 591 592
  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;
}

593
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
594 595 596 597 598 599 600 601
  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;
}

602
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
603 604 605 606 607 608 609 610
  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;
}

611
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
612
  SSdb   *pSdb = pMnode->pSdb;
613
  SVgObj *pVgroup = NULL;
614
  void   *pIter = NULL;
S
Shengliang Guan 已提交
615
  int32_t contLen;
616 617 618 619

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
620 621 622 623
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
624

S
Shengliang Guan 已提交
625
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
626
    if (pReq == NULL) {
627 628 629 630
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
S
Shengliang Guan 已提交
631

632 633
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
634
    action.pCont = pReq;
S
Shengliang Guan 已提交
635
    action.contLen = contLen;
H
Hongze Cheng 已提交
636
    action.msgType = TDMT_VND_CREATE_STB;
637
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
638
      taosMemoryFree(pReq);
639 640 641 642 643 644
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
645 646 647 648

  return 0;
}

649
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
650
  SSdb   *pSdb = pMnode->pSdb;
651
  SVgObj *pVgroup = NULL;
652
  void   *pIter = NULL;
653 654 655 656

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
657 658 659 660
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
661

S
Shengliang Guan 已提交
662
    int32_t contLen = 0;
S
Shengliang Guan 已提交
663
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
664
    if (pReq == NULL) {
665 666 667 668 669
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
670

671 672
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
673
    action.pCont = pReq;
S
Shengliang Guan 已提交
674
    action.contLen = contLen;
H
Hongze Cheng 已提交
675
    action.msgType = TDMT_VND_DROP_STB;
676
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
677
      taosMemoryFree(pReq);
678 679 680 681 682 683
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
684 685 686 687

  return 0;
}

S
sma  
Shengliang Guan 已提交
688
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
C
Cary Xu 已提交
689
  for (int32_t col = 0; col < pStb->numOfColumns; ++col) {
S
sma  
Shengliang Guan 已提交
690
    SSchema *pSchema = &pStb->pColumns[col];
C
Cary Xu 已提交
691
    if (strncasecmp(pSchema->name, colName, TSDB_COL_NAME_LEN) == 0) {
S
sma  
Shengliang Guan 已提交
692 693 694 695 696 697
      return pSchema;
    }
  }
  return NULL;
}

S
Shengliang Guan 已提交
698
static int32_t mndCreateStb(SMnode *pMnode, SNodeMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
S
Shengliang Guan 已提交
699
  SStbObj stbObj = {0};
S
Shengliang Guan 已提交
700 701
  memcpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
702 703
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
704
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
705
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
706
  stbObj.version = 1;
S
Shengliang Guan 已提交
707
  stbObj.nextColId = 1;
C
Cary Xu 已提交
708 709 710
  stbObj.xFilesFactor = pCreate->xFilesFactor;
  stbObj.aggregationMethod = pCreate->aggregationMethod;
  stbObj.delay = pCreate->delay;
S
sma  
Shengliang Guan 已提交
711
  stbObj.ttl = pCreate->ttl;
S
Shengliang Guan 已提交
712 713
  stbObj.numOfColumns = pCreate->numOfColumns;
  stbObj.numOfTags = pCreate->numOfTags;
S
sma  
Shengliang Guan 已提交
714
  stbObj.numOfSmas = pCreate->numOfSmas;
S
sma  
Shengliang Guan 已提交
715
  stbObj.commentLen = pCreate->commentLen;
S
sma  
Shengliang Guan 已提交
716
  if (stbObj.commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
717
    stbObj.comment = taosMemoryCalloc(stbObj.commentLen, 1);
S
sma  
Shengliang Guan 已提交
718 719 720 721 722
    if (stbObj.comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    memcpy(stbObj.comment, pCreate->comment, stbObj.commentLen);
S
sma  
Shengliang Guan 已提交
723
  }
S
Shengliang Guan 已提交
724

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  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 已提交
745 746 747
  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 已提交
748
  if (stbObj.pColumns == NULL || stbObj.pTags == NULL || stbObj.pSmas == NULL) {
S
Shengliang Guan 已提交
749 750 751
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
752

S
Shengliang Guan 已提交
753
  for (int32_t i = 0; i < stbObj.numOfColumns; ++i) {
S
Shengliang Guan 已提交
754 755 756 757 758 759
    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 已提交
760 761 762 763
    stbObj.nextColId++;
  }

  for (int32_t i = 0; i < stbObj.numOfTags; ++i) {
S
Shengliang Guan 已提交
764 765 766 767 768 769
    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 已提交
770
    stbObj.nextColId++;
S
Shengliang Guan 已提交
771 772
  }

S
sma  
Shengliang Guan 已提交
773 774 775 776 777
  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 已提交
778
      mError("stb:%s, sma:%s not found in columns", stbObj.name, pField->name);
S
sma  
Shengliang Guan 已提交
779 780 781 782 783 784
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    memcpy(pSchema, pColSchema, sizeof(SSchema));
  }

S
Shengliang 已提交
785
  int32_t code = -1;
S
Shengliang Guan 已提交
786
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_STB, &pReq->rpcMsg);
787
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
788

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

792 793 794 795 796 797
  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 已提交
798

S
Shengliang Guan 已提交
799 800
  code = 0;

801
_OVER:
S
Shengliang Guan 已提交
802
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
803
  return code;
S
Shengliang Guan 已提交
804 805
}

S
Shengliang Guan 已提交
806 807
static int32_t mndProcessMCreateStbReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
808 809 810 811
  int32_t        code = -1;
  SStbObj       *pTopicStb = NULL;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
S
Shengliang Guan 已提交
812
  SUserObj      *pUser = NULL;
S
Shengliang Guan 已提交
813
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
814

S
Shengliang Guan 已提交
815 816
  if (tDeserializeSMCreateStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
817
    goto _OVER;
S
Shengliang Guan 已提交
818
  }
S
Shengliang Guan 已提交
819

S
Shengliang Guan 已提交
820
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
821 822
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
823
    goto _OVER;
S
Shengliang Guan 已提交
824
  }
S
Shengliang Guan 已提交
825

S
Shengliang Guan 已提交
826
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
827
  if (pStb != NULL) {
S
Shengliang Guan 已提交
828 829 830
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
831
      goto _OVER;
S
Shengliang Guan 已提交
832 833
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
834
      goto _OVER;
S
Shengliang Guan 已提交
835
    }
S
Shengliang Guan 已提交
836
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
837
    goto _OVER;
S
Shengliang Guan 已提交
838 839
  }

S
Shengliang Guan 已提交
840
  pTopicStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
841
  if (pTopicStb != NULL) {
L
Liu Jicong 已提交
842
    terrno = TSDB_CODE_MND_NAME_CONFLICT_WITH_TOPIC;
843
    goto _OVER;
L
Liu Jicong 已提交
844 845
  }

S
Shengliang Guan 已提交
846
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
847 848
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
849
    goto _OVER;
S
Shengliang Guan 已提交
850 851
  }

S
Shengliang Guan 已提交
852 853
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
854
    goto _OVER;
S
Shengliang Guan 已提交
855 856 857
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
858
    goto _OVER;
S
Shengliang Guan 已提交
859 860
  }

S
Shengliang Guan 已提交
861
  code = mndCreateStb(pMnode, pReq, &createReq, pDb);
862
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
863

864
_OVER:
865
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
866
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
867 868
  }

S
Shengliang Guan 已提交
869 870 871
  mndReleaseStb(pMnode, pStb);
  mndReleaseStb(pMnode, pTopicStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
872 873
  mndReleaseUser(pMnode, pUser);
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
874 875

  return code;
S
Shengliang Guan 已提交
876 877
}

S
Shengliang Guan 已提交
878
static int32_t mndProcessVCreateStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
879
  mndTransProcessRsp(pRsp);
880 881
  return 0;
}
S
Shengliang Guan 已提交
882

S
Shengliang Guan 已提交
883
static int32_t mndCheckAlterStbReq(SMAltertbReq *pAlter) {
S
Shengliang Guan 已提交
884 885 886 887
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
888

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

S
Shengliang Guan 已提交
892
    if (pField->type <= 0) {
S
Shengliang Guan 已提交
893 894 895
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
896
    if (pField->bytes <= 0) {
S
Shengliang Guan 已提交
897 898 899
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
900
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
901 902 903
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
904 905 906 907 908
  }

  return 0;
}

S
Shengliang Guan 已提交
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
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 已提交
930 931
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
932 933 934 935 936 937 938 939 940 941
  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 已提交
942
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
943 944 945 946 947 948 949 950 951 952
  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 已提交
953 954 955 956 957
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
958
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
959 960
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
961
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
962 963 964
      return -1;
    }

S
Shengliang Guan 已提交
965
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
966
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
967 968 969
      return -1;
    }

S
Shengliang Guan 已提交
970 971 972 973
    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 已提交
974 975
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
976 977

    mDebug("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
  }

  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 已提交
996
  pNew->numOfTags--;
S
Shengliang Guan 已提交
997 998 999 1000 1001 1002

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

S
Shengliang Guan 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
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 已提交
1015 1016 1017 1018 1019 1020 1021
  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 已提交
1022
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1023 1024 1025
    return -1;
  }

S
Shengliang Guan 已提交
1026 1027
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    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 已提交
1043 1044
static int32_t mndAlterStbTagBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
  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 已提交
1056 1057 1058 1059 1060
  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 已提交
1061
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1062 1063 1064 1065
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1066
  pTag->bytes = pField->bytes;
S
Shengliang Guan 已提交
1067 1068
  pNew->version++;

S
Shengliang Guan 已提交
1069
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1070 1071 1072
  return 0;
}

S
Shengliang Guan 已提交
1073
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1074 1075 1076 1077 1078
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1079 1080 1081 1082 1083
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1084
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1085 1086
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
1087
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1088 1089 1090
      return -1;
    }

S
Shengliang Guan 已提交
1091
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
1092
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1093 1094 1095
      return -1;
    }

S
Shengliang Guan 已提交
1096 1097 1098 1099
    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 已提交
1100 1101
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1102 1103

    mDebug("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1104 1105 1106 1107 1108 1109 1110 1111
  }

  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 已提交
1112
  if (col < 0) {
S
Shengliang Guan 已提交
1113 1114 1115 1116
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
  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 已提交
1127 1128 1129 1130 1131
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
S
Shengliang Guan 已提交
1132
  pNew->numOfColumns--;
S
Shengliang Guan 已提交
1133 1134 1135 1136 1137 1138

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

S
Shengliang Guan 已提交
1139 1140
static int32_t mndAlterStbColumnBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1141 1142 1143 1144 1145 1146 1147
  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 已提交
1148
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
  }

  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 已提交
1161 1162
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1163 1164 1165
    return -1;
  }

S
Shengliang Guan 已提交
1166
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1167
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1168 1169 1170
    return -1;
  }

S
Shengliang Guan 已提交
1171
  pCol->bytes = pField->bytes;
S
Shengliang Guan 已提交
1172 1173
  pNew->version++;

S
Shengliang Guan 已提交
1174
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1175 1176 1177
  return 0;
}

S
Shengliang Guan 已提交
1178
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1179 1180 1181
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
S
Shengliang Guan 已提交
1182
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_UPDATING) != 0) return -1;
S
Shengliang Guan 已提交
1183 1184 1185 1186

  return 0;
}

S
Shengliang Guan 已提交
1187
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1188 1189 1190 1191 1192 1193 1194 1195
  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 已提交
1196
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1197 1198 1199 1200 1201 1202 1203 1204
  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 已提交
1205 1206 1207 1208
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1209

S
Shengliang Guan 已提交
1210
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
    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 已提交
1221
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1222
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1223
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1234
static int32_t mndAlterStb(SMnode *pMnode, SNodeMsg *pReq, const SMAltertbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
S
Shengliang Guan 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243
  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 已提交
1244
  STrans *pTrans = NULL;
S
Shengliang Guan 已提交
1245
  SField *pField0 = taosArrayGet(pAlter->pFields, 0);
S
Shengliang Guan 已提交
1246

S
Shengliang Guan 已提交
1247
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
1248
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
1249
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1250
      break;
S
Shengliang Guan 已提交
1251
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang Guan 已提交
1252
      code = mndDropSuperTableTag(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1253
      break;
S
Shengliang Guan 已提交
1254
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
S
Shengliang Guan 已提交
1255
      code = mndAlterStbTagName(pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
1256
      break;
S
Shengliang Guan 已提交
1257
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang Guan 已提交
1258
      code = mndAlterStbTagBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1259 1260
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
1261
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1262 1263
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang Guan 已提交
1264
      code = mndDropSuperTableColumn(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1265
      break;
S
Shengliang Guan 已提交
1266
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang Guan 已提交
1267
      code = mndAlterStbColumnBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1268 1269 1270 1271 1272 1273
      break;
    default:
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      break;
  }

1274
  if (code != 0) goto _OVER;
S
Shengliang Guan 已提交
1275 1276

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

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

1283 1284 1285 1286
  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 已提交
1287 1288

  code = 0;
S
Shengliang Guan 已提交
1289

1290
_OVER:
S
Shengliang Guan 已提交
1291
  mndTransDrop(pTrans);
wafwerar's avatar
wafwerar 已提交
1292 1293
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
S
Shengliang Guan 已提交
1294 1295
  return code;
}
S
Shengliang Guan 已提交
1296

S
Shengliang Guan 已提交
1297 1298
static int32_t mndProcessMAlterStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1299 1300 1301
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1302
  SUserObj    *pUser = NULL;
S
Shengliang Guan 已提交
1303
  SMAltertbReq alterReq = {0};
S
Shengliang Guan 已提交
1304

S
Shengliang Guan 已提交
1305
  if (tDeserializeSMAlterStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1306
    terrno = TSDB_CODE_INVALID_MSG;
1307
    goto _OVER;
S
Shengliang Guan 已提交
1308
  }
S
Shengliang Guan 已提交
1309

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

S
Shengliang Guan 已提交
1313
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1314 1315
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
1316
    goto _OVER;
S
Shengliang Guan 已提交
1317 1318
  }

S
Shengliang Guan 已提交
1319
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1320 1321
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1322
    goto _OVER;
S
Shengliang Guan 已提交
1323
  }
S
Shengliang Guan 已提交
1324

S
Shengliang Guan 已提交
1325 1326
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
1327
    goto _OVER;
S
Shengliang Guan 已提交
1328 1329 1330
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1331
    goto _OVER;
S
Shengliang Guan 已提交
1332 1333
  }

S
Shengliang Guan 已提交
1334
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
1335
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1336

1337
_OVER:
1338
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1339
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1340 1341
  }

S
Shengliang Guan 已提交
1342 1343
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1344
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
1345
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1346 1347

  return code;
S
Shengliang Guan 已提交
1348
}
S
Shengliang Guan 已提交
1349

S
Shengliang Guan 已提交
1350
static int32_t mndProcessVAlterStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1351
  mndTransProcessRsp(pRsp);
1352 1353
  return 0;
}
S
Shengliang Guan 已提交
1354

S
Shengliang Guan 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
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 已提交
1373 1374 1375 1376
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 已提交
1377

S
Shengliang Guan 已提交
1378 1379 1380
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1381 1382 1383 1384
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1385

S
Shengliang Guan 已提交
1386
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1387
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
    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 已提交
1402
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1413
static int32_t mndDropStb(SMnode *pMnode, SNodeMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1414
  int32_t code = -1;
S
Shengliang Guan 已提交
1415
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_STB, &pReq->rpcMsg);
1416
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1417

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

1421 1422 1423 1424
  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 已提交
1425

S
Shengliang Guan 已提交
1426 1427
  code = 0;

1428
_OVER:
S
Shengliang Guan 已提交
1429
  mndTransDrop(pTrans);
S
Shengliang 已提交
1430
  return code;
S
Shengliang Guan 已提交
1431 1432
}

S
Shengliang Guan 已提交
1433 1434
static int32_t mndProcessMDropStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1435 1436 1437 1438
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1439
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
1440

S
Shengliang Guan 已提交
1441
  if (tDeserializeSMDropStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1442
    terrno = TSDB_CODE_INVALID_MSG;
1443
    goto _OVER;
S
Shengliang Guan 已提交
1444
  }
S
Shengliang Guan 已提交
1445

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

S
Shengliang Guan 已提交
1448
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1449
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1450 1451
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1452
      code = 0;
1453
      goto _OVER;
S
Shengliang Guan 已提交
1454 1455
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1456
      goto _OVER;
S
Shengliang Guan 已提交
1457 1458 1459
    }
  }

S
Shengliang Guan 已提交
1460
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1461 1462
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1463
    goto _OVER;
S
Shengliang Guan 已提交
1464 1465
  }

S
Shengliang Guan 已提交
1466 1467
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
1468
    goto _OVER;
S
Shengliang Guan 已提交
1469
  }
S
Shengliang Guan 已提交
1470

S
Shengliang Guan 已提交
1471
  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1472
    goto _OVER;
S
Shengliang Guan 已提交
1473 1474 1475 1476 1477
  }

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

1478
_OVER:
S
Shengliang Guan 已提交
1479
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1480
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1481 1482
  }

S
Shengliang Guan 已提交
1483 1484 1485 1486 1487
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
1488
}
S
Shengliang Guan 已提交
1489

S
Shengliang Guan 已提交
1490
static int32_t mndProcessVDropStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1491
  mndTransProcessRsp(pRsp);
1492 1493
  return 0;
}
S
Shengliang Guan 已提交
1494

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

S
Shengliang Guan 已提交
1498
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
wafwerar's avatar
wafwerar 已提交
1499
  pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
1500
  if (pRsp->pSchemas == NULL) {
S
Shengliang Guan 已提交
1501
    taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1502 1503 1504 1505
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
  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 已提交
1518

S
Shengliang Guan 已提交
1519
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1520
    SSchema *pSchema = &pRsp->pSchemas[i];
S
Shengliang Guan 已提交
1521 1522 1523
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1524 1525
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1526 1527 1528
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
S
Shengliang Guan 已提交
1529
    SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
S
Shengliang Guan 已提交
1530
    SSchema *pSrcSchema = &pStb->pTags[i];
S
Shengliang Guan 已提交
1531 1532
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1533 1534
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1535
  }
S
Shengliang Guan 已提交
1536

S
Shengliang Guan 已提交
1537
  taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1538
  return 0;
S
Shengliang Guan 已提交
1539
}
S
Shengliang Guan 已提交
1540

S
Shengliang Guan 已提交
1541 1542 1543
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 已提交
1544

S
Shengliang Guan 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
  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 已提交
1562
}
S
Shengliang Guan 已提交
1563

S
Shengliang Guan 已提交
1564 1565
static int32_t mndProcessTableMetaReq(SNodeMsg *pReq) {
  SMnode       *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1566 1567 1568
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
1569

S
Shengliang Guan 已提交
1570
  if (tDeserializeSTableInfoReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
1571
    terrno = TSDB_CODE_INVALID_MSG;
1572
    goto _OVER;
S
Shengliang Guan 已提交
1573
  }
D
dapan 已提交
1574

D
dapan1121 已提交
1575 1576 1577
  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) {
1578
      goto _OVER;
D
dapan1121 已提交
1579
    }
D
dapan1121 已提交
1580 1581 1582
  } 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) {
1583
      goto _OVER;
D
dapan1121 已提交
1584
    }
D
dapan1121 已提交
1585 1586 1587
  } else {
    mDebug("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
1588
      goto _OVER;
D
dapan1121 已提交
1589
    }
S
Shengliang Guan 已提交
1590
  }
S
Shengliang Guan 已提交
1591

S
Shengliang Guan 已提交
1592 1593 1594
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
1595
    goto _OVER;
S
Shengliang Guan 已提交
1596
  }
S
Shengliang Guan 已提交
1597

S
Shengliang Guan 已提交
1598
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1599 1600
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1601
    goto _OVER;
S
Shengliang Guan 已提交
1602
  }
D
dapan 已提交
1603

S
Shengliang Guan 已提交
1604
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1605 1606
  pReq->pRsp = pRsp;
  pReq->rspLen = rspLen;
S
Shengliang Guan 已提交
1607
  code = 0;
S
Shengliang Guan 已提交
1608

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

1611
_OVER:
S
Shengliang Guan 已提交
1612 1613 1614
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1615

S
Shengliang Guan 已提交
1616 1617 1618
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1619

S
Shengliang Guan 已提交
1620 1621 1622 1623 1624 1625 1626 1627
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 已提交
1628

S
Shengliang Guan 已提交
1629 1630 1631 1632 1633
  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 已提交
1634

S
Shengliang Guan 已提交
1635 1636 1637 1638 1639
    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 已提交
1640
    }
S
Shengliang Guan 已提交
1641

S
Shengliang Guan 已提交
1642 1643
    if (pStbVersion->sversion != metaRsp.sversion) {
      taosArrayPush(batchMetaRsp.pArray, &metaRsp);
S
Shengliang Guan 已提交
1644
    }
S
Shengliang Guan 已提交
1645
  }
S
Shengliang Guan 已提交
1646

S
Shengliang Guan 已提交
1647 1648 1649 1650 1651
  int32_t rspLen = tSerializeSTableMetaBatchRsp(NULL, 0, &batchMetaRsp);
  if (rspLen < 0) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
1652 1653
  }

wafwerar's avatar
wafwerar 已提交
1654
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
1655 1656 1657 1658
  if (pRsp == NULL) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
1659 1660
  }

S
Shengliang Guan 已提交
1661 1662 1663
  tSerializeSTableMetaBatchRsp(pRsp, rspLen, &batchMetaRsp);
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1664 1665 1666
  return 0;
}

S
Shengliang Guan 已提交
1667
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1668
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1669 1670 1671 1672 1673 1674
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1675
  int32_t numOfStbs = 0;
1676
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1677
  while (1) {
S
Shengliang Guan 已提交
1678
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1679
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1680 1681
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1682
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1683
      numOfStbs++;
S
Shengliang Guan 已提交
1684 1685
    }

S
Shengliang Guan 已提交
1686
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1687 1688
  }

S
Shengliang Guan 已提交
1689
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1690
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1691 1692 1693
  return 0;
}

S
Shengliang Guan 已提交
1694
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
1695 1696
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
1697
  for (pos = 0; tableId[pos] != 0; ++pos) {
H
Haojun Liao 已提交
1698
    if (tableId[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
1699 1700 1701 1702 1703 1704 1705 1706
    if (num == 2) break;
  }

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

H
Hongze Cheng 已提交
1707
static int32_t mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
S
Shengliang Guan 已提交
1708
  SMnode  *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1709
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1710 1711 1712
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1713

H
Hongze Cheng 已提交
1714
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
1715 1716
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
1717
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
1718
  }
S
Shengliang Guan 已提交
1719

S
Shengliang Guan 已提交
1720
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
1721
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1722 1723
    if (pShow->pIter == NULL) break;

H
Haojun Liao 已提交
1724
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
1725
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1726 1727 1728 1729 1730
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
1731
    SName name = {0};
H
Hongze Cheng 已提交
1732
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1733 1734
    mndExtractTableName(pStb->name, &stbName[VARSTR_HEADER_SIZE]);
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
1735

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

H
Hongze Cheng 已提交
1739 1740
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
1741 1742 1743
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

1744
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
H
Hongze Cheng 已提交
1745
    colDataAppend(pColInfo, numOfRows, (const char *)db, false);
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758

    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 已提交
1759
    char *p = taosMemoryMalloc(pStb->commentLen + VARSTR_HEADER_SIZE);  // check malloc failures
1760 1761 1762 1763 1764 1765 1766
    if (p != NULL) {
      if (pStb->commentLen != 0) {
        STR_TO_VARSTR(p, pStb->comment);
      } else {
        STR_TO_VARSTR(p, "");
      }

1767
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1768 1769
      colDataAppend(pColInfo, numOfRows, (const char *)p, false);
      taosMemoryFree(p);
S
Shengliang Guan 已提交
1770
    }
H
Haojun Liao 已提交
1771

S
Shengliang Guan 已提交
1772
    numOfRows++;
S
Shengliang Guan 已提交
1773
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1774 1775
  }

H
Haojun Liao 已提交
1776 1777 1778 1779
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

1780
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1781 1782 1783
  return numOfRows;
}

S
Shengliang Guan 已提交
1784
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1785 1786
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
1787
}