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

taosdata / TDengine / #3589

24 Jan 2025 08:57AM UTC coverage: 63.191% (-0.4%) from 63.555%
#3589

push

travis-ci

web-flow
Merge pull request #29638 from taosdata/docs/TS-5846-3.0

enh: TDengine modify taosBenchmark new query rule cases and add doc

140478 of 285630 branches covered (49.18%)

Branch coverage included in aggregate %.

218744 of 282844 relevant lines covered (77.34%)

19421325.38 hits per line

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

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

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

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

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

57
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
487,878✔
58

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

66
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
490,398✔
67

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

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

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

89
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
1,736✔
90
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
1,736✔
91

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

98
void mndCleanupTrans(SMnode *pMnode) {}
1,735✔
99

100
static int32_t mndTransGetActionsSize(SArray *pArray) {
565,656✔
101
  int32_t actionNum = taosArrayGetSize(pArray);
565,656✔
102
  int32_t rawDataLen = 0;
565,656✔
103

104
  for (int32_t i = 0; i < actionNum; ++i) {
1,770,508✔
105
    STransAction *pAction = taosArrayGet(pArray, i);
1,204,852✔
106
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,204,852✔
107
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
741,572✔
108
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
463,280!
109
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
463,280✔
110
    } else {
111
      // empty
112
    }
113
    rawDataLen += sizeof(int8_t);
1,204,852✔
114
  }
115

116
  return rawDataLen;
565,656✔
117
}
118

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

126
  for (int32_t i = 0; i < actionsNum; ++i) {
1,770,508✔
127
    STransAction *pAction = taosArrayGet(pActions, i);
1,204,852✔
128
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
1,204,852!
129
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
1,204,852!
130
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
1,204,852!
131
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
1,204,852!
132
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
1,204,852!
133
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
1,204,852!
134
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
1,204,852!
135
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,204,852✔
136
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
741,572✔
137
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
741,572!
138
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
741,572!
139
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
741,572!
140
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
463,280!
141
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
463,280!
142
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
463,280!
143
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
463,280!
144
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
463,280!
145
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
463,280!
146
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
463,280!
147
    } else {
148
      // nothing
149
    }
150
  }
151
  ret = 0;
565,656✔
152

153
_OVER:
565,656✔
154
  *offset = dataPos;
565,656✔
155
  return ret;
565,656✔
156
}
157

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

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

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

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

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

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

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

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

214
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
141,414!
215

216
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
141,414✔
217
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
141,414!
218
  void *pIter = NULL;
141,414✔
219
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
141,414✔
220
  while (pIter) {
141,444✔
221
    int32_t arbGroupId = *(int32_t *)pIter;
30✔
222
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
30!
223
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
30✔
224
  }
225

226
  if (sver > TRANS_VER1_NUMBER) {
141,414!
227
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
141,414!
228
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
141,414!
229
  }
230

231
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
141,414!
232
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
141,414!
233

234
  terrno = 0;
141,414✔
235

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

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

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

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

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

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

319
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
267,630!
320

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

326
  pRow = sdbAllocRow(sizeof(STrans));
267,630✔
327
  if (pRow == NULL) goto _OVER;
267,630!
328

329
  pTrans = sdbGetRowObj(pRow);
267,630✔
330
  if (pTrans == NULL) goto _OVER;
267,630!
331

332
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
267,630!
333

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

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

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

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

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

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

389
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
267,630!
390

391
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
267,630✔
392

393
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
267,630!
394
  for (int32_t i = 0; i < arbgroupIdNum; ++i) {
267,680✔
395
    int32_t arbGroupId = 0;
50✔
396
    SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
50!
397
    if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
50!
398
  }
399

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

407
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
267,630!
408

409
  terrno = 0;
267,630✔
410

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

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

425
static const char *mndTransStr(ETrnStage stage) {
2,188,481✔
426
  switch (stage) {
2,188,481!
427
    case TRN_STAGE_PREPARE:
136,143✔
428
      return "prepare";
136,143✔
429
    case TRN_STAGE_REDO_ACTION:
961,411✔
430
      return "redoAction";
961,411✔
431
    case TRN_STAGE_ROLLBACK:
28✔
432
      return "rollback";
28✔
433
    case TRN_STAGE_UNDO_ACTION:
43,946✔
434
      return "undoAction";
43,946✔
435
    case TRN_STAGE_COMMIT:
223,434✔
436
      return "commit";
223,434✔
437
    case TRN_STAGE_COMMIT_ACTION:
490,173✔
438
      return "commitAction";
490,173✔
439
    case TRN_STAGE_FINISH:
333,318✔
440
      return "finished";
333,318✔
441
    case TRN_STAGE_PRE_FINISH:
28✔
442
      return "pre-finish";
28✔
443
    default:
×
444
      return "invalid";
×
445
  }
446
}
447

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

459
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
657,477✔
460
  if (pAction != NULL) {
657,477✔
461
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
542,152✔
462
      pTrans->lastAction = pAction->id;
352,514✔
463
      pTrans->lastMsgType = pAction->msgType;
352,514✔
464
      pTrans->lastEpset = pAction->epSet;
352,514✔
465
      pTrans->lastErrorNo = pAction->errCode;
352,514✔
466
    }
467
  } else {
468
    pTrans->lastAction = 0;
115,325✔
469
    pTrans->lastMsgType = 0;
115,325✔
470
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
115,325✔
471
    pTrans->lastErrorNo = 0;
115,325✔
472
  }
473
}
657,477✔
474

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

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

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

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

502
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
49,825✔
503

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

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

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

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

527
  return 0;
49,825✔
528
}
529

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

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

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

579
  mndTransDropData(pTrans);
158,707✔
580
  return 0;
158,707✔
581
}
582

583
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
237,048✔
584
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
765,655✔
585
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
528,607✔
586
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
528,607✔
587
    pOldAction->rawWritten = pNewAction->rawWritten;
528,607✔
588
    pOldAction->msgSent = pNewAction->msgSent;
528,607✔
589
    pOldAction->msgReceived = pNewAction->msgReceived;
528,607✔
590
    pOldAction->errCode = pNewAction->errCode;
528,607✔
591
  }
592
}
237,048✔
593

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

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

607
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
59,262✔
608
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
59,262✔
609
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
59,262✔
610
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
59,262✔
611
  pOld->stage = pNew->stage;
59,262✔
612
  pOld->actionPos = pNew->actionPos;
59,262✔
613

614
  if (pOld->stage == TRN_STAGE_COMMIT) {
59,262✔
615
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
49,610✔
616
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
49,610!
617
  }
618

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

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

629
  return 0;
59,262✔
630
}
631

632
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
253,641✔
633
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
253,641✔
634
  if (pTrans == NULL) {
253,641✔
635
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
4,610✔
636
  }
637
  return pTrans;
253,641✔
638
}
639

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

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

655
  if (opername != NULL) {
41,816!
656
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
41,816✔
657
  }
658

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

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

687
  if (pReq != NULL) {
41,816✔
688
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
55,930!
689
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
690
      return NULL;
×
691
    }
692
    pTrans->originRpcType = pReq->msgType;
27,965✔
693
  }
694

695
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
41,816!
696

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

701
static void mndTransDropActions(SArray *pArray) {
1,237,784✔
702
  int32_t size = taosArrayGetSize(pArray);
1,237,784✔
703
  for (int32_t i = 0; i < size; ++i) {
3,822,093✔
704
    STransAction *pAction = taosArrayGet(pArray, i);
2,584,309✔
705
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,584,309✔
706
      taosMemoryFreeClear(pAction->pRaw);
1,569,753!
707
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,014,556!
708
      taosMemoryFreeClear(pAction->pCont);
1,014,556!
709
    } else {
710
      // nothing
711
    }
712
  }
713

714
  taosArrayDestroy(pArray);
1,237,784✔
715
}
1,237,784✔
716

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

725
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
343,027✔
726
  pAction->id = taosArrayGetSize(pArray);
343,027✔
727

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

733
  return 0;
343,027✔
734
}
735

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

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

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

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

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

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

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

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

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

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

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

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

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

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

819
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
29,621✔
820
  if (dbname != NULL) {
29,621!
821
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
29,621✔
822
  }
823
  if (stbname != NULL) {
29,621✔
824
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
22,489✔
825
  }
826
}
29,621✔
827

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

834
void mndTransSetSerial(STrans *pTrans) { pTrans->exec = TRN_EXEC_SERIAL; }
2,884✔
835

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

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

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

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

847
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
3,684✔
848

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

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

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

875
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
3,325✔
876
  if (conflict[0] == 0) return false;
3,325!
877
  if (strcasecmp(conflict, pTrans->dbname) == 0) return true;
3,325✔
878
  return false;
3,026✔
879
}
880

881
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
20,222✔
882
  if (conflict[0] == 0) return false;
20,222✔
883
  if (strcasecmp(conflict, pTrans->stbname) == 0) return true;
18,942✔
884
  return false;
18,908✔
885
}
886

887
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
23,547✔
888
  if (conflict) {
23,547✔
889
    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,
333!
890
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
891
    *globalConflict = true;
333✔
892
  } else {
893
    mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with trans:%d db:%s stb:%s type:%d", pNew->id, pNew->dbname,
23,214!
894
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
895
  }
896
}
23,547✔
897

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

903
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
121,221✔
904

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

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

912
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
21,955✔
913

914
    if (pNew->conflict == TRN_CONFLICT_DB) {
21,955✔
915
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
2,966✔
916
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
2,966✔
917
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,910✔
918
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,910✔
919
      }
920
    }
921

922
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
21,955✔
923
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
18,985✔
924
      if (pTrans->conflict == TRN_CONFLICT_DB) {
18,985✔
925
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,415✔
926
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,415✔
927
      }
928
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
18,985✔
929
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
16,897✔
930
      }
931
    }
932

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

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

959
    sdbRelease(pMnode->pSdb, pTrans);
21,955✔
960
  }
961

962
  return conflict;
83,730✔
963
}
964

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

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

981
  TAOS_RETURN(code);
120,858✔
982
}
983

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

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

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

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

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

1019
  TAOS_RETURN(code);
307✔
1020
}
1021

1022
static bool mndTransActionsOfSameType(SArray *pActions) {
91,767✔
1023
  int32_t size = taosArrayGetSize(pActions);
91,767✔
1024
  ETrnAct lastActType = TRANS_ACTION_NULL;
91,767✔
1025
  bool    same = true;
91,767✔
1026
  for (int32_t i = 0; i < size; ++i) {
378,878✔
1027
    STransAction *pAction = taosArrayGet(pActions, i);
287,111✔
1028
    if (i > 0) {
287,111✔
1029
      if (lastActType != pAction->actionType) {
214,522!
1030
        same = false;
×
1031
        break;
×
1032
      }
1033
    }
1034
    lastActType = pAction->actionType;
287,111✔
1035
  }
1036
  return same;
91,767✔
1037
}
1038

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

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

1057
  TAOS_RETURN(code);
41,414✔
1058
}
1059

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

1073
  TAOS_RETURN(code);
41,414✔
1074
}
1075

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

1082
  mInfo("trans:%d, action list:", pTrans->id);
41,414!
1083
  int32_t index = 0;
41,414✔
1084
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
61,379✔
1085
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
19,965✔
1086
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
19,965!
1087
          mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1088
  }
1089

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

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

1102
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
85,140✔
1103
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
43,726✔
1104
    if(pAction->actionType == TRANS_ACTION_MSG){
43,726✔
1105
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
31,024!
1106
            mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1107
    }
1108
    else{
1109
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
12,702!
1110
            mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1111
    }
1112
  }
1113

1114

1115
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
41,414!
1116

1117
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
41,414!
1118

1119
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
41,414!
1120

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

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

1139
  pNew->pRpcArray = pTrans->pRpcArray;
41,392✔
1140
  pNew->rpcRsp = pTrans->rpcRsp;
41,392✔
1141
  pNew->rpcRspLen = pTrans->rpcRspLen;
41,392✔
1142
  pNew->mTraceId = pTrans->mTraceId;
41,392✔
1143
  pTrans->pRpcArray = NULL;
41,392✔
1144
  pTrans->rpcRsp = NULL;
41,392✔
1145
  pTrans->rpcRspLen = 0;
41,392✔
1146

1147
  mndTransExecute(pMnode, pNew);
41,392✔
1148
  mndReleaseTrans(pMnode, pNew);
41,392✔
1149
  // TDOD change to TAOS_RETURN(code);
1150
  return 0;
41,392✔
1151
}
1152

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

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

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

1186
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
247,448✔
1187
  bool    sendRsp = false;
247,448✔
1188
  int32_t code = pTrans->code;
247,448✔
1189

1190
  if (pTrans->stage == TRN_STAGE_FINISH) {
247,448✔
1191
    sendRsp = true;
91,016✔
1192
  }
1193

1194
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
247,448✔
1195
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
62,563!
1196
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
34✔
1197
      sendRsp = true;
34✔
1198
    }
1199
  } else {
1200
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
184,885✔
1201
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
121,690!
1202
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1203
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1204
      } else {
1205
        if (pTrans->failedTimes > 6) sendRsp = true;
121,690✔
1206
      }
1207
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
121,690✔
1208
    }
1209
  }
1210

1211
  if (!sendRsp) {
247,448✔
1212
    return;
156,396✔
1213
  } else {
1214
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
91,052!
1215
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1216
  }
