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

taosdata / TDengine / #3633

11 Mar 2025 12:59PM UTC coverage: 0.0% (-60.7%) from 60.719%
#3633

push

travis-ci

web-flow
Merge pull request #30118 from taosdata/wl30

udpate ci workflow

0 of 280412 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 275582 relevant lines covered (0.0%)

0.0 hits per line

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

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

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

30
#define MND_STREAM_MAX_NUM 60
31

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

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

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

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

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

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

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

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

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

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

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

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

131
  mndSetMsgHandle(pMnode, TDMT_MND_PAUSE_STREAM, mndProcessPauseStreamReq);
×
132
  mndSetMsgHandle(pMnode, TDMT_MND_RESUME_STREAM, mndProcessResumeStreamReq);
×
133
  mndSetMsgHandle(pMnode, TDMT_MND_RESET_STREAM, mndProcessResetStreamReq);
×
134

135
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndRetrieveStream);
×
136
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndCancelGetNextStream);
×
137
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndRetrieveStreamTask);
×
138
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndCancelGetNextStreamTask);
×
139

140
  int32_t code = mndInitExecInfo();
×
141
  if (code) {
×
142
    return code;
×
143
  }
144

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

150
  code = sdbSetTable(pMnode->pSdb, tableSeq);
×
151
  return code;
×
152
}
153

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

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

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

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

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

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

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

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

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

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

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

207
_over:
×
208
  taosMemoryFreeClear(buf);
×
209

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

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

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

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

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

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

243
  taosWLockLatch(&pOldStream->lock);
×
244

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

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

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

264
void mndReleaseStream(SMnode *pMnode, SStreamObj *pStream) {
×
265
  SSdb *pSdb = pMnode->pSdb;
×
266
  sdbRelease(pSdb, pStream);
×
267
}
×
268

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

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

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

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

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

310
  return TSDB_CODE_SUCCESS;
×
311
}
312

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

391
  pObj->sql = pCreate->sql;
×
392
  pObj->ast = pCreate->ast;
×
393

394
  pCreate->sql = NULL;
×
395
  pCreate->ast = NULL;
×
396

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

553
  return code;
×
554
}
555

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

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

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

579
  destroyStreamTaskIter(pIter);
×
580

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

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

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

599
  return code;
×
600
}
601

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

693
  SStbObj stbObj = {0};
×
694

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

699
  stbObj.uid = pStream->targetStbUid;
×
700

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

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

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

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

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

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

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

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

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

752
  return TSDB_CODE_SUCCESS;
×
753
}
754

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

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

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

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

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

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

790
  TSDB_CHECK_NULL(createReq, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
791
  TSDB_CHECK_NULL(pStream, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
792

793
  if (taosArrayGetSize(createReq->pNotifyAddrUrls) == 0) {
×
794
    goto _end;
×
795
  }
796

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

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

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

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

837
  SCMCreateStreamReq createReq = {0};
×
838
  code = tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, &createReq);
×
839
  TSDB_CHECK_CODE(code, lino, _OVER);
×
840

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

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

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

867
  if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
×
868
    goto _OVER;
×
869
  }
870

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

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

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

891
  code = mndCheckForSnode(pMnode, pSourceDb);
×
892
  mndReleaseDb(pMnode, pSourceDb);
×
893
  if (code != 0) {
×
894
    goto _OVER;
×
895
  }
896

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

903
  code = doStreamCheck(pMnode, &streamObj);
×
904
  TSDB_CHECK_CODE(code, lino, _OVER);
×
905

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

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

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

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

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

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

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

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

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

971
  mndTransDrop(pTrans);
×
972

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

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

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

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

1004
  mndReleaseStream(pMnode, pStream);
×
1005
  tFreeSCMCreateStreamReq(&createReq);
×
1006
  tFreeStreamObj(&streamObj);
×
1007

1008
  if (sql != NULL) {
×
1009
    taosMemoryFreeClear(sql);
×
1010
  }
1011

1012
  return code;
×
1013
}
1014

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

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

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

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

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

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

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

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

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

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

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

1092
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
1093
}
1094

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

1101
  while (1) {
1102
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
1103
    if (pIter == NULL) break;
×
1104

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

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

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

1124
      if (pEntry->checkpointInfo.failed) {
×
1125
        continue;
×
1126
      }
1127

1128
      if (maxCheckpointId < pEntry->checkpointInfo.latestId) {
×
1129
        maxCheckpointId = pEntry->checkpointInfo.latestId;
×
1130
      }
1131
    }
1132

1133
    if (lock) {
×
1134
      streamMutexUnlock(&execInfo.lock);
×
1135
    }
1136

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

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

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

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

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

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

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

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

1182
  taosWLockLatch(&pStream->lock);
×
1183
  pStream->currentTick = 1;
×
1184

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

1191
    if (p->info.taskLevel == TASK_LEVEL__SOURCE) {
×
1192
      int32_t sz = taosArrayGetSize(pLevel);
×
1193
      for (int32_t j = 0; j < sz; j++) {
×
1194
        SStreamTask *pTask = taosArrayGetP(pLevel, j);
×
1195
        code = mndStreamSetCheckpointAction(pMnode, pTrans, pTask, checkpointId, mndTrigger);
×
1196

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

1205
  // 2. reset tick
1206
  pStream->checkpointId = checkpointId;
×
1207
  pStream->checkpointFreq = taosGetTimestampMs();
×
1208
  pStream->currentTick = 0;
×
1209

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

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

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

1225
_ERR:
×
1226
  mndTransDrop(pTrans);
×
1227
  return code;
×
1228
}
1229

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

1239
  return taosArrayGetSize(execInfo.pNodeList);
×
1240
}
1241

1242
static int32_t mndCheckTaskAndNodeStatus(SMnode *pMnode) {
×
1243
  int32_t code = 0;
×
1244
  if (mndStreamNodeIsUpdated(pMnode)) {
×
1245
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1246
  }
1247

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

1257
  streamMutexUnlock(&execInfo.lock);
×
1258
  return code;
×
1259
}
1260

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

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

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

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

1287
    if (ts < pEntry->startTime) {
×
1288
      ts = pEntry->startTime;
×
1289
      taskId = pEntry->id.taskId;
×
1290
    }
1291
  }
1292

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

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

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

1309
  return pInt1->duration > pInt2->duration ? -1 : 1;
×
1310
}
1311

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

1316
  streamMutexLock(&execInfo.lock);
×
1317

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

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

1327
    ready = false;
×
1328
  } else {
1329
    ready = true;
×
1330
  }
1331

1332
  streamMutexUnlock(&execInfo.lock);
×
1333
  return ready;
×
1334
}
1335

1336
static int32_t mndProcessStreamCheckpoint(SRpcMsg *pReq) {
×
1337
  SMnode     *pMnode = pReq->info.node;
×
1338
  SSdb       *pSdb = pMnode->pSdb;
×
1339
  void       *pIter = NULL;
×
1340
  SStreamObj *pStream = NULL;
×
1341
  int32_t     code = 0;
×
1342
  int32_t     numOfCheckpointTrans = 0;
×
1343
  SArray     *pLongChkpts = NULL;
×
1344
  SArray     *pList = NULL;
×
1345
  int64_t     now = taosGetTimestampMs();
×
1346

1347
  if ((code = mndCheckTaskAndNodeStatus(pMnode)) != 0) {
×
1348
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
1349
  }
1350

1351
  pList = taosArrayInit(4, sizeof(SCheckpointInterval));
×
1352
  if (pList == NULL) {
×
1353
    mError("failed to init chkptInterval info, not handle stream checkpoint, code:%s", tstrerror(terrno));
×
1354
    return terrno;
×
1355
  }
1356

1357
  pLongChkpts = taosArrayInit(4, sizeof(SStreamTransInfo));
×
1358
  if (pLongChkpts == NULL) {
×
1359
    mError("failed to init long checkpoint list, not handle stream checkpoint, code:%s", tstrerror(terrno));
×
1360
    taosArrayDestroy(pList);
×
1361
    return terrno;
×
1362
  }
1363

1364
  // check if ongong checkpoint trans or long chkpt trans exist.
1365
  code = mndStreamClearFinishedTrans(pMnode, &numOfCheckpointTrans, pLongChkpts);
×
1366
  if (code) {
×
1367
    mError("failed to clear finish trans, code:%s", tstrerror(code));
×
1368

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

1374
  // kill long exec checkpoint and set task status
1375
  if (taosArrayGetSize(pLongChkpts) > 0) {
×
1376
    killChkptAndResetStreamTask(pMnode, pLongChkpts);
×
1377

1378
    taosArrayDestroy(pList);
×
1379
    taosArrayDestroy(pLongChkpts);
×
1380
    return TSDB_CODE_SUCCESS;
×
1381
  }
1382

1383
  taosArrayDestroy(pLongChkpts);
×
1384

1385
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream)) != NULL) {
×
1386
    int64_t duration = now - pStream->checkpointFreq;
×
1387
    if (duration < tsStreamCheckpointInterval * 1000) {
×
1388
      sdbRelease(pSdb, pStream);
×
1389
      continue;
×
1390
    }
1391

1392
    bool ready = isStreamReadyHelp(now, pStream);
×
1393
    if (!ready) {
×
1394
      sdbRelease(pSdb, pStream);
×
1395
      continue;
×
1396
    }
1397

1398
    SCheckpointInterval in = {.streamId = pStream->uid, .duration = duration};
×
1399
    void               *p = taosArrayPush(pList, &in);
×
1400
    if (p) {
×
1401
      int32_t currentSize = taosArrayGetSize(pList);
×
1402
      mDebug("stream:%s (uid:0x%" PRIx64 ") total %d stream(s) beyond chkpt interval threshold: %ds(%" PRId64
×
1403
             "s), concurrently launch threshold:%d",
1404
             pStream->name, pStream->uid, currentSize, tsStreamCheckpointInterval, duration / 1000,
1405
             tsMaxConcurrentCheckpoint);
1406
    } else {
1407
      mError("failed to record the checkpoint interval info, stream:0x%" PRIx64, pStream->uid);
×
1408
    }
1409
    sdbRelease(pSdb, pStream);
×
1410
  }
1411

1412
  int32_t size = taosArrayGetSize(pList);
×
1413
  if (size == 0) {
×
1414
    taosArrayDestroy(pList);
×
1415
    return code;
×
1416
  }
1417

1418
  taosArraySort(pList, streamWaitComparFn);
×
1419

1420
  int32_t numOfQual = taosArrayGetSize(pList);
×
1421
  if (numOfCheckpointTrans >= tsMaxConcurrentCheckpoint) {
×
1422
    mDebug(
×
1423
        "%d stream(s) checkpoint interval longer than %ds, ongoing checkpoint trans:%d reach maximum allowed:%d, new "
1424
        "checkpoint trans are not allowed, wait for 30s",
1425
        numOfQual, tsStreamCheckpointInterval, numOfCheckpointTrans, tsMaxConcurrentCheckpoint);
1426
    taosArrayDestroy(pList);
×
1427
    return code;
×
1428
  }
1429

1430
  int32_t capacity = tsMaxConcurrentCheckpoint - numOfCheckpointTrans;
×
1431
  mDebug(
×
1432
      "%d stream(s) checkpoint interval longer than %ds, %d ongoing checkpoint trans, %d new checkpoint trans allowed, "
1433
      "concurrent trans threshold:%d",
1434
      numOfQual, tsStreamCheckpointInterval, numOfCheckpointTrans, capacity, tsMaxConcurrentCheckpoint);
1435

1436
  int32_t started = 0;
×
1437
  int64_t checkpointId = mndStreamGenChkptId(pMnode, true);
×
1438

1439
  for (int32_t i = 0; i < numOfQual; ++i) {
×
1440
    SCheckpointInterval *pCheckpointInfo = taosArrayGet(pList, i);
×
1441
    if (pCheckpointInfo == NULL) {
×
1442
      continue;
×
1443
    }
1444

1445
    SStreamObj *p = NULL;
×
1446
    code = mndGetStreamObj(pMnode, pCheckpointInfo->streamId, &p);
×
1447
    if (p != NULL && code == 0) {
×
1448
      code = mndProcessStreamCheckpointTrans(pMnode, p, checkpointId, 1, true);
×
1449
      sdbRelease(pSdb, p);
×
1450

1451
      if (code == 0 || code == TSDB_CODE_ACTION_IN_PROGRESS) {
×
1452
        started += 1;
×
1453

1454
        if (started >= capacity) {
×
1455
          mDebug("already start %d new checkpoint trans, current active checkpoint trans:%d", started,
×
1456
                 (started + numOfCheckpointTrans));
1457
          break;
×
1458
        }
1459
      } else {
1460
        mError("failed to start checkpoint trans, code:%s", tstrerror(code));
×
1461
      }
1462
    }
1463
  }
1464

1465
  taosArrayDestroy(pList);
×
1466
  return code;
×
1467
}
1468

1469
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) {
×
1470
  SMnode     *pMnode = pReq->info.node;
×
1471
  SStreamObj *pStream = NULL;
×
1472
  int32_t     code = 0;
×
1473

1474
  SMDropStreamReq dropReq = {0};
×
1475
  if (tDeserializeSMDropStreamReq(pReq->pCont, pReq->contLen, &dropReq) < 0) {
×
1476
    mError("invalid drop stream msg recv, discarded");
×
1477
    code = TSDB_CODE_INVALID_MSG;
×
1478
    TAOS_RETURN(code);
×
1479
  }
1480

1481
  mDebug("recv drop stream:%s msg", dropReq.name);
×
1482

1483
  code = mndAcquireStream(pMnode, dropReq.name, &pStream);
×
1484
  if (pStream == NULL || code != 0) {
×
1485
    if (dropReq.igNotExists) {
×
1486
      mInfo("stream:%s not exist, ignore not exist is set, drop stream exec done with success", dropReq.name);
×
1487
      sdbRelease(pMnode->pSdb, pStream);
×
1488
      tFreeMDropStreamReq(&dropReq);
×
1489
      return 0;
×
1490
    } else {
1491
      mError("stream:%s not exist failed to drop it", dropReq.name);
×
1492
      tFreeMDropStreamReq(&dropReq);
×
1493
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1494
    }
1495
  }
1496

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

1500
    void    *pIter = NULL;
×
1501
    SSmaObj *pSma = NULL;
×
1502
    pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
×
1503
    while (pIter) {
×
1504
      if (pSma && pSma->uid == pStream->smaId) {
×
1505
        sdbRelease(pMnode->pSdb, pSma);
×
1506
        sdbRelease(pMnode->pSdb, pStream);
×
1507

1508
        sdbCancelFetch(pMnode->pSdb, pIter);
×
1509
        tFreeMDropStreamReq(&dropReq);
×
1510
        code = TSDB_CODE_TSMA_MUST_BE_DROPPED;
×
1511

1512
        mError("try to drop sma-related stream:%s, uid:0x%" PRIx64 " code:%s only allowed to be dropped along with sma",
×
1513
               dropReq.name, pStream->uid, tstrerror(terrno));
1514
        TAOS_RETURN(code);
×
1515
      }
1516

1517
      if (pSma) {
×
1518
        sdbRelease(pMnode->pSdb, pSma);
×
1519
      }
1520

1521
      pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
×
1522
    }
1523
  }
1524

1525
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
×
1526
    sdbRelease(pMnode->pSdb, pStream);
×
1527
    tFreeMDropStreamReq(&dropReq);
×
1528
    return -1;
×
1529
  }
1530

1531
  // check if it is conflict with other trans in both sourceDb and targetDb.
1532
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_DROP_NAME, true);
×
1533
  if (code) {
×
1534
    sdbRelease(pMnode->pSdb, pStream);
×
1535
    tFreeMDropStreamReq(&dropReq);
×
1536
    return code;
×
1537
  }
1538

1539
  STrans *pTrans = NULL;
×
1540
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, "drop stream", &pTrans);
×
1541
  if (pTrans == NULL || code) {
×
1542
    mError("stream:%s uid:0x%" PRIx64 " failed to drop since %s", dropReq.name, pStream->uid, tstrerror(code));
×
1543
    sdbRelease(pMnode->pSdb, pStream);
×
1544
    tFreeMDropStreamReq(&dropReq);
×
1545
    TAOS_RETURN(code);
×
1546
  }
1547

1548
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_DROP_NAME, pStream->uid);
×
1549
  if (code) {
×
1550
    mError("failed to register drop stream trans, code:%s", tstrerror(code));
×
1551
    sdbRelease(pMnode->pSdb, pStream);
×
1552
    mndTransDrop(pTrans);
×
1553
    tFreeMDropStreamReq(&dropReq);
×
1554
    TAOS_RETURN(code);
×
1555
  }
1556

1557
  // drop all tasks
1558
  code = mndStreamSetDropAction(pMnode, pTrans, pStream);
×
1559
  if (code) {
×
1560
    mError("stream:%s uid:0x%" PRIx64 " failed to drop task since %s", dropReq.name, pStream->uid, tstrerror(code));
×
1561
    sdbRelease(pMnode->pSdb, pStream);
×
1562
    mndTransDrop(pTrans);
×
1563
    tFreeMDropStreamReq(&dropReq);
×
1564
    TAOS_RETURN(code);
×
1565
  }
1566

1567
  // drop stream
1568
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_DROPPED);
×
1569
  if (code) {
×
1570
    sdbRelease(pMnode->pSdb, pStream);
×
1571
    mndTransDrop(pTrans);
×
1572
    tFreeMDropStreamReq(&dropReq);
×
1573
    TAOS_RETURN(code);
×
1574
  }
1575

1576
  code = mndTransPrepare(pMnode, pTrans);
×
1577
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
1578
    mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
1579
    sdbRelease(pMnode->pSdb, pStream);
×
1580
    mndTransDrop(pTrans);
×
1581
    tFreeMDropStreamReq(&dropReq);
×
1582
    TAOS_RETURN(code);
×
1583
  }
1584

1585
  // kill the related checkpoint trans
1586
  int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
×
1587
  if (transId != 0) {
×
1588
    mDebug("drop active transId:%d due to stream:%s uid:0x%" PRIx64 " dropped", transId, pStream->name, pStream->uid);
×
1589
    mndKillTransImpl(pMnode, transId, pStream->sourceDb);
×
1590
  }
1591

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

1595
  removeStreamTasksInBuf(pStream, &execInfo);
×
1596

1597
  SName name = {0};
×
1598
  code = tNameFromString(&name, dropReq.name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
×
1599
  auditRecord(pReq, pMnode->clusterId, "dropStream", "", name.dbname, dropReq.sql, dropReq.sqlLen);
×
1600

1601
  sdbRelease(pMnode->pSdb, pStream);
×
1602
  mndTransDrop(pTrans);
×
1603
  tFreeMDropStreamReq(&dropReq);
×
1604

1605
  if (code == 0) {
×
1606
    return TSDB_CODE_ACTION_IN_PROGRESS;
×
1607
  } else {
1608
    TAOS_RETURN(code);
×
1609
  }
1610
}
1611

1612
int32_t mndDropStreamByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
×
1613
  SSdb   *pSdb = pMnode->pSdb;
×
1614
  void   *pIter = NULL;
×
1615
  int32_t code = 0;
×
1616

1617
  while (1) {
×
1618
    SStreamObj *pStream = NULL;
×
1619
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
1620
    if (pIter == NULL) break;
×
1621

1622
    if (pStream->sourceDbUid == pDb->uid || pStream->targetDbUid == pDb->uid) {
×
1623
      if (pStream->sourceDbUid != pStream->targetDbUid) {
×
1624
        sdbRelease(pSdb, pStream);
×
1625
        sdbCancelFetch(pSdb, pIter);
×
1626
        mError("db:%s, failed to drop stream:%s since sourceDbUid:%" PRId64 " not match with targetDbUid:%" PRId64,
×
1627
               pDb->name, pStream->name, pStream->sourceDbUid, pStream->targetDbUid);
1628
        TAOS_RETURN(TSDB_CODE_MND_STREAM_MUST_BE_DELETED);
×
1629
      } else {
1630
        // kill the related checkpoint trans
1631
        int32_t transId = mndStreamGetRelTrans(pMnode, pStream->uid);
×
1632
        if (transId != 0) {
×
1633
          mDebug("drop active related transId:%d due to stream:%s dropped", transId, pStream->name);
×
1634
          mndKillTransImpl(pMnode, transId, pStream->sourceDb);
×
1635
        }
1636

1637
        // drop the stream obj in execInfo
1638
        removeStreamTasksInBuf(pStream, &execInfo);
×
1639

1640
        code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_DROPPED);
×
1641
        if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
1642
          sdbRelease(pSdb, pStream);
×
1643
          sdbCancelFetch(pSdb, pIter);
×
1644
          return code;
×
1645
        }
1646
      }
1647
    }
1648

1649
    sdbRelease(pSdb, pStream);
×
1650
  }
1651

1652
  return 0;
×
1653
}
1654

1655
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
1656
  SMnode     *pMnode = pReq->info.node;
×
1657
  SSdb       *pSdb = pMnode->pSdb;
×
1658
  int32_t     numOfRows = 0;
×
1659
  SStreamObj *pStream = NULL;
×
1660
  int32_t     code = 0;
×
1661

1662
  while (numOfRows < rows) {
×
1663
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
×
1664
    if (pShow->pIter == NULL) break;
×
1665

1666
    code = setStreamAttrInResBlock(pStream, pBlock, numOfRows);
×
1667
    if (code == 0) {
×
1668
      numOfRows++;
×
1669
    }
1670
    sdbRelease(pSdb, pStream);
×
1671
  }
