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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

134502812.98 hits per line

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

83.63
/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; }
149,194,245✔
66

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

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

83
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
213,774,207✔
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) {
525,696✔
96
  SSdbTable table = {
525,696✔
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);
525,696✔
107
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
525,696✔
108

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

115
void mndCleanupTrans(SMnode *pMnode) {}
525,634✔
116

117
static int32_t mndTransGetActionsSize(SArray *pArray) {
279,002,060✔
118
  int32_t actionNum = taosArrayGetSize(pArray);
279,002,060✔
119
  int32_t rawDataLen = 0;
279,002,060✔
120

121
  for (int32_t i = 0; i < actionNum; ++i) {
750,126,350✔
122
    STransAction *pAction = taosArrayGet(pArray, i);
471,124,290✔
123
    if (pAction->actionType == TRANS_ACTION_RAW) {
471,124,290✔
124
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
323,936,646✔
125
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
147,187,644✔
126
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
147,187,644✔
127
    } else {
128
      // empty
129
    }
130
    rawDataLen += sizeof(int8_t);
471,124,290✔
131
  }
132

133
  return rawDataLen;
279,002,060✔
134
}
135

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

144
  for (int32_t i = 0; i < actionsNum; ++i) {
750,126,350✔
145
    STransAction *pAction = taosArrayGet(pActions, i);
471,124,290✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
471,124,290✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
471,124,290✔
148
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
471,124,290✔
149
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
471,124,290✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
471,124,290✔
151
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
471,124,290✔
152
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
471,124,290✔
153
    if (sver > TRANS_VER2_NUMBER) {
471,124,290✔
154
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
471,124,290✔
155
    }
156
    if (pAction->actionType == TRANS_ACTION_RAW) {
471,124,290✔
157
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
323,936,646✔
158
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
323,936,646✔
159
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
323,936,646✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
323,936,646✔
161
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
147,187,644✔
162
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
147,187,644✔
163
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
147,187,644✔
164
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
147,187,644✔
165
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
147,187,644✔
166
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
147,187,644✔
167
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
147,187,644✔
168
    } else {
169
      // nothing
170
    }
171
  }
172
  ret = 0;
279,002,060✔
173

174
_OVER:
279,002,060✔
175
  *offset = dataPos;
279,002,060✔
176
  return ret;
279,002,060✔
177
}
178

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

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

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

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

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

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

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

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

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

237
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
69,750,515✔
238
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
69,750,515✔
239
  void *pIter = NULL;
69,750,515✔
240
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
69,750,515✔
241
  while (pIter) {
69,782,411✔
242
    int32_t arbGroupId = *(int32_t *)pIter;
31,896✔
243
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
31,896✔
244
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
31,896✔
245
  }
246

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

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

267
  if (sver >= TRANS_VER_USER_DATA) {
69,750,515✔
268
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
69,750,515✔
269
    if (pTrans->userDataLen > 0) {
69,750,515✔
270
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
127,460✔
271
    }
272
  }
273

274
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
69,750,515✔
275
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
69,750,515✔
276

277
  terrno = 0;
69,750,515✔
278

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

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

291
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
32,204,192✔
292
  if (pAction->groupId < 0) return 0;
32,204,192✔
293
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
31,557,115✔
294
  if (redoAction == NULL) {
31,557,115✔
295
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
5,519,040✔
296
    if (array != NULL) {
5,519,040✔
297
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
5,519,040✔
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,519,040✔
303
  }
304
  if (redoAction != NULL) {
31,557,115✔
305
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
63,114,230✔
306
  }
307
  return 0;
31,557,115✔
308
}
309

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

533
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
123,746,799✔
534
  if (arbgroupIdNum > 0) {
123,746,799✔
535
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
45,050✔
536
    if (pTrans->arbGroupIds == NULL) goto _OVER;
45,050✔
537
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
98,210✔
538
      int32_t arbGroupId = 0;
53,160✔
539
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
53,160✔
540
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
53,160✔
541
    }
542
  }
543

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

553
  if (sver > TRANS_VER2_NUMBER) {
123,746,799✔
554
    int32_t groupNum = -1;
123,746,799✔
555
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
123,746,799✔
556
    for (int32_t i = 0; i < groupNum; ++i) {
129,265,839✔
557
      int32_t groupId = -1;
5,519,040✔
558
      int32_t groupPos = -1;
5,519,040✔
559
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
5,519,040✔
560
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
5,519,040✔
561
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
5,519,040✔
562
        goto _OVER;
×
563
    }
564
  }
565

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

575
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
123,746,799✔
576

577
  terrno = 0;
123,746,799✔
578

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

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

