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

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

S
Shengliang Guan 已提交
27
#define TSDB_STB_VER_NUMBER 1
S
Shengliang Guan 已提交
28 29 30 31 32 33
#define TSDB_STB_RESERVE_SIZE 64

static SSdbRaw *mndStbActionEncode(SStbObj *pStb);
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 35 36 37 38 39 40 41 42 43
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
static int32_t  mndProcessMCreateStbReq(SMnodeMsg *pReq);
static int32_t  mndProcessMAlterStbReq(SMnodeMsg *pReq);
static int32_t  mndProcessMDropStbReq(SMnodeMsg *pReq);
static int32_t  mndProcessVCreateStbRsp(SMnodeMsg *pRsp);
static int32_t  mndProcessVAlterStbRsp(SMnodeMsg *pRsp);
static int32_t  mndProcessVDropStbRsp(SMnodeMsg *pRsp);
static int32_t  mndProcessStbMetaReq(SMnodeMsg *pReq);
static int32_t  mndGetStbMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
static int32_t  mndRetrieveStb(SMnodeMsg *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 56 57 58 59 60 61
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcessMCreateStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcessMAlterStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcessMDropStbReq);
  mndSetMsgHandle(pMnode, TDMT_VND_CREATE_STB_RSP, mndProcessVCreateStbRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_ALTER_STB_RSP, mndProcessVAlterStbRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_STB_RSP, mndProcessVDropStbRsp);
  mndSetMsgHandle(pMnode, TDMT_MND_STB_META, mndProcessStbMetaReq);
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

S
Shengliang Guan 已提交
72
static SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
73 74
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

  int32_t dataPos = 0;
80 81 82 83 84 85 86
  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 已提交
87
  SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, STB_ENCODE_OVER)
88 89
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, STB_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, STB_ENCODE_OVER)
S
Shengliang Guan 已提交
90

S
Shengliang Guan 已提交
91 92 93 94 95 96 97 98 99 100
  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];
101 102 103 104
    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 已提交
105 106
  }

107 108 109 110 111 112 113 114 115 116 117
  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 已提交
118

119
  mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
120 121 122
  return pRaw;
}

S
Shengliang Guan 已提交
123
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
124 125
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
126
  int8_t sver = 0;
127
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
128

S
Shengliang Guan 已提交
129
  if (sver != TSDB_STB_VER_NUMBER) {
S
Shengliang Guan 已提交
130
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
131
    goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
132 133
  }

S
Shengliang 已提交
134
  SSdbRow *pRow = sdbAllocRow(sizeof(SStbObj));
135 136
  if (pRow == NULL) goto STB_DECODE_OVER;

S
Shengliang Guan 已提交
137
  SStbObj *pStb = sdbGetRowObj(pRow);
138
  if (pStb == NULL) goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
139 140

  int32_t dataPos = 0;
141 142 143 144 145 146 147
  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 已提交
148
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, STB_DECODE_OVER)
149 150
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, STB_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, STB_DECODE_OVER)
S
Shengliang Guan 已提交
151

S
Shengliang Guan 已提交
152 153 154
  pStb->pColumns = calloc(pStb->numOfColumns, sizeof(SSchema));
  pStb->pTags = calloc(pStb->numOfTags, sizeof(SSchema));
  if (pStb->pColumns == NULL || pStb->pTags == NULL) {
S
Shengliang 已提交
155 156
    goto STB_DECODE_OVER;
  }
S
Shengliang Guan 已提交
157

S
Shengliang Guan 已提交
158 159 160 161 162 163 164 165 166 167
  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];
168 169 170 171
    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 已提交
172 173
  }

174 175 176 177 178 179 180 181 182 183
  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());
    tfree(pRow);
    return NULL;
  }
S
Shengliang Guan 已提交
184

185
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
186 187 188
  return pRow;
}

S
Shengliang Guan 已提交
189
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
190
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
191 192 193
  return 0;
}

S
Shengliang Guan 已提交
194
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
195
  mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
196 197
  tfree(pStb->pColumns);
  tfree(pStb->pTags);
S
Shengliang Guan 已提交
198 199 200
  return 0;
}

S
Shengliang Guan 已提交
201 202
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 已提交
203

S
Shengliang Guan 已提交
204
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
205 206

  if (pOld->numOfColumns < pNew->numOfColumns) {
S
Shengliang Guan 已提交
207 208
    void *pColumns = malloc(pNew->numOfColumns * sizeof(SSchema));
    if (pColumns != NULL) {
S
Shengliang Guan 已提交
209
      free(pOld->pColumns);
S
Shengliang Guan 已提交
210
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
211 212 213 214 215 216 217 218
    } 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 已提交
219 220
    void *pTags = malloc(pNew->numOfTags * sizeof(SSchema));
    if (pTags != NULL) {
S
Shengliang Guan 已提交
221
      free(pOld->pTags);
S
Shengliang Guan 已提交
222
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
223 224 225 226
    } 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 已提交
227
    }
S
Shengliang Guan 已提交
228 229
  }

S
Shengliang Guan 已提交
230 231
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
S
Shengliang Guan 已提交
232
  pOld->nextColId = pNew->nextColId;
S
Shengliang Guan 已提交
233 234
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
235 236
  memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
237
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
238 239 240
  return 0;
}

S
Shengliang Guan 已提交
241
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
242
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
243
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
244
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
245 246 247
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
248 249
}

S
Shengliang Guan 已提交
250
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
251 252 253 254
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

S
Shengliang Guan 已提交
255
static SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
256 257
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
258

S
Shengliang Guan 已提交
259 260
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
261

S
Shengliang Guan 已提交
262 263
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
264

S
Shengliang Guan 已提交
265
static void *mndBuildCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
266
  SName name = {0};
S
Shengliang Guan 已提交
267
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
268

S
Shengliang Guan 已提交
269 270
  SVCreateTbReq req = {0};
  req.ver = 0;
S
Shengliang Guan 已提交
271
  req.name = (char *)tNameGetTableName(&name);
H
more  
Hongze Cheng 已提交
272 273 274 275 276
  req.ttl = 0;
  req.keep = 0;
  req.type = TD_SUPER_TABLE;
  req.stbCfg.suid = pStb->uid;
  req.stbCfg.nCols = pStb->numOfColumns;
S
Shengliang Guan 已提交
277
  req.stbCfg.pSchema = pStb->pColumns;
H
more  
Hongze Cheng 已提交
278
  req.stbCfg.nTagCols = pStb->numOfTags;
S
Shengliang Guan 已提交
279
  req.stbCfg.pTagSchema = pStb->pTags;
H
more  
Hongze Cheng 已提交
280

S
Shengliang Guan 已提交
281 282 283
  int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
  SMsgHead *pHead = malloc(contLen);
  if (pHead == NULL) {
H
more  
Hongze Cheng 已提交
284 285 286 287
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
288 289
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
290

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

S
Shengliang Guan 已提交
294 295
  *pContLen = contLen;
  return pHead;
296 297
}

S
Shengliang Guan 已提交
298 299 300 301 302 303 304 305 306
static void *mndBuildDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
  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;
307

S
Shengliang Guan 已提交
308 309 310
  int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
  SMsgHead *pHead = malloc(contLen);
  if (pHead == NULL) {
311 312 313 314
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
315 316
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
317

S
Shengliang Guan 已提交
318 319
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
  tSerializeSVDropTbReq(&pBuf, &req);
320

S
Shengliang Guan 已提交
321 322
  *pContLen = contLen;
  return pHead;
323 324
}

S
Shengliang Guan 已提交
325
static int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
326 327 328 329
  pCreate->numOfColumns = htonl(pCreate->numOfColumns);
  pCreate->numOfTags = htonl(pCreate->numOfTags);
  int32_t totalCols = pCreate->numOfColumns + pCreate->numOfTags;
  for (int32_t i = 0; i < totalCols; ++i) {
S
Shengliang Guan 已提交
330
    SSchema *pSchema = &pCreate->pSchemas[i];
S
Shengliang Guan 已提交
331 332
    pSchema->bytes = htonl(pSchema->bytes);
  }
S
Shengliang Guan 已提交
333

S
Shengliang Guan 已提交
334
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
335
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
336 337
    return -1;
  }
S
Shengliang Guan 已提交
338

S
Shengliang Guan 已提交
339
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
340
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
341 342
    return -1;
  }
S
Shengliang Guan 已提交
343

S
Shengliang Guan 已提交
344
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
345
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
346 347
    return -1;
  }
S
Shengliang Guan 已提交
348

S
Shengliang Guan 已提交
349 350
  int32_t maxColId = (TSDB_MAX_COLUMNS + TSDB_MAX_TAGS);
  for (int32_t i = 0; i < totalCols; ++i) {
S
Shengliang Guan 已提交
351
    SSchema *pSchema = &pCreate->pSchemas[i];
S
Shengliang Guan 已提交
352 353
    if (pSchema->type < 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
354 355 356
      return -1;
    }
    if (pSchema->bytes <= 0) {
S
Shengliang Guan 已提交
357
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
358 359 360
      return -1;
    }
    if (pSchema->name[0] == 0) {
S
Shengliang Guan 已提交
361
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
362 363 364
      return -1;
    }
  }
S
Shengliang Guan 已提交
365

S
Shengliang Guan 已提交
366 367 368
  return 0;
}

369
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
370 371 372 373 374 375 376 377
  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;
}

378
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
379 380 381 382 383 384 385 386
  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;
}

387
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
388 389 390 391 392 393 394 395
  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;
}

396
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
397
  SSdb   *pSdb = pMnode->pSdb;
398
  SVgObj *pVgroup = NULL;
399
  void   *pIter = NULL;
S
Shengliang Guan 已提交
400
  int32_t contLen;
401 402 403 404 405 406

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != pDb->uid) continue;

S
Shengliang Guan 已提交
407 408
    void *pReq = mndBuildCreateStbReq(pMnode, pVgroup, pStb, &contLen);
    if (pReq == NULL) {
409 410 411 412 413
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
414

415 416
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
417
    action.pCont = pReq;
S
Shengliang Guan 已提交
418
    action.contLen = contLen;
H
Hongze Cheng 已提交
419
    action.msgType = TDMT_VND_CREATE_STB;
420
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
421
      free(pReq);
422 423 424 425 426 427
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
428 429 430 431

  return 0;
}

432
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
433
  SSdb   *pSdb = pMnode->pSdb;
434
  SVgObj *pVgroup = NULL;
435
  void   *pIter = NULL;
436 437 438 439 440 441

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != pDb->uid) continue;

S
Shengliang Guan 已提交
442 443
    int32_t contLen = 0;
    void   *pReq = mndBuildDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
444
    if (pReq == NULL) {
445 446 447 448 449
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
450

451 452
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
453
    action.pCont = pReq;
S
Shengliang Guan 已提交
454
    action.contLen = contLen;
H
Hongze Cheng 已提交
455
    action.msgType = TDMT_VND_DROP_STB;
456
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
457
      free(pReq);
458 459 460 461 462 463
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
464 465 466 467

  return 0;
}

S
Shengliang Guan 已提交
468
static int32_t mndCreateStb(SMnode *pMnode, SMnodeMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
S
Shengliang Guan 已提交
469
  SStbObj stbObj = {0};
S
Shengliang Guan 已提交
470 471
  memcpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
472 473
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
474
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
475
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
476
  stbObj.version = 1;
S
Shengliang Guan 已提交
477
  stbObj.nextColId = 1;
S
Shengliang Guan 已提交
478 479 480
  stbObj.numOfColumns = pCreate->numOfColumns;
  stbObj.numOfTags = pCreate->numOfTags;

S
Shengliang Guan 已提交
481 482
  stbObj.pColumns = malloc(stbObj.numOfColumns * sizeof(SSchema));
  stbObj.pTags = malloc(stbObj.numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
483
  if (stbObj.pColumns == NULL || stbObj.pTags == NULL) {
S
Shengliang Guan 已提交
484 485 486 487
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
488 489
  memcpy(stbObj.pColumns, pCreate->pSchemas, stbObj.numOfColumns * sizeof(SSchema));
  memcpy(stbObj.pTags, pCreate->pSchemas + stbObj.numOfColumns, stbObj.numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
490 491 492 493 494 495 496 497 498

  for (int32_t i = 0; i < stbObj.numOfColumns; ++i) {
    stbObj.pColumns[i].colId = stbObj.nextColId;
    stbObj.nextColId++;
  }

  for (int32_t i = 0; i < stbObj.numOfTags; ++i) {
    stbObj.pTags[i].colId = stbObj.nextColId;
    stbObj.nextColId++;
S
Shengliang Guan 已提交
499 500
  }

S
Shengliang 已提交
501
  int32_t code = -1;
S
Shengliang Guan 已提交
502 503
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
  if (pTrans == NULL) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
504

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

S
Shengliang Guan 已提交
507 508 509 510 511 512
  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 已提交
513

S
Shengliang Guan 已提交
514 515 516
  code = 0;

CREATE_STB_OVER:
S
Shengliang Guan 已提交
517
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
518
  return code;
S
Shengliang Guan 已提交
519 520
}

S
Shengliang Guan 已提交
521 522 523
static int32_t mndProcessMCreateStbReq(SMnodeMsg *pReq) {
  SMnode         *pMnode = pReq->pMnode;
  SMCreateStbReq *pCreate = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
524

S
Shengliang Guan 已提交
525
  mDebug("stb:%s, start to create", pCreate->name);
S
Shengliang Guan 已提交
526

S
Shengliang Guan 已提交
527
  if (mndCheckCreateStbReq(pCreate) != 0) {
S
Shengliang Guan 已提交
528 529 530 531 532 533
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

  SStbObj *pStb = mndAcquireStb(pMnode, pCreate->name);
  if (pStb != NULL) {
S
Shengliang 已提交
534
    mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
535 536 537 538 539
    if (pCreate->igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", pCreate->name);
      return 0;
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
S
Shengliang Guan 已提交
540
      mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
S
Shengliang Guan 已提交
541 542
      return -1;
    }
S
Shengliang Guan 已提交
543 544
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
S
Shengliang 已提交
545
    return -1;
S
Shengliang Guan 已提交
546 547
  }

H
more  
Hongze Cheng 已提交
548
  // topic should have different name with stb
S
Shengliang Guan 已提交
549 550 551
  SStbObj *pTopicStb = mndAcquireStb(pMnode, pCreate->name);
  if (pTopicStb != NULL) {
    mndReleaseStb(pMnode, pTopicStb);
L
Liu Jicong 已提交
552 553 554 555 556
    terrno = TSDB_CODE_MND_NAME_CONFLICT_WITH_TOPIC;
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
557 558 559 560 561 562 563
  SDbObj *pDb = mndAcquireDbByStb(pMnode, pCreate->name);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
564
  int32_t code = mndCreateStb(pMnode, pReq, pCreate, pDb);
S
Shengliang Guan 已提交
565 566 567 568 569 570 571 572 573 574
  mndReleaseDb(pMnode, pDb);

  if (code != 0) {
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
575 576
static int32_t mndProcessVCreateStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
577 578
  return 0;
}
S
Shengliang Guan 已提交
579

S
Shengliang Guan 已提交
580 581
static int32_t mndCheckAlterStbReq(SMAltertbReq *pAlter) {
  pAlter->numOfSchemas = htonl(pAlter->numOfSchemas);
S
Shengliang Guan 已提交
582

S
Shengliang Guan 已提交
583 584
  for (int32_t i = 0; i < pAlter->numOfSchemas; ++i) {
    SSchema *pSchema = &pAlter->pSchemas[i];
S
Shengliang Guan 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    pSchema->colId = htonl(pSchema->colId);
    pSchema->bytes = htonl(pSchema->bytes);

    if (pSchema->type <= 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    if (pSchema->colId < 0 || pSchema->colId >= (TSDB_MAX_COLUMNS + TSDB_MAX_TAGS)) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    if (pSchema->bytes <= 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    if (pSchema->name[0] == 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
604 605 606 607 608
  }

  return 0;
}

S
Shengliang Guan 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
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;
}

static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, const SSchema *pSchemas, int32_t ntags) {
  if (pOld->numOfTags + ntags > TSDB_MAX_TAGS) {
    terrno = TSDB_CODE_MND_TOO_MANY_TAGS;
S
Shengliang Guan 已提交
645 646
    return -1;
  }
S
Shengliang Guan 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

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

  for (int32_t i = 0; i < ntags; i++) {
    if (mndFindSuperTableColumnIndex(pOld, pSchemas[i].name) > 0) {
      terrno = TSDB_CODE_MND_TAG_ALREAY_EXIST;
      return -1;
    }

    if (mndFindSuperTableTagIndex(pOld, pSchemas[i].name) > 0) {
      terrno = TSDB_CODE_MND_COLUMN_ALREAY_EXIST;
      return -1;
    }
  }

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

  memcpy(pNew->pTags + pOld->numOfTags, pSchemas, sizeof(SSchema) * ntags);

  for (int32_t i = pOld->numOfTags; i < pNew->numOfTags; i++) {
    SSchema *pSchema = &pNew->pTags[i];
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
  }

  pNew->version++;
  mDebug("stb:%s, start to add tag %s", pNew->name, pSchemas[0].name);
  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));

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

S
Shengliang Guan 已提交
701
static int32_t mndAlterStbTagName(const SStbObj *pOld, SStbObj *pNew, const char *oldTagName, const char *newTagName) {
S
Shengliang Guan 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714
  int32_t tag = mndFindSuperTableTagIndex(pOld, oldTagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

  if (mndFindSuperTableTagIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_TAG_ALREAY_EXIST;
    return -1;
  }

  int32_t len = (int32_t)strlen(newTagName);
  if (len >= TSDB_COL_NAME_LEN) {
S
Shengliang Guan 已提交
715
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
716 717
    return -1;
  }
S
Shengliang Guan 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730

  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 已提交
731
static int32_t mndAlterStbTagBytes(const SStbObj *pOld, SStbObj *pNew, const SSchema *pSchema) {
S
Shengliang Guan 已提交
732 733 734 735 736 737 738
  int32_t tag = mndFindSuperTableTagIndex(pOld, pSchema->name);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

  if (!(pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR)) {
S
Shengliang Guan 已提交
739
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
740 741
    return -1;
  }
S
Shengliang Guan 已提交
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 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 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

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

  SSchema *pTag = pNew->pTags + tag;
  if (pSchema->bytes <= pTag->bytes) {
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

  pTag->bytes = pSchema->bytes;
  pNew->version++;

  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pSchema->name, pSchema->bytes);
  return 0;
}

static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, const SSchema *pSchemas, int32_t ncols) {
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

  for (int32_t i = 0; i < ncols; i++) {
    if (mndFindSuperTableColumnIndex(pOld, pSchemas[i].name) > 0) {
      terrno = TSDB_CODE_MND_TAG_ALREAY_EXIST;
      return -1;
    }

    if (mndFindSuperTableTagIndex(pOld, pSchemas[i].name) > 0) {
      terrno = TSDB_CODE_MND_COLUMN_ALREAY_EXIST;
      return -1;
    }
  }

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

  memcpy(pNew->pColumns + pOld->numOfColumns, pSchemas, sizeof(SSchema) * ncols);

  for (int32_t i = pOld->numOfColumns; i < pNew->numOfColumns; i++) {
    SSchema *pSchema = &pNew->pColumns[i];
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
  }

  pNew->version++;
  mDebug("stb:%s, start to add column %s", pNew->name, pSchemas[0].name);
  return 0;
}

static int32_t mndDropSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, const char *colName) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
  if (col <= 0) {
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

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

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));

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

S
Shengliang Guan 已提交
814
static int32_t mndAlterStbColumnBytes(const SStbObj *pOld, SStbObj *pNew, const SSchema *pSchema) {
S
Shengliang Guan 已提交
815 816 817 818 819 820 821
  int32_t col = mndFindSuperTableColumnIndex(pOld, pSchema->name);
  if (col < 0) {
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

  if (!(pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR)) {
S
Shengliang Guan 已提交
822
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
823 824 825
    return -1;
  }

S
Shengliang Guan 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
  uint32_t nLen = 0;
  for (int32_t i = 0; i < pOld->numOfColumns; ++i) {
    nLen += (pOld->pColumns[i].colId == col) ? pSchema->bytes : pOld->pColumns[i].bytes;
  }

  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;
  if (pSchema->bytes <= pCol->bytes) {
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

  pCol->bytes = pSchema->bytes;
  pNew->version++;

  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pSchema->name, pSchema->bytes);
  return 0;
}

S
Shengliang Guan 已提交
853
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
854 855 856
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
S
Shengliang Guan 已提交
857
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_UPDATING) != 0) return -1;
S
Shengliang Guan 已提交
858 859 860 861

  return 0;
}

S
Shengliang Guan 已提交
862
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
863 864 865 866 867 868 869 870
  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 已提交
871
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
  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;
    if (pVgroup->dbUid != pDb->uid) continue;

    void *pReq = mndBuildCreateStbReq(pMnode, pVgroup, pStb, &contLen);
    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 已提交
894
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
895 896 897 898 899 900 901 902 903
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      free(pReq);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

S
Shengliang Guan 已提交
904 905 906
  return 0;
}

S
Shengliang Guan 已提交
907
static int32_t mndAlterStb(SMnode *pMnode, SMnodeMsg *pReq, const SMAltertbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
S
Shengliang Guan 已提交
908 909 910 911 912 913 914 915 916
  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 已提交
917

S
Shengliang Guan 已提交
918
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
919
    case TSDB_ALTER_TABLE_ADD_TAG_COLUMN:
S
Shengliang Guan 已提交
920
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pSchemas, 1);
S
Shengliang Guan 已提交
921 922
      break;
    case TSDB_ALTER_TABLE_DROP_TAG_COLUMN:
S
Shengliang Guan 已提交
923
      code = mndDropSuperTableTag(pOld, &stbObj, pAlter->pSchemas[0].name);
S
Shengliang Guan 已提交
924
      break;
S
Shengliang Guan 已提交
925
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
S
Shengliang Guan 已提交
926
      code = mndAlterStbTagName(pOld, &stbObj, pAlter->pSchemas[0].name, pAlter->pSchemas[1].name);
S
Shengliang Guan 已提交
927
      break;
S
Shengliang Guan 已提交
928
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang Guan 已提交
929
      code = mndAlterStbTagBytes(pOld, &stbObj, &pAlter->pSchemas[0]);
S
Shengliang Guan 已提交
930 931
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
932
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pSchemas, 1);
S
Shengliang Guan 已提交
933 934
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang Guan 已提交
935
      code = mndDropSuperTableColumn(pOld, &stbObj, pAlter->pSchemas[0].name);
S
Shengliang Guan 已提交
936
      break;
S
Shengliang Guan 已提交
937
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang Guan 已提交
938
      code = mndAlterStbColumnBytes(pOld, &stbObj, &pAlter->pSchemas[0]);
S
Shengliang Guan 已提交
939 940 941 942 943 944
      break;
    default:
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      break;
  }

S
Shengliang Guan 已提交
945
  if (code != 0) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
946 947 948

  code = -1;
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, &pReq->rpcMsg);
S
Shengliang Guan 已提交
949
  if (pTrans == NULL) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
950

S
Shengliang Guan 已提交
951
  mDebug("trans:%d, used to alter stb:%s", pTrans->id, pAlter->name);
S
Shengliang Guan 已提交
952

S
Shengliang Guan 已提交
953 954 955 956
  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 已提交
957 958

  code = 0;
S
Shengliang Guan 已提交
959

S
Shengliang Guan 已提交
960
ALTER_STB_OVER:
S
Shengliang Guan 已提交
961 962 963
  mndTransDrop(pTrans);
  tfree(stbObj.pTags);
  tfree(stbObj.pColumns);
S
Shengliang Guan 已提交
964 965
  return code;
}
S
Shengliang Guan 已提交
966

S
Shengliang Guan 已提交
967
static int32_t mndProcessMAlterStbReq(SMnodeMsg *pReq) {
S
Shengliang Guan 已提交
968 969
  SMnode       *pMnode = pReq->pMnode;
  SMAltertbReq *pAlter = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
970 971 972

  mDebug("stb:%s, start to alter", pAlter->name);

S
Shengliang Guan 已提交
973
  if (mndCheckAlterStbReq(pAlter) != 0) {
S
Shengliang Guan 已提交
974 975 976 977 978 979 980 981 982 983 984
    mError("stb:%s, failed to alter since %s", pAlter->name, terrstr());
    return -1;
  }

  SStbObj *pStb = mndAcquireStb(pMnode, pAlter->name);
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
    mError("stb:%s, failed to alter since %s", pAlter->name, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
985
  SDbObj *pDb = mndAcquireDbByStb(pMnode, pAlter->name);
S
Shengliang Guan 已提交
986 987 988
  if (pDb == NULL) {
    mndReleaseStb(pMnode, pStb);
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
989
    mError("stb:%s, failed to alter since %s", pAlter->name, terrstr());
S
Shengliang Guan 已提交
990 991
    return -1;
  }
S
Shengliang Guan 已提交
992

S
Shengliang Guan 已提交
993
  int32_t code = mndAlterStb(pMnode, pReq, pAlter, pDb, pStb);
S
Shengliang Guan 已提交
994 995 996 997 998 999 1000 1001 1002
  mndReleaseStb(pMnode, pStb);

  if (code != 0) {
    mError("stb:%s, failed to alter since %s", pAlter->name, tstrerror(code));
    return code;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}
S
Shengliang Guan 已提交
1003

S
Shengliang Guan 已提交
1004 1005
static int32_t mndProcessVAlterStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
1006 1007
  return 0;
}
S
Shengliang Guan 已提交
1008

S
Shengliang Guan 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
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 已提交
1027 1028 1029 1030 1031
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 已提交
1032

S
Shengliang Guan 已提交
1033 1034 1035 1036
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != pDb->uid) continue;
S
Shengliang Guan 已提交
1037

S
Shengliang Guan 已提交
1038 1039
    int32_t contLen = 0;
    void   *pReq = mndBuildDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
    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;
}

static int32_t mndDropStb(SMnode *pMnode, SMnodeMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1066
  int32_t code = -1;
S
Shengliang Guan 已提交
1067
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
S
Shengliang Guan 已提交
1068
  if (pTrans == NULL) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1069

S
Shengliang Guan 已提交
1070
  mDebug("trans:%d, used to drop stb:%s", pTrans->id, pStb->name);
S
Shengliang Guan 已提交
1071

S
Shengliang Guan 已提交
1072 1073
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1074
  if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1075
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1076

S
Shengliang Guan 已提交
1077 1078 1079
  code = 0;

DROP_STB_OVER:
S
Shengliang Guan 已提交
1080
  mndTransDrop(pTrans);
S
Shengliang 已提交
1081
  return code;
S
Shengliang Guan 已提交
1082 1083
}

S
Shengliang Guan 已提交
1084 1085 1086
static int32_t mndProcessMDropStbReq(SMnodeMsg *pReq) {
  SMnode       *pMnode = pReq->pMnode;
  SMDropStbReq *pDrop = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

  mDebug("stb:%s, start to drop", pDrop->name);

  SStbObj *pStb = mndAcquireStb(pMnode, pDrop->name);
  if (pStb == NULL) {
    if (pDrop->igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", pDrop->name);
      return 0;
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
      mError("stb:%s, failed to drop since %s", pDrop->name, terrstr());
      return -1;
    }
  }

S
Shengliang Guan 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
  SDbObj *pDb = mndAcquireDbByStb(pMnode, pDrop->name);
  if (pDb == NULL) {
    mndReleaseStb(pMnode, pStb);
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    mError("stb:%s, failed to drop since %s", pDrop->name, terrstr());
    return -1;
  }

  int32_t code = mndDropStb(pMnode, pReq, pDb, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120
  mndReleaseStb(pMnode, pStb);

  if (code != 0) {
    mError("stb:%s, failed to drop since %s", pDrop->name, terrstr());
    return -1;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}
S
Shengliang Guan 已提交
1121

S
Shengliang Guan 已提交
1122 1123
static int32_t mndProcessVDropStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
1124 1125
  return 0;
}
S
Shengliang Guan 已提交
1126

S
Shengliang Guan 已提交
1127 1128 1129
static int32_t mndProcessStbMetaReq(SMnodeMsg *pReq) {
  SMnode        *pMnode = pReq->pMnode;
  STableInfoReq *pInfo = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
1130

D
dapan1121 已提交
1131 1132
  char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
  snprintf(tbFName, sizeof(tbFName), "%s.%s", pInfo->dbFName, pInfo->tbName);
S
Shengliang Guan 已提交
1133

D
dapan1121 已提交
1134 1135
  mDebug("stb:%s, start to retrieve meta", tbFName);

D
dapan 已提交
1136
  SDbObj *pDb = mndAcquireDb(pMnode, pInfo->dbFName);
S
Shengliang Guan 已提交
1137 1138
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
D
dapan1121 已提交
1139
    mError("stb:%s, failed to retrieve meta since %s", tbFName, terrstr());
S
Shengliang Guan 已提交
1140 1141 1142
    return -1;
  }

D
dapan1121 已提交
1143
  SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
S
Shengliang Guan 已提交
1144 1145
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1146
    terrno = TSDB_CODE_MND_INVALID_STB;
D
dapan1121 已提交
1147
    mError("stb:%s, failed to get meta since %s", tbFName, terrstr());
S
Shengliang Guan 已提交
1148 1149 1150
    return -1;
  }

S
Shengliang Guan 已提交
1151 1152
  taosRLockLatch(&pStb->lock);
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
S
Shengliang Guan 已提交
1153
  int32_t contLen = sizeof(STableMetaRsp) + totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
1154

S
Shengliang Guan 已提交
1155
  STableMetaRsp *pMeta = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
1156
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
1157 1158 1159
    taosRUnLockLatch(&pStb->lock);
    mndReleaseDb(pMnode, pDb);
    mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
1160
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1161
    mError("stb:%s, failed to get meta since %s", tbFName, terrstr());
S
Shengliang Guan 已提交
1162 1163 1164
    return -1;
  }

D
dapan1121 已提交
1165 1166
  strcpy(pMeta->dbFName, pStb->db);
  strcpy(pMeta->tbName, pInfo->tbName);
D
dapan 已提交
1167
  strcpy(pMeta->stbName, pInfo->tbName);
D
dapan1121 已提交
1168
  pMeta->dbId = htobe64(pDb->uid);
S
Shengliang Guan 已提交
1169 1170 1171 1172 1173 1174
  pMeta->numOfTags = htonl(pStb->numOfTags);
  pMeta->numOfColumns = htonl(pStb->numOfColumns);
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = TSDB_SUPER_TABLE;
  pMeta->update = pDb->cfg.update;
  pMeta->sversion = htonl(pStb->version);
D
dapan1121 已提交
1175 1176
  pMeta->suid = htobe64(pStb->uid);
  pMeta->tuid = htobe64(pStb->uid);
S
Shengliang Guan 已提交
1177

S
Shengliang Guan 已提交
1178
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1179
    SSchema *pSchema = &pMeta->pSchema[i];
S
Shengliang Guan 已提交
1180
    SSchema *pSrcSchema = &pStb->pColumns[i];
S
Shengliang Guan 已提交
1181 1182 1183 1184
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
    pSchema->colId = htonl(pSrcSchema->colId);
    pSchema->bytes = htonl(pSrcSchema->bytes);
S
Shengliang Guan 已提交
1185
  }
S
Shengliang Guan 已提交
1186 1187 1188 1189

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pMeta->pSchema[i + pStb->numOfColumns];
    SSchema *pSrcSchema = &pStb->pTags[i];
S
Shengliang Guan 已提交
1190 1191 1192 1193
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
    pSchema->colId = htonl(pSrcSchema->colId);
    pSchema->bytes = htonl(pSrcSchema->bytes);
S
Shengliang Guan 已提交
1194
  }
S
Shengliang Guan 已提交
1195

S
Shengliang Guan 已提交
1196 1197 1198
  taosRUnLockLatch(&pStb->lock);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
1199

S
Shengliang Guan 已提交
1200 1201
  pReq->pCont = pMeta;
  pReq->contLen = contLen;
S
Shengliang Guan 已提交
1202

D
dapan1121 已提交
1203
  mDebug("stb:%s, meta is retrieved, cols:%d tags:%d", tbFName, pStb->numOfColumns, pStb->numOfTags);
S
Shengliang Guan 已提交
1204 1205
  return 0;
}
S
Shengliang Guan 已提交
1206

D
dapan 已提交
1207
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableMetaVersion *stbs, int32_t num, void **rsp, int32_t *rspLen) {
S
Shengliang Guan 已提交
1208 1209 1210 1211 1212
  SSdb          *pSdb = pMnode->pSdb;
  int32_t        bufSize = num * (sizeof(STableMetaRsp) + 4 * sizeof(SSchema));
  void          *buf = malloc(bufSize);
  int32_t        len = 0;
  int32_t        contLen = 0;
D
dapan 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221
  STableMetaRsp *pRsp = NULL;

  for (int32_t i = 0; i < num; ++i) {
    SSTableMetaVersion *stb = &stbs[i];
    stb->suid = be64toh(stb->suid);
    stb->sversion = ntohs(stb->sversion);
    stb->tversion = ntohs(stb->tversion);

    if ((contLen + sizeof(STableMetaRsp)) > bufSize) {
S
Shengliang Guan 已提交
1222
      bufSize = contLen + (num - i) * (sizeof(STableMetaRsp) + 4 * sizeof(SSchema));
D
dapan 已提交
1223 1224 1225 1226 1227 1228 1229 1230
      buf = realloc(buf, bufSize);
    }

    pRsp = (STableMetaRsp *)((char *)buf + contLen);

    strcpy(pRsp->dbFName, stb->dbFName);
    strcpy(pRsp->tbName, stb->stbName);
    strcpy(pRsp->stbName, stb->stbName);
S
Shengliang Guan 已提交
1231

D
dapan 已提交
1232
    mDebug("start to retrieve meta, db:%s, stb:%s", stb->dbFName, stb->stbName);
S
Shengliang Guan 已提交
1233

D
dapan 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
    SDbObj *pDb = mndAcquireDb(pMnode, stb->dbFName);
    if (pDb == NULL) {
      pRsp->numOfColumns = -1;
      pRsp->suid = htobe64(stb->suid);
      contLen += sizeof(STableMetaRsp);
      mWarn("db:%s, failed to require db since %s", stb->dbFName, terrstr());
      continue;
    }

    char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
    snprintf(tbFName, sizeof(tbFName), "%s.%s", stb->dbFName, stb->stbName);
S
Shengliang Guan 已提交
1245

D
dapan 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254
    SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
    if (pStb == NULL) {
      mndReleaseDb(pMnode, pDb);
      pRsp->numOfColumns = -1;
      pRsp->suid = htobe64(stb->suid);
      contLen += sizeof(STableMetaRsp);
      mWarn("stb:%s, failed to get meta since %s", tbFName, terrstr());
      continue;
    }
S
Shengliang Guan 已提交
1255

D
dapan 已提交
1256 1257 1258 1259 1260 1261 1262 1263
    taosRLockLatch(&pStb->lock);

    if (stb->suid == pStb->uid && stb->sversion == pStb->version) {
      taosRUnLockLatch(&pStb->lock);
      mndReleaseDb(pMnode, pDb);
      mndReleaseStb(pMnode, pStb);
      continue;
    }
S
Shengliang Guan 已提交
1264

D
dapan 已提交
1265 1266
    int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
    int32_t len = totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
1267

D
dapan 已提交
1268
    contLen += sizeof(STableMetaRsp) + len;
S
Shengliang Guan 已提交
1269

D
dapan 已提交
1270
    if (contLen > bufSize) {
S
Shengliang Guan 已提交
1271
      bufSize = contLen + (num - i - 1) * (sizeof(STableMetaRsp) + 4 * sizeof(SSchema));
D
dapan 已提交
1272 1273
      buf = realloc(buf, bufSize);
    }
S
Shengliang Guan 已提交
1274

D
dapan 已提交
1275 1276 1277 1278 1279 1280 1281 1282
    pRsp->numOfTags = htonl(pStb->numOfTags);
    pRsp->numOfColumns = htonl(pStb->numOfColumns);
    pRsp->precision = pDb->cfg.precision;
    pRsp->tableType = TSDB_SUPER_TABLE;
    pRsp->update = pDb->cfg.update;
    pRsp->sversion = htonl(pStb->version);
    pRsp->suid = htobe64(pStb->uid);
    pRsp->tuid = htobe64(pStb->uid);
S
Shengliang Guan 已提交
1283 1284

    for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
D
dapan 已提交
1285
      SSchema *pSchema = &pRsp->pSchema[i];
S
Shengliang Guan 已提交
1286
      SSchema *pSrcSchema = &pStb->pColumns[i];
D
dapan 已提交
1287 1288 1289 1290 1291
      memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
      pSchema->type = pSrcSchema->type;
      pSchema->colId = htonl(pSrcSchema->colId);
      pSchema->bytes = htonl(pSrcSchema->bytes);
    }
S
Shengliang Guan 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301

    for (int32_t i = 0; i < pStb->numOfTags; ++i) {
      SSchema *pSchema = &pRsp->pSchema[i + pStb->numOfColumns];
      SSchema *pSrcSchema = &pStb->pTags[i];
      memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
      pSchema->type = pSrcSchema->type;
      pSchema->colId = htonl(pSrcSchema->colId);
      pSchema->bytes = htonl(pSrcSchema->bytes);
    }

D
dapan 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
    taosRUnLockLatch(&pStb->lock);
    mndReleaseDb(pMnode, pDb);
    mndReleaseStb(pMnode, pStb);
  }

  if (contLen > 0) {
    *rsp = buf;
    *rspLen = contLen;
  } else {
    *rsp = NULL;
    tfree(buf);
    *rspLen = 0;
  }

  return 0;
}

S
Shengliang Guan 已提交
1319
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1320 1321 1322 1323 1324 1325 1326 1327
  SSdb *pSdb = pMnode->pSdb;

  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1328
  int32_t numOfStbs = 0;
1329
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1330
  while (1) {
S
Shengliang Guan 已提交
1331
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1332
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1333 1334
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1335
    if (strcmp(pStb->db, dbName) == 0) {
S
Shengliang Guan 已提交
1336
      numOfStbs++;
S
Shengliang Guan 已提交
1337 1338
    }

S
Shengliang Guan 已提交
1339
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1340 1341
  }

S
Shengliang Guan 已提交
1342
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1343 1344 1345
  return 0;
}

S
Shengliang Guan 已提交
1346 1347
static int32_t mndGetStbMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
  SMnode *pMnode = pReq->pMnode;
1348
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1349

S
Shengliang Guan 已提交
1350
  if (mndGetNumOfStbs(pMnode, pShow->db, &pShow->numOfRows) != 0) {
S
Shengliang Guan 已提交
1351 1352 1353 1354
    return -1;
  }

  int32_t  cols = 0;
S
Shengliang Guan 已提交
1355
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
1356 1357 1358 1359

  pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
H
Haojun Liao 已提交
1360
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
1361 1362 1363 1364
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
H
Haojun Liao 已提交
1365 1366
  strcpy(pSchema[cols].name, "create_time");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
1367 1368
  cols++;

S
Shengliang Guan 已提交
1369 1370
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
1371
  strcpy(pSchema[cols].name, "columns");
H
Haojun Liao 已提交
1372
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
1373 1374
  cols++;

S
Shengliang Guan 已提交
1375 1376
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
1377
  strcpy(pSchema[cols].name, "tags");
H
Haojun Liao 已提交
1378
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
1379 1380
  cols++;

S
Shengliang Guan 已提交
1381
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
1382 1383 1384 1385 1386 1387 1388
  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 已提交
1389
  pShow->numOfRows = sdbGetSize(pSdb, SDB_STB);
S
Shengliang Guan 已提交
1390
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
D
dapan1121 已提交
1391
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
1392 1393 1394 1395

  return 0;
}

S
Shengliang Guan 已提交
1396
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
1397 1398
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  for (pos = 0; tableId[pos] != 0; ++pos) {
    if (tableId[pos] == '.') num++;
    if (num == 2) break;
  }

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

S
Shengliang Guan 已提交
1409 1410
static int32_t mndRetrieveStb(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode  *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
1411
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1412 1413 1414
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1415
  char    *pWrite;
S
Shengliang Guan 已提交
1416
  char     prefix[TSDB_DB_FNAME_LEN] = {0};
S
Shengliang Guan 已提交
1417

S
Shengliang Guan 已提交
1418
  SDbObj *pDb = mndAcquireDb(pMnode, pShow->db);
S
Shengliang Guan 已提交
1419
  if (pDb == NULL) return 0;
S
Shengliang Guan 已提交
1420

S
Shengliang Guan 已提交
1421
  tstrncpy(prefix, pShow->db, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
1422 1423 1424 1425
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = (int32_t)strlen(prefix);

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

S
Shengliang Guan 已提交
1429
    if (pStb->dbUid != pDb->uid) {
H
Haojun Liao 已提交
1430
      if (strncmp(pStb->db, pDb->name, tListLen(pStb->db)) == 0) {
S
Shengliang Guan 已提交
1431
        mError("Inconsistent table data, name:%s, db:%s, dbUid:%" PRIu64, pStb->name, pDb->name, pDb->uid);
H
Haojun Liao 已提交
1432 1433
      }

S
Shengliang Guan 已提交
1434
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1435 1436 1437 1438 1439
      continue;
    }

    cols = 0;

S
Shengliang Guan 已提交
1440 1441
    char stbName[TSDB_TABLE_NAME_LEN] = {0};
    tstrncpy(stbName, pStb->name + prefixLen, TSDB_TABLE_NAME_LEN);
S
Shengliang Guan 已提交
1442
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1443
    STR_TO_VARSTR(pWrite, stbName);
S
Shengliang Guan 已提交
1444 1445 1446
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1447
    *(int64_t *)pWrite = pStb->createdTime;
S
Shengliang Guan 已提交
1448 1449 1450
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1451
    *(int32_t *)pWrite = pStb->numOfColumns;
S
Shengliang Guan 已提交
1452 1453 1454
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1455
    *(int32_t *)pWrite = pStb->numOfTags;
S
Shengliang Guan 已提交
1456 1457 1458
    cols++;

    numOfRows++;
S
Shengliang Guan 已提交
1459
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1460 1461
  }

S
Shengliang Guan 已提交
1462
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1463
  pShow->numOfReads += numOfRows;
S
Shengliang Guan 已提交
1464
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
1465 1466 1467
  return numOfRows;
}

S
Shengliang Guan 已提交
1468
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1469 1470
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
L
Liu Jicong 已提交
1471
}
D
dapan1121 已提交
1472