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

taosdata / TDengine / #4106

19 May 2025 07:15AM UTC coverage: 62.857% (-0.2%) from 63.042%
#4106

push

travis-ci

GitHub
Merge pull request #31115 from taosdata/merge/mainto3.0

156749 of 318088 branches covered (49.28%)

Branch coverage included in aggregate %.

242535 of 317143 relevant lines covered (76.47%)

18746393.97 hits per line

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

63.9
/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; }
626,495✔
59

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

67
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
674,862✔
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) {
802,880✔
102
  int32_t actionNum = taosArrayGetSize(pArray);
802,880✔
103
  int32_t rawDataLen = 0;
802,880✔
104

105
  for (int32_t i = 0; i < actionNum; ++i) {
2,314,932✔
106
    STransAction *pAction = taosArrayGet(pArray, i);
1,512,052✔
107
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,512,052✔
108
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
954,466✔
109
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
557,586!
110
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
557,586✔
111
    } else {
112
      // empty
113
    }
114
    rawDataLen += sizeof(int8_t);
1,512,052✔
115
  }
116

117
  return rawDataLen;
802,880✔
118
}
119

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

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

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

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

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

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

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

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

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

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

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

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

232
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
200,720!
233
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
200,720!
234

235
  terrno = 0;
200,720✔
236

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

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

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

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

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

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

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

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

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

333
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
374,115!
334

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

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

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

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

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

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

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

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

408
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
374,115!
409

410
  terrno = 0;
374,115✔
411

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

426
static const char *mndTransStr(ETrnStage stage) {
2,791,903✔
427
  switch (stage) {
2,791,903!
428
    case TRN_STAGE_PREPARE:
193,793✔
429
      return "prepare";
193,793✔
430
    case TRN_STAGE_REDO_ACTION:
1,101,498✔
431
      return "redoAction";
1,101,498✔
432
    case TRN_STAGE_ROLLBACK:
10✔
433
      return "rollback";
10✔
434
    case TRN_STAGE_UNDO_ACTION:
55,849✔
435
      return "undoAction";
55,849✔
436
    case TRN_STAGE_COMMIT:
321,661✔
437
      return "commit";
321,661✔
438
    case TRN_STAGE_COMMIT_ACTION:
648,147✔
439
      return "commitAction";
648,147✔
440
    case TRN_STAGE_FINISH:
470,937✔
441
      return "finished";
470,937✔
442
    case TRN_STAGE_PRE_FINISH:
10✔
443
      return "pre-finish";
10✔
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) {
845,639✔
461
  if (pAction != NULL) {
845,639✔
462
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
681,054✔
463
      pTrans->lastAction = pAction->id;
446,123✔
464
      pTrans->lastMsgType = pAction->msgType;
446,123✔
465
      pTrans->lastEpset = pAction->epSet;
446,123✔
466
      pTrans->lastErrorNo = pAction->errCode;
446,123✔
467
    }
468
  } else {
469
    pTrans->lastAction = 0;
164,585✔
470
    pTrans->lastMsgType = 0;
164,585✔
471
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
164,585✔
472
    pTrans->lastErrorNo = 0;
164,585✔
473
  }
474
}
845,639✔
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,038✔
485
  switch (ftype) {
2,038!
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,019✔
491
      return mndRebCntInc;
1,019✔
492
    case TRANS_STOP_FUNC_MQ_REB:
1,019✔
493
      return mndRebCntDec;
1,019✔
494
    default:
×
495
      return NULL;
×
496
  }
497
}
498

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

503
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
70,825✔
504

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

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

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

523
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
70,825!
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;
70,825✔
529
}
530

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

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

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

580
  mndTransDropData(pTrans);
222,448✔
581
  return 0;
222,448✔
582
}
583

584
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
324,088✔
585
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
959,255✔
586
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
635,167✔
587
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
635,167✔
588
    pOldAction->rawWritten = pNewAction->rawWritten;
635,167✔
589
    pOldAction->msgSent = pNewAction->msgSent;
635,167✔
590
    pOldAction->msgReceived = pNewAction->msgReceived;
635,167✔
591
    pOldAction->errCode = pNewAction->errCode;
635,167✔
592
  }
593
}
324,088✔
594

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

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

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

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

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

630
  return 0;
81,022✔
631
}
632

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

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

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

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