1672

1673
  pShow->numOfRows += numOfRows;
×
1674
  return numOfRows;
×
1675
}
1676

1677
static void mndCancelGetNextStream(SMnode *pMnode, void *pIter) {
×
1678
  SSdb *pSdb = pMnode->pSdb;
×
1679
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
1680
}
×
1681

1682
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
×
1683
  SMnode     *pMnode = pReq->info.node;
×
1684
  SSdb       *pSdb = pMnode->pSdb;
×
1685
  int32_t     numOfRows = 0;
×
1686
  SStreamObj *pStream = NULL;
×
1687
  int32_t     code = 0;
×
1688

1689
  streamMutexLock(&execInfo.lock);
×
1690
  mndInitStreamExecInfo(pMnode, &execInfo);
×
1691
  streamMutexUnlock(&execInfo.lock);
×
1692

1693
  while (numOfRows < rowsCapacity) {
×
1694
    pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
×
1695
    if (pShow->pIter == NULL) {
×
1696
      break;
×
1697
    }
1698

1699
    // lock
1700
    taosRLockLatch(&pStream->lock);
×
1701

1702
    int32_t count = mndGetNumOfStreamTasks(pStream);
×
1703
    if (numOfRows + count > rowsCapacity) {
×
1704
      code = blockDataEnsureCapacity(pBlock, numOfRows + count);
×
1705
      if (code) {
×
1706
        mError("failed to prepare the result block buffer, quit return value");
×
1707
        taosRUnLockLatch(&pStream->lock);
×
1708
        sdbRelease(pSdb, pStream);
×
1709
        continue;
×
1710
      }
1711
    }
1712

1713
    int32_t precision = TSDB_TIME_PRECISION_MILLI;
×
1714
    SDbObj *pSourceDb = mndAcquireDb(pMnode, pStream->sourceDb);
×
1715
    if (pSourceDb != NULL) {
×
1716
      precision = pSourceDb->cfg.precision;
×
1717
      mndReleaseDb(pMnode, pSourceDb);
×
1718
    }
1719

1720
    // add row for each task
1721
    SStreamTaskIter *pIter = NULL;
×
1722
    code = createStreamTaskIter(pStream, &pIter);
×
1723
    if (code) {
×
1724
      taosRUnLockLatch(&pStream->lock);
×
1725
      sdbRelease(pSdb, pStream);
×
1726
      mError("failed to create task iter for stream:%s", pStream->name);
×
1727
      continue;
×
1728
    }
1729

1730
    while (streamTaskIterNextTask(pIter)) {
×
1731
      SStreamTask *pTask = NULL;
×
1732
      code = streamTaskIterGetCurrent(pIter, &pTask);
×
1733
      if (code) {
×
1734
        destroyStreamTaskIter(pIter);
×
1735
        break;
×
1736
      }
1737

1738
      code = setTaskAttrInResBlock(pStream, pTask, pBlock, numOfRows, precision);
×
1739
      if (code == TSDB_CODE_SUCCESS) {
×
1740
        numOfRows++;
×
1741
      }
1742
    }
1743

1744
    pBlock->info.rows = numOfRows;
×
1745

1746
    destroyStreamTaskIter(pIter);
×
1747
    taosRUnLockLatch(&pStream->lock);
×
1748

1749
    sdbRelease(pSdb, pStream);
×
1750
  }
1751

1752
  pShow->numOfRows += numOfRows;
×
1753
  return numOfRows;
×
1754
}
1755

1756
static void mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter) {
×
1757
  SSdb *pSdb = pMnode->pSdb;
×
1758
  sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
×
1759
}
×
1760

1761
static int32_t mndProcessPauseStreamReq(SRpcMsg *pReq) {
×
1762
  SMnode     *pMnode = pReq->info.node;
×
1763
  SStreamObj *pStream = NULL;
×
1764
  int32_t     code = 0;
×
1765

1766
  SMPauseStreamReq pauseReq = {0};
×
1767
  if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
×
1768
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1769
  }
1770

1771
  code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
×
1772
  if (pStream == NULL || code != 0) {
×
1773
    if (pauseReq.igNotExists) {
×
1774
      mInfo("stream:%s, not exist, not pause stream", pauseReq.name);
×
1775
      return 0;
×
1776
    } else {
1777
      mError("stream:%s not exist, failed to pause stream", pauseReq.name);
×
1778
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1779
    }
1780
  }
1781

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

1784
  if ((code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb)) != 0) {
×
1785
    sdbRelease(pMnode->pSdb, pStream);
×
1786
    return code;
×
1787
  }
1788

1789
  // check if it is conflict with other trans in both sourceDb and targetDb.
1790
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_PAUSE_NAME, true);
×
1791
  if (code) {
×
1792
    sdbRelease(pMnode->pSdb, pStream);
×
1793
    TAOS_RETURN(code);
×
1794
  }
1795

1796
  bool updated = mndStreamNodeIsUpdated(pMnode);
×
1797
  if (updated) {
×
1798
    mError("tasks are not ready for pause, node update detected");
×
1799
    sdbRelease(pMnode->pSdb, pStream);
×
1800
    TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1801
  }
1802

1803
  {  // check for tasks, if tasks are not ready, not allowed to pause
1804
    bool found = false;
×
1805
    bool readyToPause = true;
×
1806
    streamMutexLock(&execInfo.lock);
×
1807

1808
    for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
×
1809
      STaskId *p = taosArrayGet(execInfo.pTaskList, i);
×
1810
      if (p == NULL) {
×
1811
        continue;
×
1812
      }
1813

1814
      STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
×
1815
      if (pEntry == NULL) {
×
1816
        continue;
×
1817
      }
1818

1819
      if (pEntry->id.streamId != pStream->uid) {
×
1820
        continue;
×
1821
      }
1822

1823
      if (pEntry->status == TASK_STATUS__UNINIT || pEntry->status == TASK_STATUS__CK) {
×
1824
        mError("stream:%s uid:0x%" PRIx64 " vgId:%d task:0x%" PRIx64 " status:%s, not ready for pause", pStream->name,
×
1825
               pStream->uid, pEntry->nodeId, pEntry->id.taskId, streamTaskGetStatusStr(pEntry->status));
1826
        readyToPause = false;
×
1827
      }
1828

1829
      found = true;
×
1830
    }
1831

1832
    streamMutexUnlock(&execInfo.lock);
×
1833
    if (!found) {
×
1834
      mError("stream:%s task not report status yet, not ready for pause", pauseReq.name);
×
1835
      sdbRelease(pMnode->pSdb, pStream);
×
1836
      TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1837
    }
1838

1839
    if (!readyToPause) {
×
1840
      mError("stream:%s task not ready for pause yet", pauseReq.name);
×
1841
      sdbRelease(pMnode->pSdb, pStream);
×
1842
      TAOS_RETURN(TSDB_CODE_STREAM_TASK_IVLD_STATUS);
×
1843
    }
1844
  }
1845

1846
  STrans *pTrans = NULL;
×
1847
  code = doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_PAUSE_NAME, "pause the stream", &pTrans);
×
1848
  if (pTrans == NULL || code) {
×
1849
    mError("stream:%s failed to pause stream since %s", pauseReq.name, tstrerror(code));
×
1850
    sdbRelease(pMnode->pSdb, pStream);
×
1851
    return code;
×
1852
  }
1853

1854
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_PAUSE_NAME, pStream->uid);
×
1855
  if (code) {
×
1856
    sdbRelease(pMnode->pSdb, pStream);
×
1857
    mndTransDrop(pTrans);
×
1858
    return code;
×
1859
  }
1860

1861
  // if nodeUpdate happened, not send pause trans
1862
  code = mndStreamSetPauseAction(pMnode, pTrans, pStream);
×
1863
  if (code) {
×
1864
    mError("stream:%s, failed to pause task since %s", pauseReq.name, tstrerror(code));
×
1865
    sdbRelease(pMnode->pSdb, pStream);
×
1866
    mndTransDrop(pTrans);
×
1867
    return code;
×
1868
  }
1869

1870
  // pause stream
1871
  taosWLockLatch(&pStream->lock);
×
1872
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
×
1873
  if (code) {
×
1874
    taosWUnLockLatch(&pStream->lock);
×
1875
    sdbRelease(pMnode->pSdb, pStream);
×
1876
    mndTransDrop(pTrans);
×
1877
    return code;
×
1878
  }
1879

1880
  taosWUnLockLatch(&pStream->lock);
×
1881

1882
  code = mndTransPrepare(pMnode, pTrans);
×
1883
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
1884
    mError("trans:%d, failed to prepare pause stream trans since %s", pTrans->id, tstrerror(code));
×
1885
    sdbRelease(pMnode->pSdb, pStream);
×
1886
    mndTransDrop(pTrans);
×
1887
    return code;
×
1888
  }
1889

1890
  sdbRelease(pMnode->pSdb, pStream);
×
1891
  mndTransDrop(pTrans);
×
1892

1893
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
1894
}
1895

1896
static int32_t mndProcessResumeStreamReq(SRpcMsg *pReq) {
×
1897
  SMnode     *pMnode = pReq->info.node;
×
1898
  SStreamObj *pStream = NULL;
×
1899
  int32_t     code = 0;
×
1900

1901
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
×
1902
    return code;
×
1903
  }
1904

1905
  SMResumeStreamReq resumeReq = {0};
×
1906
  if (tDeserializeSMResumeStreamReq(pReq->pCont, pReq->contLen, &resumeReq) < 0) {
×
1907
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1908
  }
1909

1910
  code = mndAcquireStream(pMnode, resumeReq.name, &pStream);
×
1911
  if (pStream == NULL || code != 0) {
×
1912
    if (resumeReq.igNotExists) {
×
1913
      mInfo("stream:%s not exist, not resume stream", resumeReq.name);
×
1914
      sdbRelease(pMnode->pSdb, pStream);
×
1915
      return 0;
×
1916
    } else {
1917
      mError("stream:%s not exist, failed to resume stream", resumeReq.name);
×
1918
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
1919
    }
1920
  }
1921

1922
  mInfo("stream:%s,%" PRId64 " start to resume stream from pause", resumeReq.name, pStream->uid);
×
1923
  if (mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->targetDb) != 0) {
×
1924
    sdbRelease(pMnode->pSdb, pStream);
×
1925
    return -1;
×
1926
  }
1927

1928
  // check if it is conflict with other trans in both sourceDb and targetDb.
1929
  code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_RESUME_NAME, true);
×
1930
  if (code) {
×
1931
    sdbRelease(pMnode->pSdb, pStream);
×
1932
    return code;
×
1933
  }
1934

1935
  STrans *pTrans = NULL;
×
1936
  code =
1937
      doCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RESUME_NAME, "resume the stream", &pTrans);
×
1938
  if (pTrans == NULL || code) {
×
1939
    mError("stream:%s, failed to resume stream since %s", resumeReq.name, tstrerror(code));
×
1940
    sdbRelease(pMnode->pSdb, pStream);
×
1941
    return code;
×
1942
  }
1943

1944
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_RESUME_NAME, pStream->uid);
×
1945
  if (code) {
×
1946
    sdbRelease(pMnode->pSdb, pStream);
×
1947
    mndTransDrop(pTrans);
×
1948
    return code;
×
1949
  }
1950

1951
  // set the resume action
1952
  code = mndStreamSetResumeAction(pTrans, pMnode, pStream, resumeReq.igUntreated);
×
1953
  if (code) {
×
1954
    mError("stream:%s, failed to drop task since %s", resumeReq.name, tstrerror(code));
×
1955
    sdbRelease(pMnode->pSdb, pStream);
×
1956
    mndTransDrop(pTrans);
×
1957
    return code;
×
1958
  }
1959

1960
  // resume stream
1961
  taosWLockLatch(&pStream->lock);
×
1962
  pStream->status = STREAM_STATUS__NORMAL;
×
1963
  if (mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY) < 0) {
×
1964
    taosWUnLockLatch(&pStream->lock);
×
1965

1966
    sdbRelease(pMnode->pSdb, pStream);
×
1967
    mndTransDrop(pTrans);
×
1968
    return code;
×
1969
  }
1970

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

1980
  sdbRelease(pMnode->pSdb, pStream);
×
1981
  mndTransDrop(pTrans);
×
1982

1983
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
1984
}
1985

1986
static int32_t mndProcessResetStreamReq(SRpcMsg *pReq) {
×
1987
  SMnode     *pMnode = pReq->info.node;
×
1988
  SStreamObj *pStream = NULL;
×
1989
  int32_t     code = 0;
×
1990

1991
  if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
×
1992
    return code;
×
1993
  }
1994

1995
  SMResetStreamReq resetReq = {0};
×
1996
  if (tDeserializeSMResetStreamReq(pReq->pCont, pReq->contLen, &resetReq) < 0) {
×
1997
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1998
  }
1999

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

2002
  code = mndAcquireStream(pMnode, resetReq.name, &pStream);
×
2003
  if (pStream == NULL || code != 0) {
×
2004
    if (resetReq.igNotExists) {
×
2005
      mInfo("stream:%s, not exist, not pause stream", resetReq.name);
×
2006
      return 0;
×
2007
    } else {
2008
      mError("stream:%s not exist, failed to pause stream", resetReq.name);
×
2009
      TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2010
    }
2011
  }
2012

2013
  //todo(liao hao jun)
2014
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
2015
}
2016

2017
static int32_t mndProcessVgroupChange(SMnode *pMnode, SVgroupChangeInfo *pChangeInfo, bool includeAllNodes, STrans** pUpdateTrans) {
×
2018
  SSdb       *pSdb = pMnode->pSdb;
×
2019
  void       *pIter = NULL;
×
2020
  STrans     *pTrans = NULL;
×
2021
  int32_t     code = 0;
×
2022
  *pUpdateTrans = NULL;
×
2023

2024
  // conflict check for nodeUpdate trans, here we randomly chose one stream to add into the trans pool
2025
  while (1) {
×
2026
    SStreamObj *pStream = NULL;
×
2027
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
2028
    if (pIter == NULL) {
×
2029
      break;
×
2030
    }
2031

2032
    code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_TASK_UPDATE_NAME, false);
×
2033
    sdbRelease(pSdb, pStream);
×
2034

2035
    if (code) {
×
2036
      mError("nodeUpdate conflict with other trans, current nodeUpdate ignored, code:%s", tstrerror(code));
×
2037
      sdbCancelFetch(pSdb, pIter);
×
2038
      return code;
×
2039
    }
2040
  }
2041

2042
  while (1) {
×
2043
    SStreamObj *pStream = NULL;
×
2044
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
2045
    if (pIter == NULL) {
×
2046
      break;
×
2047
    }
2048

2049
    // here create only one trans
2050
    if (pTrans == NULL) {
×
2051
      code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_TASK_UPDATE_NAME,
×
2052
                           "update task epsets", &pTrans);
2053
      if (pTrans == NULL || code) {
×
2054
        sdbRelease(pSdb, pStream);
×
2055
        sdbCancelFetch(pSdb, pIter);
×
2056
        return terrno = code;
×
2057
      }
2058
    }
2059

2060
    if (!includeAllNodes) {
×
2061
      void *p1 = taosHashGet(pChangeInfo->pDBMap, pStream->targetDb, strlen(pStream->targetDb));
×
2062
      void *p2 = taosHashGet(pChangeInfo->pDBMap, pStream->sourceDb, strlen(pStream->sourceDb));
×
2063
      if (p1 == NULL && p2 == NULL) {
×
2064
        mDebug("stream:0x%" PRIx64 " %s not involved in nodeUpdate, ignore", pStream->uid, pStream->name);
×
2065
        sdbRelease(pSdb, pStream);
×
2066
        continue;
×
2067
      }
2068
    }
2069

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

2073
    // NOTE: for each stream, we register one trans entry for task update
2074
    code = mndStreamRegisterTrans(pTrans, MND_STREAM_TASK_UPDATE_NAME, pStream->uid);
×
2075
    if (code) {
×
2076
      mError("failed to register trans, transId:%d, and continue", pTrans->id);
×
2077
    }
2078

2079
    code = mndStreamSetUpdateEpsetAction(pMnode, pStream, pChangeInfo, pTrans);
×
2080

2081
    // todo: not continue, drop all and retry again
2082
    if (code != TSDB_CODE_SUCCESS) {
×
2083
      mError("stream:0x%" PRIx64 " build nodeUpdate trans failed, ignore and continue, code:%s", pStream->uid,
×
2084
             tstrerror(code));
2085
      sdbRelease(pSdb, pStream);
×
2086
      continue;
×
2087
    }
2088

2089
    code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
×
2090
    sdbRelease(pSdb, pStream);
×
2091

2092
    if (code != TSDB_CODE_SUCCESS) {
×
2093
      sdbCancelFetch(pSdb, pIter);
×
2094
      return code;
×
2095
    }
2096
  }
2097

2098
  // no need to build the trans to handle the vgroup update
2099
  *pUpdateTrans = pTrans;
×
2100
  return code;
×
2101
}
2102

2103
static int32_t refreshNodeListFromExistedStreams(SMnode *pMnode, SArray *pNodeList) {
×
2104
  SSdb       *pSdb = pMnode->pSdb;
×
2105
  SStreamObj *pStream = NULL;
×
2106
  void       *pIter = NULL;
×
2107
  int32_t     code = 0;
×
2108

2109
  mDebug("start to refresh node list by existed streams");
×
2110

2111
  SHashObj *pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
×
2112
  if (pHash == NULL) {
×
2113
    return terrno;
×
2114
  }
2115

2116
  while (1) {
×
2117
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
2118
    if (pIter == NULL) {
×
2119
      break;
×
2120
    }
2121

2122
    taosWLockLatch(&pStream->lock);
×
2123

2124
    SStreamTaskIter *pTaskIter = NULL;
×
2125
    code = createStreamTaskIter(pStream, &pTaskIter);
×
2126
    if (code) {
×
2127
      taosWUnLockLatch(&pStream->lock);
×
2128
      sdbRelease(pSdb, pStream);
×
2129
      mError("failed to create task iter for stream:%s", pStream->name);
×
2130
      continue;
×
2131
    }
2132

2133
    while (streamTaskIterNextTask(pTaskIter)) {
×
2134
      SStreamTask *pTask = NULL;
×
2135
      code = streamTaskIterGetCurrent(pTaskIter, &pTask);
×
2136
      if (code) {
×
2137
        break;
×
2138
      }
2139

2140
      SNodeEntry entry = {.hbTimestamp = -1, .nodeId = pTask->info.nodeId, .lastHbMsgId = -1};
×
2141
      epsetAssign(&entry.epset, &pTask->info.epSet);
×
2142
      int32_t ret = taosHashPut(pHash, &entry.nodeId, sizeof(entry.nodeId), &entry, sizeof(entry));
×
2143
      if (ret != 0 && ret != TSDB_CODE_DUP_KEY) {
×
2144
        mError("failed to put entry into hash map, nodeId:%d, code:%s", entry.nodeId, tstrerror(code));
×
2145
      }
2146
    }
2147

2148
    destroyStreamTaskIter(pTaskIter);
×
2149
    taosWUnLockLatch(&pStream->lock);
×
2150

2151
    sdbRelease(pSdb, pStream);
×
2152
  }
2153

2154
  taosArrayClear(pNodeList);
×
2155

2156
  // convert to list
2157
  pIter = NULL;
×
2158
  while ((pIter = taosHashIterate(pHash, pIter)) != NULL) {
×
2159
    SNodeEntry *pEntry = (SNodeEntry *)pIter;
×
2160

2161
    void *p = taosArrayPush(pNodeList, pEntry);
×
2162
    if (p == NULL) {
×
2163
      mError("failed to put entry into node list, nodeId:%d, code: out of memory", pEntry->nodeId);
×
2164
      if (code == 0) {
×
2165
        code = terrno;
×
2166
      }
2167
      continue;
×
2168
    }
2169

2170
    char    buf[256] = {0};
×
2171
    int32_t ret = epsetToStr(&pEntry->epset, buf, tListLen(buf));  // ignore this error since it is only for log file
×
2172
    if (ret != 0) {                                                // print error and continue
×
2173
      mError("failed to convert epset to str, code:%s", tstrerror(ret));
×
2174
    }
2175

2176
    mDebug("extract nodeInfo from stream obj, nodeId:%d, %s", pEntry->nodeId, buf);
×
2177
  }
2178

2179
  taosHashCleanup(pHash);
×
2180

2181
  mDebug("numOfvNodes:%d get after extracting nodeInfo from all streams", (int32_t)taosArrayGetSize(pNodeList));
×
2182
  return code;
×
2183
}
2184

2185
static void addAllDbsIntoHashmap(SHashObj *pDBMap, SSdb *pSdb) {
×
2186
  void   *pIter = NULL;
×
2187
  int32_t code = 0;
×
2188
  while (1) {
×
2189
    SVgObj *pVgroup = NULL;
×
2190
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
×
2191
    if (pIter == NULL) {
×
2192
      break;
×
2193
    }
2194

2195
    code = taosHashPut(pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
×
2196
    sdbRelease(pSdb, pVgroup);
×
2197

2198
    if (code == 0) {
×
2199
      int32_t size = taosHashGetSize(pDBMap);
×
2200
      mDebug("add Db:%s into Dbs list (total:%d) for kill checkpoint trans", pVgroup->dbName, size);
×
2201
    }
2202
  }
2203
}
×
2204

2205
static int32_t doProcessNodeCheckHelp(SArray *pNodeSnapshot, SMnode *pMnode, SVgroupChangeInfo *pChangeInfo,
×
2206
                                      bool *pUpdateAllVgroups) {
2207
  int32_t code = removeExpiredNodeEntryAndTaskInBuf(pNodeSnapshot);
×
2208
  if (code) {
×
2209
    mDebug("failed to remove expired node entry in buf, code:%s", tstrerror(code));
×
2210
    return code;
×
2211
  }
2212

2213
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, pNodeSnapshot, pChangeInfo);
×
2214
  if (code) {
×
2215
    mDebug("failed to find changed vnode(s) during vnode(s) check, code:%s", tstrerror(code));
×
2216
    return code;
×
2217
  }
2218

2219
  {
2220
    if (execInfo.role == NODE_ROLE_LEADER && execInfo.switchFromFollower) {
×
2221
      mInfo("rollback all stream due to mnode leader/follower switch by using nodeUpdate trans");
×
2222
      *pUpdateAllVgroups = true;
×
2223
      execInfo.switchFromFollower = false;  // reset the flag
×
2224
      addAllDbsIntoHashmap(pChangeInfo->pDBMap, pMnode->pSdb);
×
2225
    }
2226
  }
2227

2228
  if (taosArrayGetSize(pChangeInfo->pUpdateNodeList) > 0 || (*pUpdateAllVgroups)) {
×
2229
    // kill current active checkpoint transaction, since the transaction is vnode wide.
2230
    killAllCheckpointTrans(pMnode, pChangeInfo);
×
2231
  } else {
2232
    mDebug("no update found in vnode(s) list");
×
2233
  }
2234

2235
  return code;
×
2236
}
2237

2238
// this function runs by only one thread, so it is not multi-thread safe
2239
static int32_t mndProcessNodeCheckReq(SRpcMsg *pMsg) {
×
2240
  int32_t           code = 0;
×
2241
  bool              allReady = true;
×
2242
  SArray           *pNodeSnapshot = NULL;
×
2243
  SMnode           *pMnode = pMsg->info.node;
×
2244
  int64_t           tsms = taosGetTimestampMs();
×
2245
  int64_t           ts = tsms / 1000;
×
2246
  bool              updateAllVgroups = false;
×
2247
  SVgroupChangeInfo changeInfo = {0};
×
2248

2249
  int32_t old = atomic_val_compare_exchange_32(&mndNodeCheckSentinel, 0, 1);
×
2250
  if (old != 0) {
×
2251
    mDebug("still in checking node change");
×
2252
    return 0;
×
2253
  }
2254

2255
  mDebug("start to do node changing check, ts:%" PRId64, tsms);
×
2256

2257
  streamMutexLock(&execInfo.lock);
×
2258
  int32_t numOfNodes = extractStreamNodeList(pMnode);
×
2259
  streamMutexUnlock(&execInfo.lock);
×
2260

2261
  if (numOfNodes == 0) {
×
2262
    mDebug("end to do stream task(s) node change checking, no stream tasks exist, do nothing");
×
2263
    execInfo.ts = ts;
×
2264
    atomic_store_32(&mndNodeCheckSentinel, 0);
×
2265
    return 0;
×
2266
  }
2267

2268
  code = mndTakeVgroupSnapshot(pMnode, &allReady, &pNodeSnapshot);
×
2269
  if (code) {
×
2270
    mError("failed to take the vgroup snapshot, ignore it and continue");
×
2271
  }
2272

2273
  if (!allReady) {
×
2274
    taosArrayDestroy(pNodeSnapshot);
×
2275
    atomic_store_32(&mndNodeCheckSentinel, 0);
×
2276
    mWarn("not all vnodes are ready, ignore the exec nodeUpdate check");
×
2277
    return 0;
×
2278
  }
2279

2280
  streamMutexLock(&execInfo.lock);
×
2281
  code = doProcessNodeCheckHelp(pNodeSnapshot, pMnode, &changeInfo, &updateAllVgroups);
×
2282
  streamMutexUnlock(&execInfo.lock);
×
2283

2284
  if (code) {
×
2285
    goto _end;
×
2286
  }
2287

2288
  if (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0 || updateAllVgroups) {
×
2289
    mDebug("vnode(s) change detected, build trans to update stream task epsets");
×
2290

2291
    STrans *pTrans = NULL;
×
2292

2293
    streamMutexLock(&execInfo.lock);
×
2294
    code = mndProcessVgroupChange(pMnode, &changeInfo, updateAllVgroups, &pTrans);
×
2295
    streamMutexUnlock(&execInfo.lock);
×
2296

2297
    // NOTE: sync trans out of lock
2298
    if (code == 0 && pTrans != NULL) {
×
2299
      code = mndTransPrepare(pMnode, pTrans);
×
2300
      if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
2301
        mError("trans:%d, failed to prepare update stream trans since %s", pTrans->id, tstrerror(code));
×
2302
      }
2303

2304
      mndTransDrop(pTrans);
×
2305
    }
2306

2307
    // keep the new vnode snapshot if success
2308
    if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {
×
2309
      streamMutexLock(&execInfo.lock);
×
2310

2311
      code = refreshNodeListFromExistedStreams(pMnode, execInfo.pNodeList);
×
2312
      int32_t num = (int)taosArrayGetSize(execInfo.pNodeList);
×
2313
      if (code == 0) {
×
2314
        execInfo.ts = ts;
×
2315
        mDebug("create trans successfully, update cached node list, numOfNodes:%d", num);
×
2316
      }
2317

2318
      streamMutexUnlock(&execInfo.lock);
×
2319

2320
      if (code) {
×
2321
        mError("failed to extract node list from stream, code:%s", tstrerror(code));
×
2322
        goto _end;
×
2323
      }
2324
    }
2325
  }
2326

2327
  mndDestroyVgroupChangeInfo(&changeInfo);
×
2328

2329
_end:
×
2330
  taosArrayDestroy(pNodeSnapshot);
×
2331

2332
  mDebug("end to do stream task node change checking, elapsed time:%" PRId64 "ms", taosGetTimestampMs() - tsms);
×
2333
  atomic_store_32(&mndNodeCheckSentinel, 0);
×
2334

2335
  return 0;
×
2336
}
2337

2338
static int32_t mndProcessNodeCheck(SRpcMsg *pReq) {
×
2339
  SMnode *pMnode = pReq->info.node;
×
2340
  SSdb   *pSdb = pMnode->pSdb;
×
2341
  if (sdbGetSize(pSdb, SDB_STREAM) <= 0) {
×
2342
    return 0;
×
2343
  }
2344

2345
  int32_t               size = sizeof(SMStreamNodeCheckMsg);
×
2346
  SMStreamNodeCheckMsg *pMsg = rpcMallocCont(size);
×
2347
  if (pMsg == NULL) {
×
2348
    return terrno;
×
2349
  }
2350

2351
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_STREAM_NODECHANGE_CHECK, .pCont = pMsg, .contLen = size};
×
2352
  return tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg);
×
2353
}
2354

2355
void saveTaskAndNodeInfoIntoBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
×
2356
  SStreamTaskIter *pIter = NULL;
×
2357
  int32_t          code = createStreamTaskIter(pStream, &pIter);
×
2358
  if (code) {
×
2359
    mError("failed to create task iter for stream:%s", pStream->name);
×
2360
    return;
×
2361
  }
2362

2363
  while (streamTaskIterNextTask(pIter)) {
×
2364
    SStreamTask *pTask = NULL;
×
2365
    code = streamTaskIterGetCurrent(pIter, &pTask);
×
2366
    if (code) {
×
2367
      break;
×
2368
    }
2369

2370
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
2371
    void   *p = taosHashGet(pExecNode->pTaskMap, &id, sizeof(id));
×
2372
    if (p == NULL) {
×
2373
      STaskStatusEntry entry = {0};
×
2374
      streamTaskStatusInit(&entry, pTask);
×
2375

2376
      code = taosHashPut(pExecNode->pTaskMap, &id, sizeof(id), &entry, sizeof(entry));
×
2377
      if (code == 0) {
×
2378
        void   *px = taosArrayPush(pExecNode->pTaskList, &id);
×
2379
        int32_t num = (int32_t)taosArrayGetSize(pExecNode->pTaskList);
×
2380
        if (px) {
×
2381
          mInfo("s-task:0x%x add into task buffer, total:%d", (int32_t)entry.id.taskId, num);
×
2382
        } else {
2383
          mError("s-task:0x%x failed to add into task buffer, total:%d", (int32_t)entry.id.taskId, num);
×
2384
        }
2385
      } else {
2386
        mError("s-task:0x%x failed to add into task map, since out of memory", (int32_t)entry.id.taskId);
×
2387
      }
2388

2389
      // add the new vgroups if not added yet
2390
      bool exist = false;
×
2391
      for (int32_t j = 0; j < taosArrayGetSize(pExecNode->pNodeList); ++j) {
×
2392
        SNodeEntry *pEntry = taosArrayGet(pExecNode->pNodeList, j);
×
2393
        if ((pEntry != NULL) && (pEntry->nodeId == pTask->info.nodeId)) {
×
2394
          exist = true;
×
2395
          break;
×
2396
        }
2397
      }
2398

2399
      if (!exist) {
×
2400
        SNodeEntry nodeEntry = {.hbTimestamp = -1, .nodeId = pTask->info.nodeId, .lastHbMsgId = -1};
×
2401
        epsetAssign(&nodeEntry.epset, &pTask->info.epSet);
×
2402

2403
        void *px = taosArrayPush(pExecNode->pNodeList, &nodeEntry);
×
2404
        if (px) {
×
2405
          mInfo("vgId:%d added into nodeList, total:%d", nodeEntry.nodeId, (int)taosArrayGetSize(pExecNode->pNodeList));
×
2406
        } else {
2407
          mError("vgId:%d failed to add into nodeList, total:%d", nodeEntry.nodeId,
×
2408
                 (int)taosArrayGetSize(pExecNode->pNodeList))
2409
        }
2410
      }
2411
    }
2412
  }
2413

2414
  destroyStreamTaskIter(pIter);
×
2415
}
2416

2417
static void doAddTaskId(SArray *pList, int32_t taskId, int64_t uid, int32_t numOfTotal) {
×
2418
  int32_t num = taosArrayGetSize(pList);
×
2419
  for (int32_t i = 0; i < num; ++i) {
×
2420
    int32_t *pId = taosArrayGet(pList, i);
×
2421
    if (pId == NULL) {
×
2422
      continue;
×
2423
    }
2424

2425
    if (taskId == *pId) {
×
2426
      return;
×
2427
    }
2428
  }
2429

2430
  int32_t numOfTasks = taosArrayGetSize(pList);
×
2431
  void   *p = taosArrayPush(pList, &taskId);
×
2432
  if (p) {
×
2433
    mDebug("stream:0x%" PRIx64 " receive %d reqs for checkpoint, remain:%d", uid, numOfTasks, numOfTotal - numOfTasks);
×
2434
  } else {
2435
    mError("stream:0x%" PRIx64 " receive %d reqs for checkpoint, failed to added into task list, since out of memory",
×
2436
           uid, numOfTasks);
2437
  }
2438
}
2439

