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

taosdata / TDengine / #3620

21 Feb 2025 09:00AM UTC coverage: 63.573% (+0.2%) from 63.423%
#3620

push

travis-ci

web-flow
ci: taosBenchmark add coverage cases branch 3.0 (#29788)

* fix: add unit test for taos-tools

* fix: only .cpp include

* fix: remove no use function

* fix: restore toolsSys.c

* fix: add toolsSys case

* fix: rebuild error fixed

* fix: fix build error

* fix: support get vgroups with core and memory limit

* fix: build error for strcasecmp

* fix: add insertBasic.py case

* fix: add command line set vgroups=3

* fix: change with ns database

* toolscJson read with int replace float and add insertPrecison.py

* fix: add insertBindVGroup.json case

* fix: remove public fun removeQuotation

* fix: vgroups change method

* fix: memory leak for runInsertLimitThread slot

* insertPrecision.py word write wrong

* fix: check isFloat number

* fix: vgroups change logic error

* fix: insertBasic.py real and expect error

* fix: adjust default vgroups

* fix: adjust default vgroups modify comment

148962 of 300203 branches covered (49.62%)

Branch coverage included in aggregate %.

15 of 16 new or added lines in 1 file covered. (93.75%)

2018 existing lines in 133 files now uncovered.

233201 of 300933 relevant lines covered (77.49%)

18174406.98 hits per line

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

61.61
/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 "audit.h"
17
#include "mndDb.h"
18
#include "mndPrivilege.h"
19
#include "mndScheduler.h"
20
#include "mndShow.h"
21
#include "mndStb.h"
22
#include "mndStream.h"
23
#include "mndTrans.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 60
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
SStreamExecInfo execInfo;
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 mndProcessCreateStreamReq(SRpcMsg *pReq);
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 mndProcessStreamCheckpoint(SRpcMsg *pReq);
49
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
50
static void    mndCancelGetNextStream(SMnode *pMnode, void *pIter);
51
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
52
static void    mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter);
53
static int32_t mndProcessPauseStreamReq(SRpcMsg *pReq);
54
static int32_t mndProcessResumeStreamReq(SRpcMsg *pReq);
55
static int32_t mndProcessResetStreamReq(SRpcMsg *pReq);
56
static int32_t mndBuildStreamCheckpointSourceReq(void **pBuf, int32_t *pLen, int32_t nodeId, int64_t checkpointId,
57
                                                 int64_t streamId, int32_t taskId, int32_t transId, int8_t mndTrigger);
58
static int32_t mndProcessNodeCheck(SRpcMsg *pReq);
59
static int32_t mndProcessNodeCheckReq(SRpcMsg *pMsg);
60
static int32_t refreshNodeListFromExistedStreams(SMnode *pMnode, SArray *pNodeList);
61
static int32_t mndProcessStreamReqCheckpoint(SRpcMsg *pReq);
62
static int32_t mndProcessCheckpointReport(SRpcMsg *pReq);
63
static int32_t mndProcessConsensusInTmr(SRpcMsg *pMsg);
64
static void    doSendQuickRsp(SRpcHandleInfo *pInfo, int32_t msgSize, int32_t vgId, int32_t code);
65
static int32_t mndProcessDropOrphanTaskReq(SRpcMsg *pReq);
66
static void    saveTaskAndNodeInfoIntoBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode);
67

68
static void     addAllStreamTasksIntoBuf(SMnode *pMnode, SStreamExecInfo *pExecInfo);
69
static SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw);
70

71
SSdbRaw       *mndStreamSeqActionEncode(SStreamObj *pStream);
72
SSdbRow       *mndStreamSeqActionDecode(SSdbRaw *pRaw);
73
static int32_t mndStreamSeqActionInsert(SSdb *pSdb, SStreamSeq *pStream);
74
static int32_t mndStreamSeqActionDelete(SSdb *pSdb, SStreamSeq *pStream);
75
static int32_t mndStreamSeqActionUpdate(SSdb *pSdb, SStreamSeq *pOldStream, SStreamSeq *pNewStream);
76

77
int32_t mndInitStream(SMnode *pMnode) {
1,871✔
78
  SSdbTable table = {
1,871✔
79
      .sdbType = SDB_STREAM,
80
      .keyType = SDB_KEY_BINARY,
81
      .encodeFp = (SdbEncodeFp)mndStreamActionEncode,
82
      .decodeFp = (SdbDecodeFp)mndStreamActionDecode,
83
      .insertFp = (SdbInsertFp)mndStreamActionInsert,
84
      .updateFp = (SdbUpdateFp)mndStreamActionUpdate,
85
      .deleteFp = (SdbDeleteFp)mndStreamActionDelete,
86
  };
87
  SSdbTable tableSeq = {
1,871✔
88
      .sdbType = SDB_STREAM_SEQ,
89
      .keyType = SDB_KEY_BINARY,
90
      .encodeFp = (SdbEncodeFp)mndStreamSeqActionEncode,
91
      .decodeFp = (SdbDecodeFp)mndStreamSeqActionDecode,
92
      .insertFp = (SdbInsertFp)mndStreamSeqActionInsert,
93
      .updateFp = (SdbUpdateFp)mndStreamSeqActionUpdate,
94
      .deleteFp = (SdbDeleteFp)mndStreamSeqActionDelete,
95
  };
96

97
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STREAM, mndProcessCreateStreamReq);
1,871✔
98
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STREAM, mndProcessDropStreamReq);
1,871✔
99
  mndSetMsgHandle(pMnode, TDMT_MND_NODECHECK_TIMER, mndProcessNodeCheck);
1,871✔
100

101
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_DEPLOY_RSP, mndTransProcessRsp);
1,871✔
102
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_DROP_RSP, mndTransProcessRsp);
1,871✔
103
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_PAUSE_RSP, mndTransProcessRsp);
1,871✔
104
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_RESUME_RSP, mndTransProcessRsp);
1,871✔
105
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_STOP_RSP, mndTransProcessRsp);
1,871✔
106
  mndSetMsgHandle(pMnode, TDMT_VND_STREAM_TASK_UPDATE_RSP, mndTransProcessRsp);
1,871✔
107
  mndSetMsgHandle(pMnode, TDMT_VND_STREAM_TASK_RESET_RSP, mndTransProcessRsp);
1,871✔
108
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_UPDATE_CHKPT_RSP, mndTransProcessRsp);
1,871✔
109
  mndSetMsgHandle(pMnode, TDMT_STREAM_CONSEN_CHKPT_RSP, mndTransProcessRsp);
1,871✔
110

111
  // for msgs inside mnode
112
  // TODO change the name
113
  mndSetMsgHandle(pMnode, TDMT_STREAM_CREATE, mndProcessCreateStreamReqFromMNode);
1,871✔
114
  mndSetMsgHandle(pMnode, TDMT_STREAM_CREATE_RSP, mndTransProcessRsp);
1,871✔
115
  mndSetMsgHandle(pMnode, TDMT_STREAM_DROP, mndProcessDropStreamReqFromMNode);
1,871✔
116
  mndSetMsgHandle(pMnode, TDMT_STREAM_DROP_RSP, mndTransProcessRsp);
1,871✔
117

118
  mndSetMsgHandle(pMnode, TDMT_VND_STREAM_CHECK_POINT_SOURCE_RSP, mndTransProcessRsp);
1,871✔
119
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_BEGIN_CHECKPOINT, mndProcessStreamCheckpoint);
1,871✔
120
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_DROP_ORPHANTASKS, mndProcessDropOrphanTaskReq);
1,871✔
121
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_TASK_RESET, mndProcessResetStatusReq);
1,871✔
122
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_REQ_CHKPT, mndProcessStreamReqCheckpoint);
1,871✔
123
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_CHKPT_REPORT, mndProcessCheckpointReport);
1,871✔
124
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_UPDATE_CHKPT_EVT, mndScanCheckpointReportInfo);
1,871✔
125
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_REPORT_CHECKPOINT, mndTransProcessRsp);
1,871✔
126
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_HEARTBEAT, mndProcessStreamHb);
1,871✔
127
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_NODECHANGE_CHECK, mndProcessNodeCheckReq);
1,871✔
128
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_CONSEN_TIMER, mndProcessConsensusInTmr);
1,871✔
129

130
  mndSetMsgHandle(pMnode, TDMT_MND_PAUSE_STREAM, mndProcessPauseStreamReq);
1,871✔
131
  mndSetMsgHandle(pMnode, TDMT_MND_RESUME_STREAM, mndProcessResumeStreamReq);
1,871✔
132
  mndSetMsgHandle(pMnode, TDMT_MND_RESET_STREAM, mndProcessResetStreamReq);
1,871✔
133

134
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndRetrieveStream);
1,871✔
135
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndCancelGetNextStream);
1,871✔
136
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndRetrieveStreamTask);
1,871✔
137
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndCancelGetNextStreamTask);
1,871✔
138

139
  int32_t code = mndInitExecInfo();
1,871✔
140
  if (code) {
1,871!
141
    return code;
×
142
  }
143

144
  code = sdbSetTable(pMnode->pSdb, table);
1,871✔
145
  if (code) {
1,871!
146
    return code;
×
147
  }
148

149
  code = sdbSetTable(pMnode->pSdb, tableSeq);
1,871✔
150
  return code;
1,871✔
151
}
152

153
void mndCleanupStream(SMnode *pMnode) {
1,870✔
154
  taosArrayDestroy(execInfo.pTaskList);
1,870✔
155
  taosArrayDestroy(execInfo.pNodeList);
1,870✔
156
  taosArrayDestroy(execInfo.pKilledChkptTrans);
1,870✔
157
  taosHashCleanup(execInfo.pTaskMap);
1,870✔
158
  taosHashCleanup(execInfo.transMgmt.pDBTrans);
1,870✔
159
  taosHashCleanup(execInfo.pTransferStateStreams);
1,870✔
160
  taosHashCleanup(execInfo.pChkptStreams);
1,870✔
161
  taosHashCleanup(execInfo.pStreamConsensus);
1,870✔
162
  (void)taosThreadMutexDestroy(&execInfo.lock);
1,870✔
163
  mDebug("mnd stream exec info cleanup");
1,870✔
164
}
1,870✔
165

166
SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw) {
6,784✔
167
  int32_t     code = 0;
6,784✔
168
  int32_t     lino = 0;
6,784✔
169
  SSdbRow    *pRow = NULL;
6,784✔
170
  SStreamObj *pStream = NULL;
6,784✔
171
  void       *buf = NULL;
6,784✔
172
  int8_t      sver = 0;
6,784✔
173
  int32_t     tlen;
174
  int32_t     dataPos = 0;
6,784✔
175

176
  code = sdbGetRawSoftVer(pRaw, &sver);
6,784✔
177
  TSDB_CHECK_CODE(code, lino, _over);
6,784!
178

179
  if (sver < 1 || sver > MND_STREAM_VER_NUMBER) {
6,784!
180
    mError("stream read invalid ver, data ver: %d, curr ver: %d", sver, MND_STREAM_VER_NUMBER);
×
181
    goto _over;
×
182
  }
183

184
  pRow = sdbAllocRow(sizeof(SStreamObj));
6,784✔
185
  TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
6,784!
186

187
  pStream = sdbGetRowObj(pRow);
6,784✔
188
  TSDB_CHECK_NULL(pStream, code, lino, _over, terrno);
6,784!
189

190
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
6,784!
191

192
  buf = taosMemoryMalloc(tlen + 1);
6,784!
193
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
6,784!
194

195
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
6,784!
196

197
  SDecoder decoder;
198
  tDecoderInit(&decoder, buf, tlen + 1);
6,784✔
199
  code = tDecodeSStreamObj(&decoder, pStream, sver);
6,784✔
200
  tDecoderClear(&decoder);
6,784✔
201

202
  if (code < 0) {
6,784!
203
    tFreeStreamObj(pStream);
×
204
  }
205

206
_over:
6,784✔
207
  taosMemoryFreeClear(buf);
6,784!
208

209
  if (code != TSDB_CODE_SUCCESS) {
6,784!
210
    char *p = (pStream == NULL) ? "null" : pStream->name;
×
211
    mError("stream:%s, failed to decode from raw:%p since %s at:%d", p, pRaw, tstrerror(code), lino);
×
212
    taosMemoryFreeClear(pRow);
×
213

214
    terrno = code;
×
215
    return NULL;
×
216
  } else {
217
    mTrace("stream:%s, decode from raw:%p, row:%p, checkpoint:%" PRId64, pStream->name, pRaw, pStream,
6,784✔
218
           pStream->checkpointId);
219

220
    terrno = 0;
6,784✔
221
    return pRow;
6,784✔
222
  }
223
}
224

225
static int32_t mndStreamActionInsert(SSdb *pSdb, SStreamObj *pStream) {
1,835✔
226
  mTrace("stream:%s, perform insert action", pStream->name);
1,835✔
227
  return 0;
1,835✔
228
}
229

230
static int32_t mndStreamActionDelete(SSdb *pSdb, SStreamObj *pStream) {
6,784✔
231
  mTrace("stream:%s, perform delete action", pStream->name);
6,784✔
232
  taosWLockLatch(&pStream->lock);
6,784✔
233
  tFreeStreamObj(pStream);
6,784✔
234
  taosWUnLockLatch(&pStream->lock);
6,784✔
235
  return 0;
6,784✔
236
}
237

238
static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pOldStream, SStreamObj *pNewStream) {
3,576✔
239
  mTrace("stream:%s, perform update action", pOldStream->name);
3,576✔
240
  (void)atomic_exchange_32(&pOldStream->version, pNewStream->version);
3,576✔
241

242
  taosWLockLatch(&pOldStream->lock);
3,576✔
243

244
  pOldStream->status = pNewStream->status;
3,576✔
245
  pOldStream->updateTime = pNewStream->updateTime;
3,576✔
246
  pOldStream->checkpointId = pNewStream->checkpointId;
3,576✔
247
  pOldStream->checkpointFreq = pNewStream->checkpointFreq;
3,576✔
248

249
  taosWUnLockLatch(&pOldStream->lock);
3,576✔
250
  return 0;
3,576✔
251
}
252

253
int32_t mndAcquireStream(SMnode *pMnode, char *streamName, SStreamObj **pStream) {
6,808✔
254
  int32_t code = 0;
6,808✔
255
  SSdb   *pSdb = pMnode->pSdb;
6,808✔
256
  (*pStream) = sdbAcquire(pSdb, SDB_STREAM, streamName);
6,808✔
257
  if ((*pStream) == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
6,808!
258
    code = TSDB_CODE_MND_STREAM_NOT_EXIST;
2,740✔
259
  }
260
  return code;
6,808✔
261
}
262

263
void mndReleaseStream(SMnode *pMnode, SStreamObj *pStream) {
15,000✔
264
  SSdb *pSdb = pMnode->pSdb;
15,000✔
265
  sdbRelease(pSdb, pStream);
15,000✔
266
}
15,000✔
267

268
SSdbRaw *mndStreamSeqActionEncode(SStreamObj *pStream) { return NULL; }
×
269
SSdbRow *mndStreamSeqActionDecode(SSdbRaw *pRaw) { return NULL; }
×
270
int32_t  mndStreamSeqActionInsert(SSdb *pSdb, SStreamSeq *pStream) { return 0; }
×
271
int32_t  mndStreamSeqActionDelete(SSdb *pSdb, SStreamSeq *pStream) { return 0; }
×
272
int32_t  mndStreamSeqActionUpdate(SSdb *pSdb, SStreamSeq *pOldStream, SStreamSeq *pNewStream) { return 0; }
×
273

274
static int32_t mndCheckCreateStreamReq(SCMCreateStreamReq *pCreate) {
1,744✔
275
  if (pCreate->name[0] == 0 || pCreate->sql == NULL || pCreate->sql[0] == 0 || pCreate->sourceDB[0] == 0 ||
1,744!
276
      pCreate->targetStbFullName[0] == 0) {
1,744!
277
    return TSDB_CODE_MND_INVALID_STREAM_OPTION;
×
278
  }
279
  return TSDB_CODE_SUCCESS;
1,744✔
280
}
281

282
static int32_t createSchemaByFields(const SArray *pFields, SSchemaWrapper *pWrapper) {
1,740✔
283
  pWrapper->nCols = taosArrayGetSize(pFields);
1,740✔
284
  pWrapper->pSchema = taosMemoryCalloc(pWrapper->nCols, sizeof(SSchema));
1,740!
285
  if (NULL == pWrapper->pSchema) {
1,740!
286
    return terrno;
×
287
  }
288

289
  int32_t index = 0;
1,740✔
290
  for (int32_t i = 0; i < pWrapper->nCols; i++) {
62,400✔
291
    SField *pField = (SField *)taosArrayGet(pFields, i);
60,660✔
292
    if (pField == NULL) {
60,660!
293
      return terrno;
×
294
    }
295

296
    if (TSDB_DATA_TYPE_NULL == pField->type) {
60,660!
297
      pWrapper->pSchema[index].type = TSDB_DATA_TYPE_VARCHAR;
×
298
      pWrapper->pSchema[index].bytes = VARSTR_HEADER_SIZE;
×
299
    } else {
300
      pWrapper->pSchema[index].type = pField->type;
60,660✔
301
      pWrapper->pSchema[index].bytes = pField->bytes;
60,660✔
302
    }
303
    pWrapper->pSchema[index].colId = index + 1;
60,660✔
304
    tstrncpy(pWrapper->pSchema[index].name, pField->name, sizeof(pWrapper->pSchema[index].name));
60,660✔
305
    pWrapper->pSchema[index].flags = pField->flags;
60,660✔
306
    index += 1;
60,660✔
307
  }
308

309
  return TSDB_CODE_SUCCESS;
1,740✔
310
}
311

312
static bool hasDestPrimaryKey(SSchemaWrapper *pWrapper) {
1,740✔
313
  if (pWrapper->nCols < 2) {
1,740!
314
    return false;
×
315
  }
316
  for (int32_t i = 1; i < pWrapper->nCols; i++) {
60,841✔
317
    if (pWrapper->pSchema[i].flags & COL_IS_KEY) {
59,128✔
318
      return true;
27✔
319
    }
320
  }
321
  return false;
1,713✔
322
}
323

