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

taosdata / TDengine / #4917

07 Jan 2026 03:52PM UTC coverage: 65.42% (+0.02%) from 65.402%
#4917

push

travis-ci

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

31 of 34 new or added lines in 2 files covered. (91.18%)

819 existing lines in 129 files now uncovered.

202679 of 309814 relevant lines covered (65.42%)

116724351.99 hits per line

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

84.06
/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; }
112,352,318✔
64

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

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

81
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
153,155,675✔
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) {
385,909✔
94
  SSdbTable table = {
385,909✔
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);
385,909✔
105
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
385,909✔
106

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

113
void mndCleanupTrans(SMnode *pMnode) {}
385,851✔
114

115
static int32_t mndTransGetActionsSize(SArray *pArray) {
198,361,872✔
116
  int32_t actionNum = taosArrayGetSize(pArray);
198,361,872✔
117
  int32_t rawDataLen = 0;
198,361,872✔
118

119
  for (int32_t i = 0; i < actionNum; ++i) {
541,196,811✔
120
    STransAction *pAction = taosArrayGet(pArray, i);
342,834,939✔
121
    if (pAction->actionType == TRANS_ACTION_RAW) {
342,834,939✔
122
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
225,147,066✔
123
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
117,687,873✔
124
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
117,687,873✔
125
    } else {
126
      // empty
127
    }
128
    rawDataLen += sizeof(int8_t);
342,834,939✔
129
  }
130

131
  return rawDataLen;
198,361,872✔
132
}
133

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

142
  for (int32_t i = 0; i < actionsNum; ++i) {
541,196,811✔
143
    STransAction *pAction = taosArrayGet(pActions, i);
342,834,939✔
144
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
342,834,939✔
145
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
342,834,939✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
342,834,939✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
342,834,939✔
148
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
342,834,939✔
149
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
342,834,939✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
342,834,939✔
151
    if (sver > TRANS_VER2_NUMBER) {
342,834,939✔
152
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
342,834,939✔
153
    }
154
    if (pAction->actionType == TRANS_ACTION_RAW) {
342,834,939✔
155
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
225,147,066✔
156
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
225,147,066✔
157
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
225,147,066✔
158
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
225,147,066✔
159
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
117,687,873✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
117,687,873✔
161
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
117,687,873✔
162
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
117,687,873✔
163
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
117,687,873✔
164
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
117,687,873✔
165
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
117,687,873✔
166
    } else {
167
      // nothing
168
    }
169
  }
170
  ret = 0;
198,361,872✔
171

172
_OVER:
198,361,872✔
173
  *offset = dataPos;
198,361,872✔
174
  return ret;
198,361,872✔
175
}
176

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

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

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

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

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

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

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

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

235
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
49,590,468✔
236
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
49,590,468✔
237
  void *pIter = NULL;
49,590,468✔
238
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
49,590,468✔
239
  while (pIter) {
49,619,250✔
240
    int32_t arbGroupId = *(int32_t *)pIter;
28,782✔
241
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
28,782✔
242
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
28,782✔
243
  }
244

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

250
  if (sver > TRANS_VER2_NUMBER) {
49,590,468✔
251
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
49,590,468✔
252
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
49,590,468✔
253
    void *pIter = NULL;
49,590,468✔
254
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
49,590,468✔
255
    while (pIter) {
51,810,371✔
256
      size_t   keysize = 0;
2,219,903✔
257
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,219,903✔
258
      int32_t  groupPos = *(int32_t *)pIter;
2,219,903✔
259
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,219,903✔
260
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,219,903✔
261
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,219,903✔
262
    }
263
  }
264

265
  if (sver >= TRANS_VER_USER_DATA) {
49,590,468✔
266
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
49,590,468✔
267
    if (pTrans->userDataLen > 0) {
49,590,468✔
268
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
240✔
269
    }
270
  }
271

272
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
49,590,468✔
273
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
49,590,468✔
274

275
  terrno = 0;
49,590,468✔
276

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

289
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
25,727,076✔
290
  if (pAction->groupId < 0) return 0;
25,727,076✔
291
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
25,261,319✔
292
  if (redoAction == NULL) {
25,261,319✔
293
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
4,403,225✔
294
    if (array != NULL) {
4,403,225✔
295
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
4,403,225✔
296
        mInfo("failed put action into redo group actions");
×
297
        return TSDB_CODE_INTERNAL_ERROR;
×
298
      }
299
    }
300
    redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
4,403,225✔
301
  }
302
  if (redoAction != NULL) {
25,261,319✔
303
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
50,522,638✔
304
  }
305
  return 0;
25,261,319✔
306
}
307

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

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

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

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

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

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

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

449
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
87,684,713✔
450

451
  if (sver > TRANS_VER_CURRENT) {
87,684,713✔
452
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
453
    goto _OVER;
×
454
  }
455

456
  pRow = sdbAllocRow(sizeof(STrans));
87,684,713✔
457
  if (pRow == NULL) goto _OVER;
87,684,713✔
458

459
  pTrans = sdbGetRowObj(pRow);
87,684,713✔
460
  if (pTrans == NULL) goto _OVER;
87,684,713✔
461

462
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
87,684,713✔
463

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

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

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

504
  if (pTrans->prepareActions == NULL) goto _OVER;
87,684,713✔
505
  if (pTrans->redoActions == NULL) goto _OVER;
87,684,713✔
506
  if (pTrans->undoActions == NULL) goto _OVER;
87,684,713✔
507
  if (pTrans->commitActions == NULL) goto _OVER;
87,684,713✔
508

509
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
87,684,713✔
510
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
87,684,713✔
511
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,485,955✔
512
                                      sver) < 0)
513
      goto _OVER;
×
514
  } else {
515
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
86,198,758✔
516
  }
517
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
87,684,713✔
518
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
87,684,713✔
519

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

531
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
87,684,713✔
532
  if (arbgroupIdNum > 0) {
87,684,713✔
533
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
40,960✔
534
    if (pTrans->arbGroupIds == NULL) goto _OVER;
40,960✔
535
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
88,930✔
536
      int32_t arbGroupId = 0;
47,970✔
537
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
47,970✔
538
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
47,970✔
539
    }
540
  }
541

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

551
  if (sver > TRANS_VER2_NUMBER) {
87,684,713✔
552
    int32_t groupNum = -1;
87,684,713✔
553
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
87,684,713✔
554
    for (int32_t i = 0; i < groupNum; ++i) {
92,087,938✔
555
      int32_t groupId = -1;
4,403,225✔
556
      int32_t groupPos = -1;
4,403,225✔
557
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
4,403,225✔
558
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
4,403,225✔
559
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
4,403,225✔
560
        goto _OVER;
×
561
    }
562
  }
563

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

573
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
87,684,713✔
574

575
  terrno = 0;
87,684,713✔
576

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

592
static const char *mndTransStr(ETrnStage stage) {
637,799,991✔
593
  switch (stage) {
637,799,991✔
594
    case TRN_STAGE_PREPARE:
50,382,183✔
595
      return "prepare";
50,382,183✔
596
    case TRN_STAGE_REDO_ACTION:
236,499,077✔
597
      return "redoAction";
236,499,077✔
598
    case TRN_STAGE_ROLLBACK:
7,090✔
599
      return "rollback";
7,090✔
600
    case TRN_STAGE_UNDO_ACTION:
9,613,960✔
601
      return "undoAction";
9,613,960✔
602
    case TRN_STAGE_COMMIT:
79,021,860✔
603
      return "commit";
79,021,860✔
604
    case TRN_STAGE_COMMIT_ACTION:
145,250,583✔
605
      return "commitAction";
145,250,583✔
606
    case TRN_STAGE_FINISH:
117,017,638✔
607
      return "finished";
117,017,638✔
608
    case TRN_STAGE_PRE_FINISH:
7,600✔
609
      return "pre-finish";
7,600✔
610
    default:
×
611
      return "invalid";
×
612
  }
613
}
614

615
static const char *mndTransTypeStr(ETrnAct actionType) {
55,682✔
616
  switch (actionType) {
55,682✔
617
    case TRANS_ACTION_MSG:
45,762✔
618
      return "msg";
45,762✔
619
    case TRANS_ACTION_RAW:
9,920✔
620
      return "sdb";
9,920✔
621
    default:
×
622
      return "invalid";
×
623
  }
624
}
625

626
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
176,239,527✔
627
  if (pAction != NULL) {
176,239,527✔
628
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
135,899,786✔
629
      pTrans->lastAction = pAction->id;
94,957,258✔
630
      pTrans->lastMsgType = pAction->msgType;
94,957,258✔
631
      pTrans->lastEpset = pAction->epSet;
94,957,258✔
632
      pTrans->lastErrorNo = pAction->errCode;
94,957,258✔
633
    }
634
  } else {
635
    pTrans->lastAction = 0;
40,339,741✔
636
    pTrans->lastMsgType = 0;
40,339,741✔
637
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
40,339,741✔
638
    pTrans->lastErrorNo = 0;
40,339,741✔
639
  }
640
}
176,239,527✔
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) {
495,012✔
651
  switch (ftype) {
495,012✔
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:
247,506✔
657
      return mndRebCntInc;
247,506✔
658
    case TRANS_STOP_FUNC_MQ_REB:
247,506✔
659
      return mndRebCntDec;
247,506✔
660
    default:
×
661
      return NULL;
×
662
  }
663
}
664

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

669
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
16,557,672✔
670

671
  if (pTrans->startFunc > 0) {
16,557,672✔
672
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
247,506✔
673
    if (fp) {
247,506✔
674
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
247,506✔
675
    }
676
    // pTrans->startFunc = 0;
677
  }
678

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

684
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
16,557,672✔
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) {
16,557,672✔
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;
16,557,672✔
695
}
696

697
void mndTransDropData(STrans *pTrans) {
103,276,096✔
698
  if (pTrans->prepareActions != NULL) {
103,276,096✔
699
    mndTransDropActions(pTrans->prepareActions);
103,276,096✔
700
    pTrans->prepareActions = NULL;
103,276,096✔
701
  }
702
  if (pTrans->redoActions != NULL) {
103,276,096✔
703
    mndTransDropActions(pTrans->redoActions);
103,276,096✔
704
    pTrans->redoActions = NULL;
103,276,096✔
705
  }
706
  if (pTrans->undoActions != NULL) {
103,276,096✔
707
    mndTransDropActions(pTrans->undoActions);
103,276,096✔
708
    pTrans->undoActions = NULL;
103,276,096✔
709
  }
710
  if (pTrans->commitActions != NULL) {
103,276,096✔
711
    mndTransDropActions(pTrans->commitActions);
103,276,096✔
712
    pTrans->commitActions = NULL;
103,276,096✔
713
  }
714
  if (pTrans->redoGroupActions != NULL) {
103,276,096✔
715
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
17,077,338✔
716
    while (pIter) {
21,700,809✔
717
      SArray **redoActions = pIter;
4,623,471✔
718
      taosArrayDestroy(*redoActions);
4,623,471✔
719
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,623,471✔
720
    }
721

722
    taosHashCleanup(pTrans->redoGroupActions);
17,077,338✔
723
    pTrans->redoGroupActions = NULL;
17,077,338✔
724
  }
725
  if (pTrans->groupActionPos != NULL) {
103,276,096✔
726
    taosHashCleanup(pTrans->groupActionPos);
17,077,338✔
727
    pTrans->groupActionPos = NULL;
17,077,338✔
728
  }
729
  if (pTrans->arbGroupIds != NULL) {
103,276,096✔
730
    taosHashCleanup(pTrans->arbGroupIds);
15,632,343✔
731
  }
732
  if (pTrans->pRpcArray != NULL) {
103,276,096✔
733
    taosArrayDestroy(pTrans->pRpcArray);
15,591,383✔
734
    pTrans->pRpcArray = NULL;
15,591,383✔
735
  }
736
  if (pTrans->rpcRsp != NULL) {
103,276,096✔
737
    taosMemoryFree(pTrans->rpcRsp);
6,488,076✔
738
    pTrans->rpcRsp = NULL;
6,488,076✔
739
    pTrans->rpcRspLen = 0;
6,488,076✔
740
  }
741
  if (pTrans->param != NULL) {
103,276,096✔
742
    taosMemoryFree(pTrans->param);
×
743
    pTrans->param = NULL;
×
744
    pTrans->paramLen = 0;
×
745
  }
746
  if (pTrans->userData != NULL) {
103,276,096✔
747
    taosMemoryFree(pTrans->userData);
400✔
748
    pTrans->userData = NULL;
400✔
749
    pTrans->userDataLen = 0;
400✔
750
  }
751
  (void)taosThreadMutexDestroy(&pTrans->mutex);
103,276,096✔
752
}
103,276,096✔
753

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

758
  if (pTrans->stopFunc > 0 && callFunc) {
52,119,444✔
759
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
247,506✔
760
    if (fp) {
247,506✔
761
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
247,506✔
762
    }
763
    // pTrans->stopFunc = 0;
764
  }
765

766
  mndTransDropData(pTrans);
52,119,444✔
767
  return 0;
52,119,444✔
768
}
769

770
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
76,085,308✔
771
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
236,861,921✔
772
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
160,776,613✔
773
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
160,776,613✔
774
    pOldAction->rawWritten = pNewAction->rawWritten;
160,776,613✔
775
    pOldAction->msgSent = pNewAction->msgSent;
160,776,613✔
776
    pOldAction->msgReceived = pNewAction->msgReceived;
160,776,613✔
777
    pOldAction->errCode = pNewAction->errCode;
160,776,613✔
778
  }
779
}
76,085,308✔
780

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

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

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

803
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
19,021,327✔
804
  while (pIter != NULL) {
20,886,055✔
805
    int32_t newActionPos = *(int32_t *)pIter;
1,864,728✔
806

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

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

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

819
  if (pOld->stage == TRN_STAGE_COMMIT) {
19,021,327✔
820
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
16,538,679✔
821
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
16,538,679✔
822
  }
823

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

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

834
  return 0;
19,021,327✔
835
}
836

837
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
71,632,354✔
838
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
71,632,354✔
839
  if (pTrans == NULL) {
71,632,354✔
840
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
1,218✔
841
  }
842
  return pTrans;
71,632,354✔
843
}
844

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

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

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

886
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
15,591,383✔
887
      pTrans->pRpcArray == NULL) {
15,591,383✔
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) {
15,591,383✔
895
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
21,471,836✔
896
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
897
      return NULL;
×
898
    }
899
    pTrans->originRpcType = pReq->msgType;
10,735,918✔
900
  }
901

902
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
15,591,383✔
903

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

908
static void mndTransDropActions(SArray *pArray) {
413,104,384✔
909
  int32_t size = taosArrayGetSize(pArray);
413,104,384✔
910
  for (int32_t i = 0; i < size; ++i) {
1,120,815,821✔
911
    STransAction *pAction = taosArrayGet(pArray, i);
707,711,437✔
912
    if (pAction->actionType == TRANS_ACTION_RAW) {
707,711,437✔
913
      taosMemoryFreeClear(pAction->pRaw);
462,704,036✔
914
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
245,007,401✔
915
      taosMemoryFreeClear(pAction->pCont);
245,007,401✔
916
    } else {
917
      // nothing
918
    }
919
  }
920

921
  taosArrayDestroy(pArray);
413,104,384✔
922
}
413,104,384✔
923

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

932
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
91,508,247✔
933
  pAction->id = taosArrayGetSize(pArray);
