mndStb.c 46.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 20 21 22 23
#include "mndDnode.h"
#include "mndMnode.h"
#include "mndShow.h"
#include "mndTrans.h"
#include "mndUser.h"
24
#include "mndVgroup.h"
S
Shengliang Guan 已提交
25
#include "tname.h"
S
Shengliang Guan 已提交
26

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

static SSdbRaw *mndStbActionEncode(SStbObj *pStb);
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw);
static int32_t  mndStbActionInsert(SSdb *pSdb, SStbObj *pStb);
static int32_t  mndStbActionDelete(SSdb *pSdb, SStbObj *pStb);
S
Shengliang Guan 已提交
34 35
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
static int32_t  mndProcessMCreateStbReq(SMnodeMsg *pReq);
S
Shengliang Guan 已提交
36
static int32_t  mndProcessMAlterStbReq(SMnodeMsg *pReq);
S
Shengliang Guan 已提交
37 38
static int32_t  mndProcessMDropStbReq(SMnodeMsg *pReq);
static int32_t  mndProcessVCreateStbRsp(SMnodeMsg *pRsp);
S
Shengliang Guan 已提交
39
static int32_t  mndProcessVAlterStbRsp(SMnodeMsg *pRsp);
S
Shengliang Guan 已提交
40 41 42 43
static int32_t  mndProcessVDropStbRsp(SMnodeMsg *pRsp);
static int32_t  mndProcessStbMetaReq(SMnodeMsg *pReq);
static int32_t  mndGetStbMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
static int32_t  mndRetrieveStb(SMnodeMsg *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 61
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_STB_RSP, mndProcessVDropStbRsp);
  mndSetMsgHandle(pMnode, TDMT_MND_STB_META, mndProcessStbMetaReq);
S
Shengliang Guan 已提交
62 63 64 65

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

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

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

S
Shengliang Guan 已提交
72
static SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
73 74
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
75
  int32_t  size = sizeof(SStbObj) + (pStb->numOfColumns + pStb->numOfTags) * 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)
88 89
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, STB_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, STB_ENCODE_OVER)
S
Shengliang Guan 已提交
90

S
Shengliang Guan 已提交
91 92 93 94 95 96 97 98 99 100
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, STB_ENCODE_OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->colId, STB_ENCODE_OVER)
    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];
101 102 103 104
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, STB_ENCODE_OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->colId, STB_ENCODE_OVER)
    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 已提交
105 106
  }

S
Shengliang Guan 已提交
107
  SDB_SET_BINARY(pRaw, dataPos, pStb->comment, TSDB_STB_COMMENT_LEN, STB_ENCODE_OVER)
108 109 110 111 112 113 114 115 116 117 118
  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 已提交
119

120
  mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
121 122 123
  return pRaw;
}

S
Shengliang Guan 已提交
124
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
125 126
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
127
  int8_t sver = 0;
128
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
129

S
Shengliang Guan 已提交
130
  if (sver != TSDB_STB_VER_NUMBER) {
S
Shengliang Guan 已提交
131
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
132
    goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
133 134
  }

S
Shengliang 已提交
135
  SSdbRow *pRow = sdbAllocRow(sizeof(SStbObj));
136 137
  if (pRow == NULL) goto STB_DECODE_OVER;

S
Shengliang Guan 已提交
138
  SStbObj *pStb = sdbGetRowObj(pRow);
139
  if (pStb == NULL) goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
140 141

  int32_t dataPos = 0;
142 143 144 145 146 147 148
  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 已提交
149
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, STB_DECODE_OVER)
150 151
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, STB_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, STB_DECODE_OVER)
S
Shengliang Guan 已提交
152

S
Shengliang Guan 已提交
153 154 155
  pStb->pColumns = calloc(pStb->numOfColumns, sizeof(SSchema));
  pStb->pTags = calloc(pStb->numOfTags, sizeof(SSchema));
  if (pStb->pColumns == NULL || pStb->pTags == NULL) {
S
Shengliang 已提交
156 157
    goto STB_DECODE_OVER;
  }
S
Shengliang Guan 已提交
158

S
Shengliang Guan 已提交
159 160 161 162 163 164 165 166 167 168
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, STB_DECODE_OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->colId, STB_DECODE_OVER)
    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];
169 170 171 172
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, STB_DECODE_OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->colId, STB_DECODE_OVER)
    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 已提交
173 174
  }

S
Shengliang Guan 已提交
175
  SDB_GET_BINARY(pRaw, dataPos, pStb->comment, TSDB_STB_COMMENT_LEN, STB_DECODE_OVER)
176 177 178 179 180 181 182 183 184 185
  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());
    tfree(pRow);
    return NULL;
  }
S
Shengliang Guan 已提交
186

187
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
188 189 190
  return pRow;
}

S
Shengliang Guan 已提交
191
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
192
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
193 194 195
  return 0;
}

S
Shengliang Guan 已提交
196
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
197
  mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
198 199
  tfree(pStb->pColumns);
  tfree(pStb->pTags);
S
Shengliang Guan 已提交
200 201 202
  return 0;
}

S
Shengliang Guan 已提交
203 204
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 已提交
205

