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

taosdata / TDengine / #5062

17 May 2026 01:15AM UTC coverage: 73.408%. Remained the same
#5062

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)

281738 of 383795 relevant lines covered (73.41%)

133896459.32 hits per line

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

83.72
/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; }
154,910,858✔
66

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

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

83
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
223,991,660✔
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) {
545,182✔
96
  SSdbTable table = {
545,182✔
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);
545,182✔
107
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
545,182✔
108

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

115
void mndCleanupTrans(SMnode *pMnode) {}
545,112✔
116

117
static int32_t mndTransGetActionsSize(SArray *pArray) {
292,033,540✔
118
  int32_t actionNum = taosArrayGetSize(pArray);
292,033,540✔
119
  int32_t rawDataLen = 0;
292,033,540✔
120

121
  for (int32_t i = 0; i < actionNum; ++i) {
782,118,492✔
122
    STransAction *pAction = taosArrayGet(pArray, i);
490,084,952✔
123
    if (pAction->actionType == TRANS_ACTION_RAW) {
490,084,952✔
124
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
336,119,146✔
125
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
153,965,806✔
126
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
153,965,806✔
127
    } else {
128
      // empty
129
    }
130
    rawDataLen += sizeof(int8_t);
490,084,952✔
131
  }
132

133
  return rawDataLen;
292,033,540✔
134
}
135

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

144
  for (int32_t i = 0; i < actionsNum; ++i) {
782,118,492✔
145
    STransAction *pAction = taosArrayGet(pActions, i);
490,084,952✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
490,084,952✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
490,084,952✔
148
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
490,084,952✔
149
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
490,084,952✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
490,084,952✔
151
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
490,084,952✔
152
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
490,084,952✔
153
    if (sver > TRANS_VER2_NUMBER) {
490,084,952✔
154
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
490,084,952✔
155
    }
156
    if (pAction->actionType == TRANS_ACTION_RAW) {
490,084,952✔
157
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
336,119,146✔
158
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
336,119,146✔
159
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
336,119,146✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
336,119,146✔
161
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
153,965,806✔
162
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
153,965,806✔
163
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
153,965,806✔
164
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
153,965,806✔
165
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
153,965,806✔
166
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
153,965,806✔
167
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
153,965,806✔
168
    } else {
169
      // nothing
170
    }
171
  }
172
  ret = 0;
292,033,540✔
173

174
_OVER:
292,033,540✔
175
  *offset = dataPos;
292,033,540✔
176
  return ret;
292,033,540✔
177
}
178

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

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

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

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

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

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

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

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

237
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
73,008,385✔
238
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
73,008,385✔
239
  void *pIter = NULL;
73,008,385✔
240
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
73,008,385✔
241
  while (pIter) {
73,038,688✔
242
    int32_t arbGroupId = *(int32_t *)pIter;
30,303✔
243
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
30,303✔
244
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
30,303✔
245
  }
246

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

252
  if (sver > TRANS_VER2_NUMBER) {
73,008,385✔
253
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
73,008,385✔
254
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
73,008,385✔
255
    void *pIter = NULL;
73,008,385✔
256
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
73,008,385✔
257
    while (pIter) {
75,740,182✔
258
      size_t   keysize = 0;
2,731,797✔
259
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,731,797✔
260
      int32_t  groupPos = *(int32_t *)pIter;
2,731,797✔
261
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,731,797✔
262
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,731,797✔
263
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,731,797✔
264
    }
265
  }
266

267
  if (sver >= TRANS_VER_USER_DATA) {
73,008,385✔
268
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
73,008,385✔
269
    if (pTrans->userDataLen > 0) {
73,008,385✔
270
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
137,109✔
271
    }
272
  }
273

274
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
73,008,385✔
275
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
73,008,385✔
276

277
  terrno = 0;
73,008,385✔
278

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

291
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
32,674,978✔
292
  if (pAction->groupId < 0) return 0;
32,674,978✔
293
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
32,065,012✔
294
  if (redoAction == NULL) {
32,065,012✔
295
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
5,480,391✔
296
    if (array != NULL) {
5,480,391✔
297
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
5,480,391✔
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,480,391✔
303
  }
304
  if (redoAction != NULL) {
32,065,012✔
305
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
64,130,024✔
306
  }
307
  return 0;
32,065,012✔
308
}
309

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

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

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

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

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

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

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

451
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
130,792,938✔
452

453
  if (sver > TRANS_VER_CURRENT) {
130,792,938✔
454
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
455
    goto _OVER;
×
456
  }
457

458
  pRow = sdbAllocRow(sizeof(STrans));
130,792,938✔
459
  if (pRow == NULL) goto _OVER;
130,792,938✔
460

461
  pTrans = sdbGetRowObj(pRow);
130,792,938✔
462
  if (pTrans == NULL) goto _OVER;
130,792,938✔
463

464
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
130,792,938✔
465

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

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

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

506
  if (pTrans->prepareActions == NULL) goto _OVER;
130,792,938✔
507
  if (pTrans->redoActions == NULL) goto _OVER;
130,792,938✔
508
  if (pTrans->undoActions == NULL) goto _OVER;