594
static const char *mndTransStr(ETrnStage stage) {
872,062,178✔
595
  switch (stage) {
872,062,178✔
596
    case TRN_STAGE_PREPARE:
69,274,963✔
597
      return "prepare";
69,274,963✔
598
    case TRN_STAGE_REDO_ACTION:
296,921,493✔
599
      return "redoAction";
296,921,493✔
600
    case TRN_STAGE_ROLLBACK:
8,300✔
601
      return "rollback";
8,300✔
602
    case TRN_STAGE_UNDO_ACTION:
13,484,087✔
603
      return "undoAction";
13,484,087✔
604
    case TRN_STAGE_COMMIT:
111,957,716✔
605
      return "commit";
111,957,716✔
606
    case TRN_STAGE_COMMIT_ACTION:
215,138,525✔
607
      return "commitAction";
215,138,525✔
608
    case TRN_STAGE_FINISH:
165,268,134✔
609
      return "finished";
165,268,134✔
610
    case TRN_STAGE_PRE_FINISH:
8,960✔
611
      return "pre-finish";
8,960✔
612
    default:
×
613
      return "invalid";
×
614
  }
615
}
616

617
static const char *mndTransTypeStr(ETrnAct actionType) {
77,836✔
618
  switch (actionType) {
77,836✔
619
    case TRANS_ACTION_MSG:
63,948✔
620
      return "msg";
63,948✔
621
    case TRANS_ACTION_RAW:
13,888✔
622
      return "sdb";
13,888✔
623
    default:
×
624
      return "invalid";
×
625
  }
626
}
627

628
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
243,239,248✔
629
  if (pAction != NULL) {
243,239,248✔
630
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
187,252,835✔
631
      pTrans->lastAction = pAction->id;
134,306,567✔
632
      pTrans->lastMsgType = pAction->msgType;
134,306,567✔
633
      pTrans->lastEpset = pAction->epSet;
134,306,567✔
634
      pTrans->lastErrorNo = pAction->errCode;
134,306,567✔
635
    }
636
  } else {
637
    pTrans->lastAction = 0;
55,986,413✔
638
    pTrans->lastMsgType = 0;
55,986,413✔
639
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
55,986,413✔
640
    pTrans->lastErrorNo = 0;
55,986,413✔
641
  }
642
}
243,239,248✔
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) {
674,809✔
653
  switch (ftype) {
674,809✔
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:
337,309✔
659
      return mndRebCntInc;
337,309✔
660
    case TRANS_STOP_FUNC_MQ_REB:
337,309✔
661
      return mndRebCntDec;
337,309✔
662
    case TRANS_STOP_FUNC_SOD:
191✔
663
      return mndSodTransStop;
191✔
664
    case TRANS_STOP_FUNC_SOD_ROLE_CHECK:
×
665
      return mndSodGrantRoleStop;
×
666
    default:
×
667
      return NULL;
×
668
  }
669
}
670

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

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

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

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

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

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

700
  return 0;
23,548,821✔
701
}
702

703
void mndTransDropData(STrans *pTrans) {
145,744,962✔
704
  if (pTrans->prepareActions != NULL) {
145,744,962✔
705
    mndTransDropActions(pTrans->prepareActions);
145,744,962✔
706
    pTrans->prepareActions = NULL;
145,744,962✔
707
  }
708
  if (pTrans->redoActions != NULL) {
145,744,962✔
709
    mndTransDropActions(pTrans->redoActions);
145,744,962✔
710
    pTrans->redoActions = NULL;
145,744,962✔
711
  }
712
  if (pTrans->undoActions != NULL) {
145,744,962✔
713
    mndTransDropActions(pTrans->undoActions);
145,744,962✔
714
    pTrans->undoActions = NULL;
145,744,962✔
715
  }
716
  if (pTrans->commitActions != NULL) {
145,744,962✔
717
    mndTransDropActions(pTrans->commitActions);
145,744,962✔
718
    pTrans->commitActions = NULL;
145,744,962✔
719
  }
720
  if (pTrans->redoGroupActions != NULL) {
145,744,962✔
721
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
23,835,581✔
722
    while (pIter) {
29,621,256✔
723
      SArray **redoActions = pIter;
5,785,675✔
724
      taosArrayDestroy(*redoActions);
5,785,675✔
725
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
5,785,675✔
726
    }
727

728
    taosHashCleanup(pTrans->redoGroupActions);
23,835,581✔
729
    pTrans->redoGroupActions = NULL;
23,835,581✔
730
  }
731
  if (pTrans->groupActionPos != NULL) {
145,744,962✔
732
    taosHashCleanup(pTrans->groupActionPos);
23,835,581✔
733
    pTrans->groupActionPos = NULL;
23,835,581✔
734
  }
735
  if (pTrans->arbGroupIds != NULL) {
145,744,962✔
736
    taosHashCleanup(pTrans->arbGroupIds);
22,043,213✔
737
  }
738
  if (pTrans->pRpcArray != NULL) {
145,744,962✔
739
    taosArrayDestroy(pTrans->pRpcArray);
21,998,163✔
740
    pTrans->pRpcArray = NULL;
21,998,163✔
741
  }
742
  if (pTrans->rpcRsp != NULL) {
145,744,962✔
743
    taosMemoryFree(pTrans->rpcRsp);
8,271,615✔
744
    pTrans->rpcRsp = NULL;
8,271,615✔
745
    pTrans->rpcRspLen = 0;
8,271,615✔
746
  }
747
  if (pTrans->param != NULL) {
145,744,962✔
748
    taosMemoryFree(pTrans->param);
×
749
    pTrans->param = NULL;
×
750
    pTrans->paramLen = 0;
×
751
  }
752
  if (pTrans->userData != NULL) {
145,744,962✔
753
    taosMemoryFree(pTrans->userData);
213,128✔
754
    pTrans->userData = NULL;
213,128✔
755
    pTrans->userDataLen = 0;
213,128✔
756
  }
757
  (void)taosThreadMutexDestroy(&pTrans->mutex);
145,744,962✔
758
}
145,744,962✔
759

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

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

772
  mndTransDropData(pTrans);
73,645,214✔
773
  return 0;
73,645,214✔
774
}
775

776
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
106,304,976✔
777
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
317,685,146✔
778
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
211,380,170✔
779
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
211,380,170✔
780
    pOldAction->rawWritten = pNewAction->rawWritten;
211,380,170✔
781
    pOldAction->msgSent = pNewAction->msgSent;
211,380,170✔
782
    pOldAction->msgReceived = pNewAction->msgReceived;
211,380,170✔
783
    pOldAction->errCode = pNewAction->errCode;
211,380,170✔
784
  }
785
}
106,304,976✔
786

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

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

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

809
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
26,576,244✔
810
  while (pIter != NULL) {
28,922,232✔
811
    int32_t newActionPos = *(int32_t *)pIter;
2,345,988✔
812

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

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

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

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

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

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

840
  return 0;
26,576,244✔
841
}
842

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

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

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

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

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

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

900
  if (pReq != NULL) {
21,998,163✔
901
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
29,773,430✔
902
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
903
      return NULL;
×
904
    }
905
    pTrans->originRpcType = pReq->msgType;
14,886,715✔
906
  }
907

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

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

914
static void mndTransDropActions(SArray *pArray) {
582,979,848✔
915
  int32_t size = taosArrayGetSize(pArray);
582,979,848✔
916
  for (int32_t i = 0; i < size; ++i) {
1,558,908,497✔
917
    STransAction *pAction = taosArrayGet(pArray, i);
975,928,649✔
918
    if (pAction->actionType == TRANS_ACTION_RAW) {
975,928,649✔
919
      taosMemoryFreeClear(pAction->pRaw);
667,972,815✔
920
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
307,955,834✔
921
      taosMemoryFreeClear(pAction->pCont);
307,955,834✔
922
    } else {
923
      // nothing
924
    }
925
  }
926

927
  taosArrayDestroy(pArray);
582,979,848✔
928
}
582,979,848✔
929

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

938
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
130,594,136✔
939
  pAction->id = taosArrayGetSize(pArray);
130,594,136✔
940

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

946
  return 0;
130,594,136✔
947
}
948

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

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

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

981
  return 0;
611,931✔
982
}
983

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1182
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
6,607,078✔
1183
  if (conflict[0] == 0) return false;
6,607,078✔
1184
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
6,447,606✔
1185
  return false;
6,267,283✔
1186
}
1187

1188
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
6,853,951✔
1189
  if (conflict) {
6,853,951✔
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,139✔
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,139✔
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,588,812✔
1197
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1198
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1199
  }
1200
}
6,853,951✔
1201

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

1207
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
57,309,049✔
1208

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

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

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

1218
    if (pNew->conflict == TRN_CONFLICT_DB) {
6,710,624✔
1219
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
193,175✔
1220
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
193,175✔
1221
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
159,472✔
1222
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
159,472✔
1223
      }
1224
    }
1225

1226
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
6,710,624✔
1227
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
6,467,699✔
1228
      if (pTrans->conflict == TRN_CONFLICT_DB) {
6,467,699✔
1229
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
87,401✔
1230
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
87,401✔
1231
      }
1232
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
6,467,699✔
1233
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
6,360,205✔
1234
      }
1235
    }
1236

1237
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
6,710,624✔
1238
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
45,114✔
1239
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
45,114✔
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,710,624✔
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,710,624✔
1264
  }
1265

1266
  return conflict;
43,037,121✔
1267
}
1268

1269
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
57,309,049✔
1270
  int32_t code = 0;
57,309,049✔
1271
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
57,309,049✔
1272
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
36,092,298✔
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)) {
57,309,049✔
1280
    code = TSDB_CODE_MND_TRANS_CONFLICT;
325,676✔
1281
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
325,676✔
1282
    TAOS_RETURN(code);
325,676✔
1283
  }
1284

1285
  TAOS_RETURN(code);
56,983,373✔
1286
}
1287

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

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

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

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

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

1323
  TAOS_RETURN(code);
166,765✔
1324
}
1325

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

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

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

UNCOV
1343
    if (conflict) {
×
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 {
UNCOV
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
    }
UNCOV
1353
    sdbRelease(pSdb, pRetention);
×
1354
  }
1355

1356
  if (conflict) {
166,765✔
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);
166,765✔
1363
}
1364

1365
static bool mndTransActionsOfSameType(SArray *pActions) {
49,219,159✔
1366
  int32_t size = taosArrayGetSize(pActions);
49,219,159✔
1367
  ETrnAct lastActType = TRANS_ACTION_NULL;
49,219,159✔
1368
  bool    same = true;
49,219,159✔
1369
  for (int32_t i = 0; i < size; ++i) {
156,693,793✔
1370
    STransAction *pAction = taosArrayGet(pActions, i);
107,474,634✔
1371
    if (i > 0) {
107,474,634✔
1372
      if (lastActType != pAction->actionType) {
72,815,020✔
1373
        same = false;
×
1374
        break;
×
1375
      }
1376
    }
1377
    lastActType = pAction->actionType;
107,474,634✔
1378
  }
1379
  return same;
49,219,159✔
1380
}
1381

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

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

1400
  TAOS_RETURN(code);
21,643,241✔
1401
}
1402

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

1416
  TAOS_RETURN(code);
21,643,241✔
1417
}
1418

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

1425
  mInfo("trans:%d, action list:", pTrans->id);
21,644,596✔
1426
  int32_t index = 0;
21,644,596✔
1427
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
33,139,146✔
1428
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
11,494,550✔
1429
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
11,494,550✔
1430
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1431
  }
1432

1433
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
44,691,300✔
1434
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
23,046,704✔
1435
    if (pAction->actionType == TRANS_ACTION_MSG) {
23,046,704✔
1436
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
22,329,430✔
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),
717,274✔
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) {
104,147,542✔
1446
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
82,502,946✔
1447
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
82,502,946✔
1448
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1449
  }
1450

1451
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
35,057,303✔
1452
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
13,412,707✔
1453
    if (pAction->actionType == TRANS_ACTION_MSG) {
13,412,707✔
1454
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
8,614,731✔
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,797,976✔
1458
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1459
    }
1460
  }
1461

1462
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
21,644,596✔
1463

1464
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
21,643,241✔
1465

1466
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
21,643,241✔
1467

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

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

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

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

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

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

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

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

1538
  if (pTrans->stage == TRN_STAGE_FINISH) {
98,729,440✔
1539
    sendRsp = true;
45,159,192✔
1540
  }
1541

1542
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
98,729,440✔
1543
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
26,077,154✔
1544
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
9,960✔
1545
      sendRsp = true;
9,960✔
1546
    }
1547
  } else {
1548
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
72,652,286✔
1549
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
41,038,813✔
1550
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1551
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1552
      } else {
1553
        if (pTrans->failedTimes > 6) sendRsp = true;
41,038,813✔
1554
      }
1555
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
41,038,813✔
1556
    }
1557
  }
1558

1559
  if (!sendRsp) {
98,729,440✔
1560
    return;
53,560,288✔
1561
  } else {
1562
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
45,169,152✔
1563
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1564
  }
1565

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

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

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

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

1592
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
13,956,567✔
1593
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,496,609✔
1594
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,496,609✔
1595
        if (pDb != NULL) {
1,496,609✔
1596
          for (int32_t j = 0; j < 12; j++) {
1,888,902✔
1597
            bool ready = mndIsDbReady(pMnode, pDb);
1,888,090✔
1598
            if (!ready) {
1,888,090✔
1599
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
392,293✔
1600
              taosMsleep(1000);
392,293✔
1601
            } else {
1602
              break;
1,495,797✔
1603
            }
1604
          }
1605
        }
1606
        mndReleaseDb(pMnode, pDb);
1,496,609✔
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
12,459,958✔
1608
        void   *pCont = NULL;
2,213,086✔
1609
        int32_t contLen = 0;
2,213,086✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,213,086✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,211,426✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
10,246,872✔
1614
        void   *pCont = NULL;
10,501✔
1615
        int32_t contLen = 0;
10,501✔
1616
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
10,501✔
1617
          mndTransSetRpcRsp(pTrans, pCont, contLen);
10,501✔
1618
        }
1619
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
10,236,371✔
1620
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
10,475✔
1621
        if (code != 0) {
10,475✔
1622
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
10,225,896✔
1625
        void   *pCont = NULL;
21,215✔
1626
        int32_t contLen = 0;
21,215✔
1627
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
21,215✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
21,215✔
1629
        }
1630
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
10,204,681✔
1631
        void   *pCont = NULL;
21,069✔
1632
        int32_t contLen = 0;
21,069✔
1633
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
21,069✔
1634
          mndTransSetRpcRsp(pTrans, pCont, contLen);
21,069✔
1635
        }
1636
      }
1637

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

1647
      tmsgSendRsp(&rspMsg);
13,956,567✔
1648

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1766
// execute in sync context
1767
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
184,096,381✔
1768
  if (pAction->rawWritten) return 0;
184,096,381✔
1769
  if (topHalf) {
100,931,306✔
1770
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
560✔
1771
  }
1772

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

1796
    mndSetTransLastAction(pTrans, pAction);
100,930,746✔
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);
100,930,746✔
1805
}
1806

1807
// execute in trans context
1808
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
107,104,051✔
1809
                                     bool notSend) {
1810
  if (pAction->msgSent) return 0;
107,104,051✔
1811
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
40,025,798✔
1812
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
15,319,717✔
1813
  }
1814

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

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

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

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

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

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

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

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

1873
  TAOS_RETURN(code);
24,697,457✔
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,
291,200,432✔
1887
                                        bool notSend) {
1888
  if (pAction->actionType == TRANS_ACTION_RAW) {
291,200,432✔
1889
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
184,096,381✔
1890
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
107,104,051✔
1891
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
107,104,051✔
1892
  } else {
1893
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1894
  }
1895
}
1896

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

1901
  for (int32_t action = 0; action < numOfActions; ++action) {
339,450,170✔
1902
    STransAction *pAction = taosArrayGet(pArray, action);
262,561,208✔
1903
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
262,561,208✔
1904
    if (code != 0) {
262,561,208✔
1905
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
12,001,941✔
1906
            numOfActions);
1907
      break;
12,001,941✔
1908
    }
1909
  }
1910

1911
  return code;
88,890,903✔
1912
}
1913

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

1919
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
88,890,903✔
1920
    return code;
12,001,941✔
1921
  }
1922

1923
  int32_t       numOfExecuted = 0;
76,888,962✔
1924
  int32_t       errCode = 0;
76,888,962✔
1925
  STransAction *pErrAction = NULL;
76,888,962✔
1926
  for (int32_t action = 0; action < numOfActions; ++action) {
327,448,229✔
1927
    STransAction *pAction = taosArrayGet(pArray, action);
250,559,267✔
1928
    if (pAction->msgReceived || pAction->rawWritten) {
250,559,267✔
1929
      numOfExecuted++;
209,421,460✔
1930
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
209,421,460✔
1931
        errCode = pAction->errCode;
3,726✔
1932
        pErrAction = pAction;
3,726✔
1933
      }
1934
    } else {
1935
      pErrAction = pAction;
41,137,807✔
1936
    }
1937
  }
1938

1939
  mndSetTransLastAction(pTrans, pErrAction);
76,888,962✔
1940

1941
  if (numOfExecuted == numOfActions) {
76,888,962✔
1942
    if (errCode == 0) {
55,988,073✔
1943
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
55,986,413✔
1944
      return 0;
55,986,413✔
1945
    } else {
1946
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,660✔
1947
      mndTransResetActions(pMnode, pTrans, pArray);
1,660✔
1948
      terrno = errCode;
1,660✔
1949
      return errCode;
1,660✔
1950
    }
1951
  } else {
1952
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
20,900,889✔
1953

1954
    for (int32_t action = 0; action < numOfActions; ++action) {
81,399,778✔
1955
      STransAction *pAction = taosArrayGet(pArray, action);
60,498,889✔
1956
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
60,498,889✔
1957
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1958
             pAction->acceptableCode, pAction->retryCode);
1959
      if (pAction->msgSent) {
60,498,889✔
1960
        bool reset = false;
60,490,265✔
1961
        if (pAction->msgReceived) {
60,490,265✔
1962
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
19,361,082✔
1963
        } else {
1964
          int64_t timestamp = taosGetTimestampMs();
41,129,183✔
1965
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
41,129,183✔
1966
        }
1967
        if (reset) {
60,490,265✔
1968
          mndTransResetAction(pMnode, pTrans, pAction);
2,066✔
1969
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,066✔
1970
                pAction->id, pAction->errCode, pAction->startTime);
1971
        }
1972
      }
1973
    }
1974
    return TSDB_CODE_ACTION_IN_PROGRESS;
20,900,889✔
1975
  }
1976
}
1977

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

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

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

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

2010
  if (pTrans->actionPos >= numOfActions) {
8,341,907✔
2011
    return code;
369,796✔
2012
  }
2013

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

2017
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
12,623,275✔
2018
    STransAction *pAction = taosArrayGet(pActions, action);
12,298,458✔
2019

2020
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
12,298,458✔
2021
      code = TSDB_CODE_ACTION_IN_PROGRESS;
8,412✔
2022
      break;
8,559✔
2023
    }
2024

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

2028
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
12,290,046✔
2029
    if (code == 0) {
12,290,046✔
2030
      if (pAction->msgSent) {
10,498,267✔
2031
        bool reset = false;
9,210,127✔
2032
        if (pAction->msgReceived) {
9,210,127✔
2033
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
4,031,639✔
2034
            code = pAction->errCode;
2,351,696✔
2035
            reset = true;
2,351,696✔
2036
          } else {
2037
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,679,943✔
2038
          }
2039
        } else {
2040
          int64_t timestamp = taosGetTimestampMs();
5,178,488✔
2041
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
5,178,488✔
2042
          code = TSDB_CODE_ACTION_IN_PROGRESS;
5,178,488✔
2043
        }
2044
        if (reset) {
9,210,127✔
2045
          mndTransResetAction(pMnode, pTrans, pAction);
2,351,696✔
2046
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,351,696✔
2047
                pAction->id, pAction->errCode, pAction->startTime);
2048
        }
2049
      } else if (pAction->rawWritten) {
1,288,140✔
2050
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,288,140✔
2051
          code = pAction->errCode;
×
2052
        } else {
2053
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,288,140✔
2054
        }
2055
      } else {
2056
      }
2057
    }
2058

2059
    if (code == 0) {
12,290,046✔
2060
      pTrans->failedTimes = 0;
2,968,083✔
2061
    }
2062
    mndSetTransLastAction(pTrans, pAction);
12,290,046✔
2063
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
12,290,046✔
2064
      mInfo(
6,970,267✔
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,319,779✔
2070
      mError(
2,351,696✔
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,290,046✔
2078
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
12,290,046✔
2079
      pTrans->lastErrorNo = code;
2,460,394✔
2080
      pTrans->code = code;
2,460,394✔
2081
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
2,460,394✔
2082
            action, str);
2083
      break;
2,460,394✔
2084
    }
2085

2086
    if (code == 0) {
9,829,652✔
2087
      pTrans->code = 0;
2,299,468✔
2088
      pTrans->actionPos++;
2,299,468✔
2089
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
2,299,468✔
2090
            pAction->id);
2091
      (void)taosThreadMutexUnlock(&pTrans->mutex);
2,299,468✔
2092
      code = mndTransSync(pMnode, pTrans);
2,299,468✔
2093
      (void)taosThreadMutexLock(&pTrans->mutex);
2,299,468✔
2094
      if (code != 0) {
2,299,468✔
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,530,184✔
2102
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
5,178,488✔
2103
      break;
5,178,488✔
2104
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
2,351,696✔
2105
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
332✔
2106
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
2,351,696✔
2107
            code, tstrerror(code));
2108
      pTrans->lastErrorNo = code;
2,351,696✔
2109
      taosMsleep(300);
2,351,696✔
2110
      action--;
2,351,696✔
2111
      continue;
2,351,696✔
2112
    } else {
UNCOV
2113
      terrno = code;
×
UNCOV
2114
      pTrans->lastErrorNo = code;
×
UNCOV
2115
      pTrans->code = code;
×
UNCOV
2116
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2117
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
UNCOV
2118
      break;
×
2119
    }
2120
  }
2121

2122
  return code;
7,972,111✔
2123
}
2124

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

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

2149
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
4,570,410✔
2150
    STransAction **ppAction = taosArrayGet(pActions, action);
4,308,197✔
2151
    STransAction  *pAction = *ppAction;
4,308,197✔
2152

2153
    if (notSend && !pAction->msgSent) {
4,308,197✔
2154
      mInfo(
570,629✔
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;
570,629✔
2160
      break;
570,629✔
2161
    }
2162

2163
    mInfo(
3,737,568✔
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,737,568✔
2170
    if (code == 0) {
3,737,568✔
2171
      if (pAction->msgSent) {
2,211,011✔
2172
        if (pAction->msgReceived) {
1,986,309✔
2173
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
568,215✔
2174
            code = pAction->errCode;
54,770✔
2175
            mndTransResetAction(pMnode, pTrans, pAction);
54,770✔
2176
          } else {
2177
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
513,445✔
2178
                  pAction->id, action, numOfActions, groupId);
2179
          }
2180
        } else {
2181
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,418,094✔
2182
        }
2183
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,986,309✔
2184
        if (msgSent != NULL) {
1,986,309✔
2185
          *msgSent = pAction->msgSent;
1,986,309✔
2186
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,986,309✔
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) {
224,702✔
2191
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
224,702✔
2192
          code = pAction->errCode;
×
2193
        } else {
2194
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
224,702✔
2195
        }
2196
      } else {
2197
      }
2198
    }
2199

2200
    if (code == 0) {
3,737,568✔
2201
      pTrans->failedTimes = 0;
738,147✔
2202
    }
2203
    mndSetTransLastAction(pTrans, pAction);
3,737,568✔
2204

2205
    char str[200] = {0};
3,737,568✔
2206
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
3,737,568✔
2207
      pTrans->lastErrorNo = code;
1,683,211✔
2208
      pTrans->code = code;
1,683,211✔
2209
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
1,683,211✔
2210
        mInfo(
1,526,557✔
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) {
156,654✔
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,683,211✔
2223
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2224
      break;
1,683,211✔
2225
    }
2226

2227
    if (code == 0) {
2,054,357✔
2228
      pTrans->code = 0;
581,493✔
2229
      pTrans->actionPos++;
581,493✔
2230
      (*actionPos)++;
581,493✔
2231
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
581,493✔
2232
            pAction->id);
2233
      (void)taosThreadMutexUnlock(&pTrans->mutex);
581,493✔
2234
      code = mndTransSync(pMnode, pTrans);
581,493✔
2235
      (void)taosThreadMutexLock(&pTrans->mutex);
581,493✔
2236
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
581,493✔
2237
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
13,904,286✔
2238
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
13,322,793✔
2239
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
13,322,793✔
2240
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
13,322,793✔
2241
          pTmpAction->msgSent = *msgSent;
712,326✔
2242
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
712,326✔
2243
        }
2244
      }
2245
      if (code != 0) {
581,493✔
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,472,864✔
2254
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,418,094✔
2255
            mndTransStr(pAction->stage), pAction->id);
2256
      break;
1,418,094✔
2257
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
54,770✔
2258
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
226✔
2259
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
54,770✔
2260
            code, tstrerror(code));
2261
      pTrans->lastErrorNo = code;
54,770✔
2262
      taosMsleep(300);
54,770✔
2263
      action--;
54,770✔
2264
      continue;
54,770✔
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,934,147✔
2276
}
2277

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

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

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

2306
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,495,849✔
2307
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,495,849✔
2308
  int32_t currentGroup = 1;
1,495,849✔
2309
  while (pIter) {
6,220,641✔
2310
    SArray **redoActions = pIter;
4,724,792✔
2311
    size_t   keyLen = 0;
4,724,792✔
2312
    int32_t *key = taosHashGetKey(pIter, &keyLen);
4,724,792✔
2313
    int32_t  actionCount = taosArrayGetSize(*redoActions);
4,724,792✔
2314
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
4,724,792✔
2315
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2316
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
4,724,792✔
2317
                                             notSend, pHash);
2318
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
4,724,792✔
2319
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,526,557✔
2320
            tstrerror(code));
2321
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
3,198,235✔
2322
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,988,723✔
2323
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
1,209,512✔
2324
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
790,645✔
2325
    } else if (code != 0) {
418,867✔
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++;
418,867✔
2330
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
418,867✔
2331
    }
2332
    currentGroup++;
4,724,792✔
2333
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,724,792✔
2334
  }
2335

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

2338
  if (successCount == groupCount) {
1,495,849✔
2339
    total_code = 0;
26,931✔
2340
  } else {
2341
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,468,918✔
2342
  }
2343

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

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

2351
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,740,469✔
2352
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,740,469✔
2353
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,740,469✔
2354
      code = 0;
210,191✔
2355
    } else {
2356
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,530,278✔
2357
      if (pAction != NULL && pAction->groupId == -1) {
1,530,278✔
2358
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
34,429✔
2359
      } else {
2360
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,495,849✔
2361
        if (code == 0) {
1,495,849✔
2362
          if (pTrans->actionPos < numOfActions) {
26,931✔
2363
            code = TSDB_CODE_ACTION_IN_PROGRESS;
19,294✔
2364
          }
2365
        }
2366
      }
2367
    }
2368
  }
2369

2370
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,740,469✔
2371

2372
  return code;
1,740,469✔
2373
}
2374

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

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

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

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

2395
  for (int32_t action = 0; action < numOfActions; ++action) {
21,850,015✔
2396
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
12,611,610✔
2397
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
12,611,610✔
2398
    if (code != 0) {
12,611,610✔
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,238,405✔
2407
  pTrans->stage = TRN_STAGE_REDO_ACTION;
23,524,996✔
2408
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
23,524,996✔
2409
  return continueExec;
23,524,996✔
2410
}
2411

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

2417
  if (pTrans->exec == TRN_EXEC_SERIAL) {
75,199,084✔
2418
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
8,310,554✔
2419
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
66,888,530✔
2420
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
65,148,061✔
2421
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,740,469✔
2422
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,740,469✔
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 &&
75,200,744✔
2428
      mndTransIsInSyncContext(topHalf)) {
1,660✔
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)) {
75,199,084✔
2439
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
26,603,751✔
2440
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2441
      return false;
26,603,751✔
2442
    }
2443
  }
2444
  terrno = code;
48,595,333✔
2445

2446
  if (code == 0) {
48,595,333✔
2447
    pTrans->code = 0;
21,640,334✔
2448
    pTrans->stage = TRN_STAGE_COMMIT;
21,640,334✔
2449
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
21,640,334✔
2450
    continueExec = true;
21,640,334✔
2451
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
26,954,999✔
2452
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
26,953,339✔
2453
    continueExec = false;
26,953,339✔
2454
  } else {
2455
    pTrans->failedTimes++;
1,660✔
2456
    pTrans->code = terrno;
1,660✔
2457
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
1,660✔
2458
      if (pTrans->lastAction != 0) {
1,660✔
2459
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
1,660✔
2460
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
1,660✔
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,660✔
2472
      pTrans->actionPos = 0;
1,660✔
2473
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
1,660✔
2474
             pTrans->actionPos);
2475
      continueExec = true;
1,660✔
2476
    } else {
UNCOV
2477
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
2478
      continueExec = false;
×
2479
    }
2480
  }
2481

2482
  return continueExec;
48,595,333✔
2483
}
2484

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

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

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

2504
  return continueExec;
21,640,334✔
2505
}
2506

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

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

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

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

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

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

2557
  return continueExec;
9,960✔
2558
}
2559

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

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

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

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

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

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

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

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

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

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

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

2623
  while (continueExec) {
285,898,236✔
2624
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
187,168,796✔
2625
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2626
    pTrans->lastExecTime = taosGetTimestampMs();
187,168,796✔
2627
    switch (pTrans->stage) {
187,168,796✔
2628
      case TRN_STAGE_PREPARE:
×
2629
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2630
        break;
×
2631
      case TRN_STAGE_REDO_ACTION:
75,199,084✔
2632
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
75,199,084✔
2633
        break;
75,199,084✔
2634
      case TRN_STAGE_COMMIT:
21,640,449✔
2635
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
21,640,449✔
2636
        break;
21,640,449✔
2637
      case TRN_STAGE_COMMIT_ACTION:
45,155,361✔
2638
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
45,155,361✔
2639
        break;
45,155,361✔
2640
      case TRN_STAGE_ROLLBACK:
1,660✔
2641
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,660✔
2642
        break;
1,660✔
2643
      case TRN_STAGE_UNDO_ACTION:
11,620✔
2644
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
11,620✔
2645
        break;
11,620✔
2646
      case TRN_STAGE_PRE_FINISH:
1,792✔
2647
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,792✔
2648
        break;
1,792✔
2649
      case TRN_STAGE_FINISH:
45,158,830✔
2650
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
45,158,830✔
2651
        break;
45,158,830✔
2652
      default:
×
2653
        continueExec = false;
×
2654
        break;
×
2655
    }
2656
  }
2657

2658
  mndTransSendRpcRsp(pMnode, pTrans);
98,729,440✔
2659
}
98,729,440✔
2660

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

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

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

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

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

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

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

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

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

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

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

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

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

2758
  void *pIter = NULL;
18,105,595✔
2759
  while (1) {
2,290,526✔
2760
    STrans *pTrans = NULL;
20,396,121✔
2761
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
20,396,121✔
2762
    if (pIter == NULL) break;
20,396,121✔
2763
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
4,581,052✔
2764
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2765
    }
2766
    sdbRelease(pSdb, pTrans);
2,290,526✔
2767
  }
2768

2769
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
18,105,595✔
2770

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

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

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

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

2809
  return buf;
21,750,038✔
2810
}
2811

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

2817
  if (pTrans->stage == TRN_STAGE_PREPARE) {
497,388✔
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) {
497,388✔
2829
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
14,752,574✔
2830
      len = 0;
14,255,186✔
2831
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
14,255,186✔
2832
      if (pAction->actionType == TRANS_ACTION_MSG) {
14,255,186✔
2833
        char bufStart[40] = {0};
10,854,820✔
2834
        (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
10,854,820✔
2835

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

2844
        SEpSet epset = pAction->epSet;
10,854,820✔
2845
        if (epset.numOfEps > 0) {
10,854,820✔
2846
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
10,854,820✔
2847
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
24,375,220✔
2848
            len +=
13,520,400✔
2849
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
13,520,400✔
2850
          }
2851
        }
2852

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

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

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

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

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

2904
    cols = 0;
497,388✔
2905

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

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

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

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

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

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

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

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

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

2971
    mndTransLogAction(pTrans);
497,388✔
2972

2973
    numOfRows++;
497,388✔
2974
    sdbRelease(pSdb, pTrans);
497,388✔
2975
  }
2976

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

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

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

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

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

3016
  cols = 0;
77,836✔
3017

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

3020
  if (pAction->actionType == TRANS_ACTION_MSG) {
77,836✔
3021
    int32_t len = 0;
63,948✔
3022

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

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

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

3054
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
63,948✔
3055
    len = 0;
63,948✔
3056
    char bufStart[40] = {0};
63,948✔
3057
    if (pAction->startTime > 0)
63,948✔
3058
      (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
22,152✔
3059
    char bufEnd[40] = {0};
63,948✔
3060
    if (pAction->endTime > 0)
63,948✔
3061
      (void)formatTimestamp(bufEnd, sizeof(bufEnd), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
18,246✔
3062

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

3069
  } else {
3070
    int32_t len = 0;
13,888✔
3071

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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