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

S
Shengliang Guan 已提交
16
#include "mndStb.h"
S
Shengliang Guan 已提交
17
#include "mndAuth.h"
S
Shengliang Guan 已提交
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndDnode.h"
S
Shengliang Guan 已提交
20
#include "mndInfoSchema.h"
S
Shengliang Guan 已提交
21 22 23 24
#include "mndMnode.h"
#include "mndShow.h"
#include "mndTrans.h"
#include "mndUser.h"
25
#include "mndVgroup.h"
S
Shengliang Guan 已提交
26
#include "tname.h"
S
Shengliang Guan 已提交
27

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

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

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

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

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

S
Shengliang Guan 已提交
68
void mndCleanupStb(SMnode *pMnode) {}
S
Shengliang Guan 已提交
69

70
SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
71 72
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

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

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

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

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

  if (pStb->commentLen > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, STB_ENCODE_OVER)
  }
123 124 125 126 127 128 129 130 131 132 133
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, STB_ENCODE_OVER)
  SDB_SET_DATALEN(pRaw, dataPos, STB_ENCODE_OVER)

  terrno = 0;

STB_ENCODE_OVER:
  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 已提交
134

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

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

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

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

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

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

  int32_t dataPos = 0;
157 158 159 160 161 162 163
  SDB_GET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, STB_DECODE_OVER)
  SDB_GET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, STB_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->createdTime, STB_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->updateTime, STB_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->uid, STB_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->dbUid, STB_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->version, STB_DECODE_OVER)
S
Shengliang Guan 已提交
164
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, STB_DECODE_OVER)
S
sma  
Shengliang Guan 已提交
165 166 167 168 169
  int32_t xFilesFactor = 0;
  SDB_GET_INT32(pRaw, dataPos, &xFilesFactor, STB_DECODE_OVER)
  pStb->xFilesFactor = xFilesFactor / 10000.0f;
  SDB_GET_INT32(pRaw, dataPos, &pStb->aggregationMethod, STB_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->delay, STB_DECODE_OVER)
S
sma  
Shengliang Guan 已提交
170
  SDB_GET_INT32(pRaw, dataPos, &pStb->ttl, STB_DECODE_OVER)
171 172
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, STB_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, STB_DECODE_OVER)
S
sma  
Shengliang Guan 已提交
173
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfSmas, STB_DECODE_OVER)
S
sma  
Shengliang Guan 已提交
174
  SDB_GET_INT32(pRaw, dataPos, &pStb->commentLen, STB_DECODE_OVER)
S
Shengliang Guan 已提交
175

wafwerar's avatar
wafwerar 已提交
176 177 178
  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 已提交
179
  if (pStb->pColumns == NULL || pStb->pTags == NULL || pStb->pSmas == NULL) {
S
Shengliang 已提交
180 181
    goto STB_DECODE_OVER;
  }
S
Shengliang Guan 已提交
182

S
Shengliang Guan 已提交
183 184 185
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, STB_DECODE_OVER)
186
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, STB_DECODE_OVER)
S
Shengliang Guan 已提交
187 188 189 190 191 192
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, STB_DECODE_OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, STB_DECODE_OVER)
  }

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

S
sma  
Shengliang Guan 已提交
199 200 201
  for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
    SSchema *pSchema = &pStb->pSmas[i];
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, STB_DECODE_OVER)
202
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, STB_DECODE_OVER)
S
sma  
Shengliang Guan 已提交
203 204 205 206
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, STB_DECODE_OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, STB_DECODE_OVER)
  }

S
sma  
Shengliang Guan 已提交
207
  if (pStb->commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
208
    pStb->comment = taosMemoryCalloc(pStb->commentLen, 1);
S
sma  
Shengliang Guan 已提交
209 210 211
    if (pStb->comment == NULL) goto STB_DECODE_OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, STB_DECODE_OVER)
  }
212 213 214 215 216 217 218
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, STB_DECODE_OVER)

  terrno = 0;

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

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

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

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

S
Shengliang Guan 已提交
243 244
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 已提交
245

S
Shengliang Guan 已提交
246
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
247 248

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

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

