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

taosdata / TDengine / #4120

17 May 2025 06:43AM UTC coverage: 63.022% (+0.2%) from 62.797%
#4120

push

travis-ci

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

merge: from main to 3.0 branch

157536 of 318088 branches covered (49.53%)

Branch coverage included in aggregate %.

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

1933 existing lines in 69 files now uncovered.

242799 of 317143 relevant lines covered (76.56%)

18674429.06 hits per line

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

63.61
/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; }
605,975✔
59

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

67
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
649,947✔
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,196✔
80
  SSdbTable table = {
2,196✔
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,196✔
91
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
2,196✔
92

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

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

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

105
  for (int32_t i = 0; i < actionNum; ++i) {
2,255,549✔
106
    STransAction *pAction = taosArrayGet(pArray, i);
1,482,661✔
107
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,482,661✔
108
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
937,247✔
109
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
545,414!
110
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
545,414✔
111
    } else {
112
      // empty
113
    }
114
    rawDataLen += sizeof(int8_t);
1,482,661✔
115
  }
116

117
  return rawDataLen;
772,888✔
118
}
119

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

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

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

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

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

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

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

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

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

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

208
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
193,222!
209
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
193,222!
210
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
193,222!
211
  if (pTrans->param != NULL) {
193,222!
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)
193,222!
216

217
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
193,222✔
218
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
193,222!
219
  void *pIter = NULL;
193,222✔
220
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
193,222✔
221
  while (pIter) {
193,258✔
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) {
193,222!
228
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
193,222!
229
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
193,222!
230
  }
231

232
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
193,222!
233
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
193,222!
234

235
  terrno = 0;
193,222✔
236

237
_OVER:
193,222✔
238
  if (terrno != 0) {
193,222!
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);
193,222✔
246
  return pRaw;
193,222✔
247
}
248

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

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

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

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

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

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

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

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

333
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
357,640!
334

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

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

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

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

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

381
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
357,640!
382
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
357,640!
383
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
357,640!
384
  if (pTrans->paramLen != 0) {
357,640!
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);
357,640!
391

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

394
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
357,640!
395
  for (int32_t i = 0; i < arbgroupIdNum; ++i) {
357,700✔
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;
357,640✔
402
  int8_t killMode = 0;
357,640✔
403
  SDB_GET_INT8(pRaw, dataPos, &ableKill, _OVER)
357,640!
404
  SDB_GET_INT8(pRaw, dataPos, &killMode, _OVER)
357,640!
405
  pTrans->ableToBeKilled = ableKill;
357,640✔
406
  pTrans->killMode = killMode;
357,640✔
407

408
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
357,640!
409

410
  terrno = 0;
357,640✔
411

412
_OVER:
357,640✔
413
  if (terrno != 0 && pTrans != NULL) {
357,640!
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) {
357,640!
421
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
357,640✔
422
  }
423
  return pRow;
357,640✔
424
}
425

426
static const char *mndTransStr(ETrnStage stage) {
2,709,764✔
427
  switch (stage) {
2,709,764!
428
    case TRN_STAGE_PREPARE:
184,824✔
429
      return "prepare";
184,824✔
430
    case TRN_STAGE_REDO_ACTION:
1,069,159✔
431
      return "redoAction";
1,069,159✔
432
    case TRN_STAGE_ROLLBACK:
25✔
433
      return "rollback";
25✔
434
    case TRN_STAGE_UNDO_ACTION:
55,834✔
435
      return "undoAction";
55,834✔
436
    case TRN_STAGE_COMMIT:
310,130✔
437
      return "commit";
310,130✔
438
    case TRN_STAGE_COMMIT_ACTION:
637,075✔
439
      return "commitAction";
637,075✔
440
    case TRN_STAGE_FINISH:
452,698✔
441
      return "finished";
452,698✔
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) {
821,671✔
461
  if (pAction != NULL) {
821,671✔
462
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
663,219✔
463
      pTrans->lastAction = pAction->id;
434,975✔
464
      pTrans->lastMsgType = pAction->msgType;
434,975✔
465
      pTrans->lastEpset = pAction->epSet;
434,975✔
466
      pTrans->lastErrorNo = pAction->errCode;
434,975✔
467
    }
468
  } else {
469
    pTrans->lastAction = 0;
158,452✔
470
    pTrans->lastMsgType = 0;
158,452✔
471
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
158,452✔
472
    pTrans->lastErrorNo = 0;
158,452✔
473
  }
474
}
821,671✔
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) {
67,846✔
500
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
67,846!
501
        mndTransStr(pTrans->stage), pTrans->startFunc);
