mndStream.c 31.6 KB
Newer Older
L
Liu Jicong 已提交
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/>.
 */

#include "mndStream.h"
#include "mndDb.h"
#include "mndDnode.h"
#include "mndMnode.h"
L
Liu Jicong 已提交
20
#include "mndPrivilege.h"
L
Liu Jicong 已提交
21
#include "mndScheduler.h"
L
Liu Jicong 已提交
22 23
#include "mndShow.h"
#include "mndStb.h"
L
Liu Jicong 已提交
24
#include "mndTopic.h"
L
Liu Jicong 已提交
25 26 27
#include "mndTrans.h"
#include "mndUser.h"
#include "mndVgroup.h"
L
Liu Jicong 已提交
28
#include "parser.h"
L
Liu Jicong 已提交
29 30 31 32 33 34 35 36
#include "tname.h"

#define MND_STREAM_VER_NUMBER   1
#define MND_STREAM_RESERVE_SIZE 64

static int32_t mndStreamActionInsert(SSdb *pSdb, SStreamObj *pStream);
static int32_t mndStreamActionDelete(SSdb *pSdb, SStreamObj *pStream);
static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pStream, SStreamObj *pNewStream);
S
Shengliang Guan 已提交
37
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq);
L
Liu Jicong 已提交
38
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq);
L
Liu Jicong 已提交
39
/*static int32_t mndProcessRecoverStreamReq(SRpcMsg *pReq);*/
S
Shengliang Guan 已提交
40 41 42
static int32_t mndProcessStreamMetaReq(SRpcMsg *pReq);
static int32_t mndGetStreamMeta(SRpcMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
L
Liu Jicong 已提交
43 44 45
static void    mndCancelGetNextStream(SMnode *pMnode, void *pIter);

int32_t mndInitStream(SMnode *pMnode) {
46 47 48 49 50 51 52 53 54
  SSdbTable table = {
      .sdbType = SDB_STREAM,
      .keyType = SDB_KEY_BINARY,
      .encodeFp = (SdbEncodeFp)mndStreamActionEncode,
      .decodeFp = (SdbDecodeFp)mndStreamActionDecode,
      .insertFp = (SdbInsertFp)mndStreamActionInsert,
      .updateFp = (SdbUpdateFp)mndStreamActionUpdate,
      .deleteFp = (SdbDeleteFp)mndStreamActionDelete,
  };
L
Liu Jicong 已提交
55 56

  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STREAM, mndProcessCreateStreamReq);
L
Liu Jicong 已提交
57
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STREAM, mndProcessDropStreamReq);
L
Liu Jicong 已提交
58
  /*mndSetMsgHandle(pMnode, TDMT_MND_RECOVER_STREAM, mndProcessRecoverStreamReq);*/
L
Liu Jicong 已提交
59 60 61

  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_DEPLOY_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_DROP_RSP, mndTransProcessRsp);
L
Liu Jicong 已提交
62

L
Liu Jicong 已提交
63 64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndRetrieveStream);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndCancelGetNextStream);
L
Liu Jicong 已提交
65 66 67 68 69 70 71 72 73 74

  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupStream(SMnode *pMnode) {}

SSdbRaw *mndStreamActionEncode(SStreamObj *pStream) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  void *buf = NULL;

H
Hongze Cheng 已提交
75 76
  SEncoder encoder;
  tEncoderInit(&encoder, NULL, 0);
L
Liu Jicong 已提交
77
  if (tEncodeSStreamObj(&encoder, pStream) < 0) {
H
Hongze Cheng 已提交
78
    tEncoderClear(&encoder);
L
Liu Jicong 已提交
79 80 81
    goto STREAM_ENCODE_OVER;
  }
  int32_t tlen = encoder.pos;
H
Hongze Cheng 已提交
82
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
83 84 85 86 87

  int32_t  size = sizeof(int32_t) + tlen + MND_STREAM_RESERVE_SIZE;
  SSdbRaw *pRaw = sdbAllocRaw(SDB_STREAM, MND_STREAM_VER_NUMBER, size);
  if (pRaw == NULL) goto STREAM_ENCODE_OVER;

wafwerar's avatar
wafwerar 已提交
88
  buf = taosMemoryMalloc(tlen);
L
Liu Jicong 已提交
89 90
  if (buf == NULL) goto STREAM_ENCODE_OVER;

H
Hongze Cheng 已提交
91
  tEncoderInit(&encoder, buf, tlen);
L
Liu Jicong 已提交
92
  if (tEncodeSStreamObj(&encoder, pStream) < 0) {
H
Hongze Cheng 已提交
93
    tEncoderClear(&encoder);
L
Liu Jicong 已提交
94 95
    goto STREAM_ENCODE_OVER;
  }
H
Hongze Cheng 已提交
96
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
97 98 99 100 101 102 103 104 105

  int32_t dataPos = 0;
  SDB_SET_INT32(pRaw, dataPos, tlen, STREAM_ENCODE_OVER);
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, STREAM_ENCODE_OVER);
  SDB_SET_DATALEN(pRaw, dataPos, STREAM_ENCODE_OVER);

  terrno = TSDB_CODE_SUCCESS;

STREAM_ENCODE_OVER:
wafwerar's avatar
wafwerar 已提交
106
  taosMemoryFreeClear(buf);
