mndStb.c 27.9 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 35 36 37 38 39 40 41 42 43 44 45 46 47
#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);
static int32_t  mndProcessCreateStbMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessAlterStbMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessDropStbMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessCreateStbInRsp(SMnodeMsg *pMsg);
static int32_t  mndProcessAlterStbInRsp(SMnodeMsg *pMsg);
static int32_t  mndProcessDropStbInRsp(SMnodeMsg *pMsg);
static int32_t  mndProcessStbMetaMsg(SMnodeMsg *pMsg);
static int32_t  mndGetStbMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta);
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};

H
Hongze Cheng 已提交
55 56 57 58 59 60
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcessCreateStbMsg);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcessAlterStbMsg);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcessDropStbMsg);
  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) {
S
Shengliang Guan 已提交
73
  int32_t  size = sizeof(SStbObj) + (pStb->numOfColumns + pStb->numOfTags) * sizeof(SSchema) + TSDB_STB_RESERVE_SIZE;
S
Shengliang Guan 已提交
74
  SSdbRaw *pRaw = sdbAllocRaw(SDB_STB, TSDB_STB_VER_NUMBER, size);
S
Shengliang Guan 已提交
75 76 77
  if (pRaw == NULL) return NULL;

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
78
  SDB_SET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN)
79
  SDB_SET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN)
S
Shengliang Guan 已提交
80 81 82
  SDB_SET_INT64(pRaw, dataPos, pStb->createdTime)
  SDB_SET_INT64(pRaw, dataPos, pStb->updateTime)
  SDB_SET_INT64(pRaw, dataPos, pStb->uid)
S
Shengliang Guan 已提交
83
  SDB_SET_INT64(pRaw, dataPos, pStb->dbUid)
S
Shengliang Guan 已提交
84
  SDB_SET_INT32(pRaw, dataPos, pStb->version)
S
Shengliang Guan 已提交
85 86 87
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags)

S
Shengliang Guan 已提交
88 89 90
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pStb->pSchema[i];
S
Shengliang Guan 已提交
91 92 93 94 95 96
    SDB_SET_INT8(pRaw, dataPos, pSchema->type);
    SDB_SET_INT32(pRaw, dataPos, pSchema->colId);
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes);
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN);
  }

S
Shengliang Guan 已提交
97
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE)
S
Shengliang Guan 已提交
98 99 100 101 102
  SDB_SET_DATALEN(pRaw, dataPos);

  return pRaw;
}

S
Shengliang Guan 已提交
103
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
104 105 106
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

S
Shengliang Guan 已提交
107
  if (sver != TSDB_STB_VER_NUMBER) {
S
Shengliang Guan 已提交
108
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
109
    mError("failed to decode stable since %s", terrstr());
S
Shengliang Guan 已提交
110 111 112
    return NULL;
  }

S
Shengliang Guan 已提交
113 114 115
  int32_t  size = sizeof(SStbObj) + TSDB_MAX_COLUMNS * sizeof(SSchema);
  SSdbRow *pRow = sdbAllocRow(size);
  SStbObj *pStb = sdbGetRowObj(pRow);
S
Shengliang Guan 已提交
116
  if (pStb == NULL) return NULL;
S
Shengliang Guan 已提交
117 118

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
119
  SDB_GET_BINARY(pRaw, pRow, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN)
120
  SDB_GET_BINARY(pRaw, pRow, dataPos, pStb->db, TSDB_DB_FNAME_LEN)
S
Shengliang Guan 已提交
121 122 123
  SDB_GET_INT64(pRaw, pRow, dataPos, &pStb->createdTime)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pStb->updateTime)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pStb->uid)
S
Shengliang Guan 已提交
124
  SDB_GET_INT64(pRaw, pRow, dataPos, &pStb->dbUid)
S
Shengliang Guan 已提交
125 126 127 128
  SDB_GET_INT32(pRaw, pRow, dataPos, &pStb->version)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pStb->numOfColumns)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pStb->numOfTags)

S
Shengliang Guan 已提交
129 130
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  pStb->pSchema = calloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
131

S
Shengliang Guan 已提交
132 133
  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pStb->pSchema[i];
S
Shengliang Guan 已提交
134
    SDB_GET_INT8(pRaw, pRow, dataPos, &pSchema->type);
S
Shengliang Guan 已提交
135
    SDB_GET_INT32(pRaw, pRow, dataPos, &pSchema->colId);
