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

taosdata / TDengine / #5014

03 Apr 2026 03:59PM UTC coverage: 72.256% (-0.06%) from 72.317%
#5014

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4054 of 5985 new or added lines in 68 files covered. (67.74%)

13285 existing lines in 168 files now uncovered.

257272 of 356056 relevant lines covered (72.26%)

133154720.42 hits per line

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

71.24
/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 "mndVgroup.h"
25
#include "osMemory.h"
26
#include "parser.h"
27
#include "taoserror.h"
28
#include "tmisce.h"
29
#include "tname.h"
30

31
#define MND_STREAM_MAX_NUM 100000
32

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

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

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

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

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

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

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

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

72
SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw) {
242,167✔
73
  int32_t     code = 0;
242,167✔
74
  int32_t     lino = 0;
242,167✔
75
  SSdbRow    *pRow = NULL;
242,167✔
76
  SStreamObj *pStream = NULL;
242,167✔
77
  void       *buf = NULL;
242,167✔
78
  int8_t      sver = 0;
242,167✔
79
  int32_t     tlen;
241,694✔
80
  int32_t     dataPos = 0;
242,167✔
81

82
  code = sdbGetRawSoftVer(pRaw, &sver);
242,167✔
83
  TSDB_CHECK_CODE(code, lino, _over);
242,167✔
84

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

90
  pRow = sdbAllocRow(sizeof(SStreamObj));
242,167✔
91
  TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
242,167✔
92

93
  pStream = sdbGetRowObj(pRow);
242,167✔
94
  TSDB_CHECK_NULL(pStream, code, lino, _over, terrno);
242,167✔
95

96
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
242,167✔
97

98
  buf = taosMemoryMalloc(tlen + 1);
242,167✔
99
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
242,167✔
100

101
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
242,167✔
102

103
  SDecoder decoder;
241,694✔
104
  tDecoderInit(&decoder, buf, tlen + 1);
242,167✔
105
  code = tDecodeSStreamObj(&decoder, pStream, sver);
242,167✔
106
  tDecoderClear(&decoder);
242,167✔
107

108
  if (code < 0) {
242,167✔
UNCOV
109
    tFreeStreamObj(pStream);
×
110
  }
111

112
_over:
242,167✔
113
  taosMemoryFreeClear(buf);
242,167✔
114

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

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

125
    terrno = 0;
242,167✔
126
    return pRow;
242,167✔
127
  }
128
}
129

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

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

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

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

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

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

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

UNCOV
170
  return true;
×
171
}
172

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

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

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

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

205
  pObj->pCreate = pCreate;
199,846✔
206
  tstrncpy(pObj->name, pCreate->name, sizeof(pObj->name));
199,846✔
207
  (void)snprintf(pObj->createUser, sizeof(pObj->createUser), "%s", pOperUser->name);
199,846✔
208
  pObj->ownerId = pOperUser->uid;
199,846✔
209
  pObj->mainSnodeId = snodeId;
199,846✔
210

211
  pObj->userDropped = 0;
199,846✔
212
  pObj->userStopped = 0;
199,846✔
213

214
  pObj->createTime = taosGetTimestampMs();
199,846✔
215
  pObj->updateTime = pObj->createTime;
199,846✔
216

217
  mstLogSStreamObj("create stream", pObj);
199,846✔
218
}
199,846✔
219

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

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

235
  // build fields
236
  for (int32_t i = 0; i < createReq.numOfColumns; i++) {
378,295✔
237
    SFieldWithOptions *pField = taosArrayGet(createReq.pColumns, i);
305,162✔
238
    TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno);
305,162✔
239
    SFieldWithOptions *pSrc = taosArrayGet(pStream->outCols, i);
305,162✔
240

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

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

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

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

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

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

284
  if ((code = mndCheckCreateStbReq(&createReq)) != 0) {
73,133✔
UNCOV
285
    goto _OVER;
×
286
  }
287

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

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

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

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

310
  SStbObj stbObj = {0};
73,133✔
311

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

316
  stbObj.uid = pStream->outStbUid;
73,133✔
317

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

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

325
  tFreeSMCreateStbReq(&createReq);
73,133✔
326
  mndFreeStb(&stbObj);
73,133✔
327
  mndReleaseStb(pMnode, pStb);
73,133✔
328
  mndReleaseDb(pMnode, pDb);
73,133✔
329
  return code;
73,133✔
330

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

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

