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

taosdata / TDengine / #4916

06 Jan 2026 01:30AM UTC coverage: 65.402% (-0.3%) from 65.711%
#4916

push

travis-ci

web-flow
merge: from main to 3.0 branch #34167

185 of 324 new or added lines in 15 files covered. (57.1%)

3165 existing lines in 17 files now uncovered.

202517 of 309650 relevant lines covered (65.4%)

114862302.71 hits per line

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

83.01
/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
#include "mndToken.h"
28

29
#define TRANS_VER1_NUMBER    1
30
#define TRANS_VER2_NUMBER    2
31
#define TRANS_VER3_NUMBER    3
32
#define TRANS_VER_USER_DATA  4
33
#define TRANS_VER_CURRENT    4 // current version
34

35
#define TRANS_ARRAY_SIZE     8
36
#define TRANS_RESERVE_SIZE   39
37
#define TRANS_ACTION_TIMEOUT (1000 * 1000 * 60 * 15)
38

39
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans);
40
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *OldTrans, STrans *pOld);
41
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc);
42

43
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw);
44
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction);
45
static void    mndTransDropLogs(SArray *pArray);
46
static void    mndTransDropActions(SArray *pArray);
47

48
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend);
49
static int32_t mndTransExecuteRedoLogs(SMnode *pMnode, STrans *pTrans, bool topHalf);
50
static int32_t mndTransExecuteUndoLogs(SMnode *pMnode, STrans *pTrans, bool topHalf);
51
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend);
52
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend);
53
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf);
54
static bool    mndTransPerformRedoLogStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
55
static bool    mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend);
56
static bool    mndTransPerformUndoLogStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
57
static bool    mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend);
58
static bool    mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
59
static bool    mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
60
static bool    mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
61
static bool    mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf);
62

63
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
114,390,582✔
64

65
static bool mndCannotExecuteTrans(SMnode *pMnode, bool topHalf) {
104,042,854✔
66
  bool isLeader = mndIsLeader(pMnode);
104,042,854✔
67
  bool ret = (!pMnode->deploy && !isLeader) || mndTransIsInSyncContext(topHalf);
104,042,854✔
68
  if (ret) mDebug("cannot execute trans action, deploy:%d, isLeader:%d, topHalf:%d", pMnode->deploy, isLeader, topHalf);
104,042,854✔
69
  return ret;
104,042,854✔
70
}
71

72
static bool mndCannotExecuteTransWithInfo(SMnode *pMnode, bool topHalf, char *str, int32_t size) {
13,074,641✔
73
  bool isLeader = mndIsLeader(pMnode);
13,074,641✔
74
  bool ret = (!pMnode->deploy && !isLeader) || mndTransIsInSyncContext(topHalf);
13,074,641✔
75
  if (ret)
13,074,641✔
76
    tsnprintf(str, size, "deploy:%d, isLeader:%d, context:%s", pMnode->deploy, isLeader,
3,359,275✔
77
              topHalf ? "transContext" : "syncContext");
78
  return ret;
13,074,641✔
79
}
80

81
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
157,618,554✔
82

83
static void    mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans);
84
static int32_t mndProcessTransTimer(SRpcMsg *pReq);
85
static int32_t mndProcessTtl(SRpcMsg *pReq);
86
static int32_t mndProcessKillTransReq(SRpcMsg *pReq);
87

88
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
89
static void    mndCancelGetNextTrans(SMnode *pMnode, void *pIter);
90
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
91
static int32_t tsMaxTransId = 0;
92

93
int32_t mndInitTrans(SMnode *pMnode) {
403,129✔
94
  SSdbTable table = {
403,129✔
95
      .sdbType = SDB_TRANS,
96
      .keyType = SDB_KEY_INT32,
97
      .encodeFp = (SdbEncodeFp)mndTransEncode,
98
      .decodeFp = (SdbDecodeFp)mndTransDecode,
99
      .insertFp = (SdbInsertFp)mndTransActionInsert,
100
      .updateFp = (SdbUpdateFp)mndTransActionUpdate,
101
      .deleteFp = (SdbDeleteFp)mndTransDelete,
102
  };
103

104
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
403,129✔
105
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
403,129✔
106

107
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndRetrieveTrans);
403,129✔
108
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndCancelGetNextTrans);
403,129✔
109
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANSACTION_DETAIL, mndRetrieveTransDetail);
403,129✔
110
  return sdbSetTable(pMnode->pSdb, table);
403,129✔
111
}
112

113
void mndCleanupTrans(SMnode *pMnode) {}
403,068✔
114

115
static int32_t mndTransGetActionsSize(SArray *pArray) {
204,038,148✔
116
  int32_t actionNum = taosArrayGetSize(pArray);
204,038,148✔
117
  int32_t rawDataLen = 0;
204,038,148✔
118

119
  for (int32_t i = 0; i < actionNum; ++i) {
553,151,568✔
120
    STransAction *pAction = taosArrayGet(pArray, i);
349,113,420✔
121
    if (pAction->actionType == TRANS_ACTION_RAW) {
349,113,420✔
122
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
231,404,544✔
123
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
117,708,876✔
124
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
117,708,876✔
125
    } else {
126
      // empty
127
    }
128
    rawDataLen += sizeof(int8_t);
349,113,420✔
129
  }
130

131
  return rawDataLen;
204,038,148✔
132
}
133

134
static int32_t mndTransEncodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionsNum,
204,038,148✔
135
                                    int32_t sver) {
136
  int32_t code = 0;
204,038,148✔
137
  int32_t lino = 0;
204,038,148✔
138
  int32_t dataPos = *offset;
204,038,148✔
139
  int8_t  unused = 0;
204,038,148✔
140
  int32_t ret = -1;
204,038,148✔
141

142
  for (int32_t i = 0; i < actionsNum; ++i) {
553,151,568✔
143
    STransAction *pAction = taosArrayGet(pActions, i);
349,113,420✔
144
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
349,113,420✔
145
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
349,113,420✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
349,113,420✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
349,113,420✔
148
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
349,113,420✔
149
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
349,113,420✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
349,113,420✔
151
    if (sver > TRANS_VER2_NUMBER) {
349,113,420✔
152
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
349,113,420✔
153
    }
154
    if (pAction->actionType == TRANS_ACTION_RAW) {
349,113,420✔
155
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
231,404,544✔
156
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
231,404,544✔
157
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
231,404,544✔
158
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
231,404,544✔
159
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
117,708,876✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
117,708,876✔
161
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
117,708,876✔
162
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
117,708,876✔
163
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
117,708,876✔
164
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
117,708,876✔
165
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
117,708,876✔
166
    } else {
167
      // nothing
168
    }
169
  }
170
  ret = 0;
204,038,148✔
171

172
_OVER:
204,038,148✔
173
  *offset = dataPos;
204,038,148✔
174
  return ret;
204,038,148✔
175
}
176

177
SSdbRaw *mndTransEncode(STrans *pTrans) {
51,009,537✔
178
  int32_t code = 0;
51,009,537✔
179
  int32_t lino = 0;
51,009,537✔
180
  terrno = TSDB_CODE_INVALID_MSG;
51,009,537✔
181
  int8_t sver = TRANS_VER_CURRENT;
51,009,537✔
182

183
  int32_t rawDataLen = sizeof(STrans) + TRANS_RESERVE_SIZE + pTrans->paramLen + pTrans->userDataLen;
51,009,537✔
184
  rawDataLen += mndTransGetActionsSize(pTrans->prepareActions);
51,009,537✔
185
  rawDataLen += mndTransGetActionsSize(pTrans->redoActions);
51,009,537✔
186
  rawDataLen += mndTransGetActionsSize(pTrans->undoActions);
51,009,537✔
187
  rawDataLen += mndTransGetActionsSize(pTrans->commitActions);
51,009,537✔
188

189
  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, sver, rawDataLen);
51,009,537✔
190
  if (pRaw == NULL) {
51,009,537✔
191
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
×
192
    return NULL;
×
193
  }
194

195
  int32_t dataPos = 0;
51,009,537✔
196
  SDB_SET_INT32(pRaw, dataPos, pTrans->id, _OVER)
51,009,537✔
197
  SDB_SET_INT8(pRaw, dataPos, pTrans->stage, _OVER)
51,009,537✔
198
  SDB_SET_INT8(pRaw, dataPos, pTrans->policy, _OVER)
51,009,537✔
199
  SDB_SET_INT8(pRaw, dataPos, pTrans->conflict, _OVER)
51,009,537✔
200
  SDB_SET_INT8(pRaw, dataPos, pTrans->exec, _OVER)
51,009,537✔
201
  SDB_SET_INT8(pRaw, dataPos, pTrans->oper, _OVER)
51,009,537✔
202
  SDB_SET_INT8(pRaw, dataPos, 0, _OVER)
51,009,537✔
203
  SDB_SET_INT16(pRaw, dataPos, pTrans->originRpcType, _OVER)
51,009,537✔
204
  SDB_SET_INT64(pRaw, dataPos, pTrans->createdTime, _OVER)
51,009,537✔
205
  SDB_SET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
51,009,537✔
206
  SDB_SET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
51,009,537✔
207
  SDB_SET_INT32(pRaw, dataPos, pTrans->actionPos, _OVER)
51,009,537✔
208

209
  int32_t prepareActionNum = taosArrayGetSize(pTrans->prepareActions);
51,009,537✔
210
  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
51,009,537✔
211
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
51,009,537✔
212
  int32_t commitActionNum = taosArrayGetSize(pTrans->commitActions);
51,009,537✔
213

214
  if (sver > TRANS_VER1_NUMBER) {
51,009,537✔
215
    SDB_SET_INT32(pRaw, dataPos, prepareActionNum, _OVER)
51,009,537✔
216
  }
217
  SDB_SET_INT32(pRaw, dataPos, redoActionNum, _OVER)
51,009,537✔
218
  SDB_SET_INT32(pRaw, dataPos, undoActionNum, _OVER)
51,009,537✔
219
  SDB_SET_INT32(pRaw, dataPos, commitActionNum, _OVER)
51,009,537✔
220

221
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
51,009,537✔
222
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
51,009,537✔
223
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
51,009,537✔
224
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
51,009,537✔
225

226
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
51,009,537✔
227
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
51,009,537✔
228
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
51,009,537✔
229
  if (pTrans->param != NULL) {
51,009,537✔
230
    SDB_SET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER)
×
231
  }
232

233
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
51,009,537✔
234

235
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
51,009,537✔
236
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
51,009,537✔
237
  void *pIter = NULL;
51,009,537✔
238
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
51,009,537✔
239
  while (pIter) {
51,040,824✔
240
    int32_t arbGroupId = *(int32_t *)pIter;
31,287✔
241
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
31,287✔
242
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
31,287✔
243
  }
244

245
  if (sver > TRANS_VER1_NUMBER) {
51,009,537✔
246
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
51,009,537✔
247
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
51,009,537✔
248
  }
249

250
  if (sver > TRANS_VER2_NUMBER) {
51,009,537✔
251
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
51,009,537✔
252
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
51,009,537✔
253
    void *pIter = NULL;
51,009,537✔
254
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
51,009,537✔
255
    while (pIter) {
53,281,218✔
256
      size_t   keysize = 0;
2,271,681✔
257
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,271,681✔
258
      int32_t  groupPos = *(int32_t *)pIter;
2,271,681✔
259
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,271,681✔
260
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,271,681✔
261
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,271,681✔
262
    }
263
  }
264

265
  if (sver >= TRANS_VER_USER_DATA) {
51,009,537✔
266
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
51,009,537✔
267
    if (pTrans->userDataLen > 0) {
51,009,537✔
268
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
276✔
269
    }
270
  }
271

272
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
51,009,537✔
273
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
51,009,537✔
274

275
  terrno = 0;
51,009,537✔
276

277
_OVER:
51,009,537✔
278
  if (terrno != 0) {
51,009,537✔
279
    mError("trans:%d, failed to encode to raw:%p maxlen:%d len:%d since %s", pTrans->id, pRaw, sdbGetRawTotalSize(pRaw),
×
280
           dataPos, terrstr());
281
    sdbFreeRaw(pRaw);
×
282
    return NULL;
×
283
  }
284

285
  mTrace("trans:%d, encode to raw:%p, row:%p len:%d", pTrans->id, pRaw, pTrans, dataPos);
51,009,537✔
286
  return pRaw;
51,009,537✔
287
}
288

289
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
26,998,861✔
290
  if (pAction->groupId < 0) return 0;
26,998,861✔
291
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
26,477,080✔
292
  if (redoAction == NULL) {
26,477,080✔
293
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
4,528,643✔
294
    if (array != NULL) {
4,528,643✔
295
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
4,528,643✔
296
        mInfo("failed put action into redo group actions");
×
297
        return TSDB_CODE_INTERNAL_ERROR;
×
298
      }
299
    }
300
    redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
4,528,643✔
301
  }
302
  if (redoAction != NULL) {
26,477,080✔
303
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
52,954,160✔
304
  }
305
  return 0;
26,477,080✔
306
}
307

308
static int32_t mndTransDecodeActionWithGroup(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionNum,
1,524,234✔
309
                                             SHashObj *redoGroupActions, int32_t sver) {
310
  int32_t      code = 0;
1,524,234✔
311
  int32_t      lino = 0;
1,524,234✔
312
  STransAction action = {0};
1,524,234✔
313
  int32_t      dataPos = *offset;
1,524,234✔
314
  int8_t       unused = 0;
1,524,234✔
315
  int8_t       stage = 0;
1,524,234✔
316
  int8_t       actionType = 0;
1,524,234✔
317
  int32_t      dataLen = 0;
1,524,234✔
318
  int32_t      ret = -1;
1,524,234✔
319
  terrno = 0;
1,524,234✔
320

321
  for (int32_t i = 0; i < actionNum; ++i) {
28,523,095✔
322
    memset(&action, 0, sizeof(action));
26,998,861✔
323
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
26,998,861✔
324
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
26,998,861✔
325
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
26,998,861✔
326
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
26,998,861✔
327
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
26,998,861✔
328
    action.actionType = actionType;
26,998,861✔
329
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
26,998,861✔
330
    action.stage = stage;
26,998,861✔
331
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
26,998,861✔
332
    if (sver > TRANS_VER2_NUMBER) {
26,998,861✔
333
      SDB_GET_INT32(pRaw, dataPos, &action.groupId, _OVER)
26,998,861✔
334
    }
335
    if (action.actionType == TRANS_ACTION_RAW) {
26,998,861✔
336
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
4,933,975✔
337
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
4,933,975✔
338
      action.pRaw = taosMemoryMalloc(dataLen);
4,933,975✔
339
      if (action.pRaw == NULL) goto _OVER;
4,933,975✔
340
      mTrace("raw:%p, is created", action.pRaw);
4,933,975✔
341
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
4,933,975✔
342
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
4,933,975✔
343
      STransAction *pAction = taosArrayGet(pActions, taosArrayGetSize(pActions) - 1);
4,933,975✔
344
      if (mndTransDecodeGroupRedoAction(redoGroupActions, pAction) != 0) goto _OVER;
4,933,975✔
345
      action.pRaw = NULL;
4,933,975✔
346
    } else if (action.actionType == TRANS_ACTION_MSG) {
22,064,886✔
347
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
22,064,886✔
348
      tmsgUpdateDnodeEpSet(&action.epSet);
22,064,886✔
349
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
22,064,886✔
350
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
22,064,886✔
351
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
22,064,886✔
352
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
22,064,886✔
353
      action.pCont = taosMemoryMalloc(action.contLen);
22,064,886✔
354
      if (action.pCont == NULL) goto _OVER;
22,064,886✔
355
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
22,064,886✔
356
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
22,064,886✔
357
      STransAction *pAction = taosArrayGet(pActions, taosArrayGetSize(pActions) - 1);
22,064,886✔
358
      if (mndTransDecodeGroupRedoAction(redoGroupActions, pAction) != 0) goto _OVER;
22,064,886✔
359
      action.pCont = NULL;
22,064,886✔
360
    } else {
361
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
362
    }
363
  }
364
  ret = 0;
1,524,234✔
365

366
_OVER:
1,524,234✔
367
  if (terrno != 0) mError("failed to decode action with group at line:%d, since %s", lino, terrstr());
1,524,234✔
368
  *offset = dataPos;
1,524,234✔
369
  taosMemoryFreeClear(action.pCont);
1,524,234✔
370
  return ret;
1,524,234✔
371
}
372

373
static int32_t mndTransDecodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionNum, int32_t sver) {
360,235,118✔
374
  int32_t      code = 0;
360,235,118✔
375
  int32_t      lino = 0;
360,235,118✔
376
  STransAction action = {0};
360,235,118✔
377
  int32_t      dataPos = *offset;
360,235,118✔
378
  int8_t       unused = 0;
360,235,118✔
379
  int8_t       stage = 0;
360,235,118✔
380
  int8_t       actionType = 0;
360,235,118✔
381
  int32_t      dataLen = 0;
360,235,118✔
382
  int32_t      ret = -1;
360,235,118✔
383
  terrno = 0;
360,235,118✔
384

385
  for (int32_t i = 0; i < actionNum; ++i) {
961,459,016✔
386
    memset(&action, 0, sizeof(action));
601,223,898✔
387
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
601,223,898✔
388
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
601,223,898✔
389
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
601,223,898✔
390
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
601,223,898✔
391
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
601,223,898✔
392
    action.actionType = actionType;
601,223,898✔
393
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
601,223,898✔
394
    action.stage = stage;
601,223,898✔
395
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
601,223,898✔
396
    if (sver > TRANS_VER2_NUMBER) {
601,223,898✔
397
      SDB_GET_INT32(pRaw, dataPos, &action.groupId, _OVER)
601,223,898✔
398
    }
399
    if (action.actionType == TRANS_ACTION_RAW) {
601,223,898✔
400
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
401,125,552✔
401
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
401,125,552✔
402
      action.pRaw = taosMemoryMalloc(dataLen);
401,125,552✔
403
      if (action.pRaw == NULL) goto _OVER;
401,125,552✔
404
      mTrace("raw:%p, is created", action.pRaw);
401,125,552✔
405
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
401,125,552✔
406
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
401,125,552✔
407
      action.pRaw = NULL;
401,125,552✔
408
    } else if (action.actionType == TRANS_ACTION_MSG) {
200,098,346✔
409
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
200,098,346✔
410
      tmsgUpdateDnodeEpSet(&action.epSet);
200,098,346✔
411
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
200,098,346✔
412
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
200,098,346✔
413
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
200,098,346✔
414
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
200,098,346✔
415
      action.pCont = taosMemoryMalloc(action.contLen);
200,098,346✔
416
      if (action.pCont == NULL) goto _OVER;
200,098,346✔
417
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
200,098,346✔
418
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
200,098,346✔
419
      action.pCont = NULL;
200,098,346✔
420
    } else {
421
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
422
    }
423
  }
424
  ret = 0;
360,235,118✔
425

426
_OVER:
360,235,118✔
427
  if (terrno != 0) mError("failed to decode action at line:%d, since %s", lino, terrstr());
360,235,118✔
428
  *offset = dataPos;
360,235,118✔
429
  taosMemoryFreeClear(action.pCont);
360,235,118✔
430
  return ret;
360,235,118✔
431
}
432

433
SSdbRow *mndTransDecode(SSdbRaw *pRaw) {
90,439,838✔
434
  terrno = TSDB_CODE_INVALID_MSG;
90,439,838✔
435
  int32_t  code = 0;
90,439,838✔
436
  int32_t  lino = 0;
90,439,838✔
437
  SSdbRow *pRow = NULL;
90,439,838✔
438
  STrans  *pTrans = NULL;
90,439,838✔
439
  char    *pData = NULL;
90,439,838✔
440
  int32_t  dataLen = 0;
90,439,838✔
441
  int8_t   sver = 0;
90,439,838✔
442
  int32_t  prepareActionNum = 0;
90,439,838✔
443
  int32_t  redoActionNum = 0;
90,439,838✔
444
  int32_t  undoActionNum = 0;
90,439,838✔
445
  int32_t  commitActionNum = 0;
90,439,838✔
446
  int32_t  dataPos = 0;
90,439,838✔
447
  int32_t  arbgroupIdNum = 0;
90,439,838✔
448

449
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
90,439,838✔
450

451
  if (sver > TRANS_VER_CURRENT) {
90,439,838✔
452
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
453
    goto _OVER;
×
454
  }
455

456
  pRow = sdbAllocRow(sizeof(STrans));
90,439,838✔
457
  if (pRow == NULL) goto _OVER;
90,439,838✔
458

459
  pTrans = sdbGetRowObj(pRow);
90,439,838✔
460
  if (pTrans == NULL) goto _OVER;
90,439,838✔
461

462
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
90,439,838✔
463

464
  int8_t stage = 0;
90,439,838✔
465
  int8_t policy = 0;
90,439,838✔
466
  int8_t conflict = 0;
90,439,838✔
467
  int8_t exec = 0;
90,439,838✔
468
  int8_t oper = 0;
90,439,838✔
469
  int8_t reserved = 0;
90,439,838✔
470
  int8_t actionType = 0;
90,439,838✔
471
  SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
90,439,838✔
472
  SDB_GET_INT8(pRaw, dataPos, &policy, _OVER)
90,439,838✔
473
  SDB_GET_INT8(pRaw, dataPos, &conflict, _OVER)
90,439,838✔
474
  SDB_GET_INT8(pRaw, dataPos, &exec, _OVER)
90,439,838✔
475
  SDB_GET_INT8(pRaw, dataPos, &oper, _OVER)
90,439,838✔
476
  SDB_GET_INT8(pRaw, dataPos, &reserved, _OVER)
90,439,838✔
477
  pTrans->stage = stage;
90,439,838✔
478
  pTrans->policy = policy;
90,439,838✔
479
  pTrans->conflict = conflict;
90,439,838✔
480
  pTrans->exec = exec;
90,439,838✔
481
  pTrans->oper = oper;
90,439,838✔
482
  SDB_GET_INT16(pRaw, dataPos, &pTrans->originRpcType, _OVER)
90,439,838✔
483
  SDB_GET_INT64(pRaw, dataPos, &pTrans->createdTime, _OVER)
90,439,838✔
484
  SDB_GET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
90,439,838✔
485
  SDB_GET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
90,439,838✔
486
  SDB_GET_INT32(pRaw, dataPos, &pTrans->actionPos, _OVER)
90,439,838✔
487

488
  if (sver > TRANS_VER1_NUMBER) {
90,439,838✔
489
    SDB_GET_INT32(pRaw, dataPos, &prepareActionNum, _OVER)
90,439,838✔
490
  }
491
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, _OVER)
90,439,838✔
492
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, _OVER)
90,439,838✔
493
  SDB_GET_INT32(pRaw, dataPos, &commitActionNum, _OVER)
90,439,838✔
494

495
  pTrans->prepareActions = taosArrayInit(prepareActionNum, sizeof(STransAction));
90,439,838✔
496
  pTrans->redoActions = taosArrayInit(redoActionNum, sizeof(STransAction));
90,439,838✔
497
  pTrans->undoActions = taosArrayInit(undoActionNum, sizeof(STransAction));
90,439,838✔
498
  pTrans->commitActions = taosArrayInit(commitActionNum, sizeof(STransAction));
90,439,838✔
499
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
90,439,838✔
500
    pTrans->redoGroupActions = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,524,234✔
501
    pTrans->groupActionPos = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,524,234✔
502
  }
503

504
  if (pTrans->prepareActions == NULL) goto _OVER;
90,439,838✔
505
  if (pTrans->redoActions == NULL) goto _OVER;
90,439,838✔
506
  if (pTrans->undoActions == NULL) goto _OVER;
90,439,838✔
507
  if (pTrans->commitActions == NULL) goto _OVER;
90,439,838✔
508

509
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
90,439,838✔
510
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
90,439,838✔
511
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,524,234✔
512
                                      sver) < 0)
513
      goto _OVER;
×
514
  } else {
515
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
88,915,604✔
516
  }
517
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
90,439,838✔
518
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
90,439,838✔
519

520
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
90,439,838✔
521
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
90,439,838✔
522
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
90,439,838✔
523
  if (pTrans->paramLen != 0) {
90,439,838✔
524
    pTrans->param = taosMemoryMalloc(pTrans->paramLen);
×
525
    if (pTrans->param == NULL) goto _OVER;
×
526
    SDB_GET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER);
×
527
  }
528

529
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
90,439,838✔
530

531
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
90,439,838✔
532
  if (arbgroupIdNum > 0) {
90,439,838✔
533
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
45,115✔
534
    if (pTrans->arbGroupIds == NULL) goto _OVER;
45,115✔
535
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
97,260✔
536
      int32_t arbGroupId = 0;
52,145✔
537
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
52,145✔
538
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
52,145✔
539
    }
540
  }
541

542
  int8_t ableKill = 0;
90,439,838✔
543
  int32_t killMode = 0;
90,439,838✔
544
  if (sver > TRANS_VER1_NUMBER) {
90,439,838✔
545
    SDB_GET_INT8(pRaw, dataPos, &ableKill, _OVER)
90,439,838✔
546
    SDB_GET_INT32(pRaw, dataPos, &killMode, _OVER)
90,439,838✔
547
  }
548
  pTrans->ableToBeKilled = ableKill;
90,439,838✔
549
  pTrans->killMode = (int8_t)killMode;
90,439,838✔
550

551
  if (sver > TRANS_VER2_NUMBER) {
90,439,838✔
552
    int32_t groupNum = -1;
90,439,838✔
553
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
90,439,838✔
554
    for (int32_t i = 0; i < groupNum; ++i) {
94,968,481✔
555
      int32_t groupId = -1;
4,528,643✔
556
      int32_t groupPos = -1;
4,528,643✔
557
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
4,528,643✔
558
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
4,528,643✔
559
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
4,528,643✔
560
        goto _OVER;
×
561
    }
562
  }
563

564
  if (sver >= TRANS_VER_USER_DATA) {
90,439,838✔
565
    SDB_GET_INT32(pRaw, dataPos, &pTrans->userDataLen, _OVER)
90,439,838✔
566
    if (pTrans->userDataLen > 0) {
90,439,838✔
567
      pTrans->userData = taosMemoryMalloc(pTrans->userDataLen);
460✔
568
      if (pTrans->userData == NULL) goto _OVER;
460✔
569
      SDB_GET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
460✔
570
    }
571
  }
572

573
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
90,439,838✔
574

575
  terrno = 0;
90,439,838✔
576

577
_OVER:
90,439,838✔
578
  if (terrno != 0 && pTrans != NULL) {
90,439,838✔
579
    mError("trans:%d, failed to parse from raw:%p at line:%d dataPos:%d dataLen:%d since %s", pTrans->id, pRaw, lino,
×
580
           dataPos, pRaw->dataLen, terrstr());
581
    mndTransDropData(pTrans);
×
582
    taosMemoryFreeClear(pRow);
×
583
    return NULL;
×
584
  }
585

586
  if (pTrans != NULL) {
90,439,838✔
587
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
90,439,838✔
588
  }
589
  return pRow;
90,439,838✔
590
}
591

592
static const char *mndTransStr(ETrnStage stage) {
647,976,339✔
593
  switch (stage) {
647,976,339✔
594
    case TRN_STAGE_PREPARE:
51,792,759✔
595
      return "prepare";
51,792,759✔
596
    case TRN_STAGE_REDO_ACTION:
233,530,599✔
597
      return "redoAction";
233,530,599✔
598
    case TRN_STAGE_ROLLBACK:
7,310✔
599
      return "rollback";
7,310✔
600
    case TRN_STAGE_UNDO_ACTION:
9,969,898✔
601
      return "undoAction";
9,969,898✔
602
    case TRN_STAGE_COMMIT:
81,470,222✔
603
      return "commit";
81,470,222✔
604
    case TRN_STAGE_COMMIT_ACTION:
150,576,439✔
605
      return "commitAction";
150,576,439✔
606
    case TRN_STAGE_FINISH:
120,621,192✔
607
      return "finished";
120,621,192✔
608
    case TRN_STAGE_PRE_FINISH:
7,920✔
609
      return "pre-finish";
7,920✔
610
    default:
×
611
      return "invalid";
×
612
  }
613
}
614

615
static const char *mndTransTypeStr(ETrnAct actionType) {
58,718✔
616
  switch (actionType) {
58,718✔
617
    case TRANS_ACTION_MSG:
48,286✔
618
      return "msg";
48,286✔
619
    case TRANS_ACTION_RAW:
10,432✔
620
      return "sdb";
10,432✔
621
    default:
×
622
      return "invalid";
×
623
  }
624
}
625

626
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
181,054,382✔
627
  if (pAction != NULL) {
181,054,382✔
628
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
139,522,872✔
629
      pTrans->lastAction = pAction->id;
97,742,708✔
630
      pTrans->lastMsgType = pAction->msgType;
97,742,708✔
631
      pTrans->lastEpset = pAction->epSet;
97,742,708✔
632
      pTrans->lastErrorNo = pAction->errCode;
97,742,708✔
633
    }
634
  } else {
635
    pTrans->lastAction = 0;
41,531,510✔
636
    pTrans->lastMsgType = 0;
41,531,510✔
637
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
41,531,510✔
638
    pTrans->lastErrorNo = 0;
41,531,510✔
639
  }
640
}
181,054,382✔
641

642
static void mndTransTestStartFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
643
  mInfo("test trans start, param:%s, len:%d", (char *)param, paramLen);
×
644
}
×
645

646
static void mndTransTestStopFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
647
  mInfo("test trans stop, param:%s, len:%d", (char *)param, paramLen);
×
648
}
×
649

650
static TransCbFp mndTransGetCbFp(ETrnFunc ftype) {
524,882✔
651
  switch (ftype) {
524,882✔
652
    case TRANS_START_FUNC_TEST:
×
653
      return mndTransTestStartFunc;
×
654
    case TRANS_STOP_FUNC_TEST:
×
655
      return mndTransTestStopFunc;
×
656
    case TRANS_START_FUNC_MQ_REB:
262,441✔
657
      return mndRebCntInc;
262,441✔
658
    case TRANS_STOP_FUNC_MQ_REB:
262,441✔
659
      return mndRebCntDec;
262,441✔
660
    default:
×
661
      return NULL;
×
662
  }
663
}
664

665
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
17,113,622✔
666
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
17,113,622✔
667
        mndTransStr(pTrans->stage), pTrans->startFunc);
668

669
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
17,113,622✔
670

671
  if (pTrans->startFunc > 0) {
17,113,622✔
672
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
262,441✔
673
    if (fp) {
262,441✔
674
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
262,441✔
675
    }
676
    // pTrans->startFunc = 0;
677
  }
678

679
  if (pTrans->stage == TRN_STAGE_COMMIT) {
17,113,622✔
680
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
190✔
681
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pTrans->id);
190✔
682
  }
683

684
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
17,113,622✔
685
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
×
686
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
×
687
  }
688

689
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
17,113,622✔
690
    pTrans->stage = TRN_STAGE_FINISH;
×
691
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pTrans->id);
×
692
  }
693

694
  return 0;
17,113,622✔
695
}
696

