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

S
Shengliang Guan 已提交
16
#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
#include "mndShow.h"
26
#include "mndSma.h"
27
#include "mndTopic.h"
S
Shengliang Guan 已提交
28 29
#include "mndTrans.h"
#include "mndUser.h"
30
#include "mndVgroup.h"
S
Shengliang Guan 已提交
31
#include "tname.h"
S
Shengliang Guan 已提交
32

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

static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw);
static int32_t  mndStbActionInsert(SSdb *pSdb, SStbObj *pStb);
static int32_t  mndStbActionDelete(SSdb *pSdb, SStbObj *pStb);
S
Shengliang Guan 已提交
39
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
40 41 42
static int32_t  mndProcessCreateStbReq(SRpcMsg *pReq);
static int32_t  mndProcessAlterStbReq(SRpcMsg *pReq);
static int32_t  mndProcessDropStbReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
43 44
static int32_t  mndProcessTableMetaReq(SRpcMsg *pReq);
static int32_t  mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
45 46 47
static void     mndCancelGetNextStb(SMnode *pMnode, void *pIter);

int32_t mndInitStb(SMnode *pMnode) {
S
Shengliang Guan 已提交
48 49 50 51 52 53 54 55 56
  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 已提交
57

58 59 60
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcessCreateStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcessAlterStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcessDropStbReq);
61 62 63
  mndSetMsgHandle(pMnode, TDMT_VND_CREATE_STB_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_ALTER_STB_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_STB_RSP, mndTransProcessRsp);
D
dapan1121 已提交
64
  mndSetMsgHandle(pMnode, TDMT_MND_TABLE_META, mndProcessTableMetaReq);
S
Shengliang Guan 已提交
65 66 67

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

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

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

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

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

  int32_t dataPos = 0;
83 84 85 86 87 88
  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)
89 90
  SDB_SET_INT32(pRaw, dataPos, pStb->tagVer, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->colVer, _OVER)
91 92 93 94 95 96 97
  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
  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)
168 169
  SDB_GET_INT32(pRaw, dataPos, &pStb->tagVer, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->colVer, _OVER)
170
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, _OVER)
S
sma  
Shengliang Guan 已提交
171
  int32_t xFilesFactor = 0;
172
  SDB_GET_INT32(pRaw, dataPos, &xFilesFactor, _OVER)
S
sma  
Shengliang Guan 已提交
173
  pStb->xFilesFactor = xFilesFactor / 10000.0f;
174 175 176 177 178
  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)
179 180
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
181

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

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

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
199
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
200
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
201 202 203
    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 已提交
204 205
  }

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

  terrno = 0;

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

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

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

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

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

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

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
260
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
261
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
262
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
263
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
264 265 266 267 268 269 270 271
    } 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 已提交
272
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
273
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
274
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
275
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
276 277 278 279
    } 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 已提交
280
    }
S
Shengliang Guan 已提交
281 282
  }

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

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  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 已提交
319
  pOld->updateTime = pNew->updateTime;
320 321
  pOld->tagVer = pNew->tagVer;
  pOld->colVer = pNew->colVer;
322
  pOld->smaVer = pNew->smaVer;
323
  pOld->nextColId = pNew->nextColId;
S
Shengliang 已提交
324
  pOld->ttl = pNew->ttl;
325 326 327 328 329 330 331 332
  if (pNew->numOfColumns > 0) {
    pOld->numOfColumns = pNew->numOfColumns;
    memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  }
  if (pNew->numOfTags > 0) {
    pOld->numOfTags = pNew->numOfTags;
    memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
  }
S
Shengliang Guan 已提交
333
  if (pNew->commentLen != 0) {
S
Shengliang Guan 已提交
334
    memcpy(pOld->comment, pNew->comment, pNew->commentLen);
S
Shengliang Guan 已提交
335
  }
336 337 338 339 340 341
  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 已提交
342
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
343 344 345
  return 0;
}

S
Shengliang Guan 已提交
346
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
347
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
348
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
349
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
350 351 352
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
353 354
}

S
Shengliang Guan 已提交
355
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
356 357 358 359
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

360
SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
361 362
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
363

S
Shengliang Guan 已提交
364 365
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
366

S
Shengliang Guan 已提交
367 368
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
369

370
static FORCE_INLINE int32_t schemaExColIdCompare(const void *colId, const void *pSchema) {
H
Hongze Cheng 已提交
371
  if (*(col_id_t *)colId < ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
372
    return -1;
H
Hongze Cheng 已提交
373
  } else if (*(col_id_t *)colId > ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
374 375 376 377 378
    return 1;
  }
  return 0;
}

S
Shengliang Guan 已提交
379
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
380
  SEncoder       encoder = {0};
H
Hongze Cheng 已提交
381 382 383
  int32_t        contLen;
  SName          name = {0};
  SVCreateStbReq req = {0};
H
Hongze Cheng 已提交
384

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

S
Shengliang Guan 已提交
389
  req.name = (char *)tNameGetTableName(&name);
H
Hongze Cheng 已提交
390
  req.suid = pStb->uid;
391
  req.rollup = pStb->ast1Len > 0 ? 1 : 0;
392 393
  // todo
  req.schemaRow.nCols = pStb->numOfColumns;
394
  req.schemaRow.version = pStb->colVer;
395
  req.schemaRow.pSchema = pStb->pColumns;
H
Hongze Cheng 已提交
396
  req.schemaTag.nCols = pStb->numOfTags;
397
  req.schemaTag.version = pStb->tagVer;
H
Hongze Cheng 已提交
398
  req.schemaTag.pSchema = pStb->pTags;
H
more  
Hongze Cheng 已提交
399

C
Cary Xu 已提交
400
  if (req.rollup) {
H
Hongze Cheng 已提交
401 402
    req.pRSmaParam.xFilesFactor = pStb->xFilesFactor;
    req.pRSmaParam.delay = pStb->delay;
C
Cary Xu 已提交
403
    if (pStb->ast1Len > 0) {
404 405
      if (mndConvertRsmaTask(&req.pRSmaParam.qmsg1, &req.pRSmaParam.qmsg1Len, pStb->pAst1, pStb->uid,
                             STREAM_TRIGGER_AT_ONCE, 0, req.pRSmaParam.xFilesFactor) != TSDB_CODE_SUCCESS) {
C
Cary Xu 已提交
406 407
        return NULL;
      }
C
Cary Xu 已提交
408 409
    }
    if (pStb->ast2Len > 0) {
410 411
      if (mndConvertRsmaTask(&req.pRSmaParam.qmsg2, &req.pRSmaParam.qmsg2Len, pStb->pAst2, pStb->uid,
                             STREAM_TRIGGER_AT_ONCE, 0, req.pRSmaParam.xFilesFactor) != TSDB_CODE_SUCCESS) {
C
Cary Xu 已提交
412 413
        return NULL;
      }
C
Cary Xu 已提交
414
    }
H
Hongze Cheng 已提交
415
  }
H
Hongze Cheng 已提交
416
  // get length
wafwerar's avatar
wafwerar 已提交
417 418 419
  int32_t ret = 0;
  tEncodeSize(tEncodeSVCreateStbReq, &req, contLen, ret);
  if (ret < 0) {
H
Hongze Cheng 已提交
420 421 422
    return NULL;
  }

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

wafwerar's avatar
wafwerar 已提交
425
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
426
  if (pHead == NULL) {
427 428
    taosMemoryFreeClear(req.pRSmaParam.qmsg1);
    taosMemoryFreeClear(req.pRSmaParam.qmsg2);
H
more  
Hongze Cheng 已提交
429 430 431 432
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
433 434
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
435

S
Shengliang Guan 已提交
436
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
Hongze Cheng 已提交
437 438
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  if (tEncodeSVCreateStbReq(&encoder, &req) < 0) {
439 440 441 442
    taosMemoryFreeClear(pHead);
    taosMemoryFreeClear(req.pRSmaParam.qmsg1);
    taosMemoryFreeClear(req.pRSmaParam.qmsg2);
    tEncoderClear(&encoder);
H
Hongze Cheng 已提交
443 444
    return NULL;
  }
H
Hongze Cheng 已提交
445
  tEncoderClear(&encoder);
H
more  
Hongze Cheng 已提交
446

S
Shengliang Guan 已提交
447
  *pContLen = contLen;
448 449
  taosMemoryFreeClear(req.pRSmaParam.qmsg1);
  taosMemoryFreeClear(req.pRSmaParam.qmsg2);
S
Shengliang Guan 已提交
450
  return pHead;
451 452
}

S
Shengliang Guan 已提交
453
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
454 455 456 457 458
  SName        name = {0};
  SVDropStbReq req = {0};
  int32_t      contLen = 0;
  int32_t      ret = 0;
  SMsgHead    *pHead = NULL;
H
Hongze Cheng 已提交
459
  SEncoder     encoder = {0};
H
Hongze Cheng 已提交
460

S
Shengliang Guan 已提交
461 462 463 464
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

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

H
Hongze Cheng 已提交
466 467 468 469 470
  tEncodeSize(tEncodeSVDropStbReq, &req, contLen, ret);
  if (ret < 0) return NULL;

  contLen += sizeof(SMsgHead);
  pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
471
  if (pHead == NULL) {
472 473 474 475
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

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

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

H
Hongze Cheng 已提交
481 482 483
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  tEncodeSVDropStbReq(&encoder, &req);
  tEncoderClear(&encoder);
484

S
Shengliang Guan 已提交
485 486
  *pContLen = contLen;
  return pHead;
487 488
}

489
int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
490
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
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
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
496
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
497 498
    return -1;
  }
S
Shengliang Guan 已提交
499

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

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

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

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

S
Shengliang Guan 已提交
543 544 545
  return 0;
}

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

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

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

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

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

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

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

  return 0;
}

612
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
613
  SSdb   *pSdb = pMnode->pSdb;
614
  SVgObj *pVgroup = NULL;
615
  void   *pIter = NULL;
616 617 618 619

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

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

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

  return 0;
}

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

662 663 664 665 666 667 668
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;
669 670
  pDst->tagVer = 1;
  pDst->colVer = 1;
671 672 673 674 675 676 677 678 679 680
  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 已提交
681 682 683
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
684
    memcpy(pDst->comment, pCreate->comment, pDst->commentLen);
S
sma  
Shengliang Guan 已提交
685
  }
S
Shengliang Guan 已提交
686

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

697 698 699 700
  pDst->ast2Len = pCreate->ast2Len;
  if (pDst->ast2Len > 0) {
    pDst->pAst2 = taosMemoryCalloc(pDst->ast2Len, 1);
    if (pDst->pAst2 == NULL) {
701 702 703
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
704
    memcpy(pDst->pAst2, pCreate->pAst2, pDst->ast2Len);
705 706
  }

707 708 709
  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 已提交
710 711 712
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
713

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

725
  for (int32_t i = 0; i < pDst->numOfTags; ++i) {
S
Shengliang Guan 已提交
726
    SField  *pField = taosArrayGet(pCreate->pTags, i);
727
    SSchema *pSchema = &pDst->pTags[i];
S
Shengliang Guan 已提交
728 729 730
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
731 732
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
733
  }
734 735 736
  return 0;
}

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

S
Shengliang 已提交
740
  int32_t code = -1;
741

742
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pReq);
743
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
744

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

747
  if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER;
748 749 750

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

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

S
Shengliang Guan 已提交
753 754
  code = 0;

755
_OVER:
S
Shengliang Guan 已提交
756
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
757
  return code;
S
Shengliang Guan 已提交
758
}
S
Shengliang Guan 已提交
759

760
int32_t mndAddStbToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
761
  mndTransSetDbName(pTrans, pDb->name);
762 763 764 765 766 767 768
  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 已提交
769

770
static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
771
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
772 773 774 775
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
776

S
Shengliang Guan 已提交
777
  if (tDeserializeSMCreateStbReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
778
    terrno = TSDB_CODE_INVALID_MSG;
779
    goto _OVER;
S
Shengliang Guan 已提交
780
  }
S
Shengliang Guan 已提交
781

S
Shengliang Guan 已提交
782
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
783 784
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
785
    goto _OVER;
S
Shengliang Guan 已提交
786
  }
S
Shengliang Guan 已提交
787

S
Shengliang Guan 已提交
788
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
789
  if (pStb != NULL) {
S
Shengliang Guan 已提交
790 791 792
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
793
      goto _OVER;
S
Shengliang Guan 已提交
794 795
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
796
      goto _OVER;
S
Shengliang Guan 已提交
797
    }
S
Shengliang Guan 已提交
798
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
799
    goto _OVER;
S
Shengliang Guan 已提交
800 801
  }

S
Shengliang Guan 已提交
802
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
803 804
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
805
    goto _OVER;
S
Shengliang Guan 已提交
806 807
  }

808
  if (mndCheckDbAuth(pMnode, pReq->conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
809
    goto _OVER;
S
Shengliang Guan 已提交
810 811
  }

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

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

S
Shengliang Guan 已提交
822
  code = mndCreateStb(pMnode, pReq, &createReq, pDb);
S
Shengliang Guan 已提交
823
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
824

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

S
Shengliang Guan 已提交
830 831
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
832
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
833 834

  return code;
S
Shengliang Guan 已提交
835 836
}

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

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

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

  return 0;
}

S
Shengliang Guan 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
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 已提交
877 878
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
879 880 881 882 883 884 885 886 887 888
  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 已提交
889 890
static int32_t mndUpdateStbCommentAndTTL(const SStbObj *pOld, SStbObj *pNew, char *pComment, int32_t commentLen,
                                         int32_t ttl) {
S
Shengliang 已提交
891 892 893 894 895 896 897 898 899
  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 已提交
900 901 902
  if (ttl >= 0) {
    pNew->ttl = ttl;
  }
S
Shengliang 已提交
903 904 905 906 907 908 909

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

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

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

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

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

    mDebug("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
946 947
  }

948
  pNew->tagVer++;
S
Shengliang Guan 已提交
949 950 951
  return 0;
}

952
static int32_t mndDropSuperTableTag(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *tagName) {
S
Shengliang Guan 已提交
953 954 955 956 957 958
  int32_t tag = mndFindSuperTableTagIndex(pOld, tagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

959 960 961 962 963
  col_id_t colId = pOld->pTags[tag].colId;
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
964 965 966 967 968
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pTags + tag, pNew->pTags + tag + 1, sizeof(SSchema) * (pNew->numOfTags - tag - 1));
S
Shengliang Guan 已提交
969
  pNew->numOfTags--;
S
Shengliang Guan 已提交
970

971
  pNew->tagVer++;
S
Shengliang Guan 已提交
972 973 974 975
  mDebug("stb:%s, start to drop tag %s", pNew->name, tagName);
  return 0;
}

976
static int32_t mndAlterStbTagName(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, SArray *pFields) {
S
Shengliang Guan 已提交
977 978 979 980 981 982 983 984 985 986 987
  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 已提交
988 989 990 991 992 993
  int32_t tag = mndFindSuperTableTagIndex(pOld, oldTagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

994 995 996 997 998
  col_id_t colId = pOld->pTags[tag].colId;
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
999
  if (mndFindSuperTableTagIndex(pOld, newTagName) >= 0) {
S
Shengliang Guan 已提交
1000
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1001 1002 1003
    return -1;
  }

S
Shengliang Guan 已提交
1004 1005
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
    return -1;
  }

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

  SSchema *pSchema = (SSchema *)(pNew->pTags + tag);
  memcpy(pSchema->name, newTagName, TSDB_COL_NAME_LEN);

1016
  pNew->tagVer++;
S
Shengliang Guan 已提交
1017 1018 1019 1020
  mDebug("stb:%s, start to modify tag %s to %s", pNew->name, oldTagName, newTagName);
  return 0;
}

1021
static int32_t mndAlterStbTagBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1022
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1023 1024 1025 1026 1027
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1028 1029 1030 1031 1032
  col_id_t colId = pOld->pTags[tag].colId;
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1033 1034 1035 1036 1037 1038
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pTag = pNew->pTags + tag;

S
Shengliang Guan 已提交
1039 1040 1041 1042 1043
  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 已提交
1044
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1045 1046 1047 1048
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1049
  pTag->bytes = pField->bytes;
1050
  pNew->tagVer++;
S
Shengliang Guan 已提交
1051

S
Shengliang Guan 已提交
1052
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1053 1054 1055
  return 0;
}

S
Shengliang Guan 已提交
1056
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1057 1058 1059 1060 1061
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1062 1063 1064 1065 1066
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1067
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1068
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1069
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1070
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1071 1072 1073
      return -1;
    }

S
Shengliang 已提交
1074
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1075
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1076 1077 1078
      return -1;
    }

S
Shengliang Guan 已提交
1079 1080 1081 1082
    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 已提交
1083 1084
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1085 1086

    mDebug("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1087 1088
  }

1089
  pNew->colVer++;
S
Shengliang Guan 已提交
1090 1091 1092
  return 0;
}

1093
static int32_t mndDropSuperTableColumn(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *colName) {
S
Shengliang Guan 已提交
1094
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
S
Shengliang Guan 已提交
1095
  if (col < 0) {
S
Shengliang Guan 已提交
1096 1097 1098 1099
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
  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;
  }

1110
  col_id_t colId = pOld->pColumns[col].colId;
1111 1112 1113 1114
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1115 1116 1117 1118 1119
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
S
Shengliang Guan 已提交
1120
  pNew->numOfColumns--;
S
Shengliang Guan 已提交
1121

1122
  pNew->colVer++;
S
Shengliang Guan 已提交
1123 1124 1125 1126
  mDebug("stb:%s, start to drop col %s", pNew->name, colName);
  return 0;
}

1127
static int32_t mndAlterStbColumnBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1128
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1129 1130 1131 1132 1133 1134 1135
  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 已提交
1136
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1137 1138 1139 1140 1141 1142 1143
  }

  if (nLen > TSDB_MAX_BYTES_PER_ROW) {
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

1144
  col_id_t colId = pOld->pColumns[col].colId;
1145 1146 1147 1148
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1149 1150 1151 1152 1153
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pCol = pNew->pColumns + col;
S
Shengliang Guan 已提交
1154 1155
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1156 1157 1158
    return -1;
  }

S
Shengliang Guan 已提交
1159
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1160
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1161 1162 1163
    return -1;
  }

S
Shengliang Guan 已提交
1164
  pCol->bytes = pField->bytes;
1165
  pNew->colVer++;
S
Shengliang Guan 已提交
1166

S
Shengliang Guan 已提交
1167
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1168 1169 1170
  return 0;
}

S
Shengliang Guan 已提交
1171
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1172 1173 1174
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
S
Shengliang Guan 已提交
1175
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
S
Shengliang Guan 已提交
1176 1177 1178 1179

  return 0;
}

S
Shengliang Guan 已提交
1180
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1181 1182 1183 1184 1185 1186 1187 1188
  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 已提交
1189
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1190 1191 1192 1193 1194 1195 1196 1197
  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 已提交
1198 1199 1200 1201
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1202

S
Shengliang Guan 已提交
1203
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
    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 已提交
1214
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1215
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1216
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

D
dapan1121 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
static int32_t mndBuildStbSchemaImp(SDbObj *pDb, SStbObj *pStb, const char *tbName, STableMetaRsp *pRsp) {
  taosRLockLatch(&pStb->lock);

  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
  if (pRsp->pSchemas == NULL) {
    taosRUnLockLatch(&pStb->lock);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  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->colVer;
  pRsp->tversion = pStb->tagVer;
  pRsp->suid = pStb->uid;
  pRsp->tuid = pStb->uid;

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

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

  taosRUnLockLatch(&pStb->lock);
  return 0;
}

L
Liu Jicong 已提交
1273 1274
static int32_t mndBuildStbSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp,
                                 int32_t *smaVer) {
D
dapan1121 已提交
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
  char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
  snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, tbName);

  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);
D
dapan1121 已提交
1287
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
D
dapan1121 已提交
1288 1289 1290
    return -1;
  }

D
dapan1121 已提交
1291 1292 1293 1294
  if (smaVer) {
    *smaVer = pStb->smaVer;
  }

D
dapan1121 已提交
1295 1296 1297 1298 1299 1300
  int32_t code = mndBuildStbSchemaImp(pDb, pStb, tbName, pRsp);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}

1301 1302
static int32_t mndBuildSMAlterStbRsp(SDbObj *pDb, const SMAlterStbReq *pAlter, SStbObj *pObj, void **pCont,
                                     int32_t *pLen) {
1303
  int32_t       ret;
D
dapan1121 已提交
1304
  SEncoder      ec = {0};
1305
  uint32_t      contLen = 0;
D
dapan1121 已提交
1306
  SMAlterStbRsp alterRsp = {0};
1307
  SName         name = {0};
D
dapan1121 已提交
1308 1309 1310 1311 1312 1313 1314
  tNameFromString(&name, pAlter->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  alterRsp.pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
  if (NULL == alterRsp.pMeta) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
1315

D
dapan1121 已提交
1316
  ret = mndBuildStbSchemaImp(pDb, pObj, name.tname, alterRsp.pMeta);
D
dapan1121 已提交
1317 1318 1319 1320
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }
1321

D
dapan1121 已提交
1322 1323 1324 1325 1326 1327
  tEncodeSize(tEncodeSMAlterStbRsp, &alterRsp, contLen, ret);
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }

1328
  void *cont = taosMemoryMalloc(contLen);
D
dapan1121 已提交
1329 1330 1331 1332 1333 1334 1335 1336
  tEncoderInit(&ec, cont, contLen);
  tEncodeSMAlterStbRsp(&ec, &alterRsp);
  tEncoderClear(&ec);

  tFreeSMAlterStbRsp(&alterRsp);

  *pCont = cont;
  *pLen = contLen;
1337

D
dapan1121 已提交
1338 1339 1340
  return 0;
}

S
Shengliang Guan 已提交
1341
static int32_t mndAlterStb(SMnode *pMnode, SRpcMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
1342 1343 1344 1345 1346
  bool    needRsp = true;
  int32_t code = -1;
  STrans *pTrans = NULL;
  SField *pField0 = NULL;

S
Shengliang Guan 已提交
1347 1348 1349
  SStbObj stbObj = {0};
  taosRLockLatch(&pOld->lock);
  memcpy(&stbObj, pOld, sizeof(SStbObj));
1350
  taosRUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
1351 1352 1353
  stbObj.pColumns = NULL;
  stbObj.pTags = NULL;
  stbObj.updateTime = taosGetTimestampMs();
D
dapan1121 已提交
1354
  stbObj.lock = 0;
S
Shengliang Guan 已提交
1355

S
Shengliang Guan 已提交
1356
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
1357
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
1358
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1359
      break;
S
Shengliang Guan 已提交
1360
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang 已提交
1361
      pField0 = taosArrayGet(pAlter->pFields, 0);
1362
      code = mndDropSuperTableTag(pMnode, pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1363
      break;
S
Shengliang Guan 已提交
1364
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
1365
      code = mndAlterStbTagName(pMnode, pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
1366
      break;
S
Shengliang Guan 已提交
1367
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang 已提交
1368
      pField0 = taosArrayGet(pAlter->pFields, 0);
1369
      code = mndAlterStbTagBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1370 1371
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
1372
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1373 1374
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang 已提交
1375
      pField0 = taosArrayGet(pAlter->pFields, 0);
1376
      code = mndDropSuperTableColumn(pMnode, pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1377
      break;
S
Shengliang Guan 已提交
1378
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang 已提交
1379
      pField0 = taosArrayGet(pAlter->pFields, 0);
1380
      code = mndAlterStbColumnBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1381
      break;
S
Shengliang 已提交
1382
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
D
dapan1121 已提交
1383
      needRsp = false;
S
Shengliang 已提交
1384
      code = mndUpdateStbCommentAndTTL(pOld, &stbObj, pAlter->comment, pAlter->commentLen, pAlter->ttl);
S
Shengliang 已提交
1385
      break;
S
Shengliang Guan 已提交
1386
    default:
D
dapan1121 已提交
1387
      needRsp = false;
S
Shengliang 已提交
1388
      terrno = TSDB_CODE_OPS_NOT_SUPPORT;
S
Shengliang Guan 已提交
1389 1390 1391
      break;
  }

1392
  if (code != 0) goto _OVER;
S
Shengliang Guan 已提交
1393 1394

  code = -1;
1395
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq);
1396
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1397

S
Shengliang Guan 已提交
1398
  mDebug("trans:%d, used to alter stb:%s", pTrans->id, pAlter->name);
1399
  mndTransSetDbName(pTrans, pDb->name);
S
Shengliang Guan 已提交
1400

D
dapan1121 已提交
1401
  if (needRsp) {
1402
    void   *pCont = NULL;
D
dapan1121 已提交
1403
    int32_t contLen = 0;
1404
    if (mndBuildSMAlterStbRsp(pDb, pAlter, &stbObj, &pCont, &contLen) != 0) goto _OVER;
D
dapan1121 已提交
1405 1406
    mndTransSetRpcRsp(pTrans, pCont, contLen);
  }
1407

1408 1409 1410 1411
  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 已提交
1412 1413

  code = 0;
S
Shengliang Guan 已提交
1414

1415
_OVER:
S
Shengliang Guan 已提交
1416
  mndTransDrop(pTrans);
wafwerar's avatar
wafwerar 已提交
1417 1418
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
S
Shengliang Guan 已提交
1419 1420
  return code;
}
S
Shengliang Guan 已提交
1421

1422
static int32_t mndProcessAlterStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
1423
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1424 1425 1426 1427
  int32_t       code = -1;
  SDbObj       *pDb = NULL;
  SStbObj      *pStb = NULL;
  SMAlterStbReq alterReq = {0};
S
Shengliang Guan 已提交
1428

S
Shengliang Guan 已提交
1429
  if (tDeserializeSMAlterStbReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1430
    terrno = TSDB_CODE_INVALID_MSG;
1431
    goto _OVER;
S
Shengliang Guan 已提交
1432
  }
S
Shengliang Guan 已提交
1433

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

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

S
Shengliang Guan 已提交
1443
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1444 1445
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1446
    goto _OVER;
S
Shengliang Guan 已提交
1447
  }
S
Shengliang Guan 已提交
1448

1449 1450 1451 1452
  if ((alterReq.tagVer > 0 && alterReq.colVer > 0) &&
      (alterReq.tagVer <= pStb->tagVer || alterReq.colVer <= pStb->colVer)) {
    mDebug("stb:%s, already exist, tagVer:%d colVer:%d smaller than in mnode, tagVer:%d colVer:%d, alter success",
           alterReq.name, alterReq.tagVer, alterReq.colVer, pStb->tagVer, pStb->colVer);
1453 1454 1455 1456
    code = 0;
    goto _OVER;
  }

1457
  if (mndCheckDbAuth(pMnode, pReq->conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
1458
    goto _OVER;
S
Shengliang Guan 已提交
1459 1460
  }

S
Shengliang Guan 已提交
1461
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
S
Shengliang Guan 已提交
1462
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1463

1464
_OVER:
S
Shengliang Guan 已提交
1465
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1466
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1467 1468
  }

S
Shengliang Guan 已提交
1469 1470
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1471
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1472 1473

  return code;
S
Shengliang Guan 已提交
1474
}
S
Shengliang Guan 已提交
1475

S
Shengliang Guan 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
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 已提交
1494 1495 1496 1497
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 已提交
1498

S
Shengliang Guan 已提交
1499 1500 1501
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1502 1503 1504 1505
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1506

S
Shengliang Guan 已提交
1507
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1508
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
    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 已提交
1523
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1534
static int32_t mndDropStb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1535
  int32_t code = -1;
1536
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pReq);
1537
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1538

S
Shengliang Guan 已提交
1539
  mDebug("trans:%d, used to drop stb:%s", pTrans->id, pStb->name);
1540
  mndTransSetDbName(pTrans, pDb->name);
S
Shengliang Guan 已提交
1541

1542 1543 1544
  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;
1545
  if (mndDropSmasByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
1546
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
1547

S
Shengliang Guan 已提交
1548 1549
  code = 0;

1550
_OVER:
S
Shengliang Guan 已提交
1551
  mndTransDrop(pTrans);
S
Shengliang 已提交
1552
  return code;
S
Shengliang Guan 已提交
1553 1554
}

1555
static int32_t mndProcessDropStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
1556
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1557 1558 1559
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1560
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
1561

S
Shengliang Guan 已提交
1562
  if (tDeserializeSMDropStbReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1563
    terrno = TSDB_CODE_INVALID_MSG;
1564
    goto _OVER;
S
Shengliang Guan 已提交
1565
  }
S
Shengliang Guan 已提交
1566

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

S
Shengliang Guan 已提交
1569
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1570
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1571 1572
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1573
      code = 0;
1574
      goto _OVER;
S
Shengliang Guan 已提交
1575 1576
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1577
      goto _OVER;
S
Shengliang Guan 已提交
1578 1579 1580
    }
  }

S
Shengliang Guan 已提交
1581
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1582 1583
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1584
    goto _OVER;
S
Shengliang Guan 已提交
1585 1586
  }

1587
  if (mndCheckDbAuth(pMnode, pReq->conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
1588
    goto _OVER;
S
Shengliang Guan 已提交
1589 1590 1591
  }

  code = mndDropStb(pMnode, pReq, pDb, pStb);
S
Shengliang Guan 已提交
1592
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1593

1594
_OVER:
S
Shengliang Guan 已提交
1595
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1596
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1597 1598
  }

S
Shengliang Guan 已提交
1599 1600 1601
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
S
Shengliang Guan 已提交
1602
}
S
Shengliang Guan 已提交
1603

S
Shengliang Guan 已提交
1604 1605
static int32_t mndProcessTableMetaReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1606 1607 1608
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
1609

S
Shengliang Guan 已提交
1610
  if (tDeserializeSTableInfoReq(pReq->pCont, pReq->contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
1611
    terrno = TSDB_CODE_INVALID_MSG;
1612
    goto _OVER;
S
Shengliang Guan 已提交
1613
  }
D
dapan 已提交
1614

D
dapan1121 已提交
1615 1616 1617
  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) {
1618
      goto _OVER;
D
dapan1121 已提交
1619
    }
D
dapan1121 已提交
1620 1621 1622
  } 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) {
1623
      goto _OVER;
D
dapan1121 已提交
1624
    }
D
dapan1121 已提交
1625 1626
  } else {
    mDebug("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
D
dapan1121 已提交
1627
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp, NULL) != 0) {
1628
      goto _OVER;
D
dapan1121 已提交
1629
    }
S
Shengliang Guan 已提交
1630
  }
S
Shengliang Guan 已提交
1631

S
Shengliang Guan 已提交
1632 1633 1634
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
1635
    goto _OVER;
S
Shengliang Guan 已提交
1636
  }
S
Shengliang Guan 已提交
1637

S
Shengliang Guan 已提交
1638
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1639 1640
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1641
    goto _OVER;
S
Shengliang Guan 已提交
1642
  }
D
dapan 已提交
1643

S
Shengliang Guan 已提交
1644
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1645 1646
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = rspLen;
S
Shengliang Guan 已提交
1647
  code = 0;
S
Shengliang Guan 已提交
1648

1649
  mTrace("%s.%s, meta is retrieved", infoReq.dbFName, infoReq.tbName);
D
dapan 已提交
1650

1651
_OVER:
S
Shengliang Guan 已提交
1652 1653 1654
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1655

S
Shengliang Guan 已提交
1656 1657 1658
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1659