341
static int32_t mndStreamCreateOutTable(SMnode *pMnode, STrans *pTrans, const SCMCreateStreamReq *pStream) {
190✔
342
  int32_t  code = 0;
190✔
343
  int32_t  lino = 0;
190✔
344
  SVgObj  *pVgroup = NULL;
190✔
345
  SDbObj  *pDb = NULL;
190✔
346
  SName    name = {0};
190✔
347
  char     dbFName[TSDB_DB_FNAME_LEN] = {0};
190✔
348

349
  // Parse database and table name
350
  if ((code = tNameFromString(&name, pStream->outDB, T_NAME_ACCT | T_NAME_DB)) != 0) {
190✔
NEW
351
    mError("stream:%s failed to parse outDB:%s, code:%s", pStream->name, pStream->outDB, tstrerror(code));
×
NEW
352
    return code;
×
353
  }
354
  if ((code = tNameGetFullDbName(&name, dbFName)) != 0) {
190✔
NEW
355
    mError("stream:%s failed to get full db name, code:%s", pStream->name, tstrerror(code));
×
NEW
356
    return code;
×
357
  }
358

359
  // Get database object
360
  pDb = mndAcquireDb(pMnode, dbFName);
190✔
361
  if (pDb == NULL) {
190✔
NEW
362
    code = TSDB_CODE_MND_DB_NOT_SELECTED;
×
NEW
363
    mError("stream:%s failed to acquire db:%s, code:%s", pStream->name, dbFName, tstrerror(code));
×
NEW
364
    return code;
×
365
  }
366

367
  // Set transaction db name and check conflict (similar to mndAddStbToTrans)
368
  mndTransSetDbName(pTrans, pDb->name, pStream->outTblName);
190✔
369
  code = mndTransCheckConflict(pMnode, pTrans);
190✔
370
  if (code != TSDB_CODE_SUCCESS) {
190✔
NEW
371
    mError("stream:%s failed to check conflict, code:%s", pStream->name, tstrerror(code));
×
NEW
372
    goto _OVER;
×
373
  }
374

375
  // Get vgroup by vgId
376
  if (pStream->outTblVgId <= 0) {
190✔
NEW
377
    mError("stream:%s invalid outTblVgId:%d", pStream->name, pStream->outTblVgId);
×
NEW
378
    code = TSDB_CODE_MND_VGROUP_NOT_EXIST;
×
NEW
379
    goto _OVER;
×
380
  }
381

382
  pVgroup = mndAcquireVgroup(pMnode, pStream->outTblVgId);
190✔
383
  if (pVgroup == NULL) {
190✔
NEW
384
    code = TSDB_CODE_MND_VGROUP_NOT_EXIST;
×
NEW
385
    mError("stream:%s failed to acquire vgroup:%d, code:%s", pStream->name, pStream->outTblVgId, tstrerror(code));
×
NEW
386
    goto _OVER;
×
387
  }
388

389
  // Verify vgroup belongs to the database
390
  if (pVgroup->dbUid != pDb->uid) {
190✔
NEW
391
    code = TSDB_CODE_MND_VGROUP_NOT_EXIST;
×
NEW
392
    mError("stream:%s vgroup:%d does not belong to db:%s", pStream->name, pStream->outTblVgId, dbFName);
×
NEW
393
    goto _OVER;
×
394
  }
395

396
  // Build SVCreateTbReq (reusing logic from buildNormalTableCreateReq)
397
  SVCreateTbReq createReq = {0};
190✔
398
  createReq.type = TSDB_NORMAL_TABLE;
190✔
399
  createReq.flags = TD_CREATE_NORMAL_TB_IN_STREAM | TD_CREATE_IF_NOT_EXISTS;
190✔
400
  createReq.uid = mndGenerateUid(pStream->outTblName, strlen(pStream->outTblName));
190✔
401
  createReq.btime = taosGetTimestampMs();
190✔
402
  createReq.ttl = TSDB_DEFAULT_TABLE_TTL;
190✔
403
  createReq.commentLen = -1;
190✔
404
  createReq.name = taosStrdup(pStream->outTblName);
190✔
405
  if (createReq.name == NULL) {
190✔
NEW
406
    code = terrno;
×
NEW
407
    goto _OVER;
×
408
  }
409

410
  // Build schema from outCols (same logic as buildNormalTableCreateReq)
411
  int32_t numOfCols = taosArrayGetSize(pStream->outCols);
190✔
412
  createReq.ntb.schemaRow.nCols = numOfCols;
190✔
413
  createReq.ntb.schemaRow.version = 1;
190✔
414
  createReq.ntb.schemaRow.pSchema = taosMemoryCalloc(numOfCols, sizeof(SSchema));
190✔
415
  if (createReq.ntb.schemaRow.pSchema == NULL) {
190✔
NEW
416
    code = terrno;
×
NEW
417
    goto _OVER;
×
418
  }
419

420
  for (int32_t i = 0; i < numOfCols; i++) {
570✔
421
    SFieldWithOptions *pField = taosArrayGet(pStream->outCols, i);
380✔
422
    if (pField == NULL) {
380✔
NEW
423
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
NEW
424
      goto _OVER;
×
425
    }
426

427
    createReq.ntb.schemaRow.pSchema[i].colId = i + 1;
380✔
428
    createReq.ntb.schemaRow.pSchema[i].type = pField->type;
380✔
429
    createReq.ntb.schemaRow.pSchema[i].bytes = pField->bytes;
380✔
430
    createReq.ntb.schemaRow.pSchema[i].flags = pField->flags;
380✔
431
    tstrncpy(createReq.ntb.schemaRow.pSchema[i].name, pField->name, TSDB_COL_NAME_LEN);
380✔
432

433
    if (IS_DECIMAL_TYPE(pField->type)) {
380✔
NEW
434
      if (createReq.pExtSchemas == NULL) {
×
NEW
435
        createReq.pExtSchemas = taosMemoryCalloc(numOfCols, sizeof(SExtSchema));
×
NEW
436
        if (createReq.pExtSchemas == NULL) {
×
NEW
437
          code = terrno;
×
NEW
438
          goto _OVER;
×
439
        }
440
      }
NEW
441
      createReq.pExtSchemas[i].typeMod = pField->typeMod;
×
442
    }
443
  }
444

445
  // Initialize colCmpr with default encode/compress/level per column type
446
  code = tInitDefaultSColCmprWrapperByCols(&createReq.colCmpr, numOfCols);
190✔
447
  if (code != TSDB_CODE_SUCCESS) {
190✔
NEW
448
    goto _OVER;
×
449
  }
450
  for (int32_t i = 0; i < numOfCols; i++) {
570✔
451
    SSchema *pSchema = &createReq.ntb.schemaRow.pSchema[i];
380✔
452
    createReq.colCmpr.pColCmpr[i].id = pSchema->colId;
380✔
453
    createReq.colCmpr.pColCmpr[i].alg = createDefaultColCmprByType(pSchema->type);
380✔
454
  }
455

456
  // Build SVCreateTbBatchReq (vnode expects batch request)
457
  SVCreateTbBatchReq batchReq = {0};
190✔
458
  batchReq.nReqs = 1;
190✔
459
  batchReq.pArray = taosArrayInit(1, sizeof(SVCreateTbReq));
190✔
460
  if (batchReq.pArray == NULL) {
190✔
NEW
461
    code = terrno;
×
NEW
462
    goto _OVER;
×
463
  }
464
  if (taosArrayPush(batchReq.pArray, &createReq) == NULL) {
380✔
NEW
465
    code = terrno;
×
NEW
466
    taosArrayDestroy(batchReq.pArray);
×
NEW
467
    goto _OVER;
×
468
  }
469
  batchReq.source = TD_REQ_FROM_APP;
190✔
470

471
  // Serialize the batch request
472
  int32_t contLen = 0;
190✔
473
  int32_t ret = 0;
190✔
474
  tEncodeSize(tEncodeSVCreateTbBatchReq, &batchReq, contLen, ret);
190✔
475
  if (ret < 0) {
190✔
NEW
476
    code = terrno;
×
NEW
477
    taosArrayDestroy(batchReq.pArray);
×
NEW
478
    goto _OVER;
×
479
  }
480

481
  contLen += sizeof(SMsgHead);
190✔
482

483
  SMsgHead *pHead = taosMemoryCalloc(1, contLen);
190✔
484
  if (pHead == NULL) {
190✔
NEW
485
    code = terrno;
×
NEW
486
    taosArrayDestroy(batchReq.pArray);
×
NEW
487
    goto _OVER;
×
488
  }
489
  pHead->contLen = htonl(contLen);
190✔
490
  pHead->vgId = htonl(pVgroup->vgId);
190✔
491

492
  SEncoder encoder = {0};
190✔
493
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
190✔
494
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
190✔
495
  code = tEncodeSVCreateTbBatchReq(&encoder, &batchReq);
190✔
496
  tEncoderClear(&encoder);
190✔
497
  taosArrayDestroy(batchReq.pArray);
190✔
498
  if (code < 0) {
190✔
NEW
499
    taosMemoryFree(pHead);
×
NEW
500
    goto _OVER;
×
501
  }
502

503
  // Add to transaction redo actions
504
  STransAction action = {0};
190✔
505
  action.mTraceId = pTrans->mTraceId;
190✔
506
  action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
190✔
507
  action.pCont = pHead;
190✔
508
  action.contLen = contLen;
190✔
509
  action.msgType = TDMT_VND_CREATE_TABLE;
190✔
510
  action.acceptableCode = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
190✔
511
  action.retryCode = TSDB_CODE_TDB_TABLE_NOT_EXIST;
190✔
512
  action.groupId = pVgroup->vgId;
190✔
513

514
  code = mndTransAppendRedoAction(pTrans, &action);
190✔
515
  if (code != TSDB_CODE_SUCCESS) {
190✔
NEW
516
    taosMemoryFree(pHead);
×
NEW
517
    goto _OVER;
×
518
  }
519

520
  // Build undo action (drop table if transaction fails)
521
  SVDropTbReq dropReq = {0};
190✔
522
  dropReq.name = createReq.name;  // vnode metaCheckDropTableReq requires name
190✔
523
  dropReq.uid = createReq.uid;
190✔
524
  dropReq.igNotExists = 1;  // Ignore if table doesn't exist
190✔
525
  dropReq.isVirtual = 0;
190✔
526

527
  SVDropTbBatchReq dropBatchReq = {0};
190✔
528
  dropBatchReq.nReqs = 1;
190✔
529
  dropBatchReq.pArray = taosArrayInit(1, sizeof(SVDropTbReq));
190✔
530
  if (dropBatchReq.pArray == NULL) {
190✔
NEW
531
    code = terrno;
×
NEW
532
    goto _OVER;
×
533
  }
534
  if (taosArrayPush(dropBatchReq.pArray, &dropReq) == NULL) {
380✔
NEW
535
    code = terrno;
×
NEW
536
    taosArrayDestroy(dropBatchReq.pArray);
×
NEW
537
    goto _OVER;
×
538
  }
539

540
  // Serialize drop batch request
541
  int32_t dropContLen = 0;
190✔
542
  int32_t dropRet = 0;
190✔
543
  tEncodeSize(tEncodeSVDropTbBatchReq, &dropBatchReq, dropContLen, dropRet);
190✔
544
  if (dropRet < 0) {
190✔
NEW
545
    code = terrno;
×
NEW
546
    taosArrayDestroy(dropBatchReq.pArray);
×
NEW
547
    goto _OVER;
×
548
  }
549

550
  dropContLen += sizeof(SMsgHead);
190✔
551
  SMsgHead *pDropHead = taosMemoryCalloc(1, dropContLen);
190✔
552
  if (pDropHead == NULL) {
190✔
NEW
553
    code = terrno;
×
NEW
554
    taosArrayDestroy(dropBatchReq.pArray);
×
NEW
555
    goto _OVER;
×
556
  }
557
  pDropHead->contLen = htonl(dropContLen);
190✔
558
  pDropHead->vgId = htonl(pVgroup->vgId);
190✔
559

560
  SEncoder dropEncoder = {0};
190✔
561
  void *pDropBuf = POINTER_SHIFT(pDropHead, sizeof(SMsgHead));
190✔
562
  tEncoderInit(&dropEncoder, pDropBuf, dropContLen - sizeof(SMsgHead));
190✔
563
  code = tEncodeSVDropTbBatchReq(&dropEncoder, &dropBatchReq);
190✔
564
  tEncoderClear(&dropEncoder);
190✔
565
  taosArrayDestroy(dropBatchReq.pArray);
190✔
566
  if (code < 0) {
190✔
NEW
567
    taosMemoryFree(pDropHead);
×
NEW
568
    goto _OVER;
×
569
  }
570

571
  // Add undo action
572
  STransAction undoAction = {0};
190✔
573
  undoAction.epSet = mndGetVgroupEpset(pMnode, pVgroup);
190✔
574
  undoAction.pCont = pDropHead;
190✔
575
  undoAction.contLen = dropContLen;
190✔
576
  undoAction.msgType = TDMT_VND_DROP_TABLE;
190✔
577
  undoAction.acceptableCode = TSDB_CODE_TDB_TABLE_NOT_EXIST;
190✔
578

579
  code = mndTransAppendUndoAction(pTrans, &undoAction);
190✔
580
  if (code != TSDB_CODE_SUCCESS) {
190✔
NEW
581
    taosMemoryFree(pDropHead);
×
NEW
582
    goto _OVER;
×
583
  }
584

585
  mInfo("stream:%s created output normal table:%s in vgroup:%d", pStream->name, pStream->outTblName, pVgroup->vgId);
190✔
586

587
_OVER:
190✔
588
  // Free resources (note: pHead is owned by transaction, don't free it here)
589
  if (createReq.name) taosMemoryFree(createReq.name);
190✔
590
  if (createReq.ntb.schemaRow.pSchema) taosMemoryFree(createReq.ntb.schemaRow.pSchema);
190✔
591
  if (createReq.pExtSchemas) taosMemoryFree(createReq.pExtSchemas);
190✔
592
  if (createReq.colCmpr.pColCmpr) taosMemoryFreeClear(createReq.colCmpr.pColCmpr);
190✔
593

594
  mndReleaseVgroup(pMnode, pVgroup);
190✔
595
  mndReleaseDb(pMnode, pDb);
190✔
596

597
  if (code != TSDB_CODE_SUCCESS) {
190✔
NEW
598
    mError("stream:%s failed to create output normal table:%s, line:%d code:%s", pStream->name,
×
599
              pStream->outTblName, lino, tstrerror(code));
600
  }
601

602
  return code;
190✔
603
}
604