S
Shengliang Guan 已提交
136 137 138 139
    SDB_GET_INT32(pRaw, pRow, dataPos, &pSchema->bytes);
    SDB_GET_BINARY(pRaw, pRow, dataPos, pSchema->name, TSDB_COL_NAME_LEN);
  }

S
Shengliang Guan 已提交
140
  SDB_GET_RESERVE(pRaw, pRow, dataPos, TSDB_STB_RESERVE_SIZE)
S
Shengliang Guan 已提交
141 142 143 144

  return pRow;
}

S
Shengliang Guan 已提交
145 146
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
  mTrace("stb:%s, perform insert action", pStb->name);
S
Shengliang Guan 已提交
147 148 149
  return 0;
}

S
Shengliang Guan 已提交
150 151
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
  mTrace("stb:%s, perform delete action", pStb->name);
S
Shengliang Guan 已提交
152 153 154
  return 0;
}

S
Shengliang Guan 已提交
155 156
static int32_t mndStbActionUpdate(SSdb *pSdb, SStbObj *pOldStb, SStbObj *pNewStb) {
  mTrace("stb:%s, perform update action", pOldStb->name);
S
Shengliang Guan 已提交
157 158
  atomic_exchange_32(&pOldStb->updateTime, pNewStb->updateTime);
  atomic_exchange_32(&pOldStb->version, pNewStb->version);
S
Shengliang Guan 已提交
159

S
Shengliang Guan 已提交
160
  taosWLockLatch(&pOldStb->lock);
S
Shengliang Guan 已提交
161 162
  pOldStb->numOfColumns = pNewStb->numOfColumns;
  pOldStb->numOfTags = pNewStb->numOfTags;
S
Shengliang Guan 已提交
163 164
  int32_t totalCols = pNewStb->numOfTags + pNewStb->numOfColumns;
  int32_t totalSize = totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
165

S
Shengliang Guan 已提交
166
  if (pOldStb->numOfTags + pOldStb->numOfColumns < totalCols) {
S
Shengliang Guan 已提交
167 168 169 170 171
    void *pSchema = malloc(totalSize);
    if (pSchema != NULL) {
      free(pOldStb->pSchema);
      pOldStb->pSchema = pSchema;
    }
S
Shengliang Guan 已提交
172 173
  }

S
Shengliang Guan 已提交
174
  memcpy(pOldStb->pSchema, pNewStb->pSchema, totalSize);
S
Shengliang Guan 已提交
175
  taosWUnLockLatch(&pOldStb->lock);
S
Shengliang Guan 已提交
176 177 178
  return 0;
}

S
Shengliang Guan 已提交
179
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
H
more  
Hongze Cheng 已提交
180
  SSdb *   pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
181 182 183 184 185
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
186 187
}

S
Shengliang Guan 已提交
188
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
189 190 191 192
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

S
Shengliang Guan 已提交
193 194 195
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 已提交
196

S
Shengliang Guan 已提交
197 198
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
199

S
Shengliang Guan 已提交
200 201
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
202

H
more  
Hongze Cheng 已提交
203
static void *mndBuildCreateStbMsg(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int *pContLen) {
H
more  
Hongze Cheng 已提交
204 205 206
  SVCreateTbReq req;
  void *        buf;
  int           bsize;
H
more  
Hongze Cheng 已提交
207
  SMsgHead *    pMsgHead;
H
more  
Hongze Cheng 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220

  req.ver = 0;
  req.name = pStb->name;
  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 已提交
221
  buf = malloc(sizeof(SMsgHead) + bsize);
H
more  
Hongze Cheng 已提交
222 223 224 225 226
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

H
more  
Hongze Cheng 已提交
227 228 229 230 231 232
  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 已提交
233 234
  tSerializeSVCreateTbReq(&pBuf, &req);

H
more  
Hongze Cheng 已提交
235
  *pContLen = sizeof(SMsgHead) + bsize;
H
more  
Hongze Cheng 已提交
236
  return buf;
237 238
}

S
Shengliang Guan 已提交
239 240
static SVDropStbReq *mndBuildDropStbMsg(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb) {
  int32_t contLen = sizeof(SVDropStbReq);
241

S
Shengliang Guan 已提交
242
  SVDropStbReq *pDrop = calloc(1, contLen);
243 244 245 246 247 248 249 250 251 252 253 254 255 256
  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);
  pDrop->suid = htobe64(pStb->uid);

  return pDrop;
}