130,792,938✔
509
  if (pTrans->commitActions == NULL) goto _OVER;
130,792,938✔
510

511
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
130,792,938✔
512
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
130,792,938✔
513
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,877,769✔
514
                                      sver) < 0)
515
      goto _OVER;
×
516
  } else {
517
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
128,915,169✔
518
  }
519
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
130,792,938✔
520
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
130,792,938✔
521

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

533
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
130,792,938✔
534
  if (arbgroupIdNum > 0) {
130,792,938✔
535
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
42,235✔
536
    if (pTrans->arbGroupIds == NULL) goto _OVER;
42,235✔
537
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
92,740✔
538
      int32_t arbGroupId = 0;
50,505✔
539
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
50,505✔
540
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
50,505✔
541
    }
542
  }
543

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

553
  if (sver > TRANS_VER2_NUMBER) {
130,792,938✔
554
    int32_t groupNum = -1;
130,792,938✔
555
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
130,792,938✔
556
    for (int32_t i = 0; i < groupNum; ++i) {
136,273,329✔
557
      int32_t groupId = -1;
5,480,391✔
558
      int32_t groupPos = -1;
5,480,391✔
559
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
5,480,391✔
560
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
5,480,391✔
561
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
5,480,391✔
562
        goto _OVER;
×
563
    }
564
  }
565

566
  if (sver >= TRANS_VER_USER_DATA) {
130,792,938✔
567
    SDB_GET_INT32(pRaw, dataPos, &pTrans->userDataLen, _OVER)
130,792,938✔
568
    if (pTrans->userDataLen > 0) {
130,792,938✔
569
      pTrans->userData = taosMemoryMalloc(pTrans->userDataLen);
229,135✔
570
      if (pTrans->userData == NULL) goto _OVER;
229,135✔
571
      SDB_GET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
229,135✔
572
    }
573
  }
574

575
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
130,792,938✔
576

577
  terrno = 0;
130,792,938✔
578

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

594
static const char *mndTransStr(ETrnStage stage) {
909,988,859✔
595
  switch (stage) {
909,988,859✔
596
    case TRN_STAGE_PREPARE:
72,858,144✔
597
      return "prepare";
72,858,144✔
598
    case TRN_STAGE_REDO_ACTION:
308,530,222✔
599
      return "redoAction";
308,530,222✔
600
    case TRN_STAGE_ROLLBACK:
8,530✔
601
      return "rollback";
8,530✔
602
    case TRN_STAGE_UNDO_ACTION:
13,948,016✔
603
      return "undoAction";
13,948,016✔
604
    case TRN_STAGE_COMMIT:
117,328,360✔
605
      return "commit";
117,328,360✔
606
    case TRN_STAGE_COMMIT_ACTION:
223,613,569✔
607
      return "commitAction";
223,613,569✔
608
    case TRN_STAGE_FINISH:
173,692,778✔
609
      return "finished";
173,692,778✔
610
    case TRN_STAGE_PRE_FINISH:
9,240✔
611
      return "pre-finish";
9,240✔
612
    default:
×
613
      return "invalid";
×
614
  }
615
}
616

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

628
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
253,728,459✔
629
  if (pAction != NULL) {
253,728,459✔
630
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
195,004,535✔
631
      pTrans->lastAction = pAction->id;
140,111,151✔
632
      pTrans->lastMsgType = pAction->msgType;
140,111,151✔
633
      pTrans->lastEpset = pAction->epSet;
140,111,151✔
634
      pTrans->lastErrorNo = pAction->errCode;
140,111,151✔
635
    }
636
  } else {
637
    pTrans->lastAction = 0;
58,723,924✔
638
    pTrans->lastMsgType = 0;
58,723,924✔
639
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
58,723,924✔
640
    pTrans->lastErrorNo = 0;
58,723,924✔
641
  }