605
static int32_t mndStreamValidateCreate(SMnode *pMnode, SRpcMsg *pReq, SCMCreateStreamReq* pCreate) {
199,846✔
606
  int32_t code = 0, lino = 0;
199,846✔
607
  int64_t streamId = pCreate->streamId;
199,846✔
608
  char   *pUser = RPC_MSG_USER(pReq);
199,846✔
609

610
  if (pCreate->streamDB) {
199,846✔
611
    // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_WRITE_DB, pCreate->streamDB);
612
    code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
199,846✔
613
                                     pCreate->streamDB, false);
199,846✔
614
    if (code) {
199,846✔
UNCOV
615
      if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
616
      mstsError("user %s failed to create stream %s in db %s since %s", pUser, pCreate->name, pCreate->streamDB,
×
617
                tstrerror(code));
618
    }
619
    TSDB_CHECK_CODE(code, lino, _OVER);
199,846✔
620
  }
621

622
  if (pCreate->triggerDB) {
199,846✔
623
    // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_READ_DB, pCreate->triggerDB);
624
    code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
195,519✔
625
                                     pCreate->triggerDB, false);
195,519✔
626
    if (code) {
195,519✔
627
      if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
628
      mstsError("user %s failed to create stream %s using trigger db %s since %s", pUser, pCreate->name,
×
629
                pCreate->triggerDB, tstrerror(code));
630
    }
631
    TSDB_CHECK_CODE(code, lino, _OVER);
195,519✔
632
#if 0  // TODO check the owner of trigger table
633
    if (pCreate->triggerTblName) {
634
      // check trigger table privilege
635
      code = mndCheckObjPrivilegeRecF(pMnode, pUser, PRIV_TBL_SELECT, "", pCreate->triggerDB, pCreate->triggerTblName);
636
      if (code) {
637
        mstsError("user %s failed to create stream %s using trigger table %s.%s since %s", pUser, pCreate->name,
638
                  pCreate->triggerDB, pCreate->triggerTblName, tstrerror(code));
639
      }
640
      TSDB_CHECK_CODE(code, lino, _OVER);
641
    }
642
#endif
643
  }
644

645
  if (pCreate->calcDB) {
199,846✔
646
    int32_t dbNum = taosArrayGetSize(pCreate->calcDB);
196,706✔
647
    for (int32_t i = 0; i < dbNum; ++i) {
393,412✔
648
      char *calcDB = taosArrayGetP(pCreate->calcDB, i);
196,706✔
649
      // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_READ_DB, calcDB);
650
      code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB, calcDB, false);
196,706✔
651
      if (code) {
196,706✔
UNCOV
652
        if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
UNCOV
653
        mstsError("user %s failed to create stream %s using calcDB %s since %s", pUser, pCreate->name, calcDB,
×
654
                  tstrerror(code));
655
      }
656
      TSDB_CHECK_CODE(code, lino, _OVER);
196,706✔
657
    }
658
  }
659

660
  if (pCreate->outDB) {
199,846✔
661
    // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_WRITE_DB, pCreate->outDB);
662
    code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB, pCreate->outDB,
196,706✔
663
                                     false);
664
    if (code) {
196,706✔
UNCOV
665
      if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
UNCOV
666
      mstsError("user %s failed to create stream %s using out db %s since %s", pUser, pCreate->name, pCreate->outDB,
×
667
                tstrerror(code));
668
    }
669
    TSDB_CHECK_CODE(code, lino, _OVER);
196,706✔
670
  }
671

672
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
199,846✔
673
  if (streamNum > MND_STREAM_MAX_NUM) {
199,846✔
UNCOV
674
    code = TSDB_CODE_MND_TOO_MANY_STREAMS;
×
UNCOV
675
    mstsError("failed to create stream %s since %s, stream number:%d", pCreate->name, tstrerror(code), streamNum);
×
UNCOV
676
    return code;
×
677
  }
678

679
_OVER:
199,846✔
680

681
  return code;
199,846✔
682
}
683

684
int32_t mndDropStreamByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
721,536✔
685
  SSdb   *pSdb = pMnode->pSdb;
721,536✔
686
  void   *pIter = NULL;
721,536✔
687
  int32_t code = 0;
721,536✔
688

689
  while (1) {
30,701✔
690
    SStreamObj *pStream = NULL;
752,237✔
691
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
752,237✔
692
    if (pIter == NULL) break;
752,237✔
693

694
    if (0 == strcmp(pStream->pCreate->streamDB, pDb->name)) {
30,701✔
695
      mInfo("start to drop stream %s in db %s", pStream->pCreate->name, pDb->name);
19,167✔
696
      
697
      pStream->updateTime = taosGetTimestampMs();
38,334✔
698
      
699
      atomic_store_8(&pStream->userDropped, 1);
19,167✔
700
      
701
      MND_STREAM_SET_LAST_TS(STM_EVENT_DROP_STREAM, pStream->updateTime);
19,167✔
702
      
703
      msmUndeployStream(pMnode, pStream->pCreate->streamId, pStream->pCreate->name);
19,167✔
704
      
705
      // drop stream
706
      code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_DROPPED);
19,167✔
707
      if (code) {
19,167✔
UNCOV
708
        mError("drop db trans:%d failed to append drop stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
709
        sdbRelease(pSdb, pStream);
×
UNCOV
710
        sdbCancelFetch(pSdb, pIter);
×
UNCOV
711
        TAOS_RETURN(code);
×
712
      }
713
    }
714

715
    sdbRelease(pSdb, pStream);
30,701✔
716
  }
717

718
  return 0;
721,536✔
719
}
720

721
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
323,565✔
722
  SMnode     *pMnode = pReq->info.node;
323,565✔
723
  SSdb       *pSdb = pMnode->pSdb;
323,565✔
724
  int32_t     numOfRows = 0;
323,565✔
725
  SStreamObj *pStream = NULL;
323,565✔
726
  SUserObj   *pOperUser = NULL;
323,565✔
727
  int32_t     code = 0, lino = 0;
323,565✔
728
  bool        showAll = false;
323,565✔
729

730
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser));
323,565✔
731
  showAll =
323,565✔
732
      (0 == mndCheckObjPrivilegeRec(pMnode, pOperUser, PRIV_CM_SHOW, PRIV_OBJ_STREAM, 0, pOperUser->acctId, "*", "*"));
323,565✔
733

734
  while (numOfRows < rows) {
970,786✔
735
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
970,786✔
736
    if (pShow->pIter == NULL) break;
970,786✔
737

738
    if (!showAll) {
647,221✔
739
      if ((mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_SHOW, PRIV_OBJ_STREAM, pStream->ownerId,
3,926✔
740
                                    pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
3,926✔
741
        sdbRelease(pSdb, pStream);
3,624✔
742
        continue;
3,624✔
743
      }
744
    }
745

746
    code = mstSetStreamAttrResBlock(pMnode, pStream, pBlock, numOfRows);
643,597✔
747
    if (code == 0) {
643,597✔
748
      numOfRows++;
643,597✔
749
    }
750
    sdbRelease(pSdb, pStream);
643,597✔
751
  }
752
  code = 0;
323,565✔
753
  pShow->numOfRows += numOfRows;
323,565✔
754
_exit:
323,565✔
755
  mndReleaseUser(pMnode, pOperUser);
323,565✔
756
  if (code != 0) {
323,565✔
UNCOV
757
    mError("failed to retrieve stream list at line %d since %s", lino, tstrerror(code));
×
UNCOV
758
    TAOS_RETURN(code);
×
759
  }
760
  return numOfRows;
323,565✔
761
}
762

UNCOV
763
static void mndCancelGetNextStream(SMnode *pMnode, void *pIter) {
×
764
  SSdb *pSdb = pMnode->pSdb;
×
765
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
UNCOV
766
}
×
767

768
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
361,557✔
769
  SMnode     *pMnode = pReq->info.node;
361,557✔
770
  SSdb       *pSdb = pMnode->pSdb;
361,557✔
771
  int32_t     numOfRows = 0;
361,557✔
772
  SStreamObj *pStream = NULL;
361,557✔
773
  int32_t     code = 0;
361,557✔
774

775
  while (numOfRows < rowsCapacity) {
3,176,757✔
776
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
3,091,187✔
777
    if (pShow->pIter == NULL) {
3,091,187✔
778
      break;
275,987✔
779
    }
780

781
    code = mstSetStreamTasksResBlock(pStream, pBlock, &numOfRows, rowsCapacity);
2,815,200✔
782

783
    sdbRelease(pSdb, pStream);
2,815,200✔
784
  }
785

786
  pShow->numOfRows += numOfRows;
361,557✔
787
  return numOfRows;
361,557✔
788
}
789

UNCOV
790
static void mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter) {
×
UNCOV
791
  SSdb *pSdb = pMnode->pSdb;
×
UNCOV
792
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
UNCOV
793
}
×
794

795
static int32_t mndRetrieveStreamRecalculates(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
283✔
796
  SMnode     *pMnode = pReq->info.node;
283✔
797
  SSdb       *pSdb = pMnode->pSdb;
283✔
798
  int32_t     numOfRows = 0;
283✔
799
  SStreamObj *pStream = NULL;
283✔
800
  int32_t     code = 0;
283✔
801

802
  while (numOfRows < rowsCapacity) {
566✔
803
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
566✔
804
    if (pShow->pIter == NULL) {
566✔
805
      break;
283✔
806
    }
807

808
    code = mstSetStreamRecalculatesResBlock(pStream, pBlock, &numOfRows, rowsCapacity);
283✔
809

810
    sdbRelease(pSdb, pStream);
283✔
811
  }
812

813
  pShow->numOfRows += numOfRows;
283✔
814
  return numOfRows;
283✔
815
}
816

UNCOV
817
static void mndCancelGetNextStreamRecalculates(SMnode *pMnode, void *pIter) {
×
818
  SSdb *pSdb = pMnode->pSdb;
×
819
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
820
}
×
821

822

823
static bool mndStreamUpdateTagsFlag(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
2,244,340✔
824
  SStreamObj *pStream = pObj;
2,244,340✔
825
  if (atomic_load_8(&pStream->userDropped)) {
2,244,340✔
UNCOV
826
    return true;
×
827
  }
828

829
  if (TSDB_SUPER_TABLE != pStream->pCreate->triggerTblType && 
2,244,340✔
830
      TSDB_CHILD_TABLE != pStream->pCreate->triggerTblType && 
1,260,579✔
831
      TSDB_VIRTUAL_CHILD_TABLE != pStream->pCreate->triggerTblType) {
870,192✔
832
    return true;
112,038✔
833
  }
834

835
  if (pStream->pCreate->triggerTblSuid != *(uint64_t*)p1) {
2,132,302✔
836
    return true;
2,049,835✔
837
  }
838

839
  if (NULL == pStream->pCreate->partitionCols) {
82,467✔
840
    return true;
44,117✔
841
  }
842

843
  SNodeList* pList = NULL;
38,350✔
844
  int32_t code = nodesStringToList(pStream->pCreate->partitionCols, &pList);
38,350✔
845
  if (code) {
38,350✔
846
    nodesDestroyList(pList);
×
847
    mstError("partitionCols [%s] nodesStringToList failed with error:%s", (char*)pStream->pCreate->partitionCols, tstrerror(code));
×
UNCOV
848
    return true;
×
849
  }
850

851
  SSchema* pTags = (SSchema*)p2;
38,350✔
852
  int32_t* tagNum = (int32_t*)p3;
38,350✔
853

854
  SNode* pNode = NULL;
38,350✔
855
  FOREACH(pNode, pList) {
130,285✔
856
    SColumnNode* pCol = (SColumnNode*)pNode;
91,935✔
857
    for (int32_t i = 0; i < *tagNum; ++i) {
283,804✔
858
      if (pCol->colId == pTags[i].colId) {
269,857✔
859
        pTags[i].flags |= COL_REF_BY_STM;
77,988✔
860
        break;
77,988✔
861
      }
862
    }
863
  }
864

865
  nodesDestroyList(pList);
38,350✔
866
  
867
  return true;
38,350✔
868
}
869

870

871
void mndStreamUpdateTagsRefFlag(SMnode *pMnode, int64_t suid, SSchema* pTags, int32_t tagNum) {
16,170,369✔
872
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
16,170,369✔
873
  if (streamNum <= 0) {
16,170,369✔
874
    return;
15,979,730✔
875
  }
876

877
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mndStreamUpdateTagsFlag, &suid, pTags, &tagNum);
190,639✔
878
}
879

880
static int32_t mndProcessStopStreamReq(SRpcMsg *pReq) {
2,910✔
881
  SMnode     *pMnode = pReq->info.node;
2,910✔
882
  SStreamObj *pStream = NULL;
2,910✔
883
  SUserObj   *pOperUser = NULL;
2,910✔
884
  int32_t     code = 0;
2,910✔
885

886
  SMPauseStreamReq pauseReq = {0};
2,910✔
887
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
2,910✔
UNCOV
888
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
889
  }
890

891
  code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
2,910✔
892
  if (pStream == NULL || code != 0) {
2,910✔
893
    if (pauseReq.igNotExists) {
×
UNCOV
894
      mInfo("stream:%s, not exist, not stop stream", pauseReq.name);
×
UNCOV
895
      taosMemoryFree(pauseReq.name);
×
UNCOV
896
      return 0;
×
897
    } else {
UNCOV
898
      mError("stream:%s not exist, failed to stop stream", pauseReq.name);
×
UNCOV
899
      taosMemoryFree(pauseReq.name);
×
UNCOV
900
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
901
    }
902
  }
903

904
  taosMemoryFree(pauseReq.name);
2,910✔
905

906
  int64_t streamId = pStream->pCreate->streamId;
2,910✔
907
  
908
  mstsInfo("start to stop stream %s", pStream->name);
2,910✔
909

910
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
911
  if((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
2,910✔
UNCOV
912
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
913
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
914
    return code;
×
915
  }
916
  if ((code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
2,910✔
917
                                        pStream->pCreate->streamDB, false))) {
2,910✔
918
    if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
919
  }
920
  if ((code != TSDB_CODE_SUCCESS) ||
5,820✔
921
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_STOP, PRIV_OBJ_STREAM, pStream->ownerId,
2,910✔
922
                                       pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
2,910✔
923
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
338✔
924
    mndReleaseUser(pMnode, pOperUser);
338✔
925
    sdbRelease(pMnode->pSdb, pStream);
338✔
926
    return code;
338✔
927
  }
928

929
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
2,572✔
930

931
  if (atomic_load_8(&pStream->userDropped)) {
2,572✔
UNCOV
932
    code = TSDB_CODE_MND_STREAM_DROPPING;
×
UNCOV
933
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
934
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
935
    return code;
×
936
  }
937

938
  STrans *pTrans = NULL;
2,572✔
939
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_STOP_NAME, &pTrans);
2,572✔
940
  if (pTrans == NULL || code) {
2,572✔
941
    mstsError("failed to stop stream %s since %s", pStream->name, tstrerror(code));
×
UNCOV
942
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
943
    return code;
×
944
  }
945

946
  pStream->updateTime = taosGetTimestampMs();
5,144✔
947

948
  atomic_store_8(&pStream->userStopped, 1);
2,572✔
949

950
  MND_STREAM_SET_LAST_TS(STM_EVENT_STOP_STREAM, pStream->updateTime);
2,572✔
951

952
  msmUndeployStream(pMnode, streamId, pStream->name);
2,572✔
953

954
  // stop stream
955
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
2,572✔
956
  if (code != TSDB_CODE_SUCCESS) {
2,572✔
UNCOV
957
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
958
    mndTransDrop(pTrans);
×
UNCOV
959
    return code;
×
960
  }
961

962
  code = mndTransPrepare(pMnode, pTrans);
2,572✔
963
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2,572✔
UNCOV
964
    mError("trans:%d, failed to prepare stop stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
965
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
966
    mndTransDrop(pTrans);
×
UNCOV
967
    return code;
×
968
  }
969

970
  sdbRelease(pMnode->pSdb, pStream);
2,572✔
971
  mndTransDrop(pTrans);
2,572✔
972

973
  return TSDB_CODE_ACTION_IN_PROGRESS;
2,572✔
974
}
975

976

977
static int32_t mndProcessStartStreamReq(SRpcMsg *pReq) {
2,536✔
978
  SMnode     *pMnode = pReq->info.node;
2,536✔
979
  SStreamObj *pStream = NULL;
2,536✔
980
  SUserObj   *pOperUser = NULL;
2,536✔
981
  int32_t     code = 0;
2,536✔
982

983
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
2,536✔
UNCOV
984
    return code;
×
985
  }
986

987
  SMResumeStreamReq resumeReq = {0};
2,536✔
988
  if (tDeserializeSMResumeStreamReq(pReq->pCont, pReq->contLen, &resumeReq) < 0) {
2,536✔
UNCOV
989
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
990
  }
991

992
  code = mndAcquireStream(pMnode, resumeReq.name, &pStream);
2,536✔
993
  if (pStream == NULL || code != 0) {
2,536✔
UNCOV
994
    if (resumeReq.igNotExists) {
×
UNCOV
995
      mInfo("stream:%s not exist, not start stream", resumeReq.name);
×
UNCOV
996
      taosMemoryFree(resumeReq.name);
×
UNCOV
997
      sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
998
      return 0;
×
999
    } else {
UNCOV
1000
      mError("stream:%s not exist, failed to start stream", resumeReq.name);
×
UNCOV
1001
      taosMemoryFree(resumeReq.name);
×
UNCOV
1002
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1003
    }
1004
  }
1005

1006
  taosMemoryFree(resumeReq.name);
2,536✔
1007

1008
  int64_t streamId = pStream->pCreate->streamId;
2,536✔
1009

1010
  mstsInfo("start to start stream %s from stopped", pStream->name);
2,536✔
1011

1012
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
1013
  if ((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
2,536✔
UNCOV
1014
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
1015
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1016
    return code;
×
1017
  }
1018
  if ((code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
2,536✔
1019
                                        pStream->pCreate->streamDB, false))) {
2,536✔
UNCOV
1020
    if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
1021
  }
1022
  if ((code != TSDB_CODE_SUCCESS) ||
5,072✔
1023
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_START, PRIV_OBJ_STREAM, pStream->ownerId,
2,536✔
1024
                                       pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
2,536✔
1025
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
151✔
1026
    mndReleaseUser(pMnode, pOperUser);
151✔
1027
    sdbRelease(pMnode->pSdb, pStream);
151✔
1028
    return code;
151✔
1029
  }
1030

1031
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
2,385✔
1032

1033
  if (atomic_load_8(&pStream->userDropped)) {
2,385✔
UNCOV
1034
    code = TSDB_CODE_MND_STREAM_DROPPING;
×
UNCOV
1035
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
1036
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1037
    return code;
×
1038
  }
1039

1040
  if (0 == atomic_load_8(&pStream->userStopped)) {
2,385✔
UNCOV
1041
    code = TSDB_CODE_MND_STREAM_NOT_STOPPED;
×
UNCOV
1042
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
1043
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1044
    return code;
×
1045
  }
1046
  
1047
  atomic_store_8(&pStream->userStopped, 0);
2,385✔
1048

1049
  pStream->updateTime = taosGetTimestampMs();
4,770✔
1050

1051
  MND_STREAM_SET_LAST_TS(STM_EVENT_START_STREAM, pStream->updateTime);
2,385✔
1052

1053
  STrans *pTrans = NULL;
2,385✔
1054
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_START_NAME, &pTrans);
2,385✔
1055
  if (pTrans == NULL || code) {
2,385✔
1056
    mstsError("failed to start stream %s since %s", pStream->name, tstrerror(code));
×
1057
    sdbRelease(pMnode->pSdb, pStream);
×
1058
    return code;
×
1059
  }
1060

1061
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
2,385✔
1062
  if (code != TSDB_CODE_SUCCESS) {
2,385✔
UNCOV
1063
    mstsError("failed to start stream %s since %s", pStream->name, tstrerror(code));
×
UNCOV
1064
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1065
    mndTransDrop(pTrans);
×
UNCOV
1066
    return code;
×
1067
  }
1068

1069
  code = mndTransPrepare(pMnode, pTrans);
2,385✔
1070
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2,385✔
UNCOV
1071
    mstsError("trans:%d, failed to prepare start stream %s trans since %s", pTrans->id, pStream->name, tstrerror(code));
×
UNCOV
1072
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1073
    mndTransDrop(pTrans);
×
UNCOV
1074
    return code;
×
1075
  }
1076

1077
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->name, NULL, true, STREAM_ACT_DEPLOY);
2,385✔
1078

1079
  sdbRelease(pMnode->pSdb, pStream);
2,385✔
1080
  mndTransDrop(pTrans);
2,385✔
1081

1082
  return TSDB_CODE_ACTION_IN_PROGRESS;
2,385✔
1083
}
1084

1085
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) {
21,636✔
1086
  SMnode     *pMnode = pReq->info.node;
21,636✔
1087
  SStreamObj *pStream = NULL;
21,636✔
1088
  SUserObj   *pOperUser = NULL;
21,636✔
1089
  int32_t     code = 0;
21,636✔
1090
  int32_t     notExistNum = 0;
21,636✔
1091

1092
  SMDropStreamReq dropReq = {0};
21,636✔
1093
  int64_t         tss = taosGetTimestampMs();
21,636✔
1094
  if (tDeserializeSMDropStreamReq(pReq->pCont, pReq->contLen, &dropReq) < 0) {
21,636✔
UNCOV
1095
    mError("invalid drop stream msg recv, discarded");
×
UNCOV
1096
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1097
    TAOS_RETURN(code);
×
1098
  }
1099

1100
  mDebug("recv drop stream msg, count:%d", dropReq.count);
21,636✔
1101

1102
  // Acquire user object for privilege check
1103
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
21,636✔
1104
  if (code != 0) {
21,636✔
UNCOV
1105
    tFreeMDropStreamReq(&dropReq);
×
UNCOV
1106
    TAOS_RETURN(code);
×
1107
  }
1108

1109
  // check if all streams exist
1110
  if (!dropReq.igNotExists) {
21,636✔
1111
    for (int32_t i = 0; i < dropReq.count; i++) {
31,289✔
1112
      if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, dropReq.name[i])) {
16,342✔
1113
        mError("stream:%s not exist failed to drop it", dropReq.name[i]);
1,116✔
1114
        mndReleaseUser(pMnode, pOperUser);
1,116✔
1115
        tFreeMDropStreamReq(&dropReq);
1,116✔
1116
        TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
1,116✔
1117
      }
1118
    }
1119
  }
1120

1121
  // Create a single transaction for all stream drops
1122
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pReq, MND_STREAM_DROP_NAME);
20,520✔
1123
  if (pTrans == NULL) {
20,520✔
UNCOV
1124
    mError("failed to create drop stream transaction since %s", tstrerror(terrno));
×
UNCOV
1125
    code = terrno;
×
UNCOV
1126
    mndReleaseUser(pMnode, pOperUser);
×
UNCOV
1127
    tFreeMDropStreamReq(&dropReq);
×
UNCOV
1128
    TAOS_RETURN(code);
×
1129
  }
1130
  pTrans->ableToBeKilled = true;
20,520✔
1131

1132
  // Process all streams and add them to the transaction
1133
  for (int32_t i = 0; i < dropReq.count; i++) {
42,376✔
1134
    char *streamName = dropReq.name[i];
22,194✔
1135
    mDebug("drop stream[%d/%d]: %s", i + 1, dropReq.count, streamName);
22,194✔
1136

1137
    code = mndAcquireStream(pMnode, streamName, &pStream);
22,194✔
1138
    if (pStream == NULL || code != 0) {
22,194✔
1139
      mWarn("stream:%s not exist, ignore not exist is set, drop stream exec done with success", streamName);
5,308✔
1140
      sdbRelease(pMnode->pSdb, pStream);
5,308✔
1141
      pStream = NULL;
5,308✔
1142
      notExistNum++;
5,308✔
1143
      continue;
5,308✔
1144
    }
1145

1146
    int64_t streamId = pStream->pCreate->streamId;
16,886✔
1147

1148
    if ((code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
16,886✔
1149
                                          pStream->pCreate->streamDB, true))) {
16,886✔
UNCOV
1150
      if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
1151
    }
1152
    if ((code != TSDB_CODE_SUCCESS) ||
33,772✔
1153
        (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_DROP, PRIV_OBJ_STREAM, pStream->ownerId,
16,886✔
1154
                                         pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
16,886✔
1155
      mstsError("user %s failed to drop stream %s since %s", pReq->info.conn.user, streamName, tstrerror(code));
338✔
1156
      sdbRelease(pMnode->pSdb, pStream);
338✔
1157
      pStream = NULL;
338✔
1158
      mndTransDrop(pTrans);
338✔
1159
      pTrans = NULL;
338✔
1160
      goto _OVER;
338✔
1161
    }
1162

1163
    if (pStream->pCreate->tsmaId != 0) {
16,548✔
1164
      mstsDebug("try to drop tsma related stream, tsmaId:%" PRIx64, pStream->pCreate->tsmaId);
2,545✔
1165

1166
      void    *pIter = NULL;
2,545✔
1167
      SSmaObj *pSma = NULL;
2,545✔
1168
      pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
2,545✔
1169
      while (pIter) {
5,108✔
1170
        if (pSma && pSma->uid == pStream->pCreate->tsmaId) {
2,563✔
UNCOV
1171
          sdbRelease(pMnode->pSdb, pSma);
×
UNCOV
1172
          sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1173
          pStream = NULL;
×
1174

UNCOV
1175
          sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
1176
          code = TSDB_CODE_TSMA_MUST_BE_DROPPED;
×
1177

1178
          mstsError("refused to drop tsma-related stream %s since tsma still exists", streamName);
×
1179
          mndTransDrop(pTrans);
×
UNCOV
1180
          pTrans = NULL;
×
UNCOV
1181
          goto _OVER;
×
1182
        }
1183

1184
        if (pSma) {
2,563✔
1185
          sdbRelease(pMnode->pSdb, pSma);
2,563✔
1186
        }
1187

1188
        pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
2,563✔
1189
      }
1190
    }
1191

1192
    mstsInfo("start to drop stream %s", pStream->pCreate->name);
16,548✔
1193

1194
    pStream->updateTime = taosGetTimestampMs();
33,096✔
1195

1196
    atomic_store_8(&pStream->userDropped, 1);
16,548✔
1197

1198
    MND_STREAM_SET_LAST_TS(STM_EVENT_DROP_STREAM, pStream->updateTime);
16,548✔
1199

1200
    msmUndeployStream(pMnode, streamId, pStream->pCreate->name);
16,548✔
1201

1202
    // Append drop stream operation to the transaction
1203
    code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_DROPPED);
16,548✔
1204
    if (code) {
16,548✔
UNCOV
1205
      mstsError("trans:%d, failed to append drop stream %s trans since %s", pTrans->id, streamName, tstrerror(code));
×
1206
      sdbRelease(pMnode->pSdb, pStream);
×
1207
      pStream = NULL;
×
1208
      // mndStreamTransAppend already called mndTransDrop on failure, set pTrans to NULL to avoid double free
UNCOV
1209
      pTrans = NULL;
×
UNCOV
1210
      goto _OVER;
×
1211
    }
1212

1213
    sdbRelease(pMnode->pSdb, pStream);
16,548✔
1214
    pStream = NULL;
16,548✔
1215

1216
    mstsDebug("drop stream %s added to transaction", streamName);
16,548✔
1217
  }
1218

1219
  // Prepare and execute the transaction for all streams
1220
  if (notExistNum < dropReq.count) {
20,182✔
1221
    code = mndTransPrepare(pMnode, pTrans);
15,711✔
1222
    if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
15,711✔
1223
      mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
1224
      mndTransDrop(pTrans);
×
UNCOV
1225
      goto _OVER;
×
1226
    }
1227
    mInfo("trans:%d, drop stream transaction prepared for %d streams", pTrans->id, dropReq.count - notExistNum);
15,711✔
1228
  } else {
1229
    // All streams don't exist, no need to prepare transaction
1230
    mndTransDrop(pTrans);
4,471✔
1231
    pTrans = NULL;
4,471✔
1232
  }
1233

1234
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE && notExistNum < dropReq.count) {
20,182✔
1235
    int64_t tse = taosGetTimestampMs();
15,711✔
1236
    double  duration = (double)(tse - tss);
15,711✔
1237
    duration = duration / 1000;
15,711✔
1238
    // Use first stream's database for audit (assuming all streams are from same db in batch)
1239
    if (dropReq.count > 0) {
15,711✔
1240
      SStreamObj *pFirstStream = NULL;
15,711✔
1241
      if (mndAcquireStream(pMnode, dropReq.name[0], &pFirstStream) == 0 && pFirstStream != NULL) {
15,711✔
UNCOV
1242
        auditRecord(pReq, pMnode->clusterId, "dropStream", "", pFirstStream->pCreate->streamDB, NULL, 0, duration, 0);
×
UNCOV
1243
        sdbRelease(pMnode->pSdb, pFirstStream);
×
1244
      }
1245
    }
1246
  }
1247

1248
  // If any stream was successfully added to transaction, return ACTION_IN_PROGRESS
1249
  // Otherwise, all streams don't exist (and igNotExists is set), return SUCCESS
1250
  code = (notExistNum < dropReq.count) ? TSDB_CODE_ACTION_IN_PROGRESS : TSDB_CODE_SUCCESS;
20,182✔
1251

1252
_OVER:
20,520✔
1253
  if (pStream) {
20,520✔
1254
    sdbRelease(pMnode->pSdb, pStream);
×
1255
  }
1256
  if (pTrans) {
20,520✔
1257
    mndTransDrop(pTrans);
15,711✔
1258
  }
1259
  mndReleaseUser(pMnode, pOperUser);
20,520✔
1260
  tFreeMDropStreamReq(&dropReq);
20,520✔
1261
  TAOS_RETURN(code);
20,520✔
1262
}
1263

1264
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq) {
208,196✔
1265
  SMnode     *pMnode = pReq->info.node;
208,196✔
1266
  SStreamObj *pStream = NULL;
208,196✔
1267
  SStreamObj  streamObj = {0};
208,196✔
1268
  SUserObj    *pOperUser = NULL;
208,196✔
1269
  int32_t     code = TSDB_CODE_SUCCESS;
208,196✔
1270
  int32_t     lino = 0;
208,196✔
1271
  STrans     *pTrans = NULL;
208,196✔
1272
  uint64_t    streamId = 0;
208,196✔
1273
  SCMCreateStreamReq* pCreate = NULL;
208,196✔
1274
  int64_t             tss = taosGetTimestampMs();
208,196✔
1275

1276
  if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
208,196✔
UNCOV
1277
    goto _OVER;
×
1278
  }
1279

1280
  pCreate = taosMemoryCalloc(1, sizeof(SCMCreateStreamReq));
208,196✔
1281
  TSDB_CHECK_NULL(pCreate, code, lino, _OVER, terrno);
208,196✔
1282
  
1283
  code = tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, pCreate);
208,196✔
1284
  TSDB_CHECK_CODE(code, lino, _OVER);
208,196✔
1285

1286
  streamId = pCreate->streamId;
208,196✔
1287

1288
  mstsInfo("start to create stream %s, sql:%s", pCreate->name, pCreate->sql);
208,196✔
1289

1290
  int32_t snodeId = msmAssignRandomSnodeId(pMnode, streamId);
208,196✔
1291
  if (!GOT_SNODE(snodeId)) {
208,196✔
1292
    code = terrno;
2,790✔
1293
    TSDB_CHECK_CODE(code, lino, _OVER);
2,790✔
1294
  }
1295
  
1296
  code = mndAcquireStream(pMnode, pCreate->name, &pStream);
205,406✔
1297
  if (pStream != NULL && code == 0) {
205,406✔
1298
    if (pCreate->igExists) {
5,560✔
1299
      mstsInfo("stream %s already exist, ignore exist is set", pCreate->name);
×
1300
    } else {
1301
      code = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
5,560✔
1302
    }
1303

1304
    mndReleaseStream(pMnode, pStream);
5,560✔
1305
    goto _OVER;
5,560✔
1306
  } else if (code != TSDB_CODE_MND_STREAM_NOT_EXIST) {
199,846✔
1307
    goto _OVER;
×
1308
  }
1309

1310
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
199,846✔
1311
  if (pOperUser == NULL) {
199,846✔
UNCOV
1312
    TSDB_CHECK_CODE(TSDB_CODE_MND_NO_USER_FROM_CONN, lino, _OVER);
×
1313
  }
1314

1315
  code = mndStreamValidateCreate(pMnode, pReq, pCreate);
199,846✔
1316
  TSDB_CHECK_CODE(code, lino, _OVER);
199,846✔
1317

1318
  mndStreamBuildObj(pMnode, &streamObj, pCreate, pOperUser, snodeId);
199,846✔
1319
  pCreate = NULL;
199,846✔
1320

1321
  pStream = &streamObj;
199,846✔
1322

1323
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_DB, MND_STREAM_CREATE_NAME, &pTrans);
199,846✔
1324
  if (pTrans == NULL || code) {
199,846✔
UNCOV
1325
    goto _OVER;
×
1326
  }
1327

1328
  // create output table for stream if it doesn't exist
1329
  if (!pStream->pCreate->outStbExists) {
199,846✔
1330
    if (TSDB_SUPER_TABLE == pStream->pCreate->outTblType) {
195,742✔
1331
      // Create super table in mnode
1332
      pStream->pCreate->outStbUid = mndGenerateUid(pStream->pCreate->outTblName, strlen(pStream->pCreate->outTblName));
73,133✔
1333
      code = mndStreamCreateOutStb(pMnode, pTrans, pStream->pCreate, RPC_MSG_USER(pReq));
73,133✔
1334
      TSDB_CHECK_CODE(code, lino, _OVER);
73,133✔
1335
      mstsInfo("stream:%s created output super table:%s", pStream->pCreate->name, pStream->pCreate->outTblName);
73,133✔
1336
    } else if (TSDB_NORMAL_TABLE == pStream->pCreate->outTblType && pStream->pCreate->nodelayCreateSubtable) {
122,609✔
1337
      // Create normal table in vnode
1338
      code = mndStreamCreateOutTable(pMnode, pTrans, pStream->pCreate);
190✔
1339
      TSDB_CHECK_CODE(code, lino, _OVER);
190✔
1340
      mstsInfo("stream:%s created output normal table:%s", pStream->pCreate->name, pStream->pCreate->outTblName);
190✔
1341
    }
1342
  } else {
1343
    // Table exists, schema validation should have been done in client side
1344
    mstsInfo("stream:%s output table:%s already exists, using existing table",
4,104✔
1345
             pStream->pCreate->name, pStream->pCreate->outTblName);
1346
  }
1347

1348
  // add stream to trans
1349
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
199,846✔
1350
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
199,846✔
1351
    mstsError("failed to persist stream %s since %s", pStream->pCreate->name, tstrerror(code));
×
1352
    goto _OVER;
×
1353
  }
1354

1355
  // execute creation
1356
  code = mndTransPrepare(pMnode, pTrans);
199,846✔
1357
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
199,846✔
UNCOV
1358
    mstsError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
UNCOV
1359
    goto _OVER;
×
1360
  }
1361
  code = TSDB_CODE_ACTION_IN_PROGRESS;
199,846✔
1362

1363
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE) {
199,846✔
1364
    int64_t tse = taosGetTimestampMs();
199,846✔
1365
    double  duration = (double)(tse - tss);
199,846✔
1366
    duration = duration / 1000;
199,846✔
1367
    auditRecord(pReq, pMnode->clusterId, "createStream", pStream->pCreate->streamDB, pStream->pCreate->name,
199,846✔
1368
                pStream->pCreate->sql, strlen(pStream->pCreate->sql), duration, 0);
199,846✔
1369
  }
1370

1371
  MND_STREAM_SET_LAST_TS(STM_EVENT_CREATE_STREAM, taosGetTimestampMs());
377,020✔
1372

1373
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, true, STREAM_ACT_DEPLOY);
199,846✔
1374

1375
_OVER:
208,196✔
1376

1377
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
208,196✔
1378
    if (pStream && pStream->pCreate) {
8,350✔
1379
      mstsError("failed to create stream %s at line:%d since %s", pStream->pCreate->name, lino, tstrerror(code));
5,560✔
1380
    } else {
1381
      mstsError("failed to create stream at line:%d since %s", lino, tstrerror(code));
2,790✔
1382
    }
1383
  } else {
1384
    mstsDebug("create stream %s half completed", pStream->pCreate ? pStream->pCreate->name : "unknown");
199,846✔
1385
  }
1386

1387
  tFreeSCMCreateStreamReq(pCreate);
208,196✔
1388
  taosMemoryFreeClear(pCreate);
208,196✔
1389

1390
  mndTransDrop(pTrans);
208,196✔
1391
  tFreeStreamObj(&streamObj);
208,196✔
1392
  mndReleaseUser(pMnode, pOperUser);
208,196✔
1393

1394
  return code;
208,196✔
1395
}
1396

1397
static int32_t mndProcessRecalcStreamReq(SRpcMsg *pReq) {
12,424✔
1398
  SMnode     *pMnode = pReq->info.node;
12,424✔
1399
  SStreamObj *pStream = NULL;
12,424✔
1400
  SUserObj   *pOperUser = NULL;
12,424✔
1401
  int32_t     code = 0;
12,424✔
1402
  int64_t     tss = taosGetTimestampMs();
12,424✔
1403

1404
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
12,424✔
1405
    return code;
×
1406
  }
1407

1408
  SMRecalcStreamReq recalcReq = {0};
12,424✔
1409
  if (tDeserializeSMRecalcStreamReq(pReq->pCont, pReq->contLen, &recalcReq) < 0) {
12,424✔
UNCOV
1410
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1411
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1412
  }
1413

1414
  code = mndAcquireStream(pMnode, recalcReq.name, &pStream);
12,424✔
1415
  if (pStream == NULL || code != 0) {
12,424✔
UNCOV
1416
    mError("stream:%s not exist, failed to recalc stream", recalcReq.name);
×
UNCOV
1417
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1418
    TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1419
  }
1420

1421
  int64_t streamId = pStream->pCreate->streamId;
12,424✔
1422
  
1423
  mstsInfo("start to recalc stream %s", recalcReq.name);
12,424✔
1424

1425
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
1426
  // if (code != TSDB_CODE_SUCCESS) {
1427
  //   mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
1428
  //   sdbRelease(pMnode->pSdb, pStream);
1429
  //   tFreeMRecalcStreamReq(&recalcReq);
1430
  //   return code;
1431
  // }
1432

1433
  if ((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
12,424✔
UNCOV
1434
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
UNCOV
1435
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1436
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1437
    TAOS_RETURN(code);
×
1438
  }
1439

1440
  if ((code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
12,424✔
1441
                                        pStream->pCreate->streamDB, false))) {
12,424✔
1442
    if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
2,800✔
1443
  }
1444
  if ((code != TSDB_CODE_SUCCESS) ||
22,048✔
1445
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_RECALC, PRIV_OBJ_STREAM, pStream->ownerId,
9,624✔
1446
                                       pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
9,624✔
1447
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
8,551✔
1448
    mndReleaseUser(pMnode, pOperUser);
8,551✔
1449
    sdbRelease(pMnode->pSdb, pStream);
8,551✔
1450
    tFreeMRecalcStreamReq(&recalcReq);
8,551✔
1451
    return code;
8,551✔
1452
  }
1453

1454
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
3,873✔
1455

1456
  if (atomic_load_8(&pStream->userDropped)) {
3,873✔
1457
    code = TSDB_CODE_MND_STREAM_DROPPING;
×
1458
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
×
1459
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1460
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1461
    return code;
×
1462
  }
1463

1464
  if (atomic_load_8(&pStream->userStopped)) {
3,873✔
UNCOV
1465
    code = TSDB_CODE_MND_STREAM_STOPPED;
×
UNCOV
1466
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
×
UNCOV
1467
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1468
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1469
    return code;
×
1470
  }
1471

1472
  if (WINDOW_TYPE_PERIOD == pStream->pCreate->triggerType) {
3,873✔
1473
    code = TSDB_CODE_OPS_NOT_SUPPORT;
213✔
1474
    mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
213✔
1475
    sdbRelease(pMnode->pSdb, pStream);
213✔
1476
    tFreeMRecalcStreamReq(&recalcReq);
213✔
1477
    return code;
213✔
1478
  }
1479

1480
  /*
1481
  pStream->updateTime = taosGetTimestampMs();
1482

1483
  STrans *pTrans = NULL;
1484
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RECALC_NAME, &pTrans);
1485
  if (pTrans == NULL || code) {
1486
    mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
1487
    sdbRelease(pMnode->pSdb, pStream);
1488
    return code;
1489
  }
1490

1491
  // stop stream
1492
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
1493
  if (code != TSDB_CODE_SUCCESS) {
1494
    sdbRelease(pMnode->pSdb, pStream);
1495
    mndTransDrop(pTrans);
1496
    return code;
1497
  }
1498

1499
  code = mndTransPrepare(pMnode, pTrans);
1500
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1501
    mError("trans:%d, failed to prepare stop stream trans since %s", pTrans->id, tstrerror(code));
1502
    sdbRelease(pMnode->pSdb, pStream);
1503
    mndTransDrop(pTrans);
1504
    return code;
1505
  }
1506
*/
1507

1508
  code = msmRecalcStream(pMnode, pStream->pCreate->streamId, &recalcReq.timeRange);
3,660✔
1509
  if (code != TSDB_CODE_SUCCESS) {
3,660✔
UNCOV
1510
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1511
    tFreeMRecalcStreamReq(&recalcReq);
×
UNCOV
1512
    return code;
×
1513
  }
1514

1515
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE){
3,660✔
1516
    char buf[128];
3,660✔
1517
    snprintf(buf, sizeof(buf), "start:%" PRId64 ", end:%" PRId64, recalcReq.timeRange.skey, recalcReq.timeRange.ekey);
3,660✔
1518
    int64_t tse = taosGetTimestampMs();
3,660✔
1519
    double  duration = (double)(tse - tss);
3,660✔
1520
    duration = duration / 1000;
3,660✔
1521
    auditRecord(pReq, pMnode->clusterId, "recalcStream", pStream->name, recalcReq.name, buf, strlen(buf), duration, 0);
3,660✔
1522
  }  
1523

1524
  sdbRelease(pMnode->pSdb, pStream);
3,660✔
1525
  tFreeMRecalcStreamReq(&recalcReq);
3,660✔
1526
//  mndTransDrop(pTrans);
1527

1528
  return TSDB_CODE_SUCCESS;
3,660✔
1529
}
1530

1531

1532
int32_t mndInitStream(SMnode *pMnode) {
477,523✔
1533
  SSdbTable table = {
477,523✔
1534
      .sdbType = SDB_STREAM,
1535
      .keyType = SDB_KEY_BINARY,
1536
      .encodeFp = (SdbEncodeFp)mndStreamActionEncode,
1537
      .decodeFp = (SdbDecodeFp)mndStreamActionDecode,
1538
      .insertFp = (SdbInsertFp)mndStreamActionInsert,
1539
      .updateFp = (SdbUpdateFp)mndStreamActionUpdate,
1540
      .deleteFp = (SdbDeleteFp)mndStreamActionDelete,
1541
  };
1542

1543
  if (!tsDisableStream) {
477,523✔
1544
    mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STREAM, mndProcessCreateStreamReq);
477,523✔
1545
    mndSetMsgHandle(pMnode, TDMT_MND_DROP_STREAM, mndProcessDropStreamReq);
477,523✔
1546
    mndSetMsgHandle(pMnode, TDMT_MND_START_STREAM, mndProcessStartStreamReq);
477,523✔
1547
    mndSetMsgHandle(pMnode, TDMT_MND_STOP_STREAM, mndProcessStopStreamReq);
477,523✔
1548
    mndSetMsgHandle(pMnode, TDMT_MND_STREAM_HEARTBEAT, mndProcessStreamHb);  
477,523✔
1549
    mndSetMsgHandle(pMnode, TDMT_MND_RECALC_STREAM, mndProcessRecalcStreamReq);
477,523✔
1550
  }
1551
  
1552
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndRetrieveStream);
477,523✔
1553
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndCancelGetNextStream);
477,523✔
1554
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndRetrieveStreamTask);
477,523✔
1555
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndCancelGetNextStreamTask);
477,523✔
1556
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndRetrieveStreamRecalculates);
477,523✔
1557
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndCancelGetNextStreamRecalculates);
477,523✔
1558

1559
  int32_t code = sdbSetTable(pMnode->pSdb, table);
477,523✔
1560
  if (code) {
477,523✔
UNCOV
1561
    return code;
×
1562
  }
1563

1564
  //code = sdbSetTable(pMnode->pSdb, tableSeq);
1565
  return code;
477,523✔
1566
}
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