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

taosdata / TDengine / #4905

29 Dec 2025 02:08PM UTC coverage: 65.423% (-0.3%) from 65.734%
#4905

push

travis-ci

web-flow
enh: sign connect request (#34067)

23 of 29 new or added lines in 4 files covered. (79.31%)

11614 existing lines in 186 files now uncovered.

193476 of 295730 relevant lines covered (65.42%)

115752566.53 hits per line

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

69.08
/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) {
385,825✔
64
  mDebug("try to clean up stream");
385,825✔
65
  
66
  msmHandleBecomeNotLeader(pMnode);
385,825✔
67
  
68
  mDebug("mnd stream runtime info cleanup");
385,825✔
69
}
385,825✔
70

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

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

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

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

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

95
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
137,747✔
96

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

100
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
137,747✔
101

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

107
  if (code < 0) {
137,747✔
UNCOV
108
    tFreeStreamObj(pStream);
×
109
  }
110

111
_over:
137,747✔
112
  taosMemoryFreeClear(buf);
137,747✔
113

114
  if (code != TSDB_CODE_SUCCESS) {
137,747✔
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);
×
UNCOV
117
    taosMemoryFreeClear(pRow);
×
118

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

124
    terrno = 0;
137,747✔
125
    return pRow;
137,747✔
126
  }
127
}
128

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

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

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

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

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

UNCOV
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) {
×
UNCOV
165
    strncpy((char*)p2, pStream->name, TSDB_STREAM_NAME_LEN);
×
UNCOV
166
    return false;
×
167
  }
168

UNCOV
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;
×
UNCOV
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);
×
UNCOV
181
    if ((*pStream) == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
×
UNCOV
182
      code = TSDB_CODE_MND_STREAM_NOT_EXIST;
×
183
    }
184
  }
185
  
UNCOV
186
  return code;
×
187
}
188

189
void mndReleaseStream(SMnode *pMnode, SStreamObj *pStream) {
134,965✔
190
  SSdb *pSdb = pMnode->pSdb;
134,965✔
191
  sdbRelease(pSdb, pStream);
134,965✔
192
}
134,965✔
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; }
×
UNCOV
197
int32_t  mndStreamSeqActionDelete(SSdb *pSdb, SStreamSeq *pStream) { return 0; }
×
UNCOV
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,
125,279✔
201
                              int32_t snodeId) {
202
  int32_t code = 0;
125,279✔
203

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

210
  pObj->userDropped = 0;
125,279✔
211
  pObj->userStopped = 0;
125,279✔
212

213
  pObj->createTime = taosGetTimestampMs();
125,279✔
214
  pObj->updateTime = pObj->createTime;
125,279✔
215

216
  mstLogSStreamObj("create stream", pObj);
125,279✔
217
}
125,279✔
218

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

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

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

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

251
  if (NULL == pStream->outTags) {
50,995✔
252
    createReq.numOfTags = 1;
×
253
    createReq.pTags = taosArrayInit_s(sizeof(SField), 1);
×
UNCOV
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

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

269
    for (int32_t i = 0; i < createReq.numOfTags; i++) {
130,487✔
270
      SField *pField = taosArrayGet(createReq.pTags, i);
79,492✔
271
      if (pField == NULL) {
79,492✔
UNCOV
272
        continue;
×
273
      }
274

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

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

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

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

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

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

309
  SStbObj stbObj = {0};
50,995✔
310

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

315
  stbObj.uid = pStream->outStbUid;
50,995✔
316

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

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

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

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

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

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

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

348
  if (pCreate->streamDB) {
125,279✔
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);
125,279✔
351
    if (code) {
125,279✔
UNCOV
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);
125,279✔
356
  }
357

358
  if (pCreate->triggerDB) {
125,279✔
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);
124,373✔
361
    if (code) {
124,373✔
UNCOV
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);
124,373✔
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) {
125,279✔
380
    int32_t dbNum = taosArrayGetSize(pCreate->calcDB);
122,262✔
381
    for (int32_t i = 0; i < dbNum; ++i) {
244,524✔
382
      char *calcDB = taosArrayGetP(pCreate->calcDB, i);
122,262✔
383
      // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_READ_DB, calcDB);
384
      code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, calcDB, NULL);
122,262✔
385
      if (code) {
122,262✔
UNCOV
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);
122,262✔
390
    }
