mndStb.c 53.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 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 45
static int32_t  mndProcessMCreateStbReq(SRpcMsg *pReq);
static int32_t  mndProcessMAlterStbReq(SRpcMsg *pReq);
static int32_t  mndProcessMDropStbReq(SRpcMsg *pReq);
static int32_t  mndProcessVCreateStbRsp(SRpcMsg *pRsp);
static int32_t  mndProcessVAlterStbRsp(SRpcMsg *pRsp);
static int32_t  mndProcessVDropStbRsp(SRpcMsg *pRsp);
static int32_t  mndProcessTableMetaReq(SRpcMsg *pReq);
static int32_t  mndRetrieveStb(SRpcMsg *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
  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)
90 91
  SDB_SET_INT32(pRaw, dataPos, pStb->tagVer, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->colVer, _OVER)
92 93 94 95 96 97 98
  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)
99 100
  SDB_SET_INT32(pRaw, dataPos, pStb->ast1Len, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
101

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

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
113
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
114
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
115 116 117
    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 已提交
118 119
  }

S
sma  
Shengliang Guan 已提交
120
  if (pStb->commentLen > 0) {
121
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, _OVER)
S
sma  
Shengliang Guan 已提交
122
  }
123 124 125 126 127 128
  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 已提交
129
  SDB_SET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
130
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
131 132 133

  terrno = 0;

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

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

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

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

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

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

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

  int32_t dataPos = 0;
163 164 165 166 167 168
  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)
169 170
  SDB_GET_INT32(pRaw, dataPos, &pStb->tagVer, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->colVer, _OVER)
171
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, _OVER)
S
sma  
Shengliang Guan 已提交
172
  int32_t xFilesFactor = 0;
173
  SDB_GET_INT32(pRaw, dataPos, &xFilesFactor, _OVER)
S
sma  
Shengliang Guan 已提交
174
  pStb->xFilesFactor = xFilesFactor / 10000.0f;
175 176 177 178 179
  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)
180 181
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
182

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

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

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

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

  terrno = 0;

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
284
  if (pOld->commentLen < pNew->commentLen) {
wafwerar's avatar
wafwerar 已提交
285
    void *comment = taosMemoryMalloc(pNew->commentLen);
S
Shengliang Guan 已提交
286
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
287
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
288 289 290 291 292 293 294 295
      pOld->comment = comment;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

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

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

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

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

S
Shengliang Guan 已提交
360 361
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
362

S
Shengliang Guan 已提交
363 364
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
365

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
429 430
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
431

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

S
Shengliang Guan 已提交
443
  *pContLen = contLen;
444 445
  taosMemoryFreeClear(req.pRSmaParam.qmsg1);
  taosMemoryFreeClear(req.pRSmaParam.qmsg2);
S
Shengliang Guan 已提交
446
  return pHead;
447 448
}

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

S
Shengliang Guan 已提交
457 458 459 460
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

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

H
Hongze Cheng 已提交
462 463 464 465 466
  tEncodeSize(tEncodeSVDropStbReq, &req, contLen, ret);
  if (ret < 0) return NULL;

  contLen += sizeof(SMsgHead);
  pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
467
  if (pHead == NULL) {
468 469 470 471
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
472 473 474 475
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

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

H
Hongze Cheng 已提交
477 478 479
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  tEncodeSVDropStbReq(&encoder, &req);
  tEncoderClear(&encoder);
480

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return 0;
}

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

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

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

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

  return 0;
}

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

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

683 684 685 686
  pDst->ast1Len = pCreate->ast1Len;
  if (pDst->ast1Len > 0) {
    pDst->pAst1 = taosMemoryCalloc(pDst->ast1Len, 1);
    if (pDst->pAst1 == NULL) {
687 688 689
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
690
    memcpy(pDst->pAst1, pCreate->pAst1, pDst->ast1Len);
691 692
  }

693 694 695 696
  pDst->ast2Len = pCreate->ast2Len;
  if (pDst->ast2Len > 0) {
    pDst->pAst2 = taosMemoryCalloc(pDst->ast2Len, 1);
    if (pDst->pAst2 == NULL) {
697 698 699
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
700
    memcpy(pDst->pAst2, pCreate->pAst2, pDst->ast2Len);
701 702
  }

703 704 705
  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 已提交
706 707 708
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
709

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

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

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

S
Shengliang 已提交
736
  int32_t code = -1;
737

S
Shengliang Guan 已提交
738
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_STB, pReq);
739
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
740

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

743
  if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER;
744 745 746

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

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

S
Shengliang Guan 已提交
749 750
  code = 0;

751
_OVER:
S
Shengliang Guan 已提交
752
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
753
  return code;
S
Shengliang Guan 已提交
754
}
S
Shengliang Guan 已提交
755

756 757 758 759 760 761 762 763 764
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 已提交
765

S
Shengliang Guan 已提交
766 767
static int32_t mndProcessMCreateStbReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
768 769 770
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
S
Shengliang Guan 已提交
771
  SUserObj      *pUser = NULL;
S
Shengliang Guan 已提交
772
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
773

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

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

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

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

S
Shengliang Guan 已提交
805
  pUser = mndAcquireUser(pMnode, pReq->conn.user);
S
Shengliang Guan 已提交
806
  if (pUser == NULL) {
807
    goto _OVER;
S
Shengliang Guan 已提交
808 809 810
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
811
    goto _OVER;
S
Shengliang Guan 已提交
812 813
  }

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

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

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

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

S
Shengliang Guan 已提交
832 833
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
834 835
  mndReleaseUser(pMnode, pUser);
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
836 837

  return code;
S
Shengliang Guan 已提交
838 839
}

S
Shengliang Guan 已提交
840
static int32_t mndProcessVCreateStbRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
841
  mndTransProcessRsp(pRsp);
842 843
  return 0;
}
S
Shengliang Guan 已提交
844

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

S
Shengliang Guan 已提交
848 849 850 851
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
852

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

  return 0;
}

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

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

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

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

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

S
Shengliang Guan 已提交
946 947 948 949
    SSchema *pSchema = &pNew->pTags[pOld->numOfTags + i];
    pSchema->bytes = pField->bytes;
    pSchema->type = pField->type;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
S
Shengliang Guan 已提交
950 951
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
952 953

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

956
  pNew->tagVer++;
S
Shengliang Guan 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
  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 已提交
972
  pNew->numOfTags--;
S
Shengliang Guan 已提交
973

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

S
Shengliang Guan 已提交
979 980 981 982 983 984 985 986 987 988 989 990
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 已提交
991 992 993 994 995 996 997
  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 已提交
998
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
999 1000 1001
    return -1;
  }

S
Shengliang Guan 已提交
1002 1003
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
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);

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

S
Shengliang Guan 已提交
1019 1020
static int32_t mndAlterStbTagBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  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 已提交
1032 1033 1034 1035 1036
  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 已提交
1037
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1038 1039 1040 1041
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1042
  pTag->bytes = pField->bytes;
1043
  pNew->tagVer++;
S
Shengliang Guan 已提交
1044

S
Shengliang Guan 已提交
1045
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1046 1047 1048
  return 0;
}

S
Shengliang Guan 已提交
1049
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1050 1051 1052 1053 1054
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1055 1056 1057 1058 1059
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1060
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1061
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1062
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1063
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1064 1065 1066
      return -1;
    }

S
Shengliang 已提交
1067
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1068
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1069 1070 1071
      return -1;
    }

S
Shengliang Guan 已提交
1072 1073 1074 1075
    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 已提交
1076 1077
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1078 1079

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

1082
  pNew->colVer++;
S
Shengliang Guan 已提交
1083 1084 1085 1086 1087
  return 0;
}

static int32_t mndDropSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, const char *colName) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
S
Shengliang Guan 已提交
1088
  if (col < 0) {
S
Shengliang Guan 已提交
1089 1090 1091 1092
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
  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 已提交
1103 1104 1105 1106 1107
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

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

1110
  pNew->colVer++;
S
Shengliang Guan 已提交
1111 1112 1113 1114
  mDebug("stb:%s, start to drop col %s", pNew->name, colName);
  return 0;
}

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

  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 已提交
1137 1138
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1139 1140 1141
    return -1;
  }

S
Shengliang Guan 已提交
1142
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1143
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1144 1145 1146
    return -1;
  }

S
Shengliang Guan 已提交
1147
  pCol->bytes = pField->bytes;
1148
  pNew->colVer++;
S
Shengliang Guan 已提交
1149

S
Shengliang Guan 已提交
1150
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1151 1152 1153
  return 0;
}

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

  return 0;
}

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

S
Shengliang Guan 已提交
1186
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
    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 已提交
1197
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1198
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1199
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1210
static int32_t mndAlterStb(SMnode *pMnode, SRpcMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
S
Shengliang Guan 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219
  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 已提交
1220
  STrans *pTrans = NULL;
S
Shengliang 已提交
1221
  SField *pField0 = NULL;
S
Shengliang Guan 已提交
1222

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

1257
  if (code != 0) goto _OVER;
S
Shengliang Guan 已提交
1258 1259

  code = -1;
S
Shengliang Guan 已提交
1260
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_ALTER_STB, pReq);
1261
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1262

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

1266 1267 1268 1269
  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 已提交
1270 1271

  code = 0;
S
Shengliang Guan 已提交
1272

1273
_OVER:
S
Shengliang Guan 已提交
1274
  mndTransDrop(pTrans);
wafwerar's avatar
wafwerar 已提交
1275 1276
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
S
Shengliang Guan 已提交
1277 1278
  return code;
}
S
Shengliang Guan 已提交
1279

S
Shengliang Guan 已提交
1280 1281
static int32_t mndProcessMAlterStbReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1282 1283 1284 1285 1286
  int32_t       code = -1;
  SDbObj       *pDb = NULL;
  SStbObj      *pStb = NULL;
  SUserObj     *pUser = NULL;
  SMAlterStbReq alterReq = {0};
S
Shengliang Guan 已提交
1287

S
Shengliang Guan 已提交
1288
  if (tDeserializeSMAlterStbReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1289
    terrno = TSDB_CODE_INVALID_MSG;
1290
    goto _OVER;
S
Shengliang Guan 已提交
1291
  }
S
Shengliang Guan 已提交
1292

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

S
Shengliang Guan 已提交
1296
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1297 1298
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
1299
    goto _OVER;
S
Shengliang Guan 已提交
1300 1301
  }

S
Shengliang Guan 已提交
1302
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1303 1304
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1305
    goto _OVER;
S
Shengliang Guan 已提交
1306
  }
S
Shengliang Guan 已提交
1307

1308 1309 1310 1311
  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);
1312 1313 1314 1315
    code = 0;
    goto _OVER;
  }

S
Shengliang Guan 已提交
1316
  pUser = mndAcquireUser(pMnode, pReq->conn.user);
S
Shengliang Guan 已提交
1317
  if (pUser == NULL) {
1318
    goto _OVER;
S
Shengliang Guan 已提交
1319 1320 1321
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1322
    goto _OVER;
S
Shengliang Guan 已提交
1323 1324
  }

S
Shengliang Guan 已提交
1325
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
S
Shengliang Guan 已提交
1326
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1327

1328
_OVER:
S
Shengliang Guan 已提交
1329
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1330
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1331 1332
  }

S
Shengliang Guan 已提交
1333 1334
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1335
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
1336
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1337 1338

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

S
Shengliang Guan 已提交
1341
static int32_t mndProcessVAlterStbRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
1342
  mndTransProcessRsp(pRsp);
1343 1344
  return 0;
}
S
Shengliang Guan 已提交
1345

S
Shengliang Guan 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
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 已提交
1364 1365 1366 1367
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 已提交
1368

S
Shengliang Guan 已提交
1369 1370 1371
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1372 1373 1374 1375
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1376

S
Shengliang Guan 已提交
1377
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1378
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
    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 已提交
1393
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1404
static int32_t mndDropStb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1405
  int32_t code = -1;
S
Shengliang Guan 已提交
1406
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_STB, pReq);
1407
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1408

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

1412 1413 1414 1415
  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 已提交
1416

S
Shengliang Guan 已提交
1417 1418
  code = 0;

1419
_OVER:
S
Shengliang Guan 已提交
1420
  mndTransDrop(pTrans);
S
Shengliang 已提交
1421
  return code;
S
Shengliang Guan 已提交
1422 1423
}

S
Shengliang Guan 已提交
1424 1425
static int32_t mndProcessMDropStbReq(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1426 1427 1428 1429
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1430
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
1431

S
Shengliang Guan 已提交
1432
  if (tDeserializeSMDropStbReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1433
    terrno = TSDB_CODE_INVALID_MSG;
1434
    goto _OVER;
S
Shengliang Guan 已提交
1435
  }
S
Shengliang Guan 已提交
1436

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

S
Shengliang Guan 已提交
1439
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1440
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1441 1442
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1443
      code = 0;
1444
      goto _OVER;
S
Shengliang Guan 已提交
1445 1446
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1447
      goto _OVER;
S
Shengliang Guan 已提交
1448 1449 1450
    }
  }

S
Shengliang Guan 已提交
1451
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1452 1453
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1454
    goto _OVER;
S
Shengliang Guan 已提交
1455 1456
  }

S
Shengliang Guan 已提交
1457
  pUser = mndAcquireUser(pMnode, pReq->conn.user);
S
Shengliang Guan 已提交
1458
  if (pUser == NULL) {
1459
    goto _OVER;
S
Shengliang Guan 已提交
1460
  }
S
Shengliang Guan 已提交
1461

S
Shengliang Guan 已提交
1462
  if (mndCheckWriteAuth(pUser, pDb) != 0) {
1463
    goto _OVER;
S
Shengliang Guan 已提交
1464 1465 1466
  }

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

1469
_OVER:
S
Shengliang Guan 已提交
1470
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1471
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1472 1473
  }

S
Shengliang Guan 已提交
1474 1475 1476 1477 1478
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
1479
}
S
Shengliang Guan 已提交
1480

S
Shengliang Guan 已提交
1481
static int32_t mndProcessVDropStbRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
1482
  mndTransProcessRsp(pRsp);
1483 1484
  return 0;
}
S
Shengliang Guan 已提交
1485

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

S
Shengliang Guan 已提交
1489
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
wafwerar's avatar
wafwerar 已提交
1490
  pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
1491
  if (pRsp->pSchemas == NULL) {
S
Shengliang Guan 已提交
1492
    taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1493 1494 1495 1496
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
1497 1498 1499 1500 1501 1502 1503 1504
  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;
1505
  pRsp->sversion = pStb->colVer;
D
dapan1121 已提交
1506
  pRsp->tversion = pStb->tagVer;
S
Shengliang Guan 已提交
1507 1508
  pRsp->suid = pStb->uid;
  pRsp->tuid = pStb->uid;
S
Shengliang Guan 已提交
1509

S
Shengliang Guan 已提交
1510
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1511
    SSchema *pSchema = &pRsp->pSchemas[i];
S
Shengliang Guan 已提交
1512 1513 1514
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1515 1516
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1517 1518 1519
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
S
Shengliang Guan 已提交
1520
    SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
S
Shengliang Guan 已提交
1521
    SSchema *pSrcSchema = &pStb->pTags[i];
S
Shengliang Guan 已提交
1522 1523
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1524 1525
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1526
  }
S
Shengliang Guan 已提交
1527

S
Shengliang Guan 已提交
1528
  taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1529
  return 0;
S
Shengliang Guan 已提交
1530
}
S
Shengliang Guan 已提交
1531

S
Shengliang Guan 已提交
1532 1533 1534
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 已提交
1535

S
Shengliang Guan 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
  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 已提交
1553
}
S
Shengliang Guan 已提交
1554

S
Shengliang Guan 已提交
1555 1556
static int32_t mndProcessTableMetaReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1557 1558 1559
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
1560

S
Shengliang Guan 已提交
1561
  if (tDeserializeSTableInfoReq(pReq->pCont, pReq->contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
1562
    terrno = TSDB_CODE_INVALID_MSG;
1563
    goto _OVER;
S
Shengliang Guan 已提交
1564
  }
D
dapan 已提交
1565

D
dapan1121 已提交
1566 1567 1568
  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) {
1569
      goto _OVER;
D
dapan1121 已提交
1570
    }
D
dapan1121 已提交
1571 1572 1573
  } 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) {
1574
      goto _OVER;
D
dapan1121 已提交
1575
    }
D
dapan1121 已提交
1576 1577 1578
  } else {
    mDebug("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
1579
      goto _OVER;
D
dapan1121 已提交
1580
    }
S
Shengliang Guan 已提交
1581
  }
S
Shengliang Guan 已提交
1582

S
Shengliang Guan 已提交
1583 1584 1585
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
1586
    goto _OVER;
S
Shengliang Guan 已提交
1587
  }
S
Shengliang Guan 已提交
1588

S
Shengliang Guan 已提交
1589
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1590 1591
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1592
    goto _OVER;
S
Shengliang Guan 已提交
1593
  }
D
dapan 已提交
1594

S
Shengliang Guan 已提交
1595
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1596 1597
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = rspLen;
S
Shengliang Guan 已提交
1598
  code = 0;
