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

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndStb.h"
S
Shengliang Guan 已提交
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndDnode.h"
S
Shengliang Guan 已提交
20
#include "mndInfoSchema.h"
S
Shengliang Guan 已提交
21
#include "mndMnode.h"
H
Hongze Cheng 已提交
22
#include "mndPerfSchema.h"
23
#include "mndPrivilege.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);
S
Shengliang Guan 已提交
40
static int32_t  mndProcessTtlTimer(SRpcMsg *pReq);
41 42 43
static int32_t  mndProcessCreateStbReq(SRpcMsg *pReq);
static int32_t  mndProcessAlterStbReq(SRpcMsg *pReq);
static int32_t  mndProcessDropStbReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
44 45
static int32_t  mndProcessTableMetaReq(SRpcMsg *pReq);
static int32_t  mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
46
static void     mndCancelGetNextStb(SMnode *pMnode, void *pIter);
D
dapan1121 已提交
47
static int32_t  mndProcessTableCfgReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
48 49

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

60 61 62
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcessCreateStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcessAlterStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcessDropStbReq);
63 64 65
  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 已提交
66
  mndSetMsgHandle(pMnode, TDMT_MND_TABLE_META, mndProcessTableMetaReq);
S
Shengliang Guan 已提交
67
  mndSetMsgHandle(pMnode, TDMT_MND_TTL_TIMER, mndProcessTtlTimer);
D
dapan1121 已提交
68
  mndSetMsgHandle(pMnode, TDMT_MND_TABLE_CFG, mndProcessTableCfgReq);
S
Shengliang Guan 已提交
69 70 71

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

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

S
Shengliang Guan 已提交
76
void mndCleanupStb(SMnode *pMnode) {}
S
Shengliang Guan 已提交
77

78
SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
79 80
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
81
  int32_t size = sizeof(SStbObj) + (pStb->numOfColumns + pStb->numOfTags) * sizeof(SSchema) + pStb->commentLen +
D
dapan1121 已提交
82
                 pStb->ast1Len + pStb->ast2Len + STB_RESERVE_SIZE + taosArrayGetSize(pStb->pFuncs) * TSDB_FUNC_NAME_LEN;
S
Shengliang Guan 已提交
83
  SSdbRaw *pRaw = sdbAllocRaw(SDB_STB, STB_VER_NUMBER, size);
84
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
85 86

  int32_t dataPos = 0;
87 88 89 90 91 92
  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)
93 94
  SDB_SET_INT32(pRaw, dataPos, pStb->tagVer, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->colVer, _OVER)
S
Shengliang Guan 已提交
95
  SDB_SET_INT32(pRaw, dataPos, pStb->smaVer, _OVER)
96
  SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, _OVER)
97 98 99 100
  SDB_SET_INT64(pRaw, dataPos, pStb->maxdelay[0], _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->maxdelay[1], _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->watermark[0], _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->watermark[1], _OVER)
101 102 103
  SDB_SET_INT32(pRaw, dataPos, pStb->ttl, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, _OVER)
S
Shengliang Guan 已提交
104
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfFuncs, _OVER)
105
  SDB_SET_INT32(pRaw, dataPos, pStb->commentLen, _OVER)
106 107
  SDB_SET_INT32(pRaw, dataPos, pStb->ast1Len, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
108

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

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
120
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
121
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
122 123 124
    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 已提交
125 126
  }

S
Shengliang Guan 已提交
127 128 129 130 131
  for (int32_t i = 0; i < pStb->numOfFuncs; ++i) {
    char *func = taosArrayGet(pStb->pFuncs, i);
    SDB_SET_BINARY(pRaw, dataPos, func, TSDB_FUNC_NAME_LEN, _OVER)
  }

S
sma  
Shengliang Guan 已提交
132
  if (pStb->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
133
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
S
sma  
Shengliang Guan 已提交
134
  }
S
Shengliang Guan 已提交
135

136 137 138
  if (pStb->ast1Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
  }
S
Shengliang Guan 已提交
139

140 141 142
  if (pStb->ast2Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
  }
S
Shengliang Guan 已提交
143

S
Shengliang Guan 已提交
144
  SDB_SET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
145
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
146 147 148

  terrno = 0;

149
_OVER:
150 151 152 153 154
  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 已提交
155

156
  mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
157 158 159
  return pRaw;
}

S
Shengliang Guan 已提交
160
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
161 162
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
163
  int8_t sver = 0;
164
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
165

S
Shengliang Guan 已提交
166
  if (sver != STB_VER_NUMBER) {
S
Shengliang Guan 已提交
167
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
168
    goto _OVER;
S
Shengliang Guan 已提交
169 170
  }

S
Shengliang 已提交
171
  SSdbRow *pRow = sdbAllocRow(sizeof(SStbObj));
172
  if (pRow == NULL) goto _OVER;
173

S
Shengliang Guan 已提交
174
  SStbObj *pStb = sdbGetRowObj(pRow);
175
  if (pStb == NULL) goto _OVER;
S
Shengliang Guan 已提交
176 177

  int32_t dataPos = 0;
178 179 180 181 182 183
  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)
184 185
  SDB_GET_INT32(pRaw, dataPos, &pStb->tagVer, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->colVer, _OVER)
S
Shengliang Guan 已提交
186
  SDB_GET_INT32(pRaw, dataPos, &pStb->smaVer, _OVER)
187
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, _OVER)
188 189 190 191
  SDB_GET_INT64(pRaw, dataPos, &pStb->maxdelay[0], _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->maxdelay[1], _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->watermark[0], _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->watermark[1], _OVER)
192 193 194
  SDB_GET_INT32(pRaw, dataPos, &pStb->ttl, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, _OVER)
S
Shengliang Guan 已提交
195
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfFuncs, _OVER)
196
  SDB_GET_INT32(pRaw, dataPos, &pStb->commentLen, _OVER)
197 198
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
199

wafwerar's avatar
wafwerar 已提交
200 201
  pStb->pColumns = taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchema));
  pStb->pTags = taosMemoryCalloc(pStb->numOfTags, sizeof(SSchema));
S
Shengliang Guan 已提交
202
  pStb->pFuncs = taosArrayInit(pStb->numOfFuncs, TSDB_FUNC_NAME_LEN);
S
Shengliang Guan 已提交
203
  if (pStb->pColumns == NULL || pStb->pTags == NULL || pStb->pFuncs == NULL) {
204
    goto _OVER;
S
Shengliang 已提交
205
  }
S
Shengliang Guan 已提交
206

S
Shengliang Guan 已提交
207 208
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
209
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
210
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
211 212 213
    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 已提交
214 215 216 217
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
218
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
219
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
220 221 222
    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 已提交
223 224
  }

S
Shengliang Guan 已提交
225 226 227 228 229 230
  for (int32_t i = 0; i < pStb->numOfFuncs; ++i) {
    char funcName[TSDB_FUNC_NAME_LEN] = {0};
    SDB_GET_BINARY(pRaw, dataPos, funcName, TSDB_FUNC_NAME_LEN, _OVER)
    taosArrayPush(pStb->pFuncs, funcName);
  }

S
sma  
Shengliang Guan 已提交
231
  if (pStb->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
232
    pStb->comment = taosMemoryCalloc(pStb->commentLen + 1, 1);
233
    if (pStb->comment == NULL) goto _OVER;
wmmhello's avatar
wmmhello 已提交
234
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
S
sma  
Shengliang Guan 已提交
235
  }
S
Shengliang Guan 已提交
236

237 238 239 240 241
  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)
  }
S
Shengliang Guan 已提交
242

243 244 245 246 247
  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 已提交
248
  SDB_GET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
249 250 251

  terrno = 0;

252
_OVER:
253 254
  if (terrno != 0) {
    mError("stb:%s, failed to decode from raw:%p since %s", pStb->name, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
255 256 257 258
    taosMemoryFreeClear(pStb->pColumns);
    taosMemoryFreeClear(pStb->pTags);
    taosMemoryFreeClear(pStb->comment);
    taosMemoryFreeClear(pRow);
259 260
    return NULL;
  }
S
Shengliang Guan 已提交
261

262
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
263 264 265
  return pRow;
}

S
Shengliang Guan 已提交
266
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
267
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
268 269 270
  return 0;
}

S
Shengliang Guan 已提交
271
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
272
  mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
wafwerar's avatar
wafwerar 已提交
273 274 275
  taosMemoryFreeClear(pStb->pColumns);
  taosMemoryFreeClear(pStb->pTags);
  taosMemoryFreeClear(pStb->comment);
S
Shengliang Guan 已提交
276
  taosMemoryFreeClear(pStb->pFuncs);
277 278
  taosMemoryFreeClear(pStb->pAst1);
  taosMemoryFreeClear(pStb->pAst2);
D
dapan1121 已提交
279
  taosArrayDestroy(pStb->pFuncs);
S
Shengliang Guan 已提交
280 281 282
  return 0;
}

S
Shengliang Guan 已提交
283 284
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 已提交
285

S
Shengliang Guan 已提交
286
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
287 288

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
289
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
290
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
291
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
292
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
293 294 295 296 297 298 299 300
    } 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 已提交
301
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
302
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
303
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
304
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
305 306 307 308
    } 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 已提交
309
    }
S
Shengliang Guan 已提交
310 311
  }

312
  if (pOld->commentLen < pNew->commentLen && pNew->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
313
    void *comment = taosMemoryMalloc(pNew->commentLen + 1);
S
Shengliang Guan 已提交
314
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
315
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
316 317 318 319 320 321 322
      pOld->comment = comment;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }
S
Shengliang Guan 已提交
323
  pOld->commentLen = pNew->commentLen;
S
Shengliang Guan 已提交
324

325
  if (pOld->ast1Len < pNew->ast1Len) {
S
Shengliang Guan 已提交
326
    void *pAst1 = taosMemoryMalloc(pNew->ast1Len + 1);
327 328 329 330 331 332 333 334 335 336 337
    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) {
S
Shengliang Guan 已提交
338
    void *pAst2 = taosMemoryMalloc(pNew->ast2Len + 1);
339 340 341 342 343 344 345 346 347 348
    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 已提交
349
  pOld->updateTime = pNew->updateTime;
350 351
  pOld->tagVer = pNew->tagVer;
  pOld->colVer = pNew->colVer;
352
  pOld->smaVer = pNew->smaVer;
353
  pOld->nextColId = pNew->nextColId;
S
Shengliang 已提交
354
  pOld->ttl = pNew->ttl;
355 356 357 358 359 360 361 362
  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));
  }
363
  if (pNew->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
364
    memcpy(pOld->comment, pNew->comment, pNew->commentLen + 1);
S
Shengliang Guan 已提交
365
    pOld->commentLen = pNew->commentLen;
S
Shengliang Guan 已提交
366
  }
367 368
  if (pNew->ast1Len != 0) {
    memcpy(pOld->pAst1, pNew->pAst1, pNew->ast1Len);
S
Shengliang Guan 已提交
369
    pOld->ast1Len = pNew->ast1Len;
370 371 372
  }
  if (pNew->ast2Len != 0) {
    memcpy(pOld->pAst2, pNew->pAst2, pNew->ast2Len);
S
Shengliang Guan 已提交
373
    pOld->ast2Len = pNew->ast2Len;
374
  }
S
Shengliang Guan 已提交
375
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
376 377 378
  return 0;
}

S
Shengliang Guan 已提交
379
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
380
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
381
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
382
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
383 384 385
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
386 387
}

S
Shengliang Guan 已提交
388
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
389 390 391 392
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

393
SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
394 395
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
396

S
Shengliang Guan 已提交
397 398
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
399

S
Shengliang Guan 已提交
400 401
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
402

403
static FORCE_INLINE int32_t schemaExColIdCompare(const void *colId, const void *pSchema) {
H
Hongze Cheng 已提交
404
  if (*(col_id_t *)colId < ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
405
    return -1;
H
Hongze Cheng 已提交
406
  } else if (*(col_id_t *)colId > ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
407 408 409 410 411
    return 1;
  }
  return 0;
}

S
Shengliang Guan 已提交
412
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
413
  SEncoder       encoder = {0};
H
Hongze Cheng 已提交
414 415 416
  int32_t        contLen;
  SName          name = {0};
  SVCreateStbReq req = {0};
H
Hongze Cheng 已提交
417

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

S
Shengliang Guan 已提交
422
  req.name = (char *)tNameGetTableName(&name);
H
Hongze Cheng 已提交
423
  req.suid = pStb->uid;
424
  req.rollup = pStb->ast1Len > 0 ? 1 : 0;
425 426
  // todo
  req.schemaRow.nCols = pStb->numOfColumns;
427
  req.schemaRow.version = pStb->colVer;
428
  req.schemaRow.pSchema = pStb->pColumns;
H
Hongze Cheng 已提交
429
  req.schemaTag.nCols = pStb->numOfTags;
430
  req.schemaTag.version = pStb->tagVer;
H
Hongze Cheng 已提交
431
  req.schemaTag.pSchema = pStb->pTags;
H
more  
Hongze Cheng 已提交
432

C
Cary Xu 已提交
433
  if (req.rollup) {
C
Cary Xu 已提交
434 435
    req.rsmaParam.maxdelay[0] = pStb->maxdelay[0];
    req.rsmaParam.maxdelay[1] = pStb->maxdelay[1];
C
Cary Xu 已提交
436
    if (pStb->ast1Len > 0) {
C
Cary Xu 已提交
437 438
      if (mndConvertRsmaTask(&req.rsmaParam.qmsg[0], &req.rsmaParam.qmsgLen[0], pStb->pAst1, pStb->uid,
                             STREAM_TRIGGER_WINDOW_CLOSE, req.rsmaParam.watermark[0]) < 0) {
439
        goto _err;
C
Cary Xu 已提交
440
      }
C
Cary Xu 已提交
441 442
    }
    if (pStb->ast2Len > 0) {
C
Cary Xu 已提交
443 444
      if (mndConvertRsmaTask(&req.rsmaParam.qmsg[1], &req.rsmaParam.qmsgLen[1], pStb->pAst2, pStb->uid,
                             STREAM_TRIGGER_WINDOW_CLOSE, req.rsmaParam.watermark[1]) < 0) {
445
        goto _err;
C
Cary Xu 已提交
446
      }
C
Cary Xu 已提交
447
    }
H
Hongze Cheng 已提交
448
  }
H
Hongze Cheng 已提交
449
  // get length
wafwerar's avatar
wafwerar 已提交
450 451 452
  int32_t ret = 0;
  tEncodeSize(tEncodeSVCreateStbReq, &req, contLen, ret);
  if (ret < 0) {
453
    goto _err;
H
Hongze Cheng 已提交
454 455
  }

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

wafwerar's avatar
wafwerar 已提交
458
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
459
  if (pHead == NULL) {
H
more  
Hongze Cheng 已提交
460
    terrno = TSDB_CODE_OUT_OF_MEMORY;
461
    goto _err;
H
more  
Hongze Cheng 已提交
462 463
  }

S
Shengliang Guan 已提交
464 465
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
466

S
Shengliang Guan 已提交
467
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
Hongze Cheng 已提交
468 469
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  if (tEncodeSVCreateStbReq(&encoder, &req) < 0) {
470 471
    taosMemoryFreeClear(pHead);
    tEncoderClear(&encoder);
472
    goto _err;
H
Hongze Cheng 已提交
473
  }
H
Hongze Cheng 已提交
474
  tEncoderClear(&encoder);
H
more  
Hongze Cheng 已提交
475

S
Shengliang Guan 已提交
476
  *pContLen = contLen;
C
Cary Xu 已提交
477 478
  taosMemoryFreeClear(req.rsmaParam.qmsg[0]);
  taosMemoryFreeClear(req.rsmaParam.qmsg[1]);
S
Shengliang Guan 已提交
479
  return pHead;
480
_err:
C
Cary Xu 已提交
481 482
  taosMemoryFreeClear(req.rsmaParam.qmsg[0]);
  taosMemoryFreeClear(req.rsmaParam.qmsg[1]);
483
  return NULL;
484 485
}

S
Shengliang Guan 已提交
486
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
487 488 489 490 491
  SName        name = {0};
  SVDropStbReq req = {0};
  int32_t      contLen = 0;
  int32_t      ret = 0;
  SMsgHead    *pHead = NULL;
H
Hongze Cheng 已提交
492
  SEncoder     encoder = {0};
H
Hongze Cheng 已提交
493

S
Shengliang Guan 已提交
494 495 496 497
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

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

H
Hongze Cheng 已提交
499 500 501 502 503
  tEncodeSize(tEncodeSVDropStbReq, &req, contLen, ret);
  if (ret < 0) return NULL;

  contLen += sizeof(SMsgHead);
  pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
504
  if (pHead == NULL) {
505 506 507 508
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
509 510 511 512
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

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

H
Hongze Cheng 已提交
514 515 516
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  tEncodeSVDropStbReq(&encoder, &req);
  tEncoderClear(&encoder);
517

S
Shengliang Guan 已提交
518 519
  *pContLen = contLen;
  return pHead;
520 521
}

522
int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
523
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
524
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
525 526
    return -1;
  }
S
Shengliang Guan 已提交
527

S
Shengliang Guan 已提交
528
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
529
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
530 531
    return -1;
  }
S
Shengliang Guan 已提交
532

S
Shengliang Guan 已提交
533
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
534
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
535 536
    return -1;
  }
S
Shengliang Guan 已提交
537

S
Shengliang Guan 已提交
538
  SField *pField = taosArrayGet(pCreate->pColumns, 0);
S
Shengliang Guan 已提交
539
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
S
Shengliang Guan 已提交
540 541 542 543
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
544
  for (int32_t i = 0; i < pCreate->numOfColumns; ++i) {
545
    SField *pField1 = taosArrayGet(pCreate->pColumns, i);
S
Shengliang Guan 已提交
546
    if (pField1->type < 0) {
S
Shengliang Guan 已提交
547 548 549
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
550
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
551 552 553
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
554
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
555 556 557 558 559 560
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
  }

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
561 562
    SField *pField1 = taosArrayGet(pCreate->pTags, i);
    if (pField1->type < 0) {
S
Shengliang Guan 已提交
563
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
564 565
      return -1;
    }
566
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
567
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
568 569
      return -1;
    }
570
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
571
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
572 573 574
      return -1;
    }
  }
S
Shengliang Guan 已提交
575

S
Shengliang Guan 已提交
576 577 578
  return 0;
}

579
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
580 581
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
582 583 584 585
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
586 587 588 589 590
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) return -1;

  return 0;
}

591
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
592 593
  SSdbRaw *pUndoRaw = mndStbActionEncode(pStb);
  if (pUndoRaw == NULL) return -1;
