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

taosdata / TDengine / #3858

17 Apr 2025 01:40PM UTC coverage: 62.968% (+0.5%) from 62.513%
#3858

push

travis-ci

web-flow
docs(opc): add perssit data support (#30783)

156194 of 316378 branches covered (49.37%)

Branch coverage included in aggregate %.

242021 of 316027 relevant lines covered (76.58%)

19473613.85 hits per line

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

64.17
/source/dnode/mnode/impl/src/mndTrans.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndTrans.h"
18
#include "mndDb.h"
19
#include "mndPrivilege.h"
20
#include "mndShow.h"
21
#include "mndStb.h"
22
#include "mndSubscribe.h"
23
#include "mndSync.h"
24
#include "mndUser.h"
25
#include "mndVgroup.h"
26
#include "osTime.h"
27

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

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

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

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

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

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

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

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

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

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

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

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

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

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

105
  for (int32_t i = 0; i < actionNum; ++i) {
1,984,103✔
106
    STransAction *pAction = taosArrayGet(pArray, i);
1,329,699✔
107
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,329,699✔
108
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
837,591✔
109
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
492,108!
110
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
492,108✔
111
    } else {
112
      // empty
113
    }
114
    rawDataLen += sizeof(int8_t);
1,329,699✔
115
  }
116

117
  return rawDataLen;
654,404✔
118
}
119

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

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

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

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

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

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

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

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

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

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

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

215
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
163,601!
216

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

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

232
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
163,601!
233
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
163,601!
234

235
  terrno = 0;
163,601✔
236

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

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

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

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

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

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

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

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

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

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

333
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
307,638!
334

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

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

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

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

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

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

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

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

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

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

408
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
307,638!
409

410
  terrno = 0;
307,638✔
411

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

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

426
static const char *mndTransStr(ETrnStage stage) {
2,463,657✔
427
  switch (stage) {
2,463,657✔
428
    case TRN_STAGE_PREPARE:
157,473✔
429
      return "prepare";
157,473✔
430
    case TRN_STAGE_REDO_ACTION:
1,048,105✔
431
      return "redoAction";
1,048,105✔
432
    case TRN_STAGE_ROLLBACK:
79✔
433
      return "rollback";
79✔
434
    case TRN_STAGE_UNDO_ACTION:
50,580✔
435
      return "undoAction";
50,580✔
436
    case TRN_STAGE_COMMIT:
261,450✔
437
      return "commit";
261,450✔
438
    case TRN_STAGE_COMMIT_ACTION:
561,804✔
439
      return "commitAction";
561,804✔
440
    case TRN_STAGE_FINISH:
384,101✔
441
      return "finished";
384,101✔
442
    case TRN_STAGE_PRE_FINISH:
64✔
443
      return "pre-finish";
64✔
444
    default:
1✔
445
      return "invalid";
1✔
446
  }
447
}
448

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

460
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
728,816✔
461
  if (pAction != NULL) {
728,816✔
462
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
596,756✔
463
      pTrans->lastAction = pAction->id;
389,636✔
464
      pTrans->lastMsgType = pAction->msgType;
389,636✔
465
      pTrans->lastEpset = pAction->epSet;
389,636✔
466
      pTrans->lastErrorNo = pAction->errCode;
389,636✔
467
    }
468
  } else {
469
    pTrans->lastAction = 0;
132,060✔
470
    pTrans->lastMsgType = 0;
132,060✔
471
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
132,060✔
472
    pTrans->lastErrorNo = 0;
132,060✔
473
  }
474
}
728,816✔
475

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

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

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

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

503
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
58,062✔
504

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

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

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

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

528
  return 0;
58,062✔
529
}
530

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

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

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

580
  mndTransDropData(pTrans);
182,827✔
581
  return 0;
182,827✔
582
}
583

584
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
267,852✔
585
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
833,055✔
586
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
565,203✔
587
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
565,203✔
588
    pOldAction->rawWritten = pNewAction->rawWritten;
565,203✔
589
    pOldAction->msgSent = pNewAction->msgSent;
565,203✔
590
    pOldAction->msgReceived = pNewAction->msgReceived;
565,203✔
591
    pOldAction->errCode = pNewAction->errCode;
565,203✔
592
  }
593
}
267,852✔
594

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

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

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

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

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

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

630
  return 0;
66,963✔
631
}
632

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

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

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

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

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

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

688
  if (pReq != NULL) {
49,063✔
689
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
59,600!
690
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
691
      return NULL;
×
692
    }
693
    pTrans->originRpcType = pReq->msgType;
29,800✔
694
  }
695

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

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

702
static void mndTransDropActions(SArray *pArray) {
1,426,804✔
703
  int32_t size = taosArrayGetSize(pArray);
1,426,804✔
704
  for (int32_t i = 0; i < size; ++i) {
4,260,606✔
705
    STransAction *pAction = taosArrayGet(pArray, i);
2,833,802✔
706
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,833,802✔
707
      taosMemoryFreeClear(pAction->pRaw);
1,763,885!
708
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,069,917!
709
      taosMemoryFreeClear(pAction->pCont);
1,069,917!
710
    } else {
711
      // nothing
712
    }
713
  }
714

715
  taosArrayDestroy(pArray);
1,426,804✔
716
}
1,426,804✔
717

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

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

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

734
  return 0;
387,066✔
735
}
736

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

882
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
19,926✔
883
  if (conflict[0] == 0) return false;
19,926✔
884
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
18,577✔
885
  return false;
18,505✔
886
}
887

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

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

904
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
137,994✔
905

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

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

913
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
21,685!
914

915
    if (pNew->conflict == TRN_CONFLICT_DB) {
21,685✔
916
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
3,133✔
917
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
3,133✔
918
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
2,057✔
919
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
2,057✔
920
      }
921
    }
922

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

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

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

960
    sdbRelease(pMnode->pSdb, pTrans);
21,685✔
961
  }
962

963
  return conflict;
91,635✔
964
}
965

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

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

982
  TAOS_RETURN(code);
137,603✔
983
}
984

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

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

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

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

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

1020
  TAOS_RETURN(code);
347✔
1021
}
1022

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

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

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

1058
  TAOS_RETURN(code);
48,629✔
1059
}
1060

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

1074
  TAOS_RETURN(code);
48,629✔
1075
}
1076

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

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

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

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

1103
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
98,625✔
1104
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
49,996✔
1105
    if(pAction->actionType == TRANS_ACTION_MSG){
49,996✔
1106
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
35,140!
1107
            mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1108
    }
1109
    else{
1110
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
14,856!
1111
            mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1112
    }
1113
  }
1114

1115

1116
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
48,629!
1117

1118
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
48,629!
1119

1120
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
48,629!
1121

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

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

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

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

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

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

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

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

1192
  if (pTrans->stage == TRN_STAGE_FINISH) {
281,245✔
1193
    sendRsp = true;
106,423✔
1194
  }
1195

1196
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
281,245✔
1197
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
82,589✔
1198
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
98✔
1199
      sendRsp = true;
98✔
1200
    }
1201
  } else {
1202
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
198,656✔
1203
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
131,334!
1204
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1205
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1206
      } else {
1207
        if (pTrans->failedTimes > 6) sendRsp = true;
131,334✔
1208
      }
1209
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
131,334✔
1210
    }
1211
  }
1212

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

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

1228
  for (int32_t i = 0; i < size; ++i) {
58,674✔
1229
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
29,337✔
1230
    if (pInfo->handle != NULL) {
29,337✔
1231
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
27,834!
1232
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1233
      }
1234
      if (code == TSDB_CODE_SYN_TIMEOUT) {
27,834✔
1235
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
1✔
1236
      }
1237

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

1244
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
27,834✔
1245

1246
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
27,834✔
1247
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,292!
1248
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,292✔
1249
        if (pDb != NULL) {
4,292!
1250
          for (int32_t j = 0; j < 12; j++) {
5,105✔
1251
            bool ready = mndIsDbReady(pMnode, pDb);
5,102✔
1252
            if (!ready) {
5,102✔
1253
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
813!
1254
              taosMsleep(1000);
813✔
1255
            } else {
1256
              break;
4,289✔
1257
            }
1258
          }
1259
        }
1260
        mndReleaseDb(pMnode, pDb);
4,292✔
1261
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
23,542✔
1262
        void   *pCont = NULL;
7,807✔
1263
        int32_t contLen = 0;
7,807✔
1264
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
7,807✔
1265
          mndTransSetRpcRsp(pTrans, pCont, contLen);
7,799✔
1266
        }
1267
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
15,735✔
1268
        void   *pCont = NULL;
469✔
1269
        int32_t contLen = 0;
469✔
1270
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
469!
1271
          mndTransSetRpcRsp(pTrans, pCont, contLen);
469✔
1272
        }
1273
      }
1274

1275
      if (pTrans->rpcRspLen != 0) {
27,834✔
1276
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
13,509✔
1277
        if (rpcCont != NULL) {
13,509!
1278
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
13,509✔
1279
          rspMsg.pCont = rpcCont;
13,509✔
1280
          rspMsg.contLen = pTrans->rpcRspLen;
13,509✔
1281
        }
1282
      }
1283

1284
      tmsgSendRsp(&rspMsg);
27,834✔
1285

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

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

1313
  SArray *pArray = NULL;
99,517✔
1314
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
99,517✔
1315
    pArray = pTrans->redoActions;
99,479✔
1316
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
38!
1317
    pArray = pTrans->undoActions;
38✔
1318
  } else {
1319
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1320
    goto _OVER;
×
1321
  }
1322

1323
  if (pArray == NULL) {
99,517!
1324
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1325
    goto _OVER;
×
1326
  }
1327

1328
  int32_t actionNum = taosArrayGetSize(pArray);
99,517✔
1329
  if (action < 0 || action >= actionNum) {
99,517!
1330
    mError("trans:%d, invalid action:%d", transId, action);
×
1331
    goto _OVER;
×
1332
  }
1333

1334
  STransAction *pAction = taosArrayGet(pArray, action);
99,517✔
1335
  if (pAction != NULL) {
99,517!
1336
    pAction->msgReceived = 1;
99,517✔
1337
    pAction->errCode = pRsp->code;
99,517✔
1338
    pAction->endTime = taosGetTimestampMs();
99,517✔
1339

1340
    // pTrans->lastErrorNo = pRsp->code;
1341
    mndSetTransLastAction(pTrans, pAction);
99,517✔
1342

1343
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
99,517!
1344
          mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1345
          tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1346
  } else {
1347
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1348
  }
1349

1350
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
99,517!
1351
  mndTransExecute(pMnode, pTrans, true);
99,517✔
1352

1353
_OVER:
99,517✔
1354
  mndReleaseTrans(pMnode, pTrans);
99,517✔
1355
  TAOS_RETURN(code);
99,517✔
1356
}
1357

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

1373
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
10✔
1374
  int32_t numOfActions = taosArrayGetSize(pArray);
10✔
1375

1376
  for (int32_t action = 0; action < numOfActions; ++action) {
50✔
1377
    STransAction *pAction = taosArrayGet(pArray, action);
40✔
1378
    if (pAction->msgSent && pAction->msgReceived &&
40!
1379
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
40✔
1380
      continue;
30✔
1381
    if (pAction->msgSent && !pAction->msgReceived) {
10!
1382
      int64_t timestamp = taosGetTimestampMs();
×
1383
      if (timestamp - pAction->startTime <= TRANS_ACTION_TIMEOUT) continue;
×
1384
    }
1385

1386
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
10!
1387

1388
    mndTransResetAction(pMnode, pTrans, pAction);
10✔
1389
    mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage), pAction->id,
10!
1390
          pAction->errCode, pAction->startTime);
1391
  }
1392
}
10✔
1393

1394
// execute in sync context
1395
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
487,127✔
1396
  if (pAction->rawWritten) return 0;
487,127✔
1397
  if (topHalf) {
267,993✔
1398
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
15✔
1399
  }
1400

1401
  if (pAction->pRaw->type >= SDB_MAX) {
267,978!
1402
    pAction->rawWritten = true;
×
1403
    pAction->errCode = 0;
×
1404
    mndSetTransLastAction(pTrans, pAction);
×
1405
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
1406
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1407
  }
1408

1409
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
267,978✔
1410
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
267,978!
1411
    pAction->rawWritten = true;
267,978✔
1412
    pAction->errCode = 0;
267,978✔
1413
    code = 0;
267,978✔
1414
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
267,978!
1415
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1416

1417
    mndSetTransLastAction(pTrans, pAction);
267,978✔
1418
  } else {
1419
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1420
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1421
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1422
    mndSetTransLastAction(pTrans, pAction);
×
1423
  }
1424

1425
  TAOS_RETURN(code);
267,978✔
1426
}
1427

1428
// execute in trans context
1429
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
693,722✔
1430
                                     bool notSend) {
1431
  if (pAction->msgSent) return 0;
693,722✔
1432
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
139,136✔
1433
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
39,349✔
1434
  }
1435

1436
  if (notSend) {
99,787✔
1437
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
200!
1438
    return 0;
200✔
1439
  }
1440

1441
#ifndef TD_ASTRA_32
1442
  int64_t signature = pTrans->id;
99,587✔
1443
  signature = (signature << 32);
99,587✔
1444
  signature += pAction->id;
99,587✔
1445

1446
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
99,587✔
1447
#else
1448
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1449
                    .contLen = pAction->contLen,
1450
                    .info.ahandle = (void *)pTrans->id,
1451
                    .info.ahandleEx = (void *)pAction->id};
1452
#endif
1453
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
99,587✔
1454
  if (rpcMsg.pCont == NULL) {
99,587!
1455
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1456
    return -1;
×
1457
  }
1458
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
99,587✔
1459
  rpcMsg.info.notFreeAhandle = 1;
99,587✔
1460

1461
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
99,587✔
1462

1463
  char    detail[1024] = {0};
99,587✔
1464
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
99,587!
1465
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
99,587✔
1466
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
203,822✔
1467
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
104,235✔
1468
                     pAction->epSet.eps[i].port);
104,235✔
1469
  }
1470

1471
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
99,587✔
1472
  if (code == 0) {
99,587✔
1473
    pAction->msgSent = 1;
99,583✔
1474
    // pAction->msgReceived = 0;
1475
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
99,583✔
1476
    pAction->startTime = taosGetTimestampMs();
99,583✔
1477
    pAction->endTime = 0;
99,583✔
1478
    mInfo("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
99,583!
1479

1480
    mndSetTransLastAction(pTrans, pAction);
99,583✔
1481
  } else {
1482
    pAction->msgSent = 0;
4✔
1483
    pAction->msgReceived = 0;
4✔
1484
    pAction->errCode = (terrno != 0) ? terrno : code;
4!
1485
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
4!
1486
           detail);
1487

1488
    mndSetTransLastAction(pTrans, pAction);
4✔
1489
  }
1490

1491
  TAOS_RETURN(code);
99,587✔
1492
}
1493

1494
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1495
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1496
  pAction->rawWritten = 0;
×
1497
  pAction->errCode = 0;
×
1498
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1499

1500
  mndSetTransLastAction(pTrans, pAction);
×
1501
  return 0;
×
1502
}
1503

1504
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
1,180,849✔
1505
                                        bool notSend) {
1506
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,180,849✔
1507
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
487,127✔
1508
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
693,722!
1509
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
693,722✔
1510
  } else {
1511
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1512
  }
1513
}
1514

1515
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
257,420✔
1516
  int32_t numOfActions = taosArrayGetSize(pArray);
257,420✔
1517
  int32_t code = 0;
257,420✔
1518

1519
  for (int32_t action = 0; action < numOfActions; ++action) {
1,341,682✔
1520
    STransAction *pAction = taosArrayGet(pArray, action);
1,116,267✔
1521
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
1,116,267✔
1522
    if (code != 0) {
1,116,267✔
1523
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
32,005!
1524
            numOfActions);
1525
      break;
32,005✔
1526
    }
1527
  }
1528

1529
  return code;
257,420✔
1530
}
1531

1532
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
300,540✔
1533
  int32_t numOfActions = taosArrayGetSize(pArray);
300,540✔
1534
  int32_t code = 0;
300,540✔
1535
  if (numOfActions == 0) return 0;
300,540✔
1536

1537
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
257,420✔
1538
    return code;
32,005✔
1539
  }
1540

1541
  int32_t       numOfExecuted = 0;
225,415✔
1542
  int32_t       errCode = 0;
225,415✔
1543
  STransAction *pErrAction = NULL;
225,415✔
1544
  for (int32_t action = 0; action < numOfActions; ++action) {
1,309,671✔
1545
    STransAction *pAction = taosArrayGet(pArray, action);
1,084,256✔
1546
    if (pAction->msgReceived || pAction->rawWritten) {
1,084,256✔
1547
      numOfExecuted++;
772,003✔
1548
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
772,003✔
1549
        errCode = pAction->errCode;
37✔
1550
        pErrAction = pAction;
37✔
1551
      }
1552
    } else {
1553
      pErrAction = pAction;
312,253✔
1554
    }
1555
  }
1556

1557
  mndSetTransLastAction(pTrans, pErrAction);
225,415✔
1558

1559
  if (numOfExecuted == numOfActions) {
225,415✔
1560
    if (errCode == 0) {
132,070✔
1561
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
132,060!
1562
      return 0;
132,060✔
1563
    } else {
1564
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
10!
1565
      mndTransResetActions(pMnode, pTrans, pArray);
10✔
1566
      terrno = errCode;
10✔
1567
      return errCode;
10✔
1568
    }
1569
  } else {
1570
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
93,345!
1571

1572
    for (int32_t action = 0; action < numOfActions; ++action) {
634,065✔
1573
      STransAction *pAction = taosArrayGet(pArray, action);
540,720✔
1574
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
540,720✔
1575
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1576
             pAction->acceptableCode, pAction->retryCode);
1577
      if (pAction->msgSent) {
540,720✔
1578
        bool reset = false;
540,520✔
1579
        if (pAction->msgReceived) {
540,520✔
1580
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
228,467✔
1581
        } else {
1582
          int64_t timestamp = taosGetTimestampMs();
312,053✔
1583
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
312,053!
1584
        }
1585
        if (reset) {
540,520✔
1586
          mndTransResetAction(pMnode, pTrans, pAction);
27✔
1587
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
27!
1588
                pAction->id, pAction->errCode, pAction->startTime);
1589
        }
1590
      }
1591
    }
1592
    return TSDB_CODE_ACTION_IN_PROGRESS;
93,345✔
1593
  }
1594
}
1595

1596
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
194,039✔
1597
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
194,039✔
1598
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
194,039✔
1599
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
11!
1600
           mndStrExecutionContext(topHalf));
1601
  }
1602
  return code;
194,039✔
1603
}
1604

1605
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
104✔
1606
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf, notSend);
104✔
1607
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
104✔
1608
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
3!
1609
           mndStrExecutionContext(topHalf));
1610
  }
1611
  return code;
104✔
1612
}
1613

1614
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
106,397✔
1615
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
106,397✔
1616
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
106,397!
1617
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1618
           mndStrExecutionContext(topHalf));
1619
  }
1620
  return code;
106,397✔
1621
}
1622

1623
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
29,305✔
1624
  int32_t code = 0;
29,305✔
1625
  int32_t numOfActions = taosArrayGetSize(pActions);
29,305✔
1626
  if (numOfActions == 0) return code;
29,305✔
1627

1628
  if (pTrans->actionPos >= numOfActions) {
29,303✔
1629
    return code;
3,060✔
1630
  }
1631

1632
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
26,243!
1633
        pTrans->actionPos, mndTransStr(pTrans->stage));
1634

1635
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
39,090✔
1636
    STransAction *pAction = taosArrayGet(pActions, action);
36,319✔
1637

1638
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", 
36,319!
1639
          pTrans->id, pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent,
1640
          pAction->msgReceived);
1641

1642
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
36,319✔
1643
    if (code == 0) {
36,319✔
1644
      if (pAction->msgSent) {
28,956✔
1645
        bool reset = false;
25,519✔
1646
        if (pAction->msgReceived) {
25,519✔
1647
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
11,303✔
1648
            code = pAction->errCode;
4,703✔
1649
            reset = true;
4,703✔
1650
          } else {
1651
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
6,600!
1652
          }
1653
        } else {
1654
          int64_t timestamp = taosGetTimestampMs();
14,216✔
1655
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
14,216!
1656
          code = TSDB_CODE_ACTION_IN_PROGRESS;
14,216✔
1657
        }
1658
        if (reset) {
25,519✔
1659
          mndTransResetAction(pMnode, pTrans, pAction);
4,703✔
1660
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
4,703!
1661
                pAction->id, pAction->errCode, pAction->startTime);
1662
        }
1663
      } else if (pAction->rawWritten) {
3,437!
1664
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,437!
1665
          code = pAction->errCode;
×
1666
        } else {
1667
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,437!
1668
        }
1669
      } else {
1670
      }
1671
    }
1672

1673
    if (code == 0) {
36,319✔
1674
      pTrans->failedTimes = 0;
10,037✔
1675
    }
1676
    mndSetTransLastAction(pTrans, pAction);
36,319✔
1677

1678
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
36,319✔
1679
      pTrans->lastErrorNo = code;
9,239✔
1680
      pTrans->code = code;
9,239✔
1681
      mInfo("trans:%d, %s:%d, cannot execute next action in %s, code:%s", pTrans->id, mndTransStr(pAction->stage),
9,239!
1682
            action, mndStrExecutionContext(topHalf), tstrerror(code));
1683
      break;
9,239✔
1684
    }
1685

1686
    if (code == 0) {
27,080✔
1687
      pTrans->code = 0;
8,161✔
1688
      pTrans->actionPos++;
8,161✔
1689
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
8,161!
1690
            pAction->id);
1691
      (void)taosThreadMutexUnlock(&pTrans->mutex);
8,161✔
1692
      code = mndTransSync(pMnode, pTrans);
8,161✔
1693
      (void)taosThreadMutexLock(&pTrans->mutex);
8,161✔
1694
      if (code != 0) {
8,161!
1695
        pTrans->actionPos--;
×
1696
        pTrans->code = terrno;
×
1697
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1698
               mndTransStr(pAction->stage), pAction->id, terrstr());
1699
        break;
×
1700
      }
1701
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
18,919✔
1702
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
14,216!
1703
      break;
14,216✔
1704
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,703✔
1705
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
26✔
1706
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,686!
1707
            code, tstrerror(code));
1708
      pTrans->lastErrorNo = code;
4,686✔
1709
      taosMsleep(300);
4,686✔
1710
      action--;
4,686✔
1711
      continue;
4,686✔
1712
    } else {
1713
      terrno = code;
17✔
1714
      pTrans->lastErrorNo = code;
17✔
1715
      pTrans->code = code;
17✔
1716
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
17!
1717
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1718
      break;
17✔
1719
    }
1720
  }
1721

1722
  return code;
26,243✔
1723
}
1724

1725
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
29,305✔
1726
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
29,305✔
1727
  (void)taosThreadMutexLock(&pTrans->mutex);
29,305✔
1728
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
29,305!
1729
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
29,305✔
1730
  }
1731
  (void)taosThreadMutexUnlock(&pTrans->mutex);
29,305✔
1732
  return code;
29,305✔
1733
}
1734

1735
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
1736
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1737
  (void)taosThreadMutexLock(&pTrans->mutex);
×
1738
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1739
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
1740
  }
1741
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
1742
  return code;
×
1743
}
1744

1745
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
57,848✔
1746
  bool    continueExec = true;
57,848✔
1747
  int32_t code = 0;
57,848✔
1748
  terrno = 0;
57,848✔
1749

1750
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
57,848✔
1751
  if (numOfActions == 0) goto _OVER;
57,848✔
1752

1753
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
17,737!
1754

1755
  for (int32_t action = 0; action < numOfActions; ++action) {
46,000✔
1756
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
28,263✔
1757
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
28,263✔
1758
    if (code != 0) {
28,263!
1759
      terrno = code;
×
1760
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
1761
             numOfActions, tstrerror(code));
1762
      return false;
×
1763
    }
1764
  }
1765

1766
_OVER:
17,737✔
1767
  pTrans->stage = TRN_STAGE_REDO_ACTION;
57,848✔
1768
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
57,848!
1769
  return continueExec;
57,848✔
1770
}
1771

1772
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
223,344✔
1773
  bool    continueExec = true;
223,344✔
1774
  int32_t code = 0;
223,344✔
1775
  terrno = 0;
223,344✔
1776

1777
  if (pTrans->exec == TRN_EXEC_SERIAL) {
223,344✔
1778
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
29,305✔
1779
  } else {
1780
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
194,039✔
1781
  }
1782

1783
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
223,344!
1784
    pTrans->lastErrorNo = code;
×
1785
    pTrans->code = code;
×
1786
    mInfo(
×
1787
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
1788
        "continueExec:%d, code:%s",
1789
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1790
    taosMsleep(100);
×
1791
    return true;
×
1792
  } else {
1793
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
223,344✔
1794
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
67,197!
1795
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1796
      return false;
67,197✔
1797
    }
1798
  }
1799
  terrno = code;
156,147✔
1800

1801
  if (code == 0) {
156,147✔
1802
    pTrans->code = 0;
48,625✔
1803
    pTrans->stage = TRN_STAGE_COMMIT;
48,625✔
1804
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
48,625!
1805
    continueExec = true;
48,625✔
1806
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
107,522!
1807
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
107,494!
1808
    continueExec = false;
107,494✔
1809
  } else {
1810
    pTrans->failedTimes++;
28✔
1811
    pTrans->code = terrno;
28✔
1812
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
28✔
1813
      if (pTrans->lastAction != 0) {
9✔
1814
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
6✔
1815
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
6!
1816
          if (pTrans->failedTimes < 6) {
×
1817
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
1818
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
1819
            taosMsleep(1000);
×
1820
            continueExec = true;
×
1821
            return true;
×
1822
          }
1823
        }
1824
      }
1825

1826
      pTrans->stage = TRN_STAGE_ROLLBACK;
9✔
1827
      pTrans->actionPos = 0;
9✔
1828
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
9!
1829
             pTrans->actionPos);
1830
      continueExec = true;
9✔
1831
    } else {
1832
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
19!
1833
      continueExec = false;
19✔
1834
    }
1835
  }
1836

1837
  return continueExec;
156,147✔
1838
}
1839

1840
// execute in trans context
1841
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
48,627✔
1842
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
48,627✔
1843

1844
  bool    continueExec = true;
48,625✔
1845
  int32_t code = mndTransCommit(pMnode, pTrans);
48,625✔
1846

1847
  if (code == 0) {
48,625✔
1848
    pTrans->code = 0;
48,613✔
1849
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
48,613✔
1850
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
48,613!
1851
    continueExec = true;
48,613✔
1852
  } else {
1853
    pTrans->code = terrno;
12✔
1854
    pTrans->failedTimes++;
12✔
1855
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
12!
1856
    continueExec = false;
12✔
1857
  }
1858

1859
  return continueExec;
48,625✔
1860
}
1861

1862
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
106,397✔
1863
  bool    continueExec = true;
106,397✔
1864
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
106,397✔
1865

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

1883
  return continueExec;
106,397✔
1884
}
1885

1886
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
104✔
1887
  bool    continueExec = true;
104✔
1888
  int32_t code = 0;
104✔
1889

1890
  if (pTrans->exec == TRN_EXEC_SERIAL) {
104!
1891
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
1892
  } else {
1893
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
104✔
1894
  }
1895

1896
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
104✔
1897
  terrno = code;
78✔
1898

1899
  if (code == 0) {
78✔
1900
    pTrans->stage = TRN_STAGE_PRE_FINISH;
8✔
1901
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
8!
1902
    continueExec = true;
8✔
1903
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
70!
1904
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
67!
1905
    continueExec = false;
67✔
1906
  } else {
1907
    pTrans->failedTimes++;
3✔
1908
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
3!
1909
    continueExec = false;
3✔
1910
  }
1911

1912
  return continueExec;
78✔
1913
}
1914

1915
// in trans context
1916
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
9✔
1917
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
9!
1918

1919
  bool    continueExec = true;
9✔
1920
  int32_t code = mndTransRollback(pMnode, pTrans);
9✔
1921

1922
  if (code == 0) {
9✔
1923
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
7✔
1924
    continueExec = true;
7✔
1925
  } else {
1926
    pTrans->failedTimes++;
2✔
1927
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
2!
1928
    continueExec = false;
2✔
1929
  }
1930

1931
  return continueExec;
9✔
1932
}
1933

1934
// excute in trans context
1935
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
8✔
1936
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
8!
1937

1938
  bool    continueExec = true;
8✔
1939
  int32_t code = mndTransPreFinish(pMnode, pTrans);
8✔
1940

1941
  if (code == 0) {
8!
1942
    pTrans->stage = TRN_STAGE_FINISH;
8✔
1943
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
8!
1944
    continueExec = true;
8✔
1945
  } else {
1946
    pTrans->failedTimes++;
×
1947
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1948
    continueExec = false;
×
1949
  }
1950

1951
  return continueExec;
8✔
1952
}
1953

1954
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
106,423✔
1955
  bool continueExec = false;
106,423✔
1956
  if (topHalf) return continueExec;
106,423✔
1957

1958
  SSdbRaw *pRaw = mndTransEncode(pTrans);
57,802✔
1959
  if (pRaw == NULL) {
57,802!
1960
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
1961
    return false;
×
1962
  }
1963
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
57,802!
1964

1965
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
57,802✔
1966
  if (code != 0) {
57,802!
1967
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
1968
  }
1969

1970
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
57,802!
1971
        pTrans->failedTimes, pTrans->createdTime);
1972
  return continueExec;
57,802✔
1973
}
1974

1975
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
281,245✔
1976
  bool continueExec = true;
281,245✔
1977

1978
  while (continueExec) {
766,157✔
1979
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
484,912!
1980
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1981
    pTrans->lastExecTime = taosGetTimestampMs();
484,912✔
1982
    switch (pTrans->stage) {
484,912!
1983
      case TRN_STAGE_PREPARE:
×
1984
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1985
        break;
×
1986
      case TRN_STAGE_REDO_ACTION:
223,344✔
1987
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
223,344✔
1988
        break;
223,344✔
1989
      case TRN_STAGE_COMMIT:
48,627✔
1990
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
48,627✔
1991
        break;
48,627✔
1992
      case TRN_STAGE_COMMIT_ACTION:
106,397✔
1993
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
106,397✔
1994
        break;
106,397✔
1995
      case TRN_STAGE_ROLLBACK:
9✔
1996
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
9✔
1997
        break;
9✔
1998
      case TRN_STAGE_UNDO_ACTION:
104✔
1999
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
104✔
2000
        break;
104✔
2001
      case TRN_STAGE_PRE_FINISH:
8✔
2002
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
8✔
2003
        break;
8✔
2004
      case TRN_STAGE_FINISH:
106,423✔
2005
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
106,423✔
2006
        break;
106,423✔
2007
      default:
×
2008
        continueExec = false;
×
2009
        break;
×
2010
    }
2011
  }
2012

2013
  mndTransSendRpcRsp(pMnode, pTrans);
281,245✔
2014
}
281,245✔
2015

2016
// start trans, pullup, receive rsp, kill
2017
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
156,434✔
2018
  bool topHalf = true;
156,434✔
2019
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
156,434✔
2020
}
156,434✔
2021

2022
// update trans
2023
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
124,811✔
2024
  bool topHalf = false;
124,811✔
2025
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
124,811✔
2026
}
124,811✔
2027

2028
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
43,075✔
2029
  mTrace("start to process trans timer");
43,075✔
2030
  mndTransPullup(pReq->info.node);
43,075✔
2031
  return 0;
43,075✔
2032
}
2033

2034
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
300✔
2035
  SArray *pArray = NULL;
300✔
2036
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
300!
2037
    pArray = pTrans->redoActions;
300✔
2038
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2039
    pArray = pTrans->undoActions;
×
2040
  } else {
2041
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2042
  }
2043

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

2065
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
×
2066
  mndTransExecute(pMnode, pTrans, true);
×
2067
  return 0;
×
2068
}
2069

2070
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1✔
2071
  SMnode       *pMnode = pReq->info.node;
1✔
2072
  SKillTransReq killReq = {0};
1✔
2073
  int32_t       code = -1;
1✔
2074
  STrans       *pTrans = NULL;
1✔
2075

2076
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
2077
    code = TSDB_CODE_INVALID_MSG;
×
2078
    goto _OVER;
×
2079
  }
2080

2081
  mInfo("trans:%d, start to kill", killReq.transId);
1!
2082
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1!
2083
    goto _OVER;
1✔
2084
  }
2085

2086
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
2087
  if (pTrans == NULL) {
×
2088
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
2089
    if (terrno != 0) code = terrno;
×
2090
    goto _OVER;
×
2091
  }
2092

2093
  code = mndKillTrans(pMnode, pTrans);
×
2094

2095
_OVER:
1✔
2096
  if (code != 0) {
1!
2097
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1!
2098
  }
2099

2100
  mndReleaseTrans(pMnode, pTrans);
1✔
2101
  TAOS_RETURN(code);
1✔
2102
}
2103

2104
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
331✔
2105

2106
void mndTransPullup(SMnode *pMnode) {
43,584✔
2107
  SSdb   *pSdb = pMnode->pSdb;
43,584✔
2108
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
43,584✔
2109
  if (pArray == NULL) return;
43,584!
2110

2111
  void *pIter = NULL;
43,584✔
2112
  while (1) {
8,298✔
2113
    STrans *pTrans = NULL;
51,882✔
2114
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
51,882✔
2115
    if (pIter == NULL) break;
51,882✔
2116
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
16,596!
2117
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2118
    }
2119
    sdbRelease(pSdb, pTrans);
8,298✔
2120
  }
2121

2122
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
43,584✔
2123

2124
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
51,882✔
2125
    int32_t *pTransId = taosArrayGet(pArray, i);
8,298✔
2126
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
8,298✔
2127
    if (pTrans != NULL) {
8,298!
2128
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
8,298!
2129
      mndTransExecute(pMnode, pTrans, false);
8,298✔
2130
    }
2131
    mndReleaseTrans(pMnode, pTrans);
8,298✔
2132
  }
2133
  taosArrayDestroy(pArray);
43,584✔
2134
}
2135

2136
static char *formatTimestamp(char *buf, int64_t val, int precision) {
15,154✔
2137
  time_t tt;
2138
  if (precision == TSDB_TIME_PRECISION_MICRO) {
15,154!
2139
    tt = (time_t)(val / 1000000);
×
2140
  }
2141
  if (precision == TSDB_TIME_PRECISION_NANO) {
15,154!
2142
    tt = (time_t)(val / 1000000000);
×
2143
  } else {
2144
    tt = (time_t)(val / 1000);
15,154✔
2145
  }
2146

2147
  struct tm tm;
2148
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
15,154!
2149
    mError("failed to get local time");
×
2150
    return NULL;
×
2151
  }
2152
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
15,162✔
2153

2154
  if (precision == TSDB_TIME_PRECISION_MICRO) {
15,158!
2155
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2156
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
15,158!
2157
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2158
  } else {
2159
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
15,158✔
2160
  }
2161

2162
  return buf;
15,158✔
2163
}
2164

2165
static void mndTransLogAction(STrans *pTrans) {
539✔
2166
  char    detail[512] = {0};
539✔
2167
  int32_t len = 0;
539✔
2168
  int32_t index = 0;
539✔
2169

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

2181
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
539✔
2182
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
9,568✔
2183
      len = 0;
9,028✔
2184
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
9,028✔
2185
      if (pAction->actionType == TRANS_ACTION_MSG) {
9,028✔
2186
        char bufStart[40] = {0};
7,578✔
2187
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
7,578✔
2188

2189
        char endStart[40] = {0};
7,581✔
2190
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
7,581✔
2191
        len += snprintf(detail + len, sizeof(detail) - len,
15,160!
2192
                        "action:%d, %s:%d msgType:%s,"
2193
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2194
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
7,580✔
2195
                        pAction->msgReceived, bufStart, endStart);
7,581✔
2196

2197
        SEpSet epset = pAction->epSet;
7,579✔
2198
        if (epset.numOfEps > 0) {
7,579!
2199
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
7,579✔
2200
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
17,159✔
2201
            len +=
9,580✔
2202
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
9,580✔
2203
          }
2204
        }
2205

2206
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
7,579✔
2207
                        tstrerror(pAction->errCode));
2208
      } else {
2209
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
1,450✔
2210
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,450✔
2211
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,450✔
2212
      }
2213
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
9,029✔
2214
    }
2215
  }
2216

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

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

2244
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
7,466✔
2245
  SMnode *pMnode = pReq->info.node;
7,466✔
2246
  SSdb   *pSdb = pMnode->pSdb;
7,466✔
2247
  int32_t numOfRows = 0;
7,466✔
2248
  STrans *pTrans = NULL;
7,466✔
2249
  int32_t cols = 0;
7,466✔
2250
  int32_t code = 0;
7,466✔
2251
  int32_t lino = 0;
7,466✔
2252

2253
  while (numOfRows < rows) {
8,006!
2254
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
8,006✔
2255
    if (pShow->pIter == NULL) break;
8,010✔
2256

2257
    cols = 0;
542✔
2258

2259
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
542✔
2260
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
542!
2261

2262
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
541✔
2263
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
541!
2264
                        _OVER);
2265

2266
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
541✔
2267
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
541✔
2268
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
541✔
2269
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
541!
2270

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

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

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

2286
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
542!
2287
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
542✔
2288
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
542✔
2289
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
542✔
2290
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
542!
2291

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

2300
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
541✔
2301
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
541!
2302
                        _OVER);
2303

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

2308
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
540✔
2309
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
540✔
2310
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
1,624✔
2311
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
540✔
2312
    SEpSet  epset = pTrans->lastEpset;
542✔
2313
    if (epset.numOfEps > 0) {
542✔
2314
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
958!
2315
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
956✔
2316
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
1,077✔
2317
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
598✔
2318
      }
2319
    }
2320
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
544✔
2321
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
544✔
2322
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
542!
2323

2324
    mndTransLogAction(pTrans);
539✔
2325

2326
    numOfRows++;
541✔
2327
    sdbRelease(pSdb, pTrans);
541✔
2328
  }
2329

2330
_OVER:
×
2331
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
7,468!
2332
  pShow->numOfRows += numOfRows;
7,469✔
2333
  return numOfRows;
7,469✔
2334
}
2335

2336
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
×
2337
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2338
  int32_t code = 0;
×
2339
  int32_t lino = 0;
×
2340
  int32_t len = 0;
×
2341

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

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

2362
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
×
2363
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2364
  int32_t code = 0;
×
2365
  int32_t lino = 0;
×
2366
  int32_t len = 0;
×
2367
  int32_t cols = 0;
×
2368

2369
  cols = 0;
×
2370

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

2373
  if (pAction->actionType == TRANS_ACTION_MSG) {
×
2374
    int32_t len = 0;
×
2375

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

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

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

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

2419
  } else {
2420
    int32_t len = 0;
×
2421

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

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

2444
    char target[TSDB_TRANS_TARGET_LEN] = "";
×
2445
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2446
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2447
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2448
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2449

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

2459
_OVER:
×
2460
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2461
}
2462

2463
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
×
2464
  if (stage == TRN_STAGE_PREPARE) {
×
2465
    return pTrans->prepareActions;
×
2466
  }
2467
  if (stage == TRN_STAGE_REDO_ACTION) {
×
2468
    return pTrans->redoActions;
×
2469
  }
2470
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
2471
    return pTrans->commitActions;
×
2472
  }
2473
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
2474
    return pTrans->undoActions;
×
2475
  }
2476
  return NULL;
×
2477
}
2478

2479
typedef struct STransDetailIter {
2480
  void     *pIter;
2481
  STrans   *pTrans;
2482
  ETrnStage stage;
2483
  int32_t   num;
2484
} STransDetailIter;
2485

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

2491
  for (int32_t i = start; i < actionNum; ++i) {
×
2492
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
×
2493
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
×
2494
    (*numOfRows)++;
×
2495
    if (*numOfRows >= rows) break;
×
2496
  }
2497

2498
  if (*numOfRows == end) {
×
2499
    sdbRelease(pSdb, pShowIter->pTrans);
×
2500
    pShowIter->pTrans = NULL;
×
2501
    pShowIter->num = 0;
×
2502
  } else {
2503
    pShowIter->pTrans = pShowIter->pTrans;
×
2504
    pShowIter->stage = pShowIter->pTrans->stage;
×
2505
    pShowIter->num += (*numOfRows);
×
2506
  }
2507
}
×
2508

2509
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2510
  SMnode *pMnode = pReq->info.node;
×
2511
  SSdb   *pSdb = pMnode->pSdb;
×
2512
  int32_t numOfRows = 0;
×
2513

2514
  int32_t code = 0;
×
2515
  int32_t lino = 0;
×
2516

2517
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
×
2518
        pShow->pIter);
2519

2520
  if (pShow->pIter == NULL) {
×
2521
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
×
2522
    if (pShow->pIter == NULL) {
×
2523
      mError("failed to malloc for pShow->pIter");
×
2524
      return 0;
×
2525
    }
2526
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
×
2527
  }
2528

2529
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
×
2530

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

2539
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
×
2540

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

2548
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
×
2549
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
×
2550
      break;
×
2551
    }
2552
  }
2553

2554
_OVER:
×
2555
  pShow->numOfRows += numOfRows;
×
2556

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

2569
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
2570
  SSdb *pSdb = pMnode->pSdb;
×
2571
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
2572
}
×
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