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

taosdata / TDengine / #4506

15 Jul 2025 12:33AM UTC coverage: 62.026% (-0.7%) from 62.706%
#4506

push

travis-ci

web-flow
docs: update stream docs (#31874)

155391 of 320094 branches covered (48.55%)

Branch coverage included in aggregate %.

240721 of 318525 relevant lines covered (75.57%)

6529048.03 hits per line

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

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

16
#include "mndStream.h"
17
#include "audit.h"
18
#include "mndDb.h"
19
#include "mndPrivilege.h"
20
#include "mndScheduler.h"
21
#include "mndShow.h"
22
#include "mndStb.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 mndProcessFailedStreamReq(SRpcMsg *pReq);
44
static int32_t mndProcessCheckStreamStatusReq(SRpcMsg *pReq);
45
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq);
46

47
static int32_t mndProcessCreateStreamReqFromMNode(SRpcMsg *pReq);
48
static int32_t mndProcessDropStreamReqFromMNode(SRpcMsg *pReq);
49

50
static int32_t mndProcessStreamCheckpoint(SRpcMsg *pReq);
51
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
52
static void    mndCancelGetNextStream(SMnode *pMnode, void *pIter);
53
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
54
static void    mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter);
55
static int32_t mndProcessPauseStreamReq(SRpcMsg *pReq);
56
static int32_t mndProcessResumeStreamReq(SRpcMsg *pReq);
57
static int32_t mndProcessResetStreamReq(SRpcMsg *pReq);
58
static int32_t mndBuildStreamCheckpointSourceReq(void **pBuf, int32_t *pLen, int32_t nodeId, int64_t checkpointId,
59
                                                 int64_t streamId, int32_t taskId, int32_t transId, int8_t mndTrigger);
60
static int32_t mndProcessNodeCheck(SRpcMsg *pReq);
61
static int32_t mndProcessNodeCheckReq(SRpcMsg *pMsg);
62
static int32_t refreshNodeListFromExistedStreams(SMnode *pMnode, SArray *pNodeList);
63
static int32_t mndProcessStreamReqCheckpoint(SRpcMsg *pReq);
64
static int32_t mndProcessCheckpointReport(SRpcMsg *pReq);
65
static int32_t mndProcessConsensusInTmr(SRpcMsg *pMsg);
66
static void    doSendQuickRsp(SRpcHandleInfo *pInfo, int32_t msgSize, int32_t vgId, int32_t code);
67
static int32_t mndProcessDropOrphanTaskReq(SRpcMsg *pReq);
68
static void    saveTaskAndNodeInfoIntoBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode);
69

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

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

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

99
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STREAM, mndProcessCreateStreamReq);
2,477✔
100
  mndSetMsgHandle(pMnode, TDMT_MND_FAILED_STREAM, mndProcessFailedStreamReq);
2,477✔
101
  mndSetMsgHandle(pMnode, TDMT_MND_CHECK_STREAM_TIMER, mndProcessCheckStreamStatusReq);
2,477✔
102
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STREAM, mndProcessDropStreamReq);
2,477✔
103
  mndSetMsgHandle(pMnode, TDMT_MND_NODECHECK_TIMER, mndProcessNodeCheck);
2,477✔
104

105
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_DEPLOY_RSP, mndTransProcessRsp);
2,477✔
106
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_DROP_RSP, mndTransProcessRsp);
2,477✔
107
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_PAUSE_RSP, mndTransProcessRsp);
2,477✔
108
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_RESUME_RSP, mndTransProcessRsp);
2,477✔
109
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_STOP_RSP, mndTransProcessRsp);
2,477✔
110
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_START_RSP, mndTransProcessRsp);
2,477✔
111
  mndSetMsgHandle(pMnode, TDMT_VND_STREAM_TASK_UPDATE_RSP, mndTransProcessRsp);
2,477✔
112
  mndSetMsgHandle(pMnode, TDMT_VND_STREAM_TASK_RESET_RSP, mndTransProcessRsp);
2,477✔
113
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_UPDATE_CHKPT_RSP, mndTransProcessRsp);
2,477✔
114
  mndSetMsgHandle(pMnode, TDMT_STREAM_CONSEN_CHKPT_RSP, mndTransProcessRsp);
2,477✔
115

116
  // for msgs inside mnode
117
  // TODO change the name
118
  mndSetMsgHandle(pMnode, TDMT_STREAM_CREATE, mndProcessCreateStreamReqFromMNode);
2,477✔
119
  mndSetMsgHandle(pMnode, TDMT_STREAM_CREATE_RSP, mndTransProcessRsp);
2,477✔
120
  mndSetMsgHandle(pMnode, TDMT_STREAM_DROP, mndProcessDropStreamReqFromMNode);
2,477✔
121
  mndSetMsgHandle(pMnode, TDMT_STREAM_DROP_RSP, mndTransProcessRsp);
2,477✔
122

123
  mndSetMsgHandle(pMnode, TDMT_VND_STREAM_CHECK_POINT_SOURCE_RSP, mndTransProcessRsp);
2,477✔
124
  mndSetMsgHandle(pMnode, TDMT_VND_STREAM_ALL_STOP_RSP, mndTransProcessRsp);
2,477✔
125
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_BEGIN_CHECKPOINT, mndProcessStreamCheckpoint);
2,477✔
126
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_DROP_ORPHANTASKS, mndProcessDropOrphanTaskReq);
2,477✔
127
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_TASK_RESET, mndProcessResetStatusReq);
2,477✔
128
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_REQ_CHKPT, mndProcessStreamReqCheckpoint);
2,477✔
129
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_CHKPT_REPORT, mndProcessCheckpointReport);
2,477✔
130
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_UPDATE_CHKPT_EVT, mndScanCheckpointReportInfo);
2,477✔
131
  mndSetMsgHandle(pMnode, TDMT_STREAM_TASK_REPORT_CHECKPOINT, mndTransProcessRsp);
2,477✔
132
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_HEARTBEAT, mndProcessStreamHb);
2,477✔
133
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_NODECHANGE_CHECK, mndProcessNodeCheckReq);
2,477✔
134
  mndSetMsgHandle(pMnode, TDMT_MND_STREAM_CONSEN_TIMER, mndProcessConsensusInTmr);
2,477✔
135

136
  mndSetMsgHandle(pMnode, TDMT_MND_PAUSE_STREAM, mndProcessPauseStreamReq);
2,477✔
137
  mndSetMsgHandle(pMnode, TDMT_MND_STOP_STREAM, mndProcessPauseStreamReq);
2,477✔
138
  mndSetMsgHandle(pMnode, TDMT_MND_START_STREAM, mndProcessPauseStreamReq);
2,477✔
139
  mndSetMsgHandle(pMnode, TDMT_MND_RESUME_STREAM, mndProcessResumeStreamReq);
2,477✔
140
  mndSetMsgHandle(pMnode, TDMT_MND_RESET_STREAM, mndProcessResetStreamReq);
2,477✔
141

142
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndRetrieveStream);
2,477✔
143
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndCancelGetNextStream);
2,477✔
144
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndRetrieveStreamTask);
2,477✔
145
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndCancelGetNextStreamTask);
2,477✔
146

147
  int32_t code = mndInitExecInfo();
2,477✔
148
  if (code) {
2,477!
149
    return code;
×
150
  }
151

152
  code = sdbSetTable(pMnode->pSdb, table);
2,477✔
153
  if (code) {
2,477!
154
    return code;
×
155
  }
156

157
  code = sdbSetTable(pMnode->pSdb, tableSeq);
2,477✔
158
  return code;
2,477✔
159
}
160

161
void mndCleanupStream(SMnode *pMnode) {
2,476✔
162
  taosArrayDestroy(execInfo.pTaskList);
2,476✔
163
  taosArrayDestroy(execInfo.pNodeList);
2,476✔
164
  taosArrayDestroy(execInfo.pKilledChkptTrans);
2,476✔
165
  taosHashCleanup(execInfo.pTaskMap);
2,476✔
166
  taosHashCleanup(execInfo.transMgmt.pDBTrans);
2,476✔
167
  taosHashCleanup(execInfo.pTransferStateStreams);
2,476✔
168
  taosHashCleanup(execInfo.pChkptStreams);
2,476✔
169
  taosHashCleanup(execInfo.pStreamConsensus);
2,476✔
170
  (void)taosThreadMutexDestroy(&execInfo.lock);
2,476✔
171
  mDebug("mnd stream exec info cleanup");
2,476✔
172
}
2,476✔
173

174
SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw) {
2,349✔
175
  int32_t     code = 0;
2,349✔
176
  int32_t     lino = 0;
2,349✔
177
  SSdbRow    *pRow = NULL;
2,349✔
178
  SStreamObj *pStream = NULL;
2,349✔
179
  void       *buf = NULL;
2,349✔
180
  int8_t      sver = 0;
2,349✔
181
  int32_t     tlen;
182
  int32_t     dataPos = 0;
2,349✔
183

184
  code = sdbGetRawSoftVer(pRaw, &sver);
2,349✔
185
  TSDB_CHECK_CODE(code, lino, _over);
2,349!
186

187
  if (sver < 1 || sver > MND_STREAM_VER_NUMBER) {
2,349!
188
    mError("stream read invalid ver, data ver: %d, curr ver: %d", sver, MND_STREAM_VER_NUMBER);
×
189
    goto _over;
×
190
  }
191

192
  pRow = sdbAllocRow(sizeof(SStreamObj));
2,349✔
193
  TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
2,349!
194

195
  pStream = sdbGetRowObj(pRow);
2,349✔
196
  TSDB_CHECK_NULL(pStream, code, lino, _over, terrno);
2,349!
197

198
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
2,349!
199

200
  buf = taosMemoryMalloc(tlen + 1);
2,349!
201
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
2,349!
202

203
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
2,349!
204

205
  SDecoder decoder;
206
  tDecoderInit(&decoder, buf, tlen + 1);
2,349✔
207
  code = tDecodeSStreamObj(&decoder, pStream, sver);
2,349✔
208
  tDecoderClear(&decoder);
2,349✔
209

210
  if (code < 0) {
2,349!
211
    tFreeStreamObj(pStream);
×
212
  }
213

214
_over:
2,349✔
215
  taosMemoryFreeClear(buf);
2,349!
216

217
  if (code != TSDB_CODE_SUCCESS) {
2,349!
218
    char *p = (pStream == NULL) ? "null" : pStream->name;
×
219
    mError("stream:%s, failed to decode from raw:%p since %s at:%d", p, pRaw, tstrerror(code), lino);
×
220
    taosMemoryFreeClear(pRow);
×
221

222
    terrno = code;
×
223
    return NULL;
×
224
  } else {
225
    mTrace("stream:%s, decode from raw:%p, row:%p, checkpoint:%" PRId64, pStream->name, pRaw, pStream,
2,349✔
226
           pStream->checkpointId);
227

228
    terrno = 0;
2,349✔
229
    return pRow;
2,349✔
230
  }
231
}
232

233
static int32_t mndStreamActionInsert(SSdb *pSdb, SStreamObj *pStream) {
1,084✔
234
  mTrace("stream:%s, perform insert action", pStream->name);
1,084✔
235
  return 0;
1,084✔
236
}
237

238
static int32_t mndStreamActionDelete(SSdb *pSdb, SStreamObj *pStream) {
2,349✔
239
  mInfo("stream:%s, perform delete action", pStream->name);
2,349!
240
  taosWLockLatch(&pStream->lock);
2,349✔
241
  tFreeStreamObj(pStream);
2,349✔
242
  taosWUnLockLatch(&pStream->lock);
2,349✔
243
  return 0;
2,349✔
244
}
245

246
static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pOldStream, SStreamObj *pNewStream) {
634✔
247
  mTrace("stream:%s, perform update action", pOldStream->name);
634✔
248
  (void)atomic_exchange_32(&pOldStream->version, pNewStream->version);
634✔
249

250
  taosWLockLatch(&pOldStream->lock);
634✔
251

252
  pOldStream->status = pNewStream->status;
634✔
253
  pOldStream->updateTime = pNewStream->updateTime;
634✔
254
  pOldStream->checkpointId = pNewStream->checkpointId;
634✔
255
  pOldStream->checkpointFreq = pNewStream->checkpointFreq;
634✔
256
  if (pOldStream->pTaskList == NULL) {
634✔
257
    pOldStream->pTaskList = pNewStream->pTaskList;
2✔
258
    pNewStream->pTaskList = NULL;
2✔
259
  }
260
  if (pOldStream->pHTaskList == NULL) {
634!
261
    pOldStream->pHTaskList = pNewStream->pHTaskList;
634✔
262
    pNewStream->pHTaskList = NULL;
634✔
263
  }
264
  taosWUnLockLatch(&pOldStream->lock);
634✔
265
  return 0;
634✔
266
}
267

268
int32_t mndAcquireStream(SMnode *pMnode, char *streamName, SStreamObj **pStream) {
2,921✔
269
  int32_t code = 0;
2,921✔
270
  SSdb   *pSdb = pMnode->pSdb;
2,921✔
271
  (*pStream) = sdbAcquire(pSdb, SDB_STREAM, streamName);
2,921✔
272
  if ((*pStream) == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
2,921!
273
    code = TSDB_CODE_MND_STREAM_NOT_EXIST;
1,295✔
274
  }
275
  return code;
2,921✔
276
}
277

278
void mndReleaseStream(SMnode *pMnode, SStreamObj *pStream) {
5,673✔
279
  SSdb *pSdb = pMnode->pSdb;
5,673✔
280
  sdbRelease(pSdb, pStream);
5,673✔
281
}
5,673✔
282

283
SSdbRaw *mndStreamSeqActionEncode(SStreamObj *pStream) { return NULL; }
×
284
SSdbRow *mndStreamSeqActionDecode(SSdbRaw *pRaw) { return NULL; }
×
285
int32_t  mndStreamSeqActionInsert(SSdb *pSdb, SStreamSeq *pStream) { return 0; }
×
286
int32_t  mndStreamSeqActionDelete(SSdb *pSdb, SStreamSeq *pStream) { return 0; }
×
287
int32_t  mndStreamSeqActionUpdate(SSdb *pSdb, SStreamSeq *pOldStream, SStreamSeq *pNewStream) { return 0; }
×
288

289
static int32_t mndCheckCreateStreamReq(SCMCreateStreamReq *pCreate) {
1,062✔
290
  if (pCreate->name[0] == 0 || pCreate->sql == NULL || pCreate->sql[0] == 0 || pCreate->sourceDB[0] == 0 ||
1,062!
291
      pCreate->targetStbFullName[0] == 0) {
1,062!
292
    return TSDB_CODE_MND_INVALID_STREAM_OPTION;
×
293
  }
294
  return TSDB_CODE_SUCCESS;
1,062✔
295
}
296

297
static int32_t createSchemaByFields(const SArray *pFields, SSchemaWrapper *pWrapper) {
1,055✔
298
  pWrapper->nCols = taosArrayGetSize(pFields);
1,055✔
299
  pWrapper->pSchema = taosMemoryCalloc(pWrapper->nCols, sizeof(SSchema));
1,055!
300
  if (NULL == pWrapper->pSchema) {
1,055!
301
    return terrno;
×
302
  }
303

304
  int32_t index = 0;
1,055✔
305
  for (int32_t i = 0; i < pWrapper->nCols; i++) {
10,240✔
306
    SField *pField = (SField *)taosArrayGet(pFields, i);
9,185✔
307
    if (pField == NULL) {
9,185!
308
      return terrno;
×
309
    }
310

311
    if (TSDB_DATA_TYPE_NULL == pField->type) {
9,185!
312
      pWrapper->pSchema[index].type = TSDB_DATA_TYPE_VARCHAR;
×
313
      pWrapper->pSchema[index].bytes = VARSTR_HEADER_SIZE;
×
314
    } else {
315
      pWrapper->pSchema[index].type = pField->type;
9,185✔
316
      pWrapper->pSchema[index].bytes = pField->bytes;
9,185✔
317
    }
318
    pWrapper->pSchema[index].colId = index + 1;
9,185✔
319
    tstrncpy(pWrapper->pSchema[index].name, pField->name, sizeof(pWrapper->pSchema[index].name));
9,185✔
320
    pWrapper->pSchema[index].flags = pField->flags;
9,185✔
321
    index += 1;
9,185✔
322
  }
323

324
  return TSDB_CODE_SUCCESS;
1,055✔
325
}
326

327
static bool hasDestPrimaryKey(SSchemaWrapper *pWrapper) {
1,055✔
328
  if (pWrapper->nCols < 2) {
1,055!
329
    return false;
×
330
  }
331
  for (int32_t i = 1; i < pWrapper->nCols; i++) {
9,234✔
332
    if (pWrapper->pSchema[i].flags & COL_IS_KEY) {
8,206✔
333
      return true;
27✔
334
    }
335
  }
336
  return false;
1,028✔
337
}
338

339
static int32_t mndBuildStreamObjFromCreateReq(SMnode *pMnode, SStreamObj *pObj, SCMCreateStreamReq *pCreate) {
1,055✔
340
  SNode      *pAst = NULL;
1,055✔
341
  SQueryPlan *pPlan = NULL;
1,055✔
342
  int32_t     code = 0;
1,055✔
343

344
  mInfo("stream:%s to create", pCreate->name);
1,055!
345
  memcpy(pObj->name, pCreate->name, TSDB_STREAM_FNAME_LEN);
1,055✔
346
  pObj->createTime = taosGetTimestampMs();
1,055✔
347
  pObj->updateTime = pObj->createTime;
1,055✔
348
  pObj->version = 1;
1,055✔
349

350
  if (pCreate->smaId > 0) {
1,055✔
351
    pObj->subTableWithoutMd5 = 1;
109✔
352
  }
353

354
  pObj->smaId = pCreate->smaId;
1,055✔
355
  pObj->indexForMultiAggBalance = -1;
1,055✔
356

357
  pObj->uid = mndGenerateUid(pObj->name, strlen(pObj->name));
1,055✔
358

359
  char p[TSDB_STREAM_FNAME_LEN + 32] = {0};
1,055✔
360
  snprintf(p, tListLen(p), "%s_%s", pObj->name, "fillhistory");
1,055✔
361

362
  pObj->hTaskUid = mndGenerateUid(pObj->name, strlen(pObj->name));
1,055✔
363
  pObj->status = STREAM_STATUS__NORMAL;
1,055✔
364

365
  pObj->conf.igExpired = pCreate->igExpired;
1,055✔
366
  pObj->conf.trigger = pCreate->triggerType;
1,055✔
367
  pObj->conf.triggerParam = pCreate->maxDelay;
1,055✔
368
  pObj->conf.watermark = pCreate->watermark;
1,055✔
369
  pObj->conf.fillHistory = pCreate->fillHistory;
1,055✔
370
  pObj->deleteMark = pCreate->deleteMark;
1,055✔
371
  pObj->igCheckUpdate = pCreate->igUpdate;
1,055✔
372

373
  memcpy(pObj->sourceDb, pCreate->sourceDB, TSDB_DB_FNAME_LEN);
1,055✔
374
  SDbObj *pSourceDb = mndAcquireDb(pMnode, pCreate->sourceDB);
1,055✔
375
  if (pSourceDb == NULL) {
1,055!
376
    code = terrno;
×
377
    mInfo("stream:%s failed to create, source db %s not exist since %s", pCreate->name, pObj->sourceDb,
×
378
          tstrerror(code));
379
    goto _ERR;
×
380
  }
381

382
  pObj->sourceDbUid = pSourceDb->uid;
1,055✔
383
  mndReleaseDb(pMnode, pSourceDb);
1,055✔
384

385
  memcpy(pObj->targetSTbName, pCreate->targetStbFullName, TSDB_TABLE_FNAME_LEN);
1,055✔
386

387
  SDbObj *pTargetDb = mndAcquireDbByStb(pMnode, pObj->targetSTbName);
1,055✔
388
  if (pTargetDb == NULL) {
1,055!
389
    code = terrno;
×
390
    mError("stream:%s failed to create, target db %s not exist since %s", pCreate->name, pObj->targetDb,
×
391
           tstrerror(code));
392
    goto _ERR;
×
393
  }
394

395
  tstrncpy(pObj->targetDb, pTargetDb->name, TSDB_DB_FNAME_LEN);
1,055✔
396

397
  if (pCreate->createStb == STREAM_CREATE_STABLE_TRUE) {
1,055✔
398
    pObj->targetStbUid = mndGenerateUid(pObj->targetSTbName, TSDB_TABLE_FNAME_LEN);
974✔
399
  } else {
400
    pObj->targetStbUid = pCreate->targetStbUid;
81✔
401
  }
402
  pObj->targetDbUid = pTargetDb->uid;
1,055✔
403
  mndReleaseDb(pMnode, pTargetDb);
1,055✔
404

405
  pObj->sql = pCreate->sql;
1,055✔
406
  pObj->ast = pCreate->ast;
1,055✔
407

408
  pCreate->sql = NULL;
1,055✔
409
  pCreate->ast = NULL;
1,055✔
410

411
  // deserialize ast
412
  if ((code = nodesStringToNode(pObj->ast, &pAst)) < 0) {
1,055!
413
    goto _ERR;
×
414
  }
415

416
  // create output schema
417
  if ((code = createSchemaByFields(pCreate->pCols, &pObj->outputSchema)) != TSDB_CODE_SUCCESS) {
1,055!
418
    goto _ERR;
×
419
  }
420

421
  int32_t numOfNULL = taosArrayGetSize(pCreate->fillNullCols);
1,055✔
422
  if (numOfNULL > 0) {
1,055✔
423
    pObj->outputSchema.nCols += numOfNULL;
14✔
424
    SSchema *pFullSchema = taosMemoryCalloc(pObj->outputSchema.nCols, sizeof(SSchema));
14!
425
    if (!pFullSchema) {
14!
426
      code = terrno;
×
427
      goto _ERR;
×
428
    }
429

430
    int32_t nullIndex = 0;
14✔
431
    int32_t dataIndex = 0;
14✔
432
    for (int32_t i = 0; i < pObj->outputSchema.nCols; i++) {
152✔
433
      if (nullIndex >= numOfNULL) {
138!
434
        pFullSchema[i].bytes = pObj->outputSchema.pSchema[dataIndex].bytes;
×
435
        pFullSchema[i].colId = i + 1;  // pObj->outputSchema.pSchema[dataIndex].colId;
×
436
        pFullSchema[i].flags = pObj->outputSchema.pSchema[dataIndex].flags;
×
437
        tstrncpy(pFullSchema[i].name, pObj->outputSchema.pSchema[dataIndex].name, sizeof(pFullSchema[i].name));
×
438
        pFullSchema[i].type = pObj->outputSchema.pSchema[dataIndex].type;
×
439
        dataIndex++;
×
440
      } else {
441
        SColLocation *pos = NULL;
138✔
442
        if (nullIndex < taosArrayGetSize(pCreate->fillNullCols)) {
138!
443
          pos = taosArrayGet(pCreate->fillNullCols, nullIndex);
138✔
444
        }
445

446
        if (pos == NULL) {
138!
447
          mError("invalid null column index, %d", nullIndex);
×
448
          continue;
×
449
        }
450

451
        if (i < pos->slotId) {
138✔
452
          pFullSchema[i].bytes = pObj->outputSchema.pSchema[dataIndex].bytes;
43✔
453
          pFullSchema[i].colId = i + 1;  // pObj->outputSchema.pSchema[dataIndex].colId;
43✔
454
          pFullSchema[i].flags = pObj->outputSchema.pSchema[dataIndex].flags;
43✔
455
          tstrncpy(pFullSchema[i].name, pObj->outputSchema.pSchema[dataIndex].name, sizeof(pFullSchema[i].name));
43✔
456
          pFullSchema[i].type = pObj->outputSchema.pSchema[dataIndex].type;
43✔
457
          dataIndex++;
43✔
458
        } else {
459
          pFullSchema[i].bytes = 0;
95✔
460
          pFullSchema[i].colId = pos->colId;
95✔
461
          pFullSchema[i].flags = COL_SET_NULL;
95✔
462
          memset(pFullSchema[i].name, 0, TSDB_COL_NAME_LEN);
95✔
463
          pFullSchema[i].type = pos->type;
95✔
464
          nullIndex++;
95✔
465
        }
466
      }
467
    }
468

469
    taosMemoryFree(pObj->outputSchema.pSchema);
14!
470
    pObj->outputSchema.pSchema = pFullSchema;
14✔
471
  }
472

473
  SPlanContext cxt = {
1,055✔
474
      .pAstRoot = pAst,
475
      .topicQuery = false,
476
      .streamQuery = true,
477
      .triggerType =
478
          (pObj->conf.trigger == STREAM_TRIGGER_MAX_DELAY) ? STREAM_TRIGGER_WINDOW_CLOSE : pObj->conf.trigger,
1,055✔
479
      .watermark = pObj->conf.watermark,
1,055✔
480
      .igExpired = pObj->conf.igExpired,
1,055✔
481
      .deleteMark = pObj->deleteMark,
1,055✔
482
      .igCheckUpdate = pObj->igCheckUpdate,
1,055✔
483
      .destHasPrimaryKey = hasDestPrimaryKey(&pObj->outputSchema),
1,055✔
484
      .recalculateInterval = pCreate->recalculateInterval,
1,055✔
485
  };
486
  char *pTargetFStable = strchr(pCreate->targetStbFullName, '.');
1,055✔
487
  if (pTargetFStable != NULL) {
1,055!
488
    pTargetFStable = pTargetFStable + 1;
1,055✔
489
  }
490
  tstrncpy(cxt.pStbFullName, pTargetFStable, TSDB_TABLE_FNAME_LEN);
1,055✔
491
  tstrncpy(cxt.pWstartName, pCreate->pWstartName, TSDB_COL_NAME_LEN);
1,055✔
492
  tstrncpy(cxt.pWendName, pCreate->pWendName, TSDB_COL_NAME_LEN);
1,055✔
493
  tstrncpy(cxt.pGroupIdName, pCreate->pGroupIdName, TSDB_COL_NAME_LEN);
1,055✔
494
  tstrncpy(cxt.pIsWindowFilledName, pCreate->pIsWindowFilledName, TSDB_COL_NAME_LEN);
1,055✔
495

496
  // using ast and param to build physical plan
497
  if ((code = qCreateQueryPlan(&cxt, &pPlan, NULL)) < 0) {
1,055!
498
    goto _ERR;
×
499
  }
500

501
  // save physcial plan
502
  if ((code = nodesNodeToString((SNode *)pPlan, false, &pObj->physicalPlan, NULL)) != 0) {
1,055!
503
    goto _ERR;
×
504
  }
505

506
  pObj->tagSchema.nCols = pCreate->numOfTags;
1,055✔
507
  if (pCreate->numOfTags) {
1,055✔
508
    pObj->tagSchema.pSchema = taosMemoryCalloc(pCreate->numOfTags, sizeof(SSchema));
133!
509
    if (pObj->tagSchema.pSchema == NULL) {
133!
510
      code = terrno;
×
511
      goto _ERR;
×
512
    }
513
  }
514

515
  /*A(pCreate->numOfTags == taosArrayGetSize(pCreate->pTags));*/
516
  for (int32_t i = 0; i < pCreate->numOfTags; i++) {
1,804✔
517
    SField *pField = taosArrayGet(pCreate->pTags, i);
749✔
518
    if (pField == NULL) {
749!
519
      continue;
×
520
    }
521

522
    pObj->tagSchema.pSchema[i].colId = pObj->outputSchema.nCols + i + 1;
749✔
523
    pObj->tagSchema.pSchema[i].bytes = pField->bytes;
749✔
524
    pObj->tagSchema.pSchema[i].flags = pField->flags;
749✔
525
    pObj->tagSchema.pSchema[i].type = pField->type;
749✔
526
    memcpy(pObj->tagSchema.pSchema[i].name, pField->name, TSDB_COL_NAME_LEN);
749✔
527
  }
528

529
_ERR:
1,055✔
530
  if (pAst != NULL) nodesDestroyNode(pAst);
1,055!
531
  if (pPlan != NULL) qDestroyQueryPlan(pPlan);
1,055!
532
  return code;
1,055✔
533
}
534

535
int32_t mndPersistTaskDeployReq(STrans *pTrans, SStreamTask *pTask) {
6,455✔
536
  SEncoder encoder;
537
  tEncoderInit(&encoder, NULL, 0);
6,455✔
538

539
  if (pTask->ver < SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
6,455!
540
    pTask->ver = SSTREAM_TASK_VER;
×
541
  }
542

543
  int32_t code = tEncodeStreamTask(&encoder, pTask);
6,455✔
544
  if (code == -1) {
6,455!
545
    tEncoderClear(&encoder);
×
546
    return TSDB_CODE_INVALID_MSG;
×
547
  }
548

549
  int32_t size = encoder.pos;
6,455✔
550
  int32_t tlen = sizeof(SMsgHead) + size;
6,455✔
551
  tEncoderClear(&encoder);
6,455✔
552

553
  void *buf = taosMemoryCalloc(1, tlen);
6,455!
554
  if (buf == NULL) {
6,455!
555
    return terrno;
×
556
  }
557

558
  ((SMsgHead *)buf)->vgId = htonl(pTask->info.nodeId);
6,455✔
559

560
  void *abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
6,455✔
561
  tEncoderInit(&encoder, abuf, size);
6,455✔
562
  code = tEncodeStreamTask(&encoder, pTask);
6,455✔
563
  tEncoderClear(&encoder);
6,455✔
564

565
  if (code != 0) {
6,455!
566
    mError("failed to encode stream task, code:%s", tstrerror(code));
×
567
    taosMemoryFree(buf);
×
568
    return code;
×
569
  }
570

571
  code = setTransAction(pTrans, buf, tlen, TDMT_STREAM_TASK_DEPLOY, &pTask->info.epSet, 0,
6,455✔
572
                        TSDB_CODE_VND_INVALID_VGROUP_ID);
573
  if (code) {
6,455!
574
    taosMemoryFree(buf);
×
575
  }
576

577
  return code;
6,455✔
578
}
579

580
int32_t mndPersistStreamTasks(STrans *pTrans, SStreamObj *pStream) {
1,054✔
581
  SStreamTaskIter *pIter = NULL;
1,054✔
582
  int32_t          code = createStreamTaskIter(pStream, &pIter);
1,054✔
583
  if (code) {
1,054!
584
    mError("failed to create task iter for stream:%s", pStream->name);
×
585
    return code;
×
586
  }
587

588
  while (streamTaskIterNextTask(pIter)) {
5,705✔
589
    SStreamTask *pTask = NULL;
4,651✔
590
    code = streamTaskIterGetCurrent(pIter, &pTask);
4,651✔
591
    if (code) {
4,651!
592
      destroyStreamTaskIter(pIter);
×
593
      return code;
×
594
    }
595

596
    code = mndPersistTaskDeployReq(pTrans, pTask);
4,651✔
597
    if (code) {
4,651!
598
      destroyStreamTaskIter(pIter);
×
599
      return code;
×
600
    }
601
  }
602

603
  destroyStreamTaskIter(pIter);
1,054✔
604

605
  // persistent stream task for already stored ts data
606
  if (pStream->conf.fillHistory || (pStream->conf.trigger == STREAM_TRIGGER_CONTINUOUS_WINDOW_CLOSE)) {
1,054!
607
    int32_t level = taosArrayGetSize(pStream->pHTaskList);
343✔
608

609
    for (int32_t i = 0; i < level; i++) {
1,046✔
610
      SArray *pLevel = taosArrayGetP(pStream->pHTaskList, i);
703✔
611

612
      int32_t numOfTasks = taosArrayGetSize(pLevel);
703✔
613
      for (int32_t j = 0; j < numOfTasks; j++) {
2,507✔
614
        SStreamTask *pTask = taosArrayGetP(pLevel, j);
1,804✔
615
        code = mndPersistTaskDeployReq(pTrans, pTask);
1,804✔
616
        if (code) {
1,804!
617
          return code;
×
618
        }
619
      }
620
    }
621
  }
622

623
  return code;
1,054✔
624
}
625

626
int32_t mndPersistStream(STrans *pTrans, SStreamObj *pStream) {
1,054✔
627
  int32_t code = 0;
1,054✔
628
  if ((code = mndPersistStreamTasks(pTrans, pStream)) < 0) {
1,054!
629
    return code;
×
630
  }
631

632
  return mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
1,054✔
633
}
634

635
static int32_t mndCreateStbForStream(SMnode *pMnode, STrans *pTrans, const SStreamObj *pStream, const char *user) {
974✔
636
  SStbObj *pStb = NULL;
974✔
637
  SDbObj  *pDb = NULL;
974✔
638
  int32_t  code = 0;
974✔
639
  int32_t  lino = 0;
974✔
640

641
  SMCreateStbReq createReq = {0};
974✔
642
  tstrncpy(createReq.name, pStream->targetSTbName, TSDB_TABLE_FNAME_LEN);
974✔
643
  createReq.numOfColumns = pStream->outputSchema.nCols;
974✔
644
  createReq.numOfTags = 1;  // group id
974✔
645
  createReq.pColumns = taosArrayInit_s(sizeof(SFieldWithOptions), createReq.numOfColumns);
974✔
646
  TSDB_CHECK_NULL(createReq.pColumns, code, lino, _OVER, terrno);
974!
647

648
  // build fields
649
  for (int32_t i = 0; i < createReq.numOfColumns; i++) {
9,371✔
650
    SFieldWithOptions *pField = taosArrayGet(createReq.pColumns, i);
8,397✔
651
    TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno);
8,397!
652

653
    tstrncpy(pField->name, pStream->outputSchema.pSchema[i].name, TSDB_COL_NAME_LEN);
8,397✔
654
    pField->flags = pStream->outputSchema.pSchema[i].flags;
8,397✔
655
    pField->type = pStream->outputSchema.pSchema[i].type;
8,397✔
656
    pField->bytes = pStream->outputSchema.pSchema[i].bytes;
8,397✔
657
    pField->compress = createDefaultColCmprByType(pField->type);
8,397✔
658
    if (IS_DECIMAL_TYPE(pField->type)) {
8,397✔
659
      uint8_t prec = 0, scale = 0;
65✔
660
      extractDecimalTypeInfoFromBytes(&pField->bytes, &prec, &scale);
65✔
661
      pField->typeMod = decimalCalcTypeMod(prec, scale);
65✔
662
    }
663
  }
664

665
  if (pStream->tagSchema.nCols == 0) {
974✔
666
    createReq.numOfTags = 1;
841✔
667
    createReq.pTags = taosArrayInit_s(sizeof(SField), 1);
841✔
668
    TSDB_CHECK_NULL(createReq.pTags, code, lino, _OVER, terrno);
841!
669

670
    // build tags
671
    SField *pField = taosArrayGet(createReq.pTags, 0);
841✔
672
    TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno);
841!
673

674
    tstrncpy(pField->name, "group_id", sizeof(pField->name));
841✔
675
    pField->type = TSDB_DATA_TYPE_UBIGINT;
841✔
676
    pField->flags = 0;
841✔
677
    pField->bytes = 8;
841✔
678
  } else {
679
    createReq.numOfTags = pStream->tagSchema.nCols;
133✔
680
    createReq.pTags = taosArrayInit_s(sizeof(SField), createReq.numOfTags);
133✔
681
    TSDB_CHECK_NULL(createReq.pTags, code, lino, _OVER, terrno);
133!
682

683
    for (int32_t i = 0; i < createReq.numOfTags; i++) {
882✔
684
      SField *pField = taosArrayGet(createReq.pTags, i);
749✔
685
      if (pField == NULL) {
749!
686
        continue;
×
687
      }
688

689
      pField->bytes = pStream->tagSchema.pSchema[i].bytes;
749✔
690
      pField->flags = pStream->tagSchema.pSchema[i].flags;
749✔
691
      pField->type = pStream->tagSchema.pSchema[i].type;
749✔
692
      tstrncpy(pField->name, pStream->tagSchema.pSchema[i].name, TSDB_COL_NAME_LEN);
749✔
693
    }
694
  }
695

696
  if ((code = mndCheckCreateStbReq(&createReq)) != 0) {
974!
697
    goto _OVER;
×
698
  }
699

700
  pStb = mndAcquireStb(pMnode, createReq.name);
974✔
701
  if (pStb != NULL) {
974!
702
    code = TSDB_CODE_MND_STB_ALREADY_EXIST;
×
703
    goto _OVER;
×
704
  }
705

706
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
974✔
707
  if (pDb == NULL) {
974!
708
    code = TSDB_CODE_MND_DB_NOT_SELECTED;
×
709
    goto _OVER;
×
710
  }
711

712
  int32_t numOfStbs = -1;
974✔
713
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
974!
714
    goto _OVER;
×
715
  }
716

717
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
974!
718
    code = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
×
719
    goto _OVER;
×
720
  }
721

722
  SStbObj stbObj = {0};
974✔
723

724
  if (mndBuildStbFromReq(pMnode, &stbObj, &createReq, pDb) != 0) {
974!
725
    goto _OVER;
×
726
  }
727

728
  stbObj.uid = pStream->targetStbUid;
974✔
729

730
  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) {
974!
731
    mndFreeStb(&stbObj);
×
732
    goto _OVER;
×
733
  }
734

735
  tFreeSMCreateStbReq(&createReq);
974✔
736
  mndFreeStb(&stbObj);
974✔
737
  mndReleaseStb(pMnode, pStb);
974✔
738
  mndReleaseDb(pMnode, pDb);
974✔
739
  mDebug("stream:%s create dst stable:%s, cols:%d", pStream->name, pStream->targetSTbName, pStream->outputSchema.nCols);
974!
740
  return code;
974✔
741

742
_OVER:
×
743
  tFreeSMCreateStbReq(&createReq);
×
744
  mndReleaseStb(pMnode, pStb);
×
745
  mndReleaseDb(pMnode, pDb);
×
746

747
  mDebug("stream:%s failed to create dst stable:%s, line:%d code:%s", pStream->name, pStream->targetSTbName, lino,
×
748
         tstrerror(code));
749
  return code;
×
750
}
751

752
// 1. stream number check
753
// 2. target stable can not be target table of other existed streams.
754
static int32_t doStreamCheck(SMnode *pMnode, SStreamObj *pStreamObj) {
1,055✔
755
  int32_t     numOfStream = 0;
1,055✔
756
  SStreamObj *pStream = NULL;
1,055✔
757
  void       *pIter = NULL;
1,055✔
758

759
  while ((pIter = sdbFetch(pMnode->pSdb, SDB_STREAM, pIter, (void **)&pStream)) != NULL) {
2,903✔
760
    if (pStream->sourceDbUid == pStreamObj->sourceDbUid) {
1,849✔
761
      ++numOfStream;
1,153✔
762
    }
763

764

765
    if (numOfStream > MND_STREAM_MAX_NUM) {
1,849!
766
      mError("too many streams, no more than %d for each database, failed to create stream:%s", MND_STREAM_MAX_NUM,
×
767
             pStreamObj->name);
768
      sdbRelease(pMnode->pSdb, pStream);
×
769
      sdbCancelFetch(pMnode->pSdb, pIter);
×
770
      return TSDB_CODE_MND_TOO_MANY_STREAMS;
×
771
    }
772

773
    if (pStream->targetStbUid == pStreamObj->targetStbUid) {
1,849✔
774
      mError("Cannot write the same stable as other stream:%s, failed to create stream:%s", pStream->name,
1!
775
             pStreamObj->name);
776
      sdbRelease(pMnode->pSdb, pStream);
1✔
777
      sdbCancelFetch(pMnode->pSdb, pIter);
1✔
778
      return TSDB_CODE_MND_INVALID_TARGET_TABLE;
1✔
779
    }
780
    sdbRelease(pMnode->pSdb, pStream);
1,848✔
781
  }
782

783
  return TSDB_CODE_SUCCESS;
1,054✔
784
}
785

786
static void *notifyAddrDup(void *p) { return taosStrdup((char *)p); }
×
787

788
static int32_t addStreamTaskNotifyInfo(const SCMCreateStreamReq *createReq, const SStreamObj *pStream,
×
789
                                       SStreamTask *pTask) {
790
  int32_t code = TSDB_CODE_SUCCESS;
×
791
  int32_t lino = 0;
×
792

793
  TSDB_CHECK_NULL(createReq, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
794
  TSDB_CHECK_NULL(pTask, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
795

796
  pTask->notifyInfo.pNotifyAddrUrls = taosArrayDup(createReq->pNotifyAddrUrls, notifyAddrDup);
×
797
  TSDB_CHECK_NULL(pTask->notifyInfo.pNotifyAddrUrls, code, lino, _end, terrno);
×
798
  pTask->notifyInfo.notifyEventTypes = createReq->notifyEventTypes;
×
799
  pTask->notifyInfo.notifyErrorHandle = createReq->notifyErrorHandle;
×
800
  pTask->notifyInfo.streamName = taosStrdup(mndGetDbStr(createReq->name));
×
801
  TSDB_CHECK_NULL(pTask->notifyInfo.streamName, code, lino, _end, terrno);
×
802
  pTask->notifyInfo.stbFullName = taosStrdup(createReq->targetStbFullName);
×
803
  TSDB_CHECK_NULL(pTask->notifyInfo.stbFullName, code, lino, _end, terrno);
×
804
  pTask->notifyInfo.pSchemaWrapper = tCloneSSchemaWrapper(&pStream->outputSchema);
×
805
  TSDB_CHECK_NULL(pTask->notifyInfo.pSchemaWrapper, code, lino, _end, terrno);
×
806

807
_end:
×
808
  if (code != TSDB_CODE_SUCCESS) {
×
809
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
810
  }
811
  return code;
×
812
}
813

814
static int32_t addStreamNotifyInfo(SCMCreateStreamReq *createReq, SStreamObj *pStream) {
1,054✔
815
  int32_t code = TSDB_CODE_SUCCESS;
1,054✔
816
  int32_t lino = 0;
1,054✔
817
  int32_t level = 0;
1,054✔
818
  int32_t nTasks = 0;
1,054✔
819
  SArray *pLevel = NULL;
1,054✔
820

821
  TSDB_CHECK_NULL(createReq, code, lino, _end, TSDB_CODE_INVALID_PARA);
1,054!
822
  TSDB_CHECK_NULL(pStream, code, lino, _end, TSDB_CODE_INVALID_PARA);
1,054!
823

824
  if (taosArrayGetSize(createReq->pNotifyAddrUrls) == 0) {
1,054!
825
    goto _end;
1,054✔
826
  }
827

828
  level = taosArrayGetSize(pStream->pTaskList);
×
829
  for (int32_t i = 0; i < level; ++i) {
×
830
    pLevel = taosArrayGetP(pStream->pTaskList, i);
×
831
    nTasks = taosArrayGetSize(pLevel);
×
832
    for (int32_t j = 0; j < nTasks; ++j) {
×
833
      code = addStreamTaskNotifyInfo(createReq, pStream, taosArrayGetP(pLevel, j));
×
834
      TSDB_CHECK_CODE(code, lino, _end);
×
835
    }
836
  }
837

838
  if (pStream->conf.fillHistory && createReq->notifyHistory) {
×
839
    level = taosArrayGetSize(pStream->pHTaskList);
×
840
    for (int32_t i = 0; i < level; ++i) {
×
841
      pLevel = taosArrayGetP(pStream->pHTaskList, i);
×
842
      nTasks = taosArrayGetSize(pLevel);
×
843
      for (int32_t j = 0; j < nTasks; ++j) {
×
844
        code = addStreamTaskNotifyInfo(createReq, pStream, taosArrayGetP(pLevel, j));
×
845
        TSDB_CHECK_CODE(code, lino, _end);
×
846
      }
847
    }
848
  }
849

850
_end:
×
851
  if (code != TSDB_CODE_SUCCESS) {
1,054!
852
    mError("%s for stream %s failed at line %d since %s", __func__, pStream->name, lino, tstrerror(code));
×
853
  }
854
  return code;
1,054✔
855
}
856

857
static int32_t mndProcessCheckStreamStatusReq(SRpcMsg *pReq) {
1✔
858
  SMnode     *pMnode = pReq->info.node;
1✔
859
  SStreamObj *pStream = NULL;
1✔
860
  void       *pIter = NULL;
1✔
861

862
  while ((pIter = sdbFetch(pMnode->pSdb, SDB_STREAM, pIter, (void **)&pStream)) != NULL) {
2✔
863
    taosWLockLatch(&pStream->lock);
1✔
864
    if (pStream->status == STREAM_STATUS__INIT && (taosGetTimestampMs() - pStream->createTime > tsStreamFailedTimeout ||
2!
865
                                                   taosGetTimestampMs() - pStream->createTime < 0)){
×
866
      pStream->status = STREAM_STATUS__FAILED;
1✔
867
      tstrncpy(pStream->reserve, "timeout", sizeof(pStream->reserve));
1✔
868
      mInfo("stream:%s, set status to failed success because of timeout", pStream->name);
1!
869
    }
870
    taosWUnLockLatch(&pStream->lock);
1✔
871
    sdbRelease(pMnode->pSdb, pStream);
1✔
872
  }
873

874
  return 0;
1✔
875
}
876

877
static int32_t mndProcessFailedStreamReq(SRpcMsg *pReq) {
×
878
  SMnode     *pMnode = pReq->info.node;
×
879
  SStreamObj *pStream = NULL;
×
880
  int32_t     code = TSDB_CODE_SUCCESS;
×
881
  int32_t     errCode = *(int32_t*)pReq->pCont;
×
882
  char streamName[TSDB_STREAM_FNAME_LEN] = {0};
×
883
  memcpy(streamName, POINTER_SHIFT(pReq->pCont,INT_BYTES), TMIN(pReq->contLen - INT_BYTES, TSDB_STREAM_FNAME_LEN - 1));
×
884

885
#ifdef WINDOWS
886
  code = TSDB_CODE_MND_INVALID_PLATFORM;
887
  return code;
888
#endif
889

890
  mInfo("stream:%s, start to set stream failed", streamName);
×
891

892
  code = mndAcquireStream(pMnode, streamName, &pStream);
×
893
  if (pStream == NULL) {
×
894
    mError("stream:%s, failed to get stream when failed stream since %s", streamName, tstrerror(code));
×
895
    return code;
×
896
  }
897

898
  taosWLockLatch(&pStream->lock);
×
899
  pStream->status = STREAM_STATUS__FAILED;
×
900
  tstrncpy(pStream->reserve, tstrerror(errCode), sizeof(pStream->reserve));
×
901
  taosWUnLockLatch(&pStream->lock);
×
902
  mndReleaseStream(pMnode, pStream);
×
903

904
  mInfo("stream:%s, end to set stream failed success", streamName);
×
905

906
  return code;
×
907
}
908

909
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq) {
1,062✔
910
  SMnode     *pMnode = pReq->info.node;
1,062✔
911
  SStreamObj *pStream = NULL;
1,062✔
912
  SStreamObj  streamObj = {0};
1,062✔
913
  char       *sql = NULL;
1,062✔
914
  int32_t     sqlLen = 0;
1,062✔
915
  const char *pMsg = "create stream tasks on dnodes";
1,062✔
916
  int32_t     code = TSDB_CODE_SUCCESS;
1,062✔
917
  int32_t     lino = 0;
1,062✔
918
  STrans     *pTrans = NULL;
1,062✔
919

920
  SCMCreateStreamReq createReq = {0};
1,062✔
921
  code = tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, &createReq);
1,062✔
922
  TSDB_CHECK_CODE(code, lino, _OVER);
1,062!
923

924
#ifdef WINDOWS
925
  code = TSDB_CODE_MND_INVALID_PLATFORM;
926
  goto _OVER;
927
#endif
928

929
  mInfo("stream:%s, start to create stream, sql:%s", createReq.name, createReq.sql);
1,062!
930
  if ((code = mndCheckCreateStreamReq(&createReq)) != 0) {
1,062!
931
    mError("stream:%s, failed to create since %s", createReq.name, tstrerror(code));
×
932
    goto _OVER;
×
933
  }
934

935
  code = mndAcquireStream(pMnode, createReq.name, &pStream);
1,062✔
936
  if (pStream != NULL && code == 0) {
1,062!
937
    if (pStream->pTaskList != NULL){
2!
938
      if (createReq.igExists) {
2✔
939
        mInfo("stream:%s, already exist, ignore exist is set", createReq.name);
1!
940
        mndReleaseStream(pMnode, pStream);
1✔
941
        tFreeSCMCreateStreamReq(&createReq);
1✔
942
        return code;
1✔
943
      } else {
944
        code = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
1✔
945
        goto _OVER;
1✔
946
      }
947
    }
948
  } else if (code != TSDB_CODE_MND_STREAM_NOT_EXIST) {
1,060!
949
    goto _OVER;
×
950
  }
951

952
  if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
1,060✔
953
    goto _OVER;
3✔
954
  }
955

956
  if (createReq.sql != NULL) {
1,057!
957
    sql = taosStrdup(createReq.sql);
1,057!
958
    TSDB_CHECK_NULL(sql, code, lino, _OVER, terrno);
1,057!
959
  }
960

961
  // check for the taskEp update trans
962
  if (isNodeUpdateTransActive()) {
1,057!
963
    mError("stream:%s failed to create stream, node update trans is active", createReq.name);
×
964
    code = TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
965
    goto _OVER;
×
966
  }
967

968
  SDbObj *pSourceDb = mndAcquireDb(pMnode, createReq.sourceDB);
1,057✔
969
  if (pSourceDb == NULL) {
1,057!
970
    code = terrno;
×
971
    mInfo("stream:%s failed to create, acquire source db %s failed, code:%s", createReq.name, createReq.sourceDB,
×
972
          tstrerror(code));
973
    goto _OVER;
×
974
  }
975

976
  code = mndCheckForSnode(pMnode, pSourceDb);
1,057✔
977
  mndReleaseDb(pMnode, pSourceDb);
1,057✔
978
  if (code != 0) {
1,057✔
979
    goto _OVER;
2✔
980
  }
981

982
  // build stream obj from request
983
  if ((code = mndBuildStreamObjFromCreateReq(pMnode, &streamObj, &createReq)) < 0) {
1,055!
984
    mError("stream:%s, failed to create since %s", createReq.name, tstrerror(code));
×
985
    goto _OVER;
×
986
  }
987

988
  bool buildEmptyStream = false;
1,055✔
989
  if (createReq.lastTs == 0 && createReq.fillHistory != STREAM_FILL_HISTORY_OFF){
1,055!
990
    streamObj.status = STREAM_STATUS__INIT;
×
991
    buildEmptyStream = true;
×
992
  }
993

994
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_READ_DB, streamObj.sourceDb)) != 0) {
1,055!
995
    goto _OVER;
×
996
  }
997

998
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, streamObj.targetDb)) != 0) {
1,055!
999
    goto _OVER;
×
1000
  }
1001

1002
  code = doStreamCheck(pMnode, &streamObj);
1,055✔
1003
  TSDB_CHECK_CODE(code, lino, _OVER);
1,055✔
1004

1005
  // schedule stream task for stream obj
1006
  if (!buildEmptyStream) {
1,054!
1007
    code = mndScheduleStream(pMnode, &streamObj, &createReq);
1,054✔
1008
    if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,054!
1009
      mError("stream:%s, failed to schedule since %s", createReq.name, tstrerror(code));
×
1010
      mndTransDrop(pTrans);
×
1011
      goto _OVER;
×
1012
    }
1013

1014
    // add notify info into all stream tasks
1015
    code = addStreamNotifyInfo(&createReq, &streamObj);
1,054✔
1016
    if (code != TSDB_CODE_SUCCESS) {
1,054!
1017
      mError("stream:%s failed to add stream notify info since %s", createReq.name, tstrerror(code));
×
1018
      mndTransDrop(pTrans);
×
1019
      goto _OVER;
×
1020
    }
1021

1022
    // add into buffer firstly
1023
    // to make sure when the hb from vnode arrived, the newly created tasks have been in the task map already.
1024
    streamMutexLock(&execInfo.lock);
1,054✔
1025
    mDebug("stream stream:%s start to register tasks into task nodeList and set initial checkpointId", createReq.name);
1,054!
1026
    saveTaskAndNodeInfoIntoBuf(&streamObj, &execInfo);
1,054✔
1027
    streamMutexUnlock(&execInfo.lock);
1,054✔
1028
  }
1029

1030
  code = doCreateTrans(pMnode, &streamObj, pReq, TRN_CONFLICT_DB, MND_STREAM_CREATE_NAME, pMsg, &pTrans);
1,054✔
1031
  if (pTrans == NULL || code) {
1,054!
1032
    goto _OVER;
×
1033
  }
1034

1035
  // create stb for stream
1036
  if (createReq.createStb == STREAM_CREATE_STABLE_TRUE && !buildEmptyStream) {
1,054!
1037
    if ((code = mndCreateStbForStream(pMnode, pTrans, &streamObj, pReq->info.conn.user)) < 0) {
974!
1038
      mError("trans:%d, failed to create stb for stream %s since %s", pTrans->id, createReq.name, tstrerror(code));
×
1039
      goto _OVER;
×
1040
    }
1041
  } else {
1042
    mDebug("stream:%s no need create stable", createReq.name);
80!
1043
  }
1044

1045
  // add stream to trans
1046
  code = mndPersistStream(pTrans, &streamObj);
1,054✔
1047
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,054!
1048
    mError("stream:%s, failed to persist since %s", createReq.name, tstrerror(code));
×
1049
    goto _OVER;
×
1050
  }
1051

1052
  // execute creation
1053
  code = mndTransPrepare(pMnode, pTrans);
1,054✔
1054
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,054!
1055
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
1056
    goto _OVER;
×
1057
  }
1058

1059
  SName dbname = {0};
1,054✔
1060
  if (tNameFromString(&dbname, createReq.sourceDB, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE) != 0) {
1,054!
1061
    mError("invalid source dbname:%s in create stream, code:%s", createReq.sourceDB, tstrerror(code));
×
1062
  }
1063

1064
  SName name = {0};
1,054✔
1065
  if (tNameFromString(&name, createReq.name, T_NAME_ACCT | T_NAME_TABLE) != 0) {
1,054!
1066
    mError("invalid stream name:%s in create strem, code:%s", createReq.name, tstrerror(code));
×
1067
  }
1068

1069
  // reuse this function for stream
1070
  if (sql != NULL && sqlLen > 0) {
1,054!
1071
    auditRecord(pReq, pMnode->clusterId, "createStream", dbname.dbname, name.dbname, sql, sqlLen);
×
1072
  } else {
1073
    char detail[1000] = {0};
1,054✔
1074
    snprintf(detail, tListLen(detail), "dbname:%s, stream name:%s", dbname.dbname, name.dbname);
1,054✔
1075
    auditRecord(pReq, pMnode->clusterId, "createStream", dbname.dbname, name.dbname, detail, strlen(detail));
1,054✔
1076
  }
1077

1078
_OVER:
1,061✔
1079
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,061!
1080
    mError("stream:%s, failed to create at line:%d since %s", createReq.name, lino, tstrerror(code));
7!
1081
  } else {
1082
    mDebug("stream:%s create stream completed", createReq.name);
1,054!
1083
    code = TSDB_CODE_ACTION_IN_PROGRESS;
1,054✔
1084
  }
1085

1086
  mndTransDrop(pTrans);
1,061✔
1087
  mndReleaseStream(pMnode, pStream);
1,061✔
1088
  tFreeSCMCreateStreamReq(&createReq);
1,061✔
1089
  tFreeStreamObj(&streamObj);
1,061✔
1090

1091
  if (sql != NULL) {
1,061✔
1092
    taosMemoryFreeClear(sql);
1,057!
1093
  }
1094

1095
  return code;
1,061✔
1096
}
1097

1098
static int32_t mndProcessStopStreamReq(SRpcMsg *pReq) {
×
1099
  SMnode          *pMnode = pReq->info.node;
×
1100
  SStreamObj      *pStream = NULL;
×
1101
  int32_t          code = 0;
×
1102
  SMPauseStreamReq pauseReq = {0};
×
1103

1104
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
×
1105
    return TSDB_CODE_INVALID_MSG;
×
1106
  }
1107

1108
  code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
×
1109
  if (pStream == NULL || code != 0) {
×
1110
    if (pauseReq.igNotExists) {
×
1111
      mInfo("stream:%s, not exist, not restart stream", pauseReq.name);
×
1112
      return 0;
×
1113
    } else {
1114
      mError("stream:%s not exist, failed to restart stream", pauseReq.name);
×
1115
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1116
    }
1117
  }
1118

1119
  mInfo("stream:%s,%" PRId64 " start to restart stream", pauseReq.name, pStream->uid);
×
1120
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb)) != 0) {
×
1121
    sdbRelease(pMnode->pSdb, pStream);
×
1122
    return code;
×
1123
  }
1124

1125
  // check if it is conflict with other trans in both sourceDb and targetDb.
1126
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_STOP_NAME, true);
×
1127
  if (code) {
×
1128
    sdbRelease(pMnode->pSdb, pStream);
×
1129
    return code;
×
1130
  }
1131

1132
  bool updated = mndStreamNodeIsUpdated(pMnode);
×
1133
  if (updated) {
×
1134
    mError("tasks are not ready for restart, node update detected");
×
1135
    sdbRelease(pMnode->pSdb, pStream);
×
1136
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1137
  }
1138

1139
  STrans *pTrans = NULL;
×
1140
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_STOP_NAME, "stop the stream",
×
1141
                       &pTrans);
1142
  if (pTrans == NULL || code) {
×
1143
    mError("stream:%s failed to stop stream since %s", pauseReq.name, tstrerror(code));
×
1144
    sdbRelease(pMnode->pSdb, pStream);
×
1145
    return code;
×
1146
  }
1147

1148
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_STOP_NAME, pStream->uid);
×
1149
  if (code) {
×
1150
    sdbRelease(pMnode->pSdb, pStream);
×
1151
    mndTransDrop(pTrans);
×
1152
    return code;
×
1153
  }
1154

1155
  // if nodeUpdate happened, not send pause trans
1156
  code = mndStreamSetStopAction(pMnode, pTrans, pStream);
×
1157
  if (code) {
×
1158
    mError("stream:%s, failed to restart task since %s", pauseReq.name, tstrerror(code));
×
1159
    sdbRelease(pMnode->pSdb, pStream);
×
1160
    mndTransDrop(pTrans);
×
1161
    return code;
×
1162
  }
1163

1164
  code = mndTransPrepare(pMnode, pTrans);
×
1165
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
1166
    mError("trans:%d, failed to prepare restart stream trans since %s", pTrans->id, tstrerror(code));
×
1167
    sdbRelease(pMnode->pSdb, pStream);
×
1168
    mndTransDrop(pTrans);
×
1169
    return code;
×
1170
  }
1171

1172
  sdbRelease(pMnode->pSdb, pStream);
×
1173
  mndTransDrop(pTrans);
×
1174

1175
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
1176
}
1177

1178
int64_t mndStreamGenChkptId(SMnode *pMnode, bool lock) {
329✔
1179
  SStreamObj *pStream = NULL;
329✔
1180
  void       *pIter = NULL;
329✔
1181
  SSdb       *pSdb = pMnode->pSdb;
329✔
1182
  int64_t     maxChkptId = 0;
329✔
1183

1184
  while (1) {
1185
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
1,075✔
1186
    if (pIter == NULL) break;
1,075✔
1187

1188
    maxChkptId = TMAX(maxChkptId, pStream->checkpointId);
746✔
1189
    mDebug("stream:%p, %s id:0x%" PRIx64 " checkpoint %" PRId64, pStream, pStream->name, pStream->uid,
746!
1190
           pStream->checkpointId);
1191
    sdbRelease(pSdb, pStream);
746✔
1192
  }
1193

1194
  {  // check the max checkpoint id from all vnodes.
1195
    int64_t maxCheckpointId = -1;
329✔
1196
    if (lock) {
329✔
1197
      streamMutexLock(&execInfo.lock);
22✔
1198
    }
1199

1200
    for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
3,770✔
1201
      STaskId          *p = taosArrayGet(execInfo.pTaskList, i);
3,441✔
1202
      STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
3,441✔
1203
      if (p == NULL || pEntry == NULL) {
3,441!
1204
        continue;
×
1205
      }
1206

1207
      if (pEntry->checkpointInfo.failed) {
3,441!
1208
        continue;
×
1209
      }
1210

1211
      if (maxCheckpointId < pEntry->checkpointInfo.latestId) {
3,441✔
1212
        maxCheckpointId = pEntry->checkpointInfo.latestId;
354✔
1213
      }
1214
    }
1215

1216
    if (lock) {
329✔
1217
      streamMutexUnlock(&execInfo.lock);
22✔
1218
    }
1219

1220
    if (maxCheckpointId > maxChkptId) {
329!
1221
      mDebug("max checkpointId in mnode:%" PRId64 ", smaller than max checkpointId in vnode:%" PRId64, maxChkptId,
×
1222
             maxCheckpointId);
1223
      maxChkptId = maxCheckpointId;
×
1224
    }
1225
  }
1226

1227
  mDebug("generate new checkpointId:%" PRId64, maxChkptId + 1);
329!
1228
  return maxChkptId + 1;
329✔
1229
}
1230

1231
static int32_t mndProcessStreamCheckpointTrans(SMnode *pMnode, SStreamObj *pStream, int64_t checkpointId,
331✔
1232
                                               int8_t mndTrigger, bool lock) {
1233
  int32_t code = TSDB_CODE_SUCCESS;
331✔
1234
  bool    conflict = false;
331✔
1235
  int64_t ts = taosGetTimestampMs();
331✔
1236
  STrans *pTrans = NULL;
331✔
1237

1238
  if (mndTrigger == 1 && (ts - pStream->checkpointFreq < tsStreamCheckpointInterval * 1000)) {
331!
1239
    return code;
×
1240
  }
1241

1242
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHECKPOINT_NAME, lock);
331✔
1243
  if (code) {
331!
1244
    mWarn("checkpoint conflict with other trans in %s, code:%s ignore the checkpoint for stream:%s %" PRIx64,
×
1245
          pStream->sourceDb, tstrerror(code), pStream->name, pStream->uid);
1246
    goto _ERR;
×
1247
  }
1248

1249
  code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHECKPOINT_NAME,
331✔
1250
                       "gen checkpoint for stream", &pTrans);
1251
  if (code) {
331!
1252
    mError("failed to checkpoint of stream name%s, checkpointId: %" PRId64 ", reason:%s", pStream->name, checkpointId,
×
1253
           tstrerror(code));
1254
    goto _ERR;
×
1255
  }
1256

1257
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHECKPOINT_NAME, pStream->uid);
331✔
1258
  if (code) {
331!
1259
    mError("failed to register checkpoint trans for stream:%s, checkpointId:%" PRId64, pStream->name, checkpointId);
×
1260
    goto _ERR;
×
1261
  }
1262

1263
  mDebug("start to trigger checkpoint for stream:%s, checkpoint: %" PRId64, pStream->name, checkpointId);
331!
1264

1265
  taosWLockLatch(&pStream->lock);
331✔
1266
  pStream->currentTick = 1;
331✔
1267

1268
  // 1. redo action: broadcast checkpoint source msg for all source vg
1269
  int32_t totalLevel = taosArrayGetSize(pStream->pTaskList);
331✔
1270
  for (int32_t i = 0; i < totalLevel; i++) {
992✔
1271
    SArray      *pLevel = taosArrayGetP(pStream->pTaskList, i);
661✔
1272
    SStreamTask *p = taosArrayGetP(pLevel, 0);
661✔
1273

1274
    if (p->info.taskLevel == TASK_LEVEL__SOURCE) {
661✔
1275
      int32_t sz = taosArrayGetSize(pLevel);
331✔
1276
      for (int32_t j = 0; j < sz; j++) {
1,183✔
1277
        SStreamTask *pTask = taosArrayGetP(pLevel, j);
852✔
1278
        code = mndStreamSetCheckpointAction(pMnode, pTrans, pTask, checkpointId, mndTrigger);
852✔
1279

1280
        if (code != TSDB_CODE_SUCCESS) {
852!
1281
          taosWUnLockLatch(&pStream->lock);
×
1282
          goto _ERR;
×
1283
        }
1284
      }
1285
    }
1286
  }
1287

1288
  // 2. reset tick
1289
  pStream->checkpointId = checkpointId;
331✔
1290
  pStream->checkpointFreq = taosGetTimestampMs();
331✔
1291
  pStream->currentTick = 0;
331✔
1292

1293
  // 3. commit log: stream checkpoint info
1294
  pStream->version = pStream->version + 1;
331✔
1295
  taosWUnLockLatch(&pStream->lock);
331✔
1296

1297
  if ((code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY)) != TSDB_CODE_SUCCESS) {
331!
1298
    goto _ERR;
×
1299
  }
1300

1301
  code = mndTransPrepare(pMnode, pTrans);
331✔
1302
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
331!
1303
    mError("failed to prepare checkpoint trans since %s", tstrerror(code));
×
1304
  } else {
1305
    code = TSDB_CODE_ACTION_IN_PROGRESS;
331✔
1306
  }
1307

1308
_ERR:
331✔
1309
  mndTransDrop(pTrans);
331✔
1310
  return code;
331✔
1311
}
1312

1313
int32_t extractStreamNodeList(SMnode *pMnode) {
1,399✔
1314
  if (taosArrayGetSize(execInfo.pNodeList) == 0) {
1,399✔
1315
    int32_t code = refreshNodeListFromExistedStreams(pMnode, execInfo.pNodeList);
1,092✔
1316
    if (code) {
1,092!
1317
      mError("Failed to extract node list from stream, code:%s", tstrerror(code));
×
1318
      return code;
×
1319
    }
1320
  }
1321

1322
  return taosArrayGetSize(execInfo.pNodeList);
1,399✔
1323
}
1324

1325
static int32_t mndCheckTaskAndNodeStatus(SMnode *pMnode) {
1,361✔
1326
  int32_t code = 0;
1,361✔
1327
  if (mndStreamNodeIsUpdated(pMnode)) {
1,361✔
1328
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
27✔
1329
  }
1330

1331
  streamMutexLock(&execInfo.lock);
1,334✔
1332
  if (taosArrayGetSize(execInfo.pNodeList) == 0) {
1,334✔
1333
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
1,092✔
1334
    if (taosArrayGetSize(execInfo.pTaskList) != 0) {
1,092!
1335
      mError("stream task node change checking done, no vgroups exist, but task list is not empty");
×
1336
      code = TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1337
    }
1338
  }
1339

1340
  streamMutexUnlock(&execInfo.lock);
1,334✔
1341
  return code;
1,334✔
1342
}
1343

1344
int64_t getStreamTaskLastReadyState(SArray *pTaskList, int64_t streamId) {
618✔
1345
  int64_t ts = -1;
618✔
1346
  int32_t taskId = -1;
618✔
1347

1348
  for (int32_t i = 0; i < taosArrayGetSize(pTaskList); ++i) {
20,500✔
1349
    STaskId          *p = taosArrayGet(pTaskList, i);
19,973✔
1350
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
19,973✔
1351
    if (p == NULL || pEntry == NULL || pEntry->id.streamId != streamId) {
19,973!
1352
      continue;
17,649✔
1353
    }
1354

1355
    // -1 denote not ready now or never ready till now
1356
    if (pEntry->hTaskId != 0) {
2,324!
1357
      mInfo("s-task:0x%" PRIx64 "-0x%x (nodeId:%d) status:%s related fill-history task:0x%" PRIx64
×
1358
            " exists, checkpoint not issued",
1359
            pEntry->id.streamId, (int32_t)pEntry->id.taskId, pEntry->nodeId, streamTaskGetStatusStr(pEntry->status),
1360
            pEntry->hTaskId);
1361
      return -1;
×
1362
    }
1363

1364
    if (pEntry->status != TASK_STATUS__READY) {
2,324✔
1365
      mInfo("s-task:0x%" PRIx64 "-0x%x (nodeId:%d) status:%s, not ready for checkpoint", pEntry->id.streamId,
91!
1366
            (int32_t)pEntry->id.taskId, pEntry->nodeId, streamTaskGetStatusStr(pEntry->status));
1367
      return -1;
91✔
1368
    }
1369

1370
    if (ts < pEntry->startTime) {
2,233✔
1371
      ts = pEntry->startTime;
1,219✔
1372
      taskId = pEntry->id.taskId;
1,219✔
1373
    }
1374
  }
1375

1376
  mDebug("stream:0x%" PRIx64 " last ready ts:%" PRId64 " s-task:0x%x", streamId, ts, taskId);
527!
1377
  return ts;
527✔
1378
}
1379

1380
typedef struct {
1381
  int64_t streamId;
1382
  int64_t duration;
1383
} SCheckpointInterval;
1384

1385
static int32_t streamWaitComparFn(const void *p1, const void *p2) {
81✔
1386
  const SCheckpointInterval *pInt1 = p1;
81✔
1387
  const SCheckpointInterval *pInt2 = p2;
81✔
1388
  if (pInt1->duration == pInt2->duration) {
81✔
1389
    return 0;
67✔
1390
  }
1391

1392
  return pInt1->duration > pInt2->duration ? -1 : 1;
14✔
1393
}
1394

1395
// all tasks of this stream should be ready, otherwise do nothing
1396
static bool isStreamReadyHelp(int64_t now, SStreamObj *pStream) {
618✔
1397
  bool ready = false;
618✔
1398

1399
  streamMutexLock(&execInfo.lock);
618✔
1400

1401
  int64_t lastReadyTs = getStreamTaskLastReadyState(execInfo.pTaskList, pStream->uid);
618✔
1402
  if ((lastReadyTs == -1) || ((lastReadyTs != -1) && ((now - lastReadyTs) < tsStreamCheckpointInterval * 1000))) {
618!
1403
    if (lastReadyTs != -1) {
543✔
1404
      mInfo("not start checkpoint, stream:0x%" PRIx64 " readyTs:%" PRId64 " ready duration:%.2fs less than threshold",
450!
1405
            pStream->uid, lastReadyTs, (now - lastReadyTs) / 1000.0);
1406
    }
1407

1408
    ready = false;
543✔
1409
  } else {
1410
    ready = true;
75✔
1411
  }
1412

1413
  streamMutexUnlock(&execInfo.lock);
618✔
1414
  return ready;
618✔
1415
}
1416

1417
static int32_t mndProcessStreamCheckpoint(SRpcMsg *pReq) {
1,361✔
1418
  SMnode     *pMnode = pReq->info.node;
1,361✔
1419
  SSdb       *pSdb = pMnode->pSdb;
1,361✔
1420
  void       *pIter = NULL;
1,361✔
1421
  SStreamObj *pStream = NULL;
1,361✔
1422
  int32_t     code = 0;
1,361✔
1423
  int32_t     numOfCheckpointTrans = 0;
1,361✔
1424
  SArray     *pLongChkpts = NULL;
1,361✔
1425
  SArray     *pList = NULL;
1,361✔
1426
  int64_t     now = taosGetTimestampMs();
1,361✔
1427

1428
  if ((code = mndCheckTaskAndNodeStatus(pMnode)) != 0) {
1,361✔
1429
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
27✔
1430
  }
1431

1432
  pList = taosArrayInit(4, sizeof(SCheckpointInterval));
1,334✔
1433
  if (pList == NULL) {
1,334!
1434
    mError("failed to init chkptInterval info, not handle stream checkpoint, code:%s", tstrerror(terrno));
×
1435
    return terrno;
×
1436
  }
1437

1438
  pLongChkpts = taosArrayInit(4, sizeof(SStreamTransInfo));
1,334✔
1439
  if (pLongChkpts == NULL) {
1,334!
1440
    mError("failed to init long checkpoint list, not handle stream checkpoint, code:%s", tstrerror(terrno));
×
1441
    taosArrayDestroy(pList);
×
1442
    return terrno;
×
1443
  }
1444

1445
  // check if ongong checkpoint trans or long chkpt trans exist.
1446
  code = mndStreamClearFinishedTrans(pMnode, &numOfCheckpointTrans, pLongChkpts);
1,334✔
1447
  if (code) {
1,334!
1448
    mError("failed to clear finish trans, code:%s", tstrerror(code));
×
1449

1450
    taosArrayDestroy(pList);
×
1451
    taosArrayDestroy(pLongChkpts);
×
1452
    return code;
×
1453
  }
1454

1455
  // kill long exec checkpoint and set task status
1456
  if (taosArrayGetSize(pLongChkpts) > 0) {
1,334!
1457
    killChkptAndResetStreamTask(pMnode, pLongChkpts);
×
1458

1459
    taosArrayDestroy(pList);
×
1460
    taosArrayDestroy(pLongChkpts);
×
1461
    return TSDB_CODE_SUCCESS;
×
1462
  }
1463

1464
  taosArrayDestroy(pLongChkpts);
1,334✔
1465

1466
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream)) != NULL) {
2,074✔
1467
    int64_t duration = now - pStream->checkpointFreq;
740✔
1468
    if (duration < tsStreamCheckpointInterval * 1000) {
740✔
1469
      sdbRelease(pSdb, pStream);
122✔
1470
      continue;
665✔
1471
    }
1472

1473
    bool ready = isStreamReadyHelp(now, pStream);
618✔
1474
    if (!ready) {
618✔
1475
      sdbRelease(pSdb, pStream);
543✔
1476
      continue;
543✔
1477
    }
1478

1479
    SCheckpointInterval in = {.streamId = pStream->uid, .duration = duration};
75✔
1480
    void               *p = taosArrayPush(pList, &in);
75✔
1481
    if (p) {
75!
1482
      int32_t currentSize = taosArrayGetSize(pList);
75✔
1483
      mDebug("stream:%s (uid:0x%" PRIx64 ") total %d stream(s) beyond chkpt interval threshold: %ds(%" PRId64
75!
1484
             "s), concurrently launch threshold:%d",
1485
             pStream->name, pStream->uid, currentSize, tsStreamCheckpointInterval, duration / 1000,
1486
             tsMaxConcurrentCheckpoint);
1487
    } else {
1488
      mError("failed to record the checkpoint interval info, stream:0x%" PRIx64, pStream->uid);
×
1489
    }
1490
    sdbRelease(pSdb, pStream);
75✔
1491
  }
1492

1493
  int32_t size = taosArrayGetSize(pList);
1,334✔
1494
  if (size == 0) {
1,334✔
1495
    taosArrayDestroy(pList);
1,312✔
1496
    return code;
1,312✔
1497
  }
1498

1499
  taosArraySort(pList, streamWaitComparFn);
22✔
1500

1501
  int32_t numOfQual = taosArrayGetSize(pList);
22✔
1502
  if (numOfCheckpointTrans >= tsMaxConcurrentCheckpoint) {
22!
1503
    mDebug(
×
1504
        "%d stream(s) checkpoint interval longer than %ds, ongoing checkpoint trans:%d reach maximum allowed:%d, new "
1505
        "checkpoint trans are not allowed, wait for 30s",
1506
        numOfQual, tsStreamCheckpointInterval, numOfCheckpointTrans, tsMaxConcurrentCheckpoint);
1507
    taosArrayDestroy(pList);
×
1508
    return code;
×
1509
  }
1510

1511
  int32_t capacity = tsMaxConcurrentCheckpoint - numOfCheckpointTrans;
22✔
1512
  mDebug(
22!
1513
      "%d stream(s) checkpoint interval longer than %ds, %d ongoing checkpoint trans, %d new checkpoint trans allowed, "
1514
      "concurrent trans threshold:%d",
1515
      numOfQual, tsStreamCheckpointInterval, numOfCheckpointTrans, capacity, tsMaxConcurrentCheckpoint);
1516

1517
  int32_t started = 0;
22✔
1518
  int64_t checkpointId = mndStreamGenChkptId(pMnode, true);
22✔
1519

1520
  for (int32_t i = 0; i < numOfQual; ++i) {
26✔
1521
    SCheckpointInterval *pCheckpointInfo = taosArrayGet(pList, i);
24✔
1522
    if (pCheckpointInfo == NULL) {
24!
1523
      continue;
×
1524
    }
1525

1526
    SStreamObj *p = NULL;
24✔
1527
    code = mndGetStreamObj(pMnode, pCheckpointInfo->streamId, &p);
24✔
1528
    if (p != NULL && code == 0) {
24!
1529
      code = mndProcessStreamCheckpointTrans(pMnode, p, checkpointId, 1, true);
24✔
1530
      sdbRelease(pSdb, p);
24✔
1531

1532
      if (code == 0 || code == TSDB_CODE_ACTION_IN_PROGRESS) {
24!
1533
        started += 1;
24✔
1534

1535
        if (started >= capacity) {
24✔
1536
          mDebug("already start %d new checkpoint trans, current active checkpoint trans:%d", started,
20!
1537
                 (started + numOfCheckpointTrans));
1538
          break;
20✔
1539
        }
1540
      } else {
1541
        mError("failed to start checkpoint trans, code:%s", tstrerror(code));
×
1542
      }
1543
    }
1544
  }
1545

1546
  taosArrayDestroy(pList);
22✔
1547
  return code;
22✔
1548
}
1549

1550
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) {
690✔
1551
  SMnode     *pMnode = pReq->info.node;
690✔
1552
  SStreamObj *pStream = NULL;
690✔
1553
  int32_t     code = 0;
690✔
1554

1555
  SMDropStreamReq dropReq = {0};
690✔
1556
  if (tDeserializeSMDropStreamReq(pReq->pCont, pReq->contLen, &dropReq) < 0) {
690!
1557
    mError("invalid drop stream msg recv, discarded");
×
1558
    code = TSDB_CODE_INVALID_MSG;
×
1559
    TAOS_RETURN(code);
×
1560
  }
1561

1562
  mDebug("recv drop stream:%s msg", dropReq.name);
690!
1563

1564
  code = mndAcquireStream(pMnode, dropReq.name, &pStream);
690✔
1565
  if (pStream == NULL || code != 0) {
690!
1566
    if (dropReq.igNotExists) {
122!
1567
      mInfo("stream:%s not exist, ignore not exist is set, drop stream exec done with success", dropReq.name);
122!
1568
      sdbRelease(pMnode->pSdb, pStream);
122✔
1569
      tFreeMDropStreamReq(&dropReq);
122✔
1570
      return 0;
122✔
1571
    } else {
1572
      mError("stream:%s not exist failed to drop it", dropReq.name);
×
1573
      tFreeMDropStreamReq(&dropReq);
×
1574
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1575
    }
1576
  }
1577

1578
  if (pStream->smaId != 0) {
568✔
1579
    mDebug("stream:%s, uid:0x%" PRIx64 " try to drop sma related stream", dropReq.name, pStream->uid);
89!
1580

1581
    void    *pIter = NULL;
89✔
1582
    SSmaObj *pSma = NULL;
89✔
1583
    pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
89✔
1584
    while (pIter) {
121✔
1585
      if (pSma && pSma->uid == pStream->smaId) {
32!
1586
        sdbRelease(pMnode->pSdb, pSma);
×
1587
        sdbRelease(pMnode->pSdb, pStream);
×
1588

1589
        sdbCancelFetch(pMnode->pSdb, pIter);
×
1590
        tFreeMDropStreamReq(&dropReq);
×
1591
        code = TSDB_CODE_TSMA_MUST_BE_DROPPED;
×
1592

1593
        mError("try to drop sma-related stream:%s, uid:0x%" PRIx64 " code:%s only allowed to be dropped along with sma",
×
1594
               dropReq.name, pStream->uid, tstrerror(terrno));
1595
        TAOS_RETURN(code);
×
1596
      }
1597

1598
      if (pSma) {
32!
1599
        sdbRelease(pMnode->pSdb, pSma);
32✔
1600
      }
1601

1602
      pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
32✔
1603
    }
1604
  }
1605

1606
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
568!
1607
    sdbRelease(pMnode->pSdb, pStream);
×
1608
    tFreeMDropStreamReq(&dropReq);
×
1609
    return -1;
×
1610
  }
1611

1612
  // check if it is conflict with other trans in both sourceDb and targetDb.
1613
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_DROP_NAME, true);
568✔
1614
  if (code) {
568!
1615
    sdbRelease(pMnode->pSdb, pStream);
×
1616
    tFreeMDropStreamReq(&dropReq);
×
1617
    return code;
×
1618
  }
1619

1620
  STrans *pTrans = NULL;
568✔
1621
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, "drop stream", &pTrans);
568✔
1622
  if (pTrans == NULL || code) {
568!
1623
    mError("stream:%s uid:0x%" PRIx64 " failed to drop since %s", dropReq.name, pStream->uid, tstrerror(code));
×
1624
    sdbRelease(pMnode->pSdb, pStream);
×
1625
    tFreeMDropStreamReq(&dropReq);
×
1626
    TAOS_RETURN(code);
×
1627
  }
1628

1629
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_DROP_NAME, pStream->uid);
568✔
1630
  if (code) {
568!
1631
    mError("failed to register drop stream trans, code:%s", tstrerror(code));
×
1632
    sdbRelease(pMnode->pSdb, pStream);
×
1633
    mndTransDrop(pTrans);
×
1634
    tFreeMDropStreamReq(&dropReq);
×
1635
    TAOS_RETURN(code);
×
1636
  }
1637

1638
  // drop all tasks
1639
  code = mndStreamSetDropAction(pMnode, pTrans, pStream);
568✔
1640
  if (code) {
568!
1641
    mError("stream:%s uid:0x%" PRIx64 " failed to drop task since %s", dropReq.name, pStream->uid, tstrerror(code));
×
1642
    sdbRelease(pMnode->pSdb, pStream);
×
1643
    mndTransDrop(pTrans);
×
1644
    tFreeMDropStreamReq(&dropReq);
×
1645
    TAOS_RETURN(code);
×
1646
  }
1647

1648
  // drop stream
1649
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_DROPPED);
568✔
1650
  if (code) {
568!
1651
    sdbRelease(pMnode->pSdb, pStream);
×
1652
    mndTransDrop(pTrans);
×
1653
    tFreeMDropStreamReq(&dropReq);
×
1654
    TAOS_RETURN(code);
×
1655
  }
1656

1657
  code = mndTransPrepare(pMnode, pTrans);
568✔
1658
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
568!
1659
    mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
1660
    sdbRelease(pMnode->pSdb, pStream);
×
1661
    mndTransDrop(pTrans);
×
1662
    tFreeMDropStreamReq(&dropReq);
×
1663
    TAOS_RETURN(code);
×
1664
  }
1665

1666
  // kill the related checkpoint trans
1667
  int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
568✔
1668
  if (transId != 0) {
568!
1669
    mDebug("drop active transId:%d due to stream:%s uid:0x%" PRIx64 " dropped", transId, pStream->name, pStream->uid);
×
1670
    mndKillTransImpl(pMnode, transId, pStream->sourceDb);
×
1671
  }
1672

1673
  mDebug("stream:%s uid:0x%" PRIx64 " transId:%d start to drop related task when dropping stream", dropReq.name,
568!
1674
         pStream->uid, transId);
1675

1676
  removeStreamTasksInBuf(pStream, &execInfo);
568✔
1677

1678
  SName name = {0};
568✔
1679
  code = tNameFromString(&name, dropReq.name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
568✔
1680
  auditRecord(pReq, pMnode->clusterId, "dropStream", "", name.dbname, dropReq.sql, dropReq.sqlLen);
568✔
1681

1682
  sdbRelease(pMnode->pSdb, pStream);
568✔
1683
  mndTransDrop(pTrans);
568✔
1684
  tFreeMDropStreamReq(&dropReq);
568✔
1685

1686
  if (code == 0) {
568!
1687
    return TSDB_CODE_ACTION_IN_PROGRESS;
568✔
1688
  } else {
1689
    TAOS_RETURN(code);
×
1690
  }
1691
}
1692

1693
int32_t mndDropStreamByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
2,078✔
1694
  SSdb   *pSdb = pMnode->pSdb;
2,078✔
1695
  void   *pIter = NULL;
2,078✔
1696
  int32_t code = 0;
2,078✔
1697

1698
  while (1) {
66✔
1699
    SStreamObj *pStream = NULL;
2,144✔
1700
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
2,144✔
1701
    if (pIter == NULL) break;
2,144✔
1702

1703
    if (pStream->sourceDbUid == pDb->uid || pStream->targetDbUid == pDb->uid) {
66!
1704
      if (pStream->sourceDbUid != pStream->targetDbUid) {
53!
1705
        sdbRelease(pSdb, pStream);
×
1706
        sdbCancelFetch(pSdb, pIter);
×
1707
        mError("db:%s, failed to drop stream:%s since sourceDbUid:%" PRId64 " not match with targetDbUid:%" PRId64,
×
1708
               pDb->name, pStream->name, pStream->sourceDbUid, pStream->targetDbUid);
1709
        TAOS_RETURN(TSDB_CODE_MND_STREAM_MUST_BE_DELETED);
×
1710
      } else {
1711
        // kill the related checkpoint trans
1712
        int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
53✔
1713
        if (transId != 0) {
53!
1714
          mDebug("drop active related transId:%d due to stream:%s dropped", transId, pStream->name);
×
1715
          mndKillTransImpl(pMnode, transId, pStream->sourceDb);
×
1716
        }
1717

1718
        // drop the stream obj in execInfo
1719
        removeStreamTasksInBuf(pStream, &execInfo);
53✔
1720

1721
        code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_DROPPED);
53✔
1722
        if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
53!
1723
          sdbRelease(pSdb, pStream);
×
1724
          sdbCancelFetch(pSdb, pIter);
×
1725
          return code;
×
1726
        }
1727
      }
1728
    }
1729

1730
    sdbRelease(pSdb, pStream);
66✔
1731
  }
1732

1733
  return 0;
2,078✔
1734
}
1735

1736
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
235✔
1737
  SMnode     *pMnode = pReq->info.node;
235✔
1738
  SSdb       *pSdb = pMnode->pSdb;
235✔
1739
  int32_t     numOfRows = 0;
235✔
1740
  SStreamObj *pStream = NULL;
235✔
1741
  int32_t     code = 0;
235✔
1742

1743
  while (numOfRows < rows) {
609!
1744
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
609✔
1745
    if (pShow->pIter == NULL) break;
609✔
1746

1747
    code = setStreamAttrInResBlock(pStream, pBlock, numOfRows);
374✔
1748
    if (code == 0) {
374!
1749
      numOfRows++;
374✔
1750
    }
1751
    sdbRelease(pSdb, pStream);
374✔
1752
  }
1753

1754
  pShow->numOfRows += numOfRows;
235✔
1755
  return numOfRows;
235✔
1756
}
1757

1758
static void mndCancelGetNextStream(SMnode *pMnode, void *pIter) {
×
1759
  SSdb *pSdb = pMnode->pSdb;
×
1760
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
1761
}
×
1762

1763
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
3,854✔
1764
  SMnode     *pMnode = pReq->info.node;
3,854✔
1765
  SSdb       *pSdb = pMnode->pSdb;
3,854✔
1766
  int32_t     numOfRows = 0;
3,854✔
1767
  SStreamObj *pStream = NULL;
3,854✔
1768
  int32_t     code = 0;
3,854✔
1769

1770
  streamMutexLock(&execInfo.lock);
3,854✔
1771
  mndInitStreamExecInfo(pMnode, &execInfo);
3,854✔
1772
  streamMutexUnlock(&execInfo.lock);
3,854✔
1773

1774
  while (numOfRows < rowsCapacity) {
17,433✔
1775
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
17,383✔
1776
    if (pShow->pIter == NULL) {
17,383✔
1777
      break;
3,804✔
1778
    }
1779

1780
    // lock
1781
    taosRLockLatch(&pStream->lock);
13,579✔
1782

1783
    int32_t count = mndGetNumOfStreamTasks(pStream);
13,579✔
1784
    if (numOfRows + count > rowsCapacity) {
13,579✔
1785
      code = blockDataEnsureCapacity(pBlock, numOfRows + count);
40✔
1786
      if (code) {
40!
1787
        mError("failed to prepare the result block buffer, quit return value");
×
1788
        taosRUnLockLatch(&pStream->lock);
×
1789
        sdbRelease(pSdb, pStream);
×
1790
        continue;
×
1791
      }
1792
    }
1793

1794
    int32_t precision = TSDB_TIME_PRECISION_MILLI;
13,579✔
1795
    SDbObj *pSourceDb = mndAcquireDb(pMnode, pStream->sourceDb);
13,579✔
1796
    if (pSourceDb != NULL) {
13,579!
1797
      precision = pSourceDb->cfg.precision;
13,579✔
1798
      mndReleaseDb(pMnode, pSourceDb);
13,579✔
1799
    }
1800

1801
    // add row for each task
1802
    SStreamTaskIter *pIter = NULL;
13,579✔
1803
    code = createStreamTaskIter(pStream, &pIter);
13,579✔
1804
    if (code) {
13,579!
1805
      taosRUnLockLatch(&pStream->lock);
×
1806
      sdbRelease(pSdb, pStream);
×
1807
      mError("failed to create task iter for stream:%s", pStream->name);
×
1808
      continue;
×
1809
    }
1810

1811
    while (streamTaskIterNextTask(pIter)) {
61,124✔
1812
      SStreamTask *pTask = NULL;
47,545✔
1813
      code = streamTaskIterGetCurrent(pIter, &pTask);
47,545✔
1814
      if (code) {
47,545!
1815
        destroyStreamTaskIter(pIter);
×
1816
        break;
×
1817
      }
1818

1819
      code = setTaskAttrInResBlock(pStream, pTask, pBlock, numOfRows, precision);
47,545✔
1820
      if (code == TSDB_CODE_SUCCESS) {
47,545!
1821
        numOfRows++;
47,545✔
1822
      }
1823
    }
1824

1825
    pBlock->info.rows = numOfRows;
13,579✔
1826

1827
    destroyStreamTaskIter(pIter);
13,579✔
1828
    taosRUnLockLatch(&pStream->lock);
13,579✔
1829

1830
    sdbRelease(pSdb, pStream);
13,579✔
1831
  }
1832

1833
  pShow->numOfRows += numOfRows;
3,854✔
1834
  return numOfRows;
3,854✔
1835
}
1836

1837
static void mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter) {
×
1838
  SSdb *pSdb = pMnode->pSdb;
×
1839
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
1840
}
×
1841

1842
static int32_t mndProcessPauseStreamReq(SRpcMsg *pReq) {
30✔
1843
  SMnode     *pMnode = pReq->info.node;
30✔
1844
  SStreamObj *pStream = NULL;
30✔
1845
  int32_t     code = 0;
30✔
1846

1847
  SMPauseStreamReq pauseReq = {0};
30✔
1848
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
30!
1849
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1850
  }
1851

1852
  code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
30✔
1853
  if (pStream == NULL || code != 0) {
30!
1854
    if (pauseReq.igNotExists) {
2✔
1855
      mInfo("stream:%s, not exist, not pause stream", pauseReq.name);
1!
1856
      return 0;
1✔
1857
    } else {
1858
      mError("stream:%s not exist, failed to pause stream", pauseReq.name);
1!
1859
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
1✔
1860
    }
1861
  }
1862

1863
  mInfo("stream:%s,%" PRId64 " start to pause stream", pauseReq.name, pStream->uid);
28!
1864

1865
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb)) != 0) {
28!
1866
    sdbRelease(pMnode->pSdb, pStream);
×
1867
    return code;
×
1868
  }
1869

1870
  // check if it is conflict with other trans in both sourceDb and targetDb.
1871
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_PAUSE_NAME, true);
28✔
1872
  if (code) {
28!
1873
    sdbRelease(pMnode->pSdb, pStream);
×
1874
    TAOS_RETURN(code);
×
1875
  }
1876

1877
  bool updated = mndStreamNodeIsUpdated(pMnode);
28✔
1878
  if (updated) {
28!
1879
    mError("tasks are not ready for pause, node update detected");
×
1880
    sdbRelease(pMnode->pSdb, pStream);
×
1881
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1882
  }
1883

1884
  {  // check for tasks, if tasks are not ready, not allowed to pause
1885
    bool found = false;
28✔
1886
    bool readyToPause = true;
28✔
1887
    streamMutexLock(&execInfo.lock);
28✔
1888

1889
    for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
211✔
1890
      STaskId *p = taosArrayGet(execInfo.pTaskList, i);
183✔
1891
      if (p == NULL) {
183!
1892
        continue;
×
1893
      }
1894

1895
      STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
183✔
1896
      if (pEntry == NULL) {
183!
1897
        continue;
×
1898
      }
1899

1900
      if (pEntry->id.streamId != pStream->uid) {
183✔
1901
        continue;
27✔
1902
      }
1903

1904
      if (pEntry->status == TASK_STATUS__UNINIT || pEntry->status == TASK_STATUS__CK) {
156!
1905
        mError("stream:%s uid:0x%" PRIx64 " vgId:%d task:0x%" PRIx64 " status:%s, not ready for pause", pStream->name,
×
1906
               pStream->uid, pEntry->nodeId, pEntry->id.taskId, streamTaskGetStatusStr(pEntry->status));
1907
        readyToPause = false;
×
1908
      }
1909

1910
      found = true;
156✔
1911
    }
1912

1913
    streamMutexUnlock(&execInfo.lock);
28✔
1914
    if (!found) {
28!
1915
      mError("stream:%s task not report status yet, not ready for pause", pauseReq.name);
×
1916
      sdbRelease(pMnode->pSdb, pStream);
×
1917
      TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1918
    }
1919

1920
    if (!readyToPause) {
28!
1921
      mError("stream:%s task not ready for pause yet", pauseReq.name);
×
1922
      sdbRelease(pMnode->pSdb, pStream);
×
1923
      TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1924
    }
1925
  }
1926

1927
  STrans *pTrans = NULL;
28✔
1928
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_PAUSE_NAME, "pause the stream", &pTrans);
28✔
1929
  if (pTrans == NULL || code) {
28!
1930
    mError("stream:%s failed to pause stream since %s", pauseReq.name, tstrerror(code));
×
1931
    sdbRelease(pMnode->pSdb, pStream);
×
1932
    return code;
×
1933
  }
1934

1935
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_PAUSE_NAME, pStream->uid);
28✔
1936
  if (code) {
28!
1937
    sdbRelease(pMnode->pSdb, pStream);
×
1938
    mndTransDrop(pTrans);
×
1939
    return code;
×
1940
  }
1941

1942
  // if nodeUpdate happened, not send pause trans
1943
  code = mndStreamSetPauseAction(pMnode, pTrans, pStream);
28✔
1944
  if (code) {
28!
1945
    mError("stream:%s, failed to pause task since %s", pauseReq.name, tstrerror(code));
×
1946
    sdbRelease(pMnode->pSdb, pStream);
×
1947
    mndTransDrop(pTrans);
×
1948
    return code;
×
1949
  }
1950

1951
  // pause stream
1952
  taosWLockLatch(&pStream->lock);
28✔
1953
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
28✔
1954
  if (code) {
28!
1955
    taosWUnLockLatch(&pStream->lock);
×
1956
    sdbRelease(pMnode->pSdb, pStream);
×
1957
    mndTransDrop(pTrans);
×
1958
    return code;
×
1959
  }
1960

1961
  taosWUnLockLatch(&pStream->lock);
28✔
1962

1963
  code = mndTransPrepare(pMnode, pTrans);
28✔
1964
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
28!
1965
    mError("trans:%d, failed to prepare pause stream trans since %s", pTrans->id, tstrerror(code));
×
1966
    sdbRelease(pMnode->pSdb, pStream);
×
1967
    mndTransDrop(pTrans);
×
1968
    return code;
×
1969
  }
1970

1971
  sdbRelease(pMnode->pSdb, pStream);
28✔
1972
  mndTransDrop(pTrans);
28✔
1973

1974
  return TSDB_CODE_ACTION_IN_PROGRESS;
28✔
1975
}
1976

1977
static int32_t mndProcessResumeStreamReq(SRpcMsg *pReq) {
29✔
1978
  SMnode     *pMnode = pReq->info.node;
29✔
1979
  SStreamObj *pStream = NULL;
29✔
1980
  int32_t     code = 0;
29✔
1981

1982
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
29!
1983
    return code;
×
1984
  }
1985

1986
  SMResumeStreamReq resumeReq = {0};
29✔
1987
  if (tDeserializeSMResumeStreamReq(pReq->pCont, pReq->contLen, &resumeReq) < 0) {
29!
1988
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1989
  }
1990

1991
  code = mndAcquireStream(pMnode, resumeReq.name, &pStream);
29✔
1992
  if (pStream == NULL || code != 0) {
29!
1993
    if (resumeReq.igNotExists) {
2✔
1994
      mInfo("stream:%s not exist, not resume stream", resumeReq.name);
1!
1995
      sdbRelease(pMnode->pSdb, pStream);
1✔
1996
      return 0;
1✔
1997
    } else {
1998
      mError("stream:%s not exist, failed to resume stream", resumeReq.name);
1!
1999
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
1✔
2000
    }
2001
  }
2002

2003
  mInfo("stream:%s,%" PRId64 " start to resume stream from pause", resumeReq.name, pStream->uid);
27!
2004
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
27!
2005
    sdbRelease(pMnode->pSdb, pStream);
×
2006
    return -1;
×
2007
  }
2008

2009
  // check if it is conflict with other trans in both sourceDb and targetDb.
2010
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_RESUME_NAME, true);
27✔
2011
  if (code) {
27!
2012
    sdbRelease(pMnode->pSdb, pStream);
×
2013
    return code;
×
2014
  }
2015

2016
  STrans *pTrans = NULL;
27✔
2017
  code =
2018
      doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RESUME_NAME, "resume the stream", &pTrans);
27✔
2019
  if (pTrans == NULL || code) {
27!
2020
    mError("stream:%s, failed to resume stream since %s", resumeReq.name, tstrerror(code));
×
2021
    sdbRelease(pMnode->pSdb, pStream);
×
2022
    return code;
×
2023
  }
2024

2025
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_RESUME_NAME, pStream->uid);
27✔
2026
  if (code) {
27!
2027
    sdbRelease(pMnode->pSdb, pStream);
×
2028
    mndTransDrop(pTrans);
×
2029
    return code;
×
2030
  }
2031

2032
  // set the resume action
2033
  code = mndStreamSetResumeAction(pTrans, pMnode, pStream, resumeReq.igUntreated);
27✔
2034
  if (code) {
27!
2035
    mError("stream:%s, failed to drop task since %s", resumeReq.name, tstrerror(code));
×
2036
    sdbRelease(pMnode->pSdb, pStream);
×
2037
    mndTransDrop(pTrans);
×
2038
    return code;
×
2039
  }
2040

2041
  // resume stream
2042
  taosWLockLatch(&pStream->lock);
27✔
2043
  pStream->status = STREAM_STATUS__NORMAL;
27✔
2044
  if (mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY) < 0) {
27!
2045
    taosWUnLockLatch(&pStream->lock);
×
2046

2047
    sdbRelease(pMnode->pSdb, pStream);
×
2048
    mndTransDrop(pTrans);
×
2049
    return code;
×
2050
  }
2051

2052
  taosWUnLockLatch(&pStream->lock);
27✔
2053
  code = mndTransPrepare(pMnode, pTrans);
27✔
2054
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
27!
2055
    mError("trans:%d, failed to prepare pause stream trans since %s", pTrans->id, tstrerror(code));
×
2056
    sdbRelease(pMnode->pSdb, pStream);
×
2057
    mndTransDrop(pTrans);
×
2058
    return code;
×
2059
  }
2060

2061
  sdbRelease(pMnode->pSdb, pStream);
27✔
2062
  mndTransDrop(pTrans);
27✔
2063

2064
  return TSDB_CODE_ACTION_IN_PROGRESS;
27✔
2065
}
2066

2067
static int32_t mndProcessResetStreamReq(SRpcMsg *pReq) {
×
2068
  SMnode     *pMnode = pReq->info.node;
×
2069
  SStreamObj *pStream = NULL;
×
2070
  int32_t     code = 0;
×
2071

2072
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
×
2073
    return code;
×
2074
  }
2075

2076
  SMResetStreamReq resetReq = {0};
×
2077
  if (tDeserializeSMResetStreamReq(pReq->pCont, pReq->contLen, &resetReq) < 0) {
×
2078
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2079
  }
2080

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

2083
  code = mndAcquireStream(pMnode, resetReq.name, &pStream);
×
2084
  if (pStream == NULL || code != 0) {
×
2085
    if (resetReq.igNotExists) {
×
2086
      mInfo("stream:%s, not exist, not pause stream", resetReq.name);
×
2087
      return 0;
×
2088
    } else {
2089
      mError("stream:%s not exist, failed to pause stream", resetReq.name);
×
2090
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2091
    }
2092
  }
2093

2094
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
2095
}
2096

2097
static int32_t mndProcessVgroupChange(SMnode *pMnode, SVgroupChangeInfo *pChangeInfo, bool includeAllNodes,
3✔
2098
                                      STrans **pUpdateTrans, SArray* pStreamList) {
2099
  SSdb   *pSdb = pMnode->pSdb;
3✔
2100
  void   *pIter = NULL;
3✔
2101
  STrans *pTrans = NULL;
3✔
2102
  int32_t code = 0;
3✔
2103
  SArray *pTaskNodeList = taosArrayInit(4, sizeof(STaskId));
3✔
2104

2105
  if (pTaskNodeList == NULL) {
3!
2106
    mError("failed to init task node info list, not process the vgroup change procedure, code:%s", tstrerror(terrno));
×
2107
    return terrno;
×
2108
  }
2109

2110
  *pUpdateTrans = NULL;
3✔
2111

2112
  // conflict check for nodeUpdate trans, here we randomly chose one stream to add into the trans pool
2113
  while (1) {
3✔
2114
    SStreamObj *pStream = NULL;
6✔
2115
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
6✔
2116
    if (pIter == NULL) {
6✔
2117
      break;
3✔
2118
    }
2119

2120
    code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_TASK_UPDATE_NAME, false);
3✔
2121
    sdbRelease(pSdb, pStream);
3✔
2122

2123
    if (code) {
3!
2124
      mError("nodeUpdate conflict with other trans, current nodeUpdate ignored, code:%s", tstrerror(code));
×
2125
      sdbCancelFetch(pSdb, pIter);
×
2126
      taosArrayDestroy(pTaskNodeList);
×
2127
      return code;
×
2128
    }
2129
  }
2130

2131
  while(1) {
3✔
2132
    SStreamObj *pStream = NULL;
6✔
2133
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
6✔
2134
    if (pIter == NULL) {
6✔
2135
      break;
3✔
2136
    }
2137

2138
    if (!includeAllNodes) {
3!
2139
      void *p1 = taosHashGet(pChangeInfo->pDBMap, pStream->targetDb, strlen(pStream->targetDb));
3✔
2140
      void *p2 = taosHashGet(pChangeInfo->pDBMap, pStream->sourceDb, strlen(pStream->sourceDb));
3✔
2141
      if (p1 == NULL && p2 == NULL) {
3!
2142
        mDebug("stream:0x%" PRIx64 " %s not involved in nodeUpdate, ignore", pStream->uid, pStream->name);
×
2143
        sdbRelease(pSdb, pStream);
×
2144
        continue;
×
2145
      }
2146
    }
2147

2148
    SStreamTaskIter *pTaskIter = NULL;
3✔
2149

2150
    taosWLockLatch(&pStream->lock);
3✔
2151
    int32_t code1 = createStreamTaskIter(pStream, &pTaskIter);
3✔
2152
    if (code1) {
3!
2153
      taosWUnLockLatch(&pStream->lock);
×
2154
      mError("failed to create stream task iter:%s", pStream->name);
×
2155
      taosArrayDestroy(pTaskNodeList);
×
2156
      return code;
×
2157
    }
2158

2159
    while (streamTaskIterNextTask(pTaskIter)) {
22✔
2160
      SStreamTask *pTask = NULL;
19✔
2161
      code1 = streamTaskIterGetCurrent(pTaskIter, &pTask);
19✔
2162
      if (code1) {
19!
2163
        destroyStreamTaskIter(pTaskIter);
×
2164
        taosWUnLockLatch(&pStream->lock);
×
2165
        taosArrayDestroy(pTaskNodeList);
×
2166
        return code;
×
2167
      }
2168

2169
      STaskId id = {.taskId = pTask->id.taskId, .streamId = pTask->id.streamId};
19✔
2170
      void   *ptr = taosArrayPush(pTaskNodeList, &id);
19✔
2171
      if (ptr == NULL) {
19!
2172
        mError("failed to put task node info into list, code:%s", tstrerror(terrno));
×
2173
        destroyStreamTaskIter(pTaskIter);
×
2174
        taosWUnLockLatch(&pStream->lock);
×
2175
        taosArrayDestroy(pTaskNodeList);
×
2176
        return code;
×
2177
      }
2178
    }
2179

2180
    destroyStreamTaskIter(pTaskIter);
3✔
2181
    taosWUnLockLatch(&pStream->lock);
3✔
2182

2183
    // stop all tasks in this vnode
2184
/*    {
2185
      SVPauseStreamTaskReq req = {.streamId = -1, .taskId = -1};
2186
      SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_STOP, .pCont = &req, .contLen = sizeof(SVPauseStreamTaskReq)};
2187
      mDebug("build and send stop all tasks msg to vnode");
2188

2189
      void* pVgIter = NULL;
2190
      while ((pVgIter = taosHashIterate(pHashMap, pVgIter)) != NULL) {
2191
        SEpSet *pEpset = (SEpSet *)pVgIter;
2192
        code = tmsgSendReq(pEpset, &msg);
2193

2194
        size_t keyLen = 0;
2195
        void* pKey = taosHashGetKey(pEpset, &keyLen);
2196

2197
        int32_t vgId = *(int32_t*) pKey;
2198
        mInfo("send stop tasks msg to vnode:%d", vgId);
2199
      }
2200
    }*/
2201
  }
2202

2203
  mInfo("total involved tasks:%d during vgroup status change", (int32_t)taosArrayGetSize(pTaskNodeList));
3!
2204

2205
  while (1) {
3✔
2206
    SStreamObj *pStream = NULL;
6✔
2207
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
6✔
2208
    if (pIter == NULL) {
6✔
2209
      break;
3✔
2210
    }
2211

2212
    // here create only one trans
2213
    if (pTrans == NULL) {
3!
2214
      code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_TASK_UPDATE_NAME,
3✔
2215
                           "update task epsets", &pTrans);
2216
      if (pTrans == NULL || code) {
3!
2217
        sdbRelease(pSdb, pStream);
×
2218
        sdbCancelFetch(pSdb, pIter);
×
2219
        taosArrayDestroy(pTaskNodeList);
×
2220
        return terrno = code;
×
2221
      }
2222
    }
2223

2224
    if (!includeAllNodes) {
3!
2225
      void *p1 = taosHashGet(pChangeInfo->pDBMap, pStream->targetDb, strlen(pStream->targetDb));
3✔
2226
      void *p2 = taosHashGet(pChangeInfo->pDBMap, pStream->sourceDb, strlen(pStream->sourceDb));
3✔
2227
      if (p1 == NULL && p2 == NULL) {
3!
2228
        mDebug("stream:0x%" PRIx64 " %s not involved in nodeUpdate, ignore", pStream->uid, pStream->name);
×
2229
        sdbRelease(pSdb, pStream);
×
2230
        continue;
×
2231
      }
2232
    }
2233

2234
    mDebug("stream:0x%" PRIx64 " %s involved node changed, create update trans, transId:%d", pStream->uid,
3!
2235
           pStream->name, pTrans->id);
2236

2237
    // NOTE: for each stream, we register one trans entry for task update
2238
    code = mndStreamRegisterTrans(pTrans, MND_STREAM_TASK_UPDATE_NAME, pStream->uid);
3✔
2239
    if (code) {
3!
2240
      mError("failed to register trans, transId:%d, and continue", pTrans->id);
×
2241
    }
2242

2243
    code = mndStreamSetUpdateEpsetAction(pMnode, pStream, pChangeInfo, pTrans, pTaskNodeList);
3✔
2244

2245
    // todo: not continue, drop all and retry again
2246
    if (code != TSDB_CODE_SUCCESS) {
3!
2247
      mError("stream:0x%" PRIx64 " build nodeUpdate trans failed, ignore and continue, code:%s", pStream->uid,
×
2248
             tstrerror(code));
2249
      sdbRelease(pSdb, pStream);
×
2250
      continue;
×
2251
    }
2252

2253
    code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
3✔
2254
    if (code == 0) {
3!
2255
      taosArrayPush(pStreamList, &pStream->uid);
3✔
2256
    }
2257

2258
    sdbRelease(pSdb, pStream);
3✔
2259

2260
    if (code != TSDB_CODE_SUCCESS) {
3!
2261
      sdbCancelFetch(pSdb, pIter);
×
2262
      taosArrayDestroy(pTaskNodeList);
×
2263
      return code;
×
2264
    }
2265
  }
2266

2267
  // no need to build the trans to handle the vgroup update
2268
  *pUpdateTrans = pTrans;
3✔
2269
  taosArrayDestroy(pTaskNodeList);
3✔
2270
  return code;
3✔
2271
}
2272

2273
static int32_t refreshNodeListFromExistedStreams(SMnode *pMnode, SArray *pNodeList) {
1,095✔
2274
  SSdb       *pSdb = pMnode->pSdb;
1,095✔
2275
  SStreamObj *pStream = NULL;
1,095✔
2276
  void       *pIter = NULL;
1,095✔
2277
  int32_t     code = 0;
1,095✔
2278

2279
  mDebug("start to refresh node list by existed streams");
1,095✔
2280

2281
  SHashObj *pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
1,095✔
2282
  if (pHash == NULL) {
1,095!
2283
    return terrno;
×
2284
  }
2285

2286
  while (1) {
3✔
2287
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
1,098✔
2288
    if (pIter == NULL) {
1,098✔
2289
      break;
1,095✔
2290
    }
2291

2292
    taosWLockLatch(&pStream->lock);
3✔
2293

2294
    SStreamTaskIter *pTaskIter = NULL;
3✔
2295
    code = createStreamTaskIter(pStream, &pTaskIter);
3✔
2296
    if (code) {
3!
2297
      taosWUnLockLatch(&pStream->lock);
×
2298
      sdbRelease(pSdb, pStream);
×
2299
      mError("failed to create task iter for stream:%s", pStream->name);
×
2300
      continue;
×
2301
    }
2302

2303
    while (streamTaskIterNextTask(pTaskIter)) {
22✔
2304
      SStreamTask *pTask = NULL;
19✔
2305
      code = streamTaskIterGetCurrent(pTaskIter, &pTask);
19✔
2306
      if (code) {
19!
2307
        break;
×
2308
      }
2309

2310
      SNodeEntry entry = {.hbTimestamp = -1, .nodeId = pTask->info.nodeId, .lastHbMsgId = -1};
19✔
2311
      epsetAssign(&entry.epset, &pTask->info.epSet);
19✔
2312
      int32_t ret = taosHashPut(pHash, &entry.nodeId, sizeof(entry.nodeId), &entry, sizeof(entry));
19✔
2313
      if (ret != 0 && ret != TSDB_CODE_DUP_KEY) {
19!
2314
        mError("failed to put entry into hash map, nodeId:%d, code:%s", entry.nodeId, tstrerror(code));
×
2315
      }
2316
    }
2317

2318
    destroyStreamTaskIter(pTaskIter);
3✔
2319
    taosWUnLockLatch(&pStream->lock);
3✔
2320

2321
    sdbRelease(pSdb, pStream);
3✔
2322
  }
2323

2324
  taosArrayClear(pNodeList);
1,095✔
2325

2326
  // convert to list
2327
  pIter = NULL;
1,095✔
2328
  while ((pIter = taosHashIterate(pHash, pIter)) != NULL) {
1,106✔
2329
    SNodeEntry *pEntry = (SNodeEntry *)pIter;
11✔
2330

2331
    void *p = taosArrayPush(pNodeList, pEntry);
11✔
2332
    if (p == NULL) {
11!
2333
      mError("failed to put entry into node list, nodeId:%d, code: out of memory", pEntry->nodeId);
×
2334
      if (code == 0) {
×
2335
        code = terrno;
×
2336
      }
2337
      continue;
×
2338
    }
2339

2340
    char    buf[256] = {0};
11✔
2341
    int32_t ret = epsetToStr(&pEntry->epset, buf, tListLen(buf));  // ignore this error since it is only for log file
11✔
2342
    if (ret != 0) {                                                // print error and continue
11!
2343
      mError("failed to convert epset to str, code:%s", tstrerror(ret));
×
2344
    }
2345

2346
    mDebug("extract nodeInfo from stream obj, nodeId:%d, %s", pEntry->nodeId, buf);
11!
2347
  }
2348

2349
  taosHashCleanup(pHash);
1,095✔
2350

2351
  mDebug("numOfvNodes:%d get after extracting nodeInfo from all streams", (int32_t)taosArrayGetSize(pNodeList));
1,095✔
2352
  return code;
1,095✔
2353
}
2354

2355
static void addAllDbsIntoHashmap(SHashObj *pDBMap, SSdb *pSdb) {
×
2356
  void   *pIter = NULL;
×
2357
  int32_t code = 0;
×
2358
  while (1) {
×
2359
    SVgObj *pVgroup = NULL;
×
2360
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
×
2361
    if (pIter == NULL) {
×
2362
      break;
×
2363
    }
2364
    if(pVgroup->mountVgId) {
×
2365
      sdbRelease(pSdb, pVgroup);
×
2366
      continue;
×
2367
    }
2368

2369
    code = taosHashPut(pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
×
2370
    sdbRelease(pSdb, pVgroup);
×
2371

2372
    if (code == 0) {
×
2373
      int32_t size = taosHashGetSize(pDBMap);
×
2374
      mDebug("add Db:%s into Dbs list (total:%d) for kill checkpoint trans", pVgroup->dbName, size);
×
2375
    }
2376
  }
2377
}
×
2378

2379
static int32_t doProcessNodeCheckHelp(SArray *pNodeSnapshot, SMnode *pMnode, SVgroupChangeInfo *pChangeInfo,
10✔
2380
                                      bool *pUpdateAllVgroups) {
2381
  int32_t code = removeExpiredNodeEntryAndTaskInBuf(pNodeSnapshot);
10✔
2382
  if (code) {
10!
2383
    mDebug("failed to remove expired node entry in buf, code:%s", tstrerror(code));
×
2384
    return code;
×
2385
  }
2386

2387
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, pNodeSnapshot, pChangeInfo);
10✔
2388
  if (code) {
10!
2389
    mDebug("failed to find changed vnode(s) during vnode(s) check, code:%s", tstrerror(code));
×
2390
    return code;
×
2391
  }
2392

2393
  {
2394
    if (execInfo.role == NODE_ROLE_LEADER && execInfo.switchFromFollower) {
10!
2395
      mInfo("rollback all stream due to mnode leader/follower switch by using nodeUpdate trans");
×
2396
      *pUpdateAllVgroups = true;
×
2397
      execInfo.switchFromFollower = false;  // reset the flag
×
2398
      addAllDbsIntoHashmap(pChangeInfo->pDBMap, pMnode->pSdb);
×
2399
    }
2400
  }
2401

2402
  if (taosArrayGetSize(pChangeInfo->pUpdateNodeList) > 0 || (*pUpdateAllVgroups)) {
10!
2403
    // kill current active checkpoint transaction, since the transaction is vnode wide.
2404
    killAllCheckpointTrans(pMnode, pChangeInfo);
3✔
2405
  } else {
2406
    mDebug("no update found in vnode(s) list");
7!
2407
  }
2408

2409
  return code;
10✔
2410
}
2411

2412
// this function runs by only one thread, so it is not multi-thread safe
2413
static int32_t mndProcessNodeCheckReq(SRpcMsg *pMsg) {
10✔
2414
  int32_t           code = 0;
10✔
2415
  bool              allReady = true;
10✔
2416
  SArray           *pNodeSnapshot = NULL;
10✔
2417
  SMnode           *pMnode = pMsg->info.node;
10✔
2418
  int64_t           tsms = taosGetTimestampMs();
10✔
2419
  int64_t           ts = tsms / 1000;
10✔
2420
  bool              updateAllVgroups = false;
10✔
2421
  SVgroupChangeInfo changeInfo = {0};
10✔
2422

2423
  int32_t old = atomic_val_compare_exchange_32(&mndNodeCheckSentinel, 0, 1);
10✔
2424
  if (old != 0) {
10!
2425
    mDebug("still in checking node change");
×
2426
    return 0;
×
2427
  }
2428

2429
  mDebug("start to do node changing check, ts:%" PRId64, tsms);
10!
2430

2431
  streamMutexLock(&execInfo.lock);
10✔
2432
  int32_t numOfNodes = extractStreamNodeList(pMnode);
10✔
2433
  streamMutexUnlock(&execInfo.lock);
10✔
2434

2435
  if (numOfNodes == 0) {
10!
2436
    mDebug("end to do stream task(s) node change checking, no stream tasks exist, do nothing");
×
2437
    execInfo.ts = ts;
×
2438
    atomic_store_32(&mndNodeCheckSentinel, 0);
×
2439
    return 0;
×
2440
  }
2441

2442
  code = mndTakeVgroupSnapshot(pMnode, &allReady, &pNodeSnapshot, NULL);
10✔
2443
  if (code) {
10!
2444
    mError("failed to take the vgroup snapshot, ignore it and continue");
×
2445
  }
2446

2447
  if (!allReady) {
10!
2448
    taosArrayDestroy(pNodeSnapshot);
×
2449
    atomic_store_32(&mndNodeCheckSentinel, 0);
×
2450
    mWarn("not all vnodes are ready, ignore the exec nodeUpdate check");
×
2451
    return 0;
×
2452
  }
2453

2454
  streamMutexLock(&execInfo.lock);
10✔
2455
  code = doProcessNodeCheckHelp(pNodeSnapshot, pMnode, &changeInfo, &updateAllVgroups);
10✔
2456
  streamMutexUnlock(&execInfo.lock);
10✔
2457

2458
  if (code) {
10!
2459
    goto _end;
×
2460
  }
2461

2462
  if (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0 || updateAllVgroups) {
10!
2463
    mDebug("vnode(s) change detected, build trans to update stream task epsets");
3!
2464

2465
    STrans *pTrans = NULL;
3✔
2466
    SArray* pStreamIdList = taosArrayInit(4, sizeof(int64_t));
3✔
2467

2468
    streamMutexLock(&execInfo.lock);
3✔
2469
    code = mndProcessVgroupChange(pMnode, &changeInfo, updateAllVgroups, &pTrans, pStreamIdList);
3✔
2470

2471
    // remove the consensus-checkpoint-id req of all related stream(s)
2472
    int32_t num = taosArrayGetSize(pStreamIdList);
3✔
2473
    if (num > 0) {
3!
2474
      mDebug("start to clear %d related stream in consensus-checkpoint-id list due to nodeUpdate", num);
3!
2475
      for (int32_t x = 0; x < num; ++x) {
6✔
2476
        int64_t uid = *(int64_t *)taosArrayGet(pStreamIdList, x);
3✔
2477
        int32_t ret = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, uid);
3✔
2478
        if (ret != 0) {
3!
2479
          mError("failed to remove stream:0x%" PRIx64 " from consensus-checkpoint-id list, code:%s", uid,
×
2480
                 tstrerror(ret));
2481
        }
2482
      }
2483
    }
2484

2485
    streamMutexUnlock(&execInfo.lock);
3✔
2486
    taosArrayDestroy(pStreamIdList);
3✔
2487

2488
    // NOTE: sync trans out of lock
2489
    if (code == 0 && pTrans != NULL) {
3!
2490
      code = mndTransPrepare(pMnode, pTrans);
3✔
2491
      if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
3!
2492
        mError("trans:%d, failed to prepare update stream trans since %s", pTrans->id, tstrerror(code));
×
2493
      }
2494

2495
      mndTransDrop(pTrans);
3✔
2496
    }
2497

2498
    // keep the new vnode snapshot if success
2499
    if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {
3!
2500
      streamMutexLock(&execInfo.lock);
3✔
2501

2502
      code = refreshNodeListFromExistedStreams(pMnode, execInfo.pNodeList);
3✔
2503
      int32_t num1 = (int)taosArrayGetSize(execInfo.pNodeList);
3✔
2504
      if (code == 0) {
3!
2505
        execInfo.ts = ts;
3✔
2506
        mDebug("create trans successfully, update cached node list, numOfNodes:%d", num1);
3!
2507
      }
2508

2509
      streamMutexUnlock(&execInfo.lock);
3✔
2510

2511
      if (code) {
3!
2512
        mError("failed to extract node list from stream, code:%s", tstrerror(code));
×
2513
        goto _end;
×
2514
      }
2515
    }
2516
  }
2517

2518
  mndDestroyVgroupChangeInfo(&changeInfo);
10✔
2519

2520
_end:
10✔
2521
  taosArrayDestroy(pNodeSnapshot);
10✔
2522

2523
  mDebug("end to do stream task node change checking, elapsed time:%" PRId64 "ms", taosGetTimestampMs() - tsms);
20!
2524
  atomic_store_32(&mndNodeCheckSentinel, 0);
10✔
2525

2526
  return 0;
10✔
2527
}
2528

2529
static int32_t mndProcessNodeCheck(SRpcMsg *pReq) {
38✔
2530
  SMnode *pMnode = pReq->info.node;
38✔
2531
  SSdb   *pSdb = pMnode->pSdb;
38✔
2532
  if (sdbGetSize(pSdb, SDB_STREAM) <= 0) {
38✔
2533
    return 0;
28✔
2534
  }
2535

2536
  int32_t               size = sizeof(SMStreamNodeCheckMsg);
10✔
2537
  SMStreamNodeCheckMsg *pMsg = rpcMallocCont(size);
10✔
2538
  if (pMsg == NULL) {
10!
2539
    return terrno;
×
2540
  }
2541

2542
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_NODECHANGE_CHECK, .pCont = pMsg, .contLen = size};
10✔
2543
  return tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
10✔
2544
}
2545

2546
static int32_t mndProcessStatusCheck(SRpcMsg *pReq) {
×
2547
  SMnode *pMnode = pReq->info.node;
×
2548
  SSdb   *pSdb = pMnode->pSdb;
×
2549
  if (sdbGetSize(pSdb, SDB_STREAM) <= 0) {
×
2550
    return 0;
×
2551
  }
2552

2553
  int32_t               size = sizeof(SMStreamNodeCheckMsg);
×
2554
  SMStreamNodeCheckMsg *pMsg = rpcMallocCont(size);
×
2555
  if (pMsg == NULL) {
×
2556
    return terrno;
×
2557
  }
2558

2559
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_NODECHANGE_CHECK, .pCont = pMsg, .contLen = size};
×
2560
  return tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
×
2561
}
2562

2563
void saveTaskAndNodeInfoIntoBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
1,294✔
2564
  SStreamTaskIter *pIter = NULL;
1,294✔
2565
  int32_t          code = createStreamTaskIter(pStream, &pIter);
1,294✔
2566
  if (code) {
1,294!
2567
    mError("failed to create task iter for stream:%s", pStream->name);
×
2568
    return;
×
2569
  }
2570

2571
  while (streamTaskIterNextTask(pIter)) {
6,807✔
2572
    SStreamTask *pTask = NULL;
5,513✔
2573
    code = streamTaskIterGetCurrent(pIter, &pTask);
5,513✔
2574
    if (code) {
5,513!
2575
      break;
×
2576
    }
2577

2578
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
5,513✔
2579
    void   *p = taosHashGet(pExecNode->pTaskMap, &id, sizeof(id));
5,513✔
2580
    if (p == NULL) {
5,513✔
2581
      STaskStatusEntry entry = {0};
4,728✔
2582
      streamTaskStatusInit(&entry, pTask);
4,728✔
2583

2584
      code = taosHashPut(pExecNode->pTaskMap, &id, sizeof(id), &entry, sizeof(entry));
4,728✔
2585
      if (code == 0) {
4,728!
2586
        void   *px = taosArrayPush(pExecNode->pTaskList, &id);
4,728✔
2587
        int32_t num = (int32_t)taosArrayGetSize(pExecNode->pTaskList);
4,728✔
2588
        if (px) {
4,728!
2589
          mInfo("s-task:0x%x add into task buffer, total:%d", (int32_t)entry.id.taskId, num);
4,728!
2590
        } else {
2591
          mError("s-task:0x%x failed to add into task buffer, total:%d", (int32_t)entry.id.taskId, num);
×
2592
        }
2593
      } else {
2594
        mError("s-task:0x%x failed to add into task map, since out of memory", (int32_t)entry.id.taskId);
×
2595
      }
2596

2597
      // add the new vgroups if not added yet
2598
      bool exist = false;
4,728✔
2599
      for (int32_t j = 0; j < taosArrayGetSize(pExecNode->pNodeList); ++j) {
34,906✔
2600
        SNodeEntry *pEntry = taosArrayGet(pExecNode->pNodeList, j);
33,823✔
2601
        if ((pEntry != NULL) && (pEntry->nodeId == pTask->info.nodeId)) {
33,823!
2602
          exist = true;
3,645✔
2603
          break;
3,645✔
2604
        }
2605
      }
2606

2607
      if (!exist) {
4,728✔
2608
        SNodeEntry nodeEntry = {.hbTimestamp = -1, .nodeId = pTask->info.nodeId, .lastHbMsgId = -1};
1,083✔
2609
        epsetAssign(&nodeEntry.epset, &pTask->info.epSet);
1,083✔
2610

2611
        void *px = taosArrayPush(pExecNode->pNodeList, &nodeEntry);
1,083✔
2612
        if (px) {
1,083!
2613
          mInfo("vgId:%d added into nodeList, total:%d", nodeEntry.nodeId, (int)taosArrayGetSize(pExecNode->pNodeList));
1,083!
2614
        } else {
2615
          mError("vgId:%d failed to add into nodeList, total:%d", nodeEntry.nodeId,
×
2616
                 (int)taosArrayGetSize(pExecNode->pNodeList))
2617
        }
2618
      }
2619
    }
2620
  }
2621

2622
  destroyStreamTaskIter(pIter);
1,294✔
2623
}
2624

2625
static void doAddTaskId(SArray *pList, int32_t taskId, int64_t uid, int32_t numOfTotal) {
1,680✔
2626
  int32_t num = taosArrayGetSize(pList);
1,680✔
2627
  for (int32_t i = 0; i < num; ++i) {
6,124✔
2628
    int32_t *pId = taosArrayGet(pList, i);
4,444✔
2629
    if (pId == NULL) {
4,444!
2630
      continue;
×
2631
    }
2632

2633
    if (taskId == *pId) {
4,444!
2634
      return;
×
2635
    }
2636
  }
2637

2638
  int32_t numOfTasks = taosArrayGetSize(pList);
1,680✔
2639
  void   *p = taosArrayPush(pList, &taskId);
1,680✔
2640
  if (p) {
1,680!
2641
    mDebug("stream:0x%" PRIx64 " receive %d reqs for checkpoint, remain:%d", uid, numOfTasks, numOfTotal - numOfTasks);
1,680!
2642
  } else {
2643
    mError("stream:0x%" PRIx64 " receive %d reqs for checkpoint, failed to added into task list, since out of memory",
×
2644
           uid, numOfTasks);
2645
  }
2646
}
2647

2648
int32_t mndProcessStreamReqCheckpoint(SRpcMsg *pReq) {
1,680✔
2649
  SMnode                  *pMnode = pReq->info.node;
1,680✔
2650
  SStreamTaskCheckpointReq req = {0};
1,680✔
2651

2652
  SDecoder decoder = {0};
1,680✔
2653
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
1,680✔
2654

2655
  if (tDecodeStreamTaskCheckpointReq(&decoder, &req)) {
1,680!
2656
    tDecoderClear(&decoder);
×
2657
    mError("invalid task checkpoint req msg received");
×
2658
    return TSDB_CODE_INVALID_MSG;
×
2659
  }
2660
  tDecoderClear(&decoder);
1,680✔
2661

2662
  mDebug("receive stream task checkpoint req msg, vgId:%d, s-task:0x%x", req.nodeId, req.taskId);
1,680!
2663

2664
  // register to the stream task done map, if all tasks has sent this kinds of message, start the checkpoint trans.
2665
  streamMutexLock(&execInfo.lock);
1,680✔
2666

2667
  SStreamObj *pStream = NULL;
1,680✔
2668
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
1,680✔
2669
  if (pStream == NULL || code != 0) {
1,680!
2670
    mWarn("failed to find the stream:0x%" PRIx64 ", not handle the checkpoint req, try to acquire in buf",
×
2671
          req.streamId);
2672

2673
    // not in meta-store yet, try to acquire the task in exec buffer
2674
    // the checkpoint req arrives too soon before the completion of the create stream trans.
2675
    STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
×
2676
    void   *p = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
2677
    if (p == NULL) {
×
2678
      mError("failed to find the stream:0x%" PRIx64 " in buf, not handle the checkpoint req", req.streamId);
×
2679
      streamMutexUnlock(&execInfo.lock);
×
2680
      return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
2681
    } else {
2682
      mDebug("s-task:0x%" PRIx64 "-0x%x in buf not in mnode/meta, create stream trans may not complete yet",
×
2683
             req.streamId, req.taskId);
2684
    }
2685
  }
2686

2687
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
1,680!
2688

2689
  SArray **pReqTaskList = (SArray **)taosHashGet(execInfo.pTransferStateStreams, &req.streamId, sizeof(req.streamId));
1,680✔
2690
  if (pReqTaskList == NULL) {
1,680✔
2691
    SArray *pList = taosArrayInit(4, sizeof(int32_t));
327✔
2692
    doAddTaskId(pList, req.taskId, req.streamId, numOfTasks);
327✔
2693
    code = taosHashPut(execInfo.pTransferStateStreams, &req.streamId, sizeof(int64_t), &pList, sizeof(void *));
327✔
2694
    if (code) {
327!
2695
      mError("failed to put into transfer state stream map, code: out of memory");
×
2696
    }
2697
    pReqTaskList = (SArray **)taosHashGet(execInfo.pTransferStateStreams, &req.streamId, sizeof(req.streamId));
327✔
2698
  } else {
2699
    doAddTaskId(*pReqTaskList, req.taskId, req.streamId, numOfTasks);
1,353✔
2700
  }
2701

2702
  int32_t total = taosArrayGetSize(*pReqTaskList);
1,680✔
2703
  if (total == numOfTasks) {  // all tasks have sent the reqs
1,680✔
2704
    int64_t checkpointId = mndStreamGenChkptId(pMnode, false);
307✔
2705
    mInfo("stream:0x%" PRIx64 " all tasks req checkpoint, start checkpointId:%" PRId64, req.streamId, checkpointId);
307!
2706

2707
    if (pStream != NULL) {  // TODO:handle error
307!
2708
      code = mndProcessStreamCheckpointTrans(pMnode, pStream, checkpointId, 0, false);
307✔
2709
      if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
307!
2710
        mError("stream:0x%" PRIx64 " failed to create checkpoint trans, checkpointId:%" PRId64 ", code:%s",
×
2711
               req.streamId, checkpointId, tstrerror(code));
2712
      }
2713
    } else {
2714
      // todo: wait for the create stream trans completed, and launch the checkpoint trans
2715
      // SStreamObj *pStream = mndGetStreamObj(pMnode, req.streamId);
2716
      // sleep(500ms)
2717
    }
2718

2719
    // remove this entry, not overwriting the global error code
2720
    int32_t ret = taosHashRemove(execInfo.pTransferStateStreams, &req.streamId, sizeof(int64_t));
307✔
2721
    if (ret) {
307!
2722
      mError("failed to remove transfer state stream, code:%s", tstrerror(ret));
×
2723
    }
2724

2725
    int32_t numOfStreams = taosHashGetSize(execInfo.pTransferStateStreams);
307✔
2726
    mDebug("stream:0x%" PRIx64 " removed in transfer-state list, %d stream(s) not finish fill-history process",
307!
2727
           req.streamId, numOfStreams);
2728
  }
2729

2730
  if (pStream != NULL) {
1,680!
2731
    mndReleaseStream(pMnode, pStream);
1,680✔
2732
  }
2733

2734
  streamMutexUnlock(&execInfo.lock);
1,680✔
2735

2736
  {
2737
    SRpcMsg rsp = {.code = 0, .info = pReq->info, .contLen = sizeof(SMStreamReqCheckpointRsp)};
1,680✔
2738
    rsp.pCont = rpcMallocCont(rsp.contLen);
1,680✔
2739
    if (rsp.pCont == NULL) {
1,680!
2740
      return terrno;
×
2741
    }
2742

2743
    SMsgHead *pHead = rsp.pCont;
1,680✔
2744
    pHead->vgId = htonl(req.nodeId);
1,680✔
2745

2746
    tmsgSendRsp(&rsp);
1,680✔
2747
    pReq->info.handle = NULL;  // disable auto rsp
1,680✔
2748
  }
2749

2750
  return 0;
1,680✔
2751
}
2752

2753
// valid the info according to the HbMsg
2754
static bool validateChkptReport(const SCheckpointReport *pReport, int64_t reportChkptId) {
1,671✔
2755
  STaskId           id = {.streamId = pReport->streamId, .taskId = pReport->taskId};
1,671✔
2756
  STaskStatusEntry *pTaskEntry = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
1,671✔
2757
  if (pTaskEntry == NULL) {
1,671✔
2758
    mError("invalid checkpoint-report msg from task:0x%x, discard", pReport->taskId);
3!
2759
    return false;
3✔
2760
  }
2761

2762
  if (pTaskEntry->checkpointInfo.latestId >= pReport->checkpointId) {
1,668!
2763
    mError("s-task:0x%x invalid checkpoint-report msg, checkpointId:%" PRId64 " saved checkpointId:%" PRId64 " discard",
×
2764
           pReport->taskId, pReport->checkpointId, pTaskEntry->checkpointInfo.activeId);
2765
    return false;
×
2766
  }
2767

2768
  // now the task in checkpoint procedure
2769
  if ((pTaskEntry->checkpointInfo.activeId != 0) && (pTaskEntry->checkpointInfo.activeId > pReport->checkpointId)) {
1,668!
2770
    mError("s-task:0x%x invalid checkpoint-report msg, checkpointId:%" PRId64 " active checkpointId:%" PRId64
×
2771
           " discard",
2772
           pReport->taskId, pReport->checkpointId, pTaskEntry->checkpointInfo.activeId);
2773
    return false;
×
2774
  }
2775

2776
  if (reportChkptId >= pReport->checkpointId) {
1,668!
2777
    mError("s-task:0x%x expired checkpoint-report msg, checkpointId:%" PRId64 " already update checkpointId:%" PRId64
×
2778
           " discard",
2779
           pReport->taskId, pReport->checkpointId, reportChkptId);
2780
    return false;
×
2781
  }
2782

2783
  return true;
1,668✔
2784
}
2785

2786
static void doAddReportStreamTask(SArray *pList, int64_t reportedChkptId, const SCheckpointReport *pReport) {
1,671✔
2787
  bool valid = validateChkptReport(pReport, reportedChkptId);
1,671✔
2788
  if (!valid) {
1,671✔
2789
    return;
3✔
2790
  }
2791

2792
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
6,117✔
2793
    STaskChkptInfo *p = taosArrayGet(pList, i);
4,449✔
2794
    if (p == NULL) {
4,449!
2795
      continue;
×
2796
    }
2797

2798
    if (p->taskId == pReport->taskId) {
4,449!
2799
      if (p->checkpointId > pReport->checkpointId) {
×
2800
        mError("s-task:0x%x invalid checkpoint-report msg, existed:%" PRId64 " req checkpointId:%" PRId64 ", discard",
×
2801
               pReport->taskId, p->checkpointId, pReport->checkpointId);
2802
      } else if (p->checkpointId < pReport->checkpointId) {  // expired checkpoint-report msg, update it
×
2803
        mInfo("s-task:0x%x expired checkpoint-report info in checkpoint-report list update from %" PRId64 "->%" PRId64,
×
2804
              pReport->taskId, p->checkpointId, pReport->checkpointId);
2805

2806
        // update the checkpoint report info
2807
        p->checkpointId = pReport->checkpointId;
×
2808
        p->ts = pReport->checkpointTs;
×
2809
        p->version = pReport->checkpointVer;
×
2810
        p->transId = pReport->transId;
×
2811
        p->dropHTask = pReport->dropHTask;
×
2812
      } else {
2813
        mWarn("taskId:0x%x already in checkpoint-report list", pReport->taskId);
×
2814
      }
2815
      return;
×
2816
    }
2817
  }
2818

2819
  STaskChkptInfo info = {
1,668✔
2820
      .streamId = pReport->streamId,
1,668✔
2821
      .taskId = pReport->taskId,
1,668✔
2822
      .transId = pReport->transId,
1,668✔
2823
      .dropHTask = pReport->dropHTask,
1,668✔
2824
      .version = pReport->checkpointVer,
1,668✔
2825
      .ts = pReport->checkpointTs,
1,668✔
2826
      .checkpointId = pReport->checkpointId,
1,668✔
2827
      .nodeId = pReport->nodeId,
1,668✔
2828
  };
2829

2830
  void *p = taosArrayPush(pList, &info);
1,668✔
2831
  if (p == NULL) {
1,668!
2832
    mError("failed to put into task list, taskId:0x%x", pReport->taskId);
×
2833
  } else {
2834
    int32_t size = taosArrayGetSize(pList);
1,668✔
2835
    mDebug("stream:0x%" PRIx64 " taskId:0x%x checkpoint-report recv, %d tasks has send checkpoint-report",
1,668!
2836
           pReport->streamId, pReport->taskId, size);
2837
  }
2838
}
2839

2840
int32_t mndProcessCheckpointReport(SRpcMsg *pReq) {
1,671✔
2841
  SMnode           *pMnode = pReq->info.node;
1,671✔
2842
  SCheckpointReport req = {0};
1,671✔
2843

2844
  SDecoder decoder = {0};
1,671✔
2845
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
1,671✔
2846

2847
  if (tDecodeStreamTaskChkptReport(&decoder, &req)) {
1,671!
2848
    tDecoderClear(&decoder);
×
2849
    mError("invalid task checkpoint-report msg received");
×
2850
    return TSDB_CODE_INVALID_MSG;
×
2851
  }
2852
  tDecoderClear(&decoder);
1,671✔
2853

2854
  streamMutexLock(&execInfo.lock);
1,671✔
2855
  mndInitStreamExecInfo(pMnode, &execInfo);
1,671✔
2856
  streamMutexUnlock(&execInfo.lock);
1,671✔
2857

2858
  mDebug("receive stream task checkpoint-report msg, vgId:%d, s-task:0x%x, checkpointId:%" PRId64
1,671!
2859
         " checkpointVer:%" PRId64 " transId:%d",
2860
         req.nodeId, req.taskId, req.checkpointId, req.checkpointVer, req.transId);
2861

2862
  // register to the stream task done map, if all tasks has sent these kinds of message, start the checkpoint trans.
2863
  streamMutexLock(&execInfo.lock);
1,671✔
2864

2865
  SStreamObj *pStream = NULL;
1,671✔
2866
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
1,671✔
2867
  if (pStream == NULL || code != 0) {
1,671!
2868
    mWarn("failed to find the stream:0x%" PRIx64 ", not handle checkpoint-report, try to acquire in buf", req.streamId);
×
2869

2870
    // not in meta-store yet, try to acquire the task in exec buffer
2871
    // the checkpoint req arrives too soon before the completion of the creation of stream trans.
2872
    STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
×
2873
    void   *p = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
2874
    if (p == NULL) {
×
2875
      mError("failed to find the stream:0x%" PRIx64 " in buf, not handle the checkpoint-report", req.streamId);
×
2876
      streamMutexUnlock(&execInfo.lock);
×
2877
      return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
2878
    } else {
2879
      mDebug("s-task:0x%" PRIx64 "-0x%x in buf not in mnode/meta, create stream trans may not complete yet",
×
2880
             req.streamId, req.taskId);
2881
    }
2882
  }
2883

2884
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
1,671!
2885

2886
  SChkptReportInfo *pInfo =
2887
      (SChkptReportInfo *)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId));
1,671✔
2888
  if (pInfo == NULL) {
1,671✔
2889
    SChkptReportInfo info = {.pTaskList = taosArrayInit(4, sizeof(STaskChkptInfo)), .streamId = req.streamId};
322✔
2890
    if (info.pTaskList != NULL) {
322!
2891
      doAddReportStreamTask(info.pTaskList, info.reportChkpt, &req);
322✔
2892
      code = taosHashPut(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId), &info, sizeof(info));
322✔
2893
      if (code) {
322!
2894
        mError("stream:0x%" PRIx64 " failed to put into checkpoint stream", req.streamId);
×
2895
      }
2896

2897
      pInfo = (SChkptReportInfo *)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId));
322✔
2898
    }
2899
  } else {
2900
    doAddReportStreamTask(pInfo->pTaskList, pInfo->reportChkpt, &req);
1,349✔
2901
  }
2902

2903
  int32_t total = taosArrayGetSize(pInfo->pTaskList);
1,671✔
2904
  if (total == numOfTasks) {  // all tasks have sent the reqs
1,671✔
2905
    mInfo("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, checkpoint meta-info for checkpointId:%" PRId64
321!
2906
          " will be issued soon",
2907
          req.streamId, pStream->name, total, req.checkpointId);
2908
  }
2909

2910
  if (pStream != NULL) {
1,671!
2911
    mndReleaseStream(pMnode, pStream);
1,671✔
2912
  }
2913

2914
  streamMutexUnlock(&execInfo.lock);
1,671✔
2915

2916
  doSendQuickRsp(&pReq->info, sizeof(SMStreamUpdateChkptRsp), req.nodeId, TSDB_CODE_SUCCESS);
1,671✔
2917
  return code;
1,671✔
2918
}
2919

2920
static int64_t getConsensusId(int64_t streamId, int32_t numOfTasks, int32_t *pExistedTasks, bool *pAllSame) {
11✔
2921
  int32_t num = 0;
11✔
2922
  int64_t chkId = INT64_MAX;
11✔
2923
  *pExistedTasks = 0;
11✔
2924
  *pAllSame = true;
11✔
2925

2926
  for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
116✔
2927
    STaskId *p = taosArrayGet(execInfo.pTaskList, i);
105✔
2928
    if (p == NULL) {
105!
2929
      continue;
×
2930
    }
2931

2932
    if (p->streamId != streamId) {
105✔
2933
      continue;
77✔
2934
    }
2935

2936
    num += 1;
28✔
2937
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
28✔
2938
    if (chkId > pe->checkpointInfo.latestId) {
28✔
2939
      if (chkId != INT64_MAX) {
11!
2940
        *pAllSame = false;
×
2941
        mDebug("checkpointIds not identical, prev:%" PRId64 " smaller:%" PRId64 " from task:0x%" PRIx64, chkId,
×
2942
               pe->checkpointInfo.latestId, pe->id.taskId);
2943
      }
2944
      chkId = pe->checkpointInfo.latestId;
11✔
2945
    }
2946
  }
2947

2948
  *pExistedTasks = num;
11✔
2949
  if (num < numOfTasks) {  // not all task send info to mnode through hbMsg, no valid checkpoint Id
11!
2950
    return -1;
×
2951
  }
2952

2953
  return chkId;
11✔
2954
}
2955

2956
static void doSendQuickRsp(SRpcHandleInfo *pInfo, int32_t msgSize, int32_t vgId, int32_t code) {
1,671✔
2957
  SRpcMsg rsp = {.code = code, .info = *pInfo, .contLen = msgSize};
1,671✔
2958
  rsp.pCont = rpcMallocCont(rsp.contLen);
1,671✔
2959
  if (rsp.pCont != NULL) {
1,671!
2960
    SMsgHead *pHead = rsp.pCont;
1,671✔
2961
    pHead->vgId = htonl(vgId);
1,671✔
2962

2963
    tmsgSendRsp(&rsp);
1,671✔
2964
    pInfo->handle = NULL;  // disable auto rsp
1,671✔
2965
  }
2966
}
1,671✔
2967

2968
static int32_t doCleanReqList(SArray *pList, SCheckpointConsensusInfo *pInfo) {
×
2969
  int32_t alreadySend = taosArrayGetSize(pList);
×
2970

2971
  for (int32_t i = 0; i < alreadySend; ++i) {
×
2972
    int32_t *taskId = taosArrayGet(pList, i);
×
2973
    if (taskId == NULL) {
×
2974
      continue;
×
2975
    }
2976

2977
    for (int32_t k = 0; k < taosArrayGetSize(pInfo->pTaskList); ++k) {
×
2978
      SCheckpointConsensusEntry *pe = taosArrayGet(pInfo->pTaskList, k);
×
2979
      if ((pe != NULL) && (pe->req.taskId == *taskId)) {
×
2980
        taosArrayRemove(pInfo->pTaskList, k);
×
2981
        break;
×
2982
      }
2983
    }
2984
  }
2985

2986
  return alreadySend;
×
2987
}
2988

2989
int32_t mndProcessConsensusInTmr(SRpcMsg *pMsg) {
1,361✔
2990
  SMnode *pMnode = pMsg->info.node;
1,361✔
2991
  int64_t now = taosGetTimestampMs();
1,361✔
2992
  bool    allReady = true;
1,361✔
2993
  SArray *pNodeSnapshot = NULL;
1,361✔
2994
  int32_t numOfTrans = 0;
1,361✔
2995
  int32_t code = 0;
1,361✔
2996
  void   *pIter = NULL;
1,361✔
2997

2998
  SArray *pStreamList = taosArrayInit(4, sizeof(int64_t));
1,361✔
2999
  if (pStreamList == NULL) {
1,361!
3000
    return terrno;
×
3001
  }
3002

3003
  SHashObj* pTermMap = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
1,361✔
3004
  if (pTermMap == NULL) {
1,361!
3005
    taosArrayDestroy(pStreamList);
×
3006
    return terrno;
×
3007
  }
3008

3009
  mDebug("start to process consensus-checkpointId in tmr");
1,361✔
3010

3011
  streamMutexLock(&execInfo.lock);
1,361✔
3012
  int32_t numOfTasks = taosHashGetSize(execInfo.pStreamConsensus);
1,361✔
3013
  streamMutexUnlock(&execInfo.lock);
1,361✔
3014

3015
  if (numOfTasks == 0) {
1,361✔
3016
    mDebug("no streams in consensus-checkpointId list, no need to do stream consensus");
1,337✔
3017
    taosArrayDestroy(pStreamList);
1,337✔
3018
    taosHashCleanup(pTermMap);
1,337✔
3019
    return 0;
1,337✔
3020
  } else {
3021
    mDebug("start to check %d streams in consensus-checkpointId list", numOfTasks);
24!
3022
  }
3023

3024
  code = mndTakeVgroupSnapshot(pMnode, &allReady, &pNodeSnapshot, pTermMap);
24✔
3025
  taosArrayDestroy(pNodeSnapshot);
24✔
3026
  if (code) {
24!
3027
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
3028
  }
3029

3030
  if (!allReady) {
24✔
3031
    mWarn("not all vnodes are ready, end to process the consensus-checkpointId in tmr process");
2!
3032
    taosArrayDestroy(pStreamList);
2✔
3033
    taosHashCleanup(pTermMap);
2✔
3034
    return 0;
2✔
3035
  }
3036

3037
  streamMutexLock(&execInfo.lock);
22✔
3038

3039
  while ((pIter = taosHashIterate(execInfo.pStreamConsensus, pIter)) != NULL) {
33✔
3040
    SCheckpointConsensusInfo *pInfo = (SCheckpointConsensusInfo *)pIter;
22✔
3041

3042
    int64_t     streamId = -1;
22✔
3043
    int32_t     num = taosArrayGetSize(pInfo->pTaskList);
22✔
3044
    SStreamObj *pStream = NULL;
22✔
3045

3046
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
22✔
3047
    if (pStream == NULL || code != 0) {  // stream has been dropped already
22!
3048
      mDebug("stream:0x%" PRIx64 " dropped already, continue", pInfo->streamId);
×
3049
      void *p = taosArrayPush(pStreamList, &pInfo->streamId);
×
3050
      if (p == NULL) {
×
3051
        mError("failed to record the missing stream id in concensus-stream list, streamId:%" PRId64
×
3052
               " code:%s, continue",
3053
               pInfo->streamId, tstrerror(terrno));
3054
      }
3055
      continue;
11✔
3056
    }
3057

3058
    if (pStream->uid != pInfo->streamId) {
22✔
3059
      // todo remove it
3060
    }
3061

3062
    if ((num < pInfo->numOfTasks) || (pInfo->numOfTasks == 0)) {
22!
3063
      mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-consensus req(not all), ignore", pStream->uid,
11!
3064
             pStream->name, num, pInfo->numOfTasks);
3065
      mndReleaseStream(pMnode, pStream);
11✔
3066
      continue;
11✔
3067
    }
3068

3069
    streamId = pStream->uid;
11✔
3070

3071
    int32_t existed = 0;
11✔
3072
    bool    allSame = true;
11✔
3073
    int64_t chkId = getConsensusId(pInfo->streamId, pInfo->numOfTasks, &existed, &allSame);
11✔
3074
    if (chkId == -1) {
11!
3075
      mDebug("not all(%d/%d) task(s) send hbMsg yet, wait for a while and check again", existed, pInfo->numOfTasks);
×
3076
      mndReleaseStream(pMnode, pStream);
×
3077
      continue;
×
3078
    }
3079

3080
    bool allQualified = true;
11✔
3081
    for (int32_t j = 0; j < num; ++j) {
39✔
3082
      SCheckpointConsensusEntry *pe = taosArrayGet(pInfo->pTaskList, j);
28✔
3083
      if (pe == NULL) {
28!
3084
        continue;
×
3085
      }
3086

3087
      if (pe->req.nodeId != -2) {
28✔
3088
        int32_t *pTerm = taosHashGet(pTermMap, &(pe->req.nodeId), sizeof(pe->req.nodeId));
26✔
3089
        if (pTerm == NULL) {
26!
3090
          mError("stream:0x%" PRIx64 " s-task:0x%x req from vgId:%d not found in termMap", pe->req.streamId,
×
3091
                 pe->req.taskId, pe->req.nodeId);
3092
          allQualified = false;
×
3093
          continue;
×
3094
        } else {
3095
          if (*pTerm != pe->req.term) {
26!
3096
            mWarn("stream:0x%" PRIx64 " s-task:0x%x req from vgId:%d is expired, term:%d, current term:%d",
×
3097
                  pe->req.streamId, pe->req.taskId, pe->req.nodeId, pe->req.term, *pTerm);
3098
            allQualified = false;
×
3099
            continue;
×
3100
          }
3101
        }
3102
      }
3103

3104
      if (((now - pe->ts) >= 10 * 1000) || allSame) {
28!
3105
        mInfo("s-task:0x%" PRIx64 "-0x%x vgId:%d term:%d sendTs:%" PRId64
28!
3106
              " wait %.2fs or all tasks have same checkpointId:%" PRId64,
3107
              pe->req.streamId, pe->req.taskId, pe->req.nodeId, pe->req.term, pe->req.startTs, (now - pe->ts) / 1000.0,
3108
              chkId);
3109

3110
        if (chkId > pe->req.checkpointId) {
28!
3111
          streamMutexUnlock(&execInfo.lock);
×
3112

3113
          taosArrayDestroy(pStreamList);
×
3114
          taosHashCleanup(pTermMap);
×
3115

3116
          mError("s-task:0x%x checkpointId:%" PRId64 " is updated to %" PRId64 ", update it", pe->req.taskId,
×
3117
                 pe->req.checkpointId, chkId);
3118

3119
          mndReleaseStream(pMnode, pStream);
×
3120
          taosHashCancelIterate(execInfo.pStreamConsensus, pIter);
×
3121
          return TSDB_CODE_FAILED;
×
3122
        }
3123

3124
      } else {
3125
        mDebug("s-task:0x%x sendTs:%" PRId64 " wait %.2fs already, wait for next round to check", pe->req.taskId,
×
3126
               pe->req.startTs, (now - pe->ts) / 1000.0);
3127
        allQualified = false;
×
3128
      }
3129
    }
3130

3131
    if (allQualified) {
11!
3132
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_CONSEN_NAME, false);
11✔
3133

3134
      if (code == 0) {
11!
3135
        code = mndCreateSetConsensusChkptIdTrans(pMnode, pStream, chkId, pInfo->pTaskList);
11✔
3136
        if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
11!
3137
          mError("failed to create consensus-checkpoint trans, stream:0x%" PRIx64, pStream->uid);
×
3138
        } else {
3139
          numOfTrans += 1;
11✔
3140
          mndClearConsensusRspEntry(pInfo);
11✔
3141
          void *p = taosArrayPush(pStreamList, &streamId);
11✔
3142
          if (p == NULL) {
11!
3143
            mError("failed to put into stream list, stream:0x%" PRIx64 " not remove it in consensus-chkpt list",
×
3144
                   streamId);
3145
          }
3146
        }
3147
      } else {
3148
        mDebug("stream:0x%" PRIx64 "not create chktp-consensus, due to trans conflict", pStream->uid);
×
3149
      }
3150
    }
3151

3152
    mndReleaseStream(pMnode, pStream);
11✔
3153

3154
    // create one transaction each time
3155
    if (numOfTrans > 0) {
11!
3156
      taosHashCancelIterate(execInfo.pStreamConsensus, pIter);
11✔
3157
      break;
11✔
3158
    }
3159
  }
3160

3161
  for (int32_t i = 0; i < taosArrayGetSize(pStreamList); ++i) {
33✔
3162
    int64_t *pStreamId = (int64_t *)taosArrayGet(pStreamList, i);
11✔
3163
    if (pStreamId == NULL) {
11!
3164
      continue;
×
3165
    }
3166

3167
    code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, *pStreamId);
11✔
3168
  }
3169

3170
  streamMutexUnlock(&execInfo.lock);
22✔
3171

3172
  taosArrayDestroy(pStreamList);
22✔
3173
  taosHashCleanup(pTermMap);
22✔
3174

3175
  mDebug("end to process consensus-checkpointId in tmr, send consensus-checkpoint trans:%d", numOfTrans);
22!
3176
  return code;
22✔
3177
}
3178

3179
static int32_t mndProcessCreateStreamReqFromMNode(SRpcMsg *pReq) {
109✔
3180
  int32_t code = mndProcessCreateStreamReq(pReq);
109✔
3181
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
109!
3182
    pReq->info.rsp = rpcMallocCont(1);
×
3183
    if (pReq->info.rsp == NULL) {
×
3184
      return terrno;
×
3185
    }
3186

3187
    pReq->info.rspLen = 1;
×
3188
    pReq->info.noResp = false;
×
3189
    pReq->code = code;
×
3190
  }
3191
  return code;
109✔
3192
}
3193

3194
static int32_t mndProcessDropStreamReqFromMNode(SRpcMsg *pReq) {
89✔
3195
  int32_t code = mndProcessDropStreamReq(pReq);
89✔
3196
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
89!
3197
    pReq->info.rsp = rpcMallocCont(1);
×
3198
    if (pReq->info.rsp == NULL) {
×
3199
      return terrno;
×
3200
    }
3201

3202
    pReq->info.rspLen = 1;
×
3203
    pReq->info.noResp = false;
×
3204
    pReq->code = code;
×
3205
  }
3206
  return code;
89✔
3207
}
3208

3209
void mndInitStreamExecInfo(SMnode *pMnode, SStreamExecInfo *pExecInfo) {
11,069✔
3210
  if (pExecInfo->initTaskList || pMnode == NULL) {
11,069✔
3211
    return;
10,927✔
3212
  }
3213

3214
  addAllStreamTasksIntoBuf(pMnode, pExecInfo);
142✔
3215
  pExecInfo->initTaskList = true;
142✔
3216
}
3217

3218
void mndStreamResetInitTaskListLoadFlag() {
2,216✔
3219
  mInfo("reset task list buffer init flag for leader");
2,216!
3220
  execInfo.initTaskList = false;
2,216✔
3221
}
2,216✔
3222

3223
void mndUpdateStreamExecInfoRole(SMnode *pMnode, int32_t role) {
2,582✔
3224
  execInfo.switchFromFollower = false;
2,582✔
3225

3226
  if (execInfo.role == NODE_ROLE_UNINIT) {
2,582✔
3227
    execInfo.role = role;
2,362✔
3228
    if (role == NODE_ROLE_LEADER) {
2,362✔
3229
      mInfo("init mnode is set to leader");
2,146!
3230
    } else {
3231
      mInfo("init mnode is set to follower");
216!
3232
    }
3233
  } else {
3234
    if (role == NODE_ROLE_LEADER) {
220✔
3235
      if (execInfo.role == NODE_ROLE_FOLLOWER) {
70!
3236
        execInfo.role = role;
70✔
3237
        execInfo.switchFromFollower = true;
70✔
3238
        mInfo("mnode switch to be leader from follower");
70!
3239
      } else {
3240
        mInfo("mnode remain to be leader, do nothing");
×
3241
      }
3242
    } else {  // follower's
3243
      if (execInfo.role == NODE_ROLE_LEADER) {
150✔
3244
        execInfo.role = role;
3✔
3245
        mInfo("mnode switch to be follower from leader");
3!
3246
      } else {
3247
        mInfo("mnode remain to be follower, do nothing");
147!
3248
      }
3249
    }
3250
  }
3251
}
2,582✔
3252

3253
void addAllStreamTasksIntoBuf(SMnode *pMnode, SStreamExecInfo *pExecInfo) {
142✔
3254
  SSdb       *pSdb = pMnode->pSdb;
142✔
3255
  SStreamObj *pStream = NULL;
142✔
3256
  void       *pIter = NULL;
142✔
3257

3258
  while (1) {
3259
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
382✔
3260
    if (pIter == NULL) {
382✔
3261
      break;
142✔
3262
    }
3263

3264
    saveTaskAndNodeInfoIntoBuf(pStream, pExecInfo);
240✔
3265
    sdbRelease(pSdb, pStream);
240✔
3266
  }
3267
}
142✔
3268

3269
int32_t mndCreateStreamChkptInfoUpdateTrans(SMnode *pMnode, SStreamObj *pStream, SArray *pChkptInfoList) {
232✔
3270
  STrans *pTrans = NULL;
232✔
3271
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_UPDATE_NAME,
232✔
3272
                               "update checkpoint-info", &pTrans);
3273
  if (pTrans == NULL || code) {
232!
3274
    return code;
×
3275
  }
3276

3277
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_UPDATE_NAME, pStream->uid);
232✔
3278
  if (code) {
232!
3279
    mndTransDrop(pTrans);
×
3280
    return code;
×
3281
  }
3282

3283
  code = mndStreamSetUpdateChkptAction(pMnode, pTrans, pStream);
232✔
3284
  if (code) {
232!
3285
    mndTransDrop(pTrans);
×
3286
    return code;
×
3287
  }
3288

3289
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
232✔
3290
  if (code) {
232!
3291
    mndTransDrop(pTrans);
×
3292
    return code;
×
3293
  }
3294

3295
  code = mndTransPrepare(pMnode, pTrans);
232✔
3296
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
232!
3297
    mError("trans:%d, failed to prepare update checkpoint-info meta trans since %s", pTrans->id, tstrerror(code));
×
3298
    mndTransDrop(pTrans);
×
3299
    return code;
×
3300
  }
3301

3302
  mndTransDrop(pTrans);
232✔
3303
  return TSDB_CODE_ACTION_IN_PROGRESS;
232✔
3304
}
3305

3306
static int32_t mndProcessDropOrphanTaskReq(SRpcMsg *pReq) {
×
3307
  SMnode      *pMnode = pReq->info.node;
×
3308
  int32_t      code = 0;
×
3309
  SOrphanTask *pTask = NULL;
×
3310
  int32_t      i = 0;
×
3311
  STrans      *pTrans = NULL;
×
3312
  int32_t      numOfTasks = 0;
×
3313

3314
  SMStreamDropOrphanMsg msg = {0};
×
3315
  code = tDeserializeDropOrphanTaskMsg(pReq->pCont, pReq->contLen, &msg);
×
3316
  if (code) {
×
3317
    return code;
×
3318
  }
3319

3320
  numOfTasks = taosArrayGetSize(msg.pList);
×
3321
  if (numOfTasks == 0) {
×
3322
    mDebug("no orphan tasks to drop, no need to create trans");
×
3323
    goto _err;
×
3324
  }
3325

3326
  mDebug("create trans to drop %d orphan tasks", numOfTasks);
×
3327

3328
  i = 0;
×
3329
  while (i < numOfTasks && ((pTask = taosArrayGet(msg.pList, i)) == NULL)) {
×
3330
    i += 1;
×
3331
  }
3332

3333
  if (pTask == NULL) {
×
3334
    mError("failed to extract entry in drop orphan task list, not create trans to drop orphan-task");
×
3335
    goto _err;
×
3336
  }
3337

3338
  // check if it is conflict with other trans in both sourceDb and targetDb.
3339
  code = mndStreamTransConflictCheck(pMnode, pTask->streamId, MND_STREAM_DROP_NAME, false);
×
3340
  if (code) {
×
3341
    goto _err;
×
3342
  }
3343

3344
  SStreamObj dummyObj = {.uid = pTask->streamId, .sourceDb = "", .targetSTbName = ""};
×
3345

3346
  code = doCreateTrans(pMnode, &dummyObj, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, "drop stream", &pTrans);
×
3347
  if (pTrans == NULL || code != 0) {
×
3348
    mError("failed to create trans to drop orphan tasks since %s", tstrerror(code));
×
3349
    goto _err;
×
3350
  }
3351

3352
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_DROP_NAME, pTask->streamId);
×
3353
  if (code) {
×
3354
    goto _err;
×
3355
  }
3356

3357
  // drop all tasks
3358
  if ((code = mndStreamSetDropActionFromList(pMnode, pTrans, msg.pList)) < 0) {
×
3359
    mError("failed to create trans to drop orphan tasks since %s", tstrerror(code));
×
3360
    goto _err;
×
3361
  }
3362

3363
  // drop stream
3364
  if ((code = mndPersistTransLog(&dummyObj, pTrans, SDB_STATUS_DROPPED)) < 0) {
×
3365
    goto _err;
×
3366
  }
3367

3368
  code = mndTransPrepare(pMnode, pTrans);
×
3369
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
3370
    mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
3371
    goto _err;
×
3372
  }
3373

3374
_err:
×
3375
  tDestroyDropOrphanTaskMsg(&msg);
×
3376
  mndTransDrop(pTrans);
×
3377

3378
  if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {
×
3379
    mDebug("create drop %d orphan tasks trans succ", numOfTasks);
×
3380
  }
3381
  return code;
×
3382
}
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