1217

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

1226
  for (int32_t i = 0; i < size; ++i) {
55,058✔
1227
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
27,529✔
1228
    if (pInfo->handle != NULL) {
27,529✔
1229
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
26,029!
1230
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1231
      }
1232
      if (code == TSDB_CODE_SYN_TIMEOUT) {
26,029!
1233
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1234
      }
1235

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

1242
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
26,029✔
1243

1244
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
26,029✔
1245
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
3,659!
1246
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
3,659✔
1247
        if (pDb != NULL) {
3,659!
1248
          for (int32_t j = 0; j < 12; j++) {
4,430✔
1249
            bool ready = mndIsDbReady(pMnode, pDb);
4,427✔
1250
            if (!ready) {
4,427✔
1251
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
771!
1252
              taosMsleep(1000);
771✔
1253
            } else {
1254
              break;
3,656✔
1255
            }
1256
          }
1257
        }
1258
        mndReleaseDb(pMnode, pDb);
3,659✔
1259
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
22,370✔
1260
        void   *pCont = NULL;
6,882✔
1261
        int32_t contLen = 0;
6,882✔
1262
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
6,882✔
1263
          mndTransSetRpcRsp(pTrans, pCont, contLen);
6,878✔
1264
        }
1265
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
15,488✔
1266
        void   *pCont = NULL;
481✔
1267
        int32_t contLen = 0;
481✔
1268
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
481!
1269
          mndTransSetRpcRsp(pTrans, pCont, contLen);
481✔
1270
        }
1271
      }
1272

1273
      if (pTrans->rpcRspLen != 0) {
26,029✔
1274
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
12,006✔
1275
        if (rpcCont != NULL) {
12,006!
1276
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
12,006✔
1277
          rspMsg.pCont = rpcCont;
12,006✔
1278
          rspMsg.contLen = pTrans->rpcRspLen;
12,006✔
1279
        }
1280
      }
1281

1282
      tmsgSendRsp(&rspMsg);
26,029✔
1283

1284
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
26,029!
1285
            mndTransStr(pTrans->stage), pInfo->ahandle);
1286
    }
1287
  }
1288
  taosArrayClear(pTrans->pRpcArray);
27,529✔
1289
  taosWUnLockLatch(&pTrans->lockRpcArray);
27,529✔
1290
}
1291

1292
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
92,653✔
1293
  int32_t code = 0;
92,653✔
1294
  SMnode *pMnode = pRsp->info.node;
92,653✔
1295
  int64_t signature = (int64_t)(pRsp->info.ahandle);
92,653✔
1296
  int32_t transId = (int32_t)(signature >> 32);
92,653✔
1297
  int32_t action = (int32_t)((signature << 32) >> 32);
92,653✔
1298

1299
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
92,653✔
1300
  if (pTrans == NULL) {
92,653!
1301
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1302
    if (terrno != 0) code = terrno;
×
1303
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1304
    goto _OVER;
×
1305
  }
1306

1307
  SArray *pArray = NULL;
92,653✔
1308
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
92,653✔
1309
    pArray = pTrans->redoActions;
92,637✔
1310
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
16!
1311
    pArray = pTrans->undoActions;
16✔
1312
  } else {
1313
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1314
    goto _OVER;
×
1315
  }
1316

1317
  if (pArray == NULL) {
92,653!
1318
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1319
    goto _OVER;
×
1320
  }
1321

1322
  int32_t actionNum = taosArrayGetSize(pArray);
92,653✔
1323
  if (action < 0 || action >= actionNum) {
92,653!
1324
    mError("trans:%d, invalid action:%d", transId, action);
×
1325
    goto _OVER;
×
1326
  }
1327

1328
  STransAction *pAction = taosArrayGet(pArray, action);
92,653✔
1329
  if (pAction != NULL) {
92,653!
1330
    pAction->msgReceived = 1;
92,653✔
1331
    pAction->errCode = pRsp->code;
92,653✔
1332
    pAction->endTime = taosGetTimestampMs();
92,653✔
1333

1334
    // pTrans->lastErrorNo = pRsp->code;
1335
    mndSetTransLastAction(pTrans, pAction);
92,653✔
1336

1337
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
92,653!
1338
          mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1339
          tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1340
  } else {
1341
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1342
  }
1343

1344
  mndTransExecute(pMnode, pTrans);
92,653✔
1345

1346
_OVER:
92,653✔
1347
  mndReleaseTrans(pMnode, pTrans);
92,653✔
1348
  TAOS_RETURN(code);
92,653✔
1349
}
1350

1351
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
4,924✔
1352
  pAction->rawWritten = 0;
4,924✔
1353
  pAction->msgSent = 0;
4,924✔
1354
  pAction->msgReceived = 0;
4,924✔
1355
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
4,924!
1356
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
4,924✔
1357
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
20✔
1358
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
20!
1359
          pAction->id, pAction->epSet.inUse);
1360
  } else {
1361
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,904!
1362
  }
1363
  pAction->errCode = 0;
4,924✔
1364
}
4,924✔
1365

1366
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
4✔
1367
  int32_t numOfActions = taosArrayGetSize(pArray);
4✔
1368

1369
  for (int32_t action = 0; action < numOfActions; ++action) {
20✔
1370
    STransAction *pAction = taosArrayGet(pArray, action);
16✔
1371
    if (pAction->msgSent && pAction->msgReceived &&
16!
1372
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
16!
1373
      continue;
12✔
1374
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
4!
1375

1376
    mndTransResetAction(pMnode, pTrans, pAction);
4✔
1377
  }
1378
}
4✔
1379

1380
// execute in sync context
1381
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
427,857✔
1382
  if (pAction->rawWritten) return 0;
427,857✔
1383
  if (topHalf) {
236,606!
1384
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
×
1385
  }
1386

1387
  if (pAction->pRaw->type >= SDB_MAX) {
236,606!
1388
    pAction->rawWritten = true;
×
1389
    pAction->errCode = 0;
×
1390
    mndSetTransLastAction(pTrans, pAction);
×
1391
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
1392
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1393
  }
1394

1395
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
236,606✔
1396
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
236,606!
1397
    pAction->rawWritten = true;
236,606✔
1398
    pAction->errCode = 0;
236,606✔
1399
    code = 0;
236,606✔
1400
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
236,606!
1401
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1402

1403
    mndSetTransLastAction(pTrans, pAction);
236,606✔
1404
  } else {
1405
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1406
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1407
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1408
    mndSetTransLastAction(pTrans, pAction);
×
1409
  }
1410

1411
  TAOS_RETURN(code);
236,606✔
1412
}
1413

1414
// execute in trans context
1415
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
625,045✔
1416
  if (pAction->msgSent) return 0;
625,045✔
1417
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
130,910✔
1418
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
38,196✔
1419
  }