391
  }
392

393
  if (pCreate->outDB) {
125,279✔
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);
122,262✔
396
    if (code) {
122,262✔
UNCOV
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);
122,262✔
401
  }
402

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

410
_OVER:
125,279✔
411

412
  return code;
125,279✔
413
}
414

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

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

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

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

449
  return 0;
581,583✔
450
}
451

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

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

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

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

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

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

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

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

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

514
    sdbRelease(pSdb, pStream);
2,250,511✔
515
  }
516

517
  pShow->numOfRows += numOfRows;
163,415✔
518
  return numOfRows;
163,415✔
519
}
520

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

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

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

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

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

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

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

553

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

560
  if (TSDB_SUPER_TABLE != pStream->pCreate->triggerTblType && 
1,803,526✔
561
      TSDB_CHILD_TABLE != pStream->pCreate->triggerTblType && 
1,042,051✔
562
      TSDB_VIRTUAL_CHILD_TABLE != pStream->pCreate->triggerTblType) {
809,511✔
563
    return true;
83,281✔
564
  }
565

566
  if (pStream->pCreate->triggerTblSuid != *(uint64_t*)p1) {
1,720,245✔
567
    return true;
1,690,512✔
568
  }
569

570
  if (NULL == pStream->pCreate->partitionCols) {
29,733✔
571
    return true;
3,860✔
572
  }
573

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

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

585
  SNode* pNode = NULL;
25,873✔
586
  FOREACH(pNode, pList) {
57,922✔
587
    SColumnNode* pCol = (SColumnNode*)pNode;
32,049✔
588
    for (int32_t i = 0; i < *tagNum; ++i) {
61,941✔
589
      if (pCol->colId == pTags[i].colId) {
55,612✔
590
        pTags[i].flags |= COL_REF_BY_STM;
25,720✔
591
        break;
25,720✔
592
      }
593
    }
594
  }
595

596
  nodesDestroyList(pList);
25,873✔
597
  
598
  return true;
25,873✔
599
}
600

601

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

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

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

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

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

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

637
  int64_t streamId = pStream->pCreate->streamId;
1,744✔
638
  
639
  mstsInfo("start to stop stream %s", pStream->name);
1,744✔
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,744✔
UNCOV
643
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
644
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
645
    return code;
×
646
  }
647

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

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

660
  if (atomic_load_8(&pStream->userDropped)) {
1,744✔
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,744✔
668
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_STOP_NAME, &pTrans);
1,744✔
669
  if (pTrans == NULL || code) {
1,744✔
UNCOV
670
    mstsError("failed to stop stream %s since %s", pStream->name, tstrerror(code));
×
UNCOV
671
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
672
    return code;
×
673
  }
674

675
  pStream->updateTime = taosGetTimestampMs();
3,488✔
676

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

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

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

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

691
  code = mndTransPrepare(pMnode, pTrans);
1,744✔
692
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,744✔
UNCOV
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,744✔
700
  mndTransDrop(pTrans);
1,744✔
701

702
  return TSDB_CODE_ACTION_IN_PROGRESS;
1,744✔
703
}
704

705

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

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

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

721
  code = mndAcquireStream(pMnode, resumeReq.name, &pStream);
1,744✔
722
  if (pStream == NULL || code != 0) {
1,744✔
UNCOV
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 {
UNCOV
729
      mError("stream:%s not exist, failed to start stream", resumeReq.name);
×
UNCOV
730
      taosMemoryFree(resumeReq.name);
×
UNCOV
731
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
732
    }
733
  }
734

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

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

739
  mstsInfo("start to start stream %s from stopped", pStream->name);
1,744✔
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,744✔
UNCOV
743
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
744
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
745
    return code;
×
746
  }
747

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

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