S
Shengliang Guan 已提交
1599

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

1602
_OVER:
S
Shengliang Guan 已提交
1603 1604 1605
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1606

S
Shengliang Guan 已提交
1607 1608 1609
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1610

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

S
Shengliang Guan 已提交
1620 1621 1622 1623 1624
  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 已提交
1625

S
Shengliang Guan 已提交
1626 1627 1628 1629 1630
    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 已提交
1631
    }
S
Shengliang Guan 已提交
1632

D
dapan1121 已提交
1633
    if (pStbVersion->sversion != metaRsp.sversion || pStbVersion->tversion != metaRsp.tversion) {
S
Shengliang Guan 已提交
1634
      taosArrayPush(batchMetaRsp.pArray, &metaRsp);
D
dapan1121 已提交
1635 1636
    } else {
      tFreeSTableMetaRsp(&metaRsp);
S
Shengliang Guan 已提交
1637
    }
S
Shengliang Guan 已提交
1638
  }
S
Shengliang Guan 已提交
1639

S
Shengliang Guan 已提交
1640 1641 1642 1643 1644
  int32_t rspLen = tSerializeSTableMetaBatchRsp(NULL, 0, &batchMetaRsp);
  if (rspLen < 0) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
1645 1646
  }

wafwerar's avatar
wafwerar 已提交
1647
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
1648 1649 1650 1651
  if (pRsp == NULL) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
1652 1653
  }

S
Shengliang Guan 已提交
1654
  tSerializeSTableMetaBatchRsp(pRsp, rspLen, &batchMetaRsp);
D
dapan1121 已提交
1655
  tFreeSTableMetaBatchRsp(&batchMetaRsp);
S
Shengliang Guan 已提交
1656 1657
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1658 1659 1660
  return 0;
}

1661
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1662
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1663 1664 1665 1666 1667 1668
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1669
  int32_t numOfStbs = 0;
1670
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1671
  while (1) {
S
Shengliang Guan 已提交
1672
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1673
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1674 1675
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1676
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1677
      numOfStbs++;
S
Shengliang Guan 已提交
1678 1679
    }

S
Shengliang Guan 已提交
1680
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1681 1682
  }

S
Shengliang Guan 已提交
1683
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1684
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1685 1686 1687
  return 0;
}

S
Shengliang Guan 已提交
1688
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
1689 1690
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
1691
  for (pos = 0; tableId[pos] != 0; ++pos) {
H
Haojun Liao 已提交
1692
    if (tableId[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
1693 1694 1695 1696 1697 1698 1699 1700
    if (num == 2) break;
  }

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

S
Shengliang Guan 已提交
1701 1702
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1703
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1704 1705 1706
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1707

H
Hongze Cheng 已提交
1708
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
1709 1710
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
1711
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
1712
  }
S
Shengliang Guan 已提交
1713

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

H
Haojun Liao 已提交
1718
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
1719
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1720 1721 1722 1723 1724
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
1725
    SName name = {0};
H
Hongze Cheng 已提交
1726
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1727 1728
    mndExtractTableName(pStb->name, &stbName[VARSTR_HEADER_SIZE]);
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
1729

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

H
Hongze Cheng 已提交
1733 1734
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
1735 1736 1737
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

1738
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
H
Hongze Cheng 已提交
1739
    colDataAppend(pColInfo, numOfRows, (const char *)db, false);
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752

    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 已提交
1753
    char *p = taosMemoryCalloc(1, pStb->commentLen + 1 + VARSTR_HEADER_SIZE);  // check malloc failures
1754 1755 1756 1757 1758 1759 1760
    if (p != NULL) {
      if (pStb->commentLen != 0) {
        STR_TO_VARSTR(p, pStb->comment);
      } else {
        STR_TO_VARSTR(p, "");
      }

1761
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1762 1763
      colDataAppend(pColInfo, numOfRows, (const char *)p, false);
      taosMemoryFree(p);
S
Shengliang Guan 已提交
1764
    }
H
Haojun Liao 已提交
1765

S
Shengliang Guan 已提交
1766
    numOfRows++;
S
Shengliang Guan 已提交
1767
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1768 1769
  }

H
Haojun Liao 已提交
1770 1771 1772 1773
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

1774
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1775 1776 1777
  return numOfRows;
}

S
Shengliang Guan 已提交
1778
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1779 1780
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
1781
}