324
static int32_t mndBuildStreamObjFromCreateReq(SMnode *pMnode, SStreamObj *pObj, SCMCreateStreamReq *pCreate) {
1,740✔
325
  SNode      *pAst = NULL;
1,740✔
326
  SQueryPlan *pPlan = NULL;
1,740✔
327
  int32_t     code = 0;
1,740✔
328

329
  mInfo("stream:%s to create", pCreate->name);
1,740!
330
  memcpy(pObj->name, pCreate->name, TSDB_STREAM_FNAME_LEN);
1,740✔
331
  pObj->createTime = taosGetTimestampMs();
1,740✔
332
  pObj->updateTime = pObj->createTime;
1,740✔
333
  pObj->version = 1;
1,740✔
334

335
  if (pCreate->smaId > 0) {
1,740✔
336
    pObj->subTableWithoutMd5 = 1;
259✔
337
  }
338

339
  pObj->smaId = pCreate->smaId;
1,740✔
340
  pObj->indexForMultiAggBalance = -1;
1,740✔
341

342
  pObj->uid = mndGenerateUid(pObj->name, strlen(pObj->name));
1,740✔
343

344
  char p[TSDB_STREAM_FNAME_LEN + 32] = {0};
1,740✔
345
  snprintf(p, tListLen(p), "%s_%s", pObj->name, "fillhistory");
1,740✔
346

347
  pObj->hTaskUid = mndGenerateUid(pObj->name, strlen(pObj->name));
1,740✔
348
  pObj->status = 0;
1,740✔
349

350
  pObj->conf.igExpired = pCreate->igExpired;
1,740✔
351
  pObj->conf.trigger = pCreate->triggerType;
1,740✔
352
  pObj->conf.triggerParam = pCreate->maxDelay;
1,740✔
353
  pObj->conf.watermark = pCreate->watermark;
1,740✔
354
  pObj->conf.fillHistory = pCreate->fillHistory;
1,740✔
355
  pObj->deleteMark = pCreate->deleteMark;
1,740✔
356
  pObj->igCheckUpdate = pCreate->igUpdate;
1,740✔
357

358
  memcpy(pObj->sourceDb, pCreate->sourceDB, TSDB_DB_FNAME_LEN);
1,740✔
359
  SDbObj *pSourceDb = mndAcquireDb(pMnode, pCreate->sourceDB);
1,740✔
360
  if (pSourceDb == NULL) {
1,740!
361
    code = terrno;
×
362
    mInfo("stream:%s failed to create, source db %s not exist since %s", pCreate->name, pObj->sourceDb,
×
363
          tstrerror(code));
364
    goto FAIL;
×
365
  }
366

367
  pObj->sourceDbUid = pSourceDb->uid;
1,740✔
368
  mndReleaseDb(pMnode, pSourceDb);
1,740✔
369

370
  memcpy(pObj->targetSTbName, pCreate->targetStbFullName, TSDB_TABLE_FNAME_LEN);
1,740✔
371

372
  SDbObj *pTargetDb = mndAcquireDbByStb(pMnode, pObj->targetSTbName);
1,740✔
373
  if (pTargetDb == NULL) {
1,740!
374
    code = terrno;
×
375
    mError("stream:%s failed to create, target db %s not exist since %s", pCreate->name, pObj->targetDb,
×
376
           tstrerror(code));
377
    goto FAIL;
×
378
  }
379

380
  tstrncpy(pObj->targetDb, pTargetDb->name, TSDB_DB_FNAME_LEN);
1,740✔
381

382
  if (pCreate->createStb == STREAM_CREATE_STABLE_TRUE) {
1,740✔
383
    pObj->targetStbUid = mndGenerateUid(pObj->targetSTbName, TSDB_TABLE_FNAME_LEN);
1,587✔
384
  } else {
385
    pObj->targetStbUid = pCreate->targetStbUid;
153✔
386
  }
387
  pObj->targetDbUid = pTargetDb->uid;
1,740✔
388
  mndReleaseDb(pMnode, pTargetDb);
1,740✔
389

390
  pObj->sql = pCreate->sql;
1,740✔
391
  pObj->ast = pCreate->ast;
1,740✔
392

393
  pCreate->sql = NULL;
1,740✔
394
  pCreate->ast = NULL;
1,740✔
395

396
  // deserialize ast
397
  if ((code = nodesStringToNode(pObj->ast, &pAst)) < 0) {
1,740!
398
    goto FAIL;
×
399
  }
400

401
  // create output schema
402
  if ((code = createSchemaByFields(pCreate->pCols, &pObj->outputSchema)) != TSDB_CODE_SUCCESS) {
1,740!
403
    goto FAIL;
×
404
  }
405

406
  int32_t numOfNULL = taosArrayGetSize(pCreate->fillNullCols);
1,740✔
407
  if (numOfNULL > 0) {
1,740✔
408
    pObj->outputSchema.nCols += numOfNULL;
26✔
409
    SSchema *pFullSchema = taosMemoryCalloc(pObj->outputSchema.nCols, sizeof(SSchema));
26!
410
    if (!pFullSchema) {
26!
411
      code = terrno;
×
412
      goto FAIL;
×
413
    }
414

415
    int32_t nullIndex = 0;
26✔
416
    int32_t dataIndex = 0;
26✔
417
    for (int32_t i = 0; i < pObj->outputSchema.nCols; i++) {
332✔
418
      if (nullIndex >= numOfNULL) {
306!
419
        pFullSchema[i].bytes = pObj->outputSchema.pSchema[dataIndex].bytes;
×
420
        pFullSchema[i].colId = i + 1;  // pObj->outputSchema.pSchema[dataIndex].colId;
×
421
        pFullSchema[i].flags = pObj->outputSchema.pSchema[dataIndex].flags;
×
422
        tstrncpy(pFullSchema[i].name, pObj->outputSchema.pSchema[dataIndex].name, sizeof(pFullSchema[i].name));
×
423
        pFullSchema[i].type = pObj->outputSchema.pSchema[dataIndex].type;
×
424
        dataIndex++;
×
425
      } else {
426
        SColLocation *pos = NULL;
306✔
427
        if (nullIndex < taosArrayGetSize(pCreate->fillNullCols)) {
306!
428
          pos = taosArrayGet(pCreate->fillNullCols, nullIndex);
306✔
429
        }
430

431
        if (pos == NULL) {
306!
432
          mError("invalid null column index, %d", nullIndex);
×
433
          continue;
×
434
        }
435

436
        if (i < pos->slotId) {
306✔
437
          pFullSchema[i].bytes = pObj->outputSchema.pSchema[dataIndex].bytes;
79✔
438
          pFullSchema[i].colId = i + 1;  // pObj->outputSchema.pSchema[dataIndex].colId;
79✔
439
          pFullSchema[i].flags = pObj->outputSchema.pSchema[dataIndex].flags;
79✔
440
          tstrncpy(pFullSchema[i].name, pObj->outputSchema.pSchema[dataIndex].name, sizeof(pFullSchema[i].name));
79✔
441
          pFullSchema[i].type = pObj->outputSchema.pSchema[dataIndex].type;
79✔
442
          dataIndex++;
79✔
443
        } else {
444
          pFullSchema[i].bytes = 0;
227✔
445
          pFullSchema[i].colId = pos->colId;
227✔
446
          pFullSchema[i].flags = COL_SET_NULL;
227✔
447
          memset(pFullSchema[i].name, 0, TSDB_COL_NAME_LEN);
227✔
448
          pFullSchema[i].type = pos->type;
227✔
449
          nullIndex++;
227✔
450
        }
451
      }
452
    }
453

454
    taosMemoryFree(pObj->outputSchema.pSchema);
26!
455
    pObj->outputSchema.pSchema = pFullSchema;
26✔
456
  }
457

458
  SPlanContext cxt = {
1,740✔
459
      .pAstRoot = pAst,
460
      .topicQuery = false,
461
      .streamQuery = true,
462
      .triggerType =
463
          (pObj->conf.trigger == STREAM_TRIGGER_MAX_DELAY) ? STREAM_TRIGGER_WINDOW_CLOSE : pObj->conf.trigger,
1,740✔
464
      .watermark = pObj->conf.watermark,
1,740✔
465
      .igExpired = pObj->conf.igExpired,
1,740✔
466
      .deleteMark = pObj->deleteMark,
1,740✔
467
      .igCheckUpdate = pObj->igCheckUpdate,
1,740✔
468
      .destHasPrimaryKey = hasDestPrimaryKey(&pObj->outputSchema),
1,740✔
469
  };
470

471
  // using ast and param to build physical plan
472
  if ((code = qCreateQueryPlan(&cxt, &pPlan, NULL)) < 0) {
1,740!
473
    goto FAIL;
×
474
  }
475

476
  // save physcial plan
477
  if ((code = nodesNodeToString((SNode *)pPlan, false, &pObj->physicalPlan, NULL)) != 0) {
1,740!
478
    goto FAIL;
×
479
  }
480

481
  pObj->tagSchema.nCols = pCreate->numOfTags;
1,740✔
482
  if (pCreate->numOfTags) {
1,740✔
483
    pObj->tagSchema.pSchema = taosMemoryCalloc(pCreate->numOfTags, sizeof(SSchema));
284!
484
    if (pObj->tagSchema.pSchema == NULL) {
284!
485
      code = terrno;
×
486
      goto FAIL;
×
487
    }
488
  }
489

490
  /*A(pCreate->numOfTags == taosArrayGetSize(pCreate->pTags));*/
491
  for (int32_t i = 0; i < pCreate->numOfTags; i++) {
3,330✔
492
    SField *pField = taosArrayGet(pCreate->pTags, i);
1,590✔
493
    if (pField == NULL) {
1,590!
494
      continue;
×
495
    }
496

497
    pObj->tagSchema.pSchema[i].colId = pObj->outputSchema.nCols + i + 1;
1,590✔
498
    pObj->tagSchema.pSchema[i].bytes = pField->bytes;
1,590✔
499
    pObj->tagSchema.pSchema[i].flags = pField->flags;
1,590✔
500
    pObj->tagSchema.pSchema[i].type = pField->type;
1,590✔
501
    memcpy(pObj->tagSchema.pSchema[i].name, pField->name, TSDB_COL_NAME_LEN);
1,590✔
502
  }
503

504
FAIL:
1,740✔
505
  if (pAst != NULL) nodesDestroyNode(pAst);
1,740!
506
  if (pPlan != NULL) qDestroyQueryPlan(pPlan);
1,740!
507
  return code;
1,740✔
508
}
509

510
int32_t mndPersistTaskDeployReq(STrans *pTrans, SStreamTask *pTask) {
13,999✔
511
  SEncoder encoder;
512
  tEncoderInit(&encoder, NULL, 0);
13,999✔
513

514
  if (pTask->ver < SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
13,999!
515
    pTask->ver = SSTREAM_TASK_VER;
×
516
  }
517

518
  int32_t code = tEncodeStreamTask(&encoder, pTask);
13,999✔
519
  if (code == -1) {
13,999!
520
    tEncoderClear(&encoder);
×
521
    return TSDB_CODE_INVALID_MSG;
×
522
  }
523

524
  int32_t size = encoder.pos;
13,999✔
525
  int32_t tlen = sizeof(SMsgHead) + size;
13,999✔
526
  tEncoderClear(&encoder);
13,999✔
527

528
  void *buf = taosMemoryCalloc(1, tlen);
13,999!
529
  if (buf == NULL) {
13,999!
530
    return terrno;
×
531
  }
532

533
  ((SMsgHead *)buf)->vgId = htonl(pTask->info.nodeId);
13,999✔
534

535
  void *abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
13,999✔
536
  tEncoderInit(&encoder, abuf, size);
13,999✔
537
  code = tEncodeStreamTask(&encoder, pTask);
13,999✔
538
  tEncoderClear(&encoder);
13,999✔
539

540
  if (code != 0) {
13,999!
541
    mError("failed to encode stream task, code:%s", tstrerror(code));
×
542
    taosMemoryFree(buf);
×
543
    return code;
×
544
  }
545

546
  code = setTransAction(pTrans, buf, tlen, TDMT_STREAM_TASK_DEPLOY, &pTask->info.epSet, 0,
13,999✔
547
                        TSDB_CODE_VND_INVALID_VGROUP_ID);
548
  if (code) {
13,999!
549
    taosMemoryFree(buf);
×
550
  }
551

552
  return code;
13,999✔
553
}
554

555
int32_t mndPersistStreamTasks(STrans *pTrans, SStreamObj *pStream) {
1,768✔
556
  SStreamTaskIter *pIter = NULL;
1,768✔
557
  int32_t          code = createStreamTaskIter(pStream, &pIter);
1,768✔
558
  if (code) {
1,768!
559
    mError("failed to create task iter for stream:%s", pStream->name);
×
560
    return code;
×
561
  }
562

563
  while (streamTaskIterNextTask(pIter)) {
10,934✔
564
    SStreamTask *pTask = NULL;
9,166✔
565
    code = streamTaskIterGetCurrent(pIter, &pTask);
9,166✔
566
    if (code) {
9,166!
567
      destroyStreamTaskIter(pIter);
×
568
      return code;
×
569
    }
570

571
    code = mndPersistTaskDeployReq(pTrans, pTask);
9,166✔
572
    if (code) {
9,166!
573
      destroyStreamTaskIter(pIter);
×
574
      return code;
×
575
    }
576
  }
577

578
  destroyStreamTaskIter(pIter);
1,768✔
579

580
  // persistent stream task for already stored ts data
581
  if (pStream->conf.fillHistory) {
1,768✔
582
    int32_t level = taosArrayGetSize(pStream->pHTasksList);
833✔
583

584
    for (int32_t i = 0; i < level; i++) {
2,561✔
585
      SArray *pLevel = taosArrayGetP(pStream->pHTasksList, i);
1,728✔
586

587
      int32_t numOfTasks = taosArrayGetSize(pLevel);
1,728✔
588
      for (int32_t j = 0; j < numOfTasks; j++) {
6,561✔
589
        SStreamTask *pTask = taosArrayGetP(pLevel, j);
4,833✔
590
        code = mndPersistTaskDeployReq(pTrans, pTask);
4,833✔
591
        if (code) {
4,833!
592
          return code;
×
593
        }
594
      }
595
    }
596
  }
597

598
  return code;
1,768✔
599
}
600

601
int32_t mndPersistStream(STrans *pTrans, SStreamObj *pStream) {
1,768✔
602
  int32_t code = 0;
1,768✔
603
  if ((code = mndPersistStreamTasks(pTrans, pStream)) < 0) {
1,768!
604
    return code;
×
605
  }
606

607
  return mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
1,768✔
608
}
609

610
static int32_t mndCreateStbForStream(SMnode *pMnode, STrans *pTrans, const SStreamObj *pStream, const char *user) {
1,587✔
611
  SStbObj *pStb = NULL;
1,587✔
612
  SDbObj  *pDb = NULL;
1,587✔
613
  int32_t  code = 0;
1,587✔
614
  int32_t  lino = 0;
1,587✔
615

616
  SMCreateStbReq createReq = {0};
1,587✔
617
  tstrncpy(createReq.name, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
1,587✔
618
  createReq.numOfColumns = pStream->outputSchema.nCols;
1,587✔
619
  createReq.numOfTags = 1;  // group id
1,587✔
620
  createReq.pColumns = taosArrayInit_s(sizeof(SFieldWithOptions), createReq.numOfColumns);
1,587✔
621
  TSDB_CHECK_NULL(createReq.pColumns, code, lino, _OVER, terrno);
1,587!
622

623
  // build fields
624
  for (int32_t i = 0; i < createReq.numOfColumns; i++) {
60,591✔
625
    SFieldWithOptions *pField = taosArrayGet(createReq.pColumns, i);
59,004✔
626
    TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno);
59,004!
627

628
    tstrncpy(pField->name, pStream->outputSchema.pSchema[i].name, TSDB_COL_NAME_LEN);
59,004✔
629
    pField->flags = pStream->outputSchema.pSchema[i].flags;
59,004✔
630
    pField->type = pStream->outputSchema.pSchema[i].type;
59,004✔
631
    pField->bytes = pStream->outputSchema.pSchema[i].bytes;
59,004✔
632
    pField->compress = createDefaultColCmprByType(pField->type);
59,004✔
633
  }
634

635
  if (pStream->tagSchema.nCols == 0) {
1,587✔
636
    createReq.numOfTags = 1;
1,303✔
637
    createReq.pTags = taosArrayInit_s(sizeof(SField), 1);
1,303✔
638
    TSDB_CHECK_NULL(createReq.pTags, code, lino, _OVER, terrno);
1,303!
639

640
    // build tags
641
    SField *pField = taosArrayGet(createReq.pTags, 0);
1,303✔
642
    TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno);
1,303!
643

644
    tstrncpy(pField->name, "group_id", sizeof(pField->name));
1,303✔
645
    pField->type = TSDB_DATA_TYPE_UBIGINT;
1,303✔
646
    pField->flags = 0;
1,303✔
647
    pField->bytes = 8;
1,303✔
648
  } else {
649
    createReq.numOfTags = pStream->tagSchema.nCols;
284✔
650
    createReq.pTags = taosArrayInit_s(sizeof(SField), createReq.numOfTags);
284✔
651
    TSDB_CHECK_NULL(createReq.pTags, code, lino, _OVER, terrno);
284!
652

653
    for (int32_t i = 0; i < createReq.numOfTags; i++) {
1,874✔
654
      SField *pField = taosArrayGet(createReq.pTags, i);
1,590✔
655
      if (pField == NULL) {
1,590!
656
        continue;
×
657
      }
658

659
      pField->bytes = pStream->tagSchema.pSchema[i].bytes;
1,590✔
660
      pField->flags = pStream->tagSchema.pSchema[i].flags;
1,590✔
661
      pField->type = pStream->tagSchema.pSchema[i].type;
1,590✔
662
      tstrncpy(pField->name, pStream->tagSchema.pSchema[i].name, TSDB_COL_NAME_LEN);
1,590✔
663
    }
664
  }
665

666
  if ((code = mndCheckCreateStbReq(&createReq)) != 0) {
1,587!
667
    goto _OVER;
×
668
  }
669

670
  pStb = mndAcquireStb(pMnode, createReq.name);
1,587✔
671
  if (pStb != NULL) {
1,587!
672
    code = TSDB_CODE_MND_STB_ALREADY_EXIST;
×
673
    goto _OVER;
×
674
  }
675

676
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
1,587✔
677
  if (pDb == NULL) {
1,587!
678
    code = TSDB_CODE_MND_DB_NOT_SELECTED;
×
679
    goto _OVER;
×
680
  }
681

682
  int32_t numOfStbs = -1;
1,587✔
683
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
1,587!
684
    goto _OVER;
×
685
  }
686

687
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
1,587!
688
    code = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
×
689
    goto _OVER;
×
690
  }
691

692
  SStbObj stbObj = {0};
1,587✔
693

694
  if (mndBuildStbFromReq(pMnode, &stbObj, &createReq, pDb) != 0) {
1,587!
695
    goto _OVER;
×
696
  }
697

698
  stbObj.uid = pStream->targetStbUid;
1,587✔
699

700
  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) {
1,587!
701
    mndFreeStb(&stbObj);
×
702
    goto _OVER;
×
703
  }
704

705
  tFreeSMCreateStbReq(&createReq);
1,587✔
706
  mndFreeStb(&stbObj);
1,587✔
707
  mndReleaseStb(pMnode, pStb);
1,587✔
708
  mndReleaseDb(pMnode, pDb);
1,587✔
709
  mDebug("stream:%s create dst stable:%s, cols:%d", pStream->name, pStream->targetSTbName, pStream->outputSchema.nCols);
1,587✔
710
  return code;
1,587✔
711

712
_OVER:
×
713
  tFreeSMCreateStbReq(&createReq);
×
714
  mndReleaseStb(pMnode, pStb);
×
715
  mndReleaseDb(pMnode, pDb);
×
716

717
  mDebug("stream:%s failed to create dst stable:%s, line:%d code:%s", pStream->name, pStream->targetSTbName, lino,
×
718
         tstrerror(code));
