mndStb.c 28.2 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 36 37 38 39 40 41 42 43
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
static int32_t  mndProcessMCreateStbReq(SMnodeMsg *pReq);
static int32_t  mndProcessMAlterStbReq(SMnodeMsg *pReq);
static int32_t  mndProcessMDropStbReq(SMnodeMsg *pReq);
static int32_t  mndProcessVCreateStbRsp(SMnodeMsg *pRsp);
static int32_t  mndProcessVAlterStbRsp(SMnodeMsg *pRsp);
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 56 57 58 59 60 61
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcessMCreateStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcessMAlterStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcessMDropStbReq);
  mndSetMsgHandle(pMnode, TDMT_VND_CREATE_STB_RSP, mndProcessVCreateStbRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_ALTER_STB_RSP, mndProcessVAlterStbRsp);
  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 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 已提交
126
  SSdbRow *pRow = sdbAllocRow(sizeof(SStbObj));
127 128
  if (pRow == NULL) goto STB_DECODE_OVER;

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

  int32_t dataPos = 0;
133 134 135 136 137 138 139 140 141
  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 已提交
142

S
Shengliang Guan 已提交
143 144
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  pStb->pSchema = calloc(totalCols, sizeof(SSchema));
S
Shengliang 已提交
145 146 147
  if (pStb->pSchema == NULL) {
    goto STB_DECODE_OVER;
  }
S
Shengliang Guan 已提交
148

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

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

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

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

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

S
Shengliang Guan 已提交
182 183 184 185
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);
  atomic_exchange_32(&pOld->updateTime, pNew->updateTime);
  atomic_exchange_32(&pOld->version, pNew->version);
S
Shengliang Guan 已提交
186

S
Shengliang Guan 已提交
187 188 189 190
  taosWLockLatch(&pOld->lock);
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
  int32_t totalCols = pNew->numOfTags + pNew->numOfColumns;
S
Shengliang Guan 已提交
191
  int32_t totalSize = totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
192

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

S
Shengliang Guan 已提交
201 202
  memcpy(pOld->pSchema, pNew->pSchema, totalSize);
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
203 204 205
  return 0;
}

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

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

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

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

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

S
Shengliang Guan 已提交
230
static void *mndBuildCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
more  
Hongze Cheng 已提交
231
  SVCreateTbReq req;
232
  void         *buf;
S
Shengliang Guan 已提交
233
  int32_t       bsize;
234
  SMsgHead     *pMsgHead;
H
more  
Hongze Cheng 已提交
235 236

  req.ver = 0;
237
  SName name = {0};
S
Shengliang Guan 已提交
238
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
239

S
Shengliang Guan 已提交
240
  req.name = (char *)tNameGetTableName(&name);
H
more  
Hongze Cheng 已提交
241 242 243 244 245 246 247 248 249 250
  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 已提交
251
  buf = malloc(sizeof(SMsgHead) + bsize);
H
more  
Hongze Cheng 已提交
252 253 254 255 256
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

H
more  
Hongze Cheng 已提交
257 258 259 260 261 262
  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 已提交
263 264
  tSerializeSVCreateTbReq(&pBuf, &req);

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

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

S
Shengliang Guan 已提交
272
  SVDropTbReq *pDrop = calloc(1, contLen);
273 274 275 276 277 278 279 280
  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 已提交
281
  pDrop->suid = htobe64(pStb->uid);
282 283 284 285

  return pDrop;
}

S
Shengliang Guan 已提交
286
static int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
287 288 289 290 291 292 293
  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 已提交
294

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

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

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

S
Shengliang Guan 已提交
310 311 312
  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 已提交
313 314
    if (pSchema->type < 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
315 316 317
      return -1;
    }
    if (pSchema->bytes <= 0) {
S
Shengliang Guan 已提交
318
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
319 320 321
      return -1;
    }
    if (pSchema->name[0] == 0) {
S
Shengliang Guan 已提交
322
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
323 324 325
      return -1;
    }
  }
S
Shengliang Guan 已提交
326

S
Shengliang Guan 已提交
327 328 329
  return 0;
}

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

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

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

357
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
358
  SSdb   *pSdb = pMnode->pSdb;
359
  SVgObj *pVgroup = NULL;
360
  void   *pIter = NULL;
S
Shengliang Guan 已提交
361
  int32_t     contLen;
362 363 364 365 366 367

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

S
Shengliang Guan 已提交
368 369
    void *pReq = mndBuildCreateStbReq(pMnode, pVgroup, pStb, &contLen);
    if (pReq == NULL) {
370 371 372 373 374
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
375

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

  return 0;
}

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

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

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

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

  return 0;
}

S
Shengliang Guan 已提交
428
static int32_t mndCreateStb(SMnode *pMnode, SMnodeMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
S
Shengliang Guan 已提交
429 430
  SStbObj stbObj = {0};
  tstrncpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
431
  tstrncpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
432 433
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
434
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
435
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448
  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 已提交
449 450 451 452
  for (int32_t i = 0; i < totalCols; ++i) {
    stbObj.pSchema[i].colId = i + 1;
  }

S
Shengliang 已提交
453
  int32_t code = -1;
S
Shengliang Guan 已提交
454 455
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
  if (pTrans == NULL) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
456

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

S
Shengliang Guan 已提交
459 460 461 462 463 464
  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 已提交
465

S
Shengliang Guan 已提交
466 467 468
  code = 0;

CREATE_STB_OVER:
S
Shengliang Guan 已提交
469
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
470
  return code;
S
Shengliang Guan 已提交
471 472
}

S
Shengliang Guan 已提交
473 474 475
static int32_t mndProcessMCreateStbReq(SMnodeMsg *pReq) {
  SMnode         *pMnode = pReq->pMnode;
  SMCreateStbReq *pCreate = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
476

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

S
Shengliang Guan 已提交
479
  if (mndCheckCreateStbReq(pCreate) != 0) {
S
Shengliang Guan 已提交
480 481 482 483 484 485
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

  SStbObj *pStb = mndAcquireStb(pMnode, pCreate->name);
  if (pStb != NULL) {
S
Shengliang 已提交
486
    mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
487 488 489 490 491
    if (pCreate->igExists) {
      mDebug("stb:%s, already exist, ignore exist is set", pCreate->name);
      return 0;
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
S
Shengliang Guan 已提交
492
      mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
S
Shengliang Guan 已提交
493 494
      return -1;
    }
S
Shengliang Guan 已提交
495 496
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
S
Shengliang 已提交
497
    return -1;
S
Shengliang Guan 已提交
498 499
  }

H
more  
Hongze Cheng 已提交
500
  // topic should have different name with stb
L
Liu Jicong 已提交
501 502 503 504 505 506 507 508
  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 已提交
509 510 511 512 513 514 515
  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;
  }

S
Shengliang Guan 已提交
516
  int32_t code = mndCreateStb(pMnode, pReq, pCreate, pDb);
S
Shengliang Guan 已提交
517 518 519 520 521 522 523 524 525 526 527
  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;
}

S
Shengliang Guan 已提交
528 529
static int32_t mndProcessVCreateStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
530 531
  return 0;
}
S
Shengliang Guan 已提交
532

S
Shengliang Guan 已提交
533
static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) {
S
Shengliang Guan 已提交
534 535 536 537 538
  SSchema *pSchema = &pAlter->schema;
  pSchema->colId = htonl(pSchema->colId);
  pSchema->bytes = htonl(pSchema->bytes);

  if (pSchema->type <= 0) {
S
Shengliang Guan 已提交
539
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
540 541 542
    return -1;
  }
  if (pSchema->colId < 0 || pSchema->colId >= (TSDB_MAX_COLUMNS + TSDB_MAX_TAGS)) {
S
Shengliang Guan 已提交
543
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
544 545 546
    return -1;
  }
  if (pSchema->bytes <= 0) {
S
Shengliang Guan 已提交
547
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
548 549 550
    return -1;
  }
  if (pSchema->name[0] == 0) {
S
Shengliang Guan 已提交
551
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
552 553 554 555 556 557
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
558
static int32_t mndUpdateStb(SMnode *pMnode, SMnodeMsg *pReq, SStbObj *pOld, SStbObj *pNew) { return 0; }
S
Shengliang Guan 已提交
559

S
Shengliang Guan 已提交
560 561 562
static int32_t mndProcessMAlterStbReq(SMnodeMsg *pReq) {
  SMnode        *pMnode = pReq->pMnode;
  SMAlterStbReq *pAlter = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
563 564 565

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

S
Shengliang Guan 已提交
566
  if (mndCheckAlterStbReq(pAlter) != 0) {
S
Shengliang Guan 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580
    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));

S
Shengliang Guan 已提交
581
  int32_t code = mndUpdateStb(pMnode, pReq, pStb, &stbObj);
S
Shengliang Guan 已提交
582 583 584 585 586 587 588 589 590
  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 已提交
591

S
Shengliang Guan 已提交
592 593
static int32_t mndProcessVAlterStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
594 595
  return 0;
}
S
Shengliang Guan 已提交
596

S
Shengliang Guan 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
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 已提交
628
static int32_t mndDropStb(SMnode *pMnode, SMnodeMsg *pReq, SStbObj *pStb) {
S
Shengliang Guan 已提交
629
  int32_t code = -1;
S
Shengliang Guan 已提交
630 631
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
  if (pTrans == NULL)goto DROP_STB_OVER;
S
Shengliang Guan 已提交
632

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

S
Shengliang Guan 已提交
635 636 637 638 639 640
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbUndoLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbRedoActions(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbUndoActions(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
641

S
Shengliang Guan 已提交
642 643 644
  code = 0;

DROP_STB_OVER:
S
Shengliang Guan 已提交
645
  mndTransDrop(pTrans);
S
Shengliang 已提交
646
  return code;
S
Shengliang Guan 已提交
647 648
}

S
Shengliang Guan 已提交
649 650 651
static int32_t mndProcessMDropStbReq(SMnodeMsg *pReq) {
  SMnode       *pMnode = pReq->pMnode;
  SMDropStbReq *pDrop = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

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

S
Shengliang Guan 已提交
667
  int32_t code = mndDropStb(pMnode, pReq, pStb);
S
Shengliang Guan 已提交
668 669 670 671 672 673 674 675 676
  mndReleaseStb(pMnode, pStb);

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

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}
S
Shengliang Guan 已提交
677

S
Shengliang Guan 已提交
678 679
static int32_t mndProcessVDropStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
680 681
  return 0;
}
S
Shengliang Guan 已提交
682

S
Shengliang Guan 已提交
683 684 685
static int32_t mndProcessStbMetaReq(SMnodeMsg *pReq) {
  SMnode        *pMnode = pReq->pMnode;
  STableInfoReq *pInfo = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
686

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

S
Shengliang Guan 已提交
689
  SDbObj *pDb = mndAcquireDbByStb(pMnode, pInfo->tableFname);
S
Shengliang Guan 已提交
690 691
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
692
    mError("stb:%s, failed to retrieve meta since %s", pInfo->tableFname, terrstr());
S
Shengliang Guan 已提交
693 694 695
    return -1;
  }

S
Shengliang Guan 已提交
696
  SStbObj *pStb = mndAcquireStb(pMnode, pInfo->tableFname);
S
Shengliang Guan 已提交
697 698
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
699
    terrno = TSDB_CODE_MND_INVALID_STB;
S
Shengliang Guan 已提交
700
    mError("stb:%s, failed to get meta since %s", pInfo->tableFname, terrstr());
S
Shengliang Guan 已提交
701 702 703
    return -1;
  }

S
Shengliang Guan 已提交
704 705
  taosRLockLatch(&pStb->lock);
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
S
Shengliang Guan 已提交
706
  int32_t contLen = sizeof(STableMetaRsp) + totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
707

S
Shengliang Guan 已提交
708
  STableMetaRsp *pMeta = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
709
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
710 711 712
    taosRUnLockLatch(&pStb->lock);
    mndReleaseDb(pMnode, pDb);
    mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
713
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
714
    mError("stb:%s, failed to get meta since %s", pInfo->tableFname, terrstr());
S
Shengliang Guan 已提交
715 716 717
    return -1;
  }

D
catalog  
dapan1121 已提交
718
  memcpy(pMeta->tbFname, pStb->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
719 720 721 722 723 724
  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 已提交
725 726
  pMeta->suid = htobe64(pStb->uid);
  pMeta->tuid = htobe64(pStb->uid);
S
Shengliang Guan 已提交
727

S
Shengliang Guan 已提交
728
  for (int32_t i = 0; i < totalCols; ++i) {
S
Shengliang Guan 已提交
729
    SSchema *pSchema = &pMeta->pSchema[i];
S
Shengliang Guan 已提交
730 731 732 733 734
    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 已提交
735
  }
S
Shengliang Guan 已提交
736 737 738
  taosRUnLockLatch(&pStb->lock);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
739

S
Shengliang Guan 已提交
740 741
  pReq->pCont = pMeta;
  pReq->contLen = contLen;
S
Shengliang Guan 已提交
742

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

S
Shengliang Guan 已提交
747
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
748 749 750 751 752 753 754 755
  SSdb *pSdb = pMnode->pSdb;

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

S
Shengliang Guan 已提交
756
  int32_t numOfStbs = 0;
757
  void   *pIter = NULL;
S
Shengliang Guan 已提交
758
  while (1) {
S
Shengliang Guan 已提交
759
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
760
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
761 762
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
763
    if (strcmp(pStb->db, dbName) == 0) {
S
Shengliang Guan 已提交
764
      numOfStbs++;
S
Shengliang Guan 已提交
765 766
    }

S
Shengliang Guan 已提交
767
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
768 769
  }

S
Shengliang Guan 已提交
770
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
771 772 773
  return 0;
}

S
Shengliang Guan 已提交
774 775
static int32_t mndGetStbMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
  SMnode *pMnode = pReq->pMnode;
776
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
777

S
Shengliang Guan 已提交
778
  if (mndGetNumOfStbs(pMnode, pShow->db, &pShow->numOfRows) != 0) {
S
Shengliang Guan 已提交
779 780 781 782
    return -1;
  }

  int32_t  cols = 0;
S
Shengliang Guan 已提交
783
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
784 785 786 787

  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 已提交
788
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
789 790 791 792
  cols++;

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

S
Shengliang Guan 已提交
797 798
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
799
  strcpy(pSchema[cols].name, "columns");
H
Haojun Liao 已提交
800
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
801 802
  cols++;

S
Shengliang Guan 已提交
803 804
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
805
  strcpy(pSchema[cols].name, "tags");
H
Haojun Liao 已提交
806
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
807 808
  cols++;

S
Shengliang Guan 已提交
809
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
810 811 812 813 814 815 816
  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 已提交
817
  pShow->numOfRows = sdbGetSize(pSdb, SDB_STB);
S
Shengliang Guan 已提交
818
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
819
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
820 821 822 823

  return 0;
}

S
Shengliang Guan 已提交
824
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
825 826
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
827 828 829 830 831 832 833 834 835 836
  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 已提交
837 838
static int32_t mndRetrieveStb(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode  *pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
839
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
840 841 842
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
843
  char    *pWrite;
S
Shengliang Guan 已提交
844
  char     prefix[TSDB_DB_FNAME_LEN] = {0};
S
Shengliang Guan 已提交
845

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

S
Shengliang Guan 已提交
849
  tstrncpy(prefix, pShow->db, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
850 851 852 853
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = (int32_t)strlen(prefix);

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

S
Shengliang Guan 已提交
857
    if (pStb->dbUid != pDb->uid) {
H
Haojun Liao 已提交
858 859 860 861
      if (strncmp(pStb->db, pDb->name, tListLen(pStb->db)) == 0) {
        mError("Inconsistent table data, name:%s, db:%s, dbUid:%"PRIu64, pStb->name, pDb->name, pDb->uid);
      }

S
Shengliang Guan 已提交
862
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
863 864 865 866 867
      continue;
    }

    cols = 0;

S
Shengliang Guan 已提交
868 869
    char stbName[TSDB_TABLE_NAME_LEN] = {0};
    tstrncpy(stbName, pStb->name + prefixLen, TSDB_TABLE_NAME_LEN);
S
Shengliang Guan 已提交
870
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
871
    STR_TO_VARSTR(pWrite, stbName);
S
Shengliang Guan 已提交
872 873 874
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
875
    *(int64_t *)pWrite = pStb->createdTime;
S
Shengliang Guan 已提交
876 877 878
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
879
    *(int32_t *)pWrite = pStb->numOfColumns;
S
Shengliang Guan 已提交
880 881 882
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
883
    *(int32_t *)pWrite = pStb->numOfTags;
S
Shengliang Guan 已提交
884 885 886
    cols++;

    numOfRows++;
S
Shengliang Guan 已提交
887
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
888 889
  }

S
Shengliang Guan 已提交
890
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
891
  pShow->numOfReads += numOfRows;
S
Shengliang Guan 已提交
892
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
893 894 895
  return numOfRows;
}

S
Shengliang Guan 已提交
896
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
897 898
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
L
Liu Jicong 已提交
899
}