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

S
Shengliang Guan 已提交
16
#include "mndStb.h"
S
Shengliang Guan 已提交
17
#include "mndAuth.h"
S
Shengliang Guan 已提交
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndDnode.h"
S
Shengliang Guan 已提交
20
#include "mndInfoSchema.h"
D
dapan1121 已提交
21
#include "mndPerfSchema.h"
S
Shengliang Guan 已提交
22 23 24 25
#include "mndMnode.h"
#include "mndShow.h"
#include "mndTrans.h"
#include "mndUser.h"
26
#include "mndVgroup.h"
S
Shengliang Guan 已提交
27
#include "tname.h"
S
Shengliang Guan 已提交
28

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

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

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

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

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

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

S
Shengliang Guan 已提交
69
void mndCleanupStb(SMnode *pMnode) {}
S
Shengliang Guan 已提交
70

71
SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
72 73
  terrno = TSDB_CODE_OUT_OF_MEMORY;

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

  int32_t dataPos = 0;
80 81 82 83 84 85 86
  SDB_SET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, STB_ENCODE_OVER)
  SDB_SET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, STB_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->createdTime, STB_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->updateTime, STB_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->uid, STB_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->dbUid, STB_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->version, STB_ENCODE_OVER)
S
Shengliang Guan 已提交
87
  SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, STB_ENCODE_OVER)
S
sma  
Shengliang Guan 已提交
88 89 90
  SDB_SET_INT32(pRaw, dataPos, (int32_t)(pStb->xFilesFactor * 10000), STB_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->aggregationMethod, STB_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->delay, STB_ENCODE_OVER)
S
sma  
Shengliang Guan 已提交
91
  SDB_SET_INT32(pRaw, dataPos, pStb->ttl, STB_ENCODE_OVER)
92 93
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, STB_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, STB_ENCODE_OVER)
S
sma  
Shengliang Guan 已提交
94
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfSmas, STB_ENCODE_OVER)
S
sma  
Shengliang Guan 已提交
95
  SDB_SET_INT32(pRaw, dataPos, pStb->commentLen, STB_ENCODE_OVER)
S
Shengliang Guan 已提交
96

S
Shengliang Guan 已提交
97 98 99
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, STB_ENCODE_OVER)
100
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, STB_ENCODE_OVER)
S
Shengliang Guan 已提交
101 102 103 104 105 106
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, STB_ENCODE_OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, STB_ENCODE_OVER)
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
107
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, STB_ENCODE_OVER)
108
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, STB_ENCODE_OVER)
109 110
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, STB_ENCODE_OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, STB_ENCODE_OVER)
S
Shengliang Guan 已提交
111 112
  }

S
sma  
Shengliang Guan 已提交
113
  for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
S
sma  
Shengliang Guan 已提交
114 115
    SSchema *pSchema = &pStb->pSmas[i];
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, STB_ENCODE_OVER)
116
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, STB_ENCODE_OVER)
S
sma  
Shengliang Guan 已提交
117 118 119 120 121 122 123
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, STB_ENCODE_OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, STB_ENCODE_OVER)
  }

  if (pStb->commentLen > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, STB_ENCODE_OVER)
  }
124 125 126 127 128 129 130 131 132 133 134
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, STB_ENCODE_OVER)
  SDB_SET_DATALEN(pRaw, dataPos, STB_ENCODE_OVER)

  terrno = 0;

STB_ENCODE_OVER:
  if (terrno != 0) {
    mError("stb:%s, failed to encode to raw:%p since %s", pStb->name, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
135

136
  mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
137 138 139
  return pRaw;
}

S
Shengliang Guan 已提交
140
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
141 142
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
143
  int8_t sver = 0;
144
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
145

S
Shengliang Guan 已提交
146
  if (sver != TSDB_STB_VER_NUMBER) {
S
Shengliang Guan 已提交
147
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
148
    goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
149 150
  }

S
Shengliang 已提交
151
  SSdbRow *pRow = sdbAllocRow(sizeof(SStbObj));
152 153
  if (pRow == NULL) goto STB_DECODE_OVER;

S
Shengliang Guan 已提交
154
  SStbObj *pStb = sdbGetRowObj(pRow);
155
  if (pStb == NULL) goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
156 157

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

wafwerar's avatar
wafwerar 已提交
177 178 179
  pStb->pColumns = taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchema));
  pStb->pTags = taosMemoryCalloc(pStb->numOfTags, sizeof(SSchema));
  pStb->pSmas = taosMemoryCalloc(pStb->numOfSmas, sizeof(SSchema));
S
sma  
Shengliang Guan 已提交
180
  if (pStb->pColumns == NULL || pStb->pTags == NULL || pStb->pSmas == NULL) {
S
Shengliang 已提交
181 182
    goto STB_DECODE_OVER;
  }
S
Shengliang Guan 已提交
183

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

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
194
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, STB_DECODE_OVER)
195
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, STB_DECODE_OVER)
196 197
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, STB_DECODE_OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, STB_DECODE_OVER)
S
Shengliang Guan 已提交
198 199
  }

S
sma  
Shengliang Guan 已提交
200 201 202
  for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
    SSchema *pSchema = &pStb->pSmas[i];
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, STB_DECODE_OVER)
203
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, STB_DECODE_OVER)
S
sma  
Shengliang Guan 已提交
204 205 206 207
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, STB_DECODE_OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, STB_DECODE_OVER)
  }

