mndStb.c 21.5 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"
S
Shengliang Guan 已提交
24
#include "tname.h"
S
Shengliang Guan 已提交
25

S
Shengliang Guan 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#define TSDB_STB_VER_NUM 1
#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 已提交
47
                     .keyType = SDB_KEY_BINARY,
S
Shengliang Guan 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
                     .encodeFp = (SdbEncodeFp)mndStbActionEncode,
                     .decodeFp = (SdbDecodeFp)mndStbActionDecode,
                     .insertFp = (SdbInsertFp)mndStbActionInsert,
                     .updateFp = (SdbUpdateFp)mndStbActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndStbActionDelete};

  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_STB, mndProcessCreateStbMsg);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_ALTER_STB, mndProcessAlterStbMsg);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_DROP_STB, mndProcessDropStbMsg);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_STB_IN_RSP, mndProcessCreateStbInRsp);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_ALTER_STB_IN_RSP, mndProcessAlterStbInRsp);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_DROP_STB_IN_RSP, mndProcessDropStbInRsp);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_TABLE_META, mndProcessStbMetaMsg);

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

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

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

S
Shengliang Guan 已提交
71
static SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
S
Shengliang Guan 已提交
72
  int32_t  size = sizeof(SStbObj) + (pStb->numOfColumns + pStb->numOfTags) * sizeof(SSchema) + TSDB_STB_RESERVE_SIZE;
S
Shengliang Guan 已提交
73
  SSdbRaw *pRaw = sdbAllocRaw(SDB_STB, TSDB_STB_VER_NUM, size);
S
Shengliang Guan 已提交
74 75 76
  if (pRaw == NULL) return NULL;

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

S
Shengliang Guan 已提交
87 88 89
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pStb->pSchema[i];
S
Shengliang Guan 已提交
90 91 92 93 94 95
    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 已提交
96
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_STB_RESERVE_SIZE)
S
Shengliang Guan 已提交
97 98 99 100 101
  SDB_SET_DATALEN(pRaw, dataPos);

  return pRaw;
}

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

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

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

  int32_t dataPos = 0;
S
Shengliang Guan 已提交
118 119
  SDB_GET_BINARY(pRaw, pRow, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN)
  SDB_GET_BINARY(pRaw, pRow, dataPos, pStb->db, TSDB_FULL_DB_NAME_LEN)
S
Shengliang Guan 已提交
120 121 122
  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 已提交
123
  SDB_GET_INT64(pRaw, pRow, dataPos, &pStb->dbUid)
S
Shengliang Guan 已提交
124 125 126 127
  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 已提交
128 129
  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  pStb->pSchema = calloc(totalCols, sizeof(SSchema));
S
Shengliang Guan 已提交
130

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

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

  return pRow;
}

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

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

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

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

S
Shengliang Guan 已提交
163 164
  if (pOldStb->numOfTags + pOldStb->numOfColumns < totalCols) {
    pOldStb->pSchema = malloc(totalSize);
S
Shengliang Guan 已提交
165 166
  }

S
Shengliang Guan 已提交
167
  memcpy(pOldStb->pSchema, pNewStb->pSchema, totalSize);
S
Shengliang Guan 已提交
168
  taosWUnLockLatch(&pOldStb->lock);
S
Shengliang Guan 已提交
169 170 171
  return 0;
}

S
Shengliang Guan 已提交
172
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
S
Shengliang Guan 已提交
173
  SSdb *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
174
  return sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
175 176
}

S
Shengliang Guan 已提交
177
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
178 179 180 181
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

S
Shengliang Guan 已提交
182 183 184
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 已提交
185

S
Shengliang Guan 已提交
186 187
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
188

S
Shengliang Guan 已提交
189 190
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
191

S
Shengliang Guan 已提交
192 193 194 195 196 197 198 199 200
static int32_t mndCheckStbMsg(SCreateStbMsg *pCreate) {
  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->colId = htonl(pSchema->colId);
    pSchema->bytes = htonl(pSchema->bytes);
  }
S
Shengliang Guan 已提交
201

S
Shengliang Guan 已提交
202 203 204 205
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
    terrno = TSDB_CODE_MND_STB_INVALID_IGEXIST;
    return -1;
  }
S
Shengliang Guan 已提交
206

S
Shengliang Guan 已提交
207 208 209 210
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_STB_INVALID_COLS_NUM;
    return -1;
  }
S
Shengliang Guan 已提交
211

S
Shengliang Guan 已提交
212 213 214 215
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
    terrno = TSDB_CODE_MND_STB_INVALID_TAGS_NUM;
    return -1;
  }
S
Shengliang Guan 已提交
216

