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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

6.31
/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) {
13✔
79
  SSdbTable table = {
13✔
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 = {
13✔
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);
13✔
99
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STREAM, mndProcessDropStreamReq);
13✔
100
  mndSetMsgHandle(pMnode, TDMT_MND_NODECHECK_TIMER, mndProcessNodeCheck);
13✔
101

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

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

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

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

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

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

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

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

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

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

UNCOV
176
  code = sdbGetRawSoftVer(pRaw, &sver);
×
UNCOV
177
  TSDB_CHECK_CODE(code, lino, _over);
×
178

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

UNCOV
184
  pRow = sdbAllocRow(sizeof(SStreamObj));
×
UNCOV
185
  TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
×
186

UNCOV
187
  pStream = sdbGetRowObj(pRow);
×
UNCOV
188
  TSDB_CHECK_NULL(pStream, code, lino, _over, terrno);
×
189

UNCOV
190
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
×
191

UNCOV
192
  buf = taosMemoryMalloc(tlen + 1);
×
UNCOV
193
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
×
194

UNCOV
195
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
×
196

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

UNCOV
202
  if (code < 0) {
×
203
    tFreeStreamObj(pStream);
×
204
  }
205

UNCOV
206
_over:
×
UNCOV
207
  taosMemoryFreeClear(buf);
×
208

UNCOV
209
  if (code != TSDB_CODE_SUCCESS) {
×
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 {
UNCOV
217
    mTrace("stream:%s, decode from raw:%p, row:%p, checkpoint:%" PRId64, pStream->name, pRaw, pStream,
×
218
           pStream->checkpointId);
219

UNCOV
220
    terrno = 0;
×
UNCOV
221
    return pRow;
×
222
  }
223
}
224

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

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

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

UNCOV
242
  taosWLockLatch(&pOldStream->lock);
×
243

UNCOV
244
  pOldStream->status = pNewStream->status;
×
UNCOV
245
  pOldStream->updateTime = pNewStream->updateTime;
×
UNCOV
246
  pOldStream->checkpointId = pNewStream->checkpointId;
×
UNCOV
247
  pOldStream->checkpointFreq = pNewStream->checkpointFreq;
×
248

UNCOV
249
  taosWUnLockLatch(&pOldStream->lock);
×
UNCOV
250
  return 0;
×
251
}
252

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

UNCOV
263
void mndReleaseStream(SMnode *pMnode, SStreamObj *pStream) {
×
UNCOV
264
  SSdb *pSdb = pMnode->pSdb;
×
UNCOV
265
  sdbRelease(pSdb, pStream);
×
UNCOV
266
}
×
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

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

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

UNCOV
289
  int32_t index = 0;
×
UNCOV
290
  for (int32_t i = 0; i < pWrapper->nCols; i++) {
×
UNCOV
291
    SField *pField = (SField *)taosArrayGet(pFields, i);
×
UNCOV
292
    if (pField == NULL) {
×
293
      return terrno;
×
294
    }
295

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

UNCOV
309
  return TSDB_CODE_SUCCESS;
×
310
}
311

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

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

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

UNCOV
335
  if (pCreate->smaId > 0) {
×
UNCOV
336
    pObj->subTableWithoutMd5 = 1;
×
337
  }
338

UNCOV
339
  pObj->smaId = pCreate->smaId;
×
UNCOV
340
  pObj->indexForMultiAggBalance = -1;
×
341

UNCOV
342
  pObj->uid = mndGenerateUid(pObj->name, strlen(pObj->name));
×
343

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

UNCOV
347
  pObj->hTaskUid = mndGenerateUid(pObj->name, strlen(pObj->name));
×
UNCOV
348
  pObj->status = 0;
×
349

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

UNCOV
358
  memcpy(pObj->sourceDb, pCreate->sourceDB, TSDB_DB_FNAME_LEN);
×
UNCOV
359
  SDbObj *pSourceDb = mndAcquireDb(pMnode, pCreate->sourceDB);
×
UNCOV
360
  if (pSourceDb == NULL) {
×
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

UNCOV
367
  pObj->sourceDbUid = pSourceDb->uid;
×
UNCOV
368
  mndReleaseDb(pMnode, pSourceDb);
×
369

UNCOV
370
  memcpy(pObj->targetSTbName, pCreate->targetStbFullName, TSDB_TABLE_FNAME_LEN);
×
371

UNCOV
372
  SDbObj *pTargetDb = mndAcquireDbByStb(pMnode, pObj->targetSTbName);
×
UNCOV
373
  if (pTargetDb == NULL) {
×
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

UNCOV
380
  tstrncpy(pObj->targetDb, pTargetDb->name, TSDB_DB_FNAME_LEN);
×
381

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

UNCOV
390
  pObj->sql = pCreate->sql;
×
UNCOV
391
  pObj->ast = pCreate->ast;
×
392

UNCOV
393
  pCreate->sql = NULL;
×
UNCOV
394
  pCreate->ast = NULL;
×
395

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

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

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

UNCOV
415
    int32_t nullIndex = 0;
×
UNCOV
416
    int32_t dataIndex = 0;
×
UNCOV
417
    for (int32_t i = 0; i < pObj->outputSchema.nCols; i++) {
×
UNCOV
418
      if (nullIndex >= numOfNULL) {
×
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 {
UNCOV
426
        SColLocation *pos = NULL;
×
UNCOV
427
        if (nullIndex < taosArrayGetSize(pCreate->fillNullCols)) {
×
UNCOV
428
          pos = taosArrayGet(pCreate->fillNullCols, nullIndex);
×
429
        }
430

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

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

UNCOV
454
    taosMemoryFree(pObj->outputSchema.pSchema);
×
UNCOV
455
    pObj->outputSchema.pSchema = pFullSchema;
×
456
  }
457

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

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

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

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

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

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

UNCOV
504
FAIL:
×
UNCOV
505
  if (pAst != NULL) nodesDestroyNode(pAst);
×
UNCOV
506
  if (pPlan != NULL) qDestroyQueryPlan(pPlan);
×
UNCOV
507
  return code;
×
508
}
509

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

UNCOV
514
  if (pTask->ver < SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
×
515
    pTask->ver = SSTREAM_TASK_VER;
×
516
  }
517

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

UNCOV
524
  int32_t size = encoder.pos;
×
UNCOV
525
  int32_t tlen = sizeof(SMsgHead) + size;
×
UNCOV
526
  tEncoderClear(&encoder);
×
527

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

UNCOV
533
  ((SMsgHead *)buf)->vgId = htonl(pTask->info.nodeId);
×
534

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

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

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

UNCOV
552
  return code;
×
553
}
554

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

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

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

UNCOV
578
  destroyStreamTaskIter(pIter);
×
579

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

UNCOV
584
    for (int32_t i = 0; i < level; i++) {
×
UNCOV
585
      SArray *pLevel = taosArrayGetP(pStream->pHTasksList, i);
×
586

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

UNCOV
598
  return code;
×
599
}
600

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

UNCOV
607
  return mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
×
608
}
609

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
692
  SStbObj stbObj = {0};
×
693

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

UNCOV
698
  stbObj.uid = pStream->targetStbUid;
×
699

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

UNCOV
705
  tFreeSMCreateStbReq(&createReq);
×
UNCOV
706
  mndFreeStb(&stbObj);
×
UNCOV
707
  mndReleaseStb(pMnode, pStb);
×
UNCOV
708
  mndReleaseDb(pMnode, pDb);
×
UNCOV
709
  mDebug("stream:%s create dst stable:%s, cols:%d", pStream->name, pStream->targetSTbName, pStream->outputSchema.nCols);
×
UNCOV
710
  return code;
×
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.
UNCOV
724
static int32_t doStreamCheck(SMnode *pMnode, SStreamObj *pStreamObj) {
×
UNCOV
725
  int32_t     numOfStream = 0;
×
UNCOV
726
  SStreamObj *pStream = NULL;
×
UNCOV
727
  void       *pIter = NULL;
×
728

UNCOV
729
  while ((pIter = sdbFetch(pMnode->pSdb, SDB_STREAM, pIter, (void **)&pStream)) != NULL) {
×
UNCOV
730
    if (pStream->sourceDbUid == pStreamObj->sourceDbUid) {
×
UNCOV
731
      ++numOfStream;
×
732
    }
733

UNCOV
734
    sdbRelease(pMnode->pSdb, pStream);
×
735

UNCOV
736
    if (numOfStream > MND_STREAM_MAX_NUM) {
×
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

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

UNCOV
751
  return TSDB_CODE_SUCCESS;
×
752
}
753

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

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

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

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

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

UNCOV
795
  if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
×
796
    goto _OVER;
×
797
  }
798

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

UNCOV
804
  SDbObj *pSourceDb = mndAcquireDb(pMnode, createReq.sourceDB);
×
UNCOV
805
  if (pSourceDb == NULL) {
×
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

UNCOV
812
  code = mndCheckForSnode(pMnode, pSourceDb);
×
UNCOV
813
  mndReleaseDb(pMnode, pSourceDb);
×
UNCOV
814
  if (code != 0) {
×
UNCOV
815
    goto _OVER;
×
816
  }
817

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

UNCOV
824
  code = doStreamCheck(pMnode, &streamObj);
×
UNCOV
825
  TSDB_CHECK_CODE(code, lino, _OVER);
×
826

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

832
  // create stb for stream
UNCOV
833
  if (createReq.createStb == STREAM_CREATE_STABLE_TRUE) {
×
UNCOV
834
    if ((code = mndCreateStbForStream(pMnode, pTrans, &streamObj, pReq->info.conn.user)) < 0) {
×
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 {
UNCOV
840
    mDebug("stream:%s no need create stable", createReq.name);
×
841
  }
842

843
  // schedule stream task for stream obj
UNCOV
844
  code = mndScheduleStream(pMnode, &streamObj, createReq.lastTs, createReq.pVgroupVerList);
×
UNCOV
845
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
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
UNCOV
852
  code = mndPersistStream(pTrans, &streamObj);
×
UNCOV
853
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
854
    mError("stream:%s, failed to persist since %s", createReq.name, tstrerror(code));
×
855
    mndTransDrop(pTrans);
×
856
    goto _OVER;
×
857
  }
858

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

UNCOV
864
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, streamObj.targetDb)) != 0) {
×
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.
UNCOV
871
  streamMutexLock(&execInfo.lock);
×
UNCOV
872
  mDebug("stream stream:%s start to register tasks into task nodeList and set initial checkpointId", createReq.name);
×
UNCOV
873
  saveTaskAndNodeInfoIntoBuf(&streamObj, &execInfo);
×
UNCOV
874
  streamMutexUnlock(&execInfo.lock);
×
875

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

UNCOV
884
  mndTransDrop(pTrans);
×
885

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

UNCOV
893
  SName name = {0};
×
UNCOV
894
  code = tNameFromString(&name, createReq.name, T_NAME_ACCT | T_NAME_TABLE);
×
UNCOV
895
  if (code) {
×
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
UNCOV
901
  if (sql != NULL && sqlLen > 0) {
×
902
    auditRecord(pReq, pMnode->clusterId, "createStream", dbname.dbname, name.dbname, sql, sqlLen);
×
903
  } else {
UNCOV
904
    char detail[1000] = {0};
×
UNCOV
905
    snprintf(detail, tListLen(detail), "dbname:%s, stream name:%s", dbname.dbname, name.dbname);
×
UNCOV
906
    auditRecord(pReq, pMnode->clusterId, "createStream", dbname.dbname, name.dbname, detail, strlen(detail));
×
907
  }
908

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

UNCOV
917
  mndReleaseStream(pMnode, pStream);
×
UNCOV
918
  tFreeSCMCreateStreamReq(&createReq);
×
UNCOV
919
  tFreeStreamObj(&streamObj);
×
920

UNCOV
921
  if (sql != NULL) {
×
UNCOV
922
    taosMemoryFreeClear(sql);
×
923
  }
924

UNCOV
925
  return code;
×
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

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

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

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

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

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

UNCOV
1037
      if (pEntry->checkpointInfo.failed) {
×
1038
        continue;
×
1039
      }
1040

UNCOV
1041
      if (maxCheckpointId < pEntry->checkpointInfo.latestId) {
×
UNCOV
1042
        maxCheckpointId = pEntry->checkpointInfo.latestId;
×
1043
      }
1044
    }
1045

UNCOV
1046
    if (lock) {
×
UNCOV
1047
      streamMutexUnlock(&execInfo.lock);
×
1048
    }
1049

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

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

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

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

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

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

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

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

UNCOV
1095
  taosWLockLatch(&pStream->lock);
×
UNCOV
1096
  pStream->currentTick = 1;
×
1097

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

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

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

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

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

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

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

UNCOV
1138
_ERR:
×
UNCOV
1139
  mndTransDrop(pTrans);
×
UNCOV
1140
  return code;
×
1141
}
1142

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

1152
  return taosArrayGetSize(execInfo.pNodeList);
50✔
1153
}
1154

1155
static int32_t mndCheckTaskAndNodeStatus(SMnode *pMnode) {
50✔
1156
  bool ready = true;
50✔
1157
  if (mndStreamNodeIsUpdated(pMnode)) {
50!
UNCOV
1158
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1159
  }
1160

1161
  streamMutexLock(&execInfo.lock);
50✔
1162
  if (taosArrayGetSize(execInfo.pNodeList) == 0) {
50!
1163
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
50✔
1164
    if (taosArrayGetSize(execInfo.pTaskList) != 0) {
50!
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));
50✔
1172

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

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

UNCOV
1184
    if (pEntry->status == TASK_STATUS__STOP) {
×
UNCOV
1185
      for (int32_t j = 0; j < taosArrayGetSize(pInvalidList); ++j) {
×
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

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

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

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

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

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

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

UNCOV
1236
    if (pEntry->status == TASK_STATUS__READY && ts < pEntry->startTime) {
×
UNCOV
1237
      ts = pEntry->startTime;
×
UNCOV
1238
      taskId = pEntry->id.taskId;
×
1239
    }
1240
  }
1241

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

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

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

UNCOV
1258
  return pInt1->duration > pInt2->duration ? -1 : 1;
×
1259
}
1260

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

1269
  if ((code = mndCheckTaskAndNodeStatus(pMnode)) != 0) {
50!
UNCOV
1270
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1271
  }
1272

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

1278
  int64_t now = taosGetTimestampMs();
50✔
1279

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

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

UNCOV
1296
    SCheckpointInterval in = {.streamId = pStream->uid, .duration = duration};
×
UNCOV
1297
    void               *p = taosArrayPush(pList, &in);
×
UNCOV
1298
    if (p) {
×
UNCOV
1299
      int32_t currentSize = taosArrayGetSize(pList);
×
UNCOV
1300
      mDebug("stream:%s (uid:0x%" PRIx64 ") total %d stream(s) beyond chpt interval threshold: %ds(%" PRId64
×
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
    }
UNCOV
1307
    sdbRelease(pSdb, pStream);
×
1308
  }
1309

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

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

UNCOV
1324
  int32_t numOfQual = taosArrayGetSize(pList);
×
UNCOV
1325
  if (numOfCheckpointTrans > tsMaxConcurrentCheckpoint) {
×
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

UNCOV
1334
  int32_t capacity = tsMaxConcurrentCheckpoint - numOfCheckpointTrans;
×
UNCOV
1335
  mDebug(
×
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

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

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

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

UNCOV
1355
      if (code == 0 || code == TSDB_CODE_ACTION_IN_PROGRESS) {
×
UNCOV
1356
        started += 1;
×
1357

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

UNCOV
1369
  taosArrayDestroy(pList);
×
UNCOV
1370
  return code;
×
1371
}
1372

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

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

UNCOV
1385
  mDebug("recv drop stream:%s msg", dropReq.name);
×
1386

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

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

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

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

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

UNCOV
1421
      if (pSma) {
×
UNCOV
1422
        sdbRelease(pMnode->pSdb, pSma);
×
1423
      }
1424

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

UNCOV
1429
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
×
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.
UNCOV
1436
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_DROP_NAME, true);
×
UNCOV
1437
  if (code) {
×
1438
    sdbRelease(pMnode->pSdb, pStream);
×
1439
    tFreeMDropStreamReq(&dropReq);
×
1440
    return code;
×
1441
  }
1442

UNCOV
1443
  STrans *pTrans = NULL;
×
UNCOV
1444
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, "drop stream", &pTrans);
×
UNCOV
1445
  if (pTrans == NULL || code) {
×
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

UNCOV
1452
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_DROP_NAME, pStream->uid);
×
UNCOV
1453
  if (code) {
×
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
UNCOV
1462
  code = mndStreamSetDropAction(pMnode, pTrans, pStream);
×
UNCOV
1463
  if (code) {
×
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
UNCOV
1472
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_DROPPED);
×
UNCOV
1473
  if (code) {
×
1474
    sdbRelease(pMnode->pSdb, pStream);
×
1475
    mndTransDrop(pTrans);
×
1476
    tFreeMDropStreamReq(&dropReq);
×
1477
    TAOS_RETURN(code);
×
1478
  }
1479

UNCOV
1480
  code = mndTransPrepare(pMnode, pTrans);
×
UNCOV
1481
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
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
UNCOV
1490
  int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
×
UNCOV
1491
  if (transId != 0) {
×
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

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

UNCOV
1499
  removeStreamTasksInBuf(pStream, &execInfo);
×
1500

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

UNCOV
1505
  sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1506
  mndTransDrop(pTrans);
×
UNCOV
1507
  tFreeMDropStreamReq(&dropReq);
×
1508

UNCOV
1509
  if (code == 0) {
×
UNCOV
1510
    return TSDB_CODE_ACTION_IN_PROGRESS;
×
1511
  } else {
UNCOV
1512
    TAOS_RETURN(code);
×
1513
  }
1514
}
1515

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

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

UNCOV
1526
    if (pStream->sourceDbUid == pDb->uid || pStream->targetDbUid == pDb->uid) {
×
UNCOV
1527
      if (pStream->sourceDbUid != pStream->targetDbUid) {
×
UNCOV
1528
        sdbRelease(pSdb, pStream);
×
UNCOV
1529
        sdbCancelFetch(pSdb, pIter);
×
UNCOV
1530
        mError("db:%s, failed to drop stream:%s since sourceDbUid:%" PRId64 " not match with targetDbUid:%" PRId64,
×
1531
               pDb->name, pStream->name, pStream->sourceDbUid, pStream->targetDbUid);
UNCOV
1532
        TAOS_RETURN(TSDB_CODE_MND_STREAM_MUST_BE_DELETED);
×
1533
      } else {
1534
        // kill the related checkpoint trans
UNCOV
1535
        int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
×
UNCOV
1536
        if (transId != 0) {
×
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
UNCOV
1542
        removeStreamTasksInBuf(pStream, &execInfo);
×
1543

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

UNCOV
1553
    sdbRelease(pSdb, pStream);
×
1554
  }
1555

1556
  return 0;
8✔
1557
}
1558

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

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

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

UNCOV
1577
  pShow->numOfRows += numOfRows;
×
UNCOV
1578
  return numOfRows;
×
1579
}
1580

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

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

UNCOV
1593
  streamMutexLock(&execInfo.lock);
×
UNCOV
1594
  mndInitStreamExecInfo(pMnode, &execInfo);
×
UNCOV
1595
  streamMutexUnlock(&execInfo.lock);
×
1596

UNCOV
1597
  while (numOfRows < rowsCapacity) {
×
UNCOV
1598
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
×
UNCOV
1599
    if (pShow->pIter == NULL) {
×
UNCOV
1600
      break;
×
1601
    }
1602

1603
    // lock
UNCOV
1604
    taosRLockLatch(&pStream->lock);
×
1605

UNCOV
1606
    int32_t count = mndGetNumOfStreamTasks(pStream);
×
UNCOV
1607
    if (numOfRows + count > rowsCapacity) {
×
UNCOV
1608
      code = blockDataEnsureCapacity(pBlock, numOfRows + count);
×
UNCOV
1609
      if (code) {
×
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

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

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

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

UNCOV
1642
      code = setTaskAttrInResBlock(pStream, pTask, pBlock, numOfRows, precision);
×
UNCOV
1643
      if (code == TSDB_CODE_SUCCESS) {
×
UNCOV
1644
        numOfRows++;
×
1645
      }
1646
    }
1647

UNCOV
1648
    pBlock->info.rows = numOfRows;
×
1649

UNCOV
1650
    destroyStreamTaskIter(pIter);
×
UNCOV
1651
    taosRUnLockLatch(&pStream->lock);
×
1652

UNCOV
1653
    sdbRelease(pSdb, pStream);
×
1654
  }
1655

UNCOV
1656
  pShow->numOfRows += numOfRows;
×
UNCOV
1657
  return numOfRows;
×
1658
}
1659

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

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

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

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

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

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

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

UNCOV
1700
  bool updated = mndStreamNodeIsUpdated(pMnode);
×
UNCOV
1701
  if (updated) {
×
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
UNCOV
1708
    bool found = false;
×
UNCOV
1709
    bool readyToPause = true;
×
UNCOV
1710
    streamMutexLock(&execInfo.lock);
×
1711

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

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

UNCOV
1723
      if (pEntry->id.streamId != pStream->uid) {
×
UNCOV
1724
        continue;
×
1725
      }
1726

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

UNCOV
1733
      found = true;
×
1734
    }
1735

UNCOV
1736
    streamMutexUnlock(&execInfo.lock);
×
UNCOV
1737
    if (!found) {
×
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

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

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

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

1765
  // if nodeUpdate happened, not send pause trans
UNCOV
1766
  code = mndStreamSetPauseAction(pMnode, pTrans, pStream);
×
UNCOV
1767
  if (code) {
×
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
UNCOV
1775
  taosWLockLatch(&pStream->lock);
×
UNCOV
1776
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
×
UNCOV
1777
  if (code) {
×
1778
    taosWUnLockLatch(&pStream->lock);
×
1779
    sdbRelease(pMnode->pSdb, pStream);
×
1780
    mndTransDrop(pTrans);
×
1781
    return code;
×
1782
  }
1783

UNCOV
1784
  taosWUnLockLatch(&pStream->lock);
×
1785

UNCOV
1786
  code = mndTransPrepare(pMnode, pTrans);
×
UNCOV
1787
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
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

UNCOV
1794
  sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1795
  mndTransDrop(pTrans);
×
1796

UNCOV
1797
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
1798
}
1799

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

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

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

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

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

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

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

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

1855
  // set the resume action
UNCOV
1856
  code = mndStreamSetResumeAction(pTrans, pMnode, pStream, resumeReq.igUntreated);
×
UNCOV
1857
  if (code) {
×
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
UNCOV
1865
  taosWLockLatch(&pStream->lock);
×
UNCOV
1866
  pStream->status = STREAM_STATUS__NORMAL;
×
UNCOV
1867
  if (mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY) < 0) {
×
1868
    taosWUnLockLatch(&pStream->lock);
×
1869

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

UNCOV
1875
  taosWUnLockLatch(&pStream->lock);
×
UNCOV
1876
  code = mndTransPrepare(pMnode, pTrans);
×
UNCOV
1877
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
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

UNCOV
1884
  sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
1885
  mndTransDrop(pTrans);
×
1886

UNCOV
1887
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
1888
}
1889

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

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

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

UNCOV
1907
    if (code) {
×
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) {
UNCOV
1915
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
UNCOV
1916
    if (pIter == NULL) {
×
UNCOV
1917
      break;
×
1918
    }
1919

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

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

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

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

UNCOV
1950
    code = mndStreamSetUpdateEpsetAction(pMnode, pStream, pChangeInfo, pTrans);
×
1951

1952
    // todo: not continue, drop all and retry again
UNCOV
1953
    if (code != TSDB_CODE_SUCCESS) {
×
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

UNCOV
1960
    code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
×
UNCOV
1961
    sdbRelease(pSdb, pStream);
×
1962

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

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

UNCOV
1974
  code = mndTransPrepare(pMnode, pTrans);
×
UNCOV
1975
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
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

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

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

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

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

UNCOV
2000
  while (1) {
×
2001
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
50✔
2002
    if (pIter == NULL) {
50!
2003
      break;
50✔
2004
    }
2005

UNCOV
2006
    taosWLockLatch(&pStream->lock);
×
2007

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

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

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

UNCOV
2032
    destroyStreamTaskIter(pTaskIter);
×
UNCOV
2033
    taosWUnLockLatch(&pStream->lock);
×
2034

UNCOV
2035
    sdbRelease(pSdb, pStream);
×
2036
  }
2037

2038
  taosArrayClear(pNodeList);
50✔
2039

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

UNCOV
2045
    void *p = taosArrayPush(pNodeList, pEntry);
×
UNCOV
2046
    if (p == NULL) {
×
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

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

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

2063
  taosHashCleanup(pHash);
50✔
2064

2065
  mDebug("numOfNodes:%d for stream after extract nodeInfo from stream", (int32_t)taosArrayGetSize(pNodeList));
50✔
2066
  return code;
50✔
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
UNCOV
2090
static int32_t mndProcessNodeCheckReq(SRpcMsg *pMsg) {
×
UNCOV
2091
  int32_t code = 0;
×
UNCOV
2092
  bool    allReady = true;
×
UNCOV
2093
  SArray *pNodeSnapshot = NULL;
×
UNCOV
2094
  SMnode *pMnode = pMsg->info.node;
×
UNCOV
2095
  int64_t ts = taosGetTimestampSec();
×
UNCOV
2096
  bool    updateAllVgroups = false;
×
2097

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

UNCOV
2104
  mDebug("start to do node changing check");
×
2105

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

UNCOV
2110
  if (numOfNodes == 0) {
×
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

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

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

UNCOV
2129
  streamMutexLock(&execInfo.lock);
×
2130

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

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

2142
  {
UNCOV
2143
    if (execInfo.role == NODE_ROLE_LEADER && execInfo.switchFromFollower) {
×
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

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

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

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

UNCOV
2174
  mndDestroyVgroupChangeInfo(&changeInfo);
×
2175

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

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

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

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

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

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

UNCOV
2210
  while (streamTaskIterNextTask(pIter)) {
×
UNCOV
2211
    SStreamTask *pTask = NULL;
×
UNCOV
2212
    code = streamTaskIterGetCurrent(pIter, &pTask);
×
UNCOV
2213
    if (code) {
×
2214
      break;
×
2215
    }
2216

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

UNCOV
2223
      code = taosHashPut(pExecNode->pTaskMap, &id, sizeof(id), &entry, sizeof(entry));
×
UNCOV
2224
      if (code == 0) {
×
UNCOV
2225
        void   *px = taosArrayPush(pExecNode->pTaskList, &id);
×
UNCOV
2226
        int32_t num = (int32_t)taosArrayGetSize(pExecNode->pTaskList);
×
UNCOV
2227
        if (px) {
×
UNCOV
2228
          mInfo("s-task:0x%x add into task buffer, total:%d", (int32_t)entry.id.taskId, num);
×
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
UNCOV
2237
      bool exist = false;
×
UNCOV
2238
      for (int32_t j = 0; j < taosArrayGetSize(pExecNode->pNodeList); ++j) {
×
UNCOV
2239
        SNodeEntry *pEntry = taosArrayGet(pExecNode->pNodeList, j);
×
UNCOV
2240
        if ((pEntry != NULL) && (pEntry->nodeId == pTask->info.nodeId)) {
×
UNCOV
2241
          exist = true;
×
UNCOV
2242
          break;
×
2243
        }
2244
      }
2245

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

UNCOV
2250
        void *px = taosArrayPush(pExecNode->pNodeList, &nodeEntry);
×
UNCOV
2251
        if (px) {
×
UNCOV
2252
          mInfo("vgId:%d added into nodeList, total:%d", nodeEntry.nodeId, (int)taosArrayGetSize(pExecNode->pNodeList));
×
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

UNCOV
2261
  destroyStreamTaskIter(pIter);
×
2262
}
2263

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

UNCOV
2272
    if (taskId == *pId) {
×
UNCOV
2273
      return;
×
2274
    }
2275
  }
2276

UNCOV
2277
  int32_t numOfTasks = taosArrayGetSize(pList);
×
UNCOV
2278
  void   *p = taosArrayPush(pList, &taskId);
×
UNCOV
2279
  if (p) {
×
UNCOV
2280
    mDebug("stream:0x%" PRIx64 " receive %d reqs for checkpoint, remain:%d", uid, numOfTasks, numOfTotal - numOfTasks);
×
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

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

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

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

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

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

UNCOV
2306
  SStreamObj *pStream = NULL;
×
UNCOV
2307
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
×
UNCOV
2308
  if (pStream == NULL || code != 0) {
×
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

UNCOV
2326
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
×
2327

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

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

UNCOV
2346
    if (pStream != NULL) {  // TODO:handle error
×
UNCOV
2347
      code = mndProcessStreamCheckpointTrans(pMnode, pStream, checkpointId, 0, false);
×
UNCOV
2348
      if (code) {
×
UNCOV
2349
        mError("failed to create checkpoint trans, code:%s", tstrerror(code));
×
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
UNCOV
2358
    (void) taosHashRemove(execInfo.pTransferStateStreams, &req.streamId, sizeof(int64_t));
×
2359

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

UNCOV
2364
  if (pStream != NULL) {
×
UNCOV
2365
    mndReleaseStream(pMnode, pStream);
×
2366
  }
2367

UNCOV
2368
  streamMutexUnlock(&execInfo.lock);
×
2369

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

UNCOV
2377
    SMsgHead *pHead = rsp.pCont;
×
UNCOV
2378
    pHead->vgId = htonl(req.nodeId);
×
2379

UNCOV
2380
    tmsgSendRsp(&rsp);
×
UNCOV
2381
    pReq->info.handle = NULL;  // disable auto rsp
×
2382
  }
2383

UNCOV
2384
  return 0;
×
2385
}
2386

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

UNCOV
2396
  if (pTaskEntry->checkpointInfo.latestId >= pReport->checkpointId) {
×
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
UNCOV
2403
  if ((pTaskEntry->checkpointInfo.activeId != 0) && (pTaskEntry->checkpointInfo.activeId > pReport->checkpointId)) {
×
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

UNCOV
2410
  if (reportChkptId >= pReport->checkpointId) {
×
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

UNCOV
2417
  return true;
×
2418
}
2419

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

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

UNCOV
2432
    if (p->taskId == pReport->taskId) {
×
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

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

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

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

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

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

UNCOV
2487
  streamMutexLock(&execInfo.lock);
×
UNCOV
2488
  mndInitStreamExecInfo(pMnode, &execInfo);
×
UNCOV
2489
  streamMutexUnlock(&execInfo.lock);
×
2490

UNCOV
2491
  mDebug("receive stream task checkpoint-report msg, vgId:%d, s-task:0x%x, checkpointId:%" PRId64
×
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.
UNCOV
2496
  streamMutexLock(&execInfo.lock);
×
2497

UNCOV
2498
  SStreamObj *pStream = NULL;
×
UNCOV
2499
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
×
UNCOV
2500
  if (pStream == NULL || code != 0) {
×
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

UNCOV
2517
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
×
2518

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

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

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

UNCOV
2543
  if (pStream != NULL) {
×
UNCOV
2544
    mndReleaseStream(pMnode, pStream);
×
2545
  }
2546

UNCOV
2547
  streamMutexUnlock(&execInfo.lock);
×
2548

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

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

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

UNCOV
2565
    if (p->streamId != streamId) {
×
UNCOV
2566
      continue;
×
2567
    }
2568

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

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

UNCOV
2584
  return chkId;
×
2585
}
2586

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

UNCOV
2594
    tmsgSendRsp(&rsp);
×
UNCOV
2595
    pInfo->handle = NULL;  // disable auto rsp
×
2596
  }
UNCOV
2597
}
×
2598

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

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

2609
  bool    allReady = true;
321✔
2610
  SArray *pNodeSnapshot = NULL;
321✔
2611

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

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

2624
  streamMutexLock(&execInfo.lock);
138✔
2625

2626
  void *pIter = NULL;
138✔
2627
  while ((pIter = taosHashIterate(execInfo.pStreamConsensus, pIter)) != NULL) {
138!
UNCOV
2628
    SCheckpointConsensusInfo *pInfo = (SCheckpointConsensusInfo *)pIter;
×
2629

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

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

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

UNCOV
2651
      streamId = pe->req.streamId;
×
2652

UNCOV
2653
      int32_t existed = 0;
×
UNCOV
2654
      bool    allSame = true;
×
UNCOV
2655
      int64_t chkId = getConsensusId(pe->req.streamId, pInfo->numOfTasks, &existed, &allSame);
×
UNCOV
2656
      if (chkId == -1) {
×
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

UNCOV
2662
      if (((now - pe->ts) >= 10 * 1000) || allSame) {
×
UNCOV
2663
        mDebug("s-task:0x%x sendTs:%" PRId64 " wait %.2fs and all tasks have same checkpointId", pe->req.taskId,
×
2664
               pe->req.startTs, (now - pe->ts) / 1000.0);
UNCOV
2665
        if (chkId > pe->req.checkpointId) {
×
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
        }
UNCOV
2672
        code = mndCreateSetConsensusChkptIdTrans(pMnode, pStream, pe->req.taskId, chkId, pe->req.startTs);
×
UNCOV
2673
        if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
2674
          mError("failed to create consensus-checkpoint trans, stream:0x%" PRIx64, pStream->uid);
×
2675
        }
2676

UNCOV
2677
        void *p = taosArrayPush(pList, &pe->req.taskId);
×
UNCOV
2678
        if (p == NULL) {
×
2679
          mError("failed to put into task list, taskId:0x%x", pe->req.taskId);
×
2680
        }
UNCOV
2681
        streamId = pe->req.streamId;
×
2682
      } else {
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

UNCOV
2688
    mndReleaseStream(pMnode, pStream);
×
2689

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

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

UNCOV
2707
    taosArrayDestroy(pList);
×
2708

UNCOV
2709
    if (taosArrayGetSize(pInfo->pTaskList) == 0) {
×
UNCOV
2710
      mndClearConsensusRspEntry(pInfo);
×
UNCOV
2711
      if (streamId == -1) {
×
2712
        streamMutexUnlock(&execInfo.lock);
×
2713
        taosArrayDestroy(pStreamList);
×
2714
        mError("streamId is -1, streamId:%" PRIx64, pInfo->streamId);
×
2715
        return TSDB_CODE_FAILED;
×
2716
      }
UNCOV
2717
      void *p = taosArrayPush(pStreamList, &streamId);
×
UNCOV
2718
      if (p == NULL) {
×
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) {
138!
UNCOV
2725
    int64_t *pStreamId = (int64_t *)taosArrayGet(pStreamList, i);
×
UNCOV
2726
    if (pStreamId == NULL) {
×
2727
      continue;
×
2728
    }
2729

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

2733
  streamMutexUnlock(&execInfo.lock);
138✔
2734

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

UNCOV
2740
static int32_t mndProcessCreateStreamReqFromMNode(SRpcMsg *pReq) {
×
UNCOV
2741
  int32_t code = mndProcessCreateStreamReq(pReq);
×
UNCOV
2742
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
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
  }
UNCOV
2752
  return code;
×
2753
}
2754

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

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

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

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

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

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

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

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

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

UNCOV
2825
    saveTaskAndNodeInfoIntoBuf(pStream, pExecInfo);
×
UNCOV
2826
    sdbRelease(pSdb, pStream);
×
2827
  }
UNCOV
2828
}
×
2829

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

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

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

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

UNCOV
2860
  code = mndTransPrepare(pMnode, pTrans);
×
UNCOV
2861
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
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

UNCOV
2868
  sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
2869
  mndTransDrop(pTrans);
×
2870

UNCOV
2871
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
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