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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 hits per line

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

84.49
/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 "mndCluster.h"
19
#include "mndDb.h"
20
#include "mndPrivilege.h"
21
#include "mndSecurityPolicy.h"
22
#include "mndShow.h"
23
#include "mndStb.h"
24
#include "mndSubscribe.h"
25
#include "mndSync.h"
26
#include "mndToken.h"
27
#include "mndUser.h"
28
#include "mndVgroup.h"
29
#include "osTime.h"
30

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

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

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

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

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

65
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
141,294,867✔
66

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

74
static bool mndCannotExecuteTransWithInfo(SMnode *pMnode, bool topHalf, char *str, int32_t size) {
15,063,889✔
75
  bool isLeader = mndIsLeader(pMnode);
15,063,889✔
76
  bool ret = (!pMnode->deploy && !isLeader) || mndTransIsInSyncContext(topHalf);
15,063,889✔
77
  if (ret)
15,063,889✔
78
    snprintf(str, size, "deploy:%d, isLeader:%d, context:%s", pMnode->deploy, isLeader,
3,865,695✔
79
             topHalf ? "transContext" : "syncContext");
80
  return ret;
15,063,889✔
81
}
82

83
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
201,719,000✔
84

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

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

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

106
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
489,804✔
107
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
489,804✔
108

109
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndRetrieveTrans);
489,804✔
110
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndCancelGetNextTrans);
489,804✔
111
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANSACTION_DETAIL, mndRetrieveTransDetail);
489,804✔
112
  return sdbSetTable(pMnode->pSdb, table);
489,804✔
113
}
114

115
void mndCleanupTrans(SMnode *pMnode) {}
489,741✔
116

117
static int32_t mndTransGetActionsSize(SArray *pArray) {
263,056,660✔
118
  int32_t actionNum = taosArrayGetSize(pArray);
263,056,660✔
119
  int32_t rawDataLen = 0;
263,056,660✔
120

121
  for (int32_t i = 0; i < actionNum; ++i) {
697,532,871✔
122
    STransAction *pAction = taosArrayGet(pArray, i);
434,476,211✔
123
    if (pAction->actionType == TRANS_ACTION_RAW) {
434,476,211✔
124
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
295,058,074✔
125
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
139,418,137✔
126
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
139,418,137✔
127
    } else {
128
      // empty
129
    }
130
    rawDataLen += sizeof(int8_t);
434,476,211✔
131
  }
132

133
  return rawDataLen;
263,056,660✔
134
}
135

136
static int32_t mndTransEncodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionsNum,
263,056,660✔
137
                                    int32_t sver) {
138
  int32_t code = 0;
263,056,660✔
139
  int32_t lino = 0;
263,056,660✔
140
  int32_t dataPos = *offset;
263,056,660✔
141
  int8_t  unused = 0;
263,056,660✔
142
  int32_t ret = -1;
263,056,660✔
143

144
  for (int32_t i = 0; i < actionsNum; ++i) {
697,532,871✔
145
    STransAction *pAction = taosArrayGet(pActions, i);
434,476,211✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
434,476,211✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
434,476,211✔
148
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
434,476,211✔
149
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
434,476,211✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
434,476,211✔
151
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
434,476,211✔
152
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
434,476,211✔
153
    if (sver > TRANS_VER2_NUMBER) {
434,476,211✔
154
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
434,476,211✔
155
    }
156
    if (pAction->actionType == TRANS_ACTION_RAW) {
434,476,211✔
157
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
295,058,074✔
158
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
295,058,074✔
159
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
295,058,074✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
295,058,074✔
161
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
139,418,137✔
162
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
139,418,137✔
163
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
139,418,137✔
164
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
139,418,137✔
165
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
139,418,137✔
166
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
139,418,137✔
167
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
139,418,137✔
168
    } else {
169
      // nothing
170
    }
171
  }
172
  ret = 0;
263,056,660✔
173

174
_OVER:
263,056,660✔
175
  *offset = dataPos;
263,056,660✔
176
  return ret;
263,056,660✔
177
}
178

179
SSdbRaw *mndTransEncode(STrans *pTrans) {
65,764,165✔
180
  int32_t code = 0;
65,764,165✔
181
  int32_t lino = 0;
65,764,165✔
182
  terrno = TSDB_CODE_INVALID_MSG;
65,764,165✔
183
  int8_t sver = TRANS_VER_CURRENT;
65,764,165✔
184

185
  int32_t rawDataLen = sizeof(STrans) + TRANS_RESERVE_SIZE + pTrans->paramLen + pTrans->userDataLen;
65,764,165✔
186
  rawDataLen += mndTransGetActionsSize(pTrans->prepareActions);
65,764,165✔
187
  rawDataLen += mndTransGetActionsSize(pTrans->redoActions);
65,764,165✔
188
  rawDataLen += mndTransGetActionsSize(pTrans->undoActions);
65,764,165✔
189
  rawDataLen += mndTransGetActionsSize(pTrans->commitActions);
65,764,165✔
190

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

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

211
  int32_t prepareActionNum = taosArrayGetSize(pTrans->prepareActions);
65,764,165✔
212
  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
65,764,165✔
213
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
65,764,165✔
214
  int32_t commitActionNum = taosArrayGetSize(pTrans->commitActions);
65,764,165✔
215

216
  if (sver > TRANS_VER1_NUMBER) {
65,764,165✔
217
    SDB_SET_INT32(pRaw, dataPos, prepareActionNum, _OVER)
65,764,165✔
218
  }
219
  SDB_SET_INT32(pRaw, dataPos, redoActionNum, _OVER)
65,764,165✔
220
  SDB_SET_INT32(pRaw, dataPos, undoActionNum, _OVER)
65,764,165✔
221
  SDB_SET_INT32(pRaw, dataPos, commitActionNum, _OVER)
65,764,165✔
222

223
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
65,764,165✔
224
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
65,764,165✔
225
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
65,764,165✔
226
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
65,764,165✔
227

228
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
65,764,165✔
229
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
65,764,165✔
230
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
65,764,165✔
231
  if (pTrans->param != NULL) {
65,764,165✔
232
    SDB_SET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER)
×
233
  }
234

235
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
65,764,165✔
236

237
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
65,764,165✔
238
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
65,764,165✔
239
  void *pIter = NULL;
65,764,165✔
240
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
65,764,165✔
241
  while (pIter) {
65,794,918✔
242
    int32_t arbGroupId = *(int32_t *)pIter;
30,753✔
243
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
30,753✔
244
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
30,753✔
245
  }
246

247
  if (sver > TRANS_VER1_NUMBER) {
65,764,165✔
248
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
65,764,165✔
249
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
65,764,165✔
250
  }
251

252
  if (sver > TRANS_VER2_NUMBER) {
65,764,165✔
253
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
65,764,165✔
254
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
65,764,165✔
255
    void *pIter = NULL;
65,764,165✔
256
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
65,764,165✔
257
    while (pIter) {
68,358,820✔
258
      size_t   keysize = 0;
2,594,655✔
259
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,594,655✔
260
      int32_t  groupPos = *(int32_t *)pIter;
2,594,655✔
261
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,594,655✔
262
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,594,655✔
263
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,594,655✔
264
    }
265
  }
266

267
  if (sver >= TRANS_VER_USER_DATA) {
65,764,165✔
268
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
65,764,165✔
269
    if (pTrans->userDataLen > 0) {
65,764,165✔
270
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
114,930✔
271
    }
272
  }
273

274
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
65,764,165✔
275
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
65,764,165✔
276

277
  terrno = 0;
65,764,165✔
278

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

287
  mTrace("trans:%d, encode to raw:%p, row:%p len:%d", pTrans->id, pRaw, pTrans, dataPos);
65,764,165✔
288
  return pRaw;
65,764,165✔
289
}
290

291
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
29,421,538✔
292
  if (pAction->groupId < 0) return 0;
29,421,538✔
293
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
28,839,279✔
294
  if (redoAction == NULL) {
28,839,279✔
295
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
5,146,695✔
296
    if (array != NULL) {
5,146,695✔
297
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
5,146,695✔
298
        mInfo("failed put action into redo group actions");
×
299
        return TSDB_CODE_INTERNAL_ERROR;
×
300
      }
301
    }
302
    redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
5,146,695✔
303
  }
304
  if (redoAction != NULL) {
28,839,279✔
305
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
57,678,558✔
306
  }
307
  return 0;
28,839,279✔
308
}
309

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

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

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

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

387
  for (int32_t i = 0; i < actionNum; ++i) {
1,213,992,711✔
388
    memset(&action, 0, sizeof(action));
749,446,637✔
389
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
749,446,637✔
390
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
749,446,637✔
391
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
749,446,637✔
392
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
749,446,637✔
393
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
749,446,637✔
394
    action.actionType = actionType;
749,446,637✔
395
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
749,446,637✔
396
    action.stage = stage;
749,446,637✔
397
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
749,446,637✔
398
    if (sver > TRANS_VER2_NUMBER) {
749,446,637✔
399
      SDB_GET_INT32(pRaw, dataPos, &action.groupId, _OVER)
749,446,637✔
400
    }
401
    if (action.actionType == TRANS_ACTION_RAW) {
749,446,637✔
402
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
511,518,300✔
403
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
511,518,300✔
404
      action.pRaw = taosMemoryMalloc(dataLen);
511,518,300✔
405
      if (action.pRaw == NULL) goto _OVER;
511,518,300✔
406
      mTrace("raw:%p, is created", action.pRaw);
511,518,300✔
407
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
511,518,300✔
408
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
511,518,300✔
409
      action.pRaw = NULL;
511,518,300✔
410
    } else if (action.actionType == TRANS_ACTION_MSG) {
237,928,337✔
411
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
237,928,337✔
412
      tmsgUpdateDnodeEpSet(&action.epSet);
237,928,337✔
413
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
237,928,337✔
414
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
237,928,337✔
415
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
237,928,337✔
416
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
237,928,337✔
417
      action.pCont = taosMemoryMalloc(action.contLen);
237,928,337✔
418
      if (action.pCont == NULL) goto _OVER;
237,928,337✔
419
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
237,928,337✔
420
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
237,928,337✔
421
      action.pCont = NULL;
237,928,337✔
422
    } else {
423
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
424
    }
425
  }
426
  ret = 0;
464,546,074✔
427

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

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

451
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
116,569,090✔
452

453
  if (sver > TRANS_VER_CURRENT) {
116,569,090✔
454
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
455
    goto _OVER;
×
456
  }
457

458
  pRow = sdbAllocRow(sizeof(STrans));
116,569,090✔
459
  if (pRow == NULL) goto _OVER;
116,569,090✔
460

461
  pTrans = sdbGetRowObj(pRow);
116,569,090✔
462
  if (pTrans == NULL) goto _OVER;
116,569,090✔
463

464
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
116,569,090✔
465

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

490
  if (sver > TRANS_VER1_NUMBER) {
116,569,090✔
491
    SDB_GET_INT32(pRaw, dataPos, &prepareActionNum, _OVER)
116,569,090✔
492
  }
493
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, _OVER)
116,569,090✔
494
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, _OVER)
116,569,090✔
495
  SDB_GET_INT32(pRaw, dataPos, &commitActionNum, _OVER)
116,569,090✔
496

497
  pTrans->prepareActions = taosArrayInit(prepareActionNum, sizeof(STransAction));
116,569,090✔
498
  pTrans->redoActions = taosArrayInit(redoActionNum, sizeof(STransAction));
116,569,090✔
499
  pTrans->undoActions = taosArrayInit(undoActionNum, sizeof(STransAction));
116,569,090✔
500
  pTrans->commitActions = taosArrayInit(commitActionNum, sizeof(STransAction));
116,569,090✔
501
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
116,569,090✔
502
    pTrans->redoGroupActions = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,730,286✔
503
    pTrans->groupActionPos = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,730,286✔
504
  }
505

506
  if (pTrans->prepareActions == NULL) goto _OVER;
116,569,090✔
507
  if (pTrans->redoActions == NULL) goto _OVER;
116,569,090✔
508
  if (pTrans->undoActions == NULL) goto _OVER;
116,569,090✔
509
  if (pTrans->commitActions == NULL) goto _OVER;
116,569,090✔
510

511
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
116,569,090✔
512
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
116,569,090✔
513
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,730,286✔
514
                                      sver) < 0)
515
      goto _OVER;
×
516
  } else {
517
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
114,838,804✔
518
  }
519
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
116,569,090✔
520
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
116,569,090✔
521

522
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
116,569,090✔
523
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
116,569,090✔
524
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
116,569,090✔
525
  if (pTrans->paramLen != 0) {
116,569,090✔
526
    pTrans->param = taosMemoryMalloc(pTrans->paramLen);
×
527
    if (pTrans->param == NULL) goto _OVER;
×
528
    SDB_GET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER);
×
529
  }
530

531
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
116,569,090✔
532

533
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
116,569,090✔
534
  if (arbgroupIdNum > 0) {
116,569,090✔
535
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
43,395✔
536
    if (pTrans->arbGroupIds == NULL) goto _OVER;
43,395✔
537
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
94,650✔
538
      int32_t arbGroupId = 0;
51,255✔
539
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
51,255✔
540
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
51,255✔
541
    }
542
  }
543

544
  int8_t ableKill = 0;
116,569,090✔
545
  int32_t killMode = 0;
116,569,090✔
546
  if (sver > TRANS_VER1_NUMBER) {
116,569,090✔
547
    SDB_GET_INT8(pRaw, dataPos, &ableKill, _OVER)
116,569,090✔
548
    SDB_GET_INT32(pRaw, dataPos, &killMode, _OVER)
116,569,090✔
549
  }
550
  pTrans->ableToBeKilled = ableKill;
116,569,090✔
551
  pTrans->killMode = (int8_t)killMode;
116,569,090✔
552

553
  if (sver > TRANS_VER2_NUMBER) {
116,569,090✔
554
    int32_t groupNum = -1;
116,569,090✔
555
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
116,569,090✔
556
    for (int32_t i = 0; i < groupNum; ++i) {
121,715,785✔
557
      int32_t groupId = -1;
5,146,695✔
558
      int32_t groupPos = -1;
5,146,695✔
559
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
5,146,695✔
560
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
5,146,695✔
561
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
5,146,695✔
562
        goto _OVER;
×
563
    }
564
  }
565

566
  if (sver >= TRANS_VER_USER_DATA) {
116,569,090✔
567
    SDB_GET_INT32(pRaw, dataPos, &pTrans->userDataLen, _OVER)
116,569,090✔
568
    if (pTrans->userDataLen > 0) {
116,569,090✔
569
      pTrans->userData = taosMemoryMalloc(pTrans->userDataLen);
192,010✔
570
      if (pTrans->userData == NULL) goto _OVER;
192,010✔
571
      SDB_GET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
192,010✔
572
    }
573
  }
574

575
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
116,569,090✔
576

577
  terrno = 0;
116,569,090✔
578

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

588
  if (pTrans != NULL) {
116,569,090✔
589
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
116,569,090✔
590
  }
591
  return pRow;
116,569,090✔
592
}
593

594
static const char *mndTransStr(ETrnStage stage) {
817,685,474✔
595
  switch (stage) {
817,685,474✔
596
    case TRN_STAGE_PREPARE:
65,733,221✔
597
      return "prepare";
65,733,221✔
598
    case TRN_STAGE_REDO_ACTION:
282,181,642✔
599
      return "redoAction";
282,181,642✔
600
    case TRN_STAGE_ROLLBACK:
8,785✔
601
      return "rollback";
8,785✔
602
    case TRN_STAGE_UNDO_ACTION:
12,675,419✔
603
      return "undoAction";
12,675,419✔
604
    case TRN_STAGE_COMMIT:
105,556,989✔
605
      return "commit";
105,556,989✔
606
    case TRN_STAGE_COMMIT_ACTION:
195,500,084✔
607
      return "commitAction";
195,500,084✔
608
    case TRN_STAGE_FINISH:
156,019,994✔
609
      return "finished";
156,019,994✔
610
    case TRN_STAGE_PRE_FINISH:
9,340✔
611
      return "pre-finish";
9,340✔
612
    default:
×
613
      return "invalid";
×
614
  }
615
}
616

617
static const char *mndTransTypeStr(ETrnAct actionType) {
69,861✔
618
  switch (actionType) {
69,861✔
619
    case TRANS_ACTION_MSG:
57,381✔
620
      return "msg";
57,381✔
621
    case TRANS_ACTION_RAW:
12,480✔
622
      return "sdb";
12,480✔
623
    default:
×
624
      return "invalid";
×
625
  }
626
}
627

628
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
226,474,927✔
629
  if (pAction != NULL) {
226,474,927✔
630
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
173,542,980✔
631
      pTrans->lastAction = pAction->id;
123,192,233✔
632
      pTrans->lastMsgType = pAction->msgType;
123,192,233✔
633
      pTrans->lastEpset = pAction->epSet;
123,192,233✔
634
      pTrans->lastErrorNo = pAction->errCode;
123,192,233✔
635
    }
636
  } else {
637
    pTrans->lastAction = 0;
52,931,947✔
638
    pTrans->lastMsgType = 0;
52,931,947✔
639
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
52,931,947✔
640
    pTrans->lastErrorNo = 0;
52,931,947✔
641
  }
642
}
226,474,927✔
643

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

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

652
static TransCbFp mndTransGetCbFp(ETrnFunc ftype) {
631,622✔
653
  switch (ftype) {
631,622✔
654
    case TRANS_START_FUNC_TEST:
×
655
      return mndTransTestStartFunc;
×
656
    case TRANS_STOP_FUNC_TEST:
×
657
      return mndTransTestStopFunc;
×
658
    case TRANS_START_FUNC_MQ_REB:
315,725✔
659
      return mndRebCntInc;
315,725✔
660
    case TRANS_STOP_FUNC_MQ_REB:
315,725✔
661
      return mndRebCntDec;
315,725✔
662
    case TRANS_STOP_FUNC_SOD:
172✔
663
      return mndSodTransStop;
172✔
664
    case TRANS_STOP_FUNC_SOD_ROLE_CHECK:
×
665
      return mndSodGrantRoleStop;
×
666
    default:
×
667
      return NULL;
×
668
  }
669
}
670

671
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
22,188,281✔
672
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
22,188,281✔
673
        mndTransStr(pTrans->stage), pTrans->startFunc);
674

675
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
22,188,281✔
676

677
  if (pTrans->startFunc > 0) {
22,188,281✔
678
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
315,725✔
679
    if (fp) {
315,725✔
680
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
315,725✔
681
    }
682
    // pTrans->startFunc = 0;
683
  }
684

685
  if (pTrans->stage == TRN_STAGE_COMMIT) {
22,188,281✔
686
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
371✔
687
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pTrans->id);
371✔
688
  }
689

690
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
22,188,281✔
691
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
×
692
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
×
693
  }
694

695
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
22,188,281✔
696
    pTrans->stage = TRN_STAGE_FINISH;
×
697
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pTrans->id);
×
698
  }
699

700
  return 0;
22,188,281✔
701
}
702

703
void mndTransDropData(STrans *pTrans) {
137,251,242✔
704
  if (pTrans->prepareActions != NULL) {
137,251,242✔
705
    mndTransDropActions(pTrans->prepareActions);
137,251,242✔
706
    pTrans->prepareActions = NULL;
137,251,242✔
707
  }
708
  if (pTrans->redoActions != NULL) {
137,251,242✔
709
    mndTransDropActions(pTrans->redoActions);
137,251,242✔
710
    pTrans->redoActions = NULL;
137,251,242✔
711
  }
712
  if (pTrans->undoActions != NULL) {
137,251,242✔
713
    mndTransDropActions(pTrans->undoActions);
137,251,242✔
714
    pTrans->undoActions = NULL;
137,251,242✔
715
  }
716
  if (pTrans->commitActions != NULL) {
137,251,242✔
717
    mndTransDropActions(pTrans->commitActions);
137,251,242✔
718
    pTrans->commitActions = NULL;
137,251,242✔
719
  }
720
  if (pTrans->redoGroupActions != NULL) {
137,251,242✔
721
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
22,412,438✔
722
    while (pIter) {
27,815,868✔
723
      SArray **redoActions = pIter;
5,403,430✔
724
      taosArrayDestroy(*redoActions);
5,403,430✔
725
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
5,403,430✔
726
    }
727

728
    taosHashCleanup(pTrans->redoGroupActions);
22,412,438✔
729
    pTrans->redoGroupActions = NULL;
22,412,438✔
730
  }
731
  if (pTrans->groupActionPos != NULL) {
137,251,242✔
732
    taosHashCleanup(pTrans->groupActionPos);
22,412,438✔
733
    pTrans->groupActionPos = NULL;
22,412,438✔
734
  }
735
  if (pTrans->arbGroupIds != NULL) {
137,251,242✔
736
    taosHashCleanup(pTrans->arbGroupIds);
20,725,547✔
737
  }
738
  if (pTrans->pRpcArray != NULL) {
137,251,242✔
739
    taosArrayDestroy(pTrans->pRpcArray);
20,682,152✔
740
    pTrans->pRpcArray = NULL;
20,682,152✔
741
  }
742
  if (pTrans->rpcRsp != NULL) {
137,251,242✔
743
    taosMemoryFree(pTrans->rpcRsp);
7,952,707✔
744
    pTrans->rpcRsp = NULL;
7,952,707✔
745
    pTrans->rpcRspLen = 0;
7,952,707✔
746
  }
747
  if (pTrans->param != NULL) {
137,251,242✔
748
    taosMemoryFree(pTrans->param);
×
749
    pTrans->param = NULL;
×
750
    pTrans->paramLen = 0;
×
751
  }
752
  if (pTrans->userData != NULL) {
137,251,242✔
753
    taosMemoryFree(pTrans->userData);
192,136✔
754
    pTrans->userData = NULL;
192,136✔
755
    pTrans->userDataLen = 0;
192,136✔
756
  }
757
  (void)taosThreadMutexDestroy(&pTrans->mutex);
137,251,242✔
758
}
137,251,242✔
759

760
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc) {
69,375,075✔
761
  mInfo("trans:%d, perform delete action, row:%p stage:%s callfunc:%d, stopFunc:%d", pTrans->id, pTrans,
69,375,075✔
762
        mndTransStr(pTrans->stage), callFunc, pTrans->stopFunc);
763

764
  if (pTrans->stopFunc > 0 && callFunc) {
69,375,075✔
765
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
315,897✔
766
    if (fp) {
315,897✔
767
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
315,897✔
768
    }
769
    // pTrans->stopFunc = 0;
770
  }
771

772
  mndTransDropData(pTrans);
69,375,075✔
773
  return 0;
69,375,075✔
774
}
775

776
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
100,101,948✔
777
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
295,714,445✔
778
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
195,612,497✔
779
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
195,612,497✔
780
    pOldAction->rawWritten = pNewAction->rawWritten;
195,612,497✔
781
    pOldAction->msgSent = pNewAction->msgSent;
195,612,497✔
782
    pOldAction->msgReceived = pNewAction->msgReceived;
195,612,497✔
783
    pOldAction->errCode = pNewAction->errCode;
195,612,497✔
784
  }
785
}
100,101,948✔
786

787
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) {
25,025,487✔
788
  mInfo("trans:%d, perform update action, old row:%p stage:%s create:%" PRId64 ", new row:%p stage:%s create:%" PRId64,
25,025,487✔
789
        pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage), pNew->createdTime);
790

791
  if (pOld->createdTime != pNew->createdTime) {
25,025,487✔
792
    mError("trans:%d, failed to perform update action since createTime not match, old row:%p stage:%s create:%" PRId64
×
793
           ", new row:%p stage:%s create:%" PRId64,
794
           pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage),
795
           pNew->createdTime);
796
    // only occured while sync timeout
797
    TAOS_RETURN(TSDB_CODE_MND_TRANS_SYNC_TIMEOUT);
×
798
  }
799

800
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
25,025,487✔
801
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
25,025,487✔
802
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
25,025,487✔
803
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
25,025,487✔
804
  pOld->stage = pNew->stage;
25,025,487✔
805
  pOld->actionPos = pNew->actionPos;
25,025,487✔
806
  TSWAP(pOld->userData, pNew->userData);
25,025,487✔
807
  TSWAP(pOld->userDataLen, pNew->userDataLen);
25,025,487✔
808

809
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
25,025,487✔
810
  while (pIter != NULL) {
27,205,213✔
811
    int32_t newActionPos = *(int32_t *)pIter;
2,179,726✔
812

813
    size_t   keylen = 0;
2,179,726✔
814
    int32_t *groupId = taosHashGetKey(pIter, &keylen);
2,179,726✔
815

816
    int32_t *oldActionPos = taosHashGet(pOld->groupActionPos, groupId, sizeof(int32_t));
2,179,726✔
817
    if (oldActionPos != NULL) {
2,179,726✔
818
      *oldActionPos = newActionPos;
2,179,726✔
819
    } else
820
      taosHashPut(pOld->groupActionPos, groupId, sizeof(int32_t), &newActionPos, sizeof(int32_t));
×
821

822
    pIter = taosHashIterate(pNew->groupActionPos, pIter);
2,179,726✔
823
  }
824

825
  if (pOld->stage == TRN_STAGE_COMMIT) {
25,025,487✔
826
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
22,159,150✔
827
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
22,159,150✔
828
  }
829

830
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
25,025,487✔
831
    pOld->stage = TRN_STAGE_UNDO_ACTION;
1,859✔
832
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pNew->id);
1,859✔
833
  }
834

835
  if (pOld->stage == TRN_STAGE_PRE_FINISH) {
25,025,487✔
836
    pOld->stage = TRN_STAGE_FINISH;
1,970✔
837
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pNew->id);
1,970✔
838
  }
839

840
  return 0;
25,025,487✔
841
}
842

843
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
93,270,977✔
844
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
93,270,977✔
845
  if (pTrans == NULL) {
93,270,977✔
846
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
138✔
847
  }
848
  return pTrans;
93,270,977✔
849
}
850

851
void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
93,270,977✔
852
  SSdb *pSdb = pMnode->pSdb;
93,270,977✔
853
  if (pTrans != NULL) mInfo("vgId:1, trans:%d, release transaction", pTrans->id);
93,270,977✔
854
  sdbRelease(pSdb, pTrans);
93,270,977✔
855
}
93,270,977✔
856

857
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
20,682,152✔
858
                       const char *opername) {
859
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
20,682,152✔
860
  if (pTrans == NULL) {
20,682,152✔
861
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
862
    mError("failed to create transaction since %s", terrstr());
×
863
    return NULL;
×
864
  }
865

866
  if (opername != NULL) {
20,682,152✔
867
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
20,682,152✔
868
  }
869

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

892
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
20,682,152✔
893
      pTrans->pRpcArray == NULL) {
20,682,152✔
894
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
895
    mError("failed to create transaction since %s", terrstr());
×
896
    mndTransDrop(pTrans);
×
897
    return NULL;
×
898
  }
899

900
  if (pReq != NULL) {
20,682,152✔
901
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
28,165,214✔
902
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
903
      return NULL;
×
904
    }
905
    pTrans->originRpcType = pReq->msgType;
14,082,607✔
906
  }
907

908
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
20,682,152✔
909

910
  mTrace("trans:%d, local object is created, data:%p", pTrans->id, pTrans);
20,682,152✔
911
  return pTrans;
20,682,152✔
912
}
913

914
static void mndTransDropActions(SArray *pArray) {
549,004,968✔
915
  int32_t size = taosArrayGetSize(pArray);
549,004,968✔
916
  for (int32_t i = 0; i < size; ++i) {
1,447,909,111✔
917
    STransAction *pAction = taosArrayGet(pArray, i);
898,904,143✔
918
    if (pAction->actionType == TRANS_ACTION_RAW) {
898,904,143✔
919
      taosMemoryFreeClear(pAction->pRaw);
607,491,594✔
920
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
291,412,549✔
921
      taosMemoryFreeClear(pAction->pCont);
291,412,549✔
922
    } else {
923
      // nothing
924
    }
925
  }
926

927
  taosArrayDestroy(pArray);
549,004,968✔
928
}
549,004,968✔
929

930
void mndTransDrop(STrans *pTrans) {
20,801,656✔
931
  if (pTrans != NULL) {
20,801,656✔
932
    mndTransDropData(pTrans);
20,682,152✔
933
    mTrace("trans:%d, local object is freed, data:%p", pTrans->id, pTrans);
20,682,152✔
934
    taosMemoryFreeClear(pTrans);
20,682,152✔
935
  }
936
}
20,801,656✔
937

938
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
120,035,968✔
939
  pAction->id = taosArrayGetSize(pArray);
120,035,968✔
940

941
  void *ptr = taosArrayPush(pArray, pAction);
120,035,968✔
942
  if (ptr == NULL) {
120,035,968✔
943
    TAOS_RETURN(terrno);
×
944
  }
945

946
  return 0;
120,035,968✔
947
}
948

949
static int32_t mndTransAddActionToGroup(STrans *pTrans, STransAction *pAction) {
6,789,570✔
950
  if (pTrans->exec != TRN_EXEC_GROUP_PARALLEL) return 0;
6,789,570✔
951

952
  SArray **redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
584,703✔
953
  if (redoAction == NULL) {
584,703✔
954
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
256,735✔
955
    if (array != NULL) {
256,735✔
956
      if (taosHashPut(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
256,735✔
957
        mInfo("failed put action into redo group actions");
×
958
        return TSDB_CODE_INTERNAL_ERROR;
×
959
      }
960
      redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
256,735✔
961
    }
962
  }
963
  if (redoAction != NULL) {
584,703✔
964
    mInfo("trans:%d, append action into group %d, msgType:%s", pTrans->id, pAction->groupId,
584,703✔
965
          TMSG_INFO(pAction->msgType));
966
    void *ptr = taosArrayPush(*redoAction, &pAction);
584,703✔
967
    if (ptr == NULL) {
584,703✔
968
      TAOS_RETURN(terrno);
×
969
    }
970
  }
971

972
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &pAction->groupId, sizeof(int32_t));
584,703✔
973
  if (actionPos == NULL) {
584,703✔
974
    int32_t pos = 0;
256,735✔
975
    if (taosHashPut(pTrans->groupActionPos, &pAction->groupId, sizeof(int32_t), &pos, sizeof(int32_t)) < 0) {
256,735✔
976
      mInfo("failed put action into group action pos");
×
977
      return TSDB_CODE_INTERNAL_ERROR;
×
978
    }
979
  }
980

981
  return 0;
584,703✔
982
}
983

984
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
354,727✔
985
  STransAction action = {
354,727✔
986
      .stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw, .mTraceId = pTrans->mTraceId};
354,727✔
987
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) return TSDB_CODE_INTERNAL_ERROR;
354,727✔
988
  return mndTransAppendAction(pTrans->redoActions, &action);
354,727✔
989
}
990

991
int32_t mndTransAppendGroupRedolog(STrans *pTrans, SSdbRaw *pRaw, int32_t groupId) {
320,083✔
992
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION,
320,083✔
993
                         .actionType = TRANS_ACTION_RAW,
994
                         .pRaw = pRaw,
995
                         .mTraceId = pTrans->mTraceId,
320,083✔
996
                         .groupId = groupId};
997
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
320,083✔
998
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
×
999
    return TSDB_CODE_INTERNAL_ERROR;
×
1000
  }
1001
  if (groupId > 0) TAOS_CHECK_RETURN(mndTransAddActionToGroup(pTrans, &action));
320,083✔
1002
  return mndTransAppendAction(pTrans->redoActions, &action);
320,083✔
1003
}
1004

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

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

1024
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
4,518,245✔
1025
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
4,518,245✔
1026
  return mndTransAppendAction(pTrans->undoActions, &action);
4,518,245✔
1027
}
1028

1029
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
74,419,722✔
1030
  STransAction action = {.stage = TRN_STAGE_COMMIT_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
74,419,722✔
1031
  return mndTransAppendAction(pTrans->commitActions, &action);
74,419,722✔
1032
}
1033

1034
int32_t mndTransAppendPrepareLog(STrans *pTrans, SSdbRaw *pRaw) {
11,018,579✔
1035
  STransAction action = {
11,018,579✔
1036
      .pRaw = pRaw, .stage = TRN_STAGE_PREPARE, .actionType = TRANS_ACTION_RAW, .mTraceId = pTrans->mTraceId};
11,018,579✔
1037
  return mndTransAppendAction(pTrans->prepareActions, &action);
11,018,579✔
1038
}
1039

1040
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
21,322,704✔
1041
  pAction->stage = TRN_STAGE_REDO_ACTION;
21,322,704✔
1042
  pAction->actionType = TRANS_ACTION_MSG;
21,322,704✔
1043
  pAction->mTraceId = pTrans->mTraceId;
21,322,704✔
1044
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId == 0) {
21,322,704✔
1045
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
599✔
1046
    return TSDB_CODE_INTERNAL_ERROR;
599✔
1047
  }
1048
  if (pAction->groupId > 0) TAOS_CHECK_RETURN(mndTransAddActionToGroup(pTrans, pAction));
21,322,105✔
1049
  return mndTransAppendAction(pTrans->redoActions, pAction);
21,322,105✔
1050
}
1051

1052
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
8,082,507✔
1053
  pAction->stage = TRN_STAGE_UNDO_ACTION;
8,082,507✔
1054
  pAction->actionType = TRANS_ACTION_MSG;
8,082,507✔
1055
  return mndTransAppendAction(pTrans->undoActions, pAction);
8,082,507✔
1056
}
1057

1058
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
7,952,707✔
1059
  pTrans->rpcRsp = pCont;
7,952,707✔
1060
  pTrans->rpcRspLen = contLen;
7,952,707✔
1061
}
7,952,707✔
1062

1063
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
312,466✔
1064
  pTrans->startFunc = startFunc;
312,466✔
1065
  pTrans->stopFunc = stopFunc;
312,466✔
1066
  pTrans->param = param;
312,466✔
1067
  pTrans->paramLen = paramLen;
312,466✔
1068
}
312,466✔
1069

1070
int32_t mndSetRpcInfoForDbTrans(SMnode *pMnode, SRpcMsg *pMsg, EOperType oper, const char *dbname) {
×
1071
  STrans *pTrans = NULL;
×
1072
  void   *pIter = NULL;
×
1073
  int32_t code = -1;
×
1074

1075
  while (1) {
1076
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
×
1077
    if (pIter == NULL) break;
×
1078

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

1091
        sdbRelease(pMnode->pSdb, pTrans);
×
1092
        break;
×
1093
      }
1094
    }
1095

1096
    sdbRelease(pMnode->pSdb, pTrans);
×
1097
  }
1098
  return code;
×
1099
}
1100

1101
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
11,423,239✔
1102
  if (dbname != NULL) {
11,423,239✔
1103
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
11,423,239✔
1104
  }
1105
  if (stbname != NULL) {
11,423,239✔
1106
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
8,649,460✔
1107
  }
1108
}
11,423,239✔
1109

1110
void mndTransSetUserData(STrans *pTrans, void* data, int32_t dataLen) {
38,264✔
1111
  if (pTrans->userData != NULL) {
38,264✔
1112
    taosMemoryFree(pTrans->userData);
×
1113
  }
1114
  pTrans->userData = data;
38,264✔
1115
  pTrans->userDataLen = dataLen;
38,264✔
1116
}
38,264✔
1117

1118
void mndTransAddArbGroupId(STrans *pTrans, int32_t groupId) {
39,473✔
1119
  if (taosHashPut(pTrans->arbGroupIds, &groupId, sizeof(int32_t), NULL, 0) != 0) {
39,473✔
1120
    mError("trans:%d, failed to put groupid into hash, groupId:%d", pTrans->id, groupId);
×
1121
  }
1122
}
39,473✔
1123

1124
void mndTransSetSerial(STrans *pTrans) {
305,285✔
1125
  mInfo("trans:%d, set Serial", pTrans->id);
305,285✔
1126
  pTrans->exec = TRN_EXEC_SERIAL;
305,285✔
1127
}
305,285✔
1128

1129
void mndTransSetGroupParallel(STrans *pTrans) {
104,361✔
1130
  mInfo("trans:%d, set Group Parallel", pTrans->id);
104,361✔
1131
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
104,361✔
1132
}
104,361✔
1133

1134
void mndTransSetParallel(STrans *pTrans) {
×
1135
  mInfo("trans:%d, set Parallel", pTrans->id);
×
1136
  pTrans->exec = TRN_EXEC_PARALLEL;
×
1137
}
×
1138

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

1141
void mndTransSetKillMode(STrans *pTrans, ETrnKillMode killMode) {
43,175✔
1142
  pTrans->ableToBeKilled = true; 
43,175✔
1143
  pTrans->killMode = killMode; 
43,175✔
1144
}
43,175✔
1145

1146
void mndTransSetChangeless(STrans *pTrans) { pTrans->changeless = true; }
16,198✔
1147

1148
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
1,450,823✔
1149

1150
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
43,542,500✔
1151
  int32_t  code = 0;
43,542,500✔
1152
  SSdbRaw *pRaw = mndTransEncode(pTrans);
43,542,500✔
1153
  if (pRaw == NULL) {
43,542,500✔
1154
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1155
    if (terrno != 0) code = terrno;
×
1156
    mError("trans:%d, failed to encode while sync trans since %s", pTrans->id, tstrerror(code));
×
1157
    TAOS_RETURN(code);
×
1158
  }
1159
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_READY));
43,542,500✔
1160

1161
  mInfo("trans:%d, sync to other mnodes, stage:%s createTime:%" PRId64, pTrans->id, mndTransStr(pTrans->stage),
43,542,500✔
1162
        pTrans->createdTime);
1163
  code = mndSyncPropose(pMnode, pRaw, pTrans->id);
43,542,500✔
1164
  if (code != 0) {
43,542,500✔
1165
    mError("trans:%d, failed to sync, errno:%s code:0x%x createTime:%" PRId64 " saved trans:%d", pTrans->id,
6,263✔
1166
           tstrerror(code), code, pTrans->createdTime, pMnode->syncMgmt.transId);
1167
    sdbFreeRaw(pRaw);
6,263✔
1168
    TAOS_RETURN(code);
6,263✔
1169
  }
1170

1171
  sdbFreeRaw(pRaw);
43,536,237✔
1172
  mInfo("trans:%d, sync to other mnodes finished, createTime:%" PRId64, pTrans->id, pTrans->createdTime);
43,536,237✔
1173
  TAOS_RETURN(code);
43,536,237✔
1174
}
1175

1176
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
240,496✔
1177
  if (conflict[0] == 0) return false;
240,496✔
1178
  if (taosStrcasecmp(conflict, pTrans->dbname) == 0) return true;
240,496✔
1179
  return false;
180,440✔
1180
}
1181

1182
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
5,993,593✔
1183
  if (conflict[0] == 0) return false;
5,993,593✔
1184
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
5,812,147✔
1185
  return false;
5,677,494✔
1186
}
1187

1188
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
6,234,089✔
1189
  if (conflict) {
6,234,089✔
1190
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
194,709✔
1191
      "stb:%s type:%d", 
1192
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1193
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1194
    *globalConflict = true;
194,709✔
1195
  } else {
1196
    mInfo("trans:%d, opername:%s db:%s stb:%s type:%d, not conflict with trans:%d, opername:%s db:%s stb:%s type:%d", 
6,039,380✔
1197
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1198
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1199
  }
1200
}
6,234,089✔
1201

1202
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
54,119,640✔
1203
  STrans *pTrans = NULL;
54,119,640✔
1204
  void   *pIter = NULL;
54,119,640✔
1205
  bool    conflict = false;
54,119,640✔
1206

1207
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
54,119,640✔
1208

1209
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
40,961,965✔
1210
  mDebug("trans:%d, trans hash size %d", pNew->id, size);
40,961,965✔
1211

1212
  while (1) {
1213
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
47,034,077✔
1214
    if (pIter == NULL) break;
47,034,077✔
1215

1216
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
6,072,112✔
1217

1218
    if (pNew->conflict == TRN_CONFLICT_DB) {
6,072,112✔
1219
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
203,166✔
1220
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
203,166✔
1221
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
181,446✔
1222
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
181,446✔
1223
      }
1224
    }
1225

1226
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
6,072,112✔
1227
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
5,831,161✔
1228
      if (pTrans->conflict == TRN_CONFLICT_DB) {
5,831,161✔
1229
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
59,050✔
1230
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
59,050✔
1231
      }
1232
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
5,831,161✔
1233
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
5,753,097✔
1234
      }
1235
    }
1236

1237
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
6,072,112✔
1238
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
32,366✔
1239
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
32,366✔
1240
        void *pGidIter = taosHashIterate(pNew->arbGroupIds, NULL);
×
1241
        while (pGidIter != NULL) {
×
1242
          int32_t groupId = *(int32_t *)pGidIter;
×
1243
          if (taosHashGet(pTrans->arbGroupIds, &groupId, sizeof(int32_t)) != NULL) {
×
1244
            taosHashCancelIterate(pNew->arbGroupIds, pGidIter);
×
1245
            mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1246
            break;
×
1247
          } else {
1248
            mndTransLogConflict(pNew, pTrans, false, &conflict);
×
1249
          }
1250
          pGidIter = taosHashIterate(pNew->arbGroupIds, pGidIter);
×
1251
        }
1252
      }
1253
    }
1254

1255
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
6,072,112✔
1256
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
×
1257
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1258
      } else {
1259
        mndTransLogConflict(pNew, pTrans, false, &conflict);
×
1260
      }
1261
    }
1262

1263
    sdbRelease(pMnode->pSdb, pTrans);
6,072,112✔
1264
  }
1265

1266
  return conflict;
40,961,965✔
1267
}
1268

1269
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
54,119,640✔
1270
  int32_t code = 0;
54,119,640✔
1271
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
54,119,640✔
1272
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
34,515,206✔
1273
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
1274
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
1275
      TAOS_RETURN(code);
×
1276
    }
1277
  }
1278

1279
  if (mndCheckTransConflict(pMnode, pTrans)) {
54,119,640✔
1280
    code = TSDB_CODE_MND_TRANS_CONFLICT;
234,537✔
1281
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
234,537✔
1282
    TAOS_RETURN(code);
234,537✔
1283
  }
1284

1285
  TAOS_RETURN(code);
53,885,103✔
1286
}
1287

1288
int32_t mndTransCheckConflictWithCompact(SMnode *pMnode, STrans *pTrans) {
161,205✔
1289
  int32_t      code = 0;
161,205✔
1290
  void        *pIter = NULL;
161,205✔
1291
  bool         conflict = false;
161,205✔
1292
  SCompactObj *pCompact = NULL;
161,205✔
1293

1294
  while (1) {
1,021✔
1295
    bool thisConflict = false;
162,226✔
1296
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
162,226✔
1297
    if (pIter == NULL) break;
162,226✔
1298

1299
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
1,021✔
1300
      thisConflict = true;
138✔
1301
    }
1302
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
1,021✔
1303
      if (taosStrcasecmp(pTrans->dbname, pCompact->dbname) == 0) thisConflict = true;
883✔
1304
    }
1305

1306
    if (thisConflict) {
1,021✔
1307
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
1,021✔
1308
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1309
      conflict = true;
1,021✔
1310
    } else {
1311
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with compact:%d db:%s", pTrans->id, pTrans->dbname,
×
1312
            pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1313
    }
1314
    sdbRelease(pMnode->pSdb, pCompact);
1,021✔
1315
  }
1316

1317
  if (conflict) {
161,205✔
1318
    code = TSDB_CODE_MND_TRANS_CONFLICT_COMPACT;
1,021✔
1319
    mError("trans:%d, failed to check tran conflict with compact since %s", pTrans->id, tstrerror(code));
1,021✔
1320
    TAOS_RETURN(code);
1,021✔
1321
  }
1322

1323
  TAOS_RETURN(code);
160,184✔
1324
}
1325

1326
int32_t mndTransCheckConflictWithRetention(SMnode *pMnode, STrans *pTrans) {
160,184✔
1327
  int32_t        code = 0;
160,184✔
1328
  SSdb          *pSdb = pMnode->pSdb;
160,184✔
1329
  void          *pIter = NULL;
160,184✔
1330
  bool           conflict = false;
160,184✔
1331
  SRetentionObj *pRetention = NULL;
160,184✔
1332

1333
  while ((pIter = sdbFetch(pSdb, SDB_RETENTION, pIter, (void **)&pRetention)) != NULL) {
161,674✔
1334
    conflict = false;
1,490✔
1335

1336
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
1,490✔
1337
      conflict = true;
×
1338
    }
1339
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
1,490✔
1340
      if (taosStrcasecmp(pTrans->dbname, pRetention->dbname) == 0) conflict = true;
1,490✔
1341
    }
1342

1343
    if (conflict) {
1,490✔
1344
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with retention:%d db:%s", pTrans->id,
×
1345
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pRetention->id, pRetention->dbname);
1346
      sdbRelease(pSdb, pRetention);
×
1347
      sdbCancelFetch(pSdb, pIter);
×
1348
      break;
×
1349
    } else {
1350
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with retention:%d db:%s", pTrans->id, pTrans->dbname,
1,490✔
1351
            pTrans->stbname, pTrans->conflict, pRetention->id, pRetention->dbname);
1352
    }
1353
    sdbRelease(pSdb, pRetention);
1,490✔
1354
  }
1355

1356
  if (conflict) {
160,184✔
1357
    code = TSDB_CODE_MND_TRANS_CONFLICT_RETENTION;
×
1358
    mError("trans:%d, failed to check tran conflict with retention since %s", pTrans->id, tstrerror(code));
×
1359
    TAOS_RETURN(code);
×
1360
  }
1361

1362
  TAOS_RETURN(code);
160,184✔
1363
}
1364

1365
static bool mndTransActionsOfSameType(SArray *pActions) {
46,397,875✔
1366
  int32_t size = taosArrayGetSize(pActions);
46,397,875✔
1367
  ETrnAct lastActType = TRANS_ACTION_NULL;
46,397,875✔
1368
  bool    same = true;
46,397,875✔
1369
  for (int32_t i = 0; i < size; ++i) {
144,518,537✔
1370
    STransAction *pAction = taosArrayGet(pActions, i);
98,120,662✔
1371
    if (i > 0) {
98,120,662✔
1372
      if (lastActType != pAction->actionType) {
65,278,203✔
1373
        same = false;
×
1374
        break;
×
1375
      }
1376
    }
1377
    lastActType = pAction->actionType;
98,120,662✔
1378
  }
1379
  return same;
46,397,875✔
1380
}
1381

1382
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
20,417,861✔
1383
  int32_t code = 0;
20,417,861✔
1384
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
20,417,861✔
1385
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
20,018,948✔
1386
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1387
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1388
      TAOS_RETURN(code);
×
1389
    }
1390

1391
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
20,018,948✔
1392
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
5,962,638✔
1393
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1394
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1395
        TAOS_RETURN(code);
×
1396
      }
1397
    }
1398
  }
1399

1400
  TAOS_RETURN(code);
20,417,861✔
1401
}
1402

1403
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
20,417,861✔
1404
  int32_t code = 0;
20,417,861✔
1405
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
20,417,861✔
1406
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
1,572✔
1407
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
1,572✔
1408
    TAOS_RETURN(code);
1,572✔
1409
  }
1410
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
20,416,289✔
1411
    code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1412
    mError("trans:%d, types of commit actions are not the same", pTrans->id);
×
1413
    TAOS_RETURN(code);
×
1414
  }
1415

1416
  TAOS_RETURN(code);
20,416,289✔
1417
}
1418

1419
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
20,418,926✔
1420
  int32_t code = 0;
20,418,926✔
1421
  if (pTrans == NULL) {
20,418,926✔
1422
    TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
1423
  }
1424

1425
  mInfo("trans:%d, action list:", pTrans->id);
20,418,926✔
1426
  int32_t index = 0;
20,418,926✔
1427
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
31,428,877✔
1428
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
11,009,951✔
1429
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
11,009,951✔
1430
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1431
  }
1432

1433
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
42,364,430✔
1434
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
21,945,504✔
1435
    if (pAction->actionType == TRANS_ACTION_MSG) {
21,945,504✔
1436
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
21,285,154✔
1437
            TMSG_INFO(pAction->msgType));
1438
      ;
1439
    } else {
1440
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
660,350✔
1441
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1442
    }
1443
  }
1444

1445
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
94,798,031✔
1446
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
74,379,105✔
1447
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
74,379,105✔
1448
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1449
  }
1450

1451
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
33,019,678✔
1452
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
12,600,752✔
1453
    if (pAction->actionType == TRANS_ACTION_MSG) {
12,600,752✔
1454
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
8,082,507✔
1455
            TMSG_INFO(pAction->msgType));
1456
    } else {
1457
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
4,518,245✔
1458
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1459
    }
1460
  }
1461

1462
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
20,418,926✔
1463

1464
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
20,417,861✔
1465

1466
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
20,417,861✔
1467

1468
  mInfo("trans:%d, prepare transaction", pTrans->id);
20,416,289✔
1469
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
20,416,289✔
1470
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
1,414✔
1471
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
1,414✔
1472
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
1,414✔
1473
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
1,414✔
1474
    TAOS_RETURN(code);
1,414✔
1475
  }
1476
  mInfo("trans:%d, prepare finished", pTrans->id);
20,414,875✔
1477

1478
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
20,414,875✔
1479
  if (pNew == NULL) {
20,414,875✔
1480
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1481
    if (terrno != 0) code = terrno;
×
1482
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, tstrerror(code));
×
1483
    TAOS_RETURN(code);
×
1484
  }
1485

1486
  pNew->pRpcArray = pTrans->pRpcArray;
20,414,875✔
1487
  pNew->rpcRsp = pTrans->rpcRsp;
20,414,875✔
1488
  pNew->rpcRspLen = pTrans->rpcRspLen;
20,414,875✔
1489
  pNew->mTraceId = pTrans->mTraceId;
20,414,875✔
1490
  pTrans->pRpcArray = NULL;
20,414,875✔
1491
  pTrans->rpcRsp = NULL;
20,414,875✔
1492
  pTrans->rpcRspLen = 0;
20,414,875✔
1493

1494
  mInfo("trans:%d, execute transaction in prepare", pTrans->id);
20,414,875✔
1495
  mndTransExecute(pMnode, pNew, false);
20,414,875✔
1496
  mndReleaseTrans(pMnode, pNew);
20,414,875✔
1497
  // TDOD change to TAOS_RETURN(code);
1498
  TAOS_RETURN(0);
20,414,875✔
1499
}
1500

1501
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
20,412,650✔
1502
  int32_t code = 0;
20,412,650✔
1503
  mInfo("trans:%d, commit transaction", pTrans->id);
20,412,650✔
1504
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
20,412,650✔
1505
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
4,849✔
1506
    TAOS_RETURN(code);
4,849✔
1507
  }
1508
  mInfo("trans:%d, commit finished", pTrans->id);
20,407,801✔
1509
  TAOS_RETURN(code);
20,407,801✔
1510
}
1511

1512
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
1,689✔
1513
  int32_t code = 0;
1,689✔
1514
  mInfo("trans:%d, rollback transaction", pTrans->id);
1,689✔
1515
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,689✔
1516
    mError("trans:%d, failed to rollback since %s", pTrans->id, tstrerror(code));
×
1517
    TAOS_RETURN(code);
×
1518
  }
1519
  mInfo("trans:%d, rollback finished", pTrans->id);
1,689✔
1520
  TAOS_RETURN(code);
1,689✔
1521
}
1522

1523
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
1,800✔
1524
  int32_t code = 0;
1,800✔
1525
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
1,800✔
1526
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,800✔
1527
    mError("trans:%d, failed to pre-finish since %s", pTrans->id, tstrerror(code));
×
1528
    TAOS_RETURN(code);
×
1529
  }
1530
  mInfo("trans:%d, pre-finish finished", pTrans->id);
1,800✔
1531
  TAOS_RETURN(code);
1,800✔
1532
}
1533

1534
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
93,270,839✔
1535
  bool    sendRsp = false;
93,270,839✔
1536
  int32_t code = pTrans->code;
93,270,839✔
1537

1538
  if (pTrans->stage == TRN_STAGE_FINISH) {
93,270,839✔
1539
    sendRsp = true;
42,571,116✔
1540
  }
1541

1542
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
93,270,839✔
1543
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
24,275,802✔
1544
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
10,559✔
1545
      sendRsp = true;
10,559✔
1546
    }
1547
  } else {
1548
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
68,995,037✔
1549
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
39,043,406✔
1550
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1551
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1552
      } else {
1553
        if (pTrans->failedTimes > 6) sendRsp = true;
39,043,406✔
1554
      }
1555
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
39,043,406✔
1556
    }
1557
  }
1558

1559
  if (!sendRsp) {
93,270,839✔
1560
    return;
50,689,164✔
1561
  } else {
1562
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
42,581,675✔
1563
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1564
  }
1565

1566
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
42,581,675✔
1567
  taosWLockLatch(&pTrans->lockRpcArray);
42,581,675✔
1568
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
42,581,675✔
1569
  if (size <= 0) {
42,581,675✔
1570
    taosWUnLockLatch(&pTrans->lockRpcArray);
28,742,546✔
1571
    return;
28,742,546✔
1572
  }
1573

1574
  for (int32_t i = 0; i < size; ++i) {
27,678,258✔
1575
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
13,839,129✔
1576
    if (pInfo->handle != NULL) {
13,839,129✔
1577
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
13,279,367✔
1578
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1579
      }
1580
      if (code == TSDB_CODE_SYN_TIMEOUT) {
13,279,367✔
1581
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
85✔
1582
      }
1583

1584
      if (i != 0 && code == 0) {
13,279,367✔
1585
        code = TSDB_CODE_MNODE_NOT_FOUND;
×
1586
      }
1587
      mInfo("vgId:1, trans:%d, client:%d start to send rsp, code:0x%x stage:%s app:%p", pTrans->id, i, code,
13,279,367✔
1588
            mndTransStr(pTrans->stage), pInfo->ahandle);
1589

1590
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
13,279,367✔
1591

1592
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
13,279,367✔
1593
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,410,111✔
1594
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,410,111✔
1595
        if (pDb != NULL) {
1,410,111✔
1596
          for (int32_t j = 0; j < 12; j++) {
1,772,290✔
1597
            bool ready = mndIsDbReady(pMnode, pDb);
1,771,472✔
1598
            if (!ready) {
1,771,472✔
1599
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
362,179✔
1600
              taosMsleep(1000);
362,179✔
1601
            } else {
1602
              break;
1,409,293✔
1603
            }
1604
          }
1605
        }
1606
        mndReleaseDb(pMnode, pDb);
1,410,111✔
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
11,869,256✔
1608
        void   *pCont = NULL;
2,085,424✔
1609
        int32_t contLen = 0;
2,085,424✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,085,424✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,083,735✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
9,783,832✔
1614
        void   *pCont = NULL;
9,935✔
1615
        int32_t contLen = 0;
9,935✔
1616
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
9,935✔
1617
          mndTransSetRpcRsp(pTrans, pCont, contLen);
9,935✔
1618
        }
1619
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
9,773,897✔
1620
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
9,885✔
1621
        if (code != 0) {
9,885✔
1622
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
9,764,012✔
1625
        void   *pCont = NULL;
19,119✔
1626
        int32_t contLen = 0;
19,119✔
1627
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
19,119✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
19,119✔
1629
        }
1630
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
9,744,893✔
1631
        void   *pCont = NULL;
19,019✔
1632
        int32_t contLen = 0;
19,019✔
1633
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
19,019✔
1634
          mndTransSetRpcRsp(pTrans, pCont, contLen);
19,019✔
1635
        }
1636
      }
1637

1638
      if (pTrans->rpcRspLen != 0) {
13,279,367✔
1639
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
7,936,752✔
1640
        if (rpcCont != NULL) {
7,936,752✔
1641
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
7,936,752✔
1642
          rspMsg.pCont = rpcCont;
7,936,752✔
1643
          rspMsg.contLen = pTrans->rpcRspLen;
7,936,752✔
1644
        }
1645
      }
1646

1647
      tmsgSendRsp(&rspMsg);
13,279,367✔
1648

1649
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
13,279,367✔
1650
            mndTransStr(pTrans->stage), pInfo->ahandle);
1651
    }
1652
  }
1653
  taosArrayClear(pTrans->pRpcArray);
13,839,129✔
1654
  taosWUnLockLatch(&pTrans->lockRpcArray);
13,839,129✔
1655
}
1656

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

1677
  SArray *pArray = NULL;
23,543,637✔
1678
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
23,543,637✔
1679
    pArray = pTrans->redoActions;
23,536,881✔
1680
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
6,756✔
1681
    pArray = pTrans->undoActions;
6,756✔
1682
  } else {
1683
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1684
    goto _OVER;
×
1685
  }
1686

1687
  if (pArray == NULL) {
23,543,637✔
1688
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1689
    goto _OVER;
×
1690
  }
1691

1692
  int32_t actionNum = taosArrayGetSize(pArray);
23,543,637✔
1693
  if (action < 0 || action >= actionNum) {
23,543,637✔
1694
    mError("trans:%d, invalid action:%d", transId, action);
×
1695
    goto _OVER;
×
1696
  }
1697

1698
  STransAction *pAction = taosArrayGet(pArray, action);
23,543,637✔
1699
  if (pAction != NULL) {
23,543,637✔
1700
    if (pAction->msgSent) {
23,543,637✔
1701
      pAction->msgReceived = 1;
23,543,637✔
1702
      pAction->errCode = pRsp->code;
23,543,637✔
1703
      pAction->endTime = taosGetTimestampMs();
23,543,637✔
1704

1705
      // pTrans->lastErrorNo = pRsp->code;
1706
      mndSetTransLastAction(pTrans, pAction);
23,543,637✔
1707

1708
      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,
23,543,637✔
1709
            mndTransStr(pAction->stage), action, TMSG_INFO(pAction->msgType), trace ? trace->rootId : 0, 
1710
              trace ? trace->msgId : 0, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1711
            tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1712
    } else {
1713
      mWarn("trans:%d, %s:%d response is received, but msgSent is false, code:0x%x(%s), accept:0x%x(%s) retry:0x%x", 
×
1714
            transId, mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code),
1715
            pAction->acceptableCode, tstrerror(pAction->acceptableCode), pAction->retryCode);
1716
    }
1717

1718
  } else {
1719
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1720
  }
1721

1722
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
23,543,637✔
1723
  mndTransExecute(pMnode, pTrans, true);
23,543,637✔
1724

1725
_OVER:
23,543,637✔
1726
  mndReleaseTrans(pMnode, pTrans);
23,543,637✔
1727
  TAOS_RETURN(code);
23,543,637✔
1728
}
1729

1730
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
2,298,057✔
1731
  pAction->rawWritten = 0;
2,298,057✔
1732
  pAction->msgSent = 0;
2,298,057✔
1733
  pAction->msgReceived = 0;
2,298,057✔
1734
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
2,298,057✔
1735
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
2,298,057✔
1736
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
709✔
1737
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
709✔
1738
          pAction->id, pAction->epSet.inUse);
1739
  } else {
1740
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
2,297,348✔
1741
  }
1742
  //  pAction->errCode = 0;
1743
}
2,298,057✔
1744

1745
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
1,689✔
1746
  int32_t numOfActions = taosArrayGetSize(pArray);
1,689✔
1747

1748
  for (int32_t action = 0; action < numOfActions; ++action) {
8,445✔
1749
    STransAction *pAction = taosArrayGet(pArray, action);
6,756✔
1750
    if (pAction->msgSent && pAction->msgReceived &&
6,756✔
1751
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
6,756✔
1752
      continue;
5,067✔
1753
    if (pAction->msgSent && !pAction->msgReceived) {
1,689✔
1754
      int64_t timestamp = taosGetTimestampMs();
×
1755
      if (timestamp - pAction->startTime <= TRANS_ACTION_TIMEOUT) continue;
×
1756
    }
1757

1758
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
1,689✔
1759

1760
    mndTransResetAction(pMnode, pTrans, pAction);
1,689✔
1761
    mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage), pAction->id,
1,689✔
1762
          pAction->errCode, pAction->startTime);
1763
  }
1764
}
1,689✔
1765

1766
// execute in sync context
1767
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
166,457,668✔
1768
  if (pAction->rawWritten) return 0;
166,457,668✔
1769
  if (topHalf) {
91,464,370✔
1770
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
833✔
1771
  }
1772

1773
  if (pAction->pRaw->type >= SDB_MAX) {
91,463,537✔
1774
    pAction->rawWritten = true;
×
1775
    pAction->errCode = 0;
×
1776
    mndSetTransLastAction(pTrans, pAction);
×
1777
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
1778
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1779
  }
1780

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

1788
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
91,463,537✔
1789
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
91,463,537✔
1790
    pAction->rawWritten = true;
91,463,537✔
1791
    pAction->errCode = 0;
91,463,537✔
1792
    code = 0;
91,463,537✔
1793
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
91,463,537✔
1794
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1795

1796
    mndSetTransLastAction(pTrans, pAction);
91,463,537✔
1797
  } else {
1798
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1799
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1800
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1801
    mndSetTransLastAction(pTrans, pAction);
×
1802
  }
1803

1804
  TAOS_RETURN(code);
91,463,537✔
1805
}
1806

1807
// execute in trans context
1808
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
101,374,890✔
1809
                                     bool notSend) {
1810
  if (pAction->msgSent) return 0;
101,374,890✔
1811
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
38,206,778✔
1812
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
14,644,519✔
1813
  }
1814

1815
  if (notSend) {
23,562,259✔
1816
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
12,384✔
1817
    return 0;
12,384✔
1818
  }
1819

1820
#ifndef TD_ASTRA_32
1821
  int64_t signature = pTrans->id;
23,549,875✔
1822
  signature = (signature << 32);
23,549,875✔
1823
  signature += pAction->id;
23,549,875✔
1824

1825
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
23,549,875✔
1826
#else
1827
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1828
                    .contLen = pAction->contLen,
1829
                    .info.ahandle = (void *)pTrans->id,
1830
                    .info.ahandleEx = (void *)pAction->id};
1831
#endif
1832
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
23,549,875✔
1833
  if (rpcMsg.pCont == NULL) {
23,549,875✔
1834
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1835
    return -1;
×
1836
  }
1837
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
23,549,875✔
1838
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
23,549,875✔
1839
  rpcMsg.info.notFreeAhandle = 1;
23,549,875✔
1840
  STraceId* trace = &(rpcMsg.info.traceId);
23,549,875✔
1841

1842
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
23,549,875✔
1843

1844
  char    detail[1024] = {0};
23,549,875✔
1845
  int32_t len = snprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
23,549,875✔
1846
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
23,549,875✔
1847
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
50,005,066✔
1848
    len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
26,455,191✔
1849
                    pAction->epSet.eps[i].port);
26,455,191✔
1850
  }
1851

1852
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
23,549,875✔
1853
  if (code == 0) {
23,549,875✔
1854
    pAction->msgSent = 1;
23,549,875✔
1855
    // pAction->msgReceived = 0;
1856
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
23,549,875✔
1857
    pAction->startTime = taosGetTimestampMs();
23,549,875✔
1858
    pAction->endTime = 0;
23,549,875✔
1859
    mInfo("trans:%d, %s:%d is sent, QID:0x%" PRIx64 ":0x%" PRIx64 ", %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, trace ? trace->rootId : 0, 
23,549,875✔
1860
              trace ? trace->msgId : 0, detail);
1861

1862
    mndSetTransLastAction(pTrans, pAction);
23,549,875✔
1863
  } else {
UNCOV
1864
    pAction->msgSent = 0;
×
UNCOV
1865
    pAction->msgReceived = 0;
×
UNCOV
1866
    pAction->errCode = (terrno != 0) ? terrno : code;
×
UNCOV
1867
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
×
1868
           detail);
1869

UNCOV
1870
    mndSetTransLastAction(pTrans, pAction);
×
1871
  }
1872

1873
  TAOS_RETURN(code);
23,549,875✔
1874
}
1875

1876
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1877
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1878
  pAction->rawWritten = 0;
×
1879
  pAction->errCode = 0;
×
1880
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1881

1882
  mndSetTransLastAction(pTrans, pAction);
×
1883
  return 0;
×
1884
}
1885

1886
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
267,832,558✔
1887
                                        bool notSend) {
1888
  if (pAction->actionType == TRANS_ACTION_RAW) {
267,832,558✔
1889
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
166,457,668✔
1890
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
101,374,890✔
1891
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
101,374,890✔
1892
  } else {
1893
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1894
  }
1895
}
1896

1897
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
84,396,196✔
1898
  int32_t numOfActions = taosArrayGetSize(pArray);
84,396,196✔
1899
  int32_t code = 0;
84,396,196✔
1900

1901
  for (int32_t action = 0; action < numOfActions; ++action) {
313,484,519✔
1902
    STransAction *pAction = taosArrayGet(pArray, action);
240,630,530✔
1903
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
240,630,530✔
1904
    if (code != 0) {
240,630,530✔
1905
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
11,542,207✔
1906
            numOfActions);
1907
      break;
11,542,207✔
1908
    }
1909
  }
1910

1911
  return code;
84,396,196✔
1912
}
1913

1914
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
104,234,900✔
1915
  int32_t numOfActions = taosArrayGetSize(pArray);
104,234,900✔
1916
  int32_t code = 0;
104,234,900✔
1917
  if (numOfActions == 0) return 0;
104,234,900✔
1918

1919
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
84,396,196✔
1920
    return code;
11,542,207✔
1921
  }
1922

1923
  int32_t       numOfExecuted = 0;
72,853,989✔
1924
  int32_t       errCode = 0;
72,853,989✔
1925
  STransAction *pErrAction = NULL;
72,853,989✔
1926
  for (int32_t action = 0; action < numOfActions; ++action) {
301,942,312✔
1927
    STransAction *pAction = taosArrayGet(pArray, action);
229,088,323✔
1928
    if (pAction->msgReceived || pAction->rawWritten) {
229,088,323✔
1929
      numOfExecuted++;
190,250,624✔
1930
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
190,250,624✔
1931
        errCode = pAction->errCode;
8,693✔
1932
        pErrAction = pAction;
8,693✔
1933
      }
1934
    } else {
1935
      pErrAction = pAction;
38,837,699✔
1936
    }
1937
  }
1938

1939
  mndSetTransLastAction(pTrans, pErrAction);
72,853,989✔
1940

1941
  if (numOfExecuted == numOfActions) {
72,853,989✔
1942
    if (errCode == 0) {
52,933,636✔
1943
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
52,931,947✔
1944
      return 0;
52,931,947✔
1945
    } else {
1946
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,689✔
1947
      mndTransResetActions(pMnode, pTrans, pArray);
1,689✔
1948
      terrno = errCode;
1,689✔
1949
      return errCode;
1,689✔
1950
    }
1951
  } else {
1952
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
19,920,353✔
1953

1954
    for (int32_t action = 0; action < numOfActions; ++action) {
76,919,494✔
1955
      STransAction *pAction = taosArrayGet(pArray, action);
56,999,141✔
1956
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
56,999,141✔
1957
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1958
             pAction->acceptableCode, pAction->retryCode);
1959
      if (pAction->msgSent) {
56,999,141✔
1960
        bool reset = false;
56,986,757✔
1961
        if (pAction->msgReceived) {
56,986,757✔
1962
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
18,161,442✔
1963
        } else {
1964
          int64_t timestamp = taosGetTimestampMs();
38,825,315✔
1965
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
38,825,315✔
1966
        }
1967
        if (reset) {
56,986,757✔
1968
          mndTransResetAction(pMnode, pTrans, pAction);
7,004✔
1969
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
7,004✔
1970
                pAction->id, pAction->errCode, pAction->startTime);
1971
        }
1972
      }
1973
    }
1974
    return TSDB_CODE_ACTION_IN_PROGRESS;
19,920,353✔
1975
  }
1976
}
1977

1978
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
61,655,330✔
1979
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
61,655,330✔
1980
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
61,655,330✔
1981
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
1,689✔
1982
           mndStrExecutionContext(topHalf));
1983
  }
1984
  return code;
61,655,330✔
1985
}
1986

1987
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
12,248✔
1988
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf, notSend);
12,248✔
1989
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
12,248✔
1990
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1991
           mndStrExecutionContext(topHalf));
1992
  }
1993
  return code;
12,248✔
1994
}
1995

1996
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
42,567,322✔
1997
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
42,567,322✔
1998
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
42,567,322✔
1999
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
2000
           mndStrExecutionContext(topHalf));
2001
  }
2002
  return code;
42,567,322✔
2003
}
2004

2005
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
7,841,951✔
2006
  int32_t code = 0;
7,841,951✔
2007
  int32_t numOfActions = taosArrayGetSize(pActions);
7,841,951✔
2008
  if (numOfActions == 0) return code;
7,841,951✔
2009

2010
  if (pTrans->actionPos >= numOfActions) {
7,837,945✔
2011
    return code;
334,131✔
2012
  }
2013

2014
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
7,503,814✔
2015
        pTrans->actionPos, mndTransStr(pTrans->stage));
2016

2017
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
11,897,704✔
2018
    STransAction *pAction = taosArrayGet(pActions, action);
11,602,475✔
2019

2020
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
11,602,475✔
2021
      code = TSDB_CODE_ACTION_IN_PROGRESS;
8,041✔
2022
      break;
8,188✔
2023
    }
2024

2025
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
11,594,434✔
2026
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
2027

2028
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
11,594,434✔
2029
    if (code == 0) {
11,594,434✔
2030
      if (pAction->msgSent) {
9,904,625✔
2031
        bool reset = false;
8,724,162✔
2032
        if (pAction->msgReceived) {
8,724,162✔
2033
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,824,575✔
2034
            code = pAction->errCode;
2,237,951✔
2035
            reset = true;
2,237,951✔
2036
          } else {
2037
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,586,624✔
2038
          }
2039
        } else {
2040
          int64_t timestamp = taosGetTimestampMs();
4,899,587✔
2041
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
4,899,587✔
2042
          code = TSDB_CODE_ACTION_IN_PROGRESS;
4,899,587✔
2043
        }
2044
        if (reset) {
8,724,162✔
2045
          mndTransResetAction(pMnode, pTrans, pAction);
2,237,951✔
2046
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,237,951✔
2047
                pAction->id, pAction->errCode, pAction->startTime);
2048
        }
2049
      } else if (pAction->rawWritten) {
1,180,463✔
2050
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,180,463✔
2051
          code = pAction->errCode;
×
2052
        } else {
2053
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,180,463✔
2054
        }
2055
      } else {
2056
      }
2057
    }
2058

2059
    if (code == 0) {
11,594,434✔
2060
      pTrans->failedTimes = 0;
2,767,087✔
2061
    }
2062
    mndSetTransLastAction(pTrans, pAction);
11,594,434✔
2063
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
11,594,434✔
2064
      mInfo(
6,589,396✔
2065
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2066
          "msgReceived:%d",
2067
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2068
          pAction->msgSent, pAction->msgReceived);
2069
    } else if (code != 0) {
5,005,038✔
2070
      mError(
2,237,951✔
2071
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2072
          "msgReceived:%d",
2073
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2074
          pAction->msgSent, pAction->msgReceived);
2075
    }
2076

2077
    char str[200] = {0};
11,594,434✔
2078
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
11,594,434✔
2079
      pTrans->lastErrorNo = code;
2,300,957✔
2080
      pTrans->code = code;
2,300,957✔
2081
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
2,300,957✔
2082
            action, str);
2083
      break;
2,300,957✔
2084
    }
2085

2086
    if (code == 0) {
9,293,477✔
2087
      pTrans->code = 0;
2,155,939✔
2088
      pTrans->actionPos++;
2,155,939✔
2089
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
2,155,939✔
2090
            pAction->id);
2091
      (void)taosThreadMutexUnlock(&pTrans->mutex);
2,155,939✔
2092
      code = mndTransSync(pMnode, pTrans);
2,155,939✔
2093
      (void)taosThreadMutexLock(&pTrans->mutex);
2,155,939✔
2094
      if (code != 0) {
2,155,939✔
2095
        pTrans->actionPos--;
×
2096
        pTrans->code = terrno;
×
2097
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2098
               mndTransStr(pAction->stage), pAction->id, terrstr());
2099
        break;
×
2100
      }
2101
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
7,137,538✔
2102
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,899,587✔
2103
      break;
4,899,587✔
2104
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
2,237,951✔
2105
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
709✔
2106
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
2,237,951✔
2107
            code, tstrerror(code));
2108
      pTrans->lastErrorNo = code;
2,237,951✔
2109
      taosMsleep(300);
2,237,951✔
2110
      action--;
2,237,951✔
2111
      continue;
2,237,951✔
2112
    } else {
UNCOV
2113
      terrno = code;
×
UNCOV
2114
      pTrans->lastErrorNo = code;
×
UNCOV
2115
      pTrans->code = code;
×
UNCOV
2116
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2117
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
UNCOV
2118
      break;
×
2119
    }
2120
  }
2121

2122
  return code;
7,503,814✔
2123
}
2124

2125
static int32_t mndTransExecuteActionsSerialGroup(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf,
4,353,729✔
2126
                                                 int32_t groupId, int32_t currentGroup, int32_t groupCount,
2127
                                                 bool notSend, SHashObj *pHash) {
2128
  int32_t code = 0;
4,353,729✔
2129
  int32_t numOfActions = taosArrayGetSize(pActions);
4,353,729✔
2130
  if (numOfActions == 0) return code;
4,353,729✔
2131

2132
  if (groupId <= 0) {
4,353,729✔
2133
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
2134
    return TSDB_CODE_INTERNAL_ERROR;
×
2135
  }
2136

2137
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &groupId, sizeof(int32_t));
4,353,729✔
2138
  if (actionPos == NULL) {
4,353,729✔
2139
    mError("trans:%d, failed to execute action in serail group, actionPos is null at group %d", pTrans->id, groupId);
×
2140
    return TSDB_CODE_INTERNAL_ERROR;
×
2141
  }
2142

2143
  if (*actionPos >= numOfActions) {
4,353,729✔
2144
    mInfo("trans:%d, this serial group is finished, actionPos:%d >= numOfActions:%d at group %d", pTrans->id,
710,764✔
2145
          *actionPos, numOfActions, groupId);
2146
    return TSDB_CODE_MND_TRANS_GROUP_FINISHED;
710,764✔
2147
  }
2148

2149
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
4,248,511✔
2150
    STransAction **ppAction = taosArrayGet(pActions, action);
3,995,796✔
2151
    STransAction  *pAction = *ppAction;
3,995,796✔
2152

2153
    if (notSend && !pAction->msgSent) {
3,995,796✔
2154
      mInfo(
526,341✔
2155
          "trans:%d, %s:%d (%d/%d at group %d) skip to execute, curent state(actionType(1:msg,2:log):%d, "
2156
          "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2157
          pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2158
          pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2159
      code = TSDB_CODE_ACTION_IN_PROGRESS;
526,341✔
2160
      break;
526,341✔
2161
    }
2162

2163
    mInfo(
3,469,455✔
2164
        "trans:%d, %s:%d (%d/%d at group %d) begin to execute, curent state(actionType(1:msg,2:log):%d, "
2165
        "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2166
        pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2167
        pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2168

2169
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
3,469,455✔
2170
    if (code == 0) {
3,469,455✔
2171
      if (pAction->msgSent) {
2,056,119✔
2172
        if (pAction->msgReceived) {
1,839,916✔
2173
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
540,745✔
2174
            code = pAction->errCode;
51,413✔
2175
            mndTransResetAction(pMnode, pTrans, pAction);
51,413✔
2176
          } else {
2177
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
489,332✔
2178
                  pAction->id, action, numOfActions, groupId);
2179
          }
2180
        } else {
2181
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,299,171✔
2182
        }
2183
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,839,916✔
2184
        if (msgSent != NULL) {
1,839,916✔
2185
          *msgSent = pAction->msgSent;
1,839,916✔
2186
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,839,916✔
2187
        } else {
2188
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2189
        }
2190
      } else if (pAction->rawWritten) {
216,203✔
2191
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
216,203✔
2192
          code = pAction->errCode;
×
2193
        } else {
2194
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
216,203✔
2195
        }
2196
      } else {
2197
      }
2198
    }
2199

2200
    if (code == 0) {
3,469,455✔
2201
      pTrans->failedTimes = 0;
705,535✔
2202
    }
2203
    mndSetTransLastAction(pTrans, pAction);
3,469,455✔
2204

2205
    char str[200] = {0};
3,469,455✔
2206
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
3,469,455✔
2207
      pTrans->lastErrorNo = code;
1,564,738✔
2208
      pTrans->code = code;
1,564,738✔
2209
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
1,564,738✔
2210
        mInfo(
1,413,336✔
2211
            "trans:%d, %s:%d (%d/%d at group %d) not able to execute since %s, current state("
2212
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2213
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2214
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2215
      } else if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
151,402✔
2216
        mError(
×
2217
            "trans:%d, %s:%d (%d/%d at group %d) failed to execute since %s, current action("
2218
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2219
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2220
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2221
      }
2222
      mInfo("trans:%d, %s:%d (%d/%d at group %d) cannot execute next action, stop this group execution, %s", pTrans->id,
1,564,738✔
2223
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2224
      break;
1,564,738✔
2225
    }
2226

2227
    if (code == 0) {
1,904,717✔
2228
      pTrans->code = 0;
554,133✔
2229
      pTrans->actionPos++;
554,133✔
2230
      (*actionPos)++;
554,133✔
2231
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
554,133✔
2232
            pAction->id);
2233
      (void)taosThreadMutexUnlock(&pTrans->mutex);
554,133✔
2234
      code = mndTransSync(pMnode, pTrans);
554,133✔
2235
      (void)taosThreadMutexLock(&pTrans->mutex);
554,133✔
2236
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
554,133✔
2237
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
12,733,510✔
2238
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
12,179,377✔
2239
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
12,179,377✔
2240
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
12,179,377✔
2241
          pTmpAction->msgSent = *msgSent;
653,697✔
2242
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
653,697✔
2243
        }
2244
      }
2245
      if (code != 0) {
554,133✔
2246
        pTrans->actionPos--;
×
2247
        (*actionPos)--;
×
2248
        pTrans->code = terrno;
×
2249
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2250
               mndTransStr(pAction->stage), pAction->id, terrstr());
2251
        break;
×
2252
      }
2253
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
1,350,584✔
2254
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,299,171✔
2255
            mndTransStr(pAction->stage), pAction->id);
2256
      break;
1,299,171✔
2257
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
51,413✔
2258
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
×
2259
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
51,413✔
2260
            code, tstrerror(code));
2261
      pTrans->lastErrorNo = code;
51,413✔
2262
      taosMsleep(300);
51,413✔
2263
      action--;
51,413✔
2264
      continue;
51,413✔
2265
    } else {
2266
      terrno = code;
×
2267
      pTrans->lastErrorNo = code;
×
2268
      pTrans->code = code;
×
2269
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2270
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2271
      break;
×
2272
    }
2273
  }
2274

2275
  return code;
3,642,965✔
2276
}
2277

2278
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
7,810,455✔
2279
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
7,810,455✔
2280
  (void)taosThreadMutexLock(&pTrans->mutex);
7,810,455✔
2281
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
7,810,455✔
2282
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
7,810,455✔
2283
  }
2284
  (void)taosThreadMutexUnlock(&pTrans->mutex);
7,810,455✔
2285
  return code;
7,810,455✔
2286
}
2287

2288
static int32_t mndTransExecuteRedoActionGroup(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
1,401,290✔
2289
  int32_t total_code = TSDB_CODE_ACTION_IN_PROGRESS;
1,401,290✔
2290
  int32_t code = 0;
1,401,290✔
2291
  int32_t successCount = 0;
1,401,290✔
2292
  int32_t groupCount = taosHashGetSize(pTrans->redoGroupActions);
1,401,290✔
2293

2294
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,401,290✔
2295
  if(pHash == NULL){
1,401,290✔
2296
    mError("trans:%d, failed to init hash since %s", pTrans->id, terrstr());
×
2297
    return -1;
×
2298
  }
2299
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
31,204,586✔
2300
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
29,803,296✔
2301
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
29,803,296✔
2302
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
29,803,296✔
2303
  }
2304
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
1,401,290✔
2305

2306
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,401,290✔
2307
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,401,290✔
2308
  int32_t currentGroup = 1;
1,401,290✔
2309
  while (pIter) {
5,755,019✔
2310
    SArray **redoActions = pIter;
4,353,729✔
2311
    size_t   keyLen = 0;
4,353,729✔
2312
    int32_t *key = taosHashGetKey(pIter, &keyLen);
4,353,729✔
2313
    int32_t  actionCount = taosArrayGetSize(*redoActions);
4,353,729✔
2314
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
4,353,729✔
2315
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2316
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
4,353,729✔
2317
                                             notSend, pHash);
2318
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
4,353,729✔
2319
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,413,336✔
2320
            tstrerror(code));
2321
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,940,393✔
2322
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,825,512✔
2323
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
1,114,881✔
2324
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
710,764✔
2325
    } else if (code != 0) {
404,117✔
2326
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
×
2327
             tstrerror(code));
2328
    } else {
2329
      successCount++;
404,117✔
2330
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
404,117✔
2331
    }
2332
    currentGroup++;
4,353,729✔
2333
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,353,729✔
2334
  }
2335

2336
  taosHashCleanup(pHash);
1,401,290✔
2337

2338
  if (successCount == groupCount) {
1,401,290✔
2339
    total_code = 0;
25,704✔
2340
  } else {
2341
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,375,586✔
2342
  }
2343

2344
  return total_code;
1,401,290✔
2345
}
2346

2347
static int32_t mndTransExecuteRedoActionsParallel(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
1,632,893✔
2348
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
1,632,893✔
2349
  (void)taosThreadMutexLock(&pTrans->mutex);
1,632,893✔
2350

2351
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,632,893✔
2352
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,632,893✔
2353
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,632,893✔
2354
      code = 0;
200,107✔
2355
    } else {
2356
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,432,786✔
2357
      if (pAction != NULL && pAction->groupId == -1) {
1,432,786✔
2358
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
31,496✔
2359
      } else {
2360
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,401,290✔
2361
        if (code == 0) {
1,401,290✔
2362
          if (pTrans->actionPos < numOfActions) {
25,704✔
2363
            code = TSDB_CODE_ACTION_IN_PROGRESS;
18,530✔
2364
          }
2365
        }
2366
      }
2367
    }
2368
  }
2369

2370
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,632,893✔
2371

2372
  return code;
1,632,893✔
2373
}
2374

2375
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
2376
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
2377
  (void)taosThreadMutexLock(&pTrans->mutex);
×
2378
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2379
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
2380
  }
2381
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
2382
  return code;
×
2383
}
2384

2385
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
22,168,341✔
2386
  bool    continueExec = true;
22,168,341✔
2387
  int32_t code = 0;
22,168,341✔
2388
  terrno = 0;
22,168,341✔
2389

2390
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
22,168,341✔
2391
  if (numOfActions == 0) goto _OVER;
22,168,341✔
2392

2393
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
8,967,722✔
2394

2395
  for (int32_t action = 0; action < numOfActions; ++action) {
21,105,861✔
2396
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
12,138,139✔
2397
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
12,138,139✔
2398
    if (code != 0) {
12,138,139✔
2399
      terrno = code;
×
2400
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
2401
             numOfActions, tstrerror(code));
2402
      return false;
×
2403
    }
2404
  }
2405

2406
_OVER:
8,967,722✔
2407
  pTrans->stage = TRN_STAGE_REDO_ACTION;
22,168,341✔
2408
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
22,168,341✔
2409
  return continueExec;
22,168,341✔
2410
}
2411

2412
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
71,098,678✔
2413
  bool    continueExec = true;
71,098,678✔
2414
  int32_t code = 0;
71,098,678✔
2415
  terrno = 0;
71,098,678✔
2416

2417
  if (pTrans->exec == TRN_EXEC_SERIAL) {
71,098,678✔
2418
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
7,810,455✔
2419
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
63,288,223✔
2420
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
61,655,330✔
2421
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,632,893✔
2422
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,632,893✔
2423
  } else {
2424
    code = TSDB_CODE_INTERNAL_ERROR;
×
2425
  }
2426

2427
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
71,100,367✔
2428
      mndTransIsInSyncContext(topHalf)) {
1,689✔
2429
    pTrans->lastErrorNo = code;
×
2430
    pTrans->code = code;
×
2431
    mInfo(
×
2432
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2433
        "continueExec:%d, code:%s",
2434
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2435
    taosMsleep(100);
×
2436
    return true;
×
2437
  } else {
2438
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
71,098,678✔
2439
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
25,051,832✔
2440
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2441
      return false;
25,051,832✔
2442
    }
2443
  }
2444
  terrno = code;
46,046,846✔
2445

2446
  if (code == 0) {
46,046,846✔
2447
    pTrans->code = 0;
20,412,650✔
2448
    pTrans->stage = TRN_STAGE_COMMIT;
20,412,650✔
2449
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
20,412,650✔
2450
    continueExec = true;
20,412,650✔
2451
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
25,634,196✔
2452
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
25,632,507✔
2453
    continueExec = false;
25,632,507✔
2454
  } else {
2455
    pTrans->failedTimes++;
1,689✔
2456
    pTrans->code = terrno;
1,689✔
2457
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
1,689✔
2458
      if (pTrans->lastAction != 0) {
1,689✔
2459
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
1,604✔
2460
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
1,604✔
2461
          if (pTrans->failedTimes < 6) {
×
2462
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2463
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
2464
            taosMsleep(1000);
×
2465
            continueExec = true;
×
2466
            return true;
×
2467
          }
2468
        }
2469
      }
2470

2471
      pTrans->stage = TRN_STAGE_ROLLBACK;
1,689✔
2472
      pTrans->actionPos = 0;
1,689✔
2473
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
1,689✔
2474
             pTrans->actionPos);
2475
      continueExec = true;
1,689✔
2476
    } else {
UNCOV
2477
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
2478
      continueExec = false;
×
2479
    }
2480
  }
2481

2482
  return continueExec;
46,046,846✔
2483
}
2484

2485
// execute in trans context
2486
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
20,412,834✔
2487
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
20,412,834✔
2488

2489
  bool    continueExec = true;
20,412,650✔
2490
  int32_t code = mndTransCommit(pMnode, pTrans);
20,412,650✔
2491

2492
  if (code == 0) {
20,412,650✔
2493
    pTrans->code = 0;
20,407,801✔
2494
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
20,407,801✔
2495
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
20,407,801✔
2496
    continueExec = true;
20,407,801✔
2497
  } else {
2498
    pTrans->code = terrno;
4,849✔
2499
    pTrans->failedTimes++;
4,849✔
2500
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
4,849✔
2501
    continueExec = false;
4,849✔
2502
  }
2503

2504
  return continueExec;
20,412,650✔
2505
}
2506

2507
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
42,567,322✔
2508
  bool    continueExec = true;
42,567,322✔
2509
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
42,567,322✔
2510

2511
  if (code == 0) {
42,567,322✔
2512
    pTrans->code = 0;
42,567,138✔
2513
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
42,567,138✔
2514
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
42,567,138✔
2515
    continueExec = true;
42,567,138✔
2516
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
184✔
2517
    pTrans->code = 0;
184✔
2518
    pTrans->stage = TRN_STAGE_COMMIT;
184✔
2519
    mInfo("trans:%d, back to commit stage", pTrans->id);
184✔
2520
    continueExec = true;
184✔
2521
  } else {
2522
    pTrans->code = terrno;
×
2523
    pTrans->failedTimes++;
×
2524
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2525
    continueExec = false;
×
2526
  }
2527

2528
  return continueExec;
42,567,322✔
2529
}
2530

2531
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
12,248✔
2532
  bool    continueExec = true;
12,248✔
2533
  int32_t code = 0;
12,248✔
2534

2535
  if (pTrans->exec == TRN_EXEC_SERIAL) {
12,248✔
2536
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
2537
  } else {
2538
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
12,248✔
2539
  }
2540

2541
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
12,248✔
2542
  terrno = code;
10,389✔
2543

2544
  if (code == 0) {
10,389✔
2545
    pTrans->stage = TRN_STAGE_PRE_FINISH;
1,689✔
2546
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
1,689✔
2547
    continueExec = true;
1,689✔
2548
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
8,700✔
2549
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
8,700✔
2550
    continueExec = false;
8,700✔
2551
  } else {
2552
    pTrans->failedTimes++;
×
2553
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2554
    continueExec = false;
×
2555
  }
2556

2557
  return continueExec;
10,389✔
2558
}
2559

2560
// in trans context
2561
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
1,689✔
2562
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
1,689✔
2563

2564
  bool    continueExec = true;
1,689✔
2565
  int32_t code = mndTransRollback(pMnode, pTrans);
1,689✔
2566

2567
  if (code == 0) {
1,689✔
2568
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1,689✔
2569
    continueExec = true;
1,689✔
2570
  } else {
2571
    pTrans->failedTimes++;
×
2572
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2573
    continueExec = false;
×
2574
  }
2575

2576
  return continueExec;
1,689✔
2577
}
2578

2579
// excute in trans context
2580
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
1,800✔
2581
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
1,800✔
2582

2583
  bool    continueExec = true;
1,800✔
2584
  int32_t code = mndTransPreFinish(pMnode, pTrans);
1,800✔
2585

2586
  if (code == 0) {
1,800✔
2587
    pTrans->stage = TRN_STAGE_FINISH;
1,800✔
2588
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
1,800✔
2589
    continueExec = true;
1,800✔
2590
  } else {
2591
    pTrans->failedTimes++;
×
2592
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2593
    continueExec = false;
×
2594
  }
2595

2596
  return continueExec;
1,800✔
2597
}
2598

2599
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
42,570,908✔
2600
  bool continueExec = false;
42,570,908✔
2601
  if (topHalf) return continueExec;
42,570,908✔
2602

2603
  SSdbRaw *pRaw = mndTransEncode(pTrans);
22,161,307✔
2604
  if (pRaw == NULL) {
22,161,307✔
2605
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
2606
    return false;
×
2607
  }
2608
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
22,161,307✔
2609

2610
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
22,161,307✔
2611
  if (code != 0) {
22,161,307✔
2612
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
2613
  }
2614

2615
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
22,161,307✔
2616
        pTrans->failedTimes, pTrans->createdTime);
2617
  return continueExec;
22,161,307✔
2618
}
2619

2620
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
93,270,839✔
2621
  bool continueExec = true;
93,270,839✔
2622

2623
  while (continueExec) {
269,936,318✔
2624
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
176,665,479✔
2625
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2626
    pTrans->lastExecTime = taosGetTimestampMs();
176,665,479✔
2627
    switch (pTrans->stage) {
176,665,479✔
2628
      case TRN_STAGE_PREPARE:
×
2629
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2630
        break;
×
2631
      case TRN_STAGE_REDO_ACTION:
71,098,678✔
2632
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
71,098,678✔
2633
        break;
71,098,678✔
2634
      case TRN_STAGE_COMMIT:
20,412,834✔
2635
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
20,412,834✔
2636
        break;
20,412,834✔
2637
      case TRN_STAGE_COMMIT_ACTION:
42,567,322✔
2638
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
42,567,322✔
2639
        break;
42,567,322✔
2640
      case TRN_STAGE_ROLLBACK:
1,689✔
2641
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,689✔
2642
        break;
1,689✔
2643
      case TRN_STAGE_UNDO_ACTION:
12,248✔
2644
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
12,248✔
2645
        break;
12,248✔
2646
      case TRN_STAGE_PRE_FINISH:
1,800✔
2647
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,800✔
2648
        break;
1,800✔
2649
      case TRN_STAGE_FINISH:
42,570,908✔
2650
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
42,570,908✔
2651
        break;
42,570,908✔
2652
      default:
×
2653
        continueExec = false;
×
2654
        break;
×
2655
    }
2656
  }
2657

2658
  mndTransSendRpcRsp(pMnode, pTrans);
93,270,839✔
2659
}
93,270,839✔
2660

2661
// start trans, pullup, receive rsp, kill
2662
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
46,076,824✔
2663
  bool topHalf = true;
46,076,824✔
2664
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
46,076,824✔
2665
}
46,076,824✔
2666

2667
// update trans
2668
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
47,194,015✔
2669
  bool topHalf = false;
47,194,015✔
2670
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
47,194,015✔
2671
}
47,194,015✔
2672

2673
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
16,877,943✔
2674
  mTrace("start to process trans timer");
16,877,943✔
2675
  mndTransPullup(pReq->info.node);
16,877,943✔
2676
  return 0;
16,877,943✔
2677
}
2678

2679
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
111✔
2680
  SArray *pArray = NULL;
111✔
2681
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
111✔
2682
    pArray = pTrans->redoActions;
111✔
2683
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2684
    pArray = pTrans->undoActions;
×
2685
  } else {
2686
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2687
  }
2688

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

2712
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
111✔
2713
  mndTransExecute(pMnode, pTrans, true);
111✔
2714
  return 0;
111✔
2715
}
2716

2717
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
249✔
2718
  SMnode       *pMnode = pReq->info.node;
249✔
2719
  SKillTransReq killReq = {0};
249✔
2720
  int32_t       code = -1;
249✔
2721
  STrans       *pTrans = NULL;
249✔
2722

2723
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
249✔
2724
    code = TSDB_CODE_INVALID_MSG;
×
2725
    goto _OVER;
×
2726
  }
2727

2728
  mInfo("trans:%d, start to kill, force:%d", killReq.transId, tsForceKillTrans);
249✔
2729
  if ((code = mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_TRANS)) != 0) {
249✔
2730
    goto _OVER;
×
2731
  }
2732

2733
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
249✔
2734
  if (pTrans == NULL) {
249✔
2735
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
138✔
2736
    if (terrno != 0) code = terrno;
138✔
2737
    goto _OVER;
138✔
2738
  }
2739

2740
  code = mndKillTrans(pMnode, pTrans);
111✔
2741

2742
_OVER:
249✔
2743
  if (code != 0) {
249✔
2744
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
138✔
2745
  }
2746

2747
  mndReleaseTrans(pMnode, pTrans);
249✔
2748
  TAOS_RETURN(code);
249✔
2749
}
2750

2751
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
78,485✔
2752

2753
void mndTransPullup(SMnode *pMnode) {
17,013,182✔
2754
  SSdb   *pSdb = pMnode->pSdb;
17,013,182✔
2755
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
17,013,182✔
2756
  if (pArray == NULL) return;
17,013,182✔
2757

2758
  void *pIter = NULL;
17,013,182✔
2759
  while (1) {
2,118,201✔
2760
    STrans *pTrans = NULL;
19,131,383✔
2761
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
19,131,383✔
2762
    if (pIter == NULL) break;
19,131,383✔
2763
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
4,236,402✔
2764
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2765
    }
2766
    sdbRelease(pSdb, pTrans);
2,118,201✔
2767
  }
2768

2769
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
17,013,182✔
2770

2771
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
19,131,383✔
2772
    int32_t *pTransId = taosArrayGet(pArray, i);
2,118,201✔
2773
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
2,118,201✔
2774
    if (pTrans != NULL) {
2,118,201✔
2775
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
2,118,201✔
2776
      mndTransExecute(pMnode, pTrans, false);
2,118,201✔
2777
    }
2778
    mndReleaseTrans(pMnode, pTrans);
2,118,201✔
2779
  }
2780
  taosArrayDestroy(pArray);
17,013,182✔
2781
}
2782

2783
static char *formatTimestamp(char *buf, int32_t cap, int64_t val, int precision) {
22,703,373✔
2784
  time_t tt;
22,703,373✔
2785
  if (precision == TSDB_TIME_PRECISION_MICRO) {
22,703,373✔
2786
    tt = (time_t)(val / 1000000);
×
2787
  }
2788
  if (precision == TSDB_TIME_PRECISION_NANO) {
22,703,373✔
2789
    tt = (time_t)(val / 1000000000);
×
2790
  } else {
2791
    tt = (time_t)(val / 1000);
22,703,373✔
2792
  }
2793

2794
  struct tm tm;
22,703,373✔
2795
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
22,703,373✔
2796
    mError("failed to get local time");
×
2797
    return NULL;
×
2798
  }
2799
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
22,703,373✔
2800

2801
  if (precision == TSDB_TIME_PRECISION_MICRO) {
22,703,373✔
2802
    snprintf(buf + pos, cap - (pos), ".%06d", (int)(val % 1000000));
×
2803
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
22,703,373✔
2804
    snprintf(buf + pos, cap - (pos), ".%09d", (int)(val % 1000000000));
×
2805
  } else {
2806
    snprintf(buf + pos, cap - (pos), ".%03d", (int)(val % 1000));
22,703,373✔
2807
  }
2808

2809
  return buf;
22,703,373✔
2810
}
2811

2812
static void mndTransLogAction(STrans *pTrans) {
510,178✔
2813
  char    detail[512] = {0};
510,178✔
2814
  int32_t len = 0;
510,178✔
2815
  int32_t index = 0;
510,178✔
2816

2817
  if (pTrans->stage == TRN_STAGE_PREPARE) {
510,178✔
2818
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2819
      len = 0;
×
2820
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2821
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2822
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2823
                      sdbStatusName(pAction->pRaw->status));
×
2824
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2825
    }
2826
  }
2827

2828
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
510,178✔
2829
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
15,412,445✔
2830
      len = 0;
14,902,339✔
2831
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
14,902,339✔
2832
      if (pAction->actionType == TRANS_ACTION_MSG) {
14,902,339✔
2833
        char bufStart[40] = {0};
11,338,347✔
2834
        (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
11,338,347✔
2835

2836
        char endStart[40] = {0};
11,338,347✔
2837
        (void)formatTimestamp(endStart, sizeof(endStart), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
11,338,347✔
2838
        len += snprintf(detail + len, sizeof(detail) - len,
22,676,694✔
2839
                        "action:%d, %s:%d msgType:%s,"
2840
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2841
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
22,676,694✔
2842
                        pAction->msgReceived, bufStart, endStart);
11,338,347✔
2843

2844
        SEpSet epset = pAction->epSet;
11,338,347✔
2845
        if (epset.numOfEps > 0) {
11,338,347✔
2846
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
11,338,347✔
2847
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
25,472,813✔
2848
            len +=
14,134,466✔
2849
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
14,134,466✔
2850
          }
2851
        }
2852

2853
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
11,338,347✔
2854
                        tstrerror(pAction->errCode));
2855
      } else {
2856
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
3,563,992✔
2857
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
3,563,992✔
2858
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
3,563,992✔
2859
      }
2860
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
14,902,339✔
2861
    }
2862
  }
2863

2864
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
510,178✔
2865
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
1,440✔
2866
      len = 0;
1,368✔
2867
      STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
1,368✔
2868
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
1,368✔
2869
                      mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type),
1,368✔
2870
                      sdbStatusName(pAction->pRaw->status));
1,368✔
2871
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
1,368✔
2872
    }
2873

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

2891
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
567,015✔
2892
  SMnode *pMnode = pReq->info.node;
567,015✔
2893
  SSdb   *pSdb = pMnode->pSdb;
567,015✔
2894
  int32_t numOfRows = 0;
567,015✔
2895
  STrans *pTrans = NULL;
567,015✔
2896
  int32_t cols = 0;
567,015✔
2897
  int32_t code = 0;
567,015✔
2898
  int32_t lino = 0;
567,015✔
2899

2900
  while (numOfRows < rows) {
1,077,193✔
2901
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
1,077,193✔
2902
    if (pShow->pIter == NULL) break;
1,077,193✔
2903

2904
    cols = 0;
510,178✔
2905

2906
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
510,178✔
2907
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
510,178✔
2908

2909
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
510,178✔
2910
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
510,178✔
2911
                        _OVER);
2912

2913
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
510,178✔
2914
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
510,178✔
2915
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
510,178✔
2916
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
510,178✔
2917

2918
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
510,178✔
2919
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
510,178✔
2920
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
510,178✔
2921
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
510,178✔
2922

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

2928
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
510,178✔
2929
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
510,178✔
2930
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
510,178✔
2931
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
510,178✔
2932

2933
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
510,178✔
2934
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
510,178✔
2935
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
510,178✔
2936
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
510,178✔
2937
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
510,178✔
2938

2939
    /*
2940
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2941
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2942
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2943
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2944
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2945
    */
2946

2947
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
510,178✔
2948
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
510,178✔
2949
                        _OVER);
2950

2951
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
510,178✔
2952
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
510,178✔
2953
                        _OVER);
2954

2955
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
510,178✔
2956
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
510,178✔
2957
    int32_t len = snprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
1,020,356✔
2958
                           pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
1,020,356✔
2959
    SEpSet  epset = pTrans->lastEpset;
510,178✔
2960
    if (epset.numOfEps > 0) {
510,178✔
2961
      len += snprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
1,011,368✔
2962
                      TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
1,011,368✔
2963
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
1,396,522✔
2964
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
890,838✔
2965
      }
2966
    }
2967
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
510,178✔
2968
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
510,178✔
2969
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
510,178✔
2970

2971
    mndTransLogAction(pTrans);
510,178✔
2972

2973
    numOfRows++;
510,178✔
2974
    sdbRelease(pSdb, pTrans);
510,178✔
2975
  }
2976

2977
_OVER:
567,015✔
2978
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
567,015✔
2979
  pShow->numOfRows += numOfRows;
567,015✔
2980
  return numOfRows;
567,015✔
2981
}
2982

2983
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
69,861✔
2984
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2985
  int32_t code = 0;
69,861✔
2986
  int32_t lino = 0;
69,861✔
2987
  int32_t len = 0;
69,861✔
2988

2989
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
69,861✔
2990
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&transactionId, false), &lino, _OVER);
69,861✔
2991

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

3009
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
69,861✔
3010
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
3011
  int32_t code = 0;
69,861✔
3012
  int32_t lino = 0;
69,861✔
3013
  int32_t len = 0;
69,861✔
3014
  int32_t cols = 0;
69,861✔
3015

3016
  cols = 0;
69,861✔
3017

3018
  if (mndShowTransCommonColumns(pShow, pBlock, pAction, transactionId, curActionId, numOfRows, &cols) != 0) return;
69,861✔
3019

3020
  if (pAction->actionType == TRANS_ACTION_MSG) {
69,861✔
3021
    int32_t len = 0;
57,381✔
3022

3023
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
57,381✔
3024
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
57,381✔
3025
                    pAction->msgSent, pAction->msgReceived);
57,381✔
3026
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
57,381✔
3027
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
57,381✔
3028
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
57,381✔
3029
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
57,381✔
3030

3031
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
57,381✔
3032
    len = 0;
57,381✔
3033
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
57,381✔
3034
                    tstrerror(pAction->errCode));
3035
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
57,381✔
3036
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
57,381✔
3037
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
57,381✔
3038
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
57,381✔
3039

3040
    char target[TSDB_TRANS_TARGET_LEN] = {0};
57,381✔
3041
    len = 0;
57,381✔
3042
    SEpSet epset = pAction->epSet;
57,381✔
3043
    if (epset.numOfEps > 0) {
57,381✔
3044
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
133,482✔
3045
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
76,101✔
3046
      }
3047
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
57,381✔
3048
    }
3049
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
57,381✔
3050
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
57,381✔
3051
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
57,381✔
3052
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
57,381✔
3053

3054
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
57,381✔
3055
    len = 0;
57,381✔
3056
    char bufStart[40] = {0};
57,381✔
3057
    if (pAction->startTime > 0)
57,381✔
3058
      (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
15,930✔
3059
    char bufEnd[40] = {0};
57,381✔
3060
    if (pAction->endTime > 0)
57,381✔
3061
      (void)formatTimestamp(bufEnd, sizeof(bufEnd), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
10,749✔
3062

3063
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
57,381✔
3064
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
57,381✔
3065
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
57,381✔
3066
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
57,381✔
3067
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
57,381✔
3068

3069
  } else {
3070
    int32_t len = 0;
12,480✔
3071

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

3086
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
12,480✔
3087
    len = 0;
12,480✔
3088
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
12,480✔
3089
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
12,480✔
3090
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
12,480✔
3091
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,480✔
3092
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
12,480✔
3093

3094
    char target[TSDB_TRANS_TARGET_LEN] = "";
12,480✔
3095
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
12,480✔
3096
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
12,480✔
3097
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,480✔
3098
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
12,480✔
3099

3100
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
12,480✔
3101
    len = 0;
12,480✔
3102
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
12,480✔
3103
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
12,480✔
3104
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
12,480✔
3105
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
12,480✔
3106
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
12,480✔
3107
  }
3108

3109
_OVER:
69,861✔
3110
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
69,861✔
3111
}
3112

3113
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
891✔
3114
  if (stage == TRN_STAGE_PREPARE) {
891✔
3115
    return pTrans->prepareActions;
×
3116
  }
3117
  if (stage == TRN_STAGE_REDO_ACTION) {
891✔
3118
    return pTrans->redoActions;
891✔
3119
  }
3120
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
3121
    return pTrans->commitActions;
×
3122
  }
3123
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
3124
    return pTrans->undoActions;
×
3125
  }
3126
  return NULL;
×
3127
}
3128

3129
typedef struct STransDetailIter {
3130
  void     *pIter;
3131
  STrans   *pTrans;
3132
  ETrnStage stage;
3133
  int32_t   num;
3134
} STransDetailIter;
3135

3136
static void mndTransShowActions(SSdb *pSdb, STransDetailIter *pShowIter, SShowObj *pShow, SSDataBlock *pBlock,
891✔
3137
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3138
  int32_t actionNum = taosArrayGetSize(pActions);
891✔
3139
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
891✔
3140

3141
  for (int32_t i = start; i < actionNum; ++i) {
70,362✔
3142
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
69,861✔
3143
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
69,861✔
3144
    (*numOfRows)++;
69,861✔
3145
    if (*numOfRows >= rows) break;
69,861✔
3146
  }
3147

3148
  if (*numOfRows == end) {
891✔
3149
    sdbRelease(pSdb, pShowIter->pTrans);
501✔
3150
    pShowIter->pTrans = NULL;
501✔
3151
    pShowIter->num = 0;
501✔
3152
  } else {
3153
    pShowIter->pTrans = pShowIter->pTrans;
390✔
3154
    pShowIter->stage = pShowIter->pTrans->stage;
390✔
3155
    pShowIter->num += (*numOfRows);
390✔
3156
  }
3157
}
891✔
3158

3159
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
1,392✔
3160
  SMnode *pMnode = pReq->info.node;
1,392✔
3161
  SSdb   *pSdb = pMnode->pSdb;
1,392✔
3162
  int32_t numOfRows = 0;
1,392✔
3163

3164
  int32_t code = 0;
1,392✔
3165
  int32_t lino = 0;
1,392✔
3166

3167
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
1,392✔
3168
        pShow->pIter);
3169

3170
  if (pShow->pIter == NULL) {
1,392✔
3171
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
501✔
3172
    if (pShow->pIter == NULL) {
501✔
3173
      mError("failed to malloc for pShow->pIter");
×
3174
      return 0;
×
3175
    }
3176
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
501✔
3177
  }
3178

3179
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
1,392✔
3180

3181
  while (numOfRows < rows) {
1,392✔
3182
    if (pShowIter->pTrans == NULL) {
1,392✔
3183
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
1,002✔
3184
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
1,002✔
3185
      if (pShowIter->pIter == NULL) break;
1,002✔
3186
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
501✔
3187
            pShowIter->pTrans->stage, pShowIter->num);
3188

3189
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
501✔
3190

3191
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
501✔
3192
      break;
501✔
3193
    } else {
3194
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
390✔
3195
            pShowIter->num);
3196
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
390✔
3197

3198
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
390✔
3199
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
390✔
3200
      break;
390✔
3201
    }
3202
  }
3203

3204
_OVER:
×
3205
  pShow->numOfRows += numOfRows;
1,392✔
3206

3207
  if (code != 0) {
1,392✔
3208
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
3209
  } else {
3210
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
1,392✔
3211
  }
3212
  if (numOfRows == 0) {
1,392✔
3213
    taosMemoryFree(pShow->pIter);
501✔
3214
    pShow->pIter = NULL;
501✔
3215
  }
3216
  return numOfRows;
1,392✔
3217
}
3218

3219
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
3220
  SSdb *pSdb = pMnode->pSdb;
×
3221
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
3222
}
×
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