1420

1421
  int64_t signature = pTrans->id;
92,714✔
1422
  signature = (signature << 32);
92,714✔
1423
  signature += pAction->id;
92,714✔
1424

1425
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
92,714✔
1426
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
92,714✔
1427
  if (rpcMsg.pCont == NULL) {
92,714!
1428
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1429
    return -1;
×
1430
  }
1431
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
92,714✔
1432
  rpcMsg.info.notFreeAhandle = 1;
92,714✔
1433

1434
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
92,714✔
1435

1436
  char    detail[1024] = {0};
92,714✔
1437
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
92,714!
1438
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
92,714✔
1439
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
190,909✔
1440
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
98,195✔
1441
                     pAction->epSet.eps[i].port);
98,195✔
1442
  }
1443

1444
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
92,714✔
1445
  if (code == 0) {
92,714✔
1446
    pAction->msgSent = 1;
92,713✔
1447
    // pAction->msgReceived = 0;
1448
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
92,713✔
1449
    pAction->startTime = taosGetTimestampMs();
92,713✔
1450
    pAction->endTime = 0;
92,713✔
1451
    mInfo("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
92,713!
1452

1453
    mndSetTransLastAction(pTrans, pAction);
92,713✔
1454
  } else {
1455
    pAction->msgSent = 0;
1✔
1456
    pAction->msgReceived = 0;
1✔
1457
    pAction->errCode = (terrno != 0) ? terrno : code;
1!
1458
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
1!
1459
           detail);
1460

1461
    mndSetTransLastAction(pTrans, pAction);
1✔
1462
  }
1463

1464
  TAOS_RETURN(code);
92,714✔
1465
}
1466

1467
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1468
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1469
  pAction->rawWritten = 0;
×
1470
  pAction->errCode = 0;
×
1471
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1472

1473
  mndSetTransLastAction(pTrans, pAction);
×
1474
  return 0;
×
1475
}
1476

1477
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
1,052,902✔
1478
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,052,902✔
1479
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
427,857✔
1480
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
625,045!
1481
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf);
625,045✔
1482
  } else {
1483
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1484
  }
1485
}
1486

1487
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
227,957✔
1488
  int32_t numOfActions = taosArrayGetSize(pArray);
227,957✔
1489
  int32_t code = 0;
227,957✔
1490

1491
  for (int32_t action = 0; action < numOfActions; ++action) {
1,187,249✔
1492
    STransAction *pAction = taosArrayGet(pArray, action);
989,771✔
1493
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
989,771✔
1494
    if (code != 0) {
989,771✔
1495
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
30,479!
1496
            numOfActions);
1497
      break;
30,479✔
1498
    }
1499
  }
1500

1501
  return code;
227,957✔
1502
}
1503

1504
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
258,406✔
1505
  int32_t numOfActions = taosArrayGetSize(pArray);
258,406✔
1506
  int32_t code = 0;
258,406✔
1507
  if (numOfActions == 0) return 0;
258,406✔
1508

1509
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf)) != 0) {
227,957✔
1510
    return code;
30,479✔
1511
  }
1512

1513
  int32_t       numOfExecuted = 0;
197,478✔
1514
  int32_t       errCode = 0;
197,478✔
1515
  STransAction *pErrAction = NULL;
197,478✔
1516
  for (int32_t action = 0; action < numOfActions; ++action) {
1,156,770✔
1517
    STransAction *pAction = taosArrayGet(pArray, action);
959,292✔
1518
    if (pAction->msgReceived || pAction->rawWritten) {
959,292✔
1519
      numOfExecuted++;
678,140✔
1520
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
678,140✔
1521
        errCode = pAction->errCode;
5✔
1522
        pErrAction = pAction;
5✔
1523
      }
1524
    } else {
1525
      pErrAction = pAction;
281,152✔
1526
    }
1527
  }
1528

1529
  mndSetTransLastAction(pTrans, pErrAction);
197,478✔
1530

1531
  if (numOfExecuted == numOfActions) {
197,478✔
1532
    if (errCode == 0) {
115,329✔
1533
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
115,325!
1534
      return 0;
115,325✔
1535
    } else {
1536
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
4!
1537
      mndTransResetActions(pMnode, pTrans, pArray);
4✔
1538
      terrno = errCode;
4✔
1539
      return errCode;
4✔
1540
    }
1541
  } else {
1542
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
82,149!
1543

1544
    for (int32_t action = 0; action < numOfActions; ++action) {
561,658✔
1545
      STransAction *pAction = taosArrayGet(pArray, action);
479,509✔
1546
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
479,509✔
1547
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1548
             pAction->acceptableCode, pAction->retryCode);
1549
      if (pAction->msgSent) {
479,509!
1550
        if (pAction->msgReceived) {
479,509✔
1551
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
198,357✔
1552
            mndTransResetAction(pMnode, pTrans, pAction);
1✔
1553
            mInfo("trans:%d, %s:%d reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1!
1554
          }
1555
        }
1556
      }
1557
    }
1558
    return TSDB_CODE_ACTION_IN_PROGRESS;
82,149✔
1559
  }
1560
}
1561

1562
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
167,366✔
1563
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf);
167,366✔
1564
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
167,366✔
1565
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
5!
1566
           mndStrExecutionContext(topHalf));
1567
  }
1568
  return code;
167,366✔
1569
}
1570

1571
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
38✔
1572
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf);
38✔
1573
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
38!
1574
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1575
           mndStrExecutionContext(topHalf));
1576
  }
1577
  return code;
38✔
1578
}
1579

1580
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
91,002✔
1581
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf);
91,002✔
1582
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
91,002!
1583
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1584
           mndStrExecutionContext(topHalf));
1585
  }
1586
  return code;
91,002✔
1587
}
1588

1589
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
30,428✔
1590
  int32_t code = 0;
30,428✔
1591
  int32_t numOfActions = taosArrayGetSize(pActions);
30,428✔
1592
  if (numOfActions == 0) return code;
30,428✔
1593

1594
  if (pTrans->actionPos >= numOfActions) {
30,424✔
1595
    return code;
3,158✔
1596
  }
1597

1598
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
27,266!
1599
        pTrans->actionPos, mndTransStr(pTrans->stage));
1600

1601
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
40,831✔
1602
    STransAction *pAction = taosArrayGet(pActions, action);
38,026✔
1603

1604
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", 
38,026!
1605
          pTrans->id, pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent,
1606
          pAction->msgReceived);
1607

1608
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
38,026✔
1609
    if (code == 0) {
38,026✔
1610
      if (pAction->msgSent) {
30,308✔
1611
        if (pAction->msgReceived) {
26,726✔
1612
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
11,950✔
1613
            code = pAction->errCode;
4,919✔
1614
            mndTransResetAction(pMnode, pTrans, pAction);
4,919✔
1615
          } else {
1616
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
7,031!
1617
          }
1618
        } else {
1619
          code = TSDB_CODE_ACTION_IN_PROGRESS;
14,776✔
1620
        }
1621
      } else if (pAction->rawWritten) {
3,582!
1622
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,582!
1623
          code = pAction->errCode;
×
1624
        } else {
1625
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,582!
1626
        }
1627
      } else {
1628
      }
1629
    }
1630

1631
    if (code == 0) {
38,026✔
1632
      pTrans->failedTimes = 0;
10,613✔
1633
    }
1634
    mndSetTransLastAction(pTrans, pAction);
38,026✔
1635

1636
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
38,026✔
1637
      pTrans->lastErrorNo = code;
9,666✔
1638
      pTrans->code = code;
9,666✔
1639
      mInfo("trans:%d, %s:%d, cannot execute next action in %s, code:%s", pTrans->id, mndTransStr(pAction->stage),
9,666!
1640
            action, mndStrExecutionContext(topHalf), tstrerror(code));
1641
      break;
9,666✔
1642
    }
1643

1644
    if (code == 0) {
28,360✔
1645
      pTrans->code = 0;
8,665✔
1646
      pTrans->actionPos++;
8,665✔
1647
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
8,665!
1648
            pAction->id);
1649
      (void)taosThreadMutexUnlock(&pTrans->mutex);
8,665✔
1650
      code = mndTransSync(pMnode, pTrans);
8,665✔
1651
      (void)taosThreadMutexLock(&pTrans->mutex);
8,665✔
1652
      if (code != 0) {
8,665!
1653
        pTrans->actionPos--;
×
1654
        pTrans->code = terrno;
×
1655
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1656
               mndTransStr(pAction->stage), pAction->id, terrstr());
1657
        break;
×
1658
      }
1659
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
19,695✔
1660
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
14,776!
1661
      break;
14,776✔
1662
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,919!
1663
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
39✔
1664
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,900!
1665
            code, tstrerror(code));
1666
      pTrans->lastErrorNo = code;
4,900✔
1667
      taosMsleep(300);
4,900✔
1668
      action--;
4,900✔
1669
      continue;
4,900✔
1670
    } else {
1671
      terrno = code;
19✔
1672
      pTrans->lastErrorNo = code;
19✔
1673
      pTrans->code = code;
19✔
1674
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
19!
1675
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1676
      break;
19✔
1677
    }
1678
  }
1679

1680
  return code;
27,266✔
1681
}
1682

1683
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
30,428✔
1684
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
30,428✔
1685
  (void)taosThreadMutexLock(&pTrans->mutex);
30,428✔
1686
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
30,428!
1687
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
30,428✔
1688
  }
1689
  (void)taosThreadMutexUnlock(&pTrans->mutex);
30,428✔
1690
  return code;
30,428✔
1691
}
1692

1693
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
1694
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1695
  (void)taosThreadMutexLock(&pTrans->mutex);
×
1696
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1697
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
1698
  }
1699
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
1700
  return code;
×
1701
}
1702

1703
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
49,659✔
1704
  bool    continueExec = true;
49,659✔
1705
  int32_t code = 0;
49,659✔
1706
  terrno = 0;
49,659✔
1707

1708
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
49,659✔
1709
  if (numOfActions == 0) goto _OVER;
49,659✔
1710

1711
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
16,059!
1712

1713
  for (int32_t action = 0; action < numOfActions; ++action) {
41,164✔
1714
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
25,105✔
1715
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
25,105✔
1716
    if (code != 0) {
25,105!
1717
      terrno = code;
×
1718
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
1719
             numOfActions, tstrerror(code));
1720
      return false;
×
1721
    }
1722
  }
1723

1724
_OVER:
16,059✔
1725
  pTrans->stage = TRN_STAGE_REDO_ACTION;
49,659✔
1726
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
49,659!
1727
  return continueExec;
49,659✔
1728
}
1729

1730
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
197,794✔
1731
  bool    continueExec = true;
197,794✔
1732
  int32_t code = 0;
197,794✔
1733
  terrno = 0;
197,794✔
1734

1735
  if (pTrans->exec == TRN_EXEC_SERIAL) {
197,794✔
1736
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
30,428✔
1737
  } else {
1738
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf);
167,366✔
1739
  }
1740

1741
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
197,794!
1742
    pTrans->lastErrorNo = code;
×
1743
    pTrans->code = code;
×
1744
    mInfo(
×
1745
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
1746
        "continueExec:%d, code:%s",
1747
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1748
    taosMsleep(100);
×
1749
    return true;
×
1750
  } else {
1751
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
197,794✔
1752
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
59,468!
1753
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1754
      return false;
59,468✔
1755
    }
1756
  }
1757
  terrno = code;
138,326✔
1758

1759
  if (code == 0) {
138,326✔
1760
    pTrans->code = 0;
41,403✔
1761
    pTrans->stage = TRN_STAGE_COMMIT;
41,403✔
1762
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
41,403!
1763
    continueExec = true;
41,403✔
1764
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
96,923!
1765
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
96,899!
1766
    continueExec = false;
96,899✔
1767
  } else {
1768
    pTrans->failedTimes++;
24✔
1769
    pTrans->code = terrno;
24✔
1770
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
24✔
1771
      if (pTrans->lastAction != 0) {
4✔
1772
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
1773
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
1774
          if (pTrans->failedTimes < 6) {
×
1775
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
1776
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
1777
            taosMsleep(1000);
×
1778
            continueExec = true;
×
1779
            return true;
×
1780
          }
1781
        }
1782
      }
1783

1784
      pTrans->stage = TRN_STAGE_ROLLBACK;
4✔
1785
      pTrans->actionPos = 0;
4✔
1786
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
4!
1787
             pTrans->actionPos);
1788
      continueExec = true;
4✔
1789
    } else {
1790
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
20!
1791
      continueExec = false;
20✔
1792
    }
1793
  }
1794

1795
  return continueExec;
138,326✔
1796
}
1797

1798
// execute in trans context
1799
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
41,403✔
1800
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
41,403!
1801

1802
  bool    continueExec = true;
41,403✔
1803
  int32_t code = mndTransCommit(pMnode, pTrans);
41,403✔
1804

1805
  if (code == 0) {
41,403✔
1806
    pTrans->code = 0;
41,390✔
1807
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
41,390✔
1808
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
41,390!
1809
    continueExec = true;
41,390✔
1810
  } else {
1811
    pTrans->code = terrno;
13✔
1812
    pTrans->failedTimes++;
13✔
1813
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
13!
1814
    continueExec = false;
13✔
1815
  }
1816

1817
  return continueExec;
41,403✔
1818
}
1819

1820
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
91,002✔
1821
  bool    continueExec = true;
91,002✔
1822
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
91,002✔
1823

1824
  if (code == 0) {
91,002!
1825
    pTrans->code = 0;
91,002✔
1826
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
91,002✔
1827
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
91,002!
1828
    continueExec = true;
91,002✔
1829
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
1830
    pTrans->code = 0;
×
1831
    pTrans->stage = TRN_STAGE_COMMIT;
×
1832
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
1833
    continueExec = true;
×
1834
  } else {
1835
    pTrans->code = terrno;
×
1836
    pTrans->failedTimes++;
×
1837
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1838
    continueExec = false;
×
1839
  }
1840

1841
  return continueExec;
91,002✔
1842
}
1843

1844
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
38✔
1845
  bool    continueExec = true;
38✔
1846
  int32_t code = 0;
38✔
1847

1848
  if (pTrans->exec == TRN_EXEC_SERIAL) {
38!
1849
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
1850
  } else {
1851
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf);
38✔
1852
  }
1853

1854
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
38✔
1855
  terrno = code;
30✔
1856

1857
  if (code == 0) {
30✔
1858
    pTrans->stage = TRN_STAGE_PRE_FINISH;
4✔
1859
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
4!
1860
    continueExec = true;
4✔
1861
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
26!
1862
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
26!
1863
    continueExec = false;
26✔
1864
  } else {
1865
    pTrans->failedTimes++;
×
1866
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1867
    continueExec = false;
×
1868
  }
1869

1870
  return continueExec;
30✔
1871
}
1872

1873
// in trans context
1874
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
4✔
1875
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
4!
1876

1877
  bool    continueExec = true;
4✔
1878
  int32_t code = mndTransRollback(pMnode, pTrans);
4✔
1879

1880
  if (code == 0) {
4!
1881
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
4✔
1882
    continueExec = true;
4✔
1883
  } else {
1884
    pTrans->failedTimes++;
×
1885
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1886
    continueExec = false;
×
1887
  }
1888

1889
  return continueExec;
4✔
1890
}
1891

1892
// excute in trans context
1893
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
4✔
1894
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
4!
1895

1896
  bool    continueExec = true;
4✔
1897
  int32_t code = mndTransPreFinish(pMnode, pTrans);
4✔
1898

1899
  if (code == 0) {
4!
1900
    pTrans->stage = TRN_STAGE_FINISH;
4✔
1901
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
4!
1902
    continueExec = true;
4✔
1903
  } else {
1904
    pTrans->failedTimes++;
×
1905
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1906
    continueExec = false;
×
1907
  }
1908

1909
  return continueExec;
4✔
1910
}
1911

1912
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
91,014✔
1913
  bool continueExec = false;
91,014✔
1914
  if (topHalf) return continueExec;
91,014✔
1915

1916
  SSdbRaw *pRaw = mndTransEncode(pTrans);
49,620✔
1917
  if (pRaw == NULL) {
49,620!
1918
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
1919
    return false;
×
1920
  }
1921
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
49,620!
1922

1923
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
49,620✔
1924
  if (code != 0) {
49,620!
1925
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
1926
  }
1927

1928
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
49,620!
1929
        pTrans->failedTimes, pTrans->createdTime);
1930
  return continueExec;
49,620✔
1931
}
1932

1933
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf) {
247,448✔
1934
  bool continueExec = true;
247,448✔
1935

1936
  while (continueExec) {
668,707✔
1937
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64 "", pTrans->id,
421,259!
1938
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1939
    pTrans->lastExecTime = taosGetTimestampMs();
421,259✔
1940
    switch (pTrans->stage) {
421,259!
1941
      case TRN_STAGE_PREPARE:
×
1942
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1943
        break;
×
1944
      case TRN_STAGE_REDO_ACTION:
197,794✔
1945
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf);
197,794✔
1946
        break;
197,794✔
1947
      case TRN_STAGE_COMMIT:
41,403✔
1948
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
41,403✔
1949
        break;
41,403✔
1950
      case TRN_STAGE_COMMIT_ACTION:
91,002✔
1951
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
91,002✔
1952
        break;
91,002✔
1953
      case TRN_STAGE_ROLLBACK:
4✔
1954
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
4✔
1955
        break;
4✔
1956
      case TRN_STAGE_UNDO_ACTION:
38✔
1957
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf);
38✔
1958
        break;
38✔
1959
      case TRN_STAGE_PRE_FINISH:
4✔
1960
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
4✔
1961
        break;
4✔
1962
      case TRN_STAGE_FINISH:
91,014✔
1963
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
91,014✔
1964
        break;
91,014✔
1965
      default:
×
1966
        continueExec = false;
×
1967
        break;
×
1968
    }
1969
  }
1970

1971
  mndTransSendRpcRsp(pMnode, pTrans);
247,448✔
1972
}
247,448✔
1973

1974
// start trans, pullup, receive rsp, kill
1975
void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
138,525✔
1976
  bool topHalf = true;
138,525✔
1977
  mndTransExecuteImp(pMnode, pTrans, topHalf);
138,525✔
1978
}
138,525✔
1979

1980
// update trans
1981
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
108,923✔
1982
  bool topHalf = false;
108,923✔
1983
  mndTransExecuteImp(pMnode, pTrans, topHalf);
108,923✔
1984
}
108,923✔
1985

1986
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
33,006✔
1987
  mTrace("start to process trans timer");
33,006✔
1988
  mndTransPullup(pReq->info.node);
33,006✔
1989
  return 0;
33,006✔
1990
}
1991

1992
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
1993
  SArray *pArray = NULL;
×
1994
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
1995
    pArray = pTrans->redoActions;
×
1996
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1997
    pArray = pTrans->undoActions;
×
1998
  } else {
1999
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2000
  }
2001

2002
  if(pTrans->ableToBeKilled == false){
×
2003
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2004
  }
2005
  
2006
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
×
2007
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
2008
      STransAction *pAction = taosArrayGet(pArray, i);
×
2009
      mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
2010
            mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
2011
      pAction->msgSent = 1;
×
2012
      pAction->msgReceived = 1;
×
2013
      pAction->errCode = 0;
×
2014
    }
2015
  }
2016
  else if(pTrans->killMode == TRN_KILL_MODE_INTERUPT){
×
2017
    pTrans->stage = TRN_STAGE_PRE_FINISH;
×
2018
  }
2019
  else{
2020
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2021
  }
2022

2023
  mndTransExecute(pMnode, pTrans);
×
2024
  return 0;
×
2025
}
2026

2027
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1✔
2028
  SMnode       *pMnode = pReq->info.node;
1✔
2029
  SKillTransReq killReq = {0};
1✔
2030
  int32_t       code = -1;
1✔
2031
  STrans       *pTrans = NULL;
1✔
2032

2033
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
2034
    code = TSDB_CODE_INVALID_MSG;
×
2035
    goto _OVER;
×
2036
  }
2037

2038
  mInfo("trans:%d, start to kill", killReq.transId);
1!
2039
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1!
2040
    goto _OVER;
1✔
2041
  }
2042

2043
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
2044
  if (pTrans == NULL) {
×
2045
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
2046
    if (terrno != 0) code = terrno;
×
2047
    goto _OVER;
×
2048
  }
2049

2050
  code = mndKillTrans(pMnode, pTrans);
×
2051

2052
_OVER:
1✔
2053
  if (code != 0) {
1!
2054
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1!
2055
  }
2056

2057
  mndReleaseTrans(pMnode, pTrans);
1✔
2058
  TAOS_RETURN(code);
1✔
2059
}
2060

2061
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
217✔
2062

2063
void mndTransPullup(SMnode *pMnode) {
33,490✔
2064
  SSdb   *pSdb = pMnode->pSdb;
33,490✔
2065
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
33,490✔
2066
  if (pArray == NULL) return;
33,490!
2067

2068
  void *pIter = NULL;
33,490✔
2069
  while (1) {
4,480✔
2070
    STrans *pTrans = NULL;
37,970✔
2071
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
37,970✔
2072
    if (pIter == NULL) break;
37,970✔
2073
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
8,960!
2074
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2075
    }
2076
    sdbRelease(pSdb, pTrans);
4,480✔
2077
  }
2078

2079
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
33,490✔
2080

2081
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
37,970✔
2082
    int32_t *pTransId = taosArrayGet(pArray, i);
4,480✔
2083
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
4,480✔
2084
    if (pTrans != NULL) {
4,480!
2085
      mndTransExecute(pMnode, pTrans);
4,480✔
2086
    }
2087
    mndReleaseTrans(pMnode, pTrans);
4,480✔
2088
  }
2089
  taosArrayDestroy(pArray);
33,490✔
2090
}
2091

2092
static char *formatTimestamp(char *buf, int64_t val, int precision) {
12,186✔
2093
  time_t tt;
2094
  if (precision == TSDB_TIME_PRECISION_MICRO) {
12,186!
2095
    tt = (time_t)(val / 1000000);
×
2096
  }
2097
  if (precision == TSDB_TIME_PRECISION_NANO) {
12,186!
2098
    tt = (time_t)(val / 1000000000);
×
2099
  } else {
2100
    tt = (time_t)(val / 1000);
12,186✔
2101
  }
2102

2103
  struct tm tm;
2104
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
12,186!
2105
    mError("failed to get local time");
×
2106
    return NULL;
×
2107
  }
2108
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
12,186✔
2109

2110
  if (precision == TSDB_TIME_PRECISION_MICRO) {
12,186!
2111
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2112
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
12,186!
2113
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2114
  } else {
2115
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
12,186✔
2116
  }
2117

2118
  return buf;
12,186✔
2119
}
2120

2121
static void mndTransLogAction(STrans *pTrans) {
175✔
2122
  char    detail[512] = {0};
175✔
2123
  int32_t len = 0;
175✔
2124
  int32_t index = 0;
175✔
2125

2126
  if (pTrans->stage == TRN_STAGE_PREPARE) {
175!
2127
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2128
      len = 0;
×
2129
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2130
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2131
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2132
                      sdbStatusName(pAction->pRaw->status));
×
2133
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2134
    }
2135
  }
2136

2137
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
175!
2138
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
7,738✔
2139
      len = 0;
7,563✔
2140
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
7,563✔
2141
      if (pAction->actionType == TRANS_ACTION_MSG) {
7,563✔
2142
        char bufStart[40] = {0};
6,093✔
2143
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
6,093✔
2144

2145
        char endStart[40] = {0};
6,093✔
2146
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
6,093✔
2147
        len += snprintf(detail + len, sizeof(detail) - len,
12,186!
2148
                        "action:%d, %s:%d msgType:%s,"
2149
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2150
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
6,093✔
2151
                        pAction->msgReceived, bufStart, endStart);
6,093✔
2152

2153
        SEpSet epset = pAction->epSet;
6,093✔
2154
        if (epset.numOfEps > 0) {
6,093!
2155
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
6,093✔
2156
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
14,116✔
2157
            len +=
8,023✔
2158
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
8,023✔
2159
          }
2160
        }
2161

2162
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
6,093✔
2163
                        tstrerror(pAction->errCode));
2164
      } else {
2165
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
1,470✔
2166
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,470✔
2167
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,470✔
2168
      }
2169
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
7,563✔
2170
    }
2171
  }
2172

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

2183
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
×
2184
      len = 0;
×
2185
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
2186
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
2187
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
2188
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2189
        ;
2190
      } else {
2191
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2192
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2193
                        sdbStatusName(pAction->pRaw->status));
×
2194
      }
2195
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2196
    }
2197
  }
2198
}
175✔
2199

2200
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
7,483✔
2201
  SMnode *pMnode = pReq->info.node;
7,483✔
2202
  SSdb   *pSdb = pMnode->pSdb;
7,483✔
2203
  int32_t numOfRows = 0;
7,483✔
2204
  STrans *pTrans = NULL;
7,483✔
2205
  int32_t cols = 0;
7,483✔
2206
  int32_t code = 0;
7,483✔
2207
  int32_t lino = 0;
7,483✔
2208

2209
  while (numOfRows < rows) {
7,659!
2210
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
7,659✔
2211
    if (pShow->pIter == NULL) break;
7,667✔
2212

2213
    cols = 0;
174✔
2214

2215
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
174✔
2216
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
175!
2217

2218
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
175✔
2219
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
175!
2220
                        _OVER);
2221

2222
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
175✔
2223
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
175✔
2224
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
175✔
2225
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
175!
2226

2227
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
175✔
2228
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
175✔
2229
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
175✔
2230
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
175!
2231

2232
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
175✔
2233
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
175✔
2234
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
175✔
2235
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
175!
2236

2237
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
175✔
2238
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
175✔
2239
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
175✔
2240
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
175!
2241

2242
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
175!
2243
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
175✔
2244
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
175✔
2245
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
175✔
2246
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
175!
2247

2248
    /*
2249
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2250
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2251
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2252
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2253
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2254
    */
2255

2256
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
175✔
2257
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
175!
2258
                        _OVER);
2259

2260
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
175✔
2261
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
175!
2262
                        _OVER);
2263

2264
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
175✔
2265
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
175✔
2266
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
525✔
2267
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
175✔
2268
    SEpSet  epset = pTrans->lastEpset;
175✔
2269
    if (epset.numOfEps > 0) {
175✔
2270
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
342!
2271
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
342✔
2272
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
464✔
2273
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
293✔
2274
      }
2275
    }
2276
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
175✔
2277
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
175✔
2278
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
175!
2279

2280
    mndTransLogAction(pTrans);
175✔
2281

2282
    numOfRows++;
175✔
2283
    sdbRelease(pSdb, pTrans);
175✔
2284
  }
2285

2286
_OVER:
×
2287
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
7,493!
2288
  pShow->numOfRows += numOfRows;
7,493✔
2289
  return numOfRows;
7,493✔
2290
}
2291

2292
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
×
2293
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2294
  int32_t code = 0;
×
2295
  int32_t lino = 0;
×
2296
  int32_t len = 0;
×
2297

2298
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
2299
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&transactionId, false), &lino, _OVER);
×
2300

2301
  char action[30 + 1] = {0};
×
2302
  if (curActionId == pAction->id) {
×
2303
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
×
2304
                    mndTransTypeStr(pAction->actionType));
2305
  } else {
2306
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
×
2307
                    mndTransTypeStr(pAction->actionType));
2308
  }
2309
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
×
2310
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
×
2311
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
2312
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
×
2313
_OVER:
×
2314
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2315
  return code;
×
2316
}
2317

2318
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
×
2319
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2320
  int32_t code = 0;
×
2321
  int32_t lino = 0;
×
2322
  int32_t len = 0;
×
2323
  int32_t cols = 0;
×
2324

2325
  cols = 0;
×
2326

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

2329
  if (pAction->actionType == TRANS_ACTION_MSG) {
×
2330
    int32_t len = 0;
×
2331

2332
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2333
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
×
2334
                    pAction->msgSent, pAction->msgReceived);
×
2335
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2336
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2337
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2338
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2339

2340
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2341
    len = 0;
×
2342
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
×
2343
                    tstrerror(pAction->errCode));
2344
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2345
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2346
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2347
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2348

2349
    char target[TSDB_TRANS_TARGET_LEN] = {0};
×
2350
    len = 0;
×
2351
    SEpSet epset = pAction->epSet;
×
2352
    if (epset.numOfEps > 0) {
×
2353
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
×
2354
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
×
2355
      }
2356
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
×
2357
    }
2358
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2359
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2360
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2361
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2362

2363
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2364
    len = 0;
×
2365
    char bufStart[40] = {0};
×
2366
    if (pAction->startTime > 0) (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
×
2367
    char bufEnd[40] = {0};
×
2368
    if (pAction->endTime > 0) (void)formatTimestamp(bufEnd, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
×
2369
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
×
2370
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2371
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2372
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2373
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2374

2375
  } else {
2376
    int32_t len = 0;
×
2377

2378
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2379
    if (pAction->pRaw->type == SDB_VGROUP) {
×
2380
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
×
2381
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
×
2382
      len += snprintf(objType + len, sizeof(objType) - len, "%s(%d)", sdbTableName(pAction->pRaw->type), pVgroup->vgId);
×
2383
      taosMemoryFreeClear(pRow);
×
2384
    } else {
2385
      strcpy(objType, sdbTableName(pAction->pRaw->type));
×
2386
    }
2387
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2388
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2389
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2390
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2391

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

2400
    char target[TSDB_TRANS_TARGET_LEN] = "";
×
2401
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2402
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2403
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2404
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2405

2406
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2407
    len = 0;
×
2408
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
×
2409
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2410
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2411
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2412
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2413
  }
2414

2415
_OVER:
×
2416
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2417
}
2418

2419
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
×
2420
  if (stage == TRN_STAGE_PREPARE) {
×
2421
    return pTrans->prepareActions;
×
2422
  }
2423
  if (stage == TRN_STAGE_REDO_ACTION) {
×
2424
    return pTrans->redoActions;
×
2425
  }
2426
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
2427
    return pTrans->commitActions;
×
2428
  }
2429
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
2430
    return pTrans->undoActions;
×
2431
  }
2432
  return NULL;
×
2433
}
2434

2435
typedef struct STransDetailIter {
2436
  void     *pIter;
2437
  STrans   *pTrans;
2438
  ETrnStage stage;
2439
  int32_t   num;
2440
} STransDetailIter;
2441

2442
static void mndTransShowActions(SSdb *pSdb, STransDetailIter *pShowIter, SShowObj *pShow, SSDataBlock *pBlock,
×
2443
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
2444
  int32_t actionNum = taosArrayGetSize(pActions);
×
2445
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
×
2446

2447
  for (int32_t i = start; i < actionNum; ++i) {
×
2448
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
×
2449
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
×
2450
    (*numOfRows)++;
×
2451
    if (*numOfRows >= rows) break;
×
2452
  }
2453

2454
  if (*numOfRows == end) {
×
2455
    sdbRelease(pSdb, pShowIter->pTrans);
×
2456
    pShowIter->pTrans = NULL;
×
2457
    pShowIter->num = 0;
×
2458
  } else {
2459
    pShowIter->pTrans = pShowIter->pTrans;
×
2460
    pShowIter->stage = pShowIter->pTrans->stage;
×
2461
    pShowIter->num += (*numOfRows);
×
2462
  }
2463
}
×
2464

2465
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2466
  SMnode *pMnode = pReq->info.node;
×
2467
  SSdb   *pSdb = pMnode->pSdb;
×
2468
  int32_t numOfRows = 0;
×
2469

2470
  int32_t code = 0;
×
2471
  int32_t lino = 0;
×
2472

2473
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
×
2474
        pShow->pIter);
2475

2476
  if (pShow->pIter == NULL) {
×
2477
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
×
2478
    if (pShow->pIter == NULL) {
×
2479
      mError("failed to malloc for pShow->pIter");
×
2480
      return 0;
×
2481
    }
2482
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
×
2483
  }
2484

2485
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
×
2486

2487
  while (numOfRows < rows) {
×
2488
    if (pShowIter->pTrans == NULL) {
×
2489
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
×
2490
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
×
2491
      if (pShowIter->pIter == NULL) break;
×
2492
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
×
2493
            pShowIter->pTrans->stage, pShowIter->num);
2494

2495
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
×
2496

2497
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
×
2498
      break;
×
2499
    } else {
2500
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
×
2501
            pShowIter->num);
2502
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
×
2503

2504
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
×
2505
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
×
2506
      break;
×
2507
    }
2508
  }
2509

2510
_OVER:
×
2511
  pShow->numOfRows += numOfRows;
×
2512

2513
  if (code != 0) {
×
2514
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2515
  } else {
2516
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
×
2517
  }
2518
  if (numOfRows == 0) {
×
2519
    taosMemoryFree(pShow->pIter);
×
2520
    pShow->pIter = NULL;
×
2521
  }
2522
  return numOfRows;
×
2523
}
2524

2525
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
2526
  SSdb *pSdb = pMnode->pSdb;
×
2527
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
2528
}
×
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