680
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
60,812!
681
      pTrans->pRpcArray == NULL) {
60,812!
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) {
60,812✔
689
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
72,942!
690
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
691
      return NULL;
×
692
    }
693
    pTrans->originRpcType = pReq->msgType;
36,471✔
694
  }
695

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

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

702
static void mndTransDropActions(SArray *pArray) {
1,739,708✔
703
  int32_t size = taosArrayGetSize(pArray);
1,739,708✔
704
  for (int32_t i = 0; i < size; ++i) {
4,953,979✔
705
    STransAction *pAction = taosArrayGet(pArray, i);
3,214,271✔
706
    if (pAction->actionType == TRANS_ACTION_RAW) {
3,214,271✔
707
      taosMemoryFreeClear(pAction->pRaw);
2,006,825!
708
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,207,446!
709
      taosMemoryFreeClear(pAction->pCont);
1,207,446!
710
    } else {
711
      // nothing
712
    }
713
  }
714

715
  taosArrayDestroy(pArray);
1,739,708✔
716
}
1,739,708✔
717

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

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

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

734
  return 0;
443,144✔
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,368✔
749
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
16,368✔
750
  return mndTransAppendAction(pTrans->undoActions, &action);
16,368✔
751
}
752

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

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

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

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

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

782
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
1,007✔
783
  pTrans->startFunc = startFunc;
1,007✔
784
  pTrans->stopFunc = stopFunc;
1,007✔
785
  pTrans->param = param;
1,007✔
786
  pTrans->paramLen = paramLen;
1,007✔
787
}
1,007✔
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) {
39,760✔
821
  if (dbname != NULL) {
39,760!
822
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
39,760✔
823
  }
824
  if (stbname != NULL) {
39,760✔
825
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
30,734✔
826
  }
827
}
39,760✔
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,453✔
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; }
34✔
847

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

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

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

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

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

882
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
30,355✔
883
  if (conflict[0] == 0) return false;
30,355✔
884
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
27,745✔
885
  return false;
27,633✔
886
}
887

888
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
42,463✔
889
  if (conflict) {
42,463✔
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,
401!
891
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
892
    *globalConflict = true;
401✔
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,
42,062!
895
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
896
  }
897
}
42,463✔
898

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

904
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
171,130✔
905

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

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

913
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
31,347✔
914

915
    if (pNew->conflict == TRN_CONFLICT_DB) {
31,347✔
916
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
7,286✔
917
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
7,286✔
918
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
6,645✔
919
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
6,645✔
920
      }
921
    }
922

923
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
31,347✔
924
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
24,048✔
925
      if (pTrans->conflict == TRN_CONFLICT_DB) {
24,048✔
926
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
5,460✔
927
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
5,460✔
928
      }
929
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
24,048✔
930
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
18,250✔
931
      }
932
    }
933

934
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
31,347!
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) {
31,347✔
953
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
3!
954
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
955
      } else {
956
        mndTransLogConflict(pNew, pTrans, false, &conflict);
3✔
957
      }
958
    }
959

960
    sdbRelease(pMnode->pSdb, pTrans);
31,347✔
961
  }
962

963
  return conflict;
112,100✔
964
}
965

966
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
171,130✔
967
  int32_t code = 0;
171,130✔
968
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
171,130✔
969
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
101,914!
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)) {
171,130✔
977
    code = TSDB_CODE_MND_TRANS_CONFLICT;
631✔
978
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
631!
979
    TAOS_RETURN(code);
631✔
980
  }
981

982
  TAOS_RETURN(code);
170,499✔
983
}
984

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

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

1023
static bool mndTransActionsOfSameType(SArray *pActions) {
137,809✔
1024
  int32_t size = taosArrayGetSize(pActions);
137,809✔
1025
  ETrnAct lastActType = TRANS_ACTION_NULL;
137,809✔
1026
  bool    same = true;
137,809✔
1027
  for (int32_t i = 0; i < size; ++i) {
510,578✔
1028
    STransAction *pAction = taosArrayGet(pActions, i);
372,769✔
1029
    if (i > 0) {
372,769✔
1030
      if (lastActType != pAction->actionType) {
269,113!
1031
        same = false;
×
1032
        break;
×
1033
      }
1034
    }
1035
    lastActType = pAction->actionType;
372,769✔
1036
  }
1037
  return same;
137,809✔
1038
}
1039

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

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

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

1083
  mInfo("trans:%d, action list:", pTrans->id);
60,149!
1084
  int32_t index = 0;
60,149✔
1085
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
88,320✔
1086
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
28,171✔
1087
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
28,171!
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) {
171,364✔
1092
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
111,215✔
1093
    mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
111,215!
1094
          mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1095
  }
1096

1097
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
307,674✔
1098
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
247,525✔
1099
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
247,525!
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) {
115,912✔
1104
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
55,763✔
1105
    if(pAction->actionType == TRANS_ACTION_MSG){
55,763✔
1106
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
39,395!
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,368!
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));
60,149!
1117

1118
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
60,149!
1119

1120
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
60,149!
1121

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

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

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

1155
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
60,151✔
1156
  int32_t code = 0;
60,151✔
1157
  mInfo("trans:%d, commit transaction", pTrans->id);
60,151!
1158
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
60,151✔
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);
60,140!
1163
  TAOS_RETURN(code);
60,140✔
1164
}
1165

1166
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
2✔
1167
  int32_t code = 0;
2✔
1168
  mInfo("trans:%d, rollback transaction", pTrans->id);
2!
1169
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
2!
1170
    mError("trans:%d, failed to rollback since %s", pTrans->id, tstrerror(code));
×
1171
    TAOS_RETURN(code);
×
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) {
2✔
1178
  int32_t code = 0;
2✔
1179
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
2!
1180
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
2!
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);
2!
1185
  TAOS_RETURN(code);
2✔
1186
}
1187

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

1192
  if (pTrans->stage == TRN_STAGE_FINISH) {
332,033✔
1193
    sendRsp = true;
130,745✔
1194
  }
1195

1196
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
332,033✔
1197
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
97,412!
1198
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
12!
1199
      sendRsp = true;
12✔
1200
    }
1201
  } else {
1202
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
234,621✔
1203
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
150,497!
1204
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1205
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1206
      } else {
1207
        if (pTrans->failedTimes > 6) sendRsp = true;
150,497✔
1208
      }
1209
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
150,497✔
1210
    }
1211
  }
1212

1213
  if (!sendRsp) {
332,033✔
1214
    return;
201,274✔
1215
  } else {
1216
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
130,759!
1217
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1218
  }
1219

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

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

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

1244
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
34,126✔
1245

1246
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
34,126✔
1247
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,922!
1248
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,922✔
1249
        if (pDb != NULL) {
4,922!
1250
          for (int32_t j = 0; j < 12; j++) {
5,783✔
1251
            bool ready = mndIsDbReady(pMnode, pDb);
5,779✔
1252
            if (!ready) {
5,779✔
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,918✔
1257
            }
1258
          }
1259
        }
1260
        mndReleaseDb(pMnode, pDb);
4,922✔
1261
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
29,204✔
1262
        void   *pCont = NULL;
9,750✔
1263
        int32_t contLen = 0;
9,750✔
1264
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
9,750✔
1265
          mndTransSetRpcRsp(pTrans, pCont, contLen);
9,748✔
1266
        }
1267
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
19,454✔
1268
        void   *pCont = NULL;
699✔
1269
        int32_t contLen = 0;
699✔
1270
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
699!
1271
          mndTransSetRpcRsp(pTrans, pCont, contLen);
699✔
1272
        }
1273
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
18,755✔
1274
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
24✔
1275
        if (code != 0) {
24!
1276
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1277
        }
1278
      }
1279

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

1289
      tmsgSendRsp(&rspMsg);
34,126✔
1290

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

1299
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
114,854✔
1300
  int32_t code = 0;
114,854✔
1301
  SMnode *pMnode = pRsp->info.node;
114,854✔
1302
#ifndef TD_ASTRA_32
1303
  int64_t signature = (int64_t)(pRsp->info.ahandle);
114,854✔
1304
  int32_t transId = (int32_t)(signature >> 32);
114,854✔
1305
  int32_t action = (int32_t)((signature << 32) >> 32);
114,854✔
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);
114,854✔
1311
  if (pTrans == NULL) {
114,854!
1312
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1313
    if (terrno != 0) code = terrno;
×
1314
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1315
    goto _OVER;
×
1316
  }
1317

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

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

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

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

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

1348
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
114,854!
1349
          mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1350
          tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1351
  } else {
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);
114,854!
1356
  mndTransExecute(pMnode, pTrans, true);
114,854✔
1357

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

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

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

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

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

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

1399
// execute in sync context
1400
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
555,289✔
1401
  if (pAction->rawWritten) return 0;
555,289✔
1402
  if (topHalf) {
306,415✔
1403
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
6✔
1404
  }
1405

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

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

1422
    mndSetTransLastAction(pTrans, pAction);
306,409✔
1423
  } else {
1424
    pAction->errCode = (terrno != 0) ? terrno : code;
×
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));
1427
    mndSetTransLastAction(pTrans, pAction);
×
1428
  }
1429

1430
  TAOS_RETURN(code);
306,409✔
1431
}
1432

1433
// execute in trans context
1434
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
695,695✔
1435
                                     bool notSend) {
1436
  if (pAction->msgSent) return 0;
695,695✔
1437
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
165,203✔
1438
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
50,082✔
1439
  }
1440

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

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

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

1466
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
114,934✔
1467

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

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

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

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

1496
  TAOS_RETURN(code);
114,934✔
1497
}
1498

1499
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1500
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1501
  pAction->rawWritten = 0;
×
1502
  pAction->errCode = 0;
×
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,250,984✔
1510
                                        bool notSend) {
1511
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,250,984✔
1512
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
555,289✔
1513
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
695,695!
1514
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
695,695✔
1515
  } else {
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) {
309,955✔
1521
  int32_t numOfActions = taosArrayGetSize(pArray);
309,955✔
1522
  int32_t code = 0;
309,955✔
1523

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

1534
  return code;
309,955✔
1535
}
1536

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

1542
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
309,955✔
1543
    return code;
41,241✔
1544
  }
1545

1546
  int32_t       numOfExecuted = 0;
268,714✔
1547
  int32_t       errCode = 0;
268,714✔
1548
  STransAction *pErrAction = NULL;
268,714✔
1549
  for (int32_t action = 0; action < numOfActions; ++action) {
1,402,903✔
1550
    STransAction *pAction = taosArrayGet(pArray, action);
1,134,189✔
1551
    if (pAction->msgReceived || pAction->rawWritten) {
1,134,189✔
1552
      numOfExecuted++;
826,435✔
1553
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
826,435✔
1554
        errCode = pAction->errCode;
30✔
1555
        pErrAction = pAction;
30✔
1556
      }
1557
    } else {
1558
      pErrAction = pAction;
307,754✔
1559
    }
1560
  }
1561

1562
  mndSetTransLastAction(pTrans, pErrAction);
268,714✔
1563

1564
  if (numOfExecuted == numOfActions) {
268,714✔
1565
    if (errCode == 0) {
164,592✔
1566
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
164,585!
1567
      return 0;
164,585✔
1568
    } else {
1569
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
7!
1570
      mndTransResetActions(pMnode, pTrans, pArray);
7✔
1571
      terrno = errCode;
7✔
1572
      return errCode;
7✔
1573
    }
1574
  } else {
1575
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
104,122!
1576

1577
    for (int32_t action = 0; action < numOfActions; ++action) {
619,047✔
1578
      STransAction *pAction = taosArrayGet(pArray, action);
514,925✔
1579
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
514,925✔
1580
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1581
             pAction->acceptableCode, pAction->retryCode);
1582
      if (pAction->msgSent) {
514,925✔
1583
        bool reset = false;
514,738✔
1584
        if (pAction->msgReceived) {
514,738✔
1585
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
207,171✔
1586
        } else {
1587
          int64_t timestamp = taosGetTimestampMs();
307,567✔
1588
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
307,567!
1589
        }
1590
        if (reset) {
514,738✔
1591
          mndTransResetAction(pMnode, pTrans, pAction);
23✔
1592
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
23!
1593
                pAction->id, pAction->errCode, pAction->startTime);
1594
        }
1595
      }
1596
    }
1597
    return TSDB_CODE_ACTION_IN_PROGRESS;
104,122✔
1598
  }
1599
}
1600

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

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

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

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

1633
  if (pTrans->actionPos >= numOfActions) {
33,459✔
1634
    return code;
3,668✔
1635
  }
1636

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

1640
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
44,089✔
1641
    STransAction *pAction = taosArrayGet(pActions, action);
40,728✔
1642

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

1647
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
40,728✔
1648
    if (code == 0) {
40,728✔
1649
      if (pAction->msgSent) {
31,880✔
1650
        bool reset = false;
28,846✔
1651
        if (pAction->msgReceived) {
28,846✔
1652
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
12,957!
1653
            code = pAction->errCode;
4,912✔
1654
            reset = true;
4,912✔
1655
          } else {
1656
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
8,045!
1657
          }
1658
        } else {
1659
          int64_t timestamp = taosGetTimestampMs();
15,889✔
1660
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
15,889!
1661
          code = TSDB_CODE_ACTION_IN_PROGRESS;
15,889✔
1662
        }
1663
        if (reset) {
28,846✔
1664
          mndTransResetAction(pMnode, pTrans, pAction);
4,912✔
1665
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
4,912!
1666
                pAction->id, pAction->errCode, pAction->startTime);
1667
        }
1668
      } else if (pAction->rawWritten) {
3,034!
1669
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,034!
1670
          code = pAction->errCode;
×
1671
        } else {
1672
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,034!
1673
        }
1674
      } else {
1675
      }
1676
    }
1677

1678
    if (code == 0) {
40,728✔
1679
      pTrans->failedTimes = 0;
11,079✔
1680
    }
1681
    mndSetTransLastAction(pTrans, pAction);
40,728✔
1682

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

1691
    if (code == 0) {
30,196✔
1692
      pTrans->code = 0;
9,395✔
1693
      pTrans->actionPos++;
9,395✔
1694
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
9,395!
1695
            pAction->id);
1696
      (void)taosThreadMutexUnlock(&pTrans->mutex);
9,395✔
1697
      code = mndTransSync(pMnode, pTrans);
9,395✔
1698
      (void)taosThreadMutexLock(&pTrans->mutex);
9,395✔
1699
      if (code != 0) {
9,395!
1700
        pTrans->actionPos--;
×
1701
        pTrans->code = terrno;
×
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());
1704
        break;
×
1705
      }
1706
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
20,801✔
1707
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
15,889!
1708
      break;
15,889✔
1709
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,912✔
1710
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
24✔
1711
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,903!
1712
            code, tstrerror(code));
1713
      pTrans->lastErrorNo = code;
4,903✔
1714
      taosMsleep(300);
4,903✔
1715
      action--;
4,903✔
1716
      continue;
4,903✔
1717
    } else {
1718
      terrno = code;
9✔
1719
      pTrans->lastErrorNo = code;
9✔
1720
      pTrans->code = code;
9✔
1721
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
9!
1722
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1723
      break;
9✔
1724
    }
1725
  }
1726

1727
  return code;
29,791✔
1728
}
1729

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

1740
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
1741
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1742
  (void)taosThreadMutexLock(&pTrans->mutex);
×
1743
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
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) {
70,643✔
1751
  bool    continueExec = true;
70,643✔
1752
  int32_t code = 0;
70,643✔
1753
  terrno = 0;
70,643✔
1754

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

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

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

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

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

1782
  if (pTrans->exec == TRN_EXEC_SERIAL) {
261,418✔
1783
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
33,461✔
1784
  } else {
1785
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
227,957✔
1786
  }
1787

1788
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
261,418!
1789
    pTrans->lastErrorNo = code;
×
1790
    pTrans->code = code;
×
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)) {
261,418✔
1799
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
81,249!
1800
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1801
      return false;
81,249✔
1802
    }
1803
  }
1804
  terrno = code;
180,169✔
1805

1806
  if (code == 0) {
180,169✔
1807
    pTrans->code = 0;
60,151✔
1808
    pTrans->stage = TRN_STAGE_COMMIT;
60,151✔
1809
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
60,151!
1810
    continueExec = true;
60,151✔
1811
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
120,018!
1812
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
120,001!
1813
    continueExec = false;
120,001✔
1814
  } else {
1815
    pTrans->failedTimes++;
17✔
1816
    pTrans->code = terrno;
17✔
1817
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
17✔
1818
      if (pTrans->lastAction != 0) {
2!
1819
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
1820
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
1821
          if (pTrans->failedTimes < 6) {
×
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);
1824
            taosMsleep(1000);
×
1825
            continueExec = true;
×
1826
            return true;
×
1827
          }
1828
        }
1829
      }
1830

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

1842
  return continueExec;
180,169✔
1843
}
1844

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

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

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

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

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

1888
  return continueExec;
130,741✔
1889
}
1890

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

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

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

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

1917
  return continueExec;
12✔
1918
}
1919

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

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

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

1936
  return continueExec;
2✔
1937
}
1938

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

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

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

1956
  return continueExec;
2✔
1957
}
1958

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

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

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

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

1983
  while (continueExec) {
915,106✔
1984
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
583,073!
1985
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1986
    pTrans->lastExecTime = taosGetTimestampMs();
583,073✔
1987
    switch (pTrans->stage) {
583,073!
1988
      case TRN_STAGE_PREPARE:
×
1989
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1990
        break;
×
1991
      case TRN_STAGE_REDO_ACTION:
261,418✔
1992
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
261,418✔
1993
        break;
261,418✔
1994
      case TRN_STAGE_COMMIT:
60,153✔
1995
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
60,153✔
1996
        break;
60,153✔
1997
      case TRN_STAGE_COMMIT_ACTION:
130,741✔
1998
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
130,741✔
1999
        break;
130,741✔
2000
      case TRN_STAGE_ROLLBACK:
2✔
2001
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
2✔
2002
        break;
2✔
2003
      case TRN_STAGE_UNDO_ACTION:
14✔
2004
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
14✔
2005
        break;
14✔
2006
      case TRN_STAGE_PRE_FINISH:
2✔
2007
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
2✔
2008
        break;
2✔
2009
      case TRN_STAGE_FINISH:
130,743✔
2010
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
130,743✔
2011
        break;
130,743✔
2012
      default:
×
2013
        continueExec = false;
×
2014
        break;
×
2015
    }
2016
  }
2017

2018
  mndTransSendRpcRsp(pMnode, pTrans);
332,033✔
2019
}
332,033✔
2020

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

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

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

2039
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
2040
  SArray *pArray = NULL;
×
2041
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
2042
    pArray = pTrans->redoActions;
×
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){
×
2050
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2051
  }
2052
  
2053
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
×
2054
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
2055
      STransAction *pAction = taosArrayGet(pArray, i);
×
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{
2067
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2068
  }
2069

2070
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
×
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!
2082
    code = TSDB_CODE_INVALID_MSG;
×
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

2091
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
2092
  if (pTrans == NULL) {
×
2093
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
2094
    if (terrno != 0) code = terrno;
×
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; }
186✔
2110

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

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

2127
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
62,817✔
2128

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

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

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

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

2167
  return buf;
19,295✔
2168
}
2169

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

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

2186
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
721✔
2187
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
12,203✔
2188
      len = 0;
11,482✔
2189
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
11,482✔
2190
      if (pAction->actionType == TRANS_ACTION_MSG) {
11,464✔
2191
        char bufStart[40] = {0};
9,630✔
2192
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
9,630✔
2193

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

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

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

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

2232
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
1!
2233
      len = 0;
×
2234
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
2235
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
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
      }
2244
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2245
    }
2246
  }
2247
}
718✔
2248

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

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

2262
    cols = 0;
725✔
2263

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

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

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

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

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

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

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

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

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

2329
    mndTransLogAction(pTrans);
722✔
2330

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

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

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

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};
×
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
  }
2358
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
×
2359
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
×
2360
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
2361
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
×
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;
×
2370
  int32_t lino = 0;
×
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

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

2381
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
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};
×
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));
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) {
×
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);
×
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);
×
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};
×
2428
    if (pAction->pRaw->type == SDB_VGROUP) {
×
2429
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
×
2430
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
×
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);
×
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};
×
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++);
×
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};
×
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

2468
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
×
2469
  if (stage == TRN_STAGE_PREPARE) {
×
2470
    return pTrans->prepareActions;
×
2471
  }
2472
  if (stage == TRN_STAGE_REDO_ACTION) {
×
2473
    return pTrans->redoActions;
×
2474
  }
2475
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
2476
    return pTrans->commitActions;
×
2477
  }
2478
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
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

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) {
2493
  int32_t actionNum = taosArrayGetSize(pActions);
×
2494
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
×
2495

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

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

2514
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2515
  SMnode *pMnode = pReq->info.node;
×
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) {
×
2526
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
×
2527
    if (pShow->pIter == NULL) {
×
2528
      mError("failed to malloc for pShow->pIter");
×
2529
      return 0;
×
2530
    }
2531
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
×
2532
  }
2533

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

2536
  while (numOfRows < rows) {
×
2537
    if (pShowIter->pTrans == NULL) {
×
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);
×
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);
×
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

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

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

2562
  if (code != 0) {
×
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);
×
2569
    pShow->pIter = NULL;
×
2570
  }
2571
  return numOfRows;
×
2572
}
2573

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