697
void mndTransDropData(STrans *pTrans) {
106,527,705✔
698
  if (pTrans->prepareActions != NULL) {
106,527,705✔
699
    mndTransDropActions(pTrans->prepareActions);
106,527,705✔
700
    pTrans->prepareActions = NULL;
106,527,705✔
701
  }
702
  if (pTrans->redoActions != NULL) {
106,527,705✔
703
    mndTransDropActions(pTrans->redoActions);
106,527,705✔
704
    pTrans->redoActions = NULL;
106,527,705✔
705
  }
706
  if (pTrans->undoActions != NULL) {
106,527,705✔
707
    mndTransDropActions(pTrans->undoActions);
106,527,705✔
708
    pTrans->undoActions = NULL;
106,527,705✔
709
  }
710
  if (pTrans->commitActions != NULL) {
106,527,705✔
711
    mndTransDropActions(pTrans->commitActions);
106,527,705✔
712
    pTrans->commitActions = NULL;
106,527,705✔
713
  }
714
  if (pTrans->redoGroupActions != NULL) {
106,527,705✔
715
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
17,612,101✔
716
    while (pIter) {
22,363,430✔
717
      SArray **redoActions = pIter;
4,751,329✔
718
      taosArrayDestroy(*redoActions);
4,751,329✔
719
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,751,329✔
720
    }
721

722
    taosHashCleanup(pTrans->redoGroupActions);
17,612,101✔
723
    pTrans->redoGroupActions = NULL;
17,612,101✔
724
  }
725
  if (pTrans->groupActionPos != NULL) {
106,527,705✔
726
    taosHashCleanup(pTrans->groupActionPos);
17,612,101✔
727
    pTrans->groupActionPos = NULL;
17,612,101✔
728
  }
729
  if (pTrans->arbGroupIds != NULL) {
106,527,705✔
730
    taosHashCleanup(pTrans->arbGroupIds);
16,132,982✔
731
  }
732
  if (pTrans->pRpcArray != NULL) {
106,527,705✔
733
    taosArrayDestroy(pTrans->pRpcArray);
16,087,867✔
734
    pTrans->pRpcArray = NULL;
16,087,867✔
735
  }
736
  if (pTrans->rpcRsp != NULL) {
106,527,705✔
737
    taosMemoryFree(pTrans->rpcRsp);
6,603,391✔
738
    pTrans->rpcRsp = NULL;
6,603,391✔
739
    pTrans->rpcRspLen = 0;
6,603,391✔
740
  }
741
  if (pTrans->param != NULL) {
106,527,705✔
742
    taosMemoryFree(pTrans->param);
×
743
    pTrans->param = NULL;
×
744
    pTrans->paramLen = 0;
×
745
  }
746
  if (pTrans->userData != NULL) {
106,527,705✔
747
    taosMemoryFree(pTrans->userData);
460✔
748
    pTrans->userData = NULL;
460✔
749
    pTrans->userDataLen = 0;
460✔
750
  }
751
  (void)taosThreadMutexDestroy(&pTrans->mutex);
106,527,705✔
752
}
106,527,705✔
753

754
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc) {
53,774,796✔
755
  mInfo("trans:%d, perform delete action, row:%p stage:%s callfunc:%d, stopFunc:%d", pTrans->id, pTrans,
53,774,796✔
756
        mndTransStr(pTrans->stage), callFunc, pTrans->stopFunc);
757

758
  if (pTrans->stopFunc > 0 && callFunc) {
53,774,796✔
759
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
262,441✔
760
    if (fp) {
262,441✔
761
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
262,441✔
762
    }
763
    // pTrans->stopFunc = 0;
764
  }
765

766
  mndTransDropData(pTrans);
53,774,796✔
767
  return 0;
53,774,796✔
768
}
769

770
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
78,266,404✔
771
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
239,632,945✔
772
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
161,366,541✔
773
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
161,366,541✔
774
    pOldAction->rawWritten = pNewAction->rawWritten;
161,366,541✔
775
    pOldAction->msgSent = pNewAction->msgSent;
161,366,541✔
776
    pOldAction->msgReceived = pNewAction->msgReceived;
161,366,541✔
777
    pOldAction->errCode = pNewAction->errCode;
161,366,541✔
778
  }
779
}
78,266,404✔
780

781
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) {
19,566,601✔
782
  mInfo("trans:%d, perform update action, old row:%p stage:%s create:%" PRId64 ", new row:%p stage:%s create:%" PRId64,
19,566,601✔
783
        pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage), pNew->createdTime);
784

785
  if (pOld->createdTime != pNew->createdTime) {
19,566,601✔
786
    mError("trans:%d, failed to perform update action since createTime not match, old row:%p stage:%s create:%" PRId64
×
787
           ", new row:%p stage:%s create:%" PRId64,
788
           pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage),
789
           pNew->createdTime);
790
    // only occured while sync timeout
791
    TAOS_RETURN(TSDB_CODE_MND_TRANS_SYNC_TIMEOUT);
×
792
  }
793

794
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
19,566,601✔
795
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
19,566,601✔
796
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
19,566,601✔
797
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
19,566,601✔
798
  pOld->stage = pNew->stage;
19,566,601✔
799
  pOld->actionPos = pNew->actionPos;
19,566,601✔
800
  TSWAP(pOld->userData, pNew->userData);
19,566,601✔
801
  TSWAP(pOld->userDataLen, pNew->userDataLen);
19,566,601✔
802

803
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
19,566,601✔
804
  while (pIter != NULL) {
21,490,347✔
805
    int32_t newActionPos = *(int32_t *)pIter;
1,923,746✔
806

807
    size_t   keylen = 0;
1,923,746✔
808
    int32_t *groupId = taosHashGetKey(pIter, &keylen);
1,923,746✔
809

810
    int32_t *oldActionPos = taosHashGet(pOld->groupActionPos, groupId, sizeof(int32_t));
1,923,746✔
811
    if (oldActionPos != NULL) {
1,923,746✔
812
      *oldActionPos = newActionPos;
1,923,746✔
813
    } else
814
      taosHashPut(pOld->groupActionPos, groupId, sizeof(int32_t), &newActionPos, sizeof(int32_t));
×
815

816
    pIter = taosHashIterate(pNew->groupActionPos, pIter);
1,923,746✔
817
  }
818

819
  if (pOld->stage == TRN_STAGE_COMMIT) {
19,566,601✔
820
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
17,092,799✔
821
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
17,092,799✔
822
  }
823

824
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
19,566,601✔
825
    pOld->stage = TRN_STAGE_UNDO_ACTION;
1,462✔
826
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pNew->id);
1,462✔
827
  }
828

829
  if (pOld->stage == TRN_STAGE_PRE_FINISH) {
19,566,601✔
830
    pOld->stage = TRN_STAGE_FINISH;
1,584✔
831
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pNew->id);
1,584✔
832
  }
833

834
  return 0;
19,566,601✔
835
}
836

837
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
73,652,018✔
838
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
73,652,018✔
839
  if (pTrans == NULL) {
73,652,018✔
840
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
×
841
  }
842
  return pTrans;
73,652,018✔
843
}
844

845
void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
73,652,179✔
846
  SSdb *pSdb = pMnode->pSdb;
73,652,179✔
847
  if (pTrans != NULL) mInfo("vgId:1, trans:%d, release transaction", pTrans->id);
73,652,179✔
848
  sdbRelease(pSdb, pTrans);
73,652,179✔
849
}
73,652,179✔
850

851
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
16,087,867✔
852
                       const char *opername) {
853
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
16,087,867✔
854
  if (pTrans == NULL) {
16,087,867✔
855
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
856
    mError("failed to create transaction since %s", terrstr());
×
857
    return NULL;
×
858
  }
859

860
  if (opername != NULL) {
16,087,867✔
861
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
16,087,867✔
862
  }
863

864
  int32_t sdbMaxId = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
16,087,867✔
865
  sdbReadLock(pMnode->pSdb, SDB_TRANS);
16,087,867✔
866
  pTrans->id = TMAX(sdbMaxId, tsMaxTransId + 1);
16,087,867✔
867
  sdbUnLock(pMnode->pSdb, SDB_TRANS);
16,087,867✔
868
  pTrans->stage = TRN_STAGE_PREPARE;
16,087,867✔
869
  pTrans->policy = policy;
16,087,867✔
870
  pTrans->conflict = conflict;
16,087,867✔
871
  pTrans->exec = TRN_EXEC_PARALLEL;
16,087,867✔
872
  pTrans->ableToBeKilled = false;
16,087,867✔
873
  pTrans->createdTime = taosGetTimestampMs();
16,087,867✔
874
  pTrans->prepareActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
16,087,867✔
875
  pTrans->redoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
16,087,867✔
876
  pTrans->undoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
16,087,867✔
877
  pTrans->commitActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
16,087,867✔
878
  pTrans->redoGroupActions = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
16,087,867✔
879
  pTrans->groupActionPos = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
16,087,867✔
880
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
16,087,867✔
881
  pTrans->pRpcArray = taosArrayInit(1, sizeof(SRpcHandleInfo));
16,087,867✔
882
  pTrans->mTraceId = pReq ? TRACE_GET_ROOTID(&pReq->info.traceId) : tGenIdPI64();
16,087,867✔
883
  taosInitRWLatch(&pTrans->lockRpcArray);
16,087,867✔
884
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
16,087,867✔
885

886
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
16,087,867✔
887
      pTrans->pRpcArray == NULL) {
16,087,867✔
888
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
889
    mError("failed to create transaction since %s", terrstr());
×
890
    mndTransDrop(pTrans);
×
891
    return NULL;
×
892
  }
893

894
  if (pReq != NULL) {
16,087,867✔
895
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
22,107,148✔
896
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
897
      return NULL;
×
898
    }
899
    pTrans->originRpcType = pReq->msgType;
11,053,574✔
900
  }
901

902
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
16,087,867✔
903

904
  mTrace("trans:%d, local object is created, data:%p", pTrans->id, pTrans);
16,087,867✔
905
  return pTrans;
16,087,867✔
906
}
907

908
static void mndTransDropActions(SArray *pArray) {
426,110,820✔
909
  int32_t size = taosArrayGetSize(pArray);
426,110,820✔
910
  for (int32_t i = 0; i < size; ++i) {
1,148,822,084✔
911
    STransAction *pAction = taosArrayGet(pArray, i);
722,711,264✔
912
    if (pAction->actionType == TRANS_ACTION_RAW) {
722,711,264✔
913
      taosMemoryFreeClear(pAction->pRaw);
476,563,235✔
914
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
246,148,029✔
915
      taosMemoryFreeClear(pAction->pCont);
246,148,029✔
916
    } else {
917
      // nothing
918
    }
919
  }
920

921
  taosArrayDestroy(pArray);
426,110,820✔
922
}
426,110,820✔
923

924
void mndTransDrop(STrans *pTrans) {
16,190,507✔
925
  if (pTrans != NULL) {
16,190,507✔
926
    mndTransDropData(pTrans);
16,087,867✔
927
    mTrace("trans:%d, local object is freed, data:%p", pTrans->id, pTrans);
16,087,867✔
928
    taosMemoryFreeClear(pTrans);
16,087,867✔
929
  }
930
}
16,190,507✔
931

932
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
94,488,505✔
933
  pAction->id = taosArrayGetSize(pArray);
94,488,505✔
934

935
  void *ptr = taosArrayPush(pArray, pAction);
94,488,505✔
936
  if (ptr == NULL) {
94,488,505✔
937
    TAOS_RETURN(terrno);
×
938
  }
939

940
  return 0;
94,488,505✔
941
}
942

943
static int32_t mndTransAddActionToGroup(STrans *pTrans, STransAction *pAction) {
5,589,737✔
944
  if (pTrans->exec != TRN_EXEC_GROUP_PARALLEL) return 0;
5,589,737✔
945

946
  SArray **redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
514,448✔
947
  if (redoAction == NULL) {
514,448✔
948
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
222,686✔
949
    if (array != NULL) {
222,686✔
950
      if (taosHashPut(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
222,686✔
951
        mInfo("failed put action into redo group actions");
×
952
        return TSDB_CODE_INTERNAL_ERROR;
×
953
      }
954
      redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
222,686✔
955
    }
956
  }
957
  if (redoAction != NULL) {
514,448✔
958
    mInfo("trans:%d, append action into group %d, msgType:%s", pTrans->id, pAction->groupId,
514,448✔
959
          TMSG_INFO(pAction->msgType));
960
    void *ptr = taosArrayPush(*redoAction, &pAction);
514,448✔
961
    if (ptr == NULL) {
514,448✔
962
      TAOS_RETURN(terrno);
×
963
    }
964
  }
965

966
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &pAction->groupId, sizeof(int32_t));
514,448✔
967
  if (actionPos == NULL) {
514,448✔
968
    int32_t pos = 0;
222,686✔
969
    if (taosHashPut(pTrans->groupActionPos, &pAction->groupId, sizeof(int32_t), &pos, sizeof(int32_t)) < 0) {
222,686✔
970
      mInfo("failed put action into group action pos");
×
971
      return TSDB_CODE_INTERNAL_ERROR;
×
972
    }
973
  }
974

975
  return 0;
514,448✔
976
}
977

978
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
356,556✔
979
  STransAction action = {
356,556✔
980
      .stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw, .mTraceId = pTrans->mTraceId};
356,556✔
981
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) return TSDB_CODE_INTERNAL_ERROR;
356,556✔
982
  return mndTransAppendAction(pTrans->redoActions, &action);
356,556✔
983
}
984

985
int32_t mndTransAppendGroupRedolog(STrans *pTrans, SSdbRaw *pRaw, int32_t groupId) {
281,267✔
986
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION,
281,267✔
987
                         .actionType = TRANS_ACTION_RAW,
988
                         .pRaw = pRaw,
989
                         .mTraceId = pTrans->mTraceId,
281,267✔
990
                         .groupId = groupId};
991
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
281,267✔
992
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
×
993
    return TSDB_CODE_INTERNAL_ERROR;
×
994
  }
995
  if (groupId > 0) TAOS_CHECK_RETURN(mndTransAddActionToGroup(pTrans, &action));
281,267✔
996
  return mndTransAppendAction(pTrans->redoActions, &action);
281,267✔
997
}
998

999
int32_t mndTransAppendNullLog(STrans *pTrans) {
×
1000
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_NULL};
×
1001
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
×
1002
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
×
1003
    return TSDB_CODE_INTERNAL_ERROR;
×
1004
  }
1005
  return mndTransAppendAction(pTrans->redoActions, &action);
×
1006
}
1007

1008
int32_t mndTransAppendGroupNullLog(STrans *pTrans, int32_t groupId) {
×
1009
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_NULL, .groupId = groupId};
×
1010
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
×
1011
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
×
1012
    return TSDB_CODE_INTERNAL_ERROR;
×
1013
  }
1014
  if (groupId > 0) TAOS_CHECK_RETURN(mndTransAddActionToGroup(pTrans, &action));
×
1015
  return mndTransAppendAction(pTrans->redoActions, &action);
×
1016
}
1017

1018
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
3,563,116✔
1019
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
3,563,116✔
1020
  return mndTransAppendAction(pTrans->undoActions, &action);
3,563,116✔
1021
}
1022

1023
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
57,240,317✔
1024
  STransAction action = {.stage = TRN_STAGE_COMMIT_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
57,240,317✔
1025
  return mndTransAppendAction(pTrans->commitActions, &action);
57,240,317✔
1026
}
1027

1028
int32_t mndTransAppendPrepareLog(STrans *pTrans, SSdbRaw *pRaw) {
9,062,452✔
1029
  STransAction action = {
9,062,452✔
1030
      .pRaw = pRaw, .stage = TRN_STAGE_PREPARE, .actionType = TRANS_ACTION_RAW, .mTraceId = pTrans->mTraceId};
9,062,452✔
1031
  return mndTransAppendAction(pTrans->prepareActions, &action);
9,062,452✔
1032
}
1033

1034
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
17,641,637✔
1035
  pAction->stage = TRN_STAGE_REDO_ACTION;
17,641,637✔
1036
  pAction->actionType = TRANS_ACTION_MSG;
17,641,637✔
1037
  pAction->mTraceId = pTrans->mTraceId;
17,641,637✔
1038
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId == 0) {
17,641,637✔
1039
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
756✔
1040
    return TSDB_CODE_INTERNAL_ERROR;
756✔
1041
  }
1042
  if (pAction->groupId > 0) TAOS_CHECK_RETURN(mndTransAddActionToGroup(pTrans, pAction));
17,640,881✔
1043
  return mndTransAppendAction(pTrans->redoActions, pAction);
17,640,881✔
1044
}
1045

1046
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
6,343,916✔
1047
  pAction->stage = TRN_STAGE_UNDO_ACTION;
6,343,916✔
1048
  pAction->actionType = TRANS_ACTION_MSG;
6,343,916✔
1049
  return mndTransAppendAction(pTrans->undoActions, pAction);
6,343,916✔
1050
}
1051

1052
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
6,603,391✔
1053
  pTrans->rpcRsp = pCont;
6,603,391✔
1054
  pTrans->rpcRspLen = contLen;
6,603,391✔
1055
}
6,603,391✔
1056

1057
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
259,775✔
1058
  pTrans->startFunc = startFunc;
259,775✔
1059
  pTrans->stopFunc = stopFunc;
259,775✔
1060
  pTrans->param = param;
259,775✔
1061
  pTrans->paramLen = paramLen;
259,775✔
1062
}
259,775✔
1063

1064
int32_t mndSetRpcInfoForDbTrans(SMnode *pMnode, SRpcMsg *pMsg, EOperType oper, const char *dbname) {
×
1065
  STrans *pTrans = NULL;
×
1066
  void   *pIter = NULL;
×
1067
  int32_t code = -1;
×
1068

1069
  while (1) {
1070
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
×
1071
    if (pIter == NULL) break;
×
1072

1073
    if (pTrans->oper == oper) {
×
1074
      if (taosStrcasecmp(dbname, pTrans->dbname) == 0) {
×
1075
        mInfo("trans:%d, db:%s oper:%d matched with input", pTrans->id, dbname, oper);
×
1076
        taosWLockLatch(&pTrans->lockRpcArray);
×
1077
        if (pTrans->pRpcArray == NULL) {
×
1078
          pTrans->pRpcArray = taosArrayInit(4, sizeof(SRpcHandleInfo));
×
1079
        }
1080
        if (pTrans->pRpcArray != NULL && taosArrayPush(pTrans->pRpcArray, &pMsg->info) != NULL) {
×
1081
          code = 0;
×
1082
        }
1083
        taosWUnLockLatch(&pTrans->lockRpcArray);
×
1084

1085
        sdbRelease(pMnode->pSdb, pTrans);
×
1086
        break;
×
1087
      }
1088
    }
1089

1090
    sdbRelease(pMnode->pSdb, pTrans);
×
1091
  }
1092
  return code;
×
1093
}
1094

1095
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
9,459,398✔
1096
  if (dbname != NULL) {
9,459,398✔
1097
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
9,459,398✔
1098
  }
1099
  if (stbname != NULL) {
9,459,398✔
1100
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
7,317,621✔
1101
  }
1102
}
9,459,398✔
1103

1104
void mndTransSetUserData(STrans *pTrans, void* data, int32_t dataLen) {
92✔
1105
  if (pTrans->userData != NULL) {
92✔
1106
    taosMemoryFree(pTrans->userData);
×
1107
  }
1108
  pTrans->userData = data;
92✔
1109
  pTrans->userDataLen = dataLen;
92✔
1110
}
92✔
1111

1112
void mndTransAddArbGroupId(STrans *pTrans, int32_t groupId) {
34,439✔
1113
  if (taosHashPut(pTrans->arbGroupIds, &groupId, sizeof(int32_t), NULL, 0) != 0) {
34,439✔
1114
    mError("trans:%d, failed to put groupid into hash, groupId:%d", pTrans->id, groupId);
×
1115
  }
1116
}
34,439✔
1117

1118
void mndTransSetSerial(STrans *pTrans) {
233,762✔
1119
  mInfo("trans:%d, set Serial", pTrans->id);
233,762✔
1120
  pTrans->exec = TRN_EXEC_SERIAL;
233,762✔
1121
}
233,762✔
1122

1123
void mndTransSetGroupParallel(STrans *pTrans) {
92,605✔
1124
  mInfo("trans:%d, set Group Parallel", pTrans->id);
92,605✔
1125
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
92,605✔
1126
}
92,605✔
1127

1128
void mndTransSetParallel(STrans *pTrans) {
×
1129
  mInfo("trans:%d, set Parallel", pTrans->id);
×
1130
  pTrans->exec = TRN_EXEC_PARALLEL;
×
1131
}
×
1132

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

1135
void mndTransSetKillMode(STrans *pTrans, ETrnKillMode killMode) {
22,003✔
1136
  pTrans->ableToBeKilled = true; 
22,003✔
1137
  pTrans->killMode = killMode; 
22,003✔
1138
}
22,003✔
1139

1140
void mndTransSetChangeless(STrans *pTrans) { pTrans->changeless = true; }
13,258✔
1141

1142
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
1,105,826✔
1143

1144
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
33,868,789✔
1145
  int32_t  code = 0;
33,868,789✔
1146
  SSdbRaw *pRaw = mndTransEncode(pTrans);
33,868,789✔
1147
  if (pRaw == NULL) {
33,868,789✔
1148
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1149
    if (terrno != 0) code = terrno;
×
1150
    mError("trans:%d, failed to encode while sync trans since %s", pTrans->id, tstrerror(code));
×
1151
    TAOS_RETURN(code);
×
1152
  }
1153
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_READY));
33,868,789✔
1154

1155
  mInfo("trans:%d, sync to other mnodes, stage:%s createTime:%" PRId64, pTrans->id, mndTransStr(pTrans->stage),
33,868,789✔
1156
        pTrans->createdTime);
1157
  code = mndSyncPropose(pMnode, pRaw, pTrans->id);
33,868,789✔
1158
  if (code != 0) {
33,868,789✔
1159
    mError("trans:%d, failed to sync, errno:%s code:0x%x createTime:%" PRId64 " saved trans:%d", pTrans->id,
1,444✔
1160
           tstrerror(code), code, pTrans->createdTime, pMnode->syncMgmt.transId);
1161
    sdbFreeRaw(pRaw);
1,444✔
1162
    TAOS_RETURN(code);
1,444✔
1163
  }
1164

1165
  sdbFreeRaw(pRaw);
33,867,345✔
1166
  mInfo("trans:%d, sync to other mnodes finished, createTime:%" PRId64, pTrans->id, pTrans->createdTime);
33,867,345✔
1167
  TAOS_RETURN(code);
33,867,345✔
1168
}
1169

1170
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
157,540✔
1171
  if (conflict[0] == 0) return false;
157,540✔
1172
  if (taosStrcasecmp(conflict, pTrans->dbname) == 0) return true;
157,540✔
1173
  return false;
97,025✔
1174
}
1175

1176
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
5,497,175✔
1177
  if (conflict[0] == 0) return false;
5,497,175✔
1178
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
5,403,234✔
1179
  return false;
5,194,112✔
1180
}
1181

1182
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
5,654,715✔
1183
  if (conflict) {
5,654,715✔
1184
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
269,637✔
1185
      "stb:%s type:%d", 
1186
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1187
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1188
    *globalConflict = true;
269,637✔
1189
  } else {
1190
    mInfo("trans:%d, opername:%s db:%s stb:%s type:%d, not conflict with trans:%d, opername:%s db:%s stb:%s type:%d", 
5,385,078✔
1191
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1192
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1193
  }
1194
}
5,654,715✔
1195

1196
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
42,452,678✔
1197
  STrans *pTrans = NULL;
42,452,678✔
1198
  void   *pIter = NULL;
42,452,678✔
1199
  bool    conflict = false;
42,452,678✔
1200

1201
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
42,452,678✔
1202

1203
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
33,023,167✔
1204
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
33,023,167✔
1205

1206
  while (1) {
1207
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
38,565,575✔
1208
    if (pIter == NULL) break;
38,565,575✔
1209

1210
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
5,542,408✔
1211

1212
    if (pNew->conflict == TRN_CONFLICT_DB) {
5,542,408✔
1213
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
95,134✔
1214
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
95,134✔
1215
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
93,941✔
1216
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
93,941✔
1217
      }
1218
    }
1219

1220
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
5,542,408✔
1221
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
5,417,722✔
1222
      if (pTrans->conflict == TRN_CONFLICT_DB) {
5,417,722✔
1223
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
63,599✔
1224
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
63,599✔
1225
      }
1226
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
5,417,722✔
1227
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
5,339,635✔
1228
      }
1229
    }
1230

1231
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
5,542,408✔
1232
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
24,817✔
1233
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
24,817✔
1234
        void *pGidIter = taosHashIterate(pNew->arbGroupIds, NULL);
×
1235
        while (pGidIter != NULL) {
×
1236
          int32_t groupId = *(int32_t *)pGidIter;
×
1237
          if (taosHashGet(pTrans->arbGroupIds, &groupId, sizeof(int32_t)) != NULL) {
×
1238
            taosHashCancelIterate(pNew->arbGroupIds, pGidIter);
×
1239
            mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1240
            break;
×
1241
          } else {
1242
            mndTransLogConflict(pNew, pTrans, false, &conflict);
×
1243
          }
1244
          pGidIter = taosHashIterate(pNew->arbGroupIds, pGidIter);
×
1245
        }
1246
      }
1247
    }
1248

1249
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
5,542,408✔
1250
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
×
1251
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1252
      } else {
1253
        mndTransLogConflict(pNew, pTrans, false, &conflict);
×
1254
      }
1255
    }
1256

1257
    sdbRelease(pMnode->pSdb, pTrans);
5,542,408✔
1258
  }
1259

1260
  return conflict;
33,023,167✔
1261
}
1262

1263
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
42,452,678✔
1264
  int32_t code = 0;
42,452,678✔
1265
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
42,452,678✔
1266
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
28,370,345✔
1267
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
1268
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
1269
      TAOS_RETURN(code);
×
1270
    }
1271
  }
1272

1273
  if (mndCheckTransConflict(pMnode, pTrans)) {
42,452,678✔
1274
    code = TSDB_CODE_MND_TRANS_CONFLICT;
306,144✔
1275
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
306,144✔
1276
    TAOS_RETURN(code);
306,144✔
1277
  }
1278

1279
  TAOS_RETURN(code);
42,146,534✔
1280
}
1281

1282
int32_t mndTransCheckConflictWithCompact(SMnode *pMnode, STrans *pTrans) {
143,755✔
1283
  int32_t      code = 0;
143,755✔
1284
  void        *pIter = NULL;
143,755✔
1285
  bool         conflict = false;
143,755✔
1286
  SCompactObj *pCompact = NULL;
143,755✔
1287

1288
  while (1) {
328✔
1289
    bool thisConflict = false;
144,083✔
1290
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
144,083✔
1291
    if (pIter == NULL) break;
144,083✔
1292

1293
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
328✔
1294
      thisConflict = true;
164✔
1295
    }
1296
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
328✔
1297
      if (taosStrcasecmp(pTrans->dbname, pCompact->dbname) == 0) thisConflict = true;
164✔
1298
    }
1299

1300
    if (thisConflict) {
328✔
1301
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
328✔
1302
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1303
      conflict = true;
328✔
1304
    } else {
1305
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with compact:%d db:%s", pTrans->id, pTrans->dbname,
×
1306
            pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1307
    }
1308
    sdbRelease(pMnode->pSdb, pCompact);
328✔
1309
  }
1310

1311
  if (conflict) {
143,755✔
1312
    code = TSDB_CODE_MND_TRANS_CONFLICT_COMPACT;
328✔
1313
    mError("trans:%d, failed to check tran conflict with compact since %s", pTrans->id, tstrerror(code));
328✔
1314
    TAOS_RETURN(code);
328✔
1315
  }
1316

1317
  TAOS_RETURN(code);
143,427✔
1318
}
1319

1320
int32_t mndTransCheckConflictWithRetention(SMnode *pMnode, STrans *pTrans) {
143,427✔
1321
  int32_t        code = 0;
143,427✔
1322
  SSdb          *pSdb = pMnode->pSdb;
143,427✔
1323
  void          *pIter = NULL;
143,427✔
1324
  bool           conflict = false;
143,427✔
1325
  SRetentionObj *pRetention = NULL;
143,427✔
1326

1327
  while ((pIter = sdbFetch(pSdb, SDB_RETENTION, pIter, (void **)&pRetention)) != NULL) {
143,427✔
1328
    conflict = false;
×
1329

1330
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
×
1331
      conflict = true;
×
1332
    }
1333
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
×
1334
      if (taosStrcasecmp(pTrans->dbname, pRetention->dbname) == 0) conflict = true;
×
1335
    }
1336

1337
    if (conflict) {
×
1338
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with retention:%d db:%s", pTrans->id,
×
1339
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pRetention->id, pRetention->dbname);
1340
      sdbRelease(pSdb, pRetention);
×
1341
      sdbCancelFetch(pSdb, pIter);
×
1342
      break;
×
1343
    } else {
1344
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with retention:%d db:%s", pTrans->id, pTrans->dbname,
×
1345
            pTrans->stbname, pTrans->conflict, pRetention->id, pRetention->dbname);
1346
    }
1347
    sdbRelease(pSdb, pRetention);
×
1348
  }
1349

1350
  if (conflict) {
143,427✔
1351
    code = TSDB_CODE_MND_TRANS_CONFLICT_RETENTION;
×
1352
    mError("trans:%d, failed to check tran conflict with retention since %s", pTrans->id, tstrerror(code));
×
1353
    TAOS_RETURN(code);
×
1354
  }
1355

1356
  TAOS_RETURN(code);
143,427✔
1357
}
1358

1359
static bool mndTransActionsOfSameType(SArray *pActions) {
35,336,160✔
1360
  int32_t size = taosArrayGetSize(pActions);
35,336,160✔
1361
  ETrnAct lastActType = TRANS_ACTION_NULL;
35,336,160✔
1362
  bool    same = true;
35,336,160✔
1363
  for (int32_t i = 0; i < size; ++i) {
111,992,073✔
1364
    STransAction *pAction = taosArrayGet(pActions, i);
76,655,913✔
1365
    if (i > 0) {
76,655,913✔
1366
      if (lastActType != pAction->actionType) {
50,612,594✔
1367
        same = false;
×
1368
        break;
×
1369
      }
1370
    }
1371
    lastActType = pAction->actionType;
76,655,913✔
1372
  }
1373
  return same;
35,336,160✔
1374
}
1375

1376
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
15,763,588✔
1377
  int32_t code = 0;
15,763,588✔
1378
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
15,763,588✔
1379
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
15,448,083✔
1380
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1381
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1382
      TAOS_RETURN(code);
×
1383
    }
1384

1385
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
15,448,083✔
1386
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
4,124,489✔
1387
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1388
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1389
        TAOS_RETURN(code);
×
1390
      }
1391
    }
1392
  }
1393

1394
  TAOS_RETURN(code);
15,763,588✔
1395
}
1396

1397
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
15,763,588✔
1398
  int32_t code = 0;
15,763,588✔
1399
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
15,763,588✔
1400
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
×
1401
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
×
1402
    TAOS_RETURN(code);
×
1403
  }
1404
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
15,763,588✔
1405
    code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1406
    mError("trans:%d, types of commit actions are not the same", pTrans->id);
×
1407
    TAOS_RETURN(code);
×
1408
  }
1409

1410
  TAOS_RETURN(code);
15,763,588✔
1411
}
1412

1413
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
15,764,408✔
1414
  int32_t code = 0;
15,764,408✔
1415
  if (pTrans == NULL) {
15,764,408✔
1416
    TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
1417
  }
1418

1419
  mInfo("trans:%d, action list:", pTrans->id);
15,764,408✔
1420
  int32_t index = 0;
15,764,408✔
1421
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
24,816,559✔
1422
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
9,052,151✔
1423
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
9,052,151✔
1424
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1425
  }
1426

1427
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
33,968,470✔
1428
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
18,204,062✔
1429
    if (pAction->actionType == TRANS_ACTION_MSG) {
18,204,062✔
1430
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
17,587,427✔
1431
            TMSG_INFO(pAction->msgType));
1432
      ;
1433
    } else {
1434
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
616,635✔
1435
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1436
    }
1437
  }
1438

1439
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
72,970,077✔
1440
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
57,205,669✔
1441
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
57,205,669✔
1442
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1443
  }
1444

1445
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
25,671,440✔
1446
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
9,907,032✔
1447
    if (pAction->actionType == TRANS_ACTION_MSG) {
9,907,032✔
1448
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
6,343,916✔
1449
            TMSG_INFO(pAction->msgType));
1450
    } else {
1451
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
3,563,116✔
1452
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1453
    }
1454
  }
1455

1456
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
15,764,408✔
1457

1458
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
15,763,588✔
1459

1460
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
15,763,588✔
1461

1462
  mInfo("trans:%d, prepare transaction", pTrans->id);