642
}
253,728,459✔
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) {
696,300✔
653
  switch (ftype) {
696,300✔
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:
348,046✔
659
      return mndRebCntInc;
348,046✔
660
    case TRANS_STOP_FUNC_MQ_REB:
348,046✔
661
      return mndRebCntDec;
348,046✔
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,904,863✔
672
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
24,904,863✔
673
        mndTransStr(pTrans->stage), pTrans->startFunc);
674

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

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

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

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

703
void mndTransDropData(STrans *pTrans) {
153,666,373✔
704
  if (pTrans->prepareActions != NULL) {
153,666,373✔
705
    mndTransDropActions(pTrans->prepareActions);
153,666,373✔
706
    pTrans->prepareActions = NULL;
153,666,373✔
707
  }
708
  if (pTrans->redoActions != NULL) {
153,666,373✔
709
    mndTransDropActions(pTrans->redoActions);
153,666,373✔
710
    pTrans->redoActions = NULL;
153,666,373✔
711
  }
712
  if (pTrans->undoActions != NULL) {
153,666,373✔
713
    mndTransDropActions(pTrans->undoActions);
153,666,373✔
714
    pTrans->undoActions = NULL;
153,666,373✔
715
  }
716
  if (pTrans->commitActions != NULL) {
153,666,373✔
717
    mndTransDropActions(pTrans->commitActions);
153,666,373✔
718
    pTrans->commitActions = NULL;
153,666,373✔
719
  }
720
  if (pTrans->redoGroupActions != NULL) {
153,666,373✔
721
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
24,751,204✔
722
    while (pIter) {
30,498,929✔
723
      SArray **redoActions = pIter;
5,747,725✔
724
      taosArrayDestroy(*redoActions);
5,747,725✔
725
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
5,747,725✔
726
    }
727

728
    taosHashCleanup(pTrans->redoGroupActions);
24,751,204✔
729
    pTrans->redoGroupActions = NULL;
24,751,204✔
730
  }
731
  if (pTrans->groupActionPos != NULL) {
153,666,373✔
732
    taosHashCleanup(pTrans->groupActionPos);
24,751,204✔
733
    pTrans->groupActionPos = NULL;
24,751,204✔
734
  }
735
  if (pTrans->arbGroupIds != NULL) {
153,666,373✔
736
    taosHashCleanup(pTrans->arbGroupIds);
22,915,670✔
737
  }
738
  if (pTrans->pRpcArray != NULL) {
153,666,373✔
739
    taosArrayDestroy(pTrans->pRpcArray);
22,873,435✔
740
    pTrans->pRpcArray = NULL;
22,873,435✔
741
  }
742
  if (pTrans->rpcRsp != NULL) {
153,666,373✔
743
    taosMemoryFree(pTrans->rpcRsp);
8,675,968✔
744
    pTrans->rpcRsp = NULL;
8,675,968✔
745
    pTrans->rpcRspLen = 0;
8,675,968✔
746
  }
747
  if (pTrans->param != NULL) {
153,666,373✔
748
    taosMemoryFree(pTrans->param);
×
749
    pTrans->param = NULL;
×
750
    pTrans->paramLen = 0;
×
751
  }
752
  if (pTrans->userData != NULL) {
153,666,373✔
753
    taosMemoryFree(pTrans->userData);
229,303✔
754
    pTrans->userData = NULL;
229,303✔
755
    pTrans->userDataLen = 0;
229,303✔
756
  }
757
  (void)taosThreadMutexDestroy(&pTrans->mutex);
153,666,373✔
758
}
153,666,373✔
759

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

764
  if (pTrans->stopFunc > 0 && callFunc) {
77,846,444✔
765
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
348,254✔
766
    if (fp) {
348,254✔
767
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
348,254✔
768
    }
769
    // pTrans->stopFunc = 0;
770
  }
771

772
  mndTransDropData(pTrans);
77,846,444✔
773
  return 0;
77,846,444✔
774
}
775

776
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
112,275,544✔
777
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
333,312,493✔
778
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
221,036,949✔
779
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
221,036,949✔
780
    pOldAction->rawWritten = pNewAction->rawWritten;
221,036,949✔
781
    pOldAction->msgSent = pNewAction->msgSent;
221,036,949✔
782
    pOldAction->msgReceived = pNewAction->msgReceived;
221,036,949✔
783
    pOldAction->errCode = pNewAction->errCode;
221,036,949✔
784
  }
785
}
112,275,544✔
786

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

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

809
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
28,068,886✔
810
  while (pIter != NULL) {
30,393,547✔
811
    int32_t newActionPos = *(int32_t *)pIter;
2,324,661✔
812

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

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

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

825
  if (pOld->stage == TRN_STAGE_COMMIT) {
28,068,886✔
826
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
24,870,582✔
827
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
24,870,582✔
828
  }
829

830
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
28,068,886✔
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) {
28,068,886✔
836
    pOld->stage = TRN_STAGE_FINISH;
1,848✔
837
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pNew->id);
1,848✔
838
  }
839

840
  return 0;
28,068,886✔
841
}
842

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

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

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

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

892
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
22,873,435✔
893
      pTrans->pRpcArray == NULL) {
22,873,435✔
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,873,435✔
901
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
31,082,712✔
902
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
903
      return NULL;
×
904
    }
905
    pTrans->originRpcType = pReq->msgType;
15,541,356✔
906
  }
907

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

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

914
static void mndTransDropActions(SArray *pArray) {
614,665,492✔
915
  int32_t size = taosArrayGetSize(pArray);
614,665,492✔
916
  for (int32_t i = 0; i < size; ++i) {
1,635,313,420✔
917
    STransAction *pAction = taosArrayGet(pArray, i);
1,020,647,928✔
918
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,020,647,928✔
919
      taosMemoryFreeClear(pAction->pRaw);
695,831,027✔
920
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
324,816,901✔
921
      taosMemoryFreeClear(pAction->pCont);
324,816,901✔
922
    } else {
923
      // nothing
924
    }
925
  }
926

927
  taosArrayDestroy(pArray);
614,665,492✔
928
}
614,665,492✔
929

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

938
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
135,305,861✔
939
  pAction->id = taosArrayGetSize(pArray);
135,305,861✔
940

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

946
  return 0;
135,305,861✔
947
}
948

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

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

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

981
  return 0;
625,260✔
982
}
983

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1182
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
6,810,769✔
1183
  if (conflict[0] == 0) return false;
6,810,769✔
1184
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
6,472,144✔
1185
  return false;
6,292,842✔
1186
}
1187