760
  if (atomic_load_8(&pStream->userDropped)) {
1,744✔
UNCOV
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,744✔
UNCOV
768
    code = TSDB_CODE_MND_STREAM_NOT_STOPPED;
×
UNCOV
769
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
770
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
771
    return code;
×
772
  }
773
  
774
  atomic_store_8(&pStream->userStopped, 0);
1,744✔
775

776
  pStream->updateTime = taosGetTimestampMs();
3,488✔
777

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

780
  STrans *pTrans = NULL;
1,744✔
781
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_START_NAME, &pTrans);
1,744✔
782
  if (pTrans == NULL || code) {
1,744✔
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,744✔
789
  if (code != TSDB_CODE_SUCCESS) {
1,744✔
790
    mstsError("failed to start stream %s since %s", pStream->name, tstrerror(code));
×
791
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
792
    mndTransDrop(pTrans);
×
793
    return code;
×
794
  }
795

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

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

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

809
  return TSDB_CODE_ACTION_IN_PROGRESS;
1,744✔
810
}
811

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

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

827
  mDebug("recv drop stream:%s msg", dropReq.name);
4,052✔
828

829
  code = mndAcquireStream(pMnode, dropReq.name, &pStream);
4,052✔
830
  if (pStream == NULL || code != 0) {
4,052✔
831
    if (dropReq.igNotExists) {
×
UNCOV
832
      mInfo("stream:%s not exist, ignore not exist is set, drop stream exec done with success", dropReq.name);
×
UNCOV
833
      sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
834
      tFreeMDropStreamReq(&dropReq);
×
UNCOV
835
      return 0;
×
836
    } else {
837
      mError("stream:%s not exist failed to drop it", dropReq.name);
×
838
      tFreeMDropStreamReq(&dropReq);
×
839
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
840
    }
841
  }
842

843
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser));
4,052✔
844

845
  int64_t streamId = pStream->pCreate->streamId;
4,052✔
846

847
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
848
  TAOS_CHECK_EXIT(mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, pStream->pCreate->streamDB,
4,052✔
849
                                                NULL));
850
  TAOS_CHECK_EXIT(mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_DROP, PRIV_OBJ_STREAM, pStream->ownerId,
4,052✔
851
                                           pStream->pCreate->streamDB, pStream->pCreate->name));
852

853
  if (pStream->pCreate->tsmaId != 0) {
4,052✔
UNCOV
854
    mstsDebug("try to drop tsma related stream, tsmaId:%" PRIx64, pStream->pCreate->tsmaId);
×
855

UNCOV
856
    void    *pIter = NULL;
×
UNCOV
857
    SSmaObj *pSma = NULL;
×
UNCOV
858
    pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
×
UNCOV
859
    while (pIter) {
×
UNCOV
860
      if (pSma && pSma->uid == pStream->pCreate->tsmaId) {
×
UNCOV
861
        sdbRelease(pMnode->pSdb, pSma);
×
UNCOV
862
        sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
863
        code = TSDB_CODE_TSMA_MUST_BE_DROPPED;
×
864

UNCOV
865
        mstsError("refused to drop tsma-related stream %s since tsma still exists", dropReq.name);
×
UNCOV
866
        TAOS_CHECK_EXIT(code);
×
867
      }
868

UNCOV
869
      if (pSma) {
×
UNCOV
870
        sdbRelease(pMnode->pSdb, pSma);
×
871
      }
872

873
      pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
×
874
    }
875
  }
876

877
  mstsInfo("start to drop stream %s", pStream->pCreate->name);
4,052✔
878

879
  pStream->updateTime = taosGetTimestampMs();
8,104✔
880

881
  atomic_store_8(&pStream->userDropped, 1);
4,052✔
882

883
  MND_STREAM_SET_LAST_TS(STM_EVENT_DROP_STREAM, pStream->updateTime);
4,052✔
884

885
  msmUndeployStream(pMnode, streamId, pStream->pCreate->name);
4,052✔
886

887
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, &pTrans);
4,052✔
888
  if (pTrans == NULL || code) {
4,052✔
UNCOV
889
    mstsError("failed to drop stream %s since %s", dropReq.name, tstrerror(code));
×
UNCOV
890
    TAOS_CHECK_EXIT(code);
×
891
  }
892

893
  // drop stream
894
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_DROPPED);
4,052✔
895
  if (code) {
4,052✔
UNCOV
896
    mstsError("trans:%d, failed to append drop stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
897
    TAOS_CHECK_EXIT(code);
×
898
  }
899

900
  code = mndTransPrepare(pMnode, pTrans);
4,052✔
901
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
4,052✔
UNCOV
902
    mstsError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
903
    TAOS_CHECK_EXIT(code);
×
904
  }
905

906
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE) {
4,052✔
907
    int64_t tse = taosGetTimestampMs();
4,052✔
908
    double  duration = (double)(tse - tss);
4,052✔
909
    duration = duration / 1000;
4,052✔
910
    auditRecord(pReq, pMnode->clusterId, "dropStream", "", pStream->pCreate->streamDB, NULL, 0, duration, 0);
4,052✔
911
  }
912

913
  mstsDebug("drop stream %s half completed", dropReq.name);
4,052✔
914
  code = TSDB_CODE_ACTION_IN_PROGRESS;
4,052✔
915

916
_exit:
4,052✔
917
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
4,052✔
UNCOV
918
    mstsError("failed to drop stream %s at line %d since %s", dropReq.name, lino, tstrerror(code));
×
919
  }
920
  sdbRelease(pMnode->pSdb, pStream);
4,052✔
921
  mndTransDrop(pTrans);
4,052✔
922
  mndReleaseUser(pMnode, pOperUser);
4,052✔
923
  tFreeMDropStreamReq(&dropReq);
4,052✔
924
  TAOS_RETURN(code);
4,052✔
925
}
926

927
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq) {
132,069✔
928
  SMnode     *pMnode = pReq->info.node;
132,069✔
929
  SStreamObj *pStream = NULL;
132,069✔
930
  SStreamObj  streamObj = {0};
132,069✔
931
  SUserObj    *pOperUser = NULL;
132,069✔
932
  int32_t     code = TSDB_CODE_SUCCESS;
132,069✔
933
  int32_t     lino = 0;
132,069✔
934
  STrans     *pTrans = NULL;
132,069✔
935
  uint64_t    streamId = 0;
132,069✔
936
  SCMCreateStreamReq* pCreate = NULL;
132,069✔
937
  int64_t             tss = taosGetTimestampMs();
132,069✔
938

939
  if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
132,069✔
UNCOV
940
    goto _OVER;
×
941
  }
942
  
943
#ifdef WINDOWS
944
  code = TSDB_CODE_MND_INVALID_PLATFORM;
945
  goto _OVER;
946
#endif
947

948
  pCreate = taosMemoryCalloc(1, sizeof(SCMCreateStreamReq));
132,069✔
949
  TSDB_CHECK_NULL(pCreate, code, lino, _OVER, terrno);
132,069✔
950
  
951
  code = tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, pCreate);
132,069✔
952
  TSDB_CHECK_CODE(code, lino, _OVER);
132,069✔
953

954
  streamId = pCreate->streamId;
132,069✔
955

956
  mstsInfo("start to create stream %s, sql:%s", pCreate->name, pCreate->sql);
132,069✔
957

958
  int32_t snodeId = msmAssignRandomSnodeId(pMnode, streamId);
132,069✔
959
  if (!GOT_SNODE(snodeId)) {
132,069✔
960
    code = terrno;
2,290✔
961
    TSDB_CHECK_CODE(code, lino, _OVER);
2,290✔
962
  }
963
  
964
  code = mndAcquireStream(pMnode, pCreate->name, &pStream);
129,779✔
965
  if (pStream != NULL && code == 0) {
129,779✔
966
    if (pCreate->igExists) {
4,500✔
UNCOV
967
      mstsInfo("stream %s already exist, ignore exist is set", pCreate->name);
×
968
    } else {
969
      code = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
4,500✔
970
    }
971

972
    mndReleaseStream(pMnode, pStream);
4,500✔
973
    goto _OVER;
4,500✔
974
  } else if (code != TSDB_CODE_MND_STREAM_NOT_EXIST) {
125,279✔
UNCOV
975
    goto _OVER;
×
976
  }