502

503
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
67,846✔
504

505
  if (pTrans->startFunc > 0) {
67,846✔
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) {
67,846✔
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) {
67,846✔
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) {
67,846!
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;
67,846✔
529
}
530

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

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

572
  if (pTrans->stopFunc > 0 && callFunc) {
212,712✔
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);
212,712✔
581
  return 0;
212,712✔
582
}
583

584
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
309,032✔
585
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
930,413✔
586
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
621,381✔
587
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
621,381✔
588
    pOldAction->rawWritten = pNewAction->rawWritten;
621,381✔
589
    pOldAction->msgSent = pNewAction->msgSent;
621,381✔
590
    pOldAction->msgReceived = pNewAction->msgReceived;
621,381✔
591
    pOldAction->errCode = pNewAction->errCode;
621,381✔
592
  }
593
}
309,032✔
594

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

599
  if (pOld->createdTime != pNew->createdTime) {
77,258!
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);
77,258✔
609
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
77,258✔
610
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
77,258✔
611
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
77,258✔
612
  pOld->stage = pNew->stage;
77,258✔
613
  pOld->actionPos = pNew->actionPos;
77,258✔
614

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

620
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
77,258✔
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) {
77,258✔
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;
77,258✔
631
}
632

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

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

647
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
58,904✔
648
                       const char *opername) {
649
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
58,904!
650
  if (pTrans == NULL) {
58,904!
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) {
58,904!
657
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
58,904✔
658
  }
659

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

680
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
58,904!
681
      pTrans->pRpcArray == NULL) {
58,904!
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) {
58,904✔
689
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
70,222!
690
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
691
      return NULL;
×
692
    }
693
    pTrans->originRpcType = pReq->msgType;
35,111✔
694
  }
695

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

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

702
static void mndTransDropActions(SArray *pArray) {
1,666,176✔
703
  int32_t size = taosArrayGetSize(pArray);
1,666,176✔
704
  for (int32_t i = 0; i < size; ++i) {
4,815,477✔
705
    STransAction *pAction = taosArrayGet(pArray, i);
3,149,301✔
706
    if (pAction->actionType == TRANS_ACTION_RAW) {
3,149,301✔
707
      taosMemoryFreeClear(pAction->pRaw);
1,966,729!
708
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,182,572!
709
      taosMemoryFreeClear(pAction->pCont);
1,182,572!
710
    } else {
711
      // nothing
712
    }
713
  }
714

715
  taosArrayDestroy(pArray);
1,666,176✔
716
}
1,666,176✔
717

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

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

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

734
  return 0;
435,397✔
735
}
736

737
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
1,475✔
738
  STransAction action = {
1,475✔
739
      .stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw, .mTraceId = pTrans->mTraceId};
1,475✔
740
  return mndTransAppendAction(pTrans->redoActions, &action);
1,475✔
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) {
16,257✔
749
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
16,257✔
750
  return mndTransAppendAction(pTrans->undoActions, &action);
16,257✔
751
}
752

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

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

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

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

777
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
17,000✔
778
  pTrans->rpcRsp = pCont;
17,000✔
779
  pTrans->rpcRspLen = contLen;
17,000✔
780
}
17,000✔
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) {
38,018✔
821
  if (dbname != NULL) {
38,018!
822
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
38,018✔
823
  }
824
  if (stbname != NULL) {
38,018✔
825
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
29,129✔
826
  }
827
}
38,018✔
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; }
3,059✔
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,917✔
849

850
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
125,218✔
851
  int32_t  code = 0;
125,218✔
852
  SSdbRaw *pRaw = mndTransEncode(pTrans);
125,218✔
853
  if (pRaw == NULL) {
125,218!
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));
125,218!
860

861
  mInfo("trans:%d, sync to other mnodes, stage:%s createTime:%" PRId64, pTrans->id, mndTransStr(pTrans->stage),
125,218!
862
        pTrans->createdTime);
863
  code = mndSyncPropose(pMnode, pRaw, pTrans->id);
125,218✔
864
  if (code != 0) {
125,218✔
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);
125,195✔
872
  mInfo("trans:%d, sync finished, createTime:%" PRId64, pTrans->id, pTrans->createdTime);
125,195!
873
  TAOS_RETURN(code);
125,195✔
874
}
875

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

882
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
31,151✔
883
  if (conflict[0] == 0) return false;
31,151✔
884
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
28,083✔
885
  return false;
28,001✔
886
}
887

888
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
42,197✔
889
  if (conflict) {
42,197✔
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,
41,782!
895
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
896
  }
897
}
42,197✔
898

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

904
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
164,570✔
905

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

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

913
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
32,042✔
914

915
    if (pNew->conflict == TRN_CONFLICT_DB) {
32,042✔
916
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
6,620✔
917
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
6,620✔
918
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
6,056✔
919
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
6,056✔
920
      }
921
    }
922

923
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
32,042✔
924
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
25,398✔
925
      if (pTrans->conflict == TRN_CONFLICT_DB) {
25,398✔
926
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
4,977✔
927
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
4,977✔
928
      }
929
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
25,398✔
930
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
20,118✔
931
      }
932
    }
933

934
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
32,042!
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) {
32,042✔
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);
32,042✔
961
  }
962

963
  return conflict;
107,048✔
964
}
965

966
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
164,570✔
967
  int32_t code = 0;
164,570✔
968
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
164,570✔
969
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
96,853!
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)) {
164,570✔
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);
164,010✔
983
}
984

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

991
  while (1) {
4✔
992
    bool thisConflict = false;
414✔
993
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
414✔
994
    if (pIter == NULL) break;
414✔
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) {
410✔
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);
406✔
1021
}
1022

1023
static bool mndTransActionsOfSameType(SArray *pActions) {
134,325✔
1024
  int32_t size = taosArrayGetSize(pActions);
134,325✔
1025
  ETrnAct lastActType = TRANS_ACTION_NULL;
134,325✔
1026
  bool    same = true;
134,325✔
1027
  for (int32_t i = 0; i < size; ++i) {
501,760✔
1028
    STransAction *pAction = taosArrayGet(pActions, i);
367,435✔
1029
    if (i > 0) {
367,435✔
1030
      if (lastActType != pAction->actionType) {
266,929!
1031
        same = false;
×
1032
        break;
×
1033
      }
1034
    }
1035
    lastActType = pAction->actionType;
367,435✔
1036
  }
1037
  return same;
134,325✔
1038
}
1039

1040
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
58,304✔
1041
  int32_t code = 0;
58,304✔
1042
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
58,304✔
1043
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
55,336!
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) {
55,336✔
1050
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
20,685!
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);
58,304✔
1059
}
1060

1061
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
58,304✔
1062
  int32_t code = 0;
58,304✔
1063
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
58,304!
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) {
58,304!
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);
58,304✔
1075
}
1076

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

1083
  mInfo("trans:%d, action list:", pTrans->id);
58,304!
1084
  int32_t index = 0;
58,304✔
1085
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
85,104✔
1086
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
26,800✔
1087
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
26,800!
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) {
166,020✔
1092
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
107,716✔
1093
    mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
107,716!
1094
          mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1095
  }
1096

1097
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
303,059✔
1098
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
244,755✔
1099
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
244,755!
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) {
114,006✔
1104
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
55,702✔
1105
    if(pAction->actionType == TRANS_ACTION_MSG){
55,702✔
1106
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
39,445!
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,
16,257!
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));
58,304!
1117

1118
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
58,304!
1119

1120
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
58,304!
1121

1122
  mInfo("trans:%d, prepare transaction", pTrans->id);
58,304!
1123
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
58,304✔
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);
58,294!
1131

1132
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
58,294✔
1133
  if (pNew == NULL) {
58,294!
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;
58,294✔
1141
  pNew->rpcRsp = pTrans->rpcRsp;
58,294✔
1142
  pNew->rpcRspLen = pTrans->rpcRspLen;
58,294✔
1143
  pNew->mTraceId = pTrans->mTraceId;
58,294✔
1144
  pTrans->pRpcArray = NULL;
58,294✔
1145
  pTrans->rpcRsp = NULL;
58,294✔
1146
  pTrans->rpcRspLen = 0;
58,294✔
1147

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

1155
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
58,304✔
1156
  int32_t code = 0;
58,304✔
1157
  mInfo("trans:%d, commit transaction", pTrans->id);
58,304!
1158
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
58,304✔
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);
58,293!
1163
  TAOS_RETURN(code);
