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

S
Shengliang Guan 已提交
31
#define TSDB_STB_VER_NUMBER   1
S
Shengliang Guan 已提交
32 33 34 35 36
#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 已提交
37
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44
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 已提交
45
static int32_t  mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
46 47 48 49
static void     mndCancelGetNextStb(SMnode *pMnode, void *pIter);

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

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

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

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

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

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

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

  int32_t dataPos = 0;
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->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
  if (pStb->commentLen > 0) {
116
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, _OVER)
S
sma  
Shengliang Guan 已提交
117
  }
118 119 120 121 122 123
  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)
  }
124 125
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
126 127 128

  terrno = 0;

129
_OVER:
130 131 132 133 134
  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 已提交
135

136
  mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
137 138 139
  return pRaw;
}

S
Shengliang Guan 已提交
140
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
141 142
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
143
  int8_t sver = 0;
144
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
145

S
Shengliang Guan 已提交
146
  if (sver != TSDB_STB_VER_NUMBER) {
S
Shengliang Guan 已提交
147
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
148
    goto _OVER;
S
Shengliang Guan 已提交
149 150
  }

S
Shengliang 已提交
151
  SSdbRow *pRow = sdbAllocRow(sizeof(SStbObj));
152
  if (pRow == NULL) goto _OVER;
153

S
Shengliang Guan 已提交
154
  SStbObj *pStb = sdbGetRowObj(pRow);
155
  if (pStb == NULL) goto _OVER;
S
Shengliang Guan 已提交
156 157

  int32_t dataPos = 0;
158 159 160 161 162 163 164 165
  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 已提交
166
  int32_t xFilesFactor = 0;
167
  SDB_GET_INT32(pRaw, dataPos, &xFilesFactor, _OVER)
S
sma  
Shengliang Guan 已提交
168
  pStb->xFilesFactor = xFilesFactor / 10000.0f;
169 170 171 172 173
  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->commentLen, _OVER)
174 175
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
176

wafwerar's avatar
wafwerar 已提交
177 178
  pStb->pColumns = taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchema));
  pStb->pTags = taosMemoryCalloc(pStb->numOfTags, sizeof(SSchema));
179
  if (pStb->pColumns == NULL || pStb->pTags == NULL) {
180
    goto _OVER;
S
Shengliang 已提交
181
  }
S
Shengliang Guan 已提交
182

S
Shengliang Guan 已提交
183 184
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
185 186 187 188
    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 已提交
189 190 191 192
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
193 194 195 196
    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 已提交
197 198
  }

S
sma  
Shengliang Guan 已提交
199
  if (pStb->commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
200
    pStb->comment = taosMemoryCalloc(pStb->commentLen, 1);
201 202
    if (pStb->comment == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, _OVER)
S
sma  
Shengliang Guan 已提交
203
  }
204 205 206 207 208 209 210 211 212 213
  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)
  }
214
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, _OVER)
215 216 217

  terrno = 0;

218
_OVER:
219 220
  if (terrno != 0) {
    mError("stb:%s, failed to decode from raw:%p since %s", pStb->name, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
221 222 223 224
    taosMemoryFreeClear(pStb->pColumns);
    taosMemoryFreeClear(pStb->pTags);
    taosMemoryFreeClear(pStb->comment);
    taosMemoryFreeClear(pRow);
225 226
    return NULL;
  }
S
Shengliang Guan 已提交
227

228
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
229 230 231
  return pRow;
}

S
Shengliang Guan 已提交
232
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
233
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
234 235 236
  return 0;
}

S
Shengliang Guan 已提交
237
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
238
  mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
wafwerar's avatar
wafwerar 已提交
239 240 241
  taosMemoryFreeClear(pStb->pColumns);
  taosMemoryFreeClear(pStb->pTags);
  taosMemoryFreeClear(pStb->comment);
242 243
  taosMemoryFreeClear(pStb->pAst1);
  taosMemoryFreeClear(pStb->pAst2);
S
Shengliang Guan 已提交
244 245 246
  return 0;
}

S
Shengliang Guan 已提交
247 248
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 已提交
249

S
Shengliang Guan 已提交
250
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
251 252

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
253
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
254
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
255
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
256
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
257 258 259 260 261 262 263 264
    } 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 已提交
265
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
266
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
267
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
268
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
269 270 271 272
    } 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 已提交
273
    }
S
Shengliang Guan 已提交
274 275
  }

S
Shengliang Guan 已提交
276
  if (pOld->commentLen < pNew->commentLen) {
wafwerar's avatar
wafwerar 已提交
277
    void *comment = taosMemoryMalloc(pNew->commentLen);
S
Shengliang Guan 已提交
278
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
279
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
280 281 282 283 284 285 286 287
      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);
    }
  }

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  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 已提交
312 313
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
S
Shengliang Guan 已提交
314
  pOld->nextColId = pNew->nextColId;
