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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

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

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

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

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

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

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

95
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
×
96

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

100
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
×
101

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

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

111
_over:
×
112
  taosMemoryFreeClear(buf);
×
113

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

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

124
    terrno = 0;
×
125
    return pRow;
×
126
  }
127
}
128

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

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

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

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

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

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

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

169
  return true;
×
170
}
171

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

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

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

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

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

210
  pObj->userDropped = 0;
×
211
  pObj->userStopped = 0;
×
212

213
  pObj->createTime = taosGetTimestampMs();
×
214
  pObj->updateTime = pObj->createTime;
×
215

216
  mstLogSStreamObj("create stream", pObj);
×
217
}
×
218

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

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

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

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

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

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

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

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

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

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

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

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

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

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

309
  SStbObj stbObj = {0};
×
310

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

315
  stbObj.uid = pStream->outStbUid;
×
316

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

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

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

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

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

340
static int32_t mndStreamValidateCreate(SMnode *pMnode, SRpcMsg *pReq, SCMCreateStreamReq* pCreate) {
×
341
  int32_t code = 0, lino = 0;
×
342
  int64_t streamId = pCreate->streamId;
×
343
  char   *pUser = RPC_MSG_USER(pReq);
×
344

345
  if (pCreate->streamDB) {
×
346
    // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_WRITE_DB, pCreate->streamDB);
347
    code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
×
348
                                     pCreate->streamDB, false);
×
349
    if (code) {
×
350
      if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
351
      mstsError("user %s failed to create stream %s in db %s since %s", pUser, pCreate->name, pCreate->streamDB,
×
352
                tstrerror(code));
353
    }
354
    TSDB_CHECK_CODE(code, lino, _OVER);
×
355
  }
356

357
  if (pCreate->triggerDB) {
×
358
    // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_READ_DB, pCreate->triggerDB);
359
    code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
×
360
                                     pCreate->triggerDB, false);
×
361
    if (code) {
×
362
      if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
363
      mstsError("user %s failed to create stream %s using trigger db %s since %s", pUser, pCreate->name,
×
364
                pCreate->triggerDB, tstrerror(code));
365
    }
366
    TSDB_CHECK_CODE(code, lino, _OVER);
×
367
#if 0  // TODO check the owner of trigger table
368
    if (pCreate->triggerTblName) {
369
      // check trigger table privilege
370
      code = mndCheckObjPrivilegeRecF(pMnode, pUser, PRIV_TBL_SELECT, "", pCreate->triggerDB, pCreate->triggerTblName);
371
      if (code) {
372
        mstsError("user %s failed to create stream %s using trigger table %s.%s since %s", pUser, pCreate->name,
373
                  pCreate->triggerDB, pCreate->triggerTblName, tstrerror(code));
374
      }
375
      TSDB_CHECK_CODE(code, lino, _OVER);
376
    }
377
#endif
378
  }
379

380
  if (pCreate->calcDB) {
×
381
    int32_t dbNum = taosArrayGetSize(pCreate->calcDB);
×
382
    for (int32_t i = 0; i < dbNum; ++i) {
×
383
      char *calcDB = taosArrayGetP(pCreate->calcDB, i);
×
384
      // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_READ_DB, calcDB);
385
      code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB, calcDB, false);
×
386
      if (code) {
×
387
        if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
388
        mstsError("user %s failed to create stream %s using calcDB %s since %s", pUser, pCreate->name, calcDB,
×
389
                  tstrerror(code));
390
      }
391
      TSDB_CHECK_CODE(code, lino, _OVER);
×
392
    }
393
  }
394

395
  if (pCreate->outDB) {
×
396
    // code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_WRITE_DB, pCreate->outDB);
397
    code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB, pCreate->outDB,
×
398
                                     false);
399
    if (code) {
×
400
      if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
401
      mstsError("user %s failed to create stream %s using out db %s since %s", pUser, pCreate->name, pCreate->outDB,
×
402
                tstrerror(code));
403
    }
404
    TSDB_CHECK_CODE(code, lino, _OVER);
×
405
  }
406

407
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
×
408
  if (streamNum > MND_STREAM_MAX_NUM) {
×
409
    code = TSDB_CODE_MND_TOO_MANY_STREAMS;
×
410
    mstsError("failed to create stream %s since %s, stream number:%d", pCreate->name, tstrerror(code), streamNum);
×
411
    return code;
×
412
  }
413

414
_OVER:
×
415

416
  return code;
×
417
}
418

419
int32_t mndDropStreamByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
16✔
420
  SSdb   *pSdb = pMnode->pSdb;
16✔
421
  void   *pIter = NULL;
16✔
422
  int32_t code = 0;
16✔
423

424
  while (1) {
×
425
    SStreamObj *pStream = NULL;
16✔
426
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
16✔
427
    if (pIter == NULL) break;
16✔
428

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

450
    sdbRelease(pSdb, pStream);
×
451
  }
452

453
  return 0;
16✔
454
}
455

456
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
457
  SMnode     *pMnode = pReq->info.node;
×
458
  SSdb       *pSdb = pMnode->pSdb;
×
459
  int32_t     numOfRows = 0;
×
460
  SStreamObj *pStream = NULL;
×
461
  SUserObj   *pOperUser = NULL;
×
462
  int32_t     code = 0, lino = 0;
×
463
  bool        showAll = false;
×
464

465
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser));
×
466
  showAll =
×
467
      (0 == mndCheckObjPrivilegeRec(pMnode, pOperUser, PRIV_CM_SHOW, PRIV_OBJ_STREAM, 0, pOperUser->acctId, "*", "*"));
×
468

469
  while (numOfRows < rows) {
×
470
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
×
471
    if (pShow->pIter == NULL) break;
×
472

473
    if (!showAll) {
×
474
      if ((mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_SHOW, PRIV_OBJ_STREAM, pStream->ownerId,
×
475
                                    pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
×
476
        sdbRelease(pSdb, pStream);
×
477
        continue;
×
478
      }
479
    }
480

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

498
static void mndCancelGetNextStream(SMnode *pMnode, void *pIter) {
×
499
  SSdb *pSdb = pMnode->pSdb;
×
500
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
501
}
×
502

503
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
×
504
  SMnode     *pMnode = pReq->info.node;
×
505
  SSdb       *pSdb = pMnode->pSdb;
×
506
  int32_t     numOfRows = 0;
×
507
  SStreamObj *pStream = NULL;
×
508
  int32_t     code = 0;
×
509

510
  while (numOfRows < rowsCapacity) {
×
511
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
×
512
    if (pShow->pIter == NULL) {
×
513
      break;
×
514
    }
515

516
    code = mstSetStreamTasksResBlock(pStream, pBlock, &numOfRows, rowsCapacity);
×
517

518
    sdbRelease(pSdb, pStream);
×
519
  }
520

521
  pShow->numOfRows += numOfRows;
×
522
  return numOfRows;
×
523
}
524

525
static void mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter) {
×
526
  SSdb *pSdb = pMnode->pSdb;
×
527
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
528
}
×
529

530
static int32_t mndRetrieveStreamRecalculates(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
×
531
  SMnode     *pMnode = pReq->info.node;
×
532
  SSdb       *pSdb = pMnode->pSdb;
×
533
  int32_t     numOfRows = 0;
×
534
  SStreamObj *pStream = NULL;
×
535
  int32_t     code = 0;
×
536

537
  while (numOfRows < rowsCapacity) {
×
538
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
×
539
    if (pShow->pIter == NULL) {
×
540
      break;
×
541
    }
542

543
    code = mstSetStreamRecalculatesResBlock(pStream, pBlock, &numOfRows, rowsCapacity);
×
544

545
    sdbRelease(pSdb, pStream);
×
546
  }
547

548
  pShow->numOfRows += numOfRows;
×
549
  return numOfRows;
×
550
}
551

552
static void mndCancelGetNextStreamRecalculates(SMnode *pMnode, void *pIter) {
×
553
  SSdb *pSdb = pMnode->pSdb;
×
554
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
555
}
×
556

557

558
static bool mndStreamUpdateTagsFlag(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
×
559
  SStreamObj *pStream = pObj;
×
560
  if (atomic_load_8(&pStream->userDropped)) {
×
561
    return true;
×
562
  }
563

564
  if (TSDB_SUPER_TABLE != pStream->pCreate->triggerTblType && 
×
565
      TSDB_CHILD_TABLE != pStream->pCreate->triggerTblType && 
×
566
      TSDB_VIRTUAL_CHILD_TABLE != pStream->pCreate->triggerTblType) {
×
567
    return true;
×
568
  }
569

570
  if (pStream->pCreate->triggerTblSuid != *(uint64_t*)p1) {
×
571
    return true;
×
572
  }
573

574
  if (NULL == pStream->pCreate->partitionCols) {
×
575
    return true;
×
576
  }
577

578
  SNodeList* pList = NULL;
×
579
  int32_t code = nodesStringToList(pStream->pCreate->partitionCols, &pList);
×
580
  if (code) {
×
581
    nodesDestroyList(pList);
×
582
    mstError("partitionCols [%s] nodesStringToList failed with error:%s", (char*)pStream->pCreate->partitionCols, tstrerror(code));
×
583
    return true;
×
584
  }
585

586
  SSchema* pTags = (SSchema*)p2;
×
587
  int32_t* tagNum = (int32_t*)p3;
×
588

589
  SNode* pNode = NULL;
×
590
  FOREACH(pNode, pList) {
×
591
    SColumnNode* pCol = (SColumnNode*)pNode;
×
592
    for (int32_t i = 0; i < *tagNum; ++i) {
×
593
      if (pCol->colId == pTags[i].colId) {
×
594
        pTags[i].flags |= COL_REF_BY_STM;
×
595
        break;
×
596
      }
597
    }
598
  }
599

600
  nodesDestroyList(pList);
×
601
  
602
  return true;
×
603
}
604