L
Liu Jicong 已提交
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
  if (terrno != TSDB_CODE_SUCCESS) {
    mError("stream:%s, failed to encode to raw:%p since %s", pStream->name, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }

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

SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  void *buf = NULL;

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

  if (sver != MND_STREAM_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    goto STREAM_DECODE_OVER;
  }

  int32_t  size = sizeof(SStreamObj);
  SSdbRow *pRow = sdbAllocRow(size);
  if (pRow == NULL) goto STREAM_DECODE_OVER;

  SStreamObj *pStream = sdbGetRowObj(pRow);
  if (pStream == NULL) goto STREAM_DECODE_OVER;

  int32_t tlen;
  int32_t dataPos = 0;
  SDB_GET_INT32(pRaw, dataPos, &tlen, STREAM_DECODE_OVER);
wafwerar's avatar
wafwerar 已提交
139
  buf = taosMemoryMalloc(tlen + 1);
L
Liu Jicong 已提交
140 141 142
  if (buf == NULL) goto STREAM_DECODE_OVER;
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, STREAM_DECODE_OVER);

H
Hongze Cheng 已提交
143 144
  SDecoder decoder;
  tDecoderInit(&decoder, buf, tlen + 1);
L
Liu Jicong 已提交
145 146 147 148 149 150 151
  if (tDecodeSStreamObj(&decoder, pStream) < 0) {
    goto STREAM_DECODE_OVER;
  }

  terrno = TSDB_CODE_SUCCESS;

STREAM_DECODE_OVER:
wafwerar's avatar
wafwerar 已提交
152
  taosMemoryFreeClear(buf);
L
Liu Jicong 已提交
153 154
  if (terrno != TSDB_CODE_SUCCESS) {
    mError("stream:%s, failed to decode from raw:%p since %s", pStream->name, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
155
    taosMemoryFreeClear(pRow);
L
Liu Jicong 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169
    return NULL;
  }

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

static int32_t mndStreamActionInsert(SSdb *pSdb, SStreamObj *pStream) {
  mTrace("stream:%s, perform insert action", pStream->name);
  return 0;
}

static int32_t mndStreamActionDelete(SSdb *pSdb, SStreamObj *pStream) {
  mTrace("stream:%s, perform delete action", pStream->name);
L
Liu Jicong 已提交
170 171 172
  taosWLockLatch(&pStream->lock);
  tFreeStreamObj(pStream);
  taosWUnLockLatch(&pStream->lock);
L
Liu Jicong 已提交
173 174 175 176 177
  return 0;
}

static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pOldStream, SStreamObj *pNewStream) {
  mTrace("stream:%s, perform update action", pOldStream->name);
wafwerar's avatar
wafwerar 已提交
178
  atomic_exchange_64(&pOldStream->updateTime, pNewStream->updateTime);
L
Liu Jicong 已提交
179 180 181 182
  atomic_exchange_32(&pOldStream->version, pNewStream->version);

  taosWLockLatch(&pOldStream->lock);

183
  pOldStream->status = pNewStream->status;
L
Liu Jicong 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

  taosWUnLockLatch(&pOldStream->lock);
  return 0;
}

SStreamObj *mndAcquireStream(SMnode *pMnode, char *streamName) {
  SSdb       *pSdb = pMnode->pSdb;
  SStreamObj *pStream = sdbAcquire(pSdb, SDB_STREAM, streamName);
  if (pStream == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
    terrno = TSDB_CODE_MND_STREAM_NOT_EXIST;
  }
  return pStream;
}

void mndReleaseStream(SMnode *pMnode, SStreamObj *pStream) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStream);
}

L
Liu Jicong 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
static void mndShowStreamStatus(char *dst, SStreamObj *pStream) {
  int8_t status = atomic_load_8(&pStream->status);
  if (status == STREAM_STATUS__NORMAL) {
    strcpy(dst, "normal");
  } else if (status == STREAM_STATUS__STOP) {
    strcpy(dst, "stop");
  } else if (status == STREAM_STATUS__FAILED) {
    strcpy(dst, "failed");
  } else if (status == STREAM_STATUS__RECOVER) {
    strcpy(dst, "recover");
  }
}

static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
  int8_t trigger = pStream->trigger;
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
    strcpy(dst, "at once");
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
    strcpy(dst, "window close");
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
    strcpy(dst, "max delay");
  }
}

L
Liu Jicong 已提交
227
static int32_t mndCheckCreateStreamReq(SCMCreateStreamReq *pCreate) {
L
Liu Jicong 已提交
228 229
  if (pCreate->name[0] == 0 || pCreate->sql == NULL || pCreate->sql[0] == 0 || pCreate->sourceDB[0] == 0 ||
      pCreate->targetStbFullName[0] == 0) {
L
Liu Jicong 已提交
230 231 232 233 234 235
    terrno = TSDB_CODE_MND_INVALID_STREAM_OPTION;
    return -1;
  }
  return 0;
}

X
Xiaoyu Wang 已提交
236
static int32_t mndStreamGetPlanString(const char *ast, int8_t triggerType, int64_t watermark, char **pStr) {
S
sma  
Shengliang Guan 已提交
237
  if (NULL == ast) {
L
Liu Jicong 已提交
238 239 240 241
    return TSDB_CODE_SUCCESS;
  }

  SNode  *pAst = NULL;
S
sma  
Shengliang Guan 已提交
242
  int32_t code = nodesStringToNode(ast, &pAst);
L
Liu Jicong 已提交
243 244 245 246 247 248 249

  SQueryPlan *pPlan = NULL;
  if (TSDB_CODE_SUCCESS == code) {
    SPlanContext cxt = {
        .pAstRoot = pAst,
        .topicQuery = false,
        .streamQuery = true,
250
        .triggerType = triggerType == STREAM_TRIGGER_MAX_DELAY ? STREAM_TRIGGER_WINDOW_CLOSE : triggerType,
X
Xiaoyu Wang 已提交
251
        .watermark = watermark,
L
Liu Jicong 已提交
252 253 254 255 256
    };
    code = qCreateQueryPlan(&cxt, &pPlan, NULL);
  }

  if (TSDB_CODE_SUCCESS == code) {
257
    code = nodesNodeToString((SNode *)pPlan, false, pStr, NULL);
L
Liu Jicong 已提交
258 259
  }
  nodesDestroyNode(pAst);
260
  nodesDestroyNode((SNode *)pPlan);
L
Liu Jicong 已提交
261 262 263 264
  terrno = code;
  return code;
}

