mndStb.c 51.3 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 42 43
static int32_t  mndProcessMCreateStbReq(SNodeMsg *pReq);
static int32_t  mndProcessMAlterStbReq(SNodeMsg *pReq);
static int32_t  mndProcessMDropStbReq(SNodeMsg *pReq);
static int32_t  mndProcessVCreateStbRsp(SNodeMsg *pRsp);
static int32_t  mndProcessVAlterStbRsp(SNodeMsg *pRsp);
static int32_t  mndProcessVDropStbRsp(SNodeMsg *pRsp);
static int32_t  mndProcessTableMetaReq(SNodeMsg *pReq);
static int32_t  mndGetStbMeta(SNodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
static int32_t  mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows);
S
Shengliang Guan 已提交
44 45 46 47
static void     mndCancelGetNextStb(SMnode *pMnode, void *pIter);

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

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

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

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

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

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

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

  int32_t dataPos = 0;
81 82 83 84 85 86 87
  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 已提交
88
  SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, STB_ENCODE_OVER)
S
sma  
Shengliang Guan 已提交
89 90 91
  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 已提交
92
  SDB_SET_INT32(pRaw, dataPos, pStb->ttl, STB_ENCODE_OVER)
93 94
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, STB_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, STB_ENCODE_OVER)
S
sma  
Shengliang Guan 已提交
95
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfSmas, STB_ENCODE_OVER)
S
sma  
Shengliang Guan 已提交
96
  SDB_SET_INT32(pRaw, dataPos, pStb->commentLen, STB_ENCODE_OVER)
S
Shengliang Guan 已提交
97

S
Shengliang Guan 已提交
98 99 100 101 102 103 104 105 106 107
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, STB_ENCODE_OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->colId, STB_ENCODE_OVER)
    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];
108 109 110 111
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, STB_ENCODE_OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->colId, STB_ENCODE_OVER)
    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 已提交
112 113
  }

S
sma  
Shengliang Guan 已提交
114
  for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
S
sma  
Shengliang Guan 已提交
115 116 117 118 119 120 121 122 123 124
    SSchema *pSchema = &pStb->pSmas[i];
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, STB_ENCODE_OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->colId, STB_ENCODE_OVER)
    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)
  }
125 126 127 128 129 130 131 132 133 134 135
  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 已提交
136

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

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

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

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

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

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

  int32_t dataPos = 0;
159 160 161 162 163 164 165
  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 已提交
166
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, STB_DECODE_OVER)
S
sma  
Shengliang Guan 已提交
167 168 169 170 171
  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 已提交
172
  SDB_GET_INT32(pRaw, dataPos, &pStb->ttl, STB_DECODE_OVER)
173 174
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, STB_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, STB_DECODE_OVER)
S
sma  
Shengliang Guan 已提交
175
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfSmas, STB_DECODE_OVER)
S
sma  
Shengliang Guan 已提交
176
  SDB_GET_INT32(pRaw, dataPos, &pStb->commentLen, STB_DECODE_OVER)
S
Shengliang Guan 已提交
177

S
Shengliang Guan 已提交
178 179
  pStb->pColumns = calloc(pStb->numOfColumns, sizeof(SSchema));
  pStb->pTags = calloc(pStb->numOfTags, sizeof(SSchema));
S
sma  
Shengliang Guan 已提交
180 181
  pStb->pSmas = calloc(pStb->numOfSmas, sizeof(SSchema));
  if (pStb->pColumns == NULL || pStb->pTags == NULL || pStb->pSmas == NULL) {
S
Shengliang 已提交
182 183
    goto STB_DECODE_OVER;
  }
S
Shengliang Guan 已提交
184

S
Shengliang Guan 已提交
185 186 187 188 189 190 191 192 193 194
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, STB_DECODE_OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->colId, STB_DECODE_OVER)
    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];
195 196 197 198
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, STB_DECODE_OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->colId, STB_DECODE_OVER)
    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 已提交
199 200
  }

S
sma  
Shengliang Guan 已提交
201 202 203 204 205 206 207 208
  for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
    SSchema *pSchema = &pStb->pSmas[i];
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, STB_DECODE_OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->colId, STB_DECODE_OVER)
    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 已提交
209 210 211 212 213
  if (pStb->commentLen > 0) {
    pStb->comment = calloc(pStb->commentLen, 1);
    if (pStb->comment == NULL) goto STB_DECODE_OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, STB_DECODE_OVER)
  }
214 215 216 217 218 219 220
  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());
