• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4910

30 Dec 2025 10:52AM UTC coverage: 65.864% (+0.3%) from 65.542%
#4910

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

999 existing lines in 108 files now uncovered.

194877 of 295877 relevant lines covered (65.86%)

121300574.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.0
/source/dnode/mnode/impl/src/mndStream.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "mndStream.h"
17
#include "audit.h"
18
#include "mndDb.h"
19
#include "mndPrivilege.h"
20
#include "mndShow.h"
21
#include "mndStb.h"
22
#include "mndTrans.h"
23
#include "mndUser.h"
24
#include "osMemory.h"
25
#include "parser.h"
26
#include "taoserror.h"
27
#include "tmisce.h"
28
#include "tname.h"
29

30
#define MND_STREAM_MAX_NUM 100000
31

32
typedef struct {
33
  int8_t placeHolder;  // // to fix windows compile error, define place holder
34
} SMStreamNodeCheckMsg;
35

36
static int32_t  mndNodeCheckSentinel = 0;
37
SStmRuntime  mStreamMgmt = {0};
38

39
static int32_t mndStreamActionInsert(SSdb *pSdb, SStreamObj *pStream);
40
static int32_t mndStreamActionDelete(SSdb *pSdb, SStreamObj *pStream);
41
static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pOldStream, SStreamObj *pNewStream);
42
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq);
43

44
static int32_t mndProcessCreateStreamReqFromMNode(SRpcMsg *pReq);
45
static int32_t mndProcessDropStreamReqFromMNode(SRpcMsg *pReq);
46

47
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
48
static void    mndCancelGetNextStream(SMnode *pMnode, void *pIter);
49
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
50
static void    mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter);
51
static int32_t mndProcessStopStreamReq(SRpcMsg *pReq);
52
static int32_t mndProcessStartStreamReq(SRpcMsg *pReq);
53

54
static SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw);
55

56
SSdbRaw       *mndStreamSeqActionEncode(SStreamObj *pStream);
57
SSdbRow       *mndStreamSeqActionDecode(SSdbRaw *pRaw);
58
static int32_t mndStreamSeqActionInsert(SSdb *pSdb, SStreamSeq *pStream);
59
static int32_t mndStreamSeqActionDelete(SSdb *pSdb, SStreamSeq *pStream);
60
static int32_t mndStreamSeqActionUpdate(SSdb *pSdb, SStreamSeq *pOldStream, SStreamSeq *pNewStream);
61
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq);
62

63
void mndCleanupStream(SMnode *pMnode) {
382,342✔
64
  mDebug("try to clean up stream");
382,342✔
65
  
66
  msmHandleBecomeNotLeader(pMnode);
382,342✔
67
  
68
  mDebug("mnd stream runtime info cleanup");
382,342✔
69
}
382,342✔
70

71
SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw) {
140,664✔
72
  int32_t     code = 0;
140,664✔
73
  int32_t     lino = 0;
140,664✔
74
  SSdbRow    *pRow = NULL;
140,664✔
75
  SStreamObj *pStream = NULL;
140,664✔
76
  void       *buf = NULL;
140,664✔
77
  int8_t      sver = 0;
140,664✔
78
  int32_t     tlen;
140,293✔
79
  int32_t     dataPos = 0;
140,664✔
80

81
  code = sdbGetRawSoftVer(pRaw, &sver);
140,664✔
82
  TSDB_CHECK_CODE(code, lino, _over);
140,664✔
83

84
  if (sver != MND_STREAM_VER_NUMBER && sver != MND_STREAM_COMPATIBLE_VER_NUMBER) {
140,664✔
85
    mError("stream read invalid ver, data ver: %d, curr ver: %d", sver, MND_STREAM_VER_NUMBER);
×
86
    goto _over;
×
87
  }
88

89
  pRow = sdbAllocRow(sizeof(SStreamObj));
140,664✔
90
  TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
140,664✔
91

92
  pStream = sdbGetRowObj(pRow);
140,664✔
93
  TSDB_CHECK_NULL(pStream, code, lino, _over, terrno);
140,664✔
94

95
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
140,664✔
96

97
  buf = taosMemoryMalloc(tlen + 1);
140,664✔
98
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
140,664✔
99

100
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
140,664✔
101

102
  SDecoder decoder;
140,293✔
103
  tDecoderInit(&decoder, buf, tlen + 1);
140,664✔
104
  code = tDecodeSStreamObj(&decoder, pStream, sver);
140,664✔
105
  tDecoderClear(&decoder);
140,664✔
106

107
  if (code < 0) {
140,664✔
108
    tFreeStreamObj(pStream);
×
109
  }
110

111
_over:
140,664✔
112
  taosMemoryFreeClear(buf);
140,664✔
113

114
  if (code != TSDB_CODE_SUCCESS) {
140,664✔
115
    char *p = (pStream == NULL || NULL == pStream->pCreate) ? "null" : pStream->pCreate->name;
×
116
    mError("stream:%s, failed to decode from raw:%p since %s at:%d", p, pRaw, tstrerror(code), lino);
×
117
    taosMemoryFreeClear(pRow);
×
118

119
    terrno = code;
×
120
    return NULL;
×
121
  } else {
122
    mTrace("stream:%s, decode from raw:%p, row:%p", pStream->pCreate->name, pRaw, pStream);
140,664✔
123

124
    terrno = 0;
140,664✔
125
    return pRow;
140,664✔
126
  }
127
}
128

129
static int32_t mndStreamActionInsert(SSdb *pSdb, SStreamObj *pStream) {
129,095✔
130
  mTrace("stream:%s, perform insert action", pStream->pCreate->name);
129,095✔
131
  return 0;
129,095✔
132
}
133

134
static int32_t mndStreamActionDelete(SSdb *pSdb, SStreamObj *pStream) {
140,664✔
135
  mInfo("stream:%s, perform delete action", pStream->pCreate->name);
140,664✔
136
  tFreeStreamObj(pStream);
140,664✔
137
  return 0;
140,664✔
138
}
139

140
static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pOldStream, SStreamObj *pNewStream) {
3,126✔
141
  mTrace("stream:%s, perform update action", pOldStream->pCreate->name);
3,126✔
142

143
  atomic_store_32(&pOldStream->mainSnodeId, pNewStream->mainSnodeId);
3,126✔
144
  atomic_store_8(&pOldStream->userStopped, atomic_load_8(&pNewStream->userStopped));
3,126✔
145
  pOldStream->ownerId = pNewStream->ownerId;
3,126✔
146
  pOldStream->updateTime = pNewStream->updateTime;
3,126✔
147
  
148
  return 0;
3,126✔
149
}
150

151
int32_t mndAcquireStream(SMnode *pMnode, char *streamName, SStreamObj **pStream) {
287,387✔
152
  int32_t code = 0;
287,387✔
153
  SSdb   *pSdb = pMnode->pSdb;
287,387✔
154
  (*pStream) = sdbAcquire(pSdb, SDB_STREAM, streamName);
287,387✔
155
  if ((*pStream) == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
287,387✔
156
    code = TSDB_CODE_MND_STREAM_NOT_EXIST;
134,363✔
157
  }
158
  return code;
287,387✔
159
}
160

161
static bool mndStreamGetNameFromId(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
×
162
  SStreamObj* pStream = pObj;
×
163

164
  if (pStream->pCreate->streamId == *(int64_t*)p1) {
×
165
    strncpy((char*)p2, pStream->name, TSDB_STREAM_NAME_LEN);
×
166
    return false;
×
167
  }
168

169
  return true;
×
170
}
171

