mndStb.c 29.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
static int32_t mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew) {
  mTrace("stb:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
S
Shengliang Guan 已提交
184

S
Shengliang Guan 已提交
185 186
  taosWLockLatch(&pOld->lock);
  int32_t totalCols = pNew->numOfTags + pNew->numOfColumns;
S
Shengliang Guan 已提交
187
  int32_t totalSize = totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
188
  if (pOld->numOfTags + pOld->numOfColumns < totalCols) {
S
Shengliang Guan 已提交
189 190
    void *pSchema = malloc(totalSize);
    if (pSchema != NULL) {
S
Shengliang Guan 已提交
191 192
      free(pOld->pSchema);
      pOld->pSchema = pSchema;
S
Shengliang Guan 已提交
193 194 195 196
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
197
    }
S
Shengliang Guan 已提交
198 199
  }

S
Shengliang Guan 已提交
200 201 202 203
  pOld->updateTime = pNew->updateTime;
  pOld->version = pNew->version;
  pOld->numOfColumns = pNew->numOfColumns;
  pOld->numOfTags = pNew->numOfTags;
S
Shengliang Guan 已提交
204 205
  memcpy(pOld->pSchema, pNew->pSchema, totalSize);
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
206 207 208
  return 0;
}

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

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

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

S
Shengliang Guan 已提交
227 228
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
229

S
Shengliang Guan 已提交
230 231
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
232

S
Shengliang Guan 已提交
233
static void *mndBuildCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
234
  SName name = {0};
S
Shengliang Guan 已提交
235
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
236

S
Shengliang Guan 已提交
237 238
  SVCreateTbReq req = {0};
  req.ver = 0;
S
Shengliang Guan 已提交
239
  req.name = (char *)tNameGetTableName(&name);
H
more  
Hongze Cheng 已提交
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;

S
Shengliang Guan 已提交
249 250 251
  int32_t   contLen = tSerializeSVCreateTbReq(NULL, &req) + sizeof(SMsgHead);
  SMsgHead *pHead = malloc(contLen);
  if (pHead == NULL) {
H
more  
Hongze Cheng 已提交
252 253 254 255
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
256 257
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
258

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

S
Shengliang Guan 已提交
262 263
  *pContLen = contLen;
  return pHead;
264 265
}

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

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

S
Shengliang Guan 已提交
276 277 278
  int32_t   contLen = tSerializeSVDropTbReq(NULL, &req) + sizeof(SMsgHead);
  SMsgHead *pHead = malloc(contLen);
  if (pHead == NULL) {
279 280 281 282
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
283 284
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
285

S
Shengliang Guan 已提交
286 287
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
  tSerializeSVDropTbReq(&pBuf, &req);
288

S
Shengliang Guan 已提交
289 290
  *pContLen = contLen;
  return pHead;
291 292
}

S
Shengliang Guan 已提交
293
static int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
294 295 296 297 298 299 300
  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 已提交
301

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

S
Shengliang Guan 已提交
307
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
S
Shengliang Guan 已提交
308
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
309 310
    return -1;
  }
S
Shengliang Guan 已提交
311

S
Shengliang Guan 已提交
312
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
313
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
314 315
    return -1;
  }
S
Shengliang Guan 已提交
316

S
Shengliang Guan 已提交
317 318 319
  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 已提交
320 321
    if (pSchema->type < 0) {
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
322 323 324
      return -1;
    }
    if (pSchema->bytes <= 0) {
S
Shengliang Guan 已提交
325
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
326 327 328
      return -1;
    }
    if (pSchema->name[0] == 0) {
S
Shengliang Guan 已提交
329
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
330 331 332
      return -1;
    }
  }
S
Shengliang Guan 已提交
333

S
Shengliang Guan 已提交
334 335 336
  return 0;
}

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

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

355
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
356 357 358 359 360 361 362 363
  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;
}

364
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
365
  SSdb   *pSdb = pMnode->pSdb;
366
  SVgObj *pVgroup = NULL;
367
  void   *pIter = NULL;
S
Shengliang Guan 已提交
368
  int32_t contLen;
369 370 371 372 373 374

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

S
Shengliang Guan 已提交
375 376
    void *pReq = mndBuildCreateStbReq(pMnode, pVgroup, pStb, &contLen);
    if (pReq == NULL) {
377 378 379 380 381
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
382

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

  return 0;
}

400
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
401
  SSdb   *pSdb = pMnode->pSdb;
402
  SVgObj *pVgroup = NULL;
403
  void   *pIter = NULL;
404 405 406 407 408 409

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

S
Shengliang Guan 已提交
410 411
    int32_t contLen = 0;
    void   *pReq = mndBuildDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
412
    if (pReq == NULL) {
413 414 415 416 417
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
418

419 420
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
421
    action.pCont = pReq;
S
Shengliang Guan 已提交
422
    action.contLen = contLen;
H
Hongze Cheng 已提交
423
    action.msgType = TDMT_VND_DROP_STB;
424
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
S
Shengliang Guan 已提交
425
      free(pReq);
426 427 428 429 430 431
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
432 433 434 435

  return 0;
}

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

S
Shengliang 已提交
461
  int32_t code = -1;
S
Shengliang Guan 已提交
462 463
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, &pReq->rpcMsg);
  if (pTrans == NULL) goto CREATE_STB_OVER;
S
Shengliang Guan 已提交
464

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

S
Shengliang Guan 已提交
467 468 469 470 471 472
  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 已提交
473

S
Shengliang Guan 已提交
474 475 476
  code = 0;

CREATE_STB_OVER:
S
Shengliang Guan 已提交
477
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
478
  return code;
S
Shengliang Guan 已提交
479 480
}

S
Shengliang Guan 已提交
481 482 483
static int32_t mndProcessMCreateStbReq(SMnodeMsg *pReq) {
  SMnode         *pMnode = pReq->pMnode;
  SMCreateStbReq *pCreate = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
484

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

S
Shengliang Guan 已提交
487
  if (mndCheckCreateStbReq(pCreate) != 0) {
S
Shengliang Guan 已提交
488 489 490 491 492 493
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

  SStbObj *pStb = mndAcquireStb(pMnode, pCreate->name);
  if (pStb != NULL) {
S
Shengliang 已提交
494
    mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
495 496 497 498 499
    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 已提交
500
      mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
S
Shengliang Guan 已提交
501 502
      return -1;
    }
S
Shengliang Guan 已提交
503 504
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
S
Shengliang 已提交
505
    return -1;
S
Shengliang Guan 已提交
506 507
  }

H
more  
Hongze Cheng 已提交
508
  // topic should have different name with stb
S
Shengliang Guan 已提交
509 510 511
  SStbObj *pTopicStb = mndAcquireStb(pMnode, pCreate->name);
  if (pTopicStb != NULL) {
    mndReleaseStb(pMnode, pTopicStb);
L
Liu Jicong 已提交
512 513 514 515 516
    terrno = TSDB_CODE_MND_NAME_CONFLICT_WITH_TOPIC;
    mError("stb:%s, failed to create since %s", pCreate->name, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
517 518 519 520 521 522 523
  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 已提交
524
  int32_t code = mndCreateStb(pMnode, pReq, pCreate, pDb);
S
Shengliang Guan 已提交
525 526 527 528 529 530 531 532 533 534
  mndReleaseDb(pMnode, pDb);

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

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
535 536
static int32_t mndProcessVCreateStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
537 538
  return 0;
}
S
Shengliang Guan 已提交
539

S
Shengliang Guan 已提交
540
static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) {
S
Shengliang Guan 已提交
541 542 543 544 545
  SSchema *pSchema = &pAlter->schema;
  pSchema->colId = htonl(pSchema->colId);
  pSchema->bytes = htonl(pSchema->bytes);

  if (pSchema->type <= 0) {
S
Shengliang Guan 已提交
546
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
547 548 549
    return -1;
  }
  if (pSchema->colId < 0 || pSchema->colId >= (TSDB_MAX_COLUMNS + TSDB_MAX_TAGS)) {
S
Shengliang Guan 已提交
550
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
551 552 553
    return -1;
  }
  if (pSchema->bytes <= 0) {
S
Shengliang Guan 已提交
554
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
555 556 557
    return -1;
  }
  if (pSchema->name[0] == 0) {
S
Shengliang Guan 已提交
558
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
559 560 561 562 563 564
    return -1;
  }

  return 0;
}

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

S
Shengliang Guan 已提交
567 568 569
static int32_t mndProcessMAlterStbReq(SMnodeMsg *pReq) {
  SMnode        *pMnode = pReq->pMnode;
  SMAlterStbReq *pAlter = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
570 571 572

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

S
Shengliang Guan 已提交
573
  if (mndCheckAlterStbReq(pAlter) != 0) {
S
Shengliang Guan 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587
    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 已提交
588
  int32_t code = mndUpdateStb(pMnode, pReq, pStb, &stbObj);
S
Shengliang Guan 已提交
589 590 591 592 593 594 595 596 597
  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 已提交
598

S
Shengliang Guan 已提交
599 600
static int32_t mndProcessVAlterStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
601 602
  return 0;
}
S
Shengliang Guan 已提交
603

S
Shengliang Guan 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
static int32_t mndSetDropStbRedoLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING) != 0) return -1;

  return 0;
}