605

606
void mndStreamUpdateTagsRefFlag(SMnode *pMnode, int64_t suid, SSchema* pTags, int32_t tagNum) {
2✔
607
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
2✔
608
  if (streamNum <= 0) {
2✔
609
    return;
2✔
610
  }
611

612
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mndStreamUpdateTagsFlag, &suid, pTags, &tagNum);
×
613
}
614

615
static int32_t mndProcessStopStreamReq(SRpcMsg *pReq) {
×
616
  SMnode     *pMnode = pReq->info.node;
×
617
  SStreamObj *pStream = NULL;
×
618
  SUserObj   *pOperUser = NULL;
×
619
  int32_t     code = 0;
×
620

621
  SMPauseStreamReq pauseReq = {0};
×
622
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
×
623
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
624
  }
625

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

639
  taosMemoryFree(pauseReq.name);
×
640

641
  int64_t streamId = pStream->pCreate->streamId;
×
642
  
643
  mstsInfo("start to stop stream %s", pStream->name);
×
644

645
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
646
  if((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
×
647
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
648
    sdbRelease(pMnode->pSdb, pStream);
×
649
    return code;
×
650
  }
651
  if ((code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
×
652
                                        pStream->pCreate->streamDB, false))) {
×
653
    if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
654
  }
655
  if ((code != TSDB_CODE_SUCCESS) ||
×
656
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_STOP, PRIV_OBJ_STREAM, pStream->ownerId,
×
657
                                       pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
×
658
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
659
    mndReleaseUser(pMnode, pOperUser);
×
660
    sdbRelease(pMnode->pSdb, pStream);
×
661
    return code;
×
662
  }
663

664
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
×
665

666
  if (atomic_load_8(&pStream->userDropped)) {
×
667
    code = TSDB_CODE_MND_STREAM_DROPPING;
×
668
    mstsError("user %s failed to stop stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
669
    sdbRelease(pMnode->pSdb, pStream);
×
670
    return code;
×
671
  }
672

673
  STrans *pTrans = NULL;
×
674
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_STOP_NAME, &pTrans);
×
675
  if (pTrans == NULL || code) {
×
676
    mstsError("failed to stop stream %s since %s", pStream->name, tstrerror(code));
×
677
    sdbRelease(pMnode->pSdb, pStream);
×
678
    return code;
×
679
  }
680

681
  pStream->updateTime = taosGetTimestampMs();
×
682

683
  atomic_store_8(&pStream->userStopped, 1);
×
684

685
  MND_STREAM_SET_LAST_TS(STM_EVENT_STOP_STREAM, pStream->updateTime);
×
686

687
  msmUndeployStream(pMnode, streamId, pStream->name);
×
688

689
  // stop stream
690
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
×
691
  if (code != TSDB_CODE_SUCCESS) {
×
692
    sdbRelease(pMnode->pSdb, pStream);
×
693
    mndTransDrop(pTrans);
×
694
    return code;
×
695
  }
696

697
  code = mndTransPrepare(pMnode, pTrans);
×
698
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
699
    mError("trans:%d, failed to prepare stop stream trans since %s", pTrans->id, tstrerror(code));
×
700
    sdbRelease(pMnode->pSdb, pStream);
×
701
    mndTransDrop(pTrans);
×
702
    return code;
×
703
  }
704

705
  sdbRelease(pMnode->pSdb, pStream);
×
706
  mndTransDrop(pTrans);
×
707

708
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
709
}
710

711

712
static int32_t mndProcessStartStreamReq(SRpcMsg *pReq) {
×
713
  SMnode     *pMnode = pReq->info.node;
×
714
  SStreamObj *pStream = NULL;
×
715
  SUserObj   *pOperUser = NULL;
×
716
  int32_t     code = 0;
×
717

718
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
×
719
    return code;
×
720
  }
721

722
  SMResumeStreamReq resumeReq = {0};
×
723
  if (tDeserializeSMResumeStreamReq(pReq->pCont, pReq->contLen, &resumeReq) < 0) {
×
724
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
725
  }
726

727
  code = mndAcquireStream(pMnode, resumeReq.name, &pStream);
×
728
  if (pStream == NULL || code != 0) {
×
729
    if (resumeReq.igNotExists) {
×
730
      mInfo("stream:%s not exist, not start stream", resumeReq.name);
×
731
      taosMemoryFree(resumeReq.name);
×
732
      sdbRelease(pMnode->pSdb, pStream);
×
733
      return 0;
×
734
    } else {
735
      mError("stream:%s not exist, failed to start stream", resumeReq.name);
×
736
      taosMemoryFree(resumeReq.name);
×
737
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
738
    }
739
  }
740

741
  taosMemoryFree(resumeReq.name);
×
742

743
  int64_t streamId = pStream->pCreate->streamId;
×
744

745
  mstsInfo("start to start stream %s from stopped", pStream->name);
×
746

747
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
748
  if ((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
×
749
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
750
    sdbRelease(pMnode->pSdb, pStream);
×
751
    return code;
×
752
  }
753
  if ((code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
×
754
                                        pStream->pCreate->streamDB, false))) {
×
755
    if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
756
  }
757
  if ((code != TSDB_CODE_SUCCESS) ||
×
758
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_START, PRIV_OBJ_STREAM, pStream->ownerId,
×
759
                                       pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
×
760
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
761
    mndReleaseUser(pMnode, pOperUser);
×
762
    sdbRelease(pMnode->pSdb, pStream);
×
763
    return code;
×
764
  }
765

766
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
×
767

768
  if (atomic_load_8(&pStream->userDropped)) {
×
769
    code = TSDB_CODE_MND_STREAM_DROPPING;
×
770
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
771
    sdbRelease(pMnode->pSdb, pStream);
×
772
    return code;
×
773
  }
774

775
  if (0 == atomic_load_8(&pStream->userStopped)) {
×
776
    code = TSDB_CODE_MND_STREAM_NOT_STOPPED;
×
777
    mstsError("user %s failed to start stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
778
    sdbRelease(pMnode->pSdb, pStream);
×
779
    return code;
×
780
  }
781
  
782
  atomic_store_8(&pStream->userStopped, 0);
×
783

784
  pStream->updateTime = taosGetTimestampMs();
×
785

786
  MND_STREAM_SET_LAST_TS(STM_EVENT_START_STREAM, pStream->updateTime);
×
787

788
  STrans *pTrans = NULL;
×
789
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_START_NAME, &pTrans);
×
790
  if (pTrans == NULL || code) {
×
791
    mstsError("failed to start stream %s since %s", pStream->name, tstrerror(code));
×
792
    sdbRelease(pMnode->pSdb, pStream);
×
793
    return code;
×
794
  }
795

796
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
×
797
  if (code != TSDB_CODE_SUCCESS) {
×
798
    mstsError("failed to start stream %s since %s", pStream->name, tstrerror(code));
×
799
    sdbRelease(pMnode->pSdb, pStream);
×
800
    mndTransDrop(pTrans);
×
801
    return code;
×
802
  }
803

804
  code = mndTransPrepare(pMnode, pTrans);
×
805
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
806
    mstsError("trans:%d, failed to prepare start stream %s trans since %s", pTrans->id, pStream->name, tstrerror(code));
×
807
    sdbRelease(pMnode->pSdb, pStream);
×
808
    mndTransDrop(pTrans);
×
809
    return code;
×
810
  }
811

812
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->name, NULL, true, STREAM_ACT_DEPLOY);
×
813

814
  sdbRelease(pMnode->pSdb, pStream);
×
815
  mndTransDrop(pTrans);
×
816

817
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
818
}
819

820
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) {
×
821
  SMnode     *pMnode = pReq->info.node;
×
822
  SStreamObj *pStream = NULL;
×
823
  SUserObj   *pOperUser = NULL;
×
824
  int32_t     code = 0;
×
825
  int32_t     notExistNum = 0;
×
826

827
  SMDropStreamReq dropReq = {0};
×
828
  int64_t         tss = taosGetTimestampMs();
×
829
  if (tDeserializeSMDropStreamReq(pReq->pCont, pReq->contLen, &dropReq) < 0) {
×
830
    mError("invalid drop stream msg recv, discarded");
×
831
    code = TSDB_CODE_INVALID_MSG;
×
832
    TAOS_RETURN(code);
×
833
  }
834

835
  mDebug("recv drop stream msg, count:%d", dropReq.count);
×
836

837
  // Acquire user object for privilege check
838
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
×
839
  if (code != 0) {
×
840
    tFreeMDropStreamReq(&dropReq);
×
841
    TAOS_RETURN(code);
×
842
  }
843

844
  // check if all streams exist
845
  if (!dropReq.igNotExists) {
×
846
    for (int32_t i = 0; i < dropReq.count; i++) {
×
847
      if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, dropReq.name[i])) {
×
848
        mError("stream:%s not exist failed to drop it", dropReq.name[i]);
×
849
        mndReleaseUser(pMnode, pOperUser);
×
850
        tFreeMDropStreamReq(&dropReq);
×
851
        TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
852
      }
853
    }
854
  }
855

856
  // Create a single transaction for all stream drops
857
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, pReq, MND_STREAM_DROP_NAME);
×
858
  if (pTrans == NULL) {
×
859
    mError("failed to create drop stream transaction since %s", tstrerror(terrno));
×
860
    code = terrno;
×
861
    mndReleaseUser(pMnode, pOperUser);
×
862
    tFreeMDropStreamReq(&dropReq);
×
863
    TAOS_RETURN(code);
×
864
  }
865
  pTrans->ableToBeKilled = true;
×
866

867
  // Process all streams and add them to the transaction