221 222
    tfree(pStb->pColumns);
    tfree(pStb->pTags);
S
sma  
Shengliang Guan 已提交
223
    tfree(pStb->comment);
224 225 226
    tfree(pRow);
    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);
S
Shengliang Guan 已提交
239 240
  tfree(pStb->pColumns);
  tfree(pStb->pTags);
S
sma  
Shengliang Guan 已提交
241
  tfree(pStb->comment);
S
Shengliang Guan 已提交
242 243 244
  return 0;
}

S
Shengliang Guan 已提交
245 246
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 已提交
247

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

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

S
Shengliang Guan 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  if (pOld->numOfSmas < pNew->numOfSmas) {
    void *pSmas = malloc(pNew->numOfSmas * sizeof(SSchema));
    if (pSmas != NULL) {
      free(pOld->pSmas);
      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) {
    void *comment = malloc(pNew->commentLen);
    if (comment != NULL) {
      free(pOld->comment);
      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 已提交
298 299
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
S
Shengliang Guan 已提交
300
  pOld->nextColId = pNew->nextColId;
S
Shengliang Guan 已提交
301 302
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
303 304
  memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
305 306 307
  if (pNew->commentLen != 0) {
    memcpy(pOld->comment, pNew->comment, TSDB_STB_COMMENT_LEN);
  }
S
Shengliang Guan 已提交
308
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
309 310 311
  return 0;
}

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

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

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

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

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

S
Shengliang Guan 已提交
336
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
337
  SName name = {0};
S
Shengliang Guan 已提交
338
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
X
Xiaoyu Wang 已提交
339 340
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, dbFName);
341

S
Shengliang Guan 已提交
342 343
  SVCreateTbReq req = {0};
  req.ver = 0;
X
Xiaoyu Wang 已提交
344
  req.dbFName = dbFName;
S
Shengliang Guan 已提交
345
  req.name = (char *)tNameGetTableName(&name);
H
more  
Hongze Cheng 已提交
346 347 348 349 350
  req.ttl = 0;
  req.keep = 0;
  req.type = TD_SUPER_TABLE;
  req.stbCfg.suid = pStb->uid;
  req.stbCfg.nCols = pStb->numOfColumns;
S
Shengliang Guan 已提交
351
  req.stbCfg.pSchema = pStb->pColumns;
H
more  
Hongze Cheng 已提交
352
  req.stbCfg.nTagCols = pStb->numOfTags;
S
Shengliang Guan 已提交
353
  req.stbCfg.pTagSchema = pStb->pTags;
H
more  
Hongze Cheng 已提交
354

S
Shengliang Guan 已提交
355 356 357
  int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
  SMsgHead *pHead = malloc(contLen);
  if (pHead == NULL) {
H
more  
Hongze Cheng 已提交
358 359 360 361
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
362 363
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
364

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

S
Shengliang Guan 已提交
368 369
  *pContLen = contLen;
  return pHead;
370 371
}

S
Shengliang Guan 已提交
372
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
S
Shengliang Guan 已提交
373 374 375 376 377 378 379 380
  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;
381

S
Shengliang Guan 已提交
382 383 384
  int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
  SMsgHead *pHead = malloc(contLen);
  if (pHead == NULL) {
385 386 387 388
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
389 390 391 392 393
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

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

S
Shengliang Guan 已提交
395 396
  *pContLen = contLen;
  return pHead;
397 398
}

S
Shengliang Guan 已提交
399
static int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
400
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
401
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
402 403
    return -1;
  }
S
Shengliang Guan 已提交
404

S
Shengliang Guan 已提交
405
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
406
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
407 408
    return -1;
  }
S
Shengliang Guan 已提交
409

S
Shengliang Guan 已提交
410
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
411
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
412 413
    return -1;
  }
S
Shengliang Guan 已提交
414

S
Shengliang Guan 已提交
415
  SField *pField = taosArrayGet(pCreate->pColumns, 0);
S
Shengliang Guan 已提交
416
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
S
Shengliang Guan 已提交
417 418 419 420
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  for (int32_t i = 0; i < pCreate->numOfColumns; ++i) {
    SField *pField = taosArrayGet(pCreate->pColumns, i);
    if (pField->type < 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    if (pField->bytes <= 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    if (pField->name[0] == 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
  }

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
    SField *pField = taosArrayGet(pCreate->pTags, i);
    if (pField->type < 0) {
S
Shengliang Guan 已提交
440
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
441 442
      return -1;
    }
S
Shengliang Guan 已提交
443
    if (pField->bytes <= 0) {
S
Shengliang Guan 已提交
444
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
445 446
      return -1;
    }
S
Shengliang Guan 已提交
447
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
448
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
449 450 451
      return -1;
    }
  }
S
Shengliang Guan 已提交
452

S
Shengliang Guan 已提交
453 454 455
  return 0;
}

