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

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

S
Shengliang Guan 已提交
31 32
#define STB_VER_NUMBER   1
#define STB_RESERVE_SIZE 64
S
Shengliang Guan 已提交
33 34 35 36

static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw);
static int32_t  mndStbActionInsert(SSdb *pSdb, SStbObj *pStb);
static int32_t  mndStbActionDelete(SSdb *pSdb, SStbObj *pStb);
S
Shengliang Guan 已提交
37
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44
static int32_t  mndProcessMCreateStbReq(SNodeMsg *pReq);
static int32_t  mndProcessMAlterStbReq(SNodeMsg *pReq);
static int32_t  mndProcessMDropStbReq(SNodeMsg *pReq);
static int32_t  mndProcessVCreateStbRsp(SNodeMsg *pRsp);
static int32_t  mndProcessVAlterStbRsp(SNodeMsg *pRsp);
static int32_t  mndProcessVDropStbRsp(SNodeMsg *pRsp);
static int32_t  mndProcessTableMetaReq(SNodeMsg *pReq);
H
Hongze Cheng 已提交
45
static int32_t  mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
46 47 48
static void     mndCancelGetNextStb(SMnode *pMnode, void *pIter);

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

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

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

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

S
Shengliang Guan 已提交
73
void mndCleanupStb(SMnode *pMnode) {}
S
Shengliang Guan 已提交
74

75
SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
76 77
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

  int32_t dataPos = 0;
84 85 86 87 88 89 90 91 92 93 94 95 96 97
  SDB_SET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->updateTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->uid, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->dbUid, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->version, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, _OVER)
  SDB_SET_INT32(pRaw, dataPos, (int32_t)(pStb->xFilesFactor * 10000), _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->delay, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ttl, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->commentLen, _OVER)
98 99
  SDB_SET_INT32(pRaw, dataPos, pStb->ast1Len, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101 102
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
103
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
104
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
105 106 107
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
108 109 110 111
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
112
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
113
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
114 115 116
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
117 118
  }

S
sma  
Shengliang Guan 已提交
119
  if (pStb->commentLen > 0) {
120
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, _OVER)
S
sma  
Shengliang Guan 已提交
121
  }
122 123 124 125 126 127
  if (pStb->ast1Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
  }
  if (pStb->ast2Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
  }
S
Shengliang Guan 已提交
128
  SDB_SET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
129
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
130 131 132

  terrno = 0;

133
_OVER:
134 135 136 137 138
  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 已提交
139

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

S
Shengliang Guan 已提交
144
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
145 146
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
147
  int8_t sver = 0;
148
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
149

S
Shengliang Guan 已提交
150
  if (sver != STB_VER_NUMBER) {
S
Shengliang Guan 已提交
151
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
152
    goto _OVER;
S
Shengliang Guan 已提交
153 154
  }

S
Shengliang 已提交
155
  SSdbRow *pRow = sdbAllocRow(sizeof(SStbObj));
156
  if (pRow == NULL) goto _OVER;
157

S
Shengliang Guan 已提交
158
  SStbObj *pStb = sdbGetRowObj(pRow);
159
  if (pStb == NULL) goto _OVER;
S
Shengliang Guan 已提交
160 161

  int32_t dataPos = 0;
162 163 164 165 166 167 168 169
  SDB_GET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->updateTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->uid, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->dbUid, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->version, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, _OVER)
S
sma  
Shengliang Guan 已提交
170
  int32_t xFilesFactor = 0;
171
  SDB_GET_INT32(pRaw, dataPos, &xFilesFactor, _OVER)
S
sma  
Shengliang Guan 已提交
172
  pStb->xFilesFactor = xFilesFactor / 10000.0f;
173 174 175 176 177
  SDB_GET_INT32(pRaw, dataPos, &pStb->delay, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ttl, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->commentLen, _OVER)
178 179
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
180

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

S
Shengliang Guan 已提交
187 188
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
189
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
190
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
191 192 193
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
194 195 196 197
  }

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

S
sma  
Shengliang Guan 已提交
205
  if (pStb->commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
206
    pStb->comment = taosMemoryCalloc(pStb->commentLen, 1);
207 208
    if (pStb->comment == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, _OVER)
S
sma  
Shengliang Guan 已提交
209
  }
210 211 212 213 214 215 216 217 218 219
  if (pStb->ast1Len > 0) {
    pStb->pAst1 = taosMemoryCalloc(pStb->ast1Len, 1);
    if (pStb->pAst1 == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
  }
  if (pStb->ast2Len > 0) {
    pStb->pAst2 = taosMemoryCalloc(pStb->ast2Len, 1);
    if (pStb->pAst2 == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
  }
S
Shengliang Guan 已提交
220
  SDB_GET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
221 222 223

  terrno = 0;

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

234
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
235 236 237
  return pRow;
}

S
Shengliang Guan 已提交
238
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
239
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
240 241 242
  return 0;
}

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

S
Shengliang Guan 已提交
253 254
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 已提交
255

S
Shengliang Guan 已提交
256
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
257 258

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
259
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
260
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
261
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
262
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
263 264 265 266 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);
    }
  }

  if (pOld->numOfTags < pNew->numOfTags) {
wafwerar's avatar
wafwerar 已提交
271
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
272
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
273
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
274
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
275 276 277 278
    } 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 已提交
279
    }
S
Shengliang Guan 已提交
280 281
  }

S
Shengliang Guan 已提交
282
  if (pOld->commentLen < pNew->commentLen) {
wafwerar's avatar
wafwerar 已提交
283
    void *comment = taosMemoryMalloc(pNew->commentLen);
S
Shengliang Guan 已提交
284
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
285
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
286 287 288 289 290 291 292 293
      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);
    }
  }

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
  if (pOld->ast1Len < pNew->ast1Len) {
    void *pAst1 = taosMemoryMalloc(pNew->ast1Len);
    if (pAst1 != NULL) {
      taosMemoryFree(pOld->pAst1);
      pOld->pAst1 = pAst1;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

  if (pOld->ast2Len < pNew->ast2Len) {
    void *pAst2 = taosMemoryMalloc(pNew->ast2Len);
    if (pAst2 != NULL) {
      taosMemoryFree(pOld->pAst2);
      pOld->pAst2 = pAst2;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

S
Shengliang Guan 已提交
318 319
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
S
Shengliang Guan 已提交
320
  pOld->nextColId = pNew->nextColId;
S
Shengliang 已提交
321
  pOld->ttl = pNew->ttl;
S
Shengliang Guan 已提交
322 323
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
324 325
  memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
326
  if (pNew->commentLen != 0) {
S
Shengliang Guan 已提交
327
    memcpy(pOld->comment, pNew->comment, pNew->commentLen);
S
Shengliang Guan 已提交
328
  }
329 330 331 332 333 334
  if (pNew->ast1Len != 0) {
    memcpy(pOld->pAst1, pNew->pAst1, pNew->ast1Len);
  }
  if (pNew->ast2Len != 0) {
    memcpy(pOld->pAst2, pNew->pAst2, pNew->ast2Len);
  }
S
Shengliang Guan 已提交
335
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
336 337 338
  return 0;
}

S
Shengliang Guan 已提交
339
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
340
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
341
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
342
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
343 344 345
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
346 347
}

S
Shengliang Guan 已提交
348
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
349 350 351 352
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

353
SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
354 355
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
356

S
Shengliang Guan 已提交
357 358
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
359

S
Shengliang Guan 已提交
360 361
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
362

C
Cary Xu 已提交
363
static FORCE_INLINE int schemaExColIdCompare(const void *colId, const void *pSchema) {
H
Hongze Cheng 已提交
364
  if (*(col_id_t *)colId < ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
365
    return -1;
H
Hongze Cheng 已提交
366
  } else if (*(col_id_t *)colId > ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
367 368 369 370 371
    return 1;
  }
  return 0;
}

S
Shengliang Guan 已提交
372
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
373
  SEncoder       encoder = {0};
H
Hongze Cheng 已提交
374 375 376
  int32_t        contLen;
  SName          name = {0};
  SVCreateStbReq req = {0};
H
Hongze Cheng 已提交
377

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

S
Shengliang Guan 已提交
382
  req.name = (char *)tNameGetTableName(&name);
H
Hongze Cheng 已提交
383
  req.suid = pStb->uid;
384
  req.rollup = pStb->ast1Len > 0 ? 1 : 0;
H
Hongze Cheng 已提交
385
  req.schema.nCols = pStb->numOfColumns;
H
Hongze Cheng 已提交
386
  req.schema.sver = pStb->version;
H
Hongze Cheng 已提交
387 388
  req.schema.pSchema = pStb->pColumns;
  req.schemaTag.nCols = pStb->numOfTags;
H
Hongze Cheng 已提交
389
  req.schemaTag.nCols = 0;
H
Hongze Cheng 已提交
390
  req.schemaTag.pSchema = pStb->pTags;
H
more  
Hongze Cheng 已提交
391

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

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

wafwerar's avatar
wafwerar 已提交
415
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
416
  if (pHead == NULL) {
417 418
    taosMemoryFreeClear(req.pRSmaParam.qmsg1);
    taosMemoryFreeClear(req.pRSmaParam.qmsg2);
H
more  
Hongze Cheng 已提交
419 420 421 422
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
423 424
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
425

S
Shengliang Guan 已提交
426
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
Hongze Cheng 已提交
427 428
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  if (tEncodeSVCreateStbReq(&encoder, &req) < 0) {
429 430 431 432
    taosMemoryFreeClear(pHead);
    taosMemoryFreeClear(req.pRSmaParam.qmsg1);
    taosMemoryFreeClear(req.pRSmaParam.qmsg2);
    tEncoderClear(&encoder);
H
Hongze Cheng 已提交
433 434
    return NULL;
  }
H
Hongze Cheng 已提交
435
  tEncoderClear(&encoder);
H
more  
Hongze Cheng 已提交
436

S
Shengliang Guan 已提交
437
  *pContLen = contLen;
438 439
  taosMemoryFreeClear(req.pRSmaParam.qmsg1);
  taosMemoryFreeClear(req.pRSmaParam.qmsg2);
S
Shengliang Guan 已提交
440
  return pHead;
441 442
}

S
Shengliang Guan 已提交
443
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
444 445 446 447 448
  SName        name = {0};
  SVDropStbReq req = {0};
  int32_t      contLen = 0;
  int32_t      ret = 0;
  SMsgHead    *pHead = NULL;
H
Hongze Cheng 已提交
449
  SEncoder     encoder = {0};
H
Hongze Cheng 已提交
450

S
Shengliang Guan 已提交
451 452 453 454
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

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

H
Hongze Cheng 已提交
456 457 458 459 460
  tEncodeSize(tEncodeSVDropStbReq, &req, contLen, ret);
  if (ret < 0) return NULL;

  contLen += sizeof(SMsgHead);
  pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
461
  if (pHead == NULL) {
462 463 464 465
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
466 467 468 469
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
Hongze Cheng 已提交
470

H
Hongze Cheng 已提交
471 472 473
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  tEncodeSVDropStbReq(&encoder, &req);
  tEncoderClear(&encoder);
474

S
Shengliang Guan 已提交
475 476
  *pContLen = contLen;
  return pHead;
477 478
}

479
int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
480
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
481
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
482 483
    return -1;
  }
S
Shengliang Guan 已提交
484

S
Shengliang Guan 已提交
485
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
486
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
487 488
    return -1;
  }
S
Shengliang Guan 已提交
489

S
Shengliang Guan 已提交
490
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
491
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
492 493
    return -1;
  }
S
Shengliang Guan 已提交
494

S
Shengliang Guan 已提交
495
  SField *pField = taosArrayGet(pCreate->pColumns, 0);
S
Shengliang Guan 已提交
496
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
S
Shengliang Guan 已提交
497 498 499 500
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

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

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
518 519
    SField *pField1 = taosArrayGet(pCreate->pTags, i);
    if (pField1->type < 0) {
S
Shengliang Guan 已提交
520
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
521 522
      return -1;
    }
523
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
524
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
525 526
      return -1;
    }
527
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
528
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
529 530 531
      return -1;
    }
  }
S
Shengliang Guan 已提交
532

S
Shengliang Guan 已提交
533 534 535
  return 0;
}

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

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

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

563
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
564
  SSdb   *pSdb = pMnode->pSdb;
565
  SVgObj *pVgroup = NULL;
566
  void   *pIter = NULL;
S
Shengliang Guan 已提交
567
  int32_t contLen;
568 569 570 571

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
572 573 574 575
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
576

S
Shengliang Guan 已提交
577
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
578
    if (pReq == NULL) {
579 580 581 582
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
S
Shengliang Guan 已提交
583

584 585
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
586
    action.pCont = pReq;
S
Shengliang Guan 已提交
587
    action.contLen = contLen;
H
Hongze Cheng 已提交
588
    action.msgType = TDMT_VND_CREATE_STB;
S
Shengliang Guan 已提交
589
    action.acceptableCode = TSDB_CODE_TDB_STB_ALREADY_EXIST;
590
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
591
      taosMemoryFree(pReq);
592 593 594 595 596 597
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
598 599 600 601

  return 0;
}

602
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
603
  SSdb   *pSdb = pMnode->pSdb;
604
  SVgObj *pVgroup = NULL;
605
  void   *pIter = NULL;
606 607 608 609

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
610 611 612 613
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
614

S
Shengliang Guan 已提交
615
    int32_t contLen = 0;
S
Shengliang Guan 已提交
616
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
617
    if (pReq == NULL) {
618 619 620 621 622
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
623

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

  return 0;
}

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

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
int32_t mndBuildStbFromReq(SMnode *pMnode, SStbObj *pDst, SMCreateStbReq *pCreate, SDbObj *pDb) {
  memcpy(pDst->name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(pDst->db, pDb->name, TSDB_DB_FNAME_LEN);
  pDst->createdTime = taosGetTimestampMs();
  pDst->updateTime = pDst->createdTime;
  pDst->uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
  pDst->dbUid = pDb->uid;
  pDst->version = 1;
  pDst->nextColId = 1;
  pDst->xFilesFactor = pCreate->xFilesFactor;
  pDst->delay = pCreate->delay;
  pDst->ttl = pCreate->ttl;
  pDst->numOfColumns = pCreate->numOfColumns;
  pDst->numOfTags = pCreate->numOfTags;
  pDst->commentLen = pCreate->commentLen;
  if (pDst->commentLen > 0) {
    pDst->comment = taosMemoryCalloc(pDst->commentLen, 1);
    if (pDst->comment == NULL) {
S
sma  
Shengliang Guan 已提交
670 671 672
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
673
    memcpy(pDst->comment, pCreate->comment, pDst->commentLen);
S
sma  
Shengliang Guan 已提交
674
  }
S
Shengliang Guan 已提交
675

676 677 678 679
  pDst->ast1Len = pCreate->ast1Len;
  if (pDst->ast1Len > 0) {
    pDst->pAst1 = taosMemoryCalloc(pDst->ast1Len, 1);
    if (pDst->pAst1 == NULL) {
680 681 682
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
683
    memcpy(pDst->pAst1, pCreate->pAst1, pDst->ast1Len);
684 685
  }

686 687 688 689
  pDst->ast2Len = pCreate->ast2Len;
  if (pDst->ast2Len > 0) {
    pDst->pAst2 = taosMemoryCalloc(pDst->ast2Len, 1);
    if (pDst->pAst2 == NULL) {
690 691 692
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
693
    memcpy(pDst->pAst2, pCreate->pAst2, pDst->ast2Len);
694 695
  }

696 697 698
  pDst->pColumns = taosMemoryCalloc(1, pDst->numOfColumns * sizeof(SSchema));
  pDst->pTags = taosMemoryCalloc(1, pDst->numOfTags * sizeof(SSchema));
  if (pDst->pColumns == NULL || pDst->pTags == NULL) {
S
Shengliang Guan 已提交
699 700 701
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
702

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

714
  for (int32_t i = 0; i < pDst->numOfTags; ++i) {
S
Shengliang Guan 已提交
715
    SField  *pField = taosArrayGet(pCreate->pTags, i);
716
    SSchema *pSchema = &pDst->pTags[i];
S
Shengliang Guan 已提交
717 718 719
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
720 721
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
722
  }
723 724 725 726 727
  return 0;
}

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

S
Shengliang 已提交
729
  int32_t code = -1;
730

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

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

736 737 738 739 740 741
  if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) {
    goto _OVER;
  }

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

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

S
Shengliang Guan 已提交
744 745
  code = 0;

746
_OVER:
S
Shengliang Guan 已提交
747
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
748
  return code;
S
Shengliang Guan 已提交
749
}
S
Shengliang Guan 已提交
750

751 752 753 754 755 756 757 758 759
int32_t mndAddStbToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  mndTransSetDbInfo(pTrans, pDb);
  if (mndSetCreateStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbUndoLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbUndoActions(pMnode, pTrans, pDb, pStb) != 0) return -1;
  return 0;
}
S
Shengliang Guan 已提交
760

S
Shengliang Guan 已提交
761 762
static int32_t mndProcessMCreateStbReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
763 764 765
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
S
Shengliang Guan 已提交
766
  SUserObj      *pUser = NULL;
S
Shengliang Guan 已提交
767
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
768

S
Shengliang Guan 已提交
769 770
  if (tDeserializeSMCreateStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
771
    goto _OVER;
S
Shengliang Guan 已提交
772
  }
S
Shengliang Guan 已提交
773

S
Shengliang Guan 已提交
774
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
775 776
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
777
    goto _OVER;
S
Shengliang Guan 已提交
778
  }
S
Shengliang Guan 已提交
779

S
Shengliang Guan 已提交
780
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
781
  if (pStb != NULL) {
S
Shengliang Guan 已提交
782 783 784
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
785
      goto _OVER;
S
Shengliang Guan 已提交
786 787
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
788
      goto _OVER;
S
Shengliang Guan 已提交
789
    }
S
Shengliang Guan 已提交
790
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
791
    goto _OVER;
S
Shengliang Guan 已提交
792 793
  }

S
Shengliang Guan 已提交
794
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
795 796
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
797
    goto _OVER;
S
Shengliang Guan 已提交
798 799
  }

S
Shengliang Guan 已提交
800 801
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
802
    goto _OVER;
S
Shengliang Guan 已提交
803 804 805
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
806
    goto _OVER;
S
Shengliang Guan 已提交
807 808
  }

809
  int32_t numOfStbs = -1;
S
Shengliang Guan 已提交
810 811 812 813
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
    goto _OVER;
  }

L
Liu Jicong 已提交
814
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
815 816 817 818
    terrno = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
    goto _OVER;
  }

S
Shengliang Guan 已提交
819
  code = mndCreateStb(pMnode, pReq, &createReq, pDb);
820
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
821

822
_OVER:
823
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
824
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
825 826
  }

S
Shengliang Guan 已提交
827 828
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
829 830
  mndReleaseUser(pMnode, pUser);
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
831 832

  return code;
S
Shengliang Guan 已提交
833 834
}

S
Shengliang Guan 已提交
835
static int32_t mndProcessVCreateStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
836
  mndTransProcessRsp(pRsp);
837 838
  return 0;
}
S
Shengliang Guan 已提交
839

S
Shengliang Guan 已提交
840
static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) {
S
Shengliang 已提交
841
  if (pAlter->commentLen != 0 || pAlter->ttl != 0) return 0;
S
Shengliang 已提交
842

S
Shengliang Guan 已提交
843 844 845 846
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
847

S
Shengliang Guan 已提交
848 849 850
  for (int32_t i = 0; i < pAlter->numOfFields; ++i) {
    SField *pField = taosArrayGet(pAlter->pFields, i);
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
851 852 853
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
854 855 856 857 858
  }

  return 0;
}

S
Shengliang Guan 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) {
  for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
    if (strcasecmp(pStb->pTags[tag].name, tagName) == 0) {
      return tag;
    }
  }

  return -1;
}

static int32_t mndFindSuperTableColumnIndex(const SStbObj *pStb, const char *colName) {
  for (int32_t col = 0; col < pStb->numOfColumns; col++) {
    if (strcasecmp(pStb->pColumns[col].name, colName) == 0) {
      return col;
    }
  }

  return -1;
}

static int32_t mndAllocStbSchemas(const SStbObj *pOld, SStbObj *pNew) {
wafwerar's avatar
wafwerar 已提交
880 881
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
882 883 884 885 886 887 888 889 890 891
  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 已提交
892 893
static int32_t mndUpdateStbCommentAndTTL(const SStbObj *pOld, SStbObj *pNew, char *pComment, int32_t commentLen,
                                         int32_t ttl) {
S
Shengliang 已提交
894 895 896 897 898 899 900 901 902
  if (commentLen > 0) {
    pNew->commentLen = commentLen;
    pNew->comment = taosMemoryCalloc(1, commentLen);
    if (pNew->comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    memcpy(pNew->comment, pComment, commentLen);
  }
S
Shengliang 已提交
903 904 905
  if (ttl >= 0) {
    pNew->ttl = ttl;
  }
S
Shengliang 已提交
906 907 908 909 910 911 912

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

S
Shengliang Guan 已提交
913
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
914 915 916 917 918 919 920 921 922 923
  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 已提交
924 925 926 927 928
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
929
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
930
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
931
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
932
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
933 934 935
      return -1;
    }

S
Shengliang 已提交
936
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
937
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
938 939 940
      return -1;
    }

S
Shengliang Guan 已提交
941 942 943 944
    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 已提交
945 946
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
947 948

    mDebug("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
  }

  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 已提交
967
  pNew->numOfTags--;
S
Shengliang Guan 已提交
968 969 970 971 972 973

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

S
Shengliang Guan 已提交
974 975 976 977 978 979 980 981 982 983 984 985
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 已提交
986 987 988 989 990 991 992
  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 已提交
993
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
994 995 996
    return -1;
  }

S
Shengliang Guan 已提交
997 998
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    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 已提交
1014 1015
static int32_t mndAlterStbTagBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
  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 已提交
1027 1028 1029 1030 1031
  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 已提交
1032
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1033 1034 1035 1036
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1037
  pTag->bytes = pField->bytes;
S
Shengliang Guan 已提交
1038 1039
  pNew->version++;

S
Shengliang Guan 已提交
1040
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1041 1042 1043
  return 0;
}

S
Shengliang Guan 已提交
1044
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1045 1046 1047 1048 1049
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1050 1051 1052 1053 1054
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1055
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1056
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1057
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1058
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1059 1060 1061
      return -1;
    }

S
Shengliang 已提交
1062
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1063
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1064 1065 1066
      return -1;
    }

S
Shengliang Guan 已提交
1067 1068 1069 1070
    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 已提交
1071 1072
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1073 1074

    mDebug("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1075 1076 1077 1078 1079 1080 1081 1082
  }

  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 已提交
1083
  if (col < 0) {
S
Shengliang Guan 已提交
1084 1085 1086 1087
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
  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 已提交
1098 1099 1100 1101 1102
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
S
Shengliang Guan 已提交
1103
  pNew->numOfColumns--;
S
Shengliang Guan 已提交
1104 1105 1106 1107 1108 1109

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

S
Shengliang Guan 已提交
1110 1111
static int32_t mndAlterStbColumnBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1112 1113 1114 1115 1116 1117 1118
  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 已提交
1119
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
  }

  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 已提交
1132 1133
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1134 1135 1136
    return -1;
  }

S
Shengliang Guan 已提交
1137
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1138
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1139 1140 1141
    return -1;
  }

S
Shengliang Guan 已提交
1142
  pCol->bytes = pField->bytes;
S
Shengliang Guan 已提交
1143 1144
  pNew->version++;

S
Shengliang Guan 已提交
1145
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1146 1147 1148
  return 0;
}

S
Shengliang Guan 已提交
1149
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1150 1151 1152
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
S
Shengliang Guan 已提交
1153
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
S
Shengliang Guan 已提交
1154 1155 1156 1157

  return 0;
}

S
Shengliang Guan 已提交
1158
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1159 1160 1161 1162 1163 1164 1165 1166
  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 已提交
1167
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1168 1169 1170 1171 1172 1173 1174 1175
  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 已提交
1176 1177 1178 1179
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1180

S
Shengliang Guan 已提交
1181
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    if (pReq == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }

    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pReq;
    action.contLen = contLen;
S
Shengliang Guan 已提交
1192
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1193
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1194
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1205
static int32_t mndAlterStb(SMnode *pMnode, SNodeMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
S
Shengliang Guan 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214
  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 已提交
1215
  STrans *pTrans = NULL;
S
Shengliang 已提交
1216
  SField *pField0 = NULL;
S
Shengliang Guan 已提交
1217

S
Shengliang Guan 已提交
1218
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
1219
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
1220
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1221
      break;
S
Shengliang Guan 已提交
1222
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang 已提交
1223
      pField0 = taosArrayGet(pAlter->pFields, 0);
S
Shengliang Guan 已提交
1224
      code = mndDropSuperTableTag(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1225
      break;
S
Shengliang Guan 已提交
1226
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
S
Shengliang Guan 已提交
1227
      code = mndAlterStbTagName(pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
1228
      break;
S
Shengliang Guan 已提交
1229
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang 已提交
1230
      pField0 = taosArrayGet(pAlter->pFields, 0);
S
Shengliang Guan 已提交
1231
      code = mndAlterStbTagBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1232 1233
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
1234
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1235 1236
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang 已提交
1237
      pField0 = taosArrayGet(pAlter->pFields, 0);
S
Shengliang Guan 已提交
1238
      code = mndDropSuperTableColumn(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1239
      break;
S
Shengliang Guan 已提交
1240
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang 已提交
1241
      pField0 = taosArrayGet(pAlter->pFields, 0);
S
Shengliang Guan 已提交
1242
      code = mndAlterStbColumnBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1243
      break;
S
Shengliang 已提交
1244
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
S
Shengliang 已提交
1245
      code = mndUpdateStbCommentAndTTL(pOld, &stbObj, pAlter->comment, pAlter->commentLen, pAlter->ttl);
S
Shengliang 已提交
1246
      break;
S
Shengliang Guan 已提交
1247
    default:
S
Shengliang 已提交
1248
      terrno = TSDB_CODE_OPS_NOT_SUPPORT;
S
Shengliang Guan 已提交
1249 1250 1251
      break;
  }

1252
  if (code != 0) goto _OVER;
S
Shengliang Guan 已提交
1253 1254

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

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

1261 1262 1263 1264
  if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
1265 1266

  code = 0;
S
Shengliang Guan 已提交
1267

1268
_OVER:
S
Shengliang Guan 已提交
1269
  mndTransDrop(pTrans);
wafwerar's avatar
wafwerar 已提交
1270 1271
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
S
Shengliang Guan 已提交
1272 1273
  return code;
}
S
Shengliang Guan 已提交
1274

S
Shengliang Guan 已提交
1275
static int32_t mndProcessMAlterStbReq(SNodeMsg *pReq) {
S
Shengliang Guan 已提交
1276 1277 1278 1279 1280 1281
  SMnode       *pMnode = pReq->pNode;
  int32_t       code = -1;
  SDbObj       *pDb = NULL;
  SStbObj      *pStb = NULL;
  SUserObj     *pUser = NULL;
  SMAlterStbReq alterReq = {0};
S
Shengliang Guan 已提交
1282

S
Shengliang Guan 已提交
1283
  if (tDeserializeSMAlterStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1284
    terrno = TSDB_CODE_INVALID_MSG;
1285
    goto _OVER;
S
Shengliang Guan 已提交
1286
  }
S
Shengliang Guan 已提交
1287

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

S
Shengliang Guan 已提交
1291
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1292 1293
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
1294
    goto _OVER;
S
Shengliang Guan 已提交
1295 1296
  }

S
Shengliang Guan 已提交
1297
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1298 1299
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1300
    goto _OVER;
S
Shengliang Guan 已提交
1301
  }
S
Shengliang Guan 已提交
1302

S
Shengliang Guan 已提交
1303 1304
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
1305
    goto _OVER;
S
Shengliang Guan 已提交
1306 1307 1308
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1309
    goto _OVER;
S
Shengliang Guan 已提交
1310 1311
  }

S
Shengliang Guan 已提交
1312
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
1313
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1314

1315
_OVER:
1316
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1317
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1318 1319
  }

S
Shengliang Guan 已提交
1320 1321
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1322
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
1323
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1324 1325

  return code;
S
Shengliang Guan 已提交
1326
}
S
Shengliang Guan 已提交
1327

S
Shengliang Guan 已提交
1328
static int32_t mndProcessVAlterStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1329
  mndTransProcessRsp(pRsp);
1330 1331
  return 0;
}
S
Shengliang Guan 已提交
1332

S
Shengliang Guan 已提交
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
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 已提交
1351 1352 1353 1354
static int32_t mndSetDropStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1355

S
Shengliang Guan 已提交
1356 1357 1358
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1359 1360 1361 1362
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1363

S
Shengliang Guan 已提交
1364
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1365
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
    if (pReq == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pReq;
    action.contLen = contLen;
    action.msgType = TDMT_VND_DROP_STB;
    action.acceptableCode = TSDB_CODE_VND_TB_NOT_EXIST;
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1380
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1391
static int32_t mndDropStb(SMnode *pMnode, SNodeMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1392
  int32_t code = -1;
S
Shengliang Guan 已提交
1393
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_STB, &pReq->rpcMsg);
1394
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1395

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

1399 1400 1401 1402
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
  if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
1403

S
Shengliang Guan 已提交
1404 1405
  code = 0;

1406
_OVER:
S
Shengliang Guan 已提交
1407
  mndTransDrop(pTrans);
S
Shengliang 已提交
1408
  return code;
S
Shengliang Guan 已提交
1409 1410
}

S
Shengliang Guan 已提交
1411 1412
static int32_t mndProcessMDropStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1413 1414 1415 1416
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1417
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
1418

S
Shengliang Guan 已提交
1419
  if (tDeserializeSMDropStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1420
    terrno = TSDB_CODE_INVALID_MSG;
1421
    goto _OVER;
S
Shengliang Guan 已提交
1422
  }
S
Shengliang Guan 已提交
1423

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

S
Shengliang Guan 已提交
1426
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1427
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1428 1429
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1430
      code = 0;
1431
      goto _OVER;
S
Shengliang Guan 已提交
1432 1433
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1434
      goto _OVER;
S
Shengliang Guan 已提交
1435 1436 1437
    }
  }

S
Shengliang Guan 已提交
1438
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1439 1440
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1441
    goto _OVER;
S
Shengliang Guan 已提交
1442 1443
  }

S
Shengliang Guan 已提交
1444 1445
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
1446
    goto _OVER;
S
Shengliang Guan 已提交
1447
  }
S
Shengliang Guan 已提交
1448

S
Shengliang Guan 已提交
1449
  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1450
    goto _OVER;
S
Shengliang Guan 已提交
1451 1452 1453 1454 1455
  }

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

1456
_OVER:
S
Shengliang Guan 已提交
1457
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1458
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1459 1460
  }

S
Shengliang Guan 已提交
1461 1462 1463 1464 1465
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
1466
}
S
Shengliang Guan 已提交
1467

S
Shengliang Guan 已提交
1468
static int32_t mndProcessVDropStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1469
  mndTransProcessRsp(pRsp);
1470 1471
  return 0;
}
S
Shengliang Guan 已提交
1472

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

S
Shengliang Guan 已提交
1476
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
wafwerar's avatar
wafwerar 已提交
1477
  pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
1478
  if (pRsp->pSchemas == NULL) {
S
Shengliang Guan 已提交
1479
    taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1480 1481 1482 1483
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
  strcpy(pRsp->dbFName, pStb->db);
  strcpy(pRsp->tbName, tbName);
  strcpy(pRsp->stbName, tbName);
  pRsp->dbId = pDb->uid;
  pRsp->numOfTags = pStb->numOfTags;
  pRsp->numOfColumns = pStb->numOfColumns;
  pRsp->precision = pDb->cfg.precision;
  pRsp->tableType = TSDB_SUPER_TABLE;
  pRsp->sversion = pStb->version;
  pRsp->suid = pStb->uid;
  pRsp->tuid = pStb->uid;
S
Shengliang Guan 已提交
1495

S
Shengliang Guan 已提交
1496
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1497
    SSchema *pSchema = &pRsp->pSchemas[i];
S
Shengliang Guan 已提交
1498 1499 1500
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1501 1502
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1503 1504 1505
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
S
Shengliang Guan 已提交
1506
    SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
S
Shengliang Guan 已提交
1507
    SSchema *pSrcSchema = &pStb->pTags[i];
S
Shengliang Guan 已提交
1508 1509
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1510 1511
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1512
  }
S
Shengliang Guan 已提交
1513

S
Shengliang Guan 已提交
1514
  taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1515
  return 0;
S
Shengliang Guan 已提交
1516
}
S
Shengliang Guan 已提交
1517

S
Shengliang Guan 已提交
1518 1519 1520
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 已提交
1521

S
Shengliang Guan 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
  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 已提交
1539
}
S
Shengliang Guan 已提交
1540

S
Shengliang Guan 已提交
1541 1542
static int32_t mndProcessTableMetaReq(SNodeMsg *pReq) {
  SMnode       *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1543 1544 1545
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
1546

S
Shengliang Guan 已提交
1547
  if (tDeserializeSTableInfoReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
1548
    terrno = TSDB_CODE_INVALID_MSG;
1549
    goto _OVER;
S
Shengliang Guan 已提交
1550
  }
D
dapan 已提交
1551

D
dapan1121 已提交
1552 1553 1554
  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) {
1555
      goto _OVER;
D
dapan1121 已提交
1556
    }
D
dapan1121 已提交
1557 1558 1559
  } else if (0 == strcmp(infoReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
    mDebug("performance_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildPerfsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
1560
      goto _OVER;
D
dapan1121 已提交
1561
    }
D
dapan1121 已提交
1562 1563 1564
  } else {
    mDebug("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
1565
      goto _OVER;
D
dapan1121 已提交
1566
    }
S
Shengliang Guan 已提交
1567
  }
S
Shengliang Guan 已提交
1568

S
Shengliang Guan 已提交
1569 1570 1571
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
1572
    goto _OVER;
S
Shengliang Guan 已提交
1573
  }
S
Shengliang Guan 已提交
1574

S
Shengliang Guan 已提交
1575
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1576 1577
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1578
    goto _OVER;
S
Shengliang Guan 已提交
1579
  }
D
dapan 已提交
1580

S
Shengliang Guan 已提交
1581
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1582 1583
  pReq->pRsp = pRsp;
  pReq->rspLen = rspLen;
S
Shengliang Guan 已提交
1584
  code = 0;
S
Shengliang Guan 已提交
1585

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

1588
_OVER:
S
Shengliang Guan 已提交
1589 1590 1591
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1592

S
Shengliang Guan 已提交
1593 1594 1595
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1596

S
Shengliang Guan 已提交
1597 1598 1599 1600 1601 1602 1603 1604
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 已提交
1605

S
Shengliang Guan 已提交
1606 1607 1608 1609 1610
  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 已提交
1611

S
Shengliang Guan 已提交
1612 1613 1614 1615 1616
    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 已提交
1617
    }
S
Shengliang Guan 已提交
1618

S
Shengliang Guan 已提交
1619 1620
    if (pStbVersion->sversion != metaRsp.sversion) {
      taosArrayPush(batchMetaRsp.pArray, &metaRsp);
S
Shengliang Guan 已提交
1621
    }
S
Shengliang Guan 已提交
1622
  }
S
Shengliang Guan 已提交
1623

S
Shengliang Guan 已提交
1624 1625 1626 1627 1628
  int32_t rspLen = tSerializeSTableMetaBatchRsp(NULL, 0, &batchMetaRsp);
  if (rspLen < 0) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
1629 1630
  }

wafwerar's avatar
wafwerar 已提交
1631
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
1632 1633 1634 1635
  if (pRsp == NULL) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
1636 1637
  }

S
Shengliang Guan 已提交
1638 1639 1640
  tSerializeSTableMetaBatchRsp(pRsp, rspLen, &batchMetaRsp);
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1641 1642 1643
  return 0;
}

1644
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1645
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1646 1647 1648 1649 1650 1651
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1652
  int32_t numOfStbs = 0;
1653
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1654
  while (1) {
S
Shengliang Guan 已提交
1655
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1656
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1657 1658
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1659
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1660
      numOfStbs++;
S
Shengliang Guan 已提交
1661 1662
    }

S
Shengliang Guan 已提交
1663
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1664 1665
  }

S
Shengliang Guan 已提交
1666
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1667
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1668 1669 1670
  return 0;
}

S
Shengliang Guan 已提交
1671
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
1672 1673
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
1674
  for (pos = 0; tableId[pos] != 0; ++pos) {
H
Haojun Liao 已提交
1675
    if (tableId[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
1676 1677 1678 1679 1680 1681 1682 1683
    if (num == 2) break;
  }

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

H
Hongze Cheng 已提交
1684
static int32_t mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
S
Shengliang Guan 已提交
1685
  SMnode  *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1686
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1687 1688 1689
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1690

H
Hongze Cheng 已提交
1691
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
1692 1693
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
1694
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
1695
  }
S
Shengliang Guan 已提交
1696

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

H
Haojun Liao 已提交
1701
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
1702
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1703 1704 1705 1706 1707
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
1708
    SName name = {0};
H
Hongze Cheng 已提交
1709
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1710 1711
    mndExtractTableName(pStb->name, &stbName[VARSTR_HEADER_SIZE]);
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
1712

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

H
Hongze Cheng 已提交
1716 1717
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
1718 1719 1720
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

1721
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
H
Hongze Cheng 已提交
1722
    colDataAppend(pColInfo, numOfRows, (const char *)db, false);
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->createdTime, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->numOfColumns, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->numOfTags, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->updateTime, false);  // number of tables

S
Shengliang 已提交
1736
    char *p = taosMemoryCalloc(1, pStb->commentLen + 1 + VARSTR_HEADER_SIZE);  // check malloc failures
1737 1738 1739 1740 1741 1742 1743
    if (p != NULL) {
      if (pStb->commentLen != 0) {
        STR_TO_VARSTR(p, pStb->comment);
      } else {
        STR_TO_VARSTR(p, "");
      }

1744
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1745 1746
      colDataAppend(pColInfo, numOfRows, (const char *)p, false);
      taosMemoryFree(p);
S
Shengliang Guan 已提交
1747
    }
H
Haojun Liao 已提交
1748

S
Shengliang Guan 已提交
1749
    numOfRows++;
S
Shengliang Guan 已提交
1750
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1751 1752
  }

H
Haojun Liao 已提交
1753 1754 1755 1756
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

1757
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1758 1759 1760
  return numOfRows;
}

S
Shengliang Guan 已提交
1761
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1762 1763
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
1764
}