1188
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
7,239,617✔
1189
  if (conflict) {
7,239,617✔
1190
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
265,521✔
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;
265,521✔
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,974,096✔
1197
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1198
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1199
  }
1200
}
7,239,617✔
1201

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

1207
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
60,074,099✔
1208

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

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

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

1218
    if (pNew->conflict == TRN_CONFLICT_DB) {
6,901,002✔
1219
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
365,881✔
1220
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
365,881✔
1221
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
338,625✔
1222
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
338,625✔
1223
      }
1224
    }
1225

1226
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
6,901,002✔
1227
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
6,494,005✔
1228
      if (pTrans->conflict == TRN_CONFLICT_DB) {
6,494,005✔
1229
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
90,223✔
1230
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
90,223✔
1231
      }
1232
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
6,494,005✔
1233
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
6,381,921✔
1234
      }
1235
    }
1236

1237
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
6,901,002✔
1238
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
34,357✔
1239
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
34,357✔
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,901,002✔
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,901,002✔
1264
  }
1265

1266
  return conflict;
45,241,218✔
1267
}
1268

1269
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
60,074,099✔
1270
  int32_t code = 0;
60,074,099✔
1271
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
60,074,099✔
1272
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
38,044,267✔
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)) {
60,074,099✔
1280
    code = TSDB_CODE_MND_TRANS_CONFLICT;
311,610✔
1281
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
311,610✔
1282
    TAOS_RETURN(code);
311,610✔
1283
  }
1284

1285
  TAOS_RETURN(code);
59,762,489✔
1286
}
1287

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

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

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

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

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

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

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

1333
  while ((pIter = sdbFetch(pSdb, SDB_RETENTION, pIter, (void **)&pRetention)) != NULL) {
171,413✔
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,413✔
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,413✔
1363
}
1364

1365
static bool mndTransActionsOfSameType(SArray *pActions) {
51,268,177✔
1366
  int32_t size = taosArrayGetSize(pActions);
51,268,177✔
1367
  ETrnAct lastActType = TRANS_ACTION_NULL;
51,268,177✔
1368
  bool    same = true;
51,268,177✔
1369
  for (int32_t i = 0; i < size; ++i) {
162,580,204✔
1370
    STransAction *pAction = taosArrayGet(pActions, i);
111,312,027✔
1371
    if (i > 0) {
111,312,027✔
1372
      if (lastActType != pAction->actionType) {
75,172,606✔
1373
        same = false;
×
1374
        break;
×
1375
      }
1376
    }
1377
    lastActType = pAction->actionType;
111,312,027✔
1378
  }
1379
  return same;
51,268,177✔
1380
}
1381

1382
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
22,531,146✔
1383
  int32_t code = 0;
22,531,146✔
1384
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
22,531,146✔
1385
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
22,077,367✔
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) {
22,077,367✔
1392
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
6,659,664✔
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,531,146✔
1401
}
1402

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

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

1425
  mInfo("trans:%d, action list:", pTrans->id);
22,532,236✔
1426
  int32_t index = 0;
22,532,236✔
1427
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
34,515,329✔
1428
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
11,983,093✔
1429
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
11,983,093✔
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,625,078✔
1434
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
24,092,842✔
1435
    if (pAction->actionType == TRANS_ACTION_MSG) {
24,092,842✔
1436
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
23,333,565✔
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),
759,277✔
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,760,190✔
1446
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
85,227,954✔
1447
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
85,227,954✔
1448
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1449
  }
1450

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

1462
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
22,532,236✔
1463

1464
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
22,531,146✔
1465

1466
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
22,531,146✔
1467

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

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

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

1501
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
22,528,841✔
1502
  int32_t code = 0;
22,528,841✔
1503
  mInfo("trans:%d, commit transaction", pTrans->id);
22,528,841✔
1504
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
22,528,841✔
1505
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
1,932✔
1506
    TAOS_RETURN(code);
1,932✔
1507
  }
1508
  mInfo("trans:%d, commit finished", pTrans->id);
22,526,909✔
1509
  TAOS_RETURN(code);
22,526,909✔
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,848✔
1524
  int32_t code = 0;
1,848✔
1525
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
1,848✔
1526
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,848✔
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,848✔
1531
  TAOS_RETURN(code);
1,848✔
1532
}
1533

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

1538
  if (pTrans->stage == TRN_STAGE_FINISH) {
103,427,521✔
1539
    sendRsp = true;
47,401,728✔
1540
  }
1541

1542
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
103,427,521✔
1543
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
27,366,534✔
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) {
76,060,987✔
1549
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
42,897,497✔
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,897,497✔
1554
      }
1555
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
42,897,497✔
1556
    }
1557
  }
1558

1559
  if (!sendRsp) {
103,427,521✔
1560
    return;
56,015,557✔
1561
  } else {
1562
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
47,411,964✔
1563
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1564
  }
1565

1566
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
47,411,964✔
1567
  taosWLockLatch(&pTrans->lockRpcArray);
47,411,964✔
1568
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
47,411,964✔
1569
  if (size <= 0) {
47,411,964✔
1570
    taosWUnLockLatch(&pTrans->lockRpcArray);
32,182,273✔
1571
    return;
32,182,273✔
1572
  }
1573

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

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

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

1592
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
14,573,810✔
1593
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,552,504✔
1594
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,552,504✔
1595
        if (pDb != NULL) {
1,552,504✔
1596
          for (int32_t j = 0; j < 12; j++) {
1,956,662✔
1597
            bool ready = mndIsDbReady(pMnode, pDb);
1,955,334✔
1598
            if (!ready) {
1,955,334✔
1599
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
404,158✔
1600
              taosMsleep(1000);
404,158✔
1601
            } else {
1602
              break;
1,551,176✔
1603
            }
1604
          }
1605
        }
1606
        mndReleaseDb(pMnode, pDb);
1,552,504✔
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
13,021,306✔
1608
        void   *pCont = NULL;
2,308,688✔
1609
        int32_t contLen = 0;
2,308,688✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,308,688✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,306,982✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
10,712,618✔
1614
        void   *pCont = NULL;
10,959✔
1615
        int32_t contLen = 0;
10,959✔
1616
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
10,959✔
1617
          mndTransSetRpcRsp(pTrans, pCont, contLen);
10,959✔
1618
        }
1619
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
10,701,659✔
1620
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
10,866✔
1621
        if (code != 0) {
10,866✔
1622
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
10,690,793✔
1625
        void   *pCont = NULL;
23,367✔
1626
        int32_t contLen = 0;
23,367✔
1627
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
23,367✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
23,367✔
1629
        }
1630
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
10,667,426✔
1631
        void   *pCont = NULL;
22,106✔
1632
        int32_t contLen = 0;
22,106✔
1633
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
22,106✔
1634
          mndTransSetRpcRsp(pTrans, pCont, contLen);
22,106✔
1635
        }
1636
      }
1637

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

1647
      tmsgSendRsp(&rspMsg);
14,573,810✔
1648

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

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

1677
  SArray *pArray = NULL;
25,749,699✔
1678
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
25,749,699✔
1679
    pArray = pTrans->redoActions;
25,742,875✔
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,749,699✔
1688
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1689
    goto _OVER;
×
1690
  }
1691

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

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

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

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

1730
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
2,453,955✔
1731
  pAction->rawWritten = 0;
2,453,955✔
1732
  pAction->msgSent = 0;
2,453,955✔
1733
  pAction->msgReceived = 0;
2,453,955✔
1734
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
2,453,955✔
1735
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
2,453,955✔
1736
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
381✔
1737
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
381✔
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,453,574✔
1741
  }
1742
  //  pAction->errCode = 0;
1743
}
2,453,955✔
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) {
191,299,046✔
1768
  if (pAction->rawWritten) return 0;
191,299,046✔
1769
  if (topHalf) {
105,347,878✔
1770
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
521✔
1771
  }
1772

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

1796
    mndSetTransLastAction(pTrans, pAction);
105,347,357✔
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);
105,347,357✔
1805
}
1806

1807
// execute in trans context
1808
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
111,082,938✔
1809
                                     bool notSend) {
1810
  if (pAction->msgSent) return 0;
111,082,938✔
1811
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
42,043,699✔
1812
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
16,276,519✔
1813
  }
1814

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

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

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

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

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

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

1862
    mndSetTransLastAction(pTrans, pAction);
25,755,689✔
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,755,689✔
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,
302,381,984✔
1887
                                        bool notSend) {
1888
  if (pAction->actionType == TRANS_ACTION_RAW) {
302,381,984✔
1889
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
191,299,046✔
1890
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
111,082,938✔
1891
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
111,082,938✔
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) {
93,351,777✔
1898
  int32_t numOfActions = taosArrayGetSize(pArray);
93,351,777✔
1899
  int32_t code = 0;
93,351,777✔
1900

1901
  for (int32_t action = 0; action < numOfActions; ++action) {
352,986,770✔
1902
    STransAction *pAction = taosArrayGet(pArray, action);
272,513,535✔
1903
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
272,513,535✔
1904
    if (code != 0) {
272,513,535✔
1905
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
12,878,542✔
1906
            numOfActions);
1907
      break;
12,878,542✔
1908
    }
1909
  }
1910

1911
  return code;
93,351,777✔
1912
}
1913

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

1919
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
93,351,777✔
1920
    return code;
12,878,542✔
1921
  }
1922

1923
  int32_t       numOfExecuted = 0;
80,473,235✔
1924
  int32_t       errCode = 0;
80,473,235✔
1925
  STransAction *pErrAction = NULL;
80,473,235✔
1926
  for (int32_t action = 0; action < numOfActions; ++action) {
340,108,228✔
1927
    STransAction *pAction = taosArrayGet(pArray, action);
259,634,993✔
1928
    if (pAction->msgReceived || pAction->rawWritten) {
259,634,993✔
1929
      numOfExecuted++;
217,066,203✔
1930
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
217,066,203✔
1931
        errCode = pAction->errCode;
3,916✔
1932
        pErrAction = pAction;
3,916✔
1933
      }
1934
    } else {
1935
      pErrAction = pAction;
42,568,790✔
1936
    }
1937
  }
1938

1939
  mndSetTransLastAction(pTrans, pErrAction);