868
  for (int32_t i = 0; i < dropReq.count; i++) {
×
869
    char *streamName = dropReq.name[i];
×
870
    mDebug("drop stream[%d/%d]: %s", i + 1, dropReq.count, streamName);
×
871

872
    code = mndAcquireStream(pMnode, streamName, &pStream);
×
873
    if (pStream == NULL || code != 0) {
×
874
      mWarn("stream:%s not exist, ignore not exist is set, drop stream exec done with success", streamName);
×
875
      sdbRelease(pMnode->pSdb, pStream);
×
876
      pStream = NULL;
×
877
      notExistNum++;
×
878
      continue;
×
879
    }
880

881
    int64_t streamId = pStream->pCreate->streamId;
×
882

883
    if ((code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
×
884
                                          pStream->pCreate->streamDB, true))) {
×
885
      if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
886
    }
887
    if ((code != TSDB_CODE_SUCCESS) ||
×
888
        (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_DROP, PRIV_OBJ_STREAM, pStream->ownerId,
×
889
                                         pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
×
890
      mstsError("user %s failed to drop stream %s since %s", pReq->info.conn.user, streamName, tstrerror(code));
×
891
      sdbRelease(pMnode->pSdb, pStream);
×
892
      pStream = NULL;
×
893
      mndTransDrop(pTrans);
×
894
      pTrans = NULL;
×
895
      goto _OVER;
×
896
    }
897

898
    if (pStream->pCreate->tsmaId != 0) {
×
899
      mstsDebug("try to drop tsma related stream, tsmaId:%" PRIx64, pStream->pCreate->tsmaId);
×
900

901
      void    *pIter = NULL;
×
902
      SSmaObj *pSma = NULL;
×
903
      pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
×
904
      while (pIter) {
×
905
        if (pSma && pSma->uid == pStream->pCreate->tsmaId) {
×
906
          sdbRelease(pMnode->pSdb, pSma);
×
907
          sdbRelease(pMnode->pSdb, pStream);
×
908
          pStream = NULL;
×
909

910
          sdbCancelFetch(pMnode->pSdb, pIter);
×
911
          code = TSDB_CODE_TSMA_MUST_BE_DROPPED;
×
912

913
          mstsError("refused to drop tsma-related stream %s since tsma still exists", streamName);
×
914
          mndTransDrop(pTrans);
×
915
          pTrans = NULL;
×
916
          goto _OVER;
×
917
        }
918

919
        if (pSma) {
×
920
          sdbRelease(pMnode->pSdb, pSma);
×
921
        }
922

923
        pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
×
924
      }
925
    }
926

927
    mstsInfo("start to drop stream %s", pStream->pCreate->name);
×
928

929
    pStream->updateTime = taosGetTimestampMs();
×
930

931
    atomic_store_8(&pStream->userDropped, 1);
×
932

933
    MND_STREAM_SET_LAST_TS(STM_EVENT_DROP_STREAM, pStream->updateTime);
×
934

935
    msmUndeployStream(pMnode, streamId, pStream->pCreate->name);
×
936

937
    // Append drop stream operation to the transaction
938
    code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_DROPPED);
×
939
    if (code) {
×
940
      mstsError("trans:%d, failed to append drop stream %s trans since %s", pTrans->id, streamName, tstrerror(code));
×
941
      sdbRelease(pMnode->pSdb, pStream);
×
942
      pStream = NULL;
×
943
      // mndStreamTransAppend already called mndTransDrop on failure, set pTrans to NULL to avoid double free
944
      pTrans = NULL;
×
945
      goto _OVER;
×
946
    }
947

948
    sdbRelease(pMnode->pSdb, pStream);
×
949
    pStream = NULL;
×
950

951
    mstsDebug("drop stream %s added to transaction", streamName);
×
952
  }
953

954
  // Prepare and execute the transaction for all streams
955
  if (notExistNum < dropReq.count) {
×
956
    code = mndTransPrepare(pMnode, pTrans);
×
957
    if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
958
      mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
959
      mndTransDrop(pTrans);
×
960
      goto _OVER;
×
961
    }
962
    mInfo("trans:%d, drop stream transaction prepared for %d streams", pTrans->id, dropReq.count - notExistNum);
×
963
  } else {
964
    // All streams don't exist, no need to prepare transaction
965
    mndTransDrop(pTrans);
×
966
    pTrans = NULL;
×
967
  }
968

969
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE && notExistNum < dropReq.count) {
×
970
    int64_t tse = taosGetTimestampMs();
×
971
    double  duration = (double)(tse - tss);
×
972
    duration = duration / 1000;
×
973
    // Use first stream's database for audit (assuming all streams are from same db in batch)
974
    if (dropReq.count > 0) {
×
975
      SStreamObj *pFirstStream = NULL;
×
976
      if (mndAcquireStream(pMnode, dropReq.name[0], &pFirstStream) == 0 && pFirstStream != NULL) {
×
977
        auditRecord(pReq, pMnode->clusterId, "dropStream", "", pFirstStream->pCreate->streamDB, NULL, 0, duration, 0);
×
978
        sdbRelease(pMnode->pSdb, pFirstStream);
×
979
      }
980
    }
981
  }
