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

taosdata / TDengine / #5048

10 May 2026 03:11AM UTC coverage: 73.222% (+0.07%) from 73.152%
#5048

push

travis-ci

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

353 of 452 new or added lines in 9 files covered. (78.1%)

587 existing lines in 140 files now uncovered.

278189 of 379928 relevant lines covered (73.22%)

135206397.85 hits per line

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

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

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

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

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

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

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

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

65
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
148,140,707✔
66

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

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

83
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
212,450,878✔
84

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

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

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

106
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
516,027✔
107
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
516,027✔
108

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

115
void mndCleanupTrans(SMnode *pMnode) {}
515,959✔
116

117
static int32_t mndTransGetActionsSize(SArray *pArray) {
276,808,112✔
118
  int32_t actionNum = taosArrayGetSize(pArray);
276,808,112✔
119
  int32_t rawDataLen = 0;
276,808,112✔
120

121
  for (int32_t i = 0; i < actionNum; ++i) {
733,439,056✔
122
    STransAction *pAction = taosArrayGet(pArray, i);
456,630,944✔
123
    if (pAction->actionType == TRANS_ACTION_RAW) {
456,630,944✔
124
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
309,855,446✔
125
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
146,775,498✔
126
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
146,775,498✔
127
    } else {
128
      // empty
129
    }
130
    rawDataLen += sizeof(int8_t);
456,630,944✔
131
  }
132

133
  return rawDataLen;
276,808,112✔
134
}
135

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

144
  for (int32_t i = 0; i < actionsNum; ++i) {
733,439,056✔
145
    STransAction *pAction = taosArrayGet(pActions, i);
456,630,944✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
456,630,944✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
456,630,944✔
148
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
456,630,944✔
149
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
456,630,944✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
456,630,944✔
151
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
456,630,944✔
152
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
456,630,944✔
153
    if (sver > TRANS_VER2_NUMBER) {
456,630,944✔
154
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
456,630,944✔
155
    }
156
    if (pAction->actionType == TRANS_ACTION_RAW) {
456,630,944✔
157
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
309,855,446✔
158
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
309,855,446✔
159
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
309,855,446✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
309,855,446✔
161
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
146,775,498✔
162
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
146,775,498✔
163
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
146,775,498✔
164
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
146,775,498✔
165
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
146,775,498✔
166
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
146,775,498✔
167
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
146,775,498✔
168
    } else {
169
      // nothing
170
    }
171
  }
172
  ret = 0;
276,808,112✔
173

174
_OVER:
276,808,112✔
175
  *offset = dataPos;
276,808,112✔
176
  return ret;
276,808,112✔
177
}
178

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

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

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

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

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

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

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

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

235
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
69,202,028✔
236

237
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
69,202,028✔
238
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
69,202,028✔
239
  void *pIter = NULL;
69,202,028✔
240
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
69,202,028✔
241
  while (pIter) {
69,231,212✔
242
    int32_t arbGroupId = *(int32_t *)pIter;
29,184✔
243
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
29,184✔
244
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
29,184✔
245
  }
246

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

252
  if (sver > TRANS_VER2_NUMBER) {
69,202,028✔
253
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
69,202,028✔
254
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
69,202,028✔
255
    void *pIter = NULL;
69,202,028✔
256
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
69,202,028✔
257
    while (pIter) {
71,992,542✔
258
      size_t   keysize = 0;
2,790,514✔
259
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,790,514✔
260
      int32_t  groupPos = *(int32_t *)pIter;
2,790,514✔
261
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,790,514✔
262
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,790,514✔
263
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,790,514✔
264
    }
265
  }
266

267
  if (sver >= TRANS_VER_USER_DATA) {
69,202,028✔
268
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
69,202,028✔
269
    if (pTrans->userDataLen > 0) {
69,202,028✔
270
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
124,450✔
271
    }
272
  }
273

274
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
69,202,028✔
275
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
69,202,028✔
276

277
  terrno = 0;
69,202,028✔
278

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

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

291
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
31,363,920✔
292
  if (pAction->groupId < 0) return 0;
31,363,920✔
293
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
30,737,496✔
294
  if (redoAction == NULL) {
30,737,496✔
295
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
5,536,054✔
296
    if (array != NULL) {
5,536,054✔
297
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
5,536,054✔
298
        mInfo("failed put action into redo group actions");
×
299
        return TSDB_CODE_INTERNAL_ERROR;
×
300
      }
301
    }
302
    redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
5,536,054✔
303
  }
304
  if (redoAction != NULL) {
30,737,496✔
305
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
61,474,992✔
306
  }
307
  return 0;
30,737,496✔
308
}
309

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

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

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

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

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

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

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

451
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
123,321,300✔
452

453
  if (sver > TRANS_VER_CURRENT) {
123,321,300✔
454
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
455
    goto _OVER;
×
456
  }
457

458
  pRow = sdbAllocRow(sizeof(STrans));
123,321,300✔
459
  if (pRow == NULL) goto _OVER;
123,321,300✔
460

461
  pTrans = sdbGetRowObj(pRow);
123,321,300✔
462
  if (pTrans == NULL) goto _OVER;
123,321,300✔
463

464
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
123,321,300✔
465

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

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

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

506
  if (pTrans->prepareActions == NULL) goto _OVER;
123,321,300✔
507
  if (pTrans->redoActions == NULL) goto _OVER;
123,321,300✔
508
  if (pTrans->undoActions == NULL) goto _OVER;
123,321,300✔
509
  if (pTrans->commitActions == NULL) goto _OVER;
123,321,300✔
510

511
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
123,321,300✔
512
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
123,321,300✔
513
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,803,810✔
514
                                      sver) < 0)
515
      goto _OVER;
×
516
  } else {
517
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
121,517,490✔
518
  }
519
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
123,321,300✔
520
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
123,321,300✔
521

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

531
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
123,321,300✔
532

533
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
123,321,300✔
534
  if (arbgroupIdNum > 0) {
123,321,300✔
535
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
40,590✔
536
    if (pTrans->arbGroupIds == NULL) goto _OVER;
40,590✔
537
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
89,230✔
538
      int32_t arbGroupId = 0;
48,640✔
539
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
48,640✔
540
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
48,640✔
541
    }
542
  }
543

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

553
  if (sver > TRANS_VER2_NUMBER) {
123,321,300✔
554
    int32_t groupNum = -1;
123,321,300✔
555
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
123,321,300✔
556
    for (int32_t i = 0; i < groupNum; ++i) {
128,857,354✔
557
      int32_t groupId = -1;
5,536,054✔
558
      int32_t groupPos = -1;
5,536,054✔
559
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
5,536,054✔
560
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
5,536,054✔
561
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
5,536,054✔
562
        goto _OVER;
×
563
    }
564
  }
565

566
  if (sver >= TRANS_VER_USER_DATA) {
123,321,300✔
567
    SDB_GET_INT32(pRaw, dataPos, &pTrans->userDataLen, _OVER)
123,321,300✔
568
    if (pTrans->userDataLen > 0) {
123,321,300✔
569
      pTrans->userData = taosMemoryMalloc(pTrans->userDataLen);
207,960✔
570
      if (pTrans->userData == NULL) goto _OVER;
207,960✔
571
      SDB_GET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
207,960✔
572
    }
573
  }
574

575
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
123,321,300✔
576

577
  terrno = 0;
123,321,300✔
578

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

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

594
static const char *mndTransStr(ETrnStage stage) {
860,526,537✔
595
  switch (stage) {
860,526,537✔
596
    case TRN_STAGE_PREPARE:
69,219,926✔
597
      return "prepare";
69,219,926✔
598
    case TRN_STAGE_REDO_ACTION:
296,840,502✔
599
      return "redoAction";
296,840,502✔
600
    case TRN_STAGE_ROLLBACK:
8,320✔
601
      return "rollback";
8,320✔
602
    case TRN_STAGE_UNDO_ACTION:
13,337,790✔
603
      return "undoAction";
13,337,790✔
604
    case TRN_STAGE_COMMIT:
111,132,538✔
605
      return "commit";
111,132,538✔
606
    case TRN_STAGE_COMMIT_ACTION:
205,566,433✔
607
      return "commitAction";
205,566,433✔
608
    case TRN_STAGE_FINISH:
164,412,113✔
609
      return "finished";
164,412,113✔
610
    case TRN_STAGE_PRE_FINISH:
8,915✔
611
      return "pre-finish";
8,915✔
612
    default:
×
613
      return "invalid";
×
614
  }
615
}
616

617
static const char *mndTransTypeStr(ETrnAct actionType) {
72,765✔
618
  switch (actionType) {
72,765✔
619
    case TRANS_ACTION_MSG:
59,773✔
620
      return "msg";
59,773✔
621
    case TRANS_ACTION_RAW:
12,992✔
622
      return "sdb";
12,992✔
623
    default:
×
624
      return "invalid";
×
625
  }
626
}
627

628
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
238,030,047✔
629
  if (pAction != NULL) {
238,030,047✔
630
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
182,331,953✔
631
      pTrans->lastAction = pAction->id;
129,519,742✔
632
      pTrans->lastMsgType = pAction->msgType;
129,519,742✔
633
      pTrans->lastEpset = pAction->epSet;
129,519,742✔
634
      pTrans->lastErrorNo = pAction->errCode;
129,519,742✔
635
    }
636
  } else {
637
    pTrans->lastAction = 0;
55,698,094✔
638
    pTrans->lastMsgType = 0;
55,698,094✔
639
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
55,698,094✔
640
    pTrans->lastErrorNo = 0;
55,698,094✔
641
  }
642
}
238,030,047✔
643

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

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

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

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

675
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
23,471,500✔
676

677
  if (pTrans->startFunc > 0) {
23,471,500✔
678
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
329,806✔
679
    if (fp) {
329,806✔
680
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
329,806✔
681
    }
682
    // pTrans->startFunc = 0;
683
  }
684

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

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

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

700
  return 0;
23,471,500✔
701
}
702

703
void mndTransDropData(STrans *pTrans) {
145,040,338✔
704
  if (pTrans->prepareActions != NULL) {
145,040,338✔
705
    mndTransDropActions(pTrans->prepareActions);
145,040,338✔
706
    pTrans->prepareActions = NULL;
145,040,338✔
707
  }
708
  if (pTrans->redoActions != NULL) {
145,040,338✔
709
    mndTransDropActions(pTrans->redoActions);
145,040,338✔
710
    pTrans->redoActions = NULL;
145,040,338✔
711
  }
712
  if (pTrans->undoActions != NULL) {
145,040,338✔
713
    mndTransDropActions(pTrans->undoActions);
145,040,338✔
714
    pTrans->undoActions = NULL;
145,040,338✔
715
  }
716
  if (pTrans->commitActions != NULL) {
145,040,338✔
717
    mndTransDropActions(pTrans->commitActions);
145,040,338✔
718
    pTrans->commitActions = NULL;
145,040,338✔
719
  }
720
  if (pTrans->redoGroupActions != NULL) {
145,040,338✔
721
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
23,522,848✔
722
    while (pIter) {
29,328,452✔
723
      SArray **redoActions = pIter;
5,805,604✔
724
      taosArrayDestroy(*redoActions);
5,805,604✔
725
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
5,805,604✔
726
    }
727

728
    taosHashCleanup(pTrans->redoGroupActions);
23,522,848✔
729
    pTrans->redoGroupActions = NULL;
23,522,848✔
730
  }
731
  if (pTrans->groupActionPos != NULL) {
145,040,338✔
732
    taosHashCleanup(pTrans->groupActionPos);
23,522,848✔
733
    pTrans->groupActionPos = NULL;
23,522,848✔
734
  }
735
  if (pTrans->arbGroupIds != NULL) {
145,040,338✔
736
    taosHashCleanup(pTrans->arbGroupIds);
21,759,628✔
737
  }
738
  if (pTrans->pRpcArray != NULL) {
145,040,338✔
739
    taosArrayDestroy(pTrans->pRpcArray);
21,719,038✔
740
    pTrans->pRpcArray = NULL;
21,719,038✔
741
  }
742
  if (pTrans->rpcRsp != NULL) {
145,040,338✔
743
    taosMemoryFree(pTrans->rpcRsp);
8,321,115✔
744
    pTrans->rpcRsp = NULL;
8,321,115✔
745
    pTrans->rpcRspLen = 0;
8,321,115✔
746
  }
747
  if (pTrans->param != NULL) {
145,040,338✔
748
    taosMemoryFree(pTrans->param);
×
749
    pTrans->param = NULL;
×
750
    pTrans->paramLen = 0;
×
751
  }
752
  if (pTrans->userData != NULL) {
145,040,338✔
753
    taosMemoryFree(pTrans->userData);
208,108✔
754
    pTrans->userData = NULL;
208,108✔
755
    pTrans->userDataLen = 0;
208,108✔
756
  }
757
  (void)taosThreadMutexDestroy(&pTrans->mutex);
145,040,338✔
758
}
145,040,338✔
759

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

764
  if (pTrans->stopFunc > 0 && callFunc) {
73,393,482✔
765
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
329,994✔
766
    if (fp) {
329,994✔
767
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
329,994✔
768
    }
769
    // pTrans->stopFunc = 0;
770
  }
771

772
  mndTransDropData(pTrans);
73,393,482✔
773
  return 0;
73,393,482✔
774
}
775

776
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
105,906,232✔
777
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
311,963,059✔
778
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
206,056,827✔
779
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
206,056,827✔
780
    pOldAction->rawWritten = pNewAction->rawWritten;
206,056,827✔
781
    pOldAction->msgSent = pNewAction->msgSent;
206,056,827✔
782
    pOldAction->msgReceived = pNewAction->msgReceived;
206,056,827✔
783
    pOldAction->errCode = pNewAction->errCode;
206,056,827✔
784
  }
785
}
105,906,232✔
786

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

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

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

809
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
26,476,558✔
810
  while (pIter != NULL) {
28,830,908✔
811
    int32_t newActionPos = *(int32_t *)pIter;
2,354,350✔
812

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

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

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

825
  if (pOld->stage == TRN_STAGE_COMMIT) {
26,476,558✔
826
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
23,443,306✔
827
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
23,443,306✔
828
  }
829

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

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

840
  return 0;
26,476,558✔
841
}
842

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

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

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

866
  if (opername != NULL) {
21,719,038✔
867
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
21,719,038✔
868
  }
869

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

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

900
  if (pReq != NULL) {
21,719,038✔
901
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
29,569,428✔
902
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
903
      return NULL;
×
904
    }
905
    pTrans->originRpcType = pReq->msgType;
14,784,714✔
906
  }
907

908
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
21,719,038✔
909

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

914
static void mndTransDropActions(SArray *pArray) {
580,161,352✔
915
  int32_t size = taosArrayGetSize(pArray);
580,161,352✔
916
  for (int32_t i = 0; i < size; ++i) {
1,527,832,030✔
917
    STransAction *pAction = taosArrayGet(pArray, i);
947,670,678✔
918
    if (pAction->actionType == TRANS_ACTION_RAW) {
947,670,678✔
919
      taosMemoryFreeClear(pAction->pRaw);
639,399,900✔
920
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
308,270,778✔
921
      taosMemoryFreeClear(pAction->pCont);
308,270,778✔
922
    } else {
923
      // nothing
924
    }
925
  }
926

927
  taosArrayDestroy(pArray);
580,161,352✔
928
}
580,161,352✔
929

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

938
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
125,969,514✔
939
  pAction->id = taosArrayGetSize(pArray);
125,969,514✔
940

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

946
  return 0;
125,969,514✔
947
}
948

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

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

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

981
  return 0;
609,850✔
982
}
983

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1101
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
11,986,856✔
1102
  if (dbname != NULL) {
11,986,856✔
1103
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
11,986,856✔
1104
  }
1105
  if (stbname != NULL) {
11,986,856✔
1106
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
9,058,689✔
1107
  }
1108
}
11,986,856✔
1109

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

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

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

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

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

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

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

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

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

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

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

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

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

1182
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
6,312,300✔
1183
  if (conflict[0] == 0) return false;
6,312,300✔
1184
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
6,093,111✔
1185
  return false;
5,932,090✔
1186
}
1187

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

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

1207
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
56,963,170✔
1208

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

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

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

1218
    if (pNew->conflict == TRN_CONFLICT_DB) {
6,405,684✔
1219
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
247,127✔
1220
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
247,127✔
1221
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
219,189✔
1222
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
219,189✔
1223
      }
1224
    }
1225

1226
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
6,405,684✔
1227
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
6,115,456✔
1228
      if (pTrans->conflict == TRN_CONFLICT_DB) {
6,115,456✔
1229
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
70,233✔
1230
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
70,233✔
1231
      }
1232
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
6,115,456✔
1233
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
6,022,878✔
1234
      }
1235
    }
1236

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

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

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

1266
  return conflict;
43,123,028✔
1267
}
1268

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

1279
  if (mndCheckTransConflict(pMnode, pTrans)) {
56,963,170✔
1280
    code = TSDB_CODE_MND_TRANS_CONFLICT;
271,461✔
1281
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
271,461✔
1282
    TAOS_RETURN(code);
271,461✔
1283
  }
1284

1285
  TAOS_RETURN(code);
56,691,709✔
1286
}
1287

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

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

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

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

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

1323
  TAOS_RETURN(code);
165,576✔
1324
}
1325

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

1333
  while ((pIter = sdbFetch(pSdb, SDB_RETENTION, pIter, (void **)&pRetention)) != NULL) {
165,576✔
UNCOV
1334
    conflict = false;
×
1335

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

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

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

1362
  TAOS_RETURN(code);
165,576✔
1363
}
1364

1365
static bool mndTransActionsOfSameType(SArray *pActions) {
48,677,326✔
1366
  int32_t size = taosArrayGetSize(pActions);
48,677,326✔
1367
  ETrnAct lastActType = TRANS_ACTION_NULL;
48,677,326✔
1368
  bool    same = true;
48,677,326✔
1369
  for (int32_t i = 0; i < size; ++i) {
151,675,084✔
1370
    STransAction *pAction = taosArrayGet(pActions, i);
102,997,758✔
1371
    if (i > 0) {
102,997,758✔
1372
      if (lastActType != pAction->actionType) {
68,573,953✔
1373
        same = false;
×
1374
        break;
×
1375
      }
1376
    }
1377
    lastActType = pAction->actionType;
102,997,758✔
1378
  }
1379
  return same;
48,677,326✔
1380
}
1381

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

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

1400
  TAOS_RETURN(code);
21,418,410✔
1401
}
1402

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

1416
  TAOS_RETURN(code);
21,418,410✔
1417
}
1418

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

1425
  mInfo("trans:%d, action list:", pTrans->id);
21,420,157✔
1426
  int32_t index = 0;
21,420,157✔
1427
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
32,926,675✔
1428
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
11,506,518✔
1429
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
11,506,518✔
1430
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1431
  }
1432

1433
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
44,488,205✔
1434
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
23,068,048✔
1435
    if (pAction->actionType == TRANS_ACTION_MSG) {
23,068,048✔
1436
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
22,363,575✔
1437
            TMSG_INFO(pAction->msgType));
1438
      ;
1439
    } else {
1440
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
704,473✔
1441
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1442
    }
1443
  }
1444

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

1451
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
34,686,395✔
1452
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
13,266,238✔
1453
    if (pAction->actionType == TRANS_ACTION_MSG) {
13,266,238✔
1454
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
8,533,386✔
1455
            TMSG_INFO(pAction->msgType));
1456
    } else {
1457
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
4,732,852✔
1458
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1459
    }
1460
  }
1461

1462
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
21,420,157✔
1463

1464
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
21,418,410✔
1465

1466
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
21,418,410✔
1467

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

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

1486
  pNew->pRpcArray = pTrans->pRpcArray;
21,417,186✔
1487
  pNew->rpcRsp = pTrans->rpcRsp;
21,417,186✔
1488
  pNew->rpcRspLen = pTrans->rpcRspLen;
21,417,186✔
1489
  pNew->mTraceId = pTrans->mTraceId;
21,417,186✔
1490
  pTrans->pRpcArray = NULL;
21,417,186✔
1491
  pTrans->rpcRsp = NULL;
21,417,186✔
1492
  pTrans->rpcRspLen = 0;
21,417,186✔
1493

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

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

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

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

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

1538
  if (pTrans->stage == TRN_STAGE_FINISH) {
98,255,498✔
1539
    sendRsp = true;
44,860,507✔
1540
  }
1541

1542
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
98,255,498✔
1543
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
25,678,365✔
1544
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
9,984✔
1545
      sendRsp = true;
9,984✔
1546
    }
1547
  } else {
1548
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
72,577,133✔
1549
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
41,036,487✔
1550
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1551
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1552
      } else {
1553
        if (pTrans->failedTimes > 6) sendRsp = true;
41,036,487✔
1554
      }
1555
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
41,036,487✔
1556
    }
1557
  }
1558

1559
  if (!sendRsp) {
98,255,498✔
1560
    return;
53,385,007✔
1561
  } else {
1562
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
44,870,491✔
1563
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1564
  }
1565

1566
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
44,870,491✔
1567
  taosWLockLatch(&pTrans->lockRpcArray);
44,870,491✔
1568
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
44,870,491✔
1569
  if (size <= 0) {
44,870,491✔
1570
    taosWUnLockLatch(&pTrans->lockRpcArray);
30,353,014✔
1571
    return;
30,353,014✔
1572
  }
1573

1574
  for (int32_t i = 0; i < size; ++i) {
29,034,954✔
1575
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
14,517,477✔
1576
    if (pInfo->handle != NULL) {
14,517,477✔
1577
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
13,901,966✔
1578
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1579
      }
1580
      if (code == TSDB_CODE_SYN_TIMEOUT) {
13,901,966✔
1581
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1582
      }
1583

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

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

1592
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
13,901,966✔
1593
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,477,157✔
1594
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,477,157✔
1595
        if (pDb != NULL) {
1,477,157✔
1596
          for (int32_t j = 0; j < 12; j++) {
1,859,062✔
1597
            bool ready = mndIsDbReady(pMnode, pDb);
1,857,189✔
1598
            if (!ready) {
1,857,189✔
1599
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
381,905✔
1600
              taosMsleep(1000);
381,905✔
1601
            } else {
1602
              break;
1,475,284✔
1603
            }
1604
          }
1605
        }
1606
        mndReleaseDb(pMnode, pDb);
1,477,157✔
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
12,424,809✔
1608
        void   *pCont = NULL;
2,185,600✔
1609
        int32_t contLen = 0;
2,185,600✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,185,600✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,183,936✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
10,239,209✔
1614
        void   *pCont = NULL;
10,407✔
1615
        int32_t contLen = 0;
10,407✔
1616
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
10,407✔
1617
          mndTransSetRpcRsp(pTrans, pCont, contLen);
10,407✔
1618
        }
1619
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
10,228,802✔
1620
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
10,340✔
1621
        if (code != 0) {
10,340✔
1622
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
10,218,462✔
1625
        void   *pCont = NULL;
20,959✔
1626
        int32_t contLen = 0;
20,959✔
1627
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
20,959✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
20,959✔
1629
        }
1630
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
10,197,503✔
1631
        void   *pCont = NULL;
20,322✔
1632
        int32_t contLen = 0;
20,322✔
1633
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
20,322✔
1634
          mndTransSetRpcRsp(pTrans, pCont, contLen);
20,322✔
1635
        }
1636
      }
1637

1638
      if (pTrans->rpcRspLen != 0) {
13,901,966✔
1639
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
8,289,459✔
1640
        if (rpcCont != NULL) {
8,289,459✔
1641
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
8,289,459✔
1642
          rspMsg.pCont = rpcCont;
8,289,459✔
1643
          rspMsg.contLen = pTrans->rpcRspLen;
8,289,459✔
1644
        }
1645
      }
1646

1647
      tmsgSendRsp(&rspMsg);
13,901,966✔
1648

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

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

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

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

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

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

1705
      // pTrans->lastErrorNo = pRsp->code;
1706
      mndSetTransLastAction(pTrans, pAction);
24,627,216✔
1707

1708
      mInfo("trans:%d, %s:%d response is received, msgType:%s, QID:0x%" PRIx64 ":0x%" PRIx64 ", received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
24,627,216✔
1709
            mndTransStr(pAction->stage), action, TMSG_INFO(pAction->msgType), trace ? trace->rootId : 0, 
1710
              trace ? trace->msgId : 0, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1711
            tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1712
    } else {
1713
      mWarn("trans:%d, %s:%d response is received, but msgSent is false, code:0x%x(%s), accept:0x%x(%s) retry:0x%x", 
×
1714
            transId, mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code),
1715
            pAction->acceptableCode, tstrerror(pAction->acceptableCode), pAction->retryCode);
1716
    }
1717

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

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

1725
_OVER:
24,627,684✔
1726
  mndReleaseTrans(pMnode, pTrans);
24,627,684✔
1727
  TAOS_RETURN(code);
24,627,684✔
1728
}
1729

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

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

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

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

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

1766
// execute in sync context
1767
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
175,026,039✔
1768
  if (pAction->rawWritten) return 0;
175,026,039✔
1769
  if (topHalf) {
96,357,995✔
1770
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
820✔
1771
  }
1772

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

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

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

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

1804
  TAOS_RETURN(code);
96,357,175✔
1805
}
1806

1807
// execute in trans context
1808
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
107,119,237✔
1809
                                     bool notSend) {
1810
  if (pAction->msgSent) return 0;
107,119,237✔
1811
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
40,195,102✔
1812
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
15,554,941✔
1813
  }
1814

1815
  if (notSend) {
24,640,161✔
1816
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
7,811✔
1817
    return 0;
7,811✔
1818
  }
1819

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

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

1842
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
24,632,350✔
1843

1844
  char    detail[1024] = {0};
24,632,350✔
1845
  int32_t len = snprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
24,632,350✔
1846
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
24,632,350✔
1847
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
52,291,848✔
1848
    len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
27,659,498✔
1849
                    pAction->epSet.eps[i].port);
27,659,498✔
1850
  }
1851

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

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

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

1873
  TAOS_RETURN(code);
24,632,350✔
1874
}
1875

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

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

1886
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
282,145,276✔
1887
                                        bool notSend) {
1888
  if (pAction->actionType == TRANS_ACTION_RAW) {
282,145,276✔
1889
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
175,026,039✔
1890
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
107,119,237✔
1891
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
107,119,237✔
1892
  } else {
1893
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1894
  }
1895
}
1896

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

1901
  for (int32_t action = 0; action < numOfActions; ++action) {
330,141,374✔
1902
    STransAction *pAction = taosArrayGet(pArray, action);
253,514,898✔
1903
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
253,514,898✔
1904
    if (code != 0) {
253,514,898✔
1905
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
12,242,206✔
1906
            numOfActions);
1907
      break;
12,242,206✔
1908
    }
1909
  }
1910

1911
  return code;
88,868,682✔
1912
}
1913

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

1919
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
88,868,682✔
1920
    return code;
12,242,206✔
1921
  }
1922

1923
  int32_t       numOfExecuted = 0;
76,626,476✔
1924
  int32_t       errCode = 0;
76,626,476✔
1925
  STransAction *pErrAction = NULL;
76,626,476✔
1926
  for (int32_t action = 0; action < numOfActions; ++action) {
317,899,168✔
1927
    STransAction *pAction = taosArrayGet(pArray, action);
241,272,692✔
1928
    if (pAction->msgReceived || pAction->rawWritten) {
241,272,692✔
1929
      numOfExecuted++;
199,995,541✔
1930
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
199,995,541✔
1931
        errCode = pAction->errCode;
2,901✔
1932
        pErrAction = pAction;
2,901✔
1933
      }
1934
    } else {
1935
      pErrAction = pAction;
41,277,151✔
1936
    }
1937
  }
1938

1939
  mndSetTransLastAction(pTrans, pErrAction);
76,626,476✔
1940

1941
  if (numOfExecuted == numOfActions) {
76,626,476✔
1942
    if (errCode == 0) {
55,699,758✔
1943
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
55,698,094✔
1944
      return 0;
55,698,094✔
1945
    } else {
1946
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,664✔
1947
      mndTransResetActions(pMnode, pTrans, pArray);
1,664✔
1948
      terrno = errCode;
1,664✔
1949
      return errCode;
1,664✔
1950
    }
1951
  } else {
1952
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
20,926,718✔
1953

1954
    for (int32_t action = 0; action < numOfActions; ++action) {
81,366,468✔
1955
      STransAction *pAction = taosArrayGet(pArray, action);
60,439,750✔
1956
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
60,439,750✔
1957
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1958
             pAction->acceptableCode, pAction->retryCode);
1959
      if (pAction->msgSent) {
60,439,750✔
1960
        bool reset = false;
60,431,939✔
1961
        if (pAction->msgReceived) {
60,431,939✔
1962
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
19,162,599✔
1963
        } else {
1964
          int64_t timestamp = taosGetTimestampMs();
41,269,340✔
1965
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
41,269,340✔
1966
        }
1967
        if (reset) {
60,431,939✔
1968
          mndTransResetAction(pMnode, pTrans, pAction);
1,237✔
1969
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1,237✔
1970
                pAction->id, pAction->errCode, pAction->startTime);
1971
        }
1972
      }
1973
    }
1974
    return TSDB_CODE_ACTION_IN_PROGRESS;
20,926,718✔
1975
  }
1976
}
1977

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

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

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

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

2010
  if (pTrans->actionPos >= numOfActions) {
8,244,006✔
2011
    return code;
363,056✔
2012
  }
2013

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

2017
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
12,408,047✔
2018
    STransAction *pAction = taosArrayGet(pActions, action);
12,088,417✔
2019

2020
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
12,088,417✔
2021
      code = TSDB_CODE_ACTION_IN_PROGRESS;
8,328✔
2022
      break;
8,490✔
2023
    }
2024

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

2028
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
12,080,089✔
2029
    if (code == 0) {
12,080,089✔
2030
      if (pAction->msgSent) {
10,292,430✔
2031
        bool reset = false;
9,028,219✔
2032
        if (pAction->msgReceived) {
9,028,219✔
2033
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,918,003✔
2034
            code = pAction->errCode;
2,248,522✔
2035
            reset = true;
2,248,522✔
2036
          } else {
2037
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,669,481✔
2038
          }
2039
        } else {
2040
          int64_t timestamp = taosGetTimestampMs();
5,110,216✔
2041
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
5,110,216✔
2042
          code = TSDB_CODE_ACTION_IN_PROGRESS;
5,110,216✔
2043
        }
2044
        if (reset) {
9,028,219✔
2045
          mndTransResetAction(pMnode, pTrans, pAction);
2,248,522✔
2046
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,248,522✔
2047
                pAction->id, pAction->errCode, pAction->startTime);
2048
        }
2049
      } else if (pAction->rawWritten) {
1,264,211✔
2050
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,264,211✔
2051
          code = pAction->errCode;
×
2052
        } else {
2053
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,264,211✔
2054
        }
2055
      } else {
2056
      }
2057
    }
2058

2059
    if (code == 0) {
12,080,089✔
2060
      pTrans->failedTimes = 0;
2,933,692✔
2061
    }
2062
    mndSetTransLastAction(pTrans, pAction);
12,080,089✔
2063
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
12,080,089✔
2064
      mInfo(
6,897,875✔
2065
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2066
          "msgReceived:%d",
2067
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2068
          pAction->msgSent, pAction->msgReceived);
2069
    } else if (code != 0) {
5,182,214✔
2070
      mError(
2,248,522✔
2071
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2072
          "msgReceived:%d",
2073
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2074
          pAction->msgSent, pAction->msgReceived);
2075
    }
2076

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

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

2122
  return code;
7,880,950✔
2123
}
2124

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

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

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

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

2149
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
4,528,950✔
2150
    STransAction **ppAction = taosArrayGet(pActions, action);
4,263,673✔
2151
    STransAction  *pAction = *ppAction;
4,263,673✔
2152

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

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

2169
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
3,706,741✔
2170
    if (code == 0) {
3,706,741✔
2171
      if (pAction->msgSent) {
2,180,845✔
2172
        if (pAction->msgReceived) {
1,958,008✔
2173
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
567,317✔
2174
            code = pAction->errCode;
55,494✔
2175
            mndTransResetAction(pMnode, pTrans, pAction);
55,494✔
2176
          } else {
2177
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
511,823✔
2178
                  pAction->id, action, numOfActions, groupId);
2179
          }
2180
        } else {
2181
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,390,691✔
2182
        }
2183
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,958,008✔
2184
        if (msgSent != NULL) {
1,958,008✔
2185
          *msgSent = pAction->msgSent;
1,958,008✔
2186
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,958,008✔
2187
        } else {
2188
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2189
        }
2190
      } else if (pAction->rawWritten) {
222,837✔
2191
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
222,837✔
2192
          code = pAction->errCode;
×
2193
        } else {
2194
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
222,837✔
2195
        }
2196
      } else {
2197
      }
2198
    }
2199

2200
    if (code == 0) {
3,706,741✔
2201
      pTrans->failedTimes = 0;
734,660✔
2202
    }
2203
    mndSetTransLastAction(pTrans, pAction);
3,706,741✔
2204

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

2227
    if (code == 0) {
2,025,156✔
2228
      pTrans->code = 0;
578,971✔
2229
      pTrans->actionPos++;
578,971✔
2230
      (*actionPos)++;
578,971✔
2231
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
578,971✔
2232
            pAction->id);
2233
      (void)taosThreadMutexUnlock(&pTrans->mutex);
578,971✔
2234
      code = mndTransSync(pMnode, pTrans);
578,971✔
2235
      (void)taosThreadMutexLock(&pTrans->mutex);
578,971✔
2236
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
578,971✔
2237
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
13,591,732✔
2238
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
13,012,761✔
2239
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
13,012,761✔
2240
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
13,012,761✔
2241
          pTmpAction->msgSent = *msgSent;
716,085✔
2242
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
716,085✔
2243
        }
2244
      }
2245
      if (code != 0) {
578,971✔
2246
        pTrans->actionPos--;
×
2247
        (*actionPos)--;
×
2248
        pTrans->code = terrno;
×
2249
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2250
               mndTransStr(pAction->stage), pAction->id, terrstr());
2251
        break;
×
2252
      }
2253
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
1,446,185✔
2254
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,390,691✔
2255
            mndTransStr(pAction->stage), pAction->id);
2256
      break;
1,390,691✔
2257
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
55,494✔
2258
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
224✔
2259
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
55,494✔
2260
            code, tstrerror(code));
2261
      pTrans->lastErrorNo = code;
55,494✔
2262
      taosMsleep(300);
55,494✔
2263
      action--;
55,494✔
2264
      continue;
55,494✔
2265
    } else {
UNCOV
2266
      terrno = code;
×
UNCOV
2267
      pTrans->lastErrorNo = code;
×
UNCOV
2268
      pTrans->code = code;
×
UNCOV
2269
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2270
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
UNCOV
2271
      break;
×
2272
    }
2273
  }
2274

2275
  return code;
3,894,485✔
2276
}
2277

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

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

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

2306
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,459,216✔
2307
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,459,216✔
2308
  int32_t currentGroup = 1;
1,459,216✔
2309
  while (pIter) {
6,133,021✔
2310
    SArray **redoActions = pIter;
4,673,805✔
2311
    size_t   keyLen = 0;
4,673,805✔
2312
    int32_t *key = taosHashGetKey(pIter, &keyLen);
4,673,805✔
2313
    int32_t  actionCount = taosArrayGetSize(*redoActions);
4,673,805✔
2314
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
4,673,805✔
2315
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2316
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
4,673,805✔
2317
                                             notSend, pHash);
2318
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
4,673,805✔
2319
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,525,896✔
2320
            tstrerror(code));
2321
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
3,147,909✔
2322
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,947,623✔
2323
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
1,200,286✔
2324
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
779,320✔
2325
    } else if (code != 0) {
420,966✔
UNCOV
2326
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
×
2327
             tstrerror(code));
2328
    } else {
2329
      successCount++;
420,966✔
2330
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
420,966✔
2331
    }
2332
    currentGroup++;
4,673,805✔
2333
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,673,805✔
2334
  }
2335

2336
  taosHashCleanup(pHash);
1,459,216✔
2337

2338
  if (successCount == groupCount) {
1,459,216✔
2339
    total_code = 0;
27,296✔
2340
  } else {
2341
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,431,920✔
2342
  }
2343

2344
  return total_code;
1,459,216✔
2345
}
2346

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

2351
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,699,022✔
2352
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,699,022✔
2353
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,699,022✔
2354
      code = 0;
206,242✔
2355
    } else {
2356
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,492,780✔
2357
      if (pAction != NULL && pAction->groupId == -1) {
1,492,780✔
2358
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
33,564✔
2359
      } else {
2360
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,459,216✔
2361
        if (code == 0) {
1,459,216✔
2362
          if (pTrans->actionPos < numOfActions) {
27,296✔
2363
            code = TSDB_CODE_ACTION_IN_PROGRESS;
19,816✔
2364
          }
2365
        }
2366
      }
2367
    }
2368
  }
2369

2370
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,699,022✔
2371

2372
  return code;
1,699,022✔
2373
}
2374

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

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

2390
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
23,450,925✔
2391
  if (numOfActions == 0) goto _OVER;
23,450,925✔
2392

2393
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
9,519,682✔
2394

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

2406
_OVER:
9,519,682✔
2407
  pTrans->stage = TRN_STAGE_REDO_ACTION;
23,450,925✔
2408
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
23,450,925✔
2409
  return continueExec;
23,450,925✔
2410
}
2411

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

2417
  if (pTrans->exec == TRN_EXEC_SERIAL) {
74,799,861✔
2418
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
8,216,214✔
2419
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
66,583,647✔
2420
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
64,884,625✔
2421
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,699,022✔
2422
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,699,022✔
2423
  } else {
2424
    code = TSDB_CODE_INTERNAL_ERROR;
×
2425
  }
2426

2427
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
74,801,525✔
2428
      mndTransIsInSyncContext(topHalf)) {
1,664✔
2429
    pTrans->lastErrorNo = code;
×
2430
    pTrans->code = code;
×
2431
    mInfo(
×
2432
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2433
        "continueExec:%d, code:%s",
2434
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2435
    taosMsleep(100);
×
2436
    return true;
×
2437
  } else {
2438
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
74,799,861✔
2439
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
26,502,472✔
2440
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2441
      return false;
26,502,472✔
2442
    }
2443
  }
2444
  terrno = code;
48,297,389✔
2445

2446
  if (code == 0) {
48,297,389✔
2447
    pTrans->code = 0;
21,414,824✔
2448
    pTrans->stage = TRN_STAGE_COMMIT;
21,414,824✔
2449
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
21,414,824✔
2450
    continueExec = true;
21,414,824✔
2451
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
26,882,565✔
2452
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
26,880,901✔
2453
    continueExec = false;
26,880,901✔
2454
  } else {
2455
    pTrans->failedTimes++;
1,664✔
2456
    pTrans->code = terrno;
1,664✔
2457
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
1,664✔
2458
      if (pTrans->lastAction != 0) {
1,664✔
2459
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
1,664✔
2460
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
1,664✔
2461
          if (pTrans->failedTimes < 6) {
×
2462
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2463
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
2464
            taosMsleep(1000);
×
2465
            continueExec = true;
×
2466
            return true;
×
2467
          }
2468
        }
2469
      }
2470

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

2482
  return continueExec;
48,297,389✔
2483
}
2484

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

2489
  bool    continueExec = true;
21,414,824✔
2490
  int32_t code = mndTransCommit(pMnode, pTrans);
21,414,824✔
2491

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

2504
  return continueExec;
21,414,824✔
2505
}
2506

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

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

2528
  return continueExec;
44,856,698✔
2529
}
2530

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

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

2541
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
11,648✔
2542
  terrno = code;
9,984✔
2543

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

2557
  return continueExec;
9,984✔
2558
}
2559

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

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

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

2576
  return continueExec;
1,664✔
2577
}
2578

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

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

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

2596
  return continueExec;
1,783✔
2597
}
2598

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

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

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

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

2620
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
98,255,498✔
2621
  bool continueExec = true;
98,255,498✔
2622

2623
  while (continueExec) {
284,202,240✔
2624
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
185,946,742✔
2625
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2626
    pTrans->lastExecTime = taosGetTimestampMs();
185,946,742✔
2627
    switch (pTrans->stage) {
185,946,742✔
2628
      case TRN_STAGE_PREPARE:
×
2629
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2630
        break;
×
2631
      case TRN_STAGE_REDO_ACTION:
74,799,861✔
2632
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
74,799,861✔
2633
        break;
74,799,861✔
2634
      case TRN_STAGE_COMMIT:
21,414,934✔
2635
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
21,414,934✔
2636
        break;
21,414,934✔
2637
      case TRN_STAGE_COMMIT_ACTION:
44,856,698✔
2638
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
44,856,698✔
2639
        break;
44,856,698✔
2640
      case TRN_STAGE_ROLLBACK:
1,664✔
2641
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,664✔
2642
        break;
1,664✔
2643
      case TRN_STAGE_UNDO_ACTION:
11,648✔
2644
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
11,648✔
2645
        break;
11,648✔
2646
      case TRN_STAGE_PRE_FINISH:
1,783✔
2647
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,783✔
2648
        break;
1,783✔
2649
      case TRN_STAGE_FINISH:
44,860,154✔
2650
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
44,860,154✔
2651
        break;
44,860,154✔
2652
      default:
×
2653
        continueExec = false;
×
2654
        break;
×
2655
    }
2656
  }
2657

2658
  mndTransSendRpcRsp(pMnode, pTrans);
98,255,498✔
2659
}
98,255,498✔
2660

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2758
  void *pIter = NULL;
18,192,056✔
2759
  while (1) {
2,283,159✔
2760
    STrans *pTrans = NULL;
20,475,215✔
2761
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
20,475,215✔
2762
    if (pIter == NULL) break;
20,475,215✔
2763
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
4,566,318✔
2764
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2765
    }
2766
    sdbRelease(pSdb, pTrans);
2,283,159✔
2767
  }
2768

2769
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
18,192,056✔
2770

2771
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
20,475,215✔
2772
    int32_t *pTransId = taosArrayGet(pArray, i);
2,283,159✔
2773
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
2,283,159✔
2774
    if (pTrans != NULL) {
2,283,159✔
2775
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
2,283,159✔
2776
      mndTransExecute(pMnode, pTrans, false);
2,283,159✔
2777
    }
2778
    mndReleaseTrans(pMnode, pTrans);
2,283,159✔
2779
  }
2780
  taosArrayDestroy(pArray);
18,192,056✔
2781
}
2782

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

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

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

2809
  return buf;
23,957,732✔
2810
}
2811

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

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

2828
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
559,363✔
2829
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
16,295,081✔
2830
      len = 0;
15,737,312✔
2831
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
15,737,312✔
2832
      if (pAction->actionType == TRANS_ACTION_MSG) {
15,737,312✔
2833
        char bufStart[40] = {0};
11,963,991✔
2834
        (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
11,963,991✔
2835

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

2844
        SEpSet epset = pAction->epSet;
11,963,991✔
2845
        if (epset.numOfEps > 0) {
11,963,991✔
2846
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
11,963,991✔
2847
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
26,833,342✔
2848
            len +=
14,869,351✔
2849
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
14,869,351✔
2850
          }
2851
        }
2852

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

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

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

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

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

2904
    cols = 0;
559,363✔
2905

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

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

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

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

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

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

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

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

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

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

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

2971
    mndTransLogAction(pTrans);
559,363✔
2972

2973
    numOfRows++;
559,363✔
2974
    sdbRelease(pSdb, pTrans);
559,363✔
2975
  }
2976

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

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

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

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

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

3016
  cols = 0;
72,765✔
3017

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

3020
  if (pAction->actionType == TRANS_ACTION_MSG) {
72,765✔
3021
    int32_t len = 0;
59,773✔
3022

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

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

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

3054
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
59,773✔
3055
    len = 0;
59,773✔
3056
    char bufStart[40] = {0};
59,773✔
3057
    if (pAction->startTime > 0)
59,773✔
3058
      (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
15,400✔
3059
    char bufEnd[40] = {0};
59,773✔
3060
    if (pAction->endTime > 0)
59,773✔
3061
      (void)formatTimestamp(bufEnd, sizeof(bufEnd), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
14,350✔
3062

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc