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

taosdata / TDengine / #5060

17 May 2026 01:15AM UTC coverage: 73.425% (-0.02%) from 73.443%
#5060

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281800 of 383795 relevant lines covered (73.42%)

134332207.97 hits per line

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

83.77
/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; }
153,426,557✔
66

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

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

83
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
221,431,977✔
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) {
546,745✔
96
  SSdbTable table = {
546,745✔
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);
546,745✔
107
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
546,745✔
108

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

115
void mndCleanupTrans(SMnode *pMnode) {}
546,677✔
116

117
static int32_t mndTransGetActionsSize(SArray *pArray) {
289,053,616✔
118
  int32_t actionNum = taosArrayGetSize(pArray);
289,053,616✔
119
  int32_t rawDataLen = 0;
289,053,616✔
120

121
  for (int32_t i = 0; i < actionNum; ++i) {
775,891,832✔
122
    STransAction *pAction = taosArrayGet(pArray, i);
486,838,216✔
123
    if (pAction->actionType == TRANS_ACTION_RAW) {
486,838,216✔
124
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
334,479,281✔
125
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
152,358,935✔
126
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
152,358,935✔
127
    } else {
128
      // empty
129
    }
130
    rawDataLen += sizeof(int8_t);
486,838,216✔
131
  }
132

133
  return rawDataLen;
289,053,616✔
134
}
135

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

144
  for (int32_t i = 0; i < actionsNum; ++i) {
775,891,832✔
145
    STransAction *pAction = taosArrayGet(pActions, i);
486,838,216✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
486,838,216✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
486,838,216✔
148
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
486,838,216✔
149
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
486,838,216✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
486,838,216✔
151
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
486,838,216✔
152
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
486,838,216✔
153
    if (sver > TRANS_VER2_NUMBER) {
486,838,216✔
154
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
486,838,216✔
155
    }
156
    if (pAction->actionType == TRANS_ACTION_RAW) {
486,838,216✔
157
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
334,479,281✔
158
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
334,479,281✔
159
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
334,479,281✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
334,479,281✔
161
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
152,358,935✔
162
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
152,358,935✔
163
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
152,358,935✔
164
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
152,358,935✔
165
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
152,358,935✔
166
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
152,358,935✔
167
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
152,358,935✔
168
    } else {
169
      // nothing
170
    }
171
  }
172
  ret = 0;
289,053,616✔
173

174
_OVER:
289,053,616✔
175
  *offset = dataPos;
289,053,616✔
176
  return ret;
289,053,616✔
177
}
178

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

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

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

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

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

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

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

228
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
72,263,404✔
229
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
72,263,404✔
230
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
72,263,404✔
231
  if (pTrans->param != NULL) {
72,263,404✔
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)
72,263,404✔
236

237
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
72,263,404✔
238
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
72,263,404✔
239
  void *pIter = NULL;
72,263,404✔
240
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
72,263,404✔
241
  while (pIter) {
72,298,414✔
242
    int32_t arbGroupId = *(int32_t *)pIter;
35,010✔
243
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
35,010✔
244
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
35,010✔
245
  }
246

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

252
  if (sver > TRANS_VER2_NUMBER) {
72,263,404✔
253
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
72,263,404✔
254
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
72,263,404✔
255
    void *pIter = NULL;
72,263,404✔
256
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
72,263,404✔
257
    while (pIter) {
74,972,386✔
258
      size_t   keysize = 0;
2,708,982✔
259
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,708,982✔
260
      int32_t  groupPos = *(int32_t *)pIter;
2,708,982✔
261
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,708,982✔
262
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,708,982✔
263
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,708,982✔
264
    }
265
  }
266

267
  if (sver >= TRANS_VER_USER_DATA) {
72,263,404✔
268
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
72,263,404✔
269
    if (pTrans->userDataLen > 0) {
72,263,404✔
270
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
139,120✔
271
    }
272
  }
273

274
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
72,263,404✔
275
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
72,263,404✔
276

277
  terrno = 0;
72,263,404✔
278

279
_OVER:
72,263,404✔
280
  if (terrno != 0) {
72,263,404✔
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);
72,263,404✔
288
  return pRaw;
72,263,404✔
289
}
290

291
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
32,297,826✔
292
  if (pAction->groupId < 0) return 0;
32,297,826✔
293
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
31,624,945✔
294
  if (redoAction == NULL) {
31,624,945✔
295
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
5,439,302✔
296
    if (array != NULL) {
5,439,302✔
297
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
5,439,302✔
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,439,302✔
303
  }
304
  if (redoAction != NULL) {
31,624,945✔
305
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
63,249,890✔
306
  }
307
  return 0;
31,624,945✔
308
}
309

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

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

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

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

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

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

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

451
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
128,635,121✔
452

453
  if (sver > TRANS_VER_CURRENT) {
128,635,121✔
454
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
455
    goto _OVER;
×
456
  }
457

458
  pRow = sdbAllocRow(sizeof(STrans));
128,635,121✔
459
  if (pRow == NULL) goto _OVER;
128,635,121✔
460

461
  pTrans = sdbGetRowObj(pRow);
128,635,121✔
462
  if (pTrans == NULL) goto _OVER;
128,635,121✔
463

464
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
128,635,121✔
465

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

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

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

506
  if (pTrans->prepareActions == NULL) goto _OVER;
128,635,121✔
507
  if (pTrans->redoActions == NULL) goto _OVER;
128,635,121✔
508
  if (pTrans->undoActions == NULL) goto _OVER;
128,635,121✔
509
  if (pTrans->commitActions == NULL) goto _OVER;
128,635,121✔
510

511
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
128,635,121✔
512
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
128,635,121✔
513
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,866,573✔
514
                                      sver) < 0)
515
      goto _OVER;
×
516
  } else {
517
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
126,768,548✔
518
  }
519
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
128,635,121✔
520
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
128,635,121✔
521

522
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
128,635,121✔
523
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
128,635,121✔
524
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
128,635,121✔
525
  if (pTrans->paramLen != 0) {
128,635,121✔
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);
128,635,121✔
532

533
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
128,635,121✔
534
  if (arbgroupIdNum > 0) {
128,635,121✔
535
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
50,080✔
536
    if (pTrans->arbGroupIds == NULL) goto _OVER;
50,080✔
537
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
108,430✔
538
      int32_t arbGroupId = 0;
58,350✔
539
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
58,350✔
540
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
58,350✔
541
    }
542
  }
543

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

553
  if (sver > TRANS_VER2_NUMBER) {
128,635,121✔
554
    int32_t groupNum = -1;
128,635,121✔
555
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
128,635,121✔
556
    for (int32_t i = 0; i < groupNum; ++i) {
134,074,423✔
557
      int32_t groupId = -1;
5,439,302✔
558
      int32_t groupPos = -1;
5,439,302✔
559
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
5,439,302✔
560
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
5,439,302✔
561
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
5,439,302✔
562
        goto _OVER;
×
563
    }
564
  }
565

566
  if (sver >= TRANS_VER_USER_DATA) {
128,635,121✔
567
    SDB_GET_INT32(pRaw, dataPos, &pTrans->userDataLen, _OVER)
128,635,121✔
568
    if (pTrans->userDataLen > 0) {
128,635,121✔
569
      pTrans->userData = taosMemoryMalloc(pTrans->userDataLen);
232,480✔
570
      if (pTrans->userData == NULL) goto _OVER;
232,480✔
571
      SDB_GET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
232,480✔
572
    }
573
  }
574

575
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
128,635,121✔
576

577
  terrno = 0;
128,635,121✔
578

579
_OVER:
128,635,121✔
580
  if (terrno != 0 && pTrans != NULL) {
128,635,121✔
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) {
128,635,121✔
589
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
128,635,121✔
590
  }
591
  return pRow;
128,635,121✔
592
}
593

594
static const char *mndTransStr(ETrnStage stage) {
900,045,737✔
595
  switch (stage) {
900,045,737✔
596
    case TRN_STAGE_PREPARE:
71,866,656✔
597
      return "prepare";
71,866,656✔
598
    case TRN_STAGE_REDO_ACTION:
303,731,098✔
599
      return "redoAction";
303,731,098✔
600
    case TRN_STAGE_ROLLBACK:
8,530✔
601
      return "rollback";
8,530✔
602
    case TRN_STAGE_UNDO_ACTION:
13,965,822✔
603
      return "undoAction";
13,965,822✔
604
    case TRN_STAGE_COMMIT:
116,140,009✔
605
      return "commit";
116,140,009✔
606
    case TRN_STAGE_COMMIT_ACTION:
222,702,921✔
607
      return "commitAction";
222,702,921✔
608
    case TRN_STAGE_FINISH:
171,621,466✔
609
      return "finished";
171,621,466✔
610
    case TRN_STAGE_PRE_FINISH:
9,235✔
611
      return "pre-finish";
9,235✔
612
    default:
×
613
      return "invalid";
×
614
  }
615
}
616

617
static const char *mndTransTypeStr(ETrnAct actionType) {
80,399✔
618
  switch (actionType) {
80,399✔
619
    case TRANS_ACTION_MSG:
66,063✔
620
      return "msg";
66,063✔
621
    case TRANS_ACTION_RAW:
14,336✔
622
      return "sdb";
14,336✔
623
    default:
×
624
      return "invalid";
×
625
  }
626
}
627

628
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
251,340,580✔
629
  if (pAction != NULL) {
251,340,580✔
630
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
193,242,699✔
631
      pTrans->lastAction = pAction->id;
138,912,951✔
632
      pTrans->lastMsgType = pAction->msgType;
138,912,951✔
633
      pTrans->lastEpset = pAction->epSet;
138,912,951✔
634
      pTrans->lastErrorNo = pAction->errCode;
138,912,951✔
635
    }
636
  } else {
637
    pTrans->lastAction = 0;
58,097,881✔
638
    pTrans->lastMsgType = 0;
58,097,881✔
639
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
58,097,881✔
640
    pTrans->lastErrorNo = 0;
58,097,881✔
641
  }
642
}
251,340,580✔
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) {
711,082✔
653
  switch (ftype) {
711,082✔
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:
355,437✔
659
      return mndRebCntInc;
355,437✔
660
    case TRANS_STOP_FUNC_MQ_REB:
355,437✔
661
      return mndRebCntDec;
355,437✔
662
    case TRANS_STOP_FUNC_SOD:
208✔
663
      return mndSodTransStop;
208✔
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) {
24,501,887✔
672
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
24,501,887✔
673
        mndTransStr(pTrans->stage), pTrans->startFunc);
674

675
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
24,501,887✔
676

677
  if (pTrans->startFunc > 0) {
24,501,887✔
678
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
355,437✔
679
    if (fp) {
355,437✔
680
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
355,437✔
681
    }
682
    // pTrans->startFunc = 0;
683
  }
684

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

690
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
24,501,887✔
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) {
24,501,887✔
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;
24,501,887✔
701
}
702

703
void mndTransDropData(STrans *pTrans) {
151,365,855✔
704
  if (pTrans->prepareActions != NULL) {
151,365,855✔
705
    mndTransDropActions(pTrans->prepareActions);
151,365,855✔
706
    pTrans->prepareActions = NULL;
151,365,855✔
707
  }
708
  if (pTrans->redoActions != NULL) {
151,365,855✔
709
    mndTransDropActions(pTrans->redoActions);
151,365,855✔
710
    pTrans->redoActions = NULL;
151,365,855✔
711
  }
712
  if (pTrans->undoActions != NULL) {
151,365,855✔
713
    mndTransDropActions(pTrans->undoActions);
151,365,855✔
714
    pTrans->undoActions = NULL;
151,365,855✔
715
  }
716
  if (pTrans->commitActions != NULL) {
151,365,855✔
717
    mndTransDropActions(pTrans->commitActions);
151,365,855✔
718
    pTrans->commitActions = NULL;
151,365,855✔
719
  }
720
  if (pTrans->redoGroupActions != NULL) {
151,365,855✔
721
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
24,597,307✔
722
    while (pIter) {
30,303,299✔
723
      SArray **redoActions = pIter;
5,705,992✔
724
      taosArrayDestroy(*redoActions);
5,705,992✔
725
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
5,705,992✔
726
    }
727

728
    taosHashCleanup(pTrans->redoGroupActions);
24,597,307✔
729
    pTrans->redoGroupActions = NULL;
24,597,307✔
730
  }
731
  if (pTrans->groupActionPos != NULL) {
151,365,855✔
732
    taosHashCleanup(pTrans->groupActionPos);
24,597,307✔
733
    pTrans->groupActionPos = NULL;
24,597,307✔
734
  }
735
  if (pTrans->arbGroupIds != NULL) {
151,365,855✔
736
    taosHashCleanup(pTrans->arbGroupIds);
22,780,814✔
737
  }
738
  if (pTrans->pRpcArray != NULL) {
151,365,855✔
739
    taosArrayDestroy(pTrans->pRpcArray);
22,730,734✔
740
    pTrans->pRpcArray = NULL;
22,730,734✔
741
  }
742
  if (pTrans->rpcRsp != NULL) {
151,365,855✔
743
    taosMemoryFree(pTrans->rpcRsp);
8,580,783✔
744
    pTrans->rpcRsp = NULL;
8,580,783✔
745
    pTrans->rpcRspLen = 0;
8,580,783✔
746
  }
747
  if (pTrans->param != NULL) {
151,365,855✔
748
    taosMemoryFree(pTrans->param);
×
749
    pTrans->param = NULL;
×
750
    pTrans->paramLen = 0;
×
751
  }
752
  if (pTrans->userData != NULL) {
151,365,855✔
753
    taosMemoryFree(pTrans->userData);
232,648✔
754
    pTrans->userData = NULL;
232,648✔
755
    pTrans->userDataLen = 0;
232,648✔
756
  }
757
  (void)taosThreadMutexDestroy(&pTrans->mutex);
151,365,855✔
758
}
151,365,855✔
759

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

764
  if (pTrans->stopFunc > 0 && callFunc) {
76,565,267✔
765
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
355,645✔
766
    if (fp) {
355,645✔
767
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
355,645✔
768
    }
769
    // pTrans->stopFunc = 0;
770
  }
771

772
  mndTransDropData(pTrans);
76,565,267✔
773
  return 0;
76,565,267✔
774
}
775

776
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
110,375,084✔
777
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
329,062,198✔
778
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
218,687,114✔
779
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
218,687,114✔
780
    pOldAction->rawWritten = pNewAction->rawWritten;
218,687,114✔
781
    pOldAction->msgSent = pNewAction->msgSent;
218,687,114✔
782
    pOldAction->msgReceived = pNewAction->msgReceived;
218,687,114✔
783
    pOldAction->errCode = pNewAction->errCode;
218,687,114✔
784
  }
785
}
110,375,084✔
786

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

791
  if (pOld->createdTime != pNew->createdTime) {
27,593,771✔
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);
27,593,771✔
801
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
27,593,771✔
802
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
27,593,771✔
803
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
27,593,771✔
804
  pOld->stage = pNew->stage;
27,593,771✔
805
  pOld->actionPos = pNew->actionPos;
27,593,771✔
806
  TSWAP(pOld->userData, pNew->userData);
27,593,771✔
807
  TSWAP(pOld->userDataLen, pNew->userDataLen);
27,593,771✔
808

809
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
27,593,771✔
810
  while (pIter != NULL) {
29,898,699✔
811
    int32_t newActionPos = *(int32_t *)pIter;
2,304,928✔
812

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

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

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

825
  if (pOld->stage == TRN_STAGE_COMMIT) {
27,593,771✔
826
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
24,467,498✔
827
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
24,467,498✔
828
  }
829

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

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

840
  return 0;
27,593,771✔
841
}
842

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

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

857
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
22,730,734✔
858
                       const char *opername) {
859
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
22,730,734✔
860
  if (pTrans == NULL) {
22,730,734✔
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) {
22,730,734✔
867
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
22,730,734✔
868
  }
869

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

892
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
22,730,734✔
893
      pTrans->pRpcArray == NULL) {
22,730,734✔
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) {
22,730,734✔
901
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
30,846,564✔
902
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
903
      return NULL;
×
904
    }
905
    pTrans->originRpcType = pReq->msgType;
15,423,282✔
906
  }
907

908
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
22,730,734✔
909

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

914
static void mndTransDropActions(SArray *pArray) {
605,463,420✔
915
  int32_t size = taosArrayGetSize(pArray);
605,463,420✔
916
  for (int32_t i = 0; i < size; ++i) {
1,616,619,020✔
917
    STransAction *pAction = taosArrayGet(pArray, i);
1,011,155,600✔
918
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,011,155,600✔
919
      taosMemoryFreeClear(pAction->pRaw);
691,131,440✔
920
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
320,024,160✔
921
      taosMemoryFreeClear(pAction->pCont);
320,024,160✔
922
    } else {
923
      // nothing
924
    }
925
  }
926

927
  taosArrayDestroy(pArray);
605,463,420✔
928
}
605,463,420✔
929

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

938
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
134,916,885✔
939
  pAction->id = taosArrayGetSize(pArray);
134,916,885✔
940

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

946
  return 0;
134,916,885✔
947
}
948

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

952
  SArray **redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
616,636✔
953
  if (redoAction == NULL) {
616,636✔
954
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
266,690✔
955
    if (array != NULL) {
266,690✔
956
      if (taosHashPut(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
266,690✔
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));
266,690✔
961
    }
962
  }
963
  if (redoAction != NULL) {
616,636✔
964
    mInfo("trans:%d, append action into group %d, msgType:%s", pTrans->id, pAction->groupId,
616,636✔
965
          TMSG_INFO(pAction->msgType));
966
    void *ptr = taosArrayPush(*redoAction, &pAction);
616,636✔
967
    if (ptr == NULL) {
616,636✔
968
      TAOS_RETURN(terrno);
×
969
    }
970
  }
971

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

981
  return 0;
616,636✔
982
}
983

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

991
int32_t mndTransAppendGroupRedolog(STrans *pTrans, SSdbRaw *pRaw, int32_t groupId) {
343,321✔
992
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION,
343,321✔
993
                         .actionType = TRANS_ACTION_RAW,
994
                         .pRaw = pRaw,
995
                         .mTraceId = pTrans->mTraceId,
343,321✔
996
                         .groupId = groupId};
997
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
343,321✔
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));
343,321✔
1002
  return mndTransAppendAction(pTrans->redoActions, &action);
343,321✔
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,958,312✔
1025
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
4,958,312✔
1026
  return mndTransAppendAction(pTrans->undoActions, &action);
4,958,312✔
1027
}
1028

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

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

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

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

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

1063
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
351,957✔
1064
  pTrans->startFunc = startFunc;
351,957✔
1065
  pTrans->stopFunc = stopFunc;
351,957✔
1066
  pTrans->param = param;
351,957✔
1067
  pTrans->paramLen = paramLen;
351,957✔
1068
}
351,957✔
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) {
12,438,945✔
1102
  if (dbname != NULL) {
12,438,945✔
1103
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
12,438,945✔
1104
  }
1105
  if (stbname != NULL) {
12,438,945✔
1106
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
9,382,434✔
1107
  }
1108
}
12,438,945✔
1109

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

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

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

1129
void mndTransSetGroupParallel(STrans *pTrans) {
111,750✔
1130
  mInfo("trans:%d, set Group Parallel", pTrans->id);
111,750✔
1131
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
111,750✔
1132
}
111,750✔
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,654✔
1142
  pTrans->ableToBeKilled = true; 
45,654✔
1143
  pTrans->killMode = killMode; 
45,654✔
1144
}
45,654✔
1145

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

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

1150
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
47,731,936✔
1151
  int32_t  code = 0;
47,731,936✔
1152
  SSdbRaw *pRaw = mndTransEncode(pTrans);
47,731,936✔
1153
  if (pRaw == NULL) {
47,731,936✔
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));
47,731,936✔
1160

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

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

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

1182
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
6,584,886✔
1183
  if (conflict[0] == 0) return false;
6,584,886✔
1184
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
6,394,331✔
1185
  return false;
6,228,552✔
1186
}
1187

1188
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
6,864,714✔
1189
  if (conflict) {
6,864,714✔
1190
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
246,925✔
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;
246,925✔
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,617,789✔
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,864,714✔
1201

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

1207
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
59,442,079✔
1208

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

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

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

1218
    if (pNew->conflict == TRN_CONFLICT_DB) {
6,679,720✔
1219
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
221,688✔
1220
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
221,688✔
1221
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
190,555✔
1222
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
190,555✔
1223
      }
1224
    }
1225

1226
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
6,679,720✔
1227
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
6,416,204✔
1228
      if (pTrans->conflict == TRN_CONFLICT_DB) {
6,416,204✔
1229
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
89,273✔
1230
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
89,273✔
1231
      }
1232
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
6,416,204✔
1233
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
6,305,058✔
1234
      }
1235
    }
1236

1237
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
6,679,720✔
1238
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
35,910✔
1239
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
35,910✔
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,679,720✔
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,679,720✔
1264
  }
1265

1266
  return conflict;
44,697,677✔
1267
}
1268

1269
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
59,442,079✔
1270
  int32_t code = 0;
59,442,079✔
1271
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
59,442,079✔
1272
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
37,480,865✔
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)) {
59,442,079✔
1280
    code = TSDB_CODE_MND_TRANS_CONFLICT;
293,081✔
1281
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
293,081✔
1282
    TAOS_RETURN(code);
293,081✔
1283
  }
1284

1285
  TAOS_RETURN(code);
59,148,998✔
1286
}
1287

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

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

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

1306
    if (thisConflict) {
264✔
1307
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
264✔
1308
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1309
      conflict = true;
264✔
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);
264✔
1315
  }
1316

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

1323
  TAOS_RETURN(code);
171,336✔
1324
}
1325

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

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

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

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

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

1362
  TAOS_RETURN(code);
171,336✔
1363
}
1364

1365
static bool mndTransActionsOfSameType(SArray *pActions) {
50,978,916✔
1366
  int32_t size = taosArrayGetSize(pActions);
50,978,916✔
1367
  ETrnAct lastActType = TRANS_ACTION_NULL;
50,978,916✔
1368
  bool    same = true;
50,978,916✔
1369
  for (int32_t i = 0; i < size; ++i) {
162,071,369✔
1370
    STransAction *pAction = taosArrayGet(pActions, i);
111,092,453✔
1371
    if (i > 0) {
111,092,453✔
1372
      if (lastActType != pAction->actionType) {
75,174,791✔
1373
        same = false;
×
1374
        break;
×
1375
      }
1376
    }
1377
    lastActType = pAction->actionType;
111,092,453✔
1378
  }
1379
  return same;
50,978,916✔
1380
}
1381

1382
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
22,403,624✔
1383
  int32_t code = 0;
22,403,624✔
1384
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
22,403,624✔
1385
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
21,964,125✔
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) {
21,964,125✔
1392
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
6,611,167✔
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);
22,403,624✔
1401
}
1402

1403
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
22,403,624✔
1404
  int32_t code = 0;
22,403,624✔
1405
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
22,403,624✔
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) {
22,403,624✔
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);
22,403,624✔
1417
}
1418

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

1425
  mInfo("trans:%d, action list:", pTrans->id);
22,404,652✔
1426
  int32_t index = 0;
22,404,652✔
1427
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
34,298,905✔
1428
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
11,894,253✔
1429
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
11,894,253✔
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) {
46,252,918✔
1434
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
23,848,266✔
1435
    if (pAction->actionType == TRANS_ACTION_MSG) {
23,848,266✔
1436
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
23,128,045✔
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),
720,221✔
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) {
107,555,835✔
1446
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
85,151,183✔
1447
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
85,151,183✔
1448
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1449
  }
1450

1451
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
36,297,116✔
1452
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
13,892,464✔
1453
    if (pAction->actionType == TRANS_ACTION_MSG) {
13,892,464✔
1454
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
8,934,152✔
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,958,312✔
1458
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1459
    }
1460
  }
1461

1462
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
22,404,652✔
1463

1464
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
22,403,624✔
1465

1466
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
22,403,624✔
1467

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

1478
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
22,402,749✔
1479
  if (pNew == NULL) {
22,402,749✔
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;
22,402,749✔
1487
  pNew->rpcRsp = pTrans->rpcRsp;
22,402,749✔
1488
  pNew->rpcRspLen = pTrans->rpcRspLen;
22,402,749✔
1489
  pNew->mTraceId = pTrans->mTraceId;
22,402,749✔
1490
  pTrans->pRpcArray = NULL;
22,402,749✔
1491
  pTrans->rpcRsp = NULL;
22,402,749✔
1492
  pTrans->rpcRspLen = 0;
22,402,749✔
1493

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

1501
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
22,400,638✔
1502
  int32_t code = 0;
22,400,638✔
1503
  mInfo("trans:%d, commit transaction", pTrans->id);
22,400,638✔
1504
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
22,400,638✔
1505
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
2,336✔
1506
    TAOS_RETURN(code);
2,336✔
1507
  }
1508
  mInfo("trans:%d, commit finished", pTrans->id);
22,398,302✔
1509
  TAOS_RETURN(code);
22,398,302✔
1510
}
1511

1512
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
1,706✔
1513
  int32_t code = 0;
1,706✔
1514
  mInfo("trans:%d, rollback transaction", pTrans->id);
1,706✔
1515
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,706✔
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,706✔
1520
  TAOS_RETURN(code);
1,706✔
1521
}
1522

1523
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
1,847✔
1524
  int32_t code = 0;
1,847✔
1525
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
1,847✔
1526
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,847✔
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,847✔
1531
  TAOS_RETURN(code);
1,847✔
1532
}
1533

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

1538
  if (pTrans->stage == TRN_STAGE_FINISH) {
102,133,767✔
1539
    sendRsp = true;
46,870,034✔
1540
  }
1541

1542
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
102,133,767✔
1543
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
27,183,673✔
1544
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
10,236✔
1545
      sendRsp = true;
10,236✔
1546
    }
1547
  } else {
1548
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
74,950,094✔
1549
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
42,209,886✔
1550
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1551
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1552
      } else {
1553
        if (pTrans->failedTimes > 6) sendRsp = true;
42,209,886✔
1554
      }
1555
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
42,209,886✔
1556
    }
1557
  }
1558

1559
  if (!sendRsp) {
102,133,767✔
1560
    return;
55,253,497✔
1561
  } else {
1562
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
46,880,270✔
1563
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1564
  }
1565

1566
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
46,880,270✔
1567
  taosWLockLatch(&pTrans->lockRpcArray);
46,880,270✔
1568
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
46,880,270✔
1569
  if (size <= 0) {
46,880,270✔
1570
    taosWUnLockLatch(&pTrans->lockRpcArray);
31,752,553✔
1571
    return;
31,752,553✔
1572
  }
1573

1574
  for (int32_t i = 0; i < size; ++i) {
30,255,434✔
1575
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
15,127,717✔
1576
    if (pInfo->handle != NULL) {
15,127,717✔
1577
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
14,472,934✔
1578
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1579
      }
1580
      if (code == TSDB_CODE_SYN_TIMEOUT) {
14,472,934✔
1581
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1582
      }
1583

1584
      if (i != 0 && code == 0) {
14,472,934✔
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,
14,472,934✔
1588
            mndTransStr(pTrans->stage), pInfo->ahandle);
1589

1590
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
14,472,934✔
1591

1592
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
14,472,934✔
1593
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,549,711✔
1594
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,549,711✔
1595
        if (pDb != NULL) {
1,549,711✔
1596
          for (int32_t j = 0; j < 12; j++) {
1,952,071✔
1597
            bool ready = mndIsDbReady(pMnode, pDb);
1,950,395✔
1598
            if (!ready) {
1,950,395✔
1599
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
402,360✔
1600
              taosMsleep(1000);
402,360✔
1601
            } else {
1602
              break;
1,548,035✔
1603
            }
1604
          }
1605
        }
1606
        mndReleaseDb(pMnode, pDb);
1,549,711✔
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
12,923,223✔
1608
        void   *pCont = NULL;
2,305,600✔
1609
        int32_t contLen = 0;
2,305,600✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,305,600✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,303,894✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
10,617,623✔
1614
        void   *pCont = NULL;
10,952✔
1615
        int32_t contLen = 0;
10,952✔
1616
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
10,952✔
1617
          mndTransSetRpcRsp(pTrans, pCont, contLen);
10,952✔
1618
        }
1619
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
10,606,671✔
1620
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
10,886✔
1621
        if (code != 0) {
10,886✔
1622
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
10,595,785✔
1625
        void   *pCont = NULL;
23,505✔
1626
        int32_t contLen = 0;
23,505✔
1627
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
23,505✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
23,505✔
1629
        }
1630
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
10,572,280✔
1631
        void   *pCont = NULL;
22,639✔
1632
        int32_t contLen = 0;
22,639✔
1633
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
22,639✔
1634
          mndTransSetRpcRsp(pTrans, pCont, contLen);
22,639✔
1635
        }
1636
      }
1637

1638
      if (pTrans->rpcRspLen != 0) {
14,472,934✔
1639
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
8,557,698✔
1640
        if (rpcCont != NULL) {
8,557,698✔
1641
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
8,557,698✔
1642
          rspMsg.pCont = rpcCont;
8,557,698✔
1643
          rspMsg.contLen = pTrans->rpcRspLen;
8,557,698✔
1644
        }
1645
      }
1646

1647
      tmsgSendRsp(&rspMsg);
14,472,934✔
1648

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

1657
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
25,479,517✔
1658
  int32_t code = 0;
25,479,517✔
1659
  SMnode *pMnode = pRsp->info.node;
25,479,517✔
1660
#ifndef TD_ASTRA_32
1661
  int64_t signature = (int64_t)(pRsp->info.ahandle);
25,479,517✔
1662
  int32_t transId = (int32_t)(signature >> 32);
25,479,517✔
1663
  int32_t action = (int32_t)((signature << 32) >> 32);
25,479,517✔
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);
25,479,517✔
1669
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
25,479,517✔
1670
  if (pTrans == NULL) {
25,479,517✔
1671
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1672
    if (terrno != 0) code = terrno;
×
1673
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1674
    goto _OVER;
×
1675
  }
1676

1677
  SArray *pArray = NULL;
25,479,517✔
1678
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
25,479,517✔
1679
    pArray = pTrans->redoActions;
25,472,693✔
1680
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
6,824✔
1681
    pArray = pTrans->undoActions;
6,824✔
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) {
25,479,517✔
1688
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1689
    goto _OVER;
×
1690
  }
1691

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

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

1705
      // pTrans->lastErrorNo = pRsp->code;
1706
      mndSetTransLastAction(pTrans, pAction);
25,479,517✔
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,
25,479,517✔
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);
25,479,517✔
1723
  mndTransExecute(pMnode, pTrans, true);
25,479,517✔
1724

1725
_OVER:
25,479,517✔
1726
  mndReleaseTrans(pMnode, pTrans);
25,479,517✔
1727
  TAOS_RETURN(code);
25,479,517✔
1728
}
1729

1730
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
2,398,392✔
1731
  pAction->rawWritten = 0;
2,398,392✔
1732
  pAction->msgSent = 0;
2,398,392✔
1733
  pAction->msgReceived = 0;
2,398,392✔
1734
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
2,398,392✔
1735
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
2,398,392✔
1736
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
1,361✔
1737
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
1,361✔
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,397,031✔
1741
  }
1742
  //  pAction->errCode = 0;
1743
}
2,398,392✔
1744

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

1748
  for (int32_t action = 0; action < numOfActions; ++action) {
8,530✔
1749
    STransAction *pAction = taosArrayGet(pArray, action);
6,824✔
1750
    if (pAction->msgSent && pAction->msgReceived &&
6,824✔
1751
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
6,824✔
1752
      continue;
5,118✔
1753
    if (pAction->msgSent && !pAction->msgReceived) {
1,706✔
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,706✔
1759

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

1766
// execute in sync context
1767
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
190,462,570✔
1768
  if (pAction->rawWritten) return 0;
190,462,570✔
1769
  if (topHalf) {
104,627,811✔
1770
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
1,625✔
1771
  }
1772

1773
  if (pAction->pRaw->type >= SDB_MAX) {
104,626,186✔
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);
104,626,186✔
1789
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
104,626,186✔
1790
    pAction->rawWritten = true;
104,626,186✔
1791
    pAction->errCode = 0;
104,626,186✔
1792
    code = 0;
104,626,186✔
1793
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
104,626,186✔
1794
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1795

1796
    mndSetTransLastAction(pTrans, pAction);
104,626,186✔
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);
104,626,186✔
1805
}
1806

1807
// execute in trans context
1808
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
110,207,593✔
1809
                                     bool notSend) {
1810
  if (pAction->msgSent) return 0;
110,207,593✔
1811
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
41,357,602✔
1812
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
15,867,626✔
1813
  }
1814

1815
  if (notSend) {
25,489,976✔
1816
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
8,073✔
1817
    return 0;
8,073✔
1818
  }
1819

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

1825
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
25,481,903✔
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);
25,481,903✔
1833
  if (rpcMsg.pCont == NULL) {
25,481,903✔
1834
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1835
    return -1;
×
1836
  }
1837
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
25,481,903✔
1838
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
25,481,903✔
1839
  rpcMsg.info.notFreeAhandle = 1;
25,481,903✔
1840
  STraceId* trace = &(rpcMsg.info.traceId);
25,481,903✔
1841

1842
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
25,481,903✔
1843

1844
  char    detail[1024] = {0};
25,481,903✔
1845
  int32_t len = snprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
25,481,903✔
1846
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
25,481,903✔
1847
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
54,280,448✔
1848
    len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
28,798,545✔
1849
                    pAction->epSet.eps[i].port);
28,798,545✔
1850
  }
1851

1852
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
25,481,903✔
1853
  if (code == 0) {
25,481,903✔
1854
    pAction->msgSent = 1;
25,481,903✔
1855
    // pAction->msgReceived = 0;
1856
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
25,481,903✔
1857
    pAction->startTime = taosGetTimestampMs();
25,481,903✔
1858
    pAction->endTime = 0;
25,481,903✔
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, 
25,481,903✔
1860
              trace ? trace->msgId : 0, detail);
1861

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

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

1873
  TAOS_RETURN(code);
25,481,903✔
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,
300,670,163✔
1887
                                        bool notSend) {
1888
  if (pAction->actionType == TRANS_ACTION_RAW) {
300,670,163✔
1889
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
190,462,570✔
1890
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
110,207,593✔
1891
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
110,207,593✔
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) {
92,163,405✔
1898
  int32_t numOfActions = taosArrayGetSize(pArray);
92,163,405✔
1899
  int32_t code = 0;
92,163,405✔
1900

1901
  for (int32_t action = 0; action < numOfActions; ++action) {
351,152,730✔
1902
    STransAction *pAction = taosArrayGet(pArray, action);
271,488,774✔
1903
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
271,488,774✔
1904
    if (code != 0) {
271,488,774✔
1905
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
12,499,449✔
1906
            numOfActions);
1907
      break;
12,499,449✔
1908
    }
1909
  }
1910

1911
  return code;
92,163,405✔
1912
}
1913

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

1919
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
92,163,405✔
1920
    return code;
12,499,449✔
1921
  }
1922

1923
  int32_t       numOfExecuted = 0;
79,663,956✔
1924
  int32_t       errCode = 0;
79,663,956✔
1925
  STransAction *pErrAction = NULL;
79,663,956✔
1926
  for (int32_t action = 0; action < numOfActions; ++action) {
338,653,281✔
1927
    STransAction *pAction = taosArrayGet(pArray, action);
258,989,325✔
1928
    if (pAction->msgReceived || pAction->rawWritten) {
258,989,325✔
1929
      numOfExecuted++;
216,513,811✔
1930
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
216,513,811✔
1931
        errCode = pAction->errCode;
3,056✔
1932
        pErrAction = pAction;
3,056✔
1933
      }
1934
    } else {
1935
      pErrAction = pAction;
42,475,514✔
1936
    }
1937
  }
1938

1939
  mndSetTransLastAction(pTrans, pErrAction);
79,663,956✔
1940

1941
  if (numOfExecuted == numOfActions) {
79,663,956✔
1942
    if (errCode == 0) {
58,099,587✔
1943
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
58,097,881✔
1944
      return 0;
58,097,881✔
1945
    } else {
1946
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,706✔
1947
      mndTransResetActions(pMnode, pTrans, pArray);
1,706✔
1948
      terrno = errCode;
1,706✔
1949
      return errCode;
1,706✔
1950
    }
1951
  } else {
1952
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
21,564,369✔
1953

1954
    for (int32_t action = 0; action < numOfActions; ++action) {
83,861,555✔
1955
      STransAction *pAction = taosArrayGet(pArray, action);
62,297,186✔
1956
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
62,297,186✔
1957
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1958
             pAction->acceptableCode, pAction->retryCode);
1959
      if (pAction->msgSent) {
62,297,186✔
1960
        bool reset = false;
62,289,113✔
1961
        if (pAction->msgReceived) {
62,289,113✔
1962
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
19,821,672✔
1963
        } else {
1964
          int64_t timestamp = taosGetTimestampMs();
42,467,441✔
1965
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
42,467,441✔
1966
        }
1967
        if (reset) {
62,289,113✔
1968
          mndTransResetAction(pMnode, pTrans, pAction);
1,350✔
1969
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1,350✔
1970
                pAction->id, pAction->errCode, pAction->startTime);
1971
        }
1972
      }
1973
    }
1974
    return TSDB_CODE_ACTION_IN_PROGRESS;
21,564,369✔
1975
  }
1976
}
1977

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

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

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

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

2010
  if (pTrans->actionPos >= numOfActions) {
8,437,863✔
2011
    return code;
378,856✔
2012
  }
2013

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

2017
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
12,735,518✔
2018
    STransAction *pAction = taosArrayGet(pActions, action);
12,406,055✔
2019

2020
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
12,406,055✔
2021
      code = TSDB_CODE_ACTION_IN_PROGRESS;
8,660✔
2022
      break;
8,822✔
2023
    }
2024

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

2028
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
12,397,395✔
2029
    if (code == 0) {
12,397,395✔
2030
      if (pAction->msgSent) {
10,545,166✔
2031
        bool reset = false;
9,250,530✔
2032
        if (pAction->msgReceived) {
9,250,530✔
2033
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
4,055,294✔
2034
            code = pAction->errCode;
2,335,901✔
2035
            reset = true;
2,335,901✔
2036
          } else {
2037
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,719,393✔
2038
          }
2039
        } else {
2040
          int64_t timestamp = taosGetTimestampMs();
5,195,236✔
2041
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
5,195,236✔
2042
          code = TSDB_CODE_ACTION_IN_PROGRESS;
5,195,236✔
2043
        }
2044
        if (reset) {
9,250,530✔
2045
          mndTransResetAction(pMnode, pTrans, pAction);
2,335,901✔
2046
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,335,901✔
2047
                pAction->id, pAction->errCode, pAction->startTime);
2048
        }
2049
      } else if (pAction->rawWritten) {
1,294,636✔
2050
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,294,636✔
2051
          code = pAction->errCode;
×
2052
        } else {
2053
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,294,636✔
2054
        }
2055
      } else {
2056
      }
2057
    }
2058

2059
    if (code == 0) {
12,397,395✔
2060
      pTrans->failedTimes = 0;
3,014,029✔
2061
    }
2062
    mndSetTransLastAction(pTrans, pAction);
12,397,395✔
2063
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
12,397,395✔
2064
      mInfo(
7,047,465✔
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,349,930✔
2070
      mError(
2,335,901✔
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,397,395✔
2078
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
12,397,395✔
2079
      pTrans->lastErrorNo = code;
2,525,648✔
2080
      pTrans->code = code;
2,525,648✔
2081
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
2,525,648✔
2082
            action, str);
2083
      break;
2,525,648✔
2084
    }
2085

2086
    if (code == 0) {
9,871,747✔
2087
      pTrans->code = 0;
2,340,610✔
2088
      pTrans->actionPos++;
2,340,610✔
2089
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
2,340,610✔
2090
            pAction->id);
2091
      (void)taosThreadMutexUnlock(&pTrans->mutex);
2,340,610✔
2092
      code = mndTransSync(pMnode, pTrans);
2,340,610✔
2093
      (void)taosThreadMutexLock(&pTrans->mutex);
2,340,610✔
2094
      if (code != 0) {
2,340,610✔
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,531,137✔
2102
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
5,195,236✔
2103
      break;
5,195,236✔
2104
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
2,335,901✔
2105
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
1,118✔
2106
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
2,335,901✔
2107
            code, tstrerror(code));
2108
      pTrans->lastErrorNo = code;
2,335,901✔
2109
      taosMsleep(300);
2,335,901✔
2110
      action--;
2,335,901✔
2111
      continue;
2,335,901✔
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;
8,059,007✔
2123
}
2124

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

2132
  if (groupId <= 0) {
4,594,807✔
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,594,807✔
2138
  if (actionPos == NULL) {
4,594,807✔
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,594,807✔
2144
    mInfo("trans:%d, this serial group is finished, actionPos:%d >= numOfActions:%d at group %d", pTrans->id,
687,824✔
2145
          *actionPos, numOfActions, groupId);
2146
    return TSDB_CODE_MND_TRANS_GROUP_FINISHED;
687,824✔
2147
  }
2148

2149
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
4,549,929✔
2150
    STransAction **ppAction = taosArrayGet(pActions, action);
4,287,858✔
2151
    STransAction  *pAction = *ppAction;
4,287,858✔
2152

2153
    if (notSend && !pAction->msgSent) {
4,287,858✔
2154
      mInfo(
596,235✔
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;
596,235✔
2160
      break;
596,235✔
2161
    }
2162

2163
    mInfo(
3,691,623✔
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,691,623✔
2170
    if (code == 0) {
3,691,623✔
2171
      if (pAction->msgSent) {
2,174,050✔
2172
        if (pAction->msgReceived) {
1,943,546✔
2173
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
574,168✔
2174
            code = pAction->errCode;
59,435✔
2175
            mndTransResetAction(pMnode, pTrans, pAction);
59,435✔
2176
          } else {
2177
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
514,733✔
2178
                  pAction->id, action, numOfActions, groupId);
2179
          }
2180
        } else {
2181
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,369,378✔
2182
        }
2183
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,943,546✔
2184
        if (msgSent != NULL) {
1,943,546✔
2185
          *msgSent = pAction->msgSent;
1,943,546✔
2186
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,943,546✔
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) {
230,504✔
2191
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
230,504✔
2192
          code = pAction->errCode;
×
2193
        } else {
2194
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
230,504✔
2195
        }
2196
      } else {
2197
      }
2198
    }
2199

2200
    if (code == 0) {
3,691,623✔
2201
      pTrans->failedTimes = 0;
745,237✔
2202
    }
2203
    mndSetTransLastAction(pTrans, pAction);
3,691,623✔
2204

2205
    char str[200] = {0};
3,691,623✔
2206
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
3,691,623✔
2207
      pTrans->lastErrorNo = code;
1,679,299✔
2208
      pTrans->code = code;
1,679,299✔
2209
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
1,679,299✔
2210
        mInfo(
1,517,573✔
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) {
161,726✔
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,679,299✔
2223
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2224
      break;
1,679,299✔
2225
    }
2226

2227
    if (code == 0) {
2,012,324✔
2228
      pTrans->code = 0;
583,511✔
2229
      pTrans->actionPos++;
583,511✔
2230
      (*actionPos)++;
583,511✔
2231
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
583,511✔
2232
            pAction->id);
2233
      (void)taosThreadMutexUnlock(&pTrans->mutex);
583,511✔
2234
      code = mndTransSync(pMnode, pTrans);
583,511✔
2235
      (void)taosThreadMutexLock(&pTrans->mutex);
583,511✔
2236
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
583,511✔
2237
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
13,839,178✔
2238
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
13,255,667✔
2239
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
13,255,667✔
2240
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
13,255,667✔
2241
          pTmpAction->msgSent = *msgSent;
676,807✔
2242
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
676,807✔
2243
        }
2244
      }
2245
      if (code != 0) {
583,511✔
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,428,813✔
2254
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,369,378✔
2255
            mndTransStr(pAction->stage), pAction->id);
2256
      break;
1,369,378✔
2257
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
59,435✔
2258
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
243✔
2259
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
59,435✔
2260
            code, tstrerror(code));
2261
      pTrans->lastErrorNo = code;
59,435✔
2262
      taosMsleep(300);
59,435✔
2263
      action--;
59,435✔
2264
      continue;
59,435✔
2265
    } else {
2266
      terrno = code;
×
2267
      pTrans->lastErrorNo = code;
×
2268
      pTrans->code = code;
×
2269
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2270
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2271
      break;
×
2272
    }
2273
  }
2274

2275
  return code;
3,906,983✔
2276
}
2277

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

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

2294
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,495,164✔
2295
  if(pHash == NULL){
1,495,164✔
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) {
33,955,537✔
2300
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
32,460,373✔
2301
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
32,460,373✔
2302
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
32,460,373✔
2303
  }
2304
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
1,495,164✔
2305

2306
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,495,164✔
2307
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,495,164✔
2308
  int32_t currentGroup = 1;
1,495,164✔
2309
  while (pIter) {
6,089,971✔
2310
    SArray **redoActions = pIter;
4,594,807✔
2311
    size_t   keyLen = 0;
4,594,807✔
2312
    int32_t *key = taosHashGetKey(pIter, &keyLen);
4,594,807✔
2313
    int32_t  actionCount = taosArrayGetSize(*redoActions);
4,594,807✔
2314
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
4,594,807✔
2315
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2316
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
4,594,807✔
2317
                                             notSend, pHash);
2318
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
4,594,807✔
2319
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,517,573✔
2320
            tstrerror(code));
2321
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
3,077,234✔
2322
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,965,613✔
2323
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
1,111,621✔
2324
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
687,824✔
2325
    } else if (code != 0) {
423,797✔
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++;
423,797✔
2330
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
423,797✔
2331
    }
2332
    currentGroup++;
4,594,807✔
2333
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,594,807✔
2334
  }
2335

2336
  taosHashCleanup(pHash);
1,495,164✔
2337

2338
  if (successCount == groupCount) {
1,495,164✔
2339
    total_code = 0;
30,610✔
2340
  } else {
2341
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,464,554✔
2342
  }
2343

2344
  return total_code;
1,495,164✔
2345
}
2346

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

2351
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,746,593✔
2352
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,746,593✔
2353
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,746,593✔
2354
      code = 0;
215,218✔
2355
    } else {
2356
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,531,375✔
2357
      if (pAction != NULL && pAction->groupId == -1) {
1,531,375✔
2358
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
36,211✔
2359
      } else {
2360
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,495,164✔
2361
        if (code == 0) {
1,495,164✔
2362
          if (pTrans->actionPos < numOfActions) {
30,610✔
2363
            code = TSDB_CODE_ACTION_IN_PROGRESS;
21,958✔
2364
          }
2365
        }
2366
      }
2367
    }
2368
  }
2369

2370
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,746,593✔
2371

2372
  return code;
1,746,593✔
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) {
24,475,819✔
2386
  bool    continueExec = true;
24,475,819✔
2387
  int32_t code = 0;
24,475,819✔
2388
  terrno = 0;
24,475,819✔
2389

2390
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
24,475,819✔
2391
  if (numOfActions == 0) goto _OVER;
24,475,819✔
2392

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

2395
  for (int32_t action = 0; action < numOfActions; ++action) {
22,683,493✔
2396
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
13,092,371✔
2397
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
13,092,371✔
2398
    if (code != 0) {
13,092,371✔
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,591,122✔
2407
  pTrans->stage = TRN_STAGE_REDO_ACTION;
24,475,819✔
2408
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
24,475,819✔
2409
  return continueExec;
24,475,819✔
2410
}
2411

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

2417
  if (pTrans->exec == TRN_EXEC_SERIAL) {
77,653,781✔
2418
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
8,406,178✔
2419
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
69,247,603✔
2420
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
67,501,010✔
2421
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,746,593✔
2422
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,746,593✔
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 &&
77,655,487✔
2428
      mndTransIsInSyncContext(topHalf)) {
1,706✔
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)) {
77,653,781✔
2439
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
27,624,535✔
2440
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2441
      return false;
27,624,535✔
2442
    }
2443
  }
2444
  terrno = code;
50,029,246✔
2445

2446
  if (code == 0) {
50,029,246✔
2447
    pTrans->code = 0;
22,400,638✔
2448
    pTrans->stage = TRN_STAGE_COMMIT;
22,400,638✔
2449
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
22,400,638✔
2450
    continueExec = true;
22,400,638✔
2451
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
27,628,608✔
2452
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
27,626,902✔
2453
    continueExec = false;
27,626,902✔
2454
  } else {
2455
    pTrans->failedTimes++;
1,706✔
2456
    pTrans->code = terrno;
1,706✔
2457
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
1,706✔
2458
      if (pTrans->lastAction != 0) {
1,706✔
2459
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
1,706✔
2460
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
1,706✔
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,706✔
2472
      pTrans->actionPos = 0;
1,706✔
2473
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
1,706✔
2474
             pTrans->actionPos);
2475
      continueExec = true;
1,706✔
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;
50,029,246✔
2483
}
2484

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

2489
  bool    continueExec = true;
22,400,638✔
2490
  int32_t code = mndTransCommit(pMnode, pTrans);
22,400,638✔
2491

2492
  if (code == 0) {
22,400,638✔
2493
    pTrans->code = 0;
22,398,302✔
2494
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
22,398,302✔
2495
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
22,398,302✔
2496
    continueExec = true;
22,398,302✔
2497
  } else {
2498
    pTrans->code = terrno;
2,336✔
2499
    pTrans->failedTimes++;
2,336✔
2500
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
2,336✔
2501
    continueExec = false;
2,336✔
2502
  }
2503

2504
  return continueExec;
22,400,638✔
2505
}
2506

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

2511
  if (code == 0) {
46,866,064✔
2512
    pTrans->code = 0;
46,866,064✔
2513
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
46,866,064✔
2514
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
46,866,064✔
2515
    continueExec = true;
46,866,064✔
2516
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
2517
    pTrans->code = 0;
×
2518
    pTrans->stage = TRN_STAGE_COMMIT;
×
2519
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
2520
    continueExec = true;
×
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;
46,866,064✔
2529
}
2530

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

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

2541
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
11,942✔
2542
  terrno = code;
10,236✔
2543

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

2557
  return continueExec;
10,236✔
2558
}
2559

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

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

2567
  if (code == 0) {
1,706✔
2568
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1,706✔
2569
    continueExec = true;
1,706✔
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,706✔
2577
}
2578

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

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

2586
  if (code == 0) {
1,847✔
2587
    pTrans->stage = TRN_STAGE_FINISH;
1,847✔
2588
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
1,847✔
2589
    continueExec = true;
1,847✔
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,847✔
2597
}
2598

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

2603
  SSdbRaw *pRaw = mndTransEncode(pTrans);
24,469,609✔
2604
  if (pRaw == NULL) {
24,469,609✔
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));
24,469,609✔
2609

2610
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
24,469,609✔
2611
  if (code != 0) {
24,469,609✔
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,
24,469,609✔
2616
        pTrans->failedTimes, pTrans->createdTime);
2617
  return continueExec;
24,469,609✔
2618
}
2619

2620
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
102,133,767✔
2621
  bool continueExec = true;
102,133,767✔
2622

2623
  while (continueExec) {
295,939,503✔
2624
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
193,805,736✔
2625
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2626
    pTrans->lastExecTime = taosGetTimestampMs();
193,805,736✔
2627
    switch (pTrans->stage) {
193,805,736✔
2628
      case TRN_STAGE_PREPARE:
×
2629
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2630
        break;
×
2631
      case TRN_STAGE_REDO_ACTION:
77,653,781✔
2632
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
77,653,781✔
2633
        break;
77,653,781✔
2634
      case TRN_STAGE_COMMIT:
22,400,638✔
2635
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
22,400,638✔
2636
        break;
22,400,638✔
2637
      case TRN_STAGE_COMMIT_ACTION:
46,866,064✔
2638
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
46,866,064✔
2639
        break;
46,866,064✔
2640
      case TRN_STAGE_ROLLBACK:
1,706✔
2641
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,706✔
2642
        break;
1,706✔
2643
      case TRN_STAGE_UNDO_ACTION:
11,942✔
2644
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
11,942✔
2645
        break;
11,942✔
2646
      case TRN_STAGE_PRE_FINISH:
1,847✔
2647
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,847✔
2648
        break;
1,847✔
2649
      case TRN_STAGE_FINISH:
46,869,758✔
2650
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
46,869,758✔
2651
        break;
46,869,758✔
2652
      default:
×
2653
        continueExec = false;
×
2654
        break;
×
2655
    }
2656
  }
2657

2658
  mndTransSendRpcRsp(pMnode, pTrans);
102,133,767✔
2659
}
102,133,767✔
2660

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

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

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

2679
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
141✔
2680
  SArray *pArray = NULL;
141✔
2681
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
141✔
2682
    pArray = pTrans->redoActions;
141✔
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){
141✔
2690
    if(pTrans->ableToBeKilled == false){
141✔
2691
      return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2692
    }
2693
  }
2694
  
2695
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
141✔
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){
141✔
2706
    pTrans->stage = TRN_STAGE_PRE_FINISH;
141✔
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);
141✔
2713
  mndTransExecute(pMnode, pTrans, true);
141✔
2714
  return 0;
141✔
2715
}
2716

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

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

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

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

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

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

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

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

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

2758
  void *pIter = NULL;
18,177,597✔
2759
  while (1) {
2,181,506✔
2760
    STrans *pTrans = NULL;
20,359,103✔
2761
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
20,359,103✔
2762
    if (pIter == NULL) break;
20,359,103✔
2763
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
4,363,012✔
2764
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2765
    }
2766
    sdbRelease(pSdb, pTrans);
2,181,506✔
2767
  }
2768

2769
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
18,177,597✔
2770

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

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

2794
  struct tm tm;
20,969,104✔
2795
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
20,969,104✔
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);
20,969,104✔
2800

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

2809
  return buf;
20,969,104✔
2810
}
2811

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

2817
  if (pTrans->stage == TRN_STAGE_PREPARE) {
513,570✔
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) {
513,570✔
2829
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
14,229,692✔
2830
      len = 0;
13,719,327✔
2831
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
13,719,327✔
2832
      if (pAction->actionType == TRANS_ACTION_MSG) {
13,719,327✔
2833
        char bufStart[40] = {0};
10,463,936✔
2834
        (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
10,463,936✔
2835

2836
        char endStart[40] = {0};
10,463,936✔
2837
        (void)formatTimestamp(endStart, sizeof(endStart), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
10,463,936✔
2838
        len += snprintf(detail + len, sizeof(detail) - len,
20,927,872✔
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,
20,927,872✔
2842
                        pAction->msgReceived, bufStart, endStart);
10,463,936✔
2843

2844
        SEpSet epset = pAction->epSet;
10,463,936✔
2845
        if (epset.numOfEps > 0) {
10,463,936✔
2846
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
10,463,936✔
2847
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
23,467,044✔
2848
            len +=
13,003,108✔
2849
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
13,003,108✔
2850
          }
2851
        }
2852

2853
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
10,463,936✔
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,255,391✔
2857
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
3,255,391✔
2858
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
3,255,391✔
2859
      }
2860
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
13,719,327✔
2861
    }
2862
  }
2863

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

2874
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
370✔
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
}
513,570✔
2890

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

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

2904
    cols = 0;
513,570✔
2905

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

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

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

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

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

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

2933
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
513,570✔
2934
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
513,570✔
2935
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
513,570✔
2936
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
513,570✔
2937
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
513,570✔
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++);
513,570✔
2948
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
513,570✔
2949
                        _OVER);
2950

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

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

2971
    mndTransLogAction(pTrans);
513,570✔
2972

2973
    numOfRows++;
513,570✔
2974
    sdbRelease(pSdb, pTrans);
513,570✔
2975
  }
2976

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

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

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

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

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

3016
  cols = 0;
80,399✔
3017

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

3020
  if (pAction->actionType == TRANS_ACTION_MSG) {
80,399✔
3021
    int32_t len = 0;
66,063✔
3022

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

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

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

3054
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
66,063✔
3055
    len = 0;
66,063✔
3056
    char bufStart[40] = {0};
66,063✔
3057
    if (pAction->startTime > 0)
66,063✔
3058
      (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
22,018✔
3059
    char bufEnd[40] = {0};
66,063✔
3060
    if (pAction->endTime > 0)
66,063✔
3061
      (void)formatTimestamp(bufEnd, sizeof(bufEnd), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
19,214✔
3062

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

3069
  } else {
3070
    int32_t len = 0;
14,336✔
3071

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

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

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

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

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

3113
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
1,037✔
3114
  if (stage == TRN_STAGE_PREPARE) {
1,037✔
3115
    return pTrans->prepareActions;
×
3116
  }
3117
  if (stage == TRN_STAGE_REDO_ACTION) {
1,037✔
3118
    return pTrans->redoActions;
1,037✔
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,
1,037✔
3137
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3138
  int32_t actionNum = taosArrayGetSize(pActions);
1,037✔
3139
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
1,037✔
3140

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

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

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

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

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

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

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

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

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

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

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

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

3207
  if (code != 0) {
1,626✔
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,626✔
3211
  }
3212
  if (numOfRows == 0) {
1,626✔
3213
    taosMemoryFree(pShow->pIter);
589✔
3214
    pShow->pIter = NULL;
589✔
3215
  }
3216
  return numOfRows;
1,626✔
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