S
sma  
Shengliang Guan 已提交
208
  if (pStb->commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
209
    pStb->comment = taosMemoryCalloc(pStb->commentLen, 1);
S
sma  
Shengliang Guan 已提交
210 211 212
    if (pStb->comment == NULL) goto STB_DECODE_OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen, STB_DECODE_OVER)
  }
213 214 215 216 217 218 219
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE, STB_DECODE_OVER)

  terrno = 0;

STB_DECODE_OVER:
  if (terrno != 0) {
    mError("stb:%s, failed to decode from raw:%p since %s", pStb->name, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
220 221 222 223
    taosMemoryFreeClear(pStb->pColumns);
    taosMemoryFreeClear(pStb->pTags);
    taosMemoryFreeClear(pStb->comment);
    taosMemoryFreeClear(pRow);
224 225
    return NULL;
  }
S
Shengliang Guan 已提交
226

227
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
228 229 230
  return pRow;
}

S
Shengliang Guan 已提交
231
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
232
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
233 234 235
  return 0;
}

S
Shengliang Guan 已提交
236
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
237
  mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
wafwerar's avatar
wafwerar 已提交
238 239 240
  taosMemoryFreeClear(pStb->pColumns);
  taosMemoryFreeClear(pStb->pTags);
  taosMemoryFreeClear(pStb->comment);
S
Shengliang Guan 已提交
241 242 243
  return 0;
}

S
Shengliang Guan 已提交
244 245
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 已提交
246

S
Shengliang Guan 已提交
247
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
248 249

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
250
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
251
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
252
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
253
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
254 255 256 257 258 259 260 261
    } 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 已提交
262
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
263
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
264
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
265
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
266 267 268 269
    } 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 已提交
270
    }
S
Shengliang Guan 已提交
271 272
  }

S
Shengliang Guan 已提交
273
  if (pOld->numOfSmas < pNew->numOfSmas) {
wafwerar's avatar
wafwerar 已提交
274
    void *pSmas = taosMemoryMalloc(pNew->numOfSmas * sizeof(SSchema));
S
Shengliang Guan 已提交
275
    if (pSmas != NULL) {
wafwerar's avatar
wafwerar 已提交
276
      taosMemoryFree(pOld->pSmas);
S
Shengliang Guan 已提交
277 278 279 280 281 282 283 284 285
      pOld->pSmas = pSmas;
    } 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->commentLen < pNew->commentLen) {
wafwerar's avatar
wafwerar 已提交
286
    void *comment = taosMemoryMalloc(pNew->commentLen);
S
Shengliang Guan 已提交
287
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
288
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
289 290 291 292 293 294 295 296
      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 已提交
297 298
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
S
Shengliang Guan 已提交
299
  pOld->nextColId = pNew->nextColId;
S
Shengliang Guan 已提交
300 301
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
302 303
  memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
304 305 306
  if (pNew->commentLen != 0) {
    memcpy(pOld->comment, pNew->comment, TSDB_STB_COMMENT_LEN);
  }
S
Shengliang Guan 已提交
307
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
308 309 310
  return 0;
}

S
Shengliang Guan 已提交
311
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
312
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
313
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
314
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
315 316 317
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
318 319
}

S
Shengliang Guan 已提交
320
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
321 322 323 324
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

S
Shengliang Guan 已提交
325
static SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
326 327
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
328

S
Shengliang Guan 已提交
329 330
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
331

S
Shengliang Guan 已提交
332 333
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
334

C
Cary Xu 已提交
335 336 337 338 339 340 341 342 343
static FORCE_INLINE int schemaExColIdCompare(const void *colId, const void *pSchema) {
  if (*(col_id_t *)colId < ((SSchemaEx *)pSchema)->colId) {
    return -1;
  } else if (*(col_id_t *)colId > ((SSchemaEx *)pSchema)->colId) {
    return 1;
  }
  return 0;
}

S
Shengliang Guan 已提交
344
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
345
  SName name = {0};
S
Shengliang Guan 已提交
346
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
X
Xiaoyu Wang 已提交
347 348
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, dbFName);
349

S
Shengliang Guan 已提交
350 351
  SVCreateTbReq req = {0};
  req.ver = 0;
X
Xiaoyu Wang 已提交
352
  req.dbFName = dbFName;
S
Shengliang Guan 已提交
353
  req.name = (char *)tNameGetTableName(&name);
H
more  
Hongze Cheng 已提交
354 355
  req.ttl = 0;
  req.keep = 0;
C
Cary Xu 已提交
356
  req.rollup = pStb->aggregationMethod > -1 ? 1 : 0;
H
more  
Hongze Cheng 已提交
357 358 359 360
  req.type = TD_SUPER_TABLE;
  req.stbCfg.suid = pStb->uid;
  req.stbCfg.nCols = pStb->numOfColumns;
  req.stbCfg.nTagCols = pStb->numOfTags;
S
Shengliang Guan 已提交
361
  req.stbCfg.pTagSchema = pStb->pTags;
C
Cary Xu 已提交
362 363 364 365 366 367
  req.stbCfg.nBSmaCols = pStb->numOfSmas;
  req.stbCfg.pSchema = (SSchemaEx *)taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchemaEx));
  if (req.stbCfg.pSchema == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
C
Cary Xu 已提交
368

C
Cary Xu 已提交
369 370 371 372 373 374 375
  int bSmaStat = 0;                             // no column has bsma
  if (pStb->numOfSmas == pStb->numOfColumns) {  // assume pColumns > 0
    bSmaStat = 1;                               // all columns have bsma
  } else if (pStb->numOfSmas != 0) {
    bSmaStat = 2;                  // partial columns have bsma
    TASSERT(pStb->pSmas != NULL);  // TODO: remove the assert
  }
C
Cary Xu 已提交
376

C
Cary Xu 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  for (int32_t i = 0; i < req.stbCfg.nCols; ++i) {
    SSchemaEx *pSchemaEx = req.stbCfg.pSchema + i;
    SSchema   *pSchema = pStb->pColumns + i;
    pSchemaEx->type = pSchema->type;
    pSchemaEx->sma = (bSmaStat == 1) ? TSDB_BSMA_TYPE_LATEST : TSDB_BSMA_TYPE_NONE;
    pSchemaEx->colId = pSchema->colId;
    pSchemaEx->bytes = pSchema->bytes;
    memcpy(pSchemaEx->name, pSchema->name, TSDB_COL_NAME_LEN);
  }

  if (bSmaStat == 2) {
    if (pStb->pSmas == NULL) {
      mError("stb:%s, sma options is empty", pStb->name);
      taosMemoryFreeClear(req.stbCfg.pSchema);
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return NULL;
    }
    for (int32_t i = 0; i < pStb->numOfSmas; ++i) {
      SSchema   *pSmaSchema = pStb->pSmas + i;
      SSchemaEx *pColSchema = taosbsearch(&pSmaSchema->colId, req.stbCfg.pSchema, req.stbCfg.nCols, sizeof(SSchemaEx),
                                          schemaExColIdCompare, TD_EQ);
      if (pColSchema == NULL) {
        terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
        taosMemoryFreeClear(req.stbCfg.pSchema);
        mError("stb:%s, sma col:%s not found in columns", pStb->name, pSmaSchema->name);
        return NULL;
      }
      pColSchema->sma = TSDB_BSMA_TYPE_LATEST;
    }
  }
H
more  
Hongze Cheng 已提交
407

C
Cary Xu 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  SRSmaParam *pRSmaParam = NULL;
  if (req.rollup) {
    pRSmaParam = (SRSmaParam *)taosMemoryCalloc(1, sizeof(SRSmaParam));
    if (pRSmaParam == NULL) {
      taosMemoryFreeClear(req.stbCfg.pSchema);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return NULL;
    }

    pRSmaParam->xFilesFactor = pStb->xFilesFactor;
    pRSmaParam->delay = pStb->delay;
    pRSmaParam->nFuncIds = 1;  // only 1 aggregation method supported currently
    pRSmaParam->pFuncIds = (func_id_t *)taosMemoryCalloc(pRSmaParam->nFuncIds, sizeof(func_id_t));
    if (pRSmaParam->pFuncIds == NULL) {
      taosMemoryFreeClear(req.stbCfg.pRSmaParam);
      taosMemoryFreeClear(req.stbCfg.pSchema);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return NULL;
    }
    for (int32_t f = 0; f < pRSmaParam->nFuncIds; ++f) {
      *(pRSmaParam->pFuncIds + f) = pStb->aggregationMethod;
    }
    req.stbCfg.pRSmaParam = pRSmaParam;
  }  

C
Cary Xu 已提交
433
  int32_t contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
wafwerar's avatar
wafwerar 已提交
434
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
435
  if (pHead == NULL) {
C
Cary Xu 已提交
436 437 438 439
    if (pRSmaParam) {
      taosMemoryFreeClear(pRSmaParam->pFuncIds);
      taosMemoryFreeClear(pRSmaParam);
    }
C
Cary Xu 已提交
440
    taosMemoryFreeClear(req.stbCfg.pSchema);
H
more  
Hongze Cheng 已提交
441 442 443 444
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
445 446
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
447

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

S
Shengliang Guan 已提交
451
  *pContLen = contLen;
C
Cary Xu 已提交
452 453 454 455
  if (pRSmaParam) {
    taosMemoryFreeClear(pRSmaParam->pFuncIds);
    taosMemoryFreeClear(pRSmaParam);
  }
C
Cary Xu 已提交
456
  taosMemoryFreeClear(req.stbCfg.pSchema);
S
Shengliang Guan 已提交
457
  return pHead;
458 459
}

S
Shengliang Guan 已提交
460
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
S
Shengliang Guan 已提交
461 462 463 464 465 466 467 468
  SName name = {0};
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  SVDropTbReq req = {0};
  req.ver = 0;
  req.name = (char *)tNameGetTableName(&name);
  req.type = TD_SUPER_TABLE;
  req.suid = pStb->uid;
469

S
Shengliang Guan 已提交
470
  int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
wafwerar's avatar
wafwerar 已提交
471
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
472
  if (pHead == NULL) {
473 474 475 476
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

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

  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
  tSerializeSVDropTbReq(&pBuf, &req);
482

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
541 542 543
  return 0;
}

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

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

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

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

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

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

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

  return 0;
}

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

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

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

631 632
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
633
    action.pCont = pReq;
S
Shengliang Guan 已提交
634
    action.contLen = contLen;
H
Hongze Cheng 已提交
635
    action.msgType = TDMT_VND_DROP_STB;
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;
}

S
Shengliang Guan 已提交
658
static int32_t mndCreateStb(SMnode *pMnode, SNodeMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
S
Shengliang Guan 已提交
659
  SStbObj stbObj = {0};
S
Shengliang Guan 已提交
660 661
  memcpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
662 663
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
664
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
665
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
666
  stbObj.version = 1;
S
Shengliang Guan 已提交
667
  stbObj.nextColId = 1;
C
Cary Xu 已提交
668 669 670
  stbObj.xFilesFactor = pCreate->xFilesFactor;
  stbObj.aggregationMethod = pCreate->aggregationMethod;
  stbObj.delay = pCreate->delay;
S
sma  
Shengliang Guan 已提交
671
  stbObj.ttl = pCreate->ttl;
S
Shengliang Guan 已提交
672 673
  stbObj.numOfColumns = pCreate->numOfColumns;
  stbObj.numOfTags = pCreate->numOfTags;
S
sma  
Shengliang Guan 已提交
674
  stbObj.numOfSmas = pCreate->numOfSmas;
S
sma  
Shengliang Guan 已提交
675
  stbObj.commentLen = pCreate->commentLen;
S
sma  
Shengliang Guan 已提交
676
  if (stbObj.commentLen > 0) {
wafwerar's avatar
wafwerar 已提交
677
    stbObj.comment = taosMemoryCalloc(stbObj.commentLen, 1);
S
sma  
Shengliang Guan 已提交
678 679 680 681 682
    if (stbObj.comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
    memcpy(stbObj.comment, pCreate->comment, stbObj.commentLen);
S
sma  
Shengliang Guan 已提交
683
  }
S
Shengliang Guan 已提交
684

wafwerar's avatar
wafwerar 已提交
685 686 687
  stbObj.pColumns = taosMemoryMalloc(stbObj.numOfColumns * sizeof(SSchema));
  stbObj.pTags = taosMemoryMalloc(stbObj.numOfTags * sizeof(SSchema));
  stbObj.pSmas = taosMemoryMalloc(stbObj.numOfSmas * sizeof(SSchema));
S
sma  
Shengliang Guan 已提交
688
  if (stbObj.pColumns == NULL || stbObj.pTags == NULL || stbObj.pSmas == NULL) {
S
Shengliang Guan 已提交
689 690 691
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
692

S
Shengliang Guan 已提交
693
  for (int32_t i = 0; i < stbObj.numOfColumns; ++i) {
S
Shengliang Guan 已提交
694 695 696 697 698 699
    SField  *pField = taosArrayGet(pCreate->pColumns, i);
    SSchema *pSchema = &stbObj.pColumns[i];
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
    pSchema->colId = stbObj.nextColId;
S
Shengliang Guan 已提交
700 701 702 703
    stbObj.nextColId++;
  }

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

S
sma  
Shengliang Guan 已提交
713 714 715 716 717
  for (int32_t i = 0; i < stbObj.numOfSmas; ++i) {
    SField  *pField = taosArrayGet(pCreate->pSmas, i);
    SSchema *pSchema = &stbObj.pSmas[i];
    SSchema *pColSchema = mndFindStbColumns(&stbObj, pField->name);
    if (pColSchema == NULL) {
C
Cary Xu 已提交
718
      mError("stb:%s, sma:%s not found in columns", stbObj.name, pField->name);
S
sma  
Shengliang Guan 已提交
719 720 721 722 723 724
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    memcpy(pSchema, pColSchema, sizeof(SSchema));
  }

S
Shengliang 已提交
725
  int32_t code = -1;
S
Shengliang Guan 已提交
726
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_STB, &pReq->rpcMsg);
S
Shengliang Guan 已提交
727
  if (pTrans == NULL) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
728

S
Shengliang Guan 已提交
729
  mDebug("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
S
Shengliang Guan 已提交
730
  mndTransSetDbInfo(pTrans, pDb);
S
Shengliang Guan 已提交
731

S
Shengliang Guan 已提交
732 733 734 735 736 737
  if (mndSetCreateStbRedoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndSetCreateStbUndoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndSetCreateStbCommitLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndSetCreateStbRedoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndSetCreateStbUndoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto CREATE_STB_OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
738

S
Shengliang Guan 已提交
739 740 741
  code = 0;

CREATE_STB_OVER:
S
Shengliang Guan 已提交
742
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
743
  return code;
S
Shengliang Guan 已提交
744 745
}

S
Shengliang Guan 已提交
746 747
static int32_t mndProcessMCreateStbReq(SNodeMsg *pReq) {
  SMnode        *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
748 749 750 751
  int32_t        code = -1;
  SStbObj       *pTopicStb = NULL;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
S
Shengliang Guan 已提交
752
  SUserObj      *pUser = NULL;
S
Shengliang Guan 已提交
753
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
754

S
Shengliang Guan 已提交
755 756 757 758
  if (tDeserializeSMCreateStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_STB_OVER;
  }
S
Shengliang Guan 已提交
759

S
Shengliang Guan 已提交
760
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
761 762 763 764
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto CREATE_STB_OVER;
  }
S
Shengliang Guan 已提交
765

S
Shengliang Guan 已提交
766
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
767
  if (pStb != NULL) {
S
Shengliang Guan 已提交
768 769 770 771
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
      goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
772 773
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
S
Shengliang Guan 已提交
774
      goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
775
    }
S
Shengliang Guan 已提交
776
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
S
Shengliang Guan 已提交
777
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
778 779
  }

S
Shengliang Guan 已提交
780
  pTopicStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
781
  if (pTopicStb != NULL) {
L
Liu Jicong 已提交
782
    terrno = TSDB_CODE_MND_NAME_CONFLICT_WITH_TOPIC;
S
Shengliang Guan 已提交
783
    goto CREATE_STB_OVER;
L
Liu Jicong 已提交
784 785
  }

S
Shengliang Guan 已提交
786
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
787 788
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
789
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
790 791
  }

S
Shengliang Guan 已提交
792 793 794 795 796 797 798 799 800
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    goto CREATE_STB_OVER;
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
    goto CREATE_STB_OVER;
  }

S
Shengliang Guan 已提交
801
  code = mndCreateStb(pMnode, pReq, &createReq, pDb);
802
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
803

S
Shengliang Guan 已提交
804
CREATE_STB_OVER:
805
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
806
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
807 808
  }

S
Shengliang Guan 已提交
809 810 811
  mndReleaseStb(pMnode, pStb);
  mndReleaseStb(pMnode, pTopicStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
812 813
  mndReleaseUser(pMnode, pUser);
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
814 815

  return code;
S
Shengliang Guan 已提交
816 817
}

S
Shengliang Guan 已提交
818
static int32_t mndProcessVCreateStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
819
  mndTransProcessRsp(pRsp);
820 821
  return 0;
}
S
Shengliang Guan 已提交
822

S
Shengliang Guan 已提交
823
static int32_t mndCheckAlterStbReq(SMAltertbReq *pAlter) {
S
Shengliang Guan 已提交
824 825 826 827
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
828

S
Shengliang Guan 已提交
829 830
  for (int32_t i = 0; i < pAlter->numOfFields; ++i) {
    SField *pField = taosArrayGet(pAlter->pFields, i);
S
Shengliang Guan 已提交
831

S
Shengliang Guan 已提交
832
    if (pField->type <= 0) {
S
Shengliang Guan 已提交
833 834 835
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
836
    if (pField->bytes <= 0) {
S
Shengliang Guan 已提交
837 838 839
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
840
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
841 842 843
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
844 845 846 847 848
  }

  return 0;
}

S
Shengliang Guan 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
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 已提交
870 871
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
872 873 874 875 876 877 878 879 880 881
  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 Guan 已提交
882
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
883 884 885 886 887 888 889 890 891 892
  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 已提交
893 894 895 896 897
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
898
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
899 900
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
901
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
902 903 904
      return -1;
    }

S
Shengliang Guan 已提交
905
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
906
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
907 908 909
      return -1;
    }

S
Shengliang Guan 已提交
910 911 912 913
    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 已提交
914 915
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
916 917

    mDebug("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
  }

  pNew->version++;
  return 0;
}

static int32_t mndDropSuperTableTag(const SStbObj *pOld, SStbObj *pNew, const char *tagName) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, tagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

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

  memmove(pNew->pTags + tag, pNew->pTags + tag + 1, sizeof(SSchema) * (pNew->numOfTags - tag - 1));
S
Shengliang Guan 已提交
936
  pNew->numOfTags--;
S
Shengliang Guan 已提交
937 938 939 940 941 942

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

S
Shengliang Guan 已提交
943 944 945 946 947 948 949 950 951 952 953 954
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 已提交
955 956 957 958 959 960 961
  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 已提交
962
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
963 964 965
    return -1;
  }

S
Shengliang Guan 已提交
966 967
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
    return -1;
  }

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

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

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

S
Shengliang Guan 已提交
983 984
static int32_t mndAlterStbTagBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
985 986 987 988 989 990 991 992 993 994 995
  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 已提交
996 997 998 999 1000
  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 已提交
1001
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1002 1003 1004 1005
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1006
  pTag->bytes = pField->bytes;
S
Shengliang Guan 已提交
1007 1008
  pNew->version++;

S
Shengliang Guan 已提交
1009
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1010 1011 1012
  return 0;
}

S
Shengliang Guan 已提交
1013
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1014 1015 1016 1017 1018
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1019 1020 1021 1022 1023
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1024
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1025 1026
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
1027
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1028 1029 1030
      return -1;
    }

S
Shengliang Guan 已提交
1031
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
1032
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1033 1034 1035
      return -1;
    }

S
Shengliang Guan 已提交
1036 1037 1038 1039
    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 已提交
1040 1041
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1042 1043

    mDebug("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1044 1045 1046 1047 1048 1049 1050 1051
  }

  pNew->version++;
  return 0;
}

static int32_t mndDropSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, const char *colName) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
S
Shengliang Guan 已提交
1052
  if (col < 0) {
S
Shengliang Guan 已提交
1053 1054 1055 1056
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
  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 已提交
1067 1068 1069 1070 1071
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
S
Shengliang Guan 已提交
1072
  pNew->numOfColumns--;
S
Shengliang Guan 已提交
1073 1074 1075 1076 1077 1078

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

S
Shengliang Guan 已提交
1079 1080
static int32_t mndAlterStbColumnBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1081 1082 1083 1084 1085 1086 1087
  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 已提交
1088
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
  }

  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 已提交
1101 1102
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1103 1104 1105
    return -1;
  }

S
Shengliang Guan 已提交
1106
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1107
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1108 1109 1110
    return -1;
  }

S
Shengliang Guan 已提交
1111
  pCol->bytes = pField->bytes;
S
Shengliang Guan 已提交
1112 1113
  pNew->version++;

S
Shengliang Guan 已提交
1114
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1115 1116 1117
  return 0;
}

S
Shengliang Guan 已提交
1118
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1119 1120 1121
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
S
Shengliang Guan 已提交
1122
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_UPDATING) != 0) return -1;
S
Shengliang Guan 已提交
1123 1124 1125 1126

  return 0;
}

S
Shengliang Guan 已提交
1127
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1128 1129 1130 1131 1132 1133 1134 1135
  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 已提交
1136
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1137 1138 1139 1140 1141 1142 1143 1144
  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 已提交
1145 1146 1147 1148
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1149

S
Shengliang Guan 已提交
1150
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    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 已提交
1161
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1162
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1163
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1174
static int32_t mndAlterStb(SMnode *pMnode, SNodeMsg *pReq, const SMAltertbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
S
Shengliang Guan 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183
  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 已提交
1184
  STrans *pTrans = NULL;
S
Shengliang Guan 已提交
1185
  SField *pField0 = taosArrayGet(pAlter->pFields, 0);
S
Shengliang Guan 已提交
1186

S
Shengliang Guan 已提交
1187
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
1188
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
1189
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1190
      break;
S
Shengliang Guan 已提交
1191
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang Guan 已提交
1192
      code = mndDropSuperTableTag(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1193
      break;
S
Shengliang Guan 已提交
1194
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
S
Shengliang Guan 已提交
1195
      code = mndAlterStbTagName(pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
1196
      break;
S
Shengliang Guan 已提交
1197
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang Guan 已提交
1198
      code = mndAlterStbTagBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1199 1200
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
1201
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1202 1203
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang Guan 已提交
1204
      code = mndDropSuperTableColumn(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1205
      break;
S
Shengliang Guan 已提交
1206
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang Guan 已提交
1207
      code = mndAlterStbColumnBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1208 1209 1210 1211 1212 1213
      break;
    default:
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      break;
  }

S
Shengliang Guan 已提交
1214
  if (code != 0) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1215 1216

  code = -1;
S
Shengliang Guan 已提交
1217
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_ALTER_STB, &pReq->rpcMsg);
S
Shengliang Guan 已提交
1218
  if (pTrans == NULL) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1219

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

S
Shengliang Guan 已提交
1223 1224 1225 1226
  if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto ALTER_STB_OVER;
  if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, &stbObj) != 0) goto ALTER_STB_OVER;
  if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, &stbObj) != 0) goto ALTER_STB_OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1227 1228

  code = 0;
S
Shengliang Guan 已提交
1229

S
Shengliang Guan 已提交
1230
ALTER_STB_OVER:
S
Shengliang Guan 已提交
1231
  mndTransDrop(pTrans);
wafwerar's avatar
wafwerar 已提交
1232 1233
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
S
Shengliang Guan 已提交
1234 1235
  return code;
}
S
Shengliang Guan 已提交
1236

S
Shengliang Guan 已提交
1237 1238
static int32_t mndProcessMAlterStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1239 1240 1241
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1242
  SUserObj    *pUser = NULL;
S
Shengliang Guan 已提交
1243
  SMAltertbReq alterReq = {0};
S
Shengliang Guan 已提交
1244

S
Shengliang Guan 已提交
1245
  if (tDeserializeSMAlterStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1246 1247 1248
    terrno = TSDB_CODE_INVALID_MSG;
    goto ALTER_STB_OVER;
  }
S
Shengliang Guan 已提交
1249

S
Shengliang Guan 已提交
1250 1251
  mDebug("stb:%s, start to alter", alterReq.name);
  if (mndCheckAlterStbReq(&alterReq) != 0) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1252

S
Shengliang Guan 已提交
1253
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1254 1255
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
S
Shengliang Guan 已提交
1256
    goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1257 1258
  }

S
Shengliang Guan 已提交
1259
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1260 1261
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
S
Shengliang Guan 已提交
1262
    goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1263
  }
S
Shengliang Guan 已提交
1264

S
Shengliang Guan 已提交
1265 1266 1267 1268 1269 1270 1271 1272 1273
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    goto ALTER_STB_OVER;
  }

  if (mndCheckWriteAuth(pUser, pDb) != 0) {
    goto ALTER_STB_OVER;
  }

S
Shengliang Guan 已提交
1274
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
1275
  if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1276

S
Shengliang Guan 已提交
1277
ALTER_STB_OVER:
1278
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1279
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1280 1281
  }

S
Shengliang Guan 已提交
1282 1283
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1284
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
1285
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1286 1287

  return code;
S
Shengliang Guan 已提交
1288
}
S
Shengliang Guan 已提交
1289

S
Shengliang Guan 已提交
1290
static int32_t mndProcessVAlterStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1291
  mndTransProcessRsp(pRsp);
1292 1293
  return 0;
}
S
Shengliang Guan 已提交
1294

S
Shengliang Guan 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
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 已提交
1313 1314 1315 1316
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 已提交
1317

S
Shengliang Guan 已提交
1318 1319 1320
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1321 1322 1323 1324
    if (pVgroup->dbUid != pDb->uid) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }
S
Shengliang Guan 已提交
1325

S
Shengliang Guan 已提交
1326
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1327
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
    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 已提交
1342
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1353
static int32_t mndDropStb(SMnode *pMnode, SNodeMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1354
  int32_t code = -1;
S
Shengliang Guan 已提交
1355
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_DROP_STB, &pReq->rpcMsg);
S
Shengliang Guan 已提交
1356
  if (pTrans == NULL) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1357

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

S
Shengliang Guan 已提交
1361 1362
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1363
  if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1364
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1365

S
Shengliang Guan 已提交
1366 1367 1368
  code = 0;

DROP_STB_OVER:
S
Shengliang Guan 已提交
1369
  mndTransDrop(pTrans);
S
Shengliang 已提交
1370
  return code;
S
Shengliang Guan 已提交
1371 1372
}

S
Shengliang Guan 已提交
1373 1374
static int32_t mndProcessMDropStbReq(SNodeMsg *pReq) {
  SMnode      *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1375 1376 1377 1378
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
1379
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
1380

S
Shengliang Guan 已提交
1381
  if (tDeserializeSMDropStbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
1382 1383 1384
    terrno = TSDB_CODE_INVALID_MSG;
    goto DROP_STB_OVER;
  }
S
Shengliang Guan 已提交
1385

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

S
Shengliang Guan 已提交
1388
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1389
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1390 1391
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1392 1393
      code = 0;
      goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1394 1395
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
S
Shengliang Guan 已提交
1396
      goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1397 1398 1399
    }
  }

S
Shengliang Guan 已提交
1400
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1401 1402
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
1403
    goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1404 1405
  }

S
Shengliang Guan 已提交
1406 1407 1408 1409
  pUser = mndAcquireUser(pMnode, pReq->user);
  if (pUser == NULL) {
    goto DROP_STB_OVER;
  }
S
Shengliang Guan 已提交
1410

S
Shengliang Guan 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419
  if (mndCheckWriteAuth(pUser, pDb) != 0) {
    goto DROP_STB_OVER;
  }

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

DROP_STB_OVER:
  if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1420
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1421 1422
  }

S
Shengliang Guan 已提交
1423 1424 1425 1426 1427
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  mndReleaseUser(pMnode, pUser);

  return code;
S
Shengliang Guan 已提交
1428
}
S
Shengliang Guan 已提交
1429

S
Shengliang Guan 已提交
1430
static int32_t mndProcessVDropStbRsp(SNodeMsg *pRsp) {
S
Shengliang Guan 已提交
1431
  mndTransProcessRsp(pRsp);
1432 1433
  return 0;
}
S
Shengliang Guan 已提交
1434

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

S
Shengliang Guan 已提交
1438
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
wafwerar's avatar
wafwerar 已提交
1439
  pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
1440
  if (pRsp->pSchemas == NULL) {
S
Shengliang Guan 已提交
1441
    taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1442 1443 1444 1445
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
  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->update = pDb->cfg.update;
  pRsp->sversion = pStb->version;
  pRsp->suid = pStb->uid;
  pRsp->tuid = pStb->uid;
S
Shengliang Guan 已提交
1458

S
Shengliang Guan 已提交
1459
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1460
    SSchema *pSchema = &pRsp->pSchemas[i];
S
Shengliang Guan 已提交
1461 1462 1463
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1464 1465
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1466 1467 1468
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
S
Shengliang Guan 已提交
1469
    SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
S
Shengliang Guan 已提交
1470
    SSchema *pSrcSchema = &pStb->pTags[i];
S
Shengliang Guan 已提交
1471 1472
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
S
Shengliang Guan 已提交
1473 1474
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
S
Shengliang Guan 已提交
1475
  }
S
Shengliang Guan 已提交
1476

S
Shengliang Guan 已提交
1477
  taosRUnLockLatch(&pStb->lock);
S
Shengliang Guan 已提交
1478
  return 0;
S
Shengliang Guan 已提交
1479
}
S
Shengliang Guan 已提交
1480

S
Shengliang Guan 已提交
1481 1482 1483
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 已提交
1484

S
Shengliang Guan 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
  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 已提交
1502
}
S
Shengliang Guan 已提交
1503

S
Shengliang Guan 已提交
1504 1505
static int32_t mndProcessTableMetaReq(SNodeMsg *pReq) {
  SMnode       *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1506 1507 1508
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
1509

S
Shengliang Guan 已提交
1510
  if (tDeserializeSTableInfoReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
1511 1512 1513
    terrno = TSDB_CODE_INVALID_MSG;
    goto RETRIEVE_META_OVER;
  }
D
dapan 已提交
1514

D
dapan1121 已提交
1515 1516 1517 1518 1519
  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) {
      goto RETRIEVE_META_OVER;
    }
D
dapan1121 已提交
1520 1521 1522 1523 1524
  } 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) {
      goto RETRIEVE_META_OVER;
    }
D
dapan1121 已提交
1525 1526 1527 1528 1529
  } else {
    mDebug("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
      goto RETRIEVE_META_OVER;
    }
S
Shengliang Guan 已提交
1530
  }
S
Shengliang Guan 已提交
1531

S
Shengliang Guan 已提交
1532 1533 1534 1535 1536
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto RETRIEVE_META_OVER;
  }
S
Shengliang Guan 已提交
1537

S
Shengliang Guan 已提交
1538
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
1539 1540 1541 1542
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto RETRIEVE_META_OVER;
  }
D
dapan 已提交
1543

S
Shengliang Guan 已提交
1544
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
1545 1546
  pReq->pRsp = pRsp;
  pReq->rspLen = rspLen;
S
Shengliang Guan 已提交
1547
  code = 0;
S
Shengliang Guan 已提交
1548

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

S
Shengliang Guan 已提交
1551 1552 1553 1554
RETRIEVE_META_OVER:
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
1555

S
Shengliang Guan 已提交
1556 1557 1558
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
1559

S
Shengliang Guan 已提交
1560 1561 1562 1563 1564 1565 1566 1567
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 已提交
1568

S
Shengliang Guan 已提交
1569 1570 1571 1572 1573
  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 已提交
1574

S
Shengliang Guan 已提交
1575 1576 1577 1578 1579
    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 已提交
1580
    }
S
Shengliang Guan 已提交
1581

S
Shengliang Guan 已提交
1582 1583
    if (pStbVersion->sversion != metaRsp.sversion) {
      taosArrayPush(batchMetaRsp.pArray, &metaRsp);
S
Shengliang Guan 已提交
1584
    }
S
Shengliang Guan 已提交
1585
  }
S
Shengliang Guan 已提交
1586

S
Shengliang Guan 已提交
1587 1588 1589 1590 1591
  int32_t rspLen = tSerializeSTableMetaBatchRsp(NULL, 0, &batchMetaRsp);
  if (rspLen < 0) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
1592 1593
  }

wafwerar's avatar
wafwerar 已提交
1594
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
1595 1596 1597 1598
  if (pRsp == NULL) {
    tFreeSTableMetaBatchRsp(&batchMetaRsp);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
1599 1600
  }

S
Shengliang Guan 已提交
1601 1602 1603
  tSerializeSTableMetaBatchRsp(pRsp, rspLen, &batchMetaRsp);
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
1604 1605 1606
  return 0;
}

S
Shengliang Guan 已提交
1607
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1608
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1609 1610 1611 1612 1613 1614
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1615
  int32_t numOfStbs = 0;
1616
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1617
  while (1) {
S
Shengliang Guan 已提交
1618
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1619
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1620 1621
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1622
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
1623
      numOfStbs++;
S
Shengliang Guan 已提交
1624 1625
    }

S
Shengliang Guan 已提交
1626
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1627 1628
  }

S
Shengliang Guan 已提交
1629
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1630
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1631 1632 1633
  return 0;
}

S
Shengliang Guan 已提交
1634
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
1635 1636
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
1637
  for (pos = 0; tableId[pos] != 0; ++pos) {
H
Haojun Liao 已提交
1638
    if (tableId[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
1639 1640 1641 1642 1643 1644 1645 1646
    if (num == 2) break;
  }

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

S
Shengliang Guan 已提交
1647 1648
static int32_t mndRetrieveStb(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode  *pMnode = pReq->pNode;
S
Shengliang Guan 已提交
1649
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1650 1651 1652
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1653
  char    *pWrite;
S
Shengliang Guan 已提交
1654

H
Haojun Liao 已提交
1655 1656 1657
  SDbObj* pDb = NULL;
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
1658
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
1659
  }
S
Shengliang Guan 已提交
1660

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

H
Haojun Liao 已提交
1665
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
1666
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1667 1668 1669 1670 1671
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
1672
    SName name = {0};
S
Shengliang Guan 已提交
1673
    char stbName[TSDB_TABLE_NAME_LEN] = {0};
H
Haojun Liao 已提交
1674
    mndExtractTableName(pStb->name, stbName);
S
Shengliang Guan 已提交
1675
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1676
    STR_TO_VARSTR(pWrite, stbName);
S
Shengliang Guan 已提交
1677 1678
    cols++;

H
Haojun Liao 已提交
1679 1680 1681 1682 1683 1684 1685
    char  db[TSDB_DB_NAME_LEN] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT|T_NAME_DB);
    tNameGetDbName(&name, db);
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, db);
    cols++;

S
Shengliang Guan 已提交
1686
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1687
    *(int64_t *)pWrite = pStb->createdTime;
S
Shengliang Guan 已提交
1688 1689 1690
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1691
    *(int32_t *)pWrite = pStb->numOfColumns;
S
Shengliang Guan 已提交
1692 1693 1694
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1695
    *(int32_t *)pWrite = pStb->numOfTags;
S
Shengliang Guan 已提交
1696 1697
    cols++;

H
Haojun Liao 已提交
1698 1699 1700 1701 1702
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pStb->updateTime; // number of tables
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1703 1704 1705 1706 1707
    if (pStb->commentLen != 0) {
      STR_TO_VARSTR(pWrite, pStb->comment);
    } else {
      STR_TO_VARSTR(pWrite, "");
    }
H
Haojun Liao 已提交
1708 1709
    cols++;

S
Shengliang Guan 已提交
1710
    numOfRows++;
S
Shengliang Guan 已提交
1711
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1712 1713
  }

H
Haojun Liao 已提交
1714 1715 1716 1717
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

S
Shengliang Guan 已提交
1718
  pShow->numOfReads += numOfRows;
S
Shengliang Guan 已提交
1719
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
1720 1721 1722
  return numOfRows;
}

S
Shengliang Guan 已提交
1723
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1724 1725
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
1726
}