static int32_t mndSetDropStbCommitLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
622 623 624 625 626
static int32_t mndSetDropStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
  int32_t contLen;
S
Shengliang Guan 已提交
627

S
Shengliang Guan 已提交
628 629 630 631
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (pVgroup->dbUid != pDb->uid) continue;
S
Shengliang Guan 已提交
632

S
Shengliang Guan 已提交
633 634
    int32_t contLen = 0;
    void   *pReq = mndBuildDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
    if (pReq == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pReq;
    action.contLen = contLen;
    action.msgType = TDMT_VND_DROP_STB;
    action.acceptableCode = TSDB_CODE_VND_TB_NOT_EXIST;
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      free(pReq);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

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

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

S
Shengliang Guan 已提交
667 668
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
669
  if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
670
  if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_STB_OVER;
S
Shengliang Guan 已提交
671

S
Shengliang Guan 已提交
672 673 674
  code = 0;

DROP_STB_OVER:
S
Shengliang Guan 已提交
675
  mndTransDrop(pTrans);
S
Shengliang 已提交
676
  return code;
S
Shengliang Guan 已提交
677 678
}

S
Shengliang Guan 已提交
679 680 681
static int32_t mndProcessMDropStbReq(SMnodeMsg *pReq) {
  SMnode       *pMnode = pReq->pMnode;
  SMDropStbReq *pDrop = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

  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 已提交
697 698 699 700 701 702 703 704 705 706
  SDbObj *pDb = mndAcquireDbByStb(pMnode, pDrop->name);
  if (pDb == NULL) {
    mndReleaseStb(pMnode, pStb);
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    mError("stb:%s, failed to drop since %s", pDrop->name, terrstr());
    return -1;
  }

  int32_t code = mndDropStb(pMnode, pReq, pDb, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
707 708 709 710 711 712 713 714 715
  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 已提交
716

S
Shengliang Guan 已提交
717 718
static int32_t mndProcessVDropStbRsp(SMnodeMsg *pRsp) {
  mndTransProcessRsp(pRsp);
719 720
  return 0;
}
S
Shengliang Guan 已提交
721

S
Shengliang Guan 已提交
722 723 724
static int32_t mndProcessStbMetaReq(SMnodeMsg *pReq) {
  SMnode        *pMnode = pReq->pMnode;
  STableInfoReq *pInfo = pReq->rpcMsg.pCont;
S
Shengliang Guan 已提交
725

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

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

  SDbObj *pDb = mndAcquireDbByStb(pMnode, tbFName);
S
Shengliang Guan 已提交
732 733
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
D
dapan1121 已提交
734
    mError("stb:%s, failed to retrieve meta since %s", tbFName, terrstr());
S
Shengliang Guan 已提交
735 736 737
    return -1;
  }

D
dapan1121 已提交
738
  SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
S
Shengliang Guan 已提交
739 740
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
741
    terrno = TSDB_CODE_MND_INVALID_STB;
D
dapan1121 已提交
742
    mError("stb:%s, failed to get meta since %s", tbFName, terrstr());
S
Shengliang Guan 已提交
743 744 745
    return -1;
  }

S
Shengliang Guan 已提交
746 747
  taosRLockLatch(&pStb->lock);
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
S
Shengliang Guan 已提交
748
  int32_t contLen = sizeof(STableMetaRsp) + totalCols * sizeof(SSchema);
S
Shengliang Guan 已提交
749

S
Shengliang Guan 已提交
750
  STableMetaRsp *pMeta = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
751
  if (pMeta == NULL) {
S
Shengliang Guan 已提交
752 753 754
    taosRUnLockLatch(&pStb->lock);
    mndReleaseDb(pMnode, pDb);
    mndReleaseStb(pMnode, pStb);
S
Shengliang Guan 已提交
755
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
756
    mError("stb:%s, failed to get meta since %s", tbFName, terrstr());
S
Shengliang Guan 已提交
757 758 759
    return -1;
  }

D
dapan1121 已提交
760 761
  strcpy(pMeta->dbFName, pStb->db);
  strcpy(pMeta->tbName, pInfo->tbName);
S
Shengliang Guan 已提交
762 763 764 765 766 767
  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 已提交
768 769
  pMeta->suid = htobe64(pStb->uid);
  pMeta->tuid = htobe64(pStb->uid);
S
Shengliang Guan 已提交
770

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

S
Shengliang Guan 已提交
783 784
  pReq->pCont = pMeta;
  pReq->contLen = contLen;
S
Shengliang Guan 已提交
785

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

S
Shengliang Guan 已提交
790
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
791 792 793 794 795 796 797 798
  SSdb *pSdb = pMnode->pSdb;

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

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

S
Shengliang Guan 已提交
806
    if (strcmp(pStb->db, dbName) == 0) {
S
Shengliang Guan 已提交
807
      numOfStbs++;
S
Shengliang Guan 已提交
808 809
    }

S
Shengliang Guan 已提交
810
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
811 812
  }

