mndStable.c 11.0 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 18 19 20 21 22 23
#include "mndStable.h"
#include "mndDnode.h"
#include "mndMnode.h"
#include "mndShow.h"
#include "mndTrans.h"
#include "mndUser.h"
#include "mndDb.h"
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#define TSDB_STABLE_VER_NUM 1
#define TSDB_STABLE_RESERVE_SIZE 64

static SSdbRaw *mndStableActionEncode(SStableObj *pStable);
static SSdbRow *mndStableActionDecode(SSdbRaw *pRaw);
static int32_t  mndStableActionInsert(SSdb *pSdb, SStableObj *pStable);
static int32_t  mndStableActionDelete(SSdb *pSdb, SStableObj *pStable);
static int32_t  mndStableActionUpdate(SSdb *pSdb, SStableObj *pOldStable, SStableObj *pNewStable);
static int32_t  mndProcessCreateStableMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessAlterStableMsg(SMnodeMsg *pMsg);
static int32_t  mndProcessDropStableMsg(SMnodeMsg *pMsg);
static int32_t  mndGetStableMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta);
static int32_t  mndRetrieveStables(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows);
static void     mndCancelGetNextStable(SMnode *pMnode, void *pIter);

int32_t mndInitStable(SMnode *pMnode) {
  SSdbTable table = {.sdbType = SDB_STABLE,
                     .keyType = SDB_KEY_BINARY,
                     .encodeFp = (SdbEncodeFp)mndStableActionEncode,
                     .decodeFp = (SdbDecodeFp)mndStableActionDecode,
                     .insertFp = (SdbInsertFp)mndStableActionInsert,
                     .updateFp = (SdbUpdateFp)mndStableActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndStableActionDelete};

  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_DB, mndProcessCreateStableMsg);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_ALTER_DB, mndProcessAlterStableMsg);
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_DROP_DB, mndProcessDropStableMsg);

  mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_DB, mndGetStableMeta);
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_DB, mndRetrieveStables);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_DB, mndCancelGetNextStable);
S
Shengliang Guan 已提交
56 57

  return sdbSetTable(pMnode->pSdb, table);
S
Shengliang Guan 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
}

void mndCleanupStable(SMnode *pMnode) {}

static SSdbRaw *mndStableActionEncode(SStableObj *pStable) {
  int32_t  size = sizeof(SStableObj) + (pStable->numOfFields + pStable->numOfTags) * sizeof(SSchema);
  SSdbRaw *pRaw = sdbAllocRaw(SDB_STABLE, TSDB_STABLE_VER_NUM, size);
  if (pRaw == NULL) return NULL;

  int32_t dataPos = 0;
  SDB_SET_BINARY(pRaw, dataPos, pStable->name, TSDB_TABLE_NAME_LEN)
  SDB_SET_INT64(pRaw, dataPos, pStable->createdTime)
  SDB_SET_INT64(pRaw, dataPos, pStable->updateTime)
  SDB_SET_INT64(pRaw, dataPos, pStable->uid)
  SDB_SET_INT64(pRaw, dataPos, pStable->version)
  SDB_SET_INT16(pRaw, dataPos, pStable->numOfFields)
  SDB_SET_INT16(pRaw, dataPos, pStable->numOfTags)

  for (int32_t i = 0; i < pStable->numOfFields; ++i) {
    SSchema *pSchema = &pStable->fieldSchema[i];
    SDB_SET_INT8(pRaw, dataPos, pSchema->type);
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId);
    SDB_SET_INT16(pRaw, dataPos, pSchema->bytes);
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN);
  }

  for (int32_t i = 0; i < pStable->numOfTags; ++i) {
    SSchema *pSchema = &pStable->tagSchema[i];
    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);
  }

  SDB_SET_RESERVE(pRaw, dataPos, TSDB_STABLE_RESERVE_SIZE)
  SDB_SET_DATALEN(pRaw, dataPos);

  return pRaw;
}

static SSdbRow *mndStableActionDecode(SSdbRaw *pRaw) {
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;

  if (sver != TSDB_STABLE_VER_NUM) {
    mError("failed to decode stable since %s", terrstr());
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    return NULL;
  }

  int32_t     size = sizeof(SStableObj) + TSDB_MAX_COLUMNS * sizeof(SSchema);
  SSdbRow    *pRow = sdbAllocRow(size);
  SStableObj *pStable = sdbGetRowObj(pRow);
  if (pStable == NULL) return NULL;

  int32_t dataPos = 0;
  SDB_GET_BINARY(pRaw, pRow, dataPos, pStable->name, TSDB_TABLE_NAME_LEN)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pStable->createdTime)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pStable->updateTime)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pStable->uid)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pStable->version)
  SDB_GET_INT16(pRaw, pRow, dataPos, &pStable->numOfFields)
  SDB_GET_INT16(pRaw, pRow, dataPos, &pStable->numOfTags)

  pStable->fieldSchema = (SSchema *)pStable->pCont;
  pStable->tagSchema = (SSchema *)(pStable->pCont + pStable->numOfFields * sizeof(SSchema));

  for (int32_t i = 0; i < pStable->numOfFields; ++i) {
    SSchema *pSchema = &pStable->fieldSchema[i];
    SDB_GET_INT8(pRaw, pRow, dataPos, &pSchema->type);
    SDB_GET_INT16(pRaw, pRow, dataPos, &pSchema->colId);
    SDB_GET_INT32(pRaw, pRow, dataPos, &pSchema->bytes);
    SDB_GET_BINARY(pRaw, pRow, dataPos, pSchema->name, TSDB_COL_NAME_LEN);
  }

  for (int32_t i = 0; i < pStable->numOfTags; ++i) {
    SSchema *pSchema = &pStable->tagSchema[i];
    SDB_GET_INT8(pRaw, pRow, dataPos, &pSchema->type);
    SDB_GET_INT16(pRaw, pRow, dataPos, &pSchema->colId);
    SDB_GET_INT32(pRaw, pRow, dataPos, &pSchema->bytes);
    SDB_GET_BINARY(pRaw, pRow, dataPos, pSchema->name, TSDB_COL_NAME_LEN);
  }

  SDB_GET_RESERVE(pRaw, pRow, dataPos, TSDB_STABLE_RESERVE_SIZE)

  return pRow;
}

static int32_t mndStableActionInsert(SSdb *pSdb, SStableObj *pStable) {
  mTrace("stable:%s, perform insert action", pStable->name);
  return 0;
}

static int32_t mndStableActionDelete(SSdb *pSdb, SStableObj *pStable) {
  mTrace("stable:%s, perform delete action", pStable->name);
  return 0;
}

static int32_t mndStableActionUpdate(SSdb *pSdb, SStableObj *pOldStable, SStableObj *pNewStable) {
  mTrace("stable:%s, perform update action", pOldStable->name);
  memcpy(pOldStable->name, pNewStable->name, TSDB_TABLE_NAME_LEN);
  pOldStable->createdTime = pNewStable->createdTime;
  pOldStable->updateTime = pNewStable->updateTime;
  pOldStable->uid = pNewStable->uid;
  pOldStable->version = pNewStable->version;
  pOldStable->numOfFields = pNewStable->numOfFields;
  pOldStable->numOfTags = pNewStable->numOfTags;
  pOldStable->createdTime = pNewStable->createdTime;

  memcpy(pOldStable->pCont, pNewStable, sizeof(SDbObj));
  // pStable->fieldSchema = pStable->pCont;
  // pStable->tagSchema = pStable->pCont + pStable->numOfFields * sizeof(SSchema);

  return 0;
}

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

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

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

static int32_t mndGetNumOfStables(SMnode *pMnode, char *dbName, int32_t *pNumOfStables) {
  SSdb *pSdb = pMnode->pSdb;

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

  int32_t numOfStables = 0;
  void   *pIter = NULL;
  while (1) {
    SStableObj *pStable = NULL;
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pStable);
    if (pIter == NULL) break;

    if (strcmp(pStable->db, dbName) == 0) {
      numOfStables++;
    }

    sdbRelease(pSdb, pStable);
  }

  *pNumOfStables = numOfStables;
  return 0;
}

static int32_t mndGetStableMeta(SMnodeMsg *pMsg, SShowObj *pShow, STableMetaMsg *pMeta) {
  SMnode *pMnode = pMsg->pMnode;
  SSdb   *pSdb = pMnode->pSdb;

  if (mndGetNumOfStables(pMnode, pShow->db, &pShow->numOfRows) != 0) {
    return -1;
  }

  int32_t  cols = 0;
  SSchema *pSchema = pMeta->schema;

  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];
  strcpy(pMeta->tableFname, mndShowStr(pShow->type));

  return 0;
}

static void mnodeExtractTableName(char* tableId, char* name) {
  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);
  }
}

static int32_t mndRetrieveStables(SMnodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
  SMnode     *pMnode = pMsg->pMnode;
  SSdb       *pSdb = pMnode->pSdb;
  int32_t     numOfRows = 0;
  SStableObj *pStable = NULL;
  int32_t     cols = 0;
  char       *pWrite;
  char        prefix[64] = {0};

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

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_STABLE, pShow->pIter, (void **)&pStable);
    if (pShow->pIter == NULL) break;

    if (strncmp(pStable->name, prefix, prefixLen) != 0) {
      sdbRelease(pSdb, pStable);
      continue;
    }

    cols = 0;

    char stableName[TSDB_TABLE_FNAME_LEN] = {0};
    memcpy(stableName, pStable->name + prefixLen, TSDB_TABLE_FNAME_LEN - prefixLen);
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, stableName);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pStable->createdTime;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = pStable->numOfFields;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int16_t *)pWrite = pStable->numOfTags;
    cols++;

    numOfRows++;
    sdbRelease(pSdb, pStable);
  }

  pShow->numOfReads += numOfRows;
  mnodeVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
  return numOfRows;
}

static void mndCancelGetNextStable(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}