719
  return code;
×
720
}
721

722
// 1. stream number check
723
// 2. target stable can not be target table of other existed streams.
724
static int32_t doStreamCheck(SMnode *pMnode, SStreamObj *pStreamObj) {
1,740✔
725
  int32_t     numOfStream = 0;
1,740✔
726
  SStreamObj *pStream = NULL;
1,740✔
727
  void       *pIter = NULL;
1,740✔
728

729
  while ((pIter = sdbFetch(pMnode->pSdb, SDB_STREAM, pIter, (void **)&pStream)) != NULL) {
3,986✔
730
    if (pStream->sourceDbUid == pStreamObj->sourceDbUid) {
2,247✔
731
      ++numOfStream;
1,564✔
732
    }
733

734
    sdbRelease(pMnode->pSdb, pStream);
2,247✔
735

736
    if (numOfStream > MND_STREAM_MAX_NUM) {
2,247!
737
      mError("too many streams, no more than %d for each database, failed to create stream:%s", MND_STREAM_MAX_NUM,
×
738
             pStreamObj->name);
739
      sdbCancelFetch(pMnode->pSdb, pIter);
×
740
      return TSDB_CODE_MND_TOO_MANY_STREAMS;
×
741
    }
742

743
    if (pStream->targetStbUid == pStreamObj->targetStbUid) {
2,247✔
744
      mError("Cannot write the same stable as other stream:%s, failed to create stream:%s", pStream->name,
1!
745
             pStreamObj->name);
746
      sdbCancelFetch(pMnode->pSdb, pIter);
1✔
747
      return TSDB_CODE_MND_INVALID_TARGET_TABLE;
1✔
748
    }
749
  }
750

751
  return TSDB_CODE_SUCCESS;
1,739✔
752
}
753

754
static void *notifyAddrDup(void *p) { return taosStrdup((char *)p); }
×
755

756
static int32_t addStreamTaskNotifyInfo(const SCMCreateStreamReq *createReq, const SStreamObj *pStream,
×
757
                                       SStreamTask *pTask) {
758
  int32_t code = TSDB_CODE_SUCCESS;
×
759
  int32_t lino = 0;
×
760

761
  TSDB_CHECK_NULL(createReq, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
762
  TSDB_CHECK_NULL(pTask, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
763

764
  pTask->notifyInfo.pNotifyAddrUrls = taosArrayDup(createReq->pNotifyAddrUrls, notifyAddrDup);
×
765
  TSDB_CHECK_NULL(pTask->notifyInfo.pNotifyAddrUrls, code, lino, _end, terrno);
×
766
  pTask->notifyInfo.notifyEventTypes = createReq->notifyEventTypes;
×
767
  pTask->notifyInfo.notifyErrorHandle = createReq->notifyErrorHandle;
×
768
  pTask->notifyInfo.streamName = taosStrdup(mndGetDbStr(createReq->name));
×
769
  TSDB_CHECK_NULL(pTask->notifyInfo.streamName, code, lino, _end, terrno);
×
770
  pTask->notifyInfo.stbFullName = taosStrdup(createReq->targetStbFullName);
×
771
  TSDB_CHECK_NULL(pTask->notifyInfo.stbFullName, code, lino, _end, terrno);
×
772
  pTask->notifyInfo.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
×
773
  TSDB_CHECK_NULL(pTask->notifyInfo.pSchemaWrapper, code, lino, _end, terrno);
×
774

775
_end:
×
776
  if (code != TSDB_CODE_SUCCESS) {
×
777
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
778
  }
779
  return code;
×
780
}
781

782
static int32_t addStreamNotifyInfo(SCMCreateStreamReq *createReq, SStreamObj *pStream) {
1,739✔
783
  int32_t code = TSDB_CODE_SUCCESS;
1,739✔
784
  int32_t lino = 0;
1,739✔
785
  int32_t level = 0;
1,739✔
786
  int32_t nTasks = 0;
1,739✔
787
  SArray *pLevel = NULL;
1,739✔
788

789
  TSDB_CHECK_NULL(createReq, code, lino, _end, TSDB_CODE_INVALID_PARA);
1,739!
790
  TSDB_CHECK_NULL(pStream, code, lino, _end, TSDB_CODE_INVALID_PARA);
1,739!
791

792
  if (taosArrayGetSize(createReq->pNotifyAddrUrls) == 0) {
1,739!
793
    goto _end;
1,739✔
794
  }
795

796
  level = taosArrayGetSize(pStream->tasks);
×
797
  for (int32_t i = 0; i < level; ++i) {
×
798
    pLevel = taosArrayGetP(pStream->tasks, i);
×
799
    nTasks = taosArrayGetSize(pLevel);
×
800
    for (int32_t j = 0; j < nTasks; ++j) {
×
801
      code = addStreamTaskNotifyInfo(createReq, pStream, taosArrayGetP(pLevel, j));
×
802
      TSDB_CHECK_CODE(code, lino, _end);
×
803
    }
804
  }
805

806
  if (pStream->conf.fillHistory && createReq->notifyHistory) {
×
807
    level = taosArrayGetSize(pStream->pHTasksList);
×
808
    for (int32_t i = 0; i < level; ++i) {
×
809
      pLevel = taosArrayGetP(pStream->pHTasksList, i);
×
810
      nTasks = taosArrayGetSize(pLevel);
×
811
      for (int32_t j = 0; j < nTasks; ++j) {
×
812
        code = addStreamTaskNotifyInfo(createReq, pStream, taosArrayGetP(pLevel, j));
×
813
        TSDB_CHECK_CODE(code, lino, _end);
×
814
      }
815
    }
816
  }
817

818
_end:
×
819
  if (code != TSDB_CODE_SUCCESS) {
1,739!
820
    mError("%s for stream %s failed at line %d since %s", __func__, pStream->name, lino, tstrerror(code));
×
821
  }
822
  return code;
1,739✔
823
}
824

825
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq) {
1,744✔
826
  SMnode     *pMnode = pReq->info.node;
1,744✔
827
  SStreamObj *pStream = NULL;
1,744✔
828
  SStreamObj  streamObj = {0};
1,744✔
829
  char       *sql = NULL;
1,744✔
830
  int32_t     sqlLen = 0;
1,744✔
831
  const char *pMsg = "create stream tasks on dnodes";
1,744✔
832
  int32_t     code = TSDB_CODE_SUCCESS;
1,744✔
833
  int32_t     lino = 0;
1,744✔
834
  STrans     *pTrans = NULL;
1,744✔
835

836
  SCMCreateStreamReq createReq = {0};
1,744✔
837
  code = tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, &createReq);
1,744✔
838
  TSDB_CHECK_CODE(code, lino, _OVER);
1,744!
839

840
#ifdef WINDOWS
841
  code = TSDB_CODE_MND_INVALID_PLATFORM;
842
  goto _OVER;
843
#endif
844

845
  mInfo("stream:%s, start to create stream, sql:%s", createReq.name, createReq.sql);
1,744!
846
  if ((code = mndCheckCreateStreamReq(&createReq)) != 0) {
1,744!
847
    mError("stream:%s, failed to create since %s", createReq.name, tstrerror(code));
×
848
    goto _OVER;
×
849
  }
850

851
  code = mndAcquireStream(pMnode, createReq.name, &pStream);
1,744✔
852
  if (pStream != NULL && code == 0) {
1,744!
853
    if (createReq.igExists) {
2✔
854
      mInfo("stream:%s, already exist, ignore exist is set", createReq.name);
1!
855
      mndReleaseStream(pMnode, pStream);
1✔
856
      tFreeSCMCreateStreamReq(&createReq);
1✔
857
      return code;
1✔
858
    } else {
859
      code = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
1✔
860
      goto _OVER;
1✔
861
    }
862
  } else if (code != TSDB_CODE_MND_STREAM_NOT_EXIST) {
1,742!
863
    goto _OVER;
×
864
  }
865

866
  if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
1,742!
867
    goto _OVER;
×
868
  }
869

870
  if (createReq.sql != NULL) {
1,742!
871
    sql = taosStrdup(createReq.sql);
1,742!
872
    TSDB_CHECK_NULL(sql, code, lino, _OVER, terrno);
1,742!
873
  }
874

875
  // check for the taskEp update trans
876
  if (isNodeUpdateTransActive()) {
1,742!
877
    mError("stream:%s failed to create stream, node update trans is active", createReq.name);
×
878
    code = TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
879
    goto _OVER;
×
880
  }
881

882
  SDbObj *pSourceDb = mndAcquireDb(pMnode, createReq.sourceDB);
1,742✔
883
  if (pSourceDb == NULL) {
1,742!
884
    code = terrno;
×
885
    mInfo("stream:%s failed to create, acquire source db %s failed, code:%s", createReq.name, createReq.sourceDB,
×
886
          tstrerror(code));
887
    goto _OVER;
×
888
  }
889

890
  code = mndCheckForSnode(pMnode, pSourceDb);
1,742✔
891
  mndReleaseDb(pMnode, pSourceDb);
1,742✔
892
  if (code != 0) {
1,742✔
893
    goto _OVER;
2✔
894
  }
895

896
  // build stream obj from request
897
  if ((code = mndBuildStreamObjFromCreateReq(pMnode, &streamObj, &createReq)) < 0) {
1,740!
898
    mError("stream:%s, failed to create since %s", createReq.name, tstrerror(code));
×
899
    goto _OVER;
×
900
  }
901

902
  code = doStreamCheck(pMnode, &streamObj);
1,740✔
903
  TSDB_CHECK_CODE(code, lino, _OVER);
1,740✔
904

905
  code = doCreateTrans(pMnode, &streamObj, pReq, TRN_CONFLICT_DB, MND_STREAM_CREATE_NAME, pMsg, &pTrans);
1,739✔
906
  if (pTrans == NULL || code) {
1,739!
907
    goto _OVER;
×
908
  }
909

910
  // create stb for stream
911
  if (createReq.createStb == STREAM_CREATE_STABLE_TRUE) {
1,739✔
912
    if ((code = mndCreateStbForStream(pMnode, pTrans, &streamObj, pReq->info.conn.user)) < 0) {
1,587!
913
      mError("trans:%d, failed to create stb for stream %s since %s", pTrans->id, createReq.name, tstrerror(code));
×
914
      mndTransDrop(pTrans);
×
915
      goto _OVER;
×
916
    }
917
  } else {
918
    mDebug("stream:%s no need create stable", createReq.name);
152✔
919
  }
920

921
  // schedule stream task for stream obj
922
  code = mndScheduleStream(pMnode, &streamObj, createReq.lastTs, createReq.pVgroupVerList);
1,739✔
923
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,739!
924
    mError("stream:%s, failed to schedule since %s", createReq.name, tstrerror(code));
×
925
    mndTransDrop(pTrans);
×
926
    goto _OVER;
×
927
  }
928

929
  // add notify info into all stream tasks
930
  code = addStreamNotifyInfo(&createReq, &streamObj);
1,739✔
931
  if (code != TSDB_CODE_SUCCESS) {
1,739!
932
    mError("stream:%s failed to add stream notify info since %s", createReq.name, tstrerror(code));
×
933
    mndTransDrop(pTrans);
×
934
    goto _OVER;
×
935
  }
936

937
  // add stream to trans
938
  code = mndPersistStream(pTrans, &streamObj);
1,739✔
939
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,739!
940
    mError("stream:%s, failed to persist since %s", createReq.name, tstrerror(code));
×
941
    mndTransDrop(pTrans);
×
942
    goto _OVER;
×
943
  }
944

945
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_READ_DB, streamObj.sourceDb)) != 0) {
1,739!
946
    mndTransDrop(pTrans);
×
947
    goto _OVER;
×
948
  }
949

950
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, streamObj.targetDb)) != 0) {
1,739!
951
    mndTransDrop(pTrans);
×
952
    goto _OVER;
×
953
  }
954

955
  // add into buffer firstly
956
  // to make sure when the hb from vnode arrived, the newly created tasks have been in the task map already.
957
  streamMutexLock(&execInfo.lock);
1,739✔
958
  mDebug("stream stream:%s start to register tasks into task nodeList and set initial checkpointId", createReq.name);
1,739✔
959
  saveTaskAndNodeInfoIntoBuf(&streamObj, &execInfo);
1,739✔
960
  streamMutexUnlock(&execInfo.lock);
1,739✔
961

962
  // execute creation
963
  code = mndTransPrepare(pMnode, pTrans);
1,739✔
964
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,739!
965
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
966
    mndTransDrop(pTrans);
×
967
    goto _OVER;
×
968
  }
969

970
  mndTransDrop(pTrans);
1,739✔
971

972
  SName dbname = {0};
1,739✔
973
  code = tNameFromString(&dbname, createReq.sourceDB, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
1,739✔
974
  if (code) {
1,739!
975
    mError("invalid source dbname:%s in create stream, code:%s", createReq.sourceDB, tstrerror(code));
×
976
    goto _OVER;
×
977
  }
978

979
  SName name = {0};
1,739✔
980
  code = tNameFromString(&name, createReq.name, T_NAME_ACCT | T_NAME_TABLE);
1,739✔
981
  if (code) {
1,739!
982
    mError("invalid stream name:%s in create strem, code:%s", createReq.name, tstrerror(code));
×
983
    goto _OVER;
×
984
  }
985

986
  // reuse this function for stream
987
  if (sql != NULL && sqlLen > 0) {
1,739!
988
    auditRecord(pReq, pMnode->clusterId, "createStream", dbname.dbname, name.dbname, sql, sqlLen);
×
989
  } else {
990
    char detail[1000] = {0};
1,739✔
991
    snprintf(detail, tListLen(detail), "dbname:%s, stream name:%s", dbname.dbname, name.dbname);
1,739✔
992
    auditRecord(pReq, pMnode->clusterId, "createStream", dbname.dbname, name.dbname, detail, strlen(detail));
1,739✔
993
  }
994

995
_OVER:
1,743✔
996
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,743!
997
    mError("stream:%s, failed to create at line:%d since %s", createReq.name, lino, tstrerror(code));
4!
998
  } else {
999
    mDebug("stream:%s create stream completed", createReq.name);
1,739✔
1000
    code = TSDB_CODE_ACTION_IN_PROGRESS;
1,739✔
1001
  }
1002

1003
  mndReleaseStream(pMnode, pStream);
1,743✔
1004
  tFreeSCMCreateStreamReq(&createReq);
1,743✔
1005
  tFreeStreamObj(&streamObj);
1,743✔
1006

1007
  if (sql != NULL) {
1,743✔
1008
    taosMemoryFreeClear(sql);
1,742!
1009
  }
1010

1011
  return code;
1,743✔
1012
}
1013

1014
static int32_t mndProcessRestartStreamReq(SRpcMsg *pReq) {
×
1015
  SMnode          *pMnode = pReq->info.node;
×
1016
  SStreamObj      *pStream = NULL;
×
1017
  int32_t          code = 0;
×
1018
  SMPauseStreamReq pauseReq = {0};
×
1019

1020
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
×
1021
    return TSDB_CODE_INVALID_MSG;
×
1022
  }
1023

1024
  code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
×
1025
  if (pStream == NULL || code != 0) {
×
1026
    if (pauseReq.igNotExists) {
×
1027
      mInfo("stream:%s, not exist, not restart stream", pauseReq.name);
×
1028
      return 0;
×
1029
    } else {
1030
      mError("stream:%s not exist, failed to restart stream", pauseReq.name);
×
1031
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1032
    }
1033
  }
1034

1035
  mInfo("stream:%s,%" PRId64 " start to restart stream", pauseReq.name, pStream->uid);
×
1036
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb)) != 0) {
×
1037
    sdbRelease(pMnode->pSdb, pStream);
×
1038
    return code;
×
1039
  }
1040

1041
  // check if it is conflict with other trans in both sourceDb and targetDb.
1042
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_RESTART_NAME, true);
×
1043
  if (code) {
×
1044
    sdbRelease(pMnode->pSdb, pStream);
×
1045
    return code;
×
1046
  }
1047

1048
  bool updated = mndStreamNodeIsUpdated(pMnode);
×
1049
  if (updated) {
×
1050
    mError("tasks are not ready for restart, node update detected");
×
1051
    sdbRelease(pMnode->pSdb, pStream);
×
1052
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1053
  }
1054

1055
  STrans *pTrans = NULL;
×
1056
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RESTART_NAME, "restart the stream",
×
1057
                       &pTrans);
1058
  if (pTrans == NULL || code) {
×
1059
    mError("stream:%s failed to pause stream since %s", pauseReq.name, tstrerror(code));
×
1060
    sdbRelease(pMnode->pSdb, pStream);
×
1061
    return code;
×
1062
  }
1063

1064
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_RESTART_NAME, pStream->uid);
×
1065
  if (code) {
×
1066
    sdbRelease(pMnode->pSdb, pStream);
×
1067
    mndTransDrop(pTrans);
×
1068
    return code;
×
1069
  }
1070

1071
  // if nodeUpdate happened, not send pause trans
1072
  code = mndStreamSetRestartAction(pMnode, pTrans, pStream);
×
1073
  if (code) {
×
1074
    mError("stream:%s, failed to restart task since %s", pauseReq.name, tstrerror(code));
×
1075
    sdbRelease(pMnode->pSdb, pStream);
×
1076
    mndTransDrop(pTrans);
×
1077
    return code;
×
1078
  }
1079

1080
  code = mndTransPrepare(pMnode, pTrans);
×
1081
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
1082
    mError("trans:%d, failed to prepare restart stream trans since %s", pTrans->id, tstrerror(code));
×
1083
    sdbRelease(pMnode->pSdb, pStream);
×
1084
    mndTransDrop(pTrans);
×
1085
    return code;
×
1086
  }
1087

1088
  sdbRelease(pMnode->pSdb, pStream);
×
1089
  mndTransDrop(pTrans);
×
1090

1091
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
1092
}
1093

1094
int64_t mndStreamGenChkptId(SMnode *pMnode, bool lock) {
1,354✔
1095
  SStreamObj *pStream = NULL;
1,354✔
1096
  void       *pIter = NULL;
1,354✔
1097
  SSdb       *pSdb = pMnode->pSdb;
1,354✔
1098
  int64_t     maxChkptId = 0;
1,354✔
1099

1100
  while (1) {
1101
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
4,704✔
1102
    if (pIter == NULL) break;
4,704✔
1103

1104
    maxChkptId = TMAX(maxChkptId, pStream->checkpointId);
3,350✔
1105
    mDebug("stream:%p, %s id:0x%" PRIx64 " checkpoint %" PRId64 "", pStream, pStream->name, pStream->uid,
3,350✔
1106
           pStream->checkpointId);
1107
    sdbRelease(pSdb, pStream);
3,350✔
1108
  }
1109

1110
  {  // check the max checkpoint id from all vnodes.
1111
    int64_t maxCheckpointId = -1;
1,354✔
1112
    if (lock) {
1,354✔
1113
      streamMutexLock(&execInfo.lock);
624✔
1114
    }
1115

1116
    for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
15,930✔
1117
      STaskId          *p = taosArrayGet(execInfo.pTaskList, i);
14,576✔
1118
      STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
14,576✔
1119
      if (p == NULL || pEntry == NULL) {
14,576!
1120
        continue;
×
1121
      }
1122

1123
      if (pEntry->checkpointInfo.failed) {
14,576!
1124
        continue;
×
1125
      }
1126

1127
      if (maxCheckpointId < pEntry->checkpointInfo.latestId) {
14,576✔
1128
        maxCheckpointId = pEntry->checkpointInfo.latestId;
2,003✔
1129
      }
1130
    }
1131

1132
    if (lock) {
1,354✔
1133
      streamMutexUnlock(&execInfo.lock);
624✔
1134
    }
1135

1136
    if (maxCheckpointId > maxChkptId) {
1,354!
1137
      mDebug("max checkpointId in mnode:%" PRId64 ", smaller than max checkpointId in vnode:%" PRId64, maxChkptId,
×
1138
             maxCheckpointId);
1139
      maxChkptId = maxCheckpointId;
×
1140
    }
1141
  }
