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

taosdata / TDengine / #3561

19 Dec 2024 03:15AM UTC coverage: 58.812% (-1.3%) from 60.124%
#3561

push

travis-ci

web-flow
Merge pull request #29213 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

130770 of 287658 branches covered (45.46%)

Branch coverage included in aggregate %.

32 of 78 new or added lines in 4 files covered. (41.03%)

7347 existing lines in 166 files now uncovered.

205356 of 283866 relevant lines covered (72.34%)

7187865.64 hits per line

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

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

16
#include "audit.h"
17
#include "mndDb.h"
18
#include "mndPrivilege.h"
19
#include "mndScheduler.h"
20
#include "mndShow.h"
21
#include "mndStb.h"
22
#include "mndStream.h"
23
#include "mndTrans.h"
24
#include "osMemory.h"
25
#include "parser.h"
26
#include "taoserror.h"
27
#include "tmisce.h"
28
#include "tname.h"
29

30
#define MND_STREAM_MAX_NUM 60
31

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

36
static int32_t  mndNodeCheckSentinel = 0;
37
SStreamExecInfo execInfo;
38

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

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

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

67
static void     addAllStreamTasksIntoBuf(SMnode *pMnode, SStreamExecInfo *pExecInfo);
68
static void     removeExpiredNodeInfo(const SArray *pNodeSnapshot);
69
static int32_t  doKillCheckpointTrans(SMnode *pMnode, const char *pDbName, size_t len);
70
static SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw);
71

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

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

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

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

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

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

131
  mndSetMsgHandle(pMnode, TDMT_MND_PAUSE_STREAM, mndProcessPauseStreamReq);
1,366✔
132
  mndSetMsgHandle(pMnode, TDMT_MND_RESUME_STREAM, mndProcessResumeStreamReq);
1,366✔
133

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

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

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

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

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

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

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

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

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

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

190
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
5,331!
191

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

195
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
5,331!
196

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

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

206
_over:
5,331✔
207
  taosMemoryFreeClear(buf);
5,331!
208

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

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

220
    terrno = 0;
5,331✔
221
    return pRow;
5,331✔
222
  }
223
}
224

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

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

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

242
  taosWLockLatch(&pOldStream->lock);
2,271✔
243

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

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

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

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

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

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

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

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

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

309
  return TSDB_CODE_SUCCESS;
1,629✔
310
}
311

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

552
  return code;
13,456✔
553
}
554

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

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

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

578
  destroyStreamTaskIter(pIter);
1,653✔
579

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

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

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

598
  return code;
1,653✔
599
}
600

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

734
    sdbRelease(pMnode->pSdb, pStream);
1,943✔
735

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

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

751
  return TSDB_CODE_SUCCESS;
1,628✔
752
}
753

754
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq) {
1,633✔
755
  SMnode     *pMnode = pReq->info.node;
1,633✔
756
  SStreamObj *pStream = NULL;
1,633✔
757
  SStreamObj  streamObj = {0};
1,633✔
758
  char       *sql = NULL;
1,633✔
759
  int32_t     sqlLen = 0;
1,633✔
760
  const char *pMsg = "create stream tasks on dnodes";
1,633✔
761
  int32_t     code = TSDB_CODE_SUCCESS;
1,633✔
762
  int32_t     lino = 0;
1,633✔
763
  STrans     *pTrans = NULL;
1,633✔
764

765
  SCMCreateStreamReq createReq = {0};
1,633✔
766
  code = tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, &createReq);
1,633✔
767
  TSDB_CHECK_CODE(code, lino, _OVER);
1,633!
768

769
#ifdef WINDOWS
770
  code = TSDB_CODE_MND_INVALID_PLATFORM;
771
  goto _OVER;
772
#endif
773

774
  mInfo("stream:%s, start to create stream, sql:%s", createReq.name, createReq.sql);
1,633!
775
  if ((code = mndCheckCreateStreamReq(&createReq)) != 0) {
1,633!
776
    mError("stream:%s, failed to create since %s", createReq.name, tstrerror(code));
×
777
    goto _OVER;
×
778
  }
779

780
  code = mndAcquireStream(pMnode, createReq.name, &pStream);
1,633✔
781
  if (pStream != NULL && code == 0) {
1,633!
782
    if (createReq.igExists) {
2✔
783
      mInfo("stream:%s, already exist, ignore exist is set", createReq.name);
1!
784
      mndReleaseStream(pMnode, pStream);
1✔
785
      tFreeSCMCreateStreamReq(&createReq);
1✔
786
      return code;
1✔
787
    } else {
788
      code = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
1✔
789
      goto _OVER;
1✔
790
    }
791
  } else if (code != TSDB_CODE_MND_STREAM_NOT_EXIST) {
1,631!
792
    goto _OVER;
×
793
  }
794

795
  if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
1,631!
796
    goto _OVER;
×
797
  }
798

799
  if (createReq.sql != NULL) {
1,631!
800
    sql = taosStrdup(createReq.sql);
1,631!
801
    TSDB_CHECK_NULL(sql, code, lino, _OVER, terrno);
1,631!
802
  }
803

804
  SDbObj *pSourceDb = mndAcquireDb(pMnode, createReq.sourceDB);
1,631✔
805
  if (pSourceDb == NULL) {
1,631!
806
    code = terrno;
×
807
    mInfo("stream:%s failed to create, acquire source db %s failed, code:%s", createReq.name, createReq.sourceDB,
×
808
          tstrerror(code));
809
    goto _OVER;
×
810
  }
811

812
  code = mndCheckForSnode(pMnode, pSourceDb);
1,631✔
813
  mndReleaseDb(pMnode, pSourceDb);
1,631✔
814
  if (code != 0) {
1,631✔
815
    goto _OVER;
2✔
816
  }
817

818
  // build stream obj from request
819
  if ((code = mndBuildStreamObjFromCreateReq(pMnode, &streamObj, &createReq)) < 0) {
1,629!
820
    mError("stream:%s, failed to create since %s", createReq.name, tstrerror(code));
×
821
    goto _OVER;
×
822
  }
823

824
  code = doStreamCheck(pMnode, &streamObj);
1,629✔
825
  TSDB_CHECK_CODE(code, lino, _OVER);
1,629✔
826

827
  code = doCreateTrans(pMnode, &streamObj, pReq, TRN_CONFLICT_DB, MND_STREAM_CREATE_NAME, pMsg, &pTrans);
1,628✔
828
  if (pTrans == NULL || code) {
1,628!
829
    goto _OVER;
×
830
  }
831

832
  // create stb for stream
833
  if (createReq.createStb == STREAM_CREATE_STABLE_TRUE) {
1,628✔
834
    if ((code = mndCreateStbForStream(pMnode, pTrans, &streamObj, pReq->info.conn.user)) < 0) {
1,486!
835
      mError("trans:%d, failed to create stb for stream %s since %s", pTrans->id, createReq.name, tstrerror(code));
×
836
      mndTransDrop(pTrans);
×
837
      goto _OVER;
×
838
    }
839
  } else {
840
    mDebug("stream:%s no need create stable", createReq.name);
142✔
841
  }
842

843
  // schedule stream task for stream obj
844
  code = mndScheduleStream(pMnode, &streamObj, createReq.lastTs, createReq.pVgroupVerList);
1,628✔
845
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,628!
846
    mError("stream:%s, failed to schedule since %s", createReq.name, tstrerror(code));
×
847
    mndTransDrop(pTrans);
×
848
    goto _OVER;
×
849
  }
850

851
  // add stream to trans
852
  code = mndPersistStream(pTrans, &streamObj);
1,628✔
853
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,628!
854
    mError("stream:%s, failed to persist since %s", createReq.name, tstrerror(code));
×
855
    mndTransDrop(pTrans);
×
856
    goto _OVER;
×
857
  }
858

859
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_READ_DB, streamObj.sourceDb)) != 0) {
1,628!
860
    mndTransDrop(pTrans);
×
861
    goto _OVER;
×
862
  }
863

864
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, streamObj.targetDb)) != 0) {
1,628!
865
    mndTransDrop(pTrans);
×
866
    goto _OVER;
×
867
  }
868

869
  // add into buffer firstly
870
  // to make sure when the hb from vnode arrived, the newly created tasks have been in the task map already.
871
  streamMutexLock(&execInfo.lock);
1,628✔
872
  mDebug("stream stream:%s start to register tasks into task nodeList and set initial checkpointId", createReq.name);
1,628✔
873
  saveTaskAndNodeInfoIntoBuf(&streamObj, &execInfo);
1,628✔
874
  streamMutexUnlock(&execInfo.lock);
1,628✔
875

876
  // execute creation
877
  code = mndTransPrepare(pMnode, pTrans);
1,628✔
878
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,628!
879
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
880
    mndTransDrop(pTrans);
×
881
    goto _OVER;
×
882
  }
883

884
  mndTransDrop(pTrans);
1,628✔
885

886
  SName dbname = {0};
1,628✔
887
  code = tNameFromString(&dbname, createReq.sourceDB, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
1,628✔
888
  if (code) {
1,628!
889
    mError("invalid source dbname:%s in create stream, code:%s", createReq.sourceDB, tstrerror(code));
×
890
    goto _OVER;
×
891
  }
892

893
  SName name = {0};
1,628✔
894
  code = tNameFromString(&name, createReq.name, T_NAME_ACCT | T_NAME_TABLE);
1,628✔
895
  if (code) {
1,628!
896
    mError("invalid stream name:%s in create strem, code:%s", createReq.name, tstrerror(code));
×
897
    goto _OVER;
×
898
  }
899

900
  // reuse this function for stream
901
  if (sql != NULL && sqlLen > 0) {
1,628!
902
    auditRecord(pReq, pMnode->clusterId, "createStream", dbname.dbname, name.dbname, sql, sqlLen);
×
903
  } else {
904
    char detail[1000] = {0};
1,628✔
905
    snprintf(detail, tListLen(detail), "dbname:%s, stream name:%s", dbname.dbname, name.dbname);
1,628✔
906
    auditRecord(pReq, pMnode->clusterId, "createStream", dbname.dbname, name.dbname, detail, strlen(detail));
1,628✔
907
  }
908

909
_OVER:
1,632✔
910
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,632!
911
    mError("stream:%s, failed to create at line:%d since %s", createReq.name, lino, tstrerror(code));
4!
912
  } else {
913
    mDebug("stream:%s create stream completed", createReq.name);
1,628✔
914
    code = TSDB_CODE_ACTION_IN_PROGRESS;
1,628✔
915
  }
916

917
  mndReleaseStream(pMnode, pStream);
1,632✔
918
  tFreeSCMCreateStreamReq(&createReq);
1,632✔
919
  tFreeStreamObj(&streamObj);
1,632✔
920

921
  if (sql != NULL) {
1,632✔
922
    taosMemoryFreeClear(sql);
1,631!
923
  }
924

925
  return code;
1,632✔
926
}
927

928
static int32_t mndProcessRestartStreamReq(SRpcMsg *pReq) {
×
929
  SMnode          *pMnode = pReq->info.node;
×
930
  SStreamObj      *pStream = NULL;
×
931
  int32_t          code = 0;
×
932
  SMPauseStreamReq pauseReq = {0};
×
933

934
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
×
935
    return TSDB_CODE_INVALID_MSG;
×
936
  }
937

938
  code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
×
939
  if (pStream == NULL || code != 0) {
×
940
    if (pauseReq.igNotExists) {
×
941
      mInfo("stream:%s, not exist, not restart stream", pauseReq.name);
×
942
      return 0;
×
943
    } else {
944
      mError("stream:%s not exist, failed to restart stream", pauseReq.name);
×
945
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
946
    }
947
  }
948

949
  mInfo("stream:%s,%" PRId64 " start to restart stream", pauseReq.name, pStream->uid);
×
950
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb)) != 0) {
×
951
    sdbRelease(pMnode->pSdb, pStream);
×
952
    return code;
×
953
  }
954

955
  // check if it is conflict with other trans in both sourceDb and targetDb.
956
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_RESTART_NAME, true);
×
957
  if (code) {
×
958
    sdbRelease(pMnode->pSdb, pStream);
×
959
    return code;
×
960
  }
961

962
  bool updated = mndStreamNodeIsUpdated(pMnode);
×
963
  if (updated) {
×
964
    mError("tasks are not ready for restart, node update detected");
×
965
    sdbRelease(pMnode->pSdb, pStream);
×
966
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
967
  }
968

969
  STrans *pTrans = NULL;
×
970
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RESTART_NAME, "restart the stream",
×
971
                       &pTrans);
972
  if (pTrans == NULL || code) {
×
973
    mError("stream:%s failed to pause stream since %s", pauseReq.name, tstrerror(code));
×
974
    sdbRelease(pMnode->pSdb, pStream);
×
975
    return code;
×
976
  }
977

978
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_RESTART_NAME, pStream->uid);
×
979
  if (code) {
×
980
    sdbRelease(pMnode->pSdb, pStream);
×
981
    mndTransDrop(pTrans);
×
982
    return code;
×
983
  }
984

985
  // if nodeUpdate happened, not send pause trans
986
  code = mndStreamSetRestartAction(pMnode, pTrans, pStream);
×
987
  if (code) {
×
988
    mError("stream:%s, failed to restart task since %s", pauseReq.name, tstrerror(code));
×
989
    sdbRelease(pMnode->pSdb, pStream);
×
990
    mndTransDrop(pTrans);
×
991
    return code;
×
992
  }
993

994
  code = mndTransPrepare(pMnode, pTrans);
×
995
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
996
    mError("trans:%d, failed to prepare restart stream trans since %s", pTrans->id, tstrerror(code));
×
997
    sdbRelease(pMnode->pSdb, pStream);
×
998
    mndTransDrop(pTrans);
×
999
    return code;
×
1000
  }
1001

1002
  sdbRelease(pMnode->pSdb, pStream);
×
1003
  mndTransDrop(pTrans);
×
1004

1005
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
1006
}
1007

1008
int64_t mndStreamGenChkptId(SMnode *pMnode, bool lock) {
753✔
1009
  SStreamObj *pStream = NULL;
753✔
1010
  void       *pIter = NULL;
753✔
1011
  SSdb       *pSdb = pMnode->pSdb;
753✔
1012
  int64_t     maxChkptId = 0;
753✔
1013

1014
  while (1) {
1015
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
2,301✔
1016
    if (pIter == NULL) break;
2,301✔
1017

1018
    maxChkptId = TMAX(maxChkptId, pStream->checkpointId);
1,548✔
1019
    mDebug("stream:%p, %s id:0x%" PRIx64 " checkpoint %" PRId64 "", pStream, pStream->name, pStream->uid,
1,548✔
1020
           pStream->checkpointId);
1021
    sdbRelease(pSdb, pStream);
1,548✔
1022
  }
1023

1024
  {  // check the max checkpoint id from all vnodes.
1025
    int64_t maxCheckpointId = -1;
753✔
1026
    if (lock) {
753✔
1027
      streamMutexLock(&execInfo.lock);
22✔
1028
    }
1029

1030
    for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
9,532✔
1031
      STaskId          *p = taosArrayGet(execInfo.pTaskList, i);
8,779✔
1032
      STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
8,779✔
1033
      if (p == NULL || pEntry == NULL) {
8,779!
1034
        continue;
×
1035
      }
1036

1037
      if (pEntry->checkpointInfo.failed) {
8,779!
1038
        continue;
×
1039
      }
1040

1041
      if (maxCheckpointId < pEntry->checkpointInfo.latestId) {
8,779✔
1042
        maxCheckpointId = pEntry->checkpointInfo.latestId;
820✔
1043
      }
1044
    }
1045

1046
    if (lock) {
753✔
1047
      streamMutexUnlock(&execInfo.lock);
22✔
1048
    }
1049

1050
    if (maxCheckpointId > maxChkptId) {
753!
1051
      mDebug("max checkpointId in mnode:%" PRId64 ", smaller than max checkpointId in vnode:%" PRId64, maxChkptId,
×
1052
             maxCheckpointId);
1053
      maxChkptId = maxCheckpointId;
×
1054
    }
1055
  }
1056

1057
  mDebug("generate new checkpointId:%" PRId64, maxChkptId + 1);
753✔
1058
  return maxChkptId + 1;
753✔
1059
}
1060

1061
static int32_t mndProcessStreamCheckpointTrans(SMnode *pMnode, SStreamObj *pStream, int64_t checkpointId,
755✔
1062
                                               int8_t mndTrigger, bool lock) {
1063
  int32_t code = TSDB_CODE_SUCCESS;
755✔
1064
  bool    conflict = false;
755✔
1065
  int64_t ts = taosGetTimestampMs();
755✔
1066
  STrans *pTrans = NULL;
755✔
1067

1068
  if (mndTrigger == 1 && (ts - pStream->checkpointFreq < tsStreamCheckpointInterval * 1000)) {
755!
1069
    return code;
×
1070
  }
1071

1072
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHECKPOINT_NAME, lock);
755✔
1073
  if (code) {
755✔
1074
    mWarn("checkpoint conflict with other trans in %s, code:%s ignore the checkpoint for stream:%s %" PRIx64,
1!
1075
          pStream->sourceDb, tstrerror(code), pStream->name, pStream->uid);
1076
    goto _ERR;
1✔
1077
  }
1078

1079
  code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHECKPOINT_NAME,
754✔
1080
                       "gen checkpoint for stream", &pTrans);
1081
  if (code) {
754!
1082
    mError("failed to checkpoint of stream name%s, checkpointId: %" PRId64 ", reason:%s", pStream->name, checkpointId,
×
1083
           tstrerror(code));
1084
    goto _ERR;
×
1085
  }
1086

1087
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHECKPOINT_NAME, pStream->uid);
754✔
1088
  if (code) {
754!
1089
    mError("failed to register checkpoint trans for stream:%s, checkpointId:%" PRId64, pStream->name, checkpointId);
×
1090
    goto _ERR;
×
1091
  }
1092

1093
  mDebug("start to trigger checkpoint for stream:%s, checkpoint: %" PRId64 "", pStream->name, checkpointId);
754✔
1094

1095
  taosWLockLatch(&pStream->lock);
754✔
1096
  pStream->currentTick = 1;
754✔
1097

1098
  // 1. redo action: broadcast checkpoint source msg for all source vg
1099
  int32_t totalLevel = taosArrayGetSize(pStream->tasks);
754✔
1100
  for (int32_t i = 0; i < totalLevel; i++) {
2,305✔
1101
    SArray      *pLevel = taosArrayGetP(pStream->tasks, i);
1,551✔
1102
    SStreamTask *p = taosArrayGetP(pLevel, 0);
1,551✔
1103

1104
    if (p->info.taskLevel == TASK_LEVEL__SOURCE) {
1,551✔
1105
      int32_t sz = taosArrayGetSize(pLevel);
754✔
1106
      for (int32_t j = 0; j < sz; j++) {
2,945✔
1107
        SStreamTask *pTask = taosArrayGetP(pLevel, j);
2,191✔
1108
        code = mndStreamSetCheckpointAction(pMnode, pTrans, pTask, checkpointId, mndTrigger);
2,191✔
1109

1110
        if (code != TSDB_CODE_SUCCESS) {
2,191!
1111
          taosWUnLockLatch(&pStream->lock);
×
1112
          goto _ERR;
×
1113
        }
1114
      }
1115
    }
1116
  }
1117

1118
  // 2. reset tick
1119
  pStream->checkpointId = checkpointId;
754✔
1120
  pStream->checkpointFreq = taosGetTimestampMs();
754✔
1121
  pStream->currentTick = 0;
754✔
1122

1123
  // 3. commit log: stream checkpoint info
1124
  pStream->version = pStream->version + 1;
754✔
1125
  taosWUnLockLatch(&pStream->lock);
754✔
1126

1127
  if ((code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY)) != TSDB_CODE_SUCCESS) {
754!
1128
    goto _ERR;
×
1129
  }
1130

1131
  code = mndTransPrepare(pMnode, pTrans);
754✔
1132
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
754!
1133
    mError("failed to prepare checkpoint trans since %s", tstrerror(code));
×
1134
  } else {
1135
    code = TSDB_CODE_ACTION_IN_PROGRESS;
754✔
1136
  }
1137

1138
_ERR:
755✔
1139
  mndTransDrop(pTrans);
755✔
1140
  return code;
755✔
1141
}
1142

1143
int32_t extractStreamNodeList(SMnode *pMnode) {
1,900✔
1144
  if (taosArrayGetSize(execInfo.pNodeList) == 0) {
1,900✔
1145
    int32_t code = refreshNodeListFromExistedStreams(pMnode, execInfo.pNodeList);
635✔
1146
    if (code) {
635!
1147
      mError("Failed to extract node list from stream, code:%s", tstrerror(code));
×
1148
      return code;
×
1149
    }
1150
  }
1151

1152
  return taosArrayGetSize(execInfo.pNodeList);
1,900✔
1153
}
1154

1155
static int32_t mndCheckTaskAndNodeStatus(SMnode *pMnode) {
1,013✔
1156
  bool ready = true;
1,013✔
1157
  if (mndStreamNodeIsUpdated(pMnode)) {
1,013✔
1158
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
30✔
1159
  }
1160

1161
  streamMutexLock(&execInfo.lock);
983✔
1162
  if (taosArrayGetSize(execInfo.pNodeList) == 0) {
983✔
1163
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
635✔
1164
    if (taosArrayGetSize(execInfo.pTaskList) != 0) {
635!
1165
      streamMutexUnlock(&execInfo.lock);
×
1166
      mError("stream task node change checking done, no vgroups exist, but task list is not empty");
×
1167
      return TSDB_CODE_FAILED;
×
1168
    }
1169
  }
1170

1171
  SArray *pInvalidList = taosArrayInit(4, sizeof(STaskId));
983✔
1172

1173
  for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
4,065✔
1174
    STaskId *p = taosArrayGet(execInfo.pTaskList, i);
3,224✔
1175
    if (p == NULL) {
3,224!
1176
      continue;
×
1177
    }
1178

1179
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
3,224✔
1180
    if (pEntry == NULL) {
3,224!
1181
      continue;
×
1182
    }
1183

1184
    if (pEntry->status == TASK_STATUS__STOP) {
3,224✔
1185
      for (int32_t j = 0; j < taosArrayGetSize(pInvalidList); ++j) {
75!
1186
        STaskId *pId = taosArrayGet(pInvalidList, j);
×
1187
        if (pId == NULL) {
×
1188
          continue;
×
1189
        }
1190

1191
        if (pEntry->id.streamId == pId->streamId) {
×
1192
          void *px = taosArrayPush(pInvalidList, &pEntry->id);
×
1193
          if (px == NULL) {
×
1194
            mError("failed to put stream into invalid list, code:%s", tstrerror(TSDB_CODE_OUT_OF_MEMORY));
×
1195
          }
1196
          break;
×
1197
        }
1198
      }
1199
    }
1200

1201
    if (pEntry->status != TASK_STATUS__READY) {
3,224✔
1202
      mDebug("s-task:0x%" PRIx64 "-0x%x (nodeId:%d) status:%s, checkpoint not issued", pEntry->id.streamId,
133✔
1203
             (int32_t)pEntry->id.taskId, pEntry->nodeId, streamTaskGetStatusStr(pEntry->status));
1204
      ready = false;
133✔
1205
      break;
133✔
1206
    }
1207

1208
    if (pEntry->hTaskId != 0) {
3,091✔
1209
      mDebug("s-task:0x%" PRIx64 "-0x%x (nodeId:%d) status:%s related fill-history task:0x%" PRIx64
9✔
1210
             " exists, checkpoint not issued",
1211
             pEntry->id.streamId, (int32_t)pEntry->id.taskId, pEntry->nodeId, streamTaskGetStatusStr(pEntry->status),
1212
             pEntry->hTaskId);
1213
      ready = false;
9✔
1214
      break;
9✔
1215
    }
1216
  }
1217

1218
  removeTasksInBuf(pInvalidList, &execInfo);
983✔
1219
  taosArrayDestroy(pInvalidList);
983✔
1220

1221
  streamMutexUnlock(&execInfo.lock);
983✔
1222
  return ready ? 0 : -1;
983✔
1223
}
1224

1225
int64_t getStreamTaskLastReadyState(SArray *pTaskList, int64_t streamId) {
470✔
1226
  int64_t ts = -1;
470✔
1227
  int32_t taskId = -1;
470✔
1228

1229
  for (int32_t i = 0; i < taosArrayGetSize(pTaskList); ++i) {
13,911✔
1230
    STaskId          *p = taosArrayGet(pTaskList, i);
13,441✔
1231
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
13,441✔
1232
    if (p == NULL || pEntry == NULL || pEntry->id.streamId != streamId) {
13,441!
1233
      continue;
11,384✔
1234
    }
1235

1236
    if (pEntry->status == TASK_STATUS__READY && ts < pEntry->startTime) {
2,057!
1237
      ts = pEntry->startTime;
1,165✔
1238
      taskId = pEntry->id.taskId;
1,165✔
1239
    }
1240
  }
1241

1242
  mDebug("stream:0x%" PRIx64 " last ready ts:%" PRId64 " s-task:0x%x", streamId, ts, taskId);
470✔
1243
  return ts;
470✔
1244
}
1245

1246
typedef struct {
1247
  int64_t streamId;
1248
  int64_t duration;
1249
} SCheckpointInterval;
1250

1251
static int32_t streamWaitComparFn(const void *p1, const void *p2) {
68✔
1252
  const SCheckpointInterval *pInt1 = p1;
68✔
1253
  const SCheckpointInterval *pInt2 = p2;
68✔
1254
  if (pInt1->duration == pInt2->duration) {
68✔
1255
    return 0;
54✔
1256
  }
1257

1258
  return pInt1->duration > pInt2->duration ? -1 : 1;
14✔
1259
}
1260

1261
static int32_t mndProcessStreamCheckpoint(SRpcMsg *pReq) {
1,013✔
1262
  SMnode     *pMnode = pReq->info.node;
1,013✔
1263
  SSdb       *pSdb = pMnode->pSdb;
1,013✔
1264
  void       *pIter = NULL;
1,013✔
1265
  SStreamObj *pStream = NULL;
1,013✔
1266
  int32_t     code = 0;
1,013✔
1267
  int32_t     numOfCheckpointTrans = 0;
1,013✔
1268

1269
  if ((code = mndCheckTaskAndNodeStatus(pMnode)) != 0) {
1,013✔
1270
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
172✔
1271
  }
1272

1273
  SArray *pList = taosArrayInit(4, sizeof(SCheckpointInterval));
841✔
1274
  if (pList == NULL) {
841!
1275
    return terrno;
×
1276
  }
1277

1278
  int64_t now = taosGetTimestampMs();
841✔
1279

1280
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream)) != NULL) {
1,417✔
1281
    int64_t duration = now - pStream->checkpointFreq;
576✔
1282
    if (duration < tsStreamCheckpointInterval * 1000) {
576✔
1283
      sdbRelease(pSdb, pStream);
106✔
1284
      continue;
509✔
1285
    }
1286

1287
    streamMutexLock(&execInfo.lock);
470✔
1288
    int64_t startTs = getStreamTaskLastReadyState(execInfo.pTaskList, pStream->uid);
470✔
1289
    if (startTs != -1 && (now - startTs) < tsStreamCheckpointInterval * 1000) {
470✔
1290
      streamMutexUnlock(&execInfo.lock);
403✔
1291
      sdbRelease(pSdb, pStream);
403✔
1292
      continue;
403✔
1293
    }
1294
    streamMutexUnlock(&execInfo.lock);
67✔
1295

1296
    SCheckpointInterval in = {.streamId = pStream->uid, .duration = duration};
67✔
1297
    void               *p = taosArrayPush(pList, &in);
67✔
1298
    if (p) {
67!
1299
      int32_t currentSize = taosArrayGetSize(pList);
67✔
1300
      mDebug("stream:%s (uid:0x%" PRIx64 ") total %d stream(s) beyond chpt interval threshold: %ds(%" PRId64
67✔
1301
             "s), concurrently launch threshold:%d",
1302
             pStream->name, pStream->uid, currentSize, tsStreamCheckpointInterval, duration / 1000,
1303
             tsMaxConcurrentCheckpoint);
1304
    } else {
1305
      mError("failed to record the checkpoint interval info, stream:0x%" PRIx64, pStream->uid);
×
1306
    }
1307
    sdbRelease(pSdb, pStream);
67✔
1308
  }
1309

1310
  int32_t size = taosArrayGetSize(pList);
841✔
1311
  if (size == 0) {
841✔
1312
    taosArrayDestroy(pList);
819✔
1313
    return code;
819✔
1314
  }
1315

1316
  taosArraySort(pList, streamWaitComparFn);
22✔
1317
  code = mndStreamClearFinishedTrans(pMnode, &numOfCheckpointTrans);
22✔
1318
  if (code) {
22!
1319
    mError("failed to clear finish trans, code:%s", tstrerror(code));
×
1320
    taosArrayDestroy(pList);
×
1321
    return code;
×
1322
  }
1323

1324
  int32_t numOfQual = taosArrayGetSize(pList);
22✔
1325
  if (numOfCheckpointTrans > tsMaxConcurrentCheckpoint) {
22!
1326
    mDebug(
×
1327
        "%d stream(s) checkpoint interval longer than %ds, ongoing checkpoint trans:%d reach maximum allowed:%d, new "
1328
        "checkpoint trans are not allowed, wait for 30s",
1329
        numOfQual, tsStreamCheckpointInterval, numOfCheckpointTrans, tsMaxConcurrentCheckpoint);
1330
    taosArrayDestroy(pList);
×
1331
    return code;
×
1332
  }
1333

1334
  int32_t capacity = tsMaxConcurrentCheckpoint - numOfCheckpointTrans;
22✔
1335
  mDebug(
22✔
1336
      "%d stream(s) checkpoint interval longer than %ds, %d ongoing checkpoint trans, %d new checkpoint trans allowed, "
1337
      "concurrent trans threshold:%d",
1338
      numOfQual, tsStreamCheckpointInterval, numOfCheckpointTrans, capacity, tsMaxConcurrentCheckpoint);
1339

1340
  int32_t started = 0;
22✔
1341
  int64_t checkpointId = mndStreamGenChkptId(pMnode, true);
22✔
1342

1343
  for (int32_t i = 0; i < numOfQual; ++i) {
26✔
1344
    SCheckpointInterval *pCheckpointInfo = taosArrayGet(pList, i);
24✔
1345
    if (pCheckpointInfo == NULL) {
24!
1346
      continue;
×
1347
    }
1348

1349
    SStreamObj *p = NULL;
24✔
1350
    code = mndGetStreamObj(pMnode, pCheckpointInfo->streamId, &p);
24✔
1351
    if (p != NULL && code == 0) {
24!
1352
      code = mndProcessStreamCheckpointTrans(pMnode, p, checkpointId, 1, true);
24✔
1353
      sdbRelease(pSdb, p);
24✔
1354

1355
      if (code == 0 || code == TSDB_CODE_ACTION_IN_PROGRESS) {
24!
1356
        started += 1;
24✔
1357

1358
        if (started >= capacity) {
24✔
1359
          mDebug("already start %d new checkpoint trans, current active checkpoint trans:%d", started,
20✔
1360
                 (started + numOfCheckpointTrans));
1361
          break;
20✔
1362
        }
1363
      } else {
UNCOV
1364
        mError("failed to start checkpoint trans, code:%s", tstrerror(code));
×
1365
      }
1366
    }
1367
  }
1368

1369
  taosArrayDestroy(pList);
22✔
1370
  return code;
22✔
1371
}
1372

1373
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) {
1,411✔
1374
  SMnode     *pMnode = pReq->info.node;
1,411✔
1375
  SStreamObj *pStream = NULL;
1,411✔
1376
  int32_t     code = 0;
1,411✔
1377

1378
  SMDropStreamReq dropReq = {0};
1,411✔
1379
  if (tDeserializeSMDropStreamReq(pReq->pCont, pReq->contLen, &dropReq) < 0) {
1,411!
1380
    mError("invalid drop stream msg recv, discarded");
×
1381
    code = TSDB_CODE_INVALID_MSG;
×
1382
    TAOS_RETURN(code);
×
1383
  }
1384

1385
  mDebug("recv drop stream:%s msg", dropReq.name);
1,411✔
1386

1387
  code = mndAcquireStream(pMnode, dropReq.name, &pStream);
1,411✔
1388
  if (pStream == NULL || code != 0) {
1,411!
1389
    if (dropReq.igNotExists) {
139✔
1390
      mInfo("stream:%s not exist, ignore not exist is set, drop stream exec done with success", dropReq.name);
129!
1391
      sdbRelease(pMnode->pSdb, pStream);
129✔
1392
      tFreeMDropStreamReq(&dropReq);
129✔
1393
      return 0;
129✔
1394
    } else {
1395
      mError("stream:%s not exist failed to drop it", dropReq.name);
10!
1396
      tFreeMDropStreamReq(&dropReq);
10✔
1397
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
10✔
1398
    }
1399
  }
1400

1401
  if (pStream->smaId != 0) {
1,272✔
1402
    mDebug("stream:%s, uid:0x%" PRIx64 " try to drop sma related stream", dropReq.name, pStream->uid);
215!
1403

1404
    void    *pIter = NULL;
215✔
1405
    SSmaObj *pSma = NULL;
215✔
1406
    pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
215✔
1407
    while (pIter) {
355✔
1408
      if (pSma && pSma->uid == pStream->smaId) {
145!
1409
        sdbRelease(pMnode->pSdb, pSma);
5✔
1410
        sdbRelease(pMnode->pSdb, pStream);
5✔
1411

1412
        sdbCancelFetch(pMnode->pSdb, pIter);
5✔
1413
        tFreeMDropStreamReq(&dropReq);
5✔
1414
        code = TSDB_CODE_TSMA_MUST_BE_DROPPED;
5✔
1415

1416
        mError("try to drop sma-related stream:%s, uid:0x%" PRIx64 " code:%s only allowed to be dropped along with sma",
5!
1417
               dropReq.name, pStream->uid, tstrerror(terrno));
1418
        TAOS_RETURN(code);
5✔
1419
      }
1420

1421
      if (pSma) {
140!
1422
        sdbRelease(pMnode->pSdb, pSma);
140✔
1423
      }
1424

1425
      pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
140✔
1426
    }
1427
  }
1428

1429
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
1,267!
1430
    sdbRelease(pMnode->pSdb, pStream);
×
1431
    tFreeMDropStreamReq(&dropReq);
×
1432
    return -1;
×
1433
  }
1434

1435
  // check if it is conflict with other trans in both sourceDb and targetDb.
1436
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_DROP_NAME, true);
1,267✔
1437
  if (code) {
1,267!
1438
    sdbRelease(pMnode->pSdb, pStream);
×
1439
    tFreeMDropStreamReq(&dropReq);
×
1440
    return code;
×
1441
  }
1442

1443
  STrans *pTrans = NULL;
1,267✔
1444
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, "drop stream", &pTrans);
1,267✔
1445
  if (pTrans == NULL || code) {
1,267!
1446
    mError("stream:%s uid:0x%" PRIx64 " failed to drop since %s", dropReq.name, pStream->uid, tstrerror(code));
×
1447
    sdbRelease(pMnode->pSdb, pStream);
×
1448
    tFreeMDropStreamReq(&dropReq);
×
1449
    TAOS_RETURN(code);
×
1450
  }
1451

1452
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_DROP_NAME, pStream->uid);
1,267✔
1453
  if (code) {
1,267!
1454
    mError("failed to register drop stream trans, code:%s", tstrerror(code));
×
1455
    sdbRelease(pMnode->pSdb, pStream);
×
1456
    mndTransDrop(pTrans);
×
1457
    tFreeMDropStreamReq(&dropReq);
×
1458
    TAOS_RETURN(code);
×
1459
  }
1460

1461
  // drop all tasks
1462
  code = mndStreamSetDropAction(pMnode, pTrans, pStream);
1,267✔
1463
  if (code) {
1,267!
1464
    mError("stream:%s uid:0x%" PRIx64 " failed to drop task since %s", dropReq.name, pStream->uid, tstrerror(code));
×
1465
    sdbRelease(pMnode->pSdb, pStream);
×
1466
    mndTransDrop(pTrans);
×
1467
    tFreeMDropStreamReq(&dropReq);
×
1468
    TAOS_RETURN(code);
×
1469
  }
1470

1471
  // drop stream
1472
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_DROPPED);
1,267✔
1473
  if (code) {
1,267!
1474
    sdbRelease(pMnode->pSdb, pStream);
×
1475
    mndTransDrop(pTrans);
×
1476
    tFreeMDropStreamReq(&dropReq);
×
1477
    TAOS_RETURN(code);
×
1478
  }
1479

1480
  code = mndTransPrepare(pMnode, pTrans);
1,267✔
1481
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,267!
1482
    mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
1483
    sdbRelease(pMnode->pSdb, pStream);
×
1484
    mndTransDrop(pTrans);
×
1485
    tFreeMDropStreamReq(&dropReq);
×
1486
    TAOS_RETURN(code);
×
1487
  }
1488

1489
  // kill the related checkpoint trans
1490
  int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
1,267✔
1491
  if (transId != 0) {
1,267!
1492
    mDebug("drop active transId:%d due to stream:%s uid:0x%" PRIx64 " dropped", transId, pStream->name, pStream->uid);
×
1493
    mndKillTransImpl(pMnode, transId, pStream->sourceDb);
×
1494
  }
1495

1496
  mDebug("stream:%s uid:0x%" PRIx64 " transId:%d start to drop related task when dropping stream", dropReq.name,
1,267✔
1497
         pStream->uid, transId);
1498

1499
  removeStreamTasksInBuf(pStream, &execInfo);
1,267✔
1500

1501
  SName name = {0};
1,267✔
1502
  code = tNameFromString(&name, dropReq.name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
1,267✔
1503
  auditRecord(pReq, pMnode->clusterId, "dropStream", "", name.dbname, dropReq.sql, dropReq.sqlLen);
1,267✔
1504

1505
  sdbRelease(pMnode->pSdb, pStream);
1,267✔
1506
  mndTransDrop(pTrans);
1,267✔
1507
  tFreeMDropStreamReq(&dropReq);
1,267✔
1508

1509
  if (code == 0) {
1,267✔
1510
    return TSDB_CODE_ACTION_IN_PROGRESS;
1,257✔
1511
  } else {
1512
    TAOS_RETURN(code);
10✔
1513
  }
1514
}
1515

1516
int32_t mndDropStreamByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
1,557✔
1517
  SSdb   *pSdb = pMnode->pSdb;
1,557✔
1518
  void   *pIter = NULL;
1,557✔
1519
  int32_t code = 0;
1,557✔
1520

1521
  while (1) {
92✔
1522
    SStreamObj *pStream = NULL;
1,649✔
1523
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
1,649✔
1524
    if (pIter == NULL) break;
1,649✔
1525

1526
    if (pStream->sourceDbUid == pDb->uid || pStream->targetDbUid == pDb->uid) {
93✔
1527
      if (pStream->sourceDbUid != pStream->targetDbUid) {
75✔
1528
        sdbRelease(pSdb, pStream);
1✔
1529
        sdbCancelFetch(pSdb, pIter);
1✔
1530
        mError("db:%s, failed to drop stream:%s since sourceDbUid:%" PRId64 " not match with targetDbUid:%" PRId64,
1!
1531
               pDb->name, pStream->name, pStream->sourceDbUid, pStream->targetDbUid);
1532
        TAOS_RETURN(TSDB_CODE_MND_STREAM_MUST_BE_DELETED);
1✔
1533
      } else {
1534
        // kill the related checkpoint trans
1535
        int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
74✔
1536
        if (transId != 0) {
74!
1537
          mDebug("drop active related transId:%d due to stream:%s dropped", transId, pStream->name);
×
1538
          mndKillTransImpl(pMnode, transId, pStream->sourceDb);
×
1539
        }
1540

1541
        // drop the stream obj in execInfo
1542
        removeStreamTasksInBuf(pStream, &execInfo);
74✔
1543

1544
        code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_DROPPED);
74✔
1545
        if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
74!
1546
          sdbRelease(pSdb, pStream);
×
1547
          sdbCancelFetch(pSdb, pIter);
×
1548
          return code;
×
1549
        }
1550
      }
1551
    }
1552

1553
    sdbRelease(pSdb, pStream);
92✔
1554
  }
1555

1556
  return 0;
1,556✔
1557
}
1558

1559
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
463✔
1560
  SMnode     *pMnode = pReq->info.node;
463✔
1561
  SSdb       *pSdb = pMnode->pSdb;
463✔
1562
  int32_t     numOfRows = 0;
463✔
1563
  SStreamObj *pStream = NULL;
463✔
1564
  int32_t     code = 0;
463✔
1565

1566
  while (numOfRows < rows) {
1,272!
1567
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
1,272✔
1568
    if (pShow->pIter == NULL) break;
1,272✔
1569

1570
    code = setStreamAttrInResBlock(pStream, pBlock, numOfRows);
809✔
1571
    if (code == 0) {
809!
1572
      numOfRows++;
809✔
1573
    }
1574
    sdbRelease(pSdb, pStream);
809✔
1575
  }
1576

1577
  pShow->numOfRows += numOfRows;
463✔
1578
  return numOfRows;
463✔
1579
}
1580

1581
static void mndCancelGetNextStream(SMnode *pMnode, void *pIter) {
×
1582
  SSdb *pSdb = pMnode->pSdb;
×
1583
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
1584
}
×
1585

1586
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
2,726✔
1587
  SMnode     *pMnode = pReq->info.node;
2,726✔
1588
  SSdb       *pSdb = pMnode->pSdb;
2,726✔
1589
  int32_t     numOfRows = 0;
2,726✔
1590
  SStreamObj *pStream = NULL;
2,726✔
1591
  int32_t     code = 0;
2,726✔
1592

1593
  streamMutexLock(&execInfo.lock);
2,726✔
1594
  mndInitStreamExecInfo(pMnode, &execInfo);
2,726✔
1595
  streamMutexUnlock(&execInfo.lock);
2,726✔
1596

1597
  while (numOfRows < rowsCapacity) {
11,306✔
1598
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
11,266✔
1599
    if (pShow->pIter == NULL) {
11,266✔
1600
      break;
2,686✔
1601
    }
1602

1603
    // lock
1604
    taosRLockLatch(&pStream->lock);
8,580✔
1605

1606
    int32_t count = mndGetNumOfStreamTasks(pStream);
8,580✔
1607
    if (numOfRows + count > rowsCapacity) {
8,580✔
1608
      code = blockDataEnsureCapacity(pBlock, numOfRows + count);
30✔
1609
      if (code) {
30!
1610
        mError("failed to prepare the result block buffer, quit return value");
×
1611
        taosRUnLockLatch(&pStream->lock);
×
1612
        sdbRelease(pSdb, pStream);
×
1613
        continue;
×
1614
      }
1615
    }
1616

1617
    int32_t precision = TSDB_TIME_PRECISION_MILLI;
8,580✔
1618
    SDbObj *pSourceDb = mndAcquireDb(pMnode, pStream->sourceDb);
8,580✔
1619
    if (pSourceDb != NULL) {
8,580!
1620
      precision = pSourceDb->cfg.precision;
8,580✔
1621
      mndReleaseDb(pMnode, pSourceDb);
8,580✔
1622
    }
1623

1624
    // add row for each task
1625
    SStreamTaskIter *pIter = NULL;
8,580✔
1626
    code = createStreamTaskIter(pStream, &pIter);
8,580✔
1627
    if (code) {
8,580!
1628
      taosRUnLockLatch(&pStream->lock);
×
1629
      sdbRelease(pSdb, pStream);
×
1630
      mError("failed to create task iter for stream:%s", pStream->name);
×
1631
      continue;
×
1632
    }
1633

1634
    while (streamTaskIterNextTask(pIter)) {
44,771✔
1635
      SStreamTask *pTask = NULL;
36,191✔
1636
      code = streamTaskIterGetCurrent(pIter, &pTask);
36,191✔
1637
      if (code) {
36,191!
1638
        destroyStreamTaskIter(pIter);
×
1639
        break;
×
1640
      }
1641

1642
      code = setTaskAttrInResBlock(pStream, pTask, pBlock, numOfRows, precision);
36,191✔
1643
      if (code == TSDB_CODE_SUCCESS) {
36,191!
1644
        numOfRows++;
36,191✔
1645
      }
1646
    }
1647

1648
    pBlock->info.rows = numOfRows;
8,580✔
1649

1650
    destroyStreamTaskIter(pIter);
8,580✔
1651
    taosRUnLockLatch(&pStream->lock);
8,580✔
1652

1653
    sdbRelease(pSdb, pStream);
8,580✔
1654
  }
1655

1656
  pShow->numOfRows += numOfRows;
2,726✔
1657
  return numOfRows;
2,726✔
1658
}
1659

1660
static void mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter) {
×
1661
  SSdb *pSdb = pMnode->pSdb;
×
1662
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
1663
}
×
1664

1665
static int32_t mndProcessPauseStreamReq(SRpcMsg *pReq) {
696✔
1666
  SMnode     *pMnode = pReq->info.node;
696✔
1667
  SStreamObj *pStream = NULL;
696✔
1668
  int32_t     code = 0;
696✔
1669

1670
  SMPauseStreamReq pauseReq = {0};
696✔
1671
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
696!
1672
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1673
  }
1674

1675
  code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
696✔
1676
  if (pStream == NULL || code != 0) {
696!
1677
    if (pauseReq.igNotExists) {
359✔
1678
      mInfo("stream:%s, not exist, not pause stream", pauseReq.name);
106!
1679
      return 0;
106✔
1680
    } else {
1681
      mError("stream:%s not exist, failed to pause stream", pauseReq.name);
253!
1682
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
253✔
1683
    }
1684
  }
1685

1686
  mInfo("stream:%s,%" PRId64 " start to pause stream", pauseReq.name, pStream->uid);
337!
1687

1688
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb)) != 0) {
337!
1689
    sdbRelease(pMnode->pSdb, pStream);
×
1690
    return code;
×
1691
  }
1692

1693
  // check if it is conflict with other trans in both sourceDb and targetDb.
1694
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_PAUSE_NAME, true);
337✔
1695
  if (code) {
337!
1696
    sdbRelease(pMnode->pSdb, pStream);
×
1697
    TAOS_RETURN(code);
×
1698
  }
1699

1700
  bool updated = mndStreamNodeIsUpdated(pMnode);
337✔
1701
  if (updated) {
337!
1702
    mError("tasks are not ready for pause, node update detected");
×
1703
    sdbRelease(pMnode->pSdb, pStream);
×
1704
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1705
  }
1706

1707
  {  // check for tasks, if tasks are not ready, not allowed to pause
1708
    bool found = false;
337✔
1709
    bool readyToPause = true;
337✔
1710
    streamMutexLock(&execInfo.lock);
337✔
1711

1712
    for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
4,890✔
1713
      STaskId *p = taosArrayGet(execInfo.pTaskList, i);
4,553✔
1714
      if (p == NULL) {
4,553!
1715
        continue;
×
1716
      }
1717

1718
      STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
4,553✔
1719
      if (pEntry == NULL) {
4,553!
1720
        continue;
×
1721
      }
1722

1723
      if (pEntry->id.streamId != pStream->uid) {
4,553✔
1724
        continue;
2,943✔
1725
      }
1726

1727
      if (pEntry->status == TASK_STATUS__UNINIT || pEntry->status == TASK_STATUS__CK) {
1,610!
1728
        mError("stream:%s uid:0x%" PRIx64 " vgId:%d task:0x%" PRIx64 " status:%s, not ready for pause", pStream->name,
180!
1729
               pStream->uid, pEntry->nodeId, pEntry->id.taskId, streamTaskGetStatusStr(pEntry->status));
1730
        readyToPause = false;
180✔
1731
      }
1732

1733
      found = true;
1,610✔
1734
    }
1735

1736
    streamMutexUnlock(&execInfo.lock);
337✔
1737
    if (!found) {
337!
1738
      mError("stream:%s task not report status yet, not ready for pause", pauseReq.name);
×
1739
      sdbRelease(pMnode->pSdb, pStream);
×
1740
      TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1741
    }
1742

1743
    if (!readyToPause) {
337✔
1744
      mError("stream:%s task not ready for pause yet", pauseReq.name);
44!
1745
      sdbRelease(pMnode->pSdb, pStream);
44✔
1746
      TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
44✔
1747
    }
1748
  }
1749

1750
  STrans *pTrans = NULL;
293✔
1751
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_PAUSE_NAME, "pause the stream", &pTrans);
293✔
1752
  if (pTrans == NULL || code) {
293!
1753
    mError("stream:%s failed to pause stream since %s", pauseReq.name, tstrerror(code));
×
1754
    sdbRelease(pMnode->pSdb, pStream);
×
1755
    return code;
×
1756
  }
1757

1758
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_PAUSE_NAME, pStream->uid);
293✔
1759
  if (code) {
293!
1760
    sdbRelease(pMnode->pSdb, pStream);
×
1761
    mndTransDrop(pTrans);
×
1762
    return code;
×
1763
  }
1764

1765
  // if nodeUpdate happened, not send pause trans
1766
  code = mndStreamSetPauseAction(pMnode, pTrans, pStream);
293✔
1767
  if (code) {
293!
1768
    mError("stream:%s, failed to pause task since %s", pauseReq.name, tstrerror(code));
×
1769
    sdbRelease(pMnode->pSdb, pStream);
×
1770
    mndTransDrop(pTrans);
×
1771
    return code;
×
1772
  }
1773

1774
  // pause stream
1775
  taosWLockLatch(&pStream->lock);
293✔
1776
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
293✔
1777
  if (code) {
293!
1778
    taosWUnLockLatch(&pStream->lock);
×
1779
    sdbRelease(pMnode->pSdb, pStream);
×
1780
    mndTransDrop(pTrans);
×
1781
    return code;
×
1782
  }
1783

1784
  taosWUnLockLatch(&pStream->lock);
293✔
1785

1786
  code = mndTransPrepare(pMnode, pTrans);
293✔
1787
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
293!
1788
    mError("trans:%d, failed to prepare pause stream trans since %s", pTrans->id, tstrerror(code));
×
1789
    sdbRelease(pMnode->pSdb, pStream);
×
1790
    mndTransDrop(pTrans);
×
1791
    return code;
×
1792
  }
1793

1794
  sdbRelease(pMnode->pSdb, pStream);
293✔
1795
  mndTransDrop(pTrans);
293✔
1796

1797
  return TSDB_CODE_ACTION_IN_PROGRESS;
293✔
1798
}
1799

1800
static int32_t mndProcessResumeStreamReq(SRpcMsg *pReq) {
756✔
1801
  SMnode     *pMnode = pReq->info.node;
756✔
1802
  SStreamObj *pStream = NULL;
756✔
1803
  int32_t     code = 0;
756✔
1804

1805
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
756!
1806
    return code;
×
1807
  }
1808

1809
  SMResumeStreamReq resumeReq = {0};
756✔
1810
  if (tDeserializeSMResumeStreamReq(pReq->pCont, pReq->contLen, &resumeReq) < 0) {
756!
1811
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1812
  }
1813

1814
  code = mndAcquireStream(pMnode, resumeReq.name, &pStream);
756✔
1815
  if (pStream == NULL || code != 0) {
756!
1816
    if (resumeReq.igNotExists) {
212✔
1817
      mInfo("stream:%s not exist, not resume stream", resumeReq.name);
211!
1818
      sdbRelease(pMnode->pSdb, pStream);
211✔
1819
      return 0;
211✔
1820
    } else {
1821
      mError("stream:%s not exist, failed to resume stream", resumeReq.name);
1!
1822
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
1✔
1823
    }
1824
  }
1825

1826
  mInfo("stream:%s,%" PRId64 " start to resume stream from pause", resumeReq.name, pStream->uid);
544!
1827
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
544!
1828
    sdbRelease(pMnode->pSdb, pStream);
×
1829
    return -1;
×
1830
  }
1831

1832
  // check if it is conflict with other trans in both sourceDb and targetDb.
1833
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_RESUME_NAME, true);
544✔
1834
  if (code) {
544!
1835
    sdbRelease(pMnode->pSdb, pStream);
×
1836
    return code;
×
1837
  }
1838

1839
  STrans *pTrans = NULL;
544✔
1840
  code =
1841
      doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RESUME_NAME, "resume the stream", &pTrans);
544✔
1842
  if (pTrans == NULL || code) {
544!
1843
    mError("stream:%s, failed to resume stream since %s", resumeReq.name, tstrerror(code));
×
1844
    sdbRelease(pMnode->pSdb, pStream);
×
1845
    return code;
×
1846
  }
1847

1848
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_RESUME_NAME, pStream->uid);
544✔
1849
  if (code) {
544!
1850
    sdbRelease(pMnode->pSdb, pStream);
×
1851
    mndTransDrop(pTrans);
×
1852
    return code;
×
1853
  }
1854

1855
  // set the resume action
1856
  code = mndStreamSetResumeAction(pTrans, pMnode, pStream, resumeReq.igUntreated);
544✔
1857
  if (code) {
544!
1858
    mError("stream:%s, failed to drop task since %s", resumeReq.name, tstrerror(code));
×
1859
    sdbRelease(pMnode->pSdb, pStream);
×
1860
    mndTransDrop(pTrans);
×
1861
    return code;
×
1862
  }
1863

1864
  // resume stream
1865
  taosWLockLatch(&pStream->lock);
544✔
1866
  pStream->status = STREAM_STATUS__NORMAL;
544✔
1867
  if (mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY) < 0) {
544!
1868
    taosWUnLockLatch(&pStream->lock);
×
1869

1870
    sdbRelease(pMnode->pSdb, pStream);
×
1871
    mndTransDrop(pTrans);
×
1872
    return code;
×
1873
  }
1874

1875
  taosWUnLockLatch(&pStream->lock);
544✔
1876
  code = mndTransPrepare(pMnode, pTrans);
544✔
1877
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
544!
1878
    mError("trans:%d, failed to prepare pause stream trans since %s", pTrans->id, tstrerror(code));
×
1879
    sdbRelease(pMnode->pSdb, pStream);
×
1880
    mndTransDrop(pTrans);
×
1881
    return code;
×
1882
  }
1883

1884
  sdbRelease(pMnode->pSdb, pStream);
544✔
1885
  mndTransDrop(pTrans);
544✔
1886

1887
  return TSDB_CODE_ACTION_IN_PROGRESS;
544✔
1888
}
1889

1890
static int32_t mndProcessVgroupChange(SMnode *pMnode, SVgroupChangeInfo *pChangeInfo, bool includeAllNodes) {
9✔
1891
  SSdb       *pSdb = pMnode->pSdb;
9✔
1892
  SStreamObj *pStream = NULL;
9✔
1893
  void       *pIter = NULL;
9✔
1894
  STrans     *pTrans = NULL;
9✔
1895
  int32_t     code = 0;
9✔
1896

1897
  // conflict check for nodeUpdate trans, here we randomly chose one stream to add into the trans pool
1898
  while (1) {
1899
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
18✔
1900
    if (pIter == NULL) {
18✔
1901
      break;
9✔
1902
    }
1903

1904
    code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_TASK_UPDATE_NAME, false);
9✔
1905
    sdbRelease(pSdb, pStream);
9✔
1906

1907
    if (code) {
9!
1908
      mError("nodeUpdate conflict with other trans, current nodeUpdate ignored, code:%s", tstrerror(code));
×
1909
      sdbCancelFetch(pSdb, pIter);
×
1910
      return code;
×
1911
    }
1912
  }
1913

1914
  while (1) {
1915
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
18✔
1916
    if (pIter == NULL) {
18✔
1917
      break;
9✔
1918
    }
1919

1920
    // here create only one trans
1921
    if (pTrans == NULL) {
9!
1922
      code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_TASK_UPDATE_NAME,
9✔
1923
                           "update task epsets", &pTrans);
1924
      if (pTrans == NULL || code) {
9!
1925
        sdbRelease(pSdb, pStream);
×
1926
        sdbCancelFetch(pSdb, pIter);
×
1927
        return terrno = code;
×
1928
      }
1929
    }
1930

1931
    if (!includeAllNodes) {
9!
1932
      void *p1 = taosHashGet(pChangeInfo->pDBMap, pStream->targetDb, strlen(pStream->targetDb));
9✔
1933
      void *p2 = taosHashGet(pChangeInfo->pDBMap, pStream->sourceDb, strlen(pStream->sourceDb));
9✔
1934
      if (p1 == NULL && p2 == NULL) {
9!
1935
        mDebug("stream:0x%" PRIx64 " %s not involved nodeUpdate, ignore", pStream->uid, pStream->name);
×
1936
        sdbRelease(pSdb, pStream);
×
1937
        continue;
×
1938
      }
1939
    }
1940

1941
    mDebug("stream:0x%" PRIx64 " %s involved node changed, create update trans, transId:%d", pStream->uid,
9✔
1942
           pStream->name, pTrans->id);
1943

1944
    // NOTE: for each stream, we register one trans entry for task update
1945
    code = mndStreamRegisterTrans(pTrans, MND_STREAM_TASK_UPDATE_NAME, pStream->uid);
9✔
1946
    if (code) {
9!
1947
      mError("failed to register trans, transId:%d, and continue", pTrans->id);
×
1948
    }
1949

1950
    code = mndStreamSetUpdateEpsetAction(pMnode, pStream, pChangeInfo, pTrans);
9✔
1951

1952
    // todo: not continue, drop all and retry again
1953
    if (code != TSDB_CODE_SUCCESS) {
9!
1954
      mError("stream:0x%" PRIx64 " build nodeUpdate trans failed, ignore and continue, code:%s", pStream->uid,
×
1955
             tstrerror(code));
1956
      sdbRelease(pSdb, pStream);
×
1957
      continue;
×
1958
    }
1959

1960
    code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
9✔
1961
    sdbRelease(pSdb, pStream);
9✔
1962

1963
    if (code != TSDB_CODE_SUCCESS) {
9!
1964
      sdbCancelFetch(pSdb, pIter);
×
1965
      return code;
×
1966
    }
1967
  }
1968

1969
  // no need to build the trans to handle the vgroup update
1970
  if (pTrans == NULL) {
9!
1971
    return 0;
×
1972
  }
1973

1974
  code = mndTransPrepare(pMnode, pTrans);
9✔
1975
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
9!
1976
    mError("trans:%d, failed to prepare update stream trans since %s", pTrans->id, tstrerror(code));
×
1977
    sdbRelease(pMnode->pSdb, pStream);
×
1978
    mndTransDrop(pTrans);
×
1979
    return code;
×
1980
  }
1981

1982
  sdbRelease(pMnode->pSdb, pStream);
9✔
1983
  mndTransDrop(pTrans);
9✔
1984
  return code;
9✔
1985
}
1986

1987
static int32_t refreshNodeListFromExistedStreams(SMnode *pMnode, SArray *pNodeList) {
644✔
1988
  SSdb       *pSdb = pMnode->pSdb;
644✔
1989
  SStreamObj *pStream = NULL;
644✔
1990
  void       *pIter = NULL;
644✔
1991
  int32_t     code = 0;
644✔
1992

1993
  mDebug("start to refresh node list by existed streams");
644✔
1994

1995
  SHashObj *pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
644✔
1996
  if (pHash == NULL) {
644!
1997
    return terrno;
×
1998
  }
1999

2000
  while (1) {
9✔
2001
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
653✔
2002
    if (pIter == NULL) {
653✔
2003
      break;
644✔
2004
    }
2005

2006
    taosWLockLatch(&pStream->lock);
9✔
2007

2008
    SStreamTaskIter *pTaskIter = NULL;
9✔
2009
    code = createStreamTaskIter(pStream, &pTaskIter);
9✔
2010
    if (code) {
9!
2011
      taosWUnLockLatch(&pStream->lock);
×
2012
      sdbRelease(pSdb, pStream);
×
2013
      mError("failed to create task iter for stream:%s", pStream->name);
×
2014
      continue;
×
2015
    }
2016

2017
    while (streamTaskIterNextTask(pTaskIter)) {
64✔
2018
      SStreamTask *pTask = NULL;
55✔
2019
      code = streamTaskIterGetCurrent(pTaskIter, &pTask);
55✔
2020
      if (code) {
55!
2021
        break;
×
2022
      }
2023

2024
      SNodeEntry entry = {.hbTimestamp = -1, .nodeId = pTask->info.nodeId, .lastHbMsgId = -1};
55✔
2025
      epsetAssign(&entry.epset, &pTask->info.epSet);
55✔
2026
      int32_t ret = taosHashPut(pHash, &entry.nodeId, sizeof(entry.nodeId), &entry, sizeof(entry));
55✔
2027
      if (ret != 0 && ret != TSDB_CODE_DUP_KEY) {
55!
2028
        mError("failed to put entry into hash map, nodeId:%d, code:%s", entry.nodeId, tstrerror(code));
×
2029
      }
2030
    }
2031

2032
    destroyStreamTaskIter(pTaskIter);
9✔
2033
    taosWUnLockLatch(&pStream->lock);
9✔
2034

2035
    sdbRelease(pSdb, pStream);
9✔
2036
  }
2037

2038
  taosArrayClear(pNodeList);
644✔
2039

2040
  // convert to list
2041
  pIter = NULL;
644✔
2042
  while ((pIter = taosHashIterate(pHash, pIter)) != NULL) {
673✔
2043
    SNodeEntry *pEntry = (SNodeEntry *)pIter;
29✔
2044

2045
    void *p = taosArrayPush(pNodeList, pEntry);
29✔
2046
    if (p == NULL) {
29!
2047
      mError("failed to put entry into node list, nodeId:%d, code: out of memory", pEntry->nodeId);
×
2048
      if (code == 0) {
×
2049
        code = terrno;
×
2050
      }
2051
      continue;
×
2052
    }
2053

2054
    char    buf[256] = {0};
29✔
2055
    int32_t ret = epsetToStr(&pEntry->epset, buf, tListLen(buf));  // ignore this error since it is only for log file
29✔
2056
    if (ret != 0) {                                                // print error and continue
29!
2057
      mError("failed to convert epset to str, code:%s", tstrerror(ret));
×
2058
    }
2059

2060
    mDebug("extract nodeInfo from stream obj, nodeId:%d, %s", pEntry->nodeId, buf);
29✔
2061
  }
2062

2063
  taosHashCleanup(pHash);
644✔
2064

2065
  mDebug("numOfNodes:%d for stream after extract nodeInfo from stream", (int32_t)taosArrayGetSize(pNodeList));
644✔
2066
  return code;
644✔
2067
}
2068

2069
static void addAllDbsIntoHashmap(SHashObj *pDBMap, SSdb *pSdb) {
×
2070
  void   *pIter = NULL;
×
2071
  int32_t code = 0;
×
2072
  while (1) {
×
2073
    SVgObj *pVgroup = NULL;
×
2074
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
×
2075
    if (pIter == NULL) {
×
2076
      break;
×
2077
    }
2078

2079
    code = taosHashPut(pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
×
2080
    sdbRelease(pSdb, pVgroup);
×
2081

2082
    if (code == 0) {
×
2083
      int32_t size = taosHashGetSize(pDBMap);
×
2084
      mDebug("add Db:%s into Dbs list (total:%d) for kill checkpoint trans", pVgroup->dbName, size);
×
2085
    }
2086
  }
2087
}
×
2088

2089
// this function runs by only one thread, so it is not multi-thread safe
2090
static int32_t mndProcessNodeCheckReq(SRpcMsg *pMsg) {
550✔
2091
  int32_t code = 0;
550✔
2092
  bool    allReady = true;
550✔
2093
  SArray *pNodeSnapshot = NULL;
550✔
2094
  SMnode *pMnode = pMsg->info.node;
550✔
2095
  int64_t ts = taosGetTimestampSec();
550✔
2096
  bool    updateAllVgroups = false;
550✔
2097

2098
  int32_t old = atomic_val_compare_exchange_32(&mndNodeCheckSentinel, 0, 1);
550✔
2099
  if (old != 0) {
550!
2100
    mDebug("still in checking node change");
×
2101
    return 0;
×
2102
  }
2103

2104
  mDebug("start to do node changing check");
550✔
2105

2106
  streamMutexLock(&execInfo.lock);
550✔
2107
  int32_t numOfNodes = extractStreamNodeList(pMnode);
550✔
2108
  streamMutexUnlock(&execInfo.lock);
550✔
2109

2110
  if (numOfNodes == 0) {
550!
2111
    mDebug("end to do stream task(s) node change checking, no stream tasks exist, do nothing");
×
2112
    execInfo.ts = ts;
×
2113
    atomic_store_32(&mndNodeCheckSentinel, 0);
×
2114
    return 0;
×
2115
  }
2116

2117
  code = mndTakeVgroupSnapshot(pMnode, &allReady, &pNodeSnapshot);
550✔
2118
  if (code) {
550!
2119
    mError("failed to take the vgroup snapshot, ignore it and continue");
×
2120
  }
2121

2122
  if (!allReady) {
550✔
2123
    taosArrayDestroy(pNodeSnapshot);
36✔
2124
    atomic_store_32(&mndNodeCheckSentinel, 0);
36✔
2125
    mWarn("not all vnodes are ready, ignore the exec nodeUpdate check");
36!
2126
    return 0;
36✔
2127
  }
2128

2129
  streamMutexLock(&execInfo.lock);
514✔
2130

2131
  code = removeExpiredNodeEntryAndTaskInBuf(pNodeSnapshot);
514✔
2132
  if (code) {
514!
2133
    goto _end;
×
2134
  }
2135

2136
  SVgroupChangeInfo changeInfo = {0};
514✔
2137
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, pNodeSnapshot, &changeInfo);
514✔
2138
  if (code) {
514!
2139
    goto _end;
×
2140
  }
2141

2142
  {
2143
    if (execInfo.role == NODE_ROLE_LEADER && execInfo.switchFromFollower) {
514!
2144
      mInfo("rollback all stream due to mnode leader/follower switch by using nodeUpdate trans");
×
2145
      updateAllVgroups = true;
×
2146
      execInfo.switchFromFollower = false;  // reset the flag
×
2147
      addAllDbsIntoHashmap(changeInfo.pDBMap, pMnode->pSdb);
×
2148
    }
2149
  }
2150

2151
  if (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0 || updateAllVgroups) {
514!
2152
    // kill current active checkpoint transaction, since the transaction is vnode wide.
2153
    killAllCheckpointTrans(pMnode, &changeInfo);
9✔
2154
    code = mndProcessVgroupChange(pMnode, &changeInfo, updateAllVgroups);
9✔
2155

2156
    // keep the new vnode snapshot if success
2157
    if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {
9!
2158
      code = refreshNodeListFromExistedStreams(pMnode, execInfo.pNodeList);
9✔
2159
      if (code) {
9!
2160
        mError("failed to extract node list from stream, code:%s", tstrerror(code));
×
2161
        goto _end;
×
2162
      }
2163

2164
      execInfo.ts = ts;
9✔
2165
      mDebug("create trans successfully, update cached node list, numOfNodes:%d",
9✔
2166
             (int)taosArrayGetSize(execInfo.pNodeList));
2167
    } else {
2168
      mError("unexpected code during create nodeUpdate trans, code:%s", tstrerror(code));
×
2169
    }
2170
  } else {
2171
    mDebug("no update found in nodeList");
505✔
2172
  }
2173

2174
  mndDestroyVgroupChangeInfo(&changeInfo);
514✔
2175

2176
_end:
514✔
2177
  streamMutexUnlock(&execInfo.lock);
514✔
2178
  taosArrayDestroy(pNodeSnapshot);
514✔
2179

2180
  mDebug("end to do stream task node change checking");
514✔
2181
  atomic_store_32(&mndNodeCheckSentinel, 0);
514✔
2182
  return 0;
514✔
2183
}
2184

2185
static int32_t mndProcessNodeCheck(SRpcMsg *pReq) {
1,658✔
2186
  SMnode *pMnode = pReq->info.node;
1,658✔
2187
  SSdb   *pSdb = pMnode->pSdb;
1,658✔
2188
  if (sdbGetSize(pSdb, SDB_STREAM) <= 0) {
1,658✔
2189
    return 0;
1,108✔
2190
  }
2191

2192
  int32_t               size = sizeof(SMStreamNodeCheckMsg);
550✔
2193
  SMStreamNodeCheckMsg *pMsg = rpcMallocCont(size);
550✔
2194
  if (pMsg == NULL) {
550!
2195
    return terrno;
×
2196
  }
2197

2198
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_NODECHANGE_CHECK, .pCont = pMsg, .contLen = size};
550✔
2199
  return tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
550✔
2200
}
2201

2202
void saveTaskAndNodeInfoIntoBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
1,843✔
2203
  SStreamTaskIter *pIter = NULL;
1,843✔
2204
  int32_t          code = createStreamTaskIter(pStream, &pIter);
1,843✔
2205
  if (code) {
1,843!
2206
    mError("failed to create task iter for stream:%s", pStream->name);
×
2207
    return;
×
2208
  }
2209

2210
  while (streamTaskIterNextTask(pIter)) {
11,264✔
2211
    SStreamTask *pTask = NULL;
9,421✔
2212
    code = streamTaskIterGetCurrent(pIter, &pTask);
9,421✔
2213
    if (code) {
9,421!
2214
      break;
×
2215
    }
2216

2217
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
9,421✔
2218
    void   *p = taosHashGet(pExecNode->pTaskMap, &id, sizeof(id));
9,421✔
2219
    if (p == NULL) {
9,421✔
2220
      STaskStatusEntry entry = {0};
8,740✔
2221
      streamTaskStatusInit(&entry, pTask);
8,740✔
2222

2223
      code = taosHashPut(pExecNode->pTaskMap, &id, sizeof(id), &entry, sizeof(entry));
8,740✔
2224
      if (code == 0) {
8,740!
2225
        void   *px = taosArrayPush(pExecNode->pTaskList, &id);
8,740✔
2226
        int32_t num = (int32_t)taosArrayGetSize(pExecNode->pTaskList);
8,740✔
2227
        if (px) {
8,740!
2228
          mInfo("s-task:0x%x add into task buffer, total:%d", (int32_t)entry.id.taskId, num);
8,740!
2229
        } else {
2230
          mError("s-task:0x%x failed to add into task buffer, total:%d", (int32_t)entry.id.taskId, num);
×
2231
        }
2232
      } else {
2233
        mError("s-task:0x%x failed to add into task map, since out of memory", (int32_t)entry.id.taskId);
×
2234
      }
2235

2236
      // add the new vgroups if not added yet
2237
      bool exist = false;
8,740✔
2238
      for (int32_t j = 0; j < taosArrayGetSize(pExecNode->pNodeList); ++j) {
46,046✔
2239
        SNodeEntry *pEntry = taosArrayGet(pExecNode->pNodeList, j);
44,345✔
2240
        if ((pEntry != NULL) && (pEntry->nodeId == pTask->info.nodeId)) {
44,345!
2241
          exist = true;
7,039✔
2242
          break;
7,039✔
2243
        }
2244
      }
2245

2246
      if (!exist) {
8,740✔
2247
        SNodeEntry nodeEntry = {.hbTimestamp = -1, .nodeId = pTask->info.nodeId, .lastHbMsgId = -1};
1,701✔
2248
        epsetAssign(&nodeEntry.epset, &pTask->info.epSet);
1,701✔
2249

2250
        void *px = taosArrayPush(pExecNode->pNodeList, &nodeEntry);
1,701✔
2251
        if (px) {
1,701!
2252
          mInfo("vgId:%d added into nodeList, total:%d", nodeEntry.nodeId, (int)taosArrayGetSize(pExecNode->pNodeList));
1,701!
2253
        } else {
2254
          mError("vgId:%d failed to add into nodeList, total:%d", nodeEntry.nodeId,
×
2255
                 (int)taosArrayGetSize(pExecNode->pNodeList))
2256
        }
2257
      }
2258
    }
2259
  }
2260

2261
  destroyStreamTaskIter(pIter);
1,843✔
2262
}
2263

2264
static void doAddTaskId(SArray *pList, int32_t taskId, int64_t uid, int32_t numOfTotal) {
4,432✔
2265
  int32_t num = taosArrayGetSize(pList);
4,432✔
2266
  for (int32_t i = 0; i < num; ++i) {
16,434✔
2267
    int32_t *pId = taosArrayGet(pList, i);
12,011✔
2268
    if (pId == NULL) {
12,011!
2269
      continue;
×
2270
    }
2271

2272
    if (taskId == *pId) {
12,011✔
2273
      return;
9✔
2274
    }
2275
  }
2276

2277
  int32_t numOfTasks = taosArrayGetSize(pList);
4,423✔
2278
  void   *p = taosArrayPush(pList, &taskId);
4,423✔
2279
  if (p) {
4,423!
2280
    mDebug("stream:0x%" PRIx64 " receive %d reqs for checkpoint, remain:%d", uid, numOfTasks, numOfTotal - numOfTasks);
4,423✔
2281
  } else {
2282
    mError("stream:0x%" PRIx64 " receive %d reqs for checkpoint, failed to added into task list, since out of memory",
×
2283
           uid, numOfTasks);
2284
  }
2285
}
2286

2287
int32_t mndProcessStreamReqCheckpoint(SRpcMsg *pReq) {
4,432✔
2288
  SMnode                  *pMnode = pReq->info.node;
4,432✔
2289
  SStreamTaskCheckpointReq req = {0};
4,432✔
2290

2291
  SDecoder decoder = {0};
4,432✔
2292
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
4,432✔
2293

2294
  if (tDecodeStreamTaskCheckpointReq(&decoder, &req)) {
4,432!
2295
    tDecoderClear(&decoder);
×
2296
    mError("invalid task checkpoint req msg received");
×
2297
    return TSDB_CODE_INVALID_MSG;
×
2298
  }
2299
  tDecoderClear(&decoder);
4,432✔
2300

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

2303
  // register to the stream task done map, if all tasks has sent this kinds of message, start the checkpoint trans.
2304
  streamMutexLock(&execInfo.lock);
4,432✔
2305

2306
  SStreamObj *pStream = NULL;
4,432✔
2307
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
4,432✔
2308
  if (pStream == NULL || code != 0) {
4,432!
2309
    mWarn("failed to find the stream:0x%" PRIx64 ", not handle the checkpoint req, try to acquire in buf",
×
2310
          req.streamId);
2311

2312
    // not in meta-store yet, try to acquire the task in exec buffer
2313
    // the checkpoint req arrives too soon before the completion of the create stream trans.
2314
    STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
×
2315
    void   *p = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
2316
    if (p == NULL) {
×
2317
      mError("failed to find the stream:0x%" PRIx64 " in buf, not handle the checkpoint req", req.streamId);
×
2318
      streamMutexUnlock(&execInfo.lock);
×
2319
      return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
2320
    } else {
2321
      mDebug("s-task:0x%" PRIx64 "-0x%x in buf not in mnode/meta, create stream trans may not complete yet",
×
2322
             req.streamId, req.taskId);
2323
    }
2324
  }
2325

2326
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
4,432!
2327

2328
  SArray **pReqTaskList = (SArray **)taosHashGet(execInfo.pTransferStateStreams, &req.streamId, sizeof(req.streamId));
4,432✔
2329
  if (pReqTaskList == NULL) {
4,432✔
2330
    SArray *pList = taosArrayInit(4, sizeof(int32_t));
768✔
2331
    doAddTaskId(pList, req.taskId, req.streamId, numOfTasks);
768✔
2332
    code = taosHashPut(execInfo.pTransferStateStreams, &req.streamId, sizeof(int64_t), &pList, sizeof(void *));
768✔
2333
    if (code) {
768!
2334
      mError("failed to put into transfer state stream map, code: out of memory");
×
2335
    }
2336
    pReqTaskList = (SArray **)taosHashGet(execInfo.pTransferStateStreams, &req.streamId, sizeof(req.streamId));
768✔
2337
  } else {
2338
    doAddTaskId(*pReqTaskList, req.taskId, req.streamId, numOfTasks);
3,664✔
2339
  }
2340

2341
  int32_t total = taosArrayGetSize(*pReqTaskList);
4,432✔
2342
  if (total == numOfTasks) {  // all tasks have sent the reqs
4,432✔
2343
    int64_t checkpointId = mndStreamGenChkptId(pMnode, false);
731✔
2344
    mInfo("stream:0x%" PRIx64 " all tasks req checkpoint, start checkpointId:%" PRId64, req.streamId, checkpointId);
731!
2345

2346
    if (pStream != NULL) {  // TODO:handle error
731!
2347
      code = mndProcessStreamCheckpointTrans(pMnode, pStream, checkpointId, 0, false);
731✔
2348
      if (code) {
731!
2349
        mError("failed to create checkpoint trans, code:%s", tstrerror(code));
731!
2350
      }
2351
    } else {
2352
      // todo: wait for the create stream trans completed, and launch the checkpoint trans
2353
      // SStreamObj *pStream = mndGetStreamObj(pMnode, req.streamId);
2354
      // sleep(500ms)
2355
    }
2356

2357
    // remove this entry
2358
    (void) taosHashRemove(execInfo.pTransferStateStreams, &req.streamId, sizeof(int64_t));
731✔
2359

2360
    int32_t numOfStreams = taosHashGetSize(execInfo.pTransferStateStreams);
731✔
2361
    mDebug("stream:0x%" PRIx64 " removed, remain streams:%d fill-history not completed", req.streamId, numOfStreams);
731✔
2362
  }
2363

2364
  if (pStream != NULL) {
4,432!
2365
    mndReleaseStream(pMnode, pStream);
4,432✔
2366
  }
2367

2368
  streamMutexUnlock(&execInfo.lock);
4,432✔
2369

2370
  {
2371
    SRpcMsg rsp = {.code = 0, .info = pReq->info, .contLen = sizeof(SMStreamReqCheckpointRsp)};
4,432✔
2372
    rsp.pCont = rpcMallocCont(rsp.contLen);
4,432✔
2373
    if (rsp.pCont == NULL) {
4,432!
2374
      return terrno;
×
2375
    }
2376

2377
    SMsgHead *pHead = rsp.pCont;
4,432✔
2378
    pHead->vgId = htonl(req.nodeId);
4,432✔
2379

2380
    tmsgSendRsp(&rsp);
4,432✔
2381
    pReq->info.handle = NULL;  // disable auto rsp
4,432✔
2382
  }
2383

2384
  return 0;
4,432✔
2385
}
2386

2387
// valid the info according to the HbMsg
2388
static bool validateChkptReport(const SCheckpointReport *pReport, int64_t reportChkptId) {
4,253✔
2389
  STaskId           id = {.streamId = pReport->streamId, .taskId = pReport->taskId};
4,253✔
2390
  STaskStatusEntry *pTaskEntry = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
4,253✔
2391
  if (pTaskEntry == NULL) {
4,253✔
2392
    mError("invalid checkpoint-report msg from task:0x%x, discard", pReport->taskId);
16!
2393
    return false;
16✔
2394
  }
2395

2396
  if (pTaskEntry->checkpointInfo.latestId >= pReport->checkpointId) {
4,237!
2397
    mError("s-task:0x%x invalid checkpoint-report msg, checkpointId:%" PRId64 " saved checkpointId:%" PRId64 " discard",
×
2398
           pReport->taskId, pReport->checkpointId, pTaskEntry->checkpointInfo.activeId);
2399
    return false;
×
2400
  }
2401

2402
  // now the task in checkpoint procedure
2403
  if ((pTaskEntry->checkpointInfo.activeId != 0) && (pTaskEntry->checkpointInfo.activeId > pReport->checkpointId)) {
4,237!
2404
    mError("s-task:0x%x invalid checkpoint-report msg, checkpointId:%" PRId64 " active checkpointId:%" PRId64
×
2405
           " discard",
2406
           pReport->taskId, pReport->checkpointId, pTaskEntry->checkpointInfo.activeId);
2407
    return false;
×
2408
  }
2409

2410
  if (reportChkptId >= pReport->checkpointId) {
4,237!
2411
    mError("s-task:0x%x expired checkpoint-report msg, checkpointId:%" PRId64 " already update checkpointId:%" PRId64
×
2412
           " discard",
2413
           pReport->taskId, pReport->checkpointId, reportChkptId);
2414
    return false;
×
2415
  }
2416

2417
  return true;
4,237✔
2418
}
2419

2420
static void doAddReportStreamTask(SArray *pList, int64_t reportChkptId, const SCheckpointReport *pReport) {
4,253✔
2421
  bool valid = validateChkptReport(pReport, reportChkptId);
4,253✔
2422
  if (!valid) {
4,253✔
2423
    return;
16✔
2424
  }
2425

2426
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
15,982✔
2427
    STaskChkptInfo *p = taosArrayGet(pList, i);
11,745✔
2428
    if (p == NULL) {
11,745!
2429
      continue;
×
2430
    }
2431

2432
    if (p->taskId == pReport->taskId) {
11,745!
2433
      if (p->checkpointId > pReport->checkpointId) {
×
2434
        mError("s-task:0x%x invalid checkpoint-report msg, existed:%" PRId64 " req checkpointId:%" PRId64 ", discard",
×
2435
               pReport->taskId, p->checkpointId, pReport->checkpointId);
2436
      } else if (p->checkpointId < pReport->checkpointId) {  // expired checkpoint-report msg, update it
×
2437
        mDebug("s-task:0x%x expired checkpoint-report msg in checkpoint-report list update from %" PRId64 "->%" PRId64,
×
2438
               pReport->taskId, p->checkpointId, pReport->checkpointId);
2439

2440
        // update the checkpoint report info
2441
        p->checkpointId = pReport->checkpointId;
×
2442
        p->ts = pReport->checkpointTs;
×
2443
        p->version = pReport->checkpointVer;
×
2444
        p->transId = pReport->transId;
×
2445
        p->dropHTask = pReport->dropHTask;
×
2446
      } else {
2447
        mWarn("taskId:0x%x already in checkpoint-report list", pReport->taskId);
×
2448
      }
2449
      return;
×
2450
    }
2451
  }
2452

2453
  STaskChkptInfo info = {
4,237✔
2454
      .streamId = pReport->streamId,
4,237✔
2455
      .taskId = pReport->taskId,
4,237✔
2456
      .transId = pReport->transId,
4,237✔
2457
      .dropHTask = pReport->dropHTask,
4,237✔
2458
      .version = pReport->checkpointVer,
4,237✔
2459
      .ts = pReport->checkpointTs,
4,237✔
2460
      .checkpointId = pReport->checkpointId,
4,237✔
2461
      .nodeId = pReport->nodeId,
4,237✔
2462
  };
2463

2464
  void *p = taosArrayPush(pList, &info);
4,237✔
2465
  if (p == NULL) {
4,237!
2466
    mError("failed to put into task list, taskId:0x%x", pReport->taskId);
×
2467
  } else {
2468
    int32_t size = taosArrayGetSize(pList);
4,237✔
2469
    mDebug("stream:0x%" PRIx64 " %d tasks has send checkpoint-report", pReport->streamId, size);
4,237✔
2470
  }
2471
}
2472

2473
int32_t mndProcessCheckpointReport(SRpcMsg *pReq) {
4,253✔
2474
  SMnode           *pMnode = pReq->info.node;
4,253✔
2475
  SCheckpointReport req = {0};
4,253✔
2476

2477
  SDecoder decoder = {0};
4,253✔
2478
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
4,253✔
2479

2480
  if (tDecodeStreamTaskChkptReport(&decoder, &req)) {
4,253!
2481
    tDecoderClear(&decoder);
×
2482
    mError("invalid task checkpoint-report msg received");
×
2483
    return TSDB_CODE_INVALID_MSG;
×
2484
  }
2485
  tDecoderClear(&decoder);
4,253✔
2486

2487
  streamMutexLock(&execInfo.lock);
4,253✔
2488
  mndInitStreamExecInfo(pMnode, &execInfo);
4,253✔
2489
  streamMutexUnlock(&execInfo.lock);
4,253✔
2490

2491
  mDebug("receive stream task checkpoint-report msg, vgId:%d, s-task:0x%x, checkpointId:%" PRId64
4,253✔
2492
         " checkpointVer:%" PRId64 " transId:%d",
2493
         req.nodeId, req.taskId, req.checkpointId, req.checkpointVer, req.transId);
2494

2495
  // register to the stream task done map, if all tasks has sent this kinds of message, start the checkpoint trans.
2496
  streamMutexLock(&execInfo.lock);
4,253✔
2497

2498
  SStreamObj *pStream = NULL;
4,253✔
2499
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
4,253✔
2500
  if (pStream == NULL || code != 0) {
4,253!
2501
    mWarn("failed to find the stream:0x%" PRIx64 ", not handle checkpoint-report, try to acquire in buf", req.streamId);
×
2502

2503
    // not in meta-store yet, try to acquire the task in exec buffer
2504
    // the checkpoint req arrives too soon before the completion of the create stream trans.
2505
    STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
×
2506
    void   *p = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
2507
    if (p == NULL) {
×
2508
      mError("failed to find the stream:0x%" PRIx64 " in buf, not handle the checkpoint-report", req.streamId);
×
2509
      streamMutexUnlock(&execInfo.lock);
×
2510
      return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
2511
    } else {
2512
      mDebug("s-task:0x%" PRIx64 "-0x%x in buf not in mnode/meta, create stream trans may not complete yet",
×
2513
             req.streamId, req.taskId);
2514
    }
2515
  }
2516

2517
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
4,253!
2518

2519
  SChkptReportInfo *pInfo =
2520
      (SChkptReportInfo *)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId));
4,253✔
2521
  if (pInfo == NULL) {
4,253✔
2522
    SChkptReportInfo info = {.pTaskList = taosArrayInit(4, sizeof(STaskChkptInfo)), .streamId = req.streamId};
722✔
2523
    if (info.pTaskList != NULL) {
722!
2524
      doAddReportStreamTask(info.pTaskList, info.reportChkpt, &req);
722✔
2525
      code = taosHashPut(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId), &info, sizeof(info));
722✔
2526
      if (code) {
722!
2527
        mError("stream:0x%" PRIx64 " failed to put into checkpoint stream", req.streamId);
×
2528
      }
2529

2530
      pInfo = (SChkptReportInfo *)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId));
722✔
2531
    }
2532
  } else {
2533
    doAddReportStreamTask(pInfo->pTaskList, pInfo->reportChkpt, &req);
3,531✔
2534
  }
2535

2536
  int32_t total = taosArrayGetSize(pInfo->pTaskList);
4,253✔
2537
  if (total == numOfTasks) {  // all tasks has send the reqs
4,253✔
2538
    mInfo("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, checkpoint meta-info for checkpointId:%" PRId64
702!
2539
          " will be issued soon",
2540
          req.streamId, pStream->name, total, req.checkpointId);
2541
  }
2542

2543
  if (pStream != NULL) {
4,253!
2544
    mndReleaseStream(pMnode, pStream);
4,253✔
2545
  }
2546

2547
  streamMutexUnlock(&execInfo.lock);
4,253✔
2548

2549
  doSendQuickRsp(&pReq->info, sizeof(SMStreamUpdateChkptRsp), req.nodeId, TSDB_CODE_SUCCESS);
4,253✔
2550
  return code;
4,253✔
2551
}
2552

2553
static int64_t getConsensusId(int64_t streamId, int32_t numOfTasks, int32_t *pExistedTasks, bool *pAllSame) {
117✔
2554
  int32_t num = 0;
117✔
2555
  int64_t chkId = INT64_MAX;
117✔
2556
  *pExistedTasks = 0;
117✔
2557
  *pAllSame = true;
117✔
2558

2559
  for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
1,652✔
2560
    STaskId *p = taosArrayGet(execInfo.pTaskList, i);
1,535✔
2561
    if (p == NULL) {
1,535!
2562
      continue;
×
2563
    }
2564

2565
    if (p->streamId != streamId) {
1,535✔
2566
      continue;
880✔
2567
    }
2568

2569
    num += 1;
655✔
2570
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
655✔
2571
    if (chkId > pe->checkpointInfo.latestId) {
655✔
2572
      if (chkId != INT64_MAX) {
117!
UNCOV
2573
        *pAllSame = false;
×
2574
      }
2575
      chkId = pe->checkpointInfo.latestId;
117✔
2576
    }
2577
  }
2578

2579
  *pExistedTasks = num;
117✔
2580
  if (num < numOfTasks) {  // not all task send info to mnode through hbMsg, no valid checkpoint Id
117!
2581
    return -1;
×
2582
  }
2583

2584
  return chkId;
117✔
2585
}
2586

2587
static void doSendQuickRsp(SRpcHandleInfo *pInfo, int32_t msgSize, int32_t vgId, int32_t code) {
4,253✔
2588
  SRpcMsg rsp = {.code = code, .info = *pInfo, .contLen = msgSize};
4,253✔
2589
  rsp.pCont = rpcMallocCont(rsp.contLen);
4,253✔
2590
  if (rsp.pCont != NULL) {
4,253!
2591
    SMsgHead *pHead = rsp.pCont;
4,253✔
2592
    pHead->vgId = htonl(vgId);
4,253✔
2593

2594
    tmsgSendRsp(&rsp);
4,253✔
2595
    pInfo->handle = NULL;  // disable auto rsp
4,253✔
2596
  }
2597
}
4,253✔
2598

2599
int32_t mndProcessConsensusInTmr(SRpcMsg *pMsg) {
7,697✔
2600
  SMnode *pMnode = pMsg->info.node;
7,697✔
2601
  int64_t now = taosGetTimestampMs();
7,697✔
2602
  SArray *pStreamList = taosArrayInit(4, sizeof(int64_t));
7,697✔
2603
  if (pStreamList == NULL) {
7,697!
2604
    return terrno;
×
2605
  }
2606

2607
  mDebug("start to process consensus-checkpointId in tmr");
7,697✔
2608

2609
  bool    allReady = true;
7,697✔
2610
  SArray *pNodeSnapshot = NULL;
7,697✔
2611

2612
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, &pNodeSnapshot);
7,697✔
2613
  taosArrayDestroy(pNodeSnapshot);
7,697✔
2614
  if (code) {
7,697✔
2615
    mError("failed to get the vgroup snapshot, ignore it and continue");
45!
2616
  }
2617

2618
  if (!allReady) {
7,697✔
2619
    mWarn("not all vnodes are ready, end to process the consensus-checkpointId in tmr process");
1,053!
2620
    taosArrayDestroy(pStreamList);
1,053✔
2621
    return 0;
1,053✔
2622
  }
2623

2624
  streamMutexLock(&execInfo.lock);
6,644✔
2625

2626
  void *pIter = NULL;
6,644✔
2627
  while ((pIter = taosHashIterate(execInfo.pStreamConsensus, pIter)) != NULL) {
6,673✔
2628
    SCheckpointConsensusInfo *pInfo = (SCheckpointConsensusInfo *)pIter;
29✔
2629

2630
    int64_t streamId = -1;
29✔
2631
    int32_t num = taosArrayGetSize(pInfo->pTaskList);
29✔
2632
    SArray *pList = taosArrayInit(4, sizeof(int32_t));
29✔
2633
    if (pList == NULL) {
29!
2634
      continue;
×
2635
    }
2636

2637
    SStreamObj *pStream = NULL;
29✔
2638
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
29✔
2639
    if (pStream == NULL || code != 0) {  // stream has been dropped already
29!
2640
      mDebug("stream:0x%" PRIx64 " dropped already, continue", pInfo->streamId);
×
2641
      taosArrayDestroy(pList);
×
2642
      continue;
×
2643
    }
2644

2645
    for (int32_t j = 0; j < num; ++j) {
146✔
2646
      SCheckpointConsensusEntry *pe = taosArrayGet(pInfo->pTaskList, j);
117✔
2647
      if (pe == NULL) {
117!
2648
        continue;
×
2649
      }
2650

2651
      streamId = pe->req.streamId;
117✔
2652

2653
      int32_t existed = 0;
117✔
2654
      bool    allSame = true;
117✔
2655
      int64_t chkId = getConsensusId(pe->req.streamId, pInfo->numOfTasks, &existed, &allSame);
117✔
2656
      if (chkId == -1) {
117!
2657
        mDebug("not all(%d/%d) task(s) send hbMsg yet, wait for a while and check again, s-task:0x%x", existed,
×
2658
               pInfo->numOfTasks, pe->req.taskId);
2659
        break;
×
2660
      }
2661

2662
      if (((now - pe->ts) >= 10 * 1000) || allSame) {
117!
2663
        mDebug("s-task:0x%x sendTs:%" PRId64 " wait %.2fs and all tasks have same checkpointId", pe->req.taskId,
117✔
2664
               pe->req.startTs, (now - pe->ts) / 1000.0);
2665
        if (chkId > pe->req.checkpointId) {
117!
2666
          streamMutexUnlock(&execInfo.lock);
×
2667
          taosArrayDestroy(pStreamList);
×
2668
          mError("s-task:0x%x checkpointId:%" PRId64 " is updated to %" PRId64 ", update it", pe->req.taskId,
×
2669
                 pe->req.checkpointId, chkId);
2670
          return TSDB_CODE_FAILED;
×
2671
        }
2672
        code = mndCreateSetConsensusChkptIdTrans(pMnode, pStream, pe->req.taskId, chkId, pe->req.startTs);
117✔
2673
        if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
117!
2674
          mError("failed to create consensus-checkpoint trans, stream:0x%" PRIx64, pStream->uid);
×
2675
        }
2676

2677
        void *p = taosArrayPush(pList, &pe->req.taskId);
117✔
2678
        if (p == NULL) {
117!
2679
          mError("failed to put into task list, taskId:0x%x", pe->req.taskId);
×
2680
        }
2681
        streamId = pe->req.streamId;
117✔
2682
      } else {
UNCOV
2683
        mDebug("s-task:0x%x sendTs:%" PRId64 " wait %.2fs already, wait for next round to check", pe->req.taskId,
×
2684
               pe->req.startTs, (now - pe->ts) / 1000.0);
2685
      }
2686
    }
2687

2688
    mndReleaseStream(pMnode, pStream);
29✔
2689

2690
    if (taosArrayGetSize(pList) > 0) {
29!
2691
      for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
146✔
2692
        int32_t *taskId = taosArrayGet(pList, i);
117✔
2693
        if (taskId == NULL) {
117!
2694
          continue;
×
2695
        }
2696

2697
        for (int32_t k = 0; k < taosArrayGetSize(pInfo->pTaskList); ++k) {
117!
2698
          SCheckpointConsensusEntry *pe = taosArrayGet(pInfo->pTaskList, k);
117✔
2699
          if ((pe != NULL) && (pe->req.taskId == *taskId)) {
117!
2700
            taosArrayRemove(pInfo->pTaskList, k);
117✔
2701
            break;
117✔
2702
          }
2703
        }
2704
      }
2705
    }
2706

2707
    taosArrayDestroy(pList);
29✔
2708

2709
    if (taosArrayGetSize(pInfo->pTaskList) == 0) {
29!
2710
      mndClearConsensusRspEntry(pInfo);
29✔
2711
      if (streamId == -1) {
29!
2712
        streamMutexUnlock(&execInfo.lock);
×
2713
        taosArrayDestroy(pStreamList);
×
2714
        mError("streamId is -1, streamId:%" PRIx64, pInfo->streamId);
×
2715
        return TSDB_CODE_FAILED;
×
2716
      }
2717
      void *p = taosArrayPush(pStreamList, &streamId);
29✔
2718
      if (p == NULL) {
29!
2719
        mError("failed to put into stream list, stream:0x%" PRIx64, streamId);
×
2720
      }
2721
    }
2722
  }
2723

2724
  for (int32_t i = 0; i < taosArrayGetSize(pStreamList); ++i) {
6,673✔
2725
    int64_t *pStreamId = (int64_t *)taosArrayGet(pStreamList, i);
29✔
2726
    if (pStreamId == NULL) {
29!
2727
      continue;
×
2728
    }
2729

2730
    code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, *pStreamId);
29✔
2731
  }
2732

2733
  streamMutexUnlock(&execInfo.lock);
6,644✔
2734

2735
  taosArrayDestroy(pStreamList);
6,644✔
2736
  mDebug("end to process consensus-checkpointId in tmr");
6,644✔
2737
  return code;
6,644✔
2738
}
2739

2740
static int32_t mndProcessCreateStreamReqFromMNode(SRpcMsg *pReq) {
255✔
2741
  int32_t code = mndProcessCreateStreamReq(pReq);
255✔
2742
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
255!
2743
    pReq->info.rsp = rpcMallocCont(1);
×
2744
    if (pReq->info.rsp == NULL) {
×
2745
      return terrno;
×
2746
    }
2747

2748
    pReq->info.rspLen = 1;
×
2749
    pReq->info.noResp = false;
×
2750
    pReq->code = code;
×
2751
  }
2752
  return code;
255✔
2753
}
2754

2755
static int32_t mndProcessDropStreamReqFromMNode(SRpcMsg *pReq) {
220✔
2756
  int32_t code = mndProcessDropStreamReq(pReq);
220✔
2757
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
220!
2758
    pReq->info.rsp = rpcMallocCont(1);
20✔
2759
    if (pReq->info.rsp == NULL) {
20!
2760
      return terrno;
×
2761
    }
2762

2763
    pReq->info.rspLen = 1;
20✔
2764
    pReq->info.noResp = false;
20✔
2765
    pReq->code = code;
20✔
2766
  }
2767
  return code;
220✔
2768
}
2769

2770
void mndInitStreamExecInfo(SMnode *pMnode, SStreamExecInfo *pExecInfo) {
13,868✔
2771
  if (pExecInfo->initTaskList || pMnode == NULL) {
13,868✔
2772
    return;
13,739✔
2773
  }
2774

2775
  addAllStreamTasksIntoBuf(pMnode, pExecInfo);
129✔
2776
  pExecInfo->initTaskList = true;
129✔
2777
}
2778

2779
void mndStreamResetInitTaskListLoadFlag() {
1,288✔
2780
  mInfo("reset task list buffer init flag for leader");
1,288!
2781
  execInfo.initTaskList = false;
1,288✔
2782
}
1,288✔
2783

2784
void mndUpdateStreamExecInfoRole(SMnode *pMnode, int32_t role) {
1,356✔
2785
  execInfo.switchFromFollower = false;
1,356✔
2786

2787
  if (execInfo.role == NODE_ROLE_UNINIT) {
1,356✔
2788
    execInfo.role = role;
1,329✔
2789
    if (role == NODE_ROLE_LEADER) {
1,329✔
2790
      mInfo("init mnode is set to leader");
1,279!
2791
    } else {
2792
      mInfo("init mnode is set to follower");
50!
2793
    }
2794
  } else {
2795
    if (role == NODE_ROLE_LEADER) {
27✔
2796
      if (execInfo.role == NODE_ROLE_FOLLOWER) {
9!
2797
        execInfo.role = role;
9✔
2798
        execInfo.switchFromFollower = true;
9✔
2799
        mInfo("mnode switch to be leader from follower");
9!
2800
      } else {
2801
        mInfo("mnode remain to be leader, do nothing");
×
2802
      }
2803
    } else {  // follower's
2804
      if (execInfo.role == NODE_ROLE_LEADER) {
18✔
2805
        execInfo.role = role;
1✔
2806
        mInfo("mnode switch to be follower from leader");
1!
2807
      } else {
2808
        mInfo("mnode remain to be follower, do nothing");
17!
2809
      }
2810
    }
2811
  }
2812
}
1,356✔
2813

2814
void addAllStreamTasksIntoBuf(SMnode *pMnode, SStreamExecInfo *pExecInfo) {
129✔
2815
  SSdb       *pSdb = pMnode->pSdb;
129✔
2816
  SStreamObj *pStream = NULL;
129✔
2817
  void       *pIter = NULL;
129✔
2818

2819
  while (1) {
2820
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
344✔
2821
    if (pIter == NULL) {
344✔
2822
      break;
129✔
2823
    }
2824

2825
    saveTaskAndNodeInfoIntoBuf(pStream, pExecInfo);
215✔
2826
    sdbRelease(pSdb, pStream);
215✔
2827
  }
2828
}
129✔
2829

2830
int32_t mndCreateStreamChkptInfoUpdateTrans(SMnode *pMnode, SStreamObj *pStream, SArray *pChkptInfoList) {
555✔
2831
  STrans *pTrans = NULL;
555✔
2832
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_UPDATE_NAME,
555✔
2833
                               "update checkpoint-info", &pTrans);
2834
  if (pTrans == NULL || code) {
555!
2835
    sdbRelease(pMnode->pSdb, pStream);
×
2836
    return code;
×
2837
  }
2838

2839
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_UPDATE_NAME, pStream->uid);
555✔
2840
  if (code) {
555!
2841
    sdbRelease(pMnode->pSdb, pStream);
×
2842
    mndTransDrop(pTrans);
×
2843
    return code;
×
2844
  }
2845

2846
  code = mndStreamSetUpdateChkptAction(pMnode, pTrans, pStream);
555✔
2847
  if (code) {
555!
2848
    sdbRelease(pMnode->pSdb, pStream);
×
2849
    mndTransDrop(pTrans);
×
2850
    return code;
×
2851
  }
2852

2853
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
555✔
2854
  if (code) {
555!
2855
    sdbRelease(pMnode->pSdb, pStream);
×
2856
    mndTransDrop(pTrans);
×
2857
    return code;
×
2858
  }
2859

2860
  code = mndTransPrepare(pMnode, pTrans);
555✔
2861
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
555!
2862
    mError("trans:%d, failed to prepare update checkpoint-info meta trans since %s", pTrans->id, tstrerror(code));
×
2863
    sdbRelease(pMnode->pSdb, pStream);
×
2864
    mndTransDrop(pTrans);
×
2865
    return code;
×
2866
  }
2867

2868
  sdbRelease(pMnode->pSdb, pStream);
555✔
2869
  mndTransDrop(pTrans);
555✔
2870

2871
  return TSDB_CODE_ACTION_IN_PROGRESS;
555✔
2872
}
2873

2874
static int32_t mndProcessDropOrphanTaskReq(SRpcMsg *pReq) {
×
2875
  SMnode      *pMnode = pReq->info.node;
×
2876
  int32_t      code = 0;
×
2877
  SOrphanTask *pTask = NULL;
×
2878
  int32_t      i = 0;
×
2879
  STrans      *pTrans = NULL;
×
2880
  int32_t      numOfTasks = 0;
×
2881

2882
  SMStreamDropOrphanMsg msg = {0};
×
2883
  code = tDeserializeDropOrphanTaskMsg(pReq->pCont, pReq->contLen, &msg);
×
2884
  if (code) {
×
2885
    return code;
×
2886
  }
2887

2888
  numOfTasks = taosArrayGetSize(msg.pList);
×
2889
  if (numOfTasks == 0) {
×
2890
    mDebug("no orphan tasks to drop, no need to create trans");
×
2891
    goto _err;
×
2892
  }
2893

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

2896
  i = 0;
×
2897
  while (i < numOfTasks && ((pTask = taosArrayGet(msg.pList, i)) == NULL)) {
×
2898
    i += 1;
×
2899
  }
2900

2901
  if (pTask == NULL) {
×
2902
    mError("failed to extract entry in drop orphan task list, not create trans to drop orphan-task");
×
2903
    goto _err;
×
2904
  }
2905

2906
  // check if it is conflict with other trans in both sourceDb and targetDb.
2907
  code = mndStreamTransConflictCheck(pMnode, pTask->streamId, MND_STREAM_DROP_NAME, false);
×
2908
  if (code) {
×
2909
    goto _err;
×
2910
  }
2911

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

2914
  code = doCreateTrans(pMnode, &dummyObj, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, "drop stream", &pTrans);
×
2915
  if (pTrans == NULL || code != 0) {
×
2916
    mError("failed to create trans to drop orphan tasks since %s", tstrerror(code));
×
2917
    goto _err;
×
2918
  }
2919

2920
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_DROP_NAME, pTask->streamId);
×
2921
  if (code) {
×
2922
    goto _err;
×
2923
  }
2924

2925
  // drop all tasks
2926
  if ((code = mndStreamSetDropActionFromList(pMnode, pTrans, msg.pList)) < 0) {
×
2927
    mError("failed to create trans to drop orphan tasks since %s", tstrerror(code));
×
2928
    goto _err;
×
2929
  }
2930

2931
  // drop stream
2932
  if ((code = mndPersistTransLog(&dummyObj, pTrans, SDB_STATUS_DROPPED)) < 0) {
×
2933
    goto _err;
×
2934
  }
2935

2936
  code = mndTransPrepare(pMnode, pTrans);
×
2937
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
2938
    mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
2939
    goto _err;
×
2940
  }
2941

2942
_err:
×
2943
  tDestroyDropOrphanTaskMsg(&msg);
×
2944
  mndTransDrop(pTrans);
×
2945

2946
  if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {
×
2947
    mDebug("create drop %d orphan tasks trans succ", numOfTasks);
×
2948
  }
2949
  return code;
×
2950
}
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