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

taosdata / TDengine / #3910

23 Apr 2025 02:47AM UTC coverage: 62.362% (-0.7%) from 63.063%
#3910

push

travis-ci

web-flow
docs(datain): add missing health status types (#30828)

155061 of 317305 branches covered (48.87%)

Branch coverage included in aggregate %.

240172 of 316469 relevant lines covered (75.89%)

6269478.46 hits per line

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

63.27
/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; }
365,220✔
59

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

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

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

99
void mndCleanupTrans(SMnode *pMnode) {}
1,977✔
100

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

105
  for (int32_t i = 0; i < actionNum; ++i) {
1,605,883✔
106
    STransAction *pAction = taosArrayGet(pArray, i);
1,090,987✔
107
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,090,987✔
108
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
734,386✔
109
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
356,601!
110
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
356,601✔
111
    } else {
112
      // empty
113
    }
114
    rawDataLen += sizeof(int8_t);
1,090,987✔
115
  }
116

117
  return rawDataLen;
514,896✔
118
}
119

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

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

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

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

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

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

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

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

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

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

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

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

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

232
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
128,724!
233
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
128,724!
234

235
  terrno = 0;
128,724✔
236

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

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

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

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

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

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

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

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

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

333
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
247,978!
334

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

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

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

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

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

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

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

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

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

408
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
247,978!
409

410
  terrno = 0;
247,978✔
411

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

426
static const char *mndTransStr(ETrnStage stage) {
1,931,118✔
427
  switch (stage) {
1,931,118!
428
    case TRN_STAGE_PREPARE:
125,993✔
429
      return "prepare";
125,993✔
430
    case TRN_STAGE_REDO_ACTION:
751,169✔
431
      return "redoAction";
751,169✔
432
    case TRN_STAGE_ROLLBACK:
36✔
433
      return "rollback";
36✔
434
    case TRN_STAGE_UNDO_ACTION:
40,495✔
435
      return "undoAction";
40,495✔
436
    case TRN_STAGE_COMMIT:
209,845✔
437
      return "commit";
209,845✔
438
    case TRN_STAGE_COMMIT_ACTION:
496,332✔
439
      return "commitAction";
496,332✔
440
    case TRN_STAGE_FINISH:
307,220✔
441
      return "finished";
307,220✔
442
    case TRN_STAGE_PRE_FINISH:
28✔
443
      return "pre-finish";
28✔
444
    default:
×
445
      return "invalid";
×
446
  }
447
}
448

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

460
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
554,734✔
461
  if (pAction != NULL) {
554,734✔
462
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
450,593✔
463
      pTrans->lastAction = pAction->id;
318,144✔
464
      pTrans->lastMsgType = pAction->msgType;
318,144✔
465
      pTrans->lastEpset = pAction->epSet;
318,144✔
466
      pTrans->lastErrorNo = pAction->errCode;
318,144✔
467
    }
468
  } else {
469
    pTrans->lastAction = 0;
104,141✔
470
    pTrans->lastMsgType = 0;
104,141✔
471
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
104,141✔
472
    pTrans->lastErrorNo = 0;
104,141✔
473
  }
474
}
554,734✔
475

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

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

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

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

503
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
47,671✔
504

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

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

518
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
47,671✔
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) {
47,671!
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;
47,671✔
529
}
530

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

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

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

580
  mndTransDropData(pTrans);
147,801✔
581
  return 0;
147,801✔
582
}
583

584
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
210,880✔
585
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
683,475✔
586
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
472,595✔
587
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
472,595✔
588
    pOldAction->rawWritten = pNewAction->rawWritten;
472,595✔
589
    pOldAction->msgSent = pNewAction->msgSent;
472,595✔
590
    pOldAction->msgReceived = pNewAction->msgReceived;
472,595✔
591
    pOldAction->errCode = pNewAction->errCode;
472,595✔
592
  }
593
}
210,880✔
594

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

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

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

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

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

630
  return 0;
52,720✔
631
}
632

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

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

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

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

680
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
38,771!
681
      pTrans->pRpcArray == NULL) {
38,771!
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) {
38,771✔
689
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
44,642!
690
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
691
      return NULL;
×
692
    }
693
    pTrans->originRpcType = pReq->msgType;
22,321✔
694
  }
695

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

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

702
static void mndTransDropActions(SArray *pArray) {
1,146,996✔
703
  int32_t size = taosArrayGetSize(pArray);
1,146,996✔
704
  for (int32_t i = 0; i < size; ++i) {
3,507,912✔
705
    STransAction *pAction = taosArrayGet(pArray, i);
2,360,916✔
706
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,360,916✔
707
      taosMemoryFreeClear(pAction->pRaw);
1,557,032!
708
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
803,884!
709
      taosMemoryFreeClear(pAction->pCont);
803,884!
710
    } else {
711
      // nothing
712
    }
713
  }
714

715
  taosArrayDestroy(pArray);
1,146,996✔
716
}
1,146,996✔
717

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

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

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

734
  return 0;
314,015✔
735
}
736

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

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

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

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

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

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

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

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

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

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

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

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

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

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

835
void mndTransSetSerial(STrans *pTrans) { pTrans->exec = TRN_EXEC_SERIAL; }
1,000✔
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; }
4✔
847

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

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

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

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

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

882
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
17,732✔
883
  if (conflict[0] == 0) return false;
17,732✔
884
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
16,569✔
885
  return false;
16,542✔
886
}
887

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

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

904
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
107,494✔
905

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

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

913
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
18,488!
914

915
    if (pNew->conflict == TRN_CONFLICT_DB) {
18,488✔
916
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
1,617✔
917
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
1,617!
918
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,163✔
919
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,163✔
920
      }
921
    }
922

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

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

963
  return conflict;
71,653✔
964
}
965

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

982
  TAOS_RETURN(code);
107,110✔
983
}
984

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

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

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

1040
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
38,344✔
1041
  int32_t code = 0;
38,344✔
1042
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
38,344✔
1043
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
37,418!
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) {
37,418✔
1050
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
14,211!
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);
38,344✔
1059
}
1060

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

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

1083
  mInfo("trans:%d, action list:", pTrans->id);
38,344!
1084
  int32_t index = 0;
38,344✔
1085
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
55,889✔
1086
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
17,545✔
1087
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
17,545!
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) {
99,031✔
1092
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
60,687✔
1093
    mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
60,687!
1094
          mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1095
  }
1096

1097
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
233,412✔
1098
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
195,068✔
1099
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
195,068!
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) {
78,626✔
1104
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
40,282✔
1105
    if(pAction->actionType == TRANS_ACTION_MSG){
40,282✔
1106
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
27,504!
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,
12,778!
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));
38,344!
1117

1118
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
38,344!
1119

1120
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
38,344!
1121

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

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

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

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

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

1177
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
4✔
1178
  int32_t code = 0;
4✔
1179
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
4!
1180
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
4!
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);
4!
1185
  TAOS_RETURN(code);
4✔
1186
}
1187

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

1192
  if (pTrans->stage == TRN_STAGE_FINISH) {
207,046✔
1193
    sendRsp = true;
85,748✔
1194
  }
1195

1196
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
207,046✔
1197
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
69,652✔
1198
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
36✔
1199
      sendRsp = true;
36✔
1200
    }
1201
  } else {
1202
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
137,394✔
1203
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
85,050!
1204
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1205
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1206
      } else {
1207
        if (pTrans->failedTimes > 6) sendRsp = true;
85,050✔
1208
      }
1209
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
85,050✔
1210
    }
1211
  }
1212

1213
  if (!sendRsp) {
207,046✔
1214
    return;
121,260✔
1215
  } else {
1216
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
85,786!
1217
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1218
  }
1219

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

1228
  for (int32_t i = 0; i < size; ++i) {
43,718✔
1229
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
21,859✔
1230
    if (pInfo->handle != NULL) {
21,859✔
1231
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
20,457!
1232
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1233
      }
1234
      if (code == TSDB_CODE_SYN_TIMEOUT) {
20,457!
1235
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1236
      }
1237

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

1244
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
20,457✔
1245

1246
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
20,457✔
1247
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
3,757!
1248
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
3,757✔
1249
        if (pDb != NULL) {
3,757!
1250
          for (int32_t j = 0; j < 12; j++) {
4,541✔
1251
            bool ready = mndIsDbReady(pMnode, pDb);
4,539✔
1252
            if (!ready) {
4,539✔
1253
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
784!
1254
              taosMsleep(1000);
784✔
1255
            } else {
1256
              break;
3,755✔
1257
            }
1258
          }
1259
        }
1260
        mndReleaseDb(pMnode, pDb);
3,757✔
1261
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
16,700✔
1262
        void   *pCont = NULL;
6,005✔
1263
        int32_t contLen = 0;
6,005✔
1264
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
6,005✔
1265
          mndTransSetRpcRsp(pTrans, pCont, contLen);
6,001✔
1266
        }
1267
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
10,695✔
1268
        void   *pCont = NULL;
9✔
1269
        int32_t contLen = 0;
9✔
1270
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
9!
1271
          mndTransSetRpcRsp(pTrans, pCont, contLen);
9✔
1272
        }
1273
      }
1274

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

1284
      tmsgSendRsp(&rspMsg);
20,457✔
1285

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

1294
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
64,104✔
1295
  int32_t code = 0;
64,104✔
1296
  SMnode *pMnode = pRsp->info.node;
64,104✔
1297
#ifndef TD_ASTRA_32
1298
  int64_t signature = (int64_t)(pRsp->info.ahandle);
64,104✔
1299
  int32_t transId = (int32_t)(signature >> 32);
64,104✔
1300
  int32_t action = (int32_t)((signature << 32) >> 32);
64,104✔
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);
64,104✔
1306
  if (pTrans == NULL) {
64,104!
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;
64,104✔
1314
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
64,104✔
1315
    pArray = pTrans->redoActions;
64,085✔
1316
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
19!
1317
    pArray = pTrans->undoActions;
19✔
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) {
64,104!
1324
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1325
    goto _OVER;
×
1326
  }
1327

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

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

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

1343
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
64,104!
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);
64,104!
1351
  mndTransExecute(pMnode, pTrans, true);
64,104✔
1352

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

1358
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
4,560✔
1359
  pAction->rawWritten = 0;
4,560✔
1360
  pAction->msgSent = 0;
4,560✔
1361
  pAction->msgReceived = 0;
4,560✔
1362
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
4,560!
1363
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
4,560✔
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,550!
1369
  }
1370
  pAction->errCode = 0;
4,560✔
1371
}
4,560✔
1372

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

1376
  for (int32_t action = 0; action < numOfActions; ++action) {
10✔
1377
    STransAction *pAction = taosArrayGet(pArray, action);
8✔
1378
    if (pAction->msgSent && pAction->msgReceived &&
8!
1379
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
8!
1380
      continue;
6✔
1381
    if (pAction->msgSent && !pAction->msgReceived) {
2!
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;
2!
1387

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

1394
// execute in sync context
1395
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
436,068✔
1396
  if (pAction->rawWritten) return 0;
436,068✔
1397
  if (topHalf) {
239,760✔
1398
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
6✔
1399
  }
1400

1401
  if (pAction->pRaw->type >= SDB_MAX) {
239,754!
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);
239,754✔
1410
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
239,754!
1411
    pAction->rawWritten = true;
239,754✔
1412
    pAction->errCode = 0;
239,754✔
1413
    code = 0;
239,754✔
1414
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
239,754!
1415
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1416

1417
    mndSetTransLastAction(pTrans, pAction);
239,754✔
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);
239,754✔
1426
}
1427

1428
// execute in trans context
1429
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
396,648✔
1430
                                     bool notSend) {
1431
  if (pAction->msgSent) return 0;
396,648✔
1432
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
92,750✔
1433
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
28,379✔
1434
  }
1435

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

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

1446
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
64,189✔
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);
64,189✔
1454
  if (rpcMsg.pCont == NULL) {
64,189!
1455
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1456
    return -1;
×
1457
  }
1458
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
64,189✔
1459
  rpcMsg.info.notFreeAhandle = 1;
64,189✔
1460

1461
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
64,189✔
1462

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

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

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

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

1491
  TAOS_RETURN(code);
64,189✔
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,
832,716✔
1505
                                        bool notSend) {
1506
  if (pAction->actionType == TRANS_ACTION_RAW) {
832,716✔
1507
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
436,068✔
1508
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
396,648!
1509
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
396,648✔
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) {
186,710✔
1516
  int32_t numOfActions = taosArrayGetSize(pArray);
186,710✔
1517
  int32_t code = 0;
186,710✔
1518

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

1529
  return code;
186,710✔
1530
}
1531

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

1537
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
186,710✔
1538
    return code;
24,551✔
1539
  }
1540

1541
  int32_t       numOfExecuted = 0;
162,159✔
1542
  int32_t       errCode = 0;
162,159✔
1543
  STransAction *pErrAction = NULL;
162,159✔
1544
  for (int32_t action = 0; action < numOfActions; ++action) {
923,148✔
1545
    STransAction *pAction = taosArrayGet(pArray, action);
760,989✔
1546
    if (pAction->msgReceived || pAction->rawWritten) {
760,989✔
1547
      numOfExecuted++;
586,349✔
1548
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
586,349✔
1549
        errCode = pAction->errCode;
32✔
1550
        pErrAction = pAction;
32✔
1551
      }
1552
    } else {
1553
      pErrAction = pAction;
174,640✔
1554
    }
1555
  }
1556

1557
  mndSetTransLastAction(pTrans, pErrAction);
162,159✔
1558

1559
  if (numOfExecuted == numOfActions) {
162,159✔
1560
    if (errCode == 0) {
104,143✔
1561
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
104,141!
1562
      return 0;
104,141✔
1563
    } else {
1564
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
2!
1565
      mndTransResetActions(pMnode, pTrans, pArray);
2✔
1566
      terrno = errCode;
2✔
1567
      return errCode;
2✔
1568
    }
1569
  } else {
1570
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
58,016!
1571

1572
    for (int32_t action = 0; action < numOfActions; ++action) {
352,009✔
1573
      STransAction *pAction = taosArrayGet(pArray, action);
293,993✔
1574
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
293,993✔
1575
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1576
             pAction->acceptableCode, pAction->retryCode);
1577
      if (pAction->msgSent) {
293,993✔
1578
        bool reset = false;
293,811✔
1579
        if (pAction->msgReceived) {
293,811✔
1580
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
119,353✔
1581
        } else {
1582
          int64_t timestamp = taosGetTimestampMs();
174,458✔
1583
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
174,458!
1584
        }
1585
        if (reset) {
293,811✔
1586
          mndTransResetAction(pMnode, pTrans, pAction);
30✔
1587
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
30!
1588
                pAction->id, pAction->errCode, pAction->startTime);
1589
        }
1590
      }
1591
    }
1592
    return TSDB_CODE_ACTION_IN_PROGRESS;
58,016✔
1593
  }
1594
}
1595

1596
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
141,780✔
1597
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
141,780✔
1598
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
141,780✔
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;
141,780✔
1603
}
1604

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

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

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

1628
  if (pTrans->actionPos >= numOfActions) {
17,816✔
1629
    return code;
1,211✔
1630
  }
1631

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

1635
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
25,452✔
1636
    STransAction *pAction = taosArrayGet(pActions, action);
24,528✔
1637

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

1642
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
24,528✔
1643
    if (code == 0) {
24,528✔
1644
      if (pAction->msgSent) {
20,685✔
1645
        bool reset = false;
17,879✔
1646
        if (pAction->msgReceived) {
17,879✔
1647
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
7,609!
1648
            code = pAction->errCode;
4,528✔
1649
            reset = true;
4,528✔
1650
          } else {
1651
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,081!
1652
          }
1653
        } else {
1654
          int64_t timestamp = taosGetTimestampMs();
10,270✔
1655
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
10,270!
1656
          code = TSDB_CODE_ACTION_IN_PROGRESS;
10,270✔
1657
        }
1658
        if (reset) {
17,879✔
1659
          mndTransResetAction(pMnode, pTrans, pAction);
4,528✔
1660
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
4,528!
1661
                pAction->id, pAction->errCode, pAction->startTime);
1662
        }
1663
      } else if (pAction->rawWritten) {
2,806!
1664
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
2,806!
1665
          code = pAction->errCode;
×
1666
        } else {
1667
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
2,806!
1668
        }
1669
      } else {
1670
      }
1671
    }
1672

1673
    if (code == 0) {
24,528✔
1674
      pTrans->failedTimes = 0;
5,887✔
1675
    }
1676
    mndSetTransLastAction(pTrans, pAction);
24,528✔
1677

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

1686
    if (code == 0) {
19,124✔
1687
      pTrans->code = 0;
4,326✔
1688
      pTrans->actionPos++;
4,326✔
1689
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
4,326!
1690
            pAction->id);
1691
      (void)taosThreadMutexUnlock(&pTrans->mutex);
4,326✔
1692
      code = mndTransSync(pMnode, pTrans);
4,326✔
1693
      (void)taosThreadMutexLock(&pTrans->mutex);
4,326✔
1694
      if (code != 0) {
4,326!
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) {
14,798✔
1702
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
10,270!
1703
      break;
10,270✔
1704
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,528✔
1705
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
17✔
1706
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,521!
1707
            code, tstrerror(code));
1708
      pTrans->lastErrorNo = code;
4,521✔
1709
      taosMsleep(300);
4,521✔
1710
      action--;
4,521✔
1711
      continue;
4,521✔
1712
    } else {
1713
      terrno = code;
7✔
1714
      pTrans->lastErrorNo = code;
7✔
1715
      pTrans->code = code;
7✔
1716
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
7!
1717
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1718
      break;
7✔
1719
    }
1720
  }
1721

1722
  return code;
16,605✔
1723
}
1724

1725
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
17,818✔
1726
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
17,818✔
1727
  (void)taosThreadMutexLock(&pTrans->mutex);
17,818✔
1728
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
17,818!
1729
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
17,818✔
1730
  }
1731
  (void)taosThreadMutexUnlock(&pTrans->mutex);
17,818✔
1732
  return code;
17,818✔
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) {
47,457✔
1746
  bool    continueExec = true;
47,457✔
1747
  int32_t code = 0;
47,457✔
1748
  terrno = 0;
47,457✔
1749

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

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

1755
  for (int32_t action = 0; action < numOfActions; ++action) {
36,137✔
1756
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
22,647✔
1757
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
22,647✔
1758
    if (code != 0) {
22,647!
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:
13,490✔
1767
  pTrans->stage = TRN_STAGE_REDO_ACTION;
47,457✔
1768
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
47,457!
1769
  return continueExec;
47,457✔
1770
}
1771

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

1777
  if (pTrans->exec == TRN_EXEC_SERIAL) {
159,598✔
1778
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
17,818✔
1779
  } else {
1780
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
141,780✔
1781
  }
1782

1783
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
159,598!
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)) {
159,598✔
1794
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
52,971!
1795
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1796
      return false;
52,971✔
1797
    }
1798
  }
1799
  terrno = code;
106,627✔
1800

1801
  if (code == 0) {
106,627✔
1802
    pTrans->code = 0;
38,345✔
1803
    pTrans->stage = TRN_STAGE_COMMIT;
38,345✔
1804
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
38,345!
1805
    continueExec = true;
38,345✔
1806
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
68,282!
1807
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
68,264!
1808
    continueExec = false;
68,264✔
1809
  } else {
1810
    pTrans->failedTimes++;
18✔
1811
    pTrans->code = terrno;
18✔
1812
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
18✔
1813
      if (pTrans->lastAction != 0) {
4✔
1814
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
3✔
1815
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
3!
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;
4✔
1827
      pTrans->actionPos = 0;
4✔
1828
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
4!
1829
             pTrans->actionPos);
1830
      continueExec = true;
4✔
1831
    } else {
1832
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
14!
1833
      continueExec = false;
14✔
1834
    }
1835
  }
1836

1837
  return continueExec;
106,627✔
1838
}
1839

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

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

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

1859
  return continueExec;
38,345✔
1860
}
1861

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

1866
  if (code == 0) {
85,738✔
1867
    pTrans->code = 0;
85,736✔
1868
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
85,736✔
1869
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
85,736!
1870
    continueExec = true;
85,736✔
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;
85,738✔
1884
}
1885

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

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

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

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

1912
  return continueExec;
26✔
1913
}
1914

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

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

1922
  if (code == 0) {
4✔
1923
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
2✔
1924
    continueExec = true;
2✔
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;
4✔
1932
}
1933

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

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

1941
  if (code == 0) {
4!
1942
    pTrans->stage = TRN_STAGE_FINISH;
4✔
1943
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
4!
1944
    continueExec = true;
4✔
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;
4✔
1952
}
1953

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

1958
  SSdbRaw *pRaw = mndTransEncode(pTrans);
47,410✔
1959
  if (pRaw == NULL) {
47,410!
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));
47,410!
1964

1965
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
47,410✔
1966
  if (code != 0) {
47,410!
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,
47,410!
1971
        pTrans->failedTimes, pTrans->createdTime);
1972
  return continueExec;
47,410✔
1973
}
1974

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

1978
  while (continueExec) {
576,523✔
1979
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
369,477!
1980
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1981
    pTrans->lastExecTime = taosGetTimestampMs();
369,477✔
1982
    switch (pTrans->stage) {
369,477!
1983
      case TRN_STAGE_PREPARE:
×
1984
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1985
        break;
×
1986
      case TRN_STAGE_REDO_ACTION:
159,598✔
1987
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
159,598✔
1988
        break;
159,598✔
1989
      case TRN_STAGE_COMMIT:
38,347✔
1990
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
38,347✔
1991
        break;
38,347✔
1992
      case TRN_STAGE_COMMIT_ACTION:
85,738✔
1993
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
85,738✔
1994
        break;
85,738✔
1995
      case TRN_STAGE_ROLLBACK:
4✔
1996
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
4✔
1997
        break;
4✔
1998
      case TRN_STAGE_UNDO_ACTION:
38✔
1999
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
38✔
2000
        break;
38✔
2001
      case TRN_STAGE_PRE_FINISH:
4✔
2002
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
4✔
2003
        break;
4✔
2004
      case TRN_STAGE_FINISH:
85,748✔
2005
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
85,748✔
2006
        break;
85,748✔
2007
      default:
×
2008
        continueExec = false;
×
2009
        break;
×
2010
    }
2011
  }
2012

2013
  mndTransSendRpcRsp(pMnode, pTrans);
207,046✔
2014
}
207,046✔
2015

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

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

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

2034
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
2035
  SArray *pArray = NULL;
×
2036
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
2037
    pArray = pTrans->redoActions;
×
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){
×
2045
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
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; }
277✔
2105

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

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

2122
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
23,855✔
2123

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

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

2147
  struct tm tm;
2148
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
13,314!
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);
13,314✔
2153

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

2162
  return buf;
13,314✔
2163
}
2164

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

2170
  if (pTrans->stage == TRN_STAGE_PREPARE) {
178!
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) {
178!
2182
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
8,410✔
2183
      len = 0;
8,232✔
2184
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
8,232✔
2185
      if (pAction->actionType == TRANS_ACTION_MSG) {
8,232✔
2186
        char bufStart[40] = {0};
6,657✔
2187
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
6,657✔
2188

2189
        char endStart[40] = {0};
6,657✔
2190
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
6,657✔
2191
        len += snprintf(detail + len, sizeof(detail) - len,
13,314!
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,
6,657✔
2195
                        pAction->msgReceived, bufStart, endStart);
6,657✔
2196

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

2206
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
6,657✔
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,575✔
2210
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,575✔
2211
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,575✔
2212
      }
2213
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
8,232!
2214
    }
2215
  }
2216

2217
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
178!
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
}
178✔
2243

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

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

2257
    cols = 0;
178✔
2258

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

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

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

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

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

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

2286
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
178!
2287
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
178✔
2288
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
178✔
2289
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
178✔
2290
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
178!
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++);
178✔
2301
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
178!
2302
                        _OVER);
2303

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

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

2324
    mndTransLogAction(pTrans);
178✔
2325

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

2330
_OVER:
×
2331
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
204!
2332
  pShow->numOfRows += numOfRows;
204✔
2333
  return numOfRows;
204✔
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