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

taosdata / TDengine / #5022

14 Apr 2026 05:32AM UTC coverage: 72.278% (-0.01%) from 72.291%
#5022

push

travis-ci

web-flow
merge: from main to3.0 branch #35128

74 of 97 new or added lines in 12 files covered. (76.29%)

555 existing lines in 127 files now uncovered.

257556 of 356343 relevant lines covered (72.28%)

133355051.72 hits per line

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

84.79
/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; }
135,822,410✔
64

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

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

81
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
192,242,707✔
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) {
483,903✔
94
  SSdbTable table = {
483,903✔
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);
483,903✔
105
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
483,903✔
106

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

113
void mndCleanupTrans(SMnode *pMnode) {}
483,833✔
114

115
static int32_t mndTransGetActionsSize(SArray *pArray) {
250,596,424✔
116
  int32_t actionNum = taosArrayGetSize(pArray);
250,596,424✔
117
  int32_t rawDataLen = 0;
250,596,424✔
118

119
  for (int32_t i = 0; i < actionNum; ++i) {
673,936,246✔
120
    STransAction *pAction = taosArrayGet(pArray, i);
423,339,822✔
121
    if (pAction->actionType == TRANS_ACTION_RAW) {
423,339,822✔
122
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
286,790,996✔
123
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
136,548,826✔
124
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
136,548,826✔
125
    } else {
126
      // empty
127
    }
128
    rawDataLen += sizeof(int8_t);
423,339,822✔
129
  }
130

131
  return rawDataLen;
250,596,424✔
132
}
133

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

142
  for (int32_t i = 0; i < actionsNum; ++i) {
673,936,246✔
143
    STransAction *pAction = taosArrayGet(pActions, i);
423,339,822✔
144
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
423,339,822✔
145
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
423,339,822✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
423,339,822✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
423,339,822✔
148
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
423,339,822✔
149
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
423,339,822✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
423,339,822✔
151
    if (sver > TRANS_VER2_NUMBER) {
423,339,822✔
152
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
423,339,822✔
153
    }
154
    if (pAction->actionType == TRANS_ACTION_RAW) {
423,339,822✔
155
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
286,790,996✔
156
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
286,790,996✔
157
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
286,790,996✔
158
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
286,790,996✔
159
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
136,548,826✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
136,548,826✔
161
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
136,548,826✔
162
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
136,548,826✔
163
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
136,548,826✔
164
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
136,548,826✔
165
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
136,548,826✔
166
    } else {
167
      // nothing
168
    }
169
  }
170
  ret = 0;
250,596,424✔
171

172
_OVER:
250,596,424✔
173
  *offset = dataPos;
250,596,424✔
174
  return ret;
250,596,424✔
175
}
176

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

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

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

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

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

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

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

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

235
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
62,649,106✔
236
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
62,649,106✔
237
  void *pIter = NULL;
62,649,106✔
238
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
62,649,106✔
239
  while (pIter) {
62,688,307✔
240
    int32_t arbGroupId = *(int32_t *)pIter;
39,201✔
241
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
39,201✔
242
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
39,201✔
243
  }
244

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

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

265
  if (sver >= TRANS_VER_USER_DATA) {
62,649,106✔
266
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
62,649,106✔
267
    if (pTrans->userDataLen > 0) {
62,649,106✔
268
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
116,457✔
269
    }
270
  }
271

272
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
62,649,106✔
273
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
62,649,106✔
274

275
  terrno = 0;
62,649,106✔
276

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

289
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
29,681,881✔
290
  if (pAction->groupId < 0) return 0;
29,681,881✔
291
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
29,083,015✔
292
  if (redoAction == NULL) {
29,083,015✔
293
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
5,114,800✔
294
    if (array != NULL) {
5,114,800✔
295
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
5,114,800✔
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,114,800✔
301
  }
302
  if (redoAction != NULL) {
29,083,015✔
303
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
58,166,030✔
304
  }
305
  return 0;
29,083,015✔
306
}
307

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

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

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

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

385
  for (int32_t i = 0; i < actionNum; ++i) {
1,171,702,785✔
386
    memset(&action, 0, sizeof(action));
729,723,434✔
387
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
729,723,434✔
388
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
729,723,434✔
389
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
729,723,434✔
390
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
729,723,434✔
391
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
729,723,434✔
392
    action.actionType = actionType;
729,723,434✔
393
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
729,723,434✔
394
    action.stage = stage;
729,723,434✔
395
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
729,723,434✔
396
    if (sver > TRANS_VER2_NUMBER) {
729,723,434✔
397
      SDB_GET_INT32(pRaw, dataPos, &action.groupId, _OVER)
729,723,434✔
398
    }
399
    if (action.actionType == TRANS_ACTION_RAW) {
729,723,434✔
400
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
497,028,471✔
401
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
497,028,471✔
402
      action.pRaw = taosMemoryMalloc(dataLen);
497,028,471✔
403
      if (action.pRaw == NULL) goto _OVER;
497,028,471✔
404
      mTrace("raw:%p, is created", action.pRaw);
497,028,471✔
405
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
497,028,471✔
406
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
497,028,471✔
407
      action.pRaw = NULL;
497,028,471✔
408
    } else if (action.actionType == TRANS_ACTION_MSG) {
232,694,963✔
409
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
232,694,963✔
410
      tmsgUpdateDnodeEpSet(&action.epSet);
232,694,963✔
411
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
232,694,963✔
412
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
232,694,963✔
413
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
232,694,963✔
414
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
232,694,963✔
415
      action.pCont = taosMemoryMalloc(action.contLen);
232,694,963✔
416
      if (action.pCont == NULL) goto _OVER;
232,694,963✔
417
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
232,694,963✔
418
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
232,694,963✔
419
      action.pCont = NULL;
232,694,963✔
420
    } else {
421
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
422
    }
423
  }
424
  ret = 0;
441,979,351✔
425

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

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

449
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
110,919,943✔
450

451
  if (sver > TRANS_VER_CURRENT) {
110,919,943✔
452
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
453
    goto _OVER;
×
454
  }
455

456
  pRow = sdbAllocRow(sizeof(STrans));
110,919,943✔
457
  if (pRow == NULL) goto _OVER;
110,919,943✔
458

459
  pTrans = sdbGetRowObj(pRow);
110,919,943✔
460
  if (pTrans == NULL) goto _OVER;
110,919,943✔
461

462
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
110,919,943✔
463

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

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

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

504
  if (pTrans->prepareActions == NULL) goto _OVER;
110,919,943✔
505
  if (pTrans->redoActions == NULL) goto _OVER;
110,919,943✔
506
  if (pTrans->undoActions == NULL) goto _OVER;
110,919,943✔
507
  if (pTrans->commitActions == NULL) goto _OVER;
110,919,943✔
508

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

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

531
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
110,919,943✔
532
  if (arbgroupIdNum > 0) {
110,919,943✔
533
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
57,575✔
534
    if (pTrans->arbGroupIds == NULL) goto _OVER;
57,575✔
535
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
122,910✔
536
      int32_t arbGroupId = 0;
65,335✔
537
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
65,335✔
538
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
65,335✔
539
    }
540
  }
541

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

551
  if (sver > TRANS_VER2_NUMBER) {
110,919,943✔
552
    int32_t groupNum = -1;
110,919,943✔
553
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
110,919,943✔
554
    for (int32_t i = 0; i < groupNum; ++i) {
116,034,743✔
555
      int32_t groupId = -1;
5,114,800✔
556
      int32_t groupPos = -1;
5,114,800✔
557
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
5,114,800✔
558
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
5,114,800✔
559
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
5,114,800✔
560
        goto _OVER;
×
561
    }
562
  }
563

564
  if (sver >= TRANS_VER_USER_DATA) {
110,919,943✔
565
    SDB_GET_INT32(pRaw, dataPos, &pTrans->userDataLen, _OVER)
110,919,943✔
566
    if (pTrans->userDataLen > 0) {
110,919,943✔
567
      pTrans->userData = taosMemoryMalloc(pTrans->userDataLen);
194,095✔
568
      if (pTrans->userData == NULL) goto _OVER;
194,095✔
569
      SDB_GET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
194,095✔
570
    }
571
  }
572

573
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
110,919,943✔
574

575
  terrno = 0;
110,919,943✔
576

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

592
static const char *mndTransStr(ETrnStage stage) {
783,006,708✔
593
  switch (stage) {
783,006,708✔
594
    case TRN_STAGE_PREPARE:
62,725,372✔
595
      return "prepare";
62,725,372✔
596
    case TRN_STAGE_REDO_ACTION:
269,824,561✔
597
      return "redoAction";
269,824,561✔
598
    case TRN_STAGE_ROLLBACK:
8,010✔
599
      return "rollback";
8,010✔
600
    case TRN_STAGE_UNDO_ACTION:
12,329,224✔
601
      return "undoAction";
12,329,224✔
602
    case TRN_STAGE_COMMIT:
100,366,263✔
603
      return "commit";
100,366,263✔
604
    case TRN_STAGE_COMMIT_ACTION:
189,093,257✔
605
      return "commitAction";
189,093,257✔
606
    case TRN_STAGE_FINISH:
148,651,396✔
607
      return "finished";
148,651,396✔
608
    case TRN_STAGE_PRE_FINISH:
8,625✔
609
      return "pre-finish";
8,625✔
610
    default:
×
611
      return "invalid";
×
612
  }
613
}
614

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

626
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
218,547,137✔
627
  if (pAction != NULL) {
218,547,137✔
628
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
168,108,854✔
629
      pTrans->lastAction = pAction->id;
119,560,969✔
630
      pTrans->lastMsgType = pAction->msgType;
119,560,969✔
631
      pTrans->lastEpset = pAction->epSet;
119,560,969✔
632
      pTrans->lastErrorNo = pAction->errCode;
119,560,969✔
633
    }
634
  } else {
635
    pTrans->lastAction = 0;
50,438,283✔
636
    pTrans->lastMsgType = 0;
50,438,283✔
637
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
50,438,283✔
638
    pTrans->lastErrorNo = 0;
50,438,283✔
639
  }
640
}
218,547,137✔
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) {
553,568✔
651
  switch (ftype) {
553,568✔
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:
276,784✔
657
      return mndRebCntInc;
276,784✔
658
    case TRANS_STOP_FUNC_MQ_REB:
276,784✔
659
      return mndRebCntDec;
276,784✔
660
    default:
×
661
      return NULL;
×
662
  }
663
}
664

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

669
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
21,061,137✔
670

671
  if (pTrans->startFunc > 0) {
21,061,137✔
672
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
276,784✔
673
    if (fp) {
276,784✔
674
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
276,784✔
675
    }
676
    // pTrans->startFunc = 0;
677
  }
678

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

684
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
21,061,137✔
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) {
21,061,137✔
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;
21,061,137✔
695
}
696

697
void mndTransDropData(STrans *pTrans) {
130,718,589✔
698
  if (pTrans->prepareActions != NULL) {
130,718,589✔
699
    mndTransDropActions(pTrans->prepareActions);
130,718,589✔
700
    pTrans->prepareActions = NULL;
130,718,589✔
701
  }
702
  if (pTrans->redoActions != NULL) {
130,718,589✔
703
    mndTransDropActions(pTrans->redoActions);
130,718,589✔
704
    pTrans->redoActions = NULL;
130,718,589✔
705
  }
706
  if (pTrans->undoActions != NULL) {
130,718,589✔
707
    mndTransDropActions(pTrans->undoActions);
130,718,589✔
708
    pTrans->undoActions = NULL;
130,718,589✔
709
  }
710
  if (pTrans->commitActions != NULL) {
130,718,589✔
711
    mndTransDropActions(pTrans->commitActions);
130,718,589✔
712
    pTrans->commitActions = NULL;
130,718,589✔
713
  }
714
  if (pTrans->redoGroupActions != NULL) {
130,718,589✔
715
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
21,499,067✔
716
    while (pIter) {
26,865,601✔
717
      SArray **redoActions = pIter;
5,366,534✔
718
      taosArrayDestroy(*redoActions);
5,366,534✔
719
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
5,366,534✔
720
    }
721

722
    taosHashCleanup(pTrans->redoGroupActions);
21,499,067✔
723
    pTrans->redoGroupActions = NULL;
21,499,067✔
724
  }
725
  if (pTrans->groupActionPos != NULL) {
130,718,589✔
726
    taosHashCleanup(pTrans->groupActionPos);
21,499,067✔
727
    pTrans->groupActionPos = NULL;
21,499,067✔
728
  }
729
  if (pTrans->arbGroupIds != NULL) {
130,718,589✔
730
    taosHashCleanup(pTrans->arbGroupIds);
19,856,221✔
731
  }
732
  if (pTrans->pRpcArray != NULL) {
130,718,589✔
733
    taosArrayDestroy(pTrans->pRpcArray);
19,798,646✔
734
    pTrans->pRpcArray = NULL;
19,798,646✔
735
  }
736
  if (pTrans->rpcRsp != NULL) {
130,718,589✔
737
    taosMemoryFree(pTrans->rpcRsp);
7,683,096✔
738
    pTrans->rpcRsp = NULL;
7,683,096✔
739
    pTrans->rpcRspLen = 0;
7,683,096✔
740
  }
741
  if (pTrans->param != NULL) {
130,718,589✔
742
    taosMemoryFree(pTrans->param);
×
743
    pTrans->param = NULL;
×
744
    pTrans->paramLen = 0;
×
745
  }
746
  if (pTrans->userData != NULL) {
130,718,589✔
747
    taosMemoryFree(pTrans->userData);
194,239✔
748
    pTrans->userData = NULL;
194,239✔
749
    pTrans->userDataLen = 0;
194,239✔
750
  }
751
  (void)taosThreadMutexDestroy(&pTrans->mutex);
130,718,589✔
752
}
130,718,589✔
753

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

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