S
Shengliang Guan 已提交
594 595 596 597
  if (mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
    sdbFreeRaw(pUndoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
598 599 600 601 602
  if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

603
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
604 605
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
S
Shengliang Guan 已提交
606 607 608 609
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
610 611 612 613 614
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

615
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
616
  SSdb   *pSdb = pMnode->pSdb;
617
  SVgObj *pVgroup = NULL;
618
  void   *pIter = NULL;
S
Shengliang Guan 已提交
619
  int32_t contLen;
620 621 622 623

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
624
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
625 626 627 628
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
629
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
630
    if (pReq == NULL) {
631 632 633 634
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
S
Shengliang Guan 已提交
635

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

  return 0;
}

654
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
655
  SSdb   *pSdb = pMnode->pSdb;
656
  SVgObj *pVgroup = NULL;
657
  void   *pIter = NULL;
658 659 660 661

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
662
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
663 664 665 666
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
667
    int32_t contLen = 0;
S
Shengliang Guan 已提交
668
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
669
    if (pReq == NULL) {
670 671 672 673 674
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
675

676 677
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
678
    action.pCont = pReq;
S
Shengliang Guan 已提交
679
    action.contLen = contLen;
H
Hongze Cheng 已提交
680
    action.msgType = TDMT_VND_DROP_STB;
S
Shengliang Guan 已提交
681
    action.acceptableCode = TSDB_CODE_TDB_STB_NOT_EXIST;
682
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
683
      taosMemoryFree(pReq);
684 685 686 687 688 689
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
690 691 692 693

  return 0;
}

S
sma  
Shengliang Guan 已提交
694
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
C
Cary Xu 已提交
695
  for (int32_t col = 0; col < pStb->numOfColumns; ++col) {
S
sma  
Shengliang Guan 已提交
696
    SSchema *pSchema = &pStb->pColumns[col];
C
Cary Xu 已提交
697
    if (strncasecmp(pSchema->name, colName, TSDB_COL_NAME_LEN) == 0) {
S
sma  
Shengliang Guan 已提交
698 699 700 701 702 703
      return pSchema;
    }
  }
  return NULL;
}

704 705 706 707 708 709 710
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;
711 712
  pDst->tagVer = 1;
  pDst->colVer = 1;
S
Shengliang Guan 已提交
713
  pDst->smaVer = 1;
714
  pDst->nextColId = 1;
715 716 717 718
  pDst->maxdelay[0] = pCreate->delay1;
  pDst->maxdelay[1] = pCreate->delay2;
  pDst->watermark[0] = pCreate->watermark1;
  pDst->watermark[1] = pCreate->watermark2;
719 720 721
  pDst->ttl = pCreate->ttl;
  pDst->numOfColumns = pCreate->numOfColumns;
  pDst->numOfTags = pCreate->numOfTags;
S
Shengliang Guan 已提交
722
  pDst->numOfFuncs = pCreate->numOfFuncs;
723
  pDst->commentLen = pCreate->commentLen;
D
dapan1121 已提交
724 725
  pDst->pFuncs = pCreate->pFuncs;
  pCreate->pFuncs = NULL;
726

727
  if (pDst->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
728
    pDst->comment = taosMemoryCalloc(pDst->commentLen + 1, 1);
729
    if (pDst->comment == NULL) {
S
sma  
Shengliang Guan 已提交
730 731 732
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
733
    memcpy(pDst->comment, pCreate->pComment, pDst->commentLen + 1);
S
sma  
Shengliang Guan 已提交
734
  }
S
Shengliang Guan 已提交
735

736 737 738 739
  pDst->ast1Len = pCreate->ast1Len;
  if (pDst->ast1Len > 0) {
    pDst->pAst1 = taosMemoryCalloc(pDst->ast1Len, 1);
    if (pDst->pAst1 == NULL) {
740 741 742
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
743
    memcpy(pDst->pAst1, pCreate->pAst1, pDst->ast1Len);
744 745
  }

746 747 748 749
  pDst->ast2Len = pCreate->ast2Len;
  if (pDst->ast2Len > 0) {
    pDst->pAst2 = taosMemoryCalloc(pDst->ast2Len, 1);
    if (pDst->pAst2 == NULL) {
750 751 752
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
753
    memcpy(pDst->pAst2, pCreate->pAst2, pDst->ast2Len);
754 755
  }

756 757 758
  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 已提交
759 760 761
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
762

763
  for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
S
Shengliang Guan 已提交
764
    SField  *pField = taosArrayGet(pCreate->pColumns, i);
765
    SSchema *pSchema = &pDst->pColumns[i];
S
Shengliang Guan 已提交
766 767
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
768
    pSchema->flags = pField->flags;
S
Shengliang Guan 已提交
769
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
770 771
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
772 773
  }

774
  for (int32_t i = 0; i < pDst->numOfTags; ++i) {
S
Shengliang Guan 已提交
775
    SField  *pField = taosArrayGet(pCreate->pTags, i);
776
    SSchema *pSchema = &pDst->pTags[i];
S
Shengliang Guan 已提交
777 778 779
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
780 781
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
782
  }
783 784 785
  return 0;
}

S
Shengliang Guan 已提交
786
static int32_t mndCreateStb(SMnode *pMnode, SRpcMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
787
  SStbObj stbObj = {0};
S
Shengliang 已提交
788
  int32_t code = -1;
789

790
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pReq);
791
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
792

S
Shengliang Guan 已提交
793
  mDebug("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
794
  if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER;
795
  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) goto _OVER;
796
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
797 798
  code = 0;

799
_OVER:
S
Shengliang Guan 已提交
800
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
801
  return code;
S
Shengliang Guan 已提交
802
}
S
Shengliang Guan 已提交
803

804
int32_t mndAddStbToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
805
  mndTransSetDbName(pTrans, pDb->name, NULL);
806 807 808 809 810 811 812
  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 已提交
813

S
Shengliang Guan 已提交
814
static int32_t mndProcessTtlTimer(SRpcMsg *pReq) {
wmmhello's avatar
wmmhello 已提交
815 816 817 818 819 820 821 822 823
  SMnode *pMnode = pReq->info.node;
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
824 825
    int32_t   contLen = sizeof(SMsgHead) + sizeof(int32_t);
    SMsgHead *pHead = rpcMallocCont(contLen);
wmmhello's avatar
wmmhello 已提交
826
    if (pHead == NULL) {
S
Shengliang Guan 已提交
827
      sdbCancelFetch(pSdb, pVgroup);
wmmhello's avatar
wmmhello 已提交
828 829 830 831 832 833 834
      sdbRelease(pSdb, pVgroup);
      continue;
    }
    pHead->contLen = htonl(contLen);
    pHead->vgId = htonl(pVgroup->vgId);

    int32_t t = taosGetTimestampSec();
S
Shengliang Guan 已提交
835
    *(int32_t *)((char *)pHead + sizeof(SMsgHead)) = htonl(t);
wmmhello's avatar
wmmhello 已提交
836 837

    SRpcMsg rpcMsg = {.msgType = TDMT_VND_DROP_TTL_TABLE, .pCont = pHead, .contLen = contLen};
S
Shengliang Guan 已提交
838
    SEpSet  epSet = mndGetVgroupEpset(pMnode, pVgroup);
wmmhello's avatar
wmmhello 已提交
839
    int32_t code = tmsgSendReq(&epSet, &rpcMsg);
S
Shengliang Guan 已提交
840 841 842 843
    if (code != 0) {
      mError("failed to send ttl time seed, code:0x%x", code);
    } else {
      mDebug("send ttl time seed success, time:%d", t);
wmmhello's avatar
wmmhello 已提交
844 845 846
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
847

wmmhello's avatar
wmmhello 已提交
848 849 850
  return 0;
}

851
static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
852
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
853 854 855 856
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
857

S
Shengliang Guan 已提交
858
  if (tDeserializeSMCreateStbReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
859
    terrno = TSDB_CODE_INVALID_MSG;
860
    goto _OVER;
S
Shengliang Guan 已提交
861
  }
S
Shengliang Guan 已提交
862

S
Shengliang Guan 已提交
863
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
864 865
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
866
    goto _OVER;
S
Shengliang Guan 已提交
867
  }
S
Shengliang Guan 已提交
868

S
Shengliang Guan 已提交
869
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
870
  if (pStb != NULL) {
S
Shengliang Guan 已提交
871 872 873
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
874
      goto _OVER;
S
Shengliang Guan 已提交
875 876
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
877
      goto _OVER;
S
Shengliang Guan 已提交
878
    }
S
Shengliang Guan 已提交
879
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
880
    goto _OVER;
S
Shengliang Guan 已提交
881 882
  }

S
Shengliang Guan 已提交
883
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
884 885
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
886
    goto _OVER;
S
Shengliang Guan 已提交
887 888
  }

889
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
890
    goto _OVER;
S
Shengliang Guan 已提交
891 892
  }

893
  int32_t numOfStbs = -1;
S
Shengliang Guan 已提交
894 895 896 897
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
    goto _OVER;
  }

L
Liu Jicong 已提交
898
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
899 900 901 902
    terrno = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
    goto _OVER;
  }

S
Shengliang Guan 已提交
903
  code = mndCreateStb(pMnode, pReq, &createReq, pDb);
S
Shengliang Guan 已提交
904
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
905

906
_OVER:
S
Shengliang Guan 已提交
907
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
908
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
909 910
  }

S
Shengliang Guan 已提交
911 912
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
913
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
914 915

  return code;
S
Shengliang Guan 已提交
916 917
}

S
Shengliang Guan 已提交
918
static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) {
S
Shengliang Guan 已提交
919 920
  if (pAlter->commentLen >= 0) return 0;
  if (pAlter->ttl != 0) return 0;
S
Shengliang 已提交
921

S
Shengliang Guan 已提交
922 923 924 925
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
926

S
Shengliang Guan 已提交
927 928 929
  for (int32_t i = 0; i < pAlter->numOfFields; ++i) {
    SField *pField = taosArrayGet(pAlter->pFields, i);
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
930 931 932
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
933 934 935 936 937
  }

  return 0;
}

S
Shengliang Guan 已提交
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
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 已提交
959 960
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
961 962 963 964 965 966 967 968 969 970
  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 已提交
971 972
static int32_t mndUpdateStbCommentAndTTL(const SStbObj *pOld, SStbObj *pNew, char *pComment, int32_t commentLen,
                                         int32_t ttl) {
S
Shengliang 已提交
973 974
  if (commentLen > 0) {
    pNew->commentLen = commentLen;
wmmhello's avatar
wmmhello 已提交
975
    pNew->comment = taosMemoryCalloc(1, commentLen + 1);
S
Shengliang 已提交
976 977 978 979
    if (pNew->comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
wmmhello's avatar
wmmhello 已提交
980
    memcpy(pNew->comment, pComment, commentLen + 1);
981
  } else if (commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
982
    pNew->commentLen = 0;
S
Shengliang Guan 已提交
983
  } else {
S
Shengliang 已提交
984
  }
wmmhello's avatar
wmmhello 已提交
985

S
Shengliang 已提交
986 987 988
  if (ttl >= 0) {
    pNew->ttl = ttl;
  }
S
Shengliang 已提交
989 990 991 992 993 994 995

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

S
Shengliang Guan 已提交
996
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006
  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 已提交
1007 1008 1009 1010 1011
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1012
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
1013
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1014
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1015
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1016 1017 1018
      return -1;
    }

S
Shengliang 已提交
1019
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1020
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1021 1022 1023
      return -1;
    }

S
Shengliang Guan 已提交
1024 1025 1026 1027
    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 已提交
1028 1029
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1030 1031

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

1034
  pNew->tagVer++;
S
Shengliang Guan 已提交
1035 1036 1037
  return 0;
}

1038
static int32_t mndDropSuperTableTag(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *tagName) {
S
Shengliang Guan 已提交
1039 1040 1041 1042 1043 1044
  int32_t tag = mndFindSuperTableTagIndex(pOld, tagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1045 1046 1047 1048 1049
  col_id_t colId = pOld->pTags[tag].colId;
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

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

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

1057
  pNew->tagVer++;
S
Shengliang Guan 已提交
1058 1059 1060 1061
  mDebug("stb:%s, start to drop tag %s", pNew->name, tagName);
  return 0;
}

1062
static int32_t mndAlterStbTagName(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, SArray *pFields) {
S
Shengliang Guan 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
  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 已提交
1074 1075 1076 1077 1078 1079
  int32_t tag = mndFindSuperTableTagIndex(pOld, oldTagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1080 1081 1082 1083 1084
  col_id_t colId = pOld->pTags[tag].colId;
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1085
  if (mndFindSuperTableTagIndex(pOld, newTagName) >= 0) {
S
Shengliang Guan 已提交
1086
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1087 1088 1089
    return -1;
  }

S
Shengliang Guan 已提交
1090 1091
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    return -1;
  }

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

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

1102
  pNew->tagVer++;
S
Shengliang Guan 已提交
1103 1104 1105 1106
  mDebug("stb:%s, start to modify tag %s to %s", pNew->name, oldTagName, newTagName);
  return 0;
}

1107
static int32_t mndAlterStbTagBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1108
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1109 1110 1111 1112 1113
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1114 1115 1116 1117 1118
  col_id_t colId = pOld->pTags[tag].colId;
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1119 1120 1121 1122 1123 1124
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pTag = pNew->pTags + tag;

S
Shengliang Guan 已提交
1125 1126 1127 1128 1129
  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 已提交
1130
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1131 1132 1133 1134
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1135
  pTag->bytes = pField->bytes;
1136
  pNew->tagVer++;
S
Shengliang Guan 已提交
1137

S
Shengliang Guan 已提交
1138
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1139 1140 1141
  return 0;
}

S
Shengliang Guan 已提交
1142
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1143 1144 1145 1146 1147
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

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

S
Shengliang Guan 已提交
1153
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1154
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1155
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1156
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1157 1158 1159
      return -1;
    }

S
Shengliang 已提交
1160
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1161
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1162 1163 1164
      return -1;
    }

S
Shengliang Guan 已提交
1165 1166 1167 1168
    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 已提交
1169 1170
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1171 1172

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

1175
  pNew->colVer++;
S
Shengliang Guan 已提交
1176 1177 1178
  return 0;
}

1179
static int32_t mndDropSuperTableColumn(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *colName) {
S
Shengliang Guan 已提交
1180
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
S
Shengliang Guan 已提交
1181
  if (col < 0) {
S
Shengliang Guan 已提交
1182 1183 1184 1185
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
  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;
  }

1196
  col_id_t colId = pOld->pColumns[col].colId;
1197 1198 1199 1200
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1201 1202 1203 1204 1205
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

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

1208
  pNew->colVer++;
S
Shengliang Guan 已提交
1209 1210 1211 1212
  mDebug("stb:%s, start to drop col %s", pNew->name, colName);
  return 0;
}

1213
static int32_t mndAlterStbColumnBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1214
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1215 1216 1217 1218 1219 1220 1221
  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 已提交
1222
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1223 1224 1225 1226 1227 1228 1229
  }

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

1230
  col_id_t colId = pOld->pColumns[col].colId;
1231 1232 1233 1234
  if (mndCheckColAndTagModifiable(pMnode, pOld->uid, colId) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1235 1236 1237 1238 1239
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pCol = pNew->pColumns + col;
S
Shengliang Guan 已提交
1240 1241
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1242 1243 1244
    return -1;
  }

S
Shengliang Guan 已提交
1245
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1246
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1247 1248 1249
    return -1;
  }

S
Shengliang Guan 已提交
1250
  pCol->bytes = pField->bytes;
1251
  pNew->colVer++;
S
Shengliang Guan 已提交
1252

S
Shengliang Guan 已提交
1253
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1254 1255 1256
  return 0;
}

S
Shengliang Guan 已提交
1257
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1258 1259
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
1260 1261 1262 1263
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1264
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
S
Shengliang Guan 已提交
1265 1266 1267 1268

  return 0;
}

S
Shengliang Guan 已提交
1269
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1270 1271
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
S
Shengliang Guan 已提交
1272 1273 1274 1275
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1276 1277 1278 1279 1280
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
1281
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1282 1283 1284 1285 1286 1287 1288 1289
  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 已提交
1290
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
1291 1292 1293 1294
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
1295
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304
    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 已提交
1305
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1306
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1307
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

D
dapan1121 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
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;
}

D
dapan1121 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
static int32_t mndBuildStbCfgImp(SDbObj *pDb, SStbObj *pStb, const char *tbName, STableCfgRsp *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->numOfTags = pStb->numOfTags;
  pRsp->numOfColumns = pStb->numOfColumns;
  pRsp->tableType = TSDB_SUPER_TABLE;
D
dapan1121 已提交
1381 1382 1383 1384
  pRsp->delay1 = pStb->maxdelay[0];
  pRsp->delay2 = pStb->maxdelay[1];
  pRsp->watermark1 = pStb->watermark[0];
  pRsp->watermark2 = pStb->watermark[1];
D
dapan1121 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  pRsp->ttl = pStb->ttl;
  pRsp->commentLen = pStb->commentLen;
  if (pStb->commentLen > 0) {
    pRsp->pComment = strdup(pStb->comment);
  }

  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;
  }

S
Shengliang Guan 已提交
1409
  if (pStb->numOfFuncs > 0) {
D
dapan1121 已提交
1410 1411
    pRsp->pFuncs = taosArrayDup(pStb->pFuncs);
  }
1412

D
dapan1121 已提交
1413 1414 1415 1416
  taosRUnLockLatch(&pStb->lock);
  return 0;
}

L
Liu Jicong 已提交
1417 1418
static int32_t mndBuildStbSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp,
                                 int32_t *smaVer) {
D
dapan1121 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
  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 已提交
1431
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
D
dapan1121 已提交
1432 1433 1434
    return -1;
  }

D
dapan1121 已提交
1435 1436 1437 1438
  if (smaVer) {
    *smaVer = pStb->smaVer;
  }

D
dapan1121 已提交
1439 1440 1441 1442 1443 1444
  int32_t code = mndBuildStbSchemaImp(pDb, pStb, tbName, pRsp);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}

D
dapan1121 已提交
1445
static int32_t mndBuildStbCfg(SMnode *pMnode, const char *dbFName, const char *tbName, STableCfgRsp *pRsp) {
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
  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);
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
    return -1;
  }
D
dapan1121 已提交
1461

1462
  int32_t code = mndBuildStbCfgImp(pDb, pStb, tbName, pRsp);
D
dapan1121 已提交
1463

1464 1465 1466 1467
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}
D
dapan1121 已提交
1468

1469 1470
static int32_t mndBuildSMAlterStbRsp(SDbObj *pDb, const SMAlterStbReq *pAlter, SStbObj *pObj, void **pCont,
                                     int32_t *pLen) {
1471
  int32_t       ret;
D
dapan1121 已提交
1472
  SEncoder      ec = {0};
1473
  uint32_t      contLen = 0;
D
dapan1121 已提交
1474
  SMAlterStbRsp alterRsp = {0};
1475
  SName         name = {0};
D
dapan1121 已提交
1476 1477 1478 1479 1480 1481 1482
  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;
  }
1483

D
dapan1121 已提交
1484
  ret = mndBuildStbSchemaImp(pDb, pObj, name.tname, alterRsp.pMeta);
D
dapan1121 已提交
1485 1486 1487 1488
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }
1489

D
dapan1121 已提交
1490 1491 1492 1493 1494 1495
  tEncodeSize(tEncodeSMAlterStbRsp, &alterRsp, contLen, ret);
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }

1496
  void *cont = taosMemoryMalloc(contLen);
D
dapan1121 已提交
1497 1498 1499 1500 1501 1502 1503 1504
  tEncoderInit(&ec, cont, contLen);
  tEncodeSMAlterStbRsp(&ec, &alterRsp);
  tEncoderClear(&ec);

  tFreeSMAlterStbRsp(&alterRsp);

  *pCont = cont;
  *pLen = contLen;
1505

D
dapan1121 已提交
1506 1507 1508
  return 0;
}

S
Shengliang Guan 已提交
1509
static int32_t mndAlterStb(SMnode *pMnode, SRpcMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
1510 1511 1512 1513 1514
  bool    needRsp = true;
  int32_t code = -1;
  STrans *pTrans = NULL;
  SField *pField0 = NULL;

S
Shengliang Guan 已提交
1515 1516 1517
  SStbObj stbObj = {0};
  taosRLockLatch(&pOld->lock);
  memcpy(&stbObj, pOld, sizeof(SStbObj));
1518
  taosRUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
1519 1520 1521
  stbObj.pColumns = NULL;
  stbObj.pTags = NULL;
  stbObj.updateTime = taosGetTimestampMs();
D
dapan1121 已提交
1522
  stbObj.lock = 0;
S
Shengliang Guan 已提交
1523

S
Shengliang Guan 已提交
1524
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
1525
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
1526
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1527
      break;
S
Shengliang Guan 已提交
1528
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang 已提交
1529
      pField0 = taosArrayGet(pAlter->pFields, 0);
1530
      code = mndDropSuperTableTag(pMnode, pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1531
      break;
S
Shengliang Guan 已提交
1532
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
1533
      code = mndAlterStbTagName(pMnode, pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
1534
      break;
S
Shengliang Guan 已提交
1535
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang 已提交
1536
      pField0 = taosArrayGet(pAlter->pFields, 0);
1537
      code = mndAlterStbTagBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1538 1539
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
1540
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1541 1542
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang 已提交
1543
      pField0 = taosArrayGet(pAlter->pFields, 0);
1544
      code = mndDropSuperTableColumn(pMnode, pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1545
      break;
S
Shengliang Guan 已提交
1546
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang 已提交
1547
      pField0 = taosArrayGet(pAlter->pFields, 0);
1548
      code = mndAlterStbColumnBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1549
      break;
S
Shengliang 已提交
1550
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
D
dapan1121 已提交
1551
      needRsp = false;
S
Shengliang 已提交
1552
      code = mndUpdateStbCommentAndTTL(pOld, &stbObj, pAlter->comment, pAlter->commentLen, pAlter->ttl);
S
Shengliang 已提交
1553
      break;
S
Shengliang Guan 已提交
1554
    default:
D
dapan1121 已提交
1555
      needRsp = false;
S
Shengliang 已提交
1556
      terrno = TSDB_CODE_OPS_NOT_SUPPORT;
S
Shengliang Guan 已提交
1557 1558 1559
      break;
  }

1560
  if (code != 0) goto _OVER;
S
Shengliang Guan 已提交
1561 1562

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

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

D
dapan1121 已提交
1569
  if (needRsp) {
1570
    void   *pCont = NULL;
D
dapan1121 已提交
1571
    int32_t contLen = 0;
1572
    if (mndBuildSMAlterStbRsp(pDb, pAlter, &stbObj, &pCont, &contLen) != 0) goto _OVER;
D
dapan1121 已提交
1573 1574
    mndTransSetRpcRsp(pTrans, pCont, contLen);
  }
1575

1576 1577 1578 1579
  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 已提交
1580 1581

  code = 0;
S
Shengliang Guan 已提交
1582

1583
_OVER:
S
Shengliang Guan 已提交
1584
  mndTransDrop(pTrans);
wafwerar's avatar
wafwerar 已提交
1585 1586
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
S
Shengliang Guan 已提交
1587 1588
  return code;
}
S
Shengliang Guan 已提交
1589

1590
static int32_t mndProcessAlterStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
1591
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1592 1593 1594 1595
  int32_t       code = -1;
  SDbObj       *pDb = NULL;
  SStbObj      *pStb = NULL;
  SMAlterStbReq alterReq = {0};
S
Shengliang Guan 已提交
1596

S
Shengliang Guan 已提交
1597
  if (tDeserializeSMAlterStbReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1598
    terrno = TSDB_CODE_INVALID_MSG;
1599
    goto _OVER;
S
Shengliang Guan 已提交
1600
  }
S
Shengliang Guan 已提交
1601

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

S
Shengliang Guan 已提交
1605
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1606 1607
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
1608
    goto _OVER;
S
Shengliang Guan 已提交
1609 1610
  }

S
Shengliang Guan 已提交
1611
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1612 1613
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1614
    goto _OVER;
S
Shengliang Guan 已提交
1615
  }