S
Shengliang Guan 已提交
206
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
207 208

  if (pOld->numOfColumns < pNew->numOfColumns) {
S
Shengliang Guan 已提交
209 210
    void *pColumns = malloc(pNew->numOfColumns * sizeof(SSchema));
    if (pColumns != NULL) {
S
Shengliang Guan 已提交
211
      free(pOld->pColumns);
S
Shengliang Guan 已提交
212
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
213 214 215 216 217 218 219 220
    } 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) {
S
Shengliang Guan 已提交
221 222
    void *pTags = malloc(pNew->numOfTags * sizeof(SSchema));
    if (pTags != NULL) {
S
Shengliang Guan 已提交
223
      free(pOld->pTags);
S
Shengliang Guan 已提交
224
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
225 226 227 228
    } 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 已提交
229
    }
S
Shengliang Guan 已提交
230 231
  }

S
Shengliang Guan 已提交
232 233
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
S
Shengliang Guan 已提交
234
  pOld->nextColId = pNew->nextColId;
S
Shengliang Guan 已提交
235 236
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
237 238
  memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
239
  memcpy(pOld->comment, pNew->comment, TSDB_STB_COMMENT_LEN);
S
Shengliang Guan 已提交
240
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
241 242 243
  return 0;
}

S
Shengliang Guan 已提交
244
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
245
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
246
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
247
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
248 249 250
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
251 252
}

S
Shengliang Guan 已提交
253
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
254 255 256 257
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

S
Shengliang Guan 已提交
258
static SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
259 260
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
261

S
Shengliang Guan 已提交
262 263
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
264

S
Shengliang Guan 已提交
265 266
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
267

S
Shengliang Guan 已提交
268
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
269
  SName name = {0};
S
Shengliang Guan 已提交
270
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
271

S
Shengliang Guan 已提交
272 273
  SVCreateTbReq req = {0};
  req.ver = 0;
S
Shengliang Guan 已提交
274
  req.name = (char *)tNameGetTableName(&name);
H
more  
Hongze Cheng 已提交
275 276 277 278 279
  req.ttl = 0;
  req.keep = 0;
  req.type = TD_SUPER_TABLE;
  req.stbCfg.suid = pStb->uid;
  req.stbCfg.nCols = pStb->numOfColumns;
S
Shengliang Guan 已提交
280
  req.stbCfg.pSchema = pStb->pColumns;
H
more  
Hongze Cheng 已提交
281
  req.stbCfg.nTagCols = pStb->numOfTags;
S
Shengliang Guan 已提交
282
  req.stbCfg.pTagSchema = pStb->pTags;
H
more  
Hongze Cheng 已提交
283

S
Shengliang Guan 已提交
284 285 286
  int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
  SMsgHead *pHead = malloc(contLen);
  if (pHead == NULL) {
H
more  
Hongze Cheng 已提交
287 288 289 290
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
291 292
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
293

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

S
Shengliang Guan 已提交
297 298
  *pContLen = contLen;
  return pHead;
299 300
}

S
Shengliang Guan 已提交
301
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
S
Shengliang Guan 已提交
302 303 304 305 306 307 308 309
  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;
310

S
Shengliang Guan 已提交
311 312 313
  int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
  SMsgHead *pHead = malloc(contLen);
  if (pHead == NULL) {
314 315 316 317
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
318 319 320 321 322
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

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

S
Shengliang Guan 已提交
324 325
  *pContLen = contLen;
  return pHead;
326 327
}

S
Shengliang Guan 已提交
328
static int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
329
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
330
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
331 332
    return -1;
  }
S
Shengliang Guan 已提交
333

S
Shengliang Guan 已提交
334
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
335
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
336 337
    return -1;
  }
S
Shengliang Guan 已提交
338

S
Shengliang Guan 已提交
339
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
340
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
341 342
    return -1;
  }
S
Shengliang Guan 已提交
343

S
Shengliang Guan 已提交
344 345
  SField *pField = taosArrayGet(pCreate->pColumns, 0) ;
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
S
Shengliang Guan 已提交
346 347 348 349
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  for (int32_t i = 0; i < pCreate->numOfColumns; ++i) {
    SField *pField = taosArrayGet(pCreate->pColumns, i);
    if (pField->type < 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    if (pField->bytes <= 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
    if (pField->name[0] == 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
  }

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
    SField *pField = taosArrayGet(pCreate->pTags, i);
    if (pField->type < 0) {
S
Shengliang Guan 已提交
369
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
370 371
      return -1;
    }
S
Shengliang Guan 已提交
372
    if (pField->bytes <= 0) {
S
Shengliang Guan 已提交
373
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
374 375
      return -1;
    }
S
Shengliang Guan 已提交
376
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
377
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
378 379 380
      return -1;
    }
  }
S
Shengliang Guan 已提交
381

S
Shengliang Guan 已提交
382 383 384
  return 0;
}

385
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
386 387 388 389 390 391 392 393
  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;
}

394
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
395 396 397 398 399 400 401 402
  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;
}

403
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
404 405 406 407 408 409 410 411
  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;
}

412
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
413
  SSdb   *pSdb = pMnode->pSdb;
414
  SVgObj *pVgroup = NULL;
415
  void   *pIter = NULL;
S
Shengliang Guan 已提交
416
  int32_t contLen;
417 418 419 420 421 422

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != pDb->uid) continue;

S
Shengliang Guan 已提交
423
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
424
    if (pReq == NULL) {
425 426 427 428 429
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
430

431 432
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
433
    action.pCont = pReq;
S
Shengliang Guan 已提交
434
    action.contLen = contLen;
H
Hongze Cheng 已提交
435
    action.msgType = TDMT_VND_CREATE_STB;
436
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
437
      free(pReq);
438 439 440 441 442 443
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
444 445 446 447

  return 0;
}

448
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
449
  SSdb   *pSdb = pMnode->pSdb;
450
  SVgObj *pVgroup = NULL;
451
  void   *pIter = NULL;
452 453 454 455 456 457

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != pDb->uid) continue;

S
Shengliang Guan 已提交
458
    int32_t contLen = 0;
S
Shengliang Guan 已提交
459
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
460
    if (pReq == NULL) {
461 462 463 464 465
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
466

467 468
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
469
    action.pCont = pReq;
S
Shengliang Guan 已提交
470
    action.contLen = contLen;
H
Hongze Cheng 已提交
471
    action.msgType = TDMT_VND_DROP_STB;
472
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
473
      free(pReq);
474 475 476 477 478 479
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
480 481 482 483

  return 0;
}

S
Shengliang Guan 已提交
484
static int32_t mndCreateStb(SMnode *pMnode, SMnodeMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
S
Shengliang Guan 已提交
485
  SStbObj stbObj = {0};
S
Shengliang Guan 已提交
486 487
  memcpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
488 489
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
490
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
491
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
492
  stbObj.version = 1;
S
Shengliang Guan 已提交
493
  stbObj.nextColId = 1;
S
Shengliang Guan 已提交
494 495 496
  stbObj.numOfColumns = pCreate->numOfColumns;
  stbObj.numOfTags = pCreate->numOfTags;

S
Shengliang Guan 已提交
497 498
  stbObj.pColumns = malloc(stbObj.numOfColumns * sizeof(SSchema));
  stbObj.pTags = malloc(stbObj.numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
499
  if (stbObj.pColumns == NULL || stbObj.pTags == NULL) {
S
Shengliang Guan 已提交
500 501 502
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
503

S
Shengliang Guan 已提交
504
  for (int32_t i = 0; i < stbObj.numOfColumns; ++i) {
S
Shengliang Guan 已提交
505 506 507 508 509 510
    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 已提交
511 512 513 514
    stbObj.nextColId++;
  }

  for (int32_t i = 0; i < stbObj.numOfTags; ++i) {
S
Shengliang Guan 已提交
515 516 517 518 519 520
    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 已提交
521
    stbObj.nextColId++;
S
Shengliang Guan 已提交
522 523
  }

S
Shengliang 已提交
524
  int32_t code = -1;
S
Shengliang Guan 已提交
525 526
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
  if (pTrans == NULL) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
527

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

S
Shengliang Guan 已提交
530 531 532 533 534 535
  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 已提交
536

S
Shengliang Guan 已提交
537 538 539
  code = 0;

CREATE_STB_OVER:
S
Shengliang Guan 已提交
540
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
541
  return code;
S
Shengliang Guan 已提交
542 543
}

S
Shengliang Guan 已提交
544
static int32_t mndProcessMCreateStbReq(SMnodeMsg *pReq) {
S
Shengliang Guan 已提交
545 546 547 548 549 550
  SMnode        *pMnode = pReq->pMnode;
  int32_t        code = -1;
  SStbObj       *pTopicStb = NULL;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
  SMCreateStbReq createReq = {0};
S
Shengliang Guan 已提交
551

S
Shengliang Guan 已提交
552
  if (tDeserializeSMCreateStbReq(pReq->rpcMsg.pCont, &createReq) == NULL) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
553

S
Shengliang Guan 已提交
554 555
  mDebug("stb:%s, start to create", createReq.name);
  if (mndCheckCreateStbReq(&createReq) != 0) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
556

S
Shengliang Guan 已提交
557
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
558
  if (pStb != NULL) {
S
Shengliang Guan 已提交
559 560 561 562
    if (createReq.igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
      code = 0;
      goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
563 564
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
S
Shengliang Guan 已提交
565
      goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
566
    }
S
Shengliang Guan 已提交
567
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
S
Shengliang Guan 已提交
568
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
569 570
  }

S
Shengliang Guan 已提交
571
  pTopicStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
572
  if (pTopicStb != NULL) {
L
Liu Jicong 已提交
573
    terrno = TSDB_CODE_MND_NAME_CONFLICT_WITH_TOPIC;
S
Shengliang Guan 已提交
574
    goto CREATE_STB_OVER;
L
Liu Jicong 已提交
575 576
  }

S
Shengliang Guan 已提交
577
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
578 579
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
580
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
581 582
  }

S
Shengliang Guan 已提交
583
  code = mndCreateStb(pMnode, pReq, &createReq, pDb);
S
Shengliang Guan 已提交
584

S
Shengliang Guan 已提交
585
CREATE_STB_OVER:
S
Shengliang Guan 已提交
586
  if (code != 0) {
S
Shengliang Guan 已提交
587 588 589
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
  } else {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
590 591
  }

S
Shengliang Guan 已提交
592 593 594
  mndReleaseStb(pMnode, pStb);
  mndReleaseStb(pMnode, pTopicStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
595 596
  taosArrayDestroy(createReq.pColumns);
  taosArrayDestroy(createReq.pTags);
S
Shengliang Guan 已提交
597 598

  return code;
S
Shengliang Guan 已提交
599 600
}

S
Shengliang Guan 已提交
601 602
static int32_t mndProcessVCreateStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
603 604
  return 0;
}
S
Shengliang Guan 已提交
605

S
Shengliang Guan 已提交
606
static int32_t mndCheckAlterStbReq(SMAltertbReq *pAlter) {
S
Shengliang Guan 已提交
607 608 609 610
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
611

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

S
Shengliang Guan 已提交
615
    if (pField->type <= 0) {
S
Shengliang Guan 已提交
616 617 618
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
619
    if (pField->bytes <= 0) {
S
Shengliang Guan 已提交
620 621 622
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
623
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
624 625 626
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
627 628 629 630 631
  }

  return 0;
}

S
Shengliang Guan 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
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) {
  pNew->pTags = calloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = calloc(pNew->numOfColumns, sizeof(SSchema));
  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 已提交
665
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
666 667 668 669 670 671 672 673 674 675
  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 已提交
676 677 678 679 680
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
681
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
682 683
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
684
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
685 686 687
      return -1;
    }

S
Shengliang Guan 已提交
688
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
689
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
690 691 692
      return -1;
    }

S
Shengliang Guan 已提交
693 694 695 696
    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 已提交
697 698
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
699 700

    mDebug("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
  }

  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 已提交
719
  pNew->numOfTags--;
S
Shengliang Guan 已提交
720 721 722 723 724 725

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

S
Shengliang Guan 已提交
726 727 728 729 730 731 732 733 734 735 736 737
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 已提交
738 739 740 741 742 743 744
  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 已提交
745
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
746 747 748
    return -1;
  }

S
Shengliang Guan 已提交
749 750
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
    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 已提交
766 767
static int32_t mndAlterStbTagBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
768 769 770 771 772 773 774 775 776 777 778
  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 已提交
779 780 781 782 783
  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 已提交
784
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
785 786 787 788
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
789
  pTag->bytes = pField->bytes;
S
Shengliang Guan 已提交
790 791
  pNew->version++;

S
Shengliang Guan 已提交
792
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
793 794 795
  return 0;
}

S
Shengliang Guan 已提交
796
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
797 798 799 800 801
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
802 803 804 805 806
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
807
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
808 809
    SField *pField = taosArrayGet(pFields, i);
    if (mndFindSuperTableColumnIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
810
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
811 812 813
      return -1;
    }

S
Shengliang Guan 已提交
814
    if (mndFindSuperTableTagIndex(pOld, pField->name) > 0) {
S
Shengliang Guan 已提交
815
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
816 817 818
      return -1;
    }

S
Shengliang Guan 已提交
819 820 821 822
    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 已提交
823 824
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
825 826

    mDebug("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
827 828 829 830 831 832 833 834
  }

  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 已提交
835
  if (col < 0) {
S
Shengliang Guan 已提交
836 837 838 839
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
840 841 842 843 844 845 846 847 848 849
  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 已提交
850 851 852 853 854
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
S
Shengliang Guan 已提交
855
  pNew->numOfColumns--;
S
Shengliang Guan 已提交
856 857 858 859 860 861

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

S
Shengliang Guan 已提交
862 863
static int32_t mndAlterStbColumnBytes(const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
864 865 866 867 868 869 870
  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 已提交
871
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
872 873 874 875 876 877 878 879 880 881 882 883
  }

  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 已提交
884 885
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
886 887 888
    return -1;
  }

S
Shengliang Guan 已提交
889
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
890
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
891 892 893
    return -1;
  }

S
Shengliang Guan 已提交
894
  pCol->bytes = pField->bytes;
S
Shengliang Guan 已提交
895 896
  pNew->version++;

S
Shengliang Guan 已提交
897
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
898 899 900
  return 0;
}

S
Shengliang Guan 已提交
901
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
902 903 904
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
S
Shengliang Guan 已提交
905
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_UPDATING) != 0) return -1;
S
Shengliang Guan 已提交
906 907 908 909

  return 0;
}

S
Shengliang Guan 已提交
910
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
911 912 913 914 915 916 917 918
  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 已提交
919
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
920 921 922 923 924 925 926 927 928 929
  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;
    if (pVgroup->dbUid != pDb->uid) continue;

S
Shengliang Guan 已提交
930
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
931 932 933 934 935 936 937 938 939 940 941
    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;
