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

taosdata / TDengine / #5010

29 Mar 2026 04:32AM UTC coverage: 72.292% (+0.03%) from 72.26%
#5010

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253774 of 351039 relevant lines covered (72.29%)

133420324.04 hits per line

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

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

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

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

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

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

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

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

63
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
129,160,394✔
64

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

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

81
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
183,030,394✔
82

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

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

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

104
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
454,611✔
105
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
454,611✔
106

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

113
void mndCleanupTrans(SMnode *pMnode) {}
454,549✔
114

115
static int32_t mndTransGetActionsSize(SArray *pArray) {
239,120,448✔
116
  int32_t actionNum = taosArrayGetSize(pArray);
239,120,448✔
117
  int32_t rawDataLen = 0;
239,120,448✔
118

119
  for (int32_t i = 0; i < actionNum; ++i) {
639,290,263✔
120
    STransAction *pAction = taosArrayGet(pArray, i);
400,169,815✔
121
    if (pAction->actionType == TRANS_ACTION_RAW) {
400,169,815✔
122
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
270,723,382✔
123
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
129,446,433✔
124
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
129,446,433✔
125
    } else {
126
      // empty
127
    }
128
    rawDataLen += sizeof(int8_t);
400,169,815✔
129
  }
130

131
  return rawDataLen;
239,120,448✔
132
}
133

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

142
  for (int32_t i = 0; i < actionsNum; ++i) {
639,290,263✔
143
    STransAction *pAction = taosArrayGet(pActions, i);
400,169,815✔
144
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
400,169,815✔
145
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
400,169,815✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
400,169,815✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
400,169,815✔
148
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
400,169,815✔
149
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
400,169,815✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
400,169,815✔
151
    if (sver > TRANS_VER2_NUMBER) {
400,169,815✔
152
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
400,169,815✔
153
    }
154
    if (pAction->actionType == TRANS_ACTION_RAW) {
400,169,815✔
155
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
270,723,382✔
156
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
270,723,382✔
157
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
270,723,382✔
158
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
270,723,382✔
159
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
129,446,433✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
129,446,433✔
161
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
129,446,433✔
162
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
129,446,433✔
163
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
129,446,433✔
164
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
129,446,433✔
165
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
129,446,433✔
166
    } else {
167
      // nothing
168
    }
169
  }
170
  ret = 0;
239,120,448✔
171

172
_OVER:
239,120,448✔
173
  *offset = dataPos;
239,120,448✔
174
  return ret;
239,120,448✔
175
}
176

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

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

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

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

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

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

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

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

233
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
59,780,112✔
234

235
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
59,780,112✔
236
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
59,780,112✔
237
  void *pIter = NULL;
59,780,112✔
238
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
59,780,112✔
239
  while (pIter) {
59,809,512✔
240
    int32_t arbGroupId = *(int32_t *)pIter;
29,400✔
241
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
29,400✔
242
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
29,400✔
243
  }
244

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

250
  if (sver > TRANS_VER2_NUMBER) {
59,780,112✔
251
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
59,780,112✔
252
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
59,780,112✔
253
    void *pIter = NULL;
59,780,112✔
254
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
59,780,112✔
255
    while (pIter) {
62,143,416✔
256
      size_t   keysize = 0;
2,363,304✔
257
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,363,304✔
258
      int32_t  groupPos = *(int32_t *)pIter;
2,363,304✔
259
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,363,304✔
260
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,363,304✔
261
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,363,304✔
262
    }
263
  }
264

265
  if (sver >= TRANS_VER_USER_DATA) {
59,780,112✔
266
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
59,780,112✔
267
    if (pTrans->userDataLen > 0) {
59,780,112✔
268
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
102,600✔
269
    }
270
  }
271

272
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
59,780,112✔
273
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
59,780,112✔
274

275
  terrno = 0;
59,780,112✔
276

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

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

289
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
27,663,114✔
290
  if (pAction->groupId < 0) return 0;
27,663,114✔
291
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
27,117,328✔
292
  if (redoAction == NULL) {
27,117,328✔
293
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
4,693,906✔
294
    if (array != NULL) {
4,693,906✔
295
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
4,693,906✔
296
        mInfo("failed put action into redo group actions");
×
297
        return TSDB_CODE_INTERNAL_ERROR;
×
298
      }
299
    }
300
    redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
4,693,906✔
301
  }
302
  if (redoAction != NULL) {
27,117,328✔
303
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
54,234,656✔
304
  }
305
  return 0;
27,117,328✔
306
}
307

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

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

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

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

385
  for (int32_t i = 0; i < actionNum; ++i) {
1,109,907,950✔
386
    memset(&action, 0, sizeof(action));
689,033,944✔
387
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
689,033,944✔
388
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
689,033,944✔
389
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
689,033,944✔
390
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
689,033,944✔
391
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
689,033,944✔
392
    action.actionType = actionType;
689,033,944✔
393
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
689,033,944✔
394
    action.stage = stage;
689,033,944✔
395
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
689,033,944✔
396
    if (sver > TRANS_VER2_NUMBER) {
689,033,944✔
397
      SDB_GET_INT32(pRaw, dataPos, &action.groupId, _OVER)
689,033,944✔
398
    }
399
    if (action.actionType == TRANS_ACTION_RAW) {
689,033,944✔
400
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
468,657,983✔
401
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
468,657,983✔
402
      action.pRaw = taosMemoryMalloc(dataLen);
468,657,983✔
403
      if (action.pRaw == NULL) goto _OVER;
468,657,983✔
404
      mTrace("raw:%p, is created", action.pRaw);
468,657,983✔
405
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
468,657,983✔
406
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
468,657,983✔
407
      action.pRaw = NULL;
468,657,983✔
408
    } else if (action.actionType == TRANS_ACTION_MSG) {
220,375,961✔
409
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
220,375,961✔
410
      tmsgUpdateDnodeEpSet(&action.epSet);
220,375,961✔
411
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
220,375,961✔
412
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
220,375,961✔
413
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
220,375,961✔
414
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
220,375,961✔
415
      action.pCont = taosMemoryMalloc(action.contLen);
220,375,961✔
416
      if (action.pCont == NULL) goto _OVER;
220,375,961✔
417
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
220,375,961✔
418
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
220,375,961✔
419
      action.pCont = NULL;
220,375,961✔
420
    } else {
421
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
422
    }
423
  }
424
  ret = 0;
420,874,006✔
425

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

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

449
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
105,618,092✔
450

451
  if (sver > TRANS_VER_CURRENT) {
105,618,092✔
452
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
453
    goto _OVER;
×
454
  }
455

456
  pRow = sdbAllocRow(sizeof(STrans));
105,618,092✔
457
  if (pRow == NULL) goto _OVER;
105,618,092✔
458

459
  pTrans = sdbGetRowObj(pRow);
105,618,092✔
460
  if (pTrans == NULL) goto _OVER;
105,618,092✔
461

462
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
105,618,092✔
463

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

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

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

504
  if (pTrans->prepareActions == NULL) goto _OVER;
105,618,092✔
505
  if (pTrans->redoActions == NULL) goto _OVER;
105,618,092✔
506
  if (pTrans->undoActions == NULL) goto _OVER;
105,618,092✔
507
  if (pTrans->commitActions == NULL) goto _OVER;
105,618,092✔
508

509
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
105,618,092✔
510
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
105,618,092✔
511
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,598,362✔
512
                                      sver) < 0)
513
      goto _OVER;
×
514
  } else {
515
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
104,019,730✔
516
  }
517
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
105,618,092✔
518
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
105,618,092✔
519

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

529
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
105,618,092✔
530

531
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
105,618,092✔
532
  if (arbgroupIdNum > 0) {
105,618,092✔
533
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
41,370✔
534
    if (pTrans->arbGroupIds == NULL) goto _OVER;
41,370✔
535
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
90,370✔
536
      int32_t arbGroupId = 0;
49,000✔
537
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
49,000✔
538
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
49,000✔
539
    }
540
  }
541

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

551
  if (sver > TRANS_VER2_NUMBER) {
105,618,092✔
552
    int32_t groupNum = -1;
105,618,092✔
553
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
105,618,092✔
554
    for (int32_t i = 0; i < groupNum; ++i) {
110,311,998✔
555
      int32_t groupId = -1;
4,693,906✔
556
      int32_t groupPos = -1;
4,693,906✔
557
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
4,693,906✔
558
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
4,693,906✔
559
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
4,693,906✔
560
        goto _OVER;
×
561
    }
562
  }
563

564
  if (sver >= TRANS_VER_USER_DATA) {
105,618,092✔
565
    SDB_GET_INT32(pRaw, dataPos, &pTrans->userDataLen, _OVER)
105,618,092✔
566
    if (pTrans->userDataLen > 0) {
105,618,092✔
567
      pTrans->userData = taosMemoryMalloc(pTrans->userDataLen);
171,000✔
568
      if (pTrans->userData == NULL) goto _OVER;
171,000✔
569
      SDB_GET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
171,000✔
570
    }
571
  }
572

573
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
105,618,092✔
574

575
  terrno = 0;
105,618,092✔
576

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

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