1142

1143
  mDebug("generate new checkpointId:%" PRId64, maxChkptId + 1);
1,354✔
1144
  return maxChkptId + 1;
1,354✔
1145
}
1146

1147
static int32_t mndProcessStreamCheckpointTrans(SMnode *pMnode, SStreamObj *pStream, int64_t checkpointId,
1,356✔
1148
                                               int8_t mndTrigger, bool lock) {
1149
  int32_t code = TSDB_CODE_SUCCESS;
1,356✔
1150
  bool    conflict = false;
1,356✔
1151
  int64_t ts = taosGetTimestampMs();
1,356✔
1152
  STrans *pTrans = NULL;
1,356✔
1153

1154
  if (mndTrigger == 1 && (ts - pStream->checkpointFreq < tsStreamCheckpointInterval * 1000)) {
1,356!
1155
    return code;
×
1156
  }
1157

1158
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHECKPOINT_NAME, lock);
1,356✔
1159
  if (code) {
1,356✔
1160
    mWarn("checkpoint conflict with other trans in %s, code:%s ignore the checkpoint for stream:%s %" PRIx64,
2!
1161
          pStream->sourceDb, tstrerror(code), pStream->name, pStream->uid);
1162
    goto _ERR;
2✔
1163
  }
1164

1165
  code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHECKPOINT_NAME,
1,354✔
1166
                       "gen checkpoint for stream", &pTrans);
1167
  if (code) {
1,354!
1168
    mError("failed to checkpoint of stream name%s, checkpointId: %" PRId64 ", reason:%s", pStream->name, checkpointId,
×
1169
           tstrerror(code));
1170
    goto _ERR;
×
1171
  }
1172

1173
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHECKPOINT_NAME, pStream->uid);
1,354✔
1174
  if (code) {
1,354!
1175
    mError("failed to register checkpoint trans for stream:%s, checkpointId:%" PRId64, pStream->name, checkpointId);
×
1176
    goto _ERR;
×
1177
  }
1178

1179
  mDebug("start to trigger checkpoint for stream:%s, checkpoint: %" PRId64 "", pStream->name, checkpointId);
1,354✔
1180

1181
  taosWLockLatch(&pStream->lock);
1,354✔
1182
  pStream->currentTick = 1;
1,354✔
1183

1184
  // 1. redo action: broadcast checkpoint source msg for all source vg
1185
  int32_t totalLevel = taosArrayGetSize(pStream->tasks);
1,354✔
1186
  for (int32_t i = 0; i < totalLevel; i++) {
4,094✔
1187
    SArray      *pLevel = taosArrayGetP(pStream->tasks, i);
2,740✔
1188
    SStreamTask *p = taosArrayGetP(pLevel, 0);
2,740✔
1189

1190
    if (p->info.taskLevel == TASK_LEVEL__SOURCE) {
2,740✔
1191
      int32_t sz = taosArrayGetSize(pLevel);
1,354✔
1192
      for (int32_t j = 0; j < sz; j++) {
4,549✔
1193
        SStreamTask *pTask = taosArrayGetP(pLevel, j);
3,195✔
1194
        code = mndStreamSetCheckpointAction(pMnode, pTrans, pTask, checkpointId, mndTrigger);
3,195✔
1195

1196
        if (code != TSDB_CODE_SUCCESS) {
3,195!
1197
          taosWUnLockLatch(&pStream->lock);
×
1198
          goto _ERR;
×
1199
        }
1200
      }
1201
    }
1202
  }
1203

1204
  // 2. reset tick
1205
  pStream->checkpointId = checkpointId;
1,354✔
1206
  pStream->checkpointFreq = taosGetTimestampMs();
1,354✔
1207
  pStream->currentTick = 0;
1,354✔
1208

1209
  // 3. commit log: stream checkpoint info
1210
  pStream->version = pStream->version + 1;
1,354✔
1211
  taosWUnLockLatch(&pStream->lock);
1,354✔
1212

1213
  if ((code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY)) != TSDB_CODE_SUCCESS) {
1,354!
1214
    goto _ERR;
×
1215
  }
1216

1217
  code = mndTransPrepare(pMnode, pTrans);
1,354✔
1218
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,354!
1219
    mError("failed to prepare checkpoint trans since %s", tstrerror(code));
×
1220
  } else {
1221
    code = TSDB_CODE_ACTION_IN_PROGRESS;
1,354✔
1222
  }
1223

1224
_ERR:
1,356✔
1225
  mndTransDrop(pTrans);
1,356✔
1226
  return code;
1,356✔
1227
}
1228

1229
int32_t extractStreamNodeList(SMnode *pMnode) {
3,651✔
1230
  if (taosArrayGetSize(execInfo.pNodeList) == 0) {
3,651✔
1231
    int32_t code = refreshNodeListFromExistedStreams(pMnode, execInfo.pNodeList);
785✔
1232
    if (code) {
785!
1233
      mError("Failed to extract node list from stream, code:%s", tstrerror(code));
×
1234
      return code;
×
1235
    }
1236
  }
1237

1238
  return taosArrayGetSize(execInfo.pNodeList);
3,651✔
1239
}
1240

1241
static int32_t mndCheckTaskAndNodeStatus(SMnode *pMnode) {
1,793✔
1242
  int32_t code = 0;
1,793✔
1243
  if (mndStreamNodeIsUpdated(pMnode)) {
1,793✔
1244
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
14✔
1245
  }
1246

1247
  streamMutexLock(&execInfo.lock);
1,779✔
1248
  if (taosArrayGetSize(execInfo.pNodeList) == 0) {
1,779✔
1249
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
785✔
1250
    if (taosArrayGetSize(execInfo.pTaskList) != 0) {
785!
1251
      mError("stream task node change checking done, no vgroups exist, but task list is not empty");
×
1252
      code = TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1253
    }
1254
  }
1255

1256
  streamMutexUnlock(&execInfo.lock);
1,779✔
1257
  return code;
1,779✔
1258
}
1259

1260
int64_t getStreamTaskLastReadyState(SArray *pTaskList, int64_t streamId) {
1,828✔
1261
  int64_t ts = -1;
1,828✔
1262
  int32_t taskId = -1;
1,828✔
1263

1264
  for (int32_t i = 0; i < taosArrayGetSize(pTaskList); ++i) {
31,380✔
1265
    STaskId          *p = taosArrayGet(pTaskList, i);
29,649✔
1266
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
29,649✔
1267
    if (p == NULL || pEntry == NULL || pEntry->id.streamId != streamId) {
29,649!
1268
      continue;
23,109✔
1269
    }
1270

1271
    // -1 denote not ready now or never ready till now
1272
    if (pEntry->hTaskId != 0) {
6,540✔
1273
      mInfo("s-task:0x%" PRIx64 "-0x%x (nodeId:%d) status:%s related fill-history task:0x%" PRIx64
5!
1274
            " exists, checkpoint not issued",
1275
            pEntry->id.streamId, (int32_t)pEntry->id.taskId, pEntry->nodeId, streamTaskGetStatusStr(pEntry->status),
1276
            pEntry->hTaskId);
1277
      return -1;
5✔
1278
    }
1279

1280
    if (pEntry->status != TASK_STATUS__READY) {
6,535✔
1281
      mInfo("s-task:0x%" PRIx64 "-0x%x (nodeId:%d) status:%s, not ready for checkpoint", pEntry->id.streamId,
92!
1282
            (int32_t)pEntry->id.taskId, pEntry->nodeId, streamTaskGetStatusStr(pEntry->status));
1283
      return -1;
92✔
1284
    }
1285

1286
    if (ts < pEntry->startTime) {
6,443✔
1287
      ts = pEntry->startTime;
3,360✔
1288
      taskId = pEntry->id.taskId;
3,360✔
1289
    }
1290
  }
1291

1292
  mDebug("stream:0x%" PRIx64 " last ready ts:%" PRId64 " s-task:0x%x", streamId, ts, taskId);
1,731✔
1293
  return ts;
1,731✔
1294
}
1295

1296
typedef struct {
1297
  int64_t streamId;
1298
  int64_t duration;
1299
} SCheckpointInterval;
1300

1301
static int32_t streamWaitComparFn(const void *p1, const void *p2) {
669✔
1302
  const SCheckpointInterval *pInt1 = p1;
669✔
1303
  const SCheckpointInterval *pInt2 = p2;
669✔
1304
  if (pInt1->duration == pInt2->duration) {
669✔
1305
    return 0;
54✔
1306
  }
1307

1308
  return pInt1->duration > pInt2->duration ? -1 : 1;
615✔
1309
}
1310

1311
// all tasks of this stream should be ready, otherwise do nothing
1312
static bool isStreamReadyHelp(int64_t now, SStreamObj* pStream) {
1,828✔
1313
  bool ready = false;
1,828✔
1314

1315
  streamMutexLock(&execInfo.lock);
1,828✔
1316

1317
  int64_t lastReadyTs = getStreamTaskLastReadyState(execInfo.pTaskList, pStream->uid);
1,828✔
1318
  if ((lastReadyTs == -1) || ((lastReadyTs != -1) && ((now - lastReadyTs) < tsStreamCheckpointInterval * 1000))) {
1,828!
1319

1320
    if (lastReadyTs != -1) {
562✔
1321
      mInfo("not start checkpoint, stream:0x%" PRIx64 " last ready ts:%" PRId64 " ready duration:%" PRId64
464!
1322
            "ms less than threshold",
1323
            pStream->uid, lastReadyTs, (now - lastReadyTs));
1324
    }
1325

1326
    ready = false;
562✔
1327
  } else {
1328
    ready = true;
1,266✔
1329
  }
1330

1331
  streamMutexUnlock(&execInfo.lock);
1,828✔
1332
  return ready;
1,828✔
1333
}
1334

1335
static int32_t mndProcessStreamCheckpoint(SRpcMsg *pReq) {
1,793✔
1336
  SMnode     *pMnode = pReq->info.node;
1,793✔
1337
  SSdb       *pSdb = pMnode->pSdb;
1,793✔
1338
  void       *pIter = NULL;
1,793✔
1339
  SStreamObj *pStream = NULL;
1,793✔
1340
  int32_t     code = 0;
1,793✔
1341
  int32_t     numOfCheckpointTrans = 0;
1,793✔
1342

1343
  if ((code = mndCheckTaskAndNodeStatus(pMnode)) != 0) {
1,793✔
1344
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
14✔
1345
  }
1346

1347
  SArray *pList = taosArrayInit(4, sizeof(SCheckpointInterval));
1,779✔
1348
  if (pList == NULL) {
1,779!
UNCOV
1349
    return terrno;
×
1350
  }
1351

1352
  int64_t now = taosGetTimestampMs();
1,779✔
1353

1354
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream)) != NULL) {
4,482✔
1355
    int64_t duration = now - pStream->checkpointFreq;
2,703✔
1356
    if (duration < tsStreamCheckpointInterval * 1000) {
2,703✔
1357
      sdbRelease(pSdb, pStream);
875✔
1358
      continue;
1,437✔
1359
    }
1360

1361
    bool ready = isStreamReadyHelp(now, pStream);
1,828✔
1362
    if (!ready) {
1,828✔
1363
      sdbRelease(pSdb, pStream);
562✔
1364
      continue;
562✔
1365
    }
1366

1367
    SCheckpointInterval in = {.streamId = pStream->uid, .duration = duration};
1,266✔
1368
    void               *p = taosArrayPush(pList, &in);
1,266✔
1369
    if (p) {
1,266!
1370
      int32_t currentSize = taosArrayGetSize(pList);
1,266✔
1371
      mDebug("stream:%s (uid:0x%" PRIx64 ") total %d stream(s) beyond chkpt interval threshold: %ds(%" PRId64
1,266✔
1372
             "s), concurrently launch threshold:%d",
1373
             pStream->name, pStream->uid, currentSize, tsStreamCheckpointInterval, duration / 1000,
1374
             tsMaxConcurrentCheckpoint);
1375
    } else {
UNCOV
1376
      mError("failed to record the checkpoint interval info, stream:0x%" PRIx64, pStream->uid);
×
1377
    }
1378
    sdbRelease(pSdb, pStream);
1,266✔
1379
  }
1380

1381
  int32_t size = taosArrayGetSize(pList);
1,779✔
1382
  if (size == 0) {
1,779✔
1383
    taosArrayDestroy(pList);
1,155✔
1384
    return code;
1,155✔
1385
  }
1386

1387
  taosArraySort(pList, streamWaitComparFn);
624✔
1388
  code = mndStreamClearFinishedTrans(pMnode, &numOfCheckpointTrans);
624✔
1389
  if (code) {
624!
1390
    mError("failed to clear finish trans, code:%s", tstrerror(code));
×
UNCOV
1391
    taosArrayDestroy(pList);
×
UNCOV
1392
    return code;
×
1393
  }
1394

1395
  int32_t numOfQual = taosArrayGetSize(pList);
624✔
1396
  if (numOfCheckpointTrans >= tsMaxConcurrentCheckpoint) {
624!
UNCOV
1397
    mDebug(
×
1398
        "%d stream(s) checkpoint interval longer than %ds, ongoing checkpoint trans:%d reach maximum allowed:%d, new "
1399
        "checkpoint trans are not allowed, wait for 30s",
1400
        numOfQual, tsStreamCheckpointInterval, numOfCheckpointTrans, tsMaxConcurrentCheckpoint);
UNCOV
1401
    taosArrayDestroy(pList);
×
UNCOV
1402
    return code;
×
1403
  }
1404

1405
  int32_t capacity = tsMaxConcurrentCheckpoint - numOfCheckpointTrans;
624✔
1406
  mDebug(
624✔
1407
      "%d stream(s) checkpoint interval longer than %ds, %d ongoing checkpoint trans, %d new checkpoint trans allowed, "
1408
      "concurrent trans threshold:%d",
1409
      numOfQual, tsStreamCheckpointInterval, numOfCheckpointTrans, capacity, tsMaxConcurrentCheckpoint);
1410

1411
  int32_t started = 0;
624✔
1412
  int64_t checkpointId = mndStreamGenChkptId(pMnode, true);
624✔
1413

1414
  for (int32_t i = 0; i < numOfQual; ++i) {
628✔
1415
    SCheckpointInterval *pCheckpointInfo = taosArrayGet(pList, i);
626✔
1416
    if (pCheckpointInfo == NULL) {
626!
UNCOV
1417
      continue;
×
1418
    }
1419

1420
    SStreamObj *p = NULL;
626✔
1421
    code = mndGetStreamObj(pMnode, pCheckpointInfo->streamId, &p);
626✔
1422
    if (p != NULL && code == 0) {
626!
1423
      code = mndProcessStreamCheckpointTrans(pMnode, p, checkpointId, 1, true);
626✔
1424
      sdbRelease(pSdb, p);
626✔
1425

1426
      if (code == 0 || code == TSDB_CODE_ACTION_IN_PROGRESS) {
626!
1427
        started += 1;
626✔
1428

1429
        if (started >= capacity) {
626✔
1430
          mDebug("already start %d new checkpoint trans, current active checkpoint trans:%d", started,
622✔
1431
                 (started + numOfCheckpointTrans));
1432
          break;
622✔
1433
        }
1434
      } else {
UNCOV
1435
        mError("failed to start checkpoint trans, code:%s", tstrerror(code));
×
1436
      }
1437
    }
1438
  }
1439

1440
  taosArrayDestroy(pList);
624✔
1441
  return code;
624✔
1442
}
1443

1444
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) {
1,412✔
1445
  SMnode     *pMnode = pReq->info.node;
1,412✔
1446
  SStreamObj *pStream = NULL;
1,412✔
1447
  int32_t     code = 0;
1,412✔
1448

1449
  SMDropStreamReq dropReq = {0};
1,412✔
1450
  if (tDeserializeSMDropStreamReq(pReq->pCont, pReq->contLen, &dropReq) < 0) {
1,412!
UNCOV
1451
    mError("invalid drop stream msg recv, discarded");
×
UNCOV
1452
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1453
    TAOS_RETURN(code);
×
1454
  }
1455

1456
  mDebug("recv drop stream:%s msg", dropReq.name);
1,412✔
1457

1458
  code = mndAcquireStream(pMnode, dropReq.name, &pStream);
1,412✔
1459
  if (pStream == NULL || code != 0) {
1,412!
1460
    if (dropReq.igNotExists) {
138✔
1461
      mInfo("stream:%s not exist, ignore not exist is set, drop stream exec done with success", dropReq.name);
128!
1462
      sdbRelease(pMnode->pSdb, pStream);
128✔
1463
      tFreeMDropStreamReq(&dropReq);
128✔
1464
      return 0;
128✔
1465
    } else {
1466
      mError("stream:%s not exist failed to drop it", dropReq.name);
10!
1467
      tFreeMDropStreamReq(&dropReq);
10✔
1468
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
10✔
1469
    }
1470
  }
1471

1472
  if (pStream->smaId != 0) {
1,274✔
1473
    mDebug("stream:%s, uid:0x%" PRIx64 " try to drop sma related stream", dropReq.name, pStream->uid);
219!
1474

1475
    void    *pIter = NULL;
219✔
1476
    SSmaObj *pSma = NULL;
219✔
1477
    pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
219✔
1478
    while (pIter) {
361✔
1479
      if (pSma && pSma->uid == pStream->smaId) {
147!
1480
        sdbRelease(pMnode->pSdb, pSma);
5✔
1481
        sdbRelease(pMnode->pSdb, pStream);
5✔
1482

1483
        sdbCancelFetch(pMnode->pSdb, pIter);
5✔
1484
        tFreeMDropStreamReq(&dropReq);
5✔
1485
        code = TSDB_CODE_TSMA_MUST_BE_DROPPED;
5✔
1486

1487
        mError("try to drop sma-related stream:%s, uid:0x%" PRIx64 " code:%s only allowed to be dropped along with sma",
5!
1488
               dropReq.name, pStream->uid, tstrerror(terrno));
1489
        TAOS_RETURN(code);
5✔
1490
      }
1491

1492
      if (pSma) {
142!
1493
        sdbRelease(pMnode->pSdb, pSma);
142✔
1494
      }
1495

1496
      pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
142✔
1497
    }
1498
  }
1499

1500
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
1,269!
UNCOV
1501
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1502
    tFreeMDropStreamReq(&dropReq);
×
UNCOV
1503
    return -1;
×
1504
  }
1505

1506
  // check if it is conflict with other trans in both sourceDb and targetDb.
1507
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_DROP_NAME, true);
1,269✔
1508
  if (code) {
1,269!
UNCOV
1509
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1510
    tFreeMDropStreamReq(&dropReq);
×
UNCOV
1511
    return code;
×
1512
  }
1513

1514
  STrans *pTrans = NULL;
1,269✔
1515
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, "drop stream", &pTrans);
1,269✔
1516
  if (pTrans == NULL || code) {
1,269!
1517
    mError("stream:%s uid:0x%" PRIx64 " failed to drop since %s", dropReq.name, pStream->uid, tstrerror(code));
×
UNCOV
1518
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1519
    tFreeMDropStreamReq(&dropReq);
×
UNCOV
1520
    TAOS_RETURN(code);
×
1521
  }
1522

1523
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_DROP_NAME, pStream->uid);
1,269✔
1524
  if (code) {
1,269!
1525
    mError("failed to register drop stream trans, code:%s", tstrerror(code));
×
UNCOV
1526
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1527
    mndTransDrop(pTrans);
×
UNCOV
1528
    tFreeMDropStreamReq(&dropReq);
×
UNCOV
1529
    TAOS_RETURN(code);
×
1530
  }
1531

1532
  // drop all tasks
1533
  code = mndStreamSetDropAction(pMnode, pTrans, pStream);
1,269✔
1534
  if (code) {
1,269!
UNCOV
1535
    mError("stream:%s uid:0x%" PRIx64 " failed to drop task since %s", dropReq.name, pStream->uid, tstrerror(code));
×
UNCOV
1536
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1537
    mndTransDrop(pTrans);
×
UNCOV
1538
    tFreeMDropStreamReq(&dropReq);
×
1539
    TAOS_RETURN(code);
×
1540
  }
1541

1542
  // drop stream
1543
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_DROPPED);
1,269✔
1544
  if (code) {
1,269!
UNCOV
1545
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1546
    mndTransDrop(pTrans);
×
UNCOV
1547
    tFreeMDropStreamReq(&dropReq);
×
UNCOV
1548
    TAOS_RETURN(code);
×
1549
  }
1550

1551
  code = mndTransPrepare(pMnode, pTrans);
1,269✔
1552
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,269!
1553
    mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
1554
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1555
    mndTransDrop(pTrans);
×
UNCOV
1556
    tFreeMDropStreamReq(&dropReq);
×
UNCOV
1557
    TAOS_RETURN(code);
×
1558
  }
1559

1560
  // kill the related checkpoint trans
1561
  int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
1,269✔
1562
  if (transId != 0) {
1,269!
UNCOV
1563
    mDebug("drop active transId:%d due to stream:%s uid:0x%" PRIx64 " dropped", transId, pStream->name, pStream->uid);
×
UNCOV
1564
    mndKillTransImpl(pMnode, transId, pStream->sourceDb);
×
1565
  }
1566

1567
  mDebug("stream:%s uid:0x%" PRIx64 " transId:%d start to drop related task when dropping stream", dropReq.name,
1,269✔
1568
         pStream->uid, transId);
1569

1570
  removeStreamTasksInBuf(pStream, &execInfo);
1,269✔
1571

1572
  SName name = {0};
1,269✔
1573
  code = tNameFromString(&name, dropReq.name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
1,269✔
1574
  auditRecord(pReq, pMnode->clusterId, "dropStream", "", name.dbname, dropReq.sql, dropReq.sqlLen);
1,269✔
1575

1576
  sdbRelease(pMnode->pSdb, pStream);
1,269✔
1577
  mndTransDrop(pTrans);
1,269✔
1578
  tFreeMDropStreamReq(&dropReq);
1,269✔
1579

1580
  if (code == 0) {
1,269✔
1581
    return TSDB_CODE_ACTION_IN_PROGRESS;
1,259✔
1582
  } else {
1583
    TAOS_RETURN(code);
10✔
1584
  }
1585
}
1586

1587
int32_t mndDropStreamByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
1,885✔
1588
  SSdb   *pSdb = pMnode->pSdb;
1,885✔
1589
  void   *pIter = NULL;
1,885✔
1590
  int32_t code = 0;
1,885✔
1591

1592
  while (1) {
574✔
1593
    SStreamObj *pStream = NULL;
2,459✔
1594
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
2,459✔
1595
    if (pIter == NULL) break;
2,459✔
1596

1597
    if (pStream->sourceDbUid == pDb->uid || pStream->targetDbUid == pDb->uid) {
575✔
1598
      if (pStream->sourceDbUid != pStream->targetDbUid) {
83✔
1599
        sdbRelease(pSdb, pStream);
1✔
1600
        sdbCancelFetch(pSdb, pIter);
1✔
1601
        mError("db:%s, failed to drop stream:%s since sourceDbUid:%" PRId64 " not match with targetDbUid:%" PRId64,
1!
1602
               pDb->name, pStream->name, pStream->sourceDbUid, pStream->targetDbUid);
1603
        TAOS_RETURN(TSDB_CODE_MND_STREAM_MUST_BE_DELETED);
1✔
1604
      } else {
1605
        // kill the related checkpoint trans
1606
        int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
82✔
1607
        if (transId != 0) {
82!
UNCOV
1608
          mDebug("drop active related transId:%d due to stream:%s dropped", transId, pStream->name);
×
UNCOV
1609
          mndKillTransImpl(pMnode, transId, pStream->sourceDb);
×
1610
        }
1611

1612
        // drop the stream obj in execInfo
1613
        removeStreamTasksInBuf(pStream, &execInfo);
82✔
1614

1615
        code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_DROPPED);
82✔
1616
        if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
82!
UNCOV
1617
          sdbRelease(pSdb, pStream);
×
UNCOV
1618
          sdbCancelFetch(pSdb, pIter);
×
UNCOV
1619
          return code;
×
1620
        }
1621
      }
1622
    }
1623

1624
    sdbRelease(pSdb, pStream);
574✔
1625
  }
1626

1627
  return 0;
1,884✔
1628
}
1629

1630
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
11,255✔
1631
  SMnode     *pMnode = pReq->info.node;
11,255✔
1632
  SSdb       *pSdb = pMnode->pSdb;
11,255✔
1633
  int32_t     numOfRows = 0;
11,255✔
1634
  SStreamObj *pStream = NULL;
11,255✔
1635
  int32_t     code = 0;
11,255✔
1636

1637
  while (numOfRows < rows) {
44,456!
1638
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
44,456✔
1639
    if (pShow->pIter == NULL) break;
44,470✔
1640

1641
    code = setStreamAttrInResBlock(pStream, pBlock, numOfRows);
33,202✔
1642
    if (code == 0) {
33,133✔
1643
      numOfRows++;
33,131✔
1644
    }
1645
    sdbRelease(pSdb, pStream);
33,133✔
1646
  }
1647

1648
  pShow->numOfRows += numOfRows;
11,268✔
1649
  return numOfRows;
11,268✔
1650
}
1651

UNCOV
1652
static void mndCancelGetNextStream(SMnode *pMnode, void *pIter) {
×
UNCOV
1653
  SSdb *pSdb = pMnode->pSdb;
×
UNCOV
1654
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
UNCOV
1655
}
×
1656

1657
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
21,483✔
1658
  SMnode     *pMnode = pReq->info.node;
21,483✔
1659
  SSdb       *pSdb = pMnode->pSdb;
21,483✔
1660
  int32_t     numOfRows = 0;
21,483✔
1661
  SStreamObj *pStream = NULL;
21,483✔
1662
  int32_t     code = 0;
21,483✔
1663

1664
  streamMutexLock(&execInfo.lock);
21,483✔
1665
  mndInitStreamExecInfo(pMnode, &execInfo);
21,496✔
1666
  streamMutexUnlock(&execInfo.lock);
21,496✔
1667

1668
  while (numOfRows < rowsCapacity) {
86,654✔
1669
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
86,604✔
1670
    if (pShow->pIter == NULL) {
86,596✔
1671
      break;
21,446✔
1672
    }
1673

1674
    // lock
1675
    taosRLockLatch(&pStream->lock);
65,150✔
1676

1677
    int32_t count = mndGetNumOfStreamTasks(pStream);
65,155✔
1678
    if (numOfRows + count > rowsCapacity) {
65,059✔
1679
      code = blockDataEnsureCapacity(pBlock, numOfRows + count);
40✔
1680
      if (code) {
40!
UNCOV
1681
        mError("failed to prepare the result block buffer, quit return value");
×
UNCOV
1682
        taosRUnLockLatch(&pStream->lock);
×
UNCOV
1683
        sdbRelease(pSdb, pStream);
×
UNCOV
1684
        continue;
×
1685
      }
1686
    }
1687

1688
    int32_t precision = TSDB_TIME_PRECISION_MILLI;
65,059✔
1689
    SDbObj *pSourceDb = mndAcquireDb(pMnode, pStream->sourceDb);
65,059✔
1690
    if (pSourceDb != NULL) {
65,131!
1691
      precision = pSourceDb->cfg.precision;
65,140✔
1692
      mndReleaseDb(pMnode, pSourceDb);
65,140✔
1693
    }
1694

1695
    // add row for each task
1696
    SStreamTaskIter *pIter = NULL;
65,152✔
1697
    code = createStreamTaskIter(pStream, &pIter);
65,152✔
1698
    if (code) {
65,155!
UNCOV
1699
      taosRUnLockLatch(&pStream->lock);
×
UNCOV
1700
      sdbRelease(pSdb, pStream);
×
UNCOV
1701
      mError("failed to create task iter for stream:%s", pStream->name);
×
UNCOV
1702
      continue;
×
1703
    }
1704

1705
    while (streamTaskIterNextTask(pIter)) {
289,311✔
1706
      SStreamTask *pTask = NULL;
223,191✔
1707
      code = streamTaskIterGetCurrent(pIter, &pTask);
223,191✔
1708
      if (code) {
223,313!
UNCOV
1709
        destroyStreamTaskIter(pIter);
×
UNCOV
1710
        break;
×
1711
      }
1712

1713
      code = setTaskAttrInResBlock(pStream, pTask, pBlock, numOfRows, precision);
223,313✔
1714
      if (code == TSDB_CODE_SUCCESS) {
224,155!
1715
        numOfRows++;
224,167✔
1716
      }
1717
    }
1718

1719
    pBlock->info.rows = numOfRows;
64,217✔
1720

1721
    destroyStreamTaskIter(pIter);
64,217✔
1722
    taosRUnLockLatch(&pStream->lock);
65,112✔
1723

1724
    sdbRelease(pSdb, pStream);
65,145✔
1725
  }
1726

1727
  pShow->numOfRows += numOfRows;
21,496✔
1728
  return numOfRows;
21,496✔
1729
}
1730

UNCOV
1731
static void mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter) {
×
UNCOV
1732
  SSdb *pSdb = pMnode->pSdb;
×
UNCOV
1733
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
UNCOV
1734
}
×
1735

1736
static int32_t mndProcessPauseStreamReq(SRpcMsg *pReq) {
699✔
1737
  SMnode     *pMnode = pReq->info.node;
699✔
1738
  SStreamObj *pStream = NULL;
699✔
1739
  int32_t     code = 0;
699✔
1740

1741
  SMPauseStreamReq pauseReq = {0};
699✔
1742
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
699!
UNCOV
1743
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1744
  }
1745

1746
  code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
699✔
1747
  if (pStream == NULL || code != 0) {
699!
1748
    if (pauseReq.igNotExists) {
359✔
1749
      mInfo("stream:%s, not exist, not pause stream", pauseReq.name);
106!
1750
      return 0;
106✔
1751
    } else {
1752
      mError("stream:%s not exist, failed to pause stream", pauseReq.name);
253!
1753
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
253✔
1754
    }
1755
  }
1756

1757
  mInfo("stream:%s,%" PRId64 " start to pause stream", pauseReq.name, pStream->uid);
340!
1758

1759
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb)) != 0) {
340!
UNCOV
1760
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1761
    return code;
×
1762
  }
1763

1764
  // check if it is conflict with other trans in both sourceDb and targetDb.
1765
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_PAUSE_NAME, true);
340✔
1766
  if (code) {
340!
UNCOV
1767
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1768
    TAOS_RETURN(code);
×
1769
  }
1770

1771
  bool updated = mndStreamNodeIsUpdated(pMnode);
340✔
1772
  if (updated) {
340!
UNCOV
1773
    mError("tasks are not ready for pause, node update detected");
×
1774
    sdbRelease(pMnode->pSdb, pStream);
×
1775
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1776
  }
1777

1778
  {  // check for tasks, if tasks are not ready, not allowed to pause
1779
    bool found = false;
340✔
1780
    bool readyToPause = true;
340✔
1781
    streamMutexLock(&execInfo.lock);
340✔
1782

1783
    for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
4,941✔
1784
      STaskId *p = taosArrayGet(execInfo.pTaskList, i);
4,601✔
1785
      if (p == NULL) {
4,601!
UNCOV
1786
        continue;
×
1787
      }
1788

1789
      STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
4,601✔
1790
      if (pEntry == NULL) {
4,601!
UNCOV
1791
        continue;
×
1792
      }
1793

1794
      if (pEntry->id.streamId != pStream->uid) {
4,601✔
1795
        continue;
2,975✔
1796
      }
1797

1798
      if (pEntry->status == TASK_STATUS__UNINIT || pEntry->status == TASK_STATUS__CK) {
1,626!
1799
        mError("stream:%s uid:0x%" PRIx64 " vgId:%d task:0x%" PRIx64 " status:%s, not ready for pause", pStream->name,
202!
1800
               pStream->uid, pEntry->nodeId, pEntry->id.taskId, streamTaskGetStatusStr(pEntry->status));
1801
        readyToPause = false;
202✔
1802
      }
1803

1804
      found = true;
1,626✔
1805
    }
1806

1807
    streamMutexUnlock(&execInfo.lock);
340✔
1808
    if (!found) {
340!
UNCOV
1809
      mError("stream:%s task not report status yet, not ready for pause", pauseReq.name);
×
UNCOV
1810
      sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1811
      TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1812
    }
1813

1814
    if (!readyToPause) {
340✔
1815
      mError("stream:%s task not ready for pause yet", pauseReq.name);
47!
1816
      sdbRelease(pMnode->pSdb, pStream);
47✔
1817
      TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
47✔
1818
    }
1819
  }
1820

1821
  STrans *pTrans = NULL;
293✔
1822
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_PAUSE_NAME, "pause the stream", &pTrans);
293✔
1823
  if (pTrans == NULL || code) {
293!
1824
    mError("stream:%s failed to pause stream since %s", pauseReq.name, tstrerror(code));
×
1825
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1826
    return code;
×
1827
  }
1828

1829
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_PAUSE_NAME, pStream->uid);
293✔
1830
  if (code) {
293!
UNCOV
1831
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1832
    mndTransDrop(pTrans);
×
UNCOV
1833
    return code;
×
1834
  }
1835

1836
  // if nodeUpdate happened, not send pause trans
1837
  code = mndStreamSetPauseAction(pMnode, pTrans, pStream);
293✔
1838
  if (code) {
293!
1839
    mError("stream:%s, failed to pause task since %s", pauseReq.name, tstrerror(code));
×
1840
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1841
    mndTransDrop(pTrans);
×
UNCOV
1842
    return code;
×
1843
  }
1844

1845
  // pause stream
1846
  taosWLockLatch(&pStream->lock);
293✔
1847
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
293✔
1848
  if (code) {
293!
UNCOV
1849
    taosWUnLockLatch(&pStream->lock);
×
UNCOV
1850
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1851
    mndTransDrop(pTrans);
×
UNCOV
1852
    return code;
×
1853
  }
1854

1855
  taosWUnLockLatch(&pStream->lock);
293✔
1856

1857
  code = mndTransPrepare(pMnode, pTrans);
293✔
1858
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
293!
UNCOV
1859
    mError("trans:%d, failed to prepare pause stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
1860
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1861
    mndTransDrop(pTrans);
×
UNCOV
1862
    return code;
×
1863
  }
1864

1865
  sdbRelease(pMnode->pSdb, pStream);
293✔
1866
  mndTransDrop(pTrans);
293✔
1867

1868
  return TSDB_CODE_ACTION_IN_PROGRESS;
293✔
1869
}
1870

1871
static int32_t mndProcessResumeStreamReq(SRpcMsg *pReq) {
756✔
1872
  SMnode     *pMnode = pReq->info.node;
756✔
1873
  SStreamObj *pStream = NULL;
756✔
1874
  int32_t     code = 0;
756✔
1875

1876
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
756!
UNCOV
1877
    return code;
×
1878
  }
1879

1880
  SMResumeStreamReq resumeReq = {0};
756✔
1881
  if (tDeserializeSMResumeStreamReq(pReq->pCont, pReq->contLen, &resumeReq) < 0) {
756!
UNCOV
1882
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1883
  }
1884

1885
  code = mndAcquireStream(pMnode, resumeReq.name, &pStream);
756✔
1886
  if (pStream == NULL || code != 0) {
756!
1887
    if (resumeReq.igNotExists) {
212✔
1888
      mInfo("stream:%s not exist, not resume stream", resumeReq.name);
211!
1889
      sdbRelease(pMnode->pSdb, pStream);
211✔
1890
      return 0;
211✔
1891
    } else {
1892
      mError("stream:%s not exist, failed to resume stream", resumeReq.name);
1!
1893
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
1✔
1894
    }
1895
  }
1896

1897
  mInfo("stream:%s,%" PRId64 " start to resume stream from pause", resumeReq.name, pStream->uid);
544!
1898
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
544!
UNCOV
1899
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1900
    return -1;
×
1901
  }
1902

1903
  // check if it is conflict with other trans in both sourceDb and targetDb.
1904
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_RESUME_NAME, true);
544✔
1905
  if (code) {
544!
UNCOV
1906
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1907
    return code;
×
1908
  }
1909

1910
  STrans *pTrans = NULL;
544✔
1911
  code =
1912
      doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RESUME_NAME, "resume the stream", &pTrans);
544✔
1913
  if (pTrans == NULL || code) {
544!
1914
    mError("stream:%s, failed to resume stream since %s", resumeReq.name, tstrerror(code));
×
UNCOV
1915
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1916
    return code;
×
1917
  }
1918

1919
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_RESUME_NAME, pStream->uid);
544✔
1920
  if (code) {
544!
1921
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1922
    mndTransDrop(pTrans);
×
UNCOV
1923
    return code;
×
1924
  }
1925

1926
  // set the resume action
1927
  code = mndStreamSetResumeAction(pTrans, pMnode, pStream, resumeReq.igUntreated);
544✔
1928
  if (code) {
544!
1929
    mError("stream:%s, failed to drop task since %s", resumeReq.name, tstrerror(code));
×
1930
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1931
    mndTransDrop(pTrans);
×
UNCOV
1932
    return code;
×
1933
  }
1934

1935
  // resume stream
1936
  taosWLockLatch(&pStream->lock);
544✔
1937
  pStream->status = STREAM_STATUS__NORMAL;
544✔
1938
  if (mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY) < 0) {
544!
UNCOV
1939
    taosWUnLockLatch(&pStream->lock);
×
1940

UNCOV
1941
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1942
    mndTransDrop(pTrans);
×
1943
    return code;
×
1944
  }
1945

1946
  taosWUnLockLatch(&pStream->lock);
544✔
1947
  code = mndTransPrepare(pMnode, pTrans);
544✔
1948
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
544!
UNCOV
1949
    mError("trans:%d, failed to prepare pause stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
1950
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1951
    mndTransDrop(pTrans);
×
UNCOV
1952
    return code;
×
1953
  }
1954

1955
  sdbRelease(pMnode->pSdb, pStream);
544✔
1956
  mndTransDrop(pTrans);
544✔
1957

1958
  return TSDB_CODE_ACTION_IN_PROGRESS;
544✔
1959
}
1960

UNCOV
1961
static int32_t mndProcessResetStreamReq(SRpcMsg *pReq) {
×
UNCOV
1962
  SMnode     *pMnode = pReq->info.node;
×
1963
  SStreamObj *pStream = NULL;
×
1964
  int32_t     code = 0;
×
1965

1966
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
×
UNCOV
1967
    return code;
×
1968
  }
1969

UNCOV
1970
  SMResetStreamReq resetReq = {0};
×
UNCOV
1971
  if (tDeserializeSMResetStreamReq(pReq->pCont, pReq->contLen, &resetReq) < 0) {
×
UNCOV
1972
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1973
  }
1974

1975
  mDebug("recv reset stream req, stream:%s", resetReq.name);
×
1976

1977
  code = mndAcquireStream(pMnode, resetReq.name, &pStream);
×
1978
  if (pStream == NULL || code != 0) {
×
UNCOV
1979
    if (resetReq.igNotExists) {
×
1980
      mInfo("stream:%s, not exist, not pause stream", resetReq.name);
×
1981
      return 0;
×
1982
    } else {
UNCOV
1983
      mError("stream:%s not exist, failed to pause stream", resetReq.name);
×
1984
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1985
    }
1986
  }
1987

1988
  //todo(liao hao jun)
1989
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
1990
}
1991

1992
static int32_t mndProcessVgroupChange(SMnode *pMnode, SVgroupChangeInfo *pChangeInfo, bool includeAllNodes) {
7✔
1993
  SSdb       *pSdb = pMnode->pSdb;
7✔
1994
  SStreamObj *pStream = NULL;
7✔
1995
  void       *pIter = NULL;
7✔
1996
  STrans     *pTrans = NULL;
7✔
1997
  int32_t     code = 0;
7✔
1998

1999
  // conflict check for nodeUpdate trans, here we randomly chose one stream to add into the trans pool
2000
  while (1) {
2001
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
14✔
2002
    if (pIter == NULL) {
14✔
2003
      break;
7✔
2004
    }
2005

2006
    code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_TASK_UPDATE_NAME, false);
7✔
2007
    sdbRelease(pSdb, pStream);
7✔
2008

2009
    if (code) {
7!
UNCOV
2010
      mError("nodeUpdate conflict with other trans, current nodeUpdate ignored, code:%s", tstrerror(code));
×
UNCOV
2011
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
2012
      return code;
×
2013
    }
2014
  }
2015

2016
  while (1) {
2017
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
14✔
2018
    if (pIter == NULL) {
14✔
2019
      break;
7✔
2020
    }
2021

2022
    // here create only one trans
2023
    if (pTrans == NULL) {
7!
2024
      code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_TASK_UPDATE_NAME,
7✔
2025
                           "update task epsets", &pTrans);
2026
      if (pTrans == NULL || code) {
7!
UNCOV
2027
        sdbRelease(pSdb, pStream);
×
UNCOV
2028
        sdbCancelFetch(pSdb, pIter);
×
UNCOV
2029
        return terrno = code;
×
2030
      }
2031
    }
2032

2033
    if (!includeAllNodes) {
7!
2034
      void *p1 = taosHashGet(pChangeInfo->pDBMap, pStream->targetDb, strlen(pStream->targetDb));
7✔
2035
      void *p2 = taosHashGet(pChangeInfo->pDBMap, pStream->sourceDb, strlen(pStream->sourceDb));
7✔
2036
      if (p1 == NULL && p2 == NULL) {
7!
UNCOV
2037
        mDebug("stream:0x%" PRIx64 " %s not involved nodeUpdate, ignore", pStream->uid, pStream->name);
×
UNCOV
2038
        sdbRelease(pSdb, pStream);
×
UNCOV
2039
        continue;
×
2040
      }
2041
    }
2042

2043
    mDebug("stream:0x%" PRIx64 " %s involved node changed, create update trans, transId:%d", pStream->uid,
7✔
2044
           pStream->name, pTrans->id);
2045

2046
    // NOTE: for each stream, we register one trans entry for task update
2047
    code = mndStreamRegisterTrans(pTrans, MND_STREAM_TASK_UPDATE_NAME, pStream->uid);
7✔
2048
    if (code) {
7!
UNCOV
2049
      mError("failed to register trans, transId:%d, and continue", pTrans->id);
×
2050
    }
2051

2052
    code = mndStreamSetUpdateEpsetAction(pMnode, pStream, pChangeInfo, pTrans);
7✔
2053

2054
    // todo: not continue, drop all and retry again
2055
    if (code != TSDB_CODE_SUCCESS) {
7!
UNCOV
2056
      mError("stream:0x%" PRIx64 " build nodeUpdate trans failed, ignore and continue, code:%s", pStream->uid,
×
2057
             tstrerror(code));
UNCOV
2058
      sdbRelease(pSdb, pStream);
×
UNCOV
2059
      continue;
×
2060
    }
2061

2062
    code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
7✔
2063
    sdbRelease(pSdb, pStream);
7✔
2064

2065
    if (code != TSDB_CODE_SUCCESS) {
7!
UNCOV
2066
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
2067
      return code;
×
2068
    }
2069
  }
2070

2071
  // no need to build the trans to handle the vgroup update
2072
  if (pTrans == NULL) {
7!
2073
    return 0;
×
2074
  }
2075

2076
  code = mndTransPrepare(pMnode, pTrans);
7✔
2077
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
7!
UNCOV
2078
    mError("trans:%d, failed to prepare update stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
2079
    sdbRelease(pMnode->pSdb, pStream);
×
2080
    mndTransDrop(pTrans);
×
2081
    return code;
×
2082
  }
2083

2084
  sdbRelease(pMnode->pSdb, pStream);
7✔
2085
  mndTransDrop(pTrans);
7✔
2086
  return code;
7✔
2087
}
2088

2089
static int32_t refreshNodeListFromExistedStreams(SMnode *pMnode, SArray *pNodeList) {
792✔
2090
  SSdb       *pSdb = pMnode->pSdb;
792✔
2091
  SStreamObj *pStream = NULL;
792✔
2092
  void       *pIter = NULL;
792✔
2093
  int32_t     code = 0;
792✔
2094

2095
  mDebug("start to refresh node list by existed streams");
792✔
2096

2097
  SHashObj *pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
792✔
2098
  if (pHash == NULL) {
792!
UNCOV
2099
    return terrno;
×
2100
  }
2101

2102
  while (1) {
7✔
2103
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
799✔
2104
    if (pIter == NULL) {
799✔
2105
      break;
792✔
2106
    }
2107

2108
    taosWLockLatch(&pStream->lock);
7✔
2109

2110
    SStreamTaskIter *pTaskIter = NULL;
7✔
2111
    code = createStreamTaskIter(pStream, &pTaskIter);
7✔
2112
    if (code) {
7!
2113
      taosWUnLockLatch(&pStream->lock);
×
UNCOV
2114
      sdbRelease(pSdb, pStream);
×
UNCOV
2115
      mError("failed to create task iter for stream:%s", pStream->name);
×
UNCOV
2116
      continue;
×
2117
    }
2118

2119
    while (streamTaskIterNextTask(pTaskIter)) {
50✔
2120
      SStreamTask *pTask = NULL;
43✔
2121
      code = streamTaskIterGetCurrent(pTaskIter, &pTask);
43✔
2122
      if (code) {
43!
UNCOV
2123
        break;
×
2124
      }
2125

2126
      SNodeEntry entry = {.hbTimestamp = -1, .nodeId = pTask->info.nodeId, .lastHbMsgId = -1};
43✔
2127
      epsetAssign(&entry.epset, &pTask->info.epSet);
43✔
2128
      int32_t ret = taosHashPut(pHash, &entry.nodeId, sizeof(entry.nodeId), &entry, sizeof(entry));
43✔
2129
      if (ret != 0 && ret != TSDB_CODE_DUP_KEY) {
43!
2130
        mError("failed to put entry into hash map, nodeId:%d, code:%s", entry.nodeId, tstrerror(code));
×
2131
      }
2132
    }
2133

2134
    destroyStreamTaskIter(pTaskIter);
7✔
2135
    taosWUnLockLatch(&pStream->lock);
7✔
2136

2137
    sdbRelease(pSdb, pStream);
7✔
2138
  }
2139

2140
  taosArrayClear(pNodeList);
792✔
2141

2142
  // convert to list
2143
  pIter = NULL;
792✔
2144
  while ((pIter = taosHashIterate(pHash, pIter)) != NULL) {
815✔
2145
    SNodeEntry *pEntry = (SNodeEntry *)pIter;
23✔
2146

2147
    void *p = taosArrayPush(pNodeList, pEntry);
23✔
2148
    if (p == NULL) {
23!
UNCOV
2149
      mError("failed to put entry into node list, nodeId:%d, code: out of memory", pEntry->nodeId);
×
UNCOV
2150
      if (code == 0) {
×
UNCOV
2151
        code = terrno;
×
2152
      }
UNCOV
2153
      continue;
×
2154
    }
2155

2156
    char    buf[256] = {0};
23✔
2157
    int32_t ret = epsetToStr(&pEntry->epset, buf, tListLen(buf));  // ignore this error since it is only for log file
23✔
2158
    if (ret != 0) {                                                // print error and continue
23!
UNCOV
2159
      mError("failed to convert epset to str, code:%s", tstrerror(ret));
×
2160
    }
2161

2162
    mDebug("extract nodeInfo from stream obj, nodeId:%d, %s", pEntry->nodeId, buf);
23✔
2163
  }
2164

2165
  taosHashCleanup(pHash);
792✔
2166

2167
  mDebug("numOfNodes:%d for stream after extract nodeInfo from stream", (int32_t)taosArrayGetSize(pNodeList));
792✔
2168
  return code;
792✔
2169
}
2170

UNCOV
2171
static void addAllDbsIntoHashmap(SHashObj *pDBMap, SSdb *pSdb) {
×
UNCOV
2172
  void   *pIter = NULL;
×
2173
  int32_t code = 0;
×
UNCOV
2174
  while (1) {
×
UNCOV
2175
    SVgObj *pVgroup = NULL;
×
UNCOV
2176
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
×
UNCOV
2177
    if (pIter == NULL) {
×
UNCOV
2178
      break;
×
2179
    }
2180

UNCOV
2181
    code = taosHashPut(pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
×
UNCOV
2182
    sdbRelease(pSdb, pVgroup);
×
2183

UNCOV
2184
    if (code == 0) {
×
2185
      int32_t size = taosHashGetSize(pDBMap);
×
2186
      mDebug("add Db:%s into Dbs list (total:%d) for kill checkpoint trans", pVgroup->dbName, size);
×
2187
    }
2188
  }
2189
}
×
2190

2191
// this function runs by only one thread, so it is not multi-thread safe
2192
static int32_t mndProcessNodeCheckReq(SRpcMsg *pMsg) {
1,518✔
2193
  int32_t code = 0;
1,518✔
2194
  bool    allReady = true;
1,518✔
2195
  SArray *pNodeSnapshot = NULL;
1,518✔
2196
  SMnode *pMnode = pMsg->info.node;
1,518✔
2197
  int64_t ts = taosGetTimestampSec();
1,518✔
2198
  bool    updateAllVgroups = false;
1,518✔
2199

2200
  int32_t old = atomic_val_compare_exchange_32(&mndNodeCheckSentinel, 0, 1);
1,518✔
2201
  if (old != 0) {
1,518!
UNCOV
2202
    mDebug("still in checking node change");
×
2203
    return 0;
×
2204
  }
2205

2206
  mDebug("start to do node changing check");
1,518✔
2207

2208
  streamMutexLock(&execInfo.lock);
1,518✔
2209
  int32_t numOfNodes = extractStreamNodeList(pMnode);
1,518✔
2210
  streamMutexUnlock(&execInfo.lock);
1,518✔
2211

2212
  if (numOfNodes == 0) {
1,518!
UNCOV
2213
    mDebug("end to do stream task(s) node change checking, no stream tasks exist, do nothing");
×
UNCOV
2214
    execInfo.ts = ts;
×
UNCOV
2215
    atomic_store_32(&mndNodeCheckSentinel, 0);
×
2216
    return 0;
×
2217
  }
2218

2219
  code = mndTakeVgroupSnapshot(pMnode, &allReady, &pNodeSnapshot);
1,518✔
2220
  if (code) {
1,518!
UNCOV
2221
    mError("failed to take the vgroup snapshot, ignore it and continue");
×
2222
  }
2223

2224
  if (!allReady) {
1,518✔
2225
    taosArrayDestroy(pNodeSnapshot);
29✔
2226
    atomic_store_32(&mndNodeCheckSentinel, 0);
29✔
2227
    mWarn("not all vnodes are ready, ignore the exec nodeUpdate check");
29!
2228
    return 0;
29✔
2229
  }
2230

2231
  streamMutexLock(&execInfo.lock);
1,489✔
2232

2233
  code = removeExpiredNodeEntryAndTaskInBuf(pNodeSnapshot);
1,489✔
2234
  if (code) {
1,489!
2235
    goto _end;
×
2236
  }
2237

2238
  SVgroupChangeInfo changeInfo = {0};
1,489✔
2239
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, pNodeSnapshot, &changeInfo);
1,489✔
2240
  if (code) {
1,489!
UNCOV
2241
    goto _end;
×
2242
  }
2243

2244
  {
2245
    if (execInfo.role == NODE_ROLE_LEADER && execInfo.switchFromFollower) {
1,489!
UNCOV
2246
      mInfo("rollback all stream due to mnode leader/follower switch by using nodeUpdate trans");
×
UNCOV
2247
      updateAllVgroups = true;
×
UNCOV
2248
      execInfo.switchFromFollower = false;  // reset the flag
×
2249
      addAllDbsIntoHashmap(changeInfo.pDBMap, pMnode->pSdb);
×
2250
    }
2251
  }
2252

2253
  if (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0 || updateAllVgroups) {
1,489!
2254
    // kill current active checkpoint transaction, since the transaction is vnode wide.
2255
    killAllCheckpointTrans(pMnode, &changeInfo);
7✔
2256
    code = mndProcessVgroupChange(pMnode, &changeInfo, updateAllVgroups);
7✔
2257

2258
    // keep the new vnode snapshot if success
2259
    if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {
7!
2260
      code = refreshNodeListFromExistedStreams(pMnode, execInfo.pNodeList);
7✔
2261
      if (code) {
7!
2262
        mError("failed to extract node list from stream, code:%s", tstrerror(code));
×
2263
        goto _end;
×
2264
      }
2265

2266
      execInfo.ts = ts;
7✔
2267
      mDebug("create trans successfully, update cached node list, numOfNodes:%d",
7✔
2268
             (int)taosArrayGetSize(execInfo.pNodeList));
2269
    } else {
UNCOV
2270
      mError("unexpected code during create nodeUpdate trans, code:%s", tstrerror(code));
×
2271
    }
2272
  } else {
2273
    mDebug("no update found in nodeList");
1,482✔
2274
  }
2275

2276
  mndDestroyVgroupChangeInfo(&changeInfo);
1,489✔
2277

2278
_end:
1,489✔
2279
  streamMutexUnlock(&execInfo.lock);
1,489✔
2280
  taosArrayDestroy(pNodeSnapshot);
1,489✔
2281

2282
  mDebug("end to do stream task node change checking");
1,489✔
2283
  atomic_store_32(&mndNodeCheckSentinel, 0);
1,489✔
2284
  return 0;
1,489✔
2285
}
2286

2287
static int32_t mndProcessNodeCheck(SRpcMsg *pReq) {
2,881✔
2288
  SMnode *pMnode = pReq->info.node;
2,881✔
2289
  SSdb   *pSdb = pMnode->pSdb;
2,881✔
2290
  if (sdbGetSize(pSdb, SDB_STREAM) <= 0) {
2,881✔
2291
    return 0;
1,363✔
2292
  }
2293

2294
  int32_t               size = sizeof(SMStreamNodeCheckMsg);
1,518✔
2295
  SMStreamNodeCheckMsg *pMsg = rpcMallocCont(size);
1,518✔
2296
  if (pMsg == NULL) {
1,518!
UNCOV
2297
    return terrno;
×
2298
  }
2299

2300
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_NODECHANGE_CHECK, .pCont = pMsg, .contLen = size};
1,518✔
2301
  return tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
1,518✔
2302
}
2303

2304
void saveTaskAndNodeInfoIntoBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
2,003✔
2305
  SStreamTaskIter *pIter = NULL;
2,003✔
2306
  int32_t          code = createStreamTaskIter(pStream, &pIter);
2,003✔
2307
  if (code) {
2,003!
UNCOV
2308
    mError("failed to create task iter for stream:%s", pStream->name);
×
UNCOV
2309
    return;
×
2310
  }
2311

2312
  while (streamTaskIterNextTask(pIter)) {
12,062✔
2313
    SStreamTask *pTask = NULL;
10,059✔
2314
    code = streamTaskIterGetCurrent(pIter, &pTask);
10,059✔
2315
    if (code) {
10,059!
UNCOV
2316
      break;
×
2317
    }
2318

2319
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
10,059✔
2320
    void   *p = taosHashGet(pExecNode->pTaskMap, &id, sizeof(id));
10,059✔
2321
    if (p == NULL) {
10,059✔
2322
      STaskStatusEntry entry = {0};
9,254✔
2323
      streamTaskStatusInit(&entry, pTask);
9,254✔
2324

2325
      code = taosHashPut(pExecNode->pTaskMap, &id, sizeof(id), &entry, sizeof(entry));
9,254✔
2326
      if (code == 0) {
9,254!
2327
        void   *px = taosArrayPush(pExecNode->pTaskList, &id);
9,254✔
2328
        int32_t num = (int32_t)taosArrayGetSize(pExecNode->pTaskList);
9,254✔
2329
        if (px) {
9,254!
2330
          mInfo("s-task:0x%x add into task buffer, total:%d", (int32_t)entry.id.taskId, num);
9,254!
2331
        } else {
UNCOV
2332
          mError("s-task:0x%x failed to add into task buffer, total:%d", (int32_t)entry.id.taskId, num);
×
2333
        }
2334
      } else {
UNCOV
2335
        mError("s-task:0x%x failed to add into task map, since out of memory", (int32_t)entry.id.taskId);
×
2336
      }
2337

2338
      // add the new vgroups if not added yet
2339
      bool exist = false;
9,254✔
2340
      for (int32_t j = 0; j < taosArrayGetSize(pExecNode->pNodeList); ++j) {
48,891✔
2341
        SNodeEntry *pEntry = taosArrayGet(pExecNode->pNodeList, j);
47,027✔
2342
        if ((pEntry != NULL) && (pEntry->nodeId == pTask->info.nodeId)) {
47,027!
2343
          exist = true;
7,390✔
2344
          break;
7,390✔
2345
        }
2346
      }
2347

2348
      if (!exist) {
9,254✔
2349
        SNodeEntry nodeEntry = {.hbTimestamp = -1, .nodeId = pTask->info.nodeId, .lastHbMsgId = -1};
1,864✔
2350
        epsetAssign(&nodeEntry.epset, &pTask->info.epSet);
1,864✔
2351

2352
        void *px = taosArrayPush(pExecNode->pNodeList, &nodeEntry);
1,864✔
2353
        if (px) {
1,864!
2354
          mInfo("vgId:%d added into nodeList, total:%d", nodeEntry.nodeId, (int)taosArrayGetSize(pExecNode->pNodeList));
1,864!
2355
        } else {
UNCOV
2356
          mError("vgId:%d failed to add into nodeList, total:%d", nodeEntry.nodeId,
×
2357
                 (int)taosArrayGetSize(pExecNode->pNodeList))
2358
        }
2359
      }
2360
    }
2361
  }
2362

2363
  destroyStreamTaskIter(pIter);
2,003✔
2364
}
2365

2366
static void doAddTaskId(SArray *pList, int32_t taskId, int64_t uid, int32_t numOfTotal) {
4,401✔
2367
  int32_t num = taosArrayGetSize(pList);
4,401✔
2368
  for (int32_t i = 0; i < num; ++i) {
16,341✔
2369
    int32_t *pId = taosArrayGet(pList, i);
11,942✔
2370
    if (pId == NULL) {
11,942!
UNCOV
2371
      continue;
×
2372
    }
2373

2374
    if (taskId == *pId) {
11,942✔
2375
      return;
2✔
2376
    }
2377
  }
2378

2379
  int32_t numOfTasks = taosArrayGetSize(pList);
4,399✔
2380
  void   *p = taosArrayPush(pList, &taskId);
4,399✔
2381
  if (p) {
4,399!
2382
    mDebug("stream:0x%" PRIx64 " receive %d reqs for checkpoint, remain:%d", uid, numOfTasks, numOfTotal - numOfTasks);
4,399✔
2383
  } else {
UNCOV
2384
    mError("stream:0x%" PRIx64 " receive %d reqs for checkpoint, failed to added into task list, since out of memory",
×
2385
           uid, numOfTasks);
2386
  }
2387
}
2388

2389
int32_t mndProcessStreamReqCheckpoint(SRpcMsg *pReq) {
4,401✔
2390
  SMnode                  *pMnode = pReq->info.node;
4,401✔
2391
  SStreamTaskCheckpointReq req = {0};
4,401✔
2392

2393
  SDecoder decoder = {0};
4,401✔
2394
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
4,401✔
2395

2396
  if (tDecodeStreamTaskCheckpointReq(&decoder, &req)) {
4,401!
UNCOV
2397
    tDecoderClear(&decoder);
×
2398
    mError("invalid task checkpoint req msg received");
×
UNCOV
2399
    return TSDB_CODE_INVALID_MSG;
×
2400
  }
2401
  tDecoderClear(&decoder);
4,401✔
2402

2403
  mDebug("receive stream task checkpoint req msg, vgId:%d, s-task:0x%x", req.nodeId, req.taskId);
4,401✔
2404

2405
  // register to the stream task done map, if all tasks has sent this kinds of message, start the checkpoint trans.
2406
  streamMutexLock(&execInfo.lock);
4,401✔
2407

2408
  SStreamObj *pStream = NULL;
4,401✔
2409
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
4,401✔
2410
  if (pStream == NULL || code != 0) {
4,401!
2411
    mWarn("failed to find the stream:0x%" PRIx64 ", not handle the checkpoint req, try to acquire in buf",
×
2412
          req.streamId);
2413

2414
    // not in meta-store yet, try to acquire the task in exec buffer
2415
    // the checkpoint req arrives too soon before the completion of the create stream trans.
UNCOV
2416
    STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
×
UNCOV
2417
    void   *p = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
UNCOV
2418
    if (p == NULL) {
×
UNCOV
2419
      mError("failed to find the stream:0x%" PRIx64 " in buf, not handle the checkpoint req", req.streamId);
×
UNCOV
2420
      streamMutexUnlock(&execInfo.lock);
×
UNCOV
2421
      return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
2422
    } else {
UNCOV
2423
      mDebug("s-task:0x%" PRIx64 "-0x%x in buf not in mnode/meta, create stream trans may not complete yet",
×
2424
             req.streamId, req.taskId);
2425
    }
2426
  }
2427

2428
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
4,401!
2429

2430
  SArray **pReqTaskList = (SArray **)taosHashGet(execInfo.pTransferStateStreams, &req.streamId, sizeof(req.streamId));
4,401✔
2431
  if (pReqTaskList == NULL) {
4,401✔
2432
    SArray *pList = taosArrayInit(4, sizeof(int32_t));
773✔
2433
    doAddTaskId(pList, req.taskId, req.streamId, numOfTasks);
773✔
2434
    code = taosHashPut(execInfo.pTransferStateStreams, &req.streamId, sizeof(int64_t), &pList, sizeof(void *));
773✔
2435
    if (code) {
773!
UNCOV
2436
      mError("failed to put into transfer state stream map, code: out of memory");
×
2437
    }
2438
    pReqTaskList = (SArray **)taosHashGet(execInfo.pTransferStateStreams, &req.streamId, sizeof(req.streamId));
773✔
2439
  } else {
2440
    doAddTaskId(*pReqTaskList, req.taskId, req.streamId, numOfTasks);
3,628✔
2441
  }
2442

2443
  int32_t total = taosArrayGetSize(*pReqTaskList);
4,401✔
2444
  if (total == numOfTasks) {  // all tasks have sent the reqs
4,401✔
2445
    int64_t checkpointId = mndStreamGenChkptId(pMnode, false);
730✔
2446
    mInfo("stream:0x%" PRIx64 " all tasks req checkpoint, start checkpointId:%" PRId64, req.streamId, checkpointId);
730!
2447

2448
    if (pStream != NULL) {  // TODO:handle error
730!
2449
      code = mndProcessStreamCheckpointTrans(pMnode, pStream, checkpointId, 0, false);
730✔
2450
      if (code) {
730!
2451
        mError("failed to create checkpoint trans, code:%s", tstrerror(code));
730!
2452
      }
2453
    } else {
2454
      // todo: wait for the create stream trans completed, and launch the checkpoint trans
2455
      // SStreamObj *pStream = mndGetStreamObj(pMnode, req.streamId);
2456
      // sleep(500ms)
2457
    }
2458

2459
    // remove this entry
2460
    (void) taosHashRemove(execInfo.pTransferStateStreams, &req.streamId, sizeof(int64_t));
730✔
2461

2462
    int32_t numOfStreams = taosHashGetSize(execInfo.pTransferStateStreams);
730✔
2463
    mDebug("stream:0x%" PRIx64 " removed, remain streams:%d fill-history not completed", req.streamId, numOfStreams);
730✔
2464
  }
2465

2466
  if (pStream != NULL) {
4,401!
2467
    mndReleaseStream(pMnode, pStream);
4,401✔
2468
  }
2469

2470
  streamMutexUnlock(&execInfo.lock);
4,401✔
2471

2472
  {
2473
    SRpcMsg rsp = {.code = 0, .info = pReq->info, .contLen = sizeof(SMStreamReqCheckpointRsp)};
4,401✔
2474
    rsp.pCont = rpcMallocCont(rsp.contLen);
4,401✔
2475
    if (rsp.pCont == NULL) {
4,401!
UNCOV
2476
      return terrno;
×
2477
    }
2478

2479
    SMsgHead *pHead = rsp.pCont;
4,401✔
2480
    pHead->vgId = htonl(req.nodeId);
4,401✔
2481

2482
    tmsgSendRsp(&rsp);
4,401✔
2483
    pReq->info.handle = NULL;  // disable auto rsp
4,401✔
2484
  }
2485

2486
  return 0;
4,401✔
2487
}
2488

2489
// valid the info according to the HbMsg
2490
static bool validateChkptReport(const SCheckpointReport *pReport, int64_t reportChkptId) {
6,332✔
2491
  STaskId           id = {.streamId = pReport->streamId, .taskId = pReport->taskId};
6,332✔
2492
  STaskStatusEntry *pTaskEntry = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
6,332✔
2493
  if (pTaskEntry == NULL) {
6,332✔
2494
    mError("invalid checkpoint-report msg from task:0x%x, discard", pReport->taskId);
16!
2495
    return false;
16✔
2496
  }
2497

2498
  if (pTaskEntry->checkpointInfo.latestId >= pReport->checkpointId) {
6,316!
UNCOV
2499
    mError("s-task:0x%x invalid checkpoint-report msg, checkpointId:%" PRId64 " saved checkpointId:%" PRId64 " discard",
×
2500
           pReport->taskId, pReport->checkpointId, pTaskEntry->checkpointInfo.activeId);
UNCOV
2501
    return false;
×
2502
  }
2503

2504
  // now the task in checkpoint procedure
2505
  if ((pTaskEntry->checkpointInfo.activeId != 0) && (pTaskEntry->checkpointInfo.activeId > pReport->checkpointId)) {
6,316!
UNCOV
2506
    mError("s-task:0x%x invalid checkpoint-report msg, checkpointId:%" PRId64 " active checkpointId:%" PRId64
×
2507
           " discard",
2508
           pReport->taskId, pReport->checkpointId, pTaskEntry->checkpointInfo.activeId);
UNCOV
2509
    return false;
×
2510
  }
2511

2512
  if (reportChkptId >= pReport->checkpointId) {
6,316!
2513
    mError("s-task:0x%x expired checkpoint-report msg, checkpointId:%" PRId64 " already update checkpointId:%" PRId64
×
2514
           " discard",
2515
           pReport->taskId, pReport->checkpointId, reportChkptId);
UNCOV
2516
    return false;
×
2517
  }
2518

2519
  return true;
6,316✔
2520
}
2521

2522
static void doAddReportStreamTask(SArray *pList, int64_t reportedChkptId, const SCheckpointReport *pReport) {
6,332✔
2523
  bool valid = validateChkptReport(pReport, reportedChkptId);
6,332✔
2524
  if (!valid) {
6,332✔
2525
    return;
16✔
2526
  }
2527

2528
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
21,438✔
2529
    STaskChkptInfo *p = taosArrayGet(pList, i);
15,122✔
2530
    if (p == NULL) {
15,122!
UNCOV
2531
      continue;
×
2532
    }
2533

2534
    if (p->taskId == pReport->taskId) {
15,122!
UNCOV
2535
      if (p->checkpointId > pReport->checkpointId) {
×
UNCOV
2536
        mError("s-task:0x%x invalid checkpoint-report msg, existed:%" PRId64 " req checkpointId:%" PRId64 ", discard",
×
2537
               pReport->taskId, p->checkpointId, pReport->checkpointId);
UNCOV
2538
      } else if (p->checkpointId < pReport->checkpointId) {  // expired checkpoint-report msg, update it
×
UNCOV
2539
        mInfo("s-task:0x%x expired checkpoint-report info in checkpoint-report list update from %" PRId64 "->%" PRId64,
×
2540
               pReport->taskId, p->checkpointId, pReport->checkpointId);
2541

2542
        // update the checkpoint report info
UNCOV
2543
        p->checkpointId = pReport->checkpointId;
×
UNCOV
2544
        p->ts = pReport->checkpointTs;
×
2545
        p->version = pReport->checkpointVer;
×
UNCOV
2546
        p->transId = pReport->transId;
×
UNCOV
2547
        p->dropHTask = pReport->dropHTask;
×
2548
      } else {
2549
        mWarn("taskId:0x%x already in checkpoint-report list", pReport->taskId);
×
2550
      }
UNCOV
2551
      return;
×
2552
    }
2553
  }
2554

2555
  STaskChkptInfo info = {
6,316✔
2556
      .streamId = pReport->streamId,
6,316✔
2557
      .taskId = pReport->taskId,
6,316✔
2558
      .transId = pReport->transId,
6,316✔
2559
      .dropHTask = pReport->dropHTask,
6,316✔
2560
      .version = pReport->checkpointVer,
6,316✔
2561
      .ts = pReport->checkpointTs,
6,316✔
2562
      .checkpointId = pReport->checkpointId,
6,316✔
2563
      .nodeId = pReport->nodeId,
6,316✔
2564
  };
2565

2566
  void *p = taosArrayPush(pList, &info);
6,316✔
2567
  if (p == NULL) {
6,316!
UNCOV
2568
    mError("failed to put into task list, taskId:0x%x", pReport->taskId);
×
2569
  } else {
2570
    int32_t size = taosArrayGetSize(pList);
6,316✔
2571
    mDebug("stream:0x%" PRIx64 " taskId:0x%x checkpoint-report recv, %d tasks has send checkpoint-report",
6,316✔
2572
           pReport->streamId, pReport->taskId, size);
2573
  }
2574
}
2575

2576
int32_t mndProcessCheckpointReport(SRpcMsg *pReq) {
6,332✔
2577
  SMnode           *pMnode = pReq->info.node;
6,332✔
2578
  SCheckpointReport req = {0};
6,332✔
2579

2580
  SDecoder decoder = {0};
6,332✔
2581
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
6,332✔
2582

2583
  if (tDecodeStreamTaskChkptReport(&decoder, &req)) {
6,332!
UNCOV
2584
    tDecoderClear(&decoder);
×
UNCOV
2585
    mError("invalid task checkpoint-report msg received");
×
UNCOV
2586
    return TSDB_CODE_INVALID_MSG;
×
2587
  }
2588
  tDecoderClear(&decoder);
6,332✔
2589

2590
  streamMutexLock(&execInfo.lock);
6,332✔
2591
  mndInitStreamExecInfo(pMnode, &execInfo);
6,332✔
2592
  streamMutexUnlock(&execInfo.lock);
6,332✔
2593

2594
  mDebug("receive stream task checkpoint-report msg, vgId:%d, s-task:0x%x, checkpointId:%" PRId64
6,332✔
2595
         " checkpointVer:%" PRId64 " transId:%d",
2596
         req.nodeId, req.taskId, req.checkpointId, req.checkpointVer, req.transId);
2597

2598
  // register to the stream task done map, if all tasks has sent these kinds of message, start the checkpoint trans.
2599
  streamMutexLock(&execInfo.lock);
6,332✔
2600

2601
  SStreamObj *pStream = NULL;
6,332✔
2602
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
6,332✔
2603
  if (pStream == NULL || code != 0) {
6,332!
UNCOV
2604
    mWarn("failed to find the stream:0x%" PRIx64 ", not handle checkpoint-report, try to acquire in buf", req.streamId);
×
2605

2606
    // not in meta-store yet, try to acquire the task in exec buffer
2607
    // the checkpoint req arrives too soon before the completion of the creation of stream trans.
UNCOV
2608
    STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
×
UNCOV
2609
    void   *p = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
UNCOV
2610
    if (p == NULL) {
×
UNCOV
2611
      mError("failed to find the stream:0x%" PRIx64 " in buf, not handle the checkpoint-report", req.streamId);
×
UNCOV
2612
      streamMutexUnlock(&execInfo.lock);
×
UNCOV
2613
      return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
2614
    } else {
UNCOV
2615
      mDebug("s-task:0x%" PRIx64 "-0x%x in buf not in mnode/meta, create stream trans may not complete yet",
×
2616
             req.streamId, req.taskId);
2617
    }
2618
  }
2619

2620
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
6,332!
2621

2622
  SChkptReportInfo *pInfo =
2623
      (SChkptReportInfo *)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId));
6,332✔
2624
  if (pInfo == NULL) {
6,332✔
2625
    SChkptReportInfo info = {.pTaskList = taosArrayInit(4, sizeof(STaskChkptInfo)), .streamId = req.streamId};
732✔
2626
    if (info.pTaskList != NULL) {
732!
2627
      doAddReportStreamTask(info.pTaskList, info.reportChkpt, &req);
732✔
2628
      code = taosHashPut(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId), &info, sizeof(info));
732✔
2629
      if (code) {
732!
UNCOV
2630
        mError("stream:0x%" PRIx64 " failed to put into checkpoint stream", req.streamId);
×
2631
      }
2632

2633
      pInfo = (SChkptReportInfo *)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId));
732✔
2634
    }
2635
  } else {
2636
    doAddReportStreamTask(pInfo->pTaskList, pInfo->reportChkpt, &req);
5,600✔
2637
  }
2638

2639
  int32_t total = taosArrayGetSize(pInfo->pTaskList);
6,332✔
2640
  if (total == numOfTasks) {  // all tasks have sent the reqs
6,332✔
2641
    mInfo("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, checkpoint meta-info for checkpointId:%" PRId64
1,333!
2642
          " will be issued soon",
2643
          req.streamId, pStream->name, total, req.checkpointId);
2644
  }
2645

2646
  if (pStream != NULL) {
6,332!
2647
    mndReleaseStream(pMnode, pStream);
6,332✔
2648
  }
2649

2650
  streamMutexUnlock(&execInfo.lock);
6,332✔
2651

2652
  doSendQuickRsp(&pReq->info, sizeof(SMStreamUpdateChkptRsp), req.nodeId, TSDB_CODE_SUCCESS);
6,332✔
2653
  return code;
6,332✔
2654
}
2655

2656
static int64_t getConsensusId(int64_t streamId, int32_t numOfTasks, int32_t *pExistedTasks, bool *pAllSame) {
207✔
2657
  int32_t num = 0;
207✔
2658
  int64_t chkId = INT64_MAX;
207✔
2659
  *pExistedTasks = 0;
207✔
2660
  *pAllSame = true;
207✔
2661

2662
  for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
6,634✔
2663
    STaskId *p = taosArrayGet(execInfo.pTaskList, i);
6,427✔
2664
    if (p == NULL) {
6,427!
UNCOV
2665
      continue;
×
2666
    }
2667

2668
    if (p->streamId != streamId) {
6,427✔
2669
      continue;
5,060✔
2670
    }
2671

2672
    num += 1;
1,367✔
2673
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
1,367✔
2674
    if (chkId > pe->checkpointInfo.latestId) {
1,367✔
2675
      if (chkId != INT64_MAX) {
211✔
2676
        *pAllSame = false;
4✔
2677
      }
2678
      chkId = pe->checkpointInfo.latestId;
211✔
2679
    }
2680
  }
2681

2682
  *pExistedTasks = num;
207✔
2683
  if (num < numOfTasks) {  // not all task send info to mnode through hbMsg, no valid checkpoint Id
207!
UNCOV
2684
    return -1;
×
2685
  }
2686

2687
  return chkId;
207✔
2688
}
2689

2690
static void doSendQuickRsp(SRpcHandleInfo *pInfo, int32_t msgSize, int32_t vgId, int32_t code) {
6,332✔
2691
  SRpcMsg rsp = {.code = code, .info = *pInfo, .contLen = msgSize};
6,332✔
2692
  rsp.pCont = rpcMallocCont(rsp.contLen);
6,332✔
2693
  if (rsp.pCont != NULL) {
6,332!
2694
    SMsgHead *pHead = rsp.pCont;
6,332✔
2695
    pHead->vgId = htonl(vgId);
6,332✔
2696

2697
    tmsgSendRsp(&rsp);
6,332✔
2698
    pInfo->handle = NULL;  // disable auto rsp
6,332✔
2699
  }
2700
}
6,332✔
2701

2702
static int32_t doCleanReqList(SArray* pList, SCheckpointConsensusInfo* pInfo) {
44✔
2703
  int32_t alreadySend = taosArrayGetSize(pList);
44✔
2704

2705
  for (int32_t i = 0; i < alreadySend; ++i) {
247✔
2706
    int32_t *taskId = taosArrayGet(pList, i);
203✔
2707
    if (taskId == NULL) {
203!
UNCOV
2708
      continue;
×
2709
    }
2710

2711
    for (int32_t k = 0; k < taosArrayGetSize(pInfo->pTaskList); ++k) {
203!
2712
      SCheckpointConsensusEntry *pe = taosArrayGet(pInfo->pTaskList, k);
203✔
2713
      if ((pe != NULL) && (pe->req.taskId == *taskId)) {
203!
2714
        taosArrayRemove(pInfo->pTaskList, k);
203✔
2715
        break;
203✔
2716
      }
2717
    }
2718
  }
2719

2720
  return alreadySend;
44✔
2721
}
2722

2723
int32_t mndProcessConsensusInTmr(SRpcMsg *pMsg) {
12,967✔
2724
  SMnode *pMnode = pMsg->info.node;
12,967✔
2725
  int64_t now = taosGetTimestampMs();
12,967✔
2726
  bool    allReady = true;
12,967✔
2727
  SArray *pNodeSnapshot = NULL;
12,967✔
2728
  int32_t maxAllowedTrans = 50;
12,967✔
2729
  int32_t numOfTrans = 0;
12,967✔
2730
  int32_t code = 0;
12,967✔
2731
  void   *pIter = NULL;
12,967✔
2732

2733
  SArray *pList = taosArrayInit(4, sizeof(int32_t));
12,967✔
2734
  if (pList == NULL) {
12,967!
UNCOV
2735
    return terrno;
×
2736
  }
2737

2738
  SArray *pStreamList = taosArrayInit(4, sizeof(int64_t));
12,967✔
2739
  if (pStreamList == NULL) {
12,967!
UNCOV
2740
    taosArrayDestroy(pList);
×
UNCOV
2741
    return terrno;
×
2742
  }
2743

2744
  mDebug("start to process consensus-checkpointId in tmr");
12,967✔
2745

2746
  code = mndTakeVgroupSnapshot(pMnode, &allReady, &pNodeSnapshot);
12,967✔
2747
  taosArrayDestroy(pNodeSnapshot);
12,967✔
2748
  if (code) {
12,967✔
2749
    mError("failed to get the vgroup snapshot, ignore it and continue");
130!
2750
  }
2751

2752
  if (!allReady) {
12,967✔
2753
    mWarn("not all vnodes are ready, end to process the consensus-checkpointId in tmr process");
1,233!
2754
    taosArrayDestroy(pStreamList);
1,233✔
2755
    taosArrayDestroy(pList);
1,233✔
2756
    return 0;
1,233✔
2757
  }
2758

2759
  streamMutexLock(&execInfo.lock);
11,734✔
2760

2761
  while ((pIter = taosHashIterate(execInfo.pStreamConsensus, pIter)) != NULL) {
11,777✔
2762
    SCheckpointConsensusInfo *pInfo = (SCheckpointConsensusInfo *)pIter;
44✔
2763

2764
    taosArrayClear(pList);
44✔
2765

2766
    int64_t     streamId = -1;
44✔
2767
    int32_t     num = taosArrayGetSize(pInfo->pTaskList);
44✔
2768
    SStreamObj *pStream = NULL;
44✔
2769

2770
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
44✔
2771
    if (pStream == NULL || code != 0) {  // stream has been dropped already
44!
UNCOV
2772
      mDebug("stream:0x%" PRIx64 " dropped already, continue", pInfo->streamId);
×
UNCOV
2773
      void *p = taosArrayPush(pStreamList, &pInfo->streamId);
×
UNCOV
2774
      if (p == NULL) {
×
2775
        mError("failed to record the missing stream id in concensus-stream list, streamId:%" PRId64
×
2776
               " code:%s, continue",
2777
               pInfo->streamId, tstrerror(terrno));
2778
      }
UNCOV
2779
      continue;
×
2780
    }
2781

2782
    for (int32_t j = 0; j < num; ++j) {
251✔
2783
      SCheckpointConsensusEntry *pe = taosArrayGet(pInfo->pTaskList, j);
207✔
2784
      if (pe == NULL) {
207!
2785
        continue;
×
2786
      }
2787

2788
      if (streamId == -1) {
207✔
2789
        streamId = pe->req.streamId;
44✔
2790
      }
2791

2792
      int32_t existed = 0;
207✔
2793
      bool    allSame = true;
207✔
2794
      int64_t chkId = getConsensusId(pe->req.streamId, pInfo->numOfTasks, &existed, &allSame);
207✔
2795
      if (chkId == -1) {
207!
UNCOV
2796
        mDebug("not all(%d/%d) task(s) send hbMsg yet, wait for a while and check again, s-task:0x%x", existed,
×
2797
               pInfo->numOfTasks, pe->req.taskId);
UNCOV
2798
        break;
×
2799
      }
2800

2801
      if (((now - pe->ts) >= 10 * 1000) || allSame) {
410✔
2802
        mDebug("s-task:0x%x sendTs:%" PRId64 " wait %.2fs and all tasks have same checkpointId", pe->req.taskId,
203✔
2803
               pe->req.startTs, (now - pe->ts) / 1000.0);
2804
        if (chkId > pe->req.checkpointId) {
203!
UNCOV
2805
          streamMutexUnlock(&execInfo.lock);
×
UNCOV
2806
          taosArrayDestroy(pStreamList);
×
UNCOV
2807
          mError("s-task:0x%x checkpointId:%" PRId64 " is updated to %" PRId64 ", update it", pe->req.taskId,
×
2808
                 pe->req.checkpointId, chkId);
2809

UNCOV
2810
          mndReleaseStream(pMnode, pStream);
×
UNCOV
2811
          taosHashCancelIterate(execInfo.pStreamConsensus, pIter);
×
2812
          return TSDB_CODE_FAILED;
×
2813
        }
2814

2815
        code = mndCreateSetConsensusChkptIdTrans(pMnode, pStream, pe->req.taskId, chkId, pe->req.startTs);
203✔
2816
        if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
203!
UNCOV
2817
          mError("failed to create consensus-checkpoint trans, stream:0x%" PRIx64, pStream->uid);
×
2818
        }
2819

2820
        void *p = taosArrayPush(pList, &pe->req.taskId);
203✔
2821
        if (p == NULL) {
203!
UNCOV
2822
          mError("failed to put into task list, taskId:0x%x", pe->req.taskId);
×
2823
        }
2824
      } else {
2825
        mDebug("s-task:0x%x sendTs:%" PRId64 " wait %.2fs already, wait for next round to check", pe->req.taskId,
4!
2826
               pe->req.startTs, (now - pe->ts) / 1000.0);
2827
      }
2828
    }
2829

2830
    mndReleaseStream(pMnode, pStream);
44✔
2831

2832
    int32_t alreadySend = doCleanReqList(pList, pInfo);
44✔
2833

2834
    // clear request stream item with empty task list
2835
    if (taosArrayGetSize(pInfo->pTaskList) == 0) {
44✔
2836
      mndClearConsensusRspEntry(pInfo);
43✔
2837
      if (streamId == -1) {
43!
UNCOV
2838
        mError("streamId is -1, streamId:%" PRIx64" in consensus-checkpointId hashMap, cont", pInfo->streamId);
×
2839
      }
2840

2841
      void *p = taosArrayPush(pStreamList, &streamId);
43✔
2842
      if (p == NULL) {
43!
UNCOV
2843
        mError("failed to put into stream list, stream:0x%" PRIx64 " not remove it in consensus-chkpt list", streamId);
×
2844
      }
2845
    }
2846

2847
    numOfTrans += alreadySend;
44✔
2848
    if (numOfTrans > maxAllowedTrans) {
44✔
2849
      mInfo("already send consensus-checkpointId trans:%d, try next time", alreadySend);
1!
2850
      taosHashCancelIterate(execInfo.pStreamConsensus, pIter);
1✔
2851
      break;
1✔
2852
    }
2853
  }
2854

2855
  for (int32_t i = 0; i < taosArrayGetSize(pStreamList); ++i) {
11,777✔
2856
    int64_t *pStreamId = (int64_t *)taosArrayGet(pStreamList, i);
43✔
2857
    if (pStreamId == NULL) {
43!
UNCOV
2858
      continue;
×
2859
    }
2860

2861
    code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, *pStreamId);
43✔
2862
  }
2863

2864
  streamMutexUnlock(&execInfo.lock);
11,734✔
2865

2866
  taosArrayDestroy(pStreamList);
11,734✔
2867
  taosArrayDestroy(pList);
11,734✔
2868

2869
  mDebug("end to process consensus-checkpointId in tmr, send consensus-checkpoint trans:%d", numOfTrans);
11,734✔
2870
  return code;
11,734✔
2871
}
2872

2873
static int32_t mndProcessCreateStreamReqFromMNode(SRpcMsg *pReq) {
259✔
2874
  int32_t code = mndProcessCreateStreamReq(pReq);
259✔
2875
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
259!
UNCOV
2876
    pReq->info.rsp = rpcMallocCont(1);
×
UNCOV
2877
    if (pReq->info.rsp == NULL) {
×
2878
      return terrno;
×
2879
    }
2880

UNCOV
2881
    pReq->info.rspLen = 1;
×
UNCOV
2882
    pReq->info.noResp = false;
×
UNCOV
2883
    pReq->code = code;
×
2884
  }
2885
  return code;
259✔
2886
}
2887

2888
static int32_t mndProcessDropStreamReqFromMNode(SRpcMsg *pReq) {
224✔
2889
  int32_t code = mndProcessDropStreamReq(pReq);
224✔
2890
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
224!
2891
    pReq->info.rsp = rpcMallocCont(1);
20✔
2892
    if (pReq->info.rsp == NULL) {
20!
UNCOV
2893
      return terrno;
×
2894
    }
2895

2896
    pReq->info.rspLen = 1;
20✔
2897
    pReq->info.noResp = false;
20✔
2898
    pReq->code = code;
20✔
2899
  }
2900
  return code;
224✔
2901
}
2902

2903
void mndInitStreamExecInfo(SMnode *pMnode, SStreamExecInfo *pExecInfo) {
56,823✔
2904
  if (pExecInfo->initTaskList || pMnode == NULL) {
56,823✔
2905
    return;
56,665✔
2906
  }
2907

2908
  addAllStreamTasksIntoBuf(pMnode, pExecInfo);
158✔
2909
  pExecInfo->initTaskList = true;
158✔
2910
}
2911

2912
void mndStreamResetInitTaskListLoadFlag() {
1,639✔
2913
  mInfo("reset task list buffer init flag for leader");
1,639!
2914
  execInfo.initTaskList = false;
1,639✔
2915
}
1,639✔
2916

2917
void mndUpdateStreamExecInfoRole(SMnode *pMnode, int32_t role) {
1,950✔
2918
  execInfo.switchFromFollower = false;
1,950✔
2919

2920
  if (execInfo.role == NODE_ROLE_UNINIT) {
1,950✔
2921
    execInfo.role = role;
1,770✔
2922
    if (role == NODE_ROLE_LEADER) {
1,770✔
2923
      mInfo("init mnode is set to leader");
1,581!
2924
    } else {
2925
      mInfo("init mnode is set to follower");
189!
2926
    }
2927
  } else {
2928
    if (role == NODE_ROLE_LEADER) {
180✔
2929
      if (execInfo.role == NODE_ROLE_FOLLOWER) {
58!
2930
        execInfo.role = role;
58✔
2931
        execInfo.switchFromFollower = true;
58✔
2932
        mInfo("mnode switch to be leader from follower");
58!
2933
      } else {
UNCOV
2934
        mInfo("mnode remain to be leader, do nothing");
×
2935
      }
2936
    } else {  // follower's
2937
      if (execInfo.role == NODE_ROLE_LEADER) {
122✔
2938
        execInfo.role = role;
1✔
2939
        mInfo("mnode switch to be follower from leader");
1!
2940
      } else {
2941
        mInfo("mnode remain to be follower, do nothing");
121!
2942
      }
2943
    }
2944
  }
2945
}
1,950✔
2946

2947
void addAllStreamTasksIntoBuf(SMnode *pMnode, SStreamExecInfo *pExecInfo) {
158✔
2948
  SSdb       *pSdb = pMnode->pSdb;
158✔
2949
  SStreamObj *pStream = NULL;
158✔
2950
  void       *pIter = NULL;
158✔
2951

2952
  while (1) {
2953
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
422✔
2954
    if (pIter == NULL) {
422✔
2955
      break;
158✔
2956
    }
2957

2958
    saveTaskAndNodeInfoIntoBuf(pStream, pExecInfo);
264✔
2959
    sdbRelease(pSdb, pStream);
264✔
2960
  }
2961
}
158✔
2962

2963
int32_t mndCreateStreamChkptInfoUpdateTrans(SMnode *pMnode, SStreamObj *pStream, SArray *pChkptInfoList) {
1,174✔
2964
  STrans *pTrans = NULL;
1,174✔
2965
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_UPDATE_NAME,
1,174✔
2966
                               "update checkpoint-info", &pTrans);
2967
  if (pTrans == NULL || code) {
1,174!
2968
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
2969
    return code;
×
2970
  }
2971

2972
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_UPDATE_NAME, pStream->uid);
1,174✔
2973
  if (code) {
1,174!
2974
    sdbRelease(pMnode->pSdb, pStream);
×
2975
    mndTransDrop(pTrans);
×
UNCOV
2976
    return code;
×
2977
  }
2978

2979
  code = mndStreamSetUpdateChkptAction(pMnode, pTrans, pStream);
1,174✔
2980
  if (code) {
1,174!
2981
    sdbRelease(pMnode->pSdb, pStream);
×
2982
    mndTransDrop(pTrans);
×
2983
    return code;
×
2984
  }
2985

2986
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
1,174✔
2987
  if (code) {
1,174!
UNCOV
2988
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
2989
    mndTransDrop(pTrans);
×
UNCOV
2990
    return code;
×
2991
  }
2992

2993
  code = mndTransPrepare(pMnode, pTrans);
1,174✔
2994
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,174!
UNCOV
2995
    mError("trans:%d, failed to prepare update checkpoint-info meta trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
2996
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
2997
    mndTransDrop(pTrans);
×
UNCOV
2998
    return code;
×
2999
  }
3000

3001
  sdbRelease(pMnode->pSdb, pStream);
1,174✔
3002
  mndTransDrop(pTrans);
1,174✔
3003

3004
  return TSDB_CODE_ACTION_IN_PROGRESS;
1,174✔
3005
}
3006

3007
static int32_t mndProcessDropOrphanTaskReq(SRpcMsg *pReq) {
1✔
3008
  SMnode      *pMnode = pReq->info.node;
1✔
3009
  int32_t      code = 0;
1✔
3010
  SOrphanTask *pTask = NULL;
1✔
3011
  int32_t      i = 0;
1✔
3012
  STrans      *pTrans = NULL;
1✔
3013
  int32_t      numOfTasks = 0;
1✔
3014

3015
  SMStreamDropOrphanMsg msg = {0};
1✔
3016
  code = tDeserializeDropOrphanTaskMsg(pReq->pCont, pReq->contLen, &msg);
1✔
3017
  if (code) {
1!
UNCOV
3018
    return code;
×
3019
  }
3020

3021
  numOfTasks = taosArrayGetSize(msg.pList);
1✔
3022
  if (numOfTasks == 0) {
1!
UNCOV
3023
    mDebug("no orphan tasks to drop, no need to create trans");
×
UNCOV
3024
    goto _err;
×
3025
  }
3026

3027
  mDebug("create trans to drop %d orphan tasks", numOfTasks);
1!
3028

3029
  i = 0;
1✔
3030
  while (i < numOfTasks && ((pTask = taosArrayGet(msg.pList, i)) == NULL)) {
1!
UNCOV
3031
    i += 1;
×
3032
  }
3033

3034
  if (pTask == NULL) {
1!
3035
    mError("failed to extract entry in drop orphan task list, not create trans to drop orphan-task");
×
UNCOV
3036
    goto _err;
×
3037
  }
3038

3039
  // check if it is conflict with other trans in both sourceDb and targetDb.
3040
  code = mndStreamTransConflictCheck(pMnode, pTask->streamId, MND_STREAM_DROP_NAME, false);
1✔
3041
  if (code) {
1!
UNCOV
3042
    goto _err;
×
3043
  }
3044

3045
  SStreamObj dummyObj = {.uid = pTask->streamId, .sourceDb = "", .targetSTbName = ""};
1✔
3046

3047
  code = doCreateTrans(pMnode, &dummyObj, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, "drop stream", &pTrans);
1✔
3048
  if (pTrans == NULL || code != 0) {
1!
UNCOV
3049
    mError("failed to create trans to drop orphan tasks since %s", tstrerror(code));
×
UNCOV
3050
    goto _err;
×
3051
  }
3052

3053
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_DROP_NAME, pTask->streamId);
1✔
3054
  if (code) {
1!
UNCOV
3055
    goto _err;
×
3056
  }
3057

3058
  // drop all tasks
3059
  if ((code = mndStreamSetDropActionFromList(pMnode, pTrans, msg.pList)) < 0) {
1!
UNCOV
3060
    mError("failed to create trans to drop orphan tasks since %s", tstrerror(code));
×
UNCOV
3061
    goto _err;
×
3062
  }
3063

3064
  // drop stream
3065
  if ((code = mndPersistTransLog(&dummyObj, pTrans, SDB_STATUS_DROPPED)) < 0) {
1!
UNCOV
3066
    goto _err;
×
3067
  }
3068

3069
  code = mndTransPrepare(pMnode, pTrans);
1✔
3070
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1!
UNCOV
3071
    mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
UNCOV
3072
    goto _err;
×
3073
  }
3074

3075
_err:
1✔
3076
  tDestroyDropOrphanTaskMsg(&msg);
1✔
3077
  mndTransDrop(pTrans);
1✔
3078

3079
  if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {
1!
3080
    mDebug("create drop %d orphan tasks trans succ", numOfTasks);
1!
3081
  }
3082
  return code;
1✔
3083
}
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