2440
int32_t mndProcessStreamReqCheckpoint(SRpcMsg *pReq) {
×
2441
  SMnode                  *pMnode = pReq->info.node;
×
2442
  SStreamTaskCheckpointReq req = {0};
×
2443

2444
  SDecoder decoder = {0};
×
2445
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
×
2446

2447
  if (tDecodeStreamTaskCheckpointReq(&decoder, &req)) {
×
2448
    tDecoderClear(&decoder);
×
2449
    mError("invalid task checkpoint req msg received");
×
2450
    return TSDB_CODE_INVALID_MSG;
×
2451
  }
2452
  tDecoderClear(&decoder);
×
2453

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

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

2459
  SStreamObj *pStream = NULL;
×
2460
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
×
2461
  if (pStream == NULL || code != 0) {
×
2462
    mWarn("failed to find the stream:0x%" PRIx64 ", not handle the checkpoint req, try to acquire in buf",
×
2463
          req.streamId);
2464

2465
    // not in meta-store yet, try to acquire the task in exec buffer
2466
    // the checkpoint req arrives too soon before the completion of the create stream trans.
2467
    STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
×
2468
    void   *p = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
2469
    if (p == NULL) {
×
2470
      mError("failed to find the stream:0x%" PRIx64 " in buf, not handle the checkpoint req", req.streamId);
×
2471
      streamMutexUnlock(&execInfo.lock);
×
2472
      return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
2473
    } else {
2474
      mDebug("s-task:0x%" PRIx64 "-0x%x in buf not in mnode/meta, create stream trans may not complete yet",
×
2475
             req.streamId, req.taskId);
2476
    }
2477
  }
2478

2479
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
×
2480

2481
  SArray **pReqTaskList = (SArray **)taosHashGet(execInfo.pTransferStateStreams, &req.streamId, sizeof(req.streamId));
×
2482
  if (pReqTaskList == NULL) {
×
2483
    SArray *pList = taosArrayInit(4, sizeof(int32_t));
×
2484
    doAddTaskId(pList, req.taskId, req.streamId, numOfTasks);
×
2485
    code = taosHashPut(execInfo.pTransferStateStreams, &req.streamId, sizeof(int64_t), &pList, sizeof(void *));
×
2486
    if (code) {
×
2487
      mError("failed to put into transfer state stream map, code: out of memory");
×
2488
    }
2489
    pReqTaskList = (SArray **)taosHashGet(execInfo.pTransferStateStreams, &req.streamId, sizeof(req.streamId));
×
2490
  } else {
2491
    doAddTaskId(*pReqTaskList, req.taskId, req.streamId, numOfTasks);
×
2492
  }
2493

2494
  int32_t total = taosArrayGetSize(*pReqTaskList);
×
2495
  if (total == numOfTasks) {  // all tasks have sent the reqs
×
2496
    int64_t checkpointId = mndStreamGenChkptId(pMnode, false);
×
2497
    mInfo("stream:0x%" PRIx64 " all tasks req checkpoint, start checkpointId:%" PRId64, req.streamId, checkpointId);
×
2498

2499
    if (pStream != NULL) {  // TODO:handle error
×
2500
      code = mndProcessStreamCheckpointTrans(pMnode, pStream, checkpointId, 0, false);
×
2501
      if (code) {
×
2502
        mError("failed to create checkpoint trans, code:%s", tstrerror(code));
×
2503
      }
2504
    } else {
2505
      // todo: wait for the create stream trans completed, and launch the checkpoint trans
2506
      // SStreamObj *pStream = mndGetStreamObj(pMnode, req.streamId);
2507
      // sleep(500ms)
2508
    }
2509

2510
    // remove this entry
2511
    (void) taosHashRemove(execInfo.pTransferStateStreams, &req.streamId, sizeof(int64_t));
×
2512

2513
    int32_t numOfStreams = taosHashGetSize(execInfo.pTransferStateStreams);
×
2514
    mDebug("stream:0x%" PRIx64 " removed, remain streams:%d fill-history not completed", req.streamId, numOfStreams);
×
2515
  }
2516

2517
  if (pStream != NULL) {
×
2518
    mndReleaseStream(pMnode, pStream);
×
2519
  }
2520

2521
  streamMutexUnlock(&execInfo.lock);
×
2522

2523
  {
2524
    SRpcMsg rsp = {.code = 0, .info = pReq->info, .contLen = sizeof(SMStreamReqCheckpointRsp)};
×
2525
    rsp.pCont = rpcMallocCont(rsp.contLen);
×
2526
    if (rsp.pCont == NULL) {
×
2527
      return terrno;
×
2528
    }
2529

2530
    SMsgHead *pHead = rsp.pCont;
×
2531
    pHead->vgId = htonl(req.nodeId);
×
2532

2533
    tmsgSendRsp(&rsp);
×
2534
    pReq->info.handle = NULL;  // disable auto rsp
×
2535
  }
2536

2537
  return 0;
×
2538
}
2539

2540
// valid the info according to the HbMsg
2541
static bool validateChkptReport(const SCheckpointReport *pReport, int64_t reportChkptId) {
×
2542
  STaskId           id = {.streamId = pReport->streamId, .taskId = pReport->taskId};
×
2543
  STaskStatusEntry *pTaskEntry = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
2544
  if (pTaskEntry == NULL) {
×
2545
    mError("invalid checkpoint-report msg from task:0x%x, discard", pReport->taskId);
×
2546
    return false;
×
2547
  }
2548

2549
  if (pTaskEntry->checkpointInfo.latestId >= pReport->checkpointId) {
×
2550
    mError("s-task:0x%x invalid checkpoint-report msg, checkpointId:%" PRId64 " saved checkpointId:%" PRId64 " discard",
×
2551
           pReport->taskId, pReport->checkpointId, pTaskEntry->checkpointInfo.activeId);
2552
    return false;
×
2553
  }
2554

2555
  // now the task in checkpoint procedure
2556
  if ((pTaskEntry->checkpointInfo.activeId != 0) && (pTaskEntry->checkpointInfo.activeId > pReport->checkpointId)) {
×
2557
    mError("s-task:0x%x invalid checkpoint-report msg, checkpointId:%" PRId64 " active checkpointId:%" PRId64
×
2558
           " discard",
2559
           pReport->taskId, pReport->checkpointId, pTaskEntry->checkpointInfo.activeId);
2560
    return false;
×
2561
  }
2562

2563
  if (reportChkptId >= pReport->checkpointId) {
×
2564
    mError("s-task:0x%x expired checkpoint-report msg, checkpointId:%" PRId64 " already update checkpointId:%" PRId64
×
2565
           " discard",
2566
           pReport->taskId, pReport->checkpointId, reportChkptId);
2567
    return false;
×
2568
  }
2569

2570
  return true;
×
2571
}
2572

2573
static void doAddReportStreamTask(SArray *pList, int64_t reportedChkptId, const SCheckpointReport *pReport) {
×
2574
  bool valid = validateChkptReport(pReport, reportedChkptId);
×
2575
  if (!valid) {
×
2576
    return;
×
2577
  }
2578

2579
  for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
×
2580
    STaskChkptInfo *p = taosArrayGet(pList, i);
×
2581
    if (p == NULL) {
×
2582
      continue;
×
2583
    }
2584

2585
    if (p->taskId == pReport->taskId) {
×
2586
      if (p->checkpointId > pReport->checkpointId) {
×
2587
        mError("s-task:0x%x invalid checkpoint-report msg, existed:%" PRId64 " req checkpointId:%" PRId64 ", discard",
×
2588
               pReport->taskId, p->checkpointId, pReport->checkpointId);
2589
      } else if (p->checkpointId < pReport->checkpointId) {  // expired checkpoint-report msg, update it
×
2590
        mInfo("s-task:0x%x expired checkpoint-report info in checkpoint-report list update from %" PRId64 "->%" PRId64,
×
2591
               pReport->taskId, p->checkpointId, pReport->checkpointId);
2592

2593
        // update the checkpoint report info
2594
        p->checkpointId = pReport->checkpointId;
×
2595
        p->ts = pReport->checkpointTs;
×
2596
        p->version = pReport->checkpointVer;
×
2597
        p->transId = pReport->transId;
×
2598
        p->dropHTask = pReport->dropHTask;
×
2599
      } else {
2600
        mWarn("taskId:0x%x already in checkpoint-report list", pReport->taskId);
×
2601
      }
2602
      return;
×
2603
    }
2604
  }
2605

2606
  STaskChkptInfo info = {
×
2607
      .streamId = pReport->streamId,
×
2608
      .taskId = pReport->taskId,
×
2609
      .transId = pReport->transId,
×
2610
      .dropHTask = pReport->dropHTask,
×
2611
      .version = pReport->checkpointVer,
×
2612
      .ts = pReport->checkpointTs,
×
2613
      .checkpointId = pReport->checkpointId,
×
2614
      .nodeId = pReport->nodeId,
×
2615
  };
2616

2617
  void *p = taosArrayPush(pList, &info);
×
2618
  if (p == NULL) {
×
2619
    mError("failed to put into task list, taskId:0x%x", pReport->taskId);
×
2620
  } else {
2621
    int32_t size = taosArrayGetSize(pList);
×
2622
    mDebug("stream:0x%" PRIx64 " taskId:0x%x checkpoint-report recv, %d tasks has send checkpoint-report",
×
2623
           pReport->streamId, pReport->taskId, size);
2624
  }
2625
}
2626

2627
int32_t mndProcessCheckpointReport(SRpcMsg *pReq) {
×
2628
  SMnode           *pMnode = pReq->info.node;
×
2629
  SCheckpointReport req = {0};
×
2630

2631
  SDecoder decoder = {0};
×
2632
  tDecoderInit(&decoder, pReq->pCont, pReq->contLen);
×
2633

2634
  if (tDecodeStreamTaskChkptReport(&decoder, &req)) {
×
2635
    tDecoderClear(&decoder);
×
2636
    mError("invalid task checkpoint-report msg received");
×
2637
    return TSDB_CODE_INVALID_MSG;
×
2638
  }
2639
  tDecoderClear(&decoder);
×
2640

2641
  streamMutexLock(&execInfo.lock);
×
2642
  mndInitStreamExecInfo(pMnode, &execInfo);
×
2643
  streamMutexUnlock(&execInfo.lock);
×
2644

2645
  mDebug("receive stream task checkpoint-report msg, vgId:%d, s-task:0x%x, checkpointId:%" PRId64
×
2646
         " checkpointVer:%" PRId64 " transId:%d",
2647
         req.nodeId, req.taskId, req.checkpointId, req.checkpointVer, req.transId);
2648

2649
  // register to the stream task done map, if all tasks has sent these kinds of message, start the checkpoint trans.
2650
  streamMutexLock(&execInfo.lock);
×
2651

2652
  SStreamObj *pStream = NULL;
×
2653
  int32_t     code = mndGetStreamObj(pMnode, req.streamId, &pStream);
×
2654
  if (pStream == NULL || code != 0) {
×
2655
    mWarn("failed to find the stream:0x%" PRIx64 ", not handle checkpoint-report, try to acquire in buf", req.streamId);
×
2656

2657
    // not in meta-store yet, try to acquire the task in exec buffer
2658
    // the checkpoint req arrives too soon before the completion of the creation of stream trans.
2659
    STaskId id = {.streamId = req.streamId, .taskId = req.taskId};
×
2660
    void   *p = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
2661
    if (p == NULL) {
×
2662
      mError("failed to find the stream:0x%" PRIx64 " in buf, not handle the checkpoint-report", req.streamId);
×
2663
      streamMutexUnlock(&execInfo.lock);
×
2664
      return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
2665
    } else {
2666
      mDebug("s-task:0x%" PRIx64 "-0x%x in buf not in mnode/meta, create stream trans may not complete yet",
×
2667
             req.streamId, req.taskId);
2668
    }
2669
  }
2670

2671
  int32_t numOfTasks = (pStream == NULL) ? 0 : mndGetNumOfStreamTasks(pStream);
×
2672

2673
  SChkptReportInfo *pInfo =
2674
      (SChkptReportInfo *)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId));
×
2675
  if (pInfo == NULL) {
×
2676
    SChkptReportInfo info = {.pTaskList = taosArrayInit(4, sizeof(STaskChkptInfo)), .streamId = req.streamId};
×
2677
    if (info.pTaskList != NULL) {
×
2678
      doAddReportStreamTask(info.pTaskList, info.reportChkpt, &req);
×
2679
      code = taosHashPut(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId), &info, sizeof(info));
×
2680
      if (code) {
×
2681
        mError("stream:0x%" PRIx64 " failed to put into checkpoint stream", req.streamId);
×
2682
      }
2683

2684
      pInfo = (SChkptReportInfo *)taosHashGet(execInfo.pChkptStreams, &req.streamId, sizeof(req.streamId));
×
2685
    }
2686
  } else {
2687
    doAddReportStreamTask(pInfo->pTaskList, pInfo->reportChkpt, &req);
×
2688
  }
2689

2690
  int32_t total = taosArrayGetSize(pInfo->pTaskList);
×
2691
  if (total == numOfTasks) {  // all tasks have sent the reqs
×
2692
    mInfo("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, checkpoint meta-info for checkpointId:%" PRId64
×
2693
          " will be issued soon",
2694
          req.streamId, pStream->name, total, req.checkpointId);
2695
  }
2696

2697
  if (pStream != NULL) {
×
2698
    mndReleaseStream(pMnode, pStream);
×
2699
  }
2700

2701
  streamMutexUnlock(&execInfo.lock);
×
2702

2703
  doSendQuickRsp(&pReq->info, sizeof(SMStreamUpdateChkptRsp), req.nodeId, TSDB_CODE_SUCCESS);
×
2704
  return code;
×
2705
}
2706

2707
static int64_t getConsensusId(int64_t streamId, int32_t numOfTasks, int32_t *pExistedTasks, bool *pAllSame) {
×
2708
  int32_t num = 0;
×
2709
  int64_t chkId = INT64_MAX;
×
2710
  *pExistedTasks = 0;
×
2711
  *pAllSame = true;
×
2712

2713
  for (int32_t i = 0; i < taosArrayGetSize(execInfo.pTaskList); ++i) {
×
2714
    STaskId *p = taosArrayGet(execInfo.pTaskList, i);
×
2715
    if (p == NULL) {
×
2716
      continue;
×
2717
    }
2718

2719
    if (p->streamId != streamId) {
×
2720
      continue;
×
2721
    }
2722

2723
    num += 1;
×
2724
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, p, sizeof(*p));
×
2725
    if (chkId > pe->checkpointInfo.latestId) {
×
2726
      if (chkId != INT64_MAX) {
×
2727
        *pAllSame = false;
×
2728
      }
2729
      chkId = pe->checkpointInfo.latestId;
×
2730
    }
2731
  }
2732

2733
  *pExistedTasks = num;
×
2734
  if (num < numOfTasks) {  // not all task send info to mnode through hbMsg, no valid checkpoint Id
×
2735
    return -1;
×
2736
  }
2737

2738
  return chkId;
×
2739
}
2740

2741
static void doSendQuickRsp(SRpcHandleInfo *pInfo, int32_t msgSize, int32_t vgId, int32_t code) {
×
2742
  SRpcMsg rsp = {.code = code, .info = *pInfo, .contLen = msgSize};
×
2743
  rsp.pCont = rpcMallocCont(rsp.contLen);
×
2744
  if (rsp.pCont != NULL) {
×
2745
    SMsgHead *pHead = rsp.pCont;
×
2746
    pHead->vgId = htonl(vgId);
×
2747

2748
    tmsgSendRsp(&rsp);
×
2749
    pInfo->handle = NULL;  // disable auto rsp
×
2750
  }
2751
}
×
2752

2753
static int32_t doCleanReqList(SArray* pList, SCheckpointConsensusInfo* pInfo) {
×
2754
  int32_t alreadySend = taosArrayGetSize(pList);
×
2755

2756
  for (int32_t i = 0; i < alreadySend; ++i) {
×
2757
    int32_t *taskId = taosArrayGet(pList, i);
×
2758
    if (taskId == NULL) {
×
2759
      continue;
×
2760
    }
2761

2762
    for (int32_t k = 0; k < taosArrayGetSize(pInfo->pTaskList); ++k) {
×
2763
      SCheckpointConsensusEntry *pe = taosArrayGet(pInfo->pTaskList, k);
×
2764
      if ((pe != NULL) && (pe->req.taskId == *taskId)) {
×
2765
        taosArrayRemove(pInfo->pTaskList, k);
×
2766
        break;
×
2767
      }
2768
    }
2769
  }
2770

2771
  return alreadySend;
×
2772
}
2773

2774
int32_t mndProcessConsensusInTmr(SRpcMsg *pMsg) {
×
2775
  SMnode *pMnode = pMsg->info.node;
×
2776
  int64_t now = taosGetTimestampMs();
×
2777
  bool    allReady = true;
×
2778
  SArray *pNodeSnapshot = NULL;
×
2779
  int32_t maxAllowedTrans = 20;
×
2780
  int32_t numOfTrans = 0;
×
2781
  int32_t code = 0;
×
2782
  void   *pIter = NULL;
×
2783

2784
  SArray *pList = taosArrayInit(4, sizeof(int32_t));
×
2785
  if (pList == NULL) {
×
2786
    return terrno;
×
2787
  }
2788

2789
  SArray *pStreamList = taosArrayInit(4, sizeof(int64_t));
×
2790
  if (pStreamList == NULL) {
×
2791
    taosArrayDestroy(pList);
×
2792
    return terrno;
×
2793
  }
2794

2795
  mDebug("start to process consensus-checkpointId in tmr");
×
2796

2797
  code = mndTakeVgroupSnapshot(pMnode, &allReady, &pNodeSnapshot);
×
2798
  taosArrayDestroy(pNodeSnapshot);
×
2799
  if (code) {
×
2800
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
2801
  }
2802

2803
  if (!allReady) {
×
2804
    mWarn("not all vnodes are ready, end to process the consensus-checkpointId in tmr process");
×
2805
    taosArrayDestroy(pStreamList);
×
2806
    taosArrayDestroy(pList);
×
2807
    return 0;
×
2808
  }
2809

2810
  streamMutexLock(&execInfo.lock);
×
2811

2812
  while ((pIter = taosHashIterate(execInfo.pStreamConsensus, pIter)) != NULL) {
×
2813
    SCheckpointConsensusInfo *pInfo = (SCheckpointConsensusInfo *)pIter;
×
2814

2815
    taosArrayClear(pList);
×
2816

2817
    int64_t     streamId = -1;
×
2818
    int32_t     num = taosArrayGetSize(pInfo->pTaskList);
×
2819
    SStreamObj *pStream = NULL;
×
2820

2821
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
×
2822
    if (pStream == NULL || code != 0) {  // stream has been dropped already
×
2823
      mDebug("stream:0x%" PRIx64 " dropped already, continue", pInfo->streamId);
×
2824
      void *p = taosArrayPush(pStreamList, &pInfo->streamId);
×
2825
      if (p == NULL) {
×
2826
        mError("failed to record the missing stream id in concensus-stream list, streamId:%" PRId64
×
2827
               " code:%s, continue",
2828
               pInfo->streamId, tstrerror(terrno));
2829
      }
2830
      continue;
×
2831
    }
2832

2833
    for (int32_t j = 0; j < num; ++j) {
×
2834
      SCheckpointConsensusEntry *pe = taosArrayGet(pInfo->pTaskList, j);
×
2835
      if (pe == NULL) {
×
2836
        continue;
×
2837
      }
2838

2839
      if (streamId == -1) {
×
2840
        streamId = pe->req.streamId;
×
2841
      }
2842

2843
      int32_t existed = 0;
×
2844
      bool    allSame = true;
×
2845
      int64_t chkId = getConsensusId(pe->req.streamId, pInfo->numOfTasks, &existed, &allSame);
×
2846
      if (chkId == -1) {
×
2847
        mDebug("not all(%d/%d) task(s) send hbMsg yet, wait for a while and check again, s-task:0x%x", existed,
×
2848
               pInfo->numOfTasks, pe->req.taskId);
2849
        break;
×
2850
      }
2851

2852
      if (((now - pe->ts) >= 10 * 1000) || allSame) {
×
2853
        mDebug("s-task:0x%x sendTs:%" PRId64 " wait %.2fs and all tasks have same checkpointId", pe->req.taskId,
×
2854
               pe->req.startTs, (now - pe->ts) / 1000.0);
2855
        if (chkId > pe->req.checkpointId) {
×
2856
          streamMutexUnlock(&execInfo.lock);
×
2857
          taosArrayDestroy(pStreamList);
×
2858
          mError("s-task:0x%x checkpointId:%" PRId64 " is updated to %" PRId64 ", update it", pe->req.taskId,
×
2859
                 pe->req.checkpointId, chkId);
2860

2861
          mndReleaseStream(pMnode, pStream);
×
2862
          taosHashCancelIterate(execInfo.pStreamConsensus, pIter);
×
2863
          return TSDB_CODE_FAILED;
×
2864
        }
2865

2866
        // todo: check for redundant consensus-checkpoint trans, if this kinds of trans repeatly failed.
2867
        code = mndCreateSetConsensusChkptIdTrans(pMnode, pStream, pe->req.taskId, chkId, pe->req.startTs);
×
2868
        if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
2869
          mError("failed to create consensus-checkpoint trans, stream:0x%" PRIx64, pStream->uid);
×
2870
        }
2871

2872
        void *p = taosArrayPush(pList, &pe->req.taskId);
×
2873
        if (p == NULL) {
×
2874
          mError("failed to put into task list, taskId:0x%x", pe->req.taskId);
×
2875
        }
2876
      } else {
2877
        mDebug("s-task:0x%x sendTs:%" PRId64 " wait %.2fs already, wait for next round to check", pe->req.taskId,
×
2878
               pe->req.startTs, (now - pe->ts) / 1000.0);
2879
      }
2880
    }
2881

2882
    mndReleaseStream(pMnode, pStream);
×
2883

2884
    int32_t alreadySend = doCleanReqList(pList, pInfo);
×
2885

2886
    // clear request stream item with empty task list
2887
    if (taosArrayGetSize(pInfo->pTaskList) == 0) {
×
2888
      mndClearConsensusRspEntry(pInfo);
×
2889
      if (streamId == -1) {
×
2890
        mError("streamId is -1, streamId:%" PRIx64 " in consensus-checkpointId hashMap, cont", pInfo->streamId);
×
2891
      }
2892

2893
      void *p = taosArrayPush(pStreamList, &streamId);
×
2894
      if (p == NULL) {
×
2895
        mError("failed to put into stream list, stream:0x%" PRIx64 " not remove it in consensus-chkpt list", streamId);
×
2896
      }
2897
    }
2898

2899
    numOfTrans += alreadySend;
×
2900
    if (numOfTrans > maxAllowedTrans) {
×
2901
      mInfo("already send consensus-checkpointId trans:%d, try next time", alreadySend);
×
2902
      taosHashCancelIterate(execInfo.pStreamConsensus, pIter);
×
2903
      break;
×
2904
    }
2905
  }
2906

2907
  for (int32_t i = 0; i < taosArrayGetSize(pStreamList); ++i) {
×
2908
    int64_t *pStreamId = (int64_t *)taosArrayGet(pStreamList, i);
×
2909
    if (pStreamId == NULL) {
×
2910
      continue;
×
2911
    }
2912

2913
    code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, *pStreamId);
×
2914
  }
2915

2916
  streamMutexUnlock(&execInfo.lock);
×
2917

2918
  taosArrayDestroy(pStreamList);
×
2919
  taosArrayDestroy(pList);
×
2920

2921
  mDebug("end to process consensus-checkpointId in tmr, send consensus-checkpoint trans:%d", numOfTrans);
×
2922
  return code;
×
2923
}
2924

2925
static int32_t mndProcessCreateStreamReqFromMNode(SRpcMsg *pReq) {
×
2926
  int32_t code = mndProcessCreateStreamReq(pReq);
×
2927
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
2928
    pReq->info.rsp = rpcMallocCont(1);
×
2929
    if (pReq->info.rsp == NULL) {
×
2930
      return terrno;
×
2931
    }
2932

2933
    pReq->info.rspLen = 1;
×
2934
    pReq->info.noResp = false;
×
2935
    pReq->code = code;
×
2936
  }
2937
  return code;
×
2938
}
2939

2940
static int32_t mndProcessDropStreamReqFromMNode(SRpcMsg *pReq) {
×
2941
  int32_t code = mndProcessDropStreamReq(pReq);
×
2942
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
2943
    pReq->info.rsp = rpcMallocCont(1);
×
2944
    if (pReq->info.rsp == NULL) {
×
2945
      return terrno;
×
2946
    }
2947

2948
    pReq->info.rspLen = 1;
×
2949
    pReq->info.noResp = false;
×
2950
    pReq->code = code;
×
2951
  }
2952
  return code;
×
2953
}
2954

2955
void mndInitStreamExecInfo(SMnode *pMnode, SStreamExecInfo *pExecInfo) {
×
2956
  if (pExecInfo->initTaskList || pMnode == NULL) {
×
2957
    return;
×
2958
  }
2959

2960
  addAllStreamTasksIntoBuf(pMnode, pExecInfo);
×
2961
  pExecInfo->initTaskList = true;
×
2962
}
2963

2964
void mndStreamResetInitTaskListLoadFlag() {
×
2965
  mInfo("reset task list buffer init flag for leader");
×
2966
  execInfo.initTaskList = false;
×
2967
}
×
2968

2969
void mndUpdateStreamExecInfoRole(SMnode *pMnode, int32_t role) {
×
2970
  execInfo.switchFromFollower = false;
×
2971

2972
  if (execInfo.role == NODE_ROLE_UNINIT) {
×
2973
    execInfo.role = role;
×
2974
    if (role == NODE_ROLE_LEADER) {
×
2975
      mInfo("init mnode is set to leader");
×
2976
    } else {
2977
      mInfo("init mnode is set to follower");
×
2978
    }
2979
  } else {
2980
    if (role == NODE_ROLE_LEADER) {
×
2981
      if (execInfo.role == NODE_ROLE_FOLLOWER) {
×
2982
        execInfo.role = role;
×
2983
        execInfo.switchFromFollower = true;
×
2984
        mInfo("mnode switch to be leader from follower");
×
2985
      } else {
2986
        mInfo("mnode remain to be leader, do nothing");
×
2987
      }
2988
    } else {  // follower's
2989
      if (execInfo.role == NODE_ROLE_LEADER) {
×
2990
        execInfo.role = role;
×
2991
        mInfo("mnode switch to be follower from leader");
×
2992
      } else {
2993
        mInfo("mnode remain to be follower, do nothing");
×
2994
      }
2995
    }
2996
  }
2997
}
×
2998

2999
void addAllStreamTasksIntoBuf(SMnode *pMnode, SStreamExecInfo *pExecInfo) {
×
3000
  SSdb       *pSdb = pMnode->pSdb;
×
3001
  SStreamObj *pStream = NULL;
×
3002
  void       *pIter = NULL;
×
3003

3004
  while (1) {
3005
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
3006
    if (pIter == NULL) {
×
3007
      break;
×
3008
    }
3009

3010
    saveTaskAndNodeInfoIntoBuf(pStream, pExecInfo);
×
3011
    sdbRelease(pSdb, pStream);
×
3012
  }
3013
}
×
3014

3015
int32_t mndCreateStreamChkptInfoUpdateTrans(SMnode *pMnode, SStreamObj *pStream, SArray *pChkptInfoList) {
×
3016
  STrans *pTrans = NULL;
×
3017
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_UPDATE_NAME,
×
3018
                               "update checkpoint-info", &pTrans);
3019
  if (pTrans == NULL || code) {
×
3020
    sdbRelease(pMnode->pSdb, pStream);
×
3021
    return code;
×
3022
  }
3023

3024
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_UPDATE_NAME, pStream->uid);
×
3025
  if (code) {
×
3026
    sdbRelease(pMnode->pSdb, pStream);
×
3027
    mndTransDrop(pTrans);
×
3028
    return code;
×
3029
  }
3030

3031
  code = mndStreamSetUpdateChkptAction(pMnode, pTrans, pStream);
×
3032
  if (code) {
×
3033
    sdbRelease(pMnode->pSdb, pStream);
×
3034
    mndTransDrop(pTrans);
×
3035
    return code;
×
3036
  }
3037

3038
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
×
3039
  if (code) {
×
3040
    sdbRelease(pMnode->pSdb, pStream);
×
3041
    mndTransDrop(pTrans);
×
3042
    return code;
×
3043
  }
3044

3045
  code = mndTransPrepare(pMnode, pTrans);
×
3046
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
3047
    mError("trans:%d, failed to prepare update checkpoint-info meta trans since %s", pTrans->id, tstrerror(code));
×
3048
    sdbRelease(pMnode->pSdb, pStream);
×
3049
    mndTransDrop(pTrans);
×
3050
    return code;
×
3051
  }
3052

3053
  sdbRelease(pMnode->pSdb, pStream);
×
3054
  mndTransDrop(pTrans);
×
3055

3056
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
3057
}
3058

3059
static int32_t mndProcessDropOrphanTaskReq(SRpcMsg *pReq) {
×
3060
  SMnode      *pMnode = pReq->info.node;
×
3061
  int32_t      code = 0;
×
3062
  SOrphanTask *pTask = NULL;
×
3063
  int32_t      i = 0;
×
3064
  STrans      *pTrans = NULL;
×
3065
  int32_t      numOfTasks = 0;
×
3066

3067
  SMStreamDropOrphanMsg msg = {0};
×
3068
  code = tDeserializeDropOrphanTaskMsg(pReq->pCont, pReq->contLen, &msg);
×
3069
  if (code) {
×
3070
    return code;
×
3071
  }
3072

3073
  numOfTasks = taosArrayGetSize(msg.pList);
×
3074
  if (numOfTasks == 0) {
×
3075
    mDebug("no orphan tasks to drop, no need to create trans");
×
3076
    goto _err;
×
3077
  }
3078

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

3081
  i = 0;
×
3082
  while (i < numOfTasks && ((pTask = taosArrayGet(msg.pList, i)) == NULL)) {
×
3083
    i += 1;
×
3084
  }
3085

3086
  if (pTask == NULL) {
×
3087
    mError("failed to extract entry in drop orphan task list, not create trans to drop orphan-task");
×
3088
    goto _err;
×
3089
  }
3090

3091
  // check if it is conflict with other trans in both sourceDb and targetDb.
3092
  code = mndStreamTransConflictCheck(pMnode, pTask->streamId, MND_STREAM_DROP_NAME, false);
×
3093
  if (code) {
×
3094
    goto _err;
×
3095
  }
3096

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

3099
  code = doCreateTrans(pMnode, &dummyObj, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, "drop stream", &pTrans);
×
3100
  if (pTrans == NULL || code != 0) {
×
3101
    mError("failed to create trans to drop orphan tasks since %s", tstrerror(code));
×
3102
    goto _err;
×
3103
  }
3104

3105
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_DROP_NAME, pTask->streamId);
×
3106
  if (code) {
×
3107
    goto _err;
×
3108
  }
3109

3110
  // drop all tasks
3111
  if ((code = mndStreamSetDropActionFromList(pMnode, pTrans, msg.pList)) < 0) {
×
3112
    mError("failed to create trans to drop orphan tasks since %s", tstrerror(code));
×
3113
    goto _err;
×
3114
  }
3115

3116
  // drop stream
3117
  if ((code = mndPersistTransLog(&dummyObj, pTrans, SDB_STATUS_DROPPED)) < 0) {
×
3118
    goto _err;
×
3119
  }
3120

3121
  code = mndTransPrepare(pMnode, pTrans);
×
3122
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
3123
    mError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
×
3124
    goto _err;
×
3125
  }
3126

3127
_err:
×
3128
  tDestroyDropOrphanTaskMsg(&msg);
×
3129
  mndTransDrop(pTrans);
×
3130

3131
  if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {
×
3132
    mDebug("create drop %d orphan tasks trans succ", numOfTasks);
×
3133
  }
3134
  return code;
×
3135
}
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