S
Shengliang Guan 已提交
813
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
814 815 816
  return 0;
}

S
Shengliang Guan 已提交
817 818
static int32_t mndGetStbMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
  SMnode *pMnode = pReq->pMnode;
819
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
820

S
Shengliang Guan 已提交
821
  if (mndGetNumOfStbs(pMnode, pShow->db, &pShow->numOfRows) != 0) {
S
Shengliang Guan 已提交
822 823 824 825
    return -1;
  }

  int32_t  cols = 0;
S
Shengliang Guan 已提交
826
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
827 828 829 830

  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 已提交
831
  pSchema[cols].bytes = htonl(pShow->bytes[cols]);
S
Shengliang Guan 已提交
832 833 834 835
  cols++;

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

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

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

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

  return 0;
}

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

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

S
Shengliang Guan 已提交
892
  tstrncpy(prefix, pShow->db, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
893 894 895 896
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = (int32_t)strlen(prefix);

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

S
Shengliang Guan 已提交
900
    if (pStb->dbUid != pDb->uid) {
H
Haojun Liao 已提交
901
      if (strncmp(pStb->db, pDb->name, tListLen(pStb->db)) == 0) {
S
Shengliang Guan 已提交
902
        mError("Inconsistent table data, name:%s, db:%s, dbUid:%" PRIu64, pStb->name, pDb->name, pDb->uid);
H
Haojun Liao 已提交
903 904
      }

S
Shengliang Guan 已提交
905
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
906 907 908 909 910
      continue;
    }

    cols = 0;

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

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

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

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

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

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

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