977

978
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
125,279✔
979
  if (pOperUser == NULL) {
125,279✔
UNCOV
980
    TSDB_CHECK_CODE(TSDB_CODE_MND_NO_USER_FROM_CONN, lino, _OVER);
×
981
  }
982

983
  code = mndStreamValidateCreate(pMnode, pOperUser, pCreate);
125,279✔
984
  TSDB_CHECK_CODE(code, lino, _OVER);
125,279✔
985

986
  mndStreamBuildObj(pMnode, &streamObj, pCreate, pOperUser, snodeId);
125,279✔
987
  pCreate = NULL;
125,279✔
988

989
  pStream = &streamObj;
125,279✔
990

991
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_DB, MND_STREAM_CREATE_NAME, &pTrans);
125,279✔
992
  if (pTrans == NULL || code) {
125,279✔
UNCOV
993
    goto _OVER;
×
994
  }
995

996
  // create stb for stream
997
  if (TSDB_SUPER_TABLE == pStream->pCreate->outTblType && !pStream->pCreate->outStbExists) {
125,279✔
998
    pStream->pCreate->outStbUid = mndGenerateUid(pStream->pCreate->outTblName, strlen(pStream->pCreate->outTblName));
50,995✔
999
    code = mndStreamCreateOutStb(pMnode, pTrans, pStream->pCreate, RPC_MSG_USER(pReq));
50,995✔
1000
    TSDB_CHECK_CODE(code, lino, _OVER);
50,995✔
1001
  }
1002

1003
  // add stream to trans
1004
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
125,279✔
1005
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
125,279✔
UNCOV
1006
    mstsError("failed to persist stream %s since %s", pStream->pCreate->name, tstrerror(code));
×
UNCOV
1007
    goto _OVER;
×
1008
  }
1009

1010
  // execute creation
1011
  code = mndTransPrepare(pMnode, pTrans);
125,279✔
1012
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
125,279✔
UNCOV
1013
    mstsError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
UNCOV
1014
    goto _OVER;
×
1015
  }
1016
  code = TSDB_CODE_ACTION_IN_PROGRESS;
125,279✔
1017

1018
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE) {
125,279✔
1019
    int64_t tse = taosGetTimestampMs();
125,279✔
1020
    double  duration = (double)(tse - tss);
125,279✔
1021
    duration = duration / 1000;
125,279✔
1022
    auditRecord(pReq, pMnode->clusterId, "createStream", pStream->pCreate->streamDB, pStream->pCreate->name,
125,279✔
1023
                pStream->pCreate->sql, strlen(pStream->pCreate->sql), duration, 0);
125,279✔
1024
  }
1025

1026
  MND_STREAM_SET_LAST_TS(STM_EVENT_CREATE_STREAM, taosGetTimestampMs());
226,944✔
1027

1028
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, true, STREAM_ACT_DEPLOY);
125,279✔
1029

1030
_OVER:
132,069✔
1031

1032
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
132,069✔
1033
    if (pStream && pStream->pCreate) {
6,790✔
1034
      mstsError("failed to create stream %s at line:%d since %s", pStream->pCreate->name, lino, tstrerror(code));
4,500✔
1035
    } else {
1036
      mstsError("failed to create stream at line:%d since %s", lino, tstrerror(code));
2,290✔
1037
    }
1038
  } else {
1039
    mstsDebug("create stream %s half completed", pStream->pCreate ? pStream->pCreate->name : "unknown");
125,279✔
1040
  }
1041

1042
  tFreeSCMCreateStreamReq(pCreate);
132,069✔
1043
  taosMemoryFreeClear(pCreate);
132,069✔
1044

1045
  mndTransDrop(pTrans);
132,069✔
1046
  tFreeStreamObj(&streamObj);
132,069✔
1047
  mndReleaseUser(pMnode, pOperUser);
132,069✔
1048

1049
  return code;
132,069✔
1050
}
1051

1052
static int32_t mndProcessRecalcStreamReq(SRpcMsg *pReq) {
10,062✔
1053
  SMnode     *pMnode = pReq->info.node;
10,062✔
1054
  SStreamObj *pStream = NULL;
10,062✔
1055
  SUserObj   *pOperUser = NULL;
10,062✔
1056
  int32_t     code = 0;
10,062✔
1057
  int64_t     tss = taosGetTimestampMs();
10,062✔
1058

1059
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
10,062✔
UNCOV
1060
    return code;
×
1061
  }
1062

1063
  SMRecalcStreamReq recalcReq = {0};
10,062✔
1064
  if (tDeserializeSMRecalcStreamReq(pReq->pCont, pReq->contLen, &recalcReq) < 0) {
10,062✔
UNCOV
1065
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1066
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1067
  }
1068

1069
  code = mndAcquireStream(pMnode, recalcReq.name, &pStream);
10,062✔
1070
  if (pStream == NULL || code != 0) {
10,062✔
UNCOV
1071
    mError("stream:%s not exist, failed to recalc stream", recalcReq.name);
×
UNCOV
1072
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1073
    TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1074
  }
1075

1076
  int64_t streamId = pStream->pCreate->streamId;
10,062✔
1077
  
1078
  mstsInfo("start to recalc stream %s", recalcReq.name);
10,062✔
1079

1080
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
1081
  // if (code != TSDB_CODE_SUCCESS) {
1082
  //   mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
1083
  //   sdbRelease(pMnode->pSdb, pStream);
1084
  //   tFreeMRecalcStreamReq(&recalcReq);
1085
  //   return code;
1086
  // }
1087

1088
  if ((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
10,062✔
UNCOV
1089
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
1090
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1091
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1092
    TAOS_RETURN(code);
×
1093
  }
1094

1095
  if ((code = mndCheckDbPrivilegeByNameRecF(pMnode, pOperUser, PRIV_DB_USE, PRIV_OBJ_DB, pStream->pCreate->streamDB,
10,062✔
1096
                                            NULL)) ||
3,162✔
1097
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_START, PRIV_OBJ_STREAM, pStream->ownerId,
3,162✔
1098
                                       pStream->pCreate->streamDB, pStream->pCreate->name))) {
3,162✔
1099
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
6,900✔
1100
    mndReleaseUser(pMnode, pOperUser);
6,900✔
1101
    sdbRelease(pMnode->pSdb, pStream);
6,900✔
1102
    tFreeMRecalcStreamReq(&recalcReq);
6,900✔
1103
    return code;
6,900✔
1104
  }
1105

1106
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
3,162✔
1107

1108
  if (atomic_load_8(&pStream->userDropped)) {
3,162✔
UNCOV
1109
    code = TSDB_CODE_MND_STREAM_DROPPING;
×
UNCOV
1110
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
×
UNCOV
1111
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1112
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1113
    return code;
×
1114
  }
1115

1116
  if (atomic_load_8(&pStream->userStopped)) {
3,162✔
UNCOV
1117
    code = TSDB_CODE_MND_STREAM_STOPPED;
×
UNCOV
1118
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
×
1119
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1120
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1121
    return code;
×
1122
  }
1123

1124
  if (WINDOW_TYPE_PERIOD == pStream->pCreate->triggerType) {
3,162✔
1125
    code = TSDB_CODE_OPS_NOT_SUPPORT;
174✔
1126
    mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
174✔
1127
    sdbRelease(pMnode->pSdb, pStream);
174✔
1128
    tFreeMRecalcStreamReq(&recalcReq);
174✔
1129
    return code;
174✔
1130
  }
1131

1132
  /*
1133
  pStream->updateTime = taosGetTimestampMs();
1134

1135
  STrans *pTrans = NULL;
1136
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RECALC_NAME, &pTrans);
1137
  if (pTrans == NULL || code) {
1138
    mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
1139
    sdbRelease(pMnode->pSdb, pStream);
1140
    return code;
1141
  }
1142

1143
  // stop stream
1144
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
1145
  if (code != TSDB_CODE_SUCCESS) {
1146
    sdbRelease(pMnode->pSdb, pStream);
1147
    mndTransDrop(pTrans);
1148
    return code;
1149
  }
1150

1151
  code = mndTransPrepare(pMnode, pTrans);
1152
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1153
    mError("trans:%d, failed to prepare stop stream trans since %s", pTrans->id, tstrerror(code));
1154
    sdbRelease(pMnode->pSdb, pStream);
1155
    mndTransDrop(pTrans);
1156
    return code;
1157
  }
1158
*/
1159

1160
  code = msmRecalcStream(pMnode, pStream->pCreate->streamId, &recalcReq.timeRange);
2,988✔
1161
  if (code != TSDB_CODE_SUCCESS) {
2,988✔
UNCOV
1162
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1163
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1164
    return code;
×
1165
  }
1166

1167
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE){
2,988✔
1168
    char buf[128];
2,988✔
1169
    snprintf(buf, sizeof(buf), "start:%" PRId64 ", end:%" PRId64, recalcReq.timeRange.skey, recalcReq.timeRange.ekey);
2,988✔
1170
    int64_t tse = taosGetTimestampMs();
2,988✔
1171
    double  duration = (double)(tse - tss);
2,988✔
1172
    duration = duration / 1000;
2,988✔
1173
    auditRecord(pReq, pMnode->clusterId, "recalcStream", pStream->name, recalcReq.name, buf, strlen(buf), duration, 0);
2,988✔
1174
  }  
1175

1176
  sdbRelease(pMnode->pSdb, pStream);
2,988✔
1177
  tFreeMRecalcStreamReq(&recalcReq);
2,988✔
1178
//  mndTransDrop(pTrans);
1179

1180
  return TSDB_CODE_SUCCESS;
2,988✔
1181
}
1182

1183

1184
int32_t mndInitStream(SMnode *pMnode) {
385,888✔
1185
  SSdbTable table = {
385,888✔
1186
      .sdbType = SDB_STREAM,
1187
      .keyType = SDB_KEY_BINARY,
1188
      .encodeFp = (SdbEncodeFp)mndStreamActionEncode,
1189
      .decodeFp = (SdbDecodeFp)mndStreamActionDecode,
1190
      .insertFp = (SdbInsertFp)mndStreamActionInsert,
1191
      .updateFp = (SdbUpdateFp)mndStreamActionUpdate,
1192
      .deleteFp = (SdbDeleteFp)mndStreamActionDelete,
1193
  };
1194

1195
  if (!tsDisableStream) {
385,888✔
1196
    mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STREAM, mndProcessCreateStreamReq);
385,888✔
1197
    mndSetMsgHandle(pMnode, TDMT_MND_DROP_STREAM, mndProcessDropStreamReq);
385,888✔
1198
    mndSetMsgHandle(pMnode, TDMT_MND_START_STREAM, mndProcessStartStreamReq);
385,888✔
1199
    mndSetMsgHandle(pMnode, TDMT_MND_STOP_STREAM, mndProcessStopStreamReq);
385,888✔
1200
    mndSetMsgHandle(pMnode, TDMT_MND_STREAM_HEARTBEAT, mndProcessStreamHb);  
385,888✔
1201
    mndSetMsgHandle(pMnode, TDMT_MND_RECALC_STREAM, mndProcessRecalcStreamReq);
385,888✔
1202
  }
1203
  
1204
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndRetrieveStream);
385,888✔
1205
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndCancelGetNextStream);
385,888✔
1206
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndRetrieveStreamTask);
385,888✔
1207
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndCancelGetNextStreamTask);
385,888✔
1208
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndRetrieveStreamRecalculates);
385,888✔
1209
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndCancelGetNextStreamRecalculates);
385,888✔
1210

1211
  int32_t code = sdbSetTable(pMnode->pSdb, table);
385,888✔
1212
  if (code) {
385,888✔
UNCOV
1213
    return code;
×
1214
  }
1215

1216
  //code = sdbSetTable(pMnode->pSdb, tableSeq);
1217
  return code;
385,888✔
1218
}
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