15,763,588✔
1463
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
15,763,588✔
1464
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
1465
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
×
1466
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
×
1467
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
×
1468
    TAOS_RETURN(code);
×
1469
  }
1470
  mInfo("trans:%d, prepare finished", pTrans->id);
15,763,588✔
1471

1472
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
15,763,588✔
1473
  if (pNew == NULL) {
15,763,588✔
1474
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1475
    if (terrno != 0) code = terrno;
×
1476
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, tstrerror(code));
×
1477
    TAOS_RETURN(code);
×
1478
  }
1479

1480
  pNew->pRpcArray = pTrans->pRpcArray;
15,763,588✔
1481
  pNew->rpcRsp = pTrans->rpcRsp;
15,763,588✔
1482
  pNew->rpcRspLen = pTrans->rpcRspLen;
15,763,588✔
1483
  pNew->mTraceId = pTrans->mTraceId;
15,763,588✔
1484
  pTrans->pRpcArray = NULL;
15,763,588✔
1485
  pTrans->rpcRsp = NULL;
15,763,588✔
1486
  pTrans->rpcRspLen = 0;
15,763,588✔
1487

1488
  mInfo("trans:%d, execute transaction in prepare", pTrans->id);
15,763,588✔
1489
  mndTransExecute(pMnode, pNew, false);
15,763,588✔
1490
  mndReleaseTrans(pMnode, pNew);
15,763,588✔
1491
  // TDOD change to TAOS_RETURN(code);
1492
  TAOS_RETURN(0);
15,763,588✔
1493
}
1494

1495
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
15,761,367✔
1496
  int32_t code = 0;
15,761,367✔
1497
  mInfo("trans:%d, commit transaction", pTrans->id);
15,761,367✔
1498
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
15,761,367✔
1499
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
1,444✔
1500
    TAOS_RETURN(code);
1,444✔
1501
  }
1502
  mInfo("trans:%d, commit finished", pTrans->id);
15,759,923✔
1503
  TAOS_RETURN(code);
15,759,923✔
1504
}
1505

1506
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
1,462✔
1507
  int32_t code = 0;
1,462✔
1508
  mInfo("trans:%d, rollback transaction", pTrans->id);
1,462✔
1509
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,462✔
1510
    mError("trans:%d, failed to rollback since %s", pTrans->id, tstrerror(code));
×
1511
    TAOS_RETURN(code);
×
1512
  }
1513
  mInfo("trans:%d, rollback finished", pTrans->id);
1,462✔
1514
  TAOS_RETURN(code);
1,462✔
1515
}
1516

1517
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
1,584✔
1518
  int32_t code = 0;
1,584✔
1519
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
1,584✔
1520
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,584✔
1521
    mError("trans:%d, failed to pre-finish since %s", pTrans->id, tstrerror(code));
×
1522
    TAOS_RETURN(code);
×
1523
  }
1524
  mInfo("trans:%d, pre-finish finished", pTrans->id);
1,584✔
1525
  TAOS_RETURN(code);
1,584✔
1526
}
1527

1528
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
73,652,018✔
1529
  bool    sendRsp = false;
73,652,018✔
1530
  int32_t code = pTrans->code;
73,652,018✔
1531

1532
  if (pTrans->stage == TRN_STAGE_FINISH) {
73,652,018✔
1533
    sendRsp = true;
32,856,286✔
1534
  }
1535

1536
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
73,652,018✔
1537
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
17,418,482✔
1538
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
8,772✔
1539
      sendRsp = true;
8,772✔
1540
    }
1541
  } else {
1542
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
56,233,536✔
1543
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
32,193,240✔
1544
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1545
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1546
      } else {
1547
        if (pTrans->failedTimes > 6) sendRsp = true;
32,193,240✔
1548
      }
1549
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
32,193,240✔
1550
    }
1551
  }
1552

1553
  if (!sendRsp) {
73,652,018✔
1554
    return;
40,786,960✔
1555
  } else {
1556
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
32,865,058✔
1557
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1558
  }
1559

1560
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
32,865,058✔
1561
  taosWLockLatch(&pTrans->lockRpcArray);
32,865,058✔
1562
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
32,865,058✔
1563
  if (size <= 0) {
32,865,058✔
1564
    taosWUnLockLatch(&pTrans->lockRpcArray);
22,114,833✔
1565
    return;
22,114,833✔
1566
  }
1567

1568
  for (int32_t i = 0; i < size; ++i) {
21,500,450✔
1569
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
10,750,225✔
1570
    if (pInfo->handle != NULL) {
10,750,225✔
1571
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
10,361,302✔
1572
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1573
      }
1574
      if (code == TSDB_CODE_SYN_TIMEOUT) {
10,361,302✔
1575
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1576
      }
1577

1578
      if (i != 0 && code == 0) {
10,361,302✔
1579
        code = TSDB_CODE_MNODE_NOT_FOUND;
×
1580
      }
1581
      mInfo("vgId:1, trans:%d, client:%d start to send rsp, code:0x%x stage:%s app:%p", pTrans->id, i, code,
10,361,302✔
1582
            mndTransStr(pTrans->stage), pInfo->ahandle);
1583

1584
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
10,361,302✔
1585

1586
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
10,361,302✔
1587
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,085,322✔
1588
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,085,322✔
1589
        if (pDb != NULL) {
1,085,322✔
1590
          for (int32_t j = 0; j < 12; j++) {
1,403,763✔
1591
            bool ready = mndIsDbReady(pMnode, pDb);
1,403,282✔
1592
            if (!ready) {
1,403,282✔
1593
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
318,441✔
1594
              taosMsleep(1000);
318,441✔
1595
            } else {
1596
              break;
1,084,841✔
1597
            }
1598
          }
1599
        }
1600
        mndReleaseDb(pMnode, pDb);
1,085,322✔
1601
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
9,275,980✔
1602
        void   *pCont = NULL;
1,626,041✔
1603
        int32_t contLen = 0;
1,626,041✔
1604
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
1,626,041✔
1605
          mndTransSetRpcRsp(pTrans, pCont, contLen);
1,624,579✔
1606
        }
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
7,649,939✔
1608
        void   *pCont = NULL;
2,442✔
1609
        int32_t contLen = 0;
2,442✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,442✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,442✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
7,647,497✔
1614
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
8,640✔
1615
        if (code != 0) {
8,640✔
1616
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1617
        }
1618
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
7,638,857✔
1619
        void   *pCont = NULL;
46✔
1620
        int32_t contLen = 0;
46✔
1621
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
46✔
1622
          mndTransSetRpcRsp(pTrans, pCont, contLen);
46✔
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
7,638,811✔
1625
        void   *pCont = NULL;
46✔
1626
        int32_t contLen = 0;
46✔
1627
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
46✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
46✔
1629
        }
1630
      }
1631

1632
      if (pTrans->rpcRspLen != 0) {
10,361,302✔
1633
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
6,603,391✔
1634
        if (rpcCont != NULL) {
6,603,391✔
1635
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
6,603,391✔
1636
          rspMsg.pCont = rpcCont;
6,603,391✔
1637
          rspMsg.contLen = pTrans->rpcRspLen;
6,603,391✔
1638
        }
1639
      }
1640

1641
      tmsgSendRsp(&rspMsg);
10,361,302✔
1642

1643
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
10,361,302✔
1644
            mndTransStr(pTrans->stage), pInfo->ahandle);
1645
    }
1646
  }
1647
  taosArrayClear(pTrans->pRpcArray);
10,750,225✔
1648
  taosWUnLockLatch(&pTrans->lockRpcArray);
10,750,225✔
1649
}
1650

1651
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
19,541,999✔
1652
  int32_t code = 0;
19,541,999✔
1653
  SMnode *pMnode = pRsp->info.node;
19,541,999✔
1654
#ifndef TD_ASTRA_32
1655
  int64_t signature = (int64_t)(pRsp->info.ahandle);
19,541,999✔
1656
  int32_t transId = (int32_t)(signature >> 32);
19,541,999✔
1657
  int32_t action = (int32_t)((signature << 32) >> 32);
19,541,999✔
1658
#else
1659
  int32_t transId = (int32_t)(pRsp->info.ahandle);
1660
  int32_t action = (int32_t)(pRsp->info.ahandleEx);
1661
#endif
1662
  STraceId* trace = &(pRsp->info.traceId);
19,541,999✔
1663
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
19,541,999✔
1664
  if (pTrans == NULL) {
19,541,999✔
UNCOV
1665
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
1666
    if (terrno != 0) code = terrno;
×
UNCOV
1667
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
UNCOV
1668
    goto _OVER;
×
1669
  }
1670

1671
  SArray *pArray = NULL;
19,541,999✔
1672
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
19,541,999✔
1673
    pArray = pTrans->redoActions;
19,536,151✔
1674
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
5,848✔
1675
    pArray = pTrans->undoActions;
5,848✔
1676
  } else {
UNCOV
1677
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
UNCOV
1678
    goto _OVER;
×
1679
  }
1680

1681
  if (pArray == NULL) {
19,541,999✔
UNCOV
1682
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1683
    goto _OVER;
×
1684
  }
1685

1686
  int32_t actionNum = taosArrayGetSize(pArray);
19,541,999✔
1687
  if (action < 0 || action >= actionNum) {
19,541,999✔
1688
    mError("trans:%d, invalid action:%d", transId, action);
×
1689
    goto _OVER;
×
1690
  }
1691

1692
  STransAction *pAction = taosArrayGet(pArray, action);
19,541,999✔
1693
  if (pAction != NULL) {
19,541,999✔
1694
    if (pAction->msgSent) {
19,541,999✔
1695
      pAction->msgReceived = 1;
19,541,999✔
1696
      pAction->errCode = pRsp->code;
19,541,999✔
1697
      pAction->endTime = taosGetTimestampMs();
19,541,999✔
1698

1699
      // pTrans->lastErrorNo = pRsp->code;
1700
      mndSetTransLastAction(pTrans, pAction);
19,541,999✔
1701

1702
      mInfo("trans:%d, %s:%d response is received, msgType:%s, QID:0x%" PRIx64 ":0x%" PRIx64 ", received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
19,541,999✔
1703
            mndTransStr(pAction->stage), action, TMSG_INFO(pAction->msgType), trace ? trace->rootId : 0, 
1704
              trace ? trace->msgId : 0, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1705
            tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1706
    } else {
UNCOV
1707
      mWarn("trans:%d, %s:%d response is received, but msgSent is false, code:0x%x(%s), accept:0x%x(%s) retry:0x%x", 
×
1708
            transId, mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code),
1709
            pAction->acceptableCode, tstrerror(pAction->acceptableCode), pAction->retryCode);
1710
    }
1711

1712
  } else {
1713
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1714
  }
1715

1716
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
19,541,999✔
1717
  mndTransExecute(pMnode, pTrans, true);
19,541,999✔
1718

1719
_OVER:
19,541,999✔
1720
  mndReleaseTrans(pMnode, pTrans);
19,541,999✔
1721
  TAOS_RETURN(code);
19,541,999✔
1722
}
1723

1724
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
1,997,177✔
1725
  pAction->rawWritten = 0;
1,997,177✔
1726
  pAction->msgSent = 0;
1,997,177✔
1727
  pAction->msgReceived = 0;
1,997,177✔
1728
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
1,997,177✔
1729
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
1,997,177✔
1730
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
1,435✔
1731
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
1,435✔
1732
          pAction->id, pAction->epSet.inUse);
1733
  } else {
1734
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,995,742✔
1735
  }
1736
  //  pAction->errCode = 0;
1737
}
1,997,177✔
1738

1739
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
1,462✔
1740
  int32_t numOfActions = taosArrayGetSize(pArray);
1,462✔
1741

1742
  for (int32_t action = 0; action < numOfActions; ++action) {
7,310✔
1743
    STransAction *pAction = taosArrayGet(pArray, action);
5,848✔
1744
    if (pAction->msgSent && pAction->msgReceived &&
5,848✔
1745
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
5,848✔
1746
      continue;
4,386✔
1747
    if (pAction->msgSent && !pAction->msgReceived) {
1,462✔
UNCOV
1748
      int64_t timestamp = taosGetTimestampMs();
×
UNCOV
1749
      if (timestamp - pAction->startTime <= TRANS_ACTION_TIMEOUT) continue;
×
1750
    }
1751

1752
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
1,462✔
1753

1754
    mndTransResetAction(pMnode, pTrans, pAction);
1,462✔
1755
    mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage), pAction->id,
1,462✔
1756
          pAction->errCode, pAction->startTime);
1757
  }
1758
}
1,462✔
1759

1760
// execute in sync context
1761
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
128,894,031✔
1762
  if (pAction->rawWritten) return 0;
128,894,031✔
1763
  if (topHalf) {
71,100,767✔
1764
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
650✔
1765
  }
1766

1767
  if (pAction->pRaw->type >= SDB_MAX) {
71,100,117✔
UNCOV
1768
    pAction->rawWritten = true;
×
UNCOV
1769
    pAction->errCode = 0;
×
UNCOV
1770
    mndSetTransLastAction(pTrans, pAction);
×
UNCOV
1771
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
UNCOV
1772
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1773
  }
1774

1775
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
71,100,117✔
1776
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
71,100,117✔
1777
    pAction->rawWritten = true;
71,100,117✔
1778
    pAction->errCode = 0;
71,100,117✔
1779
    code = 0;
71,100,117✔
1780
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
71,100,117✔
1781
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1782

1783
    mndSetTransLastAction(pTrans, pAction);
71,100,117✔
1784
  } else {
UNCOV
1785
    pAction->errCode = (terrno != 0) ? terrno : code;
×
UNCOV
1786
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1787
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
UNCOV
1788
    mndSetTransLastAction(pTrans, pAction);
×
1789
  }
1790

1791
  TAOS_RETURN(code);
71,100,117✔
1792
}
1793

1794
// execute in trans context
1795
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
84,058,736✔
1796
                                     bool notSend) {
1797
  if (pAction->msgSent) return 0;
84,058,736✔
1798
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
31,719,656✔
1799
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
12,165,658✔
1800
  }
1801

1802
  if (notSend) {
19,553,998✔
1803
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
9,503✔
1804
    return 0;
9,503✔
1805
  }
1806

1807
#ifndef TD_ASTRA_32
1808
  int64_t signature = pTrans->id;
19,544,495✔
1809
  signature = (signature << 32);
19,544,495✔
1810
  signature += pAction->id;
19,544,495✔
1811

1812
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
19,544,495✔
1813
#else
1814
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1815
                    .contLen = pAction->contLen,
1816
                    .info.ahandle = (void *)pTrans->id,
1817
                    .info.ahandleEx = (void *)pAction->id};
1818
#endif
1819
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
19,544,495✔
1820
  if (rpcMsg.pCont == NULL) {
19,544,495✔
UNCOV
1821
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1822
    return -1;
×
1823
  }
1824
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
19,544,495✔
1825
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
19,544,495✔
1826
  rpcMsg.info.notFreeAhandle = 1;
19,544,495✔
1827
  STraceId* trace = &(rpcMsg.info.traceId);
19,544,495✔
1828

1829
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
19,544,495✔
1830

1831
  char    detail[1024] = {0};
19,544,495✔
1832
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
19,544,495✔
1833
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
19,544,495✔
1834
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
41,811,156✔
1835
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
22,266,661✔
1836
                     pAction->epSet.eps[i].port);
22,266,661✔
1837
  }
1838

1839
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
19,544,495✔
1840
  if (code == 0) {
19,544,495✔
1841
    pAction->msgSent = 1;
19,544,495✔
1842
    // pAction->msgReceived = 0;
1843
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
19,544,495✔
1844
    pAction->startTime = taosGetTimestampMs();
19,544,495✔
1845
    pAction->endTime = 0;
19,544,495✔
1846
    mInfo("trans:%d, %s:%d is sent, QID:0x%" PRIx64 ":0x%" PRIx64 ", %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, trace ? trace->rootId : 0, 
19,544,495✔
1847
              trace ? trace->msgId : 0, detail);
1848

1849
    mndSetTransLastAction(pTrans, pAction);
19,544,495✔
1850
  } else {
UNCOV
1851
    pAction->msgSent = 0;
×
UNCOV
1852
    pAction->msgReceived = 0;
×
UNCOV
1853
    pAction->errCode = (terrno != 0) ? terrno : code;
×
UNCOV
1854
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
×
1855
           detail);
1856

1857
    mndSetTransLastAction(pTrans, pAction);
×
1858
  }
1859

1860
  TAOS_RETURN(code);
19,544,495✔
1861
}
1862

1863
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
UNCOV
1864
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
UNCOV
1865
  pAction->rawWritten = 0;
×
UNCOV
1866
  pAction->errCode = 0;
×
UNCOV
1867
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1868

1869
  mndSetTransLastAction(pTrans, pAction);
×
1870
  return 0;
×
1871
}
1872

1873
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
212,952,767✔
1874
                                        bool notSend) {
1875
  if (pAction->actionType == TRANS_ACTION_RAW) {
212,952,767✔
1876
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
128,894,031✔
1877
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
84,058,736✔
1878
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
84,058,736✔
1879
  } else {
UNCOV
1880
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1881
  }
1882
}
1883

1884
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
67,240,631✔
1885
  int32_t numOfActions = taosArrayGetSize(pArray);
67,240,631✔
1886
  int32_t code = 0;
67,240,631✔
1887

1888
  for (int32_t action = 0; action < numOfActions; ++action) {
247,792,935✔
1889
    STransAction *pAction = taosArrayGet(pArray, action);
189,999,805✔
1890
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
189,999,805✔
1891
    if (code != 0) {
189,999,805✔
1892
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
9,447,501✔
1893
            numOfActions);
1894
      break;
9,447,501✔
1895
    }
1896
  }
1897

1898
  return code;
67,240,631✔
1899
}
1900

1901
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
81,311,462✔
1902
  int32_t numOfActions = taosArrayGetSize(pArray);
81,311,462✔
1903
  int32_t code = 0;
81,311,462✔
1904
  if (numOfActions == 0) return 0;
81,311,462✔
1905

1906
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
67,240,631✔
1907
    return code;
9,447,501✔
1908
  }
1909

1910
  int32_t       numOfExecuted = 0;
57,793,130✔
1911
  int32_t       errCode = 0;
57,793,130✔
1912
  STransAction *pErrAction = NULL;
57,793,130✔
1913
  for (int32_t action = 0; action < numOfActions; ++action) {
238,345,434✔
1914
    STransAction *pAction = taosArrayGet(pArray, action);
180,552,304✔
1915
    if (pAction->msgReceived || pAction->rawWritten) {
180,552,304✔
1916
      numOfExecuted++;
148,663,540✔
1917
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
148,663,540✔
1918
        errCode = pAction->errCode;
2,924✔
1919
        pErrAction = pAction;
2,924✔
1920
      }
1921
    } else {
1922
      pErrAction = pAction;
31,888,764✔
1923
    }
1924
  }
1925

1926
  mndSetTransLastAction(pTrans, pErrAction);
57,793,130✔
1927

1928
  if (numOfExecuted == numOfActions) {
57,793,130✔
1929
    if (errCode == 0) {
41,532,972✔
1930
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
41,531,510✔
1931
      return 0;
41,531,510✔
1932
    } else {
1933
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,462✔
1934
      mndTransResetActions(pMnode, pTrans, pArray);
1,462✔
1935
      terrno = errCode;
1,462✔
1936
      return errCode;
1,462✔
1937
    }
1938
  } else {
1939
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
16,260,158✔
1940

1941
    for (int32_t action = 0; action < numOfActions; ++action) {
63,232,374✔
1942
      STransAction *pAction = taosArrayGet(pArray, action);
46,972,216✔
1943
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
46,972,216✔
1944
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1945
             pAction->acceptableCode, pAction->retryCode);
1946
      if (pAction->msgSent) {
46,972,216✔
1947
        bool reset = false;
46,962,713✔
1948
        if (pAction->msgReceived) {
46,962,713✔
1949
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
15,083,452✔
1950
        } else {
1951
          int64_t timestamp = taosGetTimestampMs();
31,879,261✔
1952
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
31,879,261✔
1953
        }
1954
        if (reset) {
46,962,713✔
1955
          mndTransResetAction(pMnode, pTrans, pAction);
1,462✔
1956
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1,462✔
1957
                pAction->id, pAction->errCode, pAction->startTime);
1958
        }
1959
      }
1960
    }
1961
    return TSDB_CODE_ACTION_IN_PROGRESS;
16,260,158✔
1962
  }
1963
}
1964

1965
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
48,448,316✔
1966
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
48,448,316✔
1967
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
48,448,316✔
1968
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
1,462✔
1969
           mndStrExecutionContext(topHalf));
1970
  }
1971
  return code;
48,448,316✔
1972
}
1973

1974
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
10,234✔
1975
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf, notSend);
10,234✔
1976
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
10,234✔
UNCOV
1977
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1978
           mndStrExecutionContext(topHalf));
1979
  }
1980
  return code;
10,234✔
1981
}
1982

1983
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
32,852,912✔
1984
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
32,852,912✔
1985
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
32,852,912✔
UNCOV
1986
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1987
           mndStrExecutionContext(topHalf));
1988
  }
1989
  return code;
32,852,912✔
1990
}
1991

1992
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
6,709,453✔
1993
  int32_t code = 0;
6,709,453✔
1994
  int32_t numOfActions = taosArrayGetSize(pActions);
6,709,453✔
1995
  if (numOfActions == 0) return code;
6,709,453✔
1996

1997
  if (pTrans->actionPos >= numOfActions) {
6,708,499✔
1998
    return code;
258,715✔
1999
  }
2000

2001
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
6,449,784✔
2002
        pTrans->actionPos, mndTransStr(pTrans->stage));
2003

2004
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
10,262,557✔
2005
    STransAction *pAction = taosArrayGet(pActions, action);
10,037,581✔
2006

2007
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
10,037,581✔
2008
      code = TSDB_CODE_ACTION_IN_PROGRESS;
7,244✔
2009
      break;
7,340✔
2010
    }
2011

2012
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
10,030,337✔
2013
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
2014

2015
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
10,030,337✔
2016
    if (code == 0) {
10,030,337✔
2017
      if (pAction->msgSent) {
8,572,254✔
2018
        bool reset = false;
7,580,573✔
2019
        if (pAction->msgReceived) {
7,580,573✔
2020
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,334,924✔
2021
            code = pAction->errCode;
1,961,348✔
2022
            reset = true;
1,961,348✔
2023
          } else {
2024
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,373,576✔
2025
          }
2026
        } else {
2027
          int64_t timestamp = taosGetTimestampMs();
4,245,649✔
2028
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
4,245,649✔
2029
          code = TSDB_CODE_ACTION_IN_PROGRESS;
4,245,649✔
2030
        }
2031
        if (reset) {
7,580,573✔
2032
          mndTransResetAction(pMnode, pTrans, pAction);
1,961,348✔
2033
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1,961,348✔
2034
                pAction->id, pAction->errCode, pAction->startTime);
2035
        }
2036
      } else if (pAction->rawWritten) {
991,681✔
2037
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
991,681✔
UNCOV
2038
          code = pAction->errCode;
×
2039
        } else {
2040
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
991,681✔
2041
        }
2042
      } else {
2043
      }
2044
    }
2045

2046
    if (code == 0) {
10,030,337✔
2047
      pTrans->failedTimes = 0;
2,365,257✔
2048
    }
2049
    mndSetTransLastAction(pTrans, pAction);
10,030,337✔
2050
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
10,030,337✔
2051
      mInfo(
5,703,732✔
2052
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2053
          "msgReceived:%d",
2054
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2055
          pAction->msgSent, pAction->msgReceived);
2056
    } else if (code != 0) {
4,326,605✔
2057
      mError(
1,961,348✔
2058
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2059
          "msgReceived:%d",
2060
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2061
          pAction->msgSent, pAction->msgReceived);
2062
    }
2063

2064
    char str[200] = {0};
10,030,337✔
2065
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
10,030,337✔
2066
      pTrans->lastErrorNo = code;
1,971,915✔
2067
      pTrans->code = code;
1,971,915✔
2068
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
1,971,915✔
2069
            action, str);
2070
      break;
1,971,915✔
2071
    }
2072

2073
    if (code == 0) {
8,058,422✔
2074
      pTrans->code = 0;
1,851,425✔
2075
      pTrans->actionPos++;
1,851,425✔
2076
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
1,851,425✔
2077
            pAction->id);
2078
      (void)taosThreadMutexUnlock(&pTrans->mutex);
1,851,425✔
2079
      code = mndTransSync(pMnode, pTrans);
1,851,425✔
2080
      (void)taosThreadMutexLock(&pTrans->mutex);
1,851,425✔
2081
      if (code != 0) {
1,851,425✔
UNCOV
2082
        pTrans->actionPos--;
×
UNCOV
2083
        pTrans->code = terrno;
×
UNCOV
2084
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2085
               mndTransStr(pAction->stage), pAction->id, terrstr());
UNCOV
2086
        break;
×
2087
      }
2088
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
6,206,997✔
2089
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,245,649✔
2090
      break;
4,245,649✔
2091
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
1,961,348✔
2092
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
1,435✔
2093
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
1,961,348✔
2094
            code, tstrerror(code));
2095
      pTrans->lastErrorNo = code;
1,961,348✔
2096
      taosMsleep(300);
1,961,348✔
2097
      action--;
1,961,348✔
2098
      continue;
1,961,348✔
2099
    } else {
UNCOV
2100
      terrno = code;
×
UNCOV
2101
      pTrans->lastErrorNo = code;
×
UNCOV
2102
      pTrans->code = code;
×
UNCOV
2103
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2104
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
UNCOV
2105
      break;
×
2106
    }
2107
  }
2108

2109
  return code;
6,449,784✔
2110
}
2111

2112
static int32_t mndTransExecuteActionsSerialGroup(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf,
3,863,282✔
2113
                                                 int32_t groupId, int32_t currentGroup, int32_t groupCount,
2114
                                                 bool notSend, SHashObj *pHash) {
2115
  int32_t code = 0;
3,863,282✔
2116
  int32_t numOfActions = taosArrayGetSize(pActions);
3,863,282✔
2117
  if (numOfActions == 0) return code;
3,863,282✔
2118

2119
  if (groupId <= 0) {
3,863,282✔
UNCOV
2120
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
UNCOV
2121
    return TSDB_CODE_INTERNAL_ERROR;
×
2122
  }
2123

2124
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &groupId, sizeof(int32_t));
3,863,282✔
2125
  if (actionPos == NULL) {
3,863,282✔
2126
    mError("trans:%d, failed to execute action in serail group, actionPos is null at group %d", pTrans->id, groupId);
×
2127
    return TSDB_CODE_INTERNAL_ERROR;
×
2128
  }
2129

2130
  if (*actionPos >= numOfActions) {
3,863,282✔
2131
    mInfo("trans:%d, this serial group is finished, actionPos:%d >= numOfActions:%d at group %d", pTrans->id,
637,900✔
2132
          *actionPos, numOfActions, groupId);
2133
    return TSDB_CODE_MND_TRANS_GROUP_FINISHED;
637,900✔
2134
  }
2135

2136
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
3,747,650✔
2137
    STransAction **ppAction = taosArrayGet(pActions, action);
3,528,592✔
2138
    STransAction  *pAction = *ppAction;
3,528,592✔
2139

2140
    if (notSend && !pAction->msgSent) {
3,528,592✔
2141
      mInfo(
484,288✔
2142
          "trans:%d, %s:%d (%d/%d at group %d) skip to execute, curent state(actionType(1:msg,2:log):%d, "
2143
          "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2144
          pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2145
          pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2146
      code = TSDB_CODE_ACTION_IN_PROGRESS;
484,288✔
2147
      break;
484,288✔
2148
    }
2149

2150
    mInfo(
3,044,304✔
2151
        "trans:%d, %s:%d (%d/%d at group %d) begin to execute, curent state(actionType(1:msg,2:log):%d, "
2152
        "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2153
        pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2154
        pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2155

2156
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
3,044,304✔
2157
    if (code == 0) {
3,044,304✔
2158
      if (pAction->msgSent) {
1,783,580✔
2159
        if (pAction->msgReceived) {
1,599,138✔
2160
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
464,462✔
2161
            code = pAction->errCode;
32,905✔
2162
            mndTransResetAction(pMnode, pTrans, pAction);
32,905✔
2163
          } else {
2164
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
431,557✔
2165
                  pAction->id, action, numOfActions, groupId);
2166
          }
2167
        } else {
2168
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,134,676✔
2169
        }
2170
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,599,138✔
2171
        if (msgSent != NULL) {
1,599,138✔
2172
          *msgSent = pAction->msgSent;
1,599,138✔
2173
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,599,138✔
2174
        } else {
UNCOV
2175
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2176
        }
2177
      } else if (pAction->rawWritten) {
184,442✔
2178
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
184,442✔
UNCOV
2179
          code = pAction->errCode;
×
2180
        } else {
2181
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
184,442✔
2182
        }
2183
      } else {
2184
      }
2185
    }
2186

2187
    if (code == 0) {
3,044,304✔
2188
      pTrans->failedTimes = 0;
615,999✔
2189
    }
2190
    mndSetTransLastAction(pTrans, pAction);
3,044,304✔
2191

2192
    char str[200] = {0};
3,044,304✔
2193
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
3,044,304✔
2194
      pTrans->lastErrorNo = code;
1,387,360✔
2195
      pTrans->code = code;
1,387,360✔
2196
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
1,387,360✔
2197
        mInfo(
1,260,724✔
2198
            "trans:%d, %s:%d (%d/%d at group %d) not able to execute since %s, current state("
2199
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2200
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2201
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2202
      } else if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
126,636✔
UNCOV
2203
        mError(
×
2204
            "trans:%d, %s:%d (%d/%d at group %d) failed to execute since %s, current action("
2205
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2206
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2207
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2208
      }
2209
      mInfo("trans:%d, %s:%d (%d/%d at group %d) cannot execute next action, stop this group execution, %s", pTrans->id,
1,387,360✔
2210
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2211
      break;
1,387,360✔
2212
    }
2213

2214
    if (code == 0) {
1,656,944✔
2215
      pTrans->code = 0;
489,363✔
2216
      pTrans->actionPos++;
489,363✔
2217
      (*actionPos)++;
489,363✔
2218
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
489,363✔
2219
            pAction->id);
2220
      (void)taosThreadMutexUnlock(&pTrans->mutex);
489,363✔
2221
      code = mndTransSync(pMnode, pTrans);
489,363✔
2222
      (void)taosThreadMutexLock(&pTrans->mutex);
489,363✔
2223
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
489,363✔
2224
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
11,632,709✔
2225
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
11,143,346✔
2226
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
11,143,346✔
2227
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
11,143,346✔
2228
          pTmpAction->msgSent = *msgSent;
562,650✔
2229
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
562,650✔
2230
        }
2231
      }
2232
      if (code != 0) {
489,363✔
UNCOV
2233
        pTrans->actionPos--;
×
UNCOV
2234
        (*actionPos)--;
×
UNCOV
2235
        pTrans->code = terrno;
×
UNCOV
2236
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2237
               mndTransStr(pAction->stage), pAction->id, terrstr());
UNCOV
2238
        break;
×
2239
      }
2240
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
1,167,581✔
2241
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,134,676✔
2242
            mndTransStr(pAction->stage), pAction->id);
2243
      break;
1,134,676✔
2244
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
32,905✔
UNCOV
2245
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
×
2246
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
32,905✔
2247
            code, tstrerror(code));
2248
      pTrans->lastErrorNo = code;
32,905✔
2249
      taosMsleep(300);
32,905✔
2250
      action--;
32,905✔
2251
      continue;
32,905✔
2252
    } else {
UNCOV
2253
      terrno = code;
×
UNCOV
2254
      pTrans->lastErrorNo = code;
×
UNCOV
2255
      pTrans->code = code;
×
UNCOV
2256
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2257
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
UNCOV
2258
      break;
×
2259
    }
2260
  }
2261

2262
  return code;
3,225,382✔
2263
}
2264

2265
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
6,681,881✔
2266
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
6,681,881✔
2267
  (void)taosThreadMutexLock(&pTrans->mutex);
6,681,881✔
2268
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
6,681,881✔
2269
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
6,681,881✔
2270
  }
2271
  (void)taosThreadMutexUnlock(&pTrans->mutex);
6,681,881✔
2272
  return code;
6,681,881✔
2273
}
2274

2275
static int32_t mndTransExecuteRedoActionGroup(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
1,215,296✔
2276
  int32_t total_code = TSDB_CODE_ACTION_IN_PROGRESS;
1,215,296✔
2277
  int32_t code = 0;
1,215,296✔
2278
  int32_t successCount = 0;
1,215,296✔
2279
  int32_t groupCount = taosHashGetSize(pTrans->redoGroupActions);
1,215,296✔
2280

2281
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,215,296✔
2282
  if(pHash == NULL){
1,215,296✔
UNCOV
2283
    mError("trans:%d, failed to init hash since %s", pTrans->id, terrstr());
×
UNCOV
2284
    return -1;
×
2285
  }
2286
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
28,597,789✔
2287
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
27,382,493✔
2288
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
27,382,493✔
2289
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
27,382,493✔
2290
  }
2291
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
1,215,296✔
2292

2293
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,215,296✔
2294
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,215,296✔
2295
  int32_t currentGroup = 1;
1,215,296✔
2296
  while (pIter) {
5,078,578✔
2297
    SArray **redoActions = pIter;
3,863,282✔
2298
    size_t   keyLen = 0;
3,863,282✔
2299
    int32_t *key = taosHashGetKey(pIter, &keyLen);
3,863,282✔
2300
    int32_t  actionCount = taosArrayGetSize(*redoActions);
3,863,282✔
2301
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
3,863,282✔
2302
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2303
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
3,863,282✔
2304
                                             notSend, pHash);
2305
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
3,863,282✔
2306
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,260,724✔
2307
            tstrerror(code));
2308
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,602,558✔
2309
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,618,964✔
2310
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
983,594✔
2311
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
637,900✔
2312
    } else if (code != 0) {
345,694✔
UNCOV
2313
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
×
2314
             tstrerror(code));
2315
    } else {
2316
      successCount++;
345,694✔
2317
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
345,694✔
2318
    }
2319
    currentGroup++;
3,863,282✔
2320
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
3,863,282✔
2321
  }
2322

2323
  taosHashCleanup(pHash);
1,215,296✔
2324

2325
  if (successCount == groupCount) {
1,215,296✔
2326
    total_code = 0;
22,468✔
2327
  } else {
2328
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,192,828✔
2329
  }
2330

2331
  return total_code;
1,215,296✔
2332
}
2333

2334
static int32_t mndTransExecuteRedoActionsParallel(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
1,418,354✔
2335
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
1,418,354✔
2336
  (void)taosThreadMutexLock(&pTrans->mutex);
1,418,354✔
2337

2338
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,418,354✔
2339
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,418,354✔
2340
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,418,354✔
2341
      code = 0;
175,486✔
2342
    } else {
2343
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,242,868✔
2344
      if (pAction != NULL && pAction->groupId == -1) {
1,242,868✔
2345
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
27,572✔
2346
      } else {
2347
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,215,296✔
2348
        if (code == 0) {
1,215,296✔
2349
          if (pTrans->actionPos < numOfActions) {
22,468✔
2350
            code = TSDB_CODE_ACTION_IN_PROGRESS;
16,689✔
2351
          }
2352
        }
2353
      }
2354
    }
2355
  }
2356

2357
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,418,354✔
2358

2359
  return code;
1,418,354✔
2360
}
2361

UNCOV
2362
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
UNCOV
2363
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
UNCOV
2364
  (void)taosThreadMutexLock(&pTrans->mutex);
×
UNCOV
2365
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
UNCOV
2366
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
2367
  }
2368
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
2369
  return code;
×
2370
}
2371

2372
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
17,098,251✔
2373
  bool    continueExec = true;
17,098,251✔
2374
  int32_t code = 0;
17,098,251✔
2375
  terrno = 0;
17,098,251✔
2376

2377
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
17,098,251✔
2378
  if (numOfActions == 0) goto _OVER;
17,098,251✔
2379

2380
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
7,389,058✔
2381

2382
  for (int32_t action = 0; action < numOfActions; ++action) {
17,267,379✔
2383
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
9,878,321✔
2384
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
9,878,321✔
2385
    if (code != 0) {
9,878,321✔
UNCOV
2386
      terrno = code;
×
UNCOV
2387
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
2388
             numOfActions, tstrerror(code));
UNCOV
2389
      return false;
×
2390
    }
2391
  }
2392

2393
_OVER:
7,389,058✔
2394
  pTrans->stage = TRN_STAGE_REDO_ACTION;
17,098,251✔
2395
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
17,098,251✔
2396
  return continueExec;
17,098,251✔
2397
}
2398

2399
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
56,548,551✔
2400
  bool    continueExec = true;
56,548,551✔
2401
  int32_t code = 0;
56,548,551✔
2402
  terrno = 0;
56,548,551✔
2403

2404
  if (pTrans->exec == TRN_EXEC_SERIAL) {
56,548,551✔
2405
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
6,681,881✔
2406
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
49,866,670✔
2407
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
48,448,316✔
2408
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,418,354✔
2409
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,418,354✔
2410
  } else {
UNCOV
2411
    code = TSDB_CODE_INTERNAL_ERROR;
×
2412
  }
2413

2414
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
56,550,013✔
2415
      mndTransIsInSyncContext(topHalf)) {
1,462✔
UNCOV
2416
    pTrans->lastErrorNo = code;
×
2417
    pTrans->code = code;
×
UNCOV
2418
    mInfo(
×
2419
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2420
        "continueExec:%d, code:%s",
2421
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2422
    taosMsleep(100);
×
2423
    return true;
×
2424
  } else {
2425
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
56,548,551✔
2426
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
19,584,902✔
2427
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2428
      return false;
19,584,902✔
2429
    }
2430
  }
2431
  terrno = code;
36,963,649✔
2432

2433
  if (code == 0) {
36,963,649✔
2434
    pTrans->code = 0;
15,761,367✔
2435
    pTrans->stage = TRN_STAGE_COMMIT;
15,761,367✔
2436
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
15,761,367✔
2437
    continueExec = true;
15,761,367✔
2438
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
21,202,282✔
2439
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
21,200,820✔
2440
    continueExec = false;
21,200,820✔
2441
  } else {
2442
    pTrans->failedTimes++;
1,462✔
2443
    pTrans->code = terrno;
1,462✔
2444
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
1,462✔
2445
      if (pTrans->lastAction != 0) {
1,462✔
2446
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
1,462✔
2447
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
1,462✔
UNCOV
2448
          if (pTrans->failedTimes < 6) {
×
UNCOV
2449
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2450
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
UNCOV
2451
            taosMsleep(1000);
×
UNCOV
2452
            continueExec = true;
×
UNCOV
2453
            return true;
×
2454
          }
2455
        }
2456
      }
2457

2458
      pTrans->stage = TRN_STAGE_ROLLBACK;
1,462✔
2459
      pTrans->actionPos = 0;
1,462✔
2460
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
1,462✔
2461
             pTrans->actionPos);
2462
      continueExec = true;
1,462✔
2463
    } else {
UNCOV
2464
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
2465
      continueExec = false;
×
2466
    }
2467
  }
2468

2469
  return continueExec;
36,963,649✔
2470
}
2471

2472
// execute in trans context
2473
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
15,761,367✔
2474
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
15,761,367✔
2475

2476
  bool    continueExec = true;
15,761,367✔
2477
  int32_t code = mndTransCommit(pMnode, pTrans);
15,761,367✔
2478

2479
  if (code == 0) {
15,761,367✔
2480
    pTrans->code = 0;
15,759,923✔
2481
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
15,759,923✔
2482
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
15,759,923✔
2483
    continueExec = true;
15,759,923✔
2484
  } else {
2485
    pTrans->code = terrno;
1,444✔
2486
    pTrans->failedTimes++;
1,444✔
2487
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
1,444✔
2488
    continueExec = false;
1,444✔
2489
  }
2490

2491
  return continueExec;
15,761,367✔
2492
}
2493

2494
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
32,852,912✔
2495
  bool    continueExec = true;
32,852,912✔
2496
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
32,852,912✔
2497

2498
  if (code == 0) {
32,852,912✔
2499
    pTrans->code = 0;
32,852,912✔
2500
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
32,852,912✔
2501
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
32,852,912✔
2502
    continueExec = true;
32,852,912✔
UNCOV
2503
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
UNCOV
2504
    pTrans->code = 0;
×
UNCOV
2505
    pTrans->stage = TRN_STAGE_COMMIT;
×
UNCOV
2506
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
UNCOV
2507
    continueExec = true;
×
2508
  } else {
2509
    pTrans->code = terrno;
×
2510
    pTrans->failedTimes++;
×
2511
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2512
    continueExec = false;
×
2513
  }
2514

2515
  return continueExec;
32,852,912✔
2516
}
2517

2518
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
10,234✔
2519
  bool    continueExec = true;
10,234✔
2520
  int32_t code = 0;
10,234✔
2521

2522
  if (pTrans->exec == TRN_EXEC_SERIAL) {
10,234✔
UNCOV
2523
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
2524
  } else {
2525
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
10,234✔
2526
  }
2527

2528
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
10,234✔
2529
  terrno = code;
8,772✔
2530

2531
  if (code == 0) {
8,772✔
2532
    pTrans->stage = TRN_STAGE_PRE_FINISH;
1,462✔
2533
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
1,462✔
2534
    continueExec = true;
1,462✔
2535
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
7,310✔
2536
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
7,310✔
2537
    continueExec = false;
7,310✔
2538
  } else {
UNCOV
2539
    pTrans->failedTimes++;
×
UNCOV
2540
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
2541
    continueExec = false;
×
2542
  }
2543

2544
  return continueExec;
8,772✔
2545
}
2546

2547
// in trans context
2548
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
1,462✔
2549
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
1,462✔
2550

2551
  bool    continueExec = true;
1,462✔
2552
  int32_t code = mndTransRollback(pMnode, pTrans);
1,462✔
2553

2554
  if (code == 0) {
1,462✔
2555
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1,462✔
2556
    continueExec = true;
1,462✔
2557
  } else {
UNCOV
2558
    pTrans->failedTimes++;
×
UNCOV
2559
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
2560
    continueExec = false;
×
2561
  }
2562

2563
  return continueExec;
1,462✔
2564
}
2565

2566
// excute in trans context
2567
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
1,584✔
2568
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
1,584✔
2569

2570
  bool    continueExec = true;
1,584✔
2571
  int32_t code = mndTransPreFinish(pMnode, pTrans);
1,584✔
2572

2573
  if (code == 0) {
1,584✔
2574
    pTrans->stage = TRN_STAGE_FINISH;
1,584✔
2575
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
1,584✔
2576
    continueExec = true;
1,584✔
2577
  } else {
UNCOV
2578
    pTrans->failedTimes++;
×
UNCOV
2579
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
2580
    continueExec = false;
×
2581
  }
2582

2583
  return continueExec;
1,584✔
2584
}
2585

2586
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
32,856,080✔
2587
  bool continueExec = false;
32,856,080✔
2588
  if (topHalf) return continueExec;
32,856,080✔
2589

2590
  SSdbRaw *pRaw = mndTransEncode(pTrans);
17,094,573✔
2591
  if (pRaw == NULL) {
17,094,573✔
UNCOV
2592
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
UNCOV
2593
    return false;
×
2594
  }
2595
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
17,094,573✔
2596

2597
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
17,094,573✔
2598
  if (code != 0) {
17,094,573✔
2599
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
2600
  }
2601

2602
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
17,094,573✔
2603
        pTrans->failedTimes, pTrans->createdTime);
2604
  return continueExec;
17,094,573✔
2605
}
2606

2607
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
73,652,018✔
2608
  bool continueExec = true;
73,652,018✔
2609

2610
  while (continueExec) {
211,684,208✔
2611
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
138,032,190✔
2612
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2613
    pTrans->lastExecTime = taosGetTimestampMs();
138,032,190✔
2614
    switch (pTrans->stage) {
138,032,190✔
UNCOV
2615
      case TRN_STAGE_PREPARE:
×
UNCOV
2616
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
UNCOV
2617
        break;
×
2618
      case TRN_STAGE_REDO_ACTION:
56,548,551✔
2619
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
56,548,551✔
2620
        break;
56,548,551✔
2621
      case TRN_STAGE_COMMIT:
15,761,367✔
2622
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
15,761,367✔
2623
        break;
15,761,367✔
2624
      case TRN_STAGE_COMMIT_ACTION:
32,852,912✔
2625
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
32,852,912✔
2626
        break;
32,852,912✔
2627
      case TRN_STAGE_ROLLBACK:
1,462✔
2628
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,462✔
2629
        break;
1,462✔
2630
      case TRN_STAGE_UNDO_ACTION:
10,234✔
2631
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
10,234✔
2632
        break;
10,234✔
2633
      case TRN_STAGE_PRE_FINISH:
1,584✔
2634
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,584✔
2635
        break;
1,584✔
2636
      case TRN_STAGE_FINISH:
32,856,080✔
2637
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
32,856,080✔
2638
        break;
32,856,080✔
UNCOV
2639
      default:
×
UNCOV
2640
        continueExec = false;
×
UNCOV
2641
        break;
×
2642
    }
2643
  }
2644

2645
  mndTransSendRpcRsp(pMnode, pTrans);
73,652,018✔
2646
}
73,652,018✔
2647

2648
// start trans, pullup, receive rsp, kill
2649
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
36,986,976✔
2650
  bool topHalf = true;
36,986,976✔
2651
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
36,986,976✔
2652
}
36,986,976✔
2653

2654
// update trans
2655
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
36,665,042✔
2656
  bool topHalf = false;
36,665,042✔
2657
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
36,665,042✔
2658
}
36,665,042✔
2659

2660
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
11,794,274✔
2661
  mTrace("start to process trans timer");
11,794,274✔
2662
  mndTransPullup(pReq->info.node);
11,794,274✔
2663
  return 0;
11,794,274✔
2664
}
2665

2666
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
122✔
2667
  SArray *pArray = NULL;
122✔
2668
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
122✔
2669
    pArray = pTrans->redoActions;
122✔
UNCOV
2670
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
UNCOV
2671
    pArray = pTrans->undoActions;
×
2672
  } else {
UNCOV
2673
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2674
  }
2675

2676
  if(!tsForceKillTrans){
122✔
2677
    if(pTrans->ableToBeKilled == false){
122✔
UNCOV
2678
      return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2679
    }
2680
  }
2681
  
2682
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
122✔
UNCOV
2683
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
2684
      STransAction *pAction = taosArrayGet(pArray, i);
×
UNCOV
2685
      mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
2686
            mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
UNCOV
2687
      pAction->msgSent = 1;
×
UNCOV
2688
      pAction->msgReceived = 1;
×
2689
      pAction->errCode = 0;
×
2690
    }
2691
  }
2692
  else if(pTrans->killMode == TRN_KILL_MODE_INTERUPT){
122✔
2693
    pTrans->stage = TRN_STAGE_PRE_FINISH;
122✔
2694
  }
2695
  else{
UNCOV
2696
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2697
  }
2698

2699
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
122✔
2700
  mndTransExecute(pMnode, pTrans, true);
122✔
2701
  return 0;
122✔
2702
}
2703

2704
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
283✔
2705
  SMnode       *pMnode = pReq->info.node;
283✔
2706
  SKillTransReq killReq = {0};
283✔
2707
  int32_t       code = -1;
283✔
2708
  STrans       *pTrans = NULL;
283✔
2709

2710
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
283✔
UNCOV
2711
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2712
    goto _OVER;
×
2713
  }
2714

2715
  mInfo("trans:%d, start to kill, force:%d", killReq.transId, tsForceKillTrans);
283✔
2716
  if ((code = mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_TRANS)) != 0) {
283✔
2717
    goto _OVER;
161✔
2718
  }
2719

2720
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
122✔
2721
  if (pTrans == NULL) {
122✔
UNCOV
2722
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
2723
    if (terrno != 0) code = terrno;
×
UNCOV
2724
    goto _OVER;
×
2725
  }
2726

2727
  code = mndKillTrans(pMnode, pTrans);
122✔
2728

2729
_OVER:
283✔
2730
  if (code != 0) {
283✔
2731
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
161✔
2732
  }
2733

2734
  mndReleaseTrans(pMnode, pTrans);
283✔
2735
  TAOS_RETURN(code);
283✔
2736
}
2737

2738
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
98,387✔
2739

2740
void mndTransPullup(SMnode *pMnode) {
11,908,012✔
2741
  SSdb   *pSdb = pMnode->pSdb;
11,908,012✔
2742
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
11,908,012✔
2743
  if (pArray == NULL) return;
11,908,012✔
2744

2745
  void *pIter = NULL;
11,908,012✔
2746
  while (1) {
1,681,267✔
2747
    STrans *pTrans = NULL;
13,589,279✔
2748
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
13,589,279✔
2749
    if (pIter == NULL) break;
13,589,279✔
2750
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
3,362,534✔
UNCOV
2751
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2752
    }
2753
    sdbRelease(pSdb, pTrans);
1,681,267✔
2754
  }
2755

2756
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
11,908,012✔
2757

2758
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
13,589,279✔
2759
    int32_t *pTransId = taosArrayGet(pArray, i);
1,681,267✔
2760
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
1,681,267✔
2761
    if (pTrans != NULL) {
1,681,267✔
2762
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
1,681,267✔
2763
      mndTransExecute(pMnode, pTrans, false);
1,681,267✔
2764
    }
2765
    mndReleaseTrans(pMnode, pTrans);
1,681,267✔
2766
  }
2767
  taosArrayDestroy(pArray);
11,908,012✔
2768
}
2769

2770
static char *formatTimestamp(char *buf, int64_t val, int precision) {
18,132,862✔
2771
  time_t tt;
18,132,862✔
2772
  if (precision == TSDB_TIME_PRECISION_MICRO) {
18,132,862✔
UNCOV
2773
    tt = (time_t)(val / 1000000);
×
2774
  }
2775
  if (precision == TSDB_TIME_PRECISION_NANO) {
18,132,862✔
UNCOV
2776
    tt = (time_t)(val / 1000000000);
×
2777
  } else {
2778
    tt = (time_t)(val / 1000);
18,132,862✔
2779
  }
2780

2781
  struct tm tm;
18,132,862✔
2782
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
18,132,862✔
UNCOV
2783
    mError("failed to get local time");
×
UNCOV
2784
    return NULL;
×
2785
  }
2786
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
18,132,862✔
2787

2788
  if (precision == TSDB_TIME_PRECISION_MICRO) {
18,132,862✔
2789
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2790
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
18,132,862✔
UNCOV
2791
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2792
  } else {
2793
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
18,132,862✔
2794
  }
2795

2796
  return buf;
18,132,862✔
2797
}
2798

2799
static void mndTransLogAction(STrans *pTrans) {
367,199✔
2800
  char    detail[512] = {0};
367,199✔
2801
  int32_t len = 0;
367,199✔
2802
  int32_t index = 0;
367,199✔
2803

2804
  if (pTrans->stage == TRN_STAGE_PREPARE) {
367,199✔
UNCOV
2805
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
UNCOV
2806
      len = 0;
×
UNCOV
2807
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
UNCOV
2808
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
UNCOV
2809
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
UNCOV
2810
                      sdbStatusName(pAction->pRaw->status));
×
2811
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2812
    }
2813
  }
2814

2815
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
367,199✔
2816
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
12,250,923✔
2817
      len = 0;
11,884,683✔
2818
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
11,884,683✔
2819
      if (pAction->actionType == TRANS_ACTION_MSG) {
11,884,683✔
2820
        char bufStart[40] = {0};
9,050,052✔
2821
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
9,050,052✔
2822

2823
        char endStart[40] = {0};
9,050,052✔
2824
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
9,050,052✔
2825
        len += snprintf(detail + len, sizeof(detail) - len,
18,100,104✔
2826
                        "action:%d, %s:%d msgType:%s,"
2827
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2828
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
18,100,104✔
2829
                        pAction->msgReceived, bufStart, endStart);
9,050,052✔
2830

2831
        SEpSet epset = pAction->epSet;
9,050,052✔
2832
        if (epset.numOfEps > 0) {
9,050,052✔
2833
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
9,050,052✔
2834
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
20,326,980✔
2835
            len +=
11,276,928✔
2836
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
11,276,928✔
2837
          }
2838
        }
2839

2840
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
9,050,052✔
2841
                        tstrerror(pAction->errCode));
2842
      } else {
2843
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
2,834,631✔
2844
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
2,834,631✔
2845
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
2,834,631✔
2846
      }
2847
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
11,884,683✔
2848
    }
2849
  }
2850

2851
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
367,199✔
2852
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
3,130✔
2853
      len = 0;
2,504✔
2854
      STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
2,504✔
2855
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
2,504✔
2856
                      mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type),
2,504✔
2857
                      sdbStatusName(pAction->pRaw->status));
2,504✔
2858
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
2,504✔
2859
    }
2860

2861
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
626✔
UNCOV
2862
      len = 0;
×
UNCOV
2863
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
UNCOV
2864
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
UNCOV
2865
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
UNCOV
2866
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2867
        ;
2868
      } else {
2869
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2870
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2871
                        sdbStatusName(pAction->pRaw->status));
×
2872
      }
UNCOV
2873
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2874
    }
2875
  }
2876
}
367,199✔
2877

2878
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
384,813✔
2879
  SMnode *pMnode = pReq->info.node;
384,813✔
2880
  SSdb   *pSdb = pMnode->pSdb;
384,813✔
2881
  int32_t numOfRows = 0;
384,813✔
2882
  STrans *pTrans = NULL;
384,813✔
2883
  int32_t cols = 0;
384,813✔
2884
  int32_t code = 0;
384,813✔
2885
  int32_t lino = 0;
384,813✔
2886

2887
  while (numOfRows < rows) {
752,012✔
2888
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
752,012✔
2889
    if (pShow->pIter == NULL) break;
752,012✔
2890

2891
    cols = 0;
367,199✔
2892

2893
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2894
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
367,199✔
2895

2896
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2897
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
367,199✔
2898
                        _OVER);
2899

2900
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
367,199✔
2901
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
367,199✔
2902
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2903
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
367,199✔
2904

2905
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
367,199✔
2906
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
367,199✔
2907
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2908
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
367,199✔
2909

2910
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
367,199✔
2911
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
367,199✔
2912
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2913
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
367,199✔
2914

2915
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
367,199✔
2916
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
367,199✔
2917
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2918
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
367,199✔
2919

2920
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
367,199✔
2921
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
367,199✔
2922
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
367,199✔
2923
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2924
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
367,199✔
2925

2926
    /*
2927
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2928
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2929
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2930
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2931
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2932
    */
2933

2934
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2935
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
367,199✔
2936
                        _OVER);
2937

2938
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2939
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
367,199✔
2940
                        _OVER);
2941

2942
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
367,199✔
2943
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
367,199✔
2944
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
734,398✔
2945
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
734,398✔
2946
    SEpSet  epset = pTrans->lastEpset;
367,199✔
2947
    if (epset.numOfEps > 0) {
367,199✔
2948
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
726,320✔
2949
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
726,320✔
2950
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
1,015,881✔
2951
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
652,721✔
2952
      }
2953
    }
2954
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
367,199✔
2955
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
367,199✔
2956
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
367,199✔
2957

2958
    mndTransLogAction(pTrans);
367,199✔
2959

2960
    numOfRows++;
367,199✔
2961
    sdbRelease(pSdb, pTrans);
367,199✔
2962
  }
2963

2964
_OVER:
384,813✔
2965
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
384,813✔
2966
  pShow->numOfRows += numOfRows;
384,813✔
2967
  return numOfRows;
384,813✔
2968
}
2969

2970
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
58,718✔
2971
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2972
  int32_t code = 0;
58,718✔
2973
  int32_t lino = 0;
58,718✔
2974
  int32_t len = 0;
58,718✔
2975

2976
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
58,718✔
2977
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&transactionId, false), &lino, _OVER);
58,718✔
2978

2979
  char action[30 + 1] = {0};
58,718✔
2980
  if (curActionId == pAction->id) {
58,718✔
2981
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
448✔
2982
                    mndTransTypeStr(pAction->actionType));
2983
  } else {
2984
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
58,270✔
2985
                    mndTransTypeStr(pAction->actionType));
2986
  }
2987
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
58,718✔
2988
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
58,718✔
2989
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
58,718✔
2990
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
58,718✔
2991
_OVER:
58,718✔
2992
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
58,718✔
2993
  return code;
58,718✔
2994
}
2995

2996
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
58,718✔
2997
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2998
  int32_t code = 0;
58,718✔
2999
  int32_t lino = 0;
58,718✔
3000
  int32_t len = 0;
58,718✔
3001
  int32_t cols = 0;
58,718✔
3002

3003
  cols = 0;
58,718✔
3004

3005
  if (mndShowTransCommonColumns(pShow, pBlock, pAction, transactionId, curActionId, numOfRows, &cols) != 0) return;
58,718✔
3006

3007
  if (pAction->actionType == TRANS_ACTION_MSG) {
58,718✔
3008
    int32_t len = 0;
48,286✔
3009

3010
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
48,286✔
3011
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
48,286✔
3012
                    pAction->msgSent, pAction->msgReceived);
48,286✔
3013
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
48,286✔
3014
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
48,286✔
3015
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
48,286✔
3016
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
48,286✔
3017

3018
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
48,286✔
3019
    len = 0;
48,286✔
3020
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
48,286✔
3021
                    tstrerror(pAction->errCode));
3022
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
48,286✔
3023
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
48,286✔
3024
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
48,286✔
3025
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
48,286✔
3026

3027
    char target[TSDB_TRANS_TARGET_LEN] = {0};
48,286✔
3028
    len = 0;
48,286✔
3029
    SEpSet epset = pAction->epSet;
48,286✔
3030
    if (epset.numOfEps > 0) {
48,286✔
3031
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
112,220✔
3032
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
63,934✔
3033
      }
3034
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
48,286✔
3035
    }
3036
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
48,286✔
3037
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
48,286✔
3038
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
48,286✔
3039
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
48,286✔
3040

3041
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
48,286✔
3042
    len = 0;
48,286✔
3043
    char bufStart[40] = {0};
48,286✔
3044
    if (pAction->startTime > 0) (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
48,286✔
3045
    char bufEnd[40] = {0};
48,286✔
3046
    if (pAction->endTime > 0) (void)formatTimestamp(bufEnd, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
48,286✔
3047
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
48,286✔
3048
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
48,286✔
3049
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
48,286✔
3050
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
48,286✔
3051
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
48,286✔
3052

3053
  } else {
3054
    int32_t len = 0;
10,432✔
3055

3056
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
10,432✔
3057
    if (pAction->pRaw->type == SDB_VGROUP) {
10,432✔
3058
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
10,432✔
3059
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
10,432✔
3060
      len += snprintf(objType + len, sizeof(objType) - len, "%s(%d)", sdbTableName(pAction->pRaw->type), pVgroup->vgId);
10,432✔
3061
      taosMemoryFreeClear(pRow);
10,432✔
3062
    } else {
UNCOV
3063
      strcpy(objType, sdbTableName(pAction->pRaw->type));
×
3064
    }
3065
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
10,432✔
3066
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
10,432✔
3067
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
10,432✔
3068
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
10,432✔
3069

3070
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
10,432✔
3071
    len = 0;
10,432✔
3072
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
10,432✔
3073
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
10,432✔
3074
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
10,432✔
3075
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
10,432✔
3076
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
10,432✔
3077

3078
    char target[TSDB_TRANS_TARGET_LEN] = "";
10,432✔
3079
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
10,432✔
3080
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
10,432✔
3081
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
10,432✔
3082
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
10,432✔
3083

3084
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
10,432✔
3085
    len = 0;
10,432✔
3086
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
10,432✔
3087
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
10,432✔
3088
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
10,432✔
3089
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
10,432✔
3090
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
10,432✔
3091
  }
3092

3093
_OVER:
58,718✔
3094
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
58,718✔
3095
}
3096

3097
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
448✔
3098
  if (stage == TRN_STAGE_PREPARE) {
448✔
UNCOV
3099
    return pTrans->prepareActions;
×
3100
  }
3101
  if (stage == TRN_STAGE_REDO_ACTION) {
448✔
3102
    return pTrans->redoActions;
448✔
3103
  }
UNCOV
3104
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
3105
    return pTrans->commitActions;
×
3106
  }
UNCOV
3107
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
UNCOV
3108
    return pTrans->undoActions;
×
3109
  }
3110
  return NULL;
×
3111
}
3112

3113
typedef struct STransDetailIter {
3114
  void     *pIter;
3115
  STrans   *pTrans;
3116
  ETrnStage stage;
3117
  int32_t   num;
3118
} STransDetailIter;
3119

3120
static void mndTransShowActions(SSdb *pSdb, STransDetailIter *pShowIter, SShowObj *pShow, SSDataBlock *pBlock,
448✔
3121
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3122
  int32_t actionNum = taosArrayGetSize(pActions);
448✔
3123
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
448✔
3124

3125
  for (int32_t i = start; i < actionNum; ++i) {
59,166✔
3126
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
58,718✔
3127
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
58,718✔
3128
    (*numOfRows)++;
58,718✔
3129
    if (*numOfRows >= rows) break;
58,718✔
3130
  }
3131

3132
  if (*numOfRows == end) {
448✔
3133
    sdbRelease(pSdb, pShowIter->pTrans);
448✔
3134
    pShowIter->pTrans = NULL;
448✔
3135
    pShowIter->num = 0;
448✔
3136
  } else {
UNCOV
3137
    pShowIter->pTrans = pShowIter->pTrans;
×
UNCOV
3138
    pShowIter->stage = pShowIter->pTrans->stage;
×
UNCOV
3139
    pShowIter->num += (*numOfRows);
×
3140
  }
3141
}
448✔
3142

3143
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
896✔
3144
  SMnode *pMnode = pReq->info.node;
896✔
3145
  SSdb   *pSdb = pMnode->pSdb;
896✔
3146
  int32_t numOfRows = 0;
896✔
3147

3148
  int32_t code = 0;
896✔
3149
  int32_t lino = 0;
896✔
3150

3151
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
896✔
3152
        pShow->pIter);
3153

3154
  if (pShow->pIter == NULL) {
896✔
3155
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
448✔
3156
    if (pShow->pIter == NULL) {
448✔
UNCOV
3157
      mError("failed to malloc for pShow->pIter");
×
UNCOV
3158
      return 0;
×
3159
    }
3160
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
448✔
3161
  }
3162

3163
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
896✔
3164

3165
  while (numOfRows < rows) {
896✔
3166
    if (pShowIter->pTrans == NULL) {
896✔
3167
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
896✔
3168
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
896✔
3169
      if (pShowIter->pIter == NULL) break;
896✔
3170
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
448✔
3171
            pShowIter->pTrans->stage, pShowIter->num);
3172

3173
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
448✔
3174

3175
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
448✔
3176
      break;
448✔
3177
    } else {
UNCOV
3178
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
×
3179
            pShowIter->num);
UNCOV
3180
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
×
3181

UNCOV
3182
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
×
UNCOV
3183
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
×
3184
      break;
×
3185
    }
3186
  }
3187

3188
_OVER:
×
3189
  pShow->numOfRows += numOfRows;
896✔
3190

3191
  if (code != 0) {
896✔
UNCOV
3192
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
3193
  } else {
3194
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
896✔
3195
  }
3196
  if (numOfRows == 0) {
896✔
3197
    taosMemoryFree(pShow->pIter);
448✔
3198
    pShow->pIter = NULL;
448✔
3199
  }
3200
  return numOfRows;
896✔
3201
}
3202

UNCOV
3203
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
UNCOV
3204
  SSdb *pSdb = pMnode->pSdb;
×
UNCOV
3205
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
UNCOV
3206
}
×
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