S
Shengliang Guan 已提交
315 316
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
317 318
  memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
319
  if (pNew->commentLen != 0) {
S
Shengliang Guan 已提交
320
    memcpy(pOld->comment, pNew->comment, pNew->commentLen);
S
Shengliang Guan 已提交
321
  }
322 323 324 325 326 327
  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 已提交
328
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
329 330 331
  return 0;
}

S
Shengliang Guan 已提交
332
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
333
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
334
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
335
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
336 337 338
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
339 340
}

S
Shengliang Guan 已提交
341
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
342 343 344 345
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

346
SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
347 348
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
349

S
Shengliang Guan 已提交
350 351
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
352

S
Shengliang Guan 已提交
353 354
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
355

C
Cary Xu 已提交
356
static FORCE_INLINE int schemaExColIdCompare(const void *colId, const void *pSchema) {
H
Hongze Cheng 已提交
357
  if (*(col_id_t *)colId < ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
358
    return -1;
H
Hongze Cheng 已提交
359
  } else if (*(col_id_t *)colId > ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
360 361 362 363 364
    return 1;
  }
  return 0;
}

S
Shengliang Guan 已提交
365
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
366 367 368 369
  SCoder         coder = {0};
  int32_t        contLen;
  SName          name = {0};
  SVCreateStbReq req = {0};
H
Hongze Cheng 已提交
370

S
Shengliang Guan 已提交
371
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
X
Xiaoyu Wang 已提交
372 373
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, dbFName);
374

S
Shengliang Guan 已提交
375
  req.name = (char *)tNameGetTableName(&name);
H
Hongze Cheng 已提交
376
  req.suid = pStb->uid;
377
  req.rollup = pStb->ast1Len > 0 ? 1 : 0;
H
Hongze Cheng 已提交
378 379 380 381 382
  req.schema.nCols = pStb->numOfColumns;
  req.schema.sver = 0;
  req.schema.pSchema = pStb->pColumns;
  req.schemaTag.nCols = pStb->numOfTags;
  req.schemaTag.pSchema = pStb->pTags;
H
more  
Hongze Cheng 已提交
383

C
Cary Xu 已提交
384
  if (req.rollup) {
H
Hongze Cheng 已提交
385 386
    req.pRSmaParam.xFilesFactor = pStb->xFilesFactor;
    req.pRSmaParam.delay = pStb->delay;
C
Cary Xu 已提交
387
    if (pStb->ast1Len > 0) {
388
      if (mndConvertRSmaTask(pStb->pAst1, 0, 0, &req.pRSmaParam.qmsg1, &req.pRSmaParam.qmsg1Len) != TSDB_CODE_SUCCESS) {
C
Cary Xu 已提交
389 390
        return NULL;
      }
C
Cary Xu 已提交
391 392
    }
    if (pStb->ast2Len > 0) {
393
      if (mndConvertRSmaTask(pStb->pAst2, 0, 0, &req.pRSmaParam.qmsg2, &req.pRSmaParam.qmsg2Len) != TSDB_CODE_SUCCESS) {
C
Cary Xu 已提交
394 395
        return NULL;
      }
C
Cary Xu 已提交
396
    }
H
Hongze Cheng 已提交
397
  }
H
Hongze Cheng 已提交
398
  // get length
wafwerar's avatar
wafwerar 已提交
399 400 401
  int32_t ret = 0;
  tEncodeSize(tEncodeSVCreateStbReq, &req, contLen, ret);
  if (ret < 0) {
H
Hongze Cheng 已提交
402 403 404
    return NULL;
  }

H
Hongze Cheng 已提交
405
  contLen += sizeof(SMsgHead);
C
Cary Xu 已提交
406

wafwerar's avatar
wafwerar 已提交
407
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
408
  if (pHead == NULL) {
409 410
    taosMemoryFreeClear(req.pRSmaParam.qmsg1);
    taosMemoryFreeClear(req.pRSmaParam.qmsg2);
H
more  
Hongze Cheng 已提交
411 412 413 414
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
415 416
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
417

S
Shengliang Guan 已提交
418
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
Hongze Cheng 已提交
419 420 421 422 423
  tCoderInit(&coder, TD_LITTLE_ENDIAN, pBuf, contLen - sizeof(SMsgHead), TD_ENCODER);
  if (tEncodeSVCreateStbReq(&coder, &req) < 0) {
    return NULL;
  }
  tCoderClear(&coder);
H
more  
Hongze Cheng 已提交
424

S
Shengliang Guan 已提交
425
  *pContLen = contLen;
426 427
  taosMemoryFreeClear(req.pRSmaParam.qmsg1);
  taosMemoryFreeClear(req.pRSmaParam.qmsg2);
S
Shengliang Guan 已提交
428
  return pHead;
429 430
}

S
Shengliang Guan 已提交
431
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
432 433 434 435 436 437 438
  SName        name = {0};
  SVDropStbReq req = {0};
  int32_t      contLen = 0;
  int32_t      ret = 0;
  SMsgHead    *pHead = NULL;
  SCoder       coder = {0};

S
Shengliang Guan 已提交
439 440 441 442
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  req.name = (char *)tNameGetTableName(&name);
  req.suid = pStb->uid;
443

H
Hongze Cheng 已提交
444 445 446 447 448
  tEncodeSize(tEncodeSVDropStbReq, &req, contLen, ret);
  if (ret < 0) return NULL;

  contLen += sizeof(SMsgHead);
  pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
449
  if (pHead == NULL) {
450 451 452 453
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
454 455 456 457
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
Hongze Cheng 已提交
458 459 460 461

  tCoderInit(&coder, TD_LITTLE_ENDIAN, pBuf, contLen - sizeof(SMsgHead), TD_ENCODER);
  tEncodeSVDropStbReq(&coder, &req);
  tCoderClear(&coder);
462

S
Shengliang Guan 已提交
463 464
  *pContLen = contLen;
  return pHead;
465 466
}

467
int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
468
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
469
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
470 471
    return -1;
  }
S
Shengliang Guan 已提交
472

S
Shengliang Guan 已提交
473
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
474
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
475 476
    return -1;
  }
S
Shengliang Guan 已提交
477

S
Shengliang Guan 已提交
478
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
479
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
480 481
    return -1;
  }
S
Shengliang Guan 已提交
482

S
Shengliang Guan 已提交
483
  SField *pField = taosArrayGet(pCreate->pColumns, 0);
S
Shengliang Guan 已提交
484
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
S
Shengliang Guan 已提交
485 486 487 488
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
489
  for (int32_t i = 0; i < pCreate->numOfColumns; ++i) {
490
    SField *pField1 = taosArrayGet(pCreate->pColumns, i);
S
Shengliang Guan 已提交
491 492 493 494
    if (pField->type < 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
495
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
496 497 498
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
499
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
500 501 502 503 504 505
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
  }

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
506 507
    SField *pField1 = taosArrayGet(pCreate->pTags, i);
    if (pField1->type < 0) {
S
Shengliang Guan 已提交
508
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
509 510
      return -1;
    }
511
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
512
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
513 514
      return -1;
    }
515
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
516
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
517 518 519
      return -1;
    }
  }
S
Shengliang Guan 已提交
520

S
Shengliang Guan 已提交
521 522 523
  return 0;
}

524
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
525 526 527 528 529 530 531 532
  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;
}

533
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
534 535 536 537 538 539 540 541
  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;
}

542
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
543 544 545 546 547 548 549 550
  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;
}

551
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
552
  SSdb   *pSdb = pMnode->pSdb;
553
  SVgObj *pVgroup = NULL;
554
  void   *pIter = NULL;
S
Shengliang Guan 已提交
555
  int32_t contLen;
556 557 558 559

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
560 561 562 563
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
564

S
Shengliang Guan 已提交
565
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
566
    if (pReq == NULL) {
567 568 569 570
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
S
Shengliang Guan 已提交
571

572 573
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
574
    action.pCont = pReq;
S
Shengliang Guan 已提交
575
    action.contLen = contLen;
H
Hongze Cheng 已提交
576
    action.msgType = TDMT_VND_CREATE_STB;
577
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
578
      taosMemoryFree(pReq);
579 580 581 582 583 584
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
585 586 587 588

  return 0;
}

589
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
590
  SSdb   *pSdb = pMnode->pSdb;
591
  SVgObj *pVgroup = NULL;
592
  void   *pIter = NULL;
593 594 595 596

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
597 598 599 600
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
601

S
Shengliang Guan 已提交
602
    int32_t contLen = 0;
S
Shengliang Guan 已提交
603
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
604
    if (pReq == NULL) {
605 606 607 608 609
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
610

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

  return 0;
}

