mndSma.c 41.1 KB
Newer Older
S
sma  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "mndSma.h"
#include "mndDb.h"
#include "mndDnode.h"
dengyihao's avatar
dengyihao 已提交
20
#include "mndIndex.h"
dengyihao's avatar
dengyihao 已提交
21
#include "mndIndexComm.h"
S
sma  
Shengliang Guan 已提交
22 23
#include "mndInfoSchema.h"
#include "mndMnode.h"
L
Liu Jicong 已提交
24
#include "mndPrivilege.h"
L
Liu Jicong 已提交
25
#include "mndScheduler.h"
S
sma  
Shengliang Guan 已提交
26
#include "mndShow.h"
S
Shengliang Guan 已提交
27
#include "mndStb.h"
S
sma  
Shengliang Guan 已提交
28
#include "mndStream.h"
S
sma  
Shengliang Guan 已提交
29 30 31
#include "mndTrans.h"
#include "mndUser.h"
#include "mndVgroup.h"
C
Cary Xu 已提交
32
#include "parser.h"
S
sma  
Shengliang Guan 已提交
33 34 35 36 37 38 39 40 41 42
#include "tname.h"

#define TSDB_SMA_VER_NUMBER   1
#define TSDB_SMA_RESERVE_SIZE 64

static SSdbRaw *mndSmaActionEncode(SSmaObj *pSma);
static SSdbRow *mndSmaActionDecode(SSdbRaw *pRaw);
static int32_t  mndSmaActionInsert(SSdb *pSdb, SSmaObj *pSma);
static int32_t  mndSmaActionDelete(SSdb *pSdb, SSmaObj *pSpSmatb);
static int32_t  mndSmaActionUpdate(SSdb *pSdb, SSmaObj *pOld, SSmaObj *pNew);
43 44
static int32_t  mndProcessCreateSmaReq(SRpcMsg *pReq);
static int32_t  mndProcessDropSmaReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
45
static int32_t  mndProcessGetSmaReq(SRpcMsg *pReq);
46
static int32_t  mndProcessGetTbSmaReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
47
static int32_t  mndRetrieveSma(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
C
Cary Xu 已提交
48
static void     mndDestroySmaObj(SSmaObj *pSmaObj);
S
sma  
Shengliang Guan 已提交
49

dengyihao's avatar
dengyihao 已提交
50 51 52 53
// sma and tag index comm func
static int32_t mndProcessDropIdxReq(SRpcMsg *pReq);
static int32_t mndRetrieveIdx(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
static void    mndCancelRetrieveIdx(SMnode *pMnode, void *pIter);
dengyihao's avatar
dengyihao 已提交
54

S
sma  
Shengliang Guan 已提交
55
int32_t mndInitSma(SMnode *pMnode) {
56 57 58 59 60 61 62 63 64
  SSdbTable table = {
      .sdbType = SDB_SMA,
      .keyType = SDB_KEY_BINARY,
      .encodeFp = (SdbEncodeFp)mndSmaActionEncode,
      .decodeFp = (SdbDecodeFp)mndSmaActionDecode,
      .insertFp = (SdbInsertFp)mndSmaActionInsert,
      .updateFp = (SdbUpdateFp)mndSmaActionUpdate,
      .deleteFp = (SdbDeleteFp)mndSmaActionDelete,
  };
S
sma  
Shengliang Guan 已提交
65

66
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_SMA, mndProcessCreateSmaReq);
dengyihao's avatar
dengyihao 已提交
67
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_SMA, mndProcessDropIdxReq);
68 69
  mndSetMsgHandle(pMnode, TDMT_VND_CREATE_SMA_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_SMA_RSP, mndTransProcessRsp);
S
Shengliang Guan 已提交
70
  mndSetMsgHandle(pMnode, TDMT_MND_GET_INDEX, mndProcessGetSmaReq);
D
dapan1121 已提交
71
  mndSetMsgHandle(pMnode, TDMT_MND_GET_TABLE_INDEX, mndProcessGetTbSmaReq);
S
sma  
Shengliang Guan 已提交
72

dengyihao's avatar
dengyihao 已提交
73
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_INDEX, mndRetrieveIdx);
dengyihao's avatar
dengyihao 已提交
74
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_INDEX, mndCancelRetrieveIdx);
S
sma  
Shengliang Guan 已提交
75 76 77 78 79 80 81 82
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupSma(SMnode *pMnode) {}

static SSdbRaw *mndSmaActionEncode(SSmaObj *pSma) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;

L
Liu Jicong 已提交
83 84
  int32_t size =
      sizeof(SSmaObj) + pSma->exprLen + pSma->tagsFilterLen + pSma->sqlLen + pSma->astLen + TSDB_SMA_RESERVE_SIZE;
S
sma  
Shengliang Guan 已提交
85 86 87 88 89 90 91
  SSdbRaw *pRaw = sdbAllocRaw(SDB_SMA, TSDB_SMA_VER_NUMBER, size);
  if (pRaw == NULL) goto _OVER;

  int32_t dataPos = 0;
  SDB_SET_BINARY(pRaw, dataPos, pSma->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pSma->stb, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pSma->db, TSDB_DB_FNAME_LEN, _OVER)
C
Cary Xu 已提交
92
  SDB_SET_BINARY(pRaw, dataPos, pSma->dstTbName, TSDB_DB_FNAME_LEN, _OVER)
S
sma  
Shengliang Guan 已提交
93 94 95 96
  SDB_SET_INT64(pRaw, dataPos, pSma->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pSma->uid, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pSma->stbUid, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pSma->dbUid, _OVER)
C
Cary Xu 已提交
97
  SDB_SET_INT64(pRaw, dataPos, pSma->dstTbUid, _OVER)
S
sma  
Shengliang Guan 已提交
98 99 100
  SDB_SET_INT8(pRaw, dataPos, pSma->intervalUnit, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pSma->slidingUnit, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pSma->timezone, _OVER)
S
sma  
Shengliang Guan 已提交
101
  SDB_SET_INT32(pRaw, dataPos, pSma->dstVgId, _OVER)
S
sma  
Shengliang Guan 已提交
102 103 104 105 106
  SDB_SET_INT64(pRaw, dataPos, pSma->interval, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pSma->offset, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pSma->sliding, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pSma->exprLen, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pSma->tagsFilterLen, _OVER)
S
sma  
Shengliang Guan 已提交
107 108
  SDB_SET_INT32(pRaw, dataPos, pSma->sqlLen, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pSma->astLen, _OVER)
109

S
sma  
Shengliang Guan 已提交
110 111 112 113 114 115 116 117 118 119 120 121
  if (pSma->exprLen > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pSma->expr, pSma->exprLen, _OVER)
  }
  if (pSma->tagsFilterLen > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pSma->tagsFilter, pSma->tagsFilterLen, _OVER)
  }
  if (pSma->sqlLen > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pSma->sql, pSma->sqlLen, _OVER)
  }
  if (pSma->astLen > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pSma->ast, pSma->astLen, _OVER)
  }
S
sma  
Shengliang Guan 已提交
122 123 124

  SDB_SET_RESERVE(pRaw, dataPos, TSDB_SMA_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
125

S
sma  
Shengliang Guan 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  terrno = 0;

_OVER:
  if (terrno != 0) {
    mError("sma:%s, failed to encode to raw:%p since %s", pSma->name, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }

  mTrace("sma:%s, encode to raw:%p, row:%p", pSma->name, pRaw, pSma);
  return pRaw;
}

static SSdbRow *mndSmaActionDecode(SSdbRaw *pRaw) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;
141 142
  SSdbRow *pRow = NULL;
  SSmaObj *pSma = NULL;
S
sma  
Shengliang Guan 已提交
143 144 145 146 147 148 149 150 151

  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;

  if (sver != TSDB_SMA_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    goto _OVER;
  }

152
  pRow = sdbAllocRow(sizeof(SSmaObj));
S
sma  
Shengliang Guan 已提交
153 154
  if (pRow == NULL) goto _OVER;

155
  pSma = sdbGetRowObj(pRow);
S
sma  
Shengliang Guan 已提交
156 157 158 159 160 161 162
  if (pSma == NULL) goto _OVER;

  int32_t dataPos = 0;

  SDB_GET_BINARY(pRaw, dataPos, pSma->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pSma->stb, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pSma->db, TSDB_DB_FNAME_LEN, _OVER)
C
Cary Xu 已提交
163
  SDB_GET_BINARY(pRaw, dataPos, pSma->dstTbName, TSDB_DB_FNAME_LEN, _OVER)
S
sma  
Shengliang Guan 已提交
164 165 166 167
  SDB_GET_INT64(pRaw, dataPos, &pSma->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pSma->uid, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pSma->stbUid, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pSma->dbUid, _OVER)
C
Cary Xu 已提交
168
  SDB_GET_INT64(pRaw, dataPos, &pSma->dstTbUid, _OVER)
S
sma  
Shengliang Guan 已提交
169 170 171
  SDB_GET_INT8(pRaw, dataPos, &pSma->intervalUnit, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pSma->slidingUnit, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pSma->timezone, _OVER)
S
sma  
Shengliang Guan 已提交
172
  SDB_GET_INT32(pRaw, dataPos, &pSma->dstVgId, _OVER)
S
sma  
Shengliang Guan 已提交
173 174 175 176 177
  SDB_GET_INT64(pRaw, dataPos, &pSma->interval, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pSma->offset, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pSma->sliding, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pSma->exprLen, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pSma->tagsFilterLen, _OVER)
S
sma  
Shengliang Guan 已提交
178 179
  SDB_GET_INT32(pRaw, dataPos, &pSma->sqlLen, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pSma->astLen, _OVER)
S
sma  
Shengliang Guan 已提交
180

S
sma  
Shengliang Guan 已提交
181
  if (pSma->exprLen > 0) {
wafwerar's avatar
wafwerar 已提交
182
    pSma->expr = taosMemoryCalloc(pSma->exprLen, 1);
S
sma  
Shengliang Guan 已提交
183 184
    if (pSma->expr == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pSma->expr, pSma->exprLen, _OVER)
S
sma  
Shengliang Guan 已提交
185 186
  }

S
sma  
Shengliang Guan 已提交
187
  if (pSma->tagsFilterLen > 0) {
wafwerar's avatar
wafwerar 已提交
188
    pSma->tagsFilter = taosMemoryCalloc(pSma->tagsFilterLen, 1);
S
sma  
Shengliang Guan 已提交
189 190 191
    if (pSma->tagsFilter == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pSma->tagsFilter, pSma->tagsFilterLen, _OVER)
  }
S
sma  
Shengliang Guan 已提交
192

S
sma  
Shengliang Guan 已提交
193
  if (pSma->sqlLen > 0) {
wafwerar's avatar
wafwerar 已提交
194
    pSma->sql = taosMemoryCalloc(pSma->sqlLen, 1);
S
sma  
Shengliang Guan 已提交
195 196 197 198 199
    if (pSma->sql == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pSma->sql, pSma->sqlLen, _OVER)
  }

  if (pSma->astLen > 0) {
wafwerar's avatar
wafwerar 已提交
200
    pSma->ast = taosMemoryCalloc(pSma->astLen, 1);
S
sma  
Shengliang Guan 已提交
201 202 203 204 205
    if (pSma->ast == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pSma->ast, pSma->astLen, _OVER)
  }

  SDB_GET_RESERVE(pRaw, dataPos, TSDB_SMA_RESERVE_SIZE, _OVER)
206

S
sma  
Shengliang Guan 已提交
207 208 209 210
  terrno = 0;

_OVER:
  if (terrno != 0) {
S
Shengliang Guan 已提交
211 212 213 214 215 216 217
    if (pSma != NULL) {
      mError("sma:%s, failed to decode from raw:%p since %s", pSma->name, pRaw, terrstr());
      taosMemoryFreeClear(pSma->expr);
      taosMemoryFreeClear(pSma->tagsFilter);
      taosMemoryFreeClear(pSma->sql);
      taosMemoryFreeClear(pSma->ast);
    }
wafwerar's avatar
wafwerar 已提交
218
    taosMemoryFreeClear(pRow);
S
sma  
Shengliang Guan 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232
    return NULL;
  }

  mTrace("sma:%s, decode from raw:%p, row:%p", pSma->name, pRaw, pSma);
  return pRow;
}

static int32_t mndSmaActionInsert(SSdb *pSdb, SSmaObj *pSma) {
  mTrace("sma:%s, perform insert action, row:%p", pSma->name, pSma);
  return 0;
}

static int32_t mndSmaActionDelete(SSdb *pSdb, SSmaObj *pSma) {
  mTrace("sma:%s, perform delete action, row:%p", pSma->name, pSma);
wafwerar's avatar
wafwerar 已提交
233 234
  taosMemoryFreeClear(pSma->tagsFilter);
  taosMemoryFreeClear(pSma->expr);
235 236
  taosMemoryFreeClear(pSma->sql);
  taosMemoryFreeClear(pSma->ast);
S
sma  
Shengliang Guan 已提交
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
  return 0;
}

static int32_t mndSmaActionUpdate(SSdb *pSdb, SSmaObj *pOld, SSmaObj *pNew) {
  mTrace("sma:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
  return 0;
}

SSmaObj *mndAcquireSma(SMnode *pMnode, char *smaName) {
  SSdb    *pSdb = pMnode->pSdb;
  SSmaObj *pSma = sdbAcquire(pSdb, SDB_SMA, smaName);
  if (pSma == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
    terrno = TSDB_CODE_MND_SMA_NOT_EXIST;
  }
  return pSma;
}

void mndReleaseSma(SMnode *pMnode, SSmaObj *pSma) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pSma);
}

SDbObj *mndAcquireDbBySma(SMnode *pMnode, const char *smaName) {
  SName name = {0};
  tNameFromString(&name, smaName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);

  return mndAcquireDb(pMnode, db);
}

static void *mndBuildVCreateSmaReq(SMnode *pMnode, SVgObj *pVgroup, SSmaObj *pSma, int32_t *pContLen) {
270 271 272
  SEncoder encoder = {0};
  int32_t  contLen = 0;
  SName    name = {0};
S
sma  
Shengliang Guan 已提交
273 274 275
  tNameFromString(&name, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  SVCreateTSmaReq req = {0};
276 277 278 279 280 281 282 283 284
  req.version = 0;
  req.intervalUnit = pSma->intervalUnit;
  req.slidingUnit = pSma->slidingUnit;
  req.timezoneInt = pSma->timezone;
  tstrncpy(req.indexName, (char *)tNameGetTableName(&name), TSDB_INDEX_NAME_LEN);
  req.exprLen = pSma->exprLen;
  req.tagsFilterLen = pSma->tagsFilterLen;
  req.indexUid = pSma->uid;
  req.tableUid = pSma->stbUid;
C
Cary Xu 已提交
285 286
  req.dstVgId = pSma->dstVgId;
  req.dstTbUid = pSma->dstTbUid;
287 288 289 290 291
  req.interval = pSma->interval;
  req.offset = pSma->offset;
  req.sliding = pSma->sliding;
  req.expr = pSma->expr;
  req.tagsFilter = pSma->tagsFilter;
C
Cary Xu 已提交
292 293 294 295
  req.schemaRow = pSma->schemaRow;
  req.schemaTag = pSma->schemaTag;
  req.dstTbName = pSma->dstTbName;

296 297 298 299 300 301 302 303
  // get length
  int32_t ret = 0;
  tEncodeSize(tEncodeSVCreateTSmaReq, &req, contLen, ret);
  if (ret < 0) {
    return NULL;
  }
  contLen += sizeof(SMsgHead);

wafwerar's avatar
wafwerar 已提交
304
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
sma  
Shengliang Guan 已提交
305 306 307 308 309 310 311 312 313
  if (pHead == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
314 315 316 317 318 319 320 321
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  if (tEncodeSVCreateTSmaReq(&encoder, &req) < 0) {
    taosMemoryFreeClear(pHead);
    tEncoderClear(&encoder);
    return NULL;
  }

  tEncoderClear(&encoder);
S
sma  
Shengliang Guan 已提交
322 323 324 325 326 327

  *pContLen = contLen;
  return pHead;
}

static void *mndBuildVDropSmaReq(SMnode *pMnode, SVgObj *pVgroup, SSmaObj *pSma, int32_t *pContLen) {
L
Liu Jicong 已提交
328 329 330
  SEncoder encoder = {0};
  int32_t  contLen;
  SName    name = {0};
S
sma  
Shengliang Guan 已提交
331 332 333 334 335 336
  tNameFromString(&name, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  SVDropTSmaReq req = {0};
  req.indexUid = pSma->uid;
  tstrncpy(req.indexName, (char *)tNameGetTableName(&name), TSDB_INDEX_NAME_LEN);

337 338 339 340 341 342 343 344 345
  // get length
  int32_t ret = 0;
  tEncodeSize(tEncodeSVDropTSmaReq, &req, contLen, ret);
  if (ret < 0) {
    return NULL;
  }

  contLen += sizeof(SMsgHead);

wafwerar's avatar
wafwerar 已提交
346
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
sma  
Shengliang Guan 已提交
347 348 349 350 351 352 353 354 355
  if (pHead == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
356 357 358 359 360 361 362 363
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));

  if (tEncodeSVDropTSmaReq(&encoder, &req) < 0) {
    taosMemoryFreeClear(pHead);
    tEncoderClear(&encoder);
    return NULL;
  }
  tEncoderClear(&encoder);
S
sma  
Shengliang Guan 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

  *pContLen = contLen;
  return pHead;
}

static int32_t mndSetCreateSmaRedoLogs(SMnode *pMnode, STrans *pTrans, SSmaObj *pSma) {
  SSdbRaw *pRedoRaw = mndSmaActionEncode(pSma);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) return -1;

  return 0;
}

static int32_t mndSetCreateSmaCommitLogs(SMnode *pMnode, STrans *pTrans, SSmaObj *pSma) {
  SSdbRaw *pCommitRaw = mndSmaActionEncode(pSma);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
387 388 389 390
static int32_t mndSetCreateSmaVgroupRedoLogs(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup) {
  SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup);
  if (pVgRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) return -1;
S
Shengliang Guan 已提交
391
  if (sdbSetRawStatus(pVgRaw, SDB_STATUS_CREATING) != 0) return -1;
S
Shengliang Guan 已提交
392 393 394 395 396 397 398 399 400 401 402
  return 0;
}

static int32_t mndSetCreateSmaVgroupCommitLogs(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup) {
  SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup);
  if (pVgRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pVgRaw) != 0) return -1;
  if (sdbSetRawStatus(pVgRaw, SDB_STATUS_READY) != 0) return -1;
  return 0;
}

403 404 405 406 407
static int32_t mndSetUpdateSmaStbCommitLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SStbObj stbObj = {0};
  taosRLockLatch(&pStb->lock);
  memcpy(&stbObj, pStb, sizeof(SStbObj));
  taosRUnLockLatch(&pStb->lock);
C
Cary Xu 已提交
408
  stbObj.numOfColumns = 0;
409
  stbObj.pColumns = NULL;
C
Cary Xu 已提交
410
  stbObj.numOfTags = 0;
411
  stbObj.pTags = NULL;
S
Shengliang Guan 已提交
412 413
  stbObj.numOfFuncs = 0;
  stbObj.pFuncs = NULL;
414 415 416 417 418 419 420 421 422 423 424 425
  stbObj.updateTime = taosGetTimestampMs();
  stbObj.lock = 0;
  stbObj.smaVer++;

  SSdbRaw *pCommitRaw = mndStbActionEncode(&stbObj);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

C
Cary Xu 已提交
426 427
static int32_t mndSetCreateSmaVgroupRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup,
                                                SSmaObj *pSma) {
S
Shengliang Guan 已提交
428 429 430 431 432 433 434 435 436
  SVnodeGid *pVgid = pVgroup->vnodeGid + 0;
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, pVgid->dnodeId);
  if (pDnode == NULL) return -1;

  STransAction action = {0};
  action.epSet = mndGetDnodeEpset(pDnode);
  mndReleaseDnode(pMnode, pDnode);

  // todo add sma info here
C
Cary Xu 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  SNode *pAst = NULL;
  if (nodesStringToNode(pSma->ast, &pAst) < 0) {
    return -1;
  }
  if (qExtractResultSchema(pAst, &pSma->schemaRow.nCols, &pSma->schemaRow.pSchema) != 0) {
    nodesDestroyNode(pAst);
    return -1;
  }
  nodesDestroyNode(pAst);
  pSma->schemaRow.version = 1;

  // TODO: the schemaTag generated by qExtractResultXXX later.
  pSma->schemaTag.nCols = 1;
  pSma->schemaTag.version = 1;
  pSma->schemaTag.pSchema = taosMemoryCalloc(1, sizeof(SSchema));
  if (!pSma->schemaTag.pSchema) {
    return -1;
  }
  pSma->schemaTag.pSchema[0].type = TSDB_DATA_TYPE_BIGINT;
  pSma->schemaTag.pSchema[0].bytes = TYPE_BYTES[TSDB_DATA_TYPE_BIGINT];
  pSma->schemaTag.pSchema[0].colId = pSma->schemaRow.nCols + PRIMARYKEY_TIMESTAMP_COL_ID;
  pSma->schemaTag.pSchema[0].flags = 0;
  snprintf(pSma->schemaTag.pSchema[0].name, TSDB_COL_NAME_LEN, "groupId");

C
Cary Xu 已提交
461 462 463 464
  int32_t smaContLen = 0;
  void   *pSmaReq = mndBuildVCreateSmaReq(pMnode, pVgroup, pSma, &smaContLen);
  if (pSmaReq == NULL) return -1;
  pVgroup->pTsma = pSmaReq;
S
Shengliang Guan 已提交
465 466

  int32_t contLen = 0;
S
Shengliang Guan 已提交
467
  void   *pReq = mndBuildCreateVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen);
K
kailixu 已提交
468 469 470 471
  if (pReq == NULL) {
    taosMemoryFreeClear(pSmaReq);
    return -1;
  }
S
Shengliang Guan 已提交
472 473 474 475

  action.pCont = pReq;
  action.contLen = contLen;
  action.msgType = TDMT_DND_CREATE_VNODE;
476
  action.acceptableCode = TSDB_CODE_VND_ALREADY_EXIST;
S
Shengliang Guan 已提交
477 478

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
K
kailixu 已提交
479 480 481 482 483 484 485 486 487 488 489 490
    taosMemoryFreeClear(pSmaReq);
    taosMemoryFree(pReq);
    return -1;
  }

  action.pCont = pSmaReq;
  action.contLen = smaContLen;
  action.msgType = TDMT_VND_CREATE_SMA;
  action.acceptableCode = TSDB_CODE_TSMA_ALREADY_EXIST;

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFreeClear(pSmaReq);
S
Shengliang Guan 已提交
491 492 493 494 495 496
    return -1;
  }

  return 0;
}

C
Cary Xu 已提交
497 498 499 500 501 502 503
static void mndDestroySmaObj(SSmaObj *pSmaObj) {
  if (pSmaObj) {
    taosMemoryFreeClear(pSmaObj->schemaRow.pSchema);
    taosMemoryFreeClear(pSmaObj->schemaTag.pSchema);
  }
}

504 505
static int32_t mndCreateSma(SMnode *pMnode, SRpcMsg *pReq, SMCreateSmaReq *pCreate, SDbObj *pDb, SStbObj *pStb,
                            const char *streamName) {
S
sma  
Shengliang Guan 已提交
506 507 508
  SSmaObj smaObj = {0};
  memcpy(smaObj.name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(smaObj.stb, pStb->name, TSDB_TABLE_FNAME_LEN);
S
sma  
Shengliang Guan 已提交
509
  memcpy(smaObj.db, pDb->name, TSDB_DB_FNAME_LEN);
S
sma  
Shengliang Guan 已提交
510 511
  smaObj.createdTime = taosGetTimestampMs();
  smaObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
S
Shengliang Guan 已提交
512

C
Cary Xu 已提交
513
  char resultTbName[TSDB_TABLE_FNAME_LEN + 16] = {0};
L
Liu Jicong 已提交
514
  snprintf(resultTbName, TSDB_TABLE_FNAME_LEN + 16, "%s_td_tsma_rst_tb", pCreate->name);
C
Cary Xu 已提交
515 516
  memcpy(smaObj.dstTbName, resultTbName, TSDB_TABLE_FNAME_LEN);
  smaObj.dstTbUid = mndGenerateUid(smaObj.dstTbName, TSDB_TABLE_FNAME_LEN);
S
sma  
Shengliang Guan 已提交
517
  smaObj.stbUid = pStb->uid;
S
sma  
Shengliang Guan 已提交
518
  smaObj.dbUid = pStb->dbUid;
S
sma  
Shengliang Guan 已提交
519 520
  smaObj.intervalUnit = pCreate->intervalUnit;
  smaObj.slidingUnit = pCreate->slidingUnit;
521
#if 0
S
sma  
Shengliang Guan 已提交
522
  smaObj.timezone = pCreate->timezone;
523
#endif
524
  smaObj.timezone = tsTimezone;  // use timezone of server
S
sma  
Shengliang Guan 已提交
525 526 527 528 529
  smaObj.interval = pCreate->interval;
  smaObj.offset = pCreate->offset;
  smaObj.sliding = pCreate->sliding;
  smaObj.exprLen = pCreate->exprLen;
  smaObj.tagsFilterLen = pCreate->tagsFilterLen;
S
sma  
Shengliang Guan 已提交
530 531 532
  smaObj.sqlLen = pCreate->sqlLen;
  smaObj.astLen = pCreate->astLen;
  if (smaObj.exprLen > 0) {
C
Cary Xu 已提交
533
    smaObj.expr = pCreate->expr;
S
sma  
Shengliang Guan 已提交
534 535
  }
  if (smaObj.tagsFilterLen > 0) {
C
Cary Xu 已提交
536
    smaObj.tagsFilter = pCreate->tagsFilter;
S
sma  
Shengliang Guan 已提交
537 538
  }
  if (smaObj.sqlLen > 0) {
C
Cary Xu 已提交
539
    smaObj.sql = pCreate->sql;
S
sma  
Shengliang Guan 已提交
540 541
  }
  if (smaObj.astLen > 0) {
C
Cary Xu 已提交
542
    smaObj.ast = pCreate->ast;
S
sma  
Shengliang Guan 已提交
543
  }
S
sma  
Shengliang Guan 已提交
544

S
sma  
Shengliang Guan 已提交
545
  SStreamObj streamObj = {0};
546
  tstrncpy(streamObj.name, streamName, TSDB_STREAM_FNAME_LEN);
L
Liu Jicong 已提交
547
  tstrncpy(streamObj.sourceDb, pDb->name, TSDB_DB_FNAME_LEN);
D
dapan1121 已提交
548
  tstrncpy(streamObj.targetDb, streamObj.sourceDb, TSDB_DB_FNAME_LEN);
S
sma  
Shengliang Guan 已提交
549 550
  streamObj.createTime = taosGetTimestampMs();
  streamObj.updateTime = streamObj.createTime;
551
  streamObj.uid = mndGenerateUid(streamName, strlen(streamName));
L
Liu Jicong 已提交
552
  streamObj.sourceDbUid = pDb->uid;
S
Shengliang Guan 已提交
553
  streamObj.targetDbUid = pDb->uid;
S
sma  
Shengliang Guan 已提交
554
  streamObj.version = 1;
L
Liu Jicong 已提交
555
  streamObj.sql = strdup(pCreate->sql);
L
Liu Jicong 已提交
556
  streamObj.smaId = smaObj.uid;
C
Cary Xu 已提交
557
  streamObj.watermark = pCreate->watermark;
558
  streamObj.deleteMark = pCreate->deleteMark;
559
  streamObj.fillHistory = STREAM_FILL_HISTORY_ON;
C
Cary Xu 已提交
560 561
  streamObj.trigger = STREAM_TRIGGER_WINDOW_CLOSE;
  streamObj.triggerParam = pCreate->maxDelay;
L
Liu Jicong 已提交
562
  streamObj.ast = strdup(smaObj.ast);
L
Liu Jicong 已提交
563

C
Cary Xu 已提交
564 565 566 567 568 569 570 571 572
  // check the maxDelay
  if (streamObj.triggerParam < TSDB_MIN_ROLLUP_MAX_DELAY) {
    int64_t msInterval = convertTimeFromPrecisionToUnit(pCreate->interval, pDb->cfg.precision, TIME_UNIT_MILLISECOND);
    streamObj.triggerParam = msInterval > TSDB_MIN_ROLLUP_MAX_DELAY ? msInterval : TSDB_MIN_ROLLUP_MAX_DELAY;
  }
  if (streamObj.triggerParam > TSDB_MAX_ROLLUP_MAX_DELAY) {
    streamObj.triggerParam = TSDB_MAX_ROLLUP_MAX_DELAY;
  }

L
Liu Jicong 已提交
573 574 575 576 577
  if (mndAllocSmaVgroup(pMnode, pDb, &streamObj.fixedSinkVg) != 0) {
    mError("sma:%s, failed to create since %s", smaObj.name, terrstr());
    return -1;
  }
  smaObj.dstVgId = streamObj.fixedSinkVg.vgId;
S
Shengliang Guan 已提交
578
  streamObj.fixedSinkVgId = smaObj.dstVgId;
S
sma  
Shengliang Guan 已提交
579

L
Liu Jicong 已提交
580 581
  SNode *pAst = NULL;
  if (nodesStringToNode(streamObj.ast, &pAst) < 0) {
S
Shengliang Guan 已提交
582 583
    terrno = TSDB_CODE_MND_INVALID_SMA_OPTION;
    mError("sma:%s, failed to create since parse ast error", smaObj.name);
L
Liu Jicong 已提交
584 585 586 587 588
    return -1;
  }

  // extract output schema from ast
  if (qExtractResultSchema(pAst, (int32_t *)&streamObj.outputSchema.nCols, &streamObj.outputSchema.pSchema) != 0) {
S
Shengliang Guan 已提交
589 590
    terrno = TSDB_CODE_MND_INVALID_SMA_OPTION;
    mError("sma:%s, failed to create since extract result schema error", smaObj.name);
L
Liu Jicong 已提交
591 592 593 594 595 596 597 598 599 600
    return -1;
  }

  SQueryPlan  *pPlan = NULL;
  SPlanContext cxt = {
      .pAstRoot = pAst,
      .topicQuery = false,
      .streamQuery = true,
      .triggerType = streamObj.trigger,
      .watermark = streamObj.watermark,
601
      .deleteMark = streamObj.deleteMark,
L
Liu Jicong 已提交
602 603 604
  };

  if (qCreateQueryPlan(&cxt, &pPlan, NULL) < 0) {
S
Shengliang Guan 已提交
605 606
    terrno = TSDB_CODE_MND_INVALID_SMA_OPTION;
    mError("sma:%s, failed to create since create query plan error", smaObj.name);
L
Liu Jicong 已提交
607 608 609 610 611
    return -1;
  }

  // save physcial plan
  if (nodesNodeToString((SNode *)pPlan, false, &streamObj.physicalPlan, NULL) != 0) {
S
Shengliang Guan 已提交
612 613
    terrno = TSDB_CODE_MND_INVALID_SMA_OPTION;
    mError("sma:%s, failed to create since save physcial plan error", smaObj.name);
L
Liu Jicong 已提交
614 615
    return -1;
  }
S
Shengliang Guan 已提交
616

L
Liu Jicong 已提交
617
  if (pAst != NULL) nodesDestroyNode(pAst);
L
Liu Jicong 已提交
618
  nodesDestroyNode((SNode *)pPlan);
L
Liu Jicong 已提交
619

S
sma  
Shengliang Guan 已提交
620
  int32_t code = -1;
621
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB, pReq, "create-sma");
S
sma  
Shengliang Guan 已提交
622
  if (pTrans == NULL) goto _OVER;
623
  mndTransSetDbName(pTrans, pDb->name, NULL);
624 625
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;

626
  mndTransSetSerial(pTrans);
627
  mInfo("trans:%d, used to create sma:%s stream:%s", pTrans->id, pCreate->name, streamObj.name);
S
sma  
Shengliang Guan 已提交
628 629

  if (mndSetCreateSmaRedoLogs(pMnode, pTrans, &smaObj) != 0) goto _OVER;
L
Liu Jicong 已提交
630
  if (mndSetCreateSmaVgroupRedoLogs(pMnode, pTrans, &streamObj.fixedSinkVg) != 0) goto _OVER;
S
sma  
Shengliang Guan 已提交
631
  if (mndSetCreateSmaCommitLogs(pMnode, pTrans, &smaObj) != 0) goto _OVER;
L
Liu Jicong 已提交
632
  if (mndSetCreateSmaVgroupCommitLogs(pMnode, pTrans, &streamObj.fixedSinkVg) != 0) goto _OVER;
633
  if (mndSetUpdateSmaStbCommitLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
C
Cary Xu 已提交
634
  if (mndSetCreateSmaVgroupRedoActions(pMnode, pTrans, pDb, &streamObj.fixedSinkVg, &smaObj) != 0) goto _OVER;
635
  if (mndScheduleStream(pMnode, &streamObj) != 0) goto _OVER;
L
Liu Jicong 已提交
636
  if (mndPersistStream(pMnode, pTrans, &streamObj) != 0) goto _OVER;
S
sma  
Shengliang Guan 已提交
637 638
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

639
  mInfo("sma:%s, uid:%" PRIi64 " create on stb:%" PRIi64 ", dstSuid:%" PRIi64 " dstTb:%s dstVg:%d", pCreate->name,
H
Hongze Cheng 已提交
640
        smaObj.uid, smaObj.stbUid, smaObj.dstTbUid, smaObj.dstTbName, smaObj.dstVgId);
C
Cary Xu 已提交
641

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

_OVER:
L
Liu Jicong 已提交
645
  tFreeStreamObj(&streamObj);
C
Cary Xu 已提交
646
  mndDestroySmaObj(&smaObj);
S
sma  
Shengliang Guan 已提交
647 648 649 650 651
  mndTransDrop(pTrans);
  return code;
}

static int32_t mndCheckCreateSmaReq(SMCreateSmaReq *pCreate) {
S
sma  
Shengliang Guan 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
  terrno = TSDB_CODE_MND_INVALID_SMA_OPTION;
  if (pCreate->name[0] == 0) return -1;
  if (pCreate->stb[0] == 0) return -1;
  if (pCreate->igExists < 0 || pCreate->igExists > 1) return -1;
  if (pCreate->intervalUnit < 0) return -1;
  if (pCreate->slidingUnit < 0) return -1;
  if (pCreate->timezone < 0) return -1;
  if (pCreate->interval < 0) return -1;
  if (pCreate->offset < 0) return -1;
  if (pCreate->sliding < 0) return -1;
  if (pCreate->exprLen < 0) return -1;
  if (pCreate->tagsFilterLen < 0) return -1;
  if (pCreate->sqlLen < 0) return -1;
  if (pCreate->astLen < 0) return -1;
  if (pCreate->exprLen != 0 && strlen(pCreate->expr) + 1 != pCreate->exprLen) return -1;
  if (pCreate->tagsFilterLen != 0 && strlen(pCreate->tagsFilter) + 1 != pCreate->tagsFilterLen) return -1;
  if (pCreate->sqlLen != 0 && strlen(pCreate->sql) + 1 != pCreate->sqlLen) return -1;
  if (pCreate->astLen != 0 && strlen(pCreate->ast) + 1 != pCreate->astLen) return -1;

  SName smaName = {0};
  if (tNameFromString(&smaName, pCreate->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE) < 0) return -1;
  if (*(char *)tNameGetTableName(&smaName) == 0) return -1;
S
sma  
Shengliang Guan 已提交
674

S
sma  
Shengliang Guan 已提交
675
  terrno = 0;
S
sma  
Shengliang Guan 已提交
676 677 678
  return 0;
}

679 680 681 682 683 684 685 686
static void mndGetStreamNameFromSmaName(char *streamName, char *smaName) {
  SName n;
  tNameFromString(&n, smaName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
  streamName[0] = '1';
  streamName[1] = '.';
  strcpy(streamName + 2, tNameGetTableName(&n));
}

687
static int32_t mndProcessCreateSmaReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
688
  SMnode        *pMnode = pReq->info.node;
S
sma  
Shengliang Guan 已提交
689 690 691
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SSmaObj       *pSma = NULL;
S
sma  
Shengliang Guan 已提交
692
  SStreamObj    *pStream = NULL;
S
sma  
Shengliang Guan 已提交
693 694 695
  SDbObj        *pDb = NULL;
  SMCreateSmaReq createReq = {0};

S
Shengliang Guan 已提交
696
  if (tDeserializeSMCreateSmaReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
sma  
Shengliang Guan 已提交
697 698 699 700
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

701
  mInfo("sma:%s, start to create", createReq.name);
S
sma  
Shengliang Guan 已提交
702 703 704 705 706 707 708 709 710
  if (mndCheckCreateSmaReq(&createReq) != 0) {
    goto _OVER;
  }

  pStb = mndAcquireStb(pMnode, createReq.stb);
  if (pStb == NULL) {
    mError("sma:%s, failed to create since stb:%s not exist", createReq.name, createReq.stb);
    goto _OVER;
  }
L
Liu Jicong 已提交
711

712 713 714 715
  char streamName[TSDB_TABLE_FNAME_LEN] = {0};
  mndGetStreamNameFromSmaName(streamName, createReq.name);

  pStream = mndAcquireStream(pMnode, streamName);
S
sma  
Shengliang Guan 已提交
716
  if (pStream != NULL) {
717
    mError("sma:%s, failed to create since stream:%s already exist", createReq.name, streamName);
C
Cary Xu 已提交
718
    terrno = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
S
sma  
Shengliang Guan 已提交
719 720
    goto _OVER;
  }
dengyihao's avatar
dengyihao 已提交
721
  SSIdx idx = {0};
722
  if (mndAcquireGlobalIdx(pMnode, createReq.name, SDB_SMA, &idx) == 0) {
dengyihao's avatar
dengyihao 已提交
723
    pSma = idx.pIdx;
dengyihao's avatar
dengyihao 已提交
724 725
  } else {
    goto _OVER;
dengyihao's avatar
dengyihao 已提交
726
  }
S
sma  
Shengliang Guan 已提交
727 728 729

  if (pSma != NULL) {
    if (createReq.igExists) {
730
      mInfo("sma:%s, already exist in sma:%s, ignore exist is set", createReq.name, pSma->name);
S
sma  
Shengliang Guan 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744
      code = 0;
      goto _OVER;
    } else {
      terrno = TSDB_CODE_MND_SMA_ALREADY_EXIST;
      goto _OVER;
    }
  }

  pDb = mndAcquireDbBySma(pMnode, createReq.name);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    goto _OVER;
  }

745
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
S
sma  
Shengliang Guan 已提交
746 747 748
    goto _OVER;
  }

749
  code = mndCreateSma(pMnode, pReq, &createReq, pDb, pStb, streamName);
S
Shengliang Guan 已提交
750
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
sma  
Shengliang Guan 已提交
751 752

_OVER:
S
Shengliang Guan 已提交
753
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
C
Cary Xu 已提交
754
    mError("sma:%s, failed to create since %s", createReq.name, terrstr());
S
sma  
Shengliang Guan 已提交
755 756 757 758
  }

  mndReleaseStb(pMnode, pStb);
  mndReleaseSma(pMnode, pSma);
S
sma  
Shengliang Guan 已提交
759
  mndReleaseStream(pMnode, pStream);
S
sma  
Shengliang Guan 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
  mndReleaseDb(pMnode, pDb);
  tFreeSMCreateSmaReq(&createReq);

  return code;
}

static int32_t mndSetDropSmaRedoLogs(SMnode *pMnode, STrans *pTrans, SSmaObj *pSma) {
  SSdbRaw *pRedoRaw = mndSmaActionEncode(pSma);
  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 mndSetDropSmaCommitLogs(SMnode *pMnode, STrans *pTrans, SSmaObj *pSma) {
  SSdbRaw *pCommitRaw = mndSmaActionEncode(pSma);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

L
Liu Jicong 已提交
784
static int32_t mndSetDropSmaVgroupRedoLogs(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup) {
S
Shengliang Guan 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
  SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup);
  if (pVgRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) return -1;
  if (sdbSetRawStatus(pVgRaw, SDB_STATUS_DROPPING) != 0) return -1;

  return 0;
}

static int32_t mndSetDropSmaVgroupCommitLogs(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup) {
  SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup);
  if (pVgRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pVgRaw) != 0) return -1;
  if (sdbSetRawStatus(pVgRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

static int32_t mndSetDropSmaVgroupRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup) {
  SVnodeGid *pVgid = pVgroup->vnodeGid + 0;
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, pVgid->dnodeId);
  if (pDnode == NULL) return -1;

  STransAction action = {0};
  action.epSet = mndGetDnodeEpset(pDnode);
  mndReleaseDnode(pMnode, pDnode);

  int32_t contLen = 0;
  void   *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen);
  if (pReq == NULL) return -1;

  action.pCont = pReq;
  action.contLen = contLen;
  action.msgType = TDMT_DND_DROP_VNODE;