456
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
457 458 459 460 461 462 463 464
  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;
}

465
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
466 467 468 469 470 471 472 473
  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;
}

474
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
475 476 477 478 479 480 481 482
  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;
}

483
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
484
  SSdb   *pSdb = pMnode->pSdb;
485
  SVgObj *pVgroup = NULL;
486
  void   *pIter = NULL;
S
Shengliang Guan 已提交
487
  int32_t contLen;
488 489 490 491

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
492 493 494 495
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
496

S
Shengliang Guan 已提交
497
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
498
    if (pReq == NULL) {
499 500 501 502 503
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
504

505 506
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
507
    action.pCont = pReq;
S
Shengliang Guan 已提交
508
    action.contLen = contLen;
H
Hongze Cheng 已提交
509
    action.msgType = TDMT_VND_CREATE_STB;
510
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
511
      free(pReq);
512 513 514 515 516 517
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
518 519 520 521

  return 0;
}

522
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
523
  SSdb   *pSdb = pMnode->pSdb;
524
  SVgObj *pVgroup = NULL;
525
  void   *pIter = NULL;
526 527 528 529

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
530 531 532 533
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
534

S
Shengliang Guan 已提交
535
    int32_t contLen = 0;
S
Shengliang Guan 已提交
536
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
537
    if (pReq == NULL) {
538 539 540 541 542
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
543

544 545
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
546
    action.pCont = pReq;
S
Shengliang Guan 已提交
547
    action.contLen = contLen;
H
Hongze Cheng 已提交
548
    action.msgType = TDMT_VND_DROP_STB;
549
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
550
      free(pReq);
551 552 553 554 555 556
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
557 558 559 560

  return 0;
}

S
sma  
Shengliang Guan 已提交
561 562 563 564 565 566 567 568 569 570
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
  for (int32_t col = 0; col < pStb->numOfColumns; col++) {
    SSchema *pSchema = &pStb->pColumns[col];
    if (strcasecmp(pStb->pColumns[col].name, colName) == 0) {
      return pSchema;
    }
  }
  return NULL;
}

S
Shengliang Guan 已提交
571
static int32_t mndCreateStb(SMnode *pMnode, SNodeMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
S
Shengliang Guan 已提交
572
  SStbObj stbObj = {0};
S
Shengliang Guan 已提交
573 574
  memcpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
575 576
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
577
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
578
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
579
  stbObj.version = 1;
S
Shengliang Guan 已提交
580
  stbObj.nextColId = 1;
S
sma  
Shengliang Guan 已提交
581
  stbObj.ttl = pCreate->ttl;
S
Shengliang Guan 已提交
582 583
  stbObj.numOfColumns = pCreate->numOfColumns;
  stbObj.numOfTags = pCreate->numOfTags;
S
sma  
Shengliang Guan 已提交
584
  stbObj.numOfSmas = pCreate->numOfSmas;
S
sma  
Shengliang Guan 已提交
585
  stbObj.commentLen = pCreate->commentLen;
S
sma  
Shengliang Guan 已提交
586 587 588 589 590 591 592
  if (stbObj.commentLen > 0) {
    stbObj.comment = calloc(stbObj.commentLen, 1);
    if (stbObj.comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    memcpy(stbObj.comment, pCreate->comment, stbObj.commentLen);
S
sma  
Shengliang Guan 已提交
593
  }
S
Shengliang Guan 已提交
594

S
Shengliang Guan 已提交
595 596
  stbObj.pColumns = malloc(stbObj.numOfColumns * sizeof(SSchema));
  stbObj.pTags = malloc(stbObj.numOfTags * sizeof(SSchema));
S
sma  
Shengliang Guan 已提交
597 598
  stbObj.pSmas = malloc(stbObj.numOfSmas * sizeof(SSchema));
  if (stbObj.pColumns == NULL || stbObj.pTags == NULL || stbObj.pSmas == NULL) {
S
Shengliang Guan 已提交
599 600 601
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
602

S
Shengliang Guan 已提交
603
  for (int32_t i = 0; i < stbObj.numOfColumns; ++i) {
S
Shengliang Guan 已提交
604 605 606 607 608 609
    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 已提交
610 611 612 613
    stbObj.nextColId++;
  }

  for (int32_t i = 0; i < stbObj.numOfTags; ++i) {
S
Shengliang Guan 已提交
614 615 616 617 618 619
    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 已提交
620
    stbObj.nextColId++;
S
Shengliang Guan 已提交
621 622
  }

S
sma  
Shengliang Guan 已提交
623 624 625 626 627 628 629 630 631 632 633 634
  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) {
      mError("stb:%s, sma:%s not found in columns", stbObj.name, pSchema->name);
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    memcpy(pSchema, pColSchema, sizeof(SSchema));
  }

S
Shengliang 已提交
635
  int32_t code = -1;
S
Shengliang Guan 已提交
636
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_STB, &pReq->rpcMsg);
S
Shengliang Guan 已提交
637
  if (pTrans == NULL) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
638

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

S
Shengliang Guan 已提交
642 643 644 645 646 647
  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 已提交
648

S
Shengliang Guan 已提交
649 650 651
  code = 0;

CREATE_STB_OVER:
S
Shengliang Guan 已提交
652
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
653
  return code;
S
Shengliang Guan 已提交
654 655
}