S
sma  
Shengliang Guan 已提交
628
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
C
Cary Xu 已提交
629
  for (int32_t col = 0; col < pStb->numOfColumns; ++col) {
S
sma  
Shengliang Guan 已提交
630
    SSchema *pSchema = &pStb->pColumns[col];
C
Cary Xu 已提交
631
    if (strncasecmp(pSchema->name, colName, TSDB_COL_NAME_LEN) == 0) {
S
sma  
Shengliang Guan 已提交
632 633 634 635 636 637
      return pSchema;
    }
  }
  return NULL;
}

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
int32_t mndBuildStbFromReq(SMnode *pMnode, SStbObj *pDst, SMCreateStbReq *pCreate, SDbObj *pDb) {
  memcpy(pDst->name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(pDst->db, pDb->name, TSDB_DB_FNAME_LEN);
  pDst->createdTime = taosGetTimestampMs();
  pDst->updateTime = pDst->createdTime;
  pDst->uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
  pDst->dbUid = pDb->uid;
  pDst->version = 1;
  pDst->nextColId = 1;
  pDst->xFilesFactor = pCreate->xFilesFactor;
  pDst->delay = pCreate->delay;
  pDst->ttl = pCreate->ttl;
  pDst->numOfColumns = pCreate->numOfColumns;
  pDst->numOfTags = pCreate->numOfTags;
  pDst->commentLen = pCreate->commentLen;
  if (pDst->commentLen > 0) {
    pDst->comment = taosMemoryCalloc(pDst->commentLen, 1);
    if (pDst->comment == NULL) {
S
sma  
Shengliang Guan 已提交
656 657 658
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
659
    memcpy(pDst->comment, pCreate->comment, pDst->commentLen);
S
sma  
Shengliang Guan 已提交
660
  }
S
Shengliang Guan 已提交
661

662 663 664 665
  pDst->ast1Len = pCreate->ast1Len;
  if (pDst->ast1Len > 0) {
    pDst->pAst1 = taosMemoryCalloc(pDst->ast1Len, 1);
    if (pDst->pAst1 == NULL) {
666 667 668
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
669
    memcpy(pDst->pAst1, pCreate->pAst1, pDst->ast1Len);
670 671
  }

672 673 674 675
  pDst->ast2Len = pCreate->ast2Len;
  if (pDst->ast2Len > 0) {
    pDst->pAst2 = taosMemoryCalloc(pDst->ast2Len, 1);
    if (pDst->pAst2 == NULL) {
676 677 678
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
679
    memcpy(pDst->pAst2, pCreate->pAst2, pDst->ast2Len);
680 681
  }

682 683 684
  pDst->pColumns = taosMemoryCalloc(1, pDst->numOfColumns * sizeof(SSchema));
  pDst->pTags = taosMemoryCalloc(1, pDst->numOfTags * sizeof(SSchema));
  if (pDst->pColumns == NULL || pDst->pTags == NULL) {
S
Shengliang Guan 已提交
685 686 687
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
688

689
  for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
S
Shengliang Guan 已提交
690
    SField  *pField = taosArrayGet(pCreate->pColumns, i);
691
    SSchema *pSchema = &pDst->pColumns[i];
S
Shengliang Guan 已提交
692 693
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
694
    pSchema->flags = pField->flags;
S
Shengliang Guan 已提交
695
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
696 697
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
698 699
  }

700
  for (int32_t i = 0; i < pDst->numOfTags; ++i) {
S
Shengliang Guan 已提交
701
    SField  *pField = taosArrayGet(pCreate->pTags, i);
702
    SSchema *pSchema = &pDst->pTags[i];
S
Shengliang Guan 已提交
703 704 705
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
706 707
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
708
  }
709 710 711 712 713
  return 0;
}

static int32_t mndCreateStb(SMnode *pMnode, SNodeMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
  SStbObj stbObj = {0};
S
Shengliang Guan 已提交
714

S
Shengliang 已提交
715
  int32_t code = -1;
716

S
Shengliang Guan 已提交
717
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_STB, &pReq->rpcMsg);
718
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
719

S
Shengliang Guan 已提交
720
  mDebug("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
S
Shengliang Guan 已提交
721

722 723 724 725 726 727
  if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) {
    goto _OVER;
  }

  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) goto _OVER;

728
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
729

S
Shengliang Guan 已提交
730 731
  code = 0;

732
_OVER:
S
Shengliang Guan 已提交
733
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
734
  return code;
S
Shengliang Guan 已提交
735
}
736 737 738 739 740 741 742 743 744
int32_t mndAddStbToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  mndTransSetDbInfo(pTrans, pDb);
  if (mndSetCreateStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbUndoLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbUndoActions(pMnode, pTrans, pDb, pStb) != 0) return -1;
  return 0;
}
S
Shengliang Guan 已提交
745

S
Shengliang Guan 已提交
746 747
static int32_t mndProcessMCreateStbReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
748 749 750
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
S
Shengliang Guan 已提交
751
  SUserObj      *pUser = NULL;
S
Shengliang Guan 已提交
752
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
753

S
Shengliang Guan 已提交
754 755
  if (tDeserializeSMCreateStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
756
    goto _OVER;
S
Shengliang Guan 已提交
757
  }
S
Shengliang Guan 已提交
758

S
Shengliang Guan 已提交
759
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
760 761
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
762
    goto _OVER;
S
Shengliang Guan 已提交
763
  }
S
Shengliang Guan 已提交
764

S
Shengliang Guan 已提交
765
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
766
  if (pStb != NULL) {
S
Shengliang Guan 已提交
767 768 769
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
770
      goto _OVER;
S
Shengliang Guan 已提交
771 772
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
773
      goto _OVER;
S
Shengliang Guan 已提交
774
    }
S
Shengliang Guan 已提交
775
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
776
    goto _OVER;
S
Shengliang Guan 已提交
777 778
  }

S
Shengliang Guan 已提交
779
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
780 781
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
782
    goto _OVER;
S
Shengliang Guan 已提交
783 784
  }

S
Shengliang Guan 已提交
785 786
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
787
    goto _OVER;
S
Shengliang Guan 已提交
788 789 790
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
791
    goto _OVER;
S
Shengliang Guan 已提交
792 793
  }

794 795
  int32_t numOfStbs = -1;
  mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs);
L
Liu Jicong 已提交
796
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
797 798 799 800
    terrno = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
    goto _OVER;
  }

S
Shengliang Guan 已提交
801
  code = mndCreateStb(pMnode, pReq, &createReq, pDb);
802
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
803

804
_OVER:
805
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
806
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
807 808
  }

S
Shengliang Guan 已提交
809 810
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
811 812
  mndReleaseUser(pMnode, pUser);
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
813 814

  return code;
S
Shengliang Guan 已提交
815 816
}

S
Shengliang Guan 已提交
817
static int32_t mndProcessVCreateStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
818
  mndTransProcessRsp(pRsp);
819 820
  return 0;
}
S
Shengliang Guan 已提交
821

S
Shengliang Guan 已提交
822
static int32_t mndCheckAlterStbReq(SMAltertbReq *pAlter) {
S
Shengliang Guan 已提交
823 824 825 826
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
827

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

S
Shengliang Guan 已提交
831
    if (pField->type <= 0) {
S
Shengliang Guan 已提交
832 833 834
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
835
    if (pField->bytes <= 0) {
S
Shengliang Guan 已提交
836 837 838
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
839
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
840 841 842
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
843 844 845 846 847
  }

  return 0;
}

S
Shengliang Guan 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
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 已提交
869 870
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
871 872 873 874 875 876 877 878 879 880
  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 已提交
881
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
882 883 884 885 886 887 888 889 890 891
  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 已提交
892 893 894 895 896
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
897
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
898 899
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
900
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
901 902 903
      return -1;
    }

S
Shengliang Guan 已提交
904
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
905
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
906 907 908
      return -1;
    }

S
Shengliang Guan 已提交
909 910 911 912
    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 已提交
913 914
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
915 916

    mDebug("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
  }

  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 已提交
935
  pNew->numOfTags--;
S
Shengliang Guan 已提交
936 937 938 939 940 941

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

S
Shengliang Guan 已提交
942 943 944 945 946 947 948 949 950 951 952 953
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 已提交
954 955 956 957 958 959 960
  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 已提交
961
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
962 963 964
    return -1;
  }

S
Shengliang Guan 已提交
965 966
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
    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 已提交
982 983
static int32_t mndAlterStbTagBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
984 985 986 987 988 989 990 991 992 993 994
  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 已提交
995 996 997 998 999
  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 已提交
1000
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1001 1002 1003 1004
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1005
  pTag->bytes = pField->bytes;
S
Shengliang Guan 已提交
1006 1007
  pNew->version++;

S
Shengliang Guan 已提交
1008
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1009 1010 1011
  return 0;
}

S
Shengliang Guan 已提交
1012
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1013 1014 1015 1016 1017
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1018 1019 1020 1021 1022
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1023
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1024 1025
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
1026
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1027 1028 1029
      return -1;
    }

S
Shengliang Guan 已提交
1030
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
1031
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1032 1033 1034
      return -1;
    }

S
Shengliang Guan 已提交
1035 1036 1037 1038
    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 已提交
1039 1040
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1041 1042

    mDebug("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1043 1044 1045 1046 1047 1048 1049 1050
  }

  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 已提交
1051
  if (col < 0) {
S
Shengliang Guan 已提交
1052 1053 1054 1055
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
  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 已提交
1066 1067 1068 1069 1070
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
S
Shengliang Guan 已提交
1071
  pNew->numOfColumns--;
S
Shengliang Guan 已提交
1072 1073 1074 1075 1076 1077

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

S
Shengliang Guan 已提交
1078 1079
static int32_t mndAlterStbColumnBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1080 1081 1082 1083 1084 1085 1086
  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 已提交
1087
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
  }

  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 已提交
1100 1101
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1102 1103 1104
    return -1;
  }

S
Shengliang Guan 已提交
1105
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1106
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1107 1108 1109
    return -1;
  }

S
Shengliang Guan 已提交
1110
  pCol->bytes = pField->bytes;
S
Shengliang Guan 已提交
1111 1112
  pNew->version++;

S
Shengliang Guan 已提交
1113
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1114 1115 1116
  return 0;
}

S
Shengliang Guan 已提交
1117
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1118 1119 1120
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
S
Shengliang Guan 已提交
1121
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
S
Shengliang Guan 已提交
1122 1123 1124 1125

  return 0;
}

S
Shengliang Guan 已提交
1126
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1127 1128 1129 1130 1131 1132 1133 1134
  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 已提交
1135
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1136 1137 1138 1139 1140 1141 1142 1143
  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 已提交
1144 1145 1146 1147
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1148

S
Shengliang Guan 已提交
1149
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
    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 已提交
1160
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1161
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1162
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1173
static int32_t mndAlterStb(SMnode *pMnode, SNodeMsg *pReq, const SMAltertbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
S
Shengliang Guan 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182
  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 已提交
1183
  STrans *pTrans = NULL;
S
Shengliang Guan 已提交
1184
  SField *pField0 = taosArrayGet(pAlter->pFields, 0);
S
Shengliang Guan 已提交
1185

S
Shengliang Guan 已提交
1186
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
1187
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
1188
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1189
      break;
S
Shengliang Guan 已提交
1190
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang Guan 已提交
1191
      code = mndDropSuperTableTag(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1192
      break;
S
Shengliang Guan 已提交
1193
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
S
Shengliang Guan 已提交
1194
      code = mndAlterStbTagName(pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
1195
      break;
S
Shengliang Guan 已提交
1196
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang Guan 已提交
1197
      code = mndAlterStbTagBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1198 1199
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
1200
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1201 1202
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang Guan 已提交
1203
      code = mndDropSuperTableColumn(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1204
      break;
S
Shengliang Guan 已提交
1205
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang Guan 已提交
1206
      code = mndAlterStbColumnBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1207 1208 1209 1210 1211 1212
      break;
    default:
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      break;
  }

1213
  if (code != 0) goto _OVER;
S
Shengliang Guan 已提交
1214 1215

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

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

1222 1223 1224 1225
  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 已提交
1226 1227

  code = 0;
S
Shengliang Guan 已提交
1228

1229
_OVER:
S
Shengliang Guan 已提交
1230
  mndTransDrop(pTrans);
wafwerar's avatar
wafwerar 已提交
1231 1232
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
S
Shengliang Guan 已提交
1233 1234
  return code;
}
S
Shengliang Guan 已提交
1235

S
Shengliang Guan 已提交
1236 1237
static int32_t mndProcessMAlterStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1238 1239 1240
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1241
  SUserObj    *pUser = NULL;
S
Shengliang Guan 已提交
1242
  SMAltertbReq alterReq = {0};
S
Shengliang Guan 已提交
1243

S
Shengliang Guan 已提交
1244
  if (tDeserializeSMAlterStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1245
    terrno = TSDB_CODE_INVALID_MSG;
1246
    goto _OVER;
S
Shengliang Guan 已提交
1247
  }
S
Shengliang Guan 已提交
1248

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

S
Shengliang Guan 已提交
1252
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1253 1254
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
1255
    goto _OVER;
S
Shengliang Guan 已提交
1256 1257
  }

S
Shengliang Guan 已提交
1258
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1259 1260
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1261
    goto _OVER;
S
Shengliang Guan 已提交
1262
  }
S
Shengliang Guan 已提交
1263

S
Shengliang Guan 已提交
1264 1265
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
1266
    goto _OVER;
S
Shengliang Guan 已提交
1267 1268 1269
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1270
    goto _OVER;
S
Shengliang Guan 已提交
1271 1272
  }

S
Shengliang Guan 已提交
1273
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
1274
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1275

1276
_OVER:
1277
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1278
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1279 1280
  }

S
Shengliang Guan 已提交
1281 1282
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1283
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
1284
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1285 1286

  return code;
S
Shengliang Guan 已提交
1287
}
S
Shengliang Guan 已提交
1288

S
Shengliang Guan 已提交
1289
static int32_t mndProcessVAlterStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1290
  mndTransProcessRsp(pRsp);
1291 1292
  return 0;
}
S
Shengliang Guan 已提交
1293

S
Shengliang Guan 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
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 已提交
1312 1313 1314 1315
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 已提交
1316

S
Shengliang Guan 已提交
1317 1318 1319
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1320 1321 1322 1323
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1324

S
Shengliang Guan 已提交
1325
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1326
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
    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 已提交
1341
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1352
static int32_t mndDropStb(SMnode *pMnode, SNodeMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1353
  int32_t code = -1;
S
Shengliang Guan 已提交
1354
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_STB, &pReq->rpcMsg);
1355
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1356

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

1360 1361 1362 1363
  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 已提交
1364

S
Shengliang Guan 已提交
1365 1366
  code = 0;

1367
_OVER:
S
Shengliang Guan 已提交
1368
  mndTransDrop(pTrans);
S
Shengliang 已提交
1369
  return code;
S
Shengliang Guan 已提交
1370 1371
}

S
Shengliang Guan 已提交
1372 1373
static int32_t mndProcessMDropStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1374 1375 1376 1377
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1378
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
1379

S
Shengliang Guan 已提交
1380
  if (tDeserializeSMDropStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1381
    terrno = TSDB_CODE_INVALID_MSG;
1382
    goto _OVER;
S
Shengliang Guan 已提交
1383
  }
S
Shengliang Guan 已提交
1384

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

S
Shengliang Guan 已提交
1387
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1388
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1389 1390
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1391
      code = 0;
1392
      goto _OVER;
S
Shengliang Guan 已提交
1393 1394
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1395
      goto _OVER;
S
Shengliang Guan 已提交
1396 1397 1398
    }
  }

S
Shengliang Guan 已提交
1399
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1400 1401
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1402
    goto _OVER;
S
Shengliang Guan 已提交
1403 1404
  }

S
Shengliang Guan 已提交
1405 1406
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
1407
    goto _OVER;
S
Shengliang Guan 已提交
1408
  }
S
Shengliang Guan 已提交
1409

S
Shengliang Guan 已提交
1410
  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1411
    goto _OVER;
S
Shengliang Guan 已提交
1412 1413 1414 1415 1416
  }

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

1417
_OVER:
S
Shengliang Guan 已提交
1418
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1419
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1420 1421
  }

S
Shengliang Guan 已提交
1422 1423 1424 1425 1426
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
1427
}
S
Shengliang Guan 已提交
1428

S
Shengliang Guan 已提交
1429
static int32_t mndProcessVDropStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1430
  mndTransProcessRsp(pRsp);
1431 1432
  return 0;
}
S
Shengliang Guan 已提交
1433

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

S
Shengliang Guan 已提交
1437
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
wafwerar's avatar
wafwerar 已提交
1438
  pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
1439
  if (pRsp->pSchemas == NULL) {
S
Shengliang Guan 已提交
1440
    taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1441 1442 1443 1444
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
  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->sversion = pStb->version;
  pRsp->suid = pStb->uid;
  pRsp->tuid = pStb->uid;
S
Shengliang Guan 已提交
1456

S
Shengliang Guan 已提交
1457
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1458
    SSchema *pSchema = &pRsp->pSchemas[i];
S
Shengliang Guan 已提交
1459 1460 1461
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1462 1463
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1464 1465 1466
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
S
Shengliang Guan 已提交
1467
    SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
S
Shengliang Guan 已提交
1468
    SSchema *pSrcSchema = &pStb->pTags[i];
S
Shengliang Guan 已提交
1469 1470
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1471 1472
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1473
  }
S
Shengliang Guan 已提交
1474

S
Shengliang Guan 已提交
1475
  taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1476
  return 0;
S
Shengliang Guan 已提交
1477
}
S
Shengliang Guan 已提交
1478

S
Shengliang Guan 已提交
1479 1480 1481
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 已提交
1482

S
Shengliang Guan 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
  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 已提交
1500
}
S
Shengliang Guan 已提交
1501

S
Shengliang Guan 已提交
1502 1503
static int32_t mndProcessTableMetaReq(SNodeMsg *pReq) {
  SMnode       *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1504 1505 1506
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
1507

S
Shengliang Guan 已提交
1508
  if (tDeserializeSTableInfoReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
1509
    terrno = TSDB_CODE_INVALID_MSG;
1510
    goto _OVER;
S
Shengliang Guan 已提交
1511
  }
D
dapan 已提交
1512

D
dapan1121 已提交
1513 1514 1515
  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) {
1516
      goto _OVER;
D
dapan1121 已提交
1517
    }
D
dapan1121 已提交
1518 1519 1520
  } 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) {
1521
      goto _OVER;
D
dapan1121 已提交
1522
    }
D
dapan1121 已提交
1523 1524 1525
  } else {
    mDebug("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
1526
      goto _OVER;
D
dapan1121 已提交
1527
    }
S
Shengliang Guan 已提交
1528
  }
S
Shengliang Guan 已提交
1529

S
Shengliang Guan 已提交
1530 1531 1532
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
1533
    goto _OVER;
S
Shengliang Guan 已提交
1534
  }
S
Shengliang Guan 已提交
1535

S
Shengliang Guan 已提交
1536
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1537 1538
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1539
    goto _OVER;
S
Shengliang Guan 已提交
1540
  }
D
dapan 已提交
1541

S
Shengliang Guan 已提交
1542
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1543 1544
  pReq->pRsp = pRsp;
  pReq->rspLen = rspLen;
S
Shengliang Guan 已提交
1545
  code = 0;
S
Shengliang Guan 已提交
1546

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

1549
_OVER:
S
Shengliang Guan 已提交
1550 1551 1552
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1553

S
Shengliang Guan 已提交
1554 1555 1556
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1557

S
Shengliang Guan 已提交
1558 1559 1560 1561 1562 1563 1564 1565
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 已提交
1566

S
Shengliang Guan 已提交
1567 1568 1569 1570 1571
  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 已提交
1572

S
Shengliang Guan 已提交
1573 1574 1575 1576 1577
    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 已提交
1578
    }
S
Shengliang Guan 已提交
1579

S
Shengliang Guan 已提交
1580 1581
    if (pStbVersion->sversion != metaRsp.sversion) {
      taosArrayPush(batchMetaRsp.pArray, &metaRsp);
S
Shengliang Guan 已提交
1582
    }
S
Shengliang Guan 已提交
1583
  }
S
Shengliang Guan 已提交
1584

S
Shengliang Guan 已提交
1585 1586 1587 1588 1589
  int32_t rspLen = tSerializeSTableMetaBatchRsp(NULL, 0, &batchMetaRsp);
  if (rspLen < 0) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
1590 1591
  }

wafwerar's avatar
wafwerar 已提交
1592
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
1593 1594 1595 1596
  if (pRsp == NULL) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
1597 1598
  }

S
Shengliang Guan 已提交
1599 1600 1601
  tSerializeSTableMetaBatchRsp(pRsp, rspLen, &batchMetaRsp);
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1602 1603 1604
  return 0;
}

1605
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1606
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1607 1608 1609 1610 1611 1612
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1613
  int32_t numOfStbs = 0;
1614
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1615
  while (1) {
S
Shengliang Guan 已提交
1616
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1617
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1618 1619
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1620
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1621
      numOfStbs++;
S
Shengliang Guan 已提交
1622 1623
    }

S
Shengliang Guan 已提交
1624
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1625 1626
  }

S
Shengliang Guan 已提交
1627
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1628
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1629 1630 1631
  return 0;
}

S
Shengliang Guan 已提交
1632
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
1633 1634
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
1635
  for (pos = 0; tableId[pos] != 0; ++pos) {
H
Haojun Liao 已提交
1636
    if (tableId[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
1637 1638 1639 1640 1641 1642 1643 1644
    if (num == 2) break;
  }

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

H
Hongze Cheng 已提交
1645
static int32_t mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
S
Shengliang Guan 已提交
1646
  SMnode  *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1647
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1648 1649 1650
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1651

H
Hongze Cheng 已提交
1652
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
1653 1654
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
1655
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
1656
  }
S
Shengliang Guan 已提交
1657

S
Shengliang Guan 已提交
1658
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
1659
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1660 1661
    if (pShow->pIter == NULL) break;

H
Haojun Liao 已提交
1662
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
1663
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1664 1665 1666 1667 1668
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
1669
    SName name = {0};
H
Hongze Cheng 已提交
1670
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1671 1672
    mndExtractTableName(pStb->name, &stbName[VARSTR_HEADER_SIZE]);
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
1673

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

H
Hongze Cheng 已提交
1677 1678
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
1679 1680 1681
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

1682
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
H
Hongze Cheng 已提交
1683
    colDataAppend(pColInfo, numOfRows, (const char *)db, false);
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696

    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 已提交
1697
    char *p = taosMemoryMalloc(pStb->commentLen + VARSTR_HEADER_SIZE);  // check malloc failures
1698 1699 1700 1701 1702 1703 1704
    if (p != NULL) {
      if (pStb->commentLen != 0) {
        STR_TO_VARSTR(p, pStb->comment);
      } else {
        STR_TO_VARSTR(p, "");
      }

1705
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1706 1707
      colDataAppend(pColInfo, numOfRows, (const char *)p, false);
      taosMemoryFree(p);
S
Shengliang Guan 已提交
1708
    }
H
Haojun Liao 已提交
1709

S
Shengliang Guan 已提交
1710
    numOfRows++;
S
Shengliang Guan 已提交
1711
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1712 1713
  }

H
Haojun Liao 已提交
1714 1715 1716 1717
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

1718
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1719 1720 1721
  return numOfRows;
}

S
Shengliang Guan 已提交
1722
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1723 1724
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
1725
}