D
dapan1121 已提交
1660
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t numOfStbs, void **ppRsp,
S
Shengliang Guan 已提交
1661
                           int32_t *pRspLen) {
D
dapan1121 已提交
1662
  SSTbHbRsp hbRsp = {0};
D
dapan1121 已提交
1663 1664
  hbRsp.pMetaRsp = taosArrayInit(numOfStbs, sizeof(STableMetaRsp));
  if (hbRsp.pMetaRsp == NULL) {
S
Shengliang Guan 已提交
1665 1666 1667
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
1668

D
dapan1121 已提交
1669
  hbRsp.pIndexRsp = taosArrayInit(numOfStbs, sizeof(STableIndexRsp));
D
dapan1121 已提交
1670
  if (NULL == hbRsp.pIndexRsp) {
D
dapan1121 已提交
1671
    taosArrayDestroy(hbRsp.pMetaRsp);
D
dapan1121 已提交
1672 1673 1674
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
1675

S
Shengliang Guan 已提交
1676
  for (int32_t i = 0; i < numOfStbs; ++i) {
D
dapan1121 已提交
1677
    SSTableVersion *pStbVersion = &pStbVersions[i];
S
Shengliang Guan 已提交
1678 1679 1680
    pStbVersion->suid = be64toh(pStbVersion->suid);
    pStbVersion->sversion = ntohs(pStbVersion->sversion);
    pStbVersion->tversion = ntohs(pStbVersion->tversion);
D
dapan1121 已提交
1681
    pStbVersion->smaVer = ntohl(pStbVersion->smaVer);
S
Shengliang Guan 已提交
1682

S
Shengliang Guan 已提交
1683
    STableMetaRsp metaRsp = {0};
L
Liu Jicong 已提交
1684
    int32_t       smaVer = 0;
S
Shengliang Guan 已提交
1685
    mDebug("stb:%s.%s, start to retrieve meta", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
1686
    if (mndBuildStbSchema(pMnode, pStbVersion->dbFName, pStbVersion->stbName, &metaRsp, &smaVer) != 0) {
S
Shengliang Guan 已提交
1687 1688
      metaRsp.numOfColumns = -1;
      metaRsp.suid = pStbVersion->suid;
D
dapan1121 已提交
1689
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
1690
      continue;
D
dapan 已提交
1691
    }
S
Shengliang Guan 已提交
1692

D
dapan1121 已提交
1693
    if (pStbVersion->sversion != metaRsp.sversion || pStbVersion->tversion != metaRsp.tversion) {
D
dapan1121 已提交
1694
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
1695 1696
    } else {
      tFreeSTableMetaRsp(&metaRsp);
S
Shengliang Guan 已提交
1697
    }
D
dapan1121 已提交
1698

D
dapan1121 已提交
1699
    if (pStbVersion->smaVer && pStbVersion->smaVer != smaVer) {
L
Liu Jicong 已提交
1700 1701
      bool           exist = false;
      char           tbFName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
1702
      STableIndexRsp indexRsp = {0};
D
dapan1121 已提交
1703 1704 1705 1706 1707
      indexRsp.pIndex = taosArrayInit(10, sizeof(STableIndexInfo));
      if (NULL == indexRsp.pIndex) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
L
Liu Jicong 已提交
1708

D
dapan1121 已提交
1709
      sprintf(tbFName, "%s.%s", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
1710
      int32_t code = mndGetTableSma(pMnode, tbFName, &indexRsp, &exist);
D
dapan1121 已提交
1711
      if (code || !exist) {
D
dapan1121 已提交
1712 1713 1714
        indexRsp.suid = pStbVersion->suid;
        indexRsp.version = -1;
        indexRsp.pIndex = NULL;
D
dapan1121 已提交
1715
      }
D
dapan1121 已提交
1716

D
dapan1121 已提交
1717 1718
      strcpy(indexRsp.dbFName, pStbVersion->dbFName);
      strcpy(indexRsp.tbName, pStbVersion->stbName);
D
dapan1121 已提交
1719 1720

      taosArrayPush(hbRsp.pIndexRsp, &indexRsp);
D
dapan1121 已提交
1721
    }
S
Shengliang Guan 已提交
1722
  }
S
Shengliang Guan 已提交
1723

D
dapan1121 已提交
1724
  int32_t rspLen = tSerializeSSTbHbRsp(NULL, 0, &hbRsp);
S
Shengliang Guan 已提交
1725
  if (rspLen < 0) {
D
dapan1121 已提交
1726
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
1727 1728
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
1729 1730
  }

wafwerar's avatar
wafwerar 已提交
1731
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
1732
  if (pRsp == NULL) {
D
dapan1121 已提交
1733
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
1734 1735
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
1736 1737
  }

D
dapan1121 已提交
1738 1739
  tSerializeSSTbHbRsp(pRsp, rspLen, &hbRsp);
  tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
1740 1741
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1742 1743 1744
  return 0;
}

1745
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1746
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1747 1748 1749 1750 1751 1752
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1753
  int32_t numOfStbs = 0;
1754
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1755
  while (1) {
S
Shengliang Guan 已提交
1756
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1757
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1758 1759
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1760
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1761
      numOfStbs++;
S
Shengliang Guan 已提交
1762 1763
    }

S
Shengliang Guan 已提交
1764
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1765 1766
  }

S
Shengliang Guan 已提交
1767
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1768
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1769 1770 1771
  return 0;
}

L
Liu Jicong 已提交
1772 1773 1774 1775 1776 1777 1778 1779
void mndExtractDbNameFromStbFullName(const char *stbFullName, char *dst) {
  SName name = {0};
  tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  tNameGetFullDbName(&name, dst);
}

void mndExtractTbNameFromStbFullName(const char *stbFullName, char *dst, int32_t dstSize) {
S
Shengliang Guan 已提交
1780 1781
  int32_t pos = -1;
  int32_t num = 0;
L
Liu Jicong 已提交
1782 1783
  for (pos = 0; stbFullName[pos] != 0; ++pos) {
    if (stbFullName[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
1784 1785 1786 1787
    if (num == 2) break;
  }

  if (num == 2) {
L
Liu Jicong 已提交
1788
    tstrncpy(dst, stbFullName + pos + 1, dstSize);
S
Shengliang Guan 已提交
1789 1790 1791
  }
}

S
Shengliang Guan 已提交
1792 1793
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1794
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1795 1796 1797
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1798

H
Hongze Cheng 已提交
1799
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
1800 1801
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
1802
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
1803
  }
S
Shengliang Guan 已提交
1804

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

H
Haojun Liao 已提交
1809
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
1810
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1811 1812 1813 1814 1815
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
1816
    SName name = {0};
H
Hongze Cheng 已提交
1817
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
L
Liu Jicong 已提交
1818
    mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
1819
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
1820

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

H
Hongze Cheng 已提交
1824 1825
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
1826 1827 1828
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

1829
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
H
Hongze Cheng 已提交
1830
    colDataAppend(pColInfo, numOfRows, (const char *)db, false);
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843

    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 已提交
1844
    char *p = taosMemoryCalloc(1, pStb->commentLen + 1 + VARSTR_HEADER_SIZE);  // check malloc failures
1845 1846 1847 1848 1849 1850 1851
    if (p != NULL) {
      if (pStb->commentLen != 0) {
        STR_TO_VARSTR(p, pStb->comment);
      } else {
        STR_TO_VARSTR(p, "");
      }

1852
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1853 1854
      colDataAppend(pColInfo, numOfRows, (const char *)p, false);
      taosMemoryFree(p);
S
Shengliang Guan 已提交
1855
    }
H
Haojun Liao 已提交
1856

S
Shengliang Guan 已提交
1857
    numOfRows++;
S
Shengliang Guan 已提交
1858
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1859 1860
  }

H
Haojun Liao 已提交
1861 1862 1863 1864
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

1865
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1866 1867 1868
  return numOfRows;
}

S
Shengliang Guan 已提交
1869
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1870 1871
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
1872
}