L
Liu Jicong 已提交
265
static int32_t mndBuildStreamObjFromCreateReq(SMnode *pMnode, SStreamObj *pObj, SCMCreateStreamReq *pCreate) {
266 267
  SNode      *pAst = NULL;
  SQueryPlan *pPlan = NULL;
L
Liu Jicong 已提交
268 269 270 271 272 273 274 275 276 277 278

  mDebug("stream:%s to create", pCreate->name);
  memcpy(pObj->name, pCreate->name, TSDB_STREAM_FNAME_LEN);
  pObj->createTime = taosGetTimestampMs();
  pObj->updateTime = pObj->createTime;
  pObj->version = 1;
  pObj->smaId = 0;

  pObj->uid = mndGenerateUid(pObj->name, strlen(pObj->name));
  pObj->status = 0;

279
  pObj->igExpired = pCreate->igExpired;
L
Liu Jicong 已提交
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 324 325 326 327 328 329 330
  pObj->trigger = pCreate->triggerType;
  pObj->triggerParam = pCreate->maxDelay;
  pObj->watermark = pCreate->watermark;

  memcpy(pObj->sourceDb, pCreate->sourceDB, TSDB_DB_FNAME_LEN);
  SDbObj *pSourceDb = mndAcquireDb(pMnode, pCreate->sourceDB);
  if (pSourceDb == NULL) {
    /*ASSERT(0);*/
    mDebug("stream:%s failed to create, source db %s not exist", pCreate->name, pObj->sourceDb);
    terrno = TSDB_CODE_MND_DB_NOT_EXIST;
    return -1;
  }
  pObj->sourceDbUid = pSourceDb->uid;

  memcpy(pObj->targetSTbName, pCreate->targetStbFullName, TSDB_TABLE_FNAME_LEN);

  SDbObj *pTargetDb = mndAcquireDbByStb(pMnode, pObj->targetSTbName);
  if (pTargetDb == NULL) {
    mDebug("stream:%s failed to create, target db %s not exist", pCreate->name, pObj->targetDb);
    terrno = TSDB_CODE_MND_DB_NOT_EXIST;
    return -1;
  }
  tstrncpy(pObj->targetDb, pTargetDb->name, TSDB_DB_FNAME_LEN);

  pObj->targetStbUid = mndGenerateUid(pObj->targetSTbName, TSDB_TABLE_FNAME_LEN);
  pObj->targetDbUid = pTargetDb->uid;

  pObj->sql = pCreate->sql;
  pObj->ast = pCreate->ast;

  pCreate->sql = NULL;
  pCreate->ast = NULL;

  // deserialize ast
  if (nodesStringToNode(pObj->ast, &pAst) < 0) {
    /*ASSERT(0);*/
    goto FAIL;
  }

  // extract output schema from ast
  if (qExtractResultSchema(pAst, (int32_t *)&pObj->outputSchema.nCols, &pObj->outputSchema.pSchema) != 0) {
    /*ASSERT(0);*/
    goto FAIL;
  }

  SPlanContext cxt = {
      .pAstRoot = pAst,
      .topicQuery = false,
      .streamQuery = true,
      .triggerType = pObj->trigger == STREAM_TRIGGER_MAX_DELAY ? STREAM_TRIGGER_WINDOW_CLOSE : pObj->trigger,
      .watermark = pObj->watermark,
331
      .igExpired = pObj->igExpired,
L
Liu Jicong 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  };

  // using ast and param to build physical plan
  if (qCreateQueryPlan(&cxt, &pPlan, NULL) < 0) {
    /*ASSERT(0);*/
    goto FAIL;
  }

  // save physcial plan
  if (nodesNodeToString((SNode *)pPlan, false, &pObj->physicalPlan, NULL) != 0) {
    /*ASSERT(0);*/
    goto FAIL;
  }

FAIL:
  if (pAst != NULL) nodesDestroyNode(pAst);
348
  if (pPlan != NULL) qDestroyQueryPlan(pPlan);
L
Liu Jicong 已提交
349 350 351
  return 0;
}

352
int32_t mndPersistTaskDeployReq(STrans *pTrans, const SStreamTask *pTask) {
353
  if (pTask->taskLevel == TASK_LEVEL__AGG) {
L
Liu Jicong 已提交
354 355
    ASSERT(taosArrayGetSize(pTask->childEpInfo) != 0);
  }
356 357 358 359 360 361 362 363 364
  SEncoder encoder;
  tEncoderInit(&encoder, NULL, 0);
  tEncodeSStreamTask(&encoder, pTask);
  int32_t size = encoder.pos;
  int32_t tlen = sizeof(SMsgHead) + size;
  tEncoderClear(&encoder);
  void *buf = taosMemoryCalloc(1, tlen);
  if (buf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
365 366
    return -1;
  }
367 368 369 370 371
  ((SMsgHead *)buf)->vgId = htonl(pTask->nodeId);
  void *abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
  tEncoderInit(&encoder, abuf, size);
  tEncodeSStreamTask(&encoder, pTask);
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
372

373 374 375 376 377 378 379
  STransAction action = {0};
  memcpy(&action.epSet, &pTask->epSet, sizeof(SEpSet));
  action.pCont = buf;
  action.contLen = tlen;
  action.msgType = TDMT_STREAM_TASK_DEPLOY;
  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(buf);
L
Liu Jicong 已提交
380 381 382 383 384
    return -1;
  }
  return 0;
}

385 386 387 388 389 390 391 392 393 394 395
int32_t mndPersistStreamTasks(SMnode *pMnode, STrans *pTrans, SStreamObj *pStream) {
  int32_t level = taosArrayGetSize(pStream->tasks);
  for (int32_t i = 0; i < level; i++) {
    SArray *pLevel = taosArrayGetP(pStream->tasks, i);
    int32_t sz = taosArrayGetSize(pLevel);
    for (int32_t j = 0; j < sz; j++) {
      SStreamTask *pTask = taosArrayGetP(pLevel, j);
      if (mndPersistTaskDeployReq(pTrans, pTask) < 0) {
        return -1;
      }
    }
L
Liu Jicong 已提交
396
  }
397 398
  return 0;
}
L
Liu Jicong 已提交
399

400 401
int32_t mndPersistStream(SMnode *pMnode, STrans *pTrans, SStreamObj *pStream) {
  if (mndPersistStreamTasks(pMnode, pTrans, pStream) < 0) {
L
Liu Jicong 已提交
402 403
    return -1;
  }
404 405 406
  SSdbRaw *pCommitRaw = mndStreamActionEncode(pStream);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
L
Liu Jicong 已提交
407 408
    return -1;
  }
409 410 411
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
  return 0;
}
L
Liu Jicong 已提交
412

413
int32_t mndPersistDropStreamLog(SMnode *pMnode, STrans *pTrans, SStreamObj *pStream) {
414 415 416
  SSdbRaw *pCommitRaw = mndStreamActionEncode(pStream);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
L
Liu Jicong 已提交
417 418 419
    mndTransDrop(pTrans);
    return -1;
  }
420
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);
S
sma  
Shengliang Guan 已提交
421 422 423
  return 0;
}

424 425 426 427 428 429 430 431 432 433 434 435 436 437
static int32_t mndSetStreamRecover(SMnode *pMnode, STrans *pTrans, const SStreamObj *pStream) {
  SStreamObj streamObj = {0};
  memcpy(streamObj.name, pStream->name, TSDB_STREAM_FNAME_LEN);
  streamObj.status = STREAM_STATUS__RECOVER;
  SSdbRaw *pCommitRaw = mndStreamActionEncode(&streamObj);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("stream trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
  return 0;
}

L
Liu Jicong 已提交
438
static int32_t mndCreateStbForStream(SMnode *pMnode, STrans *pTrans, const SStreamObj *pStream, const char *user) {
L
Liu Jicong 已提交
439 440
  SStbObj *pStb = NULL;
  SDbObj  *pDb = NULL;
441 442 443 444 445 446 447

  SMCreateStbReq createReq = {0};
  tstrncpy(createReq.name, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
  createReq.numOfColumns = pStream->outputSchema.nCols;
  createReq.numOfTags = 1;  // group id
  createReq.pColumns = taosArrayInit(createReq.numOfColumns, sizeof(SField));
  // build fields
L
Liu Jicong 已提交
448 449 450 451 452 453 454 455 456 457
  taosArraySetSize(createReq.pColumns, createReq.numOfColumns);
  for (int32_t i = 0; i < createReq.numOfColumns; i++) {
    SField *pField = taosArrayGet(createReq.pColumns, i);
    tstrncpy(pField->name, pStream->outputSchema.pSchema[i].name, TSDB_COL_NAME_LEN);
    pField->flags = pStream->outputSchema.pSchema[i].flags;
    pField->type = pStream->outputSchema.pSchema[i].type;
    pField->bytes = pStream->outputSchema.pSchema[i].bytes;
  }
  createReq.pTags = taosArrayInit(createReq.numOfTags, sizeof(SField));
  taosArraySetSize(createReq.pTags, 1);
458
  // build tags
L
Liu Jicong 已提交
459 460 461 462 463
  SField *pField = taosArrayGet(createReq.pTags, 0);
  strcpy(pField->name, "group_id");
  pField->type = TSDB_DATA_TYPE_UBIGINT;
  pField->flags = 0;
  pField->bytes = 8;
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

  if (mndCheckCreateStbReq(&createReq) != 0) {
    goto _OVER;
  }

  pStb = mndAcquireStb(pMnode, createReq.name);
  if (pStb != NULL) {
    terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
    goto _OVER;
  }

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

  int32_t numOfStbs = -1;
S
Shengliang Guan 已提交
482 483 484 485
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
    goto _OVER;
  }

486 487 488 489 490 491 492 493 494 495 496
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
    terrno = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
    goto _OVER;
  }

  SStbObj stbObj = {0};

  if (mndBuildStbFromReq(pMnode, &stbObj, &createReq, pDb) != 0) {
    goto _OVER;
  }

L
Liu Jicong 已提交
497 498
  stbObj.uid = pStream->targetStbUid;

L
Liu Jicong 已提交
499 500 501 502 503 504 505
  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) {
    mndFreeStb(&stbObj);
    goto _OVER;
  }

  tFreeSMCreateStbReq(&createReq);
  mndFreeStb(&stbObj);
506

L
Liu Jicong 已提交
507
  return 0;
508
_OVER:
L
Liu Jicong 已提交
509
  tFreeSMCreateStbReq(&createReq);
510 511
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
L
Liu Jicong 已提交
512
  return -1;
513 514
}

L
Liu Jicong 已提交
515 516 517 518
static int32_t mndPersistTaskDropReq(STrans *pTrans, SStreamTask *pTask) {
  ASSERT(pTask->nodeId != 0);

  // vnode
L
Liu Jicong 已提交
519 520 521 522 523 524 525 526 527 528 529 530
  /*if (pTask->nodeId > 0) {*/
  SVDropStreamTaskReq *pReq = taosMemoryCalloc(1, sizeof(SVDropStreamTaskReq));
  if (pReq == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  pReq->head.vgId = htonl(pTask->nodeId);
  pReq->taskId = pTask->taskId;
  STransAction action = {0};
  memcpy(&action.epSet, &pTask->epSet, sizeof(SEpSet));
  action.pCont = pReq;
  action.contLen = sizeof(SVDropStreamTaskReq);
L
Liu Jicong 已提交
531
  action.msgType = TDMT_STREAM_TASK_DROP;
L
Liu Jicong 已提交
532 533 534
  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(pReq);
    return -1;
L
Liu Jicong 已提交
535
  }
L
Liu Jicong 已提交
536
  /*}*/
L
Liu Jicong 已提交
537 538 539 540

  return 0;
}

L
Liu Jicong 已提交
541
#if 0
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
static int32_t mndPersistTaskRecoverReq(STrans *pTrans, SStreamTask *pTask) {
  SMStreamTaskRecoverReq *pReq = taosMemoryCalloc(1, sizeof(SMStreamTaskRecoverReq));
  if (pReq == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  pReq->streamId = pTask->streamId;
  pReq->taskId = pTask->taskId;
  int32_t len;
  int32_t code;
  tEncodeSize(tEncodeSMStreamTaskRecoverReq, pReq, len, code);
  if (code != 0) {
    return -1;
  }
  void *buf = taosMemoryCalloc(1, sizeof(SMsgHead) + len);
  if (buf == NULL) {
    return -1;
  }
  void    *abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
  SEncoder encoder;
  tEncoderInit(&encoder, abuf, len);
  tEncodeSMStreamTaskRecoverReq(&encoder, pReq);
  ((SMsgHead *)buf)->vgId = pTask->nodeId;

  STransAction action = {0};
  memcpy(&action.epSet, &pTask->epSet, sizeof(SEpSet));
  action.pCont = buf;
  action.contLen = sizeof(SMsgHead) + len;
  action.msgType = TDMT_STREAM_TASK_RECOVER;
  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(buf);
    return -1;
  }
  return 0;
}

int32_t mndRecoverStreamTasks(SMnode *pMnode, STrans *pTrans, SStreamObj *pStream) {
  if (pStream->isDistributed) {
    int32_t lv = taosArrayGetSize(pStream->tasks);
    for (int32_t i = 0; i < lv; i++) {
      SArray      *pTasks = taosArrayGetP(pStream->tasks, i);
      int32_t      sz = taosArrayGetSize(pTasks);
      SStreamTask *pTask = taosArrayGetP(pTasks, 0);
585
      if (pTask->taskLevel == TASK_LEVEL__AGG) {
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
        ASSERT(sz == 1);
        if (mndPersistTaskRecoverReq(pTrans, pTask) < 0) {
          return -1;
        }
      } else {
        continue;
      }
    }
  } else {
    int32_t lv = taosArrayGetSize(pStream->tasks);
    for (int32_t i = 0; i < lv; i++) {
      SArray *pTasks = taosArrayGetP(pStream->tasks, i);
      int32_t sz = taosArrayGetSize(pTasks);
      for (int32_t j = 0; j < sz; j++) {
        SStreamTask *pTask = taosArrayGetP(pTasks, j);
601 602
        if (pTask->taskLevel != TASK_LEVEL__SOURCE) break;
        ASSERT(pTask->taskLevel != TASK_LEVEL__SINK);
603 604 605 606 607 608 609 610
        if (mndPersistTaskRecoverReq(pTrans, pTask) < 0) {
          return -1;
        }
      }
    }
  }
  return 0;
}
L
Liu Jicong 已提交
611
#endif
612

L
Liu Jicong 已提交
613
int32_t mndDropStreamTasks(SMnode *pMnode, STrans *pTrans, SStreamObj *pStream) {
L
Liu Jicong 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626 627
  int32_t lv = taosArrayGetSize(pStream->tasks);
  for (int32_t i = 0; i < lv; i++) {
    SArray *pTasks = taosArrayGetP(pStream->tasks, i);
    int32_t sz = taosArrayGetSize(pTasks);
    for (int32_t j = 0; j < sz; j++) {
      SStreamTask *pTask = taosArrayGetP(pTasks, j);
      if (mndPersistTaskDropReq(pTrans, pTask) < 0) {
        return -1;
      }
    }
  }
  return 0;
}

S
Shengliang Guan 已提交
628 629
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq) {
  SMnode            *pMnode = pReq->info.node;
L
Liu Jicong 已提交
630 631 632 633
  int32_t            code = -1;
  SStreamObj        *pStream = NULL;
  SDbObj            *pDb = NULL;
  SCMCreateStreamReq createStreamReq = {0};
634
  SStreamObj         streamObj = {0};
L
Liu Jicong 已提交
635

S
Shengliang Guan 已提交
636
  if (tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, &createStreamReq) != 0) {
L
Liu Jicong 已提交
637
    terrno = TSDB_CODE_INVALID_MSG;
638
    goto _OVER;
L
Liu Jicong 已提交
639 640 641 642 643 644
  }

  mDebug("stream:%s, start to create, sql:%s", createStreamReq.name, createStreamReq.sql);

  if (mndCheckCreateStreamReq(&createStreamReq) != 0) {
    mError("stream:%s, failed to create since %s", createStreamReq.name, terrstr());
645
    goto _OVER;
L
Liu Jicong 已提交
646 647 648 649 650 651 652
  }

  pStream = mndAcquireStream(pMnode, createStreamReq.name);
  if (pStream != NULL) {
    if (createStreamReq.igExists) {
      mDebug("stream:%s, already exist, ignore exist is set", createStreamReq.name);
      code = 0;
653
      goto _OVER;
L
Liu Jicong 已提交
654 655
    } else {
      terrno = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
656
      goto _OVER;
L
Liu Jicong 已提交
657 658
    }
  } else if (terrno != TSDB_CODE_MND_STREAM_NOT_EXIST) {
659
    goto _OVER;
L
Liu Jicong 已提交
660 661
  }

L
Liu Jicong 已提交
662 663
  // build stream obj from request
  if (mndBuildStreamObjFromCreateReq(pMnode, &streamObj, &createStreamReq) < 0) {
664
    /*ASSERT(0);*/
L
Liu Jicong 已提交
665 666 667 668
    mError("stream:%s, failed to create since %s", createStreamReq.name, terrstr());
    goto _OVER;
  }

669 670 671 672 673
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pReq);
  if (pTrans == NULL) {
    mError("stream:%s, failed to create since %s", createStreamReq.name, terrstr());
    goto _OVER;
  }
674
  mndTransSetDbName(pTrans, createStreamReq.sourceDB, streamObj.targetDb); // hack way
675 676
  mDebug("trans:%d, used to create stream:%s", pTrans->id, createStreamReq.name);

L
Liu Jicong 已提交
677
  // create stb for stream
678
  if (mndCreateStbForStream(pMnode, pTrans, &streamObj, pReq->info.conn.user) < 0) {
L
Liu Jicong 已提交
679 680 681 682 683
    mError("trans:%d, failed to create stb for stream %s since %s", pTrans->id, createStreamReq.name, terrstr());
    mndTransDrop(pTrans);
    goto _OVER;
  }

L
Liu Jicong 已提交
684
  // schedule stream task for stream obj
685
  if (mndScheduleStream(pMnode, &streamObj) < 0) {
L
Liu Jicong 已提交
686 687 688 689 690
    mError("stream:%s, failed to schedule since %s", createStreamReq.name, terrstr());
    mndTransDrop(pTrans);
    goto _OVER;
  }

L
Liu Jicong 已提交
691
  // add stream to trans
L
Liu Jicong 已提交
692 693 694 695 696 697
  if (mndPersistStream(pMnode, pTrans, &streamObj) < 0) {
    mError("stream:%s, failed to schedule since %s", createStreamReq.name, terrstr());
    mndTransDrop(pTrans);
    goto _OVER;
  }

698 699 700 701 702 703 704 705 706 707
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_READ_DB, streamObj.sourceDb) != 0) {
    mndTransDrop(pTrans);
    goto _OVER;
  }

  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, streamObj.targetDb) != 0) {
    mndTransDrop(pTrans);
    goto _OVER;
  }

L
Liu Jicong 已提交
708
  // execute creation
L
Liu Jicong 已提交
709 710 711 712 713
  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    goto _OVER;
  }
L
Liu Jicong 已提交
714

L
Liu Jicong 已提交
715 716 717
  mndTransDrop(pTrans);

  code = TSDB_CODE_ACTION_IN_PROGRESS;
L
Liu Jicong 已提交
718

719
_OVER:
S
Shengliang Guan 已提交
720
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
L
Liu Jicong 已提交
721 722 723 724 725 726 727
    mError("stream:%s, failed to create since %s", createStreamReq.name, terrstr());
  }

  mndReleaseStream(pMnode, pStream);
  mndReleaseDb(pMnode, pDb);

  tFreeSCMCreateStreamReq(&createStreamReq);
L
Liu Jicong 已提交
728
  tFreeStreamObj(&streamObj);
L
Liu Jicong 已提交
729 730 731
  return code;
}

L
Liu Jicong 已提交
732 733 734 735 736 737
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) {
  SMnode     *pMnode = pReq->info.node;
  SStreamObj *pStream = NULL;
  /*SDbObj     *pDb = NULL;*/
  /*SUserObj   *pUser = NULL;*/

L
Liu Jicong 已提交
738 739 740 741 742 743
  SMDropStreamReq dropReq = {0};
  if (tDeserializeSMDropStreamReq(pReq->pCont, pReq->contLen, &dropReq) < 0) {
    ASSERT(0);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }
L
Liu Jicong 已提交
744

L
Liu Jicong 已提交
745
  pStream = mndAcquireStream(pMnode, dropReq.name);
L
Liu Jicong 已提交
746 747

  if (pStream == NULL) {
L
Liu Jicong 已提交
748 749
    if (dropReq.igNotExists) {
      mDebug("stream:%s, not exist, ignore not exist is set", dropReq.name);
L
Liu Jicong 已提交
750
      sdbRelease(pMnode->pSdb, pStream);
751
      return 0;
L
Liu Jicong 已提交
752 753 754 755
    } else {
      terrno = TSDB_CODE_MND_STREAM_NOT_EXIST;
      return -1;
    }
L
Liu Jicong 已提交
756 757
  }

758 759
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
    return -1;
L
Liu Jicong 已提交
760 761 762 763
  }

  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pReq);
  if (pTrans == NULL) {
L
Liu Jicong 已提交
764
    mError("stream:%s, failed to drop since %s", dropReq.name, terrstr());
L
Liu Jicong 已提交
765 766
    sdbRelease(pMnode->pSdb, pStream);
    return -1;
L
Liu Jicong 已提交
767
  }
L
Liu Jicong 已提交
768
  mDebug("trans:%d, used to drop stream:%s", pTrans->id, dropReq.name);
L
Liu Jicong 已提交
769 770 771

  // drop all tasks
  if (mndDropStreamTasks(pMnode, pTrans, pStream) < 0) {
L
Liu Jicong 已提交
772
    mError("stream:%s, failed to drop task since %s", dropReq.name, terrstr());
L
Liu Jicong 已提交
773 774
    sdbRelease(pMnode->pSdb, pStream);
    return -1;
L
Liu Jicong 已提交
775 776
  }

L
Liu Jicong 已提交
777
  // drop stream
L
Liu Jicong 已提交
778 779 780 781 782
  if (mndPersistDropStreamLog(pMnode, pTrans, pStream) < 0) {
    sdbRelease(pMnode->pSdb, pStream);
    return -1;
  }

L
Liu Jicong 已提交
783 784 785 786 787 788 789 790 791 792
  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, terrstr());
    sdbRelease(pMnode->pSdb, pStream);
    mndTransDrop(pTrans);
    return -1;
  }

  sdbRelease(pMnode->pSdb, pStream);

  return TSDB_CODE_ACTION_IN_PROGRESS;
L
Liu Jicong 已提交
793 794
}

L
Liu Jicong 已提交
795
#if 0
L
Liu Jicong 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
static int32_t mndProcessRecoverStreamReq(SRpcMsg *pReq) {
  SMnode     *pMnode = pReq->info.node;
  SStreamObj *pStream = NULL;
  /*SDbObj     *pDb = NULL;*/
  /*SUserObj   *pUser = NULL;*/

  SMRecoverStreamReq recoverReq = {0};
  if (tDeserializeSMRecoverStreamReq(pReq->pCont, pReq->contLen, &recoverReq) < 0) {
    ASSERT(0);
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  pStream = mndAcquireStream(pMnode, recoverReq.name);

  if (pStream == NULL) {
    if (recoverReq.igNotExists) {
      mDebug("stream:%s, not exist, ignore not exist is set", recoverReq.name);
      sdbRelease(pMnode->pSdb, pStream);
      return 0;
    } else {
      terrno = TSDB_CODE_MND_STREAM_NOT_EXIST;
      return -1;
    }
  }

  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
    return -1;
  }

  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pReq);
  if (pTrans == NULL) {
    mError("stream:%s, failed to recover since %s", recoverReq.name, terrstr());
    sdbRelease(pMnode->pSdb, pStream);
    return -1;
  }
  mDebug("trans:%d, used to drop stream:%s", pTrans->id, recoverReq.name);

  // broadcast to recover all tasks
835
  if (mndRecoverStreamTasks(pMnode, pTrans, pStream) < 0) {
L
Liu Jicong 已提交
836 837 838 839 840 841
    mError("stream:%s, failed to recover task since %s", recoverReq.name, terrstr());
    sdbRelease(pMnode->pSdb, pStream);
    return -1;
  }

  // update stream status
842
  if (mndSetStreamRecover(pMnode, pTrans, pStream) < 0) {
L
Liu Jicong 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
    sdbRelease(pMnode->pSdb, pStream);
    return -1;
  }

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare recover stream trans since %s", pTrans->id, terrstr());
    sdbRelease(pMnode->pSdb, pStream);
    mndTransDrop(pTrans);
    return -1;
  }

  sdbRelease(pMnode->pSdb, pStream);

  return TSDB_CODE_ACTION_IN_PROGRESS;
}
L
Liu Jicong 已提交
858
#endif
L
Liu Jicong 已提交
859

L
Liu Jicong 已提交
860 861
int32_t mndDropStreamByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
  SSdb *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
862
  void *pIter = NULL;
L
Liu Jicong 已提交
863 864

  while (1) {
S
Shengliang Guan 已提交
865
    SStreamObj *pStream = NULL;
L
Liu Jicong 已提交
866 867 868 869 870 871
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
    if (pIter == NULL) break;

    if (pStream->sourceDbUid == pDb->uid || pStream->targetDbUid == pDb->uid) {
      if (pStream->sourceDbUid != pStream->targetDbUid) {
        sdbRelease(pSdb, pStream);
S
Shengliang Guan 已提交
872 873 874
        sdbCancelFetch(pSdb, pIter);
        mError("db:%s, failed to drop stream:%s since sourceDbUid:%" PRId64 " not match with targetDbUid:%" PRId64,
               pDb->name, pStream->name, pStream->sourceDbUid, pStream->targetDbUid);
875
        terrno = TSDB_CODE_MND_STREAM_MUST_BE_DELETED;
L
Liu Jicong 已提交
876 877
        return -1;
      } else {
878 879 880 881 882 883 884 885
#if 0
        if (mndDropStreamTasks(pMnode, pTrans, pStream) < 0) {
          mError("stream:%s, failed to drop task since %s", pStream->name, terrstr());
          sdbRelease(pMnode->pSdb, pStream);
          sdbCancelFetch(pSdb, pIter);
          return -1;
        }
#endif
L
Liu Jicong 已提交
886 887
        if (mndPersistDropStreamLog(pMnode, pTrans, pStream) < 0) {
          sdbRelease(pSdb, pStream);
S
Shengliang Guan 已提交
888
          sdbCancelFetch(pSdb, pIter);
L
Liu Jicong 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
          return -1;
        }
      }
    }

#if 0
    if (mndSetDropOffsetStreamLogs(pMnode, pTrans, pStream) < 0) {
      sdbRelease(pSdb, pStream);
      goto END;
    }
#endif

    sdbRelease(pSdb, pStream);
  }

  return 0;
}

L
Liu Jicong 已提交
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
static int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams) {
  SSdb   *pSdb = pMnode->pSdb;
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

  int32_t numOfStreams = 0;
  void   *pIter = NULL;
  while (1) {
    SStreamObj *pStream = NULL;
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
    if (pIter == NULL) break;

L
Liu Jicong 已提交
922
    if (pStream->sourceDbUid == pDb->uid) {
L
Liu Jicong 已提交
923 924 925 926 927 928 929 930 931 932 933
      numOfStreams++;
    }

    sdbRelease(pSdb, pStream);
  }

  *pNumOfStreams = numOfStreams;
  mndReleaseDb(pMnode, pDb);
  return 0;
}

S
Shengliang Guan 已提交
934 935
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode     *pMnode = pReq->info.node;
L
Liu Jicong 已提交
936 937 938 939 940
  SSdb       *pSdb = pMnode->pSdb;
  int32_t     numOfRows = 0;
  SStreamObj *pStream = NULL;

  while (numOfRows < rows) {
L
Liu Jicong 已提交
941
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
L
Liu Jicong 已提交
942 943
    if (pShow->pIter == NULL) break;

L
Liu Jicong 已提交
944 945 946
    SColumnInfoData *pColInfo;
    SName            n;
    int32_t          cols = 0;
L
Liu Jicong 已提交
947

L
Liu Jicong 已提交
948 949 950 951 952 953
    char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&n, pStream->name, T_NAME_ACCT | T_NAME_DB);
    tNameGetDbName(&n, varDataVal(streamName));
    varDataSetLen(streamName, strlen(varDataVal(streamName)));
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)streamName, false);
L
Liu Jicong 已提交
954

L
Liu Jicong 已提交
955 956
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
L
Liu Jicong 已提交
957

L
Liu Jicong 已提交
958 959 960 961 962
    char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
    tstrncpy(&sql[VARSTR_HEADER_SIZE], pStream->sql, TSDB_SHOW_SQL_LEN);
    varDataSetLen(sql, strlen(&sql[VARSTR_HEADER_SIZE]));
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)sql, false);
L
Liu Jicong 已提交
963

L
Liu Jicong 已提交
964 965 966
    char status[20 + VARSTR_HEADER_SIZE] = {0};
    mndShowStreamStatus(&status[VARSTR_HEADER_SIZE], pStream);
    varDataSetLen(status, strlen(varDataVal(status)));
L
Liu Jicong 已提交
967
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
L
Liu Jicong 已提交
968
    colDataAppend(pColInfo, numOfRows, (const char *)&status, false);
L
Liu Jicong 已提交
969

970 971 972 973
    char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&n, pStream->sourceDb, T_NAME_ACCT | T_NAME_DB);
    tNameGetDbName(&n, varDataVal(sourceDB));
    varDataSetLen(sourceDB, strlen(varDataVal(sourceDB)));
L
Liu Jicong 已提交
974
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
975
    colDataAppend(pColInfo, numOfRows, (const char *)&sourceDB, false);
L
Liu Jicong 已提交
976

977 978 979 980
    char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&n, pStream->targetDb, T_NAME_ACCT | T_NAME_DB);
    tNameGetDbName(&n, varDataVal(targetDB));
    varDataSetLen(targetDB, strlen(varDataVal(targetDB)));
L
Liu Jicong 已提交
981
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
982
    colDataAppend(pColInfo, numOfRows, (const char *)&targetDB, false);
L
Liu Jicong 已提交
983

984 985 986 987 988 989 990 991 992 993 994
    if (pStream->targetSTbName[0] == 0) {
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataAppend(pColInfo, numOfRows, NULL, true);
    } else {
      char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
      tNameFromString(&n, pStream->targetSTbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
      strcpy(&targetSTB[VARSTR_HEADER_SIZE], tNameGetTableName(&n));
      varDataSetLen(targetSTB, strlen(varDataVal(targetSTB)));
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataAppend(pColInfo, numOfRows, (const char *)&targetSTB, false);
    }
L
Liu Jicong 已提交
995 996

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
997
    colDataAppend(pColInfo, numOfRows, (const char *)&pStream->watermark, false);
L
Liu Jicong 已提交
998

L
Liu Jicong 已提交
999 1000 1001
    char trigger[20 + VARSTR_HEADER_SIZE] = {0};
    mndShowStreamTrigger(&trigger[VARSTR_HEADER_SIZE], pStream);
    varDataSetLen(trigger, strlen(varDataVal(trigger)));
L
Liu Jicong 已提交
1002
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
L
Liu Jicong 已提交
1003
    colDataAppend(pColInfo, numOfRows, (const char *)&trigger, false);
L
Liu Jicong 已提交
1004 1005 1006

    numOfRows++;
    sdbRelease(pSdb, pStream);
L
Liu Jicong 已提交
1007
  }
L
Liu Jicong 已提交
1008 1009 1010

  pShow->numOfRows += numOfRows;
  return numOfRows;
L
Liu Jicong 已提交
1011 1012 1013 1014 1015 1016
}

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