mndStb.c 29.1 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 34
#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);
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOldStb, SStbObj *pNewStb);
S
Shengliang Guan 已提交
35 36 37
static int32_t  mndProcesSMCreateStbReq(SMnodeMsg *pMsg);
static int32_t  mndProcesSMAlterStbReq(SMnodeMsg *pMsg);
static int32_t  mndProcesSMDropStbReq(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
38 39 40 41
static int32_t  mndProcessCreateStbInRsp(SMnodeMsg *pMsg);
static int32_t  mndProcessAlterStbInRsp(SMnodeMsg *pMsg);
static int32_t  mndProcessDropStbInRsp(SMnodeMsg *pMsg);
static int32_t  mndProcessStbMetaMsg(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
42
static int32_t  mndGetStbMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaRsp *pMeta);
S
Shengliang Guan 已提交
43 44 45 46 47
static int32_t  mndRetrieveStb(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows);
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 56 57
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcesSMCreateStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcesSMAlterStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcesSMDropStbReq);
H
Hongze Cheng 已提交
58 59 60
  mndSetMsgHandle(pMnode, TDMT_VND_CREATE_STB_RSP, mndProcessCreateStbInRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_ALTER_STB_RSP, mndProcessAlterStbInRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_STB_RSP, mndProcessDropStbInRsp);
S
Shengliang Guan 已提交
61
  mndSetMsgHandle(pMnode, TDMT_MND_STB_META, mndProcessStbMetaMsg);
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 87 88
  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)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, STB_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, STB_ENCODE_OVER)
S
Shengliang Guan 已提交
89

S
Shengliang Guan 已提交
90 91 92
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pStb->pSchema[i];
93 94 95 96
    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 已提交
97 98
  }

99 100 101 102 103 104 105 106 107 108 109
  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 已提交
110

111
  mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
112 113 114
  return pRaw;
}

S
Shengliang Guan 已提交
115
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
116 117
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
118
  int8_t sver = 0;
119
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
120

S
Shengliang Guan 已提交
121
  if (sver != TSDB_STB_VER_NUMBER) {
S
Shengliang Guan 已提交
122
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
123
    goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
124 125
  }

S
Shengliang Guan 已提交
126 127
  int32_t  size = sizeof(SStbObj) + TSDB_MAX_COLUMNS * sizeof(SSchema);
  SSdbRow *pRow = sdbAllocRow(size);
128 129
  if (pRow == NULL) goto STB_DECODE_OVER;

S
Shengliang Guan 已提交
130
  SStbObj *pStb = sdbGetRowObj(pRow);
131
  if (pStb == NULL) goto STB_DECODE_OVER;
S
Shengliang Guan 已提交
132 133

  int32_t dataPos = 0;
134 135 136 137 138 139 140 141 142
  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)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, STB_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, STB_DECODE_OVER)
S
Shengliang Guan 已提交
143

S
Shengliang Guan 已提交
144 145
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  pStb->pSchema = calloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147 148
  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pStb->pSchema[i];
149 150 151 152
    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 已提交
153 154
  }

155 156 157 158 159 160 161 162 163 164
  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 已提交
165

166
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
167 168 169
  return pRow;
}

S
Shengliang Guan 已提交
170
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
171
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
172 173 174
  return 0;
}

S
Shengliang Guan 已提交
175
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
176
  mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
177 178 179
  return 0;
}

S
Shengliang Guan 已提交
180
static int32_t mndStbActionUpdate(SSdb *pSdb, SStbObj *pOldStb, SStbObj *pNewStb) {
S
Shengliang Guan 已提交
181
  mTrace("stb:%s, perform update action, old row:%p new row:%p", pOldStb->name, pOldStb, pNewStb);
S
Shengliang Guan 已提交
182 183
  atomic_exchange_32(&pOldStb->updateTime, pNewStb->updateTime);
  atomic_exchange_32(&pOldStb->version, pNewStb->version);
S
Shengliang Guan 已提交
184

S
Shengliang Guan 已提交
185
  taosWLockLatch(&pOldStb->lock);
S
Shengliang Guan 已提交
186 187
  pOldStb->numOfColumns = pNewStb->numOfColumns;
  pOldStb->numOfTags = pNewStb->numOfTags;
S
Shengliang Guan 已提交
188 189
  int32_t totalCols = pNewStb->numOfTags + pNewStb->numOfColumns;
  int32_t totalSize = totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
190

S
Shengliang Guan 已提交
191
  if (pOldStb->numOfTags + pOldStb->numOfColumns < totalCols) {
S
Shengliang Guan 已提交
192 193 194 195 196
    void *pSchema = malloc(totalSize);
    if (pSchema != NULL) {
      free(pOldStb->pSchema);
      pOldStb->pSchema = pSchema;
    }
S
Shengliang Guan 已提交
197 198
  }

S
Shengliang Guan 已提交
199
  memcpy(pOldStb->pSchema, pNewStb->pSchema, totalSize);
S
Shengliang Guan 已提交
200
  taosWUnLockLatch(&pOldStb->lock);
S
Shengliang Guan 已提交
201 202 203
  return 0;
}

S
Shengliang Guan 已提交
204
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
205
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
206 207 208 209 210
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
211 212
}

S
Shengliang Guan 已提交
213
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
214 215 216 217
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

S
Shengliang Guan 已提交
218 219 220
static SDbObj *mndAcquireDbByStb(SMnode *pMnode, char *stbName) {
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
221

S
Shengliang Guan 已提交
222 223
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
224

S
Shengliang Guan 已提交
225 226
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
227

H
more  
Hongze Cheng 已提交
228
static void *mndBuildCreateStbMsg(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int *pContLen) {
H
more  
Hongze Cheng 已提交
229
  SVCreateTbReq req;
230
  void         *buf;
H
more  
Hongze Cheng 已提交
231
  int           bsize;
232
  SMsgHead     *pMsgHead;
H
more  
Hongze Cheng 已提交
233 234

  req.ver = 0;
235 236 237 238
  SName name = {0};
  tNameFromString(&name, pStb->name, T_NAME_ACCT|T_NAME_DB|T_NAME_TABLE);

  req.name = (char*) tNameGetTableName(&name);
H
more  
Hongze Cheng 已提交
239 240 241 242 243 244 245 246 247 248
  req.ttl = 0;
  req.keep = 0;
  req.type = TD_SUPER_TABLE;
  req.stbCfg.suid = pStb->uid;
  req.stbCfg.nCols = pStb->numOfColumns;
  req.stbCfg.pSchema = pStb->pSchema;
  req.stbCfg.nTagCols = pStb->numOfTags;
  req.stbCfg.pTagSchema = pStb->pSchema + pStb->numOfColumns;

  bsize = tSerializeSVCreateTbReq(NULL, &req);
H
more  
Hongze Cheng 已提交
249
  buf = malloc(sizeof(SMsgHead) + bsize);
H
more  
Hongze Cheng 已提交
250 251 252 253 254
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

H
more  
Hongze Cheng 已提交
255 256 257 258 259 260
  pMsgHead = (SMsgHead *)buf;

  pMsgHead->contLen = htonl(sizeof(SMsgHead) + bsize);
  pMsgHead->vgId = htonl(pVgroup->vgId);

  void *pBuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
H
more  
Hongze Cheng 已提交
261 262
  tSerializeSVCreateTbReq(&pBuf, &req);

H
more  
Hongze Cheng 已提交
263
  *pContLen = sizeof(SMsgHead) + bsize;
H
more  
Hongze Cheng 已提交
264
  return buf;
265 266
}

S
Shengliang Guan 已提交
267 268
static SVDropTbReq *mndBuildDropStbMsg(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb) {
  int32_t contLen = sizeof(SVDropTbReq);
269

S
Shengliang Guan 已提交
270
  SVDropTbReq *pDrop = calloc(1, contLen);
271 272 273 274 275 276 277 278
  if (pDrop == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  pDrop->head.contLen = htonl(contLen);
  pDrop->head.vgId = htonl(pVgroup->vgId);
  memcpy(pDrop->name, pStb->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
279
  // pDrop->suid = htobe64(pStb->uid);
280 281 282 283

  return pDrop;
}

S
Shengliang Guan 已提交
284
static int32_t mndCheckCreateStbMsg(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
285 286 287 288 289 290 291
  pCreate->numOfColumns = htonl(pCreate->numOfColumns);
  pCreate->numOfTags = htonl(pCreate->numOfTags);
  int32_t totalCols = pCreate->numOfColumns + pCreate->numOfTags;
  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pCreate->pSchema[i];
    pSchema->bytes = htonl(pSchema->bytes);
  }
S
Shengliang Guan 已提交
292

S
Shengliang Guan 已提交
293
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
294
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
295 296
    return -1;
  }
S
Shengliang Guan 已提交
297

S
Shengliang Guan 已提交
298
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
299
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
300 301
    return -1;
  }
S
Shengliang Guan 已提交
302

S
Shengliang Guan 已提交
303
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
304
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
305 306
    return -1;
  }
S
Shengliang Guan 已提交
307

S
Shengliang Guan 已提交
308 309 310
  int32_t maxColId = (TSDB_MAX_COLUMNS + TSDB_MAX_TAGS);
  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pCreate->pSchema[i];
S
Shengliang Guan 已提交
311 312
    if (pSchema->type < 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
313 314 315
      return -1;
    }
    if (pSchema->bytes <= 0) {
S
Shengliang Guan 已提交
316
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
317 318 319
      return -1;
    }
    if (pSchema->name[0] == 0) {
S
Shengliang Guan 已提交
320
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
321 322 323
      return -1;
    }
  }
S
Shengliang Guan 已提交
324

S
Shengliang Guan 已提交
325 326 327
  return 0;
}

328
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
329 330 331 332 333 334 335 336
  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;
}

337
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
338 339 340 341 342 343 344 345
  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;
}

346
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
347 348 349 350 351 352 353 354
  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;
}

355
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
356
  SSdb   *pSdb = pMnode->pSdb;
357
  SVgObj *pVgroup = NULL;
358
  void   *pIter = NULL;
H
more  
Hongze Cheng 已提交
359
  int     contLen;
360 361 362 363 364 365

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

H
more  
Hongze Cheng 已提交
366
    void *pMsg = mndBuildCreateStbMsg(pMnode, pVgroup, pStb, &contLen);
367 368 369 370 371 372
    if (pMsg == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
373

374 375 376
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pMsg;
S
Shengliang Guan 已提交
377
    action.contLen = contLen;
H
Hongze Cheng 已提交
378
    action.msgType = TDMT_VND_CREATE_STB;
379 380 381 382 383 384 385 386
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      free(pMsg);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
387 388 389 390

  return 0;
}

391
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
392
  SSdb   *pSdb = pMnode->pSdb;
393
  SVgObj *pVgroup = NULL;
394
  void   *pIter = NULL;
395 396 397 398 399 400

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

S
Shengliang Guan 已提交
401
    SVDropTbReq *pMsg = mndBuildDropStbMsg(pMnode, pVgroup, pStb);
402 403 404 405 406 407
    if (pMsg == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
408

409 410 411
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pMsg;
S
Shengliang Guan 已提交
412
    action.contLen = sizeof(SVDropTbReq);
H
Hongze Cheng 已提交
413
    action.msgType = TDMT_VND_DROP_STB;
414 415 416 417 418 419 420 421
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
      free(pMsg);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
422 423 424 425

  return 0;
}

S
Shengliang Guan 已提交
426
static int32_t mndCreateStb(SMnode *pMnode, SMnodeMsg *pMsg, SMCreateStbReq *pCreate, SDbObj *pDb) {
S
Shengliang Guan 已提交
427 428
  SStbObj stbObj = {0};
  tstrncpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
429
  tstrncpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
430 431
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
432
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
433
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446
  stbObj.version = 1;
  stbObj.numOfColumns = pCreate->numOfColumns;
  stbObj.numOfTags = pCreate->numOfTags;

  int32_t totalCols = stbObj.numOfColumns + stbObj.numOfTags;
  int32_t totalSize = totalCols * sizeof(SSchema);
  stbObj.pSchema = malloc(totalSize);
  if (stbObj.pSchema == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  memcpy(stbObj.pSchema, pCreate->pSchema, totalSize);

S
Shengliang Guan 已提交
447 448 449 450
  for (int32_t i = 0; i < totalCols; ++i) {
    stbObj.pSchema[i].colId = i + 1;
  }

S
Shengliang Guan 已提交
451
  int32_t code = 0;
S
Shengliang Guan 已提交
452
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pMsg->rpcMsg);
S
Shengliang Guan 已提交
453 454 455 456 457 458
  if (pTrans == NULL) {
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }
  mDebug("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);

459
  if (mndSetCreateStbRedoLogs(pMnode, pTrans, pDb, &stbObj) != 0) {
S
Shengliang Guan 已提交
460 461
    mError("trans:%d, failed to set redo log since %s", pTrans->id, terrstr());
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
462 463
  }

464
  if (mndSetCreateStbUndoLogs(pMnode, pTrans, pDb, &stbObj) != 0) {
S
Shengliang Guan 已提交
465 466
    mError("trans:%d, failed to set undo log since %s", pTrans->id, terrstr());
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
467 468
  }

469
  if (mndSetCreateStbCommitLogs(pMnode, pTrans, pDb, &stbObj) != 0) {
S
Shengliang Guan 已提交
470 471 472 473
    mError("trans:%d, failed to set commit log since %s", pTrans->id, terrstr());
    goto CREATE_STB_OVER;
  }

474
  if (mndSetCreateStbRedoActions(pMnode, pTrans, pDb, &stbObj) != 0) {
S
Shengliang Guan 已提交
475 476 477 478
    mError("trans:%d, failed to set redo actions since %s", pTrans->id, terrstr());
    goto CREATE_STB_OVER;
  }

479
  if (mndSetCreateStbUndoActions(pMnode, pTrans, pDb, &stbObj) != 0) {
S
Shengliang Guan 已提交
480 481
    mError("trans:%d, failed to set redo actions since %s", pTrans->id, terrstr());
    goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
482 483
  }

S
Shengliang Guan 已提交
484
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
485 486 487 488 489
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

S
Shengliang Guan 已提交
490 491 492
  code = 0;

CREATE_STB_OVER:
S
Shengliang Guan 已提交
493
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
494
  return code;
S
Shengliang Guan 已提交
495 496
}

S
Shengliang Guan 已提交
497
static int32_t mndProcesSMCreateStbReq(SMnodeMsg *pMsg) {
498
  SMnode        *pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
499
  SMCreateStbReq *pCreate = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
500

S
Shengliang Guan 已提交
501
  mDebug("stb:%s, start to create", pCreate->name);
S
Shengliang Guan 已提交
502

503
  if (mndCheckCreateStbMsg(pCreate) != 0) {
S
Shengliang Guan 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

  SStbObj *pStb = mndAcquireStb(pMnode, pCreate->name);
  if (pStb != NULL) {
    sdbRelease(pMnode->pSdb, pStb);
    if (pCreate->igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", pCreate->name);
      return 0;
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
      mError("db:%s, failed to create since %s", pCreate->name, terrstr());
      return -1;
    }
  }

H
more  
Hongze Cheng 已提交
521
  // topic should have different name with stb
L
Liu Jicong 已提交
522 523 524 525 526 527 528 529
  SStbObj *pTopic = mndAcquireStb(pMnode, pCreate->name);
  if (pTopic != NULL) {
    sdbRelease(pMnode->pSdb, pTopic);
    terrno = TSDB_CODE_MND_NAME_CONFLICT_WITH_TOPIC;
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
  SDbObj *pDb = mndAcquireDbByStb(pMnode, pCreate->name);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

  int32_t code = mndCreateStb(pMnode, pMsg, pCreate, pDb);
  mndReleaseDb(pMnode, pDb);

  if (code != 0) {
    terrno = code;
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

549
static int32_t mndProcessCreateStbInRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
550
  mndTransProcessRsp(pMsg);
551 552
  return 0;
}
S
Shengliang Guan 已提交
553

S
Shengliang Guan 已提交
554
static int32_t mndCheckAlterStbMsg(SMAlterStbReq *pAlter) {
S
Shengliang Guan 已提交
555 556 557 558 559
  SSchema *pSchema = &pAlter->schema;
  pSchema->colId = htonl(pSchema->colId);
  pSchema->bytes = htonl(pSchema->bytes);

  if (pSchema->type <= 0) {
S
Shengliang Guan 已提交
560
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
561 562 563
    return -1;
  }
  if (pSchema->colId < 0 || pSchema->colId >= (TSDB_MAX_COLUMNS + TSDB_MAX_TAGS)) {
S
Shengliang Guan 已提交
564
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
565 566 567
    return -1;
  }
  if (pSchema->bytes <= 0) {
S
Shengliang Guan 已提交
568
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
569 570 571
    return -1;
  }
  if (pSchema->name[0] == 0) {
S
Shengliang Guan 已提交
572
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
573 574 575 576 577 578 579 580
    return -1;
  }

  return 0;
}

static int32_t mndUpdateStb(SMnode *pMnode, SMnodeMsg *pMsg, SStbObj *pOldStb, SStbObj *pNewStb) { return 0; }

S
Shengliang Guan 已提交
581
static int32_t mndProcesSMAlterStbReq(SMnodeMsg *pMsg) {
582
  SMnode       *pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
583
  SMAlterStbReq *pAlter = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611

  mDebug("stb:%s, start to alter", pAlter->name);

  if (mndCheckAlterStbMsg(pAlter) != 0) {
    mError("stb:%s, failed to alter since %s", pAlter->name, terrstr());
    return -1;
  }

  SStbObj *pStb = mndAcquireStb(pMnode, pAlter->name);
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
    mError("stb:%s, failed to alter since %s", pAlter->name, terrstr());
    return -1;
  }

  SStbObj stbObj = {0};
  memcpy(&stbObj, pStb, sizeof(SStbObj));

  int32_t code = mndUpdateStb(pMnode, pMsg, pStb, &stbObj);
  mndReleaseStb(pMnode, pStb);

  if (code != 0) {
    mError("stb:%s, failed to alter since %s", pAlter->name, tstrerror(code));
    return code;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}
S
Shengliang Guan 已提交
612

613
static int32_t mndProcessAlterStbInRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
614
  mndTransProcessRsp(pMsg);
615 616
  return 0;
}
S
Shengliang Guan 已提交
617

S
Shengliang Guan 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
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 mndSetDropStbUndoLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pUndoRaw = mndStbActionEncode(pStb);
  if (pUndoRaw == NULL) return -1;
  if (mndTransAppendUndolog(pTrans, pUndoRaw) != 0) return -1;
  if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_READY) != 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;
}

static int32_t mndSetDropStbRedoActions(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) { return 0; }

static int32_t mndSetDropStbUndoActions(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) { return 0; }

S
Shengliang Guan 已提交
649
static int32_t mndDropStb(SMnode *pMnode, SMnodeMsg *pMsg, SStbObj *pStb) {
S
Shengliang Guan 已提交
650
  int32_t code = -1;
S
Shengliang Guan 已提交
651
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pMsg->rpcMsg);
S
Shengliang Guan 已提交
652 653 654 655 656 657
  if (pTrans == NULL) {
    mError("stb:%s, failed to drop since %s", pStb->name, terrstr());
    return -1;
  }
  mDebug("trans:%d, used to drop stb:%s", pTrans->id, pStb->name);

S
Shengliang Guan 已提交
658 659 660
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) {
    mError("trans:%d, failed to set redo log since %s", pTrans->id, terrstr());
    goto DROP_STB_OVER;
S
Shengliang Guan 已提交
661 662
  }

S
Shengliang Guan 已提交
663 664 665
  if (mndSetDropStbUndoLogs(pMnode, pTrans, pStb) != 0) {
    mError("trans:%d, failed to set undo log since %s", pTrans->id, terrstr());
    goto DROP_STB_OVER;
S
Shengliang Guan 已提交
666 667
  }

S
Shengliang Guan 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) {
    mError("trans:%d, failed to set commit log since %s", pTrans->id, terrstr());
    goto DROP_STB_OVER;
  }

  if (mndSetDropStbRedoActions(pMnode, pTrans, pStb) != 0) {
    mError("trans:%d, failed to set redo actions since %s", pTrans->id, terrstr());
    goto DROP_STB_OVER;
  }

  if (mndSetDropStbUndoActions(pMnode, pTrans, pStb) != 0) {
    mError("trans:%d, failed to set redo actions since %s", pTrans->id, terrstr());
    goto DROP_STB_OVER;
S
Shengliang Guan 已提交
681 682
  }

S
Shengliang Guan 已提交
683
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
684
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
685
    goto DROP_STB_OVER;
S
Shengliang Guan 已提交
686 687
  }

S
Shengliang Guan 已提交
688 689 690
  code = 0;

DROP_STB_OVER:
S
Shengliang Guan 已提交
691 692 693 694
  mndTransDrop(pTrans);
  return 0;
}

S
Shengliang Guan 已提交
695
static int32_t mndProcesSMDropStbReq(SMnodeMsg *pMsg) {
696
  SMnode      *pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
697
  SMDropStbReq *pDrop = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

  mDebug("stb:%s, start to drop", pDrop->name);

  SStbObj *pStb = mndAcquireStb(pMnode, pDrop->name);
  if (pStb == NULL) {
    if (pDrop->igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", pDrop->name);
      return 0;
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
      mError("stb:%s, failed to drop since %s", pDrop->name, terrstr());
      return -1;
    }
  }

  int32_t code = mndDropStb(pMnode, pMsg, pStb);
  mndReleaseStb(pMnode, pStb);

  if (code != 0) {
    terrno = code;
    mError("stb:%s, failed to drop since %s", pDrop->name, terrstr());
    return -1;
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}
S
Shengliang Guan 已提交
724

725
static int32_t mndProcessDropStbInRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
726
  mndTransProcessRsp(pMsg);
727 728
  return 0;
}
S
Shengliang Guan 已提交
729 730

static int32_t mndProcessStbMetaMsg(SMnodeMsg *pMsg) {
731
  SMnode        *pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
732
  STableInfoReq *pInfo = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
733

S
Shengliang Guan 已提交
734
  mDebug("stb:%s, start to retrieve meta", pInfo->tableFname);
S
Shengliang Guan 已提交
735

S
Shengliang Guan 已提交
736
  SDbObj *pDb = mndAcquireDbByStb(pMnode, pInfo->tableFname);
S
Shengliang Guan 已提交
737 738
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
739
    mError("stb:%s, failed to retrieve meta since %s", pInfo->tableFname, terrstr());
S
Shengliang Guan 已提交
740 741 742
    return -1;
  }

S
Shengliang Guan 已提交
743
  SStbObj *pStb = mndAcquireStb(pMnode, pInfo->tableFname);
S
Shengliang Guan 已提交
744 745
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
746
    terrno = TSDB_CODE_MND_INVALID_STB;
S
Shengliang Guan 已提交
747
    mError("stb:%s, failed to get meta since %s", pInfo->tableFname, terrstr());
S
Shengliang Guan 已提交
748 749 750
    return -1;
  }

S
Shengliang Guan 已提交
751 752
  taosRLockLatch(&pStb->lock);
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
S
Shengliang Guan 已提交
753
  int32_t contLen = sizeof(STableMetaRsp) + totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
754

S
Shengliang Guan 已提交
755
  STableMetaRsp *pMeta = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
756
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
757 758 759
    taosRUnLockLatch(&pStb->lock);
    mndReleaseDb(pMnode, pDb);
    mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
760
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
761
    mError("stb:%s, failed to get meta since %s", pInfo->tableFname, terrstr());
S
Shengliang Guan 已提交
762 763 764
    return -1;
  }

D
catalog  
dapan1121 已提交
765
  memcpy(pMeta->tbFname, pStb->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
766 767 768 769 770 771
  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 已提交
772 773
  pMeta->suid = htobe64(pStb->uid);
  pMeta->tuid = htobe64(pStb->uid);
S
Shengliang Guan 已提交
774

S
Shengliang Guan 已提交
775
  for (int32_t i = 0; i < totalCols; ++i) {
S
Shengliang Guan 已提交
776
    SSchema *pSchema = &pMeta->pSchema[i];
S
Shengliang Guan 已提交
777 778 779 780 781
    SSchema *pSrcSchema = &pStb->pSchema[i];
    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 已提交
782
  }
S
Shengliang Guan 已提交
783 784 785
  taosRUnLockLatch(&pStb->lock);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
786 787 788 789

  pMsg->pCont = pMeta;
  pMsg->contLen = contLen;

S
Shengliang Guan 已提交
790
  mDebug("stb:%s, meta is retrieved, cols:%d tags:%d", pInfo->tableFname, pStb->numOfColumns, pStb->numOfTags);
S
Shengliang Guan 已提交
791 792
  return 0;
}
S
Shengliang Guan 已提交
793

S
Shengliang Guan 已提交
794
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
795 796 797 798 799 800 801 802
  SSdb *pSdb = pMnode->pSdb;

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

S
Shengliang Guan 已提交
803
  int32_t numOfStbs = 0;
804
  void   *pIter = NULL;
S
Shengliang Guan 已提交
805
  while (1) {
S
Shengliang Guan 已提交
806
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
807
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
808 809
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
810
    if (strcmp(pStb->db, dbName) == 0) {
S
Shengliang Guan 已提交
811
      numOfStbs++;
S
Shengliang Guan 已提交
812 813
    }

S
Shengliang Guan 已提交
814
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
815 816
  }

S
Shengliang Guan 已提交
817
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
818 819 820
  return 0;
}

S
Shengliang Guan 已提交
821
static int32_t mndGetStbMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaRsp *pMeta) {
S
Shengliang Guan 已提交
822
  SMnode *pMnode = pMsg->pMnode;
823
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
824

S
Shengliang Guan 已提交
825
  if (mndGetNumOfStbs(pMnode, pShow->db, &pShow->numOfRows) != 0) {
S
Shengliang Guan 已提交
826 827 828 829
    return -1;
  }

  int32_t  cols = 0;
S
Shengliang Guan 已提交
830
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
831 832 833 834

  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 已提交
835
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
836 837 838 839
  cols++;

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

S
Shengliang Guan 已提交
844 845
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
846
  strcpy(pSchema[cols].name, "columns");
H
Haojun Liao 已提交
847
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
848 849
  cols++;

S
Shengliang Guan 已提交
850 851
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
852
  strcpy(pSchema[cols].name, "tags");
H
Haojun Liao 已提交
853
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
854 855
  cols++;

S
Shengliang Guan 已提交
856
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
857 858 859 860 861 862 863
  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 已提交
864
  pShow->numOfRows = sdbGetSize(pSdb, SDB_STB);
S
Shengliang Guan 已提交
865
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
866
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
867 868 869 870

  return 0;
}

S
Shengliang Guan 已提交
871
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
872 873
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
874 875 876 877 878 879 880 881 882 883
  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 已提交
884
static int32_t mndRetrieveStb(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
S
Shengliang Guan 已提交
885 886
  SMnode  *pMnode = pMsg->pMnode;
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
887 888 889
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
890
  char    *pWrite;
S
Shengliang Guan 已提交
891
  char     prefix[64] = {0};
S
Shengliang Guan 已提交
892

S
Shengliang Guan 已提交
893 894 895 896 897
  SDbObj *pDb = mndAcquireDb(pMnode, pShow->db);
  if (pDb == NULL) {
    return TSDB_CODE_MND_INVALID_DB;
  }

S
Shengliang Guan 已提交
898 899 900 901 902
  tstrncpy(prefix, pShow->db, 64);
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = (int32_t)strlen(prefix);

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

S
Shengliang Guan 已提交
906
    if (pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
907
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
908 909 910 911 912
      continue;
    }

    cols = 0;

S
Shengliang Guan 已提交
913 914
    char stbName[TSDB_TABLE_NAME_LEN] = {0};
    tstrncpy(stbName, pStb->name + prefixLen, TSDB_TABLE_NAME_LEN);
S
Shengliang Guan 已提交
915
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
916
    STR_TO_VARSTR(pWrite, stbName);
S
Shengliang Guan 已提交
917 918 919
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
920
    *(int64_t *)pWrite = pStb->createdTime;
S
Shengliang Guan 已提交
921 922 923
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
924
    *(int32_t *)pWrite = pStb->numOfColumns;
S
Shengliang Guan 已提交
925 926 927
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
928
    *(int32_t *)pWrite = pStb->numOfTags;
S
Shengliang Guan 已提交
929 930 931
    cols++;

    numOfRows++;
S
Shengliang Guan 已提交
932
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
933 934
  }

S
Shengliang Guan 已提交
935
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
936
  pShow->numOfReads += numOfRows;
S
Shengliang Guan 已提交
937
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
938 939 940
  return numOfRows;
}

S
Shengliang Guan 已提交
941
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
942 943
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
L
Liu Jicong 已提交
944
}