80,473,235✔
1940

1941
  if (numOfExecuted == numOfActions) {
80,473,235✔
1942
    if (errCode == 0) {
58,725,630✔
1943
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
58,723,924✔
1944
      return 0;
58,723,924✔
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,747,605✔
1953

1954
    for (int32_t action = 0; action < numOfActions; ++action) {
84,142,588✔
1955
      STransAction *pAction = taosArrayGet(pArray, action);
62,394,983✔
1956
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
62,394,983✔
1957
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1958
             pAction->acceptableCode, pAction->retryCode);
1959
      if (pAction->msgSent) {
62,394,983✔
1960
        bool reset = false;
62,383,492✔
1961
        if (pAction->msgReceived) {
62,383,492✔
1962
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
19,826,193✔
1963
        } else {
1964
          int64_t timestamp = taosGetTimestampMs();
42,557,299✔
1965
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
42,557,299✔
1966
        }
1967
        if (reset) {
62,383,492✔
1968
          mndTransResetAction(pMnode, pTrans, pAction);
2,210✔
1969
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,210✔
1970
                pAction->id, pAction->errCode, pAction->startTime);
1971
        }
1972
      }
1973
    }
1974
    return TSDB_CODE_ACTION_IN_PROGRESS;
21,747,605✔
1975
  }
1976
}
1977

1978
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
68,189,038✔
1979
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
68,189,038✔
1980
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
68,189,038✔
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;
68,189,038✔
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) {
47,397,756✔
1997
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
47,397,756✔
1998
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
47,397,756✔
1999
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
2000
           mndStrExecutionContext(topHalf));
2001
  }
2002
  return code;
47,397,756✔
2003
}
2004

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

2010
  if (pTrans->actionPos >= numOfActions) {
8,583,396✔
2011
    return code;
391,674✔
2012
  }
2013

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

2017
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
12,984,678✔
2018
    STransAction *pAction = taosArrayGet(pActions, action);
12,641,591✔
2019

2020
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
12,641,591✔
2021
      code = TSDB_CODE_ACTION_IN_PROGRESS;
8,644✔
2022
      break;
8,803✔
2023
    }
2024

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

2028
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
12,632,947✔
2029
    if (code == 0) {
12,632,947✔
2030
      if (pAction->msgSent) {
10,750,981✔
2031
        bool reset = false;
9,383,911✔
2032
        if (pAction->msgReceived) {
9,383,911✔
2033
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
4,135,463✔
2034
            code = pAction->errCode;
2,379,569✔
2035
            reset = true;
2,379,569✔
2036
          } else {
2037
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,755,894✔
2038
          }
2039
        } else {
2040
          int64_t timestamp = taosGetTimestampMs();
5,248,448✔
2041
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
5,248,448✔
2042
          code = TSDB_CODE_ACTION_IN_PROGRESS;
5,248,448✔
2043
        }
2044
        if (reset) {
9,383,911✔
2045
          mndTransResetAction(pMnode, pTrans, pAction);
2,379,569✔
2046
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,379,569✔
2047
                pAction->id, pAction->errCode, pAction->startTime);
2048
        }
2049
      } else if (pAction->rawWritten) {
1,367,070✔
2050
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,367,070✔
2051
          code = pAction->errCode;
×
2052
        } else {
2053
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,367,070✔
2054
        }
2055
      } else {
2056
      }
2057
    }
2058

2059
    if (code == 0) {
12,632,947✔
2060
      pTrans->failedTimes = 0;
3,122,964✔
2061
    }
2062
    mndSetTransLastAction(pTrans, pAction);
12,632,947✔
2063
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
12,632,947✔
2064
      mInfo(
7,130,414✔
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,502,533✔
2070
      mError(
2,379,569✔
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,632,947✔
2078
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
12,632,947✔
2079
      pTrans->lastErrorNo = code;
2,591,543✔
2080
      pTrans->code = code;
2,591,543✔
2081
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
2,591,543✔
2082
            action, str);
2083
      break;
2,591,543✔
2084
    }
2085

2086
    if (code == 0) {
10,041,404✔
2087
      pTrans->code = 0;
2,413,387✔
2088
      pTrans->actionPos++;
2,413,387✔
2089
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
2,413,387✔
2090
            pAction->id);
2091
      (void)taosThreadMutexUnlock(&pTrans->mutex);
2,413,387✔
2092
      code = mndTransSync(pMnode, pTrans);
2,413,387✔
2093
      (void)taosThreadMutexLock(&pTrans->mutex);
2,413,387✔
2094
      if (code != 0) {
2,413,387✔
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,628,017✔
2102
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
5,248,448✔
2103
      break;
5,248,448✔
2104
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
2,379,569✔
2105
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
139✔
2106
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
2,379,569✔
2107
            code, tstrerror(code));
2108
      pTrans->lastErrorNo = code;
2,379,569✔
2109
      taosMsleep(300);
2,379,569✔
2110
      action--;
2,379,569✔
2111
      continue;
2,379,569✔
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,191,722✔
2123
}
2124

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

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

2149
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
4,633,273✔
2150
    STransAction **ppAction = taosArrayGet(pActions, action);
4,370,794✔
2151
    STransAction  *pAction = *ppAction;
4,370,794✔
2152

2153
    if (notSend && !pAction->msgSent) {
4,370,794✔
2154
      mInfo(
601,262✔
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;
601,262✔
2160
      break;
601,262✔
2161
    }
2162

2163
    mInfo(
3,769,532✔
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,769,532✔
2170
    if (code == 0) {
3,769,532✔
2171
      if (pAction->msgSent) {
2,253,000✔
2172
        if (pAction->msgReceived) {
2,011,833✔
2173
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
590,262✔
2174
            code = pAction->errCode;
70,470✔
2175
            mndTransResetAction(pMnode, pTrans, pAction);
70,470✔
2176
          } else {
2177
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
519,792✔
2178
                  pAction->id, action, numOfActions, groupId);
2179
          }
2180
        } else {
2181
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,421,571✔
2182
        }
2183
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
2,011,833✔
2184
        if (msgSent != NULL) {
2,011,833✔
2185
          *msgSent = pAction->msgSent;
2,011,833✔
2186
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
2,011,833✔
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) {
241,167✔
2191
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
241,167✔
2192
          code = pAction->errCode;
×
2193
        } else {
2194
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
241,167✔
2195
        }
2196
      } else {
2197
      }
2198
    }
2199

2200
    if (code == 0) {
3,769,532✔
2201
      pTrans->failedTimes = 0;
760,959✔
2202
    }
2203
    mndSetTransLastAction(pTrans, pAction);
3,769,532✔
2204

2205
    char str[200] = {0};
3,769,532✔
2206
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
3,769,532✔
2207
      pTrans->lastErrorNo = code;
1,687,416✔
2208
      pTrans->code = code;
1,687,416✔
2209
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
1,687,416✔
2210
        mInfo(
1,516,532✔
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) {
170,884✔
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,687,416✔
2223
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2224
      break;
1,687,416✔
2225
    }
2226

2227
    if (code == 0) {
2,082,116✔
2228
      pTrans->code = 0;
590,075✔
2229
      pTrans->actionPos++;
590,075✔
2230
      (*actionPos)++;
590,075✔
2231
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
590,075✔
2232
            pAction->id);
2233
      (void)taosThreadMutexUnlock(&pTrans->mutex);
590,075✔
2234
      code = mndTransSync(pMnode, pTrans);
590,075✔
2235
      (void)taosThreadMutexLock(&pTrans->mutex);
590,075✔
2236
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
590,075✔
2237
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
14,068,273✔
2238
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
13,478,198✔
2239
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
13,478,198✔
2240
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
13,478,198✔
2241
          pTmpAction->msgSent = *msgSent;
677,897✔
2242
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
677,897✔
2243
        }
2244
      }
2245
      if (code != 0) {
590,075✔
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,492,041✔
2254
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,421,571✔
2255
            mndTransStr(pAction->stage), pAction->id);
2256
      break;
1,421,571✔
2257
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
70,470✔
2258
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
242✔
2259
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
70,470✔
2260
            code, tstrerror(code));
2261
      pTrans->lastErrorNo = code;
70,470✔
2262
      taosMsleep(300);
70,470✔
2263
      action--;
70,470✔
2264
      continue;
70,470✔
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,972,728✔
2276
}
2277

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

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

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

2306
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,552,209✔
2307
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,552,209✔
2308
  int32_t currentGroup = 1;
1,552,209✔
2309
  while (pIter) {
6,318,299✔
2310
    SArray **redoActions = pIter;
4,766,090✔
2311
    size_t   keyLen = 0;
4,766,090✔
2312
    int32_t *key = taosHashGetKey(pIter, &keyLen);
4,766,090✔
2313
    int32_t  actionCount = taosArrayGetSize(*redoActions);
4,766,090✔
2314
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
4,766,090✔
2315
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2316
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
4,766,090✔
2317
                                             notSend, pHash);
2318
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
4,766,090✔
2319
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,516,532✔
2320
            tstrerror(code));
2321
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
3,249,558✔
2322
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
2,022,833✔
2323
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
1,226,725✔
2324
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
793,362✔
2325
    } else if (code != 0) {
433,363✔
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++;
433,363✔
2330
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
433,363✔
2331
    }
2332
    currentGroup++;
4,766,090✔
2333
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,766,090✔
2334
  }
2335

2336
  taosHashCleanup(pHash);
1,552,209✔
2337

2338
  if (successCount == groupCount) {
1,552,209✔
2339
    total_code = 0;
30,772✔
2340
  } else {
2341
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,521,437✔
2342
  }
2343

2344
  return total_code;
1,552,209✔
2345
}
2346

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

2351
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,801,691✔
2352
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,801,691✔
2353
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,801,691✔
2354
      code = 0;
215,261✔
2355
    } else {
2356
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,586,430✔
2357
      if (pAction != NULL && pAction->groupId == -1) {
1,586,430✔
2358
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
34,221✔
2359
      } else {
2360
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,552,209✔
2361
        if (code == 0) {
1,552,209✔
2362
          if (pTrans->actionPos < numOfActions) {
30,772✔
2363
            code = TSDB_CODE_ACTION_IN_PROGRESS;
21,366✔
2364
          }
2365
        }
2366
      }
2367
    }
2368
  }
2369

2370
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,801,691✔
2371

2372
  return code;
1,801,691✔
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,877,343✔
2386
  bool    continueExec = true;
24,877,343✔
2387
  int32_t code = 0;
24,877,343✔
2388
  terrno = 0;
24,877,343✔
2389

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

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

2395
  for (int32_t action = 0; action < numOfActions; ++action) {
23,440,788✔
2396
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
13,465,970✔
2397
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
13,465,970✔
2398
    if (code != 0) {
13,465,970✔
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,974,818✔
2407
  pTrans->stage = TRN_STAGE_REDO_ACTION;
24,877,343✔
2408
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
24,877,343✔
2409
  return continueExec;
24,877,343✔
2410
}
2411

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

2417
  if (pTrans->exec == TRN_EXEC_SERIAL) {
78,544,448✔
2418
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
8,553,719✔
2419
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
69,990,729✔
2420
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
68,189,038✔
2421
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,801,691✔
2422
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,801,691✔
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 &&
78,546,154✔
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)) {
78,544,448✔
2439
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
28,101,961✔
2440
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2441
      return false;
28,101,961✔
2442
    }
2443
  }
2444
  terrno = code;
50,442,487✔
2445

2446
  if (code == 0) {
50,442,487✔
2447
    pTrans->code = 0;
22,528,841✔
2448
    pTrans->stage = TRN_STAGE_COMMIT;
22,528,841✔
2449
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
22,528,841✔
2450
    continueExec = true;
22,528,841✔
2451
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
27,913,646✔
2452
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
27,911,940✔
2453
    continueExec = false;
27,911,940✔
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,442,487✔
2483
}
2484

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

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

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

2504
  return continueExec;
22,528,841✔
2505
}
2506

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

2511
  if (code == 0) {
47,397,756✔
2512
    pTrans->code = 0;
47,397,756✔
2513
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
47,397,756✔
2514
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
47,397,756✔
2515
    continueExec = true;
47,397,756✔
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;
47,397,756✔
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,848✔
2581
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
1,848✔
2582

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

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

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

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

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

2620
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
103,427,521✔
2621
  bool continueExec = true;
103,427,521✔
2622

2623
  while (continueExec) {
299,315,514✔
2624
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
195,887,993✔
2625
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2626
    pTrans->lastExecTime = taosGetTimestampMs();
195,887,993✔
2627
    switch (pTrans->stage) {
195,887,993✔
2628
      case TRN_STAGE_PREPARE:
×
2629
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2630
        break;
×
2631
      case TRN_STAGE_REDO_ACTION:
78,544,448✔
2632
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
78,544,448✔
2633
        break;
78,544,448✔
2634
      case TRN_STAGE_COMMIT:
22,528,841✔
2635
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
22,528,841✔
2636
        break;
22,528,841✔
2637
      case TRN_STAGE_COMMIT_ACTION:
47,397,756✔
2638
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
47,397,756✔
2639
        break;
47,397,756✔
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,848✔
2647
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,848✔
2648
        break;
1,848✔
2649
      case TRN_STAGE_FINISH:
47,401,452✔
2650
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
47,401,452✔
2651
        break;
47,401,452✔
2652
      default:
×
2653
        continueExec = false;
×
2654
        break;
×
2655
    }
2656
  }
2657

2658
  mndTransSendRpcRsp(pMnode, pTrans);
103,427,521✔
2659
}
103,427,521✔
2660

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

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

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

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

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

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

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

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

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

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

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

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

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

2758
  void *pIter = NULL;
18,300,344✔
2759
  while (1) {
2,200,040✔
2760
    STrans *pTrans = NULL;
20,500,384✔
2761
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
20,500,384✔
2762
    if (pIter == NULL) break;
20,500,384✔
2763
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
4,400,080✔
2764
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2765
    }
2766
    sdbRelease(pSdb, pTrans);
2,200,040✔
2767
  }
2768

2769
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
18,300,344✔
2770

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

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

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

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

2809
  return buf;
23,025,042✔
2810
}
2811

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

2817
  if (pTrans->stage == TRN_STAGE_PREPARE) {
517,077✔
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) {
517,077✔
2829
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
15,467,707✔
2830
      len = 0;
14,951,409✔
2831
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
14,951,409✔
2832
      if (pAction->actionType == TRANS_ACTION_MSG) {
14,951,409✔
2833
        char bufStart[40] = {0};
11,493,918✔
2834
        (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
11,493,918✔
2835

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

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

2853
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
11,493,918✔
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,457,491✔
2857
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
3,457,491✔
2858
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
3,457,491✔
2859
      }
2860
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
14,951,409✔
2861
    }
2862
  }
2863

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

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

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

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

2904
    cols = 0;
517,077✔
2905

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

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

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

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

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

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

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

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

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

2971
    mndTransLogAction(pTrans);
517,077✔
2972

2973
    numOfRows++;
517,077✔
2974
    sdbRelease(pSdb, pTrans);
517,077✔
2975
  }
2976

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

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

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

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

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

3016
  cols = 0;
80,762✔
3017

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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