982

983
  // If any stream was successfully added to transaction, return ACTION_IN_PROGRESS
984
  // Otherwise, all streams don't exist (and igNotExists is set), return SUCCESS
985
  code = (notExistNum < dropReq.count) ? TSDB_CODE_ACTION_IN_PROGRESS : TSDB_CODE_SUCCESS;
×
986

987
_OVER:
×
988
  if (pStream) {
×
989
    sdbRelease(pMnode->pSdb, pStream);
×
990
  }
991
  if (pTrans) {
×
992
    mndTransDrop(pTrans);
×
993
  }
994
  mndReleaseUser(pMnode, pOperUser);
×
995
  tFreeMDropStreamReq(&dropReq);
×
996
  TAOS_RETURN(code);
×
997
}
998

999
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq) {
×
1000
  SMnode     *pMnode = pReq->info.node;
×
1001
  SStreamObj *pStream = NULL;
×
1002
  SStreamObj  streamObj = {0};
×
1003
  SUserObj    *pOperUser = NULL;
×
1004
  int32_t     code = TSDB_CODE_SUCCESS;
×
1005
  int32_t     lino = 0;
×
1006
  STrans     *pTrans = NULL;
×
1007
  uint64_t    streamId = 0;
×
1008
  SCMCreateStreamReq* pCreate = NULL;
×
1009
  int64_t             tss = taosGetTimestampMs();
×
1010

1011
  if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
×
1012
    goto _OVER;
×
1013
  }
1014

1015
  pCreate = taosMemoryCalloc(1, sizeof(SCMCreateStreamReq));
×
1016
  TSDB_CHECK_NULL(pCreate, code, lino, _OVER, terrno);
×
1017
  
1018
  code = tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, pCreate);
×
1019
  TSDB_CHECK_CODE(code, lino, _OVER);
×
1020

1021
  streamId = pCreate->streamId;
×
1022

1023
  mstsInfo("start to create stream %s, sql:%s", pCreate->name, pCreate->sql);
×
1024

1025
  int32_t snodeId = msmAssignRandomSnodeId(pMnode, streamId);
×
1026
  if (!GOT_SNODE(snodeId)) {
×
1027
    code = terrno;
×
1028
    TSDB_CHECK_CODE(code, lino, _OVER);
×
1029
  }
1030
  
1031
  code = mndAcquireStream(pMnode, pCreate->name, &pStream);
×
1032
  if (pStream != NULL && code == 0) {
×
1033
    if (pCreate->igExists) {
×
1034
      mstsInfo("stream %s already exist, ignore exist is set", pCreate->name);
×
1035
    } else {
1036
      code = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
×
1037
    }
1038

1039
    mndReleaseStream(pMnode, pStream);
×
1040
    goto _OVER;
×
1041
  } else if (code != TSDB_CODE_MND_STREAM_NOT_EXIST) {
×
1042
    goto _OVER;
×
1043
  }
1044

1045
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
×
1046
  if (pOperUser == NULL) {
×
1047
    TSDB_CHECK_CODE(TSDB_CODE_MND_NO_USER_FROM_CONN, lino, _OVER);
×
1048
  }
1049

1050
  code = mndStreamValidateCreate(pMnode, pReq, pCreate);
×
1051
  TSDB_CHECK_CODE(code, lino, _OVER);
×
1052

1053
  mndStreamBuildObj(pMnode, &streamObj, pCreate, pOperUser, snodeId);
×
1054
  pCreate = NULL;
×
1055

1056
  pStream = &streamObj;
×
1057

1058
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_DB, MND_STREAM_CREATE_NAME, &pTrans);
×
1059
  if (pTrans == NULL || code) {
×
1060
    goto _OVER;
×
1061
  }
1062

1063
  // create stb for stream
1064
  if (TSDB_SUPER_TABLE == pStream->pCreate->outTblType && !pStream->pCreate->outStbExists) {
×
1065
    pStream->pCreate->outStbUid = mndGenerateUid(pStream->pCreate->outTblName, strlen(pStream->pCreate->outTblName));
×
1066
    code = mndStreamCreateOutStb(pMnode, pTrans, pStream->pCreate, RPC_MSG_USER(pReq));
×
1067
    TSDB_CHECK_CODE(code, lino, _OVER);
×
1068
  }
1069

1070
  // add stream to trans
1071
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
×
1072
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
1073
    mstsError("failed to persist stream %s since %s", pStream->pCreate->name, tstrerror(code));
×
1074
    goto _OVER;
×
1075
  }
1076

1077
  // execute creation
1078
  code = mndTransPrepare(pMnode, pTrans);
×
1079
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
1080
    mstsError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
1081
    goto _OVER;
×
1082
  }
1083
  code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1084

1085
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE) {
×
1086
    int64_t tse = taosGetTimestampMs();
×
1087
    double  duration = (double)(tse - tss);
×
1088
    duration = duration / 1000;
×
1089
    auditRecord(pReq, pMnode->clusterId, "createStream", pStream->pCreate->streamDB, pStream->pCreate->name,
×
1090
                pStream->pCreate->sql, strlen(pStream->pCreate->sql), duration, 0);
×
1091
  }
1092

1093
  MND_STREAM_SET_LAST_TS(STM_EVENT_CREATE_STREAM, taosGetTimestampMs());
×
1094

1095
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, true, STREAM_ACT_DEPLOY);
×
1096

1097
_OVER:
×
1098

1099
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
1100
    if (pStream && pStream->pCreate) {
×
1101
      mstsError("failed to create stream %s at line:%d since %s", pStream->pCreate->name, lino, tstrerror(code));
×
1102
    } else {
1103
      mstsError("failed to create stream at line:%d since %s", lino, tstrerror(code));
×
1104
    }
1105
  } else {
1106
    mstsDebug("create stream %s half completed", pStream->pCreate ? pStream->pCreate->name : "unknown");
×
1107
  }
1108

1109
  tFreeSCMCreateStreamReq(pCreate);
×
1110
  taosMemoryFreeClear(pCreate);
×
1111

1112
  mndTransDrop(pTrans);
×
1113
  tFreeStreamObj(&streamObj);
×
1114
  mndReleaseUser(pMnode, pOperUser);
×
1115

1116
  return code;
×
1117
}
1118

1119
static int32_t mndProcessRecalcStreamReq(SRpcMsg *pReq) {
×
1120
  SMnode     *pMnode = pReq->info.node;
×
1121
  SStreamObj *pStream = NULL;
×
1122
  SUserObj   *pOperUser = NULL;
×
1123
  int32_t     code = 0;
×
1124
  int64_t     tss = taosGetTimestampMs();
×
1125

1126
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
×
1127
    return code;
×
1128
  }
1129

1130
  SMRecalcStreamReq recalcReq = {0};
×
1131
  if (tDeserializeSMRecalcStreamReq(pReq->pCont, pReq->contLen, &recalcReq) < 0) {
×
1132
    tFreeMRecalcStreamReq(&recalcReq);
×
1133
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1134
  }
1135

1136
  code = mndAcquireStream(pMnode, recalcReq.name, &pStream);
×
1137
  if (pStream == NULL || code != 0) {
×
1138
    mError("stream:%s not exist, failed to recalc stream", recalcReq.name);
×
1139
    tFreeMRecalcStreamReq(&recalcReq);
×
1140
    TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1141
  }
1142

1143
  int64_t streamId = pStream->pCreate->streamId;
×
1144
  
1145
  mstsInfo("start to recalc stream %s", recalcReq.name);
×
1146

1147
  // code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
1148
  // if (code != TSDB_CODE_SUCCESS) {
1149
  //   mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
1150
  //   sdbRelease(pMnode->pSdb, pStream);
1151
  //   tFreeMRecalcStreamReq(&recalcReq);
1152
  //   return code;
1153
  // }
1154

1155
  if ((code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser))) {
×
1156
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
1157
    sdbRelease(pMnode->pSdb, pStream);
×
1158
    tFreeMRecalcStreamReq(&recalcReq);
×
1159
    TAOS_RETURN(code);
×
1160
  }
1161

1162
  if ((code = mndCheckDbPrivilegeByName(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_USE_DB,
×
1163
                                        pStream->pCreate->streamDB, false))) {
×
1164
    if (code == TSDB_CODE_MND_NO_RIGHTS) code = TSDB_CODE_PAR_DB_USE_PERMISSION_DENIED;
×
1165
  }
1166
  if ((code != TSDB_CODE_SUCCESS) ||
×
1167
      (code = mndCheckObjPrivilegeRecF(pMnode, pOperUser, PRIV_CM_RECALC, PRIV_OBJ_STREAM, pStream->ownerId,
×
1168
                                       pStream->pCreate->streamDB, mndGetStableStr(pStream->pCreate->name)))) {
×
1169
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), pStream->name, tstrerror(code));
×
1170
    mndReleaseUser(pMnode, pOperUser);
×
1171
    sdbRelease(pMnode->pSdb, pStream);
×
1172
    tFreeMRecalcStreamReq(&recalcReq);
×
1173
    return code;
×
1174
  }
1175

1176
  mndReleaseUser(pMnode, pOperUser); // release user after privilege check
×
1177

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

1186
  if (atomic_load_8(&pStream->userStopped)) {
×
1187
    code = TSDB_CODE_MND_STREAM_STOPPED;
×
1188
    mstsError("user %s failed to recalc stream %s since %s", RPC_MSG_USER(pReq), recalcReq.name, tstrerror(code));
×
1189
    sdbRelease(pMnode->pSdb, pStream);
×
1190
    tFreeMRecalcStreamReq(&recalcReq);
×
1191
    return code;
×
1192
  }
1193

1194
  if (WINDOW_TYPE_PERIOD == pStream->pCreate->triggerType) {
×
1195
    code = TSDB_CODE_OPS_NOT_SUPPORT;
×
1196
    mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
×
1197
    sdbRelease(pMnode->pSdb, pStream);
×
1198
    tFreeMRecalcStreamReq(&recalcReq);
×
1199
    return code;
×
1200
  }
1201

1202
  /*
1203
  pStream->updateTime = taosGetTimestampMs();
1204

1205
  STrans *pTrans = NULL;
1206
  code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RECALC_NAME, &pTrans);
1207
  if (pTrans == NULL || code) {
1208
    mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
1209
    sdbRelease(pMnode->pSdb, pStream);
1210
    return code;
1211
  }
1212

1213
  // stop stream
1214
  code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
1215
  if (code != TSDB_CODE_SUCCESS) {
1216
    sdbRelease(pMnode->pSdb, pStream);
1217
    mndTransDrop(pTrans);
1218
    return code;
1219
  }
1220

1221
  code = mndTransPrepare(pMnode, pTrans);
1222
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1223
    mError("trans:%d, failed to prepare stop stream trans since %s", pTrans->id, tstrerror(code));
1224
    sdbRelease(pMnode->pSdb, pStream);
1225
    mndTransDrop(pTrans);
1226
    return code;
1227
  }
1228
*/
1229

1230
  code = msmRecalcStream(pMnode, pStream->pCreate->streamId, &recalcReq.timeRange);
×
1231
  if (code != TSDB_CODE_SUCCESS) {
×
1232
    sdbRelease(pMnode->pSdb, pStream);
×
1233
    tFreeMRecalcStreamReq(&recalcReq);
×
1234
    return code;
×
1235
  }
1236

1237
  if (tsAuditLevel >= AUDIT_LEVEL_DATABASE){
×
1238
    char buf[128];
1239
    snprintf(buf, sizeof(buf), "start:%" PRId64 ", end:%" PRId64, recalcReq.timeRange.skey, recalcReq.timeRange.ekey);
×
1240
    int64_t tse = taosGetTimestampMs();
×
1241
    double  duration = (double)(tse - tss);
×
1242
    duration = duration / 1000;
×
1243
    auditRecord(pReq, pMnode->clusterId, "recalcStream", pStream->name, recalcReq.name, buf, strlen(buf), duration, 0);
×
1244
  }  
1245

1246
  sdbRelease(pMnode->pSdb, pStream);
×
1247
  tFreeMRecalcStreamReq(&recalcReq);
×
1248
//  mndTransDrop(pTrans);
1249

1250
  return TSDB_CODE_SUCCESS;
×
1251
}
1252

1253

1254
int32_t mndInitStream(SMnode *pMnode) {
16✔
1255
  SSdbTable table = {
16✔
1256
      .sdbType = SDB_STREAM,
1257
      .keyType = SDB_KEY_BINARY,
1258
      .encodeFp = (SdbEncodeFp)mndStreamActionEncode,
1259
      .decodeFp = (SdbDecodeFp)mndStreamActionDecode,
1260
      .insertFp = (SdbInsertFp)mndStreamActionInsert,
1261
      .updateFp = (SdbUpdateFp)mndStreamActionUpdate,
1262
      .deleteFp = (SdbDeleteFp)mndStreamActionDelete,
1263
  };
1264

1265
  if (!tsDisableStream) {
16✔
1266
    mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STREAM, mndProcessCreateStreamReq);
16✔
1267
    mndSetMsgHandle(pMnode, TDMT_MND_DROP_STREAM, mndProcessDropStreamReq);
16✔
1268
    mndSetMsgHandle(pMnode, TDMT_MND_START_STREAM, mndProcessStartStreamReq);
16✔
1269
    mndSetMsgHandle(pMnode, TDMT_MND_STOP_STREAM, mndProcessStopStreamReq);
16✔
1270
    mndSetMsgHandle(pMnode, TDMT_MND_STREAM_HEARTBEAT, mndProcessStreamHb);  
16✔
1271
    mndSetMsgHandle(pMnode, TDMT_MND_RECALC_STREAM, mndProcessRecalcStreamReq);
16✔
1272
  }
1273
  
1274
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndRetrieveStream);
16✔
1275
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndCancelGetNextStream);
16✔
1276
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndRetrieveStreamTask);
16✔
1277
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndCancelGetNextStreamTask);
16✔
1278
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndRetrieveStreamRecalculates);
16✔
1279
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndCancelGetNextStreamRecalculates);
16✔
1280

1281
  int32_t code = sdbSetTable(pMnode->pSdb, table);
16✔
1282
  if (code) {
16✔
1283
    return code;
×
1284
  }
1285

1286
  //code = sdbSetTable(pMnode->pSdb, tableSeq);
1287
  return code;
16✔
1288
}
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