S
Shengliang Guan 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  int32_t maxColId = (TSDB_MAX_COLUMNS + TSDB_MAX_TAGS);
  for (int32_t i = 0; i < totalCols; ++i) {
    SSchema *pSchema = &pCreate->pSchema[i];
    if (pSchema->type <= 0) {
      terrno = TSDB_CODE_MND_STB_INVALID_COL_TYPE;
      return -1;
    }
    if (pSchema->colId < 0 || pSchema->colId >= maxColId) {
      terrno = TSDB_CODE_MND_STB_INVALID_COL_ID;
      return -1;
    }
    if (pSchema->bytes <= 0) {
      terrno = TSDB_CODE_MND_STB_INVALID_COL_BYTES;
      return -1;
    }
    if (pSchema->name[0] == 0) {
      terrno = TSDB_CODE_MND_STB_INVALID_COL_NAME;
      return -1;
    }
  }
S
Shengliang Guan 已提交
237

S
Shengliang Guan 已提交
238 239 240 241 242 243 244 245 246
  return 0;
}

static int32_t mndCreateStb(SMnode *pMnode, SMnodeMsg *pMsg, SCreateStbMsg *pCreate, SDbObj *pDb) {
  SStbObj stbObj = {0};
  tstrncpy(stbObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  tstrncpy(stbObj.db, pDb->name, TSDB_FULL_DB_NAME_LEN);
  stbObj.createdTime = taosGetTimestampMs();
  stbObj.updateTime = stbObj.createdTime;
S
Shengliang Guan 已提交
247
  stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  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);

  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle);
  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);

  SSdbRaw *pRedoRaw = mndStbActionEncode(&stbObj);
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING);

  SSdbRaw *pUndoRaw = mndStbActionEncode(&stbObj);
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
    mError("trans:%d, failed to append undo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED);

  SSdbRaw *pCommitRaw = mndStbActionEncode(&stbObj);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);

S
Shengliang Guan 已提交
292
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
293 294 295 296 297 298 299
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
S
Shengliang Guan 已提交
300 301
}

S
Shengliang Guan 已提交
302 303 304
static int32_t mndProcessCreateStbMsg(SMnodeMsg *pMsg) {
  SMnode        *pMnode = pMsg->pMnode;
  SCreateStbMsg *pCreate = pMsg->rpcMsg.pCont;
S
Shengliang Guan 已提交
305

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

S
Shengliang Guan 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  if (mndCheckStbMsg(pCreate) != 0) {
    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;
    }
  }

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

static int32_t mndProcessCreateStbInRsp(SMnodeMsg *pMsg) { return 0; }

S
Shengliang Guan 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
static int32_t mndCheckAlterStbMsg(SAlterStbMsg *pAlter) {
  SSchema *pSchema = &pAlter->schema;
  pSchema->colId = htonl(pSchema->colId);
  pSchema->bytes = htonl(pSchema->bytes);

  if (pSchema->type <= 0) {
    terrno = TSDB_CODE_MND_STB_INVALID_COL_TYPE;
    return -1;
  }
  if (pSchema->colId < 0 || pSchema->colId >= (TSDB_MAX_COLUMNS + TSDB_MAX_TAGS)) {
    terrno = TSDB_CODE_MND_STB_INVALID_COL_ID;
    return -1;
  }
  if (pSchema->bytes <= 0) {
    terrno = TSDB_CODE_MND_STB_INVALID_COL_BYTES;
    return -1;
  }
  if (pSchema->name[0] == 0) {
    terrno = TSDB_CODE_MND_STB_INVALID_COL_NAME;
    return -1;
  }

  return 0;
}

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

static int32_t mndProcessAlterStbMsg(SMnodeMsg *pMsg) {
  SMnode       *pMnode = pMsg->pMnode;
  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 已提交
405 406 407

static int32_t mndProcessAlterStbInRsp(SMnodeMsg *pMsg) { return 0; }

S
Shengliang Guan 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
static int32_t mndDropStb(SMnode *pMnode, SMnodeMsg *pMsg, SStbObj *pStb) {
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle);
  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);

  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING);

  SSdbRaw *pUndoRaw = mndStbActionEncode(pStb);
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
    mError("trans:%d, failed to append undo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_READY);

  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);

S
Shengliang Guan 已提交
440
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}

static int32_t mndProcessDropStbMsg(SMnodeMsg *pMsg) {
  SMnode      *pMnode = pMsg->pMnode;
  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 已提交
479 480 481 482 483 484 485 486 487 488

static int32_t mndProcessDropStbInRsp(SMnodeMsg *pMsg) { return 0; }

static int32_t mndProcessStbMetaMsg(SMnodeMsg *pMsg) {
  SMnode      *pMnode = pMsg->pMnode;
  SStbInfoMsg *pInfo = pMsg->rpcMsg.pCont;

  mDebug("stb:%s, start to retrieve meta", pInfo->name);

  SDbObj *pDb = mndAcquireDbByStb(pMnode, pInfo->name);
S
Shengliang Guan 已提交
489 490
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
S
Shengliang Guan 已提交
491
    mError("stb:%s, failed to retrieve meta since %s", pInfo->name, terrstr());
S
Shengliang Guan 已提交
492 493 494
    return -1;
  }

S
Shengliang Guan 已提交
495
  SStbObj *pStb = mndAcquireStb(pMnode, pInfo->name);
S
Shengliang Guan 已提交
496 497 498
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
    terrno = TSDB_CODE_MND_INVALID_TABLE_NAME;
S
Shengliang Guan 已提交
499
    mError("stb:%s, failed to get meta since %s", pInfo->name, terrstr());
S
Shengliang Guan 已提交
500 501 502 503 504 505 506
    return -1;
  }

  int32_t        contLen = sizeof(STableMetaMsg) + (pStb->numOfColumns + pStb->numOfTags) * sizeof(SSchema);
  STableMetaMsg *pMeta = rpcMallocCont(contLen);
  if (pMeta == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
507
    mError("stb:%s, failed to get meta since %s", pInfo->name, terrstr());
S
Shengliang Guan 已提交
508 509 510
    return -1;
  }

S
Shengliang Guan 已提交
511
  memcpy(pMeta->stbFname, pStb->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
512 513 514 515 516 517 518 519 520 521
  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);

  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pMeta->pSchema[i];
S
Shengliang Guan 已提交
522 523 524 525 526
    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 已提交
527 528 529 530 531
  }

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

S
Shengliang Guan 已提交
532
  mDebug("stb:%s, meta is retrieved, cols:%d tags:%d", pInfo->name, pStb->numOfColumns, pStb->numOfTags);
S
Shengliang Guan 已提交
533 534
  return 0;
}
S
Shengliang Guan 已提交
535

S
Shengliang Guan 已提交
536
static int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
537 538 539 540 541 542 543 544
  SSdb *pSdb = pMnode->pSdb;

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

S
Shengliang Guan 已提交
545
  int32_t numOfStbs = 0;
S
Shengliang Guan 已提交
546 547
  void   *pIter = NULL;
  while (1) {
S
Shengliang Guan 已提交
548
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
549
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
550 551
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
552
    if (strcmp(pStb->db, dbName) == 0) {
S
Shengliang Guan 已提交
553
      numOfStbs++;
S
Shengliang Guan 已提交
554 555
    }

S
Shengliang Guan 已提交
556
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
557 558
  }

S
Shengliang Guan 已提交
559
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
560 561 562
  return 0;
}

S
Shengliang Guan 已提交
563
static int32_t mndGetStbMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) {
S
Shengliang Guan 已提交
564 565 566
  SMnode *pMnode = pMsg->pMnode;
  SSdb   *pSdb = pMnode->pSdb;

S
Shengliang Guan 已提交
567
  if (mndGetNumOfStbs(pMnode, pShow->db, &pShow->numOfRows) != 0) {
S
Shengliang Guan 已提交
568 569 570 571
    return -1;
  }

  int32_t  cols = 0;
S
Shengliang Guan 已提交
572
  SSchema *pSchema = pMeta->pSchema;
S
Shengliang Guan 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

  pShow->bytes[cols] = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
  strcpy(pSchema[cols].name, "create time");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "columns");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 2;
  pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
  strcpy(pSchema[cols].name, "tags");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pMeta->numOfColumns = htons(cols);
  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];
  }

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
607
  strcpy(pMeta->tbFname, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
608 609 610 611

  return 0;
}

S
Shengliang Guan 已提交
612
static void mndExtractTableName(char *tableId, char *name) {
S
Shengliang Guan 已提交
613 614 615 616 617 618 619 620 621 622 623 624
  int pos = -1;
  int num = 0;
  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 已提交
625 626 627 628 629 630 631 632
static int32_t mndRetrieveStb(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
  SMnode  *pMnode = pMsg->pMnode;
  SSdb    *pSdb = pMnode->pSdb;
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
  char    *pWrite;
  char     prefix[64] = {0};
S
Shengliang Guan 已提交
633 634 635 636 637 638

  tstrncpy(prefix, pShow->db, 64);
  strcat(prefix, TS_PATH_DELIMITER);
  int32_t prefixLen = (int32_t)strlen(prefix);

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

S
Shengliang Guan 已提交
642 643
    if (strncmp(pStb->name, prefix, prefixLen) != 0) {
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
644 645 646 647 648
      continue;
    }

    cols = 0;

S
Shengliang Guan 已提交
649 650
    char stbName[TSDB_TABLE_FNAME_LEN] = {0};
    memcpy(stbName, pStb->name + prefixLen, TSDB_TABLE_FNAME_LEN - prefixLen);
S
Shengliang Guan 已提交
651
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
652
    STR_TO_VARSTR(pWrite, stbName);
S
Shengliang Guan 已提交
653 654 655
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
656
    *(int64_t *)pWrite = pStb->createdTime;
S
Shengliang Guan 已提交
657 658 659
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
660
    *(int16_t *)pWrite = pStb->numOfColumns;
S
Shengliang Guan 已提交
661 662 663
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
S
Shengliang Guan 已提交
664
    *(int16_t *)pWrite = pStb->numOfTags;
S
Shengliang Guan 已提交
665 666 667
    cols++;

    numOfRows++;
S
Shengliang Guan 已提交
668
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
669 670 671
  }

  pShow->numOfReads += numOfRows;
S
Shengliang Guan 已提交
672
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
673 674 675
  return numOfRows;
}

S
Shengliang Guan 已提交
676
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
677 678 679
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}