818
  action.acceptableCode = TSDB_CODE_VND_NOT_EXIST;
S
Shengliang Guan 已提交
819 820 821 822 823 824 825 826 827

  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(pReq);
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
828
static int32_t mndDropSma(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SSmaObj *pSma) {
829 830 831 832
  int32_t  code = -1;
  SVgObj  *pVgroup = NULL;
  SStbObj *pStb = NULL;
  STrans  *pTrans = NULL;
S
Shengliang Guan 已提交
833 834 835 836

  pVgroup = mndAcquireVgroup(pMnode, pSma->dstVgId);
  if (pVgroup == NULL) goto _OVER;

837 838 839
  pStb = mndAcquireStb(pMnode, pSma->stb);
  if (pStb == NULL) goto _OVER;

840
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB, pReq, "drop-sma");
S
sma  
Shengliang Guan 已提交
841 842
  if (pTrans == NULL) goto _OVER;

843
  mInfo("trans:%d, used to drop sma:%s", pTrans->id, pSma->name);
844
  mndTransSetDbName(pTrans, pDb->name, NULL);
845 846
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;

C
Cary Xu 已提交
847
  mndTransSetSerial(pTrans);
S
sma  
Shengliang Guan 已提交
848

849 850 851 852
  char streamName[TSDB_TABLE_FNAME_LEN] = {0};
  mndGetStreamNameFromSmaName(streamName, pSma->name);

  SStreamObj *pStream = mndAcquireStream(pMnode, streamName);
L
Liu Jicong 已提交
853 854 855 856 857 858 859 860 861 862 863 864
  if (pStream == NULL || pStream->smaId != pSma->uid) {
    sdbRelease(pMnode->pSdb, pStream);
    goto _OVER;
  } else {
    if (mndDropStreamTasks(pMnode, pTrans, pStream) < 0) {
      mError("stream:%s, failed to drop task since %s", pStream->name, terrstr());
      sdbRelease(pMnode->pSdb, pStream);
      goto _OVER;
    }

    // drop stream
    if (mndPersistDropStreamLog(pMnode, pTrans, pStream) < 0) {
S
Shengliang Guan 已提交
865
      mError("stream:%s, failed to drop log since %s", pStream->name, terrstr());
L
Liu Jicong 已提交
866 867 868 869
      sdbRelease(pMnode->pSdb, pStream);
      goto _OVER;
    }
  }
S
sma  
Shengliang Guan 已提交
870
  if (mndSetDropSmaRedoLogs(pMnode, pTrans, pSma) != 0) goto _OVER;
S
Shengliang Guan 已提交
871
  if (mndSetDropSmaVgroupRedoLogs(pMnode, pTrans, pVgroup) != 0) goto _OVER;
S
sma  
Shengliang Guan 已提交
872
  if (mndSetDropSmaCommitLogs(pMnode, pTrans, pSma) != 0) goto _OVER;
S
Shengliang Guan 已提交
873
  if (mndSetDropSmaVgroupCommitLogs(pMnode, pTrans, pVgroup) != 0) goto _OVER;
874
  if (mndSetUpdateSmaStbCommitLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
S
Shengliang Guan 已提交
875
  if (mndSetDropSmaVgroupRedoActions(pMnode, pTrans, pDb, pVgroup) != 0) goto _OVER;
S
sma  
Shengliang Guan 已提交
876 877 878 879 880 881
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

  code = 0;

_OVER:
  mndTransDrop(pTrans);
882
  mndReleaseStream(pMnode, pStream);
S
Shengliang Guan 已提交
883
  mndReleaseVgroup(pMnode, pVgroup);
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
  mndReleaseStb(pMnode, pStb);
  return code;
}

int32_t mndDropSmasByStb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  SSdb    *pSdb = pMnode->pSdb;
  SSmaObj *pSma = NULL;
  void    *pIter = NULL;
  SVgObj  *pVgroup = NULL;
  int32_t  code = -1;

  while (1) {
    pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma);
    if (pIter == NULL) break;

    if (pSma->stbUid == pStb->uid) {
C
Cary Xu 已提交
900
      mndTransSetSerial(pTrans);
901 902
      pVgroup = mndAcquireVgroup(pMnode, pSma->dstVgId);
      if (pVgroup == NULL) goto _OVER;
S
Shengliang Guan 已提交
903

904 905 906 907
      char streamName[TSDB_TABLE_FNAME_LEN] = {0};
      mndGetStreamNameFromSmaName(streamName, pSma->name);

      SStreamObj *pStream = mndAcquireStream(pMnode, streamName);
S
Shengliang Guan 已提交
908 909 910 911 912 913 914 915 916
      if (pStream != NULL && pStream->smaId == pSma->uid) {
        if (mndDropStreamTasks(pMnode, pTrans, pStream) < 0) {
          mError("stream:%s, failed to drop task since %s", pStream->name, terrstr());
          goto _OVER;
        }
        if (mndPersistDropStreamLog(pMnode, pTrans, pStream) < 0) {
          goto _OVER;
        }
      }
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
      if (mndSetDropSmaVgroupCommitLogs(pMnode, pTrans, pVgroup) != 0) goto _OVER;
      if (mndSetDropSmaVgroupRedoActions(pMnode, pTrans, pDb, pVgroup) != 0) goto _OVER;
      if (mndSetDropSmaCommitLogs(pMnode, pTrans, pSma) != 0) goto _OVER;
      mndReleaseVgroup(pMnode, pVgroup);
      pVgroup = NULL;
    }

    sdbRelease(pSdb, pSma);
  }

  code = 0;

