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

taosdata / TDengine / #4118

17 May 2025 06:43AM UTC coverage: 62.797% (+0.7%) from 62.054%
#4118

push

travis-ci

web-flow
Merge pull request #31115 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

156841 of 318088 branches covered (49.31%)

Branch coverage included in aggregate %.

176 of 225 new or added lines in 20 files covered. (78.22%)

2989 existing lines in 163 files now uncovered.

242067 of 317143 relevant lines covered (76.33%)

6956088.31 hits per line

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

63.48
/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

28
#define TRANS_VER1_NUMBER  1
29
#define TRANS_VER2_NUMBER  2
30
#define TRANS_ARRAY_SIZE   8
31
#define TRANS_RESERVE_SIZE 42
32
#define TRANS_ACTION_TIMEOUT 1000 * 1000 * 60 * 15
33

34
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans);
35
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *OldTrans, STrans *pOld);
36
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc);
37

38
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw);
39
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction);
40
static void    mndTransDropLogs(SArray *pArray);
41
static void    mndTransDropActions(SArray *pArray);
42

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

58
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
536,822✔
59

60
static bool mndCannotExecuteTrans(SMnode *pMnode, bool topHalf) {
449,722✔
61
  bool isLeader = mndIsLeader(pMnode);
449,722✔
62
  bool ret = (!pMnode->deploy && !isLeader) || mndTransIsInSyncContext(topHalf);
449,722✔
63
  if (ret) mDebug("cannot execute trans action, deploy:%d, isLeader:%d, topHalf:%d", pMnode->deploy, isLeader, topHalf);
449,722✔
64
  return ret;
449,722✔
65
}
66

67
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
583,104✔
68

69
static void    mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans);
70
static int32_t mndProcessTransTimer(SRpcMsg *pReq);
71
static int32_t mndProcessTtl(SRpcMsg *pReq);
72
static int32_t mndProcessKillTransReq(SRpcMsg *pReq);
73

74
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
75
static void    mndCancelGetNextTrans(SMnode *pMnode, void *pIter);
76
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
77
static int32_t tsMaxTransId = 0;
78

79
int32_t mndInitTrans(SMnode *pMnode) {
2,189✔
80
  SSdbTable table = {
2,189✔
81
      .sdbType = SDB_TRANS,
82
      .keyType = SDB_KEY_INT32,
83
      .encodeFp = (SdbEncodeFp)mndTransEncode,
84
      .decodeFp = (SdbDecodeFp)mndTransDecode,
85
      .insertFp = (SdbInsertFp)mndTransActionInsert,
86
      .updateFp = (SdbUpdateFp)mndTransActionUpdate,
87
      .deleteFp = (SdbDeleteFp)mndTransDelete,
88
  };
89

90
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
2,189✔
91
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
2,189✔
92

93
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndRetrieveTrans);
2,189✔
94
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndCancelGetNextTrans);
2,189✔
95
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANSACTION_DETAIL, mndRetrieveTransDetail);
2,189✔
96
  return sdbSetTable(pMnode->pSdb, table);
2,189✔
97
}
98

99
void mndCleanupTrans(SMnode *pMnode) {}
2,188✔
100

101
static int32_t mndTransGetActionsSize(SArray *pArray) {
690,292✔
102
  int32_t actionNum = taosArrayGetSize(pArray);
690,292✔
103
  int32_t rawDataLen = 0;
690,292✔
104

105
  for (int32_t i = 0; i < actionNum; ++i) {
2,065,707✔
106
    STransAction *pAction = taosArrayGet(pArray, i);
1,375,415✔
107
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,375,415✔
108
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
877,899✔
109
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
497,516!
110
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
497,516✔
111
    } else {
112
      // empty
113
    }
114
    rawDataLen += sizeof(int8_t);
1,375,415✔
115
  }
116

117
  return rawDataLen;
690,292✔
118
}
119

120
static int32_t mndTransEncodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionsNum) {
690,292✔
121
  int32_t code = 0;
690,292✔
122
  int32_t lino = 0;
690,292✔
123
  int32_t dataPos = *offset;
690,292✔
124
  int8_t  unused = 0;
690,292✔
125
  int32_t ret = -1;
690,292✔
126

127
  for (int32_t i = 0; i < actionsNum; ++i) {
2,065,707✔
128
    STransAction *pAction = taosArrayGet(pActions, i);
1,375,415✔
129
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
1,375,415!
130
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
1,375,415!
131
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
1,375,415!
132
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
1,375,415!
133
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
1,375,415!
134
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
1,375,415!
135
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
1,375,415!
136
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,375,415✔
137
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
877,899✔
138
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
877,899!
139
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
877,899!
140
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
877,899!
141
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
497,516!
142
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
497,516!
143
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
497,516!
144
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
497,516!
145
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
497,516!
146
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
497,516!
147
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
497,516!
148
    } else {
149
      // nothing
150
    }
151
  }
152
  ret = 0;
690,292✔
153

154
_OVER:
690,292✔
155
  *offset = dataPos;
690,292✔
156
  return ret;
690,292✔
157
}
158

159
SSdbRaw *mndTransEncode(STrans *pTrans) {
172,573✔
160
  int32_t code = 0;
172,573✔
161
  int32_t lino = 0;
172,573✔
162
  terrno = TSDB_CODE_INVALID_MSG;
172,573✔
163
  int8_t sver = TRANS_VER2_NUMBER;
172,573✔
164

165
  int32_t rawDataLen = sizeof(STrans) + TRANS_RESERVE_SIZE + pTrans->paramLen;
172,573✔
166
  rawDataLen += mndTransGetActionsSize(pTrans->prepareActions);
172,573✔
167
  rawDataLen += mndTransGetActionsSize(pTrans->redoActions);
172,573✔
168
  rawDataLen += mndTransGetActionsSize(pTrans->undoActions);
172,573✔
169
  rawDataLen += mndTransGetActionsSize(pTrans->commitActions);
172,573✔
170

171
  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, sver, rawDataLen);
172,573✔
172
  if (pRaw == NULL) {
172,573!
173
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
×
174
    return NULL;
×
175
  }
176

177
  int32_t dataPos = 0;
172,573✔
178
  SDB_SET_INT32(pRaw, dataPos, pTrans->id, _OVER)
172,573!
179
  SDB_SET_INT8(pRaw, dataPos, pTrans->stage, _OVER)
172,573!
180
  SDB_SET_INT8(pRaw, dataPos, pTrans->policy, _OVER)
172,573!
181
  SDB_SET_INT8(pRaw, dataPos, pTrans->conflict, _OVER)
172,573!
182
  SDB_SET_INT8(pRaw, dataPos, pTrans->exec, _OVER)
172,573!
183
  SDB_SET_INT8(pRaw, dataPos, pTrans->oper, _OVER)
172,573!
184
  SDB_SET_INT8(pRaw, dataPos, 0, _OVER)
172,573!
185
  SDB_SET_INT16(pRaw, dataPos, pTrans->originRpcType, _OVER)
172,573!
186
  SDB_SET_INT64(pRaw, dataPos, pTrans->createdTime, _OVER)
172,573!
187
  SDB_SET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
172,573!
188
  SDB_SET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
172,573!
189
  SDB_SET_INT32(pRaw, dataPos, pTrans->actionPos, _OVER)
172,573!
190

191
  int32_t prepareActionNum = taosArrayGetSize(pTrans->prepareActions);
172,573✔
192
  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
172,573✔
193
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
172,573✔
194
  int32_t commitActionNum = taosArrayGetSize(pTrans->commitActions);
172,573✔
195

196
  if (sver > TRANS_VER1_NUMBER) {
172,573!
197
    SDB_SET_INT32(pRaw, dataPos, prepareActionNum, _OVER)
172,573!
198
  }
199
  SDB_SET_INT32(pRaw, dataPos, redoActionNum, _OVER)
172,573!
200
  SDB_SET_INT32(pRaw, dataPos, undoActionNum, _OVER)
172,573!
201
  SDB_SET_INT32(pRaw, dataPos, commitActionNum, _OVER)
172,573!
202

203
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum) < 0) goto _OVER;
172,573!
204
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum) < 0) goto _OVER;
172,573!
205
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum) < 0) goto _OVER;
172,573!
206
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum) < 0) goto _OVER;
172,573!
207

208
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
172,573!
209
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
172,573!
210
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
172,573!
211
  if (pTrans->param != NULL) {
172,573!
212
    SDB_SET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER)
×
213
  }
214

215
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
172,573!
216

217
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
172,573✔
218
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
172,573!
219
  void *pIter = NULL;
172,573✔
220
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
172,573✔
221
  while (pIter) {
172,609✔
222
    int32_t arbGroupId = *(int32_t *)pIter;
36✔
223
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
36!
224
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
36✔
225
  }
226

227
  if (sver > TRANS_VER1_NUMBER) {
172,573!
228
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
172,573!
229
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
172,573!
230
  }
231

232
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
172,573!
233
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
172,573!
234

235
  terrno = 0;
172,573✔
236

237
_OVER:
172,573✔
238
  if (terrno != 0) {
172,573!
239
    mError("trans:%d, failed to encode to raw:%p maxlen:%d len:%d since %s", pTrans->id, pRaw, sdbGetRawTotalSize(pRaw),
×
240
           dataPos, terrstr());
241
    sdbFreeRaw(pRaw);
×
242
    return NULL;
×
243
  }
244

245
  mTrace("trans:%d, encode to raw:%p, row:%p len:%d", pTrans->id, pRaw, pTrans, dataPos);
172,573✔
246
  return pRaw;
172,573✔
247
}
248

249
static int32_t mndTransDecodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionNum) {
1,288,988✔
250
  int32_t      code = 0;
1,288,988✔
251
  int32_t      lino = 0;
1,288,988✔
252
  STransAction action = {0};
1,288,988✔
253
  int32_t      dataPos = *offset;
1,288,988✔
254
  int8_t       unused = 0;
1,288,988✔
255
  int8_t       stage = 0;
1,288,988✔
256
  int8_t       actionType = 0;
1,288,988✔
257
  int32_t      dataLen = 0;
1,288,988✔
258
  int32_t      ret = -1;
1,288,988✔
259

260
  for (int32_t i = 0; i < actionNum; ++i) {
3,819,285✔
261
    memset(&action, 0, sizeof(action));
2,530,297✔
262
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
2,530,297!
263
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
2,530,297!
264
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
2,530,297!
265
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
2,530,297!
266
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
2,530,297!
267
    action.actionType = actionType;
2,530,297✔
268
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
2,530,297!
269
    action.stage = stage;
2,530,297✔
270
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
2,530,297!
271
    if (action.actionType == TRANS_ACTION_RAW) {
2,530,297✔
272
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
1,575,634!
273
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
1,575,634!
274
      action.pRaw = taosMemoryMalloc(dataLen);
1,575,634!
275
      if (action.pRaw == NULL) goto _OVER;
1,575,634!
276
      mTrace("raw:%p, is created", action.pRaw);
1,575,634✔
277
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
1,575,634!
278
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
1,575,634!
279
      action.pRaw = NULL;
1,575,634✔
280
    } else if (action.actionType == TRANS_ACTION_MSG) {
954,663!
281
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
954,663!
282
      tmsgUpdateDnodeEpSet(&action.epSet);
954,663✔
283
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
954,663!
284
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
954,663!
285
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
954,663!
286
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
954,663!
287
      action.pCont = taosMemoryMalloc(action.contLen);
954,663!
288
      if (action.pCont == NULL) goto _OVER;
954,663!
289
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
954,663!
290
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
954,663!
291
      action.pCont = NULL;
954,663✔
292
    } else {
293
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
294
    }
295
  }
296
  ret = 0;
1,288,988✔
297

298
_OVER:
1,288,988✔
299
  *offset = dataPos;
1,288,988✔
300
  taosMemoryFreeClear(action.pCont);
1,288,988!
301
  return ret;
1,288,988✔
302
}
303

304
SSdbRow *mndTransDecode(SSdbRaw *pRaw) {
322,247✔
305
  terrno = TSDB_CODE_INVALID_MSG;
322,247✔
306
  int32_t  code = 0;
322,247✔
307
  int32_t  lino = 0;
322,247✔
308
  SSdbRow *pRow = NULL;
322,247✔
309
  STrans  *pTrans = NULL;
322,247✔
310
  char    *pData = NULL;
322,247✔
311
  int32_t  dataLen = 0;
322,247✔
312
  int8_t   sver = 0;
322,247✔
313
  int32_t  prepareActionNum = 0;
322,247✔
314
  int32_t  redoActionNum = 0;
322,247✔
315
  int32_t  undoActionNum = 0;
322,247✔
316
  int32_t  commitActionNum = 0;
322,247✔
317
  int32_t  dataPos = 0;
322,247✔
318
  int32_t  arbgroupIdNum = 0;
322,247✔
319

320
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
322,247!
321

322
  if (sver > TRANS_VER2_NUMBER) {
322,247!
323
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
324
    goto _OVER;
×
325
  }
326

327
  pRow = sdbAllocRow(sizeof(STrans));
322,247✔
328
  if (pRow == NULL) goto _OVER;
322,247!
329

330
  pTrans = sdbGetRowObj(pRow);
322,247✔
331
  if (pTrans == NULL) goto _OVER;
322,247!
332

333
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
322,247!
334

335
  int8_t stage = 0;
322,247✔
336
  int8_t policy = 0;
322,247✔
337
  int8_t conflict = 0;
322,247✔
338
  int8_t exec = 0;
322,247✔
339
  int8_t oper = 0;
322,247✔
340
  int8_t reserved = 0;
322,247✔
341
  int8_t actionType = 0;
322,247✔
342
  SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
322,247!
343
  SDB_GET_INT8(pRaw, dataPos, &policy, _OVER)
322,247!
344
  SDB_GET_INT8(pRaw, dataPos, &conflict, _OVER)
322,247!
345
  SDB_GET_INT8(pRaw, dataPos, &exec, _OVER)
322,247!
346
  SDB_GET_INT8(pRaw, dataPos, &oper, _OVER)
322,247!
347
  SDB_GET_INT8(pRaw, dataPos, &reserved, _OVER)
322,247!
348
  pTrans->stage = stage;
322,247✔
349
  pTrans->policy = policy;
322,247✔
350
  pTrans->conflict = conflict;
322,247✔
351
  pTrans->exec = exec;
322,247✔
352
  pTrans->oper = oper;
322,247✔
353
  SDB_GET_INT16(pRaw, dataPos, &pTrans->originRpcType, _OVER)
322,247!
354
  SDB_GET_INT64(pRaw, dataPos, &pTrans->createdTime, _OVER)
322,247!
355
  SDB_GET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
322,247!
356
  SDB_GET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
322,247!
357
  SDB_GET_INT32(pRaw, dataPos, &pTrans->actionPos, _OVER)
322,247!
358

359
  if (sver > TRANS_VER1_NUMBER) {
322,247!
360
    SDB_GET_INT32(pRaw, dataPos, &prepareActionNum, _OVER)
322,247!
361
  }
362
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, _OVER)
322,247!
363
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, _OVER)
322,247!
364
  SDB_GET_INT32(pRaw, dataPos, &commitActionNum, _OVER)
322,247!
365

366
  pTrans->prepareActions = taosArrayInit(prepareActionNum, sizeof(STransAction));
322,247✔
367
  pTrans->redoActions = taosArrayInit(redoActionNum, sizeof(STransAction));
322,247✔
368
  pTrans->undoActions = taosArrayInit(undoActionNum, sizeof(STransAction));
322,247✔
369
  pTrans->commitActions = taosArrayInit(commitActionNum, sizeof(STransAction));
322,247✔
370

371
  if (pTrans->prepareActions == NULL) goto _OVER;
322,247!
372
  if (pTrans->redoActions == NULL) goto _OVER;
322,247!
373
  if (pTrans->undoActions == NULL) goto _OVER;
322,247!
374
  if (pTrans->commitActions == NULL) goto _OVER;
322,247!
375

376
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum) < 0) goto _OVER;
322,247!
377
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum) < 0) goto _OVER;
322,247!
378
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum) < 0) goto _OVER;
322,247!
379
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum) < 0) goto _OVER;
322,247!
380

381
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
322,247!
382
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
322,247!
383
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
322,247!
384
  if (pTrans->paramLen != 0) {
322,247!
385
    pTrans->param = taosMemoryMalloc(pTrans->paramLen);
×
386
    if (pTrans->param == NULL) goto _OVER;
×
387
    SDB_GET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER);
×
388
  }
389

390
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
322,247!
391

392
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
322,247✔
393

394
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
322,247!
395
  for (int32_t i = 0; i < arbgroupIdNum; ++i) {
322,307✔
396
    int32_t arbGroupId = 0;
60✔
397
    SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
60!
398
    if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
60!
399
  }
400

401
  int8_t ableKill = 0;
322,247✔
402
  int8_t killMode = 0;
322,247✔
403
  SDB_GET_INT8(pRaw, dataPos, &ableKill, _OVER)
322,247!
404
  SDB_GET_INT8(pRaw, dataPos, &killMode, _OVER)
322,247!
405
  pTrans->ableToBeKilled = ableKill;
322,247✔
406
  pTrans->killMode = killMode;
322,247✔
407

408
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
322,247!
409

410
  terrno = 0;
322,247✔
411

412
_OVER:
322,247✔
413
  if (terrno != 0 && pTrans != NULL) {
322,247!
414
    mError("trans:%d, failed to parse from raw:%p since %s", pTrans->id, pRaw, terrstr());
×
415
    mndTransDropData(pTrans);
×
416
    taosMemoryFreeClear(pRow);
×
417
    return NULL;
×
418
  }
419

420
  if (pTrans != NULL) {
322,247!
421
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
322,247✔
422
  }
423
  return pRow;
322,247✔
424
}
425

426
static const char *mndTransStr(ETrnStage stage) {
2,474,756✔
427
  switch (stage) {
2,474,756!
428
    case TRN_STAGE_PREPARE:
166,982✔
429
      return "prepare";
166,982✔
430
    case TRN_STAGE_REDO_ACTION:
964,886✔
431
      return "redoAction";
964,886✔
432
    case TRN_STAGE_ROLLBACK:
25✔
433
      return "rollback";
25✔
434
    case TRN_STAGE_UNDO_ACTION:
51,867✔
435
      return "undoAction";
51,867✔
436
    case TRN_STAGE_COMMIT:
281,070✔
437
      return "commit";
281,070✔
438
    case TRN_STAGE_COMMIT_ACTION:
600,171✔
439
      return "commitAction";
600,171✔
440
    case TRN_STAGE_FINISH:
409,736✔
441
      return "finished";
409,736✔
442
    case TRN_STAGE_PRE_FINISH:
19✔
443
      return "pre-finish";
19✔
444
    default:
×
445
      return "invalid";
×
446
  }
447
}
448

449
static const char *mndTransTypeStr(ETrnAct actionType) {
×
450
  switch (actionType) {
×
451
    case TRANS_ACTION_MSG:
×
452
      return "msg";
×
453
    case TRANS_ACTION_RAW:
×
454
      return "sdb";
×
455
    default:
×
456
      return "invalid";
×
457
  }
458
}
459

460
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
752,464✔
461
  if (pAction != NULL) {
752,464✔
462
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
608,823✔
463
      pTrans->lastAction = pAction->id;
402,049✔
464
      pTrans->lastMsgType = pAction->msgType;
402,049✔
465
      pTrans->lastEpset = pAction->epSet;
402,049✔
466
      pTrans->lastErrorNo = pAction->errCode;
402,049✔
467
    }
468
  } else {
469
    pTrans->lastAction = 0;
143,641✔
470
    pTrans->lastMsgType = 0;
143,641✔
471
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
143,641✔
472
    pTrans->lastErrorNo = 0;
143,641✔
473
  }
474
}
752,464✔
475

476
static void mndTransTestStartFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
477
  mInfo("test trans start, param:%s, len:%d", (char *)param, paramLen);
×
478
}
×
479

480
static void mndTransTestStopFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
481
  mInfo("test trans stop, param:%s, len:%d", (char *)param, paramLen);
×
482
}
×
483

484
static TransCbFp mndTransGetCbFp(ETrnFunc ftype) {
2,062✔
485
  switch (ftype) {
2,062!
486
    case TRANS_START_FUNC_TEST:
×
487
      return mndTransTestStartFunc;
×
488
    case TRANS_STOP_FUNC_TEST:
×
489
      return mndTransTestStopFunc;
×
490
    case TRANS_START_FUNC_MQ_REB:
1,031✔
491
      return mndRebCntInc;
1,031✔
492
    case TRANS_STOP_FUNC_MQ_REB:
1,031✔
493
      return mndRebCntDec;
1,031✔
494
    default:
×
495
      return NULL;
×
496
  }
497
}
498

499
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
62,035✔
500
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
62,035!
501
        mndTransStr(pTrans->stage), pTrans->startFunc);
502

503
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
62,035✔
504

505
  if (pTrans->startFunc > 0) {
62,035✔
506
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
1,031✔
507
    if (fp) {
1,031!
508
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,031✔
509
    }
510
    // pTrans->startFunc = 0;
511
  }
512

513
  if (pTrans->stage == TRN_STAGE_COMMIT) {
62,035✔
514
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
5✔
515
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pTrans->id);
5!
516
  }
517

518
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
62,035✔
519
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1✔
520
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
1!
521
  }
522

523
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
62,035!
524
    pTrans->stage = TRN_STAGE_FINISH;
×
525
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pTrans->id);
×
526
  }
527

528
  return 0;
62,035✔
529
}
530

531
void mndTransDropData(STrans *pTrans) {
375,340✔
532
  if (pTrans->prepareActions != NULL) {
375,340!
533
    mndTransDropActions(pTrans->prepareActions);
375,340✔
534
    pTrans->prepareActions = NULL;
375,340✔
535
  }
536
  if (pTrans->redoActions != NULL) {
375,340!
537
    mndTransDropActions(pTrans->redoActions);
375,340✔
538
    pTrans->redoActions = NULL;
375,340✔
539
  }
540
  if (pTrans->undoActions != NULL) {
375,340!
541
    mndTransDropActions(pTrans->undoActions);
375,340✔
542
    pTrans->undoActions = NULL;
375,340✔
543
  }
544
  if (pTrans->commitActions != NULL) {
375,340!
545
    mndTransDropActions(pTrans->commitActions);
375,340✔
546
    pTrans->commitActions = NULL;
375,340✔
547
  }
548
  if (pTrans->arbGroupIds != NULL) {
375,340!
549
    taosHashCleanup(pTrans->arbGroupIds);
375,340✔
550
  }
551
  if (pTrans->pRpcArray != NULL) {
375,340✔
552
    taosArrayDestroy(pTrans->pRpcArray);
53,093✔
553
    pTrans->pRpcArray = NULL;
53,093✔
554
  }
555
  if (pTrans->rpcRsp != NULL) {
375,340✔
556
    taosMemoryFree(pTrans->rpcRsp);
14,933!
557
    pTrans->rpcRsp = NULL;
14,933✔
558
    pTrans->rpcRspLen = 0;
14,933✔
559
  }
560
  if (pTrans->param != NULL) {
375,340!
561
    taosMemoryFree(pTrans->param);
×
562
    pTrans->param = NULL;
×
563
    pTrans->paramLen = 0;
×
564
  }
565
  (void)taosThreadMutexDestroy(&pTrans->mutex);
375,340✔
566
}
375,340✔
567

568
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc) {
192,110✔
569
  mInfo("trans:%d, perform delete action, row:%p stage:%s callfunc:%d, stopFunc:%d", pTrans->id, pTrans,
192,110!
570
        mndTransStr(pTrans->stage), callFunc, pTrans->stopFunc);
571

572
  if (pTrans->stopFunc > 0 && callFunc) {
192,110✔
573
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
1,031✔
574
    if (fp) {
1,031!
575
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,031✔
576
    }
577
    // pTrans->stopFunc = 0;
578
  }
579

580
  mndTransDropData(pTrans);
192,110✔
581
  return 0;
192,110✔
582
}
583

584
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
273,112✔
585
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
847,973✔
586
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
574,861✔
587
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
574,861✔
588
    pOldAction->rawWritten = pNewAction->rawWritten;
574,861✔
589
    pOldAction->msgSent = pNewAction->msgSent;
574,861✔
590
    pOldAction->msgReceived = pNewAction->msgReceived;
574,861✔
591
    pOldAction->errCode = pNewAction->errCode;
574,861✔
592
  }
593
}
273,112✔
594

595
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) {
68,278✔
596
  mInfo("trans:%d, perform update action, old row:%p stage:%s create:%" PRId64 ", new row:%p stage:%s create:%" PRId64,
68,278!
597
        pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage), pNew->createdTime);
598

599
  if (pOld->createdTime != pNew->createdTime) {
68,278!
600
    mError("trans:%d, failed to perform update action since createTime not match, old row:%p stage:%s create:%" PRId64
×
601
           ", new row:%p stage:%s create:%" PRId64,
602
           pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage),
603
           pNew->createdTime);
604
    // only occured while sync timeout
605
    TAOS_RETURN(TSDB_CODE_MND_TRANS_SYNC_TIMEOUT);
×
606
  }
607

608
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
68,278✔
609
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
68,278✔
610
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
68,278✔
611
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
68,278✔
612
  pOld->stage = pNew->stage;
68,278✔
613
  pOld->actionPos = pNew->actionPos;
68,278✔
614

615
  if (pOld->stage == TRN_STAGE_COMMIT) {
68,278✔
616
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
61,790✔
617
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
61,790!
618
  }
619

620
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
68,278✔
621
    pOld->stage = TRN_STAGE_UNDO_ACTION;
5✔
622
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pNew->id);
5!
623
  }
624

625
  if (pOld->stage == TRN_STAGE_PRE_FINISH) {
68,278✔
626
    pOld->stage = TRN_STAGE_FINISH;
5✔
627
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pNew->id);
5!
628
  }
629

630
  return 0;
68,278✔
631
}
632

633
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
295,318✔
634
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
295,318✔
635
  if (pTrans == NULL) {
295,318✔
636
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
5,314✔
637
  }
638
  return pTrans;
295,318✔
639
}
640

641
void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
290,005✔
642
  SSdb *pSdb = pMnode->pSdb;
290,005✔
643
  if (pTrans != NULL) mInfo("vgId:1, trans:%d, release transaction", pTrans->id);
290,005!
644
  sdbRelease(pSdb, pTrans);
290,005✔
645
}
290,005✔
646

647
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
53,093✔
648
                       const char *opername) {
649
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
53,093!
650
  if (pTrans == NULL) {
53,093!
651
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
652
    mError("failed to create transaction since %s", terrstr());
×
653
    return NULL;
×
654
  }
655

656
  if (opername != NULL) {
53,093!
657
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
53,093✔
658
  }
659

660
  int32_t sdbMaxId = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
53,093✔
661
  sdbReadLock(pMnode->pSdb, SDB_TRANS);
53,093✔
662
  pTrans->id = TMAX(sdbMaxId, tsMaxTransId + 1);
53,093✔
663
  sdbUnLock(pMnode->pSdb, SDB_TRANS);
53,093✔
664
  pTrans->stage = TRN_STAGE_PREPARE;
53,093✔
665
  pTrans->policy = policy;
53,093✔
666
  pTrans->conflict = conflict;
53,093✔
667
  pTrans->exec = TRN_EXEC_PARALLEL;
53,093✔
668
  pTrans->ableToBeKilled = false;
53,093✔
669
  pTrans->createdTime = taosGetTimestampMs();
53,093✔
670
  pTrans->prepareActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
53,093✔
671
  pTrans->redoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
53,093✔
672
  pTrans->undoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
53,093✔
673
  pTrans->commitActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
53,093✔
674
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
53,093✔
675
  pTrans->pRpcArray = taosArrayInit(1, sizeof(SRpcHandleInfo));
53,093✔
676
  pTrans->mTraceId = pReq ? TRACE_GET_ROOTID(&pReq->info.traceId) : tGenIdPI64();
53,093✔
677
  taosInitRWLatch(&pTrans->lockRpcArray);
53,093✔
678
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
53,093✔
679

680
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
53,093!
681
      pTrans->pRpcArray == NULL) {
53,093!
682
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
683
    mError("failed to create transaction since %s", terrstr());
×
684
    mndTransDrop(pTrans);
×
685
    return NULL;
×
686
  }
687

688
  if (pReq != NULL) {
53,093✔
689
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
62,010!
690
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
691
      return NULL;
×
692
    }
693
    pTrans->originRpcType = pReq->msgType;
31,005✔
694
  }
695

696
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
53,093!
697

698
  mTrace("trans:%d, local object is created, data:%p", pTrans->id, pTrans);
53,093✔
699
  return pTrans;
53,093✔
700
}
701

702
static void mndTransDropActions(SArray *pArray) {
1,501,360✔
703
  int32_t size = taosArrayGetSize(pArray);
1,501,360✔
704
  for (int32_t i = 0; i < size; ++i) {
4,436,865✔
705
    STransAction *pAction = taosArrayGet(pArray, i);
2,935,505✔
706
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,935,505✔
707
      taosMemoryFreeClear(pAction->pRaw);
1,848,485!
708
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,087,020!
709
      taosMemoryFreeClear(pAction->pCont);
1,087,020!
710
    } else {
711
      // nothing
712
    }
713
  }
714

715
  taosArrayDestroy(pArray);
1,501,360✔
716
}
1,501,360✔
717

718
void mndTransDrop(STrans *pTrans) {
54,303✔
719
  if (pTrans != NULL) {
54,303✔
720
    mndTransDropData(pTrans);
53,093✔
721
    mTrace("trans:%d, local object is freed, data:%p", pTrans->id, pTrans);
53,093✔
722
    taosMemoryFreeClear(pTrans);
53,093!
723
  }
724
}
54,303✔
725

726
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
405,208✔
727
  pAction->id = taosArrayGetSize(pArray);
405,208✔
728

729
  void *ptr = taosArrayPush(pArray, pAction);
405,208✔
730
  if (ptr == NULL) {
405,208!
731
    TAOS_RETURN(terrno);
×
732
  }
733

734
  return 0;
405,208✔
735
}
736

737
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
1,443✔
738
  STransAction action = {
1,443✔
739
      .stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw, .mTraceId = pTrans->mTraceId};
1,443✔
740
  return mndTransAppendAction(pTrans->redoActions, &action);
1,443✔
741
}
742

743
int32_t mndTransAppendNullLog(STrans *pTrans) {
×
744
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_NULL};
×
745
  return mndTransAppendAction(pTrans->redoActions, &action);
×
746
}
747

748
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
15,547✔
749
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
15,547✔
750
  return mndTransAppendAction(pTrans->undoActions, &action);
15,547✔
751
}
752

753
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
232,148✔
754
  STransAction action = {.stage = TRN_STAGE_COMMIT_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
232,148✔
755
  return mndTransAppendAction(pTrans->commitActions, &action);
232,148✔
756
}
757

758
int32_t mndTransAppendPrepareLog(STrans *pTrans, SSdbRaw *pRaw) {
23,713✔
759
  STransAction action = {
23,713✔
760
      .pRaw = pRaw, .stage = TRN_STAGE_PREPARE, .actionType = TRANS_ACTION_RAW, .mTraceId = pTrans->mTraceId};
23,713✔
761
  return mndTransAppendAction(pTrans->prepareActions, &action);
23,713✔
762
}
763

764
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
96,169✔
765
  pAction->stage = TRN_STAGE_REDO_ACTION;
96,169✔
766
  pAction->actionType = TRANS_ACTION_MSG;
96,169✔
767
  pAction->mTraceId = pTrans->mTraceId;
96,169✔
768
  return mndTransAppendAction(pTrans->redoActions, pAction);
96,169✔
769
}
770

771
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
36,188✔
772
  pAction->stage = TRN_STAGE_UNDO_ACTION;
36,188✔
773
  pAction->actionType = TRANS_ACTION_MSG;
36,188✔
774
  return mndTransAppendAction(pTrans->undoActions, pAction);
36,188✔
775
}
776

777
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
14,933✔
778
  pTrans->rpcRsp = pCont;
14,933✔
779
  pTrans->rpcRspLen = contLen;
14,933✔
780
}
14,933✔
781

782
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
1,021✔
783
  pTrans->startFunc = startFunc;
1,021✔
784
  pTrans->stopFunc = stopFunc;
1,021✔
785
  pTrans->param = param;
1,021✔
786
  pTrans->paramLen = paramLen;
1,021✔
787
}
1,021✔
788

789
int32_t mndSetRpcInfoForDbTrans(SMnode *pMnode, SRpcMsg *pMsg, EOperType oper, const char *dbname) {
×
790
  STrans *pTrans = NULL;
×
791
  void   *pIter = NULL;
×
792
  int32_t code = -1;
×
793

794
  while (1) {
795
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
×
796
    if (pIter == NULL) break;
×
797

798
    if (pTrans->oper == oper) {
×
799
      if (taosStrcasecmp(dbname, pTrans->dbname) == 0) {
×
800
        mInfo("trans:%d, db:%s oper:%d matched with input", pTrans->id, dbname, oper);
×
801
        taosWLockLatch(&pTrans->lockRpcArray);
×
802
        if (pTrans->pRpcArray == NULL) {
×
803
          pTrans->pRpcArray = taosArrayInit(4, sizeof(SRpcHandleInfo));
×
804
        }
805
        if (pTrans->pRpcArray != NULL && taosArrayPush(pTrans->pRpcArray, &pMsg->info) != NULL) {
×
806
          code = 0;
×
807
        }
808
        taosWUnLockLatch(&pTrans->lockRpcArray);
×
809

810
        sdbRelease(pMnode->pSdb, pTrans);
×
811
        break;
×
812
      }
813
    }
814

815
    sdbRelease(pMnode->pSdb, pTrans);
×
816
  }
817
  return code;
×
818
}
819

820
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
33,277✔
821
  if (dbname != NULL) {
33,277!
822
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
33,277✔
823
  }
824
  if (stbname != NULL) {
33,277✔
825
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
24,956✔
826
  }
827
}
33,277✔
828

829
void mndTransAddArbGroupId(STrans *pTrans, int32_t groupId) {
12✔
830
  if (taosHashPut(pTrans->arbGroupIds, &groupId, sizeof(int32_t), NULL, 0) != 0) {
12!
831
    mError("trans:%d, failed to put groupid into hash, groupId:%d", pTrans->id, groupId);
×
832
  }
833
}
12✔
834

835
void mndTransSetSerial(STrans *pTrans) { pTrans->exec = TRN_EXEC_SERIAL; }
1,494✔
836

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

839
void mndTransSetKillMode(STrans *pTrans, ETrnKillMode killMode) {
8✔
840
  pTrans->ableToBeKilled = true; 
8✔
841
  pTrans->killMode = killMode; 
8✔
842
}
8✔
843

844
void mndTransSetParallel(STrans *pTrans) { pTrans->exec = TRN_EXEC_PARALLEL; }
×
845

846
void mndTransSetChangeless(STrans *pTrans) { pTrans->changeless = true; }
36✔
847

848
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
4,696✔
849

850
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
110,427✔
851
  int32_t  code = 0;
110,427✔
852
  SSdbRaw *pRaw = mndTransEncode(pTrans);
110,427✔
853
  if (pRaw == NULL) {
110,427!
854
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
855
    if (terrno != 0) code = terrno;
×
856
    mError("trans:%d, failed to encode while sync trans since %s", pTrans->id, tstrerror(code));
×
857
    TAOS_RETURN(code);
×
858
  }
859
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_READY));
110,427!
860

861
  mInfo("trans:%d, sync to other mnodes, stage:%s createTime:%" PRId64, pTrans->id, mndTransStr(pTrans->stage),
110,427!
862
        pTrans->createdTime);
863
  code = mndSyncPropose(pMnode, pRaw, pTrans->id);
110,427✔
864
  if (code != 0) {
110,427✔
865
    mError("trans:%d, failed to sync, errno:%s code:0x%x createTime:%" PRId64 " saved trans:%d", pTrans->id,
23!
866
           tstrerror(code), code, pTrans->createdTime, pMnode->syncMgmt.transId);
867
    sdbFreeRaw(pRaw);
23✔
868
    TAOS_RETURN(code);
23✔
869
  }
870

871
  sdbFreeRaw(pRaw);
110,404✔
872
  mInfo("trans:%d, sync finished, createTime:%" PRId64, pTrans->id, pTrans->createdTime);
110,404!
873
  TAOS_RETURN(code);
110,404✔
874
}
875

876
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
7,487✔
877
  if (conflict[0] == 0) return false;
7,487!
878
  if (taosStrcasecmp(conflict, pTrans->dbname) == 0) return true;
7,487✔
879
  return false;
7,154✔
880
}
881

882
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
26,819✔
883
  if (conflict[0] == 0) return false;
26,819✔
884
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
24,402✔
885
  return false;
24,320✔
886
}
887

888
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
34,319✔
889
  if (conflict) {
34,319✔
890
    mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with trans:%d db:%s stb:%s type:%d", pNew->id,
415!
891
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
892
    *globalConflict = true;
415✔
893
  } else {
894
    mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with trans:%d db:%s stb:%s type:%d", pNew->id, pNew->dbname,
33,904!
895
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
896
  }
897
}
34,319✔
898

899
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
148,202✔
900
  STrans *pTrans = NULL;
148,202✔
901
  void   *pIter = NULL;
148,202✔
902
  bool    conflict = false;
148,202✔
903

904
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
148,202✔
905

906
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
94,941✔
907
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
94,941!
908

909
  while (1) {
910
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
122,645✔
911
    if (pIter == NULL) break;
122,645✔
912

913
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
27,704✔
914

915
    if (pNew->conflict == TRN_CONFLICT_DB) {
27,704✔
916
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
4,589✔
917
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4,589✔
918
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
4,031✔
919
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
4,031✔
920
      }
921
    }
922

923
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
27,704✔
924
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
23,091✔
925
      if (pTrans->conflict == TRN_CONFLICT_DB) {
23,091✔
926
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
3,456✔
927
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
3,456✔
928
      }
929
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
23,091✔
930
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
19,332✔
931
      }
932
    }
933

934
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
27,704!
935
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
×
936
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
×
937
        void *pGidIter = taosHashIterate(pNew->arbGroupIds, NULL);
×
938
        while (pGidIter != NULL) {
×
939
          int32_t groupId = *(int32_t *)pGidIter;
×
940
          if (taosHashGet(pTrans->arbGroupIds, &groupId, sizeof(int32_t)) != NULL) {
×
941
            taosHashCancelIterate(pNew->arbGroupIds, pGidIter);
×
942
            mndTransLogConflict(pNew, pTrans, true, &conflict);
×
943
            break;
×
944
          } else {
945
            mndTransLogConflict(pNew, pTrans, false, &conflict);
×
946
          }
947
          pGidIter = taosHashIterate(pNew->arbGroupIds, pGidIter);
×
948
        }
949
      }
950
    }
951

952
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
27,704✔
953
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
13!
954
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
955
      } else {
956
        mndTransLogConflict(pNew, pTrans, false, &conflict);
13✔
957
      }
958
    }
959

960
    sdbRelease(pMnode->pSdb, pTrans);
27,704✔
961
  }
962

963
  return conflict;
94,941✔
964
}
965

966
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
148,202✔
967
  int32_t code = 0;
148,202✔
968
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
148,202✔
969
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
84,806!
970
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
971
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
972
      TAOS_RETURN(code);
×
973
    }
974
  }
975

976
  if (mndCheckTransConflict(pMnode, pTrans)) {
148,202✔
977
    code = TSDB_CODE_MND_TRANS_CONFLICT;
560✔
978
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
560!
979
    TAOS_RETURN(code);
560✔
980
  }
981

982
  TAOS_RETURN(code);
147,642✔
983
}
984

985
int32_t mndTransCheckConflictWithCompact(SMnode *pMnode, STrans *pTrans) {
271✔
986
  int32_t      code = 0;
271✔
987
  void        *pIter = NULL;
271✔
988
  bool         conflict = false;
271✔
989
  SCompactObj *pCompact = NULL;
271✔
990

991
  while (1) {
4✔
992
    bool thisConflict = false;
275✔
993
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
275✔
994
    if (pIter == NULL) break;
275✔
995

996
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
4✔
997
      thisConflict = true;
2✔
998
    }
999
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4!
1000
      if (taosStrcasecmp(pTrans->dbname, pCompact->dbname) == 0) thisConflict = true;
2!
1001
    }
1002

1003
    if (thisConflict) {
4!
1004
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
4!
1005
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1006
      conflict = true;
4✔
1007
    } else {
1008
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with compact:%d db:%s", pTrans->id, pTrans->dbname,
×
1009
            pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1010
    }
1011
    sdbRelease(pMnode->pSdb, pCompact);
4✔
1012
  }
1013

1014
  if (conflict) {
271✔
1015
    code = TSDB_CODE_MND_TRANS_CONFLICT_COMPACT;
4✔
1016
    mError("trans:%d, failed to check tran conflict with compact since %s", pTrans->id, tstrerror(code));
4!
1017
    TAOS_RETURN(code);
4✔
1018
  }
1019

1020
  TAOS_RETURN(code);
267✔
1021
}
1022

1023
static bool mndTransActionsOfSameType(SArray *pActions) {
121,883✔
1024
  int32_t size = taosArrayGetSize(pActions);
121,883✔
1025
  ETrnAct lastActType = TRANS_ACTION_NULL;
121,883✔
1026
  bool    same = true;
121,883✔
1027
  for (int32_t i = 0; i < size; ++i) {
466,621✔
1028
    STransAction *pAction = taosArrayGet(pActions, i);
344,738✔
1029
    if (i > 0) {
344,738✔
1030
      if (lastActType != pAction->actionType) {
254,596!
1031
        same = false;
×
1032
        break;
×
1033
      }
1034
    }
1035
    lastActType = pAction->actionType;
344,738✔
1036
  }
1037
  return same;
121,883✔
1038
}
1039

1040
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
52,493✔
1041
  int32_t code = 0;
52,493✔
1042
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
52,493✔
1043
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
51,090!
1044
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1045
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1046
      TAOS_RETURN(code);
×
1047
    }
1048

1049
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
51,090✔
1050
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
18,300!
1051
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1052
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1053
        TAOS_RETURN(code);
×
1054
      }
1055
    }
1056
  }
1057

1058
  TAOS_RETURN(code);
52,493✔
1059
}
1060

1061
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
52,493✔
1062
  int32_t code = 0;
52,493✔
1063
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
52,493!
1064
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
×
1065
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
×
1066
    TAOS_RETURN(code);
×
1067
  }
1068
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
52,493!
1069
    code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1070
    mError("trans:%d, types of commit actions are not the same", pTrans->id);
×
1071
    TAOS_RETURN(code);
×
1072
  }
1073

1074
  TAOS_RETURN(code);
52,493✔
1075
}
1076

1077
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
52,493✔
1078
  int32_t code = 0;
52,493✔
1079
  if (pTrans == NULL) {
52,493!
1080
    return TSDB_CODE_INVALID_PARA;
×
1081
  }
1082

1083
  mInfo("trans:%d, action list:", pTrans->id);
52,493!
1084
  int32_t index = 0;
52,493✔
1085
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
76,183✔
1086
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
23,690✔
1087
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
23,690!
1088
          mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1089
  }
1090

1091
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
149,738✔
1092
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
97,245✔
1093
    mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
97,245!
1094
          mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1095
  }
1096

1097
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
284,607✔
1098
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
232,114✔
1099
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
232,114!
1100
          mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1101
  }
1102

1103
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
104,228✔
1104
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
51,735✔
1105
    if(pAction->actionType == TRANS_ACTION_MSG){
51,735✔
1106
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
36,188!
1107
            mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1108
    }
1109
    else{
1110
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
15,547!
1111
            mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1112
    }
1113
  }
1114

1115

1116
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
52,493!
1117

1118
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
52,493!
1119

1120
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
52,493!
1121

1122
  mInfo("trans:%d, prepare transaction", pTrans->id);
52,493!
1123
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
52,493✔
1124
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
10!
1125
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
10✔
1126
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
10✔
1127
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
10✔
1128
    TAOS_RETURN(code);
10✔
1129
  }
1130
  mInfo("trans:%d, prepare finished", pTrans->id);
52,483!
1131

1132
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
52,483✔
1133
  if (pNew == NULL) {
52,483!
1134
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1135
    if (terrno != 0) code = terrno;
×
1136
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, tstrerror(code));
×
1137
    TAOS_RETURN(code);
×
1138
  }
1139

1140
  pNew->pRpcArray = pTrans->pRpcArray;
52,483✔
1141
  pNew->rpcRsp = pTrans->rpcRsp;
52,483✔
1142
  pNew->rpcRspLen = pTrans->rpcRspLen;
52,483✔
1143
  pNew->mTraceId = pTrans->mTraceId;
52,483✔
1144
  pTrans->pRpcArray = NULL;
52,483✔
1145
  pTrans->rpcRsp = NULL;
52,483✔
1146
  pTrans->rpcRspLen = 0;
52,483✔
1147

1148
  mInfo("trans:%d, execute transaction in prepare", pTrans->id);
52,483!
1149
  mndTransExecute(pMnode, pNew, false);
52,483✔
1150
  mndReleaseTrans(pMnode, pNew);
52,483✔
1151
  // TDOD change to TAOS_RETURN(code);
1152
  return 0;
52,483✔
1153
}
1154

1155
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
52,493✔
1156
  int32_t code = 0;
52,493✔
1157
  mInfo("trans:%d, commit transaction", pTrans->id);
52,493!
1158
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
52,493✔
1159
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
11!
1160
    TAOS_RETURN(code);
11✔
1161
  }
1162
  mInfo("trans:%d, commit finished", pTrans->id);
52,482!
1163
  TAOS_RETURN(code);
52,482✔
1164
}
1165

1166
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
4✔
1167
  int32_t code = 0;
4✔
1168
  mInfo("trans:%d, rollback transaction", pTrans->id);
4!
1169
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
4✔
1170
    mError("trans:%d, failed to rollback since %s", pTrans->id, tstrerror(code));
2!
1171
    TAOS_RETURN(code);
2✔
1172
  }
1173
  mInfo("trans:%d, rollback finished", pTrans->id);
2!
1174
  TAOS_RETURN(code);
2✔
1175
}
1176

1177
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
3✔
1178
  int32_t code = 0;
3✔
1179
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
3!
1180
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
3!
1181
    mError("trans:%d, failed to pre-finish since %s", pTrans->id, tstrerror(code));
×
1182
    TAOS_RETURN(code);
×
1183
  }
1184
  mInfo("trans:%d, pre-finish finished", pTrans->id);
3!
1185
  TAOS_RETURN(code);
3✔
1186
}
1187

1188
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
288,736✔
1189
  bool    sendRsp = false;
288,736✔
1190
  int32_t code = pTrans->code;
288,736✔
1191

1192
  if (pTrans->stage == TRN_STAGE_FINISH) {
288,736✔
1193
    sendRsp = true;
114,284✔
1194
  }
1195

1196
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
288,736✔
1197
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
87,613✔
1198
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
23✔
1199
      sendRsp = true;
23✔
1200
    }
1201
  } else {
1202
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
201,123✔
1203
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
128,591!
1204
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1205
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1206
      } else {
1207
        if (pTrans->failedTimes > 6) sendRsp = true;
128,591✔
1208
      }
1209
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
128,591✔
1210
    }
1211
  }
1212

1213
  if (!sendRsp) {
288,736✔
1214
    return;
174,427✔
1215
  } else {
1216
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
114,309!
1217
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1218
  }
1219

1220
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
114,309!
1221
  taosWLockLatch(&pTrans->lockRpcArray);
114,309✔
1222
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
114,309✔
1223
  if (size <= 0) {
114,309✔
1224
    taosWUnLockLatch(&pTrans->lockRpcArray);
83,932✔
1225
    return;
83,932✔
1226
  }
1227

1228
  for (int32_t i = 0; i < size; ++i) {
60,754✔
1229
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
30,377✔
1230
    if (pInfo->handle != NULL) {
30,377✔
1231
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
28,791!
1232
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1233
      }
1234
      if (code == TSDB_CODE_SYN_TIMEOUT) {
28,791!
1235
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1236
      }
1237

1238
      if (i != 0 && code == 0) {
28,791!
1239
        code = TSDB_CODE_MNODE_NOT_FOUND;
×
1240
      }
1241
      mInfo("vgId:1, trans:%d, client:%d start to send rsp, code:0x%x stage:%s app:%p", pTrans->id, i, code,
28,791!
1242
            mndTransStr(pTrans->stage), pInfo->ahandle);
1243

1244
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
28,791✔
1245

1246
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
28,791✔
1247
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,679!
1248
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,679✔
1249
        if (pDb != NULL) {
4,679!
1250
          for (int32_t j = 0; j < 12; j++) {
5,540✔
1251
            bool ready = mndIsDbReady(pMnode, pDb);
5,535✔
1252
            if (!ready) {
5,535✔
1253
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
861!
1254
              taosMsleep(1000);
861✔
1255
            } else {
1256
              break;
4,674✔
1257
            }
1258
          }
1259
        }
1260
        mndReleaseDb(pMnode, pDb);
4,679✔
1261
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
24,112✔
1262
        void   *pCont = NULL;
8,349✔
1263
        int32_t contLen = 0;
8,349✔
1264
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
8,349✔
1265
          mndTransSetRpcRsp(pTrans, pCont, contLen);
8,347✔
1266
        }
1267
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
15,763✔
1268
        void   *pCont = NULL;
101✔
1269
        int32_t contLen = 0;
101✔
1270
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
101!
1271
          mndTransSetRpcRsp(pTrans, pCont, contLen);
101✔
1272
        }
1273
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
15,662✔
1274
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
24✔
1275
        if (code != 0) {
24!
NEW
1276
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1277
        }
1278
      }
1279

1280
      if (pTrans->rpcRspLen != 0) {
28,791✔
1281
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
14,931✔
1282
        if (rpcCont != NULL) {
14,931!
1283
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
14,931✔
1284
          rspMsg.pCont = rpcCont;
14,931✔
1285
          rspMsg.contLen = pTrans->rpcRspLen;
14,931✔
1286
        }
1287
      }
1288

1289
      tmsgSendRsp(&rspMsg);
28,791✔
1290

1291
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
28,791!
1292
            mndTransStr(pTrans->stage), pInfo->ahandle);
1293
    }
1294
  }
1295
  taosArrayClear(pTrans->pRpcArray);
30,377✔
1296
  taosWUnLockLatch(&pTrans->lockRpcArray);
30,377✔
1297
}
1298

1299
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
100,816✔
1300
  int32_t code = 0;
100,816✔
1301
  SMnode *pMnode = pRsp->info.node;
100,816✔
1302
#ifndef TD_ASTRA_32
1303
  int64_t signature = (int64_t)(pRsp->info.ahandle);
100,816✔
1304
  int32_t transId = (int32_t)(signature >> 32);
100,816✔
1305
  int32_t action = (int32_t)((signature << 32) >> 32);
100,816✔
1306
#else
1307
  int32_t transId = (int32_t)(pRsp->info.ahandle);
1308
  int32_t action = (int32_t)(pRsp->info.ahandleEx);
1309
#endif
1310
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
100,816✔
1311
  if (pTrans == NULL) {
100,816!
UNCOV
1312
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
1313
    if (terrno != 0) code = terrno;
×
UNCOV
1314
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
UNCOV
1315
    goto _OVER;
×
1316
  }
1317

1318
  SArray *pArray = NULL;
100,816✔
1319
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
100,816✔
1320
    pArray = pTrans->redoActions;
100,804✔
1321
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
12!
1322
    pArray = pTrans->undoActions;
12✔
1323
  } else {
UNCOV
1324
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
UNCOV
1325
    goto _OVER;
×
1326
  }
1327

1328
  if (pArray == NULL) {
100,816!
1329
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1330
    goto _OVER;
×
1331
  }
1332

1333
  int32_t actionNum = taosArrayGetSize(pArray);
100,816✔
1334
  if (action < 0 || action >= actionNum) {
100,816!
1335
    mError("trans:%d, invalid action:%d", transId, action);
×
UNCOV
1336
    goto _OVER;
×
1337
  }
1338

1339
  STransAction *pAction = taosArrayGet(pArray, action);
100,816✔
1340
  if (pAction != NULL) {
100,816!
1341
    pAction->msgReceived = 1;
100,816✔
1342
    pAction->errCode = pRsp->code;
100,816✔
1343
    pAction->endTime = taosGetTimestampMs();
100,816✔
1344

1345
    // pTrans->lastErrorNo = pRsp->code;
1346
    mndSetTransLastAction(pTrans, pAction);
100,816✔
1347

1348
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
100,816!
1349
          mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1350
          tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1351
  } else {
UNCOV
1352
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1353
  }
1354

1355
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
100,816!
1356
  mndTransExecute(pMnode, pTrans, true);
100,816✔
1357

1358
_OVER:
100,816✔
1359
  mndReleaseTrans(pMnode, pTrans);
100,816✔
1360
  TAOS_RETURN(code);
100,816✔
1361
}
1362

1363
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
4,818✔
1364
  pAction->rawWritten = 0;
4,818✔
1365
  pAction->msgSent = 0;
4,818✔
1366
  pAction->msgReceived = 0;
4,818✔
1367
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
4,818!
1368
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
4,818✔
1369
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
12✔
1370
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
12!
1371
          pAction->id, pAction->epSet.inUse);
1372
  } else {
1373
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,806!
1374
  }
1375
  pAction->errCode = 0;
4,818✔
1376
}
4,818✔
1377

1378
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
2✔
1379
  int32_t numOfActions = taosArrayGetSize(pArray);
2✔
1380

1381
  for (int32_t action = 0; action < numOfActions; ++action) {
10✔
1382
    STransAction *pAction = taosArrayGet(pArray, action);
8✔
1383
    if (pAction->msgSent && pAction->msgReceived &&
8!
1384
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
8!
1385
      continue;
6✔
1386
    if (pAction->msgSent && !pAction->msgReceived) {
2!
UNCOV
1387
      int64_t timestamp = taosGetTimestampMs();
×
UNCOV
1388
      if (timestamp - pAction->startTime <= TRANS_ACTION_TIMEOUT) continue;
×
1389
    }
1390

1391
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
2!
1392

1393
    mndTransResetAction(pMnode, pTrans, pAction);
2✔
1394
    mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage), pAction->id,
2!
1395
          pAction->errCode, pAction->startTime);
1396
  }
1397
}
2✔
1398

1399
// execute in sync context
1400
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
517,831✔
1401
  if (pAction->rawWritten) return 0;
517,831✔
1402
  if (topHalf) {
284,413✔
1403
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
11✔
1404
  }
1405

1406
  if (pAction->pRaw->type >= SDB_MAX) {
284,402!
UNCOV
1407
    pAction->rawWritten = true;
×
UNCOV
1408
    pAction->errCode = 0;
×
UNCOV
1409
    mndSetTransLastAction(pTrans, pAction);
×
UNCOV
1410
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
UNCOV
1411
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1412
  }
1413

1414
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
284,402✔
1415
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
284,402!
1416
    pAction->rawWritten = true;
284,402✔
1417
    pAction->errCode = 0;
284,402✔
1418
    code = 0;
284,402✔
1419
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
284,402!
1420
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1421

1422
    mndSetTransLastAction(pTrans, pAction);
284,402✔
1423
  } else {
UNCOV
1424
    pAction->errCode = (terrno != 0) ? terrno : code;
×
UNCOV
1425
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1426
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
UNCOV
1427
    mndSetTransLastAction(pTrans, pAction);
×
1428
  }
1429

1430
  TAOS_RETURN(code);
284,402✔
1431
}
1432

1433
// execute in trans context
1434
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
640,910✔
1435
                                     bool notSend) {
1436
  if (pAction->msgSent) return 0;
640,910✔
1437
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
141,610✔
1438
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
40,538✔
1439
  }
1440

1441
  if (notSend) {
101,072✔
1442
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
189!
1443
    return 0;
189✔
1444
  }
1445

1446
#ifndef TD_ASTRA_32
1447
  int64_t signature = pTrans->id;
100,883✔
1448
  signature = (signature << 32);
100,883✔
1449
  signature += pAction->id;
100,883✔
1450

1451
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
100,883✔
1452
#else
1453
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1454
                    .contLen = pAction->contLen,
1455
                    .info.ahandle = (void *)pTrans->id,
1456
                    .info.ahandleEx = (void *)pAction->id};
1457
#endif
1458
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
100,883✔
1459
  if (rpcMsg.pCont == NULL) {
100,883!
UNCOV
1460
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1461
    return -1;
×
1462
  }
1463
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
100,883✔
1464
  rpcMsg.info.notFreeAhandle = 1;
100,883✔
1465

1466
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
100,883✔
1467

1468
  char    detail[1024] = {0};
100,883✔
1469
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
100,883!
1470
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
100,883✔
1471
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
207,633✔
1472
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
106,750✔
1473
                     pAction->epSet.eps[i].port);
106,750✔
1474
  }
1475

1476
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
100,883✔
1477
  if (code == 0) {
100,883✔
1478
    pAction->msgSent = 1;
100,881✔
1479
    // pAction->msgReceived = 0;
1480
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
100,881✔
1481
    pAction->startTime = taosGetTimestampMs();
100,881✔
1482
    pAction->endTime = 0;
100,881✔
1483
    mInfo("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
100,881!
1484

1485
    mndSetTransLastAction(pTrans, pAction);
100,881✔
1486
  } else {
1487
    pAction->msgSent = 0;
2✔
1488
    pAction->msgReceived = 0;
2✔
1489
    pAction->errCode = (terrno != 0) ? terrno : code;
2!
1490
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
2!
1491
           detail);
1492

1493
    mndSetTransLastAction(pTrans, pAction);
2✔
1494
  }
1495

1496
  TAOS_RETURN(code);
100,883✔
1497
}
1498

UNCOV
1499
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
UNCOV
1500
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
UNCOV
1501
  pAction->rawWritten = 0;
×
UNCOV
1502
  pAction->errCode = 0;
×
UNCOV
1503
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1504

1505
  mndSetTransLastAction(pTrans, pAction);
×
1506
  return 0;
×
1507
}
1508

1509
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
1,158,741✔
1510
                                        bool notSend) {
1511
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,158,741✔
1512
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
517,831✔
1513
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
640,910!
1514
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
640,910✔
1515
  } else {
UNCOV
1516
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1517
  }
1518
}
1519

1520
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
273,317✔
1521
  int32_t numOfActions = taosArrayGetSize(pArray);
273,317✔
1522
  int32_t code = 0;
273,317✔
1523

1524
  for (int32_t action = 0; action < numOfActions; ++action) {
1,338,820✔
1525
    STransAction *pAction = taosArrayGet(pArray, action);
1,101,128✔
1526
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
1,101,128✔
1527
    if (code != 0) {
1,101,128✔
1528
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
35,625!
1529
            numOfActions);
1530
      break;
35,625✔
1531
    }
1532
  }
1533

1534
  return code;
273,317✔
1535
}
1536

1537
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
319,649✔
1538
  int32_t numOfActions = taosArrayGetSize(pArray);
319,649✔
1539
  int32_t code = 0;
319,649✔
1540
  if (numOfActions == 0) return 0;
319,649✔
1541

1542
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
273,317✔
1543
    return code;
35,625✔
1544
  }
1545

1546
  int32_t       numOfExecuted = 0;
237,692✔
1547
  int32_t       errCode = 0;
237,692✔
1548
  STransAction *pErrAction = NULL;
237,692✔
1549
  for (int32_t action = 0; action < numOfActions; ++action) {
1,303,195✔
1550
    STransAction *pAction = taosArrayGet(pArray, action);
1,065,503✔
1551
    if (pAction->msgReceived || pAction->rawWritten) {
1,065,503✔
1552
      numOfExecuted++;
775,957✔
1553
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
775,957✔
1554
        errCode = pAction->errCode;
28✔
1555
        pErrAction = pAction;
28✔
1556
      }
1557
    } else {
1558
      pErrAction = pAction;
289,546✔
1559
    }
1560
  }
1561

1562
  mndSetTransLastAction(pTrans, pErrAction);
237,692✔
1563

1564
  if (numOfExecuted == numOfActions) {
237,692✔
1565
    if (errCode == 0) {
143,643✔
1566
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
143,641!
1567
      return 0;
143,641✔
1568
    } else {
1569
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
2!
1570
      mndTransResetActions(pMnode, pTrans, pArray);
2✔
1571
      terrno = errCode;
2✔
1572
      return errCode;
2✔
1573
    }
1574
  } else {
1575
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
94,049!
1576

1577
    for (int32_t action = 0; action < numOfActions; ++action) {
581,856✔
1578
      STransAction *pAction = taosArrayGet(pArray, action);
487,807✔
1579
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
487,807✔
1580
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1581
             pAction->acceptableCode, pAction->retryCode);
1582
      if (pAction->msgSent) {
487,807✔
1583
        bool reset = false;
487,618✔
1584
        if (pAction->msgReceived) {
487,618✔
1585
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
198,261✔
1586
        } else {
1587
          int64_t timestamp = taosGetTimestampMs();
289,357✔
1588
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
289,357!
1589
        }
1590
        if (reset) {
487,618✔
1591
          mndTransResetAction(pMnode, pTrans, pAction);
26✔
1592
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
26!
1593
                pAction->id, pAction->errCode, pAction->startTime);
1594
        }
1595
      }
1596
    }
1597
    return TSDB_CODE_ACTION_IN_PROGRESS;
94,049✔
1598
  }
1599
}
1600

1601
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
205,348✔
1602
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
205,348✔
1603
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
205,348✔
1604
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
4!
1605
           mndStrExecutionContext(topHalf));
1606
  }
1607
  return code;
205,348✔
1608
}
1609

1610
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
24✔
1611
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf, notSend);
24✔
1612
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
24!
UNCOV
1613
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1614
           mndStrExecutionContext(topHalf));
1615
  }
1616
  return code;
24✔
1617
}
1618

1619
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
114,277✔
1620
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
114,277✔
1621
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
114,277!
UNCOV
1622
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1623
           mndStrExecutionContext(topHalf));
1624
  }
1625
  return code;
114,277✔
1626
}
1627

1628
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
21,566✔
1629
  int32_t code = 0;
21,566✔
1630
  int32_t numOfActions = taosArrayGetSize(pActions);
21,566✔
1631
  if (numOfActions == 0) return code;
21,566✔
1632

1633
  if (pTrans->actionPos >= numOfActions) {
21,564✔
1634
    return code;
1,709✔
1635
  }
1636

1637
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
19,855!
1638
        pTrans->actionPos, mndTransStr(pTrans->stage));
1639

1640
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
30,072✔
1641
    STransAction *pAction = taosArrayGet(pActions, action);
28,671✔
1642

1643
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", 
28,671!
1644
          pTrans->id, pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent,
1645
          pAction->msgReceived);
1646

1647
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
28,671✔
1648
    if (code == 0) {
28,671✔
1649
      if (pAction->msgSent) {
23,745✔
1650
        bool reset = false;
20,767✔
1651
        if (pAction->msgReceived) {
20,767✔
1652
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
8,906!
1653
            code = pAction->errCode;
4,790✔
1654
            reset = true;
4,790✔
1655
          } else {
1656
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
4,116!
1657
          }
1658
        } else {
1659
          int64_t timestamp = taosGetTimestampMs();
11,861✔
1660
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
11,861!
1661
          code = TSDB_CODE_ACTION_IN_PROGRESS;
11,861✔
1662
        }
1663
        if (reset) {
20,767✔
1664
          mndTransResetAction(pMnode, pTrans, pAction);
4,790✔
1665
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
4,790!
1666
                pAction->id, pAction->errCode, pAction->startTime);
1667
        }
1668
      } else if (pAction->rawWritten) {
2,978!
1669
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
2,978!
UNCOV
1670
          code = pAction->errCode;
×
1671
        } else {
1672
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
2,978!
1673
        }
1674
      } else {
1675
      }
1676
    }
1677

1678
    if (code == 0) {
28,671✔
1679
      pTrans->failedTimes = 0;
7,094✔
1680
    }
1681
    mndSetTransLastAction(pTrans, pAction);
28,671✔
1682

1683
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
28,671✔
1684
      pTrans->lastErrorNo = code;
6,586✔
1685
      pTrans->code = code;
6,586✔
1686
      mInfo("trans:%d, %s:%d, cannot execute next action in %s, code:%s", pTrans->id, mndTransStr(pAction->stage),
6,586!
1687
            action, mndStrExecutionContext(topHalf), tstrerror(code));
1688
      break;
6,586✔
1689
    }
1690

1691
    if (code == 0) {
22,085✔
1692
      pTrans->code = 0;
5,434✔
1693
      pTrans->actionPos++;
5,434✔
1694
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
5,434!
1695
            pAction->id);
1696
      (void)taosThreadMutexUnlock(&pTrans->mutex);
5,434✔
1697
      code = mndTransSync(pMnode, pTrans);
5,434✔
1698
      (void)taosThreadMutexLock(&pTrans->mutex);
5,434✔
1699
      if (code != 0) {
5,434!
UNCOV
1700
        pTrans->actionPos--;
×
UNCOV
1701
        pTrans->code = terrno;
×
UNCOV
1702
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1703
               mndTransStr(pAction->stage), pAction->id, terrstr());
UNCOV
1704
        break;
×
1705
      }
1706
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
16,651✔
1707
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
11,861!
1708
      break;
11,861✔
1709
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,790✔
1710
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
19✔
1711
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,783!
1712
            code, tstrerror(code));
1713
      pTrans->lastErrorNo = code;
4,783✔
1714
      taosMsleep(300);
4,783✔
1715
      action--;
4,783✔
1716
      continue;
4,783✔
1717
    } else {
1718
      terrno = code;
7✔
1719
      pTrans->lastErrorNo = code;
7✔
1720
      pTrans->code = code;
7✔
1721
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
7!
1722
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1723
      break;
7✔
1724
    }
1725
  }
1726

1727
  return code;
19,855✔
1728
}
1729

1730
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
21,566✔
1731
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
21,566✔
1732
  (void)taosThreadMutexLock(&pTrans->mutex);
21,566✔
1733
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
21,566!
1734
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
21,566✔
1735
  }
1736
  (void)taosThreadMutexUnlock(&pTrans->mutex);
21,566✔
1737
  return code;
21,566✔
1738
}
1739

UNCOV
1740
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
UNCOV
1741
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
UNCOV
1742
  (void)taosThreadMutexLock(&pTrans->mutex);
×
UNCOV
1743
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
UNCOV
1744
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
1745
  }
1746
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
1747
  return code;
×
1748
}
1749

1750
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
61,857✔
1751
  bool    continueExec = true;
61,857✔
1752
  int32_t code = 0;
61,857✔
1753
  terrno = 0;
61,857✔
1754

1755
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
61,857✔
1756
  if (numOfActions == 0) goto _OVER;
61,857✔
1757

1758
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
17,870!
1759

1760
  for (int32_t action = 0; action < numOfActions; ++action) {
46,812✔
1761
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
28,942✔
1762
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
28,942✔
1763
    if (code != 0) {
28,942!
UNCOV
1764
      terrno = code;
×
UNCOV
1765
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
1766
             numOfActions, tstrerror(code));
UNCOV
1767
      return false;
×
1768
    }
1769
  }
1770

1771
_OVER:
17,870✔
1772
  pTrans->stage = TRN_STAGE_REDO_ACTION;
61,857✔
1773
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
61,857!
1774
  return continueExec;
61,857✔
1775
}
1776

1777
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
226,914✔
1778
  bool    continueExec = true;
226,914✔
1779
  int32_t code = 0;
226,914✔
1780
  terrno = 0;
226,914✔
1781

1782
  if (pTrans->exec == TRN_EXEC_SERIAL) {
226,914✔
1783
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
21,566✔
1784
  } else {
1785
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
205,348✔
1786
  }
1787

1788
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
226,914!
UNCOV
1789
    pTrans->lastErrorNo = code;
×
UNCOV
1790
    pTrans->code = code;
×
UNCOV
1791
    mInfo(
×
1792
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
1793
        "continueExec:%d, code:%s",
1794
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1795
    taosMsleep(100);
×
1796
    return true;
×
1797
  } else {
1798
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
226,914✔
1799
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
68,514!
1800
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1801
      return false;
68,514✔
1802
    }
1803
  }
1804
  terrno = code;
158,400✔
1805

1806
  if (code == 0) {
158,400✔
1807
    pTrans->code = 0;
52,493✔
1808
    pTrans->stage = TRN_STAGE_COMMIT;
52,493✔
1809
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
52,493!
1810
    continueExec = true;
52,493✔
1811
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
105,907!
1812
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
105,896!
1813
    continueExec = false;
105,896✔
1814
  } else {
1815
    pTrans->failedTimes++;
11✔
1816
    pTrans->code = terrno;
11✔
1817
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
11✔
1818
      if (pTrans->lastAction != 0) {
4✔
1819
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
1820
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
UNCOV
1821
          if (pTrans->failedTimes < 6) {
×
UNCOV
1822
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
1823
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
UNCOV
1824
            taosMsleep(1000);
×
UNCOV
1825
            continueExec = true;
×
1826
            return true;
×
1827
          }
1828
        }
1829
      }
1830

1831
      pTrans->stage = TRN_STAGE_ROLLBACK;
4✔
1832
      pTrans->actionPos = 0;
4✔
1833
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
4!
1834
             pTrans->actionPos);
1835
      continueExec = true;
4✔
1836
    } else {
1837
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
7!
1838
      continueExec = false;
7✔
1839
    }
1840
  }
1841

1842
  return continueExec;
158,400✔
1843
}
1844

1845
// execute in trans context
1846
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
52,496✔
1847
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
52,496✔
1848

1849
  bool    continueExec = true;
52,493✔
1850
  int32_t code = mndTransCommit(pMnode, pTrans);
52,493✔
1851

1852
  if (code == 0) {
52,493✔
1853
    pTrans->code = 0;
52,482✔
1854
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
52,482✔
1855
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
52,482!
1856
    continueExec = true;
52,482✔
1857
  } else {
1858
    pTrans->code = terrno;
11✔
1859
    pTrans->failedTimes++;
11✔
1860
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
11!
1861
    continueExec = false;
11✔
1862
  }
1863

1864
  return continueExec;
52,493✔
1865
}
1866

1867
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
114,277✔
1868
  bool    continueExec = true;
114,277✔
1869
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
114,277✔
1870

1871
  if (code == 0) {
114,277✔
1872
    pTrans->code = 0;
114,274✔
1873
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
114,274✔
1874
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
114,274!
1875
    continueExec = true;
114,274✔
1876
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
3!
1877
    pTrans->code = 0;
3✔
1878
    pTrans->stage = TRN_STAGE_COMMIT;
3✔
1879
    mInfo("trans:%d, back to commit stage", pTrans->id);
3!
1880
    continueExec = true;
3✔
1881
  } else {
1882
    pTrans->code = terrno;
×
1883
    pTrans->failedTimes++;
×
1884
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1885
    continueExec = false;
×
1886
  }
1887

1888
  return continueExec;
114,277✔
1889
}
1890

1891
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
24✔
1892
  bool    continueExec = true;
24✔
1893
  int32_t code = 0;
24✔
1894

1895
  if (pTrans->exec == TRN_EXEC_SERIAL) {
24!
UNCOV
1896
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
1897
  } else {
1898
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
24✔
1899
  }
1900

1901
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
24✔
1902
  terrno = code;
17✔
1903

1904
  if (code == 0) {
17✔
1905
    pTrans->stage = TRN_STAGE_PRE_FINISH;
3✔
1906
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
3!
1907
    continueExec = true;
3✔
1908
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
14!
1909
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
14!
1910
    continueExec = false;
14✔
1911
  } else {
UNCOV
1912
    pTrans->failedTimes++;
×
UNCOV
1913
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
1914
    continueExec = false;
×
1915
  }
1916

1917
  return continueExec;
17✔
1918
}
1919

1920
// in trans context
1921
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
4✔
1922
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
4!
1923

1924
  bool    continueExec = true;
4✔
1925
  int32_t code = mndTransRollback(pMnode, pTrans);
4✔
1926

1927
  if (code == 0) {
4✔
1928
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
2✔
1929
    continueExec = true;
2✔
1930
  } else {
1931
    pTrans->failedTimes++;
2✔
1932
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
2!
1933
    continueExec = false;
2✔
1934
  }
1935

1936
  return continueExec;
4✔
1937
}
1938

1939
// excute in trans context
1940
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
3✔
1941
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
3!
1942

1943
  bool    continueExec = true;
3✔
1944
  int32_t code = mndTransPreFinish(pMnode, pTrans);
3✔
1945

1946
  if (code == 0) {
3!
1947
    pTrans->stage = TRN_STAGE_FINISH;
3✔
1948
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
3!
1949
    continueExec = true;
3✔
1950
  } else {
UNCOV
1951
    pTrans->failedTimes++;
×
UNCOV
1952
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
1953
    continueExec = false;
×
1954
  }
1955

1956
  return continueExec;
3✔
1957
}
1958

1959
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
114,282✔
1960
  bool continueExec = false;
114,282✔
1961
  if (topHalf) return continueExec;
114,282✔
1962

1963
  SSdbRaw *pRaw = mndTransEncode(pTrans);
61,797✔
1964
  if (pRaw == NULL) {
61,797!
UNCOV
1965
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
UNCOV
1966
    return false;
×
1967
  }
1968
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
61,797!
1969

1970
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
61,797✔
1971
  if (code != 0) {
61,797!
UNCOV
1972
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
1973
  }
1974

1975
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
61,797!
1976
        pTrans->failedTimes, pTrans->createdTime);
1977
  return continueExec;
61,797✔
1978
}
1979

1980
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
288,736✔
1981
  bool continueExec = true;
288,736✔
1982

1983
  while (continueExec) {
796,736✔
1984
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
508,000!
1985
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1986
    pTrans->lastExecTime = taosGetTimestampMs();
508,000✔
1987
    switch (pTrans->stage) {
508,000!
UNCOV
1988
      case TRN_STAGE_PREPARE:
×
UNCOV
1989
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
UNCOV
1990
        break;
×
1991
      case TRN_STAGE_REDO_ACTION:
226,914✔
1992
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
226,914✔
1993
        break;
226,914✔
1994
      case TRN_STAGE_COMMIT:
52,496✔
1995
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
52,496✔
1996
        break;
52,496✔
1997
      case TRN_STAGE_COMMIT_ACTION:
114,277✔
1998
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
114,277✔
1999
        break;
114,277✔
2000
      case TRN_STAGE_ROLLBACK:
4✔
2001
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
4✔
2002
        break;
4✔
2003
      case TRN_STAGE_UNDO_ACTION:
24✔
2004
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
24✔
2005
        break;
24✔
2006
      case TRN_STAGE_PRE_FINISH:
3✔
2007
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
3✔
2008
        break;
3✔
2009
      case TRN_STAGE_FINISH:
114,282✔
2010
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
114,282✔
2011
        break;
114,282✔
UNCOV
2012
      default:
×
UNCOV
2013
        continueExec = false;
×
UNCOV
2014
        break;
×
2015
    }
2016
  }
2017

2018
  mndTransSendRpcRsp(pMnode, pTrans);
288,736✔
2019
}
288,736✔
2020

2021
// start trans, pullup, receive rsp, kill
2022
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
158,599✔
2023
  bool topHalf = true;
158,599✔
2024
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
158,599✔
2025
}
158,599✔
2026

2027
// update trans
2028
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
130,137✔
2029
  bool topHalf = false;
130,137✔
2030
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
130,137✔
2031
}
130,137✔
2032

2033
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
50,204✔
2034
  mTrace("start to process trans timer");
50,204✔
2035
  mndTransPullup(pReq->info.node);
50,204✔
2036
  return 0;
50,204✔
2037
}
2038

UNCOV
2039
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
UNCOV
2040
  SArray *pArray = NULL;
×
UNCOV
2041
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
UNCOV
2042
    pArray = pTrans->redoActions;
×
UNCOV
2043
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2044
    pArray = pTrans->undoActions;
×
2045
  } else {
2046
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2047
  }
2048

2049
  if(pTrans->ableToBeKilled == false){
×
UNCOV
2050
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2051
  }
2052
  
UNCOV
2053
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
×
2054
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
2055
      STransAction *pAction = taosArrayGet(pArray, i);
×
UNCOV
2056
      mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
2057
            mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
2058
      pAction->msgSent = 1;
×
2059
      pAction->msgReceived = 1;
×
2060
      pAction->errCode = 0;
×
2061
    }
2062
  }
2063
  else if(pTrans->killMode == TRN_KILL_MODE_INTERUPT){
×
2064
    pTrans->stage = TRN_STAGE_PRE_FINISH;
×
2065
  }
2066
  else{
UNCOV
2067
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2068
  }
2069

UNCOV
2070
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
×
UNCOV
2071
  mndTransExecute(pMnode, pTrans, true);
×
2072
  return 0;
×
2073
}
2074

2075
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1✔
2076
  SMnode       *pMnode = pReq->info.node;
1✔
2077
  SKillTransReq killReq = {0};
1✔
2078
  int32_t       code = -1;
1✔
2079
  STrans       *pTrans = NULL;
1✔
2080

2081
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
UNCOV
2082
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2083
    goto _OVER;
×
2084
  }
2085

2086
  mInfo("trans:%d, start to kill", killReq.transId);
1!
2087
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1!
2088
    goto _OVER;
1✔
2089
  }
2090

UNCOV
2091
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
UNCOV
2092
  if (pTrans == NULL) {
×
UNCOV
2093
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
UNCOV
2094
    if (terrno != 0) code = terrno;
×
UNCOV
2095
    goto _OVER;
×
2096
  }
2097

2098
  code = mndKillTrans(pMnode, pTrans);
×
2099

2100
_OVER:
1✔
2101
  if (code != 0) {
1!
2102
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1!
2103
  }
2104

2105
  mndReleaseTrans(pMnode, pTrans);
1✔
2106
  TAOS_RETURN(code);
1✔
2107
}
2108

2109
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
222✔
2110

2111
void mndTransPullup(SMnode *pMnode) {
50,759✔
2112
  SSdb   *pSdb = pMnode->pSdb;
50,759✔
2113
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
50,759✔
2114
  if (pArray == NULL) return;
50,759!
2115

2116
  void *pIter = NULL;
50,759✔
2117
  while (1) {
5,300✔
2118
    STrans *pTrans = NULL;
56,059✔
2119
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
56,059✔
2120
    if (pIter == NULL) break;
56,059✔
2121
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
10,600!
UNCOV
2122
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2123
    }
2124
    sdbRelease(pSdb, pTrans);
5,300✔
2125
  }
2126

2127
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
50,759✔
2128

2129
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
56,059✔
2130
    int32_t *pTransId = taosArrayGet(pArray, i);
5,300✔
2131
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
5,300✔
2132
    if (pTrans != NULL) {
5,300!
2133
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
5,300!
2134
      mndTransExecute(pMnode, pTrans, false);
5,300✔
2135
    }
2136
    mndReleaseTrans(pMnode, pTrans);
5,300✔
2137
  }
2138
  taosArrayDestroy(pArray);
50,759✔
2139
}
2140

2141
static char *formatTimestamp(char *buf, int64_t val, int precision) {
13,788✔
2142
  time_t tt;
2143
  if (precision == TSDB_TIME_PRECISION_MICRO) {
13,788!
UNCOV
2144
    tt = (time_t)(val / 1000000);
×
2145
  }
2146
  if (precision == TSDB_TIME_PRECISION_NANO) {
13,788!
UNCOV
2147
    tt = (time_t)(val / 1000000000);
×
2148
  } else {
2149
    tt = (time_t)(val / 1000);
13,788✔
2150
  }
2151

2152
  struct tm tm;
2153
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
13,788!
UNCOV
2154
    mError("failed to get local time");
×
UNCOV
2155
    return NULL;
×
2156
  }
2157
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
13,788✔
2158

2159
  if (precision == TSDB_TIME_PRECISION_MICRO) {
13,788!
2160
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2161
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
13,788!
UNCOV
2162
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2163
  } else {
2164
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
13,788✔
2165
  }
2166

2167
  return buf;
13,788✔
2168
}
2169

2170
static void mndTransLogAction(STrans *pTrans) {
184✔
2171
  char    detail[512] = {0};
184✔
2172
  int32_t len = 0;
184✔
2173
  int32_t index = 0;
184✔
2174

2175
  if (pTrans->stage == TRN_STAGE_PREPARE) {
184!
UNCOV
2176
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
UNCOV
2177
      len = 0;
×
UNCOV
2178
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
UNCOV
2179
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
UNCOV
2180
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2181
                      sdbStatusName(pAction->pRaw->status));
×
2182
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2183
    }
2184
  }
2185

2186
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
184!
2187
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
8,708✔
2188
      len = 0;
8,524✔
2189
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
8,524✔
2190
      if (pAction->actionType == TRANS_ACTION_MSG) {
8,524✔
2191
        char bufStart[40] = {0};
6,894✔
2192
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
6,894✔
2193

2194
        char endStart[40] = {0};
6,894✔
2195
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
6,894✔
2196
        len += snprintf(detail + len, sizeof(detail) - len,
13,788!
2197
                        "action:%d, %s:%d msgType:%s,"
2198
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2199
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
6,894✔
2200
                        pAction->msgReceived, bufStart, endStart);
6,894✔
2201

2202
        SEpSet epset = pAction->epSet;
6,894✔
2203
        if (epset.numOfEps > 0) {
6,894!
2204
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
6,894✔
2205
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
16,002✔
2206
            len +=
9,108✔
2207
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
9,108✔
2208
          }
2209
        }
2210

2211
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
6,894✔
2212
                        tstrerror(pAction->errCode));
2213
      } else {
2214
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
1,630✔
2215
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,630✔
2216
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,630✔
2217
      }
2218
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
8,524!
2219
    }
2220
  }
2221

2222
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
184!
UNCOV
2223
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
×
UNCOV
2224
      len = 0;
×
UNCOV
2225
      STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
×
UNCOV
2226
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
UNCOV
2227
                      mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type),
×
2228
                      sdbStatusName(pAction->pRaw->status));
×
2229
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2230
    }
2231

2232
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
×
2233
      len = 0;
×
2234
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
UNCOV
2235
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
UNCOV
2236
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
2237
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2238
        ;
2239
      } else {
2240
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2241
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2242
                        sdbStatusName(pAction->pRaw->status));
×
2243
      }
UNCOV
2244
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2245
    }
2246
  }
2247
}
184✔
2248

2249
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
267✔
2250
  SMnode *pMnode = pReq->info.node;
267✔
2251
  SSdb   *pSdb = pMnode->pSdb;
267✔
2252
  int32_t numOfRows = 0;
267✔
2253
  STrans *pTrans = NULL;
267✔
2254
  int32_t cols = 0;
267✔
2255
  int32_t code = 0;
267✔
2256
  int32_t lino = 0;
267✔
2257

2258
  while (numOfRows < rows) {
451!
2259
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
451✔
2260
    if (pShow->pIter == NULL) break;
451✔
2261

2262
    cols = 0;
184✔
2263

2264
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2265
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
184!
2266

2267
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2268
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
184!
2269
                        _OVER);
2270

2271
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
184✔
2272
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
184✔
2273
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2274
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
184!
2275

2276
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
184✔
2277
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
184✔
2278
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2279
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
184!
2280

2281
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
184✔
2282
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
184✔
2283
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2284
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
184!
2285

2286
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
184✔
2287
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
184✔
2288
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2289
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
184!
2290

2291
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
184!
2292
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
184✔
2293
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
184✔
2294
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2295
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
184!
2296

2297
    /*
2298
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2299
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2300
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2301
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2302
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2303
    */
2304

2305
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2306
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
184!
2307
                        _OVER);
2308

2309
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2310
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
184!
2311
                        _OVER);
2312

2313
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
184✔
2314
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
184✔
2315
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
552✔
2316
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
184✔
2317
    SEpSet  epset = pTrans->lastEpset;
184✔
2318
    if (epset.numOfEps > 0) {
184!
2319
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
368!
2320
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
368✔
2321
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
509✔
2322
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
325✔
2323
      }
2324
    }
2325
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
184✔
2326
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
184✔
2327
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
184!
2328

2329
    mndTransLogAction(pTrans);
184✔
2330

2331
    numOfRows++;
184✔
2332
    sdbRelease(pSdb, pTrans);
184✔
2333
  }
2334

UNCOV
2335
_OVER:
×
2336
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
267!
2337
  pShow->numOfRows += numOfRows;
267✔
2338
  return numOfRows;
267✔
2339
}
2340

UNCOV
2341
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
×
2342
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
UNCOV
2343
  int32_t code = 0;
×
UNCOV
2344
  int32_t lino = 0;
×
UNCOV
2345
  int32_t len = 0;
×
2346

UNCOV
2347
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
2348
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&transactionId, false), &lino, _OVER);
×
2349

2350
  char action[30 + 1] = {0};
×
UNCOV
2351
  if (curActionId == pAction->id) {
×
2352
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
×
2353
                    mndTransTypeStr(pAction->actionType));
2354
  } else {
2355
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
×
2356
                    mndTransTypeStr(pAction->actionType));
2357
  }
UNCOV
2358
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2359
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
×
2360
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
UNCOV
2361
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
×
UNCOV
2362
_OVER:
×
2363
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2364
  return code;
×
2365
}
2366

2367
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
×
2368
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2369
  int32_t code = 0;
×
UNCOV
2370
  int32_t lino = 0;
×
UNCOV
2371
  int32_t len = 0;
×
2372
  int32_t cols = 0;
×
2373

2374
  cols = 0;
×
2375

2376
  if (mndShowTransCommonColumns(pShow, pBlock, pAction, transactionId, curActionId, numOfRows, &cols) != 0) return;
×
2377

UNCOV
2378
  if (pAction->actionType == TRANS_ACTION_MSG) {
×
2379
    int32_t len = 0;
×
2380

2381
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
UNCOV
2382
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
×
2383
                    pAction->msgSent, pAction->msgReceived);
×
2384
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2385
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2386
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2387
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2388

2389
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2390
    len = 0;
×
2391
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
×
2392
                    tstrerror(pAction->errCode));
UNCOV
2393
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2394
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2395
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2396
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2397

2398
    char target[TSDB_TRANS_TARGET_LEN] = {0};
×
2399
    len = 0;
×
2400
    SEpSet epset = pAction->epSet;
×
2401
    if (epset.numOfEps > 0) {
×
UNCOV
2402
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
×
2403
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
×
2404
      }
2405
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
×
2406
    }
2407
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2408
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
2409
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2410
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2411

2412
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2413
    len = 0;
×
2414
    char bufStart[40] = {0};
×
2415
    if (pAction->startTime > 0) (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
×
UNCOV
2416
    char bufEnd[40] = {0};
×
2417
    if (pAction->endTime > 0) (void)formatTimestamp(bufEnd, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
×
2418
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
×
2419
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2420
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2421
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2422
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2423

2424
  } else {
2425
    int32_t len = 0;
×
2426

2427
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
UNCOV
2428
    if (pAction->pRaw->type == SDB_VGROUP) {
×
UNCOV
2429
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
×
2430
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
×
UNCOV
2431
      len += snprintf(objType + len, sizeof(objType) - len, "%s(%d)", sdbTableName(pAction->pRaw->type), pVgroup->vgId);
×
2432
      taosMemoryFreeClear(pRow);
×
2433
    } else {
2434
      strcpy(objType, sdbTableName(pAction->pRaw->type));
×
2435
    }
2436
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2437
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
UNCOV
2438
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2439
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2440

2441
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2442
    len = 0;
×
2443
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
×
2444
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2445
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2446
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2447
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2448

2449
    char target[TSDB_TRANS_TARGET_LEN] = "";
×
2450
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2451
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2452
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
2453
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2454

2455
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2456
    len = 0;
×
2457
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
×
2458
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
2459
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2460
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2461
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2462
  }
2463

2464
_OVER:
×
2465
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2466
}
2467

UNCOV
2468
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
×
2469
  if (stage == TRN_STAGE_PREPARE) {
×
2470
    return pTrans->prepareActions;
×
2471
  }
UNCOV
2472
  if (stage == TRN_STAGE_REDO_ACTION) {
×
2473
    return pTrans->redoActions;
×
2474
  }
2475
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
UNCOV
2476
    return pTrans->commitActions;
×
2477
  }
2478
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
UNCOV
2479
    return pTrans->undoActions;
×
2480
  }
2481
  return NULL;
×
2482
}
2483

2484
typedef struct STransDetailIter {
2485
  void     *pIter;
2486
  STrans   *pTrans;
2487
  ETrnStage stage;
2488
  int32_t   num;
2489
} STransDetailIter;
2490

UNCOV
2491
static void mndTransShowActions(SSdb *pSdb, STransDetailIter *pShowIter, SShowObj *pShow, SSDataBlock *pBlock,
×
2492
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
UNCOV
2493
  int32_t actionNum = taosArrayGetSize(pActions);
×
UNCOV
2494
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
×
2495

2496
  for (int32_t i = start; i < actionNum; ++i) {
×
UNCOV
2497
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
×
2498
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
×
2499
    (*numOfRows)++;
×
UNCOV
2500
    if (*numOfRows >= rows) break;
×
2501
  }
2502

2503
  if (*numOfRows == end) {
×
2504
    sdbRelease(pSdb, pShowIter->pTrans);
×
2505
    pShowIter->pTrans = NULL;
×
UNCOV
2506
    pShowIter->num = 0;
×
2507
  } else {
2508
    pShowIter->pTrans = pShowIter->pTrans;
×
2509
    pShowIter->stage = pShowIter->pTrans->stage;
×
2510
    pShowIter->num += (*numOfRows);
×
2511
  }
UNCOV
2512
}
×
2513

2514
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2515
  SMnode *pMnode = pReq->info.node;
×
UNCOV
2516
  SSdb   *pSdb = pMnode->pSdb;
×
2517
  int32_t numOfRows = 0;
×
2518

2519
  int32_t code = 0;
×
2520
  int32_t lino = 0;
×
2521

2522
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
×
2523
        pShow->pIter);
2524

2525
  if (pShow->pIter == NULL) {
×
UNCOV
2526
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
×
2527
    if (pShow->pIter == NULL) {
×
UNCOV
2528
      mError("failed to malloc for pShow->pIter");
×
UNCOV
2529
      return 0;
×
2530
    }
2531
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
×
2532
  }
2533

2534
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
×
2535

2536
  while (numOfRows < rows) {
×
UNCOV
2537
    if (pShowIter->pTrans == NULL) {
×
UNCOV
2538
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
×
2539
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
×
UNCOV
2540
      if (pShowIter->pIter == NULL) break;
×
2541
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
×
2542
            pShowIter->pTrans->stage, pShowIter->num);
2543

2544
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
×
2545

2546
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
×
UNCOV
2547
      break;
×
2548
    } else {
2549
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
×
2550
            pShowIter->num);
2551
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
×
2552

UNCOV
2553
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
×
2554
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
×
UNCOV
2555
      break;
×
2556
    }
2557
  }
2558

2559
_OVER:
×
2560
  pShow->numOfRows += numOfRows;
×
2561

UNCOV
2562
  if (code != 0) {
×
UNCOV
2563
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2564
  } else {
2565
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
×
2566
  }
2567
  if (numOfRows == 0) {
×
2568
    taosMemoryFree(pShow->pIter);
×
UNCOV
2569
    pShow->pIter = NULL;
×
2570
  }
UNCOV
2571
  return numOfRows;
×
2572
}
2573

2574
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
UNCOV
2575
  SSdb *pSdb = pMnode->pSdb;
×
2576
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
UNCOV
2577
}
×
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