static int32_t mndCheckCreateStbMsg(SCreateStbMsg *pCreate) {
S
Shengliang Guan 已提交
257 258 259 260 261 262 263
  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 已提交
264

S
Shengliang Guan 已提交
265
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
266
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
267 268
    return -1;
  }
S
Shengliang Guan 已提交
269

S
Shengliang Guan 已提交
270
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
271
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
272 273
    return -1;
  }
S
Shengliang Guan 已提交
274

S
Shengliang Guan 已提交
275
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
276
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
277 278
    return -1;
  }
S
Shengliang Guan 已提交
279

S
Shengliang Guan 已提交
280 281 282
  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 已提交
283 284
    if (pSchema->type < 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
285 286 287
      return -1;
    }
    if (pSchema->bytes <= 0) {
S
Shengliang Guan 已提交
288
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
289 290 291
      return -1;
    }
    if (pSchema->name[0] == 0) {
S
Shengliang Guan 已提交
292
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
293 294 295
      return -1;
    }
  }
S
Shengliang Guan 已提交
296

S
Shengliang Guan 已提交
297 298 299
  return 0;
}

300
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
301 302 303 304 305 306 307 308
  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;
}

309
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
310 311 312 313 314 315 316 317
  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;
}

318
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
319 320 321 322 323 324 325 326
  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;
}

327
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
H
more  
Hongze Cheng 已提交
328
  SSdb *  pSdb = pMnode->pSdb;
329
  SVgObj *pVgroup = NULL;
H
more  
Hongze Cheng 已提交
330 331
  void *  pIter = NULL;
  int     contLen;
332 333 334 335 336 337

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

H
more  
Hongze Cheng 已提交
338
    void *pMsg = mndBuildCreateStbMsg(pMnode, pVgroup, pStb, &contLen);
339 340 341 342 343 344
    if (pMsg == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
345

346 347 348
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pMsg;
S
Shengliang Guan 已提交
349
    action.contLen = contLen;
H
Hongze Cheng 已提交
350
    action.msgType = TDMT_VND_CREATE_STB;
351 352 353 354 355 356 357 358
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      free(pMsg);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
359 360 361 362

  return 0;
}

363
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
H
more  
Hongze Cheng 已提交
364
  SSdb *  pSdb = pMnode->pSdb;
365
  SVgObj *pVgroup = NULL;
H
more  
Hongze Cheng 已提交
366
  void *  pIter = NULL;
367 368 369 370 371 372

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

S
Shengliang Guan 已提交
373
    SVDropStbReq *pMsg = mndBuildDropStbMsg(pMnode, pVgroup, pStb);
374 375 376 377 378 379
    if (pMsg == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
380

381 382 383
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pMsg;
S
Shengliang Guan 已提交
384
    action.contLen = sizeof(SVDropStbReq);
H
Hongze Cheng 已提交
385
    action.msgType = TDMT_VND_DROP_STB;
386 387 388 389 390 391 392 393
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
      free(pMsg);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
394 395 396 397

  return 0;
}

S
Shengliang Guan 已提交
398 399 400
static int32_t mndCreateStb(SMnode *pMnode, SMnodeMsg *pMsg, SCreateStbMsg *pCreate, SDbObj *pDb) {
  SStbObj stbObj = {0};
  tstrncpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
401
  tstrncpy(stbObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
402 403
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
404
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
405
  stbObj.dbUid = pDb->uid;
S
Shengliang Guan 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418
  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 已提交
419 420 421 422
  for (int32_t i = 0; i < totalCols; ++i) {
    stbObj.pSchema[i].colId = i + 1;
  }

S
Shengliang Guan 已提交
423
  int32_t code = 0;
S
Shengliang Guan 已提交
424
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pMsg->rpcMsg);
S
Shengliang Guan 已提交
425 426 427 428 429 430
  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);

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

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

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

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

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

S
Shengliang Guan 已提交
456
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
457 458 459 460 461
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

S
Shengliang Guan 已提交
462 463 464
  code = 0;

CREATE_STB_OVER:
S
Shengliang Guan 已提交
465
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
466
  return code;
S
Shengliang Guan 已提交
467 468
}

S
Shengliang Guan 已提交
469
static int32_t mndProcessCreateStbMsg(SMnodeMsg *pMsg) {
H
more  
Hongze Cheng 已提交
470
  SMnode *       pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
471
  SCreateStbMsg *pCreate = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
472

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

475
  if (mndCheckCreateStbMsg(pCreate) != 0) {
S
Shengliang Guan 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    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 已提交
493
  // topic should have different name with stb
L
Liu Jicong 已提交
494 495 496 497 498 499 500 501
  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 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
  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;
}

521
static int32_t mndProcessCreateStbInRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
522
  mndTransProcessRsp(pMsg);
523 524
  return 0;
}
S
Shengliang Guan 已提交
525

S
Shengliang Guan 已提交
526 527 528 529 530 531
static int32_t mndCheckAlterStbMsg(SAlterStbMsg *pAlter) {
  SSchema *pSchema = &pAlter->schema;
  pSchema->colId = htonl(pSchema->colId);
  pSchema->bytes = htonl(pSchema->bytes);

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

  return 0;
}

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

static int32_t mndProcessAlterStbMsg(SMnodeMsg *pMsg) {
H
more  
Hongze Cheng 已提交
554
  SMnode *      pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
  SAlterStbMsg *pAlter = pMsg->rpcMsg.pCont;

  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 已提交
584

585
static int32_t mndProcessAlterStbInRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
586
  mndTransProcessRsp(pMsg);
587 588
  return 0;
}
S
Shengliang Guan 已提交
589

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

S
Shengliang Guan 已提交
635 636 637
  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 已提交
638 639
  }

S
Shengliang Guan 已提交
640 641 642 643 644 645 646 647 648 649 650 651 652
  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 已提交
653 654
  }

S
Shengliang Guan 已提交
655
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
656
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
657
    goto DROP_STB_OVER;
S
Shengliang Guan 已提交
658 659
  }

S
Shengliang Guan 已提交
660 661 662
  code = 0;

DROP_STB_OVER:
S
Shengliang Guan 已提交
663 664 665 666 667
  mndTransDrop(pTrans);
  return 0;
}

static int32_t mndProcessDropStbMsg(SMnodeMsg *pMsg) {
H
more  
Hongze Cheng 已提交
668
  SMnode *     pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
  SDropStbMsg *pDrop = pMsg->rpcMsg.pCont;

  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 已提交
696

697
static int32_t mndProcessDropStbInRsp(SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
698
  mndTransProcessRsp(pMsg);
699 700
  return 0;
}
S
Shengliang Guan 已提交
701 702

static int32_t mndProcessStbMetaMsg(SMnodeMsg *pMsg) {
H
more  
Hongze Cheng 已提交
703
  SMnode *       pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
704
  STableInfoMsg *pInfo = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
705

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

S
Shengliang Guan 已提交
708
  SDbObj *pDb = mndAcquireDbByStb(pMnode, pInfo->tableFname);
S
Shengliang Guan 已提交
709 710
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
711
    mError("stb:%s, failed to retrieve meta since %s", pInfo->tableFname, terrstr());
S
Shengliang Guan 已提交
712 713 714
    return -1;
  }

S
Shengliang Guan 已提交
715
  SStbObj *pStb = mndAcquireStb(pMnode, pInfo->tableFname);
S
Shengliang Guan 已提交
716 717
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
718
    terrno = TSDB_CODE_MND_INVALID_STB;
S
Shengliang Guan 已提交
719
    mError("stb:%s, failed to get meta since %s", pInfo->tableFname, terrstr());
S
Shengliang Guan 已提交
720 721 722
    return -1;
  }

S
Shengliang Guan 已提交
723 724 725 726
  taosRLockLatch(&pStb->lock);
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  int32_t contLen = sizeof(STableMetaMsg) + totalCols * sizeof(SSchema);

S
Shengliang Guan 已提交
727 728
  STableMetaMsg *pMeta = rpcMallocCont(contLen);
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
729 730 731
    taosRUnLockLatch(&pStb->lock);
    mndReleaseDb(pMnode, pDb);
    mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
732
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
733
    mError("stb:%s, failed to get meta since %s", pInfo->tableFname, terrstr());
S
Shengliang Guan 已提交
734 735 736
    return -1;
  }

D
catalog  
dapan1121 已提交
737
  memcpy(pMeta->tbFname, pStb->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
738 739 740 741 742 743 744 745
  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);
  pMeta->suid = htonl(pStb->uid);

S
Shengliang Guan 已提交
746
  for (int32_t i = 0; i < totalCols; ++i) {
S
Shengliang Guan 已提交
747
    SSchema *pSchema = &pMeta->pSchema[i];
S
Shengliang Guan 已提交
748 749 750 751 752
    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 已提交
753
  }
S
Shengliang Guan 已提交
754 755 756
  taosRUnLockLatch(&pStb->lock);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
757 758 759 760

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

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

S
Shengliang Guan 已提交
765
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
766 767 768 769 770 771 772 773
  SSdb *pSdb = pMnode->pSdb;

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

S
Shengliang Guan 已提交
774
  int32_t numOfStbs = 0;
H
more  
Hongze Cheng 已提交
775
  void *  pIter = NULL;
S
Shengliang Guan 已提交
776
  while (1) {
S
Shengliang Guan 已提交
777
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
778
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
779 780
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
781
    if (strcmp(pStb->db, dbName) == 0) {
S
Shengliang Guan 已提交
782
      numOfStbs++;
S
Shengliang Guan 已提交
783 784
    }

S
Shengliang Guan 已提交
785
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
786 787
  }

S
Shengliang Guan 已提交
788
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
789 790 791
  return 0;
}

S
Shengliang Guan 已提交
792
static int32_t mndGetStbMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) {
S
Shengliang Guan 已提交
793
  SMnode *pMnode = pMsg->pMnode;
H
more  
Hongze Cheng 已提交
794
  SSdb *  pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
795

S
Shengliang Guan 已提交
796
  if (mndGetNumOfStbs(pMnode, pShow->db, &pShow->numOfRows) != 0) {
S
Shengliang Guan 已提交
797 798 799 800
    return -1;
  }

  int32_t  cols = 0;
S
Shengliang Guan 已提交
801
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
802 803 804 805

  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 已提交
806
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
807 808 809 810
  cols++;

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

S
Shengliang Guan 已提交
815 816
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
817
  strcpy(pSchema[cols].name, "columns");
H
Haojun Liao 已提交
818
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
819 820
  cols++;

S
Shengliang Guan 已提交
821 822
  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
S
Shengliang Guan 已提交
823
  strcpy(pSchema[cols].name, "tags");
H
Haojun Liao 已提交
824
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
825 826
  cols++;

S
Shengliang Guan 已提交
827
  pMeta->numOfColumns = htonl(cols);
S
Shengliang Guan 已提交
828 829 830 831 832 833 834
  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 已提交
835
  pShow->numOfRows = sdbGetSize(pSdb, SDB_STB);
S
Shengliang Guan 已提交
836
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
837
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
838 839 840 841

  return 0;
}

S
Shengliang Guan 已提交
842
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
843 844
  int32_t pos = -1;
  int32_t num = 0;
S
Shengliang Guan 已提交
845 846 847 848 849 850 851 852 853 854
  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 已提交
855
static int32_t mndRetrieveStb(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
S
Shengliang Guan 已提交
856 857
  SMnode  *pMnode = pMsg->pMnode;
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
858 859 860
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
861
  char    *pWrite;
S
Shengliang Guan 已提交
862
  char     prefix[64] = {0};
S
Shengliang Guan 已提交
863

S
Shengliang Guan 已提交
864 865 866 867 868
  SDbObj *pDb = mndAcquireDb(pMnode, pShow->db);
  if (pDb == NULL) {
    return TSDB_CODE_MND_INVALID_DB;
  }

S
Shengliang Guan 已提交
869 870 871 872 873
  tstrncpy(prefix, pShow->db, 64);
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = (int32_t)strlen(prefix);

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

S
Shengliang Guan 已提交
877
    if (pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
878
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
879 880 881 882 883
      continue;
    }

    cols = 0;

S
Shengliang Guan 已提交
884 885
    char stbName[TSDB_TABLE_NAME_LEN] = {0};
    tstrncpy(stbName, pStb->name + prefixLen, TSDB_TABLE_NAME_LEN);
S
Shengliang Guan 已提交
886
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
887
    STR_TO_VARSTR(pWrite, stbName);
S
Shengliang Guan 已提交
888 889 890
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
891
    *(int64_t *)pWrite = pStb->createdTime;
S
Shengliang Guan 已提交
892 893 894
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
895
    *(int32_t *)pWrite = pStb->numOfColumns;
S
Shengliang Guan 已提交
896 897 898
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
899
    *(int32_t *)pWrite = pStb->numOfTags;
S
Shengliang Guan 已提交
900 901 902
    cols++;

    numOfRows++;
S
Shengliang Guan 已提交
903
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
904 905
  }

S
Shengliang Guan 已提交
906
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
907
  pShow->numOfReads += numOfRows;
S
Shengliang Guan 已提交
908
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
909 910 911
  return numOfRows;
}

S
Shengliang Guan 已提交
912
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
913 914
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
L
Liu Jicong 已提交
915
}