_OVER:
  sdbCancelFetch(pSdb, pIter);
  sdbRelease(pSdb, pSma);
  mndReleaseVgroup(pMnode, pVgroup);
S
sma  
Shengliang Guan 已提交
933 934 935
  return code;
}

936
int32_t mndDropSmasByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
S
Shengliang Guan 已提交
937 938
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
939 940

  while (1) {
S
Shengliang Guan 已提交
941
    SSmaObj *pSma = NULL;
942 943 944 945
    pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma);
    if (pIter == NULL) break;

    if (pSma->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
946 947 948 949
      if (mndSetDropSmaCommitLogs(pMnode, pTrans, pSma) != 0) {
        sdbRelease(pSdb, pSma);
        sdbCancelFetch(pSdb, pSma);
        return -1;
S
Shengliang Guan 已提交
950
      }
951 952 953 954 955
    }

    sdbRelease(pSdb, pSma);
  }

S
Shengliang Guan 已提交
956
  return 0;
957 958 959
}

static int32_t mndProcessDropSmaReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
960
  SMnode      *pMnode = pReq->info.node;
S
sma  
Shengliang Guan 已提交
961 962 963 964 965
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SSmaObj     *pSma = NULL;
  SMDropSmaReq dropReq = {0};

S
Shengliang Guan 已提交
966
  if (tDeserializeSMDropSmaReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
sma  
Shengliang Guan 已提交
967 968 969 970
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

971
  mInfo("sma:%s, start to drop", dropReq.name);
S
sma  
Shengliang Guan 已提交
972

dengyihao's avatar
dengyihao 已提交
973
  SSIdx idx = {0};
974
  if (mndAcquireGlobalIdx(pMnode, dropReq.name, SDB_SMA, &idx) == 0) {
dengyihao's avatar
dengyihao 已提交
975
    pSma = idx.pIdx;
dengyihao's avatar
dengyihao 已提交
976 977
  } else {
    goto _OVER;
dengyihao's avatar
dengyihao 已提交
978
  }
S
sma  
Shengliang Guan 已提交
979 980
  if (pSma == NULL) {
    if (dropReq.igNotExists) {
981
      mInfo("sma:%s, not exist, ignore not exist is set", dropReq.name);
S
sma  
Shengliang Guan 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995
      code = 0;
      goto _OVER;
    } else {
      terrno = TSDB_CODE_MND_SMA_NOT_EXIST;
      goto _OVER;
    }
  }

  pDb = mndAcquireDbBySma(pMnode, dropReq.name);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    goto _OVER;
  }

996
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
S
sma  
Shengliang Guan 已提交
997 998 999 1000
    goto _OVER;
  }

  code = mndDropSma(pMnode, pReq, pDb, pSma);
S
Shengliang Guan 已提交
1001
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
sma  
Shengliang Guan 已提交
1002 1003

_OVER:
S
Shengliang Guan 已提交
1004
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
sma  
Shengliang Guan 已提交
1005 1006 1007
    mError("sma:%s, failed to drop since %s", dropReq.name, terrstr());
  }

1008
  mndReleaseSma(pMnode, pSma);
1009
  mndReleaseDb(pMnode, pDb);
S
sma  
Shengliang Guan 已提交
1010 1011 1012
  return code;
}

S
Shengliang Guan 已提交
1013
static int32_t mndGetSma(SMnode *pMnode, SUserIndexReq *indexReq, SUserIndexRsp *rsp, bool *exist) {
L
Liu Jicong 已提交
1014 1015
  int32_t  code = -1;
  SSmaObj *pSma = NULL;
D
dapan1121 已提交
1016

1017 1018 1019
  SSIdx idx = {0};
  if (0 == mndAcquireGlobalIdx(pMnode, indexReq->indexFName, SDB_SMA, &idx)) {
    pSma = idx.pIdx;
dengyihao's avatar
dengyihao 已提交
1020 1021 1022
  } else {
    *exist = false;
    return 0;
1023 1024
  }

D
dapan1121 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
  if (pSma == NULL) {
    *exist = false;
    return 0;
  }

  memcpy(rsp->dbFName, pSma->db, sizeof(pSma->db));
  memcpy(rsp->tblFName, pSma->stb, sizeof(pSma->stb));
  strcpy(rsp->indexType, TSDB_INDEX_TYPE_SMA);

  SNodeList *pList = NULL;
L
Liu Jicong 已提交
1035
  int32_t    extOffset = 0;
D
dapan1121 已提交
1036 1037 1038 1039 1040
  code = nodesStringToList(pSma->expr, &pList);
  if (0 == code) {
    SNode *node = NULL;
    FOREACH(node, pList) {
      SFunctionNode *pFunc = (SFunctionNode *)node;
L
Liu Jicong 已提交
1041 1042
      extOffset += snprintf(rsp->indexExts + extOffset, sizeof(rsp->indexExts) - extOffset - 1, "%s%s",
                            (extOffset ? "," : ""), pFunc->functionName);
D
dapan1121 已提交
1043 1044 1045 1046 1047 1048
    }

    *exist = true;
  }

  mndReleaseSma(pMnode, pSma);
S
Shengliang Guan 已提交
1049 1050 1051
  return code;
}

D
dapan1121 已提交
1052
int32_t mndGetTableSma(SMnode *pMnode, char *tbFName, STableIndexRsp *rsp, bool *exist) {
1053 1054 1055 1056
  int32_t         code = 0;
  SSmaObj        *pSma = NULL;
  SSdb           *pSdb = pMnode->pSdb;
  void           *pIter = NULL;
D
dapan1121 已提交
1057 1058
  STableIndexInfo info;

1059
  SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
D
dapan1121 已提交
1060 1061 1062 1063 1064
  if (NULL == pStb) {
    *exist = false;
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1065
  strcpy(rsp->dbFName, pStb->db);
D
dapan1121 已提交
1066
  strcpy(rsp->tbName, pStb->name + strlen(pStb->db) + 1);
D
dapan1121 已提交
1067 1068 1069 1070
  rsp->suid = pStb->uid;
  rsp->version = pStb->smaVer;
  mndReleaseStb(pMnode, pStb);

D
dapan1121 已提交
1071 1072 1073 1074
  while (1) {
    pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma);
    if (pIter == NULL) break;

D
dapan1121 已提交
1075
    if (pSma->stb[0] != tbFName[0] || strcmp(pSma->stb, tbFName)) {
D
dapan1121 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
      continue;
    }

    info.intervalUnit = pSma->intervalUnit;
    info.slidingUnit = pSma->slidingUnit;
    info.interval = pSma->interval;
    info.offset = pSma->offset;
    info.sliding = pSma->sliding;
    info.dstTbUid = pSma->dstTbUid;
    info.dstVgId = pSma->dstVgId;
D
dapan1121 已提交
1086

1087
    SVgObj *pVg = mndAcquireVgroup(pMnode, pSma->dstVgId);
D
dapan1121 已提交
1088 1089 1090 1091 1092 1093
    if (pVg == NULL) {
      code = -1;
      sdbRelease(pSdb, pSma);
      return code;
    }
    info.epSet = mndGetVgroupEpset(pMnode, pVg);
1094

D
dapan1121 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    info.expr = taosMemoryMalloc(pSma->exprLen + 1);
    if (info.expr == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      code = -1;
      sdbRelease(pSdb, pSma);
      return code;
    }

    memcpy(info.expr, pSma->expr, pSma->exprLen);
    info.expr[pSma->exprLen] = 0;

    if (NULL == taosArrayPush(rsp->pIndex, &info)) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      code = -1;
      taosMemoryFree(info.expr);
      sdbRelease(pSdb, pSma);
      return code;
    }

    *exist = true;

    sdbRelease(pSdb, pSma);
  }
1118

D
dapan1121 已提交
1119 1120 1121
  return code;
}

S
Shengliang Guan 已提交
1122
static int32_t mndProcessGetSmaReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
1123
  SUserIndexReq indexReq = {0};
S
Shengliang Guan 已提交
1124
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1125 1126 1127 1128
  int32_t       code = -1;
  SUserIndexRsp rsp = {0};
  bool          exist = false;

S
Shengliang Guan 已提交
1129
  if (tDeserializeSUserIndexReq(pReq->pCont, pReq->contLen, &indexReq) != 0) {
S
Shengliang Guan 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

  code = mndGetSma(pMnode, &indexReq, &rsp, &exist);
  if (code) {
    goto _OVER;
  }

  if (!exist) {
    // TODO GET INDEX FROM FULLTEXT
    code = -1;
    terrno = TSDB_CODE_MND_DB_INDEX_NOT_EXIST;
  } else {
    int32_t contLen = tSerializeSUserIndexRsp(NULL, 0, &rsp);
    void   *pRsp = rpcMallocCont(contLen);
    if (pRsp == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      code = -1;
      goto _OVER;
    }

    tSerializeSUserIndexRsp(pRsp, contLen, &rsp);

S
Shengliang Guan 已提交
1154 1155
    pReq->info.rsp = pRsp;
    pReq->info.rspLen = contLen;
S
Shengliang Guan 已提交
1156 1157 1158 1159 1160 1161 1162 1163

    code = 0;
  }

_OVER:
  if (code != 0) {
    mError("failed to get index %s since %s", indexReq.indexFName, terrstr());
  }
D
dapan1121 已提交
1164 1165 1166 1167

  return code;
}

D
dapan1121 已提交
1168 1169
static int32_t mndProcessGetTbSmaReq(SRpcMsg *pReq) {
  STableIndexReq indexReq = {0};
1170 1171
  SMnode        *pMnode = pReq->info.node;
  int32_t        code = -1;
D
dapan1121 已提交
1172
  STableIndexRsp rsp = {0};
1173
  bool           exist = false;
D
dapan1121 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186

  if (tDeserializeSTableIndexReq(pReq->pCont, pReq->contLen, &indexReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

  rsp.pIndex = taosArrayInit(10, sizeof(STableIndexInfo));
  if (NULL == rsp.pIndex) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    code = -1;
    goto _OVER;
  }

D
dapan1121 已提交
1187
  code = mndGetTableSma(pMnode, indexReq.tbFName, &rsp, &exist);
D
dapan1121 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
  if (code) {
    goto _OVER;
  }

  if (!exist) {
    code = -1;
    terrno = TSDB_CODE_MND_DB_INDEX_NOT_EXIST;
  } else {
    int32_t contLen = tSerializeSTableIndexRsp(NULL, 0, &rsp);
    void   *pRsp = rpcMallocCont(contLen);
    if (pRsp == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      code = -1;
      goto _OVER;
    }

    tSerializeSTableIndexRsp(pRsp, contLen, &rsp);

    pReq->info.rsp = pRsp;
    pReq->info.rspLen = contLen;

    code = 0;
  }

_OVER:
  if (code != 0) {
    mError("failed to get table index %s since %s", indexReq.tbFName, terrstr());
  }

1217
  tFreeSerializeSTableIndexRsp(&rsp);
D
dapan1121 已提交
1218 1219 1220
  return code;
}

S
Shengliang Guan 已提交
1221 1222
static int32_t mndRetrieveSma(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
S
sma  
Shengliang Guan 已提交
1223 1224 1225 1226 1227
  SSdb    *pSdb = pMnode->pSdb;
  int32_t  numOfRows = 0;
  SSmaObj *pSma = NULL;
  int32_t  cols = 0;

1228 1229 1230 1231 1232
  SDbObj *pDb = NULL;
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
    if (pDb == NULL) return 0;
  }
dengyihao's avatar
dengyihao 已提交
1233
  SSmaAndTagIter *pIter = pShow->pIter;
S
sma  
Shengliang Guan 已提交
1234
  while (numOfRows < rows) {
dengyihao's avatar
dengyihao 已提交
1235 1236
    pIter->pSmaIter = sdbFetch(pSdb, SDB_SMA, pIter->pSmaIter, (void **)&pSma);
    if (pIter->pSmaIter == NULL) break;
S
sma  
Shengliang Guan 已提交
1237

1238
    if (NULL != pDb && pSma->dbUid != pDb->uid) {
S
sma  
Shengliang Guan 已提交
1239 1240 1241 1242 1243 1244 1245 1246
      sdbRelease(pSdb, pSma);
      continue;
    }

    cols = 0;

    SName smaName = {0};
    tNameFromString(&smaName, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
X
Xiaoyu Wang 已提交
1247 1248 1249
    char n1[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_TO_VARSTR(n1, (char *)tNameGetTableName(&smaName));

S
Shengliang Guan 已提交
1250
    char n2[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
1251
    STR_TO_VARSTR(n2, (char *)mndGetDbStr(pSma->db));
X
Xiaoyu Wang 已提交
1252

S
sma  
Shengliang Guan 已提交
1253 1254
    SName stbName = {0};
    tNameFromString(&stbName, pSma->stb, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
X
Xiaoyu Wang 已提交
1255 1256
    char n3[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_TO_VARSTR(n3, (char *)tNameGetTableName(&stbName));
1257

L
Liu Jicong 已提交
1258
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
X
Xiaoyu Wang 已提交
1259
    colDataAppend(pColInfo, numOfRows, (const char *)n1, false);
1260 1261

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
X
Xiaoyu Wang 已提交
1262 1263 1264 1265
    colDataAppend(pColInfo, numOfRows, (const char *)n2, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)n3, false);
S
sma  
Shengliang Guan 已提交
1266

S
Shengliang Guan 已提交
1267 1268 1269
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pSma->dstVgId, false);

S
Shengliang Guan 已提交
1270 1271 1272
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pSma->createdTime, false);

dengyihao's avatar
opt idx  
dengyihao 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    char col[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_TO_VARSTR(col, (char *)"");

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)col, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);

    char tag[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_TO_VARSTR(tag, (char *)"sma_index");
    colDataAppend(pColInfo, numOfRows, (const char *)tag, false);

S
sma  
Shengliang Guan 已提交
1285 1286 1287 1288
    numOfRows++;
    sdbRelease(pSdb, pSma);
  }

X
Xiaoyu Wang 已提交
1289
  mndReleaseDb(pMnode, pDb);
1290
  pShow->numOfRows += numOfRows;
S
sma  
Shengliang Guan 已提交
1291 1292 1293
  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
// sma and tag index comm func
static int32_t mndProcessDropIdxReq(SRpcMsg *pReq) {
  int ret = mndProcessDropSmaReq(pReq);
  if (terrno == TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST) {
    terrno = 0;
    ret = mndProcessDropTagIdxReq(pReq);
  }
  return ret;
}

static int32_t mndRetrieveIdx(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  if (pShow->pIter == NULL) {
    pShow->pIter = taosMemoryCalloc(1, sizeof(SSmaAndTagIter));
  }
  int32_t read = mndRetrieveSma(pReq, pShow, pBlock, rows);
  if (read < rows) read += mndRetrieveTagIdx(pReq, pShow, pBlock, rows - read);
  return read;
}
static void mndCancelRetrieveIdx(SMnode *pMnode, void *pIter) {
  SSmaAndTagIter *p = pIter;
  if (p != NULL) {
    SSdb *pSdb = pMnode->pSdb;
    sdbCancelFetch(pSdb, p->pSmaIter);
    sdbCancelFetch(pSdb, p->pIdxIter);
  }
  taosMemoryFree(p);
D
dapan1121 已提交
1320
}