S
Shengliang Guan 已提交
296 297
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
S
Shengliang Guan 已提交
298
  pOld->nextColId = pNew->nextColId;
S
Shengliang Guan 已提交
299 300
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
301 302
  memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
303 304 305
  if (pNew->commentLen != 0) {
    memcpy(pOld->comment, pNew->comment, TSDB_STB_COMMENT_LEN);
  }
S
Shengliang Guan 已提交
306
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
307 308 309
  return 0;
}

S
Shengliang Guan 已提交
310
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
311
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
312
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
313
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
314 315 316
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
317 318
}

S
Shengliang Guan 已提交
319
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
320 321 322 323
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

S
Shengliang Guan 已提交
324
static SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
325 326
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
327

S
Shengliang Guan 已提交
328 329
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
330

S
Shengliang Guan 已提交
331 332
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
333

C
Cary Xu 已提交
334 335 336 337 338 339 340 341 342
static FORCE_INLINE int schemaExColIdCompare(const void *colId, const void *pSchema) {
  if (*(col_id_t *)colId < ((SSchemaEx *)pSchema)->colId) {
    return -1;
  } else if (*(col_id_t *)colId > ((SSchemaEx *)pSchema)->colId) {
    return 1;
  }
  return 0;
}

S
Shengliang Guan 已提交
343
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
344
  SName name = {0};
S
Shengliang Guan 已提交
345
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
X
Xiaoyu Wang 已提交
346 347
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, dbFName);
348

S
Shengliang Guan 已提交
349 350
  SVCreateTbReq req = {0};
  req.ver = 0;
X
Xiaoyu Wang 已提交
351
  req.dbFName = dbFName;
S
Shengliang Guan 已提交
352
  req.name = (char *)tNameGetTableName(&name);
H
more  
Hongze Cheng 已提交
353 354
  req.ttl = 0;
  req.keep = 0;
C
Cary Xu 已提交
355
  req.rollup = pStb->aggregationMethod > -1 ? 1 : 0;
H
more  
Hongze Cheng 已提交
356 357 358 359
  req.type = TD_SUPER_TABLE;
  req.stbCfg.suid = pStb->uid;
  req.stbCfg.nCols = pStb->numOfColumns;
  req.stbCfg.nTagCols = pStb->numOfTags;
S
Shengliang Guan 已提交
360
  req.stbCfg.pTagSchema = pStb->pTags;
C
Cary Xu 已提交
361 362 363 364 365 366
  req.stbCfg.nBSmaCols = pStb->numOfSmas;
  req.stbCfg.pSchema = (SSchemaEx *)taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchemaEx));
  if (req.stbCfg.pSchema == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
C
Cary Xu 已提交
367

C
Cary Xu 已提交
368 369 370 371 372 373 374
  int bSmaStat = 0;                             // no column has bsma
  if (pStb->numOfSmas == pStb->numOfColumns) {  // assume pColumns > 0
    bSmaStat = 1;                               // all columns have bsma
  } else if (pStb->numOfSmas != 0) {
    bSmaStat = 2;                  // partial columns have bsma
    TASSERT(pStb->pSmas != NULL);  // TODO: remove the assert
  }
C
Cary Xu 已提交
375

C
Cary Xu 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
  for (int32_t i = 0; i < req.stbCfg.nCols; ++i) {
    SSchemaEx *pSchemaEx = req.stbCfg.pSchema + i;
    SSchema   *pSchema = pStb->pColumns + i;
    pSchemaEx->type = pSchema->type;
    pSchemaEx->sma = (bSmaStat == 1) ? TSDB_BSMA_TYPE_LATEST : TSDB_BSMA_TYPE_NONE;
    pSchemaEx->colId = pSchema->colId;
    pSchemaEx->bytes = pSchema->bytes;
    memcpy(pSchemaEx->name, pSchema->name, TSDB_COL_NAME_LEN);
  }

  if (bSmaStat == 2) {
    if (pStb->pSmas == NULL) {
      mError("stb:%s, sma options is empty", pStb->name);
      taosMemoryFreeClear(req.stbCfg.pSchema);
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return NULL;
    }
    for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
      SSchema   *pSmaSchema = pStb->pSmas + i;
      SSchemaEx *pColSchema = taosbsearch(&pSmaSchema->colId, req.stbCfg.pSchema, req.stbCfg.nCols, sizeof(SSchemaEx),
                                          schemaExColIdCompare, TD_EQ);
      if (pColSchema == NULL) {
        terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
        taosMemoryFreeClear(req.stbCfg.pSchema);
        mError("stb:%s, sma col:%s not found in columns", pStb->name, pSmaSchema->name);
        return NULL;
      }
      pColSchema->sma = TSDB_BSMA_TYPE_LATEST;
    }
  }
H
more  
Hongze Cheng 已提交
406

C
Cary Xu 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  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;
    }
    req.stbCfg.pRSmaParam = pRSmaParam;
  }  

C
Cary Xu 已提交
432
  int32_t contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
wafwerar's avatar
wafwerar 已提交
433
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
434
  if (pHead == NULL) {
C
Cary Xu 已提交
435 436 437 438
    if (pRSmaParam) {
      taosMemoryFreeClear(pRSmaParam->pFuncIds);
      taosMemoryFreeClear(pRSmaParam);
    }
C
Cary Xu 已提交
439
    taosMemoryFreeClear(req.stbCfg.pSchema);
H
more  
Hongze Cheng 已提交
440 441 442 443
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
444 445
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
446

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

S
Shengliang Guan 已提交
450
  *pContLen = contLen;
C
Cary Xu 已提交
451 452 453 454
  if (pRSmaParam) {
    taosMemoryFreeClear(pRSmaParam->pFuncIds);
    taosMemoryFreeClear(pRSmaParam);
  }
C
Cary Xu 已提交
455
  taosMemoryFreeClear(req.stbCfg.pSchema);
S
Shengliang Guan 已提交
456
  return pHead;
457 458
}

S
Shengliang Guan 已提交
459
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
S
Shengliang Guan 已提交
460 461 462 463 464 465 466 467
  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;
468

S
Shengliang Guan 已提交
469
  int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
wafwerar's avatar
wafwerar 已提交
470
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
471
  if (pHead == NULL) {
472 473 474 475
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
476 477 478 479 480
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

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

S
Shengliang Guan 已提交
482 483
  *pContLen = contLen;
  return pHead;
484 485
}

S
Shengliang Guan 已提交
486
static int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
487
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
488
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
489 490
    return -1;
  }
S
Shengliang Guan 已提交
491

S
Shengliang Guan 已提交
492
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
493
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
494 495
    return -1;
  }
S
Shengliang Guan 已提交
496

S
Shengliang Guan 已提交
497
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
498
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
499 500
    return -1;
  }
S
Shengliang Guan 已提交
501

S
Shengliang Guan 已提交
502
  SField *pField = taosArrayGet(pCreate->pColumns, 0);
S
Shengliang Guan 已提交
503
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
S
Shengliang Guan 已提交
504 505 506 507
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

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

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
525 526
    SField *pField1 = taosArrayGet(pCreate->pTags, i);
    if (pField1->type < 0) {
S
Shengliang Guan 已提交
527
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
528 529
      return -1;
    }
530
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
531
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
532 533
      return -1;
    }
534
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
535
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
536 537 538
      return -1;
    }
  }
S
Shengliang Guan 已提交
539

S
Shengliang Guan 已提交
540 541 542
  return 0;
}

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

552
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
553 554 555 556 557 558 559 560
  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;
}

561
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
562 563 564 565 566 567 568 569
  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;
}

570
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
571
  SSdb   *pSdb = pMnode->pSdb;
572
  SVgObj *pVgroup = NULL;
573
  void   *pIter = NULL;
S
Shengliang Guan 已提交
574
  int32_t contLen;
575 576 577 578

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
579 580 581 582
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
583

S
Shengliang Guan 已提交
584
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
585
    if (pReq == NULL) {
586 587 588 589
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
S
Shengliang Guan 已提交
590

591 592
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
593
    action.pCont = pReq;
S
Shengliang Guan 已提交
594
    action.contLen = contLen;
H
Hongze Cheng 已提交
595
    action.msgType = TDMT_VND_CREATE_STB;
596
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
597
      taosMemoryFree(pReq);
598 599 600 601 602 603
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
604 605 606 607

  return 0;
}

608
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
609
  SSdb   *pSdb = pMnode->pSdb;
610
  SVgObj *pVgroup = NULL;
611
  void   *pIter = NULL;
612 613 614 615

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

S
Shengliang Guan 已提交
621
    int32_t contLen = 0;
S
Shengliang Guan 已提交
622
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
623
    if (pReq == NULL) {
624 625 626 627 628
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
629

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

  return 0;
}

S
sma  
Shengliang Guan 已提交
647
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
C
Cary Xu 已提交
648
  for (int32_t col = 0; col < pStb->numOfColumns; ++col) {
S
sma  
Shengliang Guan 已提交
649
    SSchema *pSchema = &pStb->pColumns[col];
C
Cary Xu 已提交
650
    if (strncasecmp(pSchema->name, colName, TSDB_COL_NAME_LEN) == 0) {
S
sma  
Shengliang Guan 已提交
651 652 653 654 655 656
      return pSchema;
    }
  }
  return NULL;
}

S
Shengliang Guan 已提交
657
static int32_t mndCreateStb(SMnode *pMnode, SNodeMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
S
Shengliang Guan 已提交
658
  SStbObj stbObj = {0};
S
Shengliang Guan 已提交
659 660
  memcpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
661 662
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
663
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
664
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
665
  stbObj.version = 1;
S
Shengliang Guan 已提交
666
  stbObj.nextColId = 1;
C
Cary Xu 已提交
667 668 669
  stbObj.xFilesFactor = pCreate->xFilesFactor;
  stbObj.aggregationMethod = pCreate->aggregationMethod;
  stbObj.delay = pCreate->delay;
S
sma  
Shengliang Guan 已提交
670
  stbObj.ttl = pCreate->ttl;
S
Shengliang Guan 已提交
671 672
  stbObj.numOfColumns = pCreate->numOfColumns;
  stbObj.numOfTags = pCreate->numOfTags;
S
sma  
Shengliang Guan 已提交
673
  stbObj.numOfSmas = pCreate->numOfSmas;
S
sma  
Shengliang Guan 已提交
674
  stbObj.commentLen = pCreate->commentLen;
S
sma  
Shengliang Guan 已提交
675
  if (stbObj.commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
676
    stbObj.comment = taosMemoryCalloc(stbObj.commentLen, 1);
S
sma  
Shengliang Guan 已提交
677 678 679 680 681
    if (stbObj.comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    memcpy(stbObj.comment, pCreate->comment, stbObj.commentLen);
S
sma  
Shengliang Guan 已提交
682
  }
S
Shengliang Guan 已提交
683

wafwerar's avatar
wafwerar 已提交
684 685 686
  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 已提交
687
  if (stbObj.pColumns == NULL || stbObj.pTags == NULL || stbObj.pSmas == NULL) {
S
Shengliang Guan 已提交
688 689 690
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
691

S
Shengliang Guan 已提交
692
  for (int32_t i = 0; i < stbObj.numOfColumns; ++i) {
S
Shengliang Guan 已提交
693 694 695 696 697 698
    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 已提交
699 700 701 702
    stbObj.nextColId++;
  }

  for (int32_t i = 0; i < stbObj.numOfTags; ++i) {
S
Shengliang Guan 已提交
703 704 705 706 707 708
    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 已提交
709
    stbObj.nextColId++;
S
Shengliang Guan 已提交
710 711
  }

S
sma  
Shengliang Guan 已提交
712 713 714 715 716
  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 已提交
717
      mError("stb:%s, sma:%s not found in columns", stbObj.name, pField->name);
S
sma  
Shengliang Guan 已提交
718 719 720 721 722 723
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    memcpy(pSchema, pColSchema, sizeof(SSchema));
  }

S
Shengliang 已提交
724
  int32_t code = -1;
S
Shengliang Guan 已提交
725
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_STB, &pReq->rpcMsg);
S
Shengliang Guan 已提交
726
  if (pTrans == NULL) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
727

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

S
Shengliang Guan 已提交
731 732 733 734 735 736
  if (mndSetCreateStbRedoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndSetCreateStbUndoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndSetCreateStbCommitLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndSetCreateStbRedoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndSetCreateStbUndoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
737

S
Shengliang Guan 已提交
738 739 740
  code = 0;

CREATE_STB_OVER:
S
Shengliang Guan 已提交
741
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
742
  return code;
S
Shengliang Guan 已提交
743 744
}

S
Shengliang Guan 已提交
745 746
static int32_t mndProcessMCreateStbReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
747 748 749 750
  int32_t        code = -1;
  SStbObj       *pTopicStb = NULL;
  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 756 757
  if (tDeserializeSMCreateStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_STB_OVER;
  }
S
Shengliang Guan 已提交
758

S
Shengliang Guan 已提交
759
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
760 761 762 763
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_STB_OVER;
  }
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 770
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
      goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
771 772
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
S
Shengliang Guan 已提交
773
      goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
774
    }
S
Shengliang Guan 已提交
775
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
S
Shengliang Guan 已提交
776
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
777 778
  }

S
Shengliang Guan 已提交
779
  pTopicStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
780
  if (pTopicStb != NULL) {
L
Liu Jicong 已提交
781
    terrno = TSDB_CODE_MND_NAME_CONFLICT_WITH_TOPIC;
S
Shengliang Guan 已提交
782
    goto CREATE_STB_OVER;
L
Liu Jicong 已提交
783 784
  }

S
Shengliang Guan 已提交
785
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
786 787
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
788
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
789 790
  }

S
Shengliang Guan 已提交
791 792 793 794 795 796 797 798 799
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    goto CREATE_STB_OVER;
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
    goto CREATE_STB_OVER;
  }

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

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

S
Shengliang Guan 已提交
808 809 810
  mndReleaseStb(pMnode, pStb);
  mndReleaseStb(pMnode, pTopicStb);
  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_UPDATING) != 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;
  }

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

  code = -1;
S
Shengliang Guan 已提交
1216
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_ALTER_STB, &pReq->rpcMsg);
S
Shengliang Guan 已提交
1217
  if (pTrans == NULL) goto ALTER_STB_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

S
Shengliang Guan 已提交
1222 1223 1224 1225
  if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto ALTER_STB_OVER;
  if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto ALTER_STB_OVER;
  if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto ALTER_STB_OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1226 1227

  code = 0;
S
Shengliang Guan 已提交
1228

S
Shengliang Guan 已提交
1229
ALTER_STB_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 1246 1247
    terrno = TSDB_CODE_INVALID_MSG;
    goto ALTER_STB_OVER;
  }
S
Shengliang Guan 已提交
1248

S
Shengliang Guan 已提交
1249 1250
  mDebug("stb:%s, start to alter", alterReq.name);
  if (mndCheckAlterStbReq(&alterReq) != 0) goto ALTER_STB_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;
S
Shengliang Guan 已提交
1255
    goto ALTER_STB_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;
S
Shengliang Guan 已提交
1261
    goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1262
  }
S
Shengliang Guan 已提交
1263

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

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
    goto ALTER_STB_OVER;
  }

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

S
Shengliang Guan 已提交
1276
ALTER_STB_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);
S
Shengliang Guan 已提交
1355
  if (pTrans == NULL) goto DROP_STB_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

S
Shengliang Guan 已提交
1360 1361
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1362
  if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1363
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1364

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

DROP_STB_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 1382 1383
    terrno = TSDB_CODE_INVALID_MSG;
    goto DROP_STB_OVER;
  }
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 1392
      code = 0;
      goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1393 1394
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
S
Shengliang Guan 已提交
1395
      goto DROP_STB_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;
S
Shengliang Guan 已提交
1402
    goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1403 1404
  }

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

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

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