91,508,247✔
934

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

940
  return 0;
91,508,247✔
941
}
942

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

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

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

975
  return 0;
499,799✔
976
}
977

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

985
int32_t mndTransAppendGroupRedolog(STrans *pTrans, SSdbRaw *pRaw, int32_t groupId) {
269,501✔
986
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION,
269,501✔
987
                         .actionType = TRANS_ACTION_RAW,
988
                         .pRaw = pRaw,
989
                         .mTraceId = pTrans->mTraceId,
269,501✔
990
                         .groupId = groupId};
991
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
269,501✔
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));
269,501✔
996
  return mndTransAppendAction(pTrans->redoActions, &action);
269,501✔
997
}
998

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1176
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
5,189,289✔
1177
  if (conflict[0] == 0) return false;
5,189,289✔
1178
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
5,102,999✔
1179
  return false;
4,935,005✔
1180
}
1181

1182
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
5,329,002✔
1183
  if (conflict) {
5,329,002✔
1184
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
220,232✔
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;
220,232✔
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,108,770✔
1191
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1192
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1193
  }
1194
}
5,329,002✔
1195

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

1201
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
41,186,328✔
1202

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

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

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

1212
    if (pNew->conflict == TRN_CONFLICT_DB) {
5,238,055✔
1213
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
92,290✔
1214
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
92,290✔
1215
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
86,290✔
1216
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
86,290✔
1217
      }
1218
    }
1219

1220
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
5,238,055✔
1221
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
5,115,091✔
1222
      if (pTrans->conflict == TRN_CONFLICT_DB) {
5,115,091✔
1223
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
53,423✔
1224
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
53,423✔
1225
      }
1226
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
5,115,091✔
1227
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
5,049,576✔
1228
      }
1229
    }
1230

1231
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
5,238,055✔
1232
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
26,414✔
1233
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
26,414✔
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,238,055✔
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,238,055✔
1258
  }
1259

1260
  return conflict;
32,148,079✔
1261
}
1262

1263
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
41,186,328✔
1264
  int32_t code = 0;
41,186,328✔
1265
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
41,186,328✔
1266
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
27,668,791✔
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)) {
41,186,328✔
1274
    code = TSDB_CODE_MND_TRANS_CONFLICT;
255,248✔
1275
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
255,248✔
1276
    TAOS_RETURN(code);
255,248✔
1277
  }
1278

1279
  TAOS_RETURN(code);
40,931,080✔
1280
}
1281

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

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

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

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

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

1317
  TAOS_RETURN(code);
144,686✔
1318
}
1319

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

1327
  while ((pIter = sdbFetch(pSdb, SDB_RETENTION, pIter, (void **)&pRetention)) != NULL) {
144,686✔
1328
    conflict = false;
156✔
1329

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

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

1356
  TAOS_RETURN(code);
144,530✔
1357
}
1358

1359
static bool mndTransActionsOfSameType(SArray *pActions) {
34,335,324✔
1360
  int32_t size = taosArrayGetSize(pActions);
34,335,324✔
1361
  ETrnAct lastActType = TRANS_ACTION_NULL;
34,335,324✔
1362
  bool    same = true;
34,335,324✔
1363
  for (int32_t i = 0; i < size; ++i) {
108,412,238✔
1364
    STransAction *pAction = taosArrayGet(pActions, i);
74,076,914✔
1365
    if (i > 0) {
74,076,914✔
1366
      if (lastActType != pAction->actionType) {
48,725,378✔
1367
        same = false;
×
1368
        break;
×
1369
      }
1370
    }
1371
    lastActType = pAction->actionType;
74,076,914✔
1372
  }
1373
  return same;
34,335,324✔
1374
}
1375

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

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

1394
  TAOS_RETURN(code);
15,317,350✔
1395
}
1396

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

1410
  TAOS_RETURN(code);
15,316,649✔
1411
}
1412

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

1419
  mInfo("trans:%d, action list:", pTrans->id);
15,317,950✔
1420
  int32_t index = 0;
15,317,950✔
1421
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
24,189,006✔
1422
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
8,871,056✔
1423
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
8,871,056✔
1424
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1425
  }
1426

1427
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
33,108,327✔
1428
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
17,790,377✔
1429
    if (pAction->actionType == TRANS_ACTION_MSG) {
17,790,377✔
1430
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
17,183,639✔
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),
606,738✔
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) {
70,518,644✔
1440
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
55,200,694✔
1441
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
55,200,694✔
1442
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1443
  }
1444

1445
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
24,870,936✔
1446
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
9,552,986✔
1447
    if (pAction->actionType == TRANS_ACTION_MSG) {
9,552,986✔
1448
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
6,109,124✔
1449
            TMSG_INFO(pAction->msgType));
1450
    } else {
1451
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
3,443,862✔
1452
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1453
    }
1454
  }
1455

1456
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
15,317,950✔
1457

1458
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
15,317,350✔
1459

1460
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
15,317,350✔
1461

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

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

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

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

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

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

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

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

1532
  if (pTrans->stage == TRN_STAGE_FINISH) {
71,631,136✔
1533
    sendRsp = true;
31,855,494✔
1534
  }
1535

1536
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
71,631,136✔
1537
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
16,734,301✔
1538
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
8,508✔
1539
      sendRsp = true;
8,508✔
1540
    }
1541
  } else {
1542
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
54,896,835✔
1543
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
31,544,367✔
1544
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1545
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1546
      } else {
1547
        if (pTrans->failedTimes > 6) sendRsp = true;
31,544,367✔
1548
      }
1549
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
31,544,367✔
1550
    }
1551
  }
1552

1553
  if (!sendRsp) {
71,631,136✔
1554
    return;
39,767,134✔
1555
  } else {
1556
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
31,864,002✔
1557
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1558
  }
1559

1560
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
31,864,002✔
1561
  taosWLockLatch(&pTrans->lockRpcArray);
31,864,002✔
1562
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
31,864,002✔
1563
  if (size <= 0) {
31,864,002✔
1564
    taosWUnLockLatch(&pTrans->lockRpcArray);
21,379,885✔
1565
    return;
21,379,885✔
1566
  }
1567

1568
  for (int32_t i = 0; i < size; ++i) {
20,968,234✔
1569
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
10,484,117✔
1570
    if (pInfo->handle != NULL) {
10,484,117✔
1571
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
10,114,429✔
1572
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1573
      }
1574
      if (code == TSDB_CODE_SYN_TIMEOUT) {
10,114,429✔
1575
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1576
      }
1577

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

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

1586
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
10,114,429✔
1587
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,048,883✔
1588
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,048,883✔
1589
        if (pDb != NULL) {
1,048,883✔
1590
          for (int32_t j = 0; j < 12; j++) {
1,383,794✔
1591
            bool ready = mndIsDbReady(pMnode, pDb);
1,383,552✔
1592
            if (!ready) {
1,383,552✔
1593
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
334,911✔
1594
              taosMsleep(1000);
334,911✔
1595
            } else {
1596
              break;
1,048,641✔
1597
            }
1598
          }
1599
        }
1600
        mndReleaseDb(pMnode, pDb);
1,048,883✔
1601
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
9,065,546✔
1602
        void   *pCont = NULL;
1,569,987✔
1603
        int32_t contLen = 0;
1,569,987✔
1604
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
1,569,987✔
1605
          mndTransSetRpcRsp(pTrans, pCont, contLen);
1,568,569✔
1606
        }
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
7,495,559✔
1608
        void   *pCont = NULL;
2,362✔
1609
        int32_t contLen = 0;
2,362✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,362✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,362✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
7,493,197✔
1614
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
8,226✔
1615
        if (code != 0) {
8,226✔
1616
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1617
        }
1618
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
7,484,971✔
1619
        void   *pCont = NULL;
40✔
1620
        int32_t contLen = 0;
40✔
1621
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
40✔
1622
          mndTransSetRpcRsp(pTrans, pCont, contLen);
40✔
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
7,484,931✔
1625
        void   *pCont = NULL;
40✔
1626
        int32_t contLen = 0;
40✔
1627
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
40✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
40✔
1629
        }
1630
      }
1631

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

1641
      tmsgSendRsp(&rspMsg);
10,114,429✔
1642

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

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

1671
  SArray *pArray = NULL;
19,218,721✔
1672
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
19,218,721✔
1673
    pArray = pTrans->redoActions;
19,213,049✔
1674
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
5,672✔
1675
    pArray = pTrans->undoActions;
5,672✔
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) {
19,218,721✔
1682
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1683
    goto _OVER;
×
1684
  }
1685

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

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

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

1702
      mInfo("trans:%d, %s:%d response is received, msgType:%s, QID:0x%" PRIx64 ":0x%" PRIx64 ", received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
19,218,721✔
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);
19,218,721✔
1717
  mndTransExecute(pMnode, pTrans, true);
19,218,721✔
1718

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

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

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

1742
  for (int32_t action = 0; action < numOfActions; ++action) {
7,090✔
1743
    STransAction *pAction = taosArrayGet(pArray, action);
5,672✔
1744
    if (pAction->msgSent && pAction->msgReceived &&
5,672✔
1745
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
5,672✔
1746
      continue;
4,254✔
1747
    if (pAction->msgSent && !pAction->msgReceived) {
1,418✔
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,418✔
1753

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

1760
// execute in sync context
1761
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
124,325,083✔
1762
  if (pAction->rawWritten) return 0;
124,325,083✔
1763
  if (topHalf) {
68,544,401✔
1764
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
167✔
1765
  }
1766

1767
  if (pAction->pRaw->type >= SDB_MAX) {
68,544,234✔
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
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
68,544,234✔
1776
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
68,544,234✔
1777
    pAction->rawWritten = true;
68,544,234✔
1778
    pAction->errCode = 0;
68,544,234✔
1779
    code = 0;
68,544,234✔
1780
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
68,544,234✔
1781
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1782

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

1791
  TAOS_RETURN(code);
68,544,234✔
1792
}
1793

1794
// execute in trans context
1795
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
81,518,376✔
1796
                                     bool notSend) {
1797
  if (pAction->msgSent) return 0;
81,518,376✔
1798
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
31,142,508✔
1799
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
11,912,654✔
1800
  }
1801

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

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

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

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

1831
  char    detail[1024] = {0};
19,224,182✔
1832
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
19,224,182✔
1833
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
19,224,182✔
1834
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
41,028,394✔
1835
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
21,804,212✔
1836
                     pAction->epSet.eps[i].port);
21,804,212✔
1837
  }
1838

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

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

1857
    mndSetTransLastAction(pTrans, pAction);
1,342✔
1858
  }
1859

1860
  TAOS_RETURN(code);
19,224,182✔
1861
}
1862

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

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

1873
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
205,843,459✔
1874
                                        bool notSend) {
1875
  if (pAction->actionType == TRANS_ACTION_RAW) {
205,843,459✔
1876
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
124,325,083✔
1877
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
81,518,376✔
1878
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
81,518,376✔
1879
  } else {
1880
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1881
  }
1882
}
1883

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

1888
  for (int32_t action = 0; action < numOfActions; ++action) {
238,844,893✔
1889
    STransAction *pAction = taosArrayGet(pArray, action);
182,892,796✔
1890
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
182,892,796✔
1891
    if (code != 0) {
182,892,796✔
1892
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
9,207,387✔
1893
            numOfActions);
1894
      break;
9,207,387✔
1895
    }
1896
  }
1897

1898
  return code;
65,159,484✔
1899
}
1900

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

1906
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
65,159,484✔
1907
    return code;
9,207,387✔
1908
  }
1909

1910
  int32_t       numOfExecuted = 0;
55,952,097✔
1911
  int32_t       errCode = 0;
55,952,097✔
1912
  STransAction *pErrAction = NULL;
55,952,097✔
1913
  for (int32_t action = 0; action < numOfActions; ++action) {
229,637,506✔
1914
    STransAction *pAction = taosArrayGet(pArray, action);
173,685,409✔
1915
    if (pAction->msgReceived || pAction->rawWritten) {
173,685,409✔
1916
      numOfExecuted++;
143,281,865✔
1917
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
143,281,865✔
1918
        errCode = pAction->errCode;
1,418✔
1919
        pErrAction = pAction;
1,418✔
1920
      }
1921
    } else {
1922
      pErrAction = pAction;
30,403,544✔
1923
    }
1924
  }
1925

1926
  mndSetTransLastAction(pTrans, pErrAction);
55,952,097✔
1927

1928
  if (numOfExecuted == numOfActions) {
55,952,097✔
1929
    if (errCode == 0) {
40,341,159✔
1930
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
40,339,741✔
1931
      return 0;
40,339,741✔
1932
    } else {
1933
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,418✔
1934
      mndTransResetActions(pMnode, pTrans, pArray);
1,418✔
1935
      terrno = errCode;
1,418✔
1936
      return errCode;
1,418✔
1937
    }
1938
  } else {
1939
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
15,610,938✔
1940

1941
    for (int32_t action = 0; action < numOfActions; ++action) {
60,471,257✔
1942
      STransAction *pAction = taosArrayGet(pArray, action);
44,860,319✔
1943
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
44,860,319✔
1944
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1945
             pAction->acceptableCode, pAction->retryCode);
1946
      if (pAction->msgSent) {
44,860,319✔
1947
        bool reset = false;
44,854,647✔
1948
        if (pAction->msgReceived) {
44,854,647✔
1949
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
14,456,775✔
1950
        } else {
1951
          int64_t timestamp = taosGetTimestampMs();
30,397,872✔
1952
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
30,397,872✔
1953
        }
1954
        if (reset) {
44,854,647✔
UNCOV
1955
          mndTransResetAction(pMnode, pTrans, pAction);
×
UNCOV
1956
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
×
1957
                pAction->id, pAction->errCode, pAction->startTime);
1958
        }
1959
      }
1960
    }
1961
    return TSDB_CODE_ACTION_IN_PROGRESS;
15,610,938✔
1962
  }
1963
}
1964

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

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

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

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

1997
  if (pTrans->actionPos >= numOfActions) {
6,916,606✔
1998
    return code;
242,100✔
1999
  }
2000

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

2004
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
10,601,986✔
2005
    STransAction *pAction = taosArrayGet(pActions, action);
10,389,967✔
2006

2007
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
10,389,967✔
2008
      code = TSDB_CODE_ACTION_IN_PROGRESS;
6,989✔
2009
      break;
7,073✔
2010
    }
2011

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

2015
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
10,382,978✔
2016
    if (code == 0) {
10,382,978✔
2017
      if (pAction->msgSent) {
8,894,161✔
2018
        bool reset = false;
7,915,880✔
2019
        if (pAction->msgReceived) {
7,915,880✔
2020
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,454,249✔
2021
            code = pAction->errCode;
2,042,194✔
2022
            reset = true;
2,042,194✔
2023
          } else {
2024
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,412,055✔
2025
          }
2026
        } else {
2027
          int64_t timestamp = taosGetTimestampMs();
4,461,631✔
2028
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
4,461,631✔
2029
          code = TSDB_CODE_ACTION_IN_PROGRESS;
4,461,631✔
2030
        }
2031
        if (reset) {
7,915,880✔
2032
          mndTransResetAction(pMnode, pTrans, pAction);
2,042,194✔
2033
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,042,194✔
2034
                pAction->id, pAction->errCode, pAction->startTime);
2035
        }
2036
      } else if (pAction->rawWritten) {
978,281✔
2037
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
978,281✔
2038
          code = pAction->errCode;
×
2039
        } else {
2040
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
978,281✔
2041
        }
2042
      } else {
2043
      }
2044
    }
2045

2046
    if (code == 0) {
10,382,978✔
2047
      pTrans->failedTimes = 0;
2,390,336✔
2048
    }
2049
    mndSetTransLastAction(pTrans, pAction);
10,382,978✔
2050
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
10,382,978✔
2051
      mInfo(
5,949,106✔
2052
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2053
          "msgReceived:%d",
2054
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2055
          pAction->msgSent, pAction->msgReceived);
2056
    } else if (code != 0) {
4,433,872✔
2057
      mError(
2,043,536✔
2058
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2059
          "msgReceived:%d",
2060
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2061
          pAction->msgSent, pAction->msgReceived);
2062
    }
2063

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

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

2109
  return code;
6,674,506✔
2110
}
2111

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

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

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

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

2136
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
3,578,144✔
2137
    STransAction **ppAction = taosArrayGet(pActions, action);
3,360,888✔
2138
    STransAction  *pAction = *ppAction;
3,360,888✔
2139

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

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

2156
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
2,917,315✔
2157
    if (code == 0) {
2,917,315✔
2158
      if (pAction->msgSent) {
1,699,356✔
2159
        if (pAction->msgReceived) {
1,516,736✔
2160
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
446,937✔
2161
            code = pAction->errCode;
23,646✔
2162
            mndTransResetAction(pMnode, pTrans, pAction);
23,646✔
2163
          } else {
2164
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
423,291✔
2165
                  pAction->id, action, numOfActions, groupId);
2166
          }
2167
        } else {
2168
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,069,799✔
2169
        }
2170
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,516,736✔
2171
        if (msgSent != NULL) {
1,516,736✔
2172
          *msgSent = pAction->msgSent;
1,516,736✔
2173
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,516,736✔
2174
        } else {
2175
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2176
        }
2177
      } else if (pAction->rawWritten) {
182,620✔
2178
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
182,620✔
2179
          code = pAction->errCode;
×
2180
        } else {
2181
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
182,620✔
2182
        }
2183
      } else {
2184
      }
2185
    }
2186

2187
    if (code == 0) {
2,917,315✔
2188
      pTrans->failedTimes = 0;
605,911✔
2189
    }
2190
    mndSetTransLastAction(pTrans, pAction);
2,917,315✔
2191

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

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

2262
  return code;
3,075,359✔
2263
}
2264

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

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

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

2293
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,161,383✔
2294
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,161,383✔
2295
  int32_t currentGroup = 1;
1,161,383✔
2296
  while (pIter) {
4,814,902✔
2297
    SArray **redoActions = pIter;
3,653,519✔
2298
    size_t   keyLen = 0;
3,653,519✔
2299
    int32_t *key = taosHashGetKey(pIter, &keyLen);
3,653,519✔
2300
    int32_t  actionCount = taosArrayGetSize(*redoActions);
3,653,519✔
2301
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
3,653,519✔
2302
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2303
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
3,653,519✔
2304
                                             notSend, pHash);
2305
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
3,653,519✔
2306
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,217,959✔
2307
            tstrerror(code));
2308
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,435,560✔
2309
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,513,372✔
2310
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
922,188✔
2311
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
578,160✔
2312
    } else if (code != 0) {
344,028✔
2313
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
×
2314
             tstrerror(code));
2315
    } else {
2316
      successCount++;
344,028✔
2317
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
344,028✔
2318
    }
2319
    currentGroup++;
3,653,519✔
2320
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
3,653,519✔
2321
  }
2322

2323
  taosHashCleanup(pHash);
1,161,383✔
2324

2325
  if (successCount == groupCount) {
1,161,383✔
2326
    total_code = 0;
21,762✔
2327
  } else {
2328
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,139,621✔
2329
  }
2330

2331
  return total_code;
1,161,383✔
2332
}
2333

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

2338
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,358,949✔
2339
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,358,949✔
2340
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,358,949✔
2341
      code = 0;
172,804✔
2342
    } else {
2343
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,186,145✔
2344
      if (pAction != NULL && pAction->groupId == -1) {
1,186,145✔
2345
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
24,762✔
2346
      } else {
2347
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,161,383✔
2348
        if (code == 0) {
1,161,383✔
2349
          if (pTrans->actionPos < numOfActions) {
21,762✔
2350
            code = TSDB_CODE_ACTION_IN_PROGRESS;
16,152✔
2351
          }
2352
        }
2353
      }
2354
    }
2355
  }
2356

2357
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,358,949✔
2358

2359
  return code;
1,358,949✔
2360
}
2361

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

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

2377
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
16,543,696✔
2378
  if (numOfActions == 0) goto _OVER;
16,543,696✔
2379

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

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

2393
_OVER:
7,247,687✔
2394
  pTrans->stage = TRN_STAGE_REDO_ACTION;
16,543,696✔
2395
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
16,543,696✔
2396
  return continueExec;
16,543,696✔
2397
}
2398

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

2404
  if (pTrans->exec == TRN_EXEC_SERIAL) {
55,082,081✔
2405
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
6,892,778✔
2406
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
48,189,303✔
2407
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
46,830,354✔
2408
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,358,949✔
2409
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,358,949✔
2410
  } else {
2411
    code = TSDB_CODE_INTERNAL_ERROR;
×
2412
  }
2413

2414
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
55,084,841✔
2415
      mndTransIsInSyncContext(topHalf)) {
2,760✔
2416
    pTrans->lastErrorNo = code;
×
2417
    pTrans->code = code;
×
2418
    mInfo(
×
2419
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2420
        "continueExec:%d, code:%s",
2421
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2422
    taosMsleep(100);
×
2423
    return true;
×
2424
  } else {
2425
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
55,082,081✔
2426
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
19,037,136✔
2427
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2428
      return false;
19,037,136✔
2429
    }
2430
  }
2431
  terrno = code;
36,044,945✔
2432

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

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

2469
  return continueExec;
36,044,945✔
2470
}
2471

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

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

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

2491
  return continueExec;
15,314,752✔
2492
}
2493

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

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

2515
  return continueExec;
31,852,192✔
2516
}
2517

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

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

2528
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
9,926✔
2529
  terrno = code;
8,508✔
2530

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

2544
  return continueExec;
8,508✔
2545
}
2546

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

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

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

2563
  return continueExec;
1,418✔
2564
}
2565

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

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

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

2583
  return continueExec;
1,520✔
2584
}
2585

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

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

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

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

2607
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
71,631,136✔
2608
  bool continueExec = true;
71,631,136✔
2609

2610
  while (continueExec) {
205,748,257✔
2611
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
134,117,121✔
2612
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2613
    pTrans->lastExecTime = taosGetTimestampMs();
134,117,121✔
2614
    switch (pTrans->stage) {
134,117,121✔
2615
      case TRN_STAGE_PREPARE:
×
2616
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2617
        break;
×
2618
      case TRN_STAGE_REDO_ACTION:
55,082,081✔
2619
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
55,082,081✔
2620
        break;
55,082,081✔
2621
      case TRN_STAGE_COMMIT:
15,314,752✔
2622
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
15,314,752✔
2623
        break;
15,314,752✔
2624
      case TRN_STAGE_COMMIT_ACTION:
31,852,192✔
2625
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
31,852,192✔
2626
        break;
31,852,192✔
2627
      case TRN_STAGE_ROLLBACK:
1,418✔
2628
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,418✔
2629
        break;
1,418✔
2630
      case TRN_STAGE_UNDO_ACTION:
9,926✔
2631
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
9,926✔
2632
        break;
9,926✔
2633
      case TRN_STAGE_PRE_FINISH:
1,520✔
2634
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,520✔
2635
        break;
1,520✔
2636
      case TRN_STAGE_FINISH:
31,855,232✔
2637
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
31,855,232✔
2638
        break;
31,855,232✔
2639
      default:
×
2640
        continueExec = false;
×
2641
        break;
×
2642
    }
2643
  }
2644

2645
  mndTransSendRpcRsp(pMnode, pTrans);
71,631,136✔
2646
}
71,631,136✔
2647

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2745
  void *pIter = NULL;
11,521,747✔
2746
  while (1) {
1,530,395✔
2747
    STrans *pTrans = NULL;
13,052,142✔
2748
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
13,052,142✔
2749
    if (pIter == NULL) break;
13,052,142✔
2750
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
3,060,790✔
2751
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2752
    }
2753
    sdbRelease(pSdb, pTrans);
1,530,395✔
2754
  }
2755

2756
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
11,521,747✔
2757

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

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

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

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

2796
  return buf;
27,689,040✔
2797
}
2798

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

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

2815
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
572,350✔
2816
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
18,852,784✔
2817
      len = 0;
18,280,434✔
2818
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
18,280,434✔
2819
      if (pAction->actionType == TRANS_ACTION_MSG) {
18,280,434✔
2820
        char bufStart[40] = {0};
13,833,888✔
2821
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
13,833,888✔
2822

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

2831
        SEpSet epset = pAction->epSet;
13,833,888✔
2832
        if (epset.numOfEps > 0) {
13,833,888✔
2833
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
13,833,888✔
2834
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
31,025,822✔
2835
            len +=
17,191,934✔
2836
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
17,191,934✔
2837
          }
2838
        }
2839

2840
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
13,833,888✔
2841
                        tstrerror(pAction->errCode));
2842
      } else {
2843
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
4,446,546✔
2844
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
4,446,546✔
2845
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
4,446,546✔
2846
      }
2847
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
18,280,434✔
2848
    }
2849
  }
2850

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

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

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

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

2891
    cols = 0;
572,350✔
2892

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

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

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

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

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

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

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

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

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

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

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

2958
    mndTransLogAction(pTrans);
572,350✔
2959

2960
    numOfRows++;
572,350✔
2961
    sdbRelease(pSdb, pTrans);
572,350✔
2962
  }
2963

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

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

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

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

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

3003
  cols = 0;
55,682✔
3004

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

3007
  if (pAction->actionType == TRANS_ACTION_MSG) {
55,682✔
3008
    int32_t len = 0;
45,762✔
3009

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

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

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

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

3053
  } else {
3054
    int32_t len = 0;
9,920✔
3055

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3203
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
3204
  SSdb *pSdb = pMnode->pSdb;
×
3205
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
3206
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc