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

taosdata / TDengine / #4918

08 Jan 2026 11:50AM UTC coverage: 65.916% (+0.5%) from 65.42%
#4918

push

travis-ci

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

16 of 22 new or added lines in 3 files covered. (72.73%)

788 existing lines in 116 files now uncovered.

204221 of 309822 relevant lines covered (65.92%)

126504701.11 hits per line

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

83.68
/source/dnode/mnode/impl/src/mndTrans.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndTrans.h"
18
#include "mndDb.h"
19
#include "mndPrivilege.h"
20
#include "mndShow.h"
21
#include "mndStb.h"
22
#include "mndSubscribe.h"
23
#include "mndSync.h"
24
#include "mndUser.h"
25
#include "mndVgroup.h"
26
#include "osTime.h"
27
#include "mndToken.h"
28

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

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

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

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

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

63
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
112,697,877✔
64

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

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

81
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
155,540,313✔
82

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

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

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

104
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
388,827✔
105
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
388,827✔
106

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

113
void mndCleanupTrans(SMnode *pMnode) {}
388,769✔
114

115
static int32_t mndTransGetActionsSize(SArray *pArray) {
201,311,676✔
116
  int32_t actionNum = taosArrayGetSize(pArray);
201,311,676✔
117
  int32_t rawDataLen = 0;
201,311,676✔
118

119
  for (int32_t i = 0; i < actionNum; ++i) {
543,215,284✔
120
    STransAction *pAction = taosArrayGet(pArray, i);
341,903,608✔
121
    if (pAction->actionType == TRANS_ACTION_RAW) {
341,903,608✔
122
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
225,615,133✔
123
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
116,288,475✔
124
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
116,288,475✔
125
    } else {
126
      // empty
127
    }
128
    rawDataLen += sizeof(int8_t);
341,903,608✔
129
  }
130

131
  return rawDataLen;
201,311,676✔
132
}
133

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

142
  for (int32_t i = 0; i < actionsNum; ++i) {
543,215,284✔
143
    STransAction *pAction = taosArrayGet(pActions, i);
341,903,608✔
144
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
341,903,608✔
145
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
341,903,608✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
341,903,608✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
341,903,608✔
148
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
341,903,608✔
149
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
341,903,608✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
341,903,608✔
151
    if (sver > TRANS_VER2_NUMBER) {
341,903,608✔
152
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
341,903,608✔
153
    }
154
    if (pAction->actionType == TRANS_ACTION_RAW) {
341,903,608✔
155
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
225,615,133✔
156
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
225,615,133✔
157
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
225,615,133✔
158
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
225,615,133✔
159
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
116,288,475✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
116,288,475✔
161
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
116,288,475✔
162
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
116,288,475✔
163
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
116,288,475✔
164
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
116,288,475✔
165
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
116,288,475✔
166
    } else {
167
      // nothing
168
    }
169
  }
170
  ret = 0;
201,311,676✔
171

172
_OVER:
201,311,676✔
173
  *offset = dataPos;
201,311,676✔
174
  return ret;
201,311,676✔
175
}
176

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

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

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

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

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

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

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

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

233
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
50,327,919✔
234

235
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
50,327,919✔
236
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
50,327,919✔
237
  void *pIter = NULL;
50,327,919✔
238
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
50,327,919✔
239
  while (pIter) {
50,354,697✔
240
    int32_t arbGroupId = *(int32_t *)pIter;
26,778✔
241
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
26,778✔
242
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
26,778✔
243
  }
244

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

250
  if (sver > TRANS_VER2_NUMBER) {
50,327,919✔
251
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
50,327,919✔
252
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
50,327,919✔
253
    void *pIter = NULL;
50,327,919✔
254
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
50,327,919✔
255
    while (pIter) {
52,583,647✔
256
      size_t   keysize = 0;
2,255,728✔
257
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,255,728✔
258
      int32_t  groupPos = *(int32_t *)pIter;
2,255,728✔
259
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,255,728✔
260
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,255,728✔
261
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,255,728✔
262
    }
263
  }
264

265
  if (sver >= TRANS_VER_USER_DATA) {
50,327,919✔
266
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
50,327,919✔
267
    if (pTrans->userDataLen > 0) {
50,327,919✔
268
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
234✔
269
    }
270
  }
271

272
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
50,327,919✔
273
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
50,327,919✔
274

275
  terrno = 0;
50,327,919✔
276

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

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

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

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

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

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

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

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

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

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

449
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
89,699,657✔
450