S
Shengliang Guan 已提交
942
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
943 944 945 946 947 948 949 950 951 952 953 954
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      free(pReq);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
955
static int32_t mndAlterStb(SMnode *pMnode, SMnodeMsg *pReq, const SMAltertbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
S
Shengliang Guan 已提交
956 957 958 959 960 961 962 963 964
  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 已提交
965
  STrans *pTrans = NULL;
S
Shengliang Guan 已提交
966 967
  SField *pField0 = taosArrayGet(pAlter->pFields, 0);
      
S
Shengliang Guan 已提交
968
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
969
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
970
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
971
      break;
S
Shengliang Guan 已提交
972
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang Guan 已提交
973
      code = mndDropSuperTableTag(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
974
      break;
S
Shengliang Guan 已提交
975
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
S
Shengliang Guan 已提交
976
      code = mndAlterStbTagName(pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
977
      break;
S
Shengliang Guan 已提交
978
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang Guan 已提交
979
      code = mndAlterStbTagBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
980 981
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
982
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
983 984
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang Guan 已提交
985
      code = mndDropSuperTableColumn(pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
986
      break;
S
Shengliang Guan 已提交
987
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang Guan 已提交
988
      code = mndAlterStbColumnBytes(pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
989 990 991 992 993 994
      break;
    default:
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      break;
  }

S
Shengliang Guan 已提交
995
  if (code != 0) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
996 997

  code = -1;
S
Shengliang Guan 已提交
998
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, &pReq->rpcMsg);
S
Shengliang Guan 已提交
999
  if (pTrans == NULL) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1000

S
Shengliang Guan 已提交
1001
  mDebug("trans:%d, used to alter stb:%s", pTrans->id, pAlter->name);
S
Shengliang Guan 已提交
1002

S
Shengliang Guan 已提交
1003 1004 1005 1006
  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 已提交
1007 1008

  code = 0;
S
Shengliang Guan 已提交
1009

S
Shengliang Guan 已提交
1010
ALTER_STB_OVER:
S
Shengliang Guan 已提交
1011 1012 1013
  mndTransDrop(pTrans);
  tfree(stbObj.pTags);
  tfree(stbObj.pColumns);
S
Shengliang Guan 已提交
1014 1015
  return code;
}
S
Shengliang Guan 已提交
1016

S
Shengliang Guan 已提交
1017
static int32_t mndProcessMAlterStbReq(SMnodeMsg *pReq) {
S
Shengliang Guan 已提交
1018 1019 1020 1021 1022
  SMnode      *pMnode = pReq->pMnode;
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
  SMAltertbReq alterReq = {0};
S
Shengliang Guan 已提交
1023

S
Shengliang Guan 已提交
1024
  if (tDeserializeSMAlterStbReq(pReq->rpcMsg.pCont, &alterReq) == NULL) goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1025

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

S
Shengliang Guan 已提交
1029
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1030 1031
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
S
Shengliang Guan 已提交
1032
    goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1033 1034
  }

S
Shengliang Guan 已提交
1035
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1036 1037
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
S
Shengliang Guan 已提交
1038
    goto ALTER_STB_OVER;
S
Shengliang Guan 已提交
1039
  }
S
Shengliang Guan 已提交
1040

S
Shengliang Guan 已提交
1041
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
S
Shengliang Guan 已提交
1042

S
Shengliang Guan 已提交
1043
ALTER_STB_OVER:
S
Shengliang Guan 已提交
1044
  if (code != 0) {
S
Shengliang Guan 已提交
1045 1046 1047
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
  } else {
    code = TSDB_CODE_MND_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1048 1049
  }

S
Shengliang Guan 已提交
1050 1051
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1052
  taosArrayDestroy(alterReq.pFields);
S
Shengliang Guan 已提交
1053 1054

  return code;
S
Shengliang Guan 已提交
1055
}
S
Shengliang Guan 已提交
1056

S
Shengliang Guan 已提交
1057
static int32_t mndProcessVAlterStbRsp(SMnodeMsg *pRsp) {
S
Shengliang Guan 已提交
1058
  mndTransProcessRsp(pRsp);
1059 1060
  return 0;
}
S
Shengliang Guan 已提交
1061

S
Shengliang Guan 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
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 已提交
1080 1081 1082 1083 1084
static int32_t mndSetDropStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
  int32_t contLen;
S
Shengliang Guan 已提交
1085

S
Shengliang Guan 已提交
1086 1087 1088 1089
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != pDb->uid) continue;
S
Shengliang Guan 已提交
1090

S
Shengliang Guan 已提交
1091
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1092
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    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) {
      free(pReq);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

static int32_t mndDropStb(SMnode *pMnode, SMnodeMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1119
  int32_t code = -1;
S
Shengliang Guan 已提交
1120
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
S
Shengliang Guan 已提交
1121
  if (pTrans == NULL) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1122

S
Shengliang Guan 已提交
1123
  mDebug("trans:%d, used to drop stb:%s", pTrans->id, pStb->name);
S
Shengliang Guan 已提交
1124

S
Shengliang Guan 已提交
1125 1126
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1127
  if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1128
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
1129

S
Shengliang Guan 已提交
1130 1131 1132
  code = 0;

DROP_STB_OVER:
S
Shengliang Guan 已提交
1133
  mndTransDrop(pTrans);
S
Shengliang 已提交
1134
  return code;
S
Shengliang Guan 已提交
1135 1136
}

S
Shengliang Guan 已提交
1137
static int32_t mndProcessMDropStbReq(SMnodeMsg *pReq) {
S
Shengliang Guan 已提交
1138 1139 1140 1141
  SMnode *pMnode = pReq->pMnode;

  SMDropStbReq dropReq = {0};
  tDeserializeSMDropStbReq(pReq->rpcMsg.pCont, &dropReq);
S
Shengliang Guan 已提交
1142

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

S
Shengliang Guan 已提交
1145
  SStbObj *pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1146
  if (pStb == NULL) {
S
Shengliang Guan 已提交
1147 1148
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
1149 1150 1151
      return 0;
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
S
Shengliang Guan 已提交
1152
      mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1153 1154 1155 1156
      return -1;
    }
  }

S
Shengliang Guan 已提交
1157
  SDbObj *pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
1158 1159 1160
  if (pDb == NULL) {
    mndReleaseStb(pMnode, pStb);
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
1161
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1162 1163 1164 1165 1166
    return -1;
  }

  int32_t code = mndDropStb(pMnode, pReq, pDb, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1167 1168 1169
  mndReleaseStb(pMnode, pStb);

  if (code != 0) {
S
Shengliang Guan 已提交
1170
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
1171 1172 1173 1174 1175
    return -1;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}
S
Shengliang Guan 已提交
1176

S
Shengliang Guan 已提交
1177 1178
static int32_t mndProcessVDropStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
1179 1180
  return 0;
}
S
Shengliang Guan 已提交
1181

S
Shengliang Guan 已提交
1182 1183 1184
static int32_t mndProcessStbMetaReq(SMnodeMsg *pReq) {
  SMnode        *pMnode = pReq->pMnode;
  STableInfoReq *pInfo = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
1185

D
dapan1121 已提交
1186 1187
  char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
  snprintf(tbFName, sizeof(tbFName), "%s.%s", pInfo->dbFName, pInfo->tbName);
S
Shengliang Guan 已提交
1188

D
dapan1121 已提交
1189 1190
  mDebug("stb:%s, start to retrieve meta", tbFName);

D
dapan 已提交
1191
  SDbObj *pDb = mndAcquireDb(pMnode, pInfo->dbFName);
S
Shengliang Guan 已提交
1192 1193
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
D
dapan1121 已提交
1194
    mError("stb:%s, failed to retrieve meta since %s", tbFName, terrstr());
S
Shengliang Guan 已提交
1195 1196 1197
    return -1;
  }

D
dapan1121 已提交
1198
  SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
S
Shengliang Guan 已提交
1199 1200
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1201
    terrno = TSDB_CODE_MND_INVALID_STB;
D
dapan1121 已提交
1202
    mError("stb:%s, failed to get meta since %s", tbFName, terrstr());
S
Shengliang Guan 已提交
1203 1204 1205
    return -1;
  }

S
Shengliang Guan 已提交
1206 1207
  taosRLockLatch(&pStb->lock);
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
S
Shengliang Guan 已提交
1208
  int32_t contLen = sizeof(STableMetaRsp) + totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
1209

S
Shengliang Guan 已提交
1210
  STableMetaRsp *pMeta = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
1211
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
1212 1213 1214
    taosRUnLockLatch(&pStb->lock);
    mndReleaseDb(pMnode, pDb);
    mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
1215
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1216
    mError("stb:%s, failed to get meta since %s", tbFName, terrstr());
S
Shengliang Guan 已提交
1217 1218 1219
    return -1;
  }

D
dapan1121 已提交
1220 1221
  strcpy(pMeta->dbFName, pStb->db);
  strcpy(pMeta->tbName, pInfo->tbName);
D
dapan 已提交
1222
  strcpy(pMeta->stbName, pInfo->tbName);
D
dapan1121 已提交
1223
  pMeta->dbId = htobe64(pDb->uid);
S
Shengliang Guan 已提交
1224 1225 1226 1227 1228 1229
  pMeta->numOfTags = htonl(pStb->numOfTags);
  pMeta->numOfColumns = htonl(pStb->numOfColumns);
  pMeta->precision = pDb->cfg.precision;
  pMeta->tableType = TSDB_SUPER_TABLE;
  pMeta->update = pDb->cfg.update;
  pMeta->sversion = htonl(pStb->version);
D
dapan1121 已提交
1230 1231
  pMeta->suid = htobe64(pStb->uid);
  pMeta->tuid = htobe64(pStb->uid);
S
Shengliang Guan 已提交
1232

S
Shengliang Guan 已提交
1233
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1234
    SSchema *pSchema = &pMeta->pSchema[i];
S
Shengliang Guan 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
    pSchema->colId = htonl(pSrcSchema->colId);
    pSchema->bytes = htonl(pSrcSchema->bytes);
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pMeta->pSchema[i + pStb->numOfColumns];
    SSchema *pSrcSchema = &pStb->pTags[i];
S
Shengliang Guan 已提交
1245 1246 1247 1248
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
    pSchema->colId = htonl(pSrcSchema->colId);
    pSchema->bytes = htonl(pSrcSchema->bytes);
S
Shengliang Guan 已提交
1249
  }
S
Shengliang Guan 已提交
1250

S
Shengliang Guan 已提交
1251 1252 1253
  taosRUnLockLatch(&pStb->lock);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
1254

S
Shengliang Guan 已提交
1255 1256
  pReq->pCont = pMeta;
  pReq->contLen = contLen;
S
Shengliang Guan 已提交
1257

D
dapan1121 已提交
1258
  mDebug("stb:%s, meta is retrieved, cols:%d tags:%d", tbFName, pStb->numOfColumns, pStb->numOfTags);
S
Shengliang Guan 已提交
1259 1260
  return 0;
}
S
Shengliang Guan 已提交
1261

D
dapan 已提交
1262
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableMetaVersion *stbs, int32_t num, void **rsp, int32_t *rspLen) {
S
Shengliang Guan 已提交
1263 1264 1265 1266 1267
  SSdb          *pSdb = pMnode->pSdb;
  int32_t        bufSize = num * (sizeof(STableMetaRsp) + 4 * sizeof(SSchema));
  void          *buf = malloc(bufSize);
  int32_t        len = 0;
  int32_t        contLen = 0;
D
dapan 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276
  STableMetaRsp *pRsp = NULL;

  for (int32_t i = 0; i < num; ++i) {
    SSTableMetaVersion *stb = &stbs[i];
    stb->suid = be64toh(stb->suid);
    stb->sversion = ntohs(stb->sversion);
    stb->tversion = ntohs(stb->tversion);

    if ((contLen + sizeof(STableMetaRsp)) > bufSize) {
S
Shengliang Guan 已提交
1277
      bufSize = contLen + (num - i) * (sizeof(STableMetaRsp) + 4 * sizeof(SSchema));
D
dapan 已提交
1278 1279 1280 1281 1282 1283 1284 1285
      buf = realloc(buf, bufSize);
    }

    pRsp = (STableMetaRsp *)((char *)buf + contLen);

    strcpy(pRsp->dbFName, stb->dbFName);
    strcpy(pRsp->tbName, stb->stbName);
    strcpy(pRsp->stbName, stb->stbName);
S
Shengliang Guan 已提交
1286

D
dapan 已提交
1287
    mDebug("start to retrieve meta, db:%s, stb:%s", stb->dbFName, stb->stbName);
S
Shengliang Guan 已提交
1288

D
dapan 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
    SDbObj *pDb = mndAcquireDb(pMnode, stb->dbFName);
    if (pDb == NULL) {
      pRsp->numOfColumns = -1;
      pRsp->suid = htobe64(stb->suid);
      contLen += sizeof(STableMetaRsp);
      mWarn("db:%s, failed to require db since %s", stb->dbFName, terrstr());
      continue;
    }

    char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
    snprintf(tbFName, sizeof(tbFName), "%s.%s", stb->dbFName, stb->stbName);
S
Shengliang Guan 已提交
1300

D
dapan 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309
    SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
    if (pStb == NULL) {
      mndReleaseDb(pMnode, pDb);
      pRsp->numOfColumns = -1;
      pRsp->suid = htobe64(stb->suid);
      contLen += sizeof(STableMetaRsp);
      mWarn("stb:%s, failed to get meta since %s", tbFName, terrstr());
      continue;
    }
S
Shengliang Guan 已提交
1310

D
dapan 已提交
1311 1312 1313 1314 1315 1316 1317 1318
    taosRLockLatch(&pStb->lock);

    if (stb->suid == pStb->uid && stb->sversion == pStb->version) {
      taosRUnLockLatch(&pStb->lock);
      mndReleaseDb(pMnode, pDb);
      mndReleaseStb(pMnode, pStb);
      continue;
    }
S
Shengliang Guan 已提交
1319

D
dapan 已提交
1320 1321
    int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
    int32_t len = totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
1322

D
dapan 已提交
1323
    contLen += sizeof(STableMetaRsp) + len;
S
Shengliang Guan 已提交
1324

D
dapan 已提交
1325
    if (contLen > bufSize) {
S
Shengliang Guan 已提交
1326
      bufSize = contLen + (num - i - 1) * (sizeof(STableMetaRsp) + 4 * sizeof(SSchema));
D
dapan 已提交
1327 1328
      buf = realloc(buf, bufSize);
    }
S
Shengliang Guan 已提交
1329

D
dapan 已提交
1330 1331 1332 1333 1334 1335 1336 1337
    pRsp->numOfTags = htonl(pStb->numOfTags);
    pRsp->numOfColumns = htonl(pStb->numOfColumns);
    pRsp->precision = pDb->cfg.precision;
    pRsp->tableType = TSDB_SUPER_TABLE;
    pRsp->update = pDb->cfg.update;
    pRsp->sversion = htonl(pStb->version);
    pRsp->suid = htobe64(pStb->uid);
    pRsp->tuid = htobe64(pStb->uid);
S
Shengliang Guan 已提交
1338 1339

    for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
D
dapan 已提交
1340
      SSchema *pSchema = &pRsp->pSchema[i];
S
Shengliang Guan 已提交
1341
      SSchema *pSrcSchema = &pStb->pColumns[i];
D
dapan 已提交
1342 1343 1344 1345 1346
      memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
      pSchema->type = pSrcSchema->type;
      pSchema->colId = htonl(pSrcSchema->colId);
      pSchema->bytes = htonl(pSrcSchema->bytes);
    }
S
Shengliang Guan 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

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

D
dapan 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
    taosRUnLockLatch(&pStb->lock);
    mndReleaseDb(pMnode, pDb);
    mndReleaseStb(pMnode, pStb);
  }

  if (contLen > 0) {
    *rsp = buf;
    *rspLen = contLen;
  } else {
    *rsp = NULL;
    tfree(buf);
    *rspLen = 0;
  }

  return 0;
}

S
Shengliang Guan 已提交
1374
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
1375 1376 1377 1378 1379 1380 1381 1382
  SSdb *pSdb = pMnode->pSdb;

  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
1383
  int32_t numOfStbs = 0;
1384
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1385
  while (1) {
S
Shengliang Guan 已提交
1386
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
1387
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
1388 1389
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
1390
    if (strcmp(pStb->db, dbName) == 0) {
S
Shengliang Guan 已提交
1391
      numOfStbs++;
S
Shengliang Guan 已提交
1392 1393
    }

S
Shengliang Guan 已提交
1394
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1395 1396
  }

S
Shengliang Guan 已提交
1397
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
1398 1399 1400
  return 0;
}

S
Shengliang Guan 已提交
1401 1402
static int32_t mndGetStbMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
  SMnode *pMnode = pReq->pMnode;
1403
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1404

S
Shengliang Guan 已提交
1405
  if (mndGetNumOfStbs(pMnode, pShow->db, &pShow->numOfRows) != 0) {
S
Shengliang Guan 已提交
1406 1407 1408 1409
    return -1;
  }

  int32_t  cols = 0;
S
Shengliang Guan 已提交
1410
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
1411 1412 1413 1414

  pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
H
Haojun Liao 已提交
1415
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
1416 1417 1418 1419
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
H
Haojun Liao 已提交
1420 1421
  strcpy(pSchema[cols].name, "create_time");
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
1422 1423
  cols++;

S
Shengliang Guan 已提交
1424 1425
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
1426
  strcpy(pSchema[cols].name, "columns");
H
Haojun Liao 已提交
1427
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
1428 1429
  cols++;

S
Shengliang Guan 已提交
1430 1431
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
1432
  strcpy(pSchema[cols].name, "tags");
H
Haojun Liao 已提交
1433
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
1434 1435
  cols++;

S
Shengliang Guan 已提交
1436
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
1437 1438 1439 1440 1441 1442 1443
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

S
Shengliang Guan 已提交
1444
  pShow->numOfRows = sdbGetSize(pSdb, SDB_STB);
S
Shengliang Guan 已提交
1445
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
D
dapan1121 已提交
1446
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
1447 1448 1449 1450

  return 0;
}

S
Shengliang Guan 已提交
1451
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
1452 1453
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
  for (pos = 0; tableId[pos] != 0; ++pos) {
    if (tableId[pos] == '.') num++;
    if (num == 2) break;
  }

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

S
Shengliang Guan 已提交
1464 1465
static int32_t mndRetrieveStb(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode  *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
1466
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1467 1468 1469
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
1470
  char    *pWrite;
S
Shengliang Guan 已提交
1471
  char     prefix[TSDB_DB_FNAME_LEN] = {0};
S
Shengliang Guan 已提交
1472

S
Shengliang Guan 已提交
1473
  SDbObj *pDb = mndAcquireDb(pMnode, pShow->db);
S
Shengliang Guan 已提交
1474
  if (pDb == NULL) return 0;
S
Shengliang Guan 已提交
1475

S
Shengliang Guan 已提交
1476
  tstrncpy(prefix, pShow->db, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
1477 1478 1479 1480
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = (int32_t)strlen(prefix);

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

S
Shengliang Guan 已提交
1484
    if (pStb->dbUid != pDb->uid) {
H
Haojun Liao 已提交
1485
      if (strncmp(pStb->db, pDb->name, tListLen(pStb->db)) == 0) {
S
Shengliang Guan 已提交
1486
        mError("Inconsistent table data, name:%s, db:%s, dbUid:%" PRIu64, pStb->name, pDb->name, pDb->uid);
H
Haojun Liao 已提交
1487 1488
      }

S
Shengliang Guan 已提交
1489
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1490 1491 1492 1493 1494
      continue;
    }

    cols = 0;

S
Shengliang Guan 已提交
1495 1496
    char stbName[TSDB_TABLE_NAME_LEN] = {0};
    tstrncpy(stbName, pStb->name + prefixLen, TSDB_TABLE_NAME_LEN);
S
Shengliang Guan 已提交
1497
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1498
    STR_TO_VARSTR(pWrite, stbName);
S
Shengliang Guan 已提交
1499 1500 1501
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1502
    *(int64_t *)pWrite = pStb->createdTime;
S
Shengliang Guan 已提交
1503 1504 1505
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1506
    *(int32_t *)pWrite = pStb->numOfColumns;
S
Shengliang Guan 已提交
1507 1508 1509
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
1510
    *(int32_t *)pWrite = pStb->numOfTags;
S
Shengliang Guan 已提交
1511 1512 1513
    cols++;

    numOfRows++;
S
Shengliang Guan 已提交
1514
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
1515 1516
  }

S
Shengliang Guan 已提交
1517
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1518
  pShow->numOfReads += numOfRows;
S
Shengliang Guan 已提交
1519
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
1520 1521 1522
  return numOfRows;
}

S
Shengliang Guan 已提交
1523
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
1524 1525
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
L
Liu Jicong 已提交
1526
}