172
int32_t mndAcquireStreamById(SMnode *pMnode, int64_t streamId, SStreamObj **pStream) {
×
173
  int32_t code = 0;
×
174
  SSdb   *pSdb = pMnode->pSdb;
×
175
  char streamName[TSDB_STREAM_NAME_LEN];
×
176
  streamName[0] = 0;
×
177
  
178
  sdbTraverse(pSdb, SDB_STREAM, mndStreamGetNameFromId, &streamId, streamName, NULL);
×
179
  if (streamName[0]) {
×
180
    (*pStream) = sdbAcquire(pSdb, SDB_STREAM, streamName);
×
181
    if ((*pStream) == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
×
182
      code = TSDB_CODE_MND_STREAM_NOT_EXIST;
×
183
    }
184
  }
185
  
186
  return code;
×
187
}
188

189
void mndReleaseStream(SMnode *pMnode, SStreamObj *pStream) {
135,537✔
190
  SSdb *pSdb = pMnode->pSdb;
135,537✔
191
  sdbRelease(pSdb, pStream);
135,537✔
192
}
135,537✔
193

194
SSdbRaw *mndStreamSeqActionEncode(SStreamObj *pStream) { return NULL; }
×
195
SSdbRow *mndStreamSeqActionDecode(SSdbRaw *pRaw) { return NULL; }
×
196
int32_t  mndStreamSeqActionInsert(SSdb *pSdb, SStreamSeq *pStream) { return 0; }
×
197
int32_t  mndStreamSeqActionDelete(SSdb *pSdb, SStreamSeq *pStream) { return 0; }
×
198
int32_t  mndStreamSeqActionUpdate(SSdb *pSdb, SStreamSeq *pOldStream, SStreamSeq *pNewStream) { return 0; }
×
199

200
static void mndStreamBuildObj(SMnode *pMnode, SStreamObj *pObj, SCMCreateStreamReq *pCreate, SUserObj *pOperUser,
128,330✔
201
                              int32_t snodeId) {
202
  int32_t code = 0;
128,330✔
203

204
  pObj->pCreate = pCreate;
128,330✔
205
  strncpy(pObj->name, pCreate->name, TSDB_STREAM_FNAME_LEN);
128,330✔
206
  (void)snprintf(pObj->createUser, sizeof(pObj->createUser), "%s", pOperUser->name);
128,330✔
207
  pObj->ownerId = pOperUser->uid;
128,330✔
208
  pObj->mainSnodeId = snodeId;
128,330✔
209

210
  pObj->userDropped = 0;
128,330✔
211
  pObj->userStopped = 0;
128,330✔
212

213
  pObj->createTime = taosGetTimestampMs();
128,330✔
214
  pObj->updateTime = pObj->createTime;
128,330✔
215

216
  mstLogSStreamObj("create stream", pObj);
128,330✔
217
}
128,330✔
218

219
static int32_t mndStreamCreateOutStb(SMnode *pMnode, STrans *pTrans, const SCMCreateStreamReq *pStream, const char *user) {
52,553✔
220
  SStbObj *pStb = NULL;
52,553✔
221
  SDbObj  *pDb = NULL;
52,553✔
222
  int32_t  code = 0;
52,553✔
223
  int32_t  lino = 0;
52,553✔
224

225
  SMCreateStbReq createReq = {0};
52,553✔
226
  TAOS_STRNCAT(createReq.name, pStream->outDB, TSDB_DB_FNAME_LEN);
52,553✔
227
  TAOS_STRNCAT(createReq.name, ".", 2);
52,553✔
228
  TAOS_STRNCAT(createReq.name,  pStream->outTblName, TSDB_TABLE_NAME_LEN);
52,553✔
229
  createReq.numOfColumns = taosArrayGetSize(pStream->outCols);
52,553✔
230
  createReq.numOfTags = pStream->outTags ? taosArrayGetSize(pStream->outTags) : 1;
52,553✔
231
  createReq.pColumns = taosArrayInit_s(sizeof(SFieldWithOptions), createReq.numOfColumns);
52,553✔
232
  TSDB_CHECK_NULL(createReq.pColumns, code, lino, _OVER, terrno);
52,553✔
233

234
  // build fields
235
  for (int32_t i = 0; i < createReq.numOfColumns; i++) {
278,524✔
236
    SFieldWithOptions *pField = taosArrayGet(createReq.pColumns, i);
225,971✔
237
    TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno);
225,971✔
238
    SFieldWithOptions *pSrc = taosArrayGet(pStream->outCols, i);
225,971✔
239

240
    tstrncpy(pField->name, pSrc->name, TSDB_COL_NAME_LEN);
225,971✔
241
    pField->flags = pSrc->flags;
225,971✔
242
    pField->type = pSrc->type;
225,971✔
243
    pField->bytes = pSrc->bytes;
225,971✔
244
    pField->compress = createDefaultColCmprByType(pField->type);
225,971✔
245
    if (IS_DECIMAL_TYPE(pField->type)) {
225,971✔
246
      pField->typeMod = pSrc->typeMod;
×
247
      pField->flags |= COL_HAS_TYPE_MOD;
×
248
    }
249
  }
250

251
  if (NULL == pStream->outTags) {
52,553✔
252
    createReq.numOfTags = 1;
×
253
    createReq.pTags = taosArrayInit_s(sizeof(SField), 1);
×
254
    TSDB_CHECK_NULL(createReq.pTags, code, lino, _OVER, terrno);
×
255

256
    // build tags
257
    SField *pField = taosArrayGet(createReq.pTags, 0);
×
258
    TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno);
×
259

260
    tstrncpy(pField->name, "group_id", sizeof(pField->name));
×
261
    pField->type = TSDB_DATA_TYPE_UBIGINT;
×
262
    pField->flags = 0;
×
263
    pField->bytes = 8;
×
264
  } else {
265
    createReq.numOfTags = taosArrayGetSize(pStream->outTags);
52,553✔
266
    createReq.pTags = taosArrayInit_s(sizeof(SField), createReq.numOfTags);
52,553✔
267
    TSDB_CHECK_NULL(createReq.pTags, code, lino, _OVER, terrno);
52,553✔
268

269
    for (int32_t i = 0; i < createReq.numOfTags; i++) {
134,965✔
270
      SField *pField = taosArrayGet(createReq.pTags, i);
82,412✔
271
      if (pField == NULL) {
82,412✔
272
        continue;
×
273
      }
274

275
      TAOS_FIELD_E *pSrc = taosArrayGet(pStream->outTags, i);
82,412✔
276
      pField->bytes = pSrc->bytes;
82,412✔
277
      pField->flags = 0;
82,412✔
278
      pField->type = pSrc->type;
82,412✔
279
      tstrncpy(pField->name, pSrc->name, TSDB_COL_NAME_LEN);
82,412✔
280
    }
281
  }
282

283
  if ((code = mndCheckCreateStbReq(&createReq)) != 0) {
52,553✔
284
    goto _OVER;
×
285
  }
286

287
  pStb = mndAcquireStb(pMnode, createReq.name);
52,553✔
288
  if (pStb != NULL) {
52,553✔
289
    code = TSDB_CODE_MND_STB_ALREADY_EXIST;
×
290
    goto _OVER;
×
291
  }
292

293
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
52,553✔
294
  if (pDb == NULL) {
52,553✔
295
    code = TSDB_CODE_MND_DB_NOT_SELECTED;
×
296
    goto _OVER;
×
297
  }
298

299
  int32_t numOfStbs = -1;
52,553✔
300
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
52,553✔
301
    goto _OVER;
×
302
  }
303

304
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
52,553✔
305
    code = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
×
306
    goto _OVER;
×
307
  }
308

309
  SStbObj stbObj = {0};
52,553✔
310

311
  if (mndBuildStbFromReq(pMnode, &stbObj, &createReq, pDb) != 0) {
52,553✔
312
    goto _OVER;
×
313
  }
314

315
  stbObj.uid = pStream->outStbUid;
52,553✔
316

317
  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) {
52,553✔
318
    mndFreeStb(&stbObj);
×
319
    goto _OVER;
×
320
  }
321

322
  mDebug("stream:%s create dst stable:%s, cols:%d", pStream->name, pStream->outTblName, createReq.numOfColumns);
52,553✔
323

324
  tFreeSMCreateStbReq(&createReq);
52,553✔
325
  mndFreeStb(&stbObj);
52,553✔
326
  mndReleaseStb(pMnode, pStb);
52,553✔
327
  mndReleaseDb(pMnode, pDb);
52,553✔
328
  return code;
52,553✔
329

330
_OVER:
×
331
  tFreeSMCreateStbReq(&createReq);
×
332
  mndReleaseStb(pMnode, pStb);
×
333
  mndReleaseDb(pMnode, pDb);
×
334

335
  mDebug("stream:%s failed to create dst stable:%s, line:%d code:%s", pStream->name, pStream->outTblName, lino,
×
336
         tstrerror(code));
337
  return code;
×
338
}
339

340
static int32_t mndStreamValidateCreate(SMnode *pMnode, SUserObj* pOperUser, SCMCreateStreamReq* pCreate) {
128,330✔
341
  int32_t code = 0, lino = 0;
128,330✔
342
  int64_t streamId = pCreate->streamId;
128,330✔
343
  char   *pUser = pOperUser->name;
128,330✔
344
  char    objFName[TSDB_PRIV_MAX_KEY_LEN] = {0};
128,330✔
345

346
  (void)snprintf(objFName, sizeof(objFName), "%d.*", pOperUser->acctId);
128,330✔
347

348
  if (pCreate->streamDB) {
128,330✔
349
    // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_WRITE_DB, pCreate->streamDB);
350
    code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, pCreate->streamDB, NULL);
128,330✔
351
    if (code) {
128,330✔
352
      mstsError("user %s failed to create stream %s in db %s since %s", pUser, pCreate->name, pCreate->streamDB,
×
353
                tstrerror(code));
354
    }
355
    TSDB_CHECK_CODE(code, lino, _OVER);
128,330✔
356
  }
357

358
  if (pCreate->triggerDB) {
128,330✔
359
    // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_READ_DB, pCreate->triggerDB);
360
    code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, pCreate->triggerDB, NULL);
127,448✔
361
    if (code) {
127,448✔
362
      mstsError("user %s failed to create stream %s using trigger db %s since %s", pUser, pCreate->name,
×
363
                pCreate->triggerDB, tstrerror(code));
364
    }
365
    TSDB_CHECK_CODE(code, lino, _OVER);
127,448✔
366
#if 0  // TODO check the owner of trigger table
367
    if (pCreate->triggerTblName) {
368
      // check trigger table privilege
369
      code = mndCheckObjPrivilegeRecF(pMnode, pUser, PRIV_TBL_SELECT, "", pCreate->triggerDB, pCreate->triggerTblName);
370
      if (code) {
371
        mstsError("user %s failed to create stream %s using trigger table %s.%s since %s", pUser, pCreate->name,
372
                  pCreate->triggerDB, pCreate->triggerTblName, tstrerror(code));
373
      }
374
      TSDB_CHECK_CODE(code, lino, _OVER);
375
    }
376
#endif
377
  }
378

379
  if (pCreate->calcDB) {
128,330✔
380
    int32_t dbNum = taosArrayGetSize(pCreate->calcDB);
125,407✔
381
    for (int32_t i = 0; i < dbNum; ++i) {
250,814✔
382
      char *calcDB = taosArrayGetP(pCreate->calcDB, i);
125,407✔
383
      // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_READ_DB, calcDB);
384
      code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, calcDB, NULL);
125,407✔
385
      if (code) {
125,407✔
386
        mstsError("user %s failed to create stream %s using calcDB %s since %s", pUser, pCreate->name, calcDB,
×
387
                  tstrerror(code));
388
      }
389
      TSDB_CHECK_CODE(code, lino, _OVER);
125,407✔
390
    }
391
  }
392

393
  if (pCreate->outDB) {
128,330✔
394
    // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_WRITE_DB, pCreate->outDB);
395
    code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, pCreate->outDB, NULL);
125,407✔
396
    if (code) {
125,407✔
397
      mstsError("user %s failed to create stream %s using out db %s since %s", pUser, pCreate->name, pCreate->outDB,
×
398
                tstrerror(code));
399
    }
400
    TSDB_CHECK_CODE(code, lino, _OVER);
125,407✔
401
  }
402

403
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
128,330✔
404
  if (streamNum > MND_STREAM_MAX_NUM) {
128,330✔
405
    code = TSDB_CODE_MND_TOO_MANY_STREAMS;
×
406
    mstsError("failed to create stream %s since %s, stream number:%d", pCreate->name, tstrerror(code), streamNum);
×
407
    return code;
×
408
  }
409

410
_OVER:
128,330✔
411

412
  return code;
128,330✔
413
}
414

415
int32_t mndDropStreamByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
575,632✔
416
  SSdb   *pSdb = pMnode->pSdb;
575,632✔
417
  void   *pIter = NULL;
575,632✔
418
  int32_t code = 0;
575,632✔
419

420
  while (1) {
26,566✔
421
    SStreamObj *pStream = NULL;
602,198✔
422
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
602,198✔
423
    if (pIter == NULL) break;
602,198✔
424

425
    if (0 == strcmp(pStream->pCreate->streamDB, pDb->name)) {
26,566✔
426
      mInfo("start to drop stream %s in db %s", pStream->pCreate->name, pDb->name);
3,141✔
427
      
428
      pStream->updateTime = taosGetTimestampMs();
6,282✔
429
      
430
      atomic_store_8(&pStream->userDropped, 1);
3,141✔
431
      
432
      MND_STREAM_SET_LAST_TS(STM_EVENT_DROP_STREAM, pStream->updateTime);
3,141✔
433
      
434
      msmUndeployStream(pMnode, pStream->pCreate->streamId, pStream->pCreate->name);
3,141✔
435
      
436
      // drop stream
437
      code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_DROPPED);
3,141✔
438
      if (code) {
3,141✔
439
        mError("drop db trans:%d failed to append drop stream trans since %s", pTrans->id, tstrerror(code));
×
440
        sdbRelease(pSdb, pStream);
×
441
        sdbCancelFetch(pSdb, pIter);
×
442
        TAOS_RETURN(code);
×
443
      }
444
    }
445

446
    sdbRelease(pSdb, pStream);
26,566✔
447
  }
448

449
  return 0;
575,632✔
450
}
451

452
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
178,107✔
453
  SMnode     *pMnode = pReq->info.node;
178,107✔
454
  SSdb       *pSdb = pMnode->pSdb;
178,107✔
455
  int32_t     numOfRows = 0;
178,107✔
456
  SStreamObj *pStream = NULL;
178,107✔
457
  SUserObj   *pOperUser = NULL;
178,107✔
458
  int32_t     code = 0, lino = 0;
178,107✔
459
  bool        showAll = false;
178,107✔
460

461
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser));
178,107✔
462
  showAll =
178,107✔
463
      (0 == mndCheckObjPrivilegeRec(pMnode, pOperUser, PRIV_CM_SHOW, PRIV_OBJ_STREAM, 0, pOperUser->acctId, "*", "*"));
178,107✔
464

465
  while (numOfRows < rows) {
541,762✔
466
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
541,762✔
467
    if (pShow->pIter == NULL) break;
541,762✔
468

469
    if (!showAll) {
363,655✔
470
      if ((mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_SHOW, PRIV_OBJ_STREAM, pStream->ownerId,
×
471
                                    pStream->pCreate->streamDB, pStream->pCreate->name))) {
×
472
        sdbRelease(pSdb, pStream);
×
473
        continue;
×
474
      }
475
    }
476

477
    code = mstSetStreamAttrResBlock(pMnode, pStream, pBlock, numOfRows);
363,655✔
478
    if (code == 0) {
363,655✔
479
      numOfRows++;
363,655✔
480
    }
481
    sdbRelease(pSdb, pStream);
363,655✔
482
  }
483
  code = 0;
178,107✔
484
  pShow->numOfRows += numOfRows;
178,107✔
485
_exit:
178,107✔
486
  mndReleaseUser(pMnode, pOperUser);
178,107✔
487
  if (code != 0) {
178,107✔
488
    mError("failed to retrieve stream list at line %d since %s", lino, tstrerror(code));
×
489
    TAOS_RETURN(code);
×
490
  }
491
  return numOfRows;
178,107✔
492
}
493

494
static void mndCancelGetNextStream(SMnode *pMnode, void *pIter) {
×
495
  SSdb *pSdb = pMnode->pSdb;
×
496
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
497
}
×
498

499
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
161,165✔
500
  SMnode     *pMnode = pReq->info.node;
161,165✔
501
  SSdb       *pSdb = pMnode->pSdb;
161,165✔
502
  int32_t     numOfRows = 0;
161,165✔
503
  SStreamObj *pStream = NULL;
161,165✔
504
  int32_t     code = 0;
161,165✔
505

506
  while (numOfRows < rowsCapacity) {
2,320,023✔
507
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
2,320,023✔
508
    if (pShow->pIter == NULL) {
2,320,023✔
509
      break;
161,165✔
510
    }
511

512
    code = mstSetStreamTasksResBlock(pStream, pBlock, &numOfRows, rowsCapacity);
2,158,858✔
513

514
    sdbRelease(pSdb, pStream);
2,158,858✔
515
  }
516

517
  pShow->numOfRows += numOfRows;
161,165✔
518
  return numOfRows;
161,165✔
519
}
520

521
static void mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter) {
×
522
  SSdb *pSdb = pMnode->pSdb;
×
523
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
524
}
×
525

526
static int32_t mndRetrieveStreamRecalculates(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
230✔
527
  SMnode     *pMnode = pReq->info.node;
230✔
528
  SSdb       *pSdb = pMnode->pSdb;
230✔
529
  int32_t     numOfRows = 0;
230✔
530
  SStreamObj *pStream = NULL;
230✔
531
  int32_t     code = 0;
230✔
532

533
  while (numOfRows < rowsCapacity) {
460✔
534
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
460✔
535
    if (pShow->pIter == NULL) {
460✔
536
      break;
230✔
537
    }
538

539
    code = mstSetStreamRecalculatesResBlock(pStream, pBlock, &numOfRows, rowsCapacity);
230✔
540

541
    sdbRelease(pSdb, pStream);
230✔
542
  }
543

544
  pShow->numOfRows += numOfRows;
230✔
545
  return numOfRows;
230✔
546
}
547

548
static void mndCancelGetNextStreamRecalculates(SMnode *pMnode, void *pIter) {
×
549
  SSdb *pSdb = pMnode->pSdb;
×
550
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
551
}
×
552

553

554
static bool mndStreamUpdateTagsFlag(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
1,880,787✔
555
  SStreamObj *pStream = pObj;
1,880,787✔
556
  if (atomic_load_8(&pStream->userDropped)) {
1,880,787✔
557
    return true;
×
558
  }
559

560
  if (TSDB_SUPER_TABLE != pStream->pCreate->triggerTblType && 
1,880,787✔
561
      TSDB_CHILD_TABLE != pStream->pCreate->triggerTblType && 
1,074,929✔
562
      TSDB_VIRTUAL_CHILD_TABLE != pStream->pCreate->triggerTblType) {
787,047✔
563
    return true;
83,147✔
564
  }
565

566
  if (pStream->pCreate->triggerTblSuid != *(uint64_t*)p1) {
1,797,640✔
567
    return true;
1,769,376✔
568
  }
569

570
  if (NULL == pStream->pCreate->partitionCols) {
28,264✔
571
    return true;
3,750✔
572
  }
573

574
  SNodeList* pList = NULL;
24,514✔
575
  int32_t code = nodesStringToList(pStream->pCreate->partitionCols, &pList);
24,514✔
576
  if (code) {
24,514✔
577
    nodesDestroyList(pList);
×
578
    mstError("partitionCols [%s] nodesStringToList failed with error:%s", (char*)pStream->pCreate->partitionCols, tstrerror(code));
×
579
    return true;
×
580
  }
581

582
  SSchema* pTags = (SSchema*)p2;
24,514✔
583
  int32_t* tagNum = (int32_t*)p3;
24,514✔
584

585
  SNode* pNode = NULL;
24,514✔
586
  FOREACH(pNode, pList) {
55,137✔
587
    SColumnNode* pCol = (SColumnNode*)pNode;
30,623✔
588
    for (int32_t i = 0; i < *tagNum; ++i) {
60,182✔
589
      if (pCol->colId == pTags[i].colId) {
53,944✔
590
        pTags[i].flags |= COL_REF_BY_STM;
24,385✔
591
        break;
24,385✔
592
      }
593
    }
594
  }
595

596
  nodesDestroyList(pList);
24,514✔
597
  
598
  return true;
24,514✔
599
}
600

601

602
void mndStreamUpdateTagsRefFlag(SMnode *pMnode, int64_t suid, SSchema* pTags, int32_t tagNum) {
13,636,517✔
603
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
13,636,517✔
604
  if (streamNum <= 0) {
13,636,517✔
605
    return;
13,532,115✔
606
  }
607

608
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mndStreamUpdateTagsFlag, &suid, pTags, &tagNum);
104,402✔
609
}
610

611
static int32_t mndProcessStopStreamReq(SRpcMsg *pReq) {
1,191✔
612
  SMnode     *pMnode = pReq->info.node;
1,191✔
613
  SStreamObj *pStream = NULL;
1,191✔
614
  SUserObj   *pOperUser = NULL;
1,191✔
615
  int32_t     code = 0;
1,191✔
616

617
  SMPauseStreamReq pauseReq = {0};
1,191✔
618
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
1,191✔
619
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
620
  }
621

622
  code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
1,191✔
623
  if (pStream == NULL || code != 0) {
1,191✔
624
    if (pauseReq.igNotExists) {
×
625
      mInfo("stream:%s, not exist, not stop stream", pauseReq.name);
×
626
      taosMemoryFree(pauseReq.name);
×
627
      return 0;
×
628
    } else {
629
      mError("stream:%s not exist, failed to stop stream", pauseReq.name);
×
630
      taosMemoryFree(pauseReq.name);
×
631
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
632
    }
633
  }
634

635
  taosMemoryFree(pauseReq.name);
1,191✔
636

637
  int64_t streamId = pStream->pCreate->streamId;
1,191✔
638
  
639
  mstsInfo("start to stop stream %s", pStream->name);
1,191✔
640

641
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
642
  if((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
1,191✔
643
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
644
    sdbRelease(pMnode->pSdb, pStream);
×
645
    return code;
×
646
  }
647

648
  if ((code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, pStream->pCreate->streamDB,
1,191✔
649
                                            NULL)) ||
1,191✔
650
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_STOP, PRIV_OBJ_STREAM, pStream->ownerId,
1,191✔
651
                                       pStream->pCreate->streamDB, pStream->pCreate->name))) {
1,191✔
652
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
653
    mndReleaseUser(pMnode, pOperUser);
×
654
    sdbRelease(pMnode->pSdb, pStream);
×
655
    return code;
×
656
  }
657

658
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
1,191✔
659

660
  if (atomic_load_8(&pStream->userDropped)) {
1,191✔
661
    code = TSDB_CODE_MND_STREAM_DROPPING;
×
662
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
663
    sdbRelease(pMnode->pSdb, pStream);
×
664
    return code;
×
665
  }
666

667
  STrans *pTrans = NULL;
1,191✔
668
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_STOP_NAME, &pTrans);
1,191✔
669
  if (pTrans == NULL || code) {
1,191✔
670
    mstsError("failed to stop stream %s since %s", pStream->name, tstrerror(code));
×
671
    sdbRelease(pMnode->pSdb, pStream);
×
672
    return code;
×
673
  }
674

675
  pStream->updateTime = taosGetTimestampMs();
2,382✔
676

677
  atomic_store_8(&pStream->userStopped, 1);
1,191✔
678

679
  MND_STREAM_SET_LAST_TS(STM_EVENT_STOP_STREAM, pStream->updateTime);
1,191✔
680

681
  msmUndeployStream(pMnode, streamId, pStream->name);
1,191✔
682

683
  // stop stream
684
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
1,191✔
685
  if (code != TSDB_CODE_SUCCESS) {
1,191✔
686
    sdbRelease(pMnode->pSdb, pStream);
×
687
    mndTransDrop(pTrans);
×
688
    return code;
×
689
  }
690

691
  code = mndTransPrepare(pMnode, pTrans);
1,191✔
692
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,191✔
693
    mError("trans:%d, failed to prepare stop stream trans since %s", pTrans->id, tstrerror(code));
×
694
    sdbRelease(pMnode->pSdb, pStream);
×
695
    mndTransDrop(pTrans);
×
696
    return code;
×
697
  }
698

699
  sdbRelease(pMnode->pSdb, pStream);
1,191✔
700
  mndTransDrop(pTrans);
1,191✔
701

702
  return TSDB_CODE_ACTION_IN_PROGRESS;
1,191✔
703
}
704

705

706
static int32_t mndProcessStartStreamReq(SRpcMsg *pReq) {
1,191✔
707
  SMnode     *pMnode = pReq->info.node;
1,191✔
708
  SStreamObj *pStream = NULL;
1,191✔
709
  SUserObj   *pOperUser = NULL;
1,191✔
710
  int32_t     code = 0;
1,191✔
711

712
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
1,191✔
713
    return code;
×
714
  }
715

716
  SMResumeStreamReq resumeReq = {0};
1,191✔
717
  if (tDeserializeSMResumeStreamReq(pReq->pCont, pReq->contLen, &resumeReq) < 0) {
1,191✔
718
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
719
  }
720

721
  code = mndAcquireStream(pMnode, resumeReq.name, &pStream);
1,191✔
722
  if (pStream == NULL || code != 0) {
1,191✔
723
    if (resumeReq.igNotExists) {
×
724
      mInfo("stream:%s not exist, not start stream", resumeReq.name);
×
725
      taosMemoryFree(resumeReq.name);
×
726
      sdbRelease(pMnode->pSdb, pStream);
×
727
      return 0;
×
728
    } else {
729
      mError("stream:%s not exist, failed to start stream", resumeReq.name);
×
730
      taosMemoryFree(resumeReq.name);
×
731
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
732
    }
733
  }
734

735
  taosMemoryFree(resumeReq.name);
1,191✔
736

737
  int64_t streamId = pStream->pCreate->streamId;
1,191✔
738

739
  mstsInfo("start to start stream %s from stopped", pStream->name);
1,191✔
740

741
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
742
  if ((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
1,191✔
743
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
744
    sdbRelease(pMnode->pSdb, pStream);
×
745
    return code;
×
746
  }
747

748
  if ((code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, pStream->pCreate->streamDB,
1,191✔
749
                                            NULL)) ||
1,191✔
750
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_START, PRIV_OBJ_STREAM, pStream->ownerId,
1,191✔
751
                                       pStream->pCreate->streamDB, pStream->pCreate->name))) {
1,191✔
752
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
753
    mndReleaseUser(pMnode, pOperUser);
×
754
    sdbRelease(pMnode->pSdb, pStream);
×
755
    return code;
×
756
  }
757

758
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
1,191✔
759

760
  if (atomic_load_8(&pStream->userDropped)) {
1,191✔
761
    code = TSDB_CODE_MND_STREAM_DROPPING;
×
762
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
763
    sdbRelease(pMnode->pSdb, pStream);
×
764
    return code;
×
765
  }
766

767
  if (0 == atomic_load_8(&pStream->userStopped)) {
1,191✔
768
    code = TSDB_CODE_MND_STREAM_NOT_STOPPED;
×
769
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
770
    sdbRelease(pMnode->pSdb, pStream);
×
771
    return code;
×
772
  }
773
  
774
  atomic_store_8(&pStream->userStopped, 0);
1,191✔
775

776
  pStream->updateTime = taosGetTimestampMs();
2,382✔
777

778
  MND_STREAM_SET_LAST_TS(STM_EVENT_START_STREAM, pStream->updateTime);
1,191✔
779

780
  STrans *pTrans = NULL;
1,191✔
781
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_START_NAME, &pTrans);
1,191✔
782
  if (pTrans == NULL || code) {
1,191✔
783
    mstsError("failed to start stream %s since %s", pStream->name, tstrerror(code));
×
784
    sdbRelease(pMnode->pSdb, pStream);
×
785
    return code;
×
786
  }
787

788
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
1,191✔
789
  if (code != TSDB_CODE_SUCCESS) {
1,191✔
790
    mstsError("failed to start stream %s since %s", pStream->name, tstrerror(code));
×
791
    sdbRelease(pMnode->pSdb, pStream);
×
792
    mndTransDrop(pTrans);
×
793
    return code;
×
794
  }
795

796
  code = mndTransPrepare(pMnode, pTrans);
1,191✔
797
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,191✔
798
    mstsError("trans:%d, failed to prepare start stream %s trans since %s", pTrans->id, pStream->name, tstrerror(code));
×
799
    sdbRelease(pMnode->pSdb, pStream);
×
800
    mndTransDrop(pTrans);
×
801
    return code;
×
802
  }
803

804
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->name, NULL, true, STREAM_ACT_DEPLOY);
1,191✔
805

806
  sdbRelease(pMnode->pSdb, pStream);
1,191✔
807
  mndTransDrop(pTrans);
1,191✔
808

809
  return TSDB_CODE_ACTION_IN_PROGRESS;
1,191✔
810
}
811

812
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) {
5,744✔
813
  SMnode     *pMnode = pReq->info.node;
5,744✔
814
  SStreamObj *pStream = NULL;
5,744✔
815
  SUserObj   *pOperUser = NULL;
5,744✔
816
  int32_t     code = 0;
5,744✔
817
  int32_t     notExistNum = 0;
5,744✔
818

819
  SMDropStreamReq dropReq = {0};
5,744✔
820
  int64_t         tss = taosGetTimestampMs();
5,744✔
821
  if (tDeserializeSMDropStreamReq(pReq->pCont, pReq->contLen, &dropReq) < 0) {
5,744✔
822
    mError("invalid drop stream msg recv, discarded");
×
823
    code = TSDB_CODE_INVALID_MSG;
×
824
    TAOS_RETURN(code);
×
825
  }
826

827
  mDebug("recv drop stream msg, count:%d", dropReq.count);
5,744✔
828

829
  // Acquire user object for privilege check
830
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
5,744✔
831
  if (code != 0) {
5,744✔
NEW
832
    tFreeMDropStreamReq(&dropReq);
×
NEW
833
    TAOS_RETURN(code);
×
834
  }
835

836
  // check if all streams exist
837
  if (!dropReq.igNotExists) {
5,744✔
838
    for (int32_t i = 0; i < dropReq.count; i++) {
8,633✔
839
      if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, dropReq.name[i])) {
4,869✔
840
        mError("stream:%s not exist failed to drop it", dropReq.name[i]);
884✔
841
        mndReleaseUser(pMnode, pOperUser);
884✔
842
        tFreeMDropStreamReq(&dropReq);
884✔
843
        TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
884✔
844
      }
845
    }
846
  }
847

848
  // Create a single transaction for all stream drops
849
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pReq, MND_STREAM_DROP_NAME);
4,860✔
850
  if (pTrans == NULL) {
4,860✔
NEW
851
    mError("failed to create drop stream transaction since %s", tstrerror(terrno));
×
NEW
852
    code = terrno;
×
NEW
853
    mndReleaseUser(pMnode, pOperUser);
×
NEW
854
    tFreeMDropStreamReq(&dropReq);
×
NEW
855
    TAOS_RETURN(code);
×
856
  }
857
  pTrans->ableToBeKilled = true;
4,860✔
858

859
  // Process all streams and add them to the transaction
860
  for (int32_t i = 0; i < dropReq.count; i++) {
11,046✔
861
    char *streamName = dropReq.name[i];
6,186✔
862
    mDebug("drop stream[%d/%d]: %s", i + 1, dropReq.count, streamName);
6,186✔
863

864
    code = mndAcquireStream(pMnode, streamName, &pStream);
6,186✔
865
    if (pStream == NULL || code != 0) {
6,186✔
866
      mWarn("stream:%s not exist, ignore not exist is set, drop stream exec done with success", streamName);
884✔
867
      sdbRelease(pMnode->pSdb, pStream);
884✔
868
      pStream = NULL;
884✔
869
      notExistNum++;
884✔
870
      continue;
884✔
871
    }
872

873
    int64_t streamId = pStream->pCreate->streamId;
5,302✔
874

875
    if ((code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, pStream->pCreate->streamDB,
5,302✔
876
                                              NULL)) ||
5,302✔
877
        (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_DROP, PRIV_OBJ_STREAM, pStream->ownerId,
5,302✔
878
                                         pStream->pCreate->streamDB, pStream->pCreate->name))) {
5,302✔
NEW
879
      mstsError("user %s failed to drop stream %s since %s", pReq->info.conn.user, streamName, tstrerror(code));
×
NEW
880
      sdbRelease(pMnode->pSdb, pStream);
×
NEW
881
      pStream = NULL;
×
NEW
882
      mndTransDrop(pTrans);
×
NEW
883
      pTrans = NULL;
×
NEW
884
      goto _OVER;
×
885
    }
886

887
    if (pStream->pCreate->tsmaId != 0) {
5,302✔
NEW
888
      mstsDebug("try to drop tsma related stream, tsmaId:%" PRIx64, pStream->pCreate->tsmaId);
×
889

NEW
890
      void    *pIter = NULL;
×
NEW
891
      SSmaObj *pSma = NULL;
×
UNCOV
892
      pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
×
NEW
893
      while (pIter) {
×
NEW
894
        if (pSma && pSma->uid == pStream->pCreate->tsmaId) {
×
NEW
895
          sdbRelease(pMnode->pSdb, pSma);
×
NEW
896
          sdbRelease(pMnode->pSdb, pStream);
×
NEW
897
          pStream = NULL;
×
898

NEW
899
          sdbCancelFetch(pMnode->pSdb, pIter);
×
NEW
900
          code = TSDB_CODE_TSMA_MUST_BE_DROPPED;
×
901

NEW
902
          mstsError("refused to drop tsma-related stream %s since tsma still exists", streamName);
×
NEW
903
          mndTransDrop(pTrans);
×
NEW
904
          pTrans = NULL;
×
NEW
905
          goto _OVER;
×
906
        }
907

NEW
908
        if (pSma) {
×
NEW
909
          sdbRelease(pMnode->pSdb, pSma);
×
910
        }
911

NEW
912
        pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
×
913
      }
914
    }
915

916
    mstsInfo("start to drop stream %s", pStream->pCreate->name);
5,302✔
917

918
    pStream->updateTime = taosGetTimestampMs();
10,604✔
919

920
    atomic_store_8(&pStream->userDropped, 1);
5,302✔
921

922
    MND_STREAM_SET_LAST_TS(STM_EVENT_DROP_STREAM, pStream->updateTime);
5,302✔
923

924
    msmUndeployStream(pMnode, streamId, pStream->pCreate->name);
5,302✔
925

926
    // Append drop stream operation to the transaction
927
    code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_DROPPED);
5,302✔
928
    if (code) {
5,302✔
NEW
929
      mstsError("trans:%d, failed to append drop stream %s trans since %s", pTrans->id, streamName, tstrerror(code));
×
NEW
930
      sdbRelease(pMnode->pSdb, pStream);
×
NEW
931
      pStream = NULL;
×
932
      // mndStreamTransAppend already called mndTransDrop on failure, set pTrans to NULL to avoid double free
NEW
933
      pTrans = NULL;
×
NEW
934
      goto _OVER;
×
935
    }
936

937
    sdbRelease(pMnode->pSdb, pStream);
5,302✔
938
    pStream = NULL;
5,302✔
939

940
    mstsDebug("drop stream %s added to transaction", streamName);
5,302✔
941
  }
942

943
  // Prepare and execute the transaction for all streams
944
  if (notExistNum < dropReq.count) {
4,860✔
945
    code = mndTransPrepare(pMnode, pTrans);
4,639✔
946
    if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
4,639✔
NEW
947
      mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
NEW
948
      mndTransDrop(pTrans);
×
NEW
949
      goto _OVER;
×
950
    }
951
    mInfo("trans:%d, drop stream transaction prepared for %d streams", pTrans->id, dropReq.count - notExistNum);
4,639✔
952
  } else {
953
    // All streams don't exist, no need to prepare transaction
954
    mndTransDrop(pTrans);
221✔
955
    pTrans = NULL;
221✔
956
  }
957

958
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE && notExistNum < dropReq.count) {
4,860✔
959
    int64_t tse = taosGetTimestampMs();
4,639✔
960
    double  duration = (double)(tse - tss);
4,639✔
961
    duration = duration / 1000;
4,639✔
962
    // Use first stream's database for audit (assuming all streams are from same db in batch)
963
    if (dropReq.count > 0) {
4,639✔
964
      SStreamObj *pFirstStream = NULL;
4,639✔
965
      if (mndAcquireStream(pMnode, dropReq.name[0], &pFirstStream) == 0 && pFirstStream != NULL) {
4,639✔
NEW
966
        auditRecord(pReq, pMnode->clusterId, "dropStream", "", pFirstStream->pCreate->streamDB, NULL, 0, duration, 0);
×
NEW
967
        sdbRelease(pMnode->pSdb, pFirstStream);
×
968
      }
969
    }
970
  }
971

972
  // If any stream was successfully added to transaction, return ACTION_IN_PROGRESS
973
  // Otherwise, all streams don't exist (and igNotExists is set), return SUCCESS
974
  code = (notExistNum < dropReq.count) ? TSDB_CODE_ACTION_IN_PROGRESS : TSDB_CODE_SUCCESS;
4,860✔
975

976
_OVER:
4,860✔
977
  if (pStream) {
4,860✔
NEW
978
    sdbRelease(pMnode->pSdb, pStream);
×
979
  }
980
  if (pTrans) {
4,860✔
981
    mndTransDrop(pTrans);
4,639✔
982
  }
983
  mndReleaseUser(pMnode, pOperUser);
4,860✔
984
  tFreeMDropStreamReq(&dropReq);
4,860✔
985
  TAOS_RETURN(code);
4,860✔
986
}
987

988
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq) {
134,950✔
989
  SMnode     *pMnode = pReq->info.node;
134,950✔
990
  SStreamObj *pStream = NULL;
134,950✔
991
  SStreamObj  streamObj = {0};
134,950✔
992
  SUserObj    *pOperUser = NULL;
134,950✔
993
  int32_t     code = TSDB_CODE_SUCCESS;
134,950✔
994
  int32_t     lino = 0;
134,950✔
995
  STrans     *pTrans = NULL;
134,950✔
996
  uint64_t    streamId = 0;
134,950✔
997
  SCMCreateStreamReq* pCreate = NULL;
134,950✔
998
  int64_t             tss = taosGetTimestampMs();
134,950✔
999

1000
  if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
134,950✔
UNCOV
1001
    goto _OVER;
×
1002
  }
1003
  
1004
#ifdef WINDOWS
1005
  code = TSDB_CODE_MND_INVALID_PLATFORM;
1006
  goto _OVER;
1007
#endif
1008

1009
  pCreate = taosMemoryCalloc(1, sizeof(SCMCreateStreamReq));
134,950✔
1010
  TSDB_CHECK_NULL(pCreate, code, lino, _OVER, terrno);
134,950✔
1011
  
1012
  code = tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, pCreate);
134,950✔
1013
  TSDB_CHECK_CODE(code, lino, _OVER);
134,950✔
1014

1015
  streamId = pCreate->streamId;
134,950✔
1016

1017
  mstsInfo("start to create stream %s, sql:%s", pCreate->name, pCreate->sql);
134,950✔
1018

1019
  int32_t snodeId = msmAssignRandomSnodeId(pMnode, streamId);
134,950✔
1020
  if (!GOT_SNODE(snodeId)) {
134,950✔
1021
    code = terrno;
2,220✔
1022
    TSDB_CHECK_CODE(code, lino, _OVER);
2,220✔
1023
  }
1024
  
1025
  code = mndAcquireStream(pMnode, pCreate->name, &pStream);
132,730✔
1026
  if (pStream != NULL && code == 0) {
132,730✔
1027
    if (pCreate->igExists) {
4,400✔
1028
      mstsInfo("stream %s already exist, ignore exist is set", pCreate->name);
×
1029
    } else {
1030
      code = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
4,400✔
1031
    }
1032

1033
    mndReleaseStream(pMnode, pStream);
4,400✔
1034
    goto _OVER;
4,400✔
1035
  } else if (code != TSDB_CODE_MND_STREAM_NOT_EXIST) {
128,330✔
UNCOV
1036
    goto _OVER;
×
1037
  }
1038

1039
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
128,330✔
1040
  if (pOperUser == NULL) {
128,330✔
UNCOV
1041
    TSDB_CHECK_CODE(TSDB_CODE_MND_NO_USER_FROM_CONN, lino, _OVER);
×
1042
  }
1043

1044
  code = mndStreamValidateCreate(pMnode, pOperUser, pCreate);
128,330✔
1045
  TSDB_CHECK_CODE(code, lino, _OVER);
128,330✔
1046

1047
  mndStreamBuildObj(pMnode, &streamObj, pCreate, pOperUser, snodeId);
128,330✔
1048
  pCreate = NULL;
128,330✔
1049

1050
  pStream = &streamObj;
128,330✔
1051

1052
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_DB, MND_STREAM_CREATE_NAME, &pTrans);
128,330✔
1053
  if (pTrans == NULL || code) {
128,330✔
UNCOV
1054
    goto _OVER;
×
1055
  }
1056

1057
  // create stb for stream
1058
  if (TSDB_SUPER_TABLE == pStream->pCreate->outTblType && !pStream->pCreate->outStbExists) {
128,330✔
1059
    pStream->pCreate->outStbUid = mndGenerateUid(pStream->pCreate->outTblName, strlen(pStream->pCreate->outTblName));
52,553✔
1060
    code = mndStreamCreateOutStb(pMnode, pTrans, pStream->pCreate, RPC_MSG_USER(pReq));
52,553✔
1061
    TSDB_CHECK_CODE(code, lino, _OVER);
52,553✔
1062
  }
1063

1064
  // add stream to trans
1065
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
128,330✔
1066
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
128,330✔
UNCOV
1067
    mstsError("failed to persist stream %s since %s", pStream->pCreate->name, tstrerror(code));
×
UNCOV
1068
    goto _OVER;
×
1069
  }
1070

1071
  // execute creation
1072
  code = mndTransPrepare(pMnode, pTrans);
128,330✔
1073
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
128,330✔
UNCOV
1074
    mstsError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
UNCOV
1075
    goto _OVER;
×
1076
  }
1077
  code = TSDB_CODE_ACTION_IN_PROGRESS;
128,330✔
1078

1079
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE) {
128,330✔
1080
    int64_t tse = taosGetTimestampMs();
128,330✔
1081
    double  duration = (double)(tse - tss);
128,330✔
1082
    duration = duration / 1000;
128,330✔
1083
    auditRecord(pReq, pMnode->clusterId, "createStream", pStream->pCreate->streamDB, pStream->pCreate->name,
128,330✔
1084
                pStream->pCreate->sql, strlen(pStream->pCreate->sql), duration, 0);
128,330✔
1085
  }
1086

1087
  MND_STREAM_SET_LAST_TS(STM_EVENT_CREATE_STREAM, taosGetTimestampMs());
233,204✔
1088

1089
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, true, STREAM_ACT_DEPLOY);
128,330✔
1090

1091
_OVER:
134,950✔
1092

1093
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
134,950✔
1094
    if (pStream && pStream->pCreate) {
6,620✔
1095
      mstsError("failed to create stream %s at line:%d since %s", pStream->pCreate->name, lino, tstrerror(code));
4,400✔
1096
    } else {
1097
      mstsError("failed to create stream at line:%d since %s", lino, tstrerror(code));
2,220✔
1098
    }
1099
  } else {
1100
    mstsDebug("create stream %s half completed", pStream->pCreate ? pStream->pCreate->name : "unknown");
128,330✔
1101
  }
1102

1103
  tFreeSCMCreateStreamReq(pCreate);
134,950✔
1104
  taosMemoryFreeClear(pCreate);
134,950✔
1105

1106
  mndTransDrop(pTrans);
134,950✔
1107
  tFreeStreamObj(&streamObj);
134,950✔
1108
  mndReleaseUser(pMnode, pOperUser);
134,950✔
1109

1110
  return code;
134,950✔
1111
}
1112

1113
static int32_t mndProcessRecalcStreamReq(SRpcMsg *pReq) {
9,803✔
1114
  SMnode     *pMnode = pReq->info.node;
9,803✔
1115
  SStreamObj *pStream = NULL;
9,803✔
1116
  SUserObj   *pOperUser = NULL;
9,803✔
1117
  int32_t     code = 0;
9,803✔
1118
  int64_t     tss = taosGetTimestampMs();
9,803✔
1119

1120
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
9,803✔
UNCOV
1121
    return code;
×
1122
  }
1123

1124
  SMRecalcStreamReq recalcReq = {0};
9,803✔
1125
  if (tDeserializeSMRecalcStreamReq(pReq->pCont, pReq->contLen, &recalcReq) < 0) {
9,803✔
UNCOV
1126
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1127
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1128
  }
1129

1130
  code = mndAcquireStream(pMnode, recalcReq.name, &pStream);
9,803✔
1131
  if (pStream == NULL || code != 0) {
9,803✔
UNCOV
1132
    mError("stream:%s not exist, failed to recalc stream", recalcReq.name);
×
UNCOV
1133
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1134
    TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1135
  }
1136

1137
  int64_t streamId = pStream->pCreate->streamId;
9,803✔
1138
  
1139
  mstsInfo("start to recalc stream %s", recalcReq.name);
9,803✔
1140

1141
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
1142
  // if (code != TSDB_CODE_SUCCESS) {
1143
  //   mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
1144
  //   sdbRelease(pMnode->pSdb, pStream);
1145
  //   tFreeMRecalcStreamReq(&recalcReq);
1146
  //   return code;
1147
  // }
1148

1149
  if ((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
9,803✔
UNCOV
1150
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
1151
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1152
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1153
    TAOS_RETURN(code);
×
1154
  }
1155

1156
  if ((code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, pStream->pCreate->streamDB,
9,803✔
1157
                                            NULL)) ||
3,083✔
1158
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_START, PRIV_OBJ_STREAM, pStream->ownerId,
3,083✔
1159
                                       pStream->pCreate->streamDB, pStream->pCreate->name))) {
3,083✔
1160
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
6,720✔
1161
    mndReleaseUser(pMnode, pOperUser);
6,720✔
1162
    sdbRelease(pMnode->pSdb, pStream);
6,720✔
1163
    tFreeMRecalcStreamReq(&recalcReq);
6,720✔
1164
    return code;
6,720✔
1165
  }
1166

1167
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
3,083✔
1168

1169
  if (atomic_load_8(&pStream->userDropped)) {
3,083✔
UNCOV
1170
    code = TSDB_CODE_MND_STREAM_DROPPING;
×
UNCOV
1171
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
×
UNCOV
1172
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1173
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1174
    return code;
×
1175
  }
1176

1177
  if (atomic_load_8(&pStream->userStopped)) {
3,083✔
UNCOV
1178
    code = TSDB_CODE_MND_STREAM_STOPPED;
×
UNCOV
1179
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
×
UNCOV
1180
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1181
    tFreeMRecalcStreamReq(&recalcReq);
×
1182
    return code;
×
1183
  }
1184

1185
  if (WINDOW_TYPE_PERIOD == pStream->pCreate->triggerType) {
3,083✔
1186
    code = TSDB_CODE_OPS_NOT_SUPPORT;
171✔
1187
    mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
171✔
1188
    sdbRelease(pMnode->pSdb, pStream);
171✔
1189
    tFreeMRecalcStreamReq(&recalcReq);
171✔
1190
    return code;
171✔
1191
  }
1192

1193
  /*
1194
  pStream->updateTime = taosGetTimestampMs();
1195

1196
  STrans *pTrans = NULL;
1197
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RECALC_NAME, &pTrans);
1198
  if (pTrans == NULL || code) {
1199
    mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
1200
    sdbRelease(pMnode->pSdb, pStream);
1201
    return code;
1202
  }
1203

1204
  // stop stream
1205
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
1206
  if (code != TSDB_CODE_SUCCESS) {
1207
    sdbRelease(pMnode->pSdb, pStream);
1208
    mndTransDrop(pTrans);
1209
    return code;
1210
  }
1211

1212
  code = mndTransPrepare(pMnode, pTrans);
1213
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1214
    mError("trans:%d, failed to prepare stop stream trans since %s", pTrans->id, tstrerror(code));
1215
    sdbRelease(pMnode->pSdb, pStream);
1216
    mndTransDrop(pTrans);
1217
    return code;
1218
  }
1219
*/
1220

1221
  code = msmRecalcStream(pMnode, pStream->pCreate->streamId, &recalcReq.timeRange);
2,912✔
1222
  if (code != TSDB_CODE_SUCCESS) {
2,912✔
UNCOV
1223
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1224
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1225
    return code;
×
1226
  }
1227

1228
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE){
2,912✔
1229
    char buf[128];
2,912✔
1230
    snprintf(buf, sizeof(buf), "start:%" PRId64 ", end:%" PRId64, recalcReq.timeRange.skey, recalcReq.timeRange.ekey);
2,912✔
1231
    int64_t tse = taosGetTimestampMs();
2,912✔
1232
    double  duration = (double)(tse - tss);
2,912✔
1233
    duration = duration / 1000;
2,912✔
1234
    auditRecord(pReq, pMnode->clusterId, "recalcStream", pStream->name, recalcReq.name, buf, strlen(buf), duration, 0);
2,912✔
1235
  }  
1236

1237
  sdbRelease(pMnode->pSdb, pStream);
2,912✔
1238
  tFreeMRecalcStreamReq(&recalcReq);
2,912✔
1239
//  mndTransDrop(pTrans);
1240

1241
  return TSDB_CODE_SUCCESS;
2,912✔
1242
}
1243

1244

1245
int32_t mndInitStream(SMnode *pMnode) {
382,405✔
1246
  SSdbTable table = {
382,405✔
1247
      .sdbType = SDB_STREAM,
1248
      .keyType = SDB_KEY_BINARY,
1249
      .encodeFp = (SdbEncodeFp)mndStreamActionEncode,
1250
      .decodeFp = (SdbDecodeFp)mndStreamActionDecode,
1251
      .insertFp = (SdbInsertFp)mndStreamActionInsert,
1252
      .updateFp = (SdbUpdateFp)mndStreamActionUpdate,
1253
      .deleteFp = (SdbDeleteFp)mndStreamActionDelete,
1254
  };
1255

1256
  if (!tsDisableStream) {
382,405✔
1257
    mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STREAM, mndProcessCreateStreamReq);
382,405✔
1258
    mndSetMsgHandle(pMnode, TDMT_MND_DROP_STREAM, mndProcessDropStreamReq);
382,405✔
1259
    mndSetMsgHandle(pMnode, TDMT_MND_START_STREAM, mndProcessStartStreamReq);
382,405✔
1260
    mndSetMsgHandle(pMnode, TDMT_MND_STOP_STREAM, mndProcessStopStreamReq);
382,405✔
1261
    mndSetMsgHandle(pMnode, TDMT_MND_STREAM_HEARTBEAT, mndProcessStreamHb);  
382,405✔
1262
    mndSetMsgHandle(pMnode, TDMT_MND_RECALC_STREAM, mndProcessRecalcStreamReq);
382,405✔
1263
  }
1264
  
1265
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndRetrieveStream);
382,405✔
1266
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndCancelGetNextStream);
382,405✔
1267
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndRetrieveStreamTask);
382,405✔
1268
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndCancelGetNextStreamTask);
382,405✔
1269
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndRetrieveStreamRecalculates);
382,405✔
1270
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndCancelGetNextStreamRecalculates);
382,405✔
1271

1272
  int32_t code = sdbSetTable(pMnode->pSdb, table);
382,405✔
1273
  if (code) {
382,405✔
UNCOV
1274
    return code;
×
1275
  }
1276

1277
  //code = sdbSetTable(pMnode->pSdb, tableSeq);
1278
  return code;
382,405✔
1279
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc