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

taosdata / TDengine / #5028

20 Apr 2026 09:07AM UTC coverage: 72.986% (-0.01%) from 72.996%
#5028

push

travis-ci

web-flow
perf: optimize compact progress query from O(m*n) to O(n+m) (#35115)

104 of 141 new or added lines in 4 files covered. (73.76%)

5170 existing lines in 133 files now uncovered.

273777 of 375111 relevant lines covered (72.99%)

130458474.51 hits per line

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

84.84
/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; }
135,309,451✔
64

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

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

81
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
192,253,156✔
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) {
472,033✔
94
  SSdbTable table = {
472,033✔
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);
472,033✔
105
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
472,033✔
106

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

113
void mndCleanupTrans(SMnode *pMnode) {}
471,971✔
114

115
static int32_t mndTransGetActionsSize(SArray *pArray) {
250,478,056✔
116
  int32_t actionNum = taosArrayGetSize(pArray);
250,478,056✔
117
  int32_t rawDataLen = 0;
250,478,056✔
118

119
  for (int32_t i = 0; i < actionNum; ++i) {
671,009,223✔
120
    STransAction *pAction = taosArrayGet(pArray, i);
420,531,167✔
121
    if (pAction->actionType == TRANS_ACTION_RAW) {
420,531,167✔
122
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
284,849,318✔
123
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
135,681,849✔
124
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
135,681,849✔
125
    } else {
126
      // empty
127
    }
128
    rawDataLen += sizeof(int8_t);
420,531,167✔
129
  }
130

131
  return rawDataLen;
250,478,056✔
132
}
133

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

142
  for (int32_t i = 0; i < actionsNum; ++i) {
671,009,223✔
143
    STransAction *pAction = taosArrayGet(pActions, i);
420,531,167✔
144
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
420,531,167✔
145
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
420,531,167✔
146
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
420,531,167✔
147
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
420,531,167✔
148
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
420,531,167✔
149
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
420,531,167✔
150
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
420,531,167✔
151
    if (sver > TRANS_VER2_NUMBER) {
420,531,167✔
152
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
420,531,167✔
153
    }
154
    if (pAction->actionType == TRANS_ACTION_RAW) {
420,531,167✔
155
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
284,849,318✔
156
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
284,849,318✔
157
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
284,849,318✔
158
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
284,849,318✔
159
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
135,681,849✔
160
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
135,681,849✔
161
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
135,681,849✔
162
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
135,681,849✔
163
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
135,681,849✔
164
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
135,681,849✔
165
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
135,681,849✔
166
    } else {
167
      // nothing
168
    }
169
  }
170
  ret = 0;
250,478,056✔
171

172
_OVER:
250,478,056✔
173
  *offset = dataPos;
250,478,056✔
174
  return ret;
250,478,056✔
175
}
176

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

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

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

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

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

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

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

226
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
62,619,514✔
227
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
62,619,514✔
228
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
62,619,514✔
229
  if (pTrans->param != NULL) {
62,619,514✔
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)
62,619,514✔
234

235
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
62,619,514✔
236
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
62,619,514✔
237
  void *pIter = NULL;
62,619,514✔
238
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
62,619,514✔
239
  while (pIter) {
62,658,757✔
240
    int32_t arbGroupId = *(int32_t *)pIter;
39,243✔
241
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
39,243✔
242
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
39,243✔
243
  }
244

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

250
  if (sver > TRANS_VER2_NUMBER) {
62,619,514✔
251
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
62,619,514✔
252
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
62,619,514✔
253
    void *pIter = NULL;
62,619,514✔
254
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
62,619,514✔
255
    while (pIter) {
65,108,370✔
256
      size_t   keysize = 0;
2,488,856✔
257
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
2,488,856✔
258
      int32_t  groupPos = *(int32_t *)pIter;
2,488,856✔
259
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
2,488,856✔
260
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
2,488,856✔
261
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
2,488,856✔
262
    }
263
  }
264

265
  if (sver >= TRANS_VER_USER_DATA) {
62,619,514✔
266
    SDB_SET_INT32(pRaw, dataPos, pTrans->userDataLen, _OVER)
62,619,514✔
267
    if (pTrans->userDataLen > 0) {
62,619,514✔
268
      SDB_SET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
108,252✔
269
    }
270
  }
271

272
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
62,619,514✔
273
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
62,619,514✔
274

275
  terrno = 0;
62,619,514✔
276

277
_OVER:
62,619,514✔
278
  if (terrno != 0) {
62,619,514✔
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);
62,619,514✔
286
  return pRaw;
62,619,514✔
287
}
288

289
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
29,617,093✔
290
  if (pAction->groupId < 0) return 0;
29,617,093✔
291
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
29,054,647✔
292
  if (redoAction == NULL) {
29,054,647✔
293
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
4,945,795✔
294
    if (array != NULL) {
4,945,795✔
295
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
4,945,795✔
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,945,795✔
301
  }
302
  if (redoAction != NULL) {
29,054,647✔
303
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
58,109,294✔
304
  }
305
  return 0;
29,054,647✔
306
}
307

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

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

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

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

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

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

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

449
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
111,019,883✔
450

451
  if (sver > TRANS_VER_CURRENT) {
111,019,883✔
452
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
453
    goto _OVER;
×
454
  }
455

456
  pRow = sdbAllocRow(sizeof(STrans));
111,019,883✔
457
  if (pRow == NULL) goto _OVER;
111,019,883✔
458

459
  pTrans = sdbGetRowObj(pRow);
111,019,883✔
460
  if (pTrans == NULL) goto _OVER;
111,019,883✔
461

462
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
111,019,883✔
463

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

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

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

504
  if (pTrans->prepareActions == NULL) goto _OVER;
111,019,883✔
505
  if (pTrans->redoActions == NULL) goto _OVER;
111,019,883✔
506
  if (pTrans->undoActions == NULL) goto _OVER;
111,019,883✔
507
  if (pTrans->commitActions == NULL) goto _OVER;
111,019,883✔
508

509
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
111,019,883✔
510
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
111,019,883✔
511
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
1,662,918✔
512
                                      sver) < 0)
513
      goto _OVER;
×
514
  } else {
515
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
109,356,965✔
516
  }
517
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
111,019,883✔
518
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
111,019,883✔
519

520
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
111,019,883✔
521
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
111,019,883✔
522
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
111,019,883✔
523
  if (pTrans->paramLen != 0) {
111,019,883✔
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);
111,019,883✔
530

531
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
111,019,883✔
532
  if (arbgroupIdNum > 0) {
111,019,883✔
533
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
53,765✔
534
    if (pTrans->arbGroupIds == NULL) goto _OVER;
53,765✔
535
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
119,170✔
536
      int32_t arbGroupId = 0;
65,405✔
537
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
65,405✔
538
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
65,405✔
539
    }
540
  }
541

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

551
  if (sver > TRANS_VER2_NUMBER) {
111,019,883✔
552
    int32_t groupNum = -1;
111,019,883✔
553
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
111,019,883✔
554
    for (int32_t i = 0; i < groupNum; ++i) {
115,965,678✔
555
      int32_t groupId = -1;
4,945,795✔
556
      int32_t groupPos = -1;
4,945,795✔
557
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
4,945,795✔
558
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
4,945,795✔
559
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
4,945,795✔
560
        goto _OVER;
×
561
    }
562
  }
563

564
  if (sver >= TRANS_VER_USER_DATA) {
111,019,883✔
565
    SDB_GET_INT32(pRaw, dataPos, &pTrans->userDataLen, _OVER)
111,019,883✔
566
    if (pTrans->userDataLen > 0) {
111,019,883✔
567
      pTrans->userData = taosMemoryMalloc(pTrans->userDataLen);
180,420✔
568
      if (pTrans->userData == NULL) goto _OVER;
180,420✔
569
      SDB_GET_BINARY(pRaw, dataPos, pTrans->userData, pTrans->userDataLen, _OVER)
180,420✔
570
    }
571
  }
572

573
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
111,019,883✔
574

575
  terrno = 0;
111,019,883✔
576

577
_OVER:
111,019,883✔
578
  if (terrno != 0 && pTrans != NULL) {
111,019,883✔
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) {
111,019,883✔
587
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
111,019,883✔
588
  }
589
  return pRow;
111,019,883✔
590
}
591

592
static const char *mndTransStr(ETrnStage stage) {
780,069,278✔
593
  switch (stage) {
780,069,278✔
594
    case TRN_STAGE_PREPARE:
63,063,136✔
595
      return "prepare";
63,063,136✔
596
    case TRN_STAGE_REDO_ACTION:
267,624,678✔
597
      return "redoAction";
267,624,678✔
598
    case TRN_STAGE_ROLLBACK:
8,969✔
599
      return "rollback";
8,969✔
600
    case TRN_STAGE_UNDO_ACTION:
12,178,911✔
601
      return "undoAction";
12,178,911✔
602
    case TRN_STAGE_COMMIT:
100,429,237✔
603
      return "commit";
100,429,237✔
604
    case TRN_STAGE_COMMIT_ACTION:
187,803,249✔
605
      return "commitAction";
187,803,249✔
606
    case TRN_STAGE_FINISH:
148,951,929✔
607
      return "finished";
148,951,929✔
608
    case TRN_STAGE_PRE_FINISH:
9,169✔
609
      return "pre-finish";
9,169✔
610
    default:
×
611
      return "invalid";
×
612
  }
613
}
614

615
static const char *mndTransTypeStr(ETrnAct actionType) {
66,693✔
616
  switch (actionType) {
66,693✔
617
    case TRANS_ACTION_MSG:
54,789✔
618
      return "msg";
54,789✔
619
    case TRANS_ACTION_RAW:
11,904✔
620
      return "sdb";
11,904✔
621
    default:
×
622
      return "invalid";
×
623
  }
624
}
625

626
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
217,819,724✔
627
  if (pAction != NULL) {
217,819,724✔
628
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
167,221,819✔
629
      pTrans->lastAction = pAction->id;
118,759,086✔
630
      pTrans->lastMsgType = pAction->msgType;
118,759,086✔
631
      pTrans->lastEpset = pAction->epSet;
118,759,086✔
632
      pTrans->lastErrorNo = pAction->errCode;
118,759,086✔
633
    }
634
  } else {
635
    pTrans->lastAction = 0;
50,597,905✔
636
    pTrans->lastMsgType = 0;
50,597,905✔
637
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
50,597,905✔
638
    pTrans->lastErrorNo = 0;
50,597,905✔
639
  }
640
}
217,819,724✔
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) {
613,576✔
651
  switch (ftype) {
613,576✔
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:
306,788✔
657
      return mndRebCntInc;
306,788✔
658
    case TRANS_STOP_FUNC_MQ_REB:
306,788✔
659
      return mndRebCntDec;
306,788✔
660
    default:
×
661
      return NULL;
×
662
  }
663
}
664

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

669
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
21,112,767✔
670

671
  if (pTrans->startFunc > 0) {
21,112,767✔
672
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
306,788✔
673
    if (fp) {
306,788✔
674
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
306,788✔
675
    }
676
    // pTrans->startFunc = 0;
677
  }
678

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

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

689
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
21,112,767✔
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;
21,112,767✔
695
}
696

697
void mndTransDropData(STrans *pTrans) {
130,721,490✔
698
  if (pTrans->prepareActions != NULL) {
130,721,490✔
699
    mndTransDropActions(pTrans->prepareActions);
130,721,490✔
700
    pTrans->prepareActions = NULL;
130,721,490✔
701
  }
702
  if (pTrans->redoActions != NULL) {
130,721,490✔
703
    mndTransDropActions(pTrans->redoActions);
130,721,490✔
704
    pTrans->redoActions = NULL;
130,721,490✔
705
  }
706
  if (pTrans->undoActions != NULL) {
130,721,490✔
707
    mndTransDropActions(pTrans->undoActions);
130,721,490✔
708
    pTrans->undoActions = NULL;
130,721,490✔
709
  }
710
  if (pTrans->commitActions != NULL) {
130,721,490✔
711
    mndTransDropActions(pTrans->commitActions);
130,721,490✔
712
    pTrans->commitActions = NULL;
130,721,490✔
713
  }
714
  if (pTrans->redoGroupActions != NULL) {
130,721,490✔
715
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
21,364,525✔
716
    while (pIter) {
26,552,541✔
717
      SArray **redoActions = pIter;
5,188,016✔
718
      taosArrayDestroy(*redoActions);
5,188,016✔
719
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
5,188,016✔
720
    }
721

722
    taosHashCleanup(pTrans->redoGroupActions);
21,364,525✔
723
    pTrans->redoGroupActions = NULL;
21,364,525✔
724
  }
725
  if (pTrans->groupActionPos != NULL) {
130,721,490✔
726
    taosHashCleanup(pTrans->groupActionPos);
21,364,525✔
727
    pTrans->groupActionPos = NULL;
21,364,525✔
728
  }
729
  if (pTrans->arbGroupIds != NULL) {
130,721,490✔
730
    taosHashCleanup(pTrans->arbGroupIds);
19,755,372✔
731
  }
732
  if (pTrans->pRpcArray != NULL) {
130,721,490✔
733
    taosArrayDestroy(pTrans->pRpcArray);
19,701,607✔
734
    pTrans->pRpcArray = NULL;
19,701,607✔
735
  }
736
  if (pTrans->rpcRsp != NULL) {
130,721,490✔
737
    taosMemoryFree(pTrans->rpcRsp);
7,769,993✔
738
    pTrans->rpcRsp = NULL;
7,769,993✔
739
    pTrans->rpcRspLen = 0;
7,769,993✔
740
  }
741
  if (pTrans->param != NULL) {
130,721,490✔
742
    taosMemoryFree(pTrans->param);
×
743
    pTrans->param = NULL;
×
744
    pTrans->paramLen = 0;
×
745
  }
746
  if (pTrans->userData != NULL) {
130,721,490✔
747
    taosMemoryFree(pTrans->userData);
180,539✔
748
    pTrans->userData = NULL;
180,539✔
749
    pTrans->userDataLen = 0;
180,539✔
750
  }
751
  (void)taosThreadMutexDestroy(&pTrans->mutex);
130,721,490✔
752
}
130,721,490✔
753

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

758
  if (pTrans->stopFunc > 0 && callFunc) {
66,063,209✔
759
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
306,788✔
760
    if (fp) {
306,788✔
761
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
306,788✔
762
    }
763
    // pTrans->stopFunc = 0;
764
  }
765

766
  mndTransDropData(pTrans);
66,063,209✔
767
  return 0;
66,063,209✔
768
}
769

770
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
95,440,988✔
771
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
285,411,584✔
772
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
189,970,596✔
773
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
189,970,596✔
774
    pOldAction->rawWritten = pNewAction->rawWritten;
189,970,596✔
775
    pOldAction->msgSent = pNewAction->msgSent;
189,970,596✔
776
    pOldAction->msgReceived = pNewAction->msgReceived;
189,970,596✔
777
    pOldAction->errCode = pNewAction->errCode;
189,970,596✔
778
  }
779
}
95,440,988✔
780

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

785
  if (pOld->createdTime != pNew->createdTime) {
23,860,247✔
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);
23,860,247✔
795
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
23,860,247✔
796
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
23,860,247✔
797
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
23,860,247✔
798
  pOld->stage = pNew->stage;
23,860,247✔
799
  pOld->actionPos = pNew->actionPos;
23,860,247✔
800
  TSWAP(pOld->userData, pNew->userData);
23,860,247✔
801
  TSWAP(pOld->userDataLen, pNew->userDataLen);
23,860,247✔
802

803
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
23,860,247✔
804
  while (pIter != NULL) {
25,961,666✔
805
    int32_t newActionPos = *(int32_t *)pIter;
2,101,419✔
806

807
    size_t   keylen = 0;
2,101,419✔
808
    int32_t *groupId = taosHashGetKey(pIter, &keylen);
2,101,419✔
809

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

816
    pIter = taosHashIterate(pNew->groupActionPos, pIter);
2,101,419✔
817
  }
818

819
  if (pOld->stage == TRN_STAGE_COMMIT) {
23,860,247✔
820
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
21,087,984✔
821
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
21,087,984✔
822
  }
823

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

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

834
  return 0;
23,860,247✔
835
}
836

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

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

851
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
19,701,607✔
852
                       const char *opername) {
853
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
19,701,607✔
854
  if (pTrans == NULL) {
19,701,607✔
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) {
19,701,607✔
861
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
19,701,607✔
862
  }
863

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

886
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
19,701,607✔
887
      pTrans->pRpcArray == NULL) {
19,701,607✔
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) {
19,701,607✔
895
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
27,359,264✔
896
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
897
      return NULL;
×
898
    }
899
    pTrans->originRpcType = pReq->msgType;
13,679,632✔
900
  }
901

902
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
19,701,607✔
903

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

908
static void mndTransDropActions(SArray *pArray) {
522,885,960✔
909
  int32_t size = taosArrayGetSize(pArray);
522,885,960✔
910
  for (int32_t i = 0; i < size; ++i) {
1,392,978,588✔
911
    STransAction *pAction = taosArrayGet(pArray, i);
870,092,628✔
912
    if (pAction->actionType == TRANS_ACTION_RAW) {
870,092,628✔
913
      taosMemoryFreeClear(pAction->pRaw);
586,427,314✔
914
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
283,665,314✔
915
      taosMemoryFreeClear(pAction->pCont);
283,665,314✔
916
    } else {
917
      // nothing
918
    }
919
  }
920

921
  taosArrayDestroy(pArray);
522,885,960✔
922
}
522,885,960✔
923

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

932
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
115,870,645✔
933
  pAction->id = taosArrayGetSize(pArray);
115,870,645✔
934

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

940
  return 0;
115,870,645✔
941
}
942

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

946
  SArray **redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
558,269✔
947
  if (redoAction == NULL) {
558,269✔
948
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
242,221✔
949
    if (array != NULL) {
242,221✔
950
      if (taosHashPut(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
242,221✔
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));
242,221✔
955
    }
956
  }
957
  if (redoAction != NULL) {
558,269✔
958
    mInfo("trans:%d, append action into group %d, msgType:%s", pTrans->id, pAction->groupId,
558,269✔
959
          TMSG_INFO(pAction->msgType));
960
    void *ptr = taosArrayPush(*redoAction, &pAction);
558,269✔
961
    if (ptr == NULL) {
558,269✔
962
      TAOS_RETURN(terrno);
×
963
    }
964
  }
965

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

975
  return 0;
558,269✔
976
}
977

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

985
int32_t mndTransAppendGroupRedolog(STrans *pTrans, SSdbRaw *pRaw, int32_t groupId) {
304,599✔
986
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION,
304,599✔
987
                         .actionType = TRANS_ACTION_RAW,
988
                         .pRaw = pRaw,
989
                         .mTraceId = pTrans->mTraceId,
304,599✔
990
                         .groupId = groupId};
991
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
304,599✔
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));
304,599✔
996
  return mndTransAppendAction(pTrans->redoActions, &action);
304,599✔
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,344,893✔
1019
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
4,344,893✔
1020
  return mndTransAppendAction(pTrans->undoActions, &action);
4,344,893✔
1021
}
1022

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

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

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

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

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

1057
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
303,781✔
1058
  pTrans->startFunc = startFunc;
303,781✔
1059
  pTrans->stopFunc = stopFunc;
303,781✔
1060
  pTrans->param = param;
303,781✔
1061
  pTrans->paramLen = paramLen;
303,781✔
1062
}
303,781✔
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) {
11,122,758✔
1096
  if (dbname != NULL) {
11,122,758✔
1097
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
11,122,758✔
1098
  }
1099
  if (stbname != NULL) {
11,122,758✔
1100
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
8,449,995✔
1101
  }
1102
}
11,122,758✔
1103

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

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

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

1123
void mndTransSetGroupParallel(STrans *pTrans) {
102,674✔
1124
  mInfo("trans:%d, set Group Parallel", pTrans->id);
102,674✔
1125
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
102,674✔
1126
}
102,674✔
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) {
41,218✔
1136
  pTrans->ableToBeKilled = true; 
41,218✔
1137
  pTrans->killMode = killMode; 
41,218✔
1138
}
41,218✔
1139

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

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

1144
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
41,469,493✔
1145
  int32_t  code = 0;
41,469,493✔
1146
  SSdbRaw *pRaw = mndTransEncode(pTrans);
41,469,493✔
1147
  if (pRaw == NULL) {
41,469,493✔
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));
41,469,493✔
1154

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

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

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

1176
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
5,957,391✔
1177
  if (conflict[0] == 0) return false;
5,957,391✔
1178
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
5,793,680✔
1179
  return false;
5,642,997✔
1180
}
1181

1182
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
6,183,695✔
1183
  if (conflict) {
6,183,695✔
1184
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
208,401✔
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;
208,401✔
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,975,294✔
1191
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1192
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1193
  }
1194
}
6,183,695✔
1195

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

1201
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
51,754,586✔
1202

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

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

1210
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
6,036,005✔
1211

1212
    if (pNew->conflict == TRN_CONFLICT_DB) {
6,036,005✔
1213
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
182,713✔
1214
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
182,713✔
1215
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
163,711✔
1216
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
163,711✔
1217
      }
1218
    }
1219

1220
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
6,036,005✔
1221
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
5,814,797✔
1222
      if (pTrans->conflict == TRN_CONFLICT_DB) {
5,814,797✔
1223
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
62,593✔
1224
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
62,593✔
1225
      }
1226
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
5,814,797✔
1227
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
5,731,087✔
1228
      }
1229
    }
1230

1231
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
6,036,005✔
1232
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
34,479✔
1233
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
34,479✔
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) {
6,036,005✔
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);
6,036,005✔
1258
  }
1259

1260
  return conflict;
39,772,543✔
1261
}
1262

1263
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
51,754,586✔
1264
  int32_t code = 0;
51,754,586✔
1265
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
51,754,586✔
1266
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
33,607,208✔
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)) {
51,754,586✔
1274
    code = TSDB_CODE_MND_TRANS_CONFLICT;
250,833✔
1275
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
250,833✔
1276
    TAOS_RETURN(code);
250,833✔
1277
  }
1278

1279
  TAOS_RETURN(code);
51,503,753✔
1280
}
1281

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

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

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

1300
    if (thisConflict) {
124✔
1301
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
124✔
1302
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1303
      conflict = true;
124✔
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);
124✔
1309
  }
1310

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

1317
  TAOS_RETURN(code);
157,891✔
1318
}
1319

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

1327
  while ((pIter = sdbFetch(pSdb, SDB_RETENTION, pIter, (void **)&pRetention)) != NULL) {
160,387✔
1328
    conflict = false;
4,992✔
1329

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

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

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

1356
  TAOS_RETURN(code);
155,395✔
1357
}
1358

1359
static bool mndTransActionsOfSameType(SArray *pActions) {
44,211,639✔
1360
  int32_t size = taosArrayGetSize(pActions);
44,211,639✔
1361
  ETrnAct lastActType = TRANS_ACTION_NULL;
44,211,639✔
1362
  bool    same = true;
44,211,639✔
1363
  for (int32_t i = 0; i < size; ++i) {
138,857,715✔
1364
    STransAction *pAction = taosArrayGet(pActions, i);
94,646,076✔
1365
    if (i > 0) {
94,646,076✔
1366
      if (lastActType != pAction->actionType) {
63,136,110✔
1367
        same = false;
×
1368
        break;
×
1369
      }
1370
    }
1371
    lastActType = pAction->actionType;
94,646,076✔
1372
  }
1373
  return same;
44,211,639✔
1374
}
1375

1376
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
19,424,394✔
1377
  int32_t code = 0;
19,424,394✔
1378
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
19,424,394✔
1379
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
19,044,031✔
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) {
19,044,031✔
1386
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
5,743,214✔
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);
19,424,394✔
1395
}
1396

1397
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
19,424,394✔
1398
  int32_t code = 0;
19,424,394✔
1399
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
19,424,394✔
UNCOV
1400
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
×
UNCOV
1401
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
×
UNCOV
1402
    TAOS_RETURN(code);
×
1403
  }
1404
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
19,424,394✔
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);
19,424,394✔
1411
}
1412

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

1419
  mInfo("trans:%d, action list:", pTrans->id);
19,425,698✔
1420
  int32_t index = 0;
19,425,698✔
1421
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
30,135,637✔
1422
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
10,709,939✔
1423
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
10,709,939✔
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) {
40,722,076✔
1428
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
21,296,378✔
1429
    if (pAction->actionType == TRANS_ACTION_MSG) {
21,296,378✔
1430
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
20,661,954✔
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),
634,424✔
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) {
91,085,135✔
1440
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
71,659,437✔
1441
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
71,659,437✔
1442
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1443
  }
1444

1445
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
31,533,404✔
1446
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
12,107,706✔
1447
    if (pAction->actionType == TRANS_ACTION_MSG) {
12,107,706✔
1448
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
7,762,813✔
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,344,893✔
1452
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1453
    }
1454
  }
1455

1456
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
19,425,698✔
1457

1458
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
19,424,394✔
1459

1460
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
19,424,394✔
1461

1462
  mInfo("trans:%d, prepare transaction", pTrans->id);
19,424,394✔
1463
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
19,424,394✔
1464
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
3,537✔
1465
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
3,537✔
1466
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
3,537✔
1467
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
3,537✔
1468
    TAOS_RETURN(code);
3,537✔
1469
  }
1470
  mInfo("trans:%d, prepare finished", pTrans->id);
19,420,857✔
1471

1472
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
19,420,857✔
1473
  if (pNew == NULL) {
19,420,857✔
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;
19,420,857✔
1481
  pNew->rpcRsp = pTrans->rpcRsp;
19,420,857✔
1482
  pNew->rpcRspLen = pTrans->rpcRspLen;
19,420,857✔
1483
  pNew->mTraceId = pTrans->mTraceId;
19,420,857✔
1484
  pTrans->pRpcArray = NULL;
19,420,857✔
1485
  pTrans->rpcRsp = NULL;
19,420,857✔
1486
  pTrans->rpcRspLen = 0;
19,420,857✔
1487

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

1495
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
19,417,665✔
1496
  int32_t code = 0;
19,417,665✔
1497
  mInfo("trans:%d, commit transaction", pTrans->id);
19,417,665✔
1498
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
19,417,665✔
1499
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
2,154✔
1500
    TAOS_RETURN(code);
2,154✔
1501
  }
1502
  mInfo("trans:%d, commit finished", pTrans->id);
19,415,511✔
1503
  TAOS_RETURN(code);
19,415,511✔
1504
}
1505

1506
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
1,656✔
1507
  int32_t code = 0;
1,656✔
1508
  mInfo("trans:%d, rollback transaction", pTrans->id);
1,656✔
1509
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,656✔
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,568✔
1514
  TAOS_RETURN(code);
1,568✔
1515
}
1516

1517
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
1,765✔
1518
  int32_t code = 0;
1,765✔
1519
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
1,765✔
1520
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
1,765✔
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,765✔
1525
  TAOS_RETURN(code);
1,765✔
1526
}
1527

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

1532
  if (pTrans->stage == TRN_STAGE_FINISH) {
89,026,292✔
1533
    sendRsp = true;
40,507,930✔
1534
  }
1535

1536
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
89,026,292✔
1537
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
23,256,047✔
1538
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
10,183✔
1539
      sendRsp = true;
10,183✔
1540
    }
1541
  } else {
1542
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
65,770,245✔
1543
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
37,376,397✔
1544
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1545
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1546
      } else {
1547
        if (pTrans->failedTimes > 6) sendRsp = true;
37,376,397✔
1548
      }
1549
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
37,376,397✔
1550
    }
1551
  }
1552

1553
  if (!sendRsp) {
89,026,292✔
1554
    return;
48,508,179✔
1555
  } else {
1556
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
40,518,113✔
1557
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1558
  }
1559

1560
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
40,518,113✔
1561
  taosWLockLatch(&pTrans->lockRpcArray);
40,518,113✔
1562
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
40,518,113✔
1563
  if (size <= 0) {
40,518,113✔
1564
    taosWUnLockLatch(&pTrans->lockRpcArray);
27,087,908✔
1565
    return;
27,087,908✔
1566
  }
1567

1568
  for (int32_t i = 0; i < size; ++i) {
26,860,410✔
1569
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
13,430,205✔
1570
    if (pInfo->handle != NULL) {
13,430,205✔
1571
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
12,879,725✔
1572
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1573
      }
1574
      if (code == TSDB_CODE_SYN_TIMEOUT) {
12,879,725✔
1575
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1576
      }
1577

1578
      if (i != 0 && code == 0) {
12,879,725✔
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,879,725✔
1582
            mndTransStr(pTrans->stage), pInfo->ahandle);
1583

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

1586
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
12,879,725✔
1587
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
1,354,730✔
1588
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
1,354,730✔
1589
        if (pDb != NULL) {
1,354,730✔
1590
          for (int32_t j = 0; j < 12; j++) {
1,709,761✔
1591
            bool ready = mndIsDbReady(pMnode, pDb);
1,709,282✔
1592
            if (!ready) {
1,709,282✔
1593
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
355,031✔
1594
              taosMsleep(1000);
355,031✔
1595
            } else {
1596
              break;
1,354,251✔
1597
            }
1598
          }
1599
        }
1600
        mndReleaseDb(pMnode, pDb);
1,354,730✔
1601
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
11,524,995✔
1602
        void   *pCont = NULL;
2,012,051✔
1603
        int32_t contLen = 0;
2,012,051✔
1604
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
2,012,051✔
1605
          mndTransSetRpcRsp(pTrans, pCont, contLen);
2,010,395✔
1606
        }
1607
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
9,512,944✔
1608
        void   *pCont = NULL;
9,579✔
1609
        int32_t contLen = 0;
9,579✔
1610
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
9,579✔
1611
          mndTransSetRpcRsp(pTrans, pCont, contLen);
9,579✔
1612
        }
1613
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
9,503,365✔
1614
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
9,293✔
1615
        if (code != 0) {
9,293✔
1616
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1617
        }
1618
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOKEN) {
9,494,072✔
1619
        void   *pCont = NULL;
18,860✔
1620
        int32_t contLen = 0;
18,860✔
1621
        if (0 == mndBuildSMCreateTokenResp(pTrans, &pCont, &contLen)) {
18,860✔
1622
          mndTransSetRpcRsp(pTrans, pCont, contLen);
18,860✔
1623
        }
1624
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_TOTP_SECRET) {
9,475,212✔
1625
        void   *pCont = NULL;
17,105✔
1626
        int32_t contLen = 0;
17,105✔
1627
        if (0 == mndBuildSMCreateTotpSecretResp(pTrans, &pCont, &contLen)) {
17,105✔
1628
          mndTransSetRpcRsp(pTrans, pCont, contLen);
17,105✔
1629
        }
1630
      }
1631

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

1641
      tmsgSendRsp(&rspMsg);
12,879,725✔
1642

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

1651
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
22,710,792✔
1652
  int32_t code = 0;
22,710,792✔
1653
  SMnode *pMnode = pRsp->info.node;
22,710,792✔
1654
#ifndef TD_ASTRA_32
1655
  int64_t signature = (int64_t)(pRsp->info.ahandle);
22,710,792✔
1656
  int32_t transId = (int32_t)(signature >> 32);
22,710,792✔
1657
  int32_t action = (int32_t)((signature << 32) >> 32);
22,710,792✔
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);
22,710,792✔
1663
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
22,710,792✔
1664
  if (pTrans == NULL) {
22,710,792✔
UNCOV
1665
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
1666
    if (terrno != 0) code = terrno;
×
UNCOV
1667
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
UNCOV
1668
    goto _OVER;
×
1669
  }
1670

1671
  SArray *pArray = NULL;
22,710,792✔
1672
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
22,710,792✔
1673
    pArray = pTrans->redoActions;
22,704,088✔
1674
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
6,704✔
1675
    pArray = pTrans->undoActions;
6,616✔
1676
  } else {
1677
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
88✔
1678
    goto _OVER;
88✔
1679
  }
1680

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

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

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

1699
      // pTrans->lastErrorNo = pRsp->code;
1700
      mndSetTransLastAction(pTrans, pAction);
22,710,704✔
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,
22,710,704✔
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);
22,710,704✔
1717
  mndTransExecute(pMnode, pTrans, true);
22,710,704✔
1718

1719
_OVER:
22,710,792✔
1720
  mndReleaseTrans(pMnode, pTrans);
22,710,792✔
1721
  TAOS_RETURN(code);
22,710,792✔
1722
}
1723

1724
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
2,101,557✔
1725
  pAction->rawWritten = 0;
2,101,557✔
1726
  pAction->msgSent = 0;
2,101,557✔
1727
  pAction->msgReceived = 0;
2,101,557✔
1728
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
2,101,557✔
1729
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
2,101,557✔
1730
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
812✔
1731
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
812✔
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,100,745✔
1735
  }
1736
  //  pAction->errCode = 0;
1737
}
2,101,557✔
1738

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

1742
  for (int32_t action = 0; action < numOfActions; ++action) {
7,840✔
1743
    STransAction *pAction = taosArrayGet(pArray, action);
6,272✔
1744
    if (pAction->msgSent && pAction->msgReceived &&
6,272✔
1745
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
6,272✔
1746
      continue;
4,704✔
1747
    if (pAction->msgSent && !pAction->msgReceived) {
1,568✔
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,568✔
1753

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

1760
// execute in sync context
1761
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
160,451,580✔
1762
  if (pAction->rawWritten) return 0;
160,451,580✔
1763
  if (topHalf) {
88,219,478✔
1764
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
717✔
1765
  }
1766

1767
  if (pAction->pRaw->type >= SDB_MAX) {
88,218,761✔
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);
88,218,761✔
1783
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
88,218,761✔
1784
    pAction->rawWritten = true;
88,218,761✔
1785
    pAction->errCode = 0;
88,218,761✔
1786
    code = 0;
88,218,761✔
1787
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
88,218,761✔
1788
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1789

1790
    mndSetTransLastAction(pTrans, pAction);
88,218,761✔
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);
88,218,761✔
1799
}
1800

1801
// execute in trans context
1802
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
97,341,327✔
1803
                                     bool notSend) {
1804
  if (pAction->msgSent) return 0;
97,341,327✔
1805
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
37,010,010✔
1806
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
14,287,326✔
1807
  }
1808

1809
  if (notSend) {
22,722,684✔
1810
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
8,836✔
1811
    return 0;
8,836✔
1812
  }
1813

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

1819
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
22,713,848✔
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);
22,713,848✔
1827
  if (rpcMsg.pCont == NULL) {
22,713,848✔
1828
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1829
    return -1;
×
1830
  }
1831
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
22,713,848✔
1832
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
22,713,848✔
1833
  rpcMsg.info.notFreeAhandle = 1;
22,713,848✔
1834
  STraceId* trace = &(rpcMsg.info.traceId);
22,713,848✔
1835

1836
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
22,713,848✔
1837

1838
  char    detail[1024] = {0};
22,713,848✔
1839
  int32_t len = snprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
22,713,848✔
1840
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
22,713,848✔
1841
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
48,254,278✔
1842
    len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
25,540,430✔
1843
                    pAction->epSet.eps[i].port);
25,540,430✔
1844
  }
1845

1846
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
22,713,848✔
1847
  if (code == 0) {
22,713,848✔
1848
    pAction->msgSent = 1;
22,713,048✔
1849
    // pAction->msgReceived = 0;
1850
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
22,713,048✔
1851
    pAction->startTime = taosGetTimestampMs();
22,713,048✔
1852
    pAction->endTime = 0;
22,713,048✔
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, 
22,713,048✔
1854
              trace ? trace->msgId : 0, detail);
1855

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

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

1867
  TAOS_RETURN(code);
22,713,848✔
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,
257,792,907✔
1881
                                        bool notSend) {
1882
  if (pAction->actionType == TRANS_ACTION_RAW) {
257,792,907✔
1883
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
160,451,580✔
1884
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
97,341,327✔
1885
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
97,341,327✔
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) {
81,110,255✔
1892
  int32_t numOfActions = taosArrayGetSize(pArray);
81,110,255✔
1893
  int32_t code = 0;
81,110,255✔
1894

1895
  for (int32_t action = 0; action < numOfActions; ++action) {
301,465,145✔
1896
    STransAction *pAction = taosArrayGet(pArray, action);
231,624,842✔
1897
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
231,624,842✔
1898
    if (code != 0) {
231,624,842✔
1899
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
11,269,952✔
1900
            numOfActions);
1901
      break;
11,269,952✔
1902
    }
1903
  }
1904

1905
  return code;
81,110,255✔
1906
}
1907

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

1913
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
81,110,255✔
1914
    return code;
11,269,952✔
1915
  }
1916

1917
  int32_t       numOfExecuted = 0;
69,840,303✔
1918
  int32_t       errCode = 0;
69,840,303✔
1919
  STransAction *pErrAction = NULL;
69,840,303✔
1920
  for (int32_t action = 0; action < numOfActions; ++action) {
290,194,929✔
1921
    STransAction *pAction = taosArrayGet(pArray, action);
220,354,626✔
1922
    if (pAction->msgReceived || pAction->rawWritten) {
220,354,626✔
1923
      numOfExecuted++;
183,090,326✔
1924
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
183,090,326✔
1925
        errCode = pAction->errCode;
2,564✔
1926
        pErrAction = pAction;
2,564✔
1927
      }
1928
    } else {
1929
      pErrAction = pAction;
37,264,300✔
1930
    }
1931
  }
1932

1933
  mndSetTransLastAction(pTrans, pErrAction);
69,840,303✔
1934

1935
  if (numOfExecuted == numOfActions) {
69,840,303✔
1936
    if (errCode == 0) {
50,599,473✔
1937
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
50,597,905✔
1938
      return 0;
50,597,905✔
1939
    } else {
1940
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1,568✔
1941
      mndTransResetActions(pMnode, pTrans, pArray);
1,568✔
1942
      terrno = errCode;
1,568✔
1943
      return errCode;
1,568✔
1944
    }
1945
  } else {
1946
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
19,240,830✔
1947

1948
    for (int32_t action = 0; action < numOfActions; ++action) {
73,726,061✔
1949
      STransAction *pAction = taosArrayGet(pArray, action);
54,485,231✔
1950
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
54,485,231✔
1951
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1952
             pAction->acceptableCode, pAction->retryCode);
1953
      if (pAction->msgSent) {
54,485,231✔
1954
        bool reset = false;
54,476,395✔
1955
        if (pAction->msgReceived) {
54,476,395✔
1956
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
17,220,931✔
1957
        } else {
1958
          int64_t timestamp = taosGetTimestampMs();
37,255,464✔
1959
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
37,255,464✔
1960
        }
1961
        if (reset) {
54,476,395✔
1962
          mndTransResetAction(pMnode, pTrans, pAction);
996✔
1963
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
996✔
1964
                pAction->id, pAction->errCode, pAction->startTime);
1965
        }
1966
      }
1967
    }
1968
    return TSDB_CODE_ACTION_IN_PROGRESS;
19,240,830✔
1969
  }
1970
}
1971

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

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

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

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

2004
  if (pTrans->actionPos >= numOfActions) {
7,463,393✔
2005
    return code;
318,311✔
2006
  }
2007

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

2011
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
11,303,215✔
2012
    STransAction *pAction = taosArrayGet(pActions, action);
11,021,890✔
2013

2014
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
11,021,890✔
2015
      code = TSDB_CODE_ACTION_IN_PROGRESS;
7,836✔
2016
      break;
7,986✔
2017
    }
2018

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

2022
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
11,014,054✔
2023
    if (code == 0) {
11,014,054✔
2024
      if (pAction->msgSent) {
9,375,564✔
2025
        bool reset = false;
8,241,008✔
2026
        if (pAction->msgReceived) {
8,241,008✔
2027
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,611,259✔
2028
            code = pAction->errCode;
2,068,212✔
2029
            reset = true;
2,068,212✔
2030
          } else {
2031
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,543,047✔
2032
          }
2033
        } else {
2034
          int64_t timestamp = taosGetTimestampMs();
4,629,749✔
2035
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
4,629,749✔
2036
          code = TSDB_CODE_ACTION_IN_PROGRESS;
4,629,749✔
2037
        }
2038
        if (reset) {
8,241,008✔
2039
          mndTransResetAction(pMnode, pTrans, pAction);
2,068,212✔
2040
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2,068,212✔
2041
                pAction->id, pAction->errCode, pAction->startTime);
2042
        }
2043
      } else if (pAction->rawWritten) {
1,134,556✔
2044
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,134,556✔
2045
          code = pAction->errCode;
×
2046
        } else {
2047
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1,134,556✔
2048
        }
2049
      } else {
2050
      }
2051
    }
2052

2053
    if (code == 0) {
11,014,054✔
2054
      pTrans->failedTimes = 0;
2,677,603✔
2055
    }
2056
    mndSetTransLastAction(pTrans, pAction);
11,014,054✔
2057
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
11,014,054✔
2058
      mInfo(
6,268,239✔
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,745,815✔
2064
      mError(
2,068,212✔
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};
11,014,054✔
2072
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
11,014,054✔
2073
      pTrans->lastErrorNo = code;
2,226,172✔
2074
      pTrans->code = code;
2,226,172✔
2075
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
2,226,172✔
2076
            action, str);
2077
      break;
2,226,172✔
2078
    }
2079

2080
    if (code == 0) {
8,787,882✔
2081
      pTrans->code = 0;
2,089,921✔
2082
      pTrans->actionPos++;
2,089,921✔
2083
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
2,089,921✔
2084
            pAction->id);
2085
      (void)taosThreadMutexUnlock(&pTrans->mutex);
2,089,921✔
2086
      code = mndTransSync(pMnode, pTrans);
2,089,921✔
2087
      (void)taosThreadMutexLock(&pTrans->mutex);
2,089,921✔
2088
      if (code != 0) {
2,089,921✔
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,697,961✔
2096
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,629,749✔
2097
      break;
4,629,749✔
2098
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
2,068,212✔
2099
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
812✔
2100
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
2,068,212✔
2101
            code, tstrerror(code));
2102
      pTrans->lastErrorNo = code;
2,068,212✔
2103
      taosMsleep(300);
2,068,212✔
2104
      action--;
2,068,212✔
2105
      continue;
2,068,212✔
2106
    } else {
UNCOV
2107
      terrno = code;
×
UNCOV
2108
      pTrans->lastErrorNo = code;
×
UNCOV
2109
      pTrans->code = code;
×
UNCOV
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);
UNCOV
2112
      break;
×
2113
    }
2114
  }
2115

2116
  return code;
7,145,082✔
2117
}
2118

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

2126
  if (groupId <= 0) {
4,188,003✔
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));
4,188,003✔
2132
  if (actionPos == NULL) {
4,188,003✔
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) {
4,188,003✔
2138
    mInfo("trans:%d, this serial group is finished, actionPos:%d >= numOfActions:%d at group %d", pTrans->id,
661,823✔
2139
          *actionPos, numOfActions, groupId);
2140
    return TSDB_CODE_MND_TRANS_GROUP_FINISHED;
661,823✔
2141
  }
2142

2143
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
4,091,053✔
2144
    STransAction **ppAction = taosArrayGet(pActions, action);
3,852,518✔
2145
    STransAction  *pAction = *ppAction;
3,852,518✔
2146

2147
    if (notSend && !pAction->msgSent) {
3,852,518✔
2148
      mInfo(
530,464✔
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;
530,464✔
2154
      break;
530,464✔
2155
    }
2156

2157
    mInfo(
3,322,054✔
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,322,054✔
2164
    if (code == 0) {
3,322,054✔
2165
      if (pAction->msgSent) {
1,941,653✔
2166
        if (pAction->msgReceived) {
1,730,423✔
2167
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
502,174✔
2168
            code = pAction->errCode;
30,781✔
2169
            mndTransResetAction(pMnode, pTrans, pAction);
30,781✔
2170
          } else {
2171
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
471,393✔
2172
                  pAction->id, action, numOfActions, groupId);
2173
          }
2174
        } else {
2175
          code = TSDB_CODE_ACTION_IN_PROGRESS;
1,228,249✔
2176
        }
2177
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
1,730,423✔
2178
        if (msgSent != NULL) {
1,730,423✔
2179
          *msgSent = pAction->msgSent;
1,730,423✔
2180
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
1,730,423✔
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) {
211,230✔
2185
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
211,230✔
2186
          code = pAction->errCode;
×
2187
        } else {
2188
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
211,230✔
2189
        }
2190
      } else {
2191
      }
2192
    }
2193

2194
    if (code == 0) {
3,322,054✔
2195
      pTrans->failedTimes = 0;
682,623✔
2196
    }
2197
    mndSetTransLastAction(pTrans, pAction);
3,322,054✔
2198

2199
    char str[200] = {0};
3,322,054✔
2200
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
3,322,054✔
2201
      pTrans->lastErrorNo = code;
1,528,932✔
2202
      pTrans->code = code;
1,528,932✔
2203
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
1,528,932✔
2204
        mInfo(
1,380,401✔
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,531✔
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,528,932✔
2217
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2218
      break;
1,528,932✔
2219
    }
2220

2221
    if (code == 0) {
1,793,122✔
2222
      pTrans->code = 0;
534,092✔
2223
      pTrans->actionPos++;
534,092✔
2224
      (*actionPos)++;
534,092✔
2225
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
534,092✔
2226
            pAction->id);
2227
      (void)taosThreadMutexUnlock(&pTrans->mutex);
534,092✔
2228
      code = mndTransSync(pMnode, pTrans);
534,092✔
2229
      (void)taosThreadMutexLock(&pTrans->mutex);
534,092✔
2230
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
534,092✔
2231
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
12,890,752✔
2232
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
12,356,660✔
2233
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
12,356,660✔
2234
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
12,356,660✔
2235
          pTmpAction->msgSent = *msgSent;
618,517✔
2236
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
618,517✔
2237
        }
2238
      }
2239
      if (code != 0) {
534,092✔
UNCOV
2240
        pTrans->actionPos--;
×
UNCOV
2241
        (*actionPos)--;
×
UNCOV
2242
        pTrans->code = terrno;
×
UNCOV
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());
UNCOV
2245
        break;
×
2246
      }
2247
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
1,259,030✔
2248
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
1,228,249✔
2249
            mndTransStr(pAction->stage), pAction->id);
2250
      break;
1,228,249✔
2251
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
30,781✔
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,
30,781✔
2254
            code, tstrerror(code));
2255
      pTrans->lastErrorNo = code;
30,781✔
2256
      taosMsleep(300);
30,781✔
2257
      action--;
30,781✔
2258
      continue;
30,781✔
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,526,180✔
2270
}
2271

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

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

2288
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,321,683✔
2289
  if(pHash == NULL){
1,321,683✔
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) {
31,121,169✔
2294
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
29,799,486✔
2295
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
29,799,486✔
2296
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
29,799,486✔
2297
  }
2298
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
1,321,683✔
2299

2300
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
1,321,683✔
2301
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
1,321,683✔
2302
  int32_t currentGroup = 1;
1,321,683✔
2303
  while (pIter) {
5,509,686✔
2304
    SArray **redoActions = pIter;
4,188,003✔
2305
    size_t   keyLen = 0;
4,188,003✔
2306
    int32_t *key = taosHashGetKey(pIter, &keyLen);
4,188,003✔
2307
    int32_t  actionCount = taosArrayGetSize(*redoActions);
4,188,003✔
2308
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
4,188,003✔
2309
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2310
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
4,188,003✔
2311
                                             notSend, pHash);
2312
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
4,188,003✔
2313
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,380,401✔
2314
            tstrerror(code));
2315
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,807,602✔
2316
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
1,758,713✔
2317
    } else if (code == TSDB_CODE_MND_TRANS_GROUP_FINISHED) {
1,048,889✔
2318
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
661,823✔
2319
    } else if (code != 0) {
387,066✔
UNCOV
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++;
387,066✔
2324
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
387,066✔
2325
    }
2326
    currentGroup++;
4,188,003✔
2327
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
4,188,003✔
2328
  }
2329

2330
  taosHashCleanup(pHash);
1,321,683✔
2331

2332
  if (successCount == groupCount) {
1,321,683✔
2333
    total_code = 0;
27,148✔
2334
  } else {
2335
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
1,294,535✔
2336
  }
2337

2338
  return total_code;
1,321,683✔
2339
}
2340

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

2345
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
1,542,283✔
2346
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
1,542,283✔
2347
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
1,542,283✔
2348
      code = 0;
191,030✔
2349
    } else {
2350
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
1,351,253✔
2351
      if (pAction != NULL && pAction->groupId == -1) {
1,351,253✔
2352
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
29,570✔
2353
      } else {
2354
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
1,321,683✔
2355
        if (code == 0) {
1,321,683✔
2356
          if (pTrans->actionPos < numOfActions) {
27,148✔
2357
            code = TSDB_CODE_ACTION_IN_PROGRESS;
19,663✔
2358
          }
2359
        }
2360
      }
2361
    }
2362
  }
2363

2364
  (void)taosThreadMutexUnlock(&pTrans->mutex);
1,542,283✔
2365

2366
  return code;
1,542,283✔
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) {
21,096,258✔
2380
  bool    continueExec = true;
21,096,258✔
2381
  int32_t code = 0;
21,096,258✔
2382
  terrno = 0;
21,096,258✔
2383

2384
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
21,096,258✔
2385
  if (numOfActions == 0) goto _OVER;
21,096,258✔
2386

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

2389
  for (int32_t action = 0; action < numOfActions; ++action) {
20,618,722✔
2390
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
11,831,957✔
2391
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
11,831,957✔
2392
    if (code != 0) {
11,831,957✔
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,786,765✔
2401
  pTrans->stage = TRN_STAGE_REDO_ACTION;
21,096,258✔
2402
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
21,096,258✔
2403
  return continueExec;
21,096,258✔
2404
}
2405

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

2411
  if (pTrans->exec == TRN_EXEC_SERIAL) {
67,925,805✔
2412
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
7,435,387✔
2413
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
60,490,418✔
2414
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
58,948,135✔
2415
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
1,542,283✔
2416
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
1,542,283✔
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 &&
67,928,173✔
2422
      mndTransIsInSyncContext(topHalf)) {
2,368✔
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)) {
67,925,805✔
2433
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
23,880,908✔
2434
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2435
      return false;
23,880,908✔
2436
    }
2437
  }
2438
  terrno = code;
44,044,897✔
2439

2440
  if (code == 0) {
44,044,897✔
2441
    pTrans->code = 0;
19,417,665✔
2442
    pTrans->stage = TRN_STAGE_COMMIT;
19,417,665✔
2443
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
19,417,665✔
2444
    continueExec = true;
19,417,665✔
2445
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
24,627,232✔
2446
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
24,624,864✔
2447
    continueExec = false;
24,624,864✔
2448
  } else {
2449
    pTrans->failedTimes++;
2,368✔
2450
    pTrans->code = terrno;
2,368✔
2451
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
2,368✔
2452
      if (pTrans->lastAction != 0) {
1,656✔
2453
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
1,656✔
2454
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
1,656✔
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,656✔
2466
      pTrans->actionPos = 0;
1,656✔
2467
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
1,656✔
2468
             pTrans->actionPos);
2469
      continueExec = true;
1,656✔
2470
    } else {
2471
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
712✔
2472
      continueExec = false;
712✔
2473
    }
2474
  }
2475

2476
  return continueExec;
44,044,897✔
2477
}
2478

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

2483
  bool    continueExec = true;
19,417,665✔
2484
  int32_t code = mndTransCommit(pMnode, pTrans);
19,417,665✔
2485

2486
  if (code == 0) {
19,417,665✔
2487
    pTrans->code = 0;
19,415,511✔
2488
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
19,415,511✔
2489
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
19,415,511✔
2490
    continueExec = true;
19,415,511✔
2491
  } else {
2492
    pTrans->code = terrno;
2,154✔
2493
    pTrans->failedTimes++;
2,154✔
2494
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
2,154✔
2495
    continueExec = false;
2,154✔
2496
  }
2497

2498
  return continueExec;
19,417,665✔
2499
}
2500

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

2505
  if (code == 0) {
40,503,769✔
2506
    pTrans->code = 0;
40,503,769✔
2507
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
40,503,769✔
2508
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
40,503,769✔
2509
    continueExec = true;
40,503,769✔
UNCOV
2510
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
UNCOV
2511
    pTrans->code = 0;
×
UNCOV
2512
    pTrans->stage = TRN_STAGE_COMMIT;
×
UNCOV
2513
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
UNCOV
2514
    continueExec = true;
×
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;
40,503,769✔
2523
}
2524

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

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

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

2538
  if (code == 0) {
9,838✔
2539
    pTrans->stage = TRN_STAGE_PRE_FINISH;
1,654✔
2540
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
1,654✔
2541
    continueExec = true;
1,654✔
2542
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
8,184✔
2543
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
8,184✔
2544
    continueExec = false;
8,184✔
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,838✔
2552
}
2553

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

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

2561
  if (code == 0) {
1,656✔
2562
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1,568✔
2563
    continueExec = true;
1,568✔
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,656✔
2571
}
2572

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

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

2580
  if (code == 0) {
1,765✔
2581
    pTrans->stage = TRN_STAGE_FINISH;
1,765✔
2582
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
1,765✔
2583
    continueExec = true;
1,765✔
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,765✔
2591
}
2592

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

2597
  SSdbRaw *pRaw = mndTransEncode(pTrans);
21,090,195✔
2598
  if (pRaw == NULL) {
21,090,195✔
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));
21,090,195✔
2603

2604
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
21,090,195✔
2605
  if (code != 0) {
21,090,195✔
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,
21,090,195✔
2610
        pTrans->failedTimes, pTrans->createdTime);
2611
  return continueExec;
21,090,195✔
2612
}
2613

2614
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
89,026,292✔
2615
  bool continueExec = true;
89,026,292✔
2616

2617
  while (continueExec) {
257,396,172✔
2618
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
168,369,880✔
2619
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2620
    pTrans->lastExecTime = taosGetTimestampMs();
168,369,880✔
2621
    switch (pTrans->stage) {
168,369,880✔
2622
      case TRN_STAGE_PREPARE:
×
2623
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2624
        break;
×
2625
      case TRN_STAGE_REDO_ACTION:
67,925,805✔
2626
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
67,925,805✔
2627
        break;
67,925,805✔
2628
      case TRN_STAGE_COMMIT:
19,417,665✔
2629
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
19,417,665✔
2630
        break;
19,417,665✔
2631
      case TRN_STAGE_COMMIT_ACTION:
40,503,769✔
2632
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
40,503,769✔
2633
        break;
40,503,769✔
2634
      case TRN_STAGE_ROLLBACK:
1,656✔
2635
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
1,656✔
2636
        break;
1,656✔
2637
      case TRN_STAGE_UNDO_ACTION:
11,749✔
2638
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
11,749✔
2639
        break;
11,749✔
2640
      case TRN_STAGE_PRE_FINISH:
1,765✔
2641
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
1,765✔
2642
        break;
1,765✔
2643
      case TRN_STAGE_FINISH:
40,507,471✔
2644
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
40,507,471✔
2645
        break;
40,507,471✔
2646
      default:
×
2647
        continueExec = false;
×
2648
        break;
×
2649
    }
2650
  }
2651

2652
  mndTransSendRpcRsp(pMnode, pTrans);
89,026,292✔
2653
}
89,026,292✔
2654

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

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

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

2673
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
111✔
2674
  SArray *pArray = NULL;
111✔
2675
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
111✔
2676
    pArray = pTrans->redoActions;
111✔
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){
111✔
2684
    if(pTrans->ableToBeKilled == false){
111✔
2685
      return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2686
    }
2687
  }
2688
  
2689
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
111✔
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){
111✔
2700
    pTrans->stage = TRN_STAGE_PRE_FINISH;
111✔
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);
111✔
2707
  mndTransExecute(pMnode, pTrans, true);
111✔
2708
  return 0;
111✔
2709
}
2710

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

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

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

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

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

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

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

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

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

2752
  void *pIter = NULL;
16,550,197✔
2753
  while (1) {
1,937,946✔
2754
    STrans *pTrans = NULL;
18,488,143✔
2755
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
18,488,143✔
2756
    if (pIter == NULL) break;
18,488,143✔
2757
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
3,875,892✔
2758
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2759
    }
2760
    sdbRelease(pSdb, pTrans);
1,937,946✔
2761
  }
2762

2763
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
16,550,197✔
2764

2765
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
18,488,143✔
2766
    int32_t *pTransId = taosArrayGet(pArray, i);
1,937,946✔
2767
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
1,937,946✔
2768
    if (pTrans != NULL) {
1,937,946✔
2769
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
1,937,946✔
2770
      mndTransExecute(pMnode, pTrans, false);
1,937,946✔
2771
    }
2772
    mndReleaseTrans(pMnode, pTrans);
1,937,946✔
2773
  }
2774
  taosArrayDestroy(pArray);
16,550,197✔
2775
}
2776

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

2788
  struct tm tm;
17,988,241✔
2789
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
17,988,241✔
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,988,241✔
2794

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

2803
  return buf;
17,988,241✔
2804
}
2805

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

2811
  if (pTrans->stage == TRN_STAGE_PREPARE) {
372,035✔
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) {
372,035✔
2823
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
12,166,286✔
2824
      len = 0;
11,794,251✔
2825
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
11,794,251✔
2826
      if (pAction->actionType == TRANS_ACTION_MSG) {
11,794,251✔
2827
        char bufStart[40] = {0};
8,976,065✔
2828
        (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
8,976,065✔
2829

2830
        char endStart[40] = {0};
8,976,065✔
2831
        (void)formatTimestamp(endStart, sizeof(endStart), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
8,976,065✔
2832
        len += snprintf(detail + len, sizeof(detail) - len,
17,952,130✔
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,952,130✔
2836
                        pAction->msgReceived, bufStart, endStart);
8,976,065✔
2837

2838
        SEpSet epset = pAction->epSet;
8,976,065✔
2839
        if (epset.numOfEps > 0) {
8,976,065✔
2840
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
8,976,065✔
2841
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
20,149,712✔
2842
            len +=
11,173,647✔
2843
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
11,173,647✔
2844
          }
2845
        }
2846

2847
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
8,976,065✔
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,818,186✔
2851
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
2,818,186✔
2852
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
2,818,186✔
2853
      }
2854
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
11,794,251✔
2855
    }
2856
  }
2857

2858
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
372,035✔
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
}
372,035✔
2884

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

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

2898
    cols = 0;
372,035✔
2899

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

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

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

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

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

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

2927
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
372,035✔
2928
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
372,035✔
2929
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
372,035✔
2930
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
372,035✔
2931
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
372,035✔
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++);
372,035✔
2942
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
372,035✔
2943
                        _OVER);
2944

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

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

2965
    mndTransLogAction(pTrans);
372,035✔
2966

2967
    numOfRows++;
372,035✔
2968
    sdbRelease(pSdb, pTrans);
372,035✔
2969
  }
2970

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

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

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

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

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

3010
  cols = 0;
66,693✔
3011

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

3014
  if (pAction->actionType == TRANS_ACTION_MSG) {
66,693✔
3015
    int32_t len = 0;
54,789✔
3016

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

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

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

3048
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
54,789✔
3049
    len = 0;
54,789✔
3050
    char bufStart[40] = {0};
54,789✔
3051
    if (pAction->startTime > 0)
54,789✔
3052
      (void)formatTimestamp(bufStart, sizeof(bufStart), pAction->startTime, TSDB_TIME_PRECISION_MILLI);
18,966✔
3053
    char bufEnd[40] = {0};
54,789✔
3054
    if (pAction->endTime > 0)
54,789✔
3055
      (void)formatTimestamp(bufEnd, sizeof(bufEnd), pAction->endTime, TSDB_TIME_PRECISION_MILLI);
17,145✔
3056

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

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

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

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

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

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

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

3107
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
855✔
3108
  if (stage == TRN_STAGE_PREPARE) {
855✔
3109
    return pTrans->prepareActions;
×
3110
  }
3111
  if (stage == TRN_STAGE_REDO_ACTION) {
855✔
3112
    return pTrans->redoActions;
855✔
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,
855✔
3131
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3132
  int32_t actionNum = taosArrayGetSize(pActions);
855✔
3133
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
855✔
3134

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

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

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

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

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

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

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

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

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

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

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

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

3201
  if (code != 0) {
1,338✔
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,338✔
3205
  }
3206
  if (numOfRows == 0) {
1,338✔
3207
    taosMemoryFree(pShow->pIter);
483✔
3208
    pShow->pIter = NULL;
483✔
3209
  }
3210
  return numOfRows;
1,338✔
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