S
Shengliang Guan 已提交
1616

1617 1618 1619 1620
  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);
1621 1622 1623 1624
    code = 0;
    goto _OVER;
  }

1625
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
1626
    goto _OVER;
S
Shengliang Guan 已提交
1627 1628
  }

S
Shengliang Guan 已提交
1629
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
S
Shengliang Guan 已提交
1630
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1631

1632
_OVER:
S
Shengliang Guan 已提交
1633
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1634
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1635 1636
  }

S
Shengliang Guan 已提交
1637 1638
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1639
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1640 1641

  return code;
S
Shengliang Guan 已提交
1642
}
S
Shengliang Guan 已提交
1643

S
Shengliang Guan 已提交
1644 1645 1646
static int32_t mndSetDropStbRedoLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
1647 1648 1649 1650
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1651 1652 1653 1654 1655 1656 1657 1658
  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;
S
Shengliang Guan 已提交
1659 1660 1661 1662
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1663 1664 1665 1666 1667
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
1668 1669 1670 1671
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 已提交
1672

S
Shengliang Guan 已提交
1673 1674 1675
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1676
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
1677 1678 1679 1680
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
1681
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1682
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
    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 已提交
1697
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1708
static int32_t mndDropStb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1709
  int32_t code = -1;
S
Shengliang Guan 已提交
1710
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq);
1711
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1712

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

1716 1717 1718
  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;
1719
  if (mndDropSmasByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
1720
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
1721

S
Shengliang Guan 已提交
1722 1723
  code = 0;

1724
_OVER:
S
Shengliang Guan 已提交
1725
  mndTransDrop(pTrans);
S
Shengliang 已提交
1726
  return code;
S
Shengliang Guan 已提交
1727 1728
}

1729
static int32_t mndProcessDropStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
1730
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1731 1732 1733
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1734
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
1735

S
Shengliang Guan 已提交
1736
  if (tDeserializeSMDropStbReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1737
    terrno = TSDB_CODE_INVALID_MSG;
1738
    goto _OVER;
S
Shengliang Guan 已提交
1739
  }
S
Shengliang Guan 已提交
1740

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

S
Shengliang Guan 已提交
1743
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1744
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1745 1746
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1747
      code = 0;
1748
      goto _OVER;
S
Shengliang Guan 已提交
1749 1750
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1751
      goto _OVER;
S
Shengliang Guan 已提交
1752 1753 1754
    }
  }

S
Shengliang Guan 已提交
1755
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1756 1757
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1758
    goto _OVER;
S
Shengliang Guan 已提交
1759 1760
  }

1761
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
1762
    goto _OVER;
S
Shengliang Guan 已提交
1763 1764 1765
  }

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

1768
_OVER:
S
Shengliang Guan 已提交
1769
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1770
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1771 1772
  }

S
Shengliang Guan 已提交
1773 1774 1775
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
S
Shengliang Guan 已提交
1776
}
S
Shengliang Guan 已提交
1777

S
Shengliang Guan 已提交
1778 1779
static int32_t mndProcessTableMetaReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1780 1781 1782
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
1783

S
Shengliang Guan 已提交
1784
  if (tDeserializeSTableInfoReq(pReq->pCont, pReq->contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
1785
    terrno = TSDB_CODE_INVALID_MSG;
1786
    goto _OVER;
S
Shengliang Guan 已提交
1787
  }
D
dapan 已提交
1788

D
dapan1121 已提交
1789 1790 1791
  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) {
1792
      goto _OVER;
D
dapan1121 已提交
1793
    }
D
dapan1121 已提交
1794 1795 1796
  } 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) {
1797
      goto _OVER;
D
dapan1121 已提交
1798
    }
D
dapan1121 已提交
1799 1800
  } else {
    mDebug("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
D
dapan1121 已提交
1801
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp, NULL) != 0) {
1802
      goto _OVER;
D
dapan1121 已提交
1803
    }
S
Shengliang Guan 已提交
1804
  }
S
Shengliang Guan 已提交
1805

S
Shengliang Guan 已提交
1806 1807 1808
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
1809
    goto _OVER;
S
Shengliang Guan 已提交
1810
  }
S
Shengliang Guan 已提交
1811

S
Shengliang Guan 已提交
1812
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1813 1814
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1815
    goto _OVER;
S
Shengliang Guan 已提交
1816
  }
D
dapan 已提交
1817

S
Shengliang Guan 已提交
1818
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1819 1820
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = rspLen;
S
Shengliang Guan 已提交
1821
  code = 0;
S
Shengliang Guan 已提交
1822

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

1825
_OVER:
S
Shengliang Guan 已提交
1826 1827 1828
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1829

S
Shengliang Guan 已提交
1830 1831 1832
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1833

D
dapan1121 已提交
1834
static int32_t mndProcessTableCfgReq(SRpcMsg *pReq) {
1835 1836 1837 1838
  SMnode      *pMnode = pReq->info.node;
  int32_t      code = -1;
  STableCfgReq cfgReq = {0};
  STableCfgRsp cfgRsp = {0};
D
dapan1121 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889

  if (tDeserializeSTableCfgReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

  if (0 == strcmp(cfgReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
    mDebug("information_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
    if (mndBuildInsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else if (0 == strcmp(cfgReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
    mDebug("performance_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
    if (mndBuildPerfsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else {
    mDebug("stb:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
    if (mndBuildStbCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  }

  int32_t rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

  void *pRsp = rpcMallocCont(rspLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = rspLen;
  code = 0;

  mTrace("%s.%s, cfg is retrieved", cfgReq.dbFName, cfgReq.tbName);

_OVER:
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve cfg since %s", cfgReq.dbFName, cfgReq.tbName, terrstr());
  }

  tFreeSTableCfgRsp(&cfgRsp);
  return code;
}

D
dapan1121 已提交
1890
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t numOfStbs, void **ppRsp,
S
Shengliang Guan 已提交
1891
                           int32_t *pRspLen) {
D
dapan1121 已提交
1892
  SSTbHbRsp hbRsp = {0};
D
dapan1121 已提交
1893 1894
  hbRsp.pMetaRsp = taosArrayInit(numOfStbs, sizeof(STableMetaRsp));
  if (hbRsp.pMetaRsp == NULL) {
S
Shengliang Guan 已提交
1895 1896 1897
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
1898

D
dapan1121 已提交
1899
  hbRsp.pIndexRsp = taosArrayInit(numOfStbs, sizeof(STableIndexRsp));
D
dapan1121 已提交
1900
  if (NULL == hbRsp.pIndexRsp) {
D
dapan1121 已提交
1901
    taosArrayDestroy(hbRsp.pMetaRsp);
D
dapan1121 已提交
1902 1903 1904
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
1905

S
Shengliang Guan 已提交
1906
  for (int32_t i = 0; i < numOfStbs; ++i) {
D
dapan1121 已提交
1907
    SSTableVersion *pStbVersion = &pStbVersions[i];
S
Shengliang Guan 已提交
1908 1909 1910
    pStbVersion->suid = be64toh(pStbVersion->suid);
    pStbVersion->sversion = ntohs(pStbVersion->sversion);
    pStbVersion->tversion = ntohs(pStbVersion->tversion);
D
dapan1121 已提交
1911
    pStbVersion->smaVer = ntohl(pStbVersion->smaVer);
S
Shengliang Guan 已提交
1912

S
Shengliang Guan 已提交
1913
    STableMetaRsp metaRsp = {0};
L
Liu Jicong 已提交
1914
    int32_t       smaVer = 0;
S
Shengliang Guan 已提交
1915
    mDebug("stb:%s.%s, start to retrieve meta", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
1916
    if (mndBuildStbSchema(pMnode, pStbVersion->dbFName, pStbVersion->stbName, &metaRsp, &smaVer) != 0) {
S
Shengliang Guan 已提交
1917 1918
      metaRsp.numOfColumns = -1;
      metaRsp.suid = pStbVersion->suid;
D
dapan1121 已提交
1919
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
1920
      continue;
D
dapan 已提交
1921
    }
S
Shengliang Guan 已提交
1922

D
dapan1121 已提交
1923
    if (pStbVersion->sversion != metaRsp.sversion || pStbVersion->tversion != metaRsp.tversion) {
D
dapan1121 已提交
1924
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
1925 1926
    } else {
      tFreeSTableMetaRsp(&metaRsp);
S
Shengliang Guan 已提交
1927
    }
D
dapan1121 已提交
1928

D
dapan1121 已提交
1929
    if (pStbVersion->smaVer && pStbVersion->smaVer != smaVer) {
L
Liu Jicong 已提交
1930 1931
      bool           exist = false;
      char           tbFName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
1932
      STableIndexRsp indexRsp = {0};
D
dapan1121 已提交
1933 1934 1935 1936 1937
      indexRsp.pIndex = taosArrayInit(10, sizeof(STableIndexInfo));
      if (NULL == indexRsp.pIndex) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
L
Liu Jicong 已提交
1938

D
dapan1121 已提交
1939
      sprintf(tbFName, "%s.%s", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
1940
      int32_t code = mndGetTableSma(pMnode, tbFName, &indexRsp, &exist);
D
dapan1121 已提交
1941
      if (code || !exist) {
D
dapan1121 已提交
1942 1943 1944
        indexRsp.suid = pStbVersion->suid;
        indexRsp.version = -1;
        indexRsp.pIndex = NULL;
D
dapan1121 已提交
1945
      }
D
dapan1121 已提交
1946

D
dapan1121 已提交
1947 1948
      strcpy(indexRsp.dbFName, pStbVersion->dbFName);
      strcpy(indexRsp.tbName, pStbVersion->stbName);
D
dapan1121 已提交
1949 1950

      taosArrayPush(hbRsp.pIndexRsp, &indexRsp);
D
dapan1121 已提交
1951
    }
S
Shengliang Guan 已提交
1952
  }
S
Shengliang Guan 已提交
1953

D
dapan1121 已提交
1954
  int32_t rspLen = tSerializeSSTbHbRsp(NULL, 0, &hbRsp);
S
Shengliang Guan 已提交
1955
  if (rspLen < 0) {
D
dapan1121 已提交
1956
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
1957 1958
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
1959 1960
  }

wafwerar's avatar
wafwerar 已提交
1961
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
1962
  if (pRsp == NULL) {
D
dapan1121 已提交
1963
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
1964 1965
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
1966 1967
  }

D
dapan1121 已提交
1968 1969
  tSerializeSSTbHbRsp(pRsp, rspLen, &hbRsp);
  tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
1970 1971
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1972 1973 1974
  return 0;
}

1975
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1976
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1977 1978 1979 1980 1981 1982
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1983
  int32_t numOfStbs = 0;
1984
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1985
  while (1) {
S
Shengliang Guan 已提交
1986
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1987
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1988 1989
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1990
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1991
      numOfStbs++;
S
Shengliang Guan 已提交
1992 1993
    }

S
Shengliang Guan 已提交
1994
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1995 1996
  }

S
Shengliang Guan 已提交
1997
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1998
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1999 2000 2001
  return 0;
}

L
Liu Jicong 已提交
2002 2003 2004 2005 2006 2007 2008 2009
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 已提交
2010 2011
  int32_t pos = -1;
  int32_t num = 0;
L
Liu Jicong 已提交
2012 2013
  for (pos = 0; stbFullName[pos] != 0; ++pos) {
    if (stbFullName[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
2014 2015 2016 2017
    if (num == 2) break;
  }

  if (num == 2) {
L
Liu Jicong 已提交
2018
    tstrncpy(dst, stbFullName + pos + 1, dstSize);
S
Shengliang Guan 已提交
2019 2020 2021
  }
}

S
Shengliang Guan 已提交
2022 2023
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2024
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2025 2026 2027
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
2028

H
Hongze Cheng 已提交
2029
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
2030 2031
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
2032
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
2033
  }
S
Shengliang Guan 已提交
2034

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

H
Haojun Liao 已提交
2039
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
2040
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2041 2042 2043 2044 2045
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
2046
    SName name = {0};
H
Hongze Cheng 已提交
2047
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
L
Liu Jicong 已提交
2048
    mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
2049
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
2050

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

H
Hongze Cheng 已提交
2054 2055
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
2056 2057 2058
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

2059
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
H
Hongze Cheng 已提交
2060
    colDataAppend(pColInfo, numOfRows, (const char *)db, false);
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073

    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

wmmhello's avatar
wmmhello 已提交
2074 2075 2076 2077 2078
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
    if (pStb->commentLen > 0) {
      char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, pStb->comment);
      colDataAppend(pColInfo, numOfRows, comment, false);
2079
    } else if (pStb->commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
2080 2081 2082 2083 2084
      char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, "");
      colDataAppend(pColInfo, numOfRows, comment, false);
    } else {
      colDataAppendNULL(pColInfo, numOfRows);
S
Shengliang Guan 已提交
2085
    }
H
Haojun Liao 已提交
2086

S
Shengliang Guan 已提交
2087
    numOfRows++;
S
Shengliang Guan 已提交
2088
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2089 2090
  }

H
Haojun Liao 已提交
2091 2092 2093 2094
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

2095
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
2096 2097 2098
  return numOfRows;
}

S
Shengliang Guan 已提交
2099
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
2100 2101
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
2102
}