DROP_STB_OVER:
  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 1456
  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 已提交
1457

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
1526 1527 1528 1529 1530
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto RETRIEVE_META_OVER;
  }
S
Shengliang Guan 已提交
1531

S
Shengliang Guan 已提交
1532
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1533 1534 1535 1536
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto RETRIEVE_META_OVER;
  }
D
dapan 已提交
1537

S
Shengliang Guan 已提交
1538
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1539 1540
  pReq->pRsp = pRsp;
  pReq->rspLen = rspLen;
S
Shengliang Guan 已提交
1541
  code = 0;
S
Shengliang Guan 已提交
1542

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

S
Shengliang Guan 已提交
1545 1546 1547 1548
RETRIEVE_META_OVER:
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1549

S
Shengliang Guan 已提交
1550 1551 1552
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1553

S
Shengliang Guan 已提交
1554 1555 1556 1557 1558 1559 1560 1561
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 已提交
1562

S
Shengliang Guan 已提交
1563 1564 1565 1566 1567
  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 已提交
1568

S
Shengliang Guan 已提交
1569 1570 1571 1572 1573
    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 已提交
1574
    }
S
Shengliang Guan 已提交
1575

S
Shengliang Guan 已提交
1576 1577
    if (pStbVersion->sversion != metaRsp.sversion) {
      taosArrayPush(batchMetaRsp.pArray, &metaRsp);
S
Shengliang Guan 已提交
1578
    }
S
Shengliang Guan 已提交
1579
  }
S
Shengliang Guan 已提交
1580

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

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

S
Shengliang Guan 已提交
1595 1596 1597
  tSerializeSTableMetaBatchRsp(pRsp, rspLen, &batchMetaRsp);
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1598 1599 1600
  return 0;
}

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

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

S
Shengliang Guan 已提交
1616
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1617
      numOfStbs++;
S
Shengliang Guan 已提交
1618 1619
    }

S
Shengliang Guan 已提交
1620
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1621 1622
  }

S
Shengliang Guan 已提交
1623
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1624
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1625 1626 1627
  return 0;
}

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

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

1641
static int32_t mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* pBlock, int32_t rows) {
S
Shengliang Guan 已提交
1642
  SMnode  *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1643
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1644 1645 1646
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1647

H
Haojun Liao 已提交
1648 1649 1650
  SDbObj* pDb = NULL;
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
1651
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
1652
  }
S
Shengliang Guan 已提交
1653

S
Shengliang Guan 已提交
1654
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
1655
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1656 1657
    if (pShow->pIter == NULL) break;

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

    cols = 0;

H
Haojun Liao 已提交
1665
    SName name = {0};
1666 1667 1668
    char stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    mndExtractTableName(pStb->name, &stbName[VARSTR_HEADER_SIZE]);
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
1669

1670
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1671 1672 1673
    colDataAppend(pColInfo, numOfRows, (const char*) stbName, false);

    char  db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
H
Haojun Liao 已提交
1674
    tNameFromString(&name, pStb->db, T_NAME_ACCT|T_NAME_DB);
1675 1676 1677
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

1678
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
    colDataAppend(pColInfo, numOfRows, (const char*) db, false);

    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

    char* p = taosMemoryMalloc(pStb->commentLen + VARSTR_HEADER_SIZE); // check malloc failures
    if (p != NULL) {
      if (pStb->commentLen != 0) {
        STR_TO_VARSTR(p, pStb->comment);
      } else {
        STR_TO_VARSTR(p, "");
      }

1701
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1702 1703
      colDataAppend(pColInfo, numOfRows, (const char *)p, false);
      taosMemoryFree(p);
S
Shengliang Guan 已提交
1704
    }
H
Haojun Liao 已提交
1705

S
Shengliang Guan 已提交
1706
    numOfRows++;
S
Shengliang Guan 已提交
1707
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1708 1709
  }

H
Haojun Liao 已提交
1710 1711 1712 1713
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

1714
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1715 1716 1717
  return numOfRows;
}

S
Shengliang Guan 已提交
1718
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1719 1720
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
1721
}