451
  if (sver > TRANS_VER_CURRENT) {
89,699,657✔
452
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
453
    goto _OVER;
×
454
  }
455

456
  pRow = sdbAllocRow(sizeof(STrans));
89,699,657✔
457
  if (pRow == NULL) goto _OVER;
89,699,657✔
458

459
  pTrans = sdbGetRowObj(pRow);
89,699,657✔
460
  if (pTrans == NULL) goto _OVER;
89,699,657✔
461

462
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
89,699,657✔
463

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

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

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

504
  if (pTrans->prepareActions == NULL) goto _OVER;
89,699,657✔
505
  if (pTrans->redoActions == NULL) goto _OVER;
89,699,657✔
506
  if (pTrans->undoActions == NULL) goto _OVER;
89,699,657✔
507
  if (pTrans->commitActions == NULL) goto _OVER;
89,699,657✔
508

509
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
89,699,657✔
510
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
89,699,657✔
511
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,488,438✔
512
                                      sver) < 0)
513
      goto _OVER;
×
514
  } else {
515
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
88,211,219✔
516
  }
517
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
89,699,657✔
518
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
89,699,657✔
519

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

529
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
89,699,657✔
530

531
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
89,699,657✔
532
  if (arbgroupIdNum > 0) {
89,699,657✔
533
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
37,640✔
534
    if (pTrans->arbGroupIds == NULL) goto _OVER;
37,640✔
535
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
82,270✔
536
      int32_t arbGroupId = 0;
44,630✔
537
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
44,630✔
538
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
44,630✔
539
    }
540
  }
541

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

551
  if (sver > TRANS_VER2_NUMBER) {
89,699,657✔
552
    int32_t groupNum = -1;
89,699,657✔
553
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
89,699,657✔
554
    for (int32_t i = 0; i < groupNum; ++i) {
94,173,066✔
555
      int32_t groupId = -1;
4,473,409✔
556
      int32_t groupPos = -1;
4,473,409✔
557
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
4,473,409✔
558
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
4,473,409✔
559
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
4,473,409✔
560
        goto _OVER;
×
561
    }
562
  }
563

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

573
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
89,699,657✔
574

575
  terrno = 0;
89,699,657✔
576

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

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

592
static const char *mndTransStr(ETrnStage stage) {
636,497,911✔
593
  switch (stage) {
636,497,911✔
594
    case TRN_STAGE_PREPARE:
51,484,323✔
595
      return "prepare";
51,484,323✔
596
    case TRN_STAGE_REDO_ACTION:
228,741,759✔
597
      return "redoAction";
228,741,759✔
598
    case TRN_STAGE_ROLLBACK:
7,090✔
599
      return "rollback";
7,090✔
600
    case TRN_STAGE_UNDO_ACTION:
9,622,124✔
601
      return "undoAction";
9,622,124✔
602
    case TRN_STAGE_COMMIT:
80,445,643✔
603
      return "commit";
80,445,643✔
604
    case TRN_STAGE_COMMIT_ACTION:
146,854,287✔
605
      return "commitAction";
146,854,287✔
606
    case TRN_STAGE_FINISH:
119,335,080✔
607
      return "finished";
119,335,080✔
608
    case TRN_STAGE_PRE_FINISH:
7,605✔
609
      return "pre-finish";
7,605✔
610
    default:
×
611
      return "invalid";
×
612
  }
613
}
614

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

626
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
177,984,073✔
627
  if (pAction != NULL) {
177,984,073✔
628
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
136,942,618✔
629
      pTrans->lastAction = pAction->id;
95,804,057✔
630
      pTrans->lastMsgType = pAction->msgType;
95,804,057✔
631
      pTrans->lastEpset = pAction->epSet;
95,804,057✔
632
      pTrans->lastErrorNo = pAction->errCode;
95,804,057✔
633
    }
634
  } else {
635
    pTrans->lastAction = 0;
41,041,455✔
636
    pTrans->lastMsgType = 0;
41,041,455✔
637
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
41,041,455✔
638
    pTrans->lastErrorNo = 0;
41,041,455✔
639
  }
640
}
177,984,073✔
641

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

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

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

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

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

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

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

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

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

694
  return 0;
16,985,615✔
695
}
696

697
void mndTransDropData(STrans *pTrans) {
105,538,536✔
698
  if (pTrans->prepareActions != NULL) {
105,538,536✔
699
    mndTransDropActions(pTrans->prepareActions);
105,538,536✔
700
    pTrans->prepareActions = NULL;
105,538,536✔
701
  }
702
  if (pTrans->redoActions != NULL) {
105,538,536✔
703
    mndTransDropActions(pTrans->redoActions);
105,538,536✔
704
    pTrans->redoActions = NULL;
105,538,536✔
705
  }
706
  if (pTrans->undoActions != NULL) {
105,538,536✔
707
    mndTransDropActions(pTrans->undoActions);
105,538,536✔
708
    pTrans->undoActions = NULL;
105,538,536✔
709
  }
710
  if (pTrans->commitActions != NULL) {
105,538,536✔
711
    mndTransDropActions(pTrans->commitActions);
105,538,536✔
712
    pTrans->commitActions = NULL;
105,538,536✔
713
  }
714
  if (pTrans->redoGroupActions != NULL) {
105,538,536✔
715
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
17,327,317✔
716
    while (pIter) {
22,021,725✔
717
      SArray **redoActions = pIter;
4,694,408✔
718
      taosArrayDestroy(*redoActions);
4,694,408✔
719
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,694,408✔
720
    }
721

722
    taosHashCleanup(pTrans->redoGroupActions);
17,327,317✔
723
    pTrans->redoGroupActions = NULL;
17,327,317✔
724
  }
725
  if (pTrans->groupActionPos != NULL) {
105,538,536✔
726
    taosHashCleanup(pTrans->groupActionPos);
17,327,317✔
727
    pTrans->groupActionPos = NULL;
17,327,317✔
728
  }
729
  if (pTrans->arbGroupIds != NULL) {
105,538,536✔
730
    taosHashCleanup(pTrans->arbGroupIds);
15,876,519✔
731
  }
732
  if (pTrans->pRpcArray != NULL) {
105,538,536✔
733
    taosArrayDestroy(pTrans->pRpcArray);
15,838,879✔
734
    pTrans->pRpcArray = NULL;
15,838,879✔
735
  }
736
  if (pTrans->rpcRsp != NULL) {
105,538,536✔
737
    taosMemoryFree(pTrans->rpcRsp);
6,618,798✔
738
    pTrans->rpcRsp = NULL;
6,618,798✔
739
    pTrans->rpcRspLen = 0;
6,618,798✔
740
  }
741
  if (pTrans->param != NULL) {
105,538,536✔
742
    taosMemoryFree(pTrans->param);
×
743
    pTrans->param = NULL;
×
744
    pTrans->paramLen = 0;
×
745
  }
746
  if (pTrans->userData != NULL) {
105,538,536✔
747
    taosMemoryFree(pTrans->userData);
390✔
748
    pTrans->userData = NULL;
390✔
749
    pTrans->userDataLen = 0;
390✔
750
  }
751
  (void)taosThreadMutexDestroy(&pTrans->mutex);
105,538,536✔
752
}
105,538,536✔
753

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

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

766
  mndTransDropData(pTrans);
53,341,327✔
767
  return 0;
53,341,327✔
768
}
769

770
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
77,545,952✔
771
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
236,041,760✔
772
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
158,495,808✔
773
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
158,495,808✔
774
    pOldAction->rawWritten = pNewAction->rawWritten;
158,495,808✔
775
    pOldAction->msgSent = pNewAction->msgSent;
158,495,808✔
776
    pOldAction->msgReceived = pNewAction->msgReceived;
158,495,808✔
777
    pOldAction->errCode = pNewAction->errCode;
158,495,808✔
778
  }
779
}
77,545,952✔
780

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

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

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

803
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
19,386,488✔
804
  while (pIter != NULL) {
21,285,415✔
805
    int32_t newActionPos = *(int32_t *)pIter;
1,898,927✔
806

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

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

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

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

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

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

834
  return 0;
19,386,488✔
835
}
836

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

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

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

860
  if (opername != NULL) {
15,838,879✔
861
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
15,838,879✔
862
  }
863

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

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

894
  if (pReq != NULL) {
15,838,879✔
895
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
21,828,978✔
896
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
897
      return NULL;
×
898
    }
899
    pTrans->originRpcType = pReq->msgType;
10,914,489✔
900
  }
901

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

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

908
static void mndTransDropActions(SArray *pArray) {
422,154,144✔
909
  int32_t size = taosArrayGetSize(pArray);
422,154,144✔
910
  for (int32_t i = 0; i < size; ++i) {
1,130,876,072✔
911
    STransAction *pAction = taosArrayGet(pArray, i);
708,721,928✔
912
    if (pAction->actionType == TRANS_ACTION_RAW) {
708,721,928✔
913
      taosMemoryFreeClear(pAction->pRaw);
465,098,391✔
914
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
243,623,537✔
915
      taosMemoryFreeClear(pAction->pCont);
243,623,537✔
916
    } else {
917
      // nothing
918
    }
919
  }
920

921
  taosArrayDestroy(pArray);
422,154,144✔
922
}
422,154,144✔
923

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

932
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
92,215,504✔
933
  pAction->id = taosArrayGetSize(pArray);
92,215,504✔
934

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

940
  return 0;
92,215,504✔
941
}
942

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

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

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

975
  return 0;
503,434✔
976
}
977

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1176
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
5,345,685✔
1177
  if (conflict[0] == 0) return false;
5,345,685✔
1178
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
5,258,987✔
1179
  return false;
5,060,103✔
1180
}
1181

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

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

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

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

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

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

1212
    if (pNew->conflict == TRN_CONFLICT_DB) {
5,392,079✔
1213
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
90,958✔
1214
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
90,958✔
1215
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
86,698✔
1216
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
86,698✔
1217
      }
1218
    }
1219

1220
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
5,392,079✔
1221
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
5,269,379✔
1222
      if (pTrans->conflict == TRN_CONFLICT_DB) {
5,269,379✔
1223
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
75,837✔
1224
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
75,837✔
1225
      }
1226
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
5,269,379✔
1227
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
5,183,150✔
1228
      }
1229
    }
1230

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

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

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

1260
  return conflict;
32,845,862✔
1261
}
1262

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

1273
  if (mndCheckTransConflict(pMnode, pTrans)) {
41,992,052✔
1274
    code = TSDB_CODE_MND_TRANS_CONFLICT;
307,438✔
1275
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
307,438✔
1276
    TAOS_RETURN(code);
307,438✔
1277
  }
1278

1279
  TAOS_RETURN(code);
41,684,614✔
1280
}
1281

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

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

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

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

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

1317
  TAOS_RETURN(code);
140,963✔
1318
}
1319

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

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

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

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

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

1356
  TAOS_RETURN(code);
140,963✔
1357
}
1358

1359
static bool mndTransActionsOfSameType(SArray *pActions) {
34,739,436✔
1360
  int32_t size = taosArrayGetSize(pActions);
34,739,436✔
1361
  ETrnAct lastActType = TRANS_ACTION_NULL;
34,739,436✔
1362
  bool    same = true;
34,739,436✔
1363
  for (int32_t i = 0; i < size; ++i) {
109,437,120✔
1364
    STransAction *pAction = taosArrayGet(pActions, i);
74,697,684✔
1365
    if (i > 0) {
74,697,684✔
1366
      if (lastActType != pAction->actionType) {
49,070,097✔
1367
        same = false;
×
1368
        break;
×
1369
      }
1370
    }
1371
    lastActType = pAction->actionType;
74,697,684✔
1372
  }
1373
  return same;
34,739,436✔
1374
}
1375

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

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

1394
  TAOS_RETURN(code);
15,505,556✔
1395
}
1396

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

1410
  TAOS_RETURN(code);
15,505,556✔
1411
}
1412

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

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

1427
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
33,477,845✔
1428
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
17,971,659✔
1429
    if (pAction->actionType == TRANS_ACTION_MSG) {
17,971,659✔
1430
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
17,397,964✔
1431
            TMSG_INFO(pAction->msgType));
1432
      ;
1433
    } else {
1434
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
573,695✔
1435
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1436
    }
1437
  }
1438

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

1445
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
25,067,336✔
1446
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
9,561,150✔
1447
    if (pAction->actionType == TRANS_ACTION_MSG) {
9,561,150✔
1448
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
6,135,346✔
1449
            TMSG_INFO(pAction->msgType));
1450
    } else {
1451
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
3,425,804✔
1452
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1453
    }
1454
  }
1455

1456
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
15,506,186✔
1457

1458
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
15,505,556✔
1459

1460
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
15,505,556✔
1461

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

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

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

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

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

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

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

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

1532
  if (pTrans->stage == TRN_STAGE_FINISH) {
72,654,583✔
1533
    sendRsp = true;
32,473,168✔
1534
  }
1535

1536
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
72,654,583✔
1537
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
16,906,772✔
1538
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
8,508✔
1539
      sendRsp = true;
8,508✔
1540
    }
1541
  } else {
1542
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
55,747,811✔
1543
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
31,854,192✔
1544
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1545
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1546
      } else {
1547
        if (pTrans->failedTimes > 6) sendRsp = true;
31,854,192✔
1548
      }
1549
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
31,854,192✔
1550
    }
1551
  }
1552

1553
  if (!sendRsp) {
72,654,583✔
1554
    return;
40,172,907✔
1555
  } else {
1556
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
32,481,676✔
1557
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1558
  }
1559

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

1568
  for (int32_t i = 0; i < size; ++i) {
21,212,960✔
1569
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
10,606,480✔
1570
    if (pInfo->handle != NULL) {
10,606,480✔
1571
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
10,226,653✔
1572
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1573
      }
1574
      if (code == TSDB_CODE_SYN_TIMEOUT) {
10,226,653✔
1575
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1576
      }
1577

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

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

1586
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
10,226,653✔
1587
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,051,575✔
1588
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,051,575✔
1589
        if (pDb != NULL) {
1,051,575✔
1590
          for (int32_t j = 0; j < 12; j++) {
1,374,358✔
1591
            bool ready = mndIsDbReady(pMnode, pDb);
1,374,030✔
1592
            if (!ready) {
1,374,030✔
1593
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
322,783✔
1594
              taosMsleep(1000);
322,783✔
1595
            } else {
1596
              break;
1,051,247✔
1597
            }
1598
          }
1599
        }
1600
        mndReleaseDb(pMnode, pDb);
1,051,575✔
1601
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
9,175,078✔
1602
        void   *pCont = NULL;
1,572,441✔
1603
        int32_t contLen = 0;
1,572,441✔
1604
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
1,572,441✔
1605
          mndTransSetRpcRsp(pTrans, pCont, contLen);
1,571,023✔
1606
        }
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
7,602,637✔
1608
        void   *pCont = NULL;
2,355✔
1609
        int32_t contLen = 0;
2,355✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,355✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,355✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
7,600,282✔
1614
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
8,247✔
1615
        if (code != 0) {
8,247✔
1616
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1617
        }
1618
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
7,592,035✔
1619
        void   *pCont = NULL;
39✔
1620
        int32_t contLen = 0;
39✔
1621
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
39✔
1622
          mndTransSetRpcRsp(pTrans, pCont, contLen);
39✔
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
7,591,996✔
1625
        void   *pCont = NULL;
39✔
1626
        int32_t contLen = 0;
39✔
1627
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
39✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
39✔
1629
        }
1630
      }
1631

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

1641
      tmsgSendRsp(&rspMsg);
10,226,653✔
1642

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1760
// execute in sync context
1761
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
125,613,993✔
1762
  if (pAction->rawWritten) return 0;
125,613,993✔
1763
  if (topHalf) {
69,492,071✔
1764
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
610✔
1765
  }
1766

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

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

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

1791
  TAOS_RETURN(code);
69,491,461✔
1792
}
1793

1794
// execute in trans context
1795
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
82,362,773✔
1796
                                     bool notSend) {
1797
  if (pAction->msgSent) return 0;
82,362,773✔
1798
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
31,590,682✔
1799
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
12,240,047✔
1800
  }
1801

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

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

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

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

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

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

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

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

1860
  TAOS_RETURN(code);
19,341,418✔
1861
}
1862

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

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

1873
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
207,976,766✔
1874
                                        bool notSend) {
1875
  if (pAction->actionType == TRANS_ACTION_RAW) {
207,976,766✔
1876
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
125,613,993✔
1877
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
82,362,773✔
1878
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
82,362,773✔
1879
  } else {
1880
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1881
  }
1882
}
1883

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

1888
  for (int32_t action = 0; action < numOfActions; ++action) {
241,994,400✔
1889
    STransAction *pAction = taosArrayGet(pArray, action);
185,073,930✔
1890
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
185,073,930✔
1891
    if (code != 0) {
185,073,930✔
1892
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
9,577,851✔
1893
            numOfActions);
1894
      break;
9,577,851✔
1895
    }
1896
  }
1897

1898
  return code;
66,498,321✔
1899
}
1900

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

1906
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
66,498,321✔
1907
    return code;
9,577,851✔
1908
  }
1909

1910
  int32_t       numOfExecuted = 0;
56,920,470✔
1911
  int32_t       errCode = 0;
56,920,470✔
1912
  STransAction *pErrAction = NULL;
56,920,470✔
1913
  for (int32_t action = 0; action < numOfActions; ++action) {
232,416,549✔
1914
    STransAction *pAction = taosArrayGet(pArray, action);
175,496,079✔
1915
    if (pAction->msgReceived || pAction->rawWritten) {
175,496,079✔
1916
      numOfExecuted++;
144,661,777✔
1917
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
144,661,777✔
1918
        errCode = pAction->errCode;
2,836✔
1919
        pErrAction = pAction;
2,836✔
1920
      }
1921
    } else {
1922
      pErrAction = pAction;
30,834,302✔
1923
    }
1924
  }
1925

1926
  mndSetTransLastAction(pTrans, pErrAction);
56,920,470✔
1927

1928
  if (numOfExecuted == numOfActions) {
56,920,470✔
1929
    if (errCode == 0) {
41,042,873✔
1930
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
41,041,455✔
1931
      return 0;
41,041,455✔
1932
    } else {
1933
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,418✔
1934
      mndTransResetActions(pMnode, pTrans, pArray);
1,418✔
1935
      terrno = errCode;
1,418✔
1936
      return errCode;
1,418✔
1937
    }
1938
  } else {
1939
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
15,877,597✔
1940

1941
    for (int32_t action = 0; action < numOfActions; ++action) {
61,342,482✔
1942
      STransAction *pAction = taosArrayGet(pArray, action);
45,464,885✔
1943
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
45,464,885✔
1944
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1945
             pAction->acceptableCode, pAction->retryCode);
1946
      if (pAction->msgSent) {
45,464,885✔
1947
        bool reset = false;
45,455,668✔
1948
        if (pAction->msgReceived) {
45,455,668✔
1949
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
14,630,583✔
1950
        } else {
1951
          int64_t timestamp = taosGetTimestampMs();
30,825,085✔
1952
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
30,825,085✔
1953
        }
1954
        if (reset) {
45,455,668✔
1955
          mndTransResetAction(pMnode, pTrans, pAction);
1,418✔
1956
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1,418✔
1957
                pAction->id, pAction->errCode, pAction->startTime);
1958
        }
1959
      }
1960
    }
1961
    return TSDB_CODE_ACTION_IN_PROGRESS;
15,877,597✔
1962
  }
1963
}
1964

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

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

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

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

1997
  if (pTrans->actionPos >= numOfActions) {
6,616,708✔
1998
    return code;
246,483✔
1999
  }
2000

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

2004
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
10,131,435✔
2005
    STransAction *pAction = taosArrayGet(pActions, action);
9,914,986✔
2006

2007
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
9,914,986✔
2008
      code = TSDB_CODE_ACTION_IN_PROGRESS;
7,008✔
2009
      break;
7,092✔
2010
    }
2011

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

2015
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
9,907,978✔
2016
    if (code == 0) {
9,907,978✔
2017
      if (pAction->msgSent) {
8,479,657✔
2018
        bool reset = false;
7,515,379✔
2019
        if (pAction->msgReceived) {
7,515,379✔
2020
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,294,967✔
2021
            code = pAction->errCode;
1,941,195✔
2022
            reset = true;
1,941,195✔
2023
          } else {
2024
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,353,772✔
2025
          }
2026
        } else {
2027
          int64_t timestamp = taosGetTimestampMs();
4,220,412✔
2028
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
4,220,412✔
2029
          code = TSDB_CODE_ACTION_IN_PROGRESS;
4,220,412✔
2030
        }
2031
        if (reset) {
7,515,379✔
2032
          mndTransResetAction(pMnode, pTrans, pAction);
1,941,195✔
2033
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1,941,195✔
2034
                pAction->id, pAction->errCode, pAction->startTime);
2035
        }
2036
      } else if (pAction->rawWritten) {
964,278✔
2037
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
964,278✔
2038
          code = pAction->errCode;
×
2039
        } else {
2040
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
964,278✔
2041
        }
2042
      } else {
2043
      }
2044
    }
2045

2046
    if (code == 0) {
9,907,978✔
2047
      pTrans->failedTimes = 0;
2,318,050✔
2048
    }
2049
    mndSetTransLastAction(pTrans, pAction);
9,907,978✔
2050
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
9,907,978✔
2051
      mInfo(
5,648,733✔
2052
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2053
          "msgReceived:%d",
2054
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2055
          pAction->msgSent, pAction->msgReceived);
2056
    } else if (code != 0) {
4,259,245✔
2057
      mError(
1,941,195✔
2058
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2059
          "msgReceived:%d",
2060
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2061
          pAction->msgSent, pAction->msgReceived);
2062
    }
2063

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

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

2109
  return code;
6,370,225✔
2110
}
2111

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

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

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

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

2136
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
3,655,863✔
2137
    STransAction **ppAction = taosArrayGet(pActions, action);
3,438,158✔
2138
    STransAction  *pAction = *ppAction;
3,438,158✔
2139

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

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

2156
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
2,985,551✔
2157
    if (code == 0) {
2,985,551✔
2158
      if (pAction->msgSent) {
1,750,751✔
2159
        if (pAction->msgReceived) {
1,557,675✔
2160
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
450,458✔
2161
            code = pAction->errCode;
25,579✔
2162
            mndTransResetAction(pMnode, pTrans, pAction);
25,579✔
2163
          } else {
2164
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
424,879✔
2165
                  pAction->id, action, numOfActions, groupId);
2166
          }
2167
        } else {
2168
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,107,217✔
2169
        }
2170
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,557,675✔
2171
        if (msgSent != NULL) {
1,557,675✔
2172
          *msgSent = pAction->msgSent;
1,557,675✔
2173
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,557,675✔
2174
        } else {
2175
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2176
        }
2177
      } else if (pAction->rawWritten) {
193,076✔
2178
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
193,076✔
2179
          code = pAction->errCode;
×
2180
        } else {
2181
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
193,076✔
2182
        }
2183
      } else {
2184
      }
2185
    }
2186

2187
    if (code == 0) {
2,985,551✔
2188
      pTrans->failedTimes = 0;
617,955✔
2189
    }
2190
    mndSetTransLastAction(pTrans, pAction);
2,985,551✔
2191

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

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

2262
  return code;
3,149,450✔
2263
}
2264

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

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

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

2293
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,179,975✔
2294
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,179,975✔
2295
  int32_t currentGroup = 1;
1,179,975✔
2296
  while (pIter) {
4,950,711✔
2297
    SArray **redoActions = pIter;
3,770,736✔
2298
    size_t   keyLen = 0;
3,770,736✔
2299
    int32_t *key = taosHashGetKey(pIter, &keyLen);
3,770,736✔
2300
    int32_t  actionCount = taosArrayGetSize(*redoActions);
3,770,736✔
2301
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
3,770,736✔
2302
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2303
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
3,770,736✔
2304
                                             notSend, pHash);
2305
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
3,770,736✔
2306
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,234,485✔
2307
            tstrerror(code));
2308
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,536,251✔
2309
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,559,824✔
2310
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
976,427✔
2311
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
621,286✔
2312
    } else if (code != 0) {
355,141✔
2313
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
469✔
2314
             tstrerror(code));
2315
    } else {
2316
      successCount++;
354,672✔
2317
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
354,672✔
2318
    }
2319
    currentGroup++;
3,770,736✔
2320
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
3,770,736✔
2321
  }
2322

2323
  taosHashCleanup(pHash);
1,179,975✔
2324

2325
  if (successCount == groupCount) {
1,179,975✔
2326
    total_code = 0;
23,712✔
2327
  } else {
2328
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,156,263✔
2329
  }
2330

2331
  return total_code;
1,179,975✔
2332
}
2333

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

2338
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,377,900✔
2339
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,377,900✔
2340
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,377,900✔
2341
      code = 0;
171,613✔
2342
    } else {
2343
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,206,287✔
2344
      if (pAction != NULL && pAction->groupId == -1) {
1,206,287✔
2345
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
26,312✔
2346
      } else {
2347
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,179,975✔
2348
        if (code == 0) {
1,179,975✔
2349
          if (pTrans->actionPos < numOfActions) {
23,712✔
2350
            code = TSDB_CODE_ACTION_IN_PROGRESS;
18,085✔
2351
          }
2352
        }
2353
      }
2354
    }
2355
  }
2356

2357
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,377,900✔
2358

2359
  return code;
1,377,900✔
2360
}
2361

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

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

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

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

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

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

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

2404
  if (pTrans->exec == TRN_EXEC_SERIAL) {
55,676,748✔
2405
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
6,591,332✔
2406
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
49,085,416✔
2407
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
47,707,516✔
2408
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,377,900✔
2409
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,377,900✔
2410
  } else {
2411
    code = TSDB_CODE_INTERNAL_ERROR;
×
2412
  }
2413

2414
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
55,678,166✔
2415
      mndTransIsInSyncContext(topHalf)) {
1,418✔
2416
    pTrans->lastErrorNo = code;
×
2417
    pTrans->code = code;
×
2418
    mInfo(
×
2419
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2420
        "continueExec:%d, code:%s",
2421
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2422
    taosMsleep(100);
×
2423
    return true;
×
2424
  } else {
2425
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
55,676,748✔
2426
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
19,402,869✔
2427
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2428
      return false;
19,402,869✔
2429
    }
2430
  }
2431
  terrno = code;
36,273,879✔
2432

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

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

2469
  return continueExec;
36,273,879✔
2470
}
2471

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

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

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

2491
  return continueExec;
15,503,467✔
2492
}
2493

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

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

2515
  return continueExec;
32,469,952✔
2516
}
2517

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

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

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

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

2544
  return continueExec;
8,508✔
2545
}
2546

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

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

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

2563
  return continueExec;
1,418✔
2564
}
2565

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

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

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

2583
  return continueExec;
1,521✔
2584
}
2585

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

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

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

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

2607
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
72,654,583✔
2608
  bool continueExec = true;
72,654,583✔
2609

2610
  while (continueExec) {
208,790,609✔
2611
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
136,136,026✔
2612
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2613
    pTrans->lastExecTime = taosGetTimestampMs();
136,136,026✔
2614
    switch (pTrans->stage) {
136,136,026✔
2615
      case TRN_STAGE_PREPARE:
×
2616
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2617
        break;
×
2618
      case TRN_STAGE_REDO_ACTION:
55,676,748✔
2619
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
55,676,748✔
2620
        break;
55,676,748✔
2621
      case TRN_STAGE_COMMIT:
15,503,467✔
2622
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
15,503,467✔
2623
        break;
15,503,467✔
2624
      case TRN_STAGE_COMMIT_ACTION:
32,469,952✔
2625
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
32,469,952✔
2626
        break;
32,469,952✔
2627
      case TRN_STAGE_ROLLBACK:
1,418✔
2628
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,418✔
2629
        break;
1,418✔
2630
      case TRN_STAGE_UNDO_ACTION:
9,926✔
2631
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
9,926✔
2632
        break;
9,926✔
2633
      case TRN_STAGE_PRE_FINISH:
1,521✔
2634
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,521✔
2635
        break;
1,521✔
2636
      case TRN_STAGE_FINISH:
32,472,994✔
2637
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
32,472,994✔
2638
        break;
32,472,994✔
2639
      default:
×
2640
        continueExec = false;
×
2641
        break;
×
2642
    }
2643
  }
2644

2645
  mndTransSendRpcRsp(pMnode, pTrans);
72,654,583✔
2646
}
72,654,583✔
2647

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2745
  void *pIter = NULL;
12,330,162✔
2746
  while (1) {
1,453,399✔
2747
    STrans *pTrans = NULL;
13,783,561✔
2748
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
13,783,561✔
2749
    if (pIter == NULL) break;
13,783,561✔
2750
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
2,906,798✔
2751
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2752
    }
2753
    sdbRelease(pSdb, pTrans);
1,453,399✔
2754
  }
2755

2756
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
12,330,162✔
2757

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

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

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

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

2796
  return buf;
16,655,265✔
2797
}
2798

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

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

2815
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
351,370✔
2816
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
11,273,713✔
2817
      len = 0;
10,922,343✔
2818
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
10,922,343✔
2819
      if (pAction->actionType == TRANS_ACTION_MSG) {
10,922,343✔
2820
        char bufStart[40] = {0};
8,313,821✔
2821
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
8,313,821✔
2822

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

2831
        SEpSet epset = pAction->epSet;
8,313,821✔
2832
        if (epset.numOfEps > 0) {
8,313,821✔
2833
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
8,313,821✔
2834
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
18,659,342✔
2835
            len +=
10,345,521✔
2836
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
10,345,521✔
2837
          }
2838
        }
2839

2840
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
8,313,821✔
2841
                        tstrerror(pAction->errCode));
2842
      } else {
2843
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
2,608,522✔
2844
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
2,608,522✔
2845
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
2,608,522✔
2846
      }
2847
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
10,922,343✔
2848
    }
2849
  }
2850

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

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

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

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

2891
    cols = 0;
351,370✔
2892

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

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

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

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

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

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

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

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

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

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

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

2958
    mndTransLogAction(pTrans);
351,370✔
2959

2960
    numOfRows++;
351,370✔
2961
    sdbRelease(pSdb, pTrans);
351,370✔
2962
  }
2963

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

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

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

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

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

3003
  cols = 0;
55,341✔
3004

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc