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

taosdata / TDengine / #3568

27 Dec 2024 02:27PM UTC coverage: 63.286% (+0.6%) from 62.733%
#3568

push

travis-ci

web-flow
Merge pull request #29378 from taosdata/revert-29377-revert-29165-enh/TD-29974-improve-trans

Revert "Revert "Enh:[td 29974]improve trans""

139941 of 284289 branches covered (49.22%)

Branch coverage included in aggregate %.

105 of 355 new or added lines in 12 files covered. (29.58%)

4077 existing lines in 132 files now uncovered.

218122 of 281494 relevant lines covered (77.49%)

19355607.91 hits per line

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

62.06
/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; }
482,630✔
58

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

66
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
482,418✔
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,721✔
79
  SSdbTable table = {
1,721✔
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,721✔
90
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
1,721✔
91

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

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

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

104
  for (int32_t i = 0; i < actionNum; ++i) {
1,753,140✔
105
    STransAction *pAction = taosArrayGet(pArray, i);
1,198,032✔
106
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,198,032✔
107
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
738,316✔
108
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
459,716!
109
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
459,716✔
110
    } else {
111
      // empty
112
    }
113
    rawDataLen += sizeof(int8_t);
1,198,032✔
114
  }
115

116
  return rawDataLen;
555,108✔
117
}
118

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

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

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

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

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

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

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

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

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

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

207
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
138,777!
208
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
138,777!
209
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
138,777!
210
  if (pTrans->param != NULL) {
138,777!
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)
138,777!
215

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

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

231
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
138,777!
232
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
138,777!
233

234
  terrno = 0;
138,777✔
235

236
_OVER:
138,777✔
237
  if (terrno != 0) {
138,777!
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);
138,777✔
245
  return pRaw;
138,777✔
246
}
247

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

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

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

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

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

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

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

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

332
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
262,860!
333

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

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

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

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

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

380
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
262,860!
381
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
262,860!
382
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
262,860!
383
  if (pTrans->paramLen != 0) {
262,860!
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);
262,860!
390

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

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

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

407
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
262,860!
408

409
  terrno = 0;
262,860✔
410

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

425
static const char *mndTransStr(ETrnStage stage) {
2,164,852✔
426
  switch (stage) {
2,164,852!
427
    case TRN_STAGE_PREPARE:
133,848✔
428
      return "prepare";
133,848✔
429
    case TRN_STAGE_REDO_ACTION:
954,057✔
430
      return "redoAction";
954,057✔
431
    case TRN_STAGE_ROLLBACK:
28✔
432
      return "rollback";
28✔
433
    case TRN_STAGE_UNDO_ACTION:
43,730✔
434
      return "undoAction";
43,730✔
435
    case TRN_STAGE_COMMIT:
219,143✔
436
      return "commit";
219,143✔
437
    case TRN_STAGE_COMMIT_ACTION:
486,795✔
438
      return "commitAction";
486,795✔
439
    case TRN_STAGE_FINISH:
327,223✔
440
      return "finished";
327,223✔
441
    case TRN_STAGE_PRE_FINISH:
28✔
442
      return "pre-finish";
28✔
443
    default:
×
444
      return "invalid";
×
445
  }
446
}
447

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

459
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
651,417✔
460
  if (pAction != NULL) {
651,417✔
461
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
538,302✔
462
      pTrans->lastAction = pAction->id;
350,336✔
463
      pTrans->lastMsgType = pAction->msgType;
350,336✔
464
      pTrans->lastEpset = pAction->epSet;
350,336✔
465
      pTrans->lastErrorNo = pAction->errCode;
350,336✔
466
    }
467
  } else {
468
    pTrans->lastAction = 0;
113,115✔
469
    pTrans->lastMsgType = 0;
113,115✔
470
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
113,115✔
471
    pTrans->lastErrorNo = 0;
113,115✔
472
  }
473
}
651,417✔
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,048✔
484
  switch (ftype) {
2,048!
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,024✔
490
      return mndRebCntInc;
1,024✔
491
    case TRANS_STOP_FUNC_MQ_REB:
1,024✔
492
      return mndRebCntDec;
1,024✔
493
    default:
×
494
      return NULL;
×
495
  }
496
}
497

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

502
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
48,923✔
503

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

512
  if (pTrans->stage == TRN_STAGE_COMMIT) {
48,923✔
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) {
48,923!
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) {
48,923!
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;
48,923✔
528
}
529

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

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

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

579
  mndTransDropData(pTrans);
155,868✔
580
  return 0;
155,868✔
581
}
582

583
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
232,964✔
584
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
759,129✔
585
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
526,165✔
586
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
526,165✔
587
    pOldAction->rawWritten = pNewAction->rawWritten;
526,165✔
588
    pOldAction->msgSent = pNewAction->msgSent;
526,165✔
589
    pOldAction->msgReceived = pNewAction->msgReceived;
526,165✔
590
    pOldAction->errCode = pNewAction->errCode;
526,165✔
591
  }
592
}
232,964✔
593

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

598
  if (pOld->createdTime != pNew->createdTime) {
58,241!
UNCOV
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
UNCOV
604
    TAOS_RETURN(TSDB_CODE_MND_TRANS_SYNC_TIMEOUT);
×
605
  }
606

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

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

619
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
58,241✔
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) {
58,241✔
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;
58,241✔
630
}
631

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

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

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

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

679
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
40,953!
680
      pTrans->pRpcArray == NULL) {
40,953!
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) {
40,953✔
688
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
54,984!
689
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
690
      return NULL;
×
691
    }
692
    pTrans->originRpcType = pReq->msgType;
27,492✔
693
  }
694

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

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

701
static void mndTransDropActions(SArray *pArray) {
1,215,252✔
702
  int32_t size = taosArrayGetSize(pArray);
1,215,252✔
703
  for (int32_t i = 0; i < size; ++i) {
3,783,597✔
704
    STransAction *pAction = taosArrayGet(pArray, i);
2,568,345✔
705
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,568,345✔
706
      taosMemoryFreeClear(pAction->pRaw);
1,561,981!
707
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,006,364!
708
      taosMemoryFreeClear(pAction->pCont);
1,006,364!
709
    } else {
710
      // nothing
711
    }
712
  }
713

714
  taosArrayDestroy(pArray);
1,215,252✔
715
}
1,215,252✔
716

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

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

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

733
  return 0;
340,789✔
734
}
735

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

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

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

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

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

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

781
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
1,012✔
782
  pTrans->startFunc = startFunc;
1,012✔
783
  pTrans->stopFunc = stopFunc;
1,012✔
784
  pTrans->param = param;
1,012✔
785
  pTrans->paramLen = paramLen;
1,012✔
786
}
1,012✔
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,023✔
820
  if (dbname != NULL) {
29,023!
821
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
29,023✔
822
  }
823
  if (stbname != NULL) {
29,023✔
824
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
21,932✔
825
  }
826
}
29,023✔
827

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

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

NEW
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

UNCOV
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,663✔
848

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

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

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

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

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

887
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
26,105✔
888
  if (conflict) {
26,105✔
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,
287!
890
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
891
    *globalConflict = true;
287✔
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,
25,818!
894
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
895
  }
896
}
26,105✔
897

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

903
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
118,893✔
904

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

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

912
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
23,627!
913

914
    if (pNew->conflict == TRN_CONFLICT_DB) {
23,627✔
915
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
3,663✔
916
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
3,663✔
917
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
2,533✔
918
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
2,533✔
919
      }
920
    }
921

922
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
23,627✔
923
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
19,964✔
924
      if (pTrans->conflict == TRN_CONFLICT_DB) {
19,964✔
925
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,797✔
926
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,797✔
927
      }
928
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
19,964✔
929
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
17,445✔
930
      }
931
    }
932

933
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
23,627!
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) {
23,627!
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);
23,627✔
960
  }
961

962
  return conflict;
82,711✔
963
}
964

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

981
  TAOS_RETURN(code);
118,579✔
982
}
983

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

990
  while (1) {
4✔
991
    bool thisConflict = false;
318✔
992
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
318✔
993
    if (pIter == NULL) break;
318✔
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) {
314✔
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);
310✔
1020
}
1021

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

1039
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
40,606✔
1040
  int32_t code = 0;
40,606✔
1041
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
40,606✔
1042
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
37,857!
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) {
37,857✔
1049
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
11,489!
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);
40,606✔
1058
}
1059

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

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

1082
  mInfo("trans:%d, action list:", pTrans->id);
40,606!
1083
  int32_t index = 0;
40,606✔
1084
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
60,360✔
1085
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
19,754✔
1086
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
19,754!
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) {
128,745✔
1091
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
88,139✔
1092
    mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
88,139!
1093
          mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1094
  }
1095

1096
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
229,521✔
1097
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
188,915✔
1098
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
188,915!
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) {
84,140✔
1103
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
43,534✔
1104
    if(pAction->actionType == TRANS_ACTION_MSG){
43,534✔
1105
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
30,888!
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,646!
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));
40,606!
1116

1117
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
40,606!
1118

1119
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
40,606!
1120

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

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

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

1153
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
40,584✔
1154
  int32_t code = 0;
40,584✔
1155
  mInfo("trans:%d, commit transaction", pTrans->id);
40,584!
1156
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
40,584✔
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);
40,571!
1161
  TAOS_RETURN(code);
40,571✔
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) {
243,936✔
1187
  bool    sendRsp = false;
243,936✔
1188
  int32_t code = pTrans->code;
243,936✔
1189

1190
  if (pTrans->stage == TRN_STAGE_FINISH) {
243,936✔
1191
    sendRsp = true;
89,282✔
1192
  }
1193

1194
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
243,936✔
1195
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
61,853!
1196
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
30✔
1197
      sendRsp = true;
30✔
1198
    }
1199
  } else {
1200
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
182,083✔
1201
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
120,160!
1202
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1203
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1204
      } else {
1205
        if (pTrans->failedTimes > 6) sendRsp = true;
120,160✔
1206
      }
1207
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
120,160✔
1208
    }
1209
  }
1210

1211
  if (!sendRsp) {
243,936✔
1212
    return;
154,622✔
1213
  } else {
1214
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
89,314!
1215
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1216
  }
1217

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

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

1236
      if (i != 0 && code == 0) {
25,632!
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,
25,632!
1240
            mndTransStr(pTrans->stage), pInfo->ahandle);
1241

1242
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
25,632✔
1243

1244
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
25,632✔
1245
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
3,641!
1246
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
3,641✔
1247
        if (pDb != NULL) {
3,641!
1248
          for (int32_t j = 0; j < 12; j++) {
4,445✔
1249
            bool ready = mndIsDbReady(pMnode, pDb);
4,439✔
1250
            if (!ready) {
4,439✔
1251
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
804!
1252
              taosMsleep(1000);
804✔
1253
            } else {
1254
              break;
3,635✔
1255
            }
1256
          }
1257
        }
1258
        mndReleaseDb(pMnode, pDb);
3,641✔
1259
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
21,991✔
1260
        void   *pCont = NULL;
6,846✔
1261
        int32_t contLen = 0;
6,846✔
1262
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
6,846✔
1263
          mndTransSetRpcRsp(pTrans, pCont, contLen);
6,842✔
1264
        }
1265
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
15,145✔
1266
        void   *pCont = NULL;
456✔
1267
        int32_t contLen = 0;
456✔
1268
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
456!
1269
          mndTransSetRpcRsp(pTrans, pCont, contLen);
456✔
1270
        }
1271
      }
1272

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

1282
      tmsgSendRsp(&rspMsg);
25,632✔
1283

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

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

1299
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
91,699✔
1300
  if (pTrans == NULL) {
91,699!
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;
91,699✔
1308
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
91,699✔
1309
    pArray = pTrans->redoActions;
91,683✔
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) {
91,699!
1318
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1319
    goto _OVER;
×
1320
  }
1321

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

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

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

1337
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
91,699!
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);
91,699✔
1345

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

1351
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
5,167✔
1352
  pAction->rawWritten = 0;
5,167✔
1353
  pAction->msgSent = 0;
5,167✔
1354
  pAction->msgReceived = 0;
5,167✔
1355
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
5,167!
1356
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
5,167✔
1357
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
12✔
1358
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
12!
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);
5,155!
1362
  }
1363
  pAction->errCode = 0;
5,167✔
1364
}
5,167✔
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) {
425,839✔
1382
  if (pAction->rawWritten) return 0;
425,839✔
1383
  if (topHalf) {
235,318!
1384
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
×
1385
  }
1386

1387
  if (pAction->pRaw->type >= SDB_MAX) {
235,318!
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);
235,318✔
1396
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
235,318!
1397
    pAction->rawWritten = true;
235,318✔
1398
    pAction->errCode = 0;
235,318✔
1399
    code = 0;
235,318✔
1400
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
235,318!
1401
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1402

1403
    mndSetTransLastAction(pTrans, pAction);
235,318✔
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);
235,318✔
1412
}
1413

1414
// execute in trans context
1415
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
619,598✔
1416
  if (pAction->msgSent) return 0;
619,598✔
1417
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
129,312✔
1418
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
37,501✔
1419
  }
1420

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

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

1434
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
91,811✔
1435

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

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

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

UNCOV
1461
    mndSetTransLastAction(pTrans, pAction);
×
1462
  }
1463

1464
  TAOS_RETURN(code);
91,811✔
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,045,437✔
1478
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,045,437✔
1479
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
425,839✔
1480
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
619,598!
1481
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf);
619,598✔
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) {
224,134✔
1488
  int32_t numOfActions = taosArrayGetSize(pArray);
224,134✔
1489
  int32_t code = 0;
224,134✔
1490

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

1501
  return code;
224,134✔
1502
}
1503

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

1509
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf)) != 0) {
224,134✔
1510
    return code;
29,870✔
1511
  }
1512

1513
  int32_t       numOfExecuted = 0;
194,264✔
1514
  int32_t       errCode = 0;
194,264✔
1515
  STransAction *pErrAction = NULL;
194,264✔
1516
  for (int32_t action = 0; action < numOfActions; ++action) {
1,146,766✔
1517
    STransAction *pAction = taosArrayGet(pArray, action);
952,502✔
1518
    if (pAction->msgReceived || pAction->rawWritten) {
952,502✔
1519
      numOfExecuted++;
674,001✔
1520
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
674,001✔
1521
        errCode = pAction->errCode;
6✔
1522
        pErrAction = pAction;
6✔
1523
      }
1524
    } else {
1525
      pErrAction = pAction;
278,501✔
1526
    }
1527
  }
1528

1529
  mndSetTransLastAction(pTrans, pErrAction);
194,264✔
1530

1531
  if (numOfExecuted == numOfActions) {
194,264✔
1532
    if (errCode == 0) {
113,119✔
1533
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
113,115!
1534
      return 0;
113,115✔
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);
81,145!
1543

1544
    for (int32_t action = 0; action < numOfActions; ++action) {
556,576✔
1545
      STransAction *pAction = taosArrayGet(pArray, action);
475,431✔
1546
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
475,431✔
1547
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1548
             pAction->acceptableCode, pAction->retryCode);
1549
      if (pAction->msgSent) {
475,431!
1550
        if (pAction->msgReceived) {
475,431✔
1551
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
196,930✔
1552
            mndTransResetAction(pMnode, pTrans, pAction);
2✔
1553
            mInfo("trans:%d, %s:%d reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
2!
1554
          }
1555
        }
1556
      }
1557
    }
1558
    return TSDB_CODE_ACTION_IN_PROGRESS;
81,145✔
1559
  }
1560
}
1561

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

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

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

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

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

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

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

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

1608
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
38,325✔
1609
    if (code == 0) {
38,325✔
1610
      if (pAction->msgSent) {
30,694✔
1611
        if (pAction->msgReceived) {
27,109✔
1612
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
12,098✔
1613
            code = pAction->errCode;
5,161✔
1614
            mndTransResetAction(pMnode, pTrans, pAction);
5,161✔
1615
          } else {
1616
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
6,937!
1617
          }
1618
        } else {
1619
          code = TSDB_CODE_ACTION_IN_PROGRESS;
15,011✔
1620
        }
1621
      } else if (pAction->rawWritten) {
3,585!
1622
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,585!
1623
          code = pAction->errCode;
×
1624
        } else {
1625
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,585!
1626
        }
1627
      } else {
1628
      }
1629
    }
1630

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

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

1644
    if (code == 0) {
28,747✔
1645
      pTrans->code = 0;
8,575✔
1646
      pTrans->actionPos++;
8,575✔
1647
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
8,575!
1648
            pAction->id);
1649
      (void)taosThreadMutexUnlock(&pTrans->mutex);
8,575✔
1650
      code = mndTransSync(pMnode, pTrans);
8,575✔
1651
      (void)taosThreadMutexLock(&pTrans->mutex);
8,575✔
1652
      if (code != 0) {
8,575!
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) {
20,172✔
1660
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
15,011!
1661
      break;
15,011✔
1662
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
5,161!
1663
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
30✔
1664
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
5,143!
1665
            code, tstrerror(code));
1666
      pTrans->lastErrorNo = code;
5,143✔
1667
      taosMsleep(300);
5,143✔
1668
      action--;
5,143✔
1669
      continue;
5,143✔
1670
    } else {
1671
      terrno = code;
18✔
1672
      pTrans->lastErrorNo = code;
18✔
1673
      pTrans->code = code;
18✔
1674
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
18!
1675
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1676
      break;
18✔
1677
    }
1678
  }
1679

1680
  return code;
27,353✔
1681
}
1682

1683
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
30,435✔
1684
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
30,435✔
1685
  (void)taosThreadMutexLock(&pTrans->mutex);
30,435✔
1686
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
30,435!
1687
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
30,435✔
1688
  }
1689
  (void)taosThreadMutexUnlock(&pTrans->mutex);
30,435✔
1690
  return code;
30,435✔
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) {
48,748✔
1704
  bool    continueExec = true;
48,748✔
1705
  int32_t code = 0;
48,748✔
1706
  terrno = 0;
48,748✔
1707

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

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

1713
  for (int32_t action = 0; action < numOfActions; ++action) {
40,478✔
1714
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
24,740✔
1715
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
24,740✔
1716
    if (code != 0) {
24,740!
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:
15,738✔
1725
  pTrans->stage = TRN_STAGE_REDO_ACTION;
48,748✔
1726
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
48,748!
1727
  return continueExec;
48,748✔
1728
}
1729

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

1735
  if (pTrans->exec == TRN_EXEC_SERIAL) {
195,202✔
1736
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
30,435✔
1737
  } else {
1738
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf);
164,767✔
1739
  }
1740

1741
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
195,202!
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)) {
195,202✔
1752
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
58,462!
1753
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1754
      return false;
58,462✔
1755
    }
1756
  }
1757
  terrno = code;
136,740✔
1758

1759
  if (code == 0) {
136,740✔
1760
    pTrans->code = 0;
40,584✔
1761
    pTrans->stage = TRN_STAGE_COMMIT;
40,584✔
1762
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
40,584!
1763
    continueExec = true;
40,584✔
1764
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
96,156!
1765
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
96,134!
1766
    continueExec = false;
96,134✔
1767
  } else {
1768
    pTrans->failedTimes++;
22✔
1769
    pTrans->code = terrno;
22✔
1770
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
22✔
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);
18!
1791
      continueExec = false;
18✔
1792
    }
1793
  }
1794

1795
  return continueExec;
136,740✔
1796
}
1797

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

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

1805
  if (code == 0) {
40,584✔
1806
    pTrans->code = 0;
40,571✔
1807
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
40,571✔
1808
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
40,571!
1809
    continueExec = true;
40,571✔
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;
40,584✔
1818
}
1819

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

1824
  if (code == 0) {
89,267!
1825
    pTrans->code = 0;
89,267✔
1826
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
89,267✔
1827
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
89,267!
1828
    continueExec = true;
89,267✔
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;
89,267✔
1842
}
1843

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

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

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

1857
  if (code == 0) {
26✔
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) {
22!
1862
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
22!
1863
    continueExec = false;
22✔
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;
26✔
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) {
89,279✔
1913
  bool continueExec = false;
89,279✔
1914
  if (topHalf) return continueExec;
89,279✔
1915

1916
  SSdbRaw *pRaw = mndTransEncode(pTrans);
48,704✔
1917
  if (pRaw == NULL) {
48,704!
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));
48,704!
1922

1923
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
48,704✔
1924
  if (code != 0) {
48,704!
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,
48,704!
1929
        pTrans->failedTimes, pTrans->createdTime);
1930
  return continueExec;
48,704✔
1931
}
1932

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

1936
  while (continueExec) {
658,310✔
1937
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64 "", pTrans->id,
414,374!
1938
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1939
    pTrans->lastExecTime = taosGetTimestampMs();
414,374✔
1940
    switch (pTrans->stage) {
414,374!
1941
      case TRN_STAGE_PREPARE:
×
1942
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1943
        break;
×
1944
      case TRN_STAGE_REDO_ACTION:
195,202✔
1945
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf);
195,202✔
1946
        break;
195,202✔
1947
      case TRN_STAGE_COMMIT:
40,584✔
1948
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
40,584✔
1949
        break;
40,584✔
1950
      case TRN_STAGE_COMMIT_ACTION:
89,267✔
1951
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
89,267✔
1952
        break;
89,267✔
1953
      case TRN_STAGE_ROLLBACK:
4✔
1954
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
4✔
1955
        break;
4✔
1956
      case TRN_STAGE_UNDO_ACTION:
34✔
1957
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf);
34✔
1958
        break;
34✔
1959
      case TRN_STAGE_PRE_FINISH:
4✔
1960
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
4✔
1961
        break;
4✔
1962
      case TRN_STAGE_FINISH:
89,279✔
1963
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
89,279✔
1964
        break;
89,279✔
1965
      default:
×
1966
        continueExec = false;
×
1967
        break;
×
1968
    }
1969
  }
1970

1971
  mndTransSendRpcRsp(pMnode, pTrans);
243,936✔
1972
}
243,936✔
1973

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

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

1986
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
31,008✔
1987
  mTrace("start to process trans timer");
31,008✔
1988
  mndTransPullup(pReq->info.node);
31,008✔
1989
  return 0;
31,008✔
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

NEW
2002
  if(pTrans->ableToBeKilled == false){
×
NEW
2003
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2004
  }
2005
  
NEW
2006
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
×
NEW
2007
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
NEW
2008
      STransAction *pAction = taosArrayGet(pArray, i);
×
NEW
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));
NEW
2011
      pAction->msgSent = 1;
×
NEW
2012
      pAction->msgReceived = 1;
×
NEW
2013
      pAction->errCode = 0;
×
2014
    }
2015
  }
NEW
2016
  else if(pTrans->killMode == TRN_KILL_MODE_INTERUPT){
×
NEW
2017
    pTrans->stage = TRN_STAGE_PRE_FINISH;
×
2018
  }
2019
  else{
NEW
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; }
255✔
2062

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

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

2079
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
31,485✔
2080

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

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

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

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

2118
  return buf;
11,228✔
2119
}
2120

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

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

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

2145
        char endStart[40] = {0};
5,614✔
2146
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
5,614✔
2147
        len += snprintf(detail + len, sizeof(detail) - len,
11,228!
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,
5,614✔
2151
                        pAction->msgReceived, bufStart, endStart);
5,614✔
2152

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

2162
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
5,614✔
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,355✔
2166
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,355✔
2167
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,355✔
2168
      }
2169
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
6,969✔
2170
    }
2171
  }
2172

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

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

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

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

2213
    cols = 0;
187✔
2214

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

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

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

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

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

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

2242
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
187!
2243
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
187✔
2244
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
187✔
2245
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
187✔
2246
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
187!
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++);
187✔
2257
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
187!
2258
                        _OVER);
2259

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

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

2280
    mndTransLogAction(pTrans);
187✔
2281

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

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

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

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

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

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

NEW
2325
  cols = 0;
×
2326

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

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

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

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

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

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

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

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

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

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

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

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

NEW
2419
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
×
NEW
2420
  if (stage == TRN_STAGE_PREPARE) {
×
NEW
2421
    return pTrans->prepareActions;
×
2422
  }
NEW
2423
  if (stage == TRN_STAGE_REDO_ACTION) {
×
NEW
2424
    return pTrans->redoActions;
×
2425
  }
NEW
2426
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
NEW
2427
    return pTrans->commitActions;
×
2428
  }
NEW
2429
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
NEW
2430
    return pTrans->undoActions;
×
2431
  }
NEW
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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