58,293✔
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) {
320,186✔
1189
  bool    sendRsp = false;
320,186✔
1190
  int32_t code = pTrans->code;
320,186✔
1191

1192
  if (pTrans->stage == TRN_STAGE_FINISH) {
320,186✔
1193
    sendRsp = true;
125,906✔
1194
  }
1195

1196
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
320,186✔
1197
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
97,651✔
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) {
222,535✔
1203
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
143,193!
1204
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1205
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1206
      } else {
1207
        if (pTrans->failedTimes > 6) sendRsp = true;
143,193✔
1208
      }
1209
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
143,193✔
1210
    }
1211
  }
1212

1213
  if (!sendRsp) {
320,186✔
1214
    return;
194,255✔
1215
  } else {
1216
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
125,931!
1217
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1218
  }
1219

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

1228
  for (int32_t i = 0; i < size; ++i) {
68,966✔
1229
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
34,483✔
1230
    if (pInfo->handle != NULL) {
34,483✔
1231
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
32,839!
1232
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1233
      }
1234
      if (code == TSDB_CODE_SYN_TIMEOUT) {
32,839!
1235
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1236
      }
1237

1238
      if (i != 0 && code == 0) {
32,839!
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,
32,839!
1242
            mndTransStr(pTrans->stage), pInfo->ahandle);
1243

1244
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
32,839✔
1245

1246
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
32,839✔
1247
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,900!
1248
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,900✔
1249
        if (pDb != NULL) {
4,900!
1250
          for (int32_t j = 0; j < 12; j++) {
5,761✔
1251
            bool ready = mndIsDbReady(pMnode, pDb);
5,756✔
1252
            if (!ready) {
5,756✔
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,895✔
1257
            }
1258
          }
1259
        }
1260
        mndReleaseDb(pMnode, pDb);
4,900✔
1261
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
27,939✔
1262
        void   *pCont = NULL;
9,732✔
1263
        int32_t contLen = 0;
9,732✔
1264
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
9,732✔
1265
          mndTransSetRpcRsp(pTrans, pCont, contLen);
9,730✔
1266
        }
1267
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
18,207✔
1268
        void   *pCont = NULL;
573✔
1269
        int32_t contLen = 0;
573✔
1270
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
573!
1271
          mndTransSetRpcRsp(pTrans, pCont, contLen);
573✔
1272
        }
1273
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
17,634✔
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) {
32,839✔
1281
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
16,998✔
1282
        if (rpcCont != NULL) {
16,998!
1283
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
16,998✔
1284
          rspMsg.pCont = rpcCont;
16,998✔
1285
          rspMsg.contLen = pTrans->rpcRspLen;
16,998✔
1286
        }
1287
      }
1288

1289
      tmsgSendRsp(&rspMsg);
32,839✔
1290

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

1299
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
111,438✔
1300
  int32_t code = 0;
111,438✔
1301
  SMnode *pMnode = pRsp->info.node;
111,438✔
1302
#ifndef TD_ASTRA_32
1303
  int64_t signature = (int64_t)(pRsp->info.ahandle);
111,438✔
1304
  int32_t transId = (int32_t)(signature >> 32);
111,438✔
1305
  int32_t action = (int32_t)((signature << 32) >> 32);
111,438✔
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);
111,438✔
1311
  if (pTrans == NULL) {
111,438!
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;
111,438✔
1319
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
111,438✔
1320
    pArray = pTrans->redoActions;
111,426✔
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) {
111,438!
1329
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1330
    goto _OVER;
×
1331
  }
1332

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

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

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

1348
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
111,438!
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);
111,438!
1356
  mndTransExecute(pMnode, pTrans, true);
111,438✔
1357

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

1363
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
5,001✔
1364
  pAction->rawWritten = 0;
5,001✔
1365
  pAction->msgSent = 0;
5,001✔
1366
  pAction->msgReceived = 0;
5,001✔
1367
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
5,001!
1368
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
5,001✔
1369
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
13✔
1370
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
13!
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,988!
1374
  }
1375
  pAction->errCode = 0;
5,001✔
1376
}
5,001✔
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) {
546,287✔
1401
  if (pAction->rawWritten) return 0;
546,287✔
1402
  if (topHalf) {
300,196✔
1403
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
11✔
1404
  }
1405

1406
  if (pAction->pRaw->type >= SDB_MAX) {
300,185!
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);
300,185✔
1415
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
300,185!
1416
    pAction->rawWritten = true;
300,185✔
1417
    pAction->errCode = 0;
300,185✔
1418
    code = 0;
300,185✔
1419
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
300,185!
1420
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1421

1422
    mndSetTransLastAction(pTrans, pAction);
300,185✔
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);
300,185✔
1431
}
1432

1433
// execute in trans context
1434
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
682,101✔
1435
                                     bool notSend) {
1436
  if (pAction->msgSent) return 0;
682,101✔
1437
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
158,558✔
1438
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
46,864✔
1439
  }
1440

1441
  if (notSend) {
111,694✔
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;
111,505✔
1448
  signature = (signature << 32);
111,505✔
1449
  signature += pAction->id;
111,505✔
1450

1451
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
111,505✔
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);
111,505✔
1459
  if (rpcMsg.pCont == NULL) {
111,505!
UNCOV
1460
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1461
    return -1;
×
1462
  }
1463
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
111,505✔
1464
  rpcMsg.info.notFreeAhandle = 1;
111,505✔
1465

1466
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
111,505✔
1467

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

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

1485
    mndSetTransLastAction(pTrans, pAction);
111,503✔
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);
111,505✔
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,228,388✔
1510
                                        bool notSend) {
1511
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,228,388✔
1512
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
546,287✔
1513
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
682,101!
1514
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
682,101✔
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) {
298,779✔
1521
  int32_t numOfActions = taosArrayGetSize(pArray);
298,779✔
1522
  int32_t code = 0;
298,779✔
1523

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

1534
  return code;
298,779✔
1535
}
1536

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

1542
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
298,779✔
1543
    return code;
38,814✔
1544
  }
1545

1546
  int32_t       numOfExecuted = 0;
259,965✔
1547
  int32_t       errCode = 0;
259,965✔
1548
  STransAction *pErrAction = NULL;
259,965✔
1549
  for (int32_t action = 0; action < numOfActions; ++action) {
1,378,909✔
1550
    STransAction *pAction = taosArrayGet(pArray, action);
1,118,944✔
1551
    if (pAction->msgReceived || pAction->rawWritten) {
1,118,944✔
1552
      numOfExecuted++;
815,356✔
1553
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
815,356✔
1554
        errCode = pAction->errCode;
28✔
1555
        pErrAction = pAction;
28✔
1556
      }
1557
    } else {
1558
      pErrAction = pAction;
303,588✔
1559
    }
1560
  }
1561

1562
  mndSetTransLastAction(pTrans, pErrAction);
259,965✔
1563

1564
  if (numOfExecuted == numOfActions) {
259,965✔
1565
    if (errCode == 0) {
158,454✔
1566
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
158,452!
1567
      return 0;
158,452✔
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);
101,511!
1576

1577
    for (int32_t action = 0; action < numOfActions; ++action) {
610,175✔
1578
      STransAction *pAction = taosArrayGet(pArray, action);
508,664✔
1579
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
508,664✔
1580
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1581
             pAction->acceptableCode, pAction->retryCode);
1582
      if (pAction->msgSent) {
508,664✔
1583
        bool reset = false;
508,475✔
1584
        if (pAction->msgReceived) {
508,475✔
1585
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
205,076✔
1586
        } else {
1587
          int64_t timestamp = taosGetTimestampMs();
303,399✔
1588
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
303,399!
1589
        }
1590
        if (reset) {
508,475✔
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;
101,511✔
1598
  }
1599
}
1600

1601
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
221,302✔
1602
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
221,302✔
1603
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
221,302✔
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;
221,302✔
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) {
125,899✔
1620
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
125,899✔
1621
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
125,899!
UNCOV
1622
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1623
           mndStrExecutionContext(topHalf));
1624
  }
1625
  return code;
125,899✔
1626
}
1627

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

1633
  if (pTrans->actionPos >= numOfActions) {
31,249✔
1634
    return code;
3,274✔
1635
  }
1636

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

1640
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
41,544✔
1641
    STransAction *pAction = taosArrayGet(pActions, action);
38,578✔
1642

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

1647
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
38,578✔
1648
    if (code == 0) {
38,578✔
1649
      if (pAction->msgSent) {
30,515✔
1650
        bool reset = false;
27,473✔
1651
        if (pAction->msgReceived) {
27,473✔
1652
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
12,226!
1653
            code = pAction->errCode;
4,973✔
1654
            reset = true;
4,973✔
1655
          } else {
1656
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
7,253!
1657
          }
1658
        } else {
1659
          int64_t timestamp = taosGetTimestampMs();
15,247✔
1660
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
15,247!
1661
          code = TSDB_CODE_ACTION_IN_PROGRESS;
15,247✔
1662
        }
1663
        if (reset) {
27,473✔
1664
          mndTransResetAction(pMnode, pTrans, pAction);
4,973✔
1665
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
4,973!
1666
                pAction->id, pAction->errCode, pAction->startTime);
1667
        }
1668
      } else if (pAction->rawWritten) {
3,042!
1669
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,042!
UNCOV
1670
          code = pAction->errCode;
×
1671
        } else {
1672
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,042!
1673
        }
1674
      } else {
1675
      }
1676
    }
1677

1678
    if (code == 0) {
38,578✔
1679
      pTrans->failedTimes = 0;
10,295✔
1680
    }
1681
    mndSetTransLastAction(pTrans, pAction);
38,578✔
1682

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

1691
    if (code == 0) {
28,823✔
1692
      pTrans->code = 0;
8,603✔
1693
      pTrans->actionPos++;
8,603✔
1694
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
8,603!
1695
            pAction->id);
1696
      (void)taosThreadMutexUnlock(&pTrans->mutex);
8,603✔
1697
      code = mndTransSync(pMnode, pTrans);
8,603✔
1698
      (void)taosThreadMutexLock(&pTrans->mutex);
8,603✔
1699
      if (code != 0) {
8,603!
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) {
20,220✔
1707
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
15,247!
1708
      break;
15,247✔
1709
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,973✔
1710
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
20✔
1711
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,966!
1712
            code, tstrerror(code));
1713
      pTrans->lastErrorNo = code;
4,966✔
1714
      taosMsleep(300);
4,966✔
1715
      action--;
4,966✔
1716
      continue;
4,966✔
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;
27,975✔
1728
}
1729

1730
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
31,251✔
1731
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
31,251✔
1732
  (void)taosThreadMutexLock(&pTrans->mutex);
31,251✔
1733
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
31,251!
1734
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
31,251✔
1735
  }
1736
  (void)taosThreadMutexUnlock(&pTrans->mutex);
31,251✔
1737
  return code;
31,251✔
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) {
67,668✔
1751
  bool    continueExec = true;
67,668✔
1752
  int32_t code = 0;
67,668✔
1753
  terrno = 0;
67,668✔
1754

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

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

1760
  for (int32_t action = 0; action < numOfActions; ++action) {
52,563✔
1761
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
32,052✔
1762
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
32,052✔
1763
    if (code != 0) {
32,052!
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:
20,511✔
1772
  pTrans->stage = TRN_STAGE_REDO_ACTION;
67,668✔
1773
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
67,668!
1774
  return continueExec;
67,668✔
1775
}
1776

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

1782
  if (pTrans->exec == TRN_EXEC_SERIAL) {
252,553✔
1783
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
31,251✔
1784
  } else {
1785
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
221,302✔
1786
  }
1787

1788
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
252,553!
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)) {
252,553✔
1799
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
77,494!
1800
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1801
      return false;
77,494✔
1802
    }
1803
  }
1804
  terrno = code;
175,059✔
1805

1806
  if (code == 0) {
175,059✔
1807
    pTrans->code = 0;
58,304✔
1808
    pTrans->stage = TRN_STAGE_COMMIT;
58,304✔
1809
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
58,304!
1810
    continueExec = true;
58,304✔
1811
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
116,755!
1812
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
116,744!
1813
    continueExec = false;
116,744✔
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;
175,059✔
1843
}
1844

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

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

1852
  if (code == 0) {
58,304✔
1853
    pTrans->code = 0;
58,293✔
1854
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
58,293✔
1855
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
58,293!
1856
    continueExec = true;
58,293✔
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;
58,304✔
1865
}
1866

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

1871
  if (code == 0) {
125,899✔
1872
    pTrans->code = 0;
125,896✔
1873
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
125,896✔
1874
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
125,896!
1875
    continueExec = true;
125,896✔
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 {
UNCOV
1882
    pTrans->code = terrno;
×
UNCOV
1883
    pTrans->failedTimes++;
×
UNCOV
1884
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
1885
    continueExec = false;
×
1886
  }
1887

1888
  return continueExec;
125,899✔
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) {
125,904✔
1960
  bool continueExec = false;
125,904✔
1961
  if (topHalf) return continueExec;
125,904✔
1962

1963
  SSdbRaw *pRaw = mndTransEncode(pTrans);
67,608✔
1964
  if (pRaw == NULL) {
67,608!
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));
67,608!
1969

1970
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
67,608✔
1971
  if (code != 0) {
67,608!
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,
67,608!
1976
        pTrans->failedTimes, pTrans->createdTime);
1977
  return continueExec;
67,608✔
1978
}
1979

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

1983
  while (continueExec) {
882,880✔
1984
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
562,694!
1985
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1986
    pTrans->lastExecTime = taosGetTimestampMs();
562,694✔
1987
    switch (pTrans->stage) {
562,694!
UNCOV
1988
      case TRN_STAGE_PREPARE:
×
UNCOV
1989
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
UNCOV
1990
        break;
×
1991
      case TRN_STAGE_REDO_ACTION:
252,553✔
1992
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
252,553✔
1993
        break;
252,553✔
1994
      case TRN_STAGE_COMMIT:
58,307✔
1995
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
58,307✔
1996
        break;
58,307✔
1997
      case TRN_STAGE_COMMIT_ACTION:
125,899✔
1998
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
125,899✔
1999
        break;
125,899✔
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:
125,904✔
2010
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
125,904✔
2011
        break;
125,904✔
UNCOV
2012
      default:
×
UNCOV
2013
        continueExec = false;
×
UNCOV
2014
        break;
×
2015
    }
2016
  }
2017

2018
  mndTransSendRpcRsp(pMnode, pTrans);
320,186✔
2019
}
320,186✔
2020

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

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

2033
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
59,557✔
2034
  mTrace("start to process trans timer");
59,557✔
2035
  mndTransPullup(pReq->info.node);
59,557✔
2036
  return 0;
59,557✔
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) {
60,113✔
2112
  SSdb   *pSdb = pMnode->pSdb;
60,113✔
2113
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
60,113✔
2114
  if (pArray == NULL) return;
60,113!
2115

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

2127
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
60,113✔
2128

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

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

2152
  struct tm tm;
2153
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
18,025!
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);
18,066✔
2158

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

2167
  return buf;
18,062✔
2168
}
2169

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

2175
  if (pTrans->stage == TRN_STAGE_PREPARE) {
760!
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) {
760✔
2187
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
11,427✔
2188
      len = 0;
10,671✔
2189
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
10,671✔
2190
      if (pAction->actionType == TRANS_ACTION_MSG) {
10,655✔
2191
        char bufStart[40] = {0};
9,015✔
2192
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
9,015✔
2193

2194
        char endStart[40] = {0};
9,032✔
2195
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
9,032✔
2196
        len += snprintf(detail + len, sizeof(detail) - len,
18,066!
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,
9,033✔
2200
                        pAction->msgReceived, bufStart, endStart);
9,034✔
2201

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

2211
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
9,032✔
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,640✔
2215
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,640✔
2216
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,640✔
2217
      }
2218
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
10,672✔
2219
    }
2220
  }
2221

2222
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
761!
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
}
761✔
2248

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

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

2262
    cols = 0;
760✔
2263

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

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

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

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

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

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

2291
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
757✔
2292
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
757✔
2293
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
757✔
2294
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
757✔
2295
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
757!
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++);
758✔
2306
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
757!
2307
                        _OVER);
2308

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

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

2329
    mndTransLogAction(pTrans);
760✔
2330

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

UNCOV
2335
_OVER:
×
2336
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
7,479!
2337
  pShow->numOfRows += numOfRows;
7,479✔
2338
  return numOfRows;
7,479✔
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