592
static const char *mndTransStr(ETrnStage stage) {
742,918,349✔
593
  switch (stage) {
742,918,349✔
594
    case TRN_STAGE_PREPARE:
59,822,138✔
595
      return "prepare";
59,822,138✔
596
    case TRN_STAGE_REDO_ACTION:
255,638,924✔
597
      return "redoAction";
255,638,924✔
598
    case TRN_STAGE_ROLLBACK:
8,739✔
599
      return "rollback";
8,739✔
600
    case TRN_STAGE_UNDO_ACTION:
11,481,862✔
601
      return "undoAction";
11,481,862✔
602
    case TRN_STAGE_COMMIT:
95,745,836✔
603
      return "commit";
95,745,836✔
604
    case TRN_STAGE_COMMIT_ACTION:
178,531,769✔
605
      return "commitAction";
178,531,769✔
606
    case TRN_STAGE_FINISH:
141,680,131✔
607
      return "finished";
141,680,131✔
608
    case TRN_STAGE_PRE_FINISH:
8,950✔
609
      return "pre-finish";
8,950✔
610
    default:
×
611
      return "invalid";
×
612
  }
613
}
614

615
static const char *mndTransTypeStr(ETrnAct actionType) {
63,195✔
616
  switch (actionType) {
63,195✔
617
    case TRANS_ACTION_MSG:
51,931✔
618
      return "msg";
51,931✔
619
    case TRANS_ACTION_RAW:
11,264✔
620
      return "sdb";
11,264✔
621
    default:
×
622
      return "invalid";
×
623
  }
624
}
625

626
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
206,906,399✔
627
  if (pAction != NULL) {
206,906,399✔
628
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
158,776,429✔
629
      pTrans->lastAction = pAction->id;
112,919,213✔
630
      pTrans->lastMsgType = pAction->msgType;
112,919,213✔
631
      pTrans->lastEpset = pAction->epSet;
112,919,213✔
632
      pTrans->lastErrorNo = pAction->errCode;
112,919,213✔
633
    }
634
  } else {
635
    pTrans->lastAction = 0;
48,129,970✔
636
    pTrans->lastMsgType = 0;
48,129,970✔
637
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
48,129,970✔
638
    pTrans->lastErrorNo = 0;
48,129,970✔
639
  }
640
}
206,906,399✔
641

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

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

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

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

669
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
20,057,776✔
670

671
  if (pTrans->startFunc > 0) {
20,057,776✔
672
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
267,614✔
673
    if (fp) {
267,614✔
674
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
267,614✔
675
    }
676
    // pTrans->startFunc = 0;
677
  }
678

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

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

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

694
  return 0;
20,057,776✔
695
}
696

697
void mndTransDropData(STrans *pTrans) {
124,340,706✔
698
  if (pTrans->prepareActions != NULL) {
124,340,706✔
699
    mndTransDropActions(pTrans->prepareActions);
124,340,706✔
700
    pTrans->prepareActions = NULL;
124,340,706✔
701
  }
702
  if (pTrans->redoActions != NULL) {
124,340,706✔
703
    mndTransDropActions(pTrans->redoActions);
124,340,706✔
704
    pTrans->redoActions = NULL;
124,340,706✔
705
  }
706
  if (pTrans->undoActions != NULL) {
124,340,706✔
707
    mndTransDropActions(pTrans->undoActions);
124,340,706✔
708
    pTrans->undoActions = NULL;
124,340,706✔
709
  }
710
  if (pTrans->commitActions != NULL) {
124,340,706✔
711
    mndTransDropActions(pTrans->commitActions);
124,340,706✔
712
    pTrans->commitActions = NULL;
124,340,706✔
713
  }
714
  if (pTrans->redoGroupActions != NULL) {
124,340,706✔
715
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
20,320,976✔
716
    while (pIter) {
25,249,326✔
717
      SArray **redoActions = pIter;
4,928,350✔
718
      taosArrayDestroy(*redoActions);
4,928,350✔
719
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,928,350✔
720
    }
721

722
    taosHashCleanup(pTrans->redoGroupActions);
20,320,976✔
723
    pTrans->redoGroupActions = NULL;
20,320,976✔
724
  }
725
  if (pTrans->groupActionPos != NULL) {
124,340,706✔
726
    taosHashCleanup(pTrans->groupActionPos);
20,320,976✔
727
    pTrans->groupActionPos = NULL;
20,320,976✔
728
  }
729
  if (pTrans->arbGroupIds != NULL) {
124,340,706✔
730
    taosHashCleanup(pTrans->arbGroupIds);
18,763,984✔
731
  }
732
  if (pTrans->pRpcArray != NULL) {
124,340,706✔
733
    taosArrayDestroy(pTrans->pRpcArray);
18,722,614✔
734
    pTrans->pRpcArray = NULL;
18,722,614✔
735
  }
736
  if (pTrans->rpcRsp != NULL) {
124,340,706✔
737
    taosMemoryFree(pTrans->rpcRsp);
7,362,980✔
738
    pTrans->rpcRsp = NULL;
7,362,980✔
739
    pTrans->rpcRspLen = 0;
7,362,980✔
740
  }
741
  if (pTrans->param != NULL) {
124,340,706✔
742
    taosMemoryFree(pTrans->param);
×
743
    pTrans->param = NULL;
×
744
    pTrans->paramLen = 0;
×
745
  }
746
  if (pTrans->userData != NULL) {
124,340,706✔
747
    taosMemoryFree(pTrans->userData);
171,122✔
748
    pTrans->userData = NULL;
171,122✔
749
    pTrans->userDataLen = 0;
171,122✔
750
  }
751
  (void)taosThreadMutexDestroy(&pTrans->mutex);
124,340,706✔
752
}
124,340,706✔
753

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

758
  if (pTrans->stopFunc > 0 && callFunc) {
62,835,895✔
759
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
267,614✔
760
    if (fp) {
267,614✔
761
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
267,614✔
762
    }
763
    // pTrans->stopFunc = 0;
764
  }
765

766
  mndTransDropData(pTrans);
62,835,895✔
767
  return 0;
62,835,895✔
768
}
769

770
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
90,970,460✔
771
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
272,401,720✔
772
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
181,431,260✔
773
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
181,431,260✔
774
    pOldAction->rawWritten = pNewAction->rawWritten;
181,431,260✔
775
    pOldAction->msgSent = pNewAction->msgSent;
181,431,260✔
776
    pOldAction->msgReceived = pNewAction->msgReceived;
181,431,260✔
777
    pOldAction->errCode = pNewAction->errCode;
181,431,260✔
778
  }
779
}
90,970,460✔
780

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

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

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

803
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
22,742,615✔
804
  while (pIter != NULL) {
24,730,945✔
805
    int32_t newActionPos = *(int32_t *)pIter;
1,988,330✔
806

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

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

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

819
  if (pOld->stage == TRN_STAGE_COMMIT) {
22,742,615✔
820
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
20,033,346✔
821
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
20,033,346✔
822
  }
823

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

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

834
  return 0;
22,742,615✔
835
}
836

837
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
84,550,379✔
838
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
84,550,379✔
839
  if (pTrans == NULL) {
84,550,379✔
840
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
141✔
841
  }
842
  return pTrans;
84,550,379✔
843
}
844

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

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

860
  if (opername != NULL) {
18,722,614✔
861
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
18,722,614✔
862
  }
863

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

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

894
  if (pReq != NULL) {
18,722,614✔
895
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
25,668,786✔
896
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
897
      return NULL;
×
898
    }
899
    pTrans->originRpcType = pReq->msgType;
12,834,393✔
900
  }
901

902
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
18,722,614✔
903

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

908
static void mndTransDropActions(SArray *pArray) {
497,362,824✔
909
  int32_t size = taosArrayGetSize(pArray);
497,362,824✔
910
  for (int32_t i = 0; i < size; ++i) {
1,324,022,508✔
911
    STransAction *pAction = taosArrayGet(pArray, i);
826,659,684✔
912
    if (pAction->actionType == TRANS_ACTION_RAW) {
826,659,684✔
913
      taosMemoryFreeClear(pAction->pRaw);
556,759,379✔
914
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
269,900,305✔
915
      taosMemoryFreeClear(pAction->pCont);
269,900,305✔
916
    } else {
917
      // nothing
918
    }
919
  }
920

921
  taosArrayDestroy(pArray);
497,362,824✔
922
}
497,362,824✔
923

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

932
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
109,962,626✔
933
  pAction->id = taosArrayGetSize(pArray);
109,962,626✔
934

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

940
  return 0;
109,962,626✔
941
}
942

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

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

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

975
  return 0;
539,474✔
976
}
977

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1095
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
10,395,207✔
1096
  if (dbname != NULL) {
10,395,207✔
1097
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
10,395,207✔
1098
  }
1099
  if (stbname != NULL) {
10,395,207✔
1100
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
7,897,349✔
1101
  }
1102
}
10,395,207✔
1103

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

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

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

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

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

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

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

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

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

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

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

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

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

1176
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
5,012,651✔
1177
  if (conflict[0] == 0) return false;
5,012,651✔
1178
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
4,894,310✔
1179
  return false;
4,860,220✔
1180
}
1181

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

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

1201
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
49,102,095✔
1202

1203
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
37,390,973✔
1204
  mDebug("trans:%d, trans hash size %d", pNew->id, size);
37,390,973✔
1205

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

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

1212
    if (pNew->conflict == TRN_CONFLICT_DB) {
5,091,522✔
1213
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
142,235✔
1214
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
142,235✔
1215
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
118,341✔
1216
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
118,341✔
1217
      }
1218
    }
1219

1220
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
5,091,522✔
1221
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
4,910,951✔
1222
      if (pTrans->conflict == TRN_CONFLICT_DB) {
4,910,951✔
1223
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
67,500✔
1224
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
67,500✔
1225
      }
1226
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4,910,951✔
1227
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
4,826,810✔
1228
      }
1229
    }
1230

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

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

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

1260
  return conflict;
37,390,973✔
1261
}
1262

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

1273
  if (mndCheckTransConflict(pMnode, pTrans)) {
49,102,095✔
1274
    code = TSDB_CODE_MND_TRANS_CONFLICT;
133,567✔
1275
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
133,567✔
1276
    TAOS_RETURN(code);
133,567✔
1277
  }
1278

1279
  TAOS_RETURN(code);
48,968,528✔
1280
}
1281

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

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

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

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

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

1317
  TAOS_RETURN(code);
152,170✔
1318
}
1319

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

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

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

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

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

1356
  TAOS_RETURN(code);
152,170✔
1357
}
1358

1359
static bool mndTransActionsOfSameType(SArray *pActions) {
42,344,488✔
1360
  int32_t size = taosArrayGetSize(pActions);
42,344,488✔
1361
  ETrnAct lastActType = TRANS_ACTION_NULL;
42,344,488✔
1362
  bool    same = true;
42,344,488✔
1363
  for (int32_t i = 0; i < size; ++i) {
132,106,303✔
1364
    STransAction *pAction = taosArrayGet(pActions, i);
89,761,815✔
1365
    if (i > 0) {
89,761,815✔
1366
      if (lastActType != pAction->actionType) {
59,790,945✔
1367
        same = false;
×
1368
        break;
×
1369
      }
1370
    }
1371
    lastActType = pAction->actionType;
89,761,815✔
1372
  }
1373
  return same;
42,344,488✔
1374
}
1375

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

1385
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
18,189,139✔
1386
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
5,593,264✔
1387
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1388
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1389
        TAOS_RETURN(code);
×
1390
      }
1391
    }
1392
  }
1393

1394
  TAOS_RETURN(code);
18,562,085✔
1395
}
1396

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

1410
  TAOS_RETURN(code);
18,562,085✔
1411
}
1412

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

1419
  mInfo("trans:%d, action list:", pTrans->id);
18,563,621✔
1420
  int32_t index = 0;
18,563,621✔
1421
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
28,716,140✔
1422
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
10,152,519✔
1423
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
10,152,519✔
1424
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1425
  }
1426

1427
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
38,729,702✔
1428
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
20,166,081✔
1429
    if (pAction->actionType == TRANS_ACTION_MSG) {
20,166,081✔
1430
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
19,543,748✔
1431
            TMSG_INFO(pAction->msgType));
1432
      ;
1433
    } else {
1434
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
622,333✔
1435
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1436
    }
1437
  }
1438

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

1445
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
29,976,292✔
1446
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
11,412,671✔
1447
    if (pAction->actionType == TRANS_ACTION_MSG) {
11,412,671✔
1448
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
7,316,925✔
1449
            TMSG_INFO(pAction->msgType));
1450
    } else {
1451
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
4,095,746✔
1452
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1453
    }
1454
  }
1455

1456
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
18,563,621✔
1457

1458
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
18,562,085✔
1459

1460
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
18,562,085✔
1461

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

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

1480
  pNew->pRpcArray = pTrans->pRpcArray;
18,561,769✔
1481
  pNew->rpcRsp = pTrans->rpcRsp;
18,561,769✔
1482
  pNew->rpcRspLen = pTrans->rpcRspLen;
18,561,769✔
1483
  pNew->mTraceId = pTrans->mTraceId;
18,561,769✔
1484
  pTrans->pRpcArray = NULL;
18,561,769✔
1485
  pTrans->rpcRsp = NULL;
18,561,769✔
1486
  pTrans->rpcRspLen = 0;
18,561,769✔
1487

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

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

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

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

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

1532
  if (pTrans->stage == TRN_STAGE_FINISH) {
84,550,238✔
1533
    sendRsp = true;
38,595,234✔
1534
  }
1535

1536
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
84,550,238✔
1537
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
22,554,158✔
1538
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
9,905✔
1539
      sendRsp = true;
9,905✔
1540
    }
1541
  } else {
1542
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
61,996,080✔
1543
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
35,226,691✔
1544
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1545
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1546
      } else {
1547
        if (pTrans->failedTimes > 6) sendRsp = true;
35,226,691✔
1548
      }
1549
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
35,226,691✔
1550
    }
1551
  }
1552

1553
  if (!sendRsp) {
84,550,238✔
1554
    return;
45,945,099✔
1555
  } else {
1556
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
38,605,139✔
1557
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1558
  }
1559

1560
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
38,605,139✔
1561
  taosWLockLatch(&pTrans->lockRpcArray);
38,605,139✔
1562
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
38,605,139✔
1563
  if (size <= 0) {
38,605,139✔
1564
    taosWUnLockLatch(&pTrans->lockRpcArray);
25,905,053✔
1565
    return;
25,905,053✔
1566
  }
1567

1568
  for (int32_t i = 0; i < size; ++i) {
25,400,172✔
1569
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
12,700,086✔
1570
    if (pInfo->handle != NULL) {
12,700,086✔
1571
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
12,211,070✔
1572
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1573
      }
1574
      if (code == TSDB_CODE_SYN_TIMEOUT) {
12,211,070✔
1575
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1576
      }
1577

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

1584
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
12,211,070✔
1585

1586
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
12,211,070✔
1587
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,272,386✔
1588
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,272,386✔
1589
        if (pDb != NULL) {
1,272,386✔
1590
          for (int32_t j = 0; j < 12; j++) {
1,622,930✔
1591
            bool ready = mndIsDbReady(pMnode, pDb);
1,621,918✔
1592
            if (!ready) {
1,621,918✔
1593
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
350,544✔
1594
              taosMsleep(1000);
350,544✔
1595
            } else {
1596
              break;
1,271,374✔
1597
            }
1598
          }
1599
        }
1600
        mndReleaseDb(pMnode, pDb);
1,272,386✔
1601
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
10,938,684✔
1602
        void   *pCont = NULL;
1,888,496✔
1603
        int32_t contLen = 0;
1,888,496✔
1604
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
1,888,496✔
1605
          mndTransSetRpcRsp(pTrans, pCont, contLen);
1,886,888✔
1606
        }
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
9,050,188✔
1608
        void   *pCont = NULL;
9,446✔
1609
        int32_t contLen = 0;
9,446✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
9,446✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
9,446✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
9,040,742✔
1614
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
9,031✔
1615
        if (code != 0) {
9,031✔
1616
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1617
        }
1618
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
9,031,711✔
1619
        void   *pCont = NULL;
17,403✔
1620
        int32_t contLen = 0;
17,403✔
1621
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
17,403✔
1622
          mndTransSetRpcRsp(pTrans, pCont, contLen);
17,403✔
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
9,014,308✔
1625
        void   *pCont = NULL;
16,675✔
1626
        int32_t contLen = 0;
16,675✔
1627
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
16,675✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
16,675✔
1629
        }
1630
      }
1631

1632
      if (pTrans->rpcRspLen != 0) {
12,211,070✔
1633
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
7,347,493✔
1634
        if (rpcCont != NULL) {
7,347,493✔
1635
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
7,347,493✔
1636
          rspMsg.pCont = rpcCont;
7,347,493✔
1637
          rspMsg.contLen = pTrans->rpcRspLen;
7,347,493✔
1638
        }
1639
      }
1640

1641
      tmsgSendRsp(&rspMsg);
12,211,070✔
1642

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

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

1671
  SArray *pArray = NULL;
21,600,049✔
1672
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
21,600,049✔
1673
    pArray = pTrans->redoActions;
21,593,621✔
1674
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
6,428✔
1675
    pArray = pTrans->undoActions;
6,428✔
1676
  } else {
1677
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1678
    goto _OVER;
×
1679
  }
1680

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

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

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

1699
      // pTrans->lastErrorNo = pRsp->code;
1700
      mndSetTransLastAction(pTrans, pAction);
21,600,049✔
1701

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

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

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

1719
_OVER:
21,600,049✔
1720
  mndReleaseTrans(pMnode, pTrans);
21,600,049✔
1721
  TAOS_RETURN(code);
21,600,049✔
1722
}
1723

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

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

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

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

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

1760
// execute in sync context
1761
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
152,329,665✔
1762
  if (pAction->rawWritten) return 0;
152,329,665✔
1763
  if (topHalf) {
83,638,404✔
1764
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
360✔
1765
  }
1766

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

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

1782
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
83,638,044✔
1783
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
83,638,044✔
1784
    pAction->rawWritten = true;
83,638,044✔
1785
    pAction->errCode = 0;
83,638,044✔
1786
    code = 0;
83,638,044✔
1787
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
83,638,044✔
1788
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1789

1790
    mndSetTransLastAction(pTrans, pAction);
83,638,044✔
1791
  } else {
1792
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1793
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1794
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1795
    mndSetTransLastAction(pTrans, pAction);
×
1796
  }
1797

1798
  TAOS_RETURN(code);
83,638,044✔
1799
}
1800

1801
// execute in trans context
1802
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
91,762,935✔
1803
                                     bool notSend) {
1804
  if (pAction->msgSent) return 0;
91,762,935✔
1805
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
35,030,033✔
1806
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
13,418,465✔
1807
  }
1808

1809
  if (notSend) {
21,611,568✔
1810
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
7,660✔
1811
    return 0;
7,660✔
1812
  }
1813

1814
#ifndef TD_ASTRA_32
1815
  int64_t signature = pTrans->id;
21,603,908✔
1816
  signature = (signature << 32);
21,603,908✔
1817
  signature += pAction->id;
21,603,908✔
1818

1819
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
21,603,908✔
1820
#else
1821
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1822
                    .contLen = pAction->contLen,
1823
                    .info.ahandle = (void *)pTrans->id,
1824
                    .info.ahandleEx = (void *)pAction->id};
1825
#endif
1826
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
21,603,908✔
1827
  if (rpcMsg.pCont == NULL) {
21,603,908✔
1828
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1829
    return -1;
×
1830
  }
1831
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
21,603,908✔
1832
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
21,603,908✔
1833
  rpcMsg.info.notFreeAhandle = 1;
21,603,908✔
1834
  STraceId* trace = &(rpcMsg.info.traceId);
21,603,908✔
1835

1836
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
21,603,908✔
1837

1838
  char    detail[1024] = {0};
21,603,908✔
1839
  int32_t len = snprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
21,603,908✔
1840
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
21,603,908✔
1841
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
45,984,159✔
1842
    len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
24,380,251✔
1843
                    pAction->epSet.eps[i].port);
24,380,251✔
1844
  }
1845

1846
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
21,603,908✔
1847
  if (code == 0) {
21,603,908✔
1848
    pAction->msgSent = 1;
21,603,820✔
1849
    // pAction->msgReceived = 0;
1850
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
21,603,820✔
1851
    pAction->startTime = taosGetTimestampMs();
21,603,820✔
1852
    pAction->endTime = 0;
21,603,820✔
1853
    mInfo("trans:%d, %s:%d is sent, QID:0x%" PRIx64 ":0x%" PRIx64 ", %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, trace ? trace->rootId : 0, 
21,603,820✔
1854
              trace ? trace->msgId : 0, detail);
1855

1856
    mndSetTransLastAction(pTrans, pAction);
21,603,820✔
1857
  } else {
1858
    pAction->msgSent = 0;
88✔
1859
    pAction->msgReceived = 0;
88✔
1860
    pAction->errCode = (terrno != 0) ? terrno : code;
88✔
1861
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
88✔
1862
           detail);
1863

1864
    mndSetTransLastAction(pTrans, pAction);
88✔
1865
  }
1866

1867
  TAOS_RETURN(code);
21,603,908✔
1868
}
1869

1870
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1871
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1872
  pAction->rawWritten = 0;
×
1873
  pAction->errCode = 0;
×
1874
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1875

1876
  mndSetTransLastAction(pTrans, pAction);
×
1877
  return 0;
×
1878
}
1879

1880
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
244,092,600✔
1881
                                        bool notSend) {
1882
  if (pAction->actionType == TRANS_ACTION_RAW) {
244,092,600✔
1883
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
152,329,665✔
1884
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
91,762,935✔
1885
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
91,762,935✔
1886
  } else {
1887
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1888
  }
1889
}
1890

1891
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
76,548,499✔
1892
  int32_t numOfActions = taosArrayGetSize(pArray);
76,548,499✔
1893
  int32_t code = 0;
76,548,499✔
1894

1895
  for (int32_t action = 0; action < numOfActions; ++action) {
285,041,314✔
1896
    STransAction *pAction = taosArrayGet(pArray, action);
219,000,883✔
1897
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
219,000,883✔
1898
    if (code != 0) {
219,000,883✔
1899
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
10,508,068✔
1900
            numOfActions);
1901
      break;
10,508,068✔
1902
    }
1903
  }
1904

1905
  return code;
76,548,499✔
1906
}
1907

1908
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
94,322,311✔
1909
  int32_t numOfActions = taosArrayGetSize(pArray);
94,322,311✔
1910
  int32_t code = 0;
94,322,311✔
1911
  if (numOfActions == 0) return 0;
94,322,311✔
1912

1913
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
76,548,499✔
1914
    return code;
10,508,068✔
1915
  }
1916

1917
  int32_t       numOfExecuted = 0;
66,040,431✔
1918
  int32_t       errCode = 0;
66,040,431✔
1919
  STransAction *pErrAction = NULL;
66,040,431✔
1920
  for (int32_t action = 0; action < numOfActions; ++action) {
274,533,246✔
1921
    STransAction *pAction = taosArrayGet(pArray, action);
208,492,815✔
1922
    if (pAction->msgReceived || pAction->rawWritten) {
208,492,815✔
1923
      numOfExecuted++;
173,778,214✔
1924
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
173,778,214✔
1925
        errCode = pAction->errCode;
3,100✔
1926
        pErrAction = pAction;
3,100✔
1927
      }
1928
    } else {
1929
      pErrAction = pAction;
34,714,601✔
1930
    }
1931
  }
1932

1933
  mndSetTransLastAction(pTrans, pErrAction);
66,040,431✔
1934

1935
  if (numOfExecuted == numOfActions) {
66,040,431✔
1936
    if (errCode == 0) {
48,131,490✔
1937
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
48,129,970✔
1938
      return 0;
48,129,970✔
1939
    } else {
1940
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,520✔
1941
      mndTransResetActions(pMnode, pTrans, pArray);
1,520✔
1942
      terrno = errCode;
1,520✔
1943
      return errCode;
1,520✔
1944
    }
1945
  } else {
1946
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
17,908,941✔
1947

1948
    for (int32_t action = 0; action < numOfActions; ++action) {
68,929,759✔
1949
      STransAction *pAction = taosArrayGet(pArray, action);
51,020,818✔
1950
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
51,020,818✔
1951
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1952
             pAction->acceptableCode, pAction->retryCode);
1953
      if (pAction->msgSent) {
51,020,818✔
1954
        bool reset = false;
51,013,158✔
1955
        if (pAction->msgReceived) {
51,013,158✔
1956
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
16,306,217✔
1957
        } else {
1958
          int64_t timestamp = taosGetTimestampMs();
34,706,941✔
1959
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
34,706,941✔
1960
        }
1961
        if (reset) {
51,013,158✔
1962
          mndTransResetAction(pMnode, pTrans, pAction);
1,580✔
1963
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1,580✔
1964
                pAction->id, pAction->errCode, pAction->startTime);
1965
        }
1966
      }
1967
    }
1968
    return TSDB_CODE_ACTION_IN_PROGRESS;
17,908,941✔
1969
  }
1970
}
1971

1972
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
55,719,449✔
1973
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
55,719,449✔
1974
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
55,719,449✔
1975
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
1,608✔
1976
           mndStrExecutionContext(topHalf));
1977
  }
1978
  return code;
55,719,449✔
1979
}
1980

1981
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
11,424✔
1982
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf, notSend);
11,424✔
1983
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
11,424✔
1984
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1985
           mndStrExecutionContext(topHalf));
1986
  }
1987
  return code;
11,424✔
1988
}
1989

1990
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
38,591,438✔
1991
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
38,591,438✔
1992
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
38,591,438✔
1993
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1994
           mndStrExecutionContext(topHalf));
1995
  }
1996
  return code;
38,591,438✔
1997
}
1998

1999
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
7,351,479✔
2000
  int32_t code = 0;
7,351,479✔
2001
  int32_t numOfActions = taosArrayGetSize(pActions);
7,351,479✔
2002
  if (numOfActions == 0) return code;
7,351,479✔
2003

2004
  if (pTrans->actionPos >= numOfActions) {
7,349,933✔
2005
    return code;
313,619✔
2006
  }
2007

2008
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
7,036,314✔
2009
        pTrans->actionPos, mndTransStr(pTrans->stage));
2010

2011
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
11,158,942✔
2012
    STransAction *pAction = taosArrayGet(pActions, action);
10,882,013✔
2013

2014
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
10,882,013✔
2015
      code = TSDB_CODE_ACTION_IN_PROGRESS;
7,605✔
2016
      break;
7,809✔
2017
    }
2018

2019
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
10,874,408✔
2020
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
2021

2022
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
10,874,408✔
2023
    if (code == 0) {
10,874,408✔
2024
      if (pAction->msgSent) {
9,265,188✔
2025
        bool reset = false;
8,153,763✔
2026
        if (pAction->msgReceived) {
8,153,763✔
2027
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,586,339✔
2028
            code = pAction->errCode;
2,069,300✔
2029
            reset = true;
2,069,300✔
2030
          } else {
2031
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,517,039✔
2032
          }
2033
        } else {
2034
          int64_t timestamp = taosGetTimestampMs();
4,567,424✔
2035
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
4,567,424✔
2036
          code = TSDB_CODE_ACTION_IN_PROGRESS;
4,567,424✔
2037
        }
2038
        if (reset) {
8,153,763✔
2039
          mndTransResetAction(pMnode, pTrans, pAction);
2,069,300✔
2040
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,069,300✔
2041
                pAction->id, pAction->errCode, pAction->startTime);
2042
        }
2043
      } else if (pAction->rawWritten) {
1,111,425✔
2044
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,111,425✔
2045
          code = pAction->errCode;
×
2046
        } else {
2047
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,111,425✔
2048
        }
2049
      } else {
2050
      }
2051
    }
2052

2053
    if (code == 0) {
10,874,408✔
2054
      pTrans->failedTimes = 0;
2,628,464✔
2055
    }
2056
    mndSetTransLastAction(pTrans, pAction);
10,874,408✔
2057
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
10,874,408✔
2058
      mInfo(
6,176,644✔
2059
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2060
          "msgReceived:%d",
2061
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2062
          pAction->msgSent, pAction->msgReceived);
2063
    } else if (code != 0) {
4,697,764✔
2064
      mError(
2,069,300✔
2065
          "trans:%d, failed 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
    }
2070

2071
    char str[200] = {0};
10,874,408✔
2072
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
10,874,408✔
2073
      pTrans->lastErrorNo = code;
2,184,356✔
2074
      pTrans->code = code;
2,184,356✔
2075
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
2,184,356✔
2076
            action, str);
2077
      break;
2,184,356✔
2078
    }
2079

2080
    if (code == 0) {
8,690,052✔
2081
      pTrans->code = 0;
2,053,328✔
2082
      pTrans->actionPos++;
2,053,328✔
2083
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
2,053,328✔
2084
            pAction->id);
2085
      (void)taosThreadMutexUnlock(&pTrans->mutex);
2,053,328✔
2086
      code = mndTransSync(pMnode, pTrans);
2,053,328✔
2087
      (void)taosThreadMutexLock(&pTrans->mutex);
2,053,328✔
2088
      if (code != 0) {
2,053,328✔
2089
        pTrans->actionPos--;
×
2090
        pTrans->code = terrno;
×
2091
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2092
               mndTransStr(pAction->stage), pAction->id, terrstr());
2093
        break;
×
2094
      }
2095
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
6,636,724✔
2096
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,567,424✔
2097
      break;
4,567,424✔
2098
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
2,069,300✔
2099
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
897✔
2100
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
2,069,300✔
2101
            code, tstrerror(code));
2102
      pTrans->lastErrorNo = code;
2,069,300✔
2103
      taosMsleep(300);
2,069,300✔
2104
      action--;
2,069,300✔
2105
      continue;
2,069,300✔
2106
    } else {
2107
      terrno = code;
×
2108
      pTrans->lastErrorNo = code;
×
2109
      pTrans->code = code;
×
2110
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2111
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2112
      break;
×
2113
    }
2114
  }
2115

2116
  return code;
7,036,314✔
2117
}
2118

2119
static int32_t mndTransExecuteActionsSerialGroup(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf,
3,897,251✔
2120
                                                 int32_t groupId, int32_t currentGroup, int32_t groupCount,
2121
                                                 bool notSend, SHashObj *pHash) {
2122
  int32_t code = 0;
3,897,251✔
2123
  int32_t numOfActions = taosArrayGetSize(pActions);
3,897,251✔
2124
  if (numOfActions == 0) return code;
3,897,251✔
2125

2126
  if (groupId <= 0) {
3,897,251✔
2127
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
2128
    return TSDB_CODE_INTERNAL_ERROR;
×
2129
  }
2130

2131
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &groupId, sizeof(int32_t));
3,897,251✔
2132
  if (actionPos == NULL) {
3,897,251✔
2133
    mError("trans:%d, failed to execute action in serail group, actionPos is null at group %d", pTrans->id, groupId);
×
2134
    return TSDB_CODE_INTERNAL_ERROR;
×
2135
  }
2136

2137
  if (*actionPos >= numOfActions) {
3,897,251✔
2138
    mInfo("trans:%d, this serial group is finished, actionPos:%d >= numOfActions:%d at group %d", pTrans->id,
576,642✔
2139
          *actionPos, numOfActions, groupId);
2140
    return TSDB_CODE_MND_TRANS_GROUP_FINISHED;
576,642✔
2141
  }
2142

2143
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
3,861,930✔
2144
    STransAction **ppAction = taosArrayGet(pActions, action);
3,630,867✔
2145
    STransAction  *pAction = *ppAction;
3,630,867✔
2146

2147
    if (notSend && !pAction->msgSent) {
3,630,867✔
2148
      mInfo(
481,308✔
2149
          "trans:%d, %s:%d (%d/%d at group %d) skip to execute, curent state(actionType(1:msg,2:log):%d, "
2150
          "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2151
          pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2152
          pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2153
      code = TSDB_CODE_ACTION_IN_PROGRESS;
481,308✔
2154
      break;
481,308✔
2155
    }
2156

2157
    mInfo(
3,149,559✔
2158
        "trans:%d, %s:%d (%d/%d at group %d) begin to execute, curent state(actionType(1:msg,2:log):%d, "
2159
        "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2160
        pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2161
        pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2162

2163
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
3,149,559✔
2164
    if (code == 0) {
3,149,559✔
2165
      if (pAction->msgSent) {
1,847,934✔
2166
        if (pAction->msgReceived) {
1,638,635✔
2167
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
480,527✔
2168
            code = pAction->errCode;
26,348✔
2169
            mndTransResetAction(pMnode, pTrans, pAction);
26,348✔
2170
          } else {
2171
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
454,179✔
2172
                  pAction->id, action, numOfActions, groupId);
2173
          }
2174
        } else {
2175
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,158,108✔
2176
        }
2177
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,638,635✔
2178
        if (msgSent != NULL) {
1,638,635✔
2179
          *msgSent = pAction->msgSent;
1,638,635✔
2180
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,638,635✔
2181
        } else {
2182
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2183
        }
2184
      } else if (pAction->rawWritten) {
209,299✔
2185
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
209,299✔
2186
          code = pAction->errCode;
×
2187
        } else {
2188
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
209,299✔
2189
        }
2190
      } else {
2191
      }
2192
    }
2193

2194
    if (code == 0) {
3,149,559✔
2195
      pTrans->failedTimes = 0;
663,478✔
2196
    }
2197
    mndSetTransLastAction(pTrans, pAction);
3,149,559✔
2198

2199
    char str[200] = {0};
3,149,559✔
2200
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
3,149,559✔
2201
      pTrans->lastErrorNo = code;
1,450,130✔
2202
      pTrans->code = code;
1,450,130✔
2203
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
1,450,130✔
2204
        mInfo(
1,301,625✔
2205
            "trans:%d, %s:%d (%d/%d at group %d) not able to execute since %s, current state("
2206
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2207
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2208
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2209
      } else if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
148,505✔
2210
        mError(
×
2211
            "trans:%d, %s:%d (%d/%d at group %d) failed to execute since %s, current action("
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
      }
2216
      mInfo("trans:%d, %s:%d (%d/%d at group %d) cannot execute next action, stop this group execution, %s", pTrans->id,
1,450,130✔
2217
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2218
      break;
1,450,130✔
2219
    }
2220

2221
    if (code == 0) {
1,699,429✔
2222
      pTrans->code = 0;
514,973✔
2223
      pTrans->actionPos++;
514,973✔
2224
      (*actionPos)++;
514,973✔
2225
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
514,973✔
2226
            pAction->id);
2227
      (void)taosThreadMutexUnlock(&pTrans->mutex);
514,973✔
2228
      code = mndTransSync(pMnode, pTrans);
514,973✔
2229
      (void)taosThreadMutexLock(&pTrans->mutex);
514,973✔
2230
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
514,973✔
2231
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
11,977,418✔
2232
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
11,462,445✔
2233
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
11,462,445✔
2234
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
11,462,445✔
2235
          pTmpAction->msgSent = *msgSent;
586,239✔
2236
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
586,239✔
2237
        }
2238
      }
2239
      if (code != 0) {
514,973✔
2240
        pTrans->actionPos--;
×
2241
        (*actionPos)--;
×
2242
        pTrans->code = terrno;
×
2243
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2244
               mndTransStr(pAction->stage), pAction->id, terrstr());
2245
        break;
×
2246
      }
2247
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
1,184,456✔
2248
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,158,108✔
2249
            mndTransStr(pAction->stage), pAction->id);
2250
      break;
1,158,108✔
2251
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
26,348✔
2252
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
×
2253
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
26,348✔
2254
            code, tstrerror(code));
2255
      pTrans->lastErrorNo = code;
26,348✔
2256
      taosMsleep(300);
26,348✔
2257
      action--;
26,348✔
2258
      continue;
26,348✔
2259
    } else {
2260
      terrno = code;
×
2261
      pTrans->lastErrorNo = code;
×
2262
      pTrans->code = code;
×
2263
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2264
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2265
      break;
×
2266
    }
2267
  }
2268

2269
  return code;
3,320,609✔
2270
}
2271

2272
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
7,321,466✔
2273
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
7,321,466✔
2274
  (void)taosThreadMutexLock(&pTrans->mutex);
7,321,466✔
2275
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
7,321,466✔
2276
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
7,321,466✔
2277
  }
2278
  (void)taosThreadMutexUnlock(&pTrans->mutex);
7,321,466✔
2279
  return code;
7,321,466✔
2280
}
2281

2282
static int32_t mndTransExecuteRedoActionGroup(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
1,249,028✔
2283
  int32_t total_code = TSDB_CODE_ACTION_IN_PROGRESS;
1,249,028✔
2284
  int32_t code = 0;
1,249,028✔
2285
  int32_t successCount = 0;
1,249,028✔
2286
  int32_t groupCount = taosHashGetSize(pTrans->redoGroupActions);
1,249,028✔
2287

2288
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,249,028✔
2289
  if(pHash == NULL){
1,249,028✔
2290
    mError("trans:%d, failed to init hash since %s", pTrans->id, terrstr());
×
2291
    return -1;
×
2292
  }
2293
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
28,418,608✔
2294
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
27,169,580✔
2295
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
27,169,580✔
2296
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
27,169,580✔
2297
  }
2298
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
1,249,028✔
2299

2300
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,249,028✔
2301
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,249,028✔
2302
  int32_t currentGroup = 1;
1,249,028✔
2303
  while (pIter) {
5,146,279✔
2304
    SArray **redoActions = pIter;
3,897,251✔
2305
    size_t   keyLen = 0;
3,897,251✔
2306
    int32_t *key = taosHashGetKey(pIter, &keyLen);
3,897,251✔
2307
    int32_t  actionCount = taosArrayGetSize(*redoActions);
3,897,251✔
2308
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
3,897,251✔
2309
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2310
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
3,897,251✔
2311
                                             notSend, pHash);
2312
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
3,897,251✔
2313
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,301,625✔
2314
            tstrerror(code));
2315
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,595,626✔
2316
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,639,416✔
2317
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
956,210✔
2318
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
576,642✔
2319
    } else if (code != 0) {
379,568✔
2320
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
×
2321
             tstrerror(code));
2322
    } else {
2323
      successCount++;
379,568✔
2324
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
379,568✔
2325
    }
2326
    currentGroup++;
3,897,251✔
2327
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
3,897,251✔
2328
  }
2329

2330
  taosHashCleanup(pHash);
1,249,028✔
2331

2332
  if (successCount == groupCount) {
1,249,028✔
2333
    total_code = 0;
26,554✔
2334
  } else {
2335
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,222,474✔
2336
  }
2337

2338
  return total_code;
1,249,028✔
2339
}
2340

2341
static int32_t mndTransExecuteRedoActionsParallel(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
1,463,714✔
2342
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
1,463,714✔
2343
  (void)taosThreadMutexLock(&pTrans->mutex);
1,463,714✔
2344

2345
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,463,714✔
2346
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,463,714✔
2347
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,463,714✔
2348
      code = 0;
184,673✔
2349
    } else {
2350
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,279,041✔
2351
      if (pAction != NULL && pAction->groupId == -1) {
1,279,041✔
2352
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
30,013✔
2353
      } else {
2354
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,249,028✔
2355
        if (code == 0) {
1,249,028✔
2356
          if (pTrans->actionPos < numOfActions) {
26,554✔
2357
            code = TSDB_CODE_ACTION_IN_PROGRESS;
19,717✔
2358
          }
2359
        }
2360
      }
2361
    }
2362
  }
2363

2364
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,463,714✔
2365

2366
  return code;
1,463,714✔
2367
}
2368

2369
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
2370
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
2371
  (void)taosThreadMutexLock(&pTrans->mutex);
×
2372
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2373
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
2374
  }
2375
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
2376
  return code;
×
2377
}
2378

2379
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
20,039,319✔
2380
  bool    continueExec = true;
20,039,319✔
2381
  int32_t code = 0;
20,039,319✔
2382
  terrno = 0;
20,039,319✔
2383

2384
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
20,039,319✔
2385
  if (numOfActions == 0) goto _OVER;
20,039,319✔
2386

2387
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
8,194,002✔
2388

2389
  for (int32_t action = 0; action < numOfActions; ++action) {
19,261,752✔
2390
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
11,067,750✔
2391
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
11,067,750✔
2392
    if (code != 0) {
11,067,750✔
2393
      terrno = code;
×
2394
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
2395
             numOfActions, tstrerror(code));
2396
      return false;
×
2397
    }
2398
  }
2399

2400
_OVER:
8,194,002✔
2401
  pTrans->stage = TRN_STAGE_REDO_ACTION;
20,039,319✔
2402
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
20,039,319✔
2403
  return continueExec;
20,039,319✔
2404
}
2405

2406
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
64,504,629✔
2407
  bool    continueExec = true;
64,504,629✔
2408
  int32_t code = 0;
64,504,629✔
2409
  terrno = 0;
64,504,629✔
2410

2411
  if (pTrans->exec == TRN_EXEC_SERIAL) {
64,504,629✔
2412
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
7,321,466✔
2413
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
57,183,163✔
2414
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
55,719,449✔
2415
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,463,714✔
2416
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,463,714✔
2417
  } else {
2418
    code = TSDB_CODE_INTERNAL_ERROR;
×
2419
  }
2420

2421
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
64,506,237✔
2422
      mndTransIsInSyncContext(topHalf)) {
1,608✔
2423
    pTrans->lastErrorNo = code;
×
2424
    pTrans->code = code;
×
2425
    mInfo(
×
2426
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2427
        "continueExec:%d, code:%s",
2428
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2429
    taosMsleep(100);
×
2430
    return true;
×
2431
  } else {
2432
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
64,504,629✔
2433
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
22,763,375✔
2434
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2435
      return false;
22,763,375✔
2436
    }
2437
  }
2438
  terrno = code;
41,741,254✔
2439

2440
  if (code == 0) {
41,741,254✔
2441
    pTrans->code = 0;
18,559,539✔
2442
    pTrans->stage = TRN_STAGE_COMMIT;
18,559,539✔
2443
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
18,559,539✔
2444
    continueExec = true;
18,559,539✔
2445
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
23,181,715✔
2446
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
23,180,107✔
2447
    continueExec = false;
23,180,107✔
2448
  } else {
2449
    pTrans->failedTimes++;
1,608✔
2450
    pTrans->code = terrno;
1,608✔
2451
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
1,608✔
2452
      if (pTrans->lastAction != 0) {
1,608✔
2453
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
1,520✔
2454
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
1,520✔
2455
          if (pTrans->failedTimes < 6) {
×
2456
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2457
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
2458
            taosMsleep(1000);
×
2459
            continueExec = true;
×
2460
            return true;
×
2461
          }
2462
        }
2463
      }
2464

2465
      pTrans->stage = TRN_STAGE_ROLLBACK;
1,608✔
2466
      pTrans->actionPos = 0;
1,608✔
2467
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
1,608✔
2468
             pTrans->actionPos);
2469
      continueExec = true;
1,608✔
2470
    } else {
2471
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2472
      continueExec = false;
×
2473
    }
2474
  }
2475

2476
  return continueExec;
41,741,254✔
2477
}
2478

2479
// execute in trans context
2480
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
18,559,627✔
2481
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
18,559,627✔
2482

2483
  bool    continueExec = true;
18,559,539✔
2484
  int32_t code = mndTransCommit(pMnode, pTrans);
18,559,539✔
2485

2486
  if (code == 0) {
18,559,539✔
2487
    pTrans->code = 0;
18,557,741✔
2488
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
18,557,741✔
2489
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
18,557,741✔
2490
    continueExec = true;
18,557,741✔
2491
  } else {
2492
    pTrans->code = terrno;
1,798✔
2493
    pTrans->failedTimes++;
1,798✔
2494
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
1,798✔
2495
    continueExec = false;
1,798✔
2496
  }
2497

2498
  return continueExec;
18,559,539✔
2499
}
2500

2501
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
38,591,438✔
2502
  bool    continueExec = true;
38,591,438✔
2503
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
38,591,438✔
2504

2505
  if (code == 0) {
38,591,438✔
2506
    pTrans->code = 0;
38,591,350✔
2507
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
38,591,350✔
2508
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
38,591,350✔
2509
    continueExec = true;
38,591,350✔
2510
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
88✔
2511
    pTrans->code = 0;
88✔
2512
    pTrans->stage = TRN_STAGE_COMMIT;
88✔
2513
    mInfo("trans:%d, back to commit stage", pTrans->id);
88✔
2514
    continueExec = true;
88✔
2515
  } else {
2516
    pTrans->code = terrno;
×
2517
    pTrans->failedTimes++;
×
2518
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2519
    continueExec = false;
×
2520
  }
2521

2522
  return continueExec;
38,591,438✔
2523
}
2524

2525
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
11,424✔
2526
  bool    continueExec = true;
11,424✔
2527
  int32_t code = 0;
11,424✔
2528

2529
  if (pTrans->exec == TRN_EXEC_SERIAL) {
11,424✔
2530
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
2531
  } else {
2532
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
11,424✔
2533
  }
2534

2535
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
11,424✔
2536
  terrno = code;
9,555✔
2537

2538
  if (code == 0) {
9,555✔
2539
    pTrans->stage = TRN_STAGE_PRE_FINISH;
1,607✔
2540
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
1,607✔
2541
    continueExec = true;
1,607✔
2542
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
7,948✔
2543
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
7,948✔
2544
    continueExec = false;
7,948✔
2545
  } else {
2546
    pTrans->failedTimes++;
×
2547
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2548
    continueExec = false;
×
2549
  }
2550

2551
  return continueExec;
9,555✔
2552
}
2553

2554
// in trans context
2555
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
1,608✔
2556
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
1,608✔
2557

2558
  bool    continueExec = true;
1,608✔
2559
  int32_t code = mndTransRollback(pMnode, pTrans);
1,608✔
2560

2561
  if (code == 0) {
1,608✔
2562
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1,520✔
2563
    continueExec = true;
1,520✔
2564
  } else {
2565
    pTrans->failedTimes++;
88✔
2566
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
88✔
2567
    continueExec = false;
88✔
2568
  }
2569

2570
  return continueExec;
1,608✔
2571
}
2572

2573
// excute in trans context
2574
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
1,720✔
2575
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
1,720✔
2576

2577
  bool    continueExec = true;
1,720✔
2578
  int32_t code = mndTransPreFinish(pMnode, pTrans);
1,720✔
2579

2580
  if (code == 0) {
1,720✔
2581
    pTrans->stage = TRN_STAGE_FINISH;
1,720✔
2582
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
1,720✔
2583
    continueExec = true;
1,720✔
2584
  } else {
2585
    pTrans->failedTimes++;
×
2586
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2587
    continueExec = false;
×
2588
  }
2589

2590
  return continueExec;
1,720✔
2591
}
2592

2593
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
38,594,965✔
2594
  bool continueExec = false;
38,594,965✔
2595
  if (topHalf) return continueExec;
38,594,965✔
2596

2597
  SSdbRaw *pRaw = mndTransEncode(pTrans);
20,035,504✔
2598
  if (pRaw == NULL) {
20,035,504✔
2599
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
2600
    return false;
×
2601
  }
2602
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
20,035,504✔
2603

2604
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
20,035,504✔
2605
  if (code != 0) {
20,035,504✔
2606
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
2607
  }
2608

2609
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
20,035,504✔
2610
        pTrans->failedTimes, pTrans->createdTime);
2611
  return continueExec;
20,035,504✔
2612
}
2613

2614
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
84,550,238✔
2615
  bool continueExec = true;
84,550,238✔
2616

2617
  while (continueExec) {
244,815,649✔
2618
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
160,265,411✔
2619
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2620
    pTrans->lastExecTime = taosGetTimestampMs();
160,265,411✔
2621
    switch (pTrans->stage) {
160,265,411✔
2622
      case TRN_STAGE_PREPARE:
×
2623
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2624
        break;
×
2625
      case TRN_STAGE_REDO_ACTION:
64,504,629✔
2626
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
64,504,629✔
2627
        break;
64,504,629✔
2628
      case TRN_STAGE_COMMIT:
18,559,627✔
2629
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
18,559,627✔
2630
        break;
18,559,627✔
2631
      case TRN_STAGE_COMMIT_ACTION:
38,591,438✔
2632
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
38,591,438✔
2633
        break;
38,591,438✔
2634
      case TRN_STAGE_ROLLBACK:
1,608✔
2635
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,608✔
2636
        break;
1,608✔
2637
      case TRN_STAGE_UNDO_ACTION:
11,424✔
2638
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
11,424✔
2639
        break;
11,424✔
2640
      case TRN_STAGE_PRE_FINISH:
1,720✔
2641
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,720✔
2642
        break;
1,720✔
2643
      case TRN_STAGE_FINISH:
38,594,965✔
2644
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
38,594,965✔
2645
        break;
38,594,965✔
2646
      default:
×
2647
        continueExec = false;
×
2648
        break;
×
2649
    }
2650
  }
2651

2652
  mndTransSendRpcRsp(pMnode, pTrans);
84,550,238✔
2653
}
84,550,238✔
2654

2655
// start trans, pullup, receive rsp, kill
2656
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
41,768,041✔
2657
  bool topHalf = true;
41,768,041✔
2658
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
41,768,041✔
2659
}
41,768,041✔
2660

2661
// update trans
2662
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
42,782,197✔
2663
  bool topHalf = false;
42,782,197✔
2664
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
42,782,197✔
2665
}
42,782,197✔
2666

2667
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
12,317,246✔
2668
  mTrace("start to process trans timer");
12,317,246✔
2669
  mndTransPullup(pReq->info.node);
12,317,246✔
2670
  return 0;
12,317,246✔
2671
}
2672

2673
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
113✔
2674
  SArray *pArray = NULL;
113✔
2675
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
113✔
2676
    pArray = pTrans->redoActions;
113✔
2677
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2678
    pArray = pTrans->undoActions;
×
2679
  } else {
2680
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2681
  }
2682

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

2706
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
113✔
2707
  mndTransExecute(pMnode, pTrans, true);
113✔
2708
  return 0;
113✔
2709
}
2710

2711
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
254✔
2712
  SMnode       *pMnode = pReq->info.node;
254✔
2713
  SKillTransReq killReq = {0};
254✔
2714
  int32_t       code = -1;
254✔
2715
  STrans       *pTrans = NULL;
254✔
2716

2717
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
254✔
2718
    code = TSDB_CODE_INVALID_MSG;
×
2719
    goto _OVER;
×
2720
  }
2721

2722
  mInfo("trans:%d, start to kill, force:%d", killReq.transId, tsForceKillTrans);
254✔
2723
  if ((code = mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_KILL_TRANS)) != 0) {
254✔
2724
    goto _OVER;
×
2725
  }
2726

2727
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
254✔
2728
  if (pTrans == NULL) {
254✔
2729
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
141✔
2730
    if (terrno != 0) code = terrno;
141✔
2731
    goto _OVER;
141✔
2732
  }
2733

2734
  code = mndKillTrans(pMnode, pTrans);
113✔
2735

2736
_OVER:
254✔
2737
  if (code != 0) {
254✔
2738
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
141✔
2739
  }
2740

2741
  mndReleaseTrans(pMnode, pTrans);
254✔
2742
  TAOS_RETURN(code);
254✔
2743
}
2744

2745
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
50,113✔
2746

2747
void mndTransPullup(SMnode *pMnode) {
12,443,896✔
2748
  SSdb   *pSdb = pMnode->pSdb;
12,443,896✔
2749
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
12,443,896✔
2750
  if (pArray == NULL) return;
12,443,896✔
2751

2752
  void *pIter = NULL;
12,443,896✔
2753
  while (1) {
1,606,110✔
2754
    STrans *pTrans = NULL;
14,050,006✔
2755
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
14,050,006✔
2756
    if (pIter == NULL) break;
14,050,006✔
2757
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
3,212,220✔
2758
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2759
    }
2760
    sdbRelease(pSdb, pTrans);
1,606,110✔
2761
  }
2762

2763
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
12,443,896✔
2764

2765
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
14,050,006✔
2766
    int32_t *pTransId = taosArrayGet(pArray, i);
1,606,110✔
2767
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
1,606,110✔
2768
    if (pTrans != NULL) {
1,606,110✔
2769
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
1,606,110✔
2770
      mndTransExecute(pMnode, pTrans, false);
1,606,110✔
2771
    }
2772
    mndReleaseTrans(pMnode, pTrans);
1,606,110✔
2773
  }
2774
  taosArrayDestroy(pArray);
12,443,896✔
2775
}
2776

2777
static char *formatTimestamp(char *buf, int32_t cap, int64_t val, int precision) {
17,383,474✔
2778
  time_t tt;
17,383,474✔
2779
  if (precision == TSDB_TIME_PRECISION_MICRO) {
17,383,474✔
2780
    tt = (time_t)(val / 1000000);
×
2781
  }
2782
  if (precision == TSDB_TIME_PRECISION_NANO) {
17,383,474✔
2783
    tt = (time_t)(val / 1000000000);
×
2784
  } else {
2785
    tt = (time_t)(val / 1000);
17,383,474✔
2786
  }
2787

2788
  struct tm tm;
17,383,474✔
2789
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
17,383,474✔
2790
    mError("failed to get local time");
×
2791
    return NULL;
×
2792
  }
2793
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
17,383,474✔
2794

2795
  if (precision == TSDB_TIME_PRECISION_MICRO) {
17,383,474✔
2796
    snprintf(buf + pos, cap - (pos), ".%06d", (int)(val % 1000000));
×
2797
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
17,383,474✔
2798
    snprintf(buf + pos, cap - (pos), ".%09d", (int)(val % 1000000000));
×
2799
  } else {
2800
    snprintf(buf + pos, cap - (pos), ".%03d", (int)(val % 1000));
17,383,474✔
2801
  }
2802

2803
  return buf;
17,383,474✔
2804
}
2805

2806
static void mndTransLogAction(STrans *pTrans) {
362,653✔
2807
  char    detail[512] = {0};
362,653✔
2808
  int32_t len = 0;
362,653✔
2809
  int32_t index = 0;
362,653✔
2810

2811
  if (pTrans->stage == TRN_STAGE_PREPARE) {
362,653✔
2812
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2813
      len = 0;
×
2814
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2815
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2816
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2817
                      sdbStatusName(pAction->pRaw->status));
×
2818
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2819
    }
2820
  }
2821

2822
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
362,653✔
2823
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
11,784,744✔
2824
      len = 0;
11,422,091✔
2825
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
11,422,091✔
2826
      if (pAction->actionType == TRANS_ACTION_MSG) {
11,422,091✔
2827
        char bufStart[40] = {0};
8,680,399✔
2828
        (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
8,680,399✔
2829

2830
        char endStart[40] = {0};
8,680,399✔
2831
        (void)formatTimestamp(endStart, sizeof(endStart), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
8,680,399✔
2832
        len += snprintf(detail + len, sizeof(detail) - len,
17,360,798✔
2833
                        "action:%d, %s:%d msgType:%s,"
2834
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2835
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
17,360,798✔
2836
                        pAction->msgReceived, bufStart, endStart);
8,680,399✔
2837

2838
        SEpSet epset = pAction->epSet;
8,680,399✔
2839
        if (epset.numOfEps > 0) {
8,680,399✔
2840
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
8,680,399✔
2841
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
19,470,354✔
2842
            len +=
10,789,955✔
2843
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
10,789,955✔
2844
          }
2845
        }
2846

2847
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
8,680,399✔
2848
                        tstrerror(pAction->errCode));
2849
      } else {
2850
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
2,741,692✔
2851
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
2,741,692✔
2852
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
2,741,692✔
2853
      }
2854
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
11,422,091✔
2855
    }
2856
  }
2857

2858
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
362,653✔
2859
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
×
2860
      len = 0;
×
2861
      STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
×
2862
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2863
                      mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type),
×
2864
                      sdbStatusName(pAction->pRaw->status));
×
2865
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2866
    }
2867

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

2885
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
416,657✔
2886
  SMnode *pMnode = pReq->info.node;
416,657✔
2887
  SSdb   *pSdb = pMnode->pSdb;
416,657✔
2888
  int32_t numOfRows = 0;
416,657✔
2889
  STrans *pTrans = NULL;
416,657✔
2890
  int32_t cols = 0;
416,657✔
2891
  int32_t code = 0;
416,657✔
2892
  int32_t lino = 0;
416,657✔
2893

2894
  while (numOfRows < rows) {
779,310✔
2895
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
779,310✔
2896
    if (pShow->pIter == NULL) break;
779,310✔
2897

2898
    cols = 0;
362,653✔
2899

2900
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
362,653✔
2901
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
362,653✔
2902

2903
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
362,653✔
2904
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
362,653✔
2905
                        _OVER);
2906

2907
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
362,653✔
2908
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
362,653✔
2909
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
362,653✔
2910
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
362,653✔
2911

2912
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
362,653✔
2913
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
362,653✔
2914
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
362,653✔
2915
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
362,653✔
2916

2917
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
362,653✔
2918
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
362,653✔
2919
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
362,653✔
2920
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
362,653✔
2921

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

2927
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
362,653✔
2928
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
362,653✔
2929
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
362,653✔
2930
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
362,653✔
2931
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
362,653✔
2932

2933
    /*
2934
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2935
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2936
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2937
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2938
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2939
    */
2940

2941
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
362,653✔
2942
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
362,653✔
2943
                        _OVER);
2944

2945
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
362,653✔
2946
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
362,653✔
2947
                        _OVER);
2948

2949
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
362,653✔
2950
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
362,653✔
2951
    int32_t len = snprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
725,306✔
2952
                           pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
725,306✔
2953
    SEpSet  epset = pTrans->lastEpset;
362,653✔
2954
    if (epset.numOfEps > 0) {
362,653✔
2955
      len += snprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
718,604✔
2956
                      TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
718,604✔
2957
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
998,964✔
2958
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
639,662✔
2959
      }
2960
    }
2961
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
362,653✔
2962
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
362,653✔
2963
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
362,653✔
2964

2965
    mndTransLogAction(pTrans);
362,653✔
2966

2967
    numOfRows++;
362,653✔
2968
    sdbRelease(pSdb, pTrans);
362,653✔
2969
  }
2970

2971
_OVER:
416,657✔
2972
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
416,657✔
2973
  pShow->numOfRows += numOfRows;
416,657✔
2974
  return numOfRows;
416,657✔
2975
}
2976

2977
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
63,195✔
2978
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2979
  int32_t code = 0;
63,195✔
2980
  int32_t lino = 0;
63,195✔
2981
  int32_t len = 0;
63,195✔
2982

2983
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
63,195✔
2984
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&transactionId, false), &lino, _OVER);
63,195✔
2985

2986
  char action[30 + 1] = {0};
63,195✔
2987
  if (curActionId == pAction->id) {
63,195✔
2988
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
465✔
2989
                    mndTransTypeStr(pAction->actionType));
2990
  } else {
2991
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
62,730✔
2992
                    mndTransTypeStr(pAction->actionType));
2993
  }
2994
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
63,195✔
2995
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
63,195✔
2996
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
63,195✔
2997
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
63,195✔
2998
_OVER:
63,195✔
2999
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
63,195✔
3000
  return code;
63,195✔
3001
}
3002

3003
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
63,195✔
3004
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
3005
  int32_t code = 0;
63,195✔
3006
  int32_t lino = 0;
63,195✔
3007
  int32_t len = 0;
63,195✔
3008
  int32_t cols = 0;
63,195✔
3009

3010
  cols = 0;
63,195✔
3011

3012
  if (mndShowTransCommonColumns(pShow, pBlock, pAction, transactionId, curActionId, numOfRows, &cols) != 0) return;
63,195✔
3013

3014
  if (pAction->actionType == TRANS_ACTION_MSG) {
63,195✔
3015
    int32_t len = 0;
51,931✔
3016

3017
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
51,931✔
3018
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
51,931✔
3019
                    pAction->msgSent, pAction->msgReceived);
51,931✔
3020
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
51,931✔
3021
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
51,931✔
3022
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
51,931✔
3023
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
51,931✔
3024

3025
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
51,931✔
3026
    len = 0;
51,931✔
3027
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
51,931✔
3028
                    tstrerror(pAction->errCode));
3029
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
51,931✔
3030
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
51,931✔
3031
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
51,931✔
3032
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
51,931✔
3033

3034
    char target[TSDB_TRANS_TARGET_LEN] = {0};
51,931✔
3035
    len = 0;
51,931✔
3036
    SEpSet epset = pAction->epSet;
51,931✔
3037
    if (epset.numOfEps > 0) {
51,931✔
3038
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
120,758✔
3039
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
68,827✔
3040
      }
3041
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
51,931✔
3042
    }
3043
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
51,931✔
3044
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
51,931✔
3045
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
51,931✔
3046
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
51,931✔
3047

3048
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
51,931✔
3049
    len = 0;
51,931✔
3050
    char bufStart[40] = {0};
51,931✔
3051
    if (pAction->startTime > 0)
51,931✔
3052
      (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
13,802✔
3053
    char bufEnd[40] = {0};
51,931✔
3054
    if (pAction->endTime > 0)
51,931✔
3055
      (void)formatTimestamp(bufEnd, sizeof(bufEnd), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
8,874✔
3056

3057
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
51,931✔
3058
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
51,931✔
3059
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
51,931✔
3060
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
51,931✔
3061
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
51,931✔
3062

3063
  } else {
3064
    int32_t len = 0;
11,264✔
3065

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

3080
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
11,264✔
3081
    len = 0;
11,264✔
3082
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
11,264✔
3083
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
11,264✔
3084
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
11,264✔
3085
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
11,264✔
3086
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
11,264✔
3087

3088
    char target[TSDB_TRANS_TARGET_LEN] = "";
11,264✔
3089
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
11,264✔
3090
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
11,264✔
3091
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
11,264✔
3092
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
11,264✔
3093

3094
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
11,264✔
3095
    len = 0;
11,264✔
3096
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
11,264✔
3097
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
11,264✔
3098
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
11,264✔
3099
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
11,264✔
3100
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
11,264✔
3101
  }
3102

3103
_OVER:
63,195✔
3104
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
63,195✔
3105
}
3106

3107
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
817✔
3108
  if (stage == TRN_STAGE_PREPARE) {
817✔
3109
    return pTrans->prepareActions;
×
3110
  }
3111
  if (stage == TRN_STAGE_REDO_ACTION) {
817✔
3112
    return pTrans->redoActions;
817✔
3113
  }
3114
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
3115
    return pTrans->commitActions;
×
3116
  }
3117
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
3118
    return pTrans->undoActions;
×
3119
  }
3120
  return NULL;
×
3121
}
3122

3123
typedef struct STransDetailIter {
3124
  void     *pIter;
3125
  STrans   *pTrans;
3126
  ETrnStage stage;
3127
  int32_t   num;
3128
} STransDetailIter;
3129

3130
static void mndTransShowActions(SSdb *pSdb, STransDetailIter *pShowIter, SShowObj *pShow, SSDataBlock *pBlock,
817✔
3131
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3132
  int32_t actionNum = taosArrayGetSize(pActions);
817✔
3133
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
817✔
3134

3135
  for (int32_t i = start; i < actionNum; ++i) {
63,660✔
3136
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
63,195✔
3137
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
63,195✔
3138
    (*numOfRows)++;
63,195✔
3139
    if (*numOfRows >= rows) break;
63,195✔
3140
  }
3141

3142
  if (*numOfRows == end) {
817✔
3143
    sdbRelease(pSdb, pShowIter->pTrans);
465✔
3144
    pShowIter->pTrans = NULL;
465✔
3145
    pShowIter->num = 0;
465✔
3146
  } else {
3147
    pShowIter->pTrans = pShowIter->pTrans;
352✔
3148
    pShowIter->stage = pShowIter->pTrans->stage;
352✔
3149
    pShowIter->num += (*numOfRows);
352✔
3150
  }
3151
}
817✔
3152

3153
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
1,282✔
3154
  SMnode *pMnode = pReq->info.node;
1,282✔
3155
  SSdb   *pSdb = pMnode->pSdb;
1,282✔
3156
  int32_t numOfRows = 0;
1,282✔
3157

3158
  int32_t code = 0;
1,282✔
3159
  int32_t lino = 0;
1,282✔
3160

3161
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
1,282✔
3162
        pShow->pIter);
3163

3164
  if (pShow->pIter == NULL) {
1,282✔
3165
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
465✔
3166
    if (pShow->pIter == NULL) {
465✔
3167
      mError("failed to malloc for pShow->pIter");
×
3168
      return 0;
×
3169
    }
3170
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
465✔
3171
  }
3172

3173
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
1,282✔
3174

3175
  while (numOfRows < rows) {
1,282✔
3176
    if (pShowIter->pTrans == NULL) {
1,282✔
3177
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
930✔
3178
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
930✔
3179
      if (pShowIter->pIter == NULL) break;
930✔
3180
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
465✔
3181
            pShowIter->pTrans->stage, pShowIter->num);
3182

3183
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
465✔
3184

3185
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
465✔
3186
      break;
465✔
3187
    } else {
3188
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
352✔
3189
            pShowIter->num);
3190
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
352✔
3191

3192
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
352✔
3193
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
352✔
3194
      break;
352✔
3195
    }
3196
  }
3197

3198
_OVER:
×
3199
  pShow->numOfRows += numOfRows;
1,282✔
3200

3201
  if (code != 0) {
1,282✔
3202
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
3203
  } else {
3204
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
1,282✔
3205
  }
3206
  if (numOfRows == 0) {
1,282✔
3207
    taosMemoryFree(pShow->pIter);
465✔
3208
    pShow->pIter = NULL;
465✔
3209
  }
3210
  return numOfRows;
1,282✔
3211
}
3212

3213
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
3214
  SSdb *pSdb = pMnode->pSdb;
×
3215
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
3216
}
×
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