766
  mndTransDropData(pTrans);
65,988,831✔
767
  return 0;
65,988,831✔
768
}
769

770
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
95,552,932✔
771
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
287,221,447✔
772
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
191,668,515✔
773
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
191,668,515✔
774
    pOldAction->rawWritten = pNewAction->rawWritten;
191,668,515✔
775
    pOldAction->msgSent = pNewAction->msgSent;
191,668,515✔
776
    pOldAction->msgReceived = pNewAction->msgReceived;
191,668,515✔
777
    pOldAction->errCode = pNewAction->errCode;
191,668,515✔
778
  }
779
}
95,552,932✔
780

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

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

803
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
23,888,233✔
804
  while (pIter != NULL) {
26,059,062✔
805
    int32_t newActionPos = *(int32_t *)pIter;
2,170,829✔
806

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

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

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

819
  if (pOld->stage == TRN_STAGE_COMMIT) {
23,888,233✔
820
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
21,037,519✔
821
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
21,037,519✔
822
  }
823

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

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

834
  return 0;
23,888,233✔
835
}
836

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

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

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

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

886
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
19,798,646✔
887
      pTrans->pRpcArray == NULL) {
19,798,646✔
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,798,646✔
895
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
27,350,174✔
896
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
897
      return NULL;
×
898
    }
899
    pTrans->originRpcType = pReq->msgType;
13,675,087✔
900
  }
901

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

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

908
static void mndTransDropActions(SArray *pArray) {
522,874,356✔
909
  int32_t size = taosArrayGetSize(pArray);
522,874,356✔
910
  for (int32_t i = 0; i < size; ++i) {
1,398,812,042✔
911
    STransAction *pAction = taosArrayGet(pArray, i);
875,937,686✔
912
    if (pAction->actionType == TRANS_ACTION_RAW) {
875,937,686✔
913
      taosMemoryFreeClear(pAction->pRaw);
590,420,334✔
914
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
285,517,352✔
915
      taosMemoryFreeClear(pAction->pCont);
285,517,352✔
916
    } else {
917
      // nothing
918
    }
919
  }
920

921
  taosArrayDestroy(pArray);
522,874,356✔
922
}
522,874,356✔
923

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

932
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
116,532,371✔
933
  pAction->id = taosArrayGetSize(pArray);
116,532,371✔
934

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

940
  return 0;
116,532,371✔
941
}
942

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

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

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

975
  return 0;
574,707✔
976
}
977

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

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

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

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

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

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

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

1057
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
273,664✔
1058
  pTrans->startFunc = startFunc;
273,664✔
1059
  pTrans->stopFunc = stopFunc;
273,664✔
1060
  pTrans->param = param;
273,664✔
1061
  pTrans->paramLen = paramLen;
273,664✔
1062
}
273,664✔
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) {
11,057,003✔
1096
  if (dbname != NULL) {
11,057,003✔
1097
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
11,057,003✔
1098
  }
1099
  if (stbname != NULL) {
11,057,003✔
1100
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
8,411,345✔
1101
  }
1102
}
11,057,003✔
1103

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

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

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

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

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

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

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

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

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

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

1176
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
5,862,715✔
1177
  if (conflict[0] == 0) return false;
5,862,715✔
1178
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
5,693,459✔
1179
  return false;
5,462,828✔
1180
}
1181

1182
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
6,098,276✔
1183
  if (conflict) {
6,098,276✔
1184
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
293,053✔
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;
293,053✔
1189
  } else {
1190
    mInfo("trans:%d, opername:%s db:%s stb:%s type:%d, not conflict with trans:%d, opername:%s db:%s stb:%s type:%d", 
5,805,223✔
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,098,276✔
1195

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

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

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

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

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

1212
    if (pNew->conflict == TRN_CONFLICT_DB) {
5,948,302✔
1213
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
195,419✔
1214
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
195,419✔
1215
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
169,256✔
1216
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
169,256✔
1217
      }
1218
    }
1219

1220
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
5,948,302✔
1221
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
5,711,861✔
1222
      if (pTrans->conflict == TRN_CONFLICT_DB) {
5,711,861✔
1223
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
66,305✔
1224
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
66,305✔
1225
      }
1226
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
5,711,861✔
1227
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
5,627,154✔
1228
      }
1229
    }
1230

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

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

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

1260
  return conflict;
39,446,783✔
1261
}
1262

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

1279
  TAOS_RETURN(code);
51,314,546✔
1280
}
1281

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

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

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

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

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

1317
  TAOS_RETURN(code);
158,882✔
1318
}
1319

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

1327
  while ((pIter = sdbFetch(pSdb, SDB_RETENTION, pIter, (void **)&pRetention)) != NULL) {
158,882✔
1328
    conflict = false;
1,550✔
1329

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

1337
    if (conflict) {
1,550✔
1338
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with retention:%d db:%s", pTrans->id,
1,550✔
1339
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pRetention->id, pRetention->dbname);
1340
      sdbRelease(pSdb, pRetention);
1,550✔
1341
      sdbCancelFetch(pSdb, pIter);
1,550✔
1342
      break;
1,550✔
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) {
158,882✔
1351
    code = TSDB_CODE_MND_TRANS_CONFLICT_RETENTION;
1,550✔
1352
    mError("trans:%d, failed to check tran conflict with retention since %s", pTrans->id, tstrerror(code));
1,550✔
1353
    TAOS_RETURN(code);
1,550✔
1354
  }
1355

1356
  TAOS_RETURN(code);
157,332✔
1357
}
1358

1359
static bool mndTransActionsOfSameType(SArray *pActions) {
44,331,385✔
1360
  int32_t size = taosArrayGetSize(pActions);
44,331,385✔
1361
  ETrnAct lastActType = TRANS_ACTION_NULL;
44,331,385✔
1362
  bool    same = true;
44,331,385✔
1363
  for (int32_t i = 0; i < size; ++i) {
139,573,412✔
1364
    STransAction *pAction = taosArrayGet(pActions, i);
95,242,027✔
1365
    if (i > 0) {
95,242,027✔
1366
      if (lastActType != pAction->actionType) {
63,837,063✔
1367
        same = false;
×
1368
        break;
×
1369
      }
1370
    }
1371
    lastActType = pAction->actionType;
95,242,027✔
1372
  }
1373
  return same;
44,331,385✔
1374
}
1375

1376
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
19,432,780✔
1377
  int32_t code = 0;
19,432,780✔
1378
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
19,432,780✔
1379
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
19,036,934✔
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) {
19,036,934✔
1386
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
5,861,671✔
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,432,780✔
1395
}
1396

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

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

1419
  mInfo("trans:%d, action list:", pTrans->id);
19,434,272✔
1420
  int32_t index = 0;
19,434,272✔
1421
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
30,061,527✔
1422
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
10,627,255✔
1423
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
10,627,255✔
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,678,931✔
1428
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
21,244,659✔
1429
    if (pAction->actionType == TRANS_ACTION_MSG) {
21,244,659✔
1430
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
20,586,061✔
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),
658,598✔
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) {
91,696,725✔
1440
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
72,262,453✔
1441
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
72,262,453✔
1442
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1443
  }
1444

1445
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
31,694,610✔
1446
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
12,260,338✔
1447
    if (pAction->actionType == TRANS_ACTION_MSG) {
12,260,338✔
1448
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
7,897,633✔
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,362,705✔
1452
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1453
    }
1454
  }
1455

1456
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
19,434,272✔
1457

1458
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
19,432,780✔
1459

1460
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
19,432,780✔
1461

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

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

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

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

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

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

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

1532
  if (pTrans->stage == TRN_STAGE_FINISH) {
89,003,273✔
1533
    sendRsp = true;
40,469,102✔
1534
  }
1535

1536
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
89,003,273✔
1537
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
23,885,592✔
1538
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
9,612✔
1539
      sendRsp = true;
9,612✔
1540
    }
1541
  } else {
1542
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
65,117,681✔
1543
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
37,072,198✔
1544
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1545
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1546
      } else {
1547
        if (pTrans->failedTimes > 6) sendRsp = true;
37,072,198✔
1548
      }
1549
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
37,072,198✔
1550
    }
1551
  }
1552

1553
  if (!sendRsp) {
89,003,273✔
1554
    return;
48,524,559✔
1555
  } else {
1556
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
40,478,714✔
1557
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1558
  }
1559

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

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

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

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

1586
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
12,818,886✔
1587
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,354,098✔
1588
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,354,098✔
1589
        if (pDb != NULL) {
1,354,098✔
1590
          for (int32_t j = 0; j < 12; j++) {
1,721,793✔
1591
            bool ready = mndIsDbReady(pMnode, pDb);
1,720,522✔
1592
            if (!ready) {
1,720,522✔
1593
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
367,695✔
1594
              taosMsleep(1000);
367,695✔
1595
            } else {
1596
              break;
1,352,827✔
1597
            }
1598
          }
1599
        }
1600
        mndReleaseDb(pMnode, pDb);
1,354,098✔
1601
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
11,464,788✔
1602
        void   *pCont = NULL;
2,019,552✔
1603
        int32_t contLen = 0;
2,019,552✔
1604
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,019,552✔
1605
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,017,950✔
1606
        }
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
9,445,236✔
1608
        void   *pCont = NULL;
10,000✔
1609
        int32_t contLen = 0;
10,000✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
10,000✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
10,000✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
9,435,236✔
1614
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
9,553✔
1615
        if (code != 0) {
9,553✔
1616
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1617
        }
1618
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
9,425,683✔
1619
        void   *pCont = NULL;
20,156✔
1620
        int32_t contLen = 0;
20,156✔
1621
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
20,156✔
1622
          mndTransSetRpcRsp(pTrans, pCont, contLen);
20,156✔
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
9,405,527✔
1625
        void   *pCont = NULL;
18,519✔
1626
        int32_t contLen = 0;
18,519✔
1627
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
18,519✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
18,519✔
1629
        }
1630
      }
1631

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

1641
      tmsgSendRsp(&rspMsg);
12,818,886✔
1642

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

1651
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
22,738,532✔
1652
  int32_t code = 0;
22,738,532✔
1653
  SMnode *pMnode = pRsp->info.node;
22,738,532✔
1654
#ifndef TD_ASTRA_32
1655
  int64_t signature = (int64_t)(pRsp->info.ahandle);
22,738,532✔
1656
  int32_t transId = (int32_t)(signature >> 32);
22,738,532✔
1657
  int32_t action = (int32_t)((signature << 32) >> 32);
22,738,532✔
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,738,532✔
1663
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
22,738,532✔
1664
  if (pTrans == NULL) {
22,738,532✔
1665
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1666
    if (terrno != 0) code = terrno;
×
1667
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1668
    goto _OVER;
×
1669
  }
1670

1671
  SArray *pArray = NULL;
22,738,532✔
1672
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
22,738,532✔
1673
    pArray = pTrans->redoActions;
22,732,124✔
1674
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
6,408✔
1675
    pArray = pTrans->undoActions;
6,408✔
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,738,532✔
1682
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1683
    goto _OVER;
×
1684
  }
1685

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

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

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

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

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

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

1742
  for (int32_t action = 0; action < numOfActions; ++action) {
8,010✔
1743
    STransAction *pAction = taosArrayGet(pArray, action);
6,408✔
1744
    if (pAction->msgSent && pAction->msgReceived &&
6,408✔
1745
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
6,408✔
1746
      continue;
4,806✔
1747
    if (pAction->msgSent && !pAction->msgReceived) {
1,602✔
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,602✔
1753

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

1760
// execute in sync context
1761
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
161,638,792✔
1762
  if (pAction->rawWritten) return 0;
161,638,792✔
1763
  if (topHalf) {
88,756,325✔
1764
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
551✔
1765
  }
1766

1767
  if (pAction->pRaw->type >= SDB_MAX) {
88,755,774✔
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);
88,755,774✔
1783
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
88,755,774✔
1784
    pAction->rawWritten = true;
88,755,774✔
1785
    pAction->errCode = 0;
88,755,774✔
1786
    code = 0;
88,755,774✔
1787
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
88,755,774✔
1788
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1789

1790
    mndSetTransLastAction(pTrans, pAction);
88,755,774✔
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);
88,755,774✔
1799
}
1800

1801
// execute in trans context
1802
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
97,754,860✔
1803
                                     bool notSend) {
1804
  if (pAction->msgSent) return 0;
97,754,860✔
1805
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
36,877,434✔
1806
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
14,127,408✔
1807
  }
1808

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

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

1819
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
22,740,974✔
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,740,974✔
1827
  if (rpcMsg.pCont == NULL) {
22,740,974✔
1828
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1829
    return -1;
×
1830
  }
1831
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
22,740,974✔
1832
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
22,740,974✔
1833
  rpcMsg.info.notFreeAhandle = 1;
22,740,974✔
1834
  STraceId* trace = &(rpcMsg.info.traceId);
22,740,974✔
1835

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

1838
  char    detail[1024] = {0};
22,740,974✔
1839
  int32_t len = snprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
22,740,974✔
1840
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
22,740,974✔
1841
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
48,416,347✔
1842
    len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
25,675,373✔
1843
                    pAction->epSet.eps[i].port);
25,675,373✔
1844
  }
1845

1846
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
22,740,974✔
1847
  if (code == 0) {
22,740,974✔
1848
    pAction->msgSent = 1;
22,740,220✔
1849
    // pAction->msgReceived = 0;
1850
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
22,740,220✔
1851
    pAction->startTime = taosGetTimestampMs();
22,740,220✔
1852
    pAction->endTime = 0;
22,740,220✔
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,740,220✔
1854
              trace ? trace->msgId : 0, detail);
1855

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

1864
    mndSetTransLastAction(pTrans, pAction);
754✔
1865
  }
1866

1867
  TAOS_RETURN(code);
22,740,974✔
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,
259,393,652✔
1881
                                        bool notSend) {
1882
  if (pAction->actionType == TRANS_ACTION_RAW) {
259,393,652✔
1883
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
161,638,792✔
1884
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
97,754,860✔
1885
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
97,754,860✔
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) {
80,541,740✔
1892
  int32_t numOfActions = taosArrayGetSize(pArray);
80,541,740✔
1893
  int32_t code = 0;
80,541,740✔
1894

1895
  for (int32_t action = 0; action < numOfActions; ++action) {
302,481,087✔
1896
    STransAction *pAction = taosArrayGet(pArray, action);
232,970,356✔
1897
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
232,970,356✔
1898
    if (code != 0) {
232,970,356✔
1899
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
11,031,009✔
1900
            numOfActions);
1901
      break;
11,031,009✔
1902
    }
1903
  }
1904

1905
  return code;
80,541,740✔
1906
}
1907

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

1913
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
80,541,740✔
1914
    return code;
11,031,009✔
1915
  }
1916

1917
  int32_t       numOfExecuted = 0;
69,510,731✔
1918
  int32_t       errCode = 0;
69,510,731✔
1919
  STransAction *pErrAction = NULL;
69,510,731✔
1920
  for (int32_t action = 0; action < numOfActions; ++action) {
291,450,078✔
1921
    STransAction *pAction = taosArrayGet(pArray, action);
221,939,347✔
1922
    if (pAction->msgReceived || pAction->rawWritten) {
221,939,347✔
1923
      numOfExecuted++;
184,642,982✔
1924
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
184,642,982✔
1925
        errCode = pAction->errCode;
2,731✔
1926
        pErrAction = pAction;
2,731✔
1927
      }
1928
    } else {
1929
      pErrAction = pAction;
37,296,365✔
1930
    }
1931
  }
1932

1933
  mndSetTransLastAction(pTrans, pErrAction);
69,510,731✔
1934

1935
  if (numOfExecuted == numOfActions) {
69,510,731✔
1936
    if (errCode == 0) {
50,439,885✔
1937
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
50,438,283✔
1938
      return 0;
50,438,283✔
1939
    } else {
1940
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,602✔
1941
      mndTransResetActions(pMnode, pTrans, pArray);
1,602✔
1942
      terrno = errCode;
1,602✔
1943
      return errCode;
1,602✔
1944
    }
1945
  } else {
1946
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
19,070,846✔
1947

1948
    for (int32_t action = 0; action < numOfActions; ++action) {
73,905,828✔
1949
      STransAction *pAction = taosArrayGet(pArray, action);
54,834,982✔
1950
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
54,834,982✔
1951
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1952
             pAction->acceptableCode, pAction->retryCode);
1953
      if (pAction->msgSent) {
54,834,982✔
1954
        bool reset = false;
54,825,930✔
1955
        if (pAction->msgReceived) {
54,825,930✔
1956
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
17,538,617✔
1957
        } else {
1958
          int64_t timestamp = taosGetTimestampMs();
37,287,313✔
1959
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
37,287,313✔
1960
        }
1961
        if (reset) {
54,825,930✔
1962
          mndTransResetAction(pMnode, pTrans, pAction);
1,129✔
1963
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1,129✔
1964
                pAction->id, pAction->errCode, pAction->startTime);
1965
        }
1966
      }
1967
    }
1968
    return TSDB_CODE_ACTION_IN_PROGRESS;
19,070,846✔
1969
  }
1970
}
1971

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

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

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

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

2004
  if (pTrans->actionPos >= numOfActions) {
7,686,788✔
2005
    return code;
335,782✔
2006
  }
2007

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

2011
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
11,659,846✔
2012
    STransAction *pAction = taosArrayGet(pActions, action);
11,364,145✔
2013

2014
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
11,364,145✔
2015
      code = TSDB_CODE_ACTION_IN_PROGRESS;
7,966✔
2016
      break;
8,125✔
2017
    }
2018

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

2022
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
11,356,179✔
2023
    if (code == 0) {
11,356,179✔
2024
      if (pAction->msgSent) {
9,673,786✔
2025
        bool reset = false;
8,494,380✔
2026
        if (pAction->msgReceived) {
8,494,380✔
2027
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,740,639✔
2028
            code = pAction->errCode;
2,160,687✔
2029
            reset = true;
2,160,687✔
2030
          } else {
2031
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,579,952✔
2032
          }
2033
        } else {
2034
          int64_t timestamp = taosGetTimestampMs();
4,753,741✔
2035
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
4,753,741✔
2036
          code = TSDB_CODE_ACTION_IN_PROGRESS;
4,753,741✔
2037
        }
2038
        if (reset) {
8,494,380✔
2039
          mndTransResetAction(pMnode, pTrans, pAction);
2,160,687✔
2040
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,160,687✔
2041
                pAction->id, pAction->errCode, pAction->startTime);
2042
        }
2043
      } else if (pAction->rawWritten) {
1,179,406✔
2044
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,179,406✔
2045
          code = pAction->errCode;
×
2046
        } else {
2047
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,179,406✔
2048
        }
2049
      } else {
2050
      }
2051
    }
2052

2053
    if (code == 0) {
11,356,179✔
2054
      pTrans->failedTimes = 0;
2,759,358✔
2055
    }
2056
    mndSetTransLastAction(pTrans, pAction);
11,356,179✔
2057
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
11,356,179✔
2058
      mInfo(
6,435,380✔
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,920,799✔
2064
      mError(
2,161,441✔
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,356,179✔
2072
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
11,356,179✔
2073
      pTrans->lastErrorNo = code;
2,292,844✔
2074
      pTrans->code = code;
2,292,844✔
2075
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
2,292,844✔
2076
            action, str);
2077
      break;
2,292,844✔
2078
    }
2079

2080
    if (code == 0) {
9,063,335✔
2081
      pTrans->code = 0;
2,148,153✔
2082
      pTrans->actionPos++;
2,148,153✔
2083
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
2,148,153✔
2084
            pAction->id);
2085
      (void)taosThreadMutexUnlock(&pTrans->mutex);
2,148,153✔
2086
      code = mndTransSync(pMnode, pTrans);
2,148,153✔
2087
      (void)taosThreadMutexLock(&pTrans->mutex);
2,148,153✔
2088
      if (code != 0) {
2,148,153✔
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,915,182✔
2096
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,753,741✔
2097
      break;
4,753,741✔
2098
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
2,161,441✔
2099
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
1,804✔
2100
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
2,160,687✔
2101
            code, tstrerror(code));
2102
      pTrans->lastErrorNo = code;
2,160,687✔
2103
      taosMsleep(300);
2,160,687✔
2104
      action--;
2,160,687✔
2105
      continue;
2,160,687✔
2106
    } else {
2107
      terrno = code;
754✔
2108
      pTrans->lastErrorNo = code;
754✔
2109
      pTrans->code = code;
754✔
2110
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
754✔
2111
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2112
      break;
754✔
2113
    }
2114
  }
2115

2116
  return code;
7,351,006✔
2117
}
2118

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

2126
  if (groupId <= 0) {
4,381,527✔
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,381,527✔
2132
  if (actionPos == NULL) {
4,381,527✔
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,381,527✔
2138
    mInfo("trans:%d, this serial group is finished, actionPos:%d >= numOfActions:%d at group %d", pTrans->id,
738,030✔
2139
          *actionPos, numOfActions, groupId);
2140
    return TSDB_CODE_MND_TRANS_GROUP_FINISHED;
738,030✔
2141
  }
2142

2143
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
4,226,421✔
2144
    STransAction **ppAction = taosArrayGet(pActions, action);
3,978,793✔
2145
    STransAction  *pAction = *ppAction;
3,978,793✔
2146

2147
    if (notSend && !pAction->msgSent) {
3,978,793✔
2148
      mInfo(
533,846✔
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;
533,846✔
2154
      break;
533,846✔
2155
    }
2156

2157
    mInfo(
3,444,947✔
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,444,947✔
2164
    if (code == 0) {
3,444,947✔
2165
      if (pAction->msgSent) {
2,029,636✔
2166
        if (pAction->msgReceived) {
1,819,759✔
2167
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
519,187✔
2168
            code = pAction->errCode;
38,012✔
2169
            mndTransResetAction(pMnode, pTrans, pAction);
38,012✔
2170
          } else {
2171
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
481,175✔
2172
                  pAction->id, action, numOfActions, groupId);
2173
          }
2174
        } else {
2175
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,300,572✔
2176
        }
2177
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,819,759✔
2178
        if (msgSent != NULL) {
1,819,759✔
2179
          *msgSent = pAction->msgSent;
1,819,759✔
2180
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,819,759✔
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) {
209,877✔
2185
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
209,877✔
2186
          code = pAction->errCode;
×
2187
        } else {
2188
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
209,877✔
2189
        }
2190
      } else {
2191
      }
2192
    }
2193

2194
    if (code == 0) {
3,444,947✔
2195
      pTrans->failedTimes = 0;
691,052✔
2196
    }
2197
    mndSetTransLastAction(pTrans, pAction);
3,444,947✔
2198

2199
    char str[200] = {0};
3,444,947✔
2200
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
3,444,947✔
2201
      pTrans->lastErrorNo = code;
1,561,451✔
2202
      pTrans->code = code;
1,561,451✔
2203
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
1,561,451✔
2204
        mInfo(
1,415,311✔
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) {
146,140✔
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,561,451✔
2217
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2218
      break;
1,561,451✔
2219
    }
2220

2221
    if (code == 0) {
1,883,496✔
2222
      pTrans->code = 0;
544,912✔
2223
      pTrans->actionPos++;
544,912✔
2224
      (*actionPos)++;
544,912✔
2225
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
544,912✔
2226
            pAction->id);
2227
      (void)taosThreadMutexUnlock(&pTrans->mutex);
544,912✔
2228
      code = mndTransSync(pMnode, pTrans);
544,912✔
2229
      (void)taosThreadMutexLock(&pTrans->mutex);
544,912✔
2230
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
544,912✔
2231
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
12,795,966✔
2232
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
12,251,054✔
2233
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
12,251,054✔
2234
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
12,251,054✔
2235
          pTmpAction->msgSent = *msgSent;
646,971✔
2236
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
646,971✔
2237
        }
2238
      }
2239
      if (code != 0) {
544,912✔
UNCOV
2240
        pTrans->actionPos--;
×
UNCOV
2241
        (*actionPos)--;
×
UNCOV
2242
        pTrans->code = terrno;
×
UNCOV
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());
UNCOV
2245
        break;
×
2246
      }
2247
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
1,338,584✔
2248
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,300,572✔
2249
            mndTransStr(pAction->stage), pAction->id);
2250
      break;
1,300,572✔
2251
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
38,012✔
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,
38,012✔
2254
            code, tstrerror(code));
2255
      pTrans->lastErrorNo = code;
38,012✔
2256
      taosMsleep(300);
38,012✔
2257
      action--;
38,012✔
2258
      continue;
38,012✔
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,643,497✔
2270
}
2271

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

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

2288
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,368,971✔
2289
  if(pHash == NULL){
1,368,971✔
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) {
31,888,096✔
2294
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
30,519,125✔
2295
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
30,519,125✔
2296
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
30,519,125✔
2297
  }
2298
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
1,368,971✔
2299

2300
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,368,971✔
2301
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,368,971✔
2302
  int32_t currentGroup = 1;
1,368,971✔
2303
  while (pIter) {
5,750,498✔
2304
    SArray **redoActions = pIter;
4,381,527✔
2305
    size_t   keyLen = 0;
4,381,527✔
2306
    int32_t *key = taosHashGetKey(pIter, &keyLen);
4,381,527✔
2307
    int32_t  actionCount = taosArrayGetSize(*redoActions);
4,381,527✔
2308
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
4,381,527✔
2309
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2310
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
4,381,527✔
2311
                                             notSend, pHash);
2312
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
4,381,527✔
2313
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,415,311✔
2314
            tstrerror(code));
2315
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,966,216✔
2316
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,834,418✔
2317
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
1,131,798✔
2318
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
738,030✔
2319
    } else if (code != 0) {
393,768✔
UNCOV
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++;
393,768✔
2324
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
393,768✔
2325
    }
2326
    currentGroup++;
4,381,527✔
2327
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,381,527✔
2328
  }
2329

2330
  taosHashCleanup(pHash);
1,368,971✔
2331

2332
  if (successCount == groupCount) {
1,368,971✔
2333
    total_code = 0;
25,818✔
2334
  } else {
2335
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,343,153✔
2336
  }
2337

2338
  return total_code;
1,368,971✔
2339
}
2340

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

2345
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,594,519✔
2346
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,594,519✔
2347
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,594,519✔
2348
      code = 0;
194,252✔
2349
    } else {
2350
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,400,267✔
2351
      if (pAction != NULL && pAction->groupId == -1) {
1,400,267✔
2352
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
31,296✔
2353
      } else {
2354
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,368,971✔
2355
        if (code == 0) {
1,368,971✔
2356
          if (pTrans->actionPos < numOfActions) {
25,818✔
2357
            code = TSDB_CODE_ACTION_IN_PROGRESS;
18,840✔
2358
          }
2359
        }
2360
      }
2361
    }
2362
  }
2363

2364
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,594,519✔
2365

2366
  return code;
1,594,519✔
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) {
21,042,662✔
2380
  bool    continueExec = true;
21,042,662✔
2381
  int32_t code = 0;
21,042,662✔
2382
  terrno = 0;
21,042,662✔
2383

2384
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
21,042,662✔
2385
  if (numOfActions == 0) goto _OVER;
21,042,662✔
2386

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

2389
  for (int32_t action = 0; action < numOfActions; ++action) {
20,176,540✔
2390
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
11,622,170✔
2391
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
11,622,170✔
2392
    if (code != 0) {
11,622,170✔
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,554,370✔
2401
  pTrans->stage = TRN_STAGE_REDO_ACTION;
21,042,662✔
2402
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
21,042,662✔
2403
  return continueExec;
21,042,662✔
2404
}
2405

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

2411
  if (pTrans->exec == TRN_EXEC_SERIAL) {
67,954,013✔
2412
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
7,657,158✔
2413
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
60,296,855✔
2414
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
58,702,336✔
2415
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,594,519✔
2416
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,594,519✔
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,956,369✔
2422
      mndTransIsInSyncContext(topHalf)) {
2,356✔
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,954,013✔
2433
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
23,908,243✔
2434
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2435
      return false;
23,908,243✔
2436
    }
2437
  }
2438
  terrno = code;
44,045,770✔
2439

2440
  if (code == 0) {
44,045,770✔
2441
    pTrans->code = 0;
19,430,022✔
2442
    pTrans->stage = TRN_STAGE_COMMIT;
19,430,022✔
2443
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
19,430,022✔
2444
    continueExec = true;
19,430,022✔
2445
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
24,615,748✔
2446
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
24,613,392✔
2447
    continueExec = false;
24,613,392✔
2448
  } else {
2449
    pTrans->failedTimes++;
2,356✔
2450
    pTrans->code = terrno;
2,356✔
2451
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
2,356✔
2452
      if (pTrans->lastAction != 0) {
1,602✔
2453
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
1,602✔
2454
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
1,602✔
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,602✔
2466
      pTrans->actionPos = 0;
1,602✔
2467
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
1,602✔
2468
             pTrans->actionPos);
2469
      continueExec = true;
1,602✔
2470
    } else {
2471
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
754✔
2472
      continueExec = false;
754✔
2473
    }
2474
  }
2475

2476
  return continueExec;
44,045,770✔
2477
}
2478

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

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

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

2498
  return continueExec;
19,430,022✔
2499
}
2500

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

2505
  if (code == 0) {
40,465,418✔
2506
    pTrans->code = 0;
40,465,354✔
2507
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
40,465,354✔
2508
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
40,465,354✔
2509
    continueExec = true;
40,465,354✔
2510
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
64✔
2511
    pTrans->code = 0;
64✔
2512
    pTrans->stage = TRN_STAGE_COMMIT;
64✔
2513
    mInfo("trans:%d, back to commit stage", pTrans->id);
64✔
2514
    continueExec = true;
64✔
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,465,418✔
2523
}
2524

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

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

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

2538
  if (code == 0) {
9,612✔
2539
    pTrans->stage = TRN_STAGE_PRE_FINISH;
1,602✔
2540
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
1,602✔
2541
    continueExec = true;
1,602✔
2542
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
8,010✔
2543
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
8,010✔
2544
    continueExec = false;
8,010✔
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,612✔
2552
}
2553

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

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

2561
  if (code == 0) {
1,602✔
2562
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1,602✔
2563
    continueExec = true;
1,602✔
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,602✔
2571
}
2572

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

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

2580
  if (code == 0) {
1,725✔
2581
    pTrans->stage = TRN_STAGE_FINISH;
1,725✔
2582
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
1,725✔
2583
    continueExec = true;
1,725✔
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,725✔
2591
}
2592

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

2597
  SSdbRaw *pRaw = mndTransEncode(pTrans);
21,039,461✔
2598
  if (pRaw == NULL) {
21,039,461✔
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));
21,039,461✔
2603

2604
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
21,039,461✔
2605
  if (code != 0) {
21,039,461✔
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,
21,039,461✔
2610
        pTrans->failedTimes, pTrans->createdTime);
2611
  return continueExec;
21,039,461✔
2612
}
2613

2614
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
89,003,273✔
2615
  bool continueExec = true;
89,003,273✔
2616

2617
  while (continueExec) {
257,336,135✔
2618
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
168,332,862✔
2619
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2620
    pTrans->lastExecTime = taosGetTimestampMs();
168,332,862✔
2621
    switch (pTrans->stage) {
168,332,862✔
2622
      case TRN_STAGE_PREPARE:
×
2623
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2624
        break;
×
2625
      case TRN_STAGE_REDO_ACTION:
67,954,013✔
2626
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
67,954,013✔
2627
        break;
67,954,013✔
2628
      case TRN_STAGE_COMMIT:
19,430,086✔
2629
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
19,430,086✔
2630
        break;
19,430,086✔
2631
      case TRN_STAGE_COMMIT_ACTION:
40,465,418✔
2632
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
40,465,418✔
2633
        break;
40,465,418✔
2634
      case TRN_STAGE_ROLLBACK:
1,602✔
2635
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,602✔
2636
        break;
1,602✔
2637
      case TRN_STAGE_UNDO_ACTION:
11,214✔
2638
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
11,214✔
2639
        break;
11,214✔
2640
      case TRN_STAGE_PRE_FINISH:
1,725✔
2641
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,725✔
2642
        break;
1,725✔
2643
      case TRN_STAGE_FINISH:
40,468,804✔
2644
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
40,468,804✔
2645
        break;
40,468,804✔
2646
      default:
×
2647
        continueExec = false;
×
2648
        break;
×
2649
    }
2650
  }
2651

2652
  mndTransSendRpcRsp(pMnode, pTrans);
89,003,273✔
2653
}
89,003,273✔
2654

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

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

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

2673
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
123✔
2674
  SArray *pArray = NULL;
123✔
2675
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
123✔
2676
    pArray = pTrans->redoActions;
123✔
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){
123✔
2684
    if(pTrans->ableToBeKilled == false){
123✔
2685
      return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2686
    }
2687
  }
2688
  
2689
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
123✔
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){
123✔
2700
    pTrans->stage = TRN_STAGE_PRE_FINISH;
123✔
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);
123✔
2707
  mndTransExecute(pMnode, pTrans, true);
123✔
2708
  return 0;
123✔
2709
}
2710

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

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

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

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

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

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

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

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

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

2752
  void *pIter = NULL;
15,857,795✔
2753
  while (1) {
1,901,204✔
2754
    STrans *pTrans = NULL;
17,758,999✔
2755
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
17,758,999✔
2756
    if (pIter == NULL) break;
17,758,999✔
2757
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
3,802,408✔
2758
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2759
    }
2760
    sdbRelease(pSdb, pTrans);
1,901,204✔
2761
  }
2762

2763
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
15,857,795✔
2764

2765
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
17,758,999✔
2766
    int32_t *pTransId = taosArrayGet(pArray, i);
1,901,204✔
2767
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
1,901,204✔
2768
    if (pTrans != NULL) {
1,901,204✔
2769
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
1,901,204✔
2770
      mndTransExecute(pMnode, pTrans, false);
1,901,204✔
2771
    }
2772
    mndReleaseTrans(pMnode, pTrans);
1,901,204✔
2773
  }
2774
  taosArrayDestroy(pArray);
15,857,795✔
2775
}
2776

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

2788
  struct tm tm;
17,685,188✔
2789
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
17,685,188✔
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,685,188✔
2794

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

2803
  return buf;
17,685,188✔
2804
}
2805

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

2811
  if (pTrans->stage == TRN_STAGE_PREPARE) {
382,156✔
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) {
382,156✔
2823
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
11,983,254✔
2824
      len = 0;
11,601,848✔
2825
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
11,601,848✔
2826
      if (pAction->actionType == TRANS_ACTION_MSG) {
11,601,848✔
2827
        char bufStart[40] = {0};
8,825,129✔
2828
        (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
8,825,129✔
2829

2830
        char endStart[40] = {0};
8,825,129✔
2831
        (void)formatTimestamp(endStart, sizeof(endStart), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
8,825,129✔
2832
        len += snprintf(detail + len, sizeof(detail) - len,
17,650,258✔
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,650,258✔
2836
                        pAction->msgReceived, bufStart, endStart);
8,825,129✔
2837

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

2847
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
8,825,129✔
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,776,719✔
2851
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
2,776,719✔
2852
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
2,776,719✔
2853
      }
2854
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
11,601,848✔
2855
    }
2856
  }
2857

2858
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
382,156✔
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
}
382,156✔
2884

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

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

2898
    cols = 0;
382,156✔
2899

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

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

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

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

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

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

2927
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
382,156✔
2928
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
382,156✔
2929
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
382,156✔
2930
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
382,156✔
2931
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
382,156✔
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++);
382,156✔
2942
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
382,156✔
2943
                        _OVER);
2944

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

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

2965
    mndTransLogAction(pTrans);
382,156✔
2966

2967
    numOfRows++;
382,156✔
2968
    sdbRelease(pSdb, pTrans);
382,156✔
2969
  }
2970

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

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

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

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

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

3010
  cols = 0;
68,585✔
3011

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

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

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

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

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

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

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

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

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

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

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

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

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

3107
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
887✔
3108
  if (stage == TRN_STAGE_PREPARE) {
887✔
3109
    return pTrans->prepareActions;
×
3110
  }
3111
  if (stage == TRN_STAGE_REDO_ACTION) {
887✔
3112
    return pTrans->redoActions;
887✔
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,
887✔
3131
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3132
  int32_t actionNum = taosArrayGetSize(pActions);
887✔
3133
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
887✔
3134

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

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

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

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

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

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

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

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

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

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

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

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

3201
  if (code != 0) {
1,392✔
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,392✔
3205
  }
3206
  if (numOfRows == 0) {
1,392✔
3207
    taosMemoryFree(pShow->pIter);
505✔
3208
    pShow->pIter = NULL;
505✔
3209
  }
3210
  return numOfRows;
1,392✔
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