S
Shengliang Guan 已提交
656 657
static int32_t mndProcessMCreateStbReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
658 659 660 661
  int32_t        code = -1;
  SStbObj       *pTopicStb = NULL;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
S
Shengliang Guan 已提交
662
  SUserObj      *pUser = NULL;
S
Shengliang Guan 已提交
663
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
664

S
Shengliang Guan 已提交
665 666 667 668
  if (tDeserializeSMCreateStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_STB_OVER;
  }
S
Shengliang Guan 已提交
669

S
Shengliang Guan 已提交
670
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
671 672 673 674
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_STB_OVER;
  }
S
Shengliang Guan 已提交
675

S
Shengliang Guan 已提交
676
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
677
  if (pStb != NULL) {
S
Shengliang Guan 已提交
678 679 680 681
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
      goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
682 683
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
S
Shengliang Guan 已提交
684
      goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
685
    }
S
Shengliang Guan 已提交
686
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
S
Shengliang Guan 已提交
687
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
688 689
  }

S
Shengliang Guan 已提交
690
  pTopicStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
691
  if (pTopicStb != NULL) {
L
Liu Jicong 已提交
692
    terrno = TSDB_CODE_MND_NAME_CONFLICT_WITH_TOPIC;
S
Shengliang Guan 已提交
693
    goto CREATE_STB_OVER;
L
Liu Jicong 已提交
694 695
  }

S
Shengliang Guan 已提交
696
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
697 698
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
699
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
700 701
  }

S
Shengliang Guan 已提交
702 703 704 705 706 707 708 709 710
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    goto CREATE_STB_OVER;
  }

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

S
Shengliang Guan 已提交
711
  code = mndCreateStb(pMnode, pReq, &createReq, pDb);
712
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
713

S
Shengliang Guan 已提交
714
CREATE_STB_OVER:
715
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
716
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
717 718
  }

S
Shengliang Guan 已提交
719 720 721
  mndReleaseStb(pMnode, pStb);
  mndReleaseStb(pMnode, pTopicStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
722 723
  mndReleaseUser(pMnode, pUser);
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
724 725

  return code;
S
Shengliang Guan 已提交
726 727
}

S
Shengliang Guan 已提交
728
static int32_t mndProcessVCreateStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
729
  mndTransProcessRsp(pRsp);
730 731
  return 0;
}
S
Shengliang Guan 已提交
732

S
Shengliang Guan 已提交
733
static int32_t mndCheckAlterStbReq(SMAltertbReq *pAlter) {
S
Shengliang Guan 已提交
734 735 736 737
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
738

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

S
Shengliang Guan 已提交
742
    if (pField->type <= 0) {
S
Shengliang Guan 已提交
743 744 745
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
746
    if (pField->bytes <= 0) {
S
Shengliang Guan 已提交
747 748 749
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
750
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
751 752 753
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
754 755 756 757 758
  }

  return 0;
}

S
Shengliang Guan 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
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) {
  pNew->pTags = calloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = calloc(pNew->numOfColumns, sizeof(SSchema));
  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 已提交
792
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
793 794 795 796 797 798 799 800 801 802
  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 已提交
803 804 805 806 807
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
808
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
809 810
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
811
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
812 813 814
      return -1;
    }

S
Shengliang Guan 已提交
815
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
816
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
817 818 819
      return -1;
    }

S
Shengliang Guan 已提交
820 821 822 823
    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 已提交
824 825
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
826 827

    mDebug("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
  }

  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 已提交
846
  pNew->numOfTags--;
S
Shengliang Guan 已提交
847 848 849 850 851 852

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

S
Shengliang Guan 已提交
853 854 855 856 857 858 859 860 861 862 863 864
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 已提交
865 866 867 868 869 870 871
  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 已提交
872
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
873 874 875
    return -1;
  }

S
Shengliang Guan 已提交
876 877
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
    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 已提交
893 894
static int32_t mndAlterStbTagBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
895 896 897 898 899 900 901 902 903 904 905
  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 已提交
906 907 908 909 910
  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 已提交
911
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
912 913 914 915
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
916
  pTag->bytes = pField->bytes;
S
Shengliang Guan 已提交
917 918
  pNew->version++;

S
Shengliang Guan 已提交
919
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
920 921 922
  return 0;
}

S
Shengliang Guan 已提交
923
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
924 925 926 927 928
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
929 930 931 932 933
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
934
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
935 936
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
937
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
938 939 940
      return -1;
    }

S
Shengliang Guan 已提交
941
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
942
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
943 944 945
      return -1;
    }

S
Shengliang Guan 已提交
946 947 948 949
    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 已提交
950 951
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
952 953

    mDebug("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
954 955 956 957 958 959 960 961
  }

  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 已提交
962
  if (col < 0) {
S
Shengliang Guan 已提交
963 964 965 966
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
967 968 969 970 971 972 973 974 975 976
  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 已提交
977 978 979 980 981
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
S
Shengliang Guan 已提交
982
  pNew->numOfColumns--;
S
Shengliang Guan 已提交
983 984 985 986 987 988

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

S
Shengliang Guan 已提交
989 990
static int32_t mndAlterStbColumnBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
991 992 993 994 995 996 997
  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 已提交
998
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  }

  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 已提交
1011 1012
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1013 1014 1015
    return -1;
  }

S
Shengliang Guan 已提交
1016
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1017
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1018 1019 1020
    return -1;
  }

S
Shengliang Guan 已提交
1021
  pCol->bytes = pField->bytes;
S
Shengliang Guan 已提交
1022 1023
  pNew->version++;

S
Shengliang Guan 已提交
1024
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1025 1026 1027
  return 0;
}

S
Shengliang Guan 已提交
1028
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1029 1030 1031
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
S
Shengliang Guan 已提交
1032
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_UPDATING) != 0) return -1;
S
Shengliang Guan 已提交
1033 1034 1035 1036

  return 0;
}

S
Shengliang Guan 已提交
1037
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1038 1039 1040 1041 1042 1043 1044 1045
  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 已提交
1046
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1047 1048 1049 1050 1051 1052 1053 1054
  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 已提交
1055 1056 1057 1058
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1059

S
Shengliang Guan 已提交
1060
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    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;
S
Shengliang Guan 已提交
1072
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      free(pReq);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1085
static int32_t mndAlterStb(SMnode *pMnode, SNodeMsg *pReq, const SMAltertbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
S
Shengliang Guan 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094
  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 已提交
1095
  STrans *pTrans = NULL;
S
Shengliang Guan 已提交
1096
  SField *pField0 = taosArrayGet(pAlter->pFields, 0);
S
Shengliang Guan 已提交
1097

S
Shengliang Guan 已提交
1098
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
1099
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
1100
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1101
      break;
S
Shengliang Guan 已提交
1102
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang Guan 已提交
1103
      code = mndDropSuperTableTag(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1104
      break;
S
Shengliang Guan 已提交
1105
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
S
Shengliang Guan 已提交
1106
      code = mndAlterStbTagName(pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
1107
      break;
S
Shengliang Guan 已提交
1108
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang Guan 已提交
1109
      code = mndAlterStbTagBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1110 1111
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
1112
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1113 1114
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang Guan 已提交
1115
      code = mndDropSuperTableColumn(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1116
      break;
S
Shengliang Guan 已提交
1117
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang Guan 已提交
1118
      code = mndAlterStbColumnBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1119 1120 1121 1122 1123 1124
      break;
    default:
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      break;
  }

S
Shengliang Guan 已提交
1125
  if (code != 0) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1126 1127

  code = -1;
S
Shengliang Guan 已提交
1128
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_ALTER_STB, &pReq->rpcMsg);
S
Shengliang Guan 已提交
1129
  if (pTrans == NULL) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1130

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

S
Shengliang Guan 已提交
1134 1135 1136 1137
  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 已提交
1138 1139

  code = 0;
S
Shengliang Guan 已提交
1140

S
Shengliang Guan 已提交
1141
ALTER_STB_OVER:
S
Shengliang Guan 已提交
1142 1143 1144
  mndTransDrop(pTrans);
  tfree(stbObj.pTags);
  tfree(stbObj.pColumns);
S
Shengliang Guan 已提交
1145 1146
  return code;
}
S
Shengliang Guan 已提交
1147

S
Shengliang Guan 已提交
1148 1149
static int32_t mndProcessMAlterStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1150 1151 1152
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1153
  SUserObj    *pUser = NULL;
S
Shengliang Guan 已提交
1154
  SMAltertbReq alterReq = {0};
S
Shengliang Guan 已提交
1155

S
Shengliang Guan 已提交
1156
  if (tDeserializeSMAlterStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1157 1158 1159
    terrno = TSDB_CODE_INVALID_MSG;
    goto ALTER_STB_OVER;
  }
S
Shengliang Guan 已提交
1160

S
Shengliang Guan 已提交
1161 1162
  mDebug("stb:%s, start to alter", alterReq.name);
  if (mndCheckAlterStbReq(&alterReq) != 0) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1163

S
Shengliang Guan 已提交
1164
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1165 1166
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
S
Shengliang Guan 已提交
1167
    goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1168 1169
  }

S
Shengliang Guan 已提交
1170
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1171 1172
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
S
Shengliang Guan 已提交
1173
    goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1174
  }
S
Shengliang Guan 已提交
1175

S
Shengliang Guan 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    goto ALTER_STB_OVER;
  }

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

S
Shengliang Guan 已提交
1185
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
1186
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1187

S
Shengliang Guan 已提交
1188
ALTER_STB_OVER:
1189
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1190
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1191 1192
  }

S
Shengliang Guan 已提交
1193 1194
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1195
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
1196
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1197 1198

  return code;
S
Shengliang Guan 已提交
1199
}
S
Shengliang Guan 已提交
1200

S
Shengliang Guan 已提交
1201
static int32_t mndProcessVAlterStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1202
  mndTransProcessRsp(pRsp);
1203 1204
  return 0;
}
S
Shengliang Guan 已提交
1205

S
Shengliang Guan 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
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 已提交
1224 1225 1226 1227 1228
static int32_t mndSetDropStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
  int32_t contLen;
S
Shengliang Guan 已提交
1229

S
Shengliang Guan 已提交
1230 1231 1232
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1233 1234 1235 1236
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1237

S
Shengliang Guan 已提交
1238
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1239
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    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) {
      free(pReq);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1265
static int32_t mndDropStb(SMnode *pMnode, SNodeMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1266
  int32_t code = -1;
S
Shengliang Guan 已提交
1267
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_STB, &pReq->rpcMsg);
S
Shengliang Guan 已提交
1268
  if (pTrans == NULL) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1269

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

S
Shengliang Guan 已提交
1273 1274
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1275
  if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1276
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1277

S
Shengliang Guan 已提交
1278 1279 1280
  code = 0;

DROP_STB_OVER:
S
Shengliang Guan 已提交
1281
  mndTransDrop(pTrans);
S
Shengliang 已提交
1282
  return code;
S
Shengliang Guan 已提交
1283 1284
}

S
Shengliang Guan 已提交
1285 1286
static int32_t mndProcessMDropStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1287 1288 1289 1290
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1291
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
1292

S
Shengliang Guan 已提交
1293
  if (tDeserializeSMDropStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1294 1295 1296
    terrno = TSDB_CODE_INVALID_MSG;
    goto DROP_STB_OVER;
  }
S
Shengliang Guan 已提交
1297

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

S
Shengliang Guan 已提交
1300
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1301
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1302 1303
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1304 1305
      code = 0;
      goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1306 1307
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
S
Shengliang Guan 已提交
1308
      goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1309 1310 1311
    }
  }

S
Shengliang Guan 已提交
1312
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1313 1314
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
1315
    goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1316 1317
  }

S
Shengliang Guan 已提交
1318 1319 1320 1321
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    goto DROP_STB_OVER;
  }
S
Shengliang Guan 已提交
1322

S
Shengliang Guan 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331
  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 已提交
1332
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1333 1334
  }

S
Shengliang Guan 已提交
1335 1336 1337 1338 1339
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
1340
}
S
Shengliang Guan 已提交
1341

S
Shengliang Guan 已提交
1342
static int32_t mndProcessVDropStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1343
  mndTransProcessRsp(pRsp);
1344 1345
  return 0;
}
S
Shengliang Guan 已提交
1346

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

S
Shengliang Guan 已提交
1350
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
S
Shengliang Guan 已提交
1351
  pRsp->pSchemas = calloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
1352
  if (pRsp->pSchemas == NULL) {
S
Shengliang Guan 已提交
1353
    taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1354 1355 1356 1357
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
  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 已提交
1370

S
Shengliang Guan 已提交
1371
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1372
    SSchema *pSchema = &pRsp->pSchemas[i];
S
Shengliang Guan 已提交
1373 1374 1375
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1376 1377
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1378 1379 1380
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
S
Shengliang Guan 已提交
1381
    SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
S
Shengliang Guan 已提交
1382
    SSchema *pSrcSchema = &pStb->pTags[i];
S
Shengliang Guan 已提交
1383 1384
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1385 1386
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1387
  }
S
Shengliang Guan 已提交
1388

S
Shengliang Guan 已提交
1389
  taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1390
  return 0;
S
Shengliang Guan 已提交
1391
}
S
Shengliang Guan 已提交
1392

S
Shengliang Guan 已提交
1393 1394 1395
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 已提交
1396

S
Shengliang Guan 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
  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 已提交
1414
}
S
Shengliang Guan 已提交
1415

S
Shengliang Guan 已提交
1416 1417
static int32_t mndProcessTableMetaReq(SNodeMsg *pReq) {
  SMnode       *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1418 1419 1420
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
1421

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

D
dapan1121 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
  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 已提交
1437
  }
S
Shengliang Guan 已提交
1438

S
Shengliang Guan 已提交
1439 1440 1441 1442 1443
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto RETRIEVE_META_OVER;
  }
S
Shengliang Guan 已提交
1444

S
Shengliang Guan 已提交
1445
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1446 1447 1448 1449
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto RETRIEVE_META_OVER;
  }
D
dapan 已提交
1450

S
Shengliang Guan 已提交
1451
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1452 1453
  pReq->pRsp = pRsp;
  pReq->rspLen = rspLen;
S
Shengliang Guan 已提交
1454
  code = 0;
S
Shengliang Guan 已提交
1455

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

S
Shengliang Guan 已提交
1458 1459 1460 1461
RETRIEVE_META_OVER:
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1462

S
Shengliang Guan 已提交
1463 1464 1465
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1466

S
Shengliang Guan 已提交
1467 1468 1469 1470 1471 1472 1473 1474
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 已提交
1475

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

S
Shengliang Guan 已提交
1482 1483 1484 1485 1486
    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 已提交
1487
    }
S
Shengliang Guan 已提交
1488

S
Shengliang Guan 已提交
1489 1490
    if (pStbVersion->sversion != metaRsp.sversion) {
      taosArrayPush(batchMetaRsp.pArray, &metaRsp);
S
Shengliang Guan 已提交
1491
    }
S
Shengliang Guan 已提交
1492
  }
S
Shengliang Guan 已提交
1493

S
Shengliang Guan 已提交
1494 1495 1496 1497 1498
  int32_t rspLen = tSerializeSTableMetaBatchRsp(NULL, 0, &batchMetaRsp);
  if (rspLen < 0) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
1499 1500
  }

S
Shengliang Guan 已提交
1501 1502 1503 1504 1505
  void *pRsp = malloc(rspLen);
  if (pRsp == NULL) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
1506 1507
  }

S
Shengliang Guan 已提交
1508 1509 1510
  tSerializeSTableMetaBatchRsp(pRsp, rspLen, &batchMetaRsp);
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1511 1512 1513
  return 0;
}

S
Shengliang Guan 已提交
1514
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1515
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1516 1517 1518 1519 1520 1521
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1522
  int32_t numOfStbs = 0;
1523
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1524
  while (1) {
S
Shengliang Guan 已提交
1525
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1526
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1527 1528
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1529
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1530
      numOfStbs++;
S
Shengliang Guan 已提交
1531 1532
    }

S
Shengliang Guan 已提交
1533
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1534 1535
  }

S
Shengliang Guan 已提交
1536
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1537
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1538 1539 1540
  return 0;
}

S
Shengliang Guan 已提交
1541 1542
static int32_t mndGetStbMeta(SNodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
  SMnode *pMnode = pReq->pNode;
1543
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1544

S
Shengliang Guan 已提交
1545
  if (mndGetNumOfStbs(pMnode, pShow->db, &pShow->numOfRows) != 0) {
S
Shengliang Guan 已提交
1546 1547 1548 1549
    return -1;
  }

  int32_t  cols = 0;
S
Shengliang Guan 已提交
1550
  SSchema *pSchema = pMeta->pSchemas;
S
Shengliang Guan 已提交
1551 1552 1553 1554

  pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
S
Shengliang Guan 已提交
1555
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
1556 1557 1558 1559
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
H
Haojun Liao 已提交
1560
  strcpy(pSchema[cols].name, "create_time");
S
Shengliang Guan 已提交
1561
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
1562 1563
  cols++;

S
Shengliang Guan 已提交
1564 1565
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
1566
  strcpy(pSchema[cols].name, "columns");
S
Shengliang Guan 已提交
1567
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
1568 1569
  cols++;

S
Shengliang Guan 已提交
1570 1571
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
1572
  strcpy(pSchema[cols].name, "tags");
S
Shengliang Guan 已提交
1573
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
1574 1575
  cols++;

S
Shengliang Guan 已提交
1576
  pMeta->numOfColumns = cols;
S
Shengliang Guan 已提交
1577 1578 1579 1580 1581 1582 1583
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

S
Shengliang Guan 已提交
1584
  pShow->numOfRows = sdbGetSize(pSdb, SDB_STB);
S
Shengliang Guan 已提交
1585
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
D
dapan1121 已提交
1586
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
1587 1588 1589 1590

  return 0;
}

S
Shengliang Guan 已提交
1591
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
1592 1593
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
1594
  for (pos = 0; tableId[pos] != 0; ++pos) {
H
Haojun Liao 已提交
1595
    if (tableId[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
1596 1597 1598 1599 1600 1601 1602 1603
    if (num == 2) break;
  }

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

S
Shengliang Guan 已提交
1604 1605
static int32_t mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode  *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1606
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1607 1608 1609
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1610
  char    *pWrite;
S
Shengliang Guan 已提交
1611
  char     prefix[TSDB_DB_FNAME_LEN] = {0};
S
Shengliang Guan 已提交
1612

H
Haojun Liao 已提交
1613 1614 1615 1616 1617
  SDbObj* pDb = NULL;
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
    if (pDb == NULL) return 0;
  }
S
Shengliang Guan 已提交
1618

S
Shengliang Guan 已提交
1619
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
1620
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1621 1622
    if (pShow->pIter == NULL) break;

H
Haojun Liao 已提交
1623
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
1624
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1625 1626 1627 1628 1629
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
1630
    SName name = {0};
S
Shengliang Guan 已提交
1631
    char stbName[TSDB_TABLE_NAME_LEN] = {0};
H
Haojun Liao 已提交
1632
    mndExtractTableName(pStb->name, stbName);
S
Shengliang Guan 已提交
1633
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1634
    STR_TO_VARSTR(pWrite, stbName);
S
Shengliang Guan 已提交
1635 1636
    cols++;

H
Haojun Liao 已提交
1637 1638 1639 1640 1641 1642 1643
    char  db[TSDB_DB_NAME_LEN] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT|T_NAME_DB);
    tNameGetDbName(&name, db);
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, db);
    cols++;

S
Shengliang Guan 已提交
1644
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1645
    *(int64_t *)pWrite = pStb->createdTime;
S
Shengliang Guan 已提交
1646 1647 1648
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1649
    *(int32_t *)pWrite = pStb->numOfColumns;
S
Shengliang Guan 已提交
1650 1651 1652
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1653
    *(int32_t *)pWrite = pStb->numOfTags;
S
Shengliang Guan 已提交
1654 1655
    cols++;

H
Haojun Liao 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int32_t *)pWrite = 0; // number of tables
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pStb->updateTime; // number of tables
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1665 1666 1667 1668 1669
    if (pStb->commentLen != 0) {
      STR_TO_VARSTR(pWrite, pStb->comment);
    } else {
      STR_TO_VARSTR(pWrite, "");
    }
H
Haojun Liao 已提交
1670 1671
    cols++;

S
Shengliang Guan 已提交
1672
    numOfRows++;
S
Shengliang Guan 已提交
1673
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1674 1675
  }

H
Haojun Liao 已提交
1676 1677 1678 1679
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

S
Shengliang Guan 已提交
1680
  pShow->numOfReads += numOfRows;
S
Shengliang Guan 已提交
1681
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
1682 1683 1684
  return numOfRows;
}

S
Shengliang Guan 已提交
1685
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1686 1687
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
1688
}