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

taosdata / TDengine / #5012

03 Apr 2026 03:59PM UTC coverage: 72.305% (+0.005%) from 72.3%
#5012

push

travis-ci

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

4053 of 5985 new or added lines in 68 files covered. (67.72%)

13105 existing lines in 173 files now uncovered.

257446 of 356056 relevant lines covered (72.3%)

131779375.07 hits per line

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

83.64
/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; }
134,692,100✔
64

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

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

81
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
190,317,347✔
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) {
477,765✔
94
  SSdbTable table = {
477,765✔
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);
477,765✔
105
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
477,765✔
106

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

113
void mndCleanupTrans(SMnode *pMnode) {}
477,697✔
114

115
static int32_t mndTransGetActionsSize(SArray *pArray) {
248,035,504✔
116
  int32_t actionNum = taosArrayGetSize(pArray);
248,035,504✔
117
  int32_t rawDataLen = 0;
248,035,504✔
118

119
  for (int32_t i = 0; i < actionNum; ++i) {
666,183,138✔
120
    STransAction *pAction = taosArrayGet(pArray, i);
418,147,634✔
121
    if (pAction->actionType == TRANS_ACTION_RAW) {
418,147,634✔
122
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
283,091,687✔
123
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
135,055,947✔
124
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
135,055,947✔
125
    } else {
126
      // empty
127
    }
128
    rawDataLen += sizeof(int8_t);
418,147,634✔
129
  }
130

131
  return rawDataLen;
248,035,504✔
132
}
133

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

142
  for (int32_t i = 0; i < actionsNum; ++i) {
666,183,138✔
143
    STransAction *pAction = taosArrayGet(pActions, i);
418,147,634✔
144
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
418,147,634✔
145
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
418,147,634✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
418,147,634✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
418,147,634✔
148
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
418,147,634✔
149
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
418,147,634✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
418,147,634✔
151
    if (sver > TRANS_VER2_NUMBER) {
418,147,634✔
152
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
418,147,634✔
153
    }
154
    if (pAction->actionType == TRANS_ACTION_RAW) {
418,147,634✔
155
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
283,091,687✔
156
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
283,091,687✔
157
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
283,091,687✔
158
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
283,091,687✔
159
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
135,055,947✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
135,055,947✔
161
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
135,055,947✔
162
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
135,055,947✔
163
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
135,055,947✔
164
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
135,055,947✔
165
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
135,055,947✔
166
    } else {
167
      // nothing
168
    }
169
  }
170
  ret = 0;
248,035,504✔
171

172
_OVER:
248,035,504✔
173
  *offset = dataPos;
248,035,504✔
174
  return ret;
248,035,504✔
175
}
176

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

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

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

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

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

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

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

226
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
62,008,876✔
227
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
62,008,876✔
228
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
62,008,876✔
229
  if (pTrans->param != NULL) {
62,008,876✔
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)
62,008,876✔
234

235
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
62,008,876✔
236
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
62,008,876✔
237
  void *pIter = NULL;
62,008,876✔
238
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
62,008,876✔
239
  while (pIter) {
62,041,087✔
240
    int32_t arbGroupId = *(int32_t *)pIter;
32,211✔
241
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
32,211✔
242
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
32,211✔
243
  }
244

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

250
  if (sver > TRANS_VER2_NUMBER) {
62,008,876✔
251
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
62,008,876✔
252
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
62,008,876✔
253
    void *pIter = NULL;
62,008,876✔
254
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
62,008,876✔
255
    while (pIter) {
64,549,038✔
256
      size_t   keysize = 0;
2,540,162✔
257
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,540,162✔
258
      int32_t  groupPos = *(int32_t *)pIter;
2,540,162✔
259
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,540,162✔
260
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,540,162✔
261
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,540,162✔
262
    }
263
  }
264

265
  if (sver >= TRANS_VER_USER_DATA) {
62,008,876✔
266
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
62,008,876✔
267
    if (pTrans->userDataLen > 0) {
62,008,876✔
268
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
112,887✔
269
    }
270
  }
271

272
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
62,008,876✔
273
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
62,008,876✔
274

275
  terrno = 0;
62,008,876✔
276

277
_OVER:
62,008,876✔
278
  if (terrno != 0) {
62,008,876✔
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);
62,008,876✔
286
  return pRaw;
62,008,876✔
287
}
288

289
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
29,431,684✔
290
  if (pAction->groupId < 0) return 0;
29,431,684✔
291
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
28,893,727✔
292
  if (redoAction == NULL) {
28,893,727✔
293
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
5,054,778✔
294
    if (array != NULL) {
5,054,778✔
295
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
5,054,778✔
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));
5,054,778✔
301
  }
302
  if (redoAction != NULL) {
28,893,727✔
303
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
57,787,454✔
304
  }
305
  return 0;
28,893,727✔
306
}
307

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

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

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

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

385
  for (int32_t i = 0; i < actionNum; ++i) {
1,156,990,130✔
386
    memset(&action, 0, sizeof(action));
720,152,834✔
387
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
720,152,834✔
388
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
720,152,834✔
389
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
720,152,834✔
390
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
720,152,834✔
391
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
720,152,834✔
392
    action.actionType = actionType;
720,152,834✔
393
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
720,152,834✔
394
    action.stage = stage;
720,152,834✔
395
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
720,152,834✔
396
    if (sver > TRANS_VER2_NUMBER) {
720,152,834✔
397
      SDB_GET_INT32(pRaw, dataPos, &action.groupId, _OVER)
720,152,834✔
398
    }
399
    if (action.actionType == TRANS_ACTION_RAW) {
720,152,834✔
400
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
490,424,301✔
401
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
490,424,301✔
402
      action.pRaw = taosMemoryMalloc(dataLen);
490,424,301✔
403
      if (action.pRaw == NULL) goto _OVER;
490,424,301✔
404
      mTrace("raw:%p, is created", action.pRaw);
490,424,301✔
405
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
490,424,301✔
406
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
490,424,301✔
407
      action.pRaw = NULL;
490,424,301✔
408
    } else if (action.actionType == TRANS_ACTION_MSG) {
229,728,533✔
409
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
229,728,533✔
410
      tmsgUpdateDnodeEpSet(&action.epSet);
229,728,533✔
411
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
229,728,533✔
412
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
229,728,533✔
413
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
229,728,533✔
414
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
229,728,533✔
415
      action.pCont = taosMemoryMalloc(action.contLen);
229,728,533✔
416
      if (action.pCont == NULL) goto _OVER;
229,728,533✔
417
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
229,728,533✔
418
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
229,728,533✔
419
      action.pCont = NULL;
229,728,533✔
420
    } else {
421
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
422
    }
423
  }
424
  ret = 0;
436,837,296✔
425

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

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

449
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
109,631,157✔
450

451
  if (sver > TRANS_VER_CURRENT) {
109,631,157✔
452
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
453
    goto _OVER;
×
454
  }
455

456
  pRow = sdbAllocRow(sizeof(STrans));
109,631,157✔
457
  if (pRow == NULL) goto _OVER;
109,631,157✔
458

459
  pTrans = sdbGetRowObj(pRow);
109,631,157✔
460
  if (pTrans == NULL) goto _OVER;
109,631,157✔
461

462
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
109,631,157✔
463

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

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

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

504
  if (pTrans->prepareActions == NULL) goto _OVER;
109,631,157✔
505
  if (pTrans->redoActions == NULL) goto _OVER;
109,631,157✔
506
  if (pTrans->undoActions == NULL) goto _OVER;
109,631,157✔
507
  if (pTrans->commitActions == NULL) goto _OVER;
109,631,157✔
508

509
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
109,631,157✔
510
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
109,631,157✔
511
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,687,332✔
512
                                      sver) < 0)
513
      goto _OVER;
×
514
  } else {
515
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
107,943,825✔
516
  }
517
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
109,631,157✔
518
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
109,631,157✔
519

520
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
109,631,157✔
521
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
109,631,157✔
522
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
109,631,157✔
523
  if (pTrans->paramLen != 0) {
109,631,157✔
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);
109,631,157✔
530

531
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
109,631,157✔
532
  if (arbgroupIdNum > 0) {
109,631,157✔
533
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
45,995✔
534
    if (pTrans->arbGroupIds == NULL) goto _OVER;
45,995✔
535
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
99,680✔
536
      int32_t arbGroupId = 0;
53,685✔
537
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
53,685✔
538
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
53,685✔
539
    }
540
  }
541

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

551
  if (sver > TRANS_VER2_NUMBER) {
109,631,157✔
552
    int32_t groupNum = -1;
109,631,157✔
553
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
109,631,157✔
554
    for (int32_t i = 0; i < groupNum; ++i) {
114,685,935✔
555
      int32_t groupId = -1;
5,054,778✔
556
      int32_t groupPos = -1;
5,054,778✔
557
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
5,054,778✔
558
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
5,054,778✔
559
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
5,054,778✔
560
        goto _OVER;
×
561
    }
562
  }
563

564
  if (sver >= TRANS_VER_USER_DATA) {
109,631,157✔
565
    SDB_GET_INT32(pRaw, dataPos, &pTrans->userDataLen, _OVER)
109,631,157✔
566
    if (pTrans->userDataLen > 0) {
109,631,157✔
567
      pTrans->userData = taosMemoryMalloc(pTrans->userDataLen);
188,145✔
568
      if (pTrans->userData == NULL) goto _OVER;
188,145✔
569
      SDB_GET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
188,145✔
570
    }
571
  }
572

573
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
109,631,157✔
574

575
  terrno = 0;
109,631,157✔
576

577
_OVER:
109,631,157✔
578
  if (terrno != 0 && pTrans != NULL) {
109,631,157✔
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) {
109,631,157✔
587
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
109,631,157✔
588
  }
589
  return pRow;
109,631,157✔
590
}
591

592
static const char *mndTransStr(ETrnStage stage) {
774,781,141✔
593
  switch (stage) {
774,781,141✔
594
    case TRN_STAGE_PREPARE:
61,995,348✔
595
      return "prepare";
61,995,348✔
596
    case TRN_STAGE_REDO_ACTION:
267,880,958✔
597
      return "redoAction";
267,880,958✔
598
    case TRN_STAGE_ROLLBACK:
7,960✔
599
      return "rollback";
7,960✔
600
    case TRN_STAGE_UNDO_ACTION:
12,237,848✔
601
      return "undoAction";
12,237,848✔
602
    case TRN_STAGE_COMMIT:
99,256,346✔
603
      return "commit";
99,256,346✔
604
    case TRN_STAGE_COMMIT_ACTION:
186,420,914✔
605
      return "commitAction";
186,420,914✔
606
    case TRN_STAGE_FINISH:
146,973,217✔
607
      return "finished";
146,973,217✔
608
    case TRN_STAGE_PRE_FINISH:
8,550✔
609
      return "pre-finish";
8,550✔
610
    default:
×
611
      return "invalid";
×
612
  }
613
}
614

615
static const char *mndTransTypeStr(ETrnAct actionType) {
68,178✔
616
  switch (actionType) {
68,178✔
617
    case TRANS_ACTION_MSG:
56,018✔
618
      return "msg";
56,018✔
619
    case TRANS_ACTION_RAW:
12,160✔
620
      return "sdb";
12,160✔
621
    default:
×
622
      return "invalid";
×
623
  }
624
}
625

626
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
216,078,840✔
627
  if (pAction != NULL) {
216,078,840✔
628
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
166,198,281✔
629
      pTrans->lastAction = pAction->id;
118,027,010✔
630
      pTrans->lastMsgType = pAction->msgType;
118,027,010✔
631
      pTrans->lastEpset = pAction->epSet;
118,027,010✔
632
      pTrans->lastErrorNo = pAction->errCode;
118,027,010✔
633
    }
634
  } else {
635
    pTrans->lastAction = 0;
49,880,559✔
636
    pTrans->lastMsgType = 0;
49,880,559✔
637
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
49,880,559✔
638
    pTrans->lastErrorNo = 0;
49,880,559✔
639
  }
640
}
216,078,840✔
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) {
551,902✔
651
  switch (ftype) {
551,902✔
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:
275,951✔
657
      return mndRebCntInc;
275,951✔
658
    case TRANS_STOP_FUNC_MQ_REB:
275,951✔
659
      return mndRebCntDec;
275,951✔
660
    default:
×
661
      return NULL;
×
662
  }
663
}
664

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

669
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
20,803,382✔
670

671
  if (pTrans->startFunc > 0) {
20,803,382✔
672
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
275,951✔
673
    if (fp) {
275,951✔
674
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
275,951✔
675
    }
676
    // pTrans->startFunc = 0;
677
  }
678

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

684
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
20,803,382✔
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) {
20,803,382✔
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;
20,803,382✔
695
}
696

697
void mndTransDropData(STrans *pTrans) {
129,166,889✔
698
  if (pTrans->prepareActions != NULL) {
129,166,889✔
699
    mndTransDropActions(pTrans->prepareActions);
129,166,889✔
700
    pTrans->prepareActions = NULL;
129,166,889✔
701
  }
702
  if (pTrans->redoActions != NULL) {
129,166,889✔
703
    mndTransDropActions(pTrans->redoActions);
129,166,889✔
704
    pTrans->redoActions = NULL;
129,166,889✔
705
  }
706
  if (pTrans->undoActions != NULL) {
129,166,889✔
707
    mndTransDropActions(pTrans->undoActions);
129,166,889✔
708
    pTrans->undoActions = NULL;
129,166,889✔
709
  }
710
  if (pTrans->commitActions != NULL) {
129,166,889✔
711
    mndTransDropActions(pTrans->commitActions);
129,166,889✔
712
    pTrans->commitActions = NULL;
129,166,889✔
713
  }
714
  if (pTrans->redoGroupActions != NULL) {
129,166,889✔
715
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
21,223,064✔
716
    while (pIter) {
26,527,482✔
717
      SArray **redoActions = pIter;
5,304,418✔
718
      taosArrayDestroy(*redoActions);
5,304,418✔
719
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
5,304,418✔
720
    }
721

722
    taosHashCleanup(pTrans->redoGroupActions);
21,223,064✔
723
    pTrans->redoGroupActions = NULL;
21,223,064✔
724
  }
725
  if (pTrans->groupActionPos != NULL) {
129,166,889✔
726
    taosHashCleanup(pTrans->groupActionPos);
21,223,064✔
727
    pTrans->groupActionPos = NULL;
21,223,064✔
728
  }
729
  if (pTrans->arbGroupIds != NULL) {
129,166,889✔
730
    taosHashCleanup(pTrans->arbGroupIds);
19,581,727✔
731
  }
732
  if (pTrans->pRpcArray != NULL) {
129,166,889✔
733
    taosArrayDestroy(pTrans->pRpcArray);
19,535,732✔
734
    pTrans->pRpcArray = NULL;
19,535,732✔
735
  }
736
  if (pTrans->rpcRsp != NULL) {
129,166,889✔
737
    taosMemoryFree(pTrans->rpcRsp);
7,590,659✔
738
    pTrans->rpcRsp = NULL;
7,590,659✔
739
    pTrans->rpcRspLen = 0;
7,590,659✔
740
  }
741
  if (pTrans->param != NULL) {
129,166,889✔
742
    taosMemoryFree(pTrans->param);
×
743
    pTrans->param = NULL;
×
744
    pTrans->paramLen = 0;
×
745
  }
746
  if (pTrans->userData != NULL) {
129,166,889✔
747
    taosMemoryFree(pTrans->userData);
188,284✔
748
    pTrans->userData = NULL;
188,284✔
749
    pTrans->userDataLen = 0;
188,284✔
750
  }
751
  (void)taosThreadMutexDestroy(&pTrans->mutex);
129,166,889✔
752
}
129,166,889✔
753

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

758
  if (pTrans->stopFunc > 0 && callFunc) {
65,214,544✔
759
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
275,951✔
760
    if (fp) {
275,951✔
761
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
275,951✔
762
    }
763
    // pTrans->stopFunc = 0;
764
  }
765

766
  mndTransDropData(pTrans);
65,214,544✔
767
  return 0;
65,214,544✔
768
}
769

770
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
94,531,692✔
771
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
283,868,354✔
772
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
189,336,662✔
773
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
189,336,662✔
774
    pOldAction->rawWritten = pNewAction->rawWritten;
189,336,662✔
775
    pOldAction->msgSent = pNewAction->msgSent;
189,336,662✔
776
    pOldAction->msgReceived = pNewAction->msgReceived;
189,336,662✔
777
    pOldAction->errCode = pNewAction->errCode;
189,336,662✔
778
  }
779
}
94,531,692✔
780

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

785
  if (pOld->createdTime != pNew->createdTime) {
23,632,923✔
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);
23,632,923✔
795
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
23,632,923✔
796
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
23,632,923✔
797
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
23,632,923✔
798
  pOld->stage = pNew->stage;
23,632,923✔
799
  pOld->actionPos = pNew->actionPos;
23,632,923✔
800
  TSWAP(pOld->userData, pNew->userData);
23,632,923✔
801
  TSWAP(pOld->userDataLen, pNew->userDataLen);
23,632,923✔
802

803
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
23,632,923✔
804
  while (pIter != NULL) {
25,777,492✔
805
    int32_t newActionPos = *(int32_t *)pIter;
2,144,569✔
806

807
    size_t   keylen = 0;
2,144,569✔
808
    int32_t *groupId = taosHashGetKey(pIter, &keylen);
2,144,569✔
809

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

816
    pIter = taosHashIterate(pNew->groupActionPos, pIter);
2,144,569✔
817
  }
818

819
  if (pOld->stage == TRN_STAGE_COMMIT) {
23,632,923✔
820
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
20,776,215✔
821
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
20,776,215✔
822
  }
823

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

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

834
  return 0;
23,632,923✔
835
}
836

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

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

851
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
19,535,732✔
852
                       const char *opername) {
853
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
19,535,732✔
854
  if (pTrans == NULL) {
19,535,732✔
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) {
19,535,732✔
861
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
19,535,732✔
862
  }
863

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

886
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
19,535,732✔
887
      pTrans->pRpcArray == NULL) {
19,535,732✔
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) {
19,535,732✔
895
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
26,942,954✔
896
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
897
      return NULL;
×
898
    }
899
    pTrans->originRpcType = pReq->msgType;
13,471,477✔
900
  }
901

902
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
19,535,732✔
903

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

908
static void mndTransDropActions(SArray *pArray) {
516,667,556✔
909
  int32_t size = taosArrayGetSize(pArray);
516,667,556✔
910
  for (int32_t i = 0; i < size; ++i) {
1,381,299,777✔
911
    STransAction *pAction = taosArrayGet(pArray, i);
864,632,221✔
912
    if (pAction->actionType == TRANS_ACTION_RAW) {
864,632,221✔
913
      taosMemoryFreeClear(pAction->pRaw);
582,565,268✔
914
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
282,066,953✔
915
      taosMemoryFreeClear(pAction->pCont);
282,066,953✔
916
    } else {
917
      // nothing
918
    }
919
  }
920

921
  taosArrayDestroy(pArray);
516,667,556✔
922
}
516,667,556✔
923

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

932
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
115,047,703✔
933
  pAction->id = taosArrayGetSize(pArray);
115,047,703✔
934

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

940
  return 0;
115,047,703✔
941
}
942

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

946
  SArray **redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
570,587✔
947
  if (redoAction == NULL) {
570,587✔
948
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
249,640✔
949
    if (array != NULL) {
249,640✔
950
      if (taosHashPut(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
249,640✔
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));
249,640✔
955
    }
956
  }
957
  if (redoAction != NULL) {
570,587✔
958
    mInfo("trans:%d, append action into group %d, msgType:%s", pTrans->id, pAction->groupId,
570,587✔
959
          TMSG_INFO(pAction->msgType));
960
    void *ptr = taosArrayPush(*redoAction, &pAction);
570,587✔
961
    if (ptr == NULL) {
570,587✔
962
      TAOS_RETURN(terrno);
×
963
    }
964
  }
965

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

975
  return 0;
570,587✔
976
}
977

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

985
int32_t mndTransAppendGroupRedolog(STrans *pTrans, SSdbRaw *pRaw, int32_t groupId) {
331,343✔
986
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION,
331,343✔
987
                         .actionType = TRANS_ACTION_RAW,
988
                         .pRaw = pRaw,
989
                         .mTraceId = pTrans->mTraceId,
331,343✔
990
                         .groupId = groupId};
991
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
331,343✔
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));
331,343✔
996
  return mndTransAppendAction(pTrans->redoActions, &action);
331,343✔
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) {
4,336,768✔
1019
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
4,336,768✔
1020
  return mndTransAppendAction(pTrans->undoActions, &action);
4,336,768✔
1021
}
1022

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

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

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

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

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

1057
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
272,895✔
1058
  pTrans->startFunc = startFunc;
272,895✔
1059
  pTrans->stopFunc = stopFunc;
272,895✔
1060
  pTrans->param = param;
272,895✔
1061
  pTrans->paramLen = paramLen;
272,895✔
1062
}
272,895✔
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) {
10,903,811✔
1096
  if (dbname != NULL) {
10,903,811✔
1097
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
10,903,811✔
1098
  }
1099
  if (stbname != NULL) {
10,903,811✔
1100
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
8,262,266✔
1101
  }
1102
}
10,903,811✔
1103

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

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

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

1123
void mndTransSetGroupParallel(STrans *pTrans) {
101,388✔
1124
  mInfo("trans:%d, set Group Parallel", pTrans->id);
101,388✔
1125
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
101,388✔
1126
}
101,388✔
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) {
40,928✔
1136
  pTrans->ableToBeKilled = true; 
40,928✔
1137
  pTrans->killMode = killMode; 
40,928✔
1138
}
40,928✔
1139

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

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

1144
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
41,175,396✔
1145
  int32_t  code = 0;
41,175,396✔
1146
  SSdbRaw *pRaw = mndTransEncode(pTrans);
41,175,396✔
1147
  if (pRaw == NULL) {
41,175,396✔
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));
41,175,396✔
1154

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

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

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

1176
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
6,190,777✔
1177
  if (conflict[0] == 0) return false;
6,190,777✔
1178
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
5,995,957✔
1179
  return false;
5,820,895✔
1180
}
1181

1182
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
6,446,136✔
1183
  if (conflict) {
6,446,136✔
1184
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
229,174✔
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;
229,174✔
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", 
6,216,962✔
1191
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1192
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1193
  }
1194
}
6,446,136✔
1195

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

1201
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
51,024,526✔
1202

1203
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
38,974,579✔
1204
  mDebug("trans:%d, trans hash size %d", pNew->id, size);
38,974,579✔
1205

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

1210
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
6,265,949✔
1211

1212
    if (pNew->conflict == TRN_CONFLICT_DB) {
6,265,949✔
1213
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
210,958✔
1214
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
210,958✔
1215
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
194,820✔
1216
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
194,820✔
1217
      }
1218
    }
1219

1220
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
6,265,949✔
1221
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
6,016,102✔
1222
      if (pTrans->conflict == TRN_CONFLICT_DB) {
6,016,102✔
1223
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
60,539✔
1224
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
60,539✔
1225
      }
1226
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
6,016,102✔
1227
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
5,935,418✔
1228
      }
1229
    }
1230

1231
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
6,265,949✔
1232
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
33,345✔
1233
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
33,345✔
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) {
6,265,949✔
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);
6,265,949✔
1258
  }
1259

1260
  return conflict;
38,974,579✔
1261
}
1262

1263
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
51,024,526✔
1264
  int32_t code = 0;
51,024,526✔
1265
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
51,024,526✔
1266
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
32,718,615✔
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)) {
51,024,526✔
1274
    code = TSDB_CODE_MND_TRANS_CONFLICT;
270,963✔
1275
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
270,963✔
1276
    TAOS_RETURN(code);
270,963✔
1277
  }
1278

1279
  TAOS_RETURN(code);
50,753,563✔
1280
}
1281

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

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

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

1300
    if (thisConflict) {
2,537✔
1301
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
2,537✔
1302
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1303
      conflict = true;
2,537✔
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);
2,537✔
1309
  }
1310

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

1317
  TAOS_RETURN(code);
156,522✔
1318
}
1319

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

1327
  while ((pIter = sdbFetch(pSdb, SDB_RETENTION, pIter, (void **)&pRetention)) != NULL) {
156,522✔
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) {
156,522✔
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);
156,522✔
1357
}
1358

1359
static bool mndTransActionsOfSameType(SArray *pActions) {
43,878,720✔
1360
  int32_t size = taosArrayGetSize(pActions);
43,878,720✔
1361
  ETrnAct lastActType = TRANS_ACTION_NULL;
43,878,720✔
1362
  bool    same = true;
43,878,720✔
1363
  for (int32_t i = 0; i < size; ++i) {
137,806,642✔
1364
    STransAction *pAction = taosArrayGet(pActions, i);
93,927,922✔
1365
    if (i > 0) {
93,927,922✔
1366
      if (lastActType != pAction->actionType) {
62,833,030✔
1367
        same = false;
×
1368
        break;
×
1369
      }
1370
    }
1371
    lastActType = pAction->actionType;
93,927,922✔
1372
  }
1373
  return same;
43,878,720✔
1374
}
1375

1376
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
19,237,110✔
1377
  int32_t code = 0;
19,237,110✔
1378
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
19,237,110✔
1379
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
18,836,229✔
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) {
18,836,229✔
1386
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
5,805,381✔
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);
19,237,110✔
1395
}
1396

1397
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
19,237,110✔
1398
  int32_t code = 0;
19,237,110✔
1399
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
19,237,110✔
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) {
19,237,110✔
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);
19,237,110✔
1411
}
1412

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

1419
  mInfo("trans:%d, action list:", pTrans->id);
19,237,940✔
1420
  int32_t index = 0;
19,237,940✔
1421
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
29,754,618✔
1422
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
10,516,678✔
1423
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
10,516,678✔
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) {
40,282,433✔
1428
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
21,044,493✔
1429
    if (pAction->actionType == TRANS_ACTION_MSG) {
21,044,493✔
1430
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
20,373,084✔
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),
671,409✔
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) {
90,433,401✔
1440
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
71,195,461✔
1441
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
71,195,461✔
1442
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1443
  }
1444

1445
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
31,407,332✔
1446
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
12,169,392✔
1447
    if (pAction->actionType == TRANS_ACTION_MSG) {
12,169,392✔
1448
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
7,832,624✔
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),
4,336,768✔
1452
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1453
    }
1454
  }
1455

1456
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
19,237,940✔
1457

1458
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
19,237,110✔
1459

1460
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
19,237,110✔
1461

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

1472
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
19,236,749✔
1473
  if (pNew == NULL) {
19,236,749✔
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;
19,236,749✔
1481
  pNew->rpcRsp = pTrans->rpcRsp;
19,236,749✔
1482
  pNew->rpcRspLen = pTrans->rpcRspLen;
19,236,749✔
1483
  pNew->mTraceId = pTrans->mTraceId;
19,236,749✔
1484
  pTrans->pRpcArray = NULL;
19,236,749✔
1485
  pTrans->rpcRsp = NULL;
19,236,749✔
1486
  pTrans->rpcRspLen = 0;
19,236,749✔
1487

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

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

1506
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
1,592✔
1507
  int32_t code = 0;
1,592✔
1508
  mInfo("trans:%d, rollback transaction", pTrans->id);
1,592✔
1509
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,592✔
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,592✔
1514
  TAOS_RETURN(code);
1,592✔
1515
}
1516

1517
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
1,710✔
1518
  int32_t code = 0;
1,710✔
1519
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
1,710✔
1520
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,710✔
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,710✔
1525
  TAOS_RETURN(code);
1,710✔
1526
}
1527

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

1532
  if (pTrans->stage == TRN_STAGE_FINISH) {
88,176,533✔
1533
    sendRsp = true;
40,012,847✔
1534
  }
1535

1536
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
88,176,533✔
1537
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
23,692,399✔
1538
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
9,552✔
1539
      sendRsp = true;
9,552✔
1540
    }
1541
  } else {
1542
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
64,484,134✔
1543
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
36,780,265✔
1544
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1545
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1546
      } else {
1547
        if (pTrans->failedTimes > 6) sendRsp = true;
36,780,265✔
1548
      }
1549
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
36,780,265✔
1550
    }
1551
  }
1552

1553
  if (!sendRsp) {
88,176,533✔
1554
    return;
48,154,134✔
1555
  } else {
1556
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
40,022,399✔
1557
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1558
  }
1559

1560
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
40,022,399✔
1561
  taosWLockLatch(&pTrans->lockRpcArray);
40,022,399✔
1562
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
40,022,399✔
1563
  if (size <= 0) {
40,022,399✔
1564
    taosWUnLockLatch(&pTrans->lockRpcArray);
26,820,911✔
1565
    return;
26,820,911✔
1566
  }
1567

1568
  for (int32_t i = 0; i < size; ++i) {
26,402,976✔
1569
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
13,201,488✔
1570
    if (pInfo->handle != NULL) {
13,201,488✔
1571
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
12,697,226✔
1572
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1573
      }
1574
      if (code == TSDB_CODE_SYN_TIMEOUT) {
12,697,226✔
1575
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1576
      }
1577

1578
      if (i != 0 && code == 0) {
12,697,226✔
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,
12,697,226✔
1582
            mndTransStr(pTrans->stage), pInfo->ahandle);
1583

1584
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
12,697,226✔
1585

1586
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
12,697,226✔
1587
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,345,146✔
1588
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,345,146✔
1589
        if (pDb != NULL) {
1,345,146✔
1590
          for (int32_t j = 0; j < 12; j++) {
1,696,675✔
1591
            bool ready = mndIsDbReady(pMnode, pDb);
1,696,034✔
1592
            if (!ready) {
1,696,034✔
1593
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
351,529✔
1594
              taosMsleep(1000);
351,529✔
1595
            } else {
1596
              break;
1,344,505✔
1597
            }
1598
          }
1599
        }
1600
        mndReleaseDb(pMnode, pDb);
1,345,146✔
1601
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
11,352,080✔
1602
        void   *pCont = NULL;
2,005,751✔
1603
        int32_t contLen = 0;
2,005,751✔
1604
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,005,751✔
1605
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,004,159✔
1606
        }
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
9,346,329✔
1608
        void   *pCont = NULL;
9,859✔
1609
        int32_t contLen = 0;
9,859✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
9,859✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
9,859✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
9,336,470✔
1614
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
9,453✔
1615
        if (code != 0) {
9,453✔
1616
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1617
        }
1618
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
9,327,017✔
1619
        void   *pCont = NULL;
19,404✔
1620
        int32_t contLen = 0;
19,404✔
1621
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
19,404✔
1622
          mndTransSetRpcRsp(pTrans, pCont, contLen);
19,404✔
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
9,307,613✔
1625
        void   *pCont = NULL;
18,086✔
1626
        int32_t contLen = 0;
18,086✔
1627
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
18,086✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
18,086✔
1629
        }
1630
      }
1631

1632
      if (pTrans->rpcRspLen != 0) {
12,697,226✔
1633
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
7,572,573✔
1634
        if (rpcCont != NULL) {
7,572,573✔
1635
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
7,572,573✔
1636
          rspMsg.pCont = rpcCont;
7,572,573✔
1637
          rspMsg.contLen = pTrans->rpcRspLen;
7,572,573✔
1638
        }
1639
      }
1640

1641
      tmsgSendRsp(&rspMsg);
12,697,226✔
1642

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

1651
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
22,512,725✔
1652
  int32_t code = 0;
22,512,725✔
1653
  SMnode *pMnode = pRsp->info.node;
22,512,725✔
1654
#ifndef TD_ASTRA_32
1655
  int64_t signature = (int64_t)(pRsp->info.ahandle);
22,512,725✔
1656
  int32_t transId = (int32_t)(signature >> 32);
22,512,725✔
1657
  int32_t action = (int32_t)((signature << 32) >> 32);
22,512,725✔
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);
22,512,725✔
1663
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
22,512,725✔
1664
  if (pTrans == NULL) {
22,512,725✔
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;
22,512,725✔
1672
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
22,512,725✔
1673
    pArray = pTrans->redoActions;
22,506,357✔
1674
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
6,368✔
1675
    pArray = pTrans->undoActions;
6,368✔
1676
  } else {
1677
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1678
    goto _OVER;
×
1679
  }
1680

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

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

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

1699
      // pTrans->lastErrorNo = pRsp->code;
1700
      mndSetTransLastAction(pTrans, pAction);
22,512,725✔
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,
22,512,725✔
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 {
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);
22,512,725✔
1717
  mndTransExecute(pMnode, pTrans, true);
22,512,725✔
1718

1719
_OVER:
22,512,725✔
1720
  mndReleaseTrans(pMnode, pTrans);
22,512,725✔
1721
  TAOS_RETURN(code);
22,512,725✔
1722
}
1723

1724
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
2,191,293✔
1725
  pAction->rawWritten = 0;
2,191,293✔
1726
  pAction->msgSent = 0;
2,191,293✔
1727
  pAction->msgReceived = 0;
2,191,293✔
1728
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
2,191,293✔
1729
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
2,191,293✔
1730
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
1,011✔
1731
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
1,011✔
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);
2,190,282✔
1735
  }
1736
  //  pAction->errCode = 0;
1737
}
2,191,293✔
1738

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

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

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

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

1760
// execute in sync context
1761
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
159,289,110✔
1762
  if (pAction->rawWritten) return 0;
159,289,110✔
1763
  if (topHalf) {
87,454,595✔
1764
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
1,817✔
1765
  }
1766

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

1775
  // TEST: delay stream persist to SDB so that first deploy runs before stream is in SDB -> "ignore deploy",
1776
  // then next deploy only comes from SDB loop after 5*MST_SHORT_ISOLATION_DURATION(10s)=50s -> reproduce 50s wait
1777
  // if (pAction->pRaw->type == SDB_STREAM) {
1778
  //   mInfo("trans:%d, delay 2s before writing stream to sdb for test repro 50s wait", pTrans->id);
1779
  //   taosMsleep(2000);
1780
  // }
1781

1782
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
87,452,778✔
1783
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
87,452,778✔
1784
    pAction->rawWritten = true;
87,452,778✔
1785
    pAction->errCode = 0;
87,452,778✔
1786
    code = 0;
87,452,778✔
1787
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
87,452,778✔
1788
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1789

1790
    mndSetTransLastAction(pTrans, pAction);
87,452,778✔
1791
  } else {
1792
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1793
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1794
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1795
    mndSetTransLastAction(pTrans, pAction);
×
1796
  }
1797

1798
  TAOS_RETURN(code);
87,452,778✔
1799
}
1800

1801
// execute in trans context
1802
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
96,954,085✔
1803
                                     bool notSend) {
1804
  if (pAction->msgSent) return 0;
96,954,085✔
1805
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
36,478,873✔
1806
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
13,949,840✔
1807
  }
1808

1809
  if (notSend) {
22,529,033✔
1810
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
8,809✔
1811
    return 0;
8,809✔
1812
  }
1813

1814
#ifndef TD_ASTRA_32
1815
  int64_t signature = pTrans->id;
22,520,224✔
1816
  signature = (signature << 32);
22,520,224✔
1817
  signature += pAction->id;
22,520,224✔
1818

1819
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
22,520,224✔
1820
#else
1821
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1822
                    .contLen = pAction->contLen,
1823
                    .info.ahandle = (void *)pTrans->id,
1824
                    .info.ahandleEx = (void *)pAction->id};
1825
#endif
1826
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
22,520,224✔
1827
  if (rpcMsg.pCont == NULL) {
22,520,224✔
1828
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1829
    return -1;
×
1830
  }
1831
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
22,520,224✔
1832
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
22,520,224✔
1833
  rpcMsg.info.notFreeAhandle = 1;
22,520,224✔
1834
  STraceId* trace = &(rpcMsg.info.traceId);
22,520,224✔
1835

1836
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
22,520,224✔
1837

1838
  char    detail[1024] = {0};
22,520,224✔
1839
  int32_t len = snprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
22,520,224✔
1840
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
22,520,224✔
1841
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
47,911,650✔
1842
    len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
25,391,426✔
1843
                    pAction->epSet.eps[i].port);
25,391,426✔
1844
  }
1845

1846
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
22,520,224✔
1847
  if (code == 0) {
22,520,224✔
1848
    pAction->msgSent = 1;
22,520,224✔
1849
    // pAction->msgReceived = 0;
1850
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
22,520,224✔
1851
    pAction->startTime = taosGetTimestampMs();
22,520,224✔
1852
    pAction->endTime = 0;
22,520,224✔
1853
    mInfo("trans:%d, %s:%d is sent, QID:0x%" PRIx64 ":0x%" PRIx64 ", %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, trace ? trace->rootId : 0, 
22,520,224✔
1854
              trace ? trace->msgId : 0, detail);
1855

1856
    mndSetTransLastAction(pTrans, pAction);
22,520,224✔
1857
  } else {
1858
    pAction->msgSent = 0;
×
1859
    pAction->msgReceived = 0;
×
1860
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1861
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
×
1862
           detail);
1863

1864
    mndSetTransLastAction(pTrans, pAction);
×
1865
  }
1866

1867
  TAOS_RETURN(code);
22,520,224✔
1868
}
1869

1870
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1871
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1872
  pAction->rawWritten = 0;
×
1873
  pAction->errCode = 0;
×
1874
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1875

1876
  mndSetTransLastAction(pTrans, pAction);
×
1877
  return 0;
×
1878
}
1879

1880
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
256,243,195✔
1881
                                        bool notSend) {
1882
  if (pAction->actionType == TRANS_ACTION_RAW) {
256,243,195✔
1883
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
159,289,110✔
1884
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
96,954,085✔
1885
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
96,954,085✔
1886
  } else {
1887
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1888
  }
1889
}
1890

1891
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
79,762,424✔
1892
  int32_t numOfActions = taosArrayGetSize(pArray);
79,762,424✔
1893
  int32_t code = 0;
79,762,424✔
1894

1895
  for (int32_t action = 0; action < numOfActions; ++action) {
298,966,072✔
1896
    STransAction *pAction = taosArrayGet(pArray, action);
230,079,234✔
1897
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
230,079,234✔
1898
    if (code != 0) {
230,079,234✔
1899
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
10,875,586✔
1900
            numOfActions);
1901
      break;
10,875,586✔
1902
    }
1903
  }
1904

1905
  return code;
79,762,424✔
1906
}
1907

1908
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
98,193,495✔
1909
  int32_t numOfActions = taosArrayGetSize(pArray);
98,193,495✔
1910
  int32_t code = 0;
98,193,495✔
1911
  if (numOfActions == 0) return 0;
98,193,495✔
1912

1913
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
79,762,424✔
1914
    return code;
10,875,586✔
1915
  }
1916

1917
  int32_t       numOfExecuted = 0;
68,886,838✔
1918
  int32_t       errCode = 0;
68,886,838✔
1919
  STransAction *pErrAction = NULL;
68,886,838✔
1920
  for (int32_t action = 0; action < numOfActions; ++action) {
288,090,486✔
1921
    STransAction *pAction = taosArrayGet(pArray, action);
219,203,648✔
1922
    if (pAction->msgReceived || pAction->rawWritten) {
219,203,648✔
1923
      numOfExecuted++;
182,058,870✔
1924
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
182,058,870✔
1925
        errCode = pAction->errCode;
4,086✔
1926
        pErrAction = pAction;
4,086✔
1927
      }
1928
    } else {
1929
      pErrAction = pAction;
37,144,778✔
1930
    }
1931
  }
1932

1933
  mndSetTransLastAction(pTrans, pErrAction);
68,886,838✔
1934

1935
  if (numOfExecuted == numOfActions) {
68,886,838✔
1936
    if (errCode == 0) {
49,882,151✔
1937
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
49,880,559✔
1938
      return 0;
49,880,559✔
1939
    } else {
1940
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,592✔
1941
      mndTransResetActions(pMnode, pTrans, pArray);
1,592✔
1942
      terrno = errCode;
1,592✔
1943
      return errCode;
1,592✔
1944
    }
1945
  } else {
1946
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
19,004,687✔
1947

1948
    for (int32_t action = 0; action < numOfActions; ++action) {
73,524,893✔
1949
      STransAction *pAction = taosArrayGet(pArray, action);
54,520,206✔
1950
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
54,520,206✔
1951
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1952
             pAction->acceptableCode, pAction->retryCode);
1953
      if (pAction->msgSent) {
54,520,206✔
1954
        bool reset = false;
54,511,397✔
1955
        if (pAction->msgReceived) {
54,511,397✔
1956
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
17,375,428✔
1957
        } else {
1958
          int64_t timestamp = taosGetTimestampMs();
37,135,969✔
1959
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
37,135,969✔
1960
        }
1961
        if (reset) {
54,511,397✔
1962
          mndTransResetAction(pMnode, pTrans, pAction);
2,494✔
1963
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,494✔
1964
                pAction->id, pAction->errCode, pAction->startTime);
1965
        }
1966
      }
1967
    }
1968
    return TSDB_CODE_ACTION_IN_PROGRESS;
19,004,687✔
1969
  }
1970
}
1971

1972
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
58,172,840✔
1973
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
58,172,840✔
1974
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
58,172,840✔
1975
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
1,592✔
1976
           mndStrExecutionContext(topHalf));
1977
  }
1978
  return code;
58,172,840✔
1979
}
1980

1981
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
11,144✔
1982
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf, notSend);
11,144✔
1983
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
11,144✔
1984
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1985
           mndStrExecutionContext(topHalf));
1986
  }
1987
  return code;
11,144✔
1988
}
1989

1990
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
40,009,511✔
1991
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
40,009,511✔
1992
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
40,009,511✔
1993
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1994
           mndStrExecutionContext(topHalf));
1995
  }
1996
  return code;
40,009,511✔
1997
}
1998

1999
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
7,691,280✔
2000
  int32_t code = 0;
7,691,280✔
2001
  int32_t numOfActions = taosArrayGetSize(pActions);
7,691,280✔
2002
  if (numOfActions == 0) return code;
7,691,280✔
2003

2004
  if (pTrans->actionPos >= numOfActions) {
7,689,642✔
2005
    return code;
340,847✔
2006
  }
2007

2008
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
7,348,795✔
2009
        pTrans->actionPos, mndTransStr(pTrans->stage));
2010

2011
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
11,661,332✔
2012
    STransAction *pAction = taosArrayGet(pActions, action);
11,360,415✔
2013

2014
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
11,360,415✔
2015
      code = TSDB_CODE_ACTION_IN_PROGRESS;
7,887✔
2016
      break;
8,040✔
2017
    }
2018

2019
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
11,352,528✔
2020
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
2021

2022
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
11,352,528✔
2023
    if (code == 0) {
11,352,528✔
2024
      if (pAction->msgSent) {
9,674,042✔
2025
        bool reset = false;
8,468,191✔
2026
        if (pAction->msgReceived) {
8,468,191✔
2027
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,730,915✔
2028
            code = pAction->errCode;
2,153,508✔
2029
            reset = true;
2,153,508✔
2030
          } else {
2031
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,577,407✔
2032
          }
2033
        } else {
2034
          int64_t timestamp = taosGetTimestampMs();
4,737,276✔
2035
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
4,737,276✔
2036
          code = TSDB_CODE_ACTION_IN_PROGRESS;
4,737,276✔
2037
        }
2038
        if (reset) {
8,468,191✔
2039
          mndTransResetAction(pMnode, pTrans, pAction);
2,153,508✔
2040
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,153,508✔
2041
                pAction->id, pAction->errCode, pAction->startTime);
2042
        }
2043
      } else if (pAction->rawWritten) {
1,205,851✔
2044
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,205,851✔
2045
          code = pAction->errCode;
×
2046
        } else {
2047
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,205,851✔
2048
        }
2049
      } else {
2050
      }
2051
    }
2052

2053
    if (code == 0) {
11,352,528✔
2054
      pTrans->failedTimes = 0;
2,783,258✔
2055
    }
2056
    mndSetTransLastAction(pTrans, pAction);
11,352,528✔
2057
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
11,352,528✔
2058
      mInfo(
6,415,762✔
2059
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2060
          "msgReceived:%d",
2061
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2062
          pAction->msgSent, pAction->msgReceived);
2063
    } else if (code != 0) {
4,936,766✔
2064
      mError(
2,153,508✔
2065
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2066
          "msgReceived:%d",
2067
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2068
          pAction->msgSent, pAction->msgReceived);
2069
    }
2070

2071
    char str[200] = {0};
11,352,528✔
2072
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
11,352,528✔
2073
      pTrans->lastErrorNo = code;
2,302,715✔
2074
      pTrans->code = code;
2,302,715✔
2075
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
2,302,715✔
2076
            action, str);
2077
      break;
2,302,715✔
2078
    }
2079

2080
    if (code == 0) {
9,049,813✔
2081
      pTrans->code = 0;
2,159,029✔
2082
      pTrans->actionPos++;
2,159,029✔
2083
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
2,159,029✔
2084
            pAction->id);
2085
      (void)taosThreadMutexUnlock(&pTrans->mutex);
2,159,029✔
2086
      code = mndTransSync(pMnode, pTrans);
2,159,029✔
2087
      (void)taosThreadMutexLock(&pTrans->mutex);
2,159,029✔
2088
      if (code != 0) {
2,159,029✔
2089
        pTrans->actionPos--;
×
2090
        pTrans->code = terrno;
×
2091
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2092
               mndTransStr(pAction->stage), pAction->id, terrstr());
2093
        break;
×
2094
      }
2095
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
6,890,784✔
2096
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,737,276✔
2097
      break;
4,737,276✔
2098
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
2,153,508✔
2099
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
1,011✔
2100
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
2,153,508✔
2101
            code, tstrerror(code));
2102
      pTrans->lastErrorNo = code;
2,153,508✔
2103
      taosMsleep(300);
2,153,508✔
2104
      action--;
2,153,508✔
2105
      continue;
2,153,508✔
2106
    } else {
2107
      terrno = code;
×
2108
      pTrans->lastErrorNo = code;
×
2109
      pTrans->code = code;
×
2110
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2111
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2112
      break;
×
2113
    }
2114
  }
2115

2116
  return code;
7,348,795✔
2117
}
2118

2119
static int32_t mndTransExecuteActionsSerialGroup(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf,
4,245,508✔
2120
                                                 int32_t groupId, int32_t currentGroup, int32_t groupCount,
2121
                                                 bool notSend, SHashObj *pHash) {
2122
  int32_t code = 0;
4,245,508✔
2123
  int32_t numOfActions = taosArrayGetSize(pActions);
4,245,508✔
2124
  if (numOfActions == 0) return code;
4,245,508✔
2125

2126
  if (groupId <= 0) {
4,245,508✔
2127
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
2128
    return TSDB_CODE_INTERNAL_ERROR;
×
2129
  }
2130

2131
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &groupId, sizeof(int32_t));
4,245,508✔
2132
  if (actionPos == NULL) {
4,245,508✔
2133
    mError("trans:%d, failed to execute action in serail group, actionPos is null at group %d", pTrans->id, groupId);
×
2134
    return TSDB_CODE_INTERNAL_ERROR;
×
2135
  }
2136

2137
  if (*actionPos >= numOfActions) {
4,245,508✔
2138
    mInfo("trans:%d, this serial group is finished, actionPos:%d >= numOfActions:%d at group %d", pTrans->id,
699,196✔
2139
          *actionPos, numOfActions, groupId);
2140
    return TSDB_CODE_MND_TRANS_GROUP_FINISHED;
699,196✔
2141
  }
2142

2143
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
4,121,739✔
2144
    STransAction **ppAction = taosArrayGet(pActions, action);
3,876,144✔
2145
    STransAction  *pAction = *ppAction;
3,876,144✔
2146

2147
    if (notSend && !pAction->msgSent) {
3,876,144✔
2148
      mInfo(
522,397✔
2149
          "trans:%d, %s:%d (%d/%d at group %d) skip to execute, curent state(actionType(1:msg,2:log):%d, "
2150
          "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2151
          pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2152
          pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2153
      code = TSDB_CODE_ACTION_IN_PROGRESS;
522,397✔
2154
      break;
522,397✔
2155
    }
2156

2157
    mInfo(
3,353,747✔
2158
        "trans:%d, %s:%d (%d/%d at group %d) begin to execute, curent state(actionType(1:msg,2:log):%d, "
2159
        "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2160
        pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2161
        pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2162

2163
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
3,353,747✔
2164
    if (code == 0) {
3,353,747✔
2165
      if (pAction->msgSent) {
1,956,162✔
2166
        if (pAction->msgReceived) {
1,748,689✔
2167
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
512,044✔
2168
            code = pAction->errCode;
33,699✔
2169
            mndTransResetAction(pMnode, pTrans, pAction);
33,699✔
2170
          } else {
2171
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
478,345✔
2172
                  pAction->id, action, numOfActions, groupId);
2173
          }
2174
        } else {
2175
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,236,645✔
2176
        }
2177
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,748,689✔
2178
        if (msgSent != NULL) {
1,748,689✔
2179
          *msgSent = pAction->msgSent;
1,748,689✔
2180
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,748,689✔
2181
        } else {
2182
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2183
        }
2184
      } else if (pAction->rawWritten) {
207,473✔
2185
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
207,473✔
2186
          code = pAction->errCode;
×
2187
        } else {
2188
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
207,473✔
2189
        }
2190
      } else {
2191
      }
2192
    }
2193

2194
    if (code == 0) {
3,353,747✔
2195
      pTrans->failedTimes = 0;
685,818✔
2196
    }
2197
    mndSetTransLastAction(pTrans, pAction);
3,353,747✔
2198

2199
    char str[200] = {0};
3,353,747✔
2200
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
3,353,747✔
2201
      pTrans->lastErrorNo = code;
1,541,675✔
2202
      pTrans->code = code;
1,541,675✔
2203
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
1,541,675✔
2204
        mInfo(
1,397,585✔
2205
            "trans:%d, %s:%d (%d/%d at group %d) not able to execute since %s, current state("
2206
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2207
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2208
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2209
      } else if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
144,090✔
2210
        mError(
×
2211
            "trans:%d, %s:%d (%d/%d at group %d) failed to execute since %s, current action("
2212
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2213
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2214
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2215
      }
2216
      mInfo("trans:%d, %s:%d (%d/%d at group %d) cannot execute next action, stop this group execution, %s", pTrans->id,
1,541,675✔
2217
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2218
      break;
1,541,675✔
2219
    }
2220

2221
    if (code == 0) {
1,812,072✔
2222
      pTrans->code = 0;
541,728✔
2223
      pTrans->actionPos++;
541,728✔
2224
      (*actionPos)++;
541,728✔
2225
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
541,728✔
2226
            pAction->id);
2227
      (void)taosThreadMutexUnlock(&pTrans->mutex);
541,728✔
2228
      code = mndTransSync(pMnode, pTrans);
541,728✔
2229
      (void)taosThreadMutexLock(&pTrans->mutex);
541,728✔
2230
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
541,728✔
2231
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
12,733,480✔
2232
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
12,191,752✔
2233
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
12,191,752✔
2234
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
12,191,752✔
2235
          pTmpAction->msgSent = *msgSent;
636,582✔
2236
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
636,582✔
2237
        }
2238
      }
2239
      if (code != 0) {
541,728✔
2240
        pTrans->actionPos--;
×
2241
        (*actionPos)--;
×
2242
        pTrans->code = terrno;
×
2243
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2244
               mndTransStr(pAction->stage), pAction->id, terrstr());
2245
        break;
×
2246
      }
2247
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
1,270,344✔
2248
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,236,645✔
2249
            mndTransStr(pAction->stage), pAction->id);
2250
      break;
1,236,645✔
2251
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
33,699✔
2252
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
×
2253
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
33,699✔
2254
            code, tstrerror(code));
2255
      pTrans->lastErrorNo = code;
33,699✔
2256
      taosMsleep(300);
33,699✔
2257
      action--;
33,699✔
2258
      continue;
33,699✔
2259
    } else {
2260
      terrno = code;
×
2261
      pTrans->lastErrorNo = code;
×
2262
      pTrans->code = code;
×
2263
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2264
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2265
      break;
×
2266
    }
2267
  }
2268

2269
  return code;
3,546,312✔
2270
}
2271

2272
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
7,662,100✔
2273
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
7,662,100✔
2274
  (void)taosThreadMutexLock(&pTrans->mutex);
7,662,100✔
2275
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
7,662,100✔
2276
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
7,662,100✔
2277
  }
2278
  (void)taosThreadMutexUnlock(&pTrans->mutex);
7,662,100✔
2279
  return code;
7,662,100✔
2280
}
2281

2282
static int32_t mndTransExecuteRedoActionGroup(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
1,330,746✔
2283
  int32_t total_code = TSDB_CODE_ACTION_IN_PROGRESS;
1,330,746✔
2284
  int32_t code = 0;
1,330,746✔
2285
  int32_t successCount = 0;
1,330,746✔
2286
  int32_t groupCount = taosHashGetSize(pTrans->redoGroupActions);
1,330,746✔
2287

2288
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,330,746✔
2289
  if(pHash == NULL){
1,330,746✔
2290
    mError("trans:%d, failed to init hash since %s", pTrans->id, terrstr());
×
2291
    return -1;
×
2292
  }
2293
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
30,555,813✔
2294
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
29,225,067✔
2295
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
29,225,067✔
2296
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
29,225,067✔
2297
  }
2298
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
1,330,746✔
2299

2300
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,330,746✔
2301
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,330,746✔
2302
  int32_t currentGroup = 1;
1,330,746✔
2303
  while (pIter) {
5,576,254✔
2304
    SArray **redoActions = pIter;
4,245,508✔
2305
    size_t   keyLen = 0;
4,245,508✔
2306
    int32_t *key = taosHashGetKey(pIter, &keyLen);
4,245,508✔
2307
    int32_t  actionCount = taosArrayGetSize(*redoActions);
4,245,508✔
2308
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
4,245,508✔
2309
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2310
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
4,245,508✔
2311
                                             notSend, pHash);
2312
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
4,245,508✔
2313
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,397,585✔
2314
            tstrerror(code));
2315
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,847,923✔
2316
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,759,042✔
2317
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
1,088,881✔
2318
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
699,196✔
2319
    } else if (code != 0) {
389,685✔
2320
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
×
2321
             tstrerror(code));
2322
    } else {
2323
      successCount++;
389,685✔
2324
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
389,685✔
2325
    }
2326
    currentGroup++;
4,245,508✔
2327
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,245,508✔
2328
  }
2329

2330
  taosHashCleanup(pHash);
1,330,746✔
2331

2332
  if (successCount == groupCount) {
1,330,746✔
2333
    total_code = 0;
24,991✔
2334
  } else {
2335
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,305,755✔
2336
  }
2337

2338
  return total_code;
1,330,746✔
2339
}
2340

2341
static int32_t mndTransExecuteRedoActionsParallel(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
1,553,377✔
2342
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
1,553,377✔
2343
  (void)taosThreadMutexLock(&pTrans->mutex);
1,553,377✔
2344

2345
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,553,377✔
2346
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,553,377✔
2347
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,553,377✔
2348
      code = 0;
193,451✔
2349
    } else {
2350
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,359,926✔
2351
      if (pAction != NULL && pAction->groupId == -1) {
1,359,926✔
2352
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
29,180✔
2353
      } else {
2354
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,330,746✔
2355
        if (code == 0) {
1,330,746✔
2356
          if (pTrans->actionPos < numOfActions) {
24,991✔
2357
            code = TSDB_CODE_ACTION_IN_PROGRESS;
17,666✔
2358
          }
2359
        }
2360
      }
2361
    }
2362
  }
2363

2364
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,553,377✔
2365

2366
  return code;
1,553,377✔
2367
}
2368

2369
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
2370
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
2371
  (void)taosThreadMutexLock(&pTrans->mutex);
×
2372
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2373
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
2374
  }
2375
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
2376
  return code;
×
2377
}
2378

2379
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
20,783,376✔
2380
  bool    continueExec = true;
20,783,376✔
2381
  int32_t code = 0;
20,783,376✔
2382
  terrno = 0;
20,783,376✔
2383

2384
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
20,783,376✔
2385
  if (numOfActions == 0) goto _OVER;
20,783,376✔
2386

2387
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
8,409,842✔
2388

2389
  for (int32_t action = 0; action < numOfActions; ++action) {
19,867,528✔
2390
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
11,457,686✔
2391
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
11,457,686✔
2392
    if (code != 0) {
11,457,686✔
2393
      terrno = code;
×
2394
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
2395
             numOfActions, tstrerror(code));
2396
      return false;
×
2397
    }
2398
  }
2399

2400
_OVER:
8,409,842✔
2401
  pTrans->stage = TRN_STAGE_REDO_ACTION;
20,783,376✔
2402
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
20,783,376✔
2403
  return continueExec;
20,783,376✔
2404
}
2405

2406
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
67,388,317✔
2407
  bool    continueExec = true;
67,388,317✔
2408
  int32_t code = 0;
67,388,317✔
2409
  terrno = 0;
67,388,317✔
2410

2411
  if (pTrans->exec == TRN_EXEC_SERIAL) {
67,388,317✔
2412
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
7,662,100✔
2413
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
59,726,217✔
2414
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
58,172,840✔
2415
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,553,377✔
2416
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,553,377✔
2417
  } else {
2418
    code = TSDB_CODE_INTERNAL_ERROR;
×
2419
  }
2420

2421
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
67,389,909✔
2422
      mndTransIsInSyncContext(topHalf)) {
1,592✔
2423
    pTrans->lastErrorNo = code;
×
2424
    pTrans->code = code;
×
2425
    mInfo(
×
2426
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2427
        "continueExec:%d, code:%s",
2428
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2429
    taosMsleep(100);
×
2430
    return true;
×
2431
  } else {
2432
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
67,388,317✔
2433
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
23,656,323✔
2434
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2435
      return false;
23,656,323✔
2436
    }
2437
  }
2438
  terrno = code;
43,731,994✔
2439

2440
  if (code == 0) {
43,731,994✔
2441
    pTrans->code = 0;
19,234,227✔
2442
    pTrans->stage = TRN_STAGE_COMMIT;
19,234,227✔
2443
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
19,234,227✔
2444
    continueExec = true;
19,234,227✔
2445
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
24,497,767✔
2446
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
24,496,175✔
2447
    continueExec = false;
24,496,175✔
2448
  } else {
2449
    pTrans->failedTimes++;
1,592✔
2450
    pTrans->code = terrno;
1,592✔
2451
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
1,592✔
2452
      if (pTrans->lastAction != 0) {
1,592✔
2453
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
1,592✔
2454
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
1,592✔
2455
          if (pTrans->failedTimes < 6) {
×
2456
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2457
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
2458
            taosMsleep(1000);
×
2459
            continueExec = true;
×
2460
            return true;
×
2461
          }
2462
        }
2463
      }
2464

2465
      pTrans->stage = TRN_STAGE_ROLLBACK;
1,592✔
2466
      pTrans->actionPos = 0;
1,592✔
2467
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
1,592✔
2468
             pTrans->actionPos);
2469
      continueExec = true;
1,592✔
2470
    } else {
2471
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2472
      continueExec = false;
×
2473
    }
2474
  }
2475

2476
  return continueExec;
43,731,994✔
2477
}
2478

2479
// execute in trans context
2480
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
19,234,534✔
2481
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
19,234,534✔
2482

2483
  bool    continueExec = true;
19,234,227✔
2484
  int32_t code = mndTransCommit(pMnode, pTrans);
19,234,227✔
2485

2486
  if (code == 0) {
19,234,227✔
2487
    pTrans->code = 0;
19,232,675✔
2488
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
19,232,675✔
2489
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
19,232,675✔
2490
    continueExec = true;
19,232,675✔
2491
  } else {
2492
    pTrans->code = terrno;
1,552✔
2493
    pTrans->failedTimes++;
1,552✔
2494
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
1,552✔
2495
    continueExec = false;
1,552✔
2496
  }
2497

2498
  return continueExec;
19,234,227✔
2499
}
2500

2501
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
40,009,511✔
2502
  bool    continueExec = true;
40,009,511✔
2503
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
40,009,511✔
2504

2505
  if (code == 0) {
40,009,511✔
2506
    pTrans->code = 0;
40,009,204✔
2507
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
40,009,204✔
2508
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
40,009,204✔
2509
    continueExec = true;
40,009,204✔
2510
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
307✔
2511
    pTrans->code = 0;
307✔
2512
    pTrans->stage = TRN_STAGE_COMMIT;
307✔
2513
    mInfo("trans:%d, back to commit stage", pTrans->id);
307✔
2514
    continueExec = true;
307✔
2515
  } else {
2516
    pTrans->code = terrno;
×
2517
    pTrans->failedTimes++;
×
2518
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2519
    continueExec = false;
×
2520
  }
2521

2522
  return continueExec;
40,009,511✔
2523
}
2524

2525
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
11,144✔
2526
  bool    continueExec = true;
11,144✔
2527
  int32_t code = 0;
11,144✔
2528

2529
  if (pTrans->exec == TRN_EXEC_SERIAL) {
11,144✔
2530
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
2531
  } else {
2532
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
11,144✔
2533
  }
2534

2535
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
11,144✔
2536
  terrno = code;
9,552✔
2537

2538
  if (code == 0) {
9,552✔
2539
    pTrans->stage = TRN_STAGE_PRE_FINISH;
1,592✔
2540
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
1,592✔
2541
    continueExec = true;
1,592✔
2542
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
7,960✔
2543
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
7,960✔
2544
    continueExec = false;
7,960✔
2545
  } else {
2546
    pTrans->failedTimes++;
×
2547
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2548
    continueExec = false;
×
2549
  }
2550

2551
  return continueExec;
9,552✔
2552
}
2553

2554
// in trans context
2555
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
1,592✔
2556
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
1,592✔
2557

2558
  bool    continueExec = true;
1,592✔
2559
  int32_t code = mndTransRollback(pMnode, pTrans);
1,592✔
2560

2561
  if (code == 0) {
1,592✔
2562
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1,592✔
2563
    continueExec = true;
1,592✔
2564
  } else {
2565
    pTrans->failedTimes++;
×
2566
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2567
    continueExec = false;
×
2568
  }
2569

2570
  return continueExec;
1,592✔
2571
}
2572

2573
// excute in trans context
2574
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
1,710✔
2575
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
1,710✔
2576

2577
  bool    continueExec = true;
1,710✔
2578
  int32_t code = mndTransPreFinish(pMnode, pTrans);
1,710✔
2579

2580
  if (code == 0) {
1,710✔
2581
    pTrans->stage = TRN_STAGE_FINISH;
1,710✔
2582
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
1,710✔
2583
    continueExec = true;
1,710✔
2584
  } else {
2585
    pTrans->failedTimes++;
×
2586
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2587
    continueExec = false;
×
2588
  }
2589

2590
  return continueExec;
1,710✔
2591
}
2592

2593
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
40,012,624✔
2594
  bool continueExec = false;
40,012,624✔
2595
  if (topHalf) return continueExec;
40,012,624✔
2596

2597
  SSdbRaw *pRaw = mndTransEncode(pTrans);
20,778,239✔
2598
  if (pRaw == NULL) {
20,778,239✔
2599
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
2600
    return false;
×
2601
  }
2602
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
20,778,239✔
2603

2604
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
20,778,239✔
2605
  if (code != 0) {
20,778,239✔
2606
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
2607
  }
2608

2609
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
20,778,239✔
2610
        pTrans->failedTimes, pTrans->createdTime);
2611
  return continueExec;
20,778,239✔
2612
}
2613

2614
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
88,176,533✔
2615
  bool continueExec = true;
88,176,533✔
2616

2617
  while (continueExec) {
254,835,965✔
2618
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
166,659,432✔
2619
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2620
    pTrans->lastExecTime = taosGetTimestampMs();
166,659,432✔
2621
    switch (pTrans->stage) {
166,659,432✔
2622
      case TRN_STAGE_PREPARE:
×
2623
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2624
        break;
×
2625
      case TRN_STAGE_REDO_ACTION:
67,388,317✔
2626
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
67,388,317✔
2627
        break;
67,388,317✔
2628
      case TRN_STAGE_COMMIT:
19,234,534✔
2629
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
19,234,534✔
2630
        break;
19,234,534✔
2631
      case TRN_STAGE_COMMIT_ACTION:
40,009,511✔
2632
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
40,009,511✔
2633
        break;
40,009,511✔
2634
      case TRN_STAGE_ROLLBACK:
1,592✔
2635
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,592✔
2636
        break;
1,592✔
2637
      case TRN_STAGE_UNDO_ACTION:
11,144✔
2638
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
11,144✔
2639
        break;
11,144✔
2640
      case TRN_STAGE_PRE_FINISH:
1,710✔
2641
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,710✔
2642
        break;
1,710✔
2643
      case TRN_STAGE_FINISH:
40,012,624✔
2644
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
40,012,624✔
2645
        break;
40,012,624✔
2646
      default:
×
2647
        continueExec = false;
×
2648
        break;
×
2649
    }
2650
  }
2651

2652
  mndTransSendRpcRsp(pMnode, pTrans);
88,176,533✔
2653
}
88,176,533✔
2654

2655
// start trans, pullup, receive rsp, kill
2656
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
43,759,920✔
2657
  bool topHalf = true;
43,759,920✔
2658
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
43,759,920✔
2659
}
43,759,920✔
2660

2661
// update trans
2662
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
44,416,613✔
2663
  bool topHalf = false;
44,416,613✔
2664
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
44,416,613✔
2665
}
44,416,613✔
2666

2667
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
16,339,626✔
2668
  mTrace("start to process trans timer");
16,339,626✔
2669
  mndTransPullup(pReq->info.node);
16,339,626✔
2670
  return 0;
16,339,626✔
2671
}
2672

2673
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
118✔
2674
  SArray *pArray = NULL;
118✔
2675
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
118✔
2676
    pArray = pTrans->redoActions;
118✔
2677
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2678
    pArray = pTrans->undoActions;
×
2679
  } else {
2680
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2681
  }
2682

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

2706
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
118✔
2707
  mndTransExecute(pMnode, pTrans, true);
118✔
2708
  return 0;
118✔
2709
}
2710

2711
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
273✔
2712
  SMnode       *pMnode = pReq->info.node;
273✔
2713
  SKillTransReq killReq = {0};
273✔
2714
  int32_t       code = -1;
273✔
2715
  STrans       *pTrans = NULL;
273✔
2716

2717
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
273✔
2718
    code = TSDB_CODE_INVALID_MSG;
×
2719
    goto _OVER;
×
2720
  }
2721

2722
  mInfo("trans:%d, start to kill, force:%d", killReq.transId, tsForceKillTrans);
273✔
2723
  if ((code = mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_TRANS)) != 0) {
273✔
2724
    goto _OVER;
×
2725
  }
2726

2727
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
273✔
2728
  if (pTrans == NULL) {
273✔
2729
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
155✔
2730
    if (terrno != 0) code = terrno;
155✔
2731
    goto _OVER;
155✔
2732
  }
2733

2734
  code = mndKillTrans(pMnode, pTrans);
118✔
2735

2736
_OVER:
273✔
2737
  if (code != 0) {
273✔
2738
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
155✔
2739
  }
2740

2741
  mndReleaseTrans(pMnode, pTrans);
273✔
2742
  TAOS_RETURN(code);
273✔
2743
}
2744

2745
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
107,064✔
2746

2747
void mndTransPullup(SMnode *pMnode) {
16,474,702✔
2748
  SSdb   *pSdb = pMnode->pSdb;
16,474,702✔
2749
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
16,474,702✔
2750
  if (pArray == NULL) return;
16,474,702✔
2751

2752
  void *pIter = NULL;
16,474,702✔
2753
  while (1) {
2,010,328✔
2754
    STrans *pTrans = NULL;
18,485,030✔
2755
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
18,485,030✔
2756
    if (pIter == NULL) break;
18,485,030✔
2757
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
4,020,656✔
2758
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2759
    }
2760
    sdbRelease(pSdb, pTrans);
2,010,328✔
2761
  }
2762

2763
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
16,474,702✔
2764

2765
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
18,485,030✔
2766
    int32_t *pTransId = taosArrayGet(pArray, i);
2,010,328✔
2767
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
2,010,328✔
2768
    if (pTrans != NULL) {
2,010,328✔
2769
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
2,010,328✔
2770
      mndTransExecute(pMnode, pTrans, false);
2,010,328✔
2771
    }
2772
    mndReleaseTrans(pMnode, pTrans);
2,010,328✔
2773
  }
2774
  taosArrayDestroy(pArray);
16,474,702✔
2775
}
2776

2777
static char *formatTimestamp(char *buf, int32_t cap, int64_t val, int precision) {
17,643,924✔
2778
  time_t tt;
17,643,924✔
2779
  if (precision == TSDB_TIME_PRECISION_MICRO) {
17,643,924✔
2780
    tt = (time_t)(val / 1000000);
×
2781
  }
2782
  if (precision == TSDB_TIME_PRECISION_NANO) {
17,643,924✔
2783
    tt = (time_t)(val / 1000000000);
×
2784
  } else {
2785
    tt = (time_t)(val / 1000);
17,643,924✔
2786
  }
2787

2788
  struct tm tm;
17,643,924✔
2789
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
17,643,924✔
2790
    mError("failed to get local time");
×
2791
    return NULL;
×
2792
  }
2793
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
17,643,924✔
2794

2795
  if (precision == TSDB_TIME_PRECISION_MICRO) {
17,643,924✔
2796
    snprintf(buf + pos, cap - (pos), ".%06d", (int)(val % 1000000));
×
2797
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
17,643,924✔
2798
    snprintf(buf + pos, cap - (pos), ".%09d", (int)(val % 1000000000));
×
2799
  } else {
2800
    snprintf(buf + pos, cap - (pos), ".%03d", (int)(val % 1000));
17,643,924✔
2801
  }
2802

2803
  return buf;
17,643,924✔
2804
}
2805

2806
static void mndTransLogAction(STrans *pTrans) {
375,655✔
2807
  char    detail[512] = {0};
375,655✔
2808
  int32_t len = 0;
375,655✔
2809
  int32_t index = 0;
375,655✔
2810

2811
  if (pTrans->stage == TRN_STAGE_PREPARE) {
375,655✔
2812
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2813
      len = 0;
×
2814
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2815
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2816
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2817
                      sdbStatusName(pAction->pRaw->status));
×
2818
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2819
    }
2820
  }
2821

2822
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
375,655✔
2823
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
11,972,078✔
2824
      len = 0;
11,596,423✔
2825
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
11,596,423✔
2826
      if (pAction->actionType == TRANS_ACTION_MSG) {
11,596,423✔
2827
        char bufStart[40] = {0};
8,805,012✔
2828
        (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
8,805,012✔
2829

2830
        char endStart[40] = {0};
8,805,012✔
2831
        (void)formatTimestamp(endStart, sizeof(endStart), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
8,805,012✔
2832
        len += snprintf(detail + len, sizeof(detail) - len,
17,610,024✔
2833
                        "action:%d, %s:%d msgType:%s,"
2834
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2835
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
17,610,024✔
2836
                        pAction->msgReceived, bufStart, endStart);
8,805,012✔
2837

2838
        SEpSet epset = pAction->epSet;
8,805,012✔
2839
        if (epset.numOfEps > 0) {
8,805,012✔
2840
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
8,805,012✔
2841
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
19,736,306✔
2842
            len +=
10,931,294✔
2843
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
10,931,294✔
2844
          }
2845
        }
2846

2847
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
8,805,012✔
2848
                        tstrerror(pAction->errCode));
2849
      } else {
2850
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
2,791,411✔
2851
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
2,791,411✔
2852
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
2,791,411✔
2853
      }
2854
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
11,596,423✔
2855
    }
2856
  }
2857

2858
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
375,655✔
2859
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
×
2860
      len = 0;
×
2861
      STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
×
2862
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2863
                      mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type),
×
2864
                      sdbStatusName(pAction->pRaw->status));
×
2865
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2866
    }
2867

2868
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
×
2869
      len = 0;
×
2870
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
2871
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
2872
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
2873
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2874
        ;
2875
      } else {
2876
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2877
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2878
                        sdbStatusName(pAction->pRaw->status));
×
2879
      }
2880
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2881
    }
2882
  }
2883
}
375,655✔
2884

2885
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
432,014✔
2886
  SMnode *pMnode = pReq->info.node;
432,014✔
2887
  SSdb   *pSdb = pMnode->pSdb;
432,014✔
2888
  int32_t numOfRows = 0;
432,014✔
2889
  STrans *pTrans = NULL;
432,014✔
2890
  int32_t cols = 0;
432,014✔
2891
  int32_t code = 0;
432,014✔
2892
  int32_t lino = 0;
432,014✔
2893

2894
  while (numOfRows < rows) {
807,669✔
2895
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
807,669✔
2896
    if (pShow->pIter == NULL) break;
807,669✔
2897

2898
    cols = 0;
375,655✔
2899

2900
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2901
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
375,655✔
2902

2903
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2904
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
375,655✔
2905
                        _OVER);
2906

2907
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
375,655✔
2908
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
375,655✔
2909
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2910
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
375,655✔
2911

2912
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
375,655✔
2913
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
375,655✔
2914
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2915
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
375,655✔
2916

2917
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
375,655✔
2918
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
375,655✔
2919
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2920
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
375,655✔
2921

2922
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
375,655✔
2923
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
375,655✔
2924
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2925
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
375,655✔
2926

2927
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
375,655✔
2928
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
375,655✔
2929
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
375,655✔
2930
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2931
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
375,655✔
2932

2933
    /*
2934
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2935
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2936
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2937
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2938
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2939
    */
2940

2941
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2942
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
375,655✔
2943
                        _OVER);
2944

2945
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2946
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
375,655✔
2947
                        _OVER);
2948

2949
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
375,655✔
2950
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
375,655✔
2951
    int32_t len = snprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
751,310✔
2952
                           pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
751,310✔
2953
    SEpSet  epset = pTrans->lastEpset;
375,655✔
2954
    if (epset.numOfEps > 0) {
375,655✔
2955
      len += snprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
746,446✔
2956
                      TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
746,446✔
2957
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
1,030,960✔
2958
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
657,737✔
2959
      }
2960
    }
2961
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
375,655✔
2962
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
375,655✔
2963
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
375,655✔
2964

2965
    mndTransLogAction(pTrans);
375,655✔
2966

2967
    numOfRows++;
375,655✔
2968
    sdbRelease(pSdb, pTrans);
375,655✔
2969
  }
2970

2971
_OVER:
432,014✔
2972
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
432,014✔
2973
  pShow->numOfRows += numOfRows;
432,014✔
2974
  return numOfRows;
432,014✔
2975
}
2976

2977
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
68,178✔
2978
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2979
  int32_t code = 0;
68,178✔
2980
  int32_t lino = 0;
68,178✔
2981
  int32_t len = 0;
68,178✔
2982

2983
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
68,178✔
2984
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&transactionId, false), &lino, _OVER);
68,178✔
2985

2986
  char action[30 + 1] = {0};
68,178✔
2987
  if (curActionId == pAction->id) {
68,178✔
2988
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
498✔
2989
                    mndTransTypeStr(pAction->actionType));
2990
  } else {
2991
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
67,680✔
2992
                    mndTransTypeStr(pAction->actionType));
2993
  }
2994
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
68,178✔
2995
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
68,178✔
2996
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
68,178✔
2997
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
68,178✔
2998
_OVER:
68,178✔
2999
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
68,178✔
3000
  return code;
68,178✔
3001
}
3002

3003
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
68,178✔
3004
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
3005
  int32_t code = 0;
68,178✔
3006
  int32_t lino = 0;
68,178✔
3007
  int32_t len = 0;
68,178✔
3008
  int32_t cols = 0;
68,178✔
3009

3010
  cols = 0;
68,178✔
3011

3012
  if (mndShowTransCommonColumns(pShow, pBlock, pAction, transactionId, curActionId, numOfRows, &cols) != 0) return;
68,178✔
3013

3014
  if (pAction->actionType == TRANS_ACTION_MSG) {
68,178✔
3015
    int32_t len = 0;
56,018✔
3016

3017
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
56,018✔
3018
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
56,018✔
3019
                    pAction->msgSent, pAction->msgReceived);
56,018✔
3020
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
56,018✔
3021
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
56,018✔
3022
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
56,018✔
3023
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
56,018✔
3024

3025
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
56,018✔
3026
    len = 0;
56,018✔
3027
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
56,018✔
3028
                    tstrerror(pAction->errCode));
3029
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
56,018✔
3030
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
56,018✔
3031
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
56,018✔
3032
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
56,018✔
3033

3034
    char target[TSDB_TRANS_TARGET_LEN] = {0};
56,018✔
3035
    len = 0;
56,018✔
3036
    SEpSet epset = pAction->epSet;
56,018✔
3037
    if (epset.numOfEps > 0) {
56,018✔
3038
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
130,276✔
3039
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
74,258✔
3040
      }
3041
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
56,018✔
3042
    }
3043
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
56,018✔
3044
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
56,018✔
3045
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
56,018✔
3046
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
56,018✔
3047

3048
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
56,018✔
3049
    len = 0;
56,018✔
3050
    char bufStart[40] = {0};
56,018✔
3051
    if (pAction->startTime > 0)
56,018✔
3052
      (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
18,660✔
3053
    char bufEnd[40] = {0};
56,018✔
3054
    if (pAction->endTime > 0)
56,018✔
3055
      (void)formatTimestamp(bufEnd, sizeof(bufEnd), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
15,240✔
3056

3057
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
56,018✔
3058
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
56,018✔
3059
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
56,018✔
3060
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
56,018✔
3061
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
56,018✔
3062

3063
  } else {
3064
    int32_t len = 0;
12,160✔
3065

3066
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
12,160✔
3067
    if (pAction->pRaw->type == SDB_VGROUP) {
12,160✔
3068
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
12,160✔
3069
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
12,160✔
3070
      len += snprintf(objType + len, sizeof(objType) - len, "%s(%d)", sdbTableName(pAction->pRaw->type), pVgroup->vgId);
12,160✔
3071
      taosMemoryFreeClear(pRow);
12,160✔
3072
    } else {
3073
      tstrncpy(objType, sdbTableName(pAction->pRaw->type), sizeof(objType));
×
3074
    }
3075
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
12,160✔
3076
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
12,160✔
3077
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,160✔
3078
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
12,160✔
3079

3080
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
12,160✔
3081
    len = 0;
12,160✔
3082
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
12,160✔
3083
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
12,160✔
3084
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
12,160✔
3085
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,160✔
3086
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
12,160✔
3087

3088
    char target[TSDB_TRANS_TARGET_LEN] = "";
12,160✔
3089
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
12,160✔
3090
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
12,160✔
3091
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,160✔
3092
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
12,160✔
3093

3094
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
12,160✔
3095
    len = 0;
12,160✔
3096
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
12,160✔
3097
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
12,160✔
3098
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
12,160✔
3099
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,160✔
3100
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
12,160✔
3101
  }
3102

3103
_OVER:
68,178✔
3104
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
68,178✔
3105
}
3106

3107
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
878✔
3108
  if (stage == TRN_STAGE_PREPARE) {
878✔
3109
    return pTrans->prepareActions;
×
3110
  }
3111
  if (stage == TRN_STAGE_REDO_ACTION) {
878✔
3112
    return pTrans->redoActions;
878✔
3113
  }
3114
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
3115
    return pTrans->commitActions;
×
3116
  }
3117
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
3118
    return pTrans->undoActions;
×
3119
  }
3120
  return NULL;
×
3121
}
3122

3123
typedef struct STransDetailIter {
3124
  void     *pIter;
3125
  STrans   *pTrans;
3126
  ETrnStage stage;
3127
  int32_t   num;
3128
} STransDetailIter;
3129

3130
static void mndTransShowActions(SSdb *pSdb, STransDetailIter *pShowIter, SShowObj *pShow, SSDataBlock *pBlock,
878✔
3131
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3132
  int32_t actionNum = taosArrayGetSize(pActions);
878✔
3133
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
878✔
3134

3135
  for (int32_t i = start; i < actionNum; ++i) {
68,676✔
3136
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
68,178✔
3137
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
68,178✔
3138
    (*numOfRows)++;
68,178✔
3139
    if (*numOfRows >= rows) break;
68,178✔
3140
  }
3141

3142
  if (*numOfRows == end) {
878✔
3143
    sdbRelease(pSdb, pShowIter->pTrans);
498✔
3144
    pShowIter->pTrans = NULL;
498✔
3145
    pShowIter->num = 0;
498✔
3146
  } else {
3147
    pShowIter->pTrans = pShowIter->pTrans;
380✔
3148
    pShowIter->stage = pShowIter->pTrans->stage;
380✔
3149
    pShowIter->num += (*numOfRows);
380✔
3150
  }
3151
}
878✔
3152

3153
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
1,376✔
3154
  SMnode *pMnode = pReq->info.node;
1,376✔
3155
  SSdb   *pSdb = pMnode->pSdb;
1,376✔
3156
  int32_t numOfRows = 0;
1,376✔
3157

3158
  int32_t code = 0;
1,376✔
3159
  int32_t lino = 0;
1,376✔
3160

3161
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
1,376✔
3162
        pShow->pIter);
3163

3164
  if (pShow->pIter == NULL) {
1,376✔
3165
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
498✔
3166
    if (pShow->pIter == NULL) {
498✔
3167
      mError("failed to malloc for pShow->pIter");
×
3168
      return 0;
×
3169
    }
3170
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
498✔
3171
  }
3172

3173
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
1,376✔
3174

3175
  while (numOfRows < rows) {
1,376✔
3176
    if (pShowIter->pTrans == NULL) {
1,376✔
3177
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
996✔
3178
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
996✔
3179
      if (pShowIter->pIter == NULL) break;
996✔
3180
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
498✔
3181
            pShowIter->pTrans->stage, pShowIter->num);
3182

3183
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
498✔
3184

3185
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
498✔
3186
      break;
498✔
3187
    } else {
3188
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
380✔
3189
            pShowIter->num);
3190
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
380✔
3191

3192
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
380✔
3193
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
380✔
3194
      break;
380✔
3195
    }
3196
  }
3197

3198
_OVER:
×
3199
  pShow->numOfRows += numOfRows;
1,376✔
3200

3201
  if (code != 0) {
1,376✔
3202
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
3203
  } else {
3204
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
1,376✔
3205
  }
3206
  if (numOfRows == 0) {
1,376✔
3207
    taosMemoryFree(pShow->pIter);
498✔
3208
    pShow->pIter = NULL;
498✔
3209
  }
3210
  return numOfRows;
1,376✔
3211
}
3212

3213
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
3214
  SSdb *pSdb = pMnode->pSdb;
×
3215
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
3216
}
×
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