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

taosdata / TDengine / #3585

20 Jan 2025 09:22AM UTC coverage: 63.647% (-0.1%) from 63.756%
#3585

push

travis-ci

web-flow
Merge pull request #29603 from taosdata/enh/3.0/TD-32588

enh:[TD-32588]refactor stmt-async-bind loop usleep to Producer Consumer Model

140935 of 284561 branches covered (49.53%)

Branch coverage included in aggregate %.

219502 of 281745 relevant lines covered (77.91%)

18611836.92 hits per line

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

62.13
/source/dnode/mnode/impl/src/mndTrans.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
#define _DEFAULT_SOURCE
17
#include "mndTrans.h"
18
#include "mndDb.h"
19
#include "mndPrivilege.h"
20
#include "mndShow.h"
21
#include "mndStb.h"
22
#include "mndSubscribe.h"
23
#include "mndSync.h"
24
#include "mndUser.h"
25
#include "mndVgroup.h"
26
#include "osTime.h"
27

28
#define TRANS_VER1_NUMBER  1
29
#define TRANS_VER2_NUMBER  2
30
#define TRANS_ARRAY_SIZE   8
31
#define TRANS_RESERVE_SIZE 42
32

33
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans);
34
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *OldTrans, STrans *pOld);
35
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc);
36

37
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw);
38
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction);
39
static void    mndTransDropLogs(SArray *pArray);
40
static void    mndTransDropActions(SArray *pArray);
41

42
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf);
43
static int32_t mndTransExecuteRedoLogs(SMnode *pMnode, STrans *pTrans, bool topHalf);
44
static int32_t mndTransExecuteUndoLogs(SMnode *pMnode, STrans *pTrans, bool topHalf);
45
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf);
46
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf);
47
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf);
48
static bool    mndTransPerformRedoLogStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
49
static bool    mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
50
static bool    mndTransPerformUndoLogStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
51
static bool    mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
52
static bool    mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
53
static bool    mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
54
static bool    mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
55
static bool    mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
56

57
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
485,569✔
58

59
static bool mndCannotExecuteTrans(SMnode *pMnode, bool topHalf) {
406,289✔
60
  bool isLeader = mndIsLeader(pMnode);
406,289✔
61
  bool ret = (!pMnode->deploy && !isLeader) || mndTransIsInSyncContext(topHalf);
406,289✔
62
  if (ret) mDebug("cannot execute trans action, deploy:%d, isLeader:%d, topHalf:%d", pMnode->deploy, isLeader, topHalf);
406,289✔
63
  return ret;
406,289✔
64
}
65

66
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
487,959✔
67

68
static void    mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans);
69
static int32_t mndProcessTransTimer(SRpcMsg *pReq);
70
static int32_t mndProcessTtl(SRpcMsg *pReq);
71
static int32_t mndProcessKillTransReq(SRpcMsg *pReq);
72

73
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
74
static void    mndCancelGetNextTrans(SMnode *pMnode, void *pIter);
75
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
76
static int32_t tsMaxTransId = 0;
77

78
int32_t mndInitTrans(SMnode *pMnode) {
1,735✔
79
  SSdbTable table = {
1,735✔
80
      .sdbType = SDB_TRANS,
81
      .keyType = SDB_KEY_INT32,
82
      .encodeFp = (SdbEncodeFp)mndTransEncode,
83
      .decodeFp = (SdbDecodeFp)mndTransDecode,
84
      .insertFp = (SdbInsertFp)mndTransActionInsert,
85
      .updateFp = (SdbUpdateFp)mndTransActionUpdate,
86
      .deleteFp = (SdbDeleteFp)mndTransDelete,
87
  };
88

89
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
1,735✔
90
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
1,735✔
91

92
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndRetrieveTrans);
1,735✔
93
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndCancelGetNextTrans);
1,735✔
94
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANSACTION_DETAIL, mndRetrieveTransDetail);
1,735✔
95
  return sdbSetTable(pMnode->pSdb, table);
1,735✔
96
}
97

98
void mndCleanupTrans(SMnode *pMnode) {}
1,734✔
99

100
static int32_t mndTransGetActionsSize(SArray *pArray) {
562,792✔
101
  int32_t actionNum = taosArrayGetSize(pArray);
562,792✔
102
  int32_t rawDataLen = 0;
562,792✔
103

104
  for (int32_t i = 0; i < actionNum; ++i) {
1,764,408✔
105
    STransAction *pAction = taosArrayGet(pArray, i);
1,201,616✔
106
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,201,616✔
107
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
740,166✔
108
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
461,450!
109
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
461,450✔
110
    } else {
111
      // empty
112
    }
113
    rawDataLen += sizeof(int8_t);
1,201,616✔
114
  }
115

116
  return rawDataLen;
562,792✔
117
}
118

119
static int32_t mndTransEncodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionsNum) {
562,792✔
120
  int32_t code = 0;
562,792✔
121
  int32_t lino = 0;
562,792✔
122
  int32_t dataPos = *offset;
562,792✔
123
  int8_t  unused = 0;
562,792✔
124
  int32_t ret = -1;
562,792✔
125

126
  for (int32_t i = 0; i < actionsNum; ++i) {
1,764,408✔
127
    STransAction *pAction = taosArrayGet(pActions, i);
1,201,616✔
128
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
1,201,616!
129
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
1,201,616!
130
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
1,201,616!
131
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
1,201,616!
132
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
1,201,616!
133
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
1,201,616!
134
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
1,201,616!
135
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,201,616✔
136
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
740,166✔
137
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
740,166!
138
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
740,166!
139
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
740,166!
140
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
461,450!
141
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
461,450!
142
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
461,450!
143
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
461,450!
144
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
461,450!
145
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
461,450!
146
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
461,450!
147
    } else {
148
      // nothing
149
    }
150
  }
151
  ret = 0;
562,792✔
152

153
_OVER:
562,792✔
154
  *offset = dataPos;
562,792✔
155
  return ret;
562,792✔
156
}
157

158
SSdbRaw *mndTransEncode(STrans *pTrans) {
140,698✔
159
  int32_t code = 0;
140,698✔
160
  int32_t lino = 0;
140,698✔
161
  terrno = TSDB_CODE_INVALID_MSG;
140,698✔
162
  int8_t sver = TRANS_VER2_NUMBER;
140,698✔
163

164
  int32_t rawDataLen = sizeof(STrans) + TRANS_RESERVE_SIZE + pTrans->paramLen;
140,698✔
165
  rawDataLen += mndTransGetActionsSize(pTrans->prepareActions);
140,698✔
166
  rawDataLen += mndTransGetActionsSize(pTrans->redoActions);
140,698✔
167
  rawDataLen += mndTransGetActionsSize(pTrans->undoActions);
140,698✔
168
  rawDataLen += mndTransGetActionsSize(pTrans->commitActions);
140,698✔
169

170
  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, sver, rawDataLen);
140,698✔
171
  if (pRaw == NULL) {
140,698!
172
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
×
173
    return NULL;
×
174
  }
175

176
  int32_t dataPos = 0;
140,698✔
177
  SDB_SET_INT32(pRaw, dataPos, pTrans->id, _OVER)
140,698!
178
  SDB_SET_INT8(pRaw, dataPos, pTrans->stage, _OVER)
140,698!
179
  SDB_SET_INT8(pRaw, dataPos, pTrans->policy, _OVER)
140,698!
180
  SDB_SET_INT8(pRaw, dataPos, pTrans->conflict, _OVER)
140,698!
181
  SDB_SET_INT8(pRaw, dataPos, pTrans->exec, _OVER)
140,698!
182
  SDB_SET_INT8(pRaw, dataPos, pTrans->oper, _OVER)
140,698!
183
  SDB_SET_INT8(pRaw, dataPos, 0, _OVER)
140,698!
184
  SDB_SET_INT16(pRaw, dataPos, pTrans->originRpcType, _OVER)
140,698!
185
  SDB_SET_INT64(pRaw, dataPos, pTrans->createdTime, _OVER)
140,698!
186
  SDB_SET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
140,698!
187
  SDB_SET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
140,698!
188
  SDB_SET_INT32(pRaw, dataPos, pTrans->actionPos, _OVER)
140,698!
189

190
  int32_t prepareActionNum = taosArrayGetSize(pTrans->prepareActions);
140,698✔
191
  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
140,698✔
192
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
140,698✔
193
  int32_t commitActionNum = taosArrayGetSize(pTrans->commitActions);
140,698✔
194

195
  if (sver > TRANS_VER1_NUMBER) {
140,698!
196
    SDB_SET_INT32(pRaw, dataPos, prepareActionNum, _OVER)
140,698!
197
  }
198
  SDB_SET_INT32(pRaw, dataPos, redoActionNum, _OVER)
140,698!
199
  SDB_SET_INT32(pRaw, dataPos, undoActionNum, _OVER)
140,698!
200
  SDB_SET_INT32(pRaw, dataPos, commitActionNum, _OVER)
140,698!
201

202
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum) < 0) goto _OVER;
140,698!
203
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum) < 0) goto _OVER;
140,698!
204
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum) < 0) goto _OVER;
140,698!
205
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum) < 0) goto _OVER;
140,698!
206

207
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
140,698!
208
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
140,698!
209
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
140,698!
210
  if (pTrans->param != NULL) {
140,698!
211
    SDB_SET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER)
×
212
  }
213

214
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
140,698!
215

216
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
140,698✔
217
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
140,698!
218
  void *pIter = NULL;
140,698✔
219
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
140,698✔
220
  while (pIter) {
140,731✔
221
    int32_t arbGroupId = *(int32_t *)pIter;
33✔
222
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
33!
223
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
33✔
224
  }
225

226
  if (sver > TRANS_VER1_NUMBER) {
140,698!
227
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
140,698!
228
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
140,698!
229
  }
230

231
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
140,698!
232
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
140,698!
233

234
  terrno = 0;
140,698✔
235

236
_OVER:
140,698✔
237
  if (terrno != 0) {
140,698!
238
    mError("trans:%d, failed to encode to raw:%p maxlen:%d len:%d since %s", pTrans->id, pRaw, sdbGetRawTotalSize(pRaw),
×
239
           dataPos, terrstr());
240
    sdbFreeRaw(pRaw);
×
241
    return NULL;
×
242
  }
243

244
  mTrace("trans:%d, encode to raw:%p, row:%p len:%d", pTrans->id, pRaw, pTrans, dataPos);
140,698✔
245
  return pRaw;
140,698✔
246
}
247

248
static int32_t mndTransDecodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionNum) {
1,065,256✔
249
  int32_t      code = 0;
1,065,256✔
250
  int32_t      lino = 0;
1,065,256✔
251
  STransAction action = {0};
1,065,256✔
252
  int32_t      dataPos = *offset;
1,065,256✔
253
  int8_t       unused = 0;
1,065,256✔
254
  int8_t       stage = 0;
1,065,256✔
255
  int8_t       actionType = 0;
1,065,256✔
256
  int32_t      dataLen = 0;
1,065,256✔
257
  int32_t      ret = -1;
1,065,256✔
258

259
  for (int32_t i = 0; i < actionNum; ++i) {
3,301,673✔
260
    memset(&action, 0, sizeof(action));
2,236,417✔
261
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
2,236,417!
262
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
2,236,417!
263
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
2,236,417!
264
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
2,236,417!
265
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
2,236,417!
266
    action.actionType = actionType;
2,236,417✔
267
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
2,236,417!
268
    action.stage = stage;
2,236,417✔
269
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
2,236,417!
270
    if (action.actionType == TRANS_ACTION_RAW) {
2,236,417✔
271
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
1,342,718!
272
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
1,342,718!
273
      action.pRaw = taosMemoryMalloc(dataLen);
1,342,718!
274
      if (action.pRaw == NULL) goto _OVER;
1,342,718!
275
      mTrace("raw:%p, is created", action.pRaw);
1,342,718✔
276
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
1,342,718!
277
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
1,342,718!
278
      action.pRaw = NULL;
1,342,718✔
279
    } else if (action.actionType == TRANS_ACTION_MSG) {
893,699!
280
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
893,699!
281
      tmsgUpdateDnodeEpSet(&action.epSet);
893,699✔
282
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
893,699!
283
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
893,699!
284
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
893,699!
285
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
893,699!
286
      action.pCont = taosMemoryMalloc(action.contLen);
893,699!
287
      if (action.pCont == NULL) goto _OVER;
893,699!
288
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
893,699!
289
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
893,699!
290
      action.pCont = NULL;
893,699✔
291
    } else {
292
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
293
    }
294
  }
295
  ret = 0;
1,065,256✔
296

297
_OVER:
1,065,256✔
298
  *offset = dataPos;
1,065,256✔
299
  taosMemoryFreeClear(action.pCont);
1,065,256!
300
  return ret;
1,065,256✔
301
}
302

303
SSdbRow *mndTransDecode(SSdbRaw *pRaw) {
266,314✔
304
  terrno = TSDB_CODE_INVALID_MSG;
266,314✔
305
  int32_t  code = 0;
266,314✔
306
  int32_t  lino = 0;
266,314✔
307
  SSdbRow *pRow = NULL;
266,314✔
308
  STrans  *pTrans = NULL;
266,314✔
309
  char    *pData = NULL;
266,314✔
310
  int32_t  dataLen = 0;
266,314✔
311
  int8_t   sver = 0;
266,314✔
312
  int32_t  prepareActionNum = 0;
266,314✔
313
  int32_t  redoActionNum = 0;
266,314✔
314
  int32_t  undoActionNum = 0;
266,314✔
315
  int32_t  commitActionNum = 0;
266,314✔
316
  int32_t  dataPos = 0;
266,314✔
317
  int32_t  arbgroupIdNum = 0;
266,314✔
318

319
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
266,314!
320

321
  if (sver > TRANS_VER2_NUMBER) {
266,314!
322
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
323
    goto _OVER;
×
324
  }
325

326
  pRow = sdbAllocRow(sizeof(STrans));
266,314✔
327
  if (pRow == NULL) goto _OVER;
266,314!
328

329
  pTrans = sdbGetRowObj(pRow);
266,314✔
330
  if (pTrans == NULL) goto _OVER;
266,314!
331

332
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
266,314!
333

334
  int8_t stage = 0;
266,314✔
335
  int8_t policy = 0;
266,314✔
336
  int8_t conflict = 0;
266,314✔
337
  int8_t exec = 0;
266,314✔
338
  int8_t oper = 0;
266,314✔
339
  int8_t reserved = 0;
266,314✔
340
  int8_t actionType = 0;
266,314✔
341
  SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
266,314!
342
  SDB_GET_INT8(pRaw, dataPos, &policy, _OVER)
266,314!
343
  SDB_GET_INT8(pRaw, dataPos, &conflict, _OVER)
266,314!
344
  SDB_GET_INT8(pRaw, dataPos, &exec, _OVER)
266,314!
345
  SDB_GET_INT8(pRaw, dataPos, &oper, _OVER)
266,314!
346
  SDB_GET_INT8(pRaw, dataPos, &reserved, _OVER)
266,314!
347
  pTrans->stage = stage;
266,314✔
348
  pTrans->policy = policy;
266,314✔
349
  pTrans->conflict = conflict;
266,314✔
350
  pTrans->exec = exec;
266,314✔
351
  pTrans->oper = oper;
266,314✔
352
  SDB_GET_INT16(pRaw, dataPos, &pTrans->originRpcType, _OVER)
266,314!
353
  SDB_GET_INT64(pRaw, dataPos, &pTrans->createdTime, _OVER)
266,314!
354
  SDB_GET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
266,314!
355
  SDB_GET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
266,314!
356
  SDB_GET_INT32(pRaw, dataPos, &pTrans->actionPos, _OVER)
266,314!
357

358
  if (sver > TRANS_VER1_NUMBER) {
266,314!
359
    SDB_GET_INT32(pRaw, dataPos, &prepareActionNum, _OVER)
266,314!
360
  }
361
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, _OVER)
266,314!
362
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, _OVER)
266,314!
363
  SDB_GET_INT32(pRaw, dataPos, &commitActionNum, _OVER)
266,314!
364

365
  pTrans->prepareActions = taosArrayInit(prepareActionNum, sizeof(STransAction));
266,314✔
366
  pTrans->redoActions = taosArrayInit(redoActionNum, sizeof(STransAction));
266,314✔
367
  pTrans->undoActions = taosArrayInit(undoActionNum, sizeof(STransAction));
266,314✔
368
  pTrans->commitActions = taosArrayInit(commitActionNum, sizeof(STransAction));
266,314✔
369

370
  if (pTrans->prepareActions == NULL) goto _OVER;
266,314!
371
  if (pTrans->redoActions == NULL) goto _OVER;
266,314!
372
  if (pTrans->undoActions == NULL) goto _OVER;
266,314!
373
  if (pTrans->commitActions == NULL) goto _OVER;
266,314!
374

375
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum) < 0) goto _OVER;
266,314!
376
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum) < 0) goto _OVER;
266,314!
377
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum) < 0) goto _OVER;
266,314!
378
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum) < 0) goto _OVER;
266,314!
379

380
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
266,314!
381
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
266,314!
382
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
266,314!
383
  if (pTrans->paramLen != 0) {
266,314!
384
    pTrans->param = taosMemoryMalloc(pTrans->paramLen);
×
385
    if (pTrans->param == NULL) goto _OVER;
×
386
    SDB_GET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER);
×
387
  }
388

389
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
266,314!
390

391
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
266,314✔
392

393
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
266,314!
394
  for (int32_t i = 0; i < arbgroupIdNum; ++i) {
266,369✔
395
    int32_t arbGroupId = 0;
55✔
396
    SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
55!
397
    if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
55!
398
  }
399

400
  int8_t ableKill = 0;
266,314✔
401
  int8_t killMode = 0;
266,314✔
402
  SDB_GET_INT8(pRaw, dataPos, &ableKill, _OVER)
266,314!
403
  SDB_GET_INT8(pRaw, dataPos, &killMode, _OVER)
266,314!
404
  pTrans->ableToBeKilled = ableKill;
266,314✔
405
  pTrans->killMode = killMode;
266,314✔
406

407
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
266,314!
408

409
  terrno = 0;
266,314✔
410

411
_OVER:
266,314✔
412
  if (terrno != 0 && pTrans != NULL) {
266,314!
413
    mError("trans:%d, failed to parse from raw:%p since %s", pTrans->id, pRaw, terrstr());
×
414
    mndTransDropData(pTrans);
×
415
    taosMemoryFreeClear(pRow);
×
416
    return NULL;
×
417
  }
418

419
  if (pTrans != NULL) {
266,314!
420
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
266,314✔
421
  }
422
  return pRow;
266,314✔
423
}
424

425
static const char *mndTransStr(ETrnStage stage) {
2,179,081✔
426
  switch (stage) {
2,179,081!
427
    case TRN_STAGE_PREPARE:
135,316✔
428
      return "prepare";
135,316✔
429
    case TRN_STAGE_REDO_ACTION:
957,036✔
430
      return "redoAction";
957,036✔
431
    case TRN_STAGE_ROLLBACK:
19✔
432
      return "rollback";
19✔
433
    case TRN_STAGE_UNDO_ACTION:
43,780✔
434
      return "undoAction";
43,780✔
435
    case TRN_STAGE_COMMIT:
222,272✔
436
      return "commit";
222,272✔
437
    case TRN_STAGE_COMMIT_ACTION:
489,021✔
438
      return "commitAction";
489,021✔
439
    case TRN_STAGE_FINISH:
331,619✔
440
      return "finished";
331,619✔
441
    case TRN_STAGE_PRE_FINISH:
19✔
442
      return "pre-finish";
19✔
443
    default:
×
444
      return "invalid";
×
445
  }
446
}
447

448
static const char *mndTransTypeStr(ETrnAct actionType) {
×
449
  switch (actionType) {
×
450
    case TRANS_ACTION_MSG:
×
451
      return "msg";
×
452
    case TRANS_ACTION_RAW:
×
453
      return "sdb";
×
454
    default:
×
455
      return "invalid";
×
456
  }
457
}
458

459
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
654,638✔
460
  if (pAction != NULL) {
654,638✔
461
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
539,996✔
462
      pTrans->lastAction = pAction->id;
351,431✔
463
      pTrans->lastMsgType = pAction->msgType;
351,431✔
464
      pTrans->lastEpset = pAction->epSet;
351,431✔
465
      pTrans->lastErrorNo = pAction->errCode;
351,431✔
466
    }
467
  } else {
468
    pTrans->lastAction = 0;
114,642✔
469
    pTrans->lastMsgType = 0;
114,642✔
470
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
114,642✔
471
    pTrans->lastErrorNo = 0;
114,642✔
472
  }
473
}
654,638✔
474

475
static void mndTransTestStartFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
476
  mInfo("test trans start, param:%s, len:%d", (char *)param, paramLen);
×
477
}
×
478

479
static void mndTransTestStopFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
480
  mInfo("test trans stop, param:%s, len:%d", (char *)param, paramLen);
×
481
}
×
482

483
static TransCbFp mndTransGetCbFp(ETrnFunc ftype) {
2,052✔
484
  switch (ftype) {
2,052!
485
    case TRANS_START_FUNC_TEST:
×
486
      return mndTransTestStartFunc;
×
487
    case TRANS_STOP_FUNC_TEST:
×
488
      return mndTransTestStopFunc;
×
489
    case TRANS_START_FUNC_MQ_REB:
1,026✔
490
      return mndRebCntInc;
1,026✔
491
    case TRANS_STOP_FUNC_MQ_REB:
1,026✔
492
      return mndRebCntDec;
1,026✔
493
    default:
×
494
      return NULL;
×
495
  }
496
}
497

498
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
49,582✔
499
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
49,582!
500
        mndTransStr(pTrans->stage), pTrans->startFunc);
501

502
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
49,582✔
503

504
  if (pTrans->startFunc > 0) {
49,582✔
505
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
1,026✔
506
    if (fp) {
1,026!
507
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,026✔
508
    }
509
    // pTrans->startFunc = 0;
510
  }
511

512
  if (pTrans->stage == TRN_STAGE_COMMIT) {
49,582✔
513
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
5✔
514
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pTrans->id);
5!
515
  }
516

517
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
49,582!
518
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
×
519
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
×
520
  }
521

522
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
49,582!
523
    pTrans->stage = TRN_STAGE_FINISH;
×
524
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pTrans->id);
×
525
  }
526

527
  return 0;
49,582✔
528
}
529

530
void mndTransDropData(STrans *pTrans) {
307,896✔
531
  if (pTrans->prepareActions != NULL) {
307,896!
532
    mndTransDropActions(pTrans->prepareActions);
307,896✔
533
    pTrans->prepareActions = NULL;
307,896✔
534
  }
535
  if (pTrans->redoActions != NULL) {
307,896!
536
    mndTransDropActions(pTrans->redoActions);
307,896✔
537
    pTrans->redoActions = NULL;
307,896✔
538
  }
539
  if (pTrans->undoActions != NULL) {
307,896!
540
    mndTransDropActions(pTrans->undoActions);
307,896✔
541
    pTrans->undoActions = NULL;
307,896✔
542
  }
543
  if (pTrans->commitActions != NULL) {
307,896!
544
    mndTransDropActions(pTrans->commitActions);
307,896✔
545
    pTrans->commitActions = NULL;
307,896✔
546
  }
547
  if (pTrans->arbGroupIds != NULL) {
307,896!
548
    taosHashCleanup(pTrans->arbGroupIds);
307,896✔
549
  }
550
  if (pTrans->pRpcArray != NULL) {
307,896✔
551
    taosArrayDestroy(pTrans->pRpcArray);
41,582✔
552
    pTrans->pRpcArray = NULL;
41,582✔
553
  }
554
  if (pTrans->rpcRsp != NULL) {
307,896✔
555
    taosMemoryFree(pTrans->rpcRsp);
11,920!
556
    pTrans->rpcRsp = NULL;
11,920✔
557
    pTrans->rpcRspLen = 0;
11,920✔
558
  }
559
  if (pTrans->param != NULL) {
307,896!
560
    taosMemoryFree(pTrans->param);
×
561
    pTrans->param = NULL;
×
562
    pTrans->paramLen = 0;
×
563
  }
564
  (void)taosThreadMutexDestroy(&pTrans->mutex);
307,896✔
565
}
307,896✔
566

567
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc) {
157,927✔
568
  mInfo("trans:%d, perform delete action, row:%p stage:%s callfunc:%d, stopFunc:%d", pTrans->id, pTrans,
157,927!
569
        mndTransStr(pTrans->stage), callFunc, pTrans->stopFunc);
570

571
  if (pTrans->stopFunc > 0 && callFunc) {
157,927✔
572
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
1,026✔
573
    if (fp) {
1,026!
574
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,026✔
575
    }
576
    // pTrans->stopFunc = 0;
577
  }
578

579
  mndTransDropData(pTrans);
157,927✔
580
  return 0;
157,927✔
581
}
582

583
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
235,924✔
584
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
763,442✔
585
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
527,518✔
586
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
527,518✔
587
    pOldAction->rawWritten = pNewAction->rawWritten;
527,518✔
588
    pOldAction->msgSent = pNewAction->msgSent;
527,518✔
589
    pOldAction->msgReceived = pNewAction->msgReceived;
527,518✔
590
    pOldAction->errCode = pNewAction->errCode;
527,518✔
591
  }
592
}
235,924✔
593

594
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) {
58,981✔
595
  mInfo("trans:%d, perform update action, old row:%p stage:%s create:%" PRId64 ", new row:%p stage:%s create:%" PRId64,
58,981!
596
        pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage), pNew->createdTime);
597

598
  if (pOld->createdTime != pNew->createdTime) {
58,981!
599
    mError("trans:%d, failed to perform update action since createTime not match, old row:%p stage:%s create:%" PRId64
×
600
           ", new row:%p stage:%s create:%" PRId64,
601
           pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage),
602
           pNew->createdTime);
603
    // only occured while sync timeout
604
    TAOS_RETURN(TSDB_CODE_MND_TRANS_SYNC_TIMEOUT);
×
605
  }
606

607
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
58,981✔
608
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
58,981✔
609
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
58,981✔
610
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
58,981✔
611
  pOld->stage = pNew->stage;
58,981✔
612
  pOld->actionPos = pNew->actionPos;
58,981✔
613

614
  if (pOld->stage == TRN_STAGE_COMMIT) {
58,981✔
615
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
49,356✔
616
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
49,356!
617
  }
618

619
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
58,981✔
620
    pOld->stage = TRN_STAGE_UNDO_ACTION;
5✔
621
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pNew->id);
5!
622
  }
623

624
  if (pOld->stage == TRN_STAGE_PRE_FINISH) {
58,981✔
625
    pOld->stage = TRN_STAGE_FINISH;
5✔
626
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pNew->id);
5!
627
  }
628

629
  return 0;
58,981✔
630
}
631

632
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
252,326✔
633
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
252,326✔
634
  if (pTrans == NULL) {
252,326✔
635
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
4,473✔
636
  }
637
  return pTrans;
252,326✔
638
}
639

640
void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
247,854✔
641
  SSdb *pSdb = pMnode->pSdb;
247,854✔
642
  if (pTrans != NULL) mInfo("vgId:1, trans:%d, release transaction", pTrans->id);
247,854!
643
  sdbRelease(pSdb, pTrans);
247,854✔
644
}
247,854✔
645

646
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
41,582✔
647
                       const char *opername) {
648
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
41,582!
649
  if (pTrans == NULL) {
41,582!
650
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
651
    mError("failed to create transaction since %s", terrstr());
×
652
    return NULL;
×
653
  }
654

655
  if (opername != NULL) {
41,582!
656
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
41,582✔
657
  }
658

659
  int32_t sdbMaxId = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
41,582✔
660
  sdbReadLock(pMnode->pSdb, SDB_TRANS);
41,582✔
661
  pTrans->id = TMAX(sdbMaxId, tsMaxTransId + 1);
41,582✔
662
  sdbUnLock(pMnode->pSdb, SDB_TRANS);
41,582✔
663
  pTrans->stage = TRN_STAGE_PREPARE;
41,582✔
664
  pTrans->policy = policy;
41,582✔
665
  pTrans->conflict = conflict;
41,582✔
666
  pTrans->exec = TRN_EXEC_PARALLEL;
41,582✔
667
  pTrans->ableToBeKilled = false;
41,582✔
668
  pTrans->createdTime = taosGetTimestampMs();
41,582✔
669
  pTrans->prepareActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
41,582✔
670
  pTrans->redoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
41,582✔
671
  pTrans->undoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
41,582✔
672
  pTrans->commitActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
41,582✔
673
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
41,582✔
674
  pTrans->pRpcArray = taosArrayInit(1, sizeof(SRpcHandleInfo));
41,582✔
675
  pTrans->mTraceId = pReq ? TRACE_GET_ROOTID(&pReq->info.traceId) : tGenIdPI64();
41,582✔
676
  taosInitRWLatch(&pTrans->lockRpcArray);
41,582✔
677
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
41,582✔
678

679
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
41,582!
680
      pTrans->pRpcArray == NULL) {
41,582!
681
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
682
    mError("failed to create transaction since %s", terrstr());
×
683
    mndTransDrop(pTrans);
×
684
    return NULL;
×
685
  }
686

687
  if (pReq != NULL) {
41,582✔
688
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
55,626!
689
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
690
      return NULL;
×
691
    }
692
    pTrans->originRpcType = pReq->msgType;
27,813✔
693
  }
694

695
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
41,582!
696

697
  mTrace("trans:%d, local object is created, data:%p", pTrans->id, pTrans);
41,582✔
698
  return pTrans;
41,582✔
699
}
700

701
static void mndTransDropActions(SArray *pArray) {
1,231,584✔
702
  int32_t size = taosArrayGetSize(pArray);
1,231,584✔
703
  for (int32_t i = 0; i < size; ++i) {
3,809,843✔
704
    STransAction *pAction = taosArrayGet(pArray, i);
2,578,259✔
705
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,578,259✔
706
      taosMemoryFreeClear(pAction->pRaw);
1,566,372!
707
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,011,887!
708
      taosMemoryFreeClear(pAction->pCont);
1,011,887!
709
    } else {
710
      // nothing
711
    }
712
  }
713

714
  taosArrayDestroy(pArray);
1,231,584✔
715
}
1,231,584✔
716

717
void mndTransDrop(STrans *pTrans) {
42,453✔
718
  if (pTrans != NULL) {
42,453✔
719
    mndTransDropData(pTrans);
41,582✔
720
    mTrace("trans:%d, local object is freed, data:%p", pTrans->id, pTrans);
41,582✔
721
    taosMemoryFreeClear(pTrans);
41,582!
722
  }
723
}
42,453✔
724

725
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
341,842✔
726
  pAction->id = taosArrayGetSize(pArray);
341,842✔
727

728
  void *ptr = taosArrayPush(pArray, pAction);
341,842✔
729
  if (ptr == NULL) {
341,842!
730
    TAOS_RETURN(terrno);
×
731
  }
732

733
  return 0;
341,842✔
734
}
735

736
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
1,757✔
737
  STransAction action = {
1,757✔
738
      .stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw, .mTraceId = pTrans->mTraceId};
1,757✔
739
  return mndTransAppendAction(pTrans->redoActions, &action);
1,757✔
740
}
741

742
int32_t mndTransAppendNullLog(STrans *pTrans) {
×
743
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_NULL};
×
744
  return mndTransAppendAction(pTrans->redoActions, &action);
×
745
}
746

747
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
12,692✔
748
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
12,692✔
749
  return mndTransAppendAction(pTrans->undoActions, &action);
12,692✔
750
}
751

752
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
189,316✔
753
  STransAction action = {.stage = TRN_STAGE_COMMIT_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
189,316✔
754
  return mndTransAppendAction(pTrans->commitActions, &action);
189,316✔
755
}
756

757
int32_t mndTransAppendPrepareLog(STrans *pTrans, SSdbRaw *pRaw) {
19,889✔
758
  STransAction action = {
19,889✔
759
      .pRaw = pRaw, .stage = TRN_STAGE_PREPARE, .actionType = TRANS_ACTION_RAW, .mTraceId = pTrans->mTraceId};
19,889✔
760
  return mndTransAppendAction(pTrans->prepareActions, &action);
19,889✔
761
}
762

763
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
87,217✔
764
  pAction->stage = TRN_STAGE_REDO_ACTION;
87,217✔
765
  pAction->actionType = TRANS_ACTION_MSG;
87,217✔
766
  pAction->mTraceId = pTrans->mTraceId;
87,217✔
767
  return mndTransAppendAction(pTrans->redoActions, pAction);
87,217✔
768
}
769

770
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
30,971✔
771
  pAction->stage = TRN_STAGE_UNDO_ACTION;
30,971✔
772
  pAction->actionType = TRANS_ACTION_MSG;
30,971✔
773
  return mndTransAppendAction(pTrans->undoActions, pAction);
30,971✔
774
}
775

776
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
11,920✔
777
  pTrans->rpcRsp = pCont;
11,920✔
778
  pTrans->rpcRspLen = contLen;
11,920✔
779
}
11,920✔
780

781
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
1,014✔
782
  pTrans->startFunc = startFunc;
1,014✔
783
  pTrans->stopFunc = stopFunc;
1,014✔
784
  pTrans->param = param;
1,014✔
785
  pTrans->paramLen = paramLen;
1,014✔
786
}
1,014✔
787

788
int32_t mndSetRpcInfoForDbTrans(SMnode *pMnode, SRpcMsg *pMsg, EOperType oper, const char *dbname) {
×
789
  STrans *pTrans = NULL;
×
790
  void   *pIter = NULL;
×
791
  int32_t code = -1;
×
792

793
  while (1) {
794
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
×
795
    if (pIter == NULL) break;
×
796

797
    if (pTrans->oper == oper) {
×
798
      if (strcasecmp(dbname, pTrans->dbname) == 0) {
×
799
        mInfo("trans:%d, db:%s oper:%d matched with input", pTrans->id, dbname, oper);
×
800
        taosWLockLatch(&pTrans->lockRpcArray);
×
801
        if (pTrans->pRpcArray == NULL) {
×
802
          pTrans->pRpcArray = taosArrayInit(4, sizeof(SRpcHandleInfo));
×
803
        }
804
        if (pTrans->pRpcArray != NULL && taosArrayPush(pTrans->pRpcArray, &pMsg->info) != NULL) {
×
805
          code = 0;
×
806
        }
807
        taosWUnLockLatch(&pTrans->lockRpcArray);
×
808

809
        sdbRelease(pMnode->pSdb, pTrans);
×
810
        break;
×
811
      }
812
    }
813

814
    sdbRelease(pMnode->pSdb, pTrans);
×
815
  }
816
  return code;
×
817
}
818

819
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
29,380✔
820
  if (dbname != NULL) {
29,380!
821
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
29,380✔
822
  }
823
  if (stbname != NULL) {
29,380✔
824
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
22,239✔
825
  }
826
}
29,380✔
827

828
void mndTransAddArbGroupId(STrans *pTrans, int32_t groupId) {
11✔
829
  if (taosHashPut(pTrans->arbGroupIds, &groupId, sizeof(int32_t), NULL, 0) != 0) {
11!
830
    mError("trans:%d, failed to put groupid into hash, groupId:%d", pTrans->id, groupId);
×
831
  }
832
}
11✔
833

834
void mndTransSetSerial(STrans *pTrans) { pTrans->exec = TRN_EXEC_SERIAL; }
2,870✔
835

836
void mndTransSetBeKilled(STrans *pTrans, bool ableToBeKilled) { pTrans->ableToBeKilled = ableToBeKilled; }
×
837

838
void mndTransSetKillMode(STrans *pTrans, ETrnKillMode killMode) {
9✔
839
  pTrans->ableToBeKilled = true; 
9✔
840
  pTrans->killMode = killMode; 
9✔
841
}
9✔
842

843
void mndTransSetParallel(STrans *pTrans) { pTrans->exec = TRN_EXEC_PARALLEL; }
×
844

845
void mndTransSetChangeless(STrans *pTrans) { pTrans->changeless = true; }
35✔
846

847
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
3,681✔
848

849
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
91,033✔
850
  int32_t  code = 0;
91,033✔
851
  SSdbRaw *pRaw = mndTransEncode(pTrans);
91,033✔
852
  if (pRaw == NULL) {
91,033!
853
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
854
    if (terrno != 0) code = terrno;
×
855
    mError("trans:%d, failed to encode while sync trans since %s", pTrans->id, tstrerror(code));
×
856
    TAOS_RETURN(code);
×
857
  }
858
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_READY));
91,033!
859

860
  mInfo("trans:%d, sync to other mnodes, stage:%s createTime:%" PRId64, pTrans->id, mndTransStr(pTrans->stage),
91,033!
861
        pTrans->createdTime);
862
  code = mndSyncPropose(pMnode, pRaw, pTrans->id);
91,033✔
863
  if (code != 0) {
91,033✔
864
    mError("trans:%d, failed to sync, errno:%s code:0x%x createTime:%" PRId64 " saved trans:%d", pTrans->id,
40!
865
           tstrerror(code), code, pTrans->createdTime, pMnode->syncMgmt.transId);
866
    sdbFreeRaw(pRaw);
40✔
867
    TAOS_RETURN(code);
40✔
868
  }
869

870
  sdbFreeRaw(pRaw);
90,993✔
871
  mInfo("trans:%d, sync finished, createTime:%" PRId64, pTrans->id, pTrans->createdTime);
90,993!
872
  TAOS_RETURN(code);
90,993✔
873
}
874

875
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
4,224✔
876
  if (conflict[0] == 0) return false;
4,224!
877
  if (strcasecmp(conflict, pTrans->dbname) == 0) return true;
4,224✔
878
  return false;
3,938✔
879
}
880

881
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
21,344✔
882
  if (conflict[0] == 0) return false;
21,344✔
883
  if (strcasecmp(conflict, pTrans->stbname) == 0) return true;
19,944✔
884
  return false;
19,921✔
885
}
886

887
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
25,568✔
888
  if (conflict) {
25,568✔
889
    mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with trans:%d db:%s stb:%s type:%d", pNew->id,
309!
890
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
891
    *globalConflict = true;
309✔
892
  } else {
893
    mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with trans:%d db:%s stb:%s type:%d", pNew->id, pNew->dbname,
25,259!
894
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
895
  }
896
}
25,568✔
897

898
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
120,505✔
899
  STrans *pTrans = NULL;
120,505✔
900
  void   *pIter = NULL;
120,505✔
901
  bool    conflict = false;
120,505✔
902

903
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
120,505✔
904

905
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
83,270✔
906
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
83,270!
907

908
  while (1) {
909
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
106,380✔
910
    if (pIter == NULL) break;
106,380✔
911

912
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
23,110✔
913

914
    if (pNew->conflict == TRN_CONFLICT_DB) {
23,110✔
915
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
3,750✔
916
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
3,750✔
917
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
2,672✔
918
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
2,672✔
919
      }
920
    }
921

922
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
23,110✔
923
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
19,359✔
924
      if (pTrans->conflict == TRN_CONFLICT_DB) {
19,359✔
925
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,552✔
926
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,552✔
927
      }
928
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
19,359✔
929
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
17,120✔
930
      }
931
    }
932

933
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
23,110!
934
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
×
935
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
×
936
        void *pGidIter = taosHashIterate(pNew->arbGroupIds, NULL);
×
937
        while (pGidIter != NULL) {
×
938
          int32_t groupId = *(int32_t *)pGidIter;
×
939
          if (taosHashGet(pTrans->arbGroupIds, &groupId, sizeof(int32_t)) != NULL) {
×
940
            taosHashCancelIterate(pNew->arbGroupIds, pGidIter);
×
941
            mndTransLogConflict(pNew, pTrans, true, &conflict);
×
942
            break;
×
943
          } else {
944
            mndTransLogConflict(pNew, pTrans, false, &conflict);
×
945
          }
946
          pGidIter = taosHashIterate(pNew->arbGroupIds, pGidIter);
×
947
        }
948
      }
949
    }
950

951
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
23,110!
952
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
×
953
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
954
      } else {
955
        mndTransLogConflict(pNew, pTrans, false, &conflict);
×
956
      }
957
    }
958

959
    sdbRelease(pMnode->pSdb, pTrans);
23,110✔
960
  }
961

962
  return conflict;
83,270✔
963
}
964

965
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
120,505✔
966
  int32_t code = 0;
120,505✔
967
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
120,505✔
968
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
73,982!
969
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
970
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
971
      TAOS_RETURN(code);
×
972
    }
973
  }
974

975
  if (mndCheckTransConflict(pMnode, pTrans)) {
120,505✔
976
    code = TSDB_CODE_MND_TRANS_CONFLICT;
347✔
977
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
347!
978
    TAOS_RETURN(code);
347✔
979
  }
980

981
  TAOS_RETURN(code);
120,158✔
982
}
983

984
int32_t mndTransCheckConflictWithCompact(SMnode *pMnode, STrans *pTrans) {
314✔
985
  int32_t      code = 0;
314✔
986
  void        *pIter = NULL;
314✔
987
  bool         conflict = false;
314✔
988
  SCompactObj *pCompact = NULL;
314✔
989

990
  while (1) {
4✔
991
    bool thisConflict = false;
318✔
992
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
318✔
993
    if (pIter == NULL) break;
318✔
994

995
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
4✔
996
      thisConflict = true;
2✔
997
    }
998
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4!
999
      if (strcasecmp(pTrans->dbname, pCompact->dbname) == 0) thisConflict = true;
2!
1000
    }
1001

1002
    if (thisConflict) {
4!
1003
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
4!
1004
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1005
      conflict = true;
4✔
1006
    } else {
1007
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with compact:%d db:%s", pTrans->id, pTrans->dbname,
×
1008
            pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1009
    }
1010
    sdbRelease(pMnode->pSdb, pCompact);
4✔
1011
  }
1012

1013
  if (conflict) {
314✔
1014
    code = TSDB_CODE_MND_TRANS_CONFLICT_COMPACT;
4✔
1015
    mError("trans:%d, failed to check tran conflict with compact since %s", pTrans->id, tstrerror(code));
4!
1016
    TAOS_RETURN(code);
4✔
1017
  }
1018

1019
  TAOS_RETURN(code);
310✔
1020
}
1021

1022
static bool mndTransActionsOfSameType(SArray *pActions) {
91,358✔
1023
  int32_t size = taosArrayGetSize(pActions);
91,358✔
1024
  ETrnAct lastActType = TRANS_ACTION_NULL;
91,358✔
1025
  bool    same = true;
91,358✔
1026
  for (int32_t i = 0; i < size; ++i) {
377,442✔
1027
    STransAction *pAction = taosArrayGet(pActions, i);
286,084✔
1028
    if (i > 0) {
286,084✔
1029
      if (lastActType != pAction->actionType) {
213,928!
1030
        same = false;
×
1031
        break;
×
1032
      }
1033
    }
1034
    lastActType = pAction->actionType;
286,084✔
1035
  }
1036
  return same;
91,358✔
1037
}
1038

1039
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
41,197✔
1040
  int32_t code = 0;
41,197✔
1041
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
41,197✔
1042
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
38,399!
1043
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1044
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1045
      TAOS_RETURN(code);
×
1046
    }
1047

1048
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
38,399✔
1049
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
11,762!
1050
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1051
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1052
        TAOS_RETURN(code);
×
1053
      }
1054
    }
1055
  }
1056

1057
  TAOS_RETURN(code);
41,197✔
1058
}
1059

1060
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
41,197✔
1061
  int32_t code = 0;
41,197✔
1062
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
41,197!
1063
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
×
1064
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
×
1065
    TAOS_RETURN(code);
×
1066
  }
1067
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
41,197!
1068
    code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1069
    mError("trans:%d, types of commit actions are not the same", pTrans->id);
×
1070
    TAOS_RETURN(code);
×
1071
  }
1072

1073
  TAOS_RETURN(code);
41,197✔
1074
}
1075

1076
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
41,198✔
1077
  int32_t code = 0;
41,198✔
1078
  if (pTrans == NULL) {
41,198!
1079
    return TSDB_CODE_INVALID_PARA;
×
1080
  }
1081

1082
  mInfo("trans:%d, action list:", pTrans->id);
41,198!
1083
  int32_t index = 0;
41,198✔
1084
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
61,064✔
1085
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
19,866✔
1086
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
19,866!
1087
          mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1088
  }
1089

1090
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
129,827✔
1091
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
88,629✔
1092
    mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
88,629!
1093
          mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1094
  }
1095

1096
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
230,468✔
1097
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
189,270✔
1098
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
189,270!
1099
          mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1100
  }
1101

1102
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
84,861✔
1103
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
43,663✔
1104
    if(pAction->actionType == TRANS_ACTION_MSG){
43,663✔
1105
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
30,971!
1106
            mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1107
    }
1108
    else{
1109
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
12,692!
1110
            mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1111
    }
1112
  }
1113

1114

1115
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
41,198✔
1116

1117
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
41,197!
1118

1119
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
41,197!
1120

1121
  mInfo("trans:%d, prepare transaction", pTrans->id);
41,197!
1122
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
41,197✔
1123
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
24!
1124
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
24✔
1125
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
24✔
1126
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
24✔
1127
    TAOS_RETURN(code);
24✔
1128
  }
1129
  mInfo("trans:%d, prepare finished", pTrans->id);
41,173!
1130

1131
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
41,173✔
1132
  if (pNew == NULL) {
41,173!
1133
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1134
    if (terrno != 0) code = terrno;
×
1135
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, tstrerror(code));
×
1136
    TAOS_RETURN(code);
×
1137
  }
1138

1139
  pNew->pRpcArray = pTrans->pRpcArray;
41,173✔
1140
  pNew->rpcRsp = pTrans->rpcRsp;
41,173✔
1141
  pNew->rpcRspLen = pTrans->rpcRspLen;
41,173✔
1142
  pNew->mTraceId = pTrans->mTraceId;
41,173✔
1143
  pTrans->pRpcArray = NULL;
41,173✔
1144
  pTrans->rpcRsp = NULL;
41,173✔
1145
  pTrans->rpcRspLen = 0;
41,173✔
1146

1147
  mndTransExecute(pMnode, pNew);
41,173✔
1148
  mndReleaseTrans(pMnode, pNew);
41,173✔
1149
  // TDOD change to TAOS_RETURN(code);
1150
  return 0;
41,173✔
1151
}
1152

1153
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
41,185✔
1154
  int32_t code = 0;
41,185✔
1155
  mInfo("trans:%d, commit transaction", pTrans->id);
41,185!
1156
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
41,185✔
1157
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
16!
1158
    TAOS_RETURN(code);
16✔
1159
  }
1160
  mInfo("trans:%d, commit finished", pTrans->id);
41,169!
1161
  TAOS_RETURN(code);
41,169✔
1162
}
1163

1164
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
3✔
1165
  int32_t code = 0;
3✔
1166
  mInfo("trans:%d, rollback transaction", pTrans->id);
3!
1167
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
3!
1168
    mError("trans:%d, failed to rollback since %s", pTrans->id, tstrerror(code));
×
1169
    TAOS_RETURN(code);
×
1170
  }
1171
  mInfo("trans:%d, rollback finished", pTrans->id);
3!
1172
  TAOS_RETURN(code);
3✔
1173
}
1174

1175
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
3✔
1176
  int32_t code = 0;
3✔
1177
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
3!
1178
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
3!
1179
    mError("trans:%d, failed to pre-finish since %s", pTrans->id, tstrerror(code));
×
1180
    TAOS_RETURN(code);
×
1181
  }
1182
  mInfo("trans:%d, pre-finish finished", pTrans->id);
3!
1183
  TAOS_RETURN(code);
3✔
1184
}
1185

1186
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
246,201✔
1187
  bool    sendRsp = false;
246,201✔
1188
  int32_t code = pTrans->code;
246,201✔
1189

1190
  if (pTrans->stage == TRN_STAGE_FINISH) {
246,201✔
1191
    sendRsp = true;
90,539✔
1192
  }
1193

1194
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
246,201✔
1195
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
62,784!
1196
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
17✔
1197
      sendRsp = true;
17✔
1198
    }
1199
  } else {
1200
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
183,417✔
1201
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
120,840!
1202
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1203
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1204
      } else {
1205
        if (pTrans->failedTimes > 6) sendRsp = true;
120,840✔
1206
      }
1207
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
120,840✔
1208
    }
1209
  }
1210

1211
  if (!sendRsp) {
246,201✔
1212
    return;
155,643✔
1213
  } else {
1214
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
90,558!
1215
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1216
  }
1217

1218
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
90,558!
1219
  taosWLockLatch(&pTrans->lockRpcArray);
90,558✔
1220
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
90,558✔
1221
  if (size <= 0) {
90,558✔
1222
    taosWUnLockLatch(&pTrans->lockRpcArray);
63,163✔
1223
    return;
63,163✔
1224
  }
1225

1226
  for (int32_t i = 0; i < size; ++i) {
54,790✔
1227
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
27,395✔
1228
    if (pInfo->handle != NULL) {
27,395✔
1229
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
25,912!
1230
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1231
      }
1232
      if (code == TSDB_CODE_SYN_TIMEOUT) {
25,912!
1233
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1234
      }
1235

1236
      if (i != 0 && code == 0) {
25,912!
1237
        code = TSDB_CODE_MNODE_NOT_FOUND;
×
1238
      }
1239
      mInfo("vgId:1, trans:%d, client:%d start to send rsp, code:0x%x stage:%s app:%p", pTrans->id, i, code,
25,912!
1240
            mndTransStr(pTrans->stage), pInfo->ahandle);
1241

1242
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
25,912✔
1243

1244
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
25,912✔
1245
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
3,655!
1246
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
3,655✔
1247
        if (pDb != NULL) {
3,655!
1248
          for (int32_t j = 0; j < 12; j++) {
4,466✔
1249
            bool ready = mndIsDbReady(pMnode, pDb);
4,461✔
1250
            if (!ready) {
4,461✔
1251
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
811!
1252
              taosMsleep(1000);
811✔
1253
            } else {
1254
              break;
3,650✔
1255
            }
1256
          }
1257
        }
1258
        mndReleaseDb(pMnode, pDb);
3,655✔
1259
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
22,257✔
1260
        void   *pCont = NULL;
6,877✔
1261
        int32_t contLen = 0;
6,877✔
1262
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
6,877✔
1263
          mndTransSetRpcRsp(pTrans, pCont, contLen);
6,874✔
1264
        }
1265
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
15,380✔
1266
        void   *pCont = NULL;
478✔
1267
        int32_t contLen = 0;
478✔
1268
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
478!
1269
          mndTransSetRpcRsp(pTrans, pCont, contLen);
478✔
1270
        }
1271
      }
1272

1273
      if (pTrans->rpcRspLen != 0) {
25,912✔
1274
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
11,920✔
1275
        if (rpcCont != NULL) {
11,920!
1276
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
11,920✔
1277
          rspMsg.pCont = rpcCont;
11,920✔
1278
          rspMsg.contLen = pTrans->rpcRspLen;
11,920✔
1279
        }
1280
      }
1281

1282
      tmsgSendRsp(&rspMsg);
25,912✔
1283

1284
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
25,912!
1285
            mndTransStr(pTrans->stage), pInfo->ahandle);
1286
    }
1287
  }
1288
  taosArrayClear(pTrans->pRpcArray);
27,395✔
1289
  taosWUnLockLatch(&pTrans->lockRpcArray);
27,395✔
1290
}
1291

1292
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
92,084✔
1293
  int32_t code = 0;
92,084✔
1294
  SMnode *pMnode = pRsp->info.node;
92,084✔
1295
  int64_t signature = (int64_t)(pRsp->info.ahandle);
92,084✔
1296
  int32_t transId = (int32_t)(signature >> 32);
92,084✔
1297
  int32_t action = (int32_t)((signature << 32) >> 32);
92,084✔
1298

1299
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
92,084✔
1300
  if (pTrans == NULL) {
92,084!
1301
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1302
    if (terrno != 0) code = terrno;
×
1303
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1304
    goto _OVER;
×
1305
  }
1306

1307
  SArray *pArray = NULL;
92,084✔
1308
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
92,084✔
1309
    pArray = pTrans->redoActions;
92,072✔
1310
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
12!
1311
    pArray = pTrans->undoActions;
12✔
1312
  } else {
1313
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1314
    goto _OVER;
×
1315
  }
1316

1317
  if (pArray == NULL) {
92,084!
1318
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1319
    goto _OVER;
×
1320
  }
1321

1322
  int32_t actionNum = taosArrayGetSize(pArray);
92,084✔
1323
  if (action < 0 || action >= actionNum) {
92,084!
1324
    mError("trans:%d, invalid action:%d", transId, action);
×
1325
    goto _OVER;
×
1326
  }
1327

1328
  STransAction *pAction = taosArrayGet(pArray, action);
92,084✔
1329
  if (pAction != NULL) {
92,084!
1330
    pAction->msgReceived = 1;
92,084✔
1331
    pAction->errCode = pRsp->code;
92,084✔
1332
    pAction->endTime = taosGetTimestampMs();
92,084✔
1333

1334
    // pTrans->lastErrorNo = pRsp->code;
1335
    mndSetTransLastAction(pTrans, pAction);
92,084✔
1336

1337
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
92,084!
1338
          mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1339
          tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1340
  } else {
1341
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1342
  }
1343

1344
  mndTransExecute(pMnode, pTrans);
92,084✔
1345

1346
_OVER:
92,084✔
1347
  mndReleaseTrans(pMnode, pTrans);
92,084✔
1348
  TAOS_RETURN(code);
92,084✔
1349
}
1350

1351
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
5,013✔
1352
  pAction->rawWritten = 0;
5,013✔
1353
  pAction->msgSent = 0;
5,013✔
1354
  pAction->msgReceived = 0;
5,013✔
1355
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
5,013!
1356
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
5,013✔
1357
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
22✔
1358
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
22!
1359
          pAction->id, pAction->epSet.inUse);
1360
  } else {
1361
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,991!
1362
  }
1363
  pAction->errCode = 0;
5,013✔
1364
}
5,013✔
1365

1366
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
3✔
1367
  int32_t numOfActions = taosArrayGetSize(pArray);
3✔
1368

1369
  for (int32_t action = 0; action < numOfActions; ++action) {
15✔
1370
    STransAction *pAction = taosArrayGet(pArray, action);
12✔
1371
    if (pAction->msgSent && pAction->msgReceived &&
12!
1372
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
12!
1373
      continue;
9✔
1374
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
3!
1375

1376
    mndTransResetAction(pMnode, pTrans, pAction);
3✔
1377
  }
1378
}
3✔
1379

1380
// execute in sync context
1381
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
426,918✔
1382
  if (pAction->rawWritten) return 0;
426,918✔
1383
  if (topHalf) {
236,039!
1384
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
×
1385
  }
1386

1387
  if (pAction->pRaw->type >= SDB_MAX) {
236,039!
1388
    pAction->rawWritten = true;
×
1389
    pAction->errCode = 0;
×
1390
    mndSetTransLastAction(pTrans, pAction);
×
1391
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
1392
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1393
  }
1394

1395
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
236,039✔
1396
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
236,039!
1397
    pAction->rawWritten = true;
236,039✔
1398
    pAction->errCode = 0;
236,039✔
1399
    code = 0;
236,039✔
1400
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
236,039!
1401
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1402

1403
    mndSetTransLastAction(pTrans, pAction);
236,039✔
1404
  } else {
1405
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1406
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1407
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1408
    mndSetTransLastAction(pTrans, pAction);
×
1409
  }
1410

1411
  TAOS_RETURN(code);
236,039✔
1412
}
1413

1414
// execute in trans context
1415
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
621,507✔
1416
  if (pAction->msgSent) return 0;
621,507✔
1417
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
130,057✔
1418
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
37,907✔
1419
  }
1420

1421
  int64_t signature = pTrans->id;
92,150✔
1422
  signature = (signature << 32);
92,150✔
1423
  signature += pAction->id;
92,150✔
1424

1425
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
92,150✔
1426
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
92,150✔
1427
  if (rpcMsg.pCont == NULL) {
92,150!
1428
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1429
    return -1;
×
1430
  }
1431
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
92,150✔
1432
  rpcMsg.info.notFreeAhandle = 1;
92,150✔
1433

1434
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
92,150✔
1435

1436
  char    detail[1024] = {0};
92,150✔
1437
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
92,150!
1438
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
92,150✔
1439
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
189,764✔
1440
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
97,614✔
1441
                     pAction->epSet.eps[i].port);
97,614✔
1442
  }
1443

1444
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
92,150✔
1445
  if (code == 0) {
92,150!
1446
    pAction->msgSent = 1;
92,150✔
1447
    // pAction->msgReceived = 0;
1448
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
92,150✔
1449
    pAction->startTime = taosGetTimestampMs();
92,150✔
1450
    pAction->endTime = 0;
92,150✔
1451
    mInfo("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
92,150!
1452

1453
    mndSetTransLastAction(pTrans, pAction);
92,150✔
1454
  } else {
1455
    pAction->msgSent = 0;
×
1456
    pAction->msgReceived = 0;
×
1457
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1458
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
×
1459
           detail);
1460

1461
    mndSetTransLastAction(pTrans, pAction);
×
1462
  }
1463

1464
  TAOS_RETURN(code);
92,150✔
1465
}
1466

1467
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1468
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1469
  pAction->rawWritten = 0;
×
1470
  pAction->errCode = 0;
×
1471
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1472

1473
  mndSetTransLastAction(pTrans, pAction);
×
1474
  return 0;
×
1475
}
1476

1477
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
1,048,425✔
1478
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,048,425✔
1479
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
426,918✔
1480
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
621,507!
1481
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf);
621,507✔
1482
  } else {
1483
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1484
  }
1485
}
1486

1487
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
226,368✔
1488
  int32_t numOfActions = taosArrayGetSize(pArray);
226,368✔
1489
  int32_t code = 0;
226,368✔
1490

1491
  for (int32_t action = 0; action < numOfActions; ++action) {
1,181,538✔
1492
    STransAction *pAction = taosArrayGet(pArray, action);
985,374✔
1493
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
985,374✔
1494
    if (code != 0) {
985,374✔
1495
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
30,204!
1496
            numOfActions);
1497
      break;
30,204✔
1498
    }
1499
  }
1500

1501
  return code;
226,368✔
1502
}
1503

1504
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
256,859✔
1505
  int32_t numOfActions = taosArrayGetSize(pArray);
256,859✔
1506
  int32_t code = 0;
256,859✔
1507
  if (numOfActions == 0) return 0;
256,859✔
1508

1509
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf)) != 0) {
226,368✔
1510
    return code;
30,204✔
1511
  }
1512

1513
  int32_t       numOfExecuted = 0;
196,164✔
1514
  int32_t       errCode = 0;
196,164✔
1515
  STransAction *pErrAction = NULL;
196,164✔
1516
  for (int32_t action = 0; action < numOfActions; ++action) {
1,151,334✔
1517
    STransAction *pAction = taosArrayGet(pArray, action);
955,170✔
1518
    if (pAction->msgReceived || pAction->rawWritten) {
955,170✔
1519
      numOfExecuted++;
675,884✔
1520
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
675,884✔
1521
        errCode = pAction->errCode;
4✔
1522
        pErrAction = pAction;
4✔
1523
      }
1524
    } else {
1525
      pErrAction = pAction;
279,286✔
1526
    }
1527
  }
1528

1529
  mndSetTransLastAction(pTrans, pErrAction);
196,164✔
1530

1531
  if (numOfExecuted == numOfActions) {
196,164✔
1532
    if (errCode == 0) {
114,645✔
1533
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
114,642!
1534
      return 0;
114,642✔
1535
    } else {
1536
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
3!
1537
      mndTransResetActions(pMnode, pTrans, pArray);
3✔
1538
      terrno = errCode;
3✔
1539
      return errCode;
3✔
1540
    }
1541
  } else {
1542
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
81,519!
1543

1544
    for (int32_t action = 0; action < numOfActions; ++action) {
558,187✔
1545
      STransAction *pAction = taosArrayGet(pArray, action);
476,668✔
1546
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
476,668✔
1547
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1548
             pAction->acceptableCode, pAction->retryCode);
1549
      if (pAction->msgSent) {
476,668!
1550
        if (pAction->msgReceived) {
476,668✔
1551
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
197,382✔
1552
            mndTransResetAction(pMnode, pTrans, pAction);
1✔
1553
            mInfo("trans:%d, %s:%d reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1!
1554
          }
1555
        }
1556
      }
1557
    }
1558
    return TSDB_CODE_ACTION_IN_PROGRESS;
81,519✔
1559
  }
1560
}
1561

1562
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
166,311✔
1563
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf);
166,311✔
1564
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
166,311✔
1565
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
3!
1566
           mndStrExecutionContext(topHalf));
1567
  }
1568
  return code;
166,311✔
1569
}
1570

1571
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
20✔
1572
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf);
20✔
1573
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
20!
1574
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1575
           mndStrExecutionContext(topHalf));
1576
  }
1577
  return code;
20✔
1578
}
1579

1580
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
90,528✔
1581
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf);
90,528✔
1582
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
90,528!
1583
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1584
           mndStrExecutionContext(topHalf));
1585
  }
1586
  return code;
90,528✔
1587
}
1588

1589
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
30,509✔
1590
  int32_t code = 0;
30,509✔
1591
  int32_t numOfActions = taosArrayGetSize(pActions);
30,509✔
1592
  if (numOfActions == 0) return code;
30,509✔
1593

1594
  if (pTrans->actionPos >= numOfActions) {
30,505✔
1595
    return code;
3,144✔
1596
  }
1597

1598
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
27,361!
1599
        pTrans->actionPos, mndTransStr(pTrans->stage));
1600

1601
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
40,996✔
1602
    STransAction *pAction = taosArrayGet(pActions, action);
38,201✔
1603

1604
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", 
38,201!
1605
          pTrans->id, pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent,
1606
          pAction->msgReceived);
1607

1608
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
38,201✔
1609
    if (code == 0) {
38,201✔
1610
      if (pAction->msgSent) {
30,498✔
1611
        if (pAction->msgReceived) {
26,916✔
1612
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
12,020✔
1613
            code = pAction->errCode;
5,009✔
1614
            mndTransResetAction(pMnode, pTrans, pAction);
5,009✔
1615
          } else {
1616
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
7,011!
1617
          }
1618
        } else {
1619
          code = TSDB_CODE_ACTION_IN_PROGRESS;
14,896✔
1620
        }
1621
      } else if (pAction->rawWritten) {
3,582!
1622
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,582!
1623
          code = pAction->errCode;
×
1624
        } else {
1625
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,582!
1626
        }
1627
      } else {
1628
      }
1629
    }
1630

1631
    if (code == 0) {
38,201✔
1632
      pTrans->failedTimes = 0;
10,593✔
1633
    }
1634
    mndSetTransLastAction(pTrans, pAction);
38,201✔
1635

1636
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
38,201✔
1637
      pTrans->lastErrorNo = code;
9,651✔
1638
      pTrans->code = code;
9,651✔
1639
      mInfo("trans:%d, %s:%d, cannot execute next action in %s, code:%s", pTrans->id, mndTransStr(pAction->stage),
9,651!
1640
            action, mndStrExecutionContext(topHalf), tstrerror(code));
1641
      break;
9,651✔
1642
    }
1643

1644
    if (code == 0) {
28,550✔
1645
      pTrans->code = 0;
8,645✔
1646
      pTrans->actionPos++;
8,645✔
1647
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
8,645!
1648
            pAction->id);
1649
      (void)taosThreadMutexUnlock(&pTrans->mutex);
8,645✔
1650
      code = mndTransSync(pMnode, pTrans);
8,645✔
1651
      (void)taosThreadMutexLock(&pTrans->mutex);
8,645✔
1652
      if (code != 0) {
8,645!
1653
        pTrans->actionPos--;
×
1654
        pTrans->code = terrno;
×
1655
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1656
               mndTransStr(pAction->stage), pAction->id, terrstr());
1657
        break;
×
1658
      }
1659
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
19,905✔
1660
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
14,896!
1661
      break;
14,896✔
1662
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
5,009!
1663
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
41✔
1664
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,990!
1665
            code, tstrerror(code));
1666
      pTrans->lastErrorNo = code;
4,990✔
1667
      taosMsleep(300);
4,990✔
1668
      action--;
4,990✔
1669
      continue;
4,990✔
1670
    } else {
1671
      terrno = code;
19✔
1672
      pTrans->lastErrorNo = code;
19✔
1673
      pTrans->code = code;
19✔
1674
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
19!
1675
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1676
      break;
19✔
1677
    }
1678
  }
1679

1680
  return code;
27,361✔
1681
}
1682

1683
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
30,509✔
1684
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
30,509✔
1685
  (void)taosThreadMutexLock(&pTrans->mutex);
30,509✔
1686
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
30,509!
1687
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
30,509✔
1688
  }
1689
  (void)taosThreadMutexUnlock(&pTrans->mutex);
30,509✔
1690
  return code;
30,509✔
1691
}
1692

1693
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
1694
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1695
  (void)taosThreadMutexLock(&pTrans->mutex);
×
1696
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1697
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
1698
  }
1699
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
1700
  return code;
×
1701
}
1702

1703
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
49,403✔
1704
  bool    continueExec = true;
49,403✔
1705
  int32_t code = 0;
49,403✔
1706
  terrno = 0;
49,403✔
1707

1708
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
49,403✔
1709
  if (numOfActions == 0) goto _OVER;
49,403✔
1710

1711
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
15,811!
1712

1713
  for (int32_t action = 0; action < numOfActions; ++action) {
40,661✔
1714
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
24,850✔
1715
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
24,850✔
1716
    if (code != 0) {
24,850!
1717
      terrno = code;
×
1718
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
1719
             numOfActions, tstrerror(code));
1720
      return false;
×
1721
    }
1722
  }
1723

1724
_OVER:
15,811✔
1725
  pTrans->stage = TRN_STAGE_REDO_ACTION;
49,403✔
1726
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
49,403!
1727
  return continueExec;
49,403✔
1728
}
1729

1730
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
196,820✔
1731
  bool    continueExec = true;
196,820✔
1732
  int32_t code = 0;
196,820✔
1733
  terrno = 0;
196,820✔
1734

1735
  if (pTrans->exec == TRN_EXEC_SERIAL) {
196,820✔
1736
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
30,509✔
1737
  } else {
1738
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf);
166,311✔
1739
  }
1740

1741
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
196,820!
1742
    pTrans->lastErrorNo = code;
×
1743
    pTrans->code = code;
×
1744
    mInfo(
×
1745
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
1746
        "continueExec:%d, code:%s",
1747
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1748
    taosMsleep(100);
×
1749
    return true;
×
1750
  } else {
1751
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
196,820✔
1752
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
59,210!
1753
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1754
      return false;
59,210✔
1755
    }
1756
  }
1757
  terrno = code;
137,610✔
1758

1759
  if (code == 0) {
137,610✔
1760
    pTrans->code = 0;
41,185✔
1761
    pTrans->stage = TRN_STAGE_COMMIT;
41,185✔
1762
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
41,185!
1763
    continueExec = true;
41,185✔
1764
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
96,425!
1765
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
96,403!
1766
    continueExec = false;
96,403✔
1767
  } else {
1768
    pTrans->failedTimes++;
22✔
1769
    pTrans->code = terrno;
22✔
1770
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
22✔
1771
      if (pTrans->lastAction != 0) {
3✔
1772
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
1773
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
1774
          if (pTrans->failedTimes < 6) {
×
1775
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
1776
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
1777
            taosMsleep(1000);
×
1778
            continueExec = true;
×
1779
            return true;
×
1780
          }
1781
        }
1782
      }
1783

1784
      pTrans->stage = TRN_STAGE_ROLLBACK;
3✔
1785
      pTrans->actionPos = 0;
3✔
1786
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
3!
1787
             pTrans->actionPos);
1788
      continueExec = true;
3✔
1789
    } else {
1790
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
19!
1791
      continueExec = false;
19✔
1792
    }
1793
  }
1794

1795
  return continueExec;
137,610✔
1796
}
1797

1798
// execute in trans context
1799
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
41,185✔
1800
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
41,185!
1801

1802
  bool    continueExec = true;
41,185✔
1803
  int32_t code = mndTransCommit(pMnode, pTrans);
41,185✔
1804

1805
  if (code == 0) {
41,185✔
1806
    pTrans->code = 0;
41,169✔
1807
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
41,169✔
1808
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
41,169!
1809
    continueExec = true;
41,169✔
1810
  } else {
1811
    pTrans->code = terrno;
16✔
1812
    pTrans->failedTimes++;
16✔
1813
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
16!
1814
    continueExec = false;
16✔
1815
  }
1816

1817
  return continueExec;
41,185✔
1818
}
1819

1820
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
90,528✔
1821
  bool    continueExec = true;
90,528✔
1822
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
90,528✔
1823

1824
  if (code == 0) {
90,528!
1825
    pTrans->code = 0;
90,528✔
1826
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
90,528✔
1827
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
90,528!
1828
    continueExec = true;
90,528✔
1829
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
1830
    pTrans->code = 0;
×
1831
    pTrans->stage = TRN_STAGE_COMMIT;
×
1832
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
1833
    continueExec = true;
×
1834
  } else {
1835
    pTrans->code = terrno;
×
1836
    pTrans->failedTimes++;
×
1837
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1838
    continueExec = false;
×
1839
  }
1840

1841
  return continueExec;
90,528✔
1842
}
1843

1844
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
20✔
1845
  bool    continueExec = true;
20✔
1846
  int32_t code = 0;
20✔
1847

1848
  if (pTrans->exec == TRN_EXEC_SERIAL) {
20!
1849
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
1850
  } else {
1851
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf);
20✔
1852
  }
1853

1854
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
20✔
1855
  terrno = code;
15✔
1856

1857
  if (code == 0) {
15✔
1858
    pTrans->stage = TRN_STAGE_PRE_FINISH;
3✔
1859
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
3!
1860
    continueExec = true;
3✔
1861
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
12!
1862
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
12!
1863
    continueExec = false;
12✔
1864
  } else {
1865
    pTrans->failedTimes++;
×
1866
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1867
    continueExec = false;
×
1868
  }
1869

1870
  return continueExec;
15✔
1871
}
1872

1873
// in trans context
1874
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
3✔
1875
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
3!
1876

1877
  bool    continueExec = true;
3✔
1878
  int32_t code = mndTransRollback(pMnode, pTrans);
3✔
1879

1880
  if (code == 0) {
3!
1881
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
3✔
1882
    continueExec = true;
3✔
1883
  } else {
1884
    pTrans->failedTimes++;
×
1885
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1886
    continueExec = false;
×
1887
  }
1888

1889
  return continueExec;
3✔
1890
}
1891

1892
// excute in trans context
1893
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
3✔
1894
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
3!
1895

1896
  bool    continueExec = true;
3✔
1897
  int32_t code = mndTransPreFinish(pMnode, pTrans);
3✔
1898

1899
  if (code == 0) {
3!
1900
    pTrans->stage = TRN_STAGE_FINISH;
3✔
1901
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
3!
1902
    continueExec = true;
3✔
1903
  } else {
1904
    pTrans->failedTimes++;
×
1905
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1906
    continueExec = false;
×
1907
  }
1908

1909
  return continueExec;
3✔
1910
}
1911

1912
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
90,536✔
1913
  bool continueExec = false;
90,536✔
1914
  if (topHalf) return continueExec;
90,536✔
1915

1916
  SSdbRaw *pRaw = mndTransEncode(pTrans);
49,364✔
1917
  if (pRaw == NULL) {
49,364!
1918
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
1919
    return false;
×
1920
  }
1921
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
49,364!
1922

1923
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
49,364✔
1924
  if (code != 0) {
49,364!
1925
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
1926
  }
1927

1928
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
49,364!
1929
        pTrans->failedTimes, pTrans->createdTime);
1930
  return continueExec;
49,364✔
1931
}
1932

1933
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf) {
246,201✔
1934
  bool continueExec = true;
246,201✔
1935

1936
  while (continueExec) {
665,296✔
1937
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64 "", pTrans->id,
419,095!
1938
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1939
    pTrans->lastExecTime = taosGetTimestampMs();
419,095✔
1940
    switch (pTrans->stage) {
419,095!
1941
      case TRN_STAGE_PREPARE:
×
1942
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1943
        break;
×
1944
      case TRN_STAGE_REDO_ACTION:
196,820✔
1945
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf);
196,820✔
1946
        break;
196,820✔
1947
      case TRN_STAGE_COMMIT:
41,185✔
1948
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
41,185✔
1949
        break;
41,185✔
1950
      case TRN_STAGE_COMMIT_ACTION:
90,528✔
1951
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
90,528✔
1952
        break;
90,528✔
1953
      case TRN_STAGE_ROLLBACK:
3✔
1954
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
3✔
1955
        break;
3✔
1956
      case TRN_STAGE_UNDO_ACTION:
20✔
1957
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf);
20✔
1958
        break;
20✔
1959
      case TRN_STAGE_PRE_FINISH:
3✔
1960
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
3✔
1961
        break;
3✔
1962
      case TRN_STAGE_FINISH:
90,536✔
1963
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
90,536✔
1964
        break;
90,536✔
1965
      default:
×
1966
        continueExec = false;
×
1967
        break;
×
1968
    }
1969
  }
1970

1971
  mndTransSendRpcRsp(pMnode, pTrans);
246,201✔
1972
}
246,201✔
1973

1974
// start trans, pullup, receive rsp, kill
1975
void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
137,814✔
1976
  bool topHalf = true;
137,814✔
1977
  mndTransExecuteImp(pMnode, pTrans, topHalf);
137,814✔
1978
}
137,814✔
1979

1980
// update trans
1981
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
108,387✔
1982
  bool topHalf = false;
108,387✔
1983
  mndTransExecuteImp(pMnode, pTrans, topHalf);
108,387✔
1984
}
108,387✔
1985

1986
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
32,061✔
1987
  mTrace("start to process trans timer");
32,061✔
1988
  mndTransPullup(pReq->info.node);
32,061✔
1989
  return 0;
32,061✔
1990
}
1991

1992
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
1993
  SArray *pArray = NULL;
×
1994
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
1995
    pArray = pTrans->redoActions;
×
1996
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1997
    pArray = pTrans->undoActions;
×
1998
  } else {
1999
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2000
  }
2001

2002
  if(pTrans->ableToBeKilled == false){
×
2003
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2004
  }
2005
  
2006
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
×
2007
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
2008
      STransAction *pAction = taosArrayGet(pArray, i);
×
2009
      mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
2010
            mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
2011
      pAction->msgSent = 1;
×
2012
      pAction->msgReceived = 1;
×
2013
      pAction->errCode = 0;
×
2014
    }
2015
  }
2016
  else if(pTrans->killMode == TRN_KILL_MODE_INTERUPT){
×
2017
    pTrans->stage = TRN_STAGE_PRE_FINISH;
×
2018
  }
2019
  else{
2020
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2021
  }
2022

2023
  mndTransExecute(pMnode, pTrans);
×
2024
  return 0;
×
2025
}
2026

2027
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1✔
2028
  SMnode       *pMnode = pReq->info.node;
1✔
2029
  SKillTransReq killReq = {0};
1✔
2030
  int32_t       code = -1;
1✔
2031
  STrans       *pTrans = NULL;
1✔
2032

2033
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
2034
    code = TSDB_CODE_INVALID_MSG;
×
2035
    goto _OVER;
×
2036
  }
2037

2038
  mInfo("trans:%d, start to kill", killReq.transId);
1!
2039
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1!
2040
    goto _OVER;
1✔
2041
  }
2042

2043
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
2044
  if (pTrans == NULL) {
×
2045
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
2046
    if (terrno != 0) code = terrno;
×
2047
    goto _OVER;
×
2048
  }
2049

2050
  code = mndKillTrans(pMnode, pTrans);
×
2051

2052
_OVER:
1✔
2053
  if (code != 0) {
1!
2054
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1!
2055
  }
2056

2057
  mndReleaseTrans(pMnode, pTrans);
1✔
2058
  TAOS_RETURN(code);
1✔
2059
}
2060

2061
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
264✔
2062

2063
void mndTransPullup(SMnode *pMnode) {
32,547✔
2064
  SSdb   *pSdb = pMnode->pSdb;
32,547✔
2065
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
32,547✔
2066
  if (pArray == NULL) return;
32,547!
2067

2068
  void *pIter = NULL;
32,547✔
2069
  while (1) {
4,557✔
2070
    STrans *pTrans = NULL;
37,104✔
2071
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
37,104✔
2072
    if (pIter == NULL) break;
37,104✔
2073
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
9,114!
2074
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2075
    }
2076
    sdbRelease(pSdb, pTrans);
4,557✔
2077
  }
2078

2079
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
32,547✔
2080

2081
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
37,104✔
2082
    int32_t *pTransId = taosArrayGet(pArray, i);
4,557✔
2083
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
4,557✔
2084
    if (pTrans != NULL) {
4,557!
2085
      mndTransExecute(pMnode, pTrans);
4,557✔
2086
    }
2087
    mndReleaseTrans(pMnode, pTrans);
4,557✔
2088
  }
2089
  taosArrayDestroy(pArray);
32,547✔
2090
}
2091

2092
static char *formatTimestamp(char *buf, int64_t val, int precision) {
11,362✔
2093
  time_t tt;
2094
  if (precision == TSDB_TIME_PRECISION_MICRO) {
11,362!
2095
    tt = (time_t)(val / 1000000);
×
2096
  }
2097
  if (precision == TSDB_TIME_PRECISION_NANO) {
11,362!
2098
    tt = (time_t)(val / 1000000000);
×
2099
  } else {
2100
    tt = (time_t)(val / 1000);
11,362✔
2101
  }
2102

2103
  struct tm tm;
2104
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
11,362!
2105
    mError("failed to get local time");
×
2106
    return NULL;
×
2107
  }
2108
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
11,362✔
2109

2110
  if (precision == TSDB_TIME_PRECISION_MICRO) {
11,361!
2111
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2112
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
11,361!
2113
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2114
  } else {
2115
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
11,361✔
2116
  }
2117

2118
  return buf;
11,361✔
2119
}
2120

2121
static void mndTransLogAction(STrans *pTrans) {
173✔
2122
  char    detail[512] = {0};
173✔
2123
  int32_t len = 0;
173✔
2124
  int32_t index = 0;
173✔
2125

2126
  if (pTrans->stage == TRN_STAGE_PREPARE) {
173!
2127
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2128
      len = 0;
×
2129
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2130
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2131
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2132
                      sdbStatusName(pAction->pRaw->status));
×
2133
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2134
    }
2135
  }
2136

2137
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
173!
2138
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
7,209✔
2139
      len = 0;
7,036✔
2140
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
7,036✔
2141
      if (pAction->actionType == TRANS_ACTION_MSG) {
7,036✔
2142
        char bufStart[40] = {0};
5,681✔
2143
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
5,681✔
2144

2145
        char endStart[40] = {0};
5,681✔
2146
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
5,681✔
2147
        len += snprintf(detail + len, sizeof(detail) - len,
11,362!
2148
                        "action:%d, %s:%d msgType:%s,"
2149
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2150
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
5,681✔
2151
                        pAction->msgReceived, bufStart, endStart);
5,681✔
2152

2153
        SEpSet epset = pAction->epSet;
5,681✔
2154
        if (epset.numOfEps > 0) {
5,681!
2155
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
5,681✔
2156
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
13,158✔
2157
            len +=
7,477✔
2158
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
7,477✔
2159
          }
2160
        }
2161

2162
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
5,681✔
2163
                        tstrerror(pAction->errCode));
2164
      } else {
2165
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
1,355✔
2166
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,355✔
2167
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,355✔
2168
      }
2169
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
7,036✔
2170
    }
2171
  }
2172

2173
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
172!
2174
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
×
2175
      len = 0;
×
2176
      STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
×
2177
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2178
                      mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type),
×
2179
                      sdbStatusName(pAction->pRaw->status));
×
2180
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2181
    }
2182

2183
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
×
2184
      len = 0;
×
2185
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
2186
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
2187
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
2188
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2189
        ;
2190
      } else {
2191
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2192
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2193
                        sdbStatusName(pAction->pRaw->status));
×
2194
      }
2195
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2196
    }
2197
  }
2198
}
172✔
2199

2200
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
7,466✔
2201
  SMnode *pMnode = pReq->info.node;
7,466✔
2202
  SSdb   *pSdb = pMnode->pSdb;
7,466✔
2203
  int32_t numOfRows = 0;
7,466✔
2204
  STrans *pTrans = NULL;
7,466✔
2205
  int32_t cols = 0;
7,466✔
2206
  int32_t code = 0;
7,466✔
2207
  int32_t lino = 0;
7,466✔
2208

2209
  while (numOfRows < rows) {
7,642!
2210
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
7,642✔
2211
    if (pShow->pIter == NULL) break;
7,649✔
2212

2213
    cols = 0;
172✔
2214

2215
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
172✔
2216
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
173!
2217

2218
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
172✔
2219
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
172!
2220
                        _OVER);
2221

2222
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
172✔
2223
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
172✔
2224
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
172✔
2225
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
173!
2226

2227
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
173✔
2228
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
173✔
2229
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
173✔
2230
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
173!
2231

2232
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
173✔
2233
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
173✔
2234
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
173✔
2235
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
173!
2236

2237
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
173✔
2238
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
173✔
2239
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
173✔
2240
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
173!
2241

2242
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
173!
2243
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
173✔
2244
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
173✔
2245
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
173✔
2246
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
173!
2247

2248
    /*
2249
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2250
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2251
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2252
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2253
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2254
    */
2255

2256
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
173✔
2257
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
173!
2258
                        _OVER);
2259

2260
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
173✔
2261
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
173!
2262
                        _OVER);
2263

2264
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
173✔
2265
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
173✔
2266
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
519✔
2267
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
173✔
2268
    SEpSet  epset = pTrans->lastEpset;
173✔
2269
    if (epset.numOfEps > 0) {
173✔
2270
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
316!
2271
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
316✔
2272
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
427✔
2273
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
269✔
2274
      }
2275
    }
2276
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
173✔
2277
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
173✔
2278
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
173!
2279

2280
    mndTransLogAction(pTrans);
173✔
2281

2282
    numOfRows++;
173✔
2283
    sdbRelease(pSdb, pTrans);
173✔
2284
  }
2285

2286
_OVER:
×
2287
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
7,477!
2288
  pShow->numOfRows += numOfRows;
7,477✔
2289
  return numOfRows;
7,477✔
2290
}
2291

2292
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
×
2293
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2294
  int32_t code = 0;
×
2295
  int32_t lino = 0;
×
2296
  int32_t len = 0;
×
2297

2298
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
2299
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&transactionId, false), &lino, _OVER);
×
2300

2301
  char action[30 + 1] = {0};
×
2302
  if (curActionId == pAction->id) {
×
2303
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
×
2304
                    mndTransTypeStr(pAction->actionType));
2305
  } else {
2306
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
×
2307
                    mndTransTypeStr(pAction->actionType));
2308
  }
2309
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
×
2310
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
×
2311
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
2312
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
×
2313
_OVER:
×
2314
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2315
  return code;
×
2316
}
2317

2318
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
×
2319
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2320
  int32_t code = 0;
×
2321
  int32_t lino = 0;
×
2322
  int32_t len = 0;
×
2323
  int32_t cols = 0;
×
2324

2325
  cols = 0;
×
2326

2327
  if (mndShowTransCommonColumns(pShow, pBlock, pAction, transactionId, curActionId, numOfRows, &cols) != 0) return;
×
2328

2329
  if (pAction->actionType == TRANS_ACTION_MSG) {
×
2330
    int32_t len = 0;
×
2331

2332
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2333
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
×
2334
                    pAction->msgSent, pAction->msgReceived);
×
2335
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2336
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2337
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2338
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2339

2340
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2341
    len = 0;
×
2342
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
×
2343
                    tstrerror(pAction->errCode));
2344
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2345
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2346
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2347
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2348

2349
    char target[TSDB_TRANS_TARGET_LEN] = {0};
×
2350
    len = 0;
×
2351
    SEpSet epset = pAction->epSet;
×
2352
    if (epset.numOfEps > 0) {
×
2353
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
×
2354
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
×
2355
      }
2356
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
×
2357
    }
2358
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2359
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2360
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2361
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2362

2363
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2364
    len = 0;
×
2365
    char bufStart[40] = {0};
×
2366
    if (pAction->startTime > 0) (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
×
2367
    char bufEnd[40] = {0};
×
2368
    if (pAction->endTime > 0) (void)formatTimestamp(bufEnd, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
×
2369
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
×
2370
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2371
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2372
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2373
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2374

2375
  } else {
2376
    int32_t len = 0;
×
2377

2378
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2379
    if (pAction->pRaw->type == SDB_VGROUP) {
×
2380
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
×
2381
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
×
2382
      len += snprintf(objType + len, sizeof(objType) - len, "%s(%d)", sdbTableName(pAction->pRaw->type), pVgroup->vgId);
×
2383
      taosMemoryFreeClear(pRow);
×
2384
    } else {
2385
      strcpy(objType, sdbTableName(pAction->pRaw->type));
×
2386
    }
2387
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2388
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2389
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2390
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2391

2392
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2393
    len = 0;
×
2394
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
×
2395
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2396
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2397
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2398
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2399

2400
    char target[TSDB_TRANS_TARGET_LEN] = "";
×
2401
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2402
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2403
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2404
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2405

2406
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2407
    len = 0;
×
2408
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
×
2409
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2410
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2411
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2412
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2413
  }
2414

2415
_OVER:
×
2416
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2417
}
2418

2419
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
×
2420
  if (stage == TRN_STAGE_PREPARE) {
×
2421
    return pTrans->prepareActions;
×
2422
  }
2423
  if (stage == TRN_STAGE_REDO_ACTION) {
×
2424
    return pTrans->redoActions;
×
2425
  }
2426
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
2427
    return pTrans->commitActions;
×
2428
  }
2429
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
2430
    return pTrans->undoActions;
×
2431
  }
2432
  return NULL;
×
2433
}
2434

2435
typedef struct STransDetailIter {
2436
  void     *pIter;
2437
  STrans   *pTrans;
2438
  ETrnStage stage;
2439
  int32_t   num;
2440
} STransDetailIter;
2441

2442
static void mndTransShowActions(SSdb *pSdb, STransDetailIter *pShowIter, SShowObj *pShow, SSDataBlock *pBlock,
×
2443
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
2444
  int32_t actionNum = taosArrayGetSize(pActions);
×
2445
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
×
2446

2447
  for (int32_t i = start; i < actionNum; ++i) {
×
2448
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
×
2449
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
×
2450
    (*numOfRows)++;
×
2451
    if (*numOfRows >= rows) break;
×
2452
  }
2453

2454
  if (*numOfRows == end) {
×
2455
    sdbRelease(pSdb, pShowIter->pTrans);
×
2456
    pShowIter->pTrans = NULL;
×
2457
    pShowIter->num = 0;
×
2458
  } else {
2459
    pShowIter->pTrans = pShowIter->pTrans;
×
2460
    pShowIter->stage = pShowIter->pTrans->stage;
×
2461
    pShowIter->num += (*numOfRows);
×
2462
  }
2463
}
×
2464

2465
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2466
  SMnode *pMnode = pReq->info.node;
×
2467
  SSdb   *pSdb = pMnode->pSdb;
×
2468
  int32_t numOfRows = 0;
×
2469

2470
  int32_t code = 0;
×
2471
  int32_t lino = 0;
×
2472

2473
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
×
2474
        pShow->pIter);
2475

2476
  if (pShow->pIter == NULL) {
×
2477
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
×
2478
    if (pShow->pIter == NULL) {
×
2479
      mError("failed to malloc for pShow->pIter");
×
2480
      return 0;
×
2481
    }
2482
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
×
2483
  }
2484

2485
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
×
2486

2487
  while (numOfRows < rows) {
×
2488
    if (pShowIter->pTrans == NULL) {
×
2489
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
×
2490
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
×
2491
      if (pShowIter->pIter == NULL) break;
×
2492
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
×
2493
            pShowIter->pTrans->stage, pShowIter->num);
2494

2495
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
×
2496

2497
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
×
2498
      break;
×
2499
    } else {
2500
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
×
2501
            pShowIter->num);
2502
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
×
2503

2504
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
×
2505
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
×
2506
      break;
×
2507
    }
2508
  }
2509

2510
_OVER:
×
2511
  pShow->numOfRows += numOfRows;
×
2512

2513
  if (code != 0) {
×
2514
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2515
  } else {
2516
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
×
2517
  }
2518
  if (numOfRows == 0) {
×
2519
    taosMemoryFree(pShow->pIter);
×
2520
    pShow->pIter = NULL;
×
2521
  }
2522
  return numOfRows;
×
2523
}
2524

2525
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
2526
  SSdb *pSdb = pMnode->pSdb;
×
2527
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
2528
}
×
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