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

taosdata / TDengine / #4834

31 Oct 2025 03:37AM UTC coverage: 58.764% (+0.2%) from 58.533%
#4834

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149869 of 324176 branches covered (46.23%)

Branch coverage included in aggregate %.

199000 of 269498 relevant lines covered (73.84%)

239175663.62 hits per line

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

67.45
/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_VER3_NUMBER  3
31
#define TRANS_ARRAY_SIZE   8
32
#define TRANS_RESERVE_SIZE 39
33
#define TRANS_ACTION_TIMEOUT 1000 * 1000 * 60 * 15
34

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

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

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

59
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
339,722,831✔
60

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

68
static bool mndCannotExecuteTransWithInfo(SMnode *pMnode, bool topHalf, char *str, int32_t size) {
39,758,776✔
69
  bool isLeader = mndIsLeader(pMnode);
39,758,776✔
70
  bool ret = (!pMnode->deploy && !isLeader) || mndTransIsInSyncContext(topHalf);
39,758,776✔
71
  if (ret)
39,758,776✔
72
    tsnprintf(str, size, "deploy:%d, isLeader:%d, context:%s", pMnode->deploy, isLeader,
11,425,742!
73
              topHalf ? "transContext" : "syncContext");
74
  return ret;
39,758,776✔
75
}
76

77
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
476,755,006✔
78

79
static void    mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans);
80
static int32_t mndProcessTransTimer(SRpcMsg *pReq);
81
static int32_t mndProcessTtl(SRpcMsg *pReq);
82
static int32_t mndProcessKillTransReq(SRpcMsg *pReq);
83

84
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
85
static void    mndCancelGetNextTrans(SMnode *pMnode, void *pIter);
86
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
87
static int32_t tsMaxTransId = 0;
88

89
int32_t mndInitTrans(SMnode *pMnode) {
1,753,556✔
90
  SSdbTable table = {
1,753,556✔
91
      .sdbType = SDB_TRANS,
92
      .keyType = SDB_KEY_INT32,
93
      .encodeFp = (SdbEncodeFp)mndTransEncode,
94
      .decodeFp = (SdbDecodeFp)mndTransDecode,
95
      .insertFp = (SdbInsertFp)mndTransActionInsert,
96
      .updateFp = (SdbUpdateFp)mndTransActionUpdate,
97
      .deleteFp = (SdbDeleteFp)mndTransDelete,
98
  };
99

100
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
1,753,556✔
101
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
1,753,556✔
102

103
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndRetrieveTrans);
1,753,556✔
104
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndCancelGetNextTrans);
1,753,556✔
105
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANSACTION_DETAIL, mndRetrieveTransDetail);
1,753,556✔
106
  return sdbSetTable(pMnode->pSdb, table);
1,753,556✔
107
}
108

109
void mndCleanupTrans(SMnode *pMnode) {}
1,753,266✔
110

111
static int32_t mndTransGetActionsSize(SArray *pArray) {
606,410,764✔
112
  int32_t actionNum = taosArrayGetSize(pArray);
606,410,764✔
113
  int32_t rawDataLen = 0;
606,410,764✔
114

115
  for (int32_t i = 0; i < actionNum; ++i) {
1,772,450,407✔
116
    STransAction *pAction = taosArrayGet(pArray, i);
1,166,039,643✔
117
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,166,039,643✔
118
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
781,188,161✔
119
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
384,851,482!
120
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
384,851,482✔
121
    } else {
122
      // empty
123
    }
124
    rawDataLen += sizeof(int8_t);
1,166,039,643✔
125
  }
126

127
  return rawDataLen;
606,410,764✔
128
}
129

130
static int32_t mndTransEncodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionsNum,
606,410,764✔
131
                                    int32_t sver) {
132
  int32_t code = 0;
606,410,764✔
133
  int32_t lino = 0;
606,410,764✔
134
  int32_t dataPos = *offset;
606,410,764✔
135
  int8_t  unused = 0;
606,410,764✔
136
  int32_t ret = -1;
606,410,764✔
137

138
  for (int32_t i = 0; i < actionsNum; ++i) {
1,772,450,407✔
139
    STransAction *pAction = taosArrayGet(pActions, i);
1,166,039,643✔
140
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
1,166,039,643!
141
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
1,166,039,643!
142
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
1,166,039,643!
143
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
1,166,039,643!
144
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
1,166,039,643!
145
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
1,166,039,643!
146
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
1,166,039,643!
147
    if (sver > TRANS_VER2_NUMBER) {
1,166,039,643!
148
      SDB_SET_INT32(pRaw, dataPos, pAction->groupId, _OVER)
1,166,039,643!
149
    }
150
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,166,039,643✔
151
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
781,188,161✔
152
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
781,188,161!
153
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
781,188,161!
154
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
781,188,161!
155
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
384,851,482!
156
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
384,851,482!
157
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
384,851,482!
158
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
384,851,482!
159
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
384,851,482!
160
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
384,851,482!
161
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
384,851,482!
162
    } else {
163
      // nothing
164
    }
165
  }
166
  ret = 0;
606,410,764✔
167

168
_OVER:
606,410,764✔
169
  *offset = dataPos;
606,410,764✔
170
  return ret;
606,410,764✔
171
}
172

173
SSdbRaw *mndTransEncode(STrans *pTrans) {
151,602,691✔
174
  int32_t code = 0;
151,602,691✔
175
  int32_t lino = 0;
151,602,691✔
176
  terrno = TSDB_CODE_INVALID_MSG;
151,602,691✔
177
  int8_t sver = TRANS_VER3_NUMBER;
151,602,691✔
178

179
  int32_t rawDataLen = sizeof(STrans) + TRANS_RESERVE_SIZE + pTrans->paramLen;
151,602,691✔
180
  rawDataLen += mndTransGetActionsSize(pTrans->prepareActions);
151,602,691✔
181
  rawDataLen += mndTransGetActionsSize(pTrans->redoActions);
151,602,691✔
182
  rawDataLen += mndTransGetActionsSize(pTrans->undoActions);
151,602,691✔
183
  rawDataLen += mndTransGetActionsSize(pTrans->commitActions);
151,602,691✔
184

185
  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, sver, rawDataLen);
151,602,691✔
186
  if (pRaw == NULL) {
151,602,691!
187
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
×
188
    return NULL;
×
189
  }
190

191
  int32_t dataPos = 0;
151,602,691✔
192
  SDB_SET_INT32(pRaw, dataPos, pTrans->id, _OVER)
151,602,691!
193
  SDB_SET_INT8(pRaw, dataPos, pTrans->stage, _OVER)
151,602,691!
194
  SDB_SET_INT8(pRaw, dataPos, pTrans->policy, _OVER)
151,602,691!
195
  SDB_SET_INT8(pRaw, dataPos, pTrans->conflict, _OVER)
151,602,691!
196
  SDB_SET_INT8(pRaw, dataPos, pTrans->exec, _OVER)
151,602,691!
197
  SDB_SET_INT8(pRaw, dataPos, pTrans->oper, _OVER)
151,602,691!
198
  SDB_SET_INT8(pRaw, dataPos, 0, _OVER)
151,602,691!
199
  SDB_SET_INT16(pRaw, dataPos, pTrans->originRpcType, _OVER)
151,602,691!
200
  SDB_SET_INT64(pRaw, dataPos, pTrans->createdTime, _OVER)
151,602,691!
201
  SDB_SET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
151,602,691!
202
  SDB_SET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
151,602,691!
203
  SDB_SET_INT32(pRaw, dataPos, pTrans->actionPos, _OVER)
151,602,691!
204

205
  int32_t prepareActionNum = taosArrayGetSize(pTrans->prepareActions);
151,602,691✔
206
  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
151,602,691✔
207
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
151,602,691✔
208
  int32_t commitActionNum = taosArrayGetSize(pTrans->commitActions);
151,602,691✔
209

210
  if (sver > TRANS_VER1_NUMBER) {
151,602,691!
211
    SDB_SET_INT32(pRaw, dataPos, prepareActionNum, _OVER)
151,602,691!
212
  }
213
  SDB_SET_INT32(pRaw, dataPos, redoActionNum, _OVER)
151,602,691!
214
  SDB_SET_INT32(pRaw, dataPos, undoActionNum, _OVER)
151,602,691!
215
  SDB_SET_INT32(pRaw, dataPos, commitActionNum, _OVER)
151,602,691!
216

217
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
151,602,691!
218
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
151,602,691!
219
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
151,602,691!
220
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
151,602,691!
221

222
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
151,602,691!
223
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
151,602,691!
224
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
151,602,691!
225
  if (pTrans->param != NULL) {
151,602,691!
226
    SDB_SET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER)
×
227
  }
228

229
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
151,602,691!
230

231
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
151,602,691✔
232
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
151,602,691!
233
  void *pIter = NULL;
151,602,691✔
234
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
151,602,691✔
235
  while (pIter) {
151,665,379✔
236
    int32_t arbGroupId = *(int32_t *)pIter;
62,688✔
237
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
62,688!
238
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
62,688✔
239
  }
240

241
  if (sver > TRANS_VER1_NUMBER) {
151,602,691!
242
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
151,602,691!
243
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
151,602,691!
244
  }
245

246
  if (sver > TRANS_VER2_NUMBER) {
151,602,691!
247
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
151,602,691✔
248
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
151,602,691!
249
    void *pIter = NULL;
151,602,691✔
250
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
151,602,691✔
251
    while (pIter) {
158,515,589✔
252
      size_t   keysize = 0;
6,912,898✔
253
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
6,912,898✔
254
      int32_t  groupPos = *(int32_t *)pIter;
6,912,898✔
255
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
6,912,898!
256
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
6,912,898!
257
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
6,912,898✔
258
    }
259
  }
260
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
151,602,691!
261
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
151,602,691!
262

263
  terrno = 0;
151,602,691✔
264

265
_OVER:
151,602,691✔
266
  if (terrno != 0) {
151,602,691!
267
    mError("trans:%d, failed to encode to raw:%p maxlen:%d len:%d since %s", pTrans->id, pRaw, sdbGetRawTotalSize(pRaw),
×
268
           dataPos, terrstr());
269
    sdbFreeRaw(pRaw);
×
270
    return NULL;
×
271
  }
272

273
  mTrace("trans:%d, encode to raw:%p, row:%p len:%d", pTrans->id, pRaw, pTrans, dataPos);
151,602,691✔
274
  return pRaw;
151,602,691✔
275
}
276

277
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
83,140,594✔
278
  if (pAction->groupId < 0) return 0;
83,140,594✔
279
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
80,757,791✔
280
  if (redoAction == NULL) {
80,757,791✔
281
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
13,644,740✔
282
    if (array != NULL) {
13,644,740!
283
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
13,644,740!
284
        mInfo("failed put action into redo group actions");
×
285
        return TSDB_CODE_INTERNAL_ERROR;
×
286
      }
287
    }
288
    redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
13,644,740✔
289
  }
290
  if (redoAction != NULL) {
80,757,791!
291
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
161,515,582!
292
  }
293
  return 0;
80,757,791✔
294
}
295

296
static int32_t mndTransDecodeActionWithGroup(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionNum,
4,233,327✔
297
                                             SHashObj *redoGroupActions, int32_t sver) {
298
  int32_t      code = 0;
4,233,327✔
299
  int32_t      lino = 0;
4,233,327✔
300
  STransAction action = {0};
4,233,327✔
301
  int32_t      dataPos = *offset;
4,233,327✔
302
  int8_t       unused = 0;
4,233,327✔
303
  int8_t       stage = 0;
4,233,327✔
304
  int8_t       actionType = 0;
4,233,327✔
305
  int32_t      dataLen = 0;
4,233,327✔
306
  int32_t      ret = -1;
4,233,327✔
307
  terrno = 0;
4,233,327✔
308

309
  for (int32_t i = 0; i < actionNum; ++i) {
87,373,921✔
310
    memset(&action, 0, sizeof(action));
83,140,594✔
311
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
83,140,594!
312
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
83,140,594!
313
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
83,140,594!
314
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
83,140,594!
315
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
83,140,594!
316
    action.actionType = actionType;
83,140,594✔
317
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
83,140,594!
318
    action.stage = stage;
83,140,594✔
319
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
83,140,594!
320
    if (sver > TRANS_VER2_NUMBER) {
83,140,594!
321
      SDB_GET_INT32(pRaw, dataPos, &action.groupId, _OVER)
83,140,594!
322
    }
323
    if (action.actionType == TRANS_ACTION_RAW) {
83,140,594✔
324
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
15,215,658!
325
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
15,215,658!
326
      action.pRaw = taosMemoryMalloc(dataLen);
15,215,658!
327
      if (action.pRaw == NULL) goto _OVER;
15,215,658!
328
      mTrace("raw:%p, is created", action.pRaw);
15,215,658!
329
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
15,215,658!
330
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
15,215,658!
331
      STransAction *pAction = taosArrayGet(pActions, taosArrayGetSize(pActions) - 1);
15,215,658✔
332
      if (mndTransDecodeGroupRedoAction(redoGroupActions, pAction) != 0) goto _OVER;
15,215,658!
333
      action.pRaw = NULL;
15,215,658✔
334
    } else if (action.actionType == TRANS_ACTION_MSG) {
67,924,936!
335
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
67,924,936!
336
      tmsgUpdateDnodeEpSet(&action.epSet);
67,924,936✔
337
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
67,924,936!
338
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
67,924,936!
339
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
67,924,936!
340
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
67,924,936!
341
      action.pCont = taosMemoryMalloc(action.contLen);
67,924,936!
342
      if (action.pCont == NULL) goto _OVER;
67,924,936!
343
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
67,924,936!
344
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
67,924,936!
345
      STransAction *pAction = taosArrayGet(pActions, taosArrayGetSize(pActions) - 1);
67,924,936✔
346
      if (mndTransDecodeGroupRedoAction(redoGroupActions, pAction) != 0) goto _OVER;
67,924,936!
347
      action.pCont = NULL;
67,924,936✔
348
    } else {
349
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
350
    }
351
  }
352
  ret = 0;
4,233,327✔
353

354
_OVER:
4,233,327✔
355
  if (terrno != 0) mError("failed to decode action with group at line:%d, since %s", lino, terrstr());
4,233,327!
356
  *offset = dataPos;
4,233,327✔
357
  taosMemoryFreeClear(action.pCont);
4,233,327!
358
  return ret;
4,233,327✔
359
}
360

361
static int32_t mndTransDecodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionNum, int32_t sver) {
1,136,328,265✔
362
  int32_t      code = 0;
1,136,328,265✔
363
  int32_t      lino = 0;
1,136,328,265✔
364
  STransAction action = {0};
1,136,328,265✔
365
  int32_t      dataPos = *offset;
1,136,328,265✔
366
  int8_t       unused = 0;
1,136,328,265✔
367
  int8_t       stage = 0;
1,136,328,265✔
368
  int8_t       actionType = 0;
1,136,328,265✔
369
  int32_t      dataLen = 0;
1,136,328,265✔
370
  int32_t      ret = -1;
1,136,328,265✔
371
  terrno = 0;
1,136,328,265✔
372

373
  for (int32_t i = 0; i < actionNum; ++i) {
2,147,483,647✔
374
    memset(&action, 0, sizeof(action));
2,092,768,195✔
375
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
2,092,768,195!
376
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
2,092,768,195!
377
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
2,092,768,195!
378
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
2,092,768,195!
379
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
2,092,768,195!
380
    action.actionType = actionType;
2,092,768,195✔
381
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
2,092,768,195!
382
    action.stage = stage;
2,092,768,195✔
383
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
2,092,768,195!
384
    if (sver > TRANS_VER2_NUMBER) {
2,092,768,195!
385
      SDB_GET_INT32(pRaw, dataPos, &action.groupId, _OVER)
2,092,768,195!
386
    }
387
    if (action.actionType == TRANS_ACTION_RAW) {
2,092,768,195✔
388
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
1,396,804,609!
389
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
1,396,804,609!
390
      action.pRaw = taosMemoryMalloc(dataLen);
1,396,804,609!
391
      if (action.pRaw == NULL) goto _OVER;
1,396,804,609!
392
      mTrace("raw:%p, is created", action.pRaw);
1,396,804,609✔
393
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
1,396,804,609!
394
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
1,396,804,609!
395
      action.pRaw = NULL;
1,396,804,609✔
396
    } else if (action.actionType == TRANS_ACTION_MSG) {
695,963,586!
397
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
695,963,586!
398
      tmsgUpdateDnodeEpSet(&action.epSet);
695,963,586✔
399
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
695,963,586!
400
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
695,963,586!
401
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
695,963,586!
402
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
695,963,586!
403
      action.pCont = taosMemoryMalloc(action.contLen);
695,963,586!
404
      if (action.pCont == NULL) goto _OVER;
695,963,586!
405
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
695,963,586!
406
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
695,963,586!
407
      action.pCont = NULL;
695,963,586✔
408
    } else {
409
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
410
    }
411
  }
412
  ret = 0;
1,136,328,265✔
413

414
_OVER:
1,136,328,265✔
415
  if (terrno != 0) mError("failed to decode action at line:%d, since %s", lino, terrstr());
1,136,328,265!
416
  *offset = dataPos;
1,136,328,265✔
417
  taosMemoryFreeClear(action.pCont);
1,136,328,265!
418
  return ret;
1,136,328,265✔
419
}
420

421
SSdbRow *mndTransDecode(SSdbRaw *pRaw) {
285,140,398✔
422
  terrno = TSDB_CODE_INVALID_MSG;
285,140,398✔
423
  int32_t  code = 0;
285,140,398✔
424
  int32_t  lino = 0;
285,140,398✔
425
  SSdbRow *pRow = NULL;
285,140,398✔
426
  STrans  *pTrans = NULL;
285,140,398✔
427
  char    *pData = NULL;
285,140,398✔
428
  int32_t  dataLen = 0;
285,140,398✔
429
  int8_t   sver = 0;
285,140,398✔
430
  int32_t  prepareActionNum = 0;
285,140,398✔
431
  int32_t  redoActionNum = 0;
285,140,398✔
432
  int32_t  undoActionNum = 0;
285,140,398✔
433
  int32_t  commitActionNum = 0;
285,140,398✔
434
  int32_t  dataPos = 0;
285,140,398✔
435
  int32_t  arbgroupIdNum = 0;
285,140,398✔
436

437
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
285,140,398!
438

439
  if (sver > TRANS_VER3_NUMBER) {
285,140,398!
440
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
441
    goto _OVER;
×
442
  }
443

444
  pRow = sdbAllocRow(sizeof(STrans));
285,140,398✔
445
  if (pRow == NULL) goto _OVER;
285,140,398!
446

447
  pTrans = sdbGetRowObj(pRow);
285,140,398✔
448
  if (pTrans == NULL) goto _OVER;
285,140,398!
449

450
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
285,140,398!
451

452
  int8_t stage = 0;
285,140,398✔
453
  int8_t policy = 0;
285,140,398✔
454
  int8_t conflict = 0;
285,140,398✔
455
  int8_t exec = 0;
285,140,398✔
456
  int8_t oper = 0;
285,140,398✔
457
  int8_t reserved = 0;
285,140,398✔
458
  int8_t actionType = 0;
285,140,398✔
459
  SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
285,140,398!
460
  SDB_GET_INT8(pRaw, dataPos, &policy, _OVER)
285,140,398!
461
  SDB_GET_INT8(pRaw, dataPos, &conflict, _OVER)
285,140,398!
462
  SDB_GET_INT8(pRaw, dataPos, &exec, _OVER)
285,140,398!
463
  SDB_GET_INT8(pRaw, dataPos, &oper, _OVER)
285,140,398!
464
  SDB_GET_INT8(pRaw, dataPos, &reserved, _OVER)
285,140,398!
465
  pTrans->stage = stage;
285,140,398✔
466
  pTrans->policy = policy;
285,140,398✔
467
  pTrans->conflict = conflict;
285,140,398✔
468
  pTrans->exec = exec;
285,140,398✔
469
  pTrans->oper = oper;
285,140,398✔
470
  SDB_GET_INT16(pRaw, dataPos, &pTrans->originRpcType, _OVER)
285,140,398!
471
  SDB_GET_INT64(pRaw, dataPos, &pTrans->createdTime, _OVER)
285,140,398!
472
  SDB_GET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
285,140,398!
473
  SDB_GET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
285,140,398!
474
  SDB_GET_INT32(pRaw, dataPos, &pTrans->actionPos, _OVER)
285,140,398!
475

476
  if (sver > TRANS_VER1_NUMBER) {
285,140,398!
477
    SDB_GET_INT32(pRaw, dataPos, &prepareActionNum, _OVER)
285,140,398!
478
  }
479
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, _OVER)
285,140,398!
480
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, _OVER)
285,140,398!
481
  SDB_GET_INT32(pRaw, dataPos, &commitActionNum, _OVER)
285,140,398!
482

483
  pTrans->prepareActions = taosArrayInit(prepareActionNum, sizeof(STransAction));
285,140,398✔
484
  pTrans->redoActions = taosArrayInit(redoActionNum, sizeof(STransAction));
285,140,398✔
485
  pTrans->undoActions = taosArrayInit(undoActionNum, sizeof(STransAction));
285,140,398✔
486
  pTrans->commitActions = taosArrayInit(commitActionNum, sizeof(STransAction));
285,140,398✔
487
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
285,140,398✔
488
    pTrans->redoGroupActions = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
4,233,327✔
489
    pTrans->groupActionPos = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
4,233,327✔
490
  }
491

492
  if (pTrans->prepareActions == NULL) goto _OVER;
285,140,398!
493
  if (pTrans->redoActions == NULL) goto _OVER;
285,140,398!
494
  if (pTrans->undoActions == NULL) goto _OVER;
285,140,398!
495
  if (pTrans->commitActions == NULL) goto _OVER;
285,140,398!
496

497
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
285,140,398!
498
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
285,140,398✔
499
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
4,233,327!
500
                                      sver) < 0)
501
      goto _OVER;
×
502
  } else {
503
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
280,907,071!
504
  }
505
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
285,140,398!
506
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
285,140,398!
507

508
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
285,140,398!
509
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
285,140,398!
510
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
285,140,398!
511
  if (pTrans->paramLen != 0) {
285,140,398!
512
    pTrans->param = taosMemoryMalloc(pTrans->paramLen);
×
513
    if (pTrans->param == NULL) goto _OVER;
×
514
    SDB_GET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER);
×
515
  }
516

517
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
285,140,398!
518

519
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
285,140,398!
520
  if (arbgroupIdNum > 0) {
285,140,398✔
521
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
88,380✔
522
    if (pTrans->arbGroupIds == NULL) goto _OVER;
88,380!
523
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
192,860✔
524
      int32_t arbGroupId = 0;
104,480✔
525
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
104,480!
526
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
104,480!
527
    }
528
  }
529

530
  int8_t ableKill = 0;
285,140,398✔
531
  int32_t killMode = 0;
285,140,398✔
532
  if (sver > TRANS_VER1_NUMBER) {
285,140,398!
533
    SDB_GET_INT8(pRaw, dataPos, &ableKill, _OVER)
285,140,398!
534
    SDB_GET_INT32(pRaw, dataPos, &killMode, _OVER)
285,140,398!
535
  }
536
  pTrans->ableToBeKilled = ableKill;
285,140,398✔
537
  pTrans->killMode = (int8_t)killMode;
285,140,398✔
538

539
  if (sver > TRANS_VER2_NUMBER) {
285,140,398!
540
    int32_t groupNum = -1;
285,140,398✔
541
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
285,140,398!
542
    for (int32_t i = 0; i < groupNum; ++i) {
298,785,138✔
543
      int32_t groupId = -1;
13,644,740✔
544
      int32_t groupPos = -1;
13,644,740✔
545
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
13,644,740!
546
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
13,644,740!
547
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
13,644,740!
548
        goto _OVER;
×
549
    }
550
  }
551

552
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
285,140,398!
553

554
  terrno = 0;
285,140,398✔
555

556
_OVER:
285,140,398✔
557
  if (terrno != 0 && pTrans != NULL) {
285,140,398!
558
    mError("trans:%d, failed to parse from raw:%p at line:%d dataPos:%d dataLen:%d since %s", pTrans->id, pRaw, lino,
×
559
           dataPos, pRaw->dataLen, terrstr());
560
    mndTransDropData(pTrans);
×
561
    taosMemoryFreeClear(pRow);
×
562
    return NULL;
×
563
  }
564

565
  if (pTrans != NULL) {
285,140,398!
566
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
285,140,398✔
567
  }
568
  return pRow;
285,140,398✔
569
}
570

571
static const char *mndTransStr(ETrnStage stage) {
2,053,649,091✔
572
  switch (stage) {
2,053,649,091!
573
    case TRN_STAGE_PREPARE:
153,163,084✔
574
      return "prepare";
153,163,084✔
575
    case TRN_STAGE_REDO_ACTION:
744,886,530✔
576
      return "redoAction";
744,886,530✔
577
    case TRN_STAGE_ROLLBACK:
16,260✔
578
      return "rollback";
16,260✔
579
    case TRN_STAGE_UNDO_ACTION:
37,917,674✔
580
      return "undoAction";
37,917,674✔
581
    case TRN_STAGE_COMMIT:
241,824,867✔
582
      return "commit";
241,824,867✔
583
    case TRN_STAGE_COMMIT_ACTION:
510,207,101✔
584
      return "commitAction";
510,207,101✔
585
    case TRN_STAGE_FINISH:
365,617,315✔
586
      return "finished";
365,617,315✔
587
    case TRN_STAGE_PRE_FINISH:
16,260✔
588
      return "pre-finish";
16,260✔
589
    default:
×
590
      return "invalid";
×
591
  }
592
}
593

594
static const char *mndTransTypeStr(ETrnAct actionType) {
324,192✔
595
  switch (actionType) {
324,192!
596
    case TRANS_ACTION_MSG:
265,248✔
597
      return "msg";
265,248✔
598
    case TRANS_ACTION_RAW:
58,944✔
599
      return "sdb";
58,944✔
600
    default:
×
601
      return "invalid";
×
602
  }
603
}
604

605
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
584,086,105✔
606
  if (pAction != NULL) {
584,086,105✔
607
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
461,535,165✔
608
      pTrans->lastAction = pAction->id;
331,589,500✔
609
      pTrans->lastMsgType = pAction->msgType;
331,589,500✔
610
      pTrans->lastEpset = pAction->epSet;
331,589,500✔
611
      pTrans->lastErrorNo = pAction->errCode;
331,589,500✔
612
    }
613
  } else {
614
    pTrans->lastAction = 0;
122,550,940✔
615
    pTrans->lastMsgType = 0;
122,550,940✔
616
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
122,550,940!
617
    pTrans->lastErrorNo = 0;
122,550,940✔
618
  }
619
}
584,086,105✔
620

621
static void mndTransTestStartFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
622
  mInfo("test trans start, param:%s, len:%d", (char *)param, paramLen);
×
623
}
×
624

625
static void mndTransTestStopFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
626
  mInfo("test trans stop, param:%s, len:%d", (char *)param, paramLen);
×
627
}
×
628

629
static TransCbFp mndTransGetCbFp(ETrnFunc ftype) {
1,388,260✔
630
  switch (ftype) {
1,388,260!
631
    case TRANS_START_FUNC_TEST:
×
632
      return mndTransTestStartFunc;
×
633
    case TRANS_STOP_FUNC_TEST:
×
634
      return mndTransTestStopFunc;
×
635
    case TRANS_START_FUNC_MQ_REB:
694,130✔
636
      return mndRebCntInc;
694,130✔
637
    case TRANS_STOP_FUNC_MQ_REB:
694,130✔
638
      return mndRebCntDec;
694,130✔
639
    default:
×
640
      return NULL;
×
641
  }
642
}
643

644
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
53,988,555✔
645
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
53,988,555!
646
        mndTransStr(pTrans->stage), pTrans->startFunc);
647

648
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
53,988,555✔
649

650
  if (pTrans->startFunc > 0) {
53,988,555✔
651
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
694,130✔
652
    if (fp) {
694,130!
653
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
694,130✔
654
    }
655
    // pTrans->startFunc = 0;
656
  }
657

658
  if (pTrans->stage == TRN_STAGE_COMMIT) {
53,988,555✔
659
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
1,944✔
660
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pTrans->id);
1,944!
661
  }
662

663
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
53,988,555!
664
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
×
665
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
×
666
  }
667

668
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
53,988,555!
669
    pTrans->stage = TRN_STAGE_FINISH;
×
670
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pTrans->id);
×
671
  }
672

673
  return 0;
53,988,555✔
674
}
675

676
void mndTransDropData(STrans *pTrans) {
330,724,341✔
677
  if (pTrans->prepareActions != NULL) {
330,724,341!
678
    mndTransDropActions(pTrans->prepareActions);
330,724,341✔
679
    pTrans->prepareActions = NULL;
330,724,341✔
680
  }
681
  if (pTrans->redoActions != NULL) {
330,724,341!
682
    mndTransDropActions(pTrans->redoActions);
330,724,341✔
683
    pTrans->redoActions = NULL;
330,724,341✔
684
  }
685
  if (pTrans->undoActions != NULL) {
330,724,341!
686
    mndTransDropActions(pTrans->undoActions);
330,724,341✔
687
    pTrans->undoActions = NULL;
330,724,341✔
688
  }
689
  if (pTrans->commitActions != NULL) {
330,724,341!
690
    mndTransDropActions(pTrans->commitActions);
330,724,341✔
691
    pTrans->commitActions = NULL;
330,724,341✔
692
  }
693
  if (pTrans->redoGroupActions != NULL) {
330,724,341✔
694
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
49,817,270✔
695
    while (pIter) {
64,063,106✔
696
      SArray **redoActions = pIter;
14,245,836✔
697
      taosArrayDestroy(*redoActions);
14,245,836✔
698
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
14,245,836✔
699
    }
700

701
    taosHashCleanup(pTrans->redoGroupActions);
49,817,270✔
702
    pTrans->redoGroupActions = NULL;
49,817,270✔
703
  }
704
  if (pTrans->groupActionPos != NULL) {
330,724,341✔
705
    taosHashCleanup(pTrans->groupActionPos);
49,817,270✔
706
    pTrans->groupActionPos = NULL;
49,817,270✔
707
  }
708
  if (pTrans->arbGroupIds != NULL) {
330,724,341✔
709
    taosHashCleanup(pTrans->arbGroupIds);
45,672,323✔
710
  }
711
  if (pTrans->pRpcArray != NULL) {
330,724,341✔
712
    taosArrayDestroy(pTrans->pRpcArray);
45,583,943✔
713
    pTrans->pRpcArray = NULL;
45,583,943✔
714
  }
715
  if (pTrans->rpcRsp != NULL) {
330,724,341✔
716
    taosMemoryFree(pTrans->rpcRsp);
17,746,312!
717
    pTrans->rpcRsp = NULL;
17,746,312✔
718
    pTrans->rpcRspLen = 0;
17,746,312✔
719
  }
720
  if (pTrans->param != NULL) {
330,724,341!
721
    taosMemoryFree(pTrans->param);
×
722
    pTrans->param = NULL;
×
723
    pTrans->paramLen = 0;
×
724
  }
725
  (void)taosThreadMutexDestroy(&pTrans->mutex);
330,724,341✔
726
}
330,724,341✔
727

728
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc) {
169,376,854✔
729
  mInfo("trans:%d, perform delete action, row:%p stage:%s callfunc:%d, stopFunc:%d", pTrans->id, pTrans,
169,376,854!
730
        mndTransStr(pTrans->stage), callFunc, pTrans->stopFunc);
731

732
  if (pTrans->stopFunc > 0 && callFunc) {
169,376,854✔
733
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
694,130✔
734
    if (fp) {
694,130!
735
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
694,130✔
736
    }
737
    // pTrans->stopFunc = 0;
738
  }
739

740
  mndTransDropData(pTrans);
169,376,854✔
741
  return 0;
169,376,854✔
742
}
743

744
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
247,553,740✔
745
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
782,766,680✔
746
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
535,212,940✔
747
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
535,212,940✔
748
    pOldAction->rawWritten = pNewAction->rawWritten;
535,212,940✔
749
    pOldAction->msgSent = pNewAction->msgSent;
535,212,940✔
750
    pOldAction->msgReceived = pNewAction->msgReceived;
535,212,940✔
751
    pOldAction->errCode = pNewAction->errCode;
535,212,940✔
752
  }
753
}
247,553,740✔
754

755
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) {
61,888,435✔
756
  mInfo("trans:%d, perform update action, old row:%p stage:%s create:%" PRId64 ", new row:%p stage:%s create:%" PRId64,
61,888,435!
757
        pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage), pNew->createdTime);
758

759
  if (pOld->createdTime != pNew->createdTime) {
61,888,435!
760
    mError("trans:%d, failed to perform update action since createTime not match, old row:%p stage:%s create:%" PRId64
×
761
           ", new row:%p stage:%s create:%" PRId64,
762
           pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage),
763
           pNew->createdTime);
764
    // only occured while sync timeout
765
    TAOS_RETURN(TSDB_CODE_MND_TRANS_SYNC_TIMEOUT);
×
766
  }
767

768
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
61,888,435✔
769
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
61,888,435✔
770
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
61,888,435✔
771
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
61,888,435✔
772
  pOld->stage = pNew->stage;
61,888,435✔
773
  pOld->actionPos = pNew->actionPos;
61,888,435✔
774

775
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
61,888,435✔
776
  while (pIter != NULL) {
67,786,465✔
777
    int32_t newActionPos = *(int32_t *)pIter;
5,898,030✔
778

779
    size_t   keylen = 0;
5,898,030✔
780
    int32_t *groupId = taosHashGetKey(pIter, &keylen);
5,898,030✔
781

782
    int32_t *oldActionPos = taosHashGet(pOld->groupActionPos, groupId, sizeof(int32_t));
5,898,030✔
783
    if (oldActionPos != NULL) {
5,898,030!
784
      *oldActionPos = newActionPos;
5,898,030✔
785
    } else
786
      taosHashPut(pOld->groupActionPos, groupId, sizeof(int32_t), &newActionPos, sizeof(int32_t));
×
787

788
    pIter = taosHashIterate(pNew->groupActionPos, pIter);
5,898,030✔
789
  }
790

791
  if (pOld->stage == TRN_STAGE_COMMIT) {
61,888,435✔
792
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
53,494,668✔
793
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
53,494,668!
794
  }
795

796
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
61,888,435✔
797
    pOld->stage = TRN_STAGE_UNDO_ACTION;
3,252✔
798
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pNew->id);
3,252!
799
  }
800

801
  if (pOld->stage == TRN_STAGE_PRE_FINISH) {
61,888,435✔
802
    pOld->stage = TRN_STAGE_FINISH;
3,252✔
803
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pNew->id);
3,252!
804
  }
805

806
  return 0;
61,888,435✔
807
}
808

809
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
226,063,661✔
810
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
226,063,661✔
811
  if (pTrans == NULL) {
226,063,661!
812
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
×
813
  }
814
  return pTrans;
226,063,661✔
815
}
816

817
void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
226,064,899✔
818
  SSdb *pSdb = pMnode->pSdb;
226,064,899✔
819
  if (pTrans != NULL) mInfo("vgId:1, trans:%d, release transaction", pTrans->id);
226,064,899!
820
  sdbRelease(pSdb, pTrans);
226,064,899✔
821
}
226,064,899✔
822

823
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
45,583,943✔
824
                       const char *opername) {
825
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
45,583,943!
826
  if (pTrans == NULL) {
45,583,943!
827
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
828
    mError("failed to create transaction since %s", terrstr());
×
829
    return NULL;
×
830
  }
831

832
  if (opername != NULL) {
45,583,943!
833
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
45,583,943!
834
  }
835

836
  int32_t sdbMaxId = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
45,583,943✔
837
  sdbReadLock(pMnode->pSdb, SDB_TRANS);
45,583,943✔
838
  pTrans->id = TMAX(sdbMaxId, tsMaxTransId + 1);
45,583,943✔
839
  sdbUnLock(pMnode->pSdb, SDB_TRANS);
45,583,943✔
840
  pTrans->stage = TRN_STAGE_PREPARE;
45,583,943✔
841
  pTrans->policy = policy;
45,583,943✔
842
  pTrans->conflict = conflict;
45,583,943✔
843
  pTrans->exec = TRN_EXEC_PARALLEL;
45,583,943✔
844
  pTrans->ableToBeKilled = false;
45,583,943✔
845
  pTrans->createdTime = taosGetTimestampMs();
45,583,943✔
846
  pTrans->prepareActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
45,583,943✔
847
  pTrans->redoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
45,583,943✔
848
  pTrans->undoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
45,583,943✔
849
  pTrans->commitActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
45,583,943✔
850
  pTrans->redoGroupActions = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
45,583,943✔
851
  pTrans->groupActionPos = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
45,583,943✔
852
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
45,583,943✔
853
  pTrans->pRpcArray = taosArrayInit(1, sizeof(SRpcHandleInfo));
45,583,943✔
854
  pTrans->mTraceId = pReq ? TRACE_GET_ROOTID(&pReq->info.traceId) : tGenIdPI64();
45,583,943✔
855
  taosInitRWLatch(&pTrans->lockRpcArray);
45,583,943✔
856
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
45,583,943✔
857

858
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
45,583,943!
859
      pTrans->pRpcArray == NULL) {
45,583,943!
860
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
861
    mError("failed to create transaction since %s", terrstr());
×
862
    mndTransDrop(pTrans);
×
863
    return NULL;
×
864
  }
865

866
  if (pReq != NULL) {
45,583,943✔
867
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
64,878,810!
868
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
869
      return NULL;
×
870
    }
871
    pTrans->originRpcType = pReq->msgType;
32,439,405✔
872
  }
873

874
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
45,583,943!
875

876
  mTrace("trans:%d, local object is created, data:%p", pTrans->id, pTrans);
45,583,943✔
877
  return pTrans;
45,583,943✔
878
}
879

880
static void mndTransDropActions(SArray *pArray) {
1,322,897,364✔
881
  int32_t size = taosArrayGetSize(pArray);
1,322,897,364✔
882
  for (int32_t i = 0; i < size; ++i) {
2,147,483,647✔
883
    STransAction *pAction = taosArrayGet(pArray, i);
2,147,483,647✔
884
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,147,483,647✔
885
      taosMemoryFreeClear(pAction->pRaw);
1,646,942,769!
886
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
845,040,825!
887
      taosMemoryFreeClear(pAction->pCont);
845,040,825!
888
    } else {
889
      // nothing
890
    }
891
  }
892

893
  taosArrayDestroy(pArray);
1,322,897,364✔
894
}
1,322,897,364✔
895

896
void mndTransDrop(STrans *pTrans) {
45,893,026✔
897
  if (pTrans != NULL) {
45,893,026✔
898
    mndTransDropData(pTrans);
45,583,943✔
899
    mTrace("trans:%d, local object is freed, data:%p", pTrans->id, pTrans);
45,583,943✔
900
    taosMemoryFreeClear(pTrans);
45,583,943!
901
  }
902
}
45,893,026✔
903

904
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
316,074,805✔
905
  pAction->id = taosArrayGetSize(pArray);
316,074,805✔
906

907
  void *ptr = taosArrayPush(pArray, pAction);
316,074,805✔
908
  if (ptr == NULL) {
316,074,805!
909
    TAOS_RETURN(terrno);
×
910
  }
911

912
  return 0;
316,074,805✔
913
}
914

915
static int32_t mndTransAddActionToGroup(STrans *pTrans, STransAction *pAction) {
17,262,383✔
916
  if (pTrans->exec != TRN_EXEC_GROUP_PARALLEL) return 0;
17,262,383✔
917

918
  SArray **redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
1,536,193✔
919
  if (redoAction == NULL) {
1,536,193✔
920
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
601,096✔
921
    if (array != NULL) {
601,096!
922
      if (taosHashPut(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
601,096!
923
        mInfo("failed put action into redo group actions");
×
924
        return TSDB_CODE_INTERNAL_ERROR;
×
925
      }
926
      redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
601,096✔
927
    }
928
  }
929
  if (redoAction != NULL) {
1,536,193!
930
    mInfo("trans:%d, append action into group %d, msgType:%s", pTrans->id, pAction->groupId,
1,536,193!
931
          TMSG_INFO(pAction->msgType));
932
    void *ptr = taosArrayPush(*redoAction, &pAction);
1,536,193✔
933
    if (ptr == NULL) {
1,536,193!
934
      TAOS_RETURN(terrno);
×
935
    }
936
  }
937

938
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &pAction->groupId, sizeof(int32_t));
1,536,193✔
939
  if (actionPos == NULL) {
1,536,193✔
940
    int32_t pos = 0;
601,096✔
941
    if (taosHashPut(pTrans->groupActionPos, &pAction->groupId, sizeof(int32_t), &pos, sizeof(int32_t)) < 0) {
601,096!
942
      mInfo("failed put action into group action pos");
×
943
      return TSDB_CODE_INTERNAL_ERROR;
×
944
    }
945
  }
946

947
  return 0;
1,536,193✔
948
}
949

950
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
1,113,253✔
951
  STransAction action = {
1,113,253✔
952
      .stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw, .mTraceId = pTrans->mTraceId};
1,113,253✔
953
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) return TSDB_CODE_INTERNAL_ERROR;
1,113,253!
954
  return mndTransAppendAction(pTrans->redoActions, &action);
1,113,253✔
955
}
956

957
int32_t mndTransAppendGroupRedolog(STrans *pTrans, SSdbRaw *pRaw, int32_t groupId) {
1,202,289✔
958
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION,
1,202,289✔
959
                         .actionType = TRANS_ACTION_RAW,
960
                         .pRaw = pRaw,
961
                         .mTraceId = pTrans->mTraceId,
1,202,289✔
962
                         .groupId = groupId};
963
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
1,202,289!
964
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
×
965
    return TSDB_CODE_INTERNAL_ERROR;
×
966
  }
967
  if (groupId > 0) TAOS_CHECK_RETURN(mndTransAddActionToGroup(pTrans, &action));
1,202,289!
968
  return mndTransAppendAction(pTrans->redoActions, &action);
1,202,289✔
969
}
970

971
int32_t mndTransAppendNullLog(STrans *pTrans) {
×
972
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_NULL};
×
973
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
×
974
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
×
975
    return TSDB_CODE_INTERNAL_ERROR;
×
976
  }
977
  return mndTransAppendAction(pTrans->redoActions, &action);
×
978
}
979

980
int32_t mndTransAppendGroupNullLog(STrans *pTrans, int32_t groupId) {
×
981
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_NULL, .groupId = groupId};
×
982
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
×
983
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
×
984
    return TSDB_CODE_INTERNAL_ERROR;
×
985
  }
986
  if (groupId > 0) TAOS_CHECK_RETURN(mndTransAddActionToGroup(pTrans, &action));
×
987
  return mndTransAppendAction(pTrans->redoActions, &action);
×
988
}
989

990
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
12,285,409✔
991
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
12,285,409✔
992
  return mndTransAppendAction(pTrans->undoActions, &action);
12,285,409✔
993
}
994

995
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
195,610,977✔
996
  STransAction action = {.stage = TRN_STAGE_COMMIT_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
195,610,977✔
997
  return mndTransAppendAction(pTrans->commitActions, &action);
195,610,977✔
998
}
999

1000
int32_t mndTransAppendPrepareLog(STrans *pTrans, SSdbRaw *pRaw) {
24,710,574✔
1001
  STransAction action = {
24,710,574✔
1002
      .pRaw = pRaw, .stage = TRN_STAGE_PREPARE, .actionType = TRANS_ACTION_RAW, .mTraceId = pTrans->mTraceId};
24,710,574✔
1003
  return mndTransAppendAction(pTrans->prepareActions, &action);
24,710,574✔
1004
}
1005

1006
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
55,663,811✔
1007
  pAction->stage = TRN_STAGE_REDO_ACTION;
55,663,811✔
1008
  pAction->actionType = TRANS_ACTION_MSG;
55,663,811✔
1009
  pAction->mTraceId = pTrans->mTraceId;
55,663,811✔
1010
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId == 0) {
55,663,811✔
1011
    mError("trans:%d, groupid cannot be 0 in TRN_EXEC_GROUP_PARALLEL", pTrans->id);
3,937!
1012
    return TSDB_CODE_INTERNAL_ERROR;
3,937✔
1013
  }
1014
  if (pAction->groupId > 0) TAOS_CHECK_RETURN(mndTransAddActionToGroup(pTrans, pAction));
55,659,874!
1015
  return mndTransAppendAction(pTrans->redoActions, pAction);
55,659,874✔
1016
}
1017

1018
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
25,492,429✔
1019
  pAction->stage = TRN_STAGE_UNDO_ACTION;
25,492,429✔
1020
  pAction->actionType = TRANS_ACTION_MSG;
25,492,429✔
1021
  return mndTransAppendAction(pTrans->undoActions, pAction);
25,492,429✔
1022
}
1023

1024
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
17,746,312✔
1025
  pTrans->rpcRsp = pCont;
17,746,312✔
1026
  pTrans->rpcRspLen = contLen;
17,746,312✔
1027
}
17,746,312✔
1028

1029
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
685,892✔
1030
  pTrans->startFunc = startFunc;
685,892✔
1031
  pTrans->stopFunc = stopFunc;
685,892✔
1032
  pTrans->param = param;
685,892✔
1033
  pTrans->paramLen = paramLen;
685,892✔
1034
}
685,892✔
1035

1036
int32_t mndSetRpcInfoForDbTrans(SMnode *pMnode, SRpcMsg *pMsg, EOperType oper, const char *dbname) {
×
1037
  STrans *pTrans = NULL;
×
1038
  void   *pIter = NULL;
×
1039
  int32_t code = -1;
×
1040

1041
  while (1) {
1042
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
×
1043
    if (pIter == NULL) break;
×
1044

1045
    if (pTrans->oper == oper) {
×
1046
      if (taosStrcasecmp(dbname, pTrans->dbname) == 0) {
×
1047
        mInfo("trans:%d, db:%s oper:%d matched with input", pTrans->id, dbname, oper);
×
1048
        taosWLockLatch(&pTrans->lockRpcArray);
×
1049
        if (pTrans->pRpcArray == NULL) {
×
1050
          pTrans->pRpcArray = taosArrayInit(4, sizeof(SRpcHandleInfo));
×
1051
        }
1052
        if (pTrans->pRpcArray != NULL && taosArrayPush(pTrans->pRpcArray, &pMsg->info) != NULL) {
×
1053
          code = 0;
×
1054
        }
1055
        taosWUnLockLatch(&pTrans->lockRpcArray);
×
1056

1057
        sdbRelease(pMnode->pSdb, pTrans);
×
1058
        break;
×
1059
      }
1060
    }
1061

1062
    sdbRelease(pMnode->pSdb, pTrans);
×
1063
  }
1064
  return code;
×
1065
}
1066

1067
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
26,440,822✔
1068
  if (dbname != NULL) {
26,440,822!
1069
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
26,440,822!
1070
  }
1071
  if (stbname != NULL) {
26,440,822✔
1072
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
19,542,413!
1073
  }
1074
}
26,440,822✔
1075

1076
void mndTransAddArbGroupId(STrans *pTrans, int32_t groupId) {
98,561✔
1077
  if (taosHashPut(pTrans->arbGroupIds, &groupId, sizeof(int32_t), NULL, 0) != 0) {
98,561!
1078
    mError("trans:%d, failed to put groupid into hash, groupId:%d", pTrans->id, groupId);
×
1079
  }
1080
}
98,561✔
1081

1082
void mndTransSetSerial(STrans *pTrans) {
1,131,364✔
1083
  mInfo("trans:%d, set Serial", pTrans->id);
1,131,364!
1084
  pTrans->exec = TRN_EXEC_SERIAL;
1,131,364✔
1085
}
1,131,364✔
1086

1087
void mndTransSetGroupParallel(STrans *pTrans) {
241,178✔
1088
  mInfo("trans:%d, set Group Parallel", pTrans->id);
241,178!
1089
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
241,178✔
1090
}
241,178✔
1091

1092
void mndTransSetParallel(STrans *pTrans) {
×
1093
  mInfo("trans:%d, set Parallel", pTrans->id);
×
1094
  pTrans->exec = TRN_EXEC_PARALLEL;
×
1095
}
×
1096

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

1099
void mndTransSetKillMode(STrans *pTrans, ETrnKillMode killMode) {
55,658✔
1100
  pTrans->ableToBeKilled = true; 
55,658✔
1101
  pTrans->killMode = killMode; 
55,658✔
1102
}
55,658✔
1103

1104
void mndTransSetChangeless(STrans *pTrans) { pTrans->changeless = true; }
34,311✔
1105

1106
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
3,811,668✔
1107

1108
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
97,528,825✔
1109
  int32_t  code = 0;
97,528,825✔
1110
  SSdbRaw *pRaw = mndTransEncode(pTrans);
97,528,825✔
1111
  if (pRaw == NULL) {
97,528,825!
1112
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1113
    if (terrno != 0) code = terrno;
×
1114
    mError("trans:%d, failed to encode while sync trans since %s", pTrans->id, tstrerror(code));
×
1115
    TAOS_RETURN(code);
×
1116
  }
1117
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_READY));
97,528,825!
1118

1119
  mInfo("trans:%d, sync to other mnodes, stage:%s createTime:%" PRId64, pTrans->id, mndTransStr(pTrans->stage),
97,528,825!
1120
        pTrans->createdTime);
1121
  code = mndSyncPropose(pMnode, pRaw, pTrans->id);
97,528,825✔
1122
  if (code != 0) {
97,528,825✔
1123
    mError("trans:%d, failed to sync, errno:%s code:0x%x createTime:%" PRId64 " saved trans:%d", pTrans->id,
18,108!
1124
           tstrerror(code), code, pTrans->createdTime, pMnode->syncMgmt.transId);
1125
    sdbFreeRaw(pRaw);
18,108✔
1126
    TAOS_RETURN(code);
18,108✔
1127
  }
1128

1129
  sdbFreeRaw(pRaw);
97,510,717✔
1130
  mInfo("trans:%d, sync to other mnodes finished, createTime:%" PRId64, pTrans->id, pTrans->createdTime);
97,510,717!
1131
  TAOS_RETURN(code);
97,510,717✔
1132
}
1133

1134
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
1,055,850✔
1135
  if (conflict[0] == 0) return false;
1,055,850!
1136
  if (taosStrcasecmp(conflict, pTrans->dbname) == 0) return true;
1,055,850!
1137
  return false;
895,915✔
1138
}
1139

1140
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
16,156,466✔
1141
  if (conflict[0] == 0) return false;
16,156,466✔
1142
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
15,260,201!
1143
  return false;
15,022,261✔
1144
}
1145

1146
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
17,212,316✔
1147
  if (conflict) {
17,212,316✔
1148
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
397,875!
1149
      "stb:%s type:%d", 
1150
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1151
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1152
    *globalConflict = true;
397,875✔
1153
  } else {
1154
    mInfo("trans:%d, opername:%s db:%s stb:%s type:%d, not conflict with trans:%d, opername:%s db:%s stb:%s type:%d", 
16,814,441!
1155
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1156
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1157
  }
1158
}
17,212,316✔
1159

1160
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
125,933,013✔
1161
  STrans *pTrans = NULL;
125,933,013✔
1162
  void   *pIter = NULL;
125,933,013✔
1163
  bool    conflict = false;
125,933,013✔
1164

1165
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
125,933,013!
1166

1167
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
91,384,052✔
1168
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
91,384,052!
1169

1170
  while (1) {
1171
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
109,889,478✔
1172
    if (pIter == NULL) break;
109,889,478✔
1173

1174
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
18,505,426✔
1175

1176
    if (pNew->conflict == TRN_CONFLICT_DB) {
18,505,426✔
1177
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
2,446,962!
1178
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
2,446,962✔
1179
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
896,265✔
1180
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
896,265✔
1181
      }
1182
    }
1183

1184
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
18,505,426✔
1185
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
15,957,927✔
1186
      if (pTrans->conflict == TRN_CONFLICT_DB) {
15,957,927✔
1187
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
159,585✔
1188
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
159,585✔
1189
      }
1190
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
15,957,927✔
1191
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
15,100,616✔
1192
      }
1193
    }
1194

1195
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
18,505,426✔
1196
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
77,665!
1197
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
77,665!
1198
        void *pGidIter = taosHashIterate(pNew->arbGroupIds, NULL);
×
1199
        while (pGidIter != NULL) {
×
1200
          int32_t groupId = *(int32_t *)pGidIter;
×
1201
          if (taosHashGet(pTrans->arbGroupIds, &groupId, sizeof(int32_t)) != NULL) {
×
1202
            taosHashCancelIterate(pNew->arbGroupIds, pGidIter);
×
1203
            mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1204
            break;
×
1205
          } else {
1206
            mndTransLogConflict(pNew, pTrans, false, &conflict);
×
1207
          }
1208
          pGidIter = taosHashIterate(pNew->arbGroupIds, pGidIter);
×
1209
        }
1210
      }
1211
    }
1212

1213
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
18,505,426!
1214
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
×
1215
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1216
      } else {
1217
        mndTransLogConflict(pNew, pTrans, false, &conflict);
×
1218
      }
1219
    }
1220

1221
    sdbRelease(pMnode->pSdb, pTrans);
18,505,426✔
1222
  }
1223

1224
  return conflict;
91,384,052!
1225
}
1226

1227
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
125,933,013✔
1228
  int32_t code = 0;
125,933,013✔
1229
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
125,933,013✔
1230
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
83,136,294!
1231
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
1232
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
1233
      TAOS_RETURN(code);
×
1234
    }
1235
  }
1236

1237
  if (mndCheckTransConflict(pMnode, pTrans)) {
125,933,013✔
1238
    code = TSDB_CODE_MND_TRANS_CONFLICT;
510,023✔
1239
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
510,023!
1240
    TAOS_RETURN(code);
510,023✔
1241
  }
1242

1243
  TAOS_RETURN(code);
125,422,990✔
1244
}
1245

1246
int32_t mndTransCheckConflictWithCompact(SMnode *pMnode, STrans *pTrans) {
358,696✔
1247
  int32_t      code = 0;
358,696✔
1248
  void        *pIter = NULL;
358,696✔
1249
  bool         conflict = false;
358,696✔
1250
  SCompactObj *pCompact = NULL;
358,696✔
1251

1252
  while (1) {
3,808✔
1253
    bool thisConflict = false;
362,504✔
1254
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
362,504✔
1255
    if (pIter == NULL) break;
362,504✔
1256

1257
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
3,808✔
1258
      thisConflict = true;
1,904✔
1259
    }
1260
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
3,808!
1261
      if (taosStrcasecmp(pTrans->dbname, pCompact->dbname) == 0) thisConflict = true;
1,904!
1262
    }
1263

1264
    if (thisConflict) {
3,808!
1265
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
3,808!
1266
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1267
      conflict = true;
3,808✔
1268
    } else {
1269
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with compact:%d db:%s", pTrans->id, pTrans->dbname,
×
1270
            pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1271
    }
1272
    sdbRelease(pMnode->pSdb, pCompact);
3,808✔
1273
  }
1274

1275
  if (conflict) {
358,696✔
1276
    code = TSDB_CODE_MND_TRANS_CONFLICT_COMPACT;
3,808✔
1277
    mError("trans:%d, failed to check tran conflict with compact since %s", pTrans->id, tstrerror(code));
3,808!
1278
    TAOS_RETURN(code);
3,808✔
1279
  }
1280

1281
  TAOS_RETURN(code);
354,888✔
1282
}
1283

1284
int32_t mndTransCheckConflictWithRetention(SMnode *pMnode, STrans *pTrans) {
354,888✔
1285
  int32_t        code = 0;
354,888✔
1286
  SSdb          *pSdb = pMnode->pSdb;
354,888✔
1287
  void          *pIter = NULL;
354,888✔
1288
  bool           conflict = false;
354,888✔
1289
  SRetentionObj *pRetention = NULL;
354,888✔
1290

1291
  while ((pIter = sdbFetch(pSdb, SDB_RETENTION, pIter, (void **)&pRetention)) != NULL) {
354,888!
1292
    conflict = false;
×
1293

1294
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
×
1295
      conflict = true;
×
1296
    }
1297
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
×
1298
      if (taosStrcasecmp(pTrans->dbname, pRetention->dbname) == 0) conflict = true;
×
1299
    }
1300

1301
    if (conflict) {
×
1302
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with retention:%d db:%s", pTrans->id,
×
1303
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pRetention->id, pRetention->dbname);
1304
      sdbRelease(pSdb, pRetention);
×
1305
      sdbCancelFetch(pSdb, pIter);
×
1306
      break;
×
1307
    } else {
1308
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with retention:%d db:%s", pTrans->id, pTrans->dbname,
×
1309
            pTrans->stbname, pTrans->conflict, pRetention->id, pRetention->dbname);
1310
    }
1311
    sdbRelease(pSdb, pRetention);
×
1312
  }
1313

1314
  if (conflict) {
354,888!
1315
    code = TSDB_CODE_MND_TRANS_CONFLICT_RETENTION;
×
1316
    mError("trans:%d, failed to check tran conflict with retention since %s", pTrans->id, tstrerror(code));
×
1317
    TAOS_RETURN(code);
×
1318
  }
1319

1320
  TAOS_RETURN(code);
354,888✔
1321
}
1322

1323
static bool mndTransActionsOfSameType(SArray *pActions) {
105,775,398✔
1324
  int32_t size = taosArrayGetSize(pActions);
105,775,398✔
1325
  ETrnAct lastActType = TRANS_ACTION_NULL;
105,775,398✔
1326
  bool    same = true;
105,775,398✔
1327
  for (int32_t i = 0; i < size; ++i) {
366,604,833✔
1328
    STransAction *pAction = taosArrayGet(pActions, i);
260,829,435✔
1329
    if (i > 0) {
260,829,435✔
1330
      if (lastActType != pAction->actionType) {
185,053,036!
1331
        same = false;
×
1332
        break;
×
1333
      }
1334
    }
1335
    lastActType = pAction->actionType;
260,829,435✔
1336
  }
1337
  return same;
105,775,398✔
1338
}
1339

1340
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
44,996,157✔
1341
  int32_t code = 0;
44,996,157✔
1342
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
44,996,157✔
1343
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
43,667,502!
1344
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1345
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1346
      TAOS_RETURN(code);
×
1347
    }
1348

1349
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
43,667,502✔
1350
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
17,111,739!
1351
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1352
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1353
        TAOS_RETURN(code);
×
1354
      }
1355
    }
1356
  }
1357

1358
  TAOS_RETURN(code);
44,996,157✔
1359
}
1360

1361
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
44,996,157✔
1362
  int32_t code = 0;
44,996,157✔
1363
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
44,996,157!
1364
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
×
1365
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
×
1366
    TAOS_RETURN(code);
×
1367
  }
1368
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
44,996,157!
1369
    code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1370
    mError("trans:%d, types of commit actions are not the same", pTrans->id);
×
1371
    TAOS_RETURN(code);
×
1372
  }
1373

1374
  TAOS_RETURN(code);
44,996,157✔
1375
}
1376

1377
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
45,005,677✔
1378
  int32_t code = 0;
45,005,677✔
1379
  if (pTrans == NULL) {
45,005,677!
1380
    return TSDB_CODE_INVALID_PARA;
×
1381
  }
1382

1383
  mInfo("trans:%d, action list:", pTrans->id);
45,005,677!
1384
  int32_t index = 0;
45,005,677✔
1385
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
69,689,760✔
1386
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
24,684,083✔
1387
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
24,684,083!
1388
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1389
  }
1390

1391
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
102,748,236✔
1392
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
57,742,559✔
1393
    if (pAction->actionType == TRANS_ACTION_MSG) {
57,742,559✔
1394
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
55,493,581!
1395
            TMSG_INFO(pAction->msgType));
1396
      ;
1397
    } else {
1398
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
2,248,978!
1399
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1400
    }
1401
  }
1402

1403
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
240,408,307✔
1404
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
195,402,630✔
1405
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
195,402,630!
1406
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1407
  }
1408

1409
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
82,783,515✔
1410
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
37,777,838✔
1411
    if (pAction->actionType == TRANS_ACTION_MSG) {
37,777,838✔
1412
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
25,492,429!
1413
            TMSG_INFO(pAction->msgType));
1414
      ;
1415
    } else {
1416
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
12,285,409!
1417
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1418
    }
1419
  }
1420

1421
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
45,005,677✔
1422

1423
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
44,996,157!
1424

1425
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
44,996,157!
1426

1427
  mInfo("trans:%d, prepare transaction", pTrans->id);
44,996,157!
1428
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
44,996,157✔
1429
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
1,772!
1430
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
1,772✔
1431
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
1,772✔
1432
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
1,772✔
1433
    TAOS_RETURN(code);
1,772✔
1434
  }
1435
  mInfo("trans:%d, prepare finished", pTrans->id);
44,994,385!
1436

1437
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
44,994,385✔
1438
  if (pNew == NULL) {
44,994,385!
1439
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1440
    if (terrno != 0) code = terrno;
×
1441
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, tstrerror(code));
×
1442
    TAOS_RETURN(code);
×
1443
  }
1444

1445
  pNew->pRpcArray = pTrans->pRpcArray;
44,994,385✔
1446
  pNew->rpcRsp = pTrans->rpcRsp;
44,994,385✔
1447
  pNew->rpcRspLen = pTrans->rpcRspLen;
44,994,385✔
1448
  pNew->mTraceId = pTrans->mTraceId;
44,994,385✔
1449
  pTrans->pRpcArray = NULL;
44,994,385✔
1450
  pTrans->rpcRsp = NULL;
44,994,385✔
1451
  pTrans->rpcRspLen = 0;
44,994,385✔
1452

1453
  mInfo("trans:%d, execute transaction in prepare", pTrans->id);
44,994,385!
1454
  mndTransExecute(pMnode, pNew, false);
44,994,385✔
1455
  mndReleaseTrans(pMnode, pNew);
44,994,385✔
1456
  // TDOD change to TAOS_RETURN(code);
1457
  return 0;
44,994,385✔
1458
}
1459

1460
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
44,944,529✔
1461
  int32_t code = 0;
44,944,529✔
1462
  mInfo("trans:%d, commit transaction", pTrans->id);
44,944,529!
1463
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
44,944,529✔
1464
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
14,494!
1465
    TAOS_RETURN(code);
14,494✔
1466
  }
1467
  mInfo("trans:%d, commit finished", pTrans->id);
44,930,035!
1468
  TAOS_RETURN(code);
44,930,035✔
1469
}
1470

1471
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
3,252✔
1472
  int32_t code = 0;
3,252✔
1473
  mInfo("trans:%d, rollback transaction", pTrans->id);
3,252!
1474
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
3,252!
1475
    mError("trans:%d, failed to rollback since %s", pTrans->id, tstrerror(code));
×
1476
    TAOS_RETURN(code);
×
1477
  }
1478
  mInfo("trans:%d, rollback finished", pTrans->id);
3,252!
1479
  TAOS_RETURN(code);
3,252✔
1480
}
1481

1482
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
3,252✔
1483
  int32_t code = 0;
3,252✔
1484
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
3,252!
1485
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
3,252!
1486
    mError("trans:%d, failed to pre-finish since %s", pTrans->id, tstrerror(code));
×
1487
    TAOS_RETURN(code);
×
1488
  }
1489
  mInfo("trans:%d, pre-finish finished", pTrans->id);
3,252!
1490
  TAOS_RETURN(code);
3,252✔
1491
}
1492

1493
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
226,063,661✔
1494
  bool    sendRsp = false;
226,063,661✔
1495
  int32_t code = pTrans->code;
226,063,661✔
1496

1497
  if (pTrans->stage == TRN_STAGE_FINISH) {
226,063,661✔
1498
    sendRsp = true;
98,435,102✔
1499
  }
1500

1501
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
226,063,661✔
1502
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
77,975,662!
1503
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
19,512!
1504
      sendRsp = true;
19,512✔
1505
    }
1506
  } else {
1507
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
148,087,999✔
1508
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
88,436,220!
1509
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1510
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1511
      } else {
1512
        if (pTrans->failedTimes > 6) sendRsp = true;
88,436,220!
1513
      }
1514
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
88,436,220✔
1515
    }
1516
  }
1517

1518
  if (!sendRsp) {
226,063,661✔
1519
    return;
127,609,047✔
1520
  } else {
1521
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
98,454,614!
1522
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1523
  }
1524

1525
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
98,454,614!
1526
  taosWLockLatch(&pTrans->lockRpcArray);
98,454,614✔
1527
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
98,454,614✔
1528
  if (size <= 0) {
98,454,614✔
1529
    taosWUnLockLatch(&pTrans->lockRpcArray);
66,546,043✔
1530
    return;
66,546,043✔
1531
  }
1532

1533
  for (int32_t i = 0; i < size; ++i) {
63,817,142✔
1534
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
31,908,571✔
1535
    if (pInfo->handle != NULL) {
31,908,571✔
1536
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
30,877,919!
1537
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1538
      }
1539
      if (code == TSDB_CODE_SYN_TIMEOUT) {
30,877,919!
1540
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1541
      }
1542

1543
      if (i != 0 && code == 0) {
30,877,919!
1544
        code = TSDB_CODE_MNODE_NOT_FOUND;
×
1545
      }
1546
      mInfo("vgId:1, trans:%d, client:%d start to send rsp, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,877,919!
1547
            mndTransStr(pTrans->stage), pInfo->ahandle);
1548

1549
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
30,877,919✔
1550

1551
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
30,877,919✔
1552
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
3,758,718!
1553
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
3,758,718✔
1554
        if (pDb != NULL) {
3,758,718!
1555
          for (int32_t j = 0; j < 12; j++) {
4,687,952✔
1556
            bool ready = mndIsDbReady(pMnode, pDb);
4,684,181✔
1557
            if (!ready) {
4,684,181✔
1558
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
929,234!
1559
              taosMsleep(1000);
929,234✔
1560
            } else {
1561
              break;
3,754,947✔
1562
            }
1563
          }
1564
        }
1565
        mndReleaseDb(pMnode, pDb);
3,758,718✔
1566
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
27,119,201✔
1567
        void   *pCont = NULL;
6,666,388✔
1568
        int32_t contLen = 0;
6,666,388✔
1569
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
6,666,388✔
1570
          mndTransSetRpcRsp(pTrans, pCont, contLen);
6,663,136✔
1571
        }
1572
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
20,452,813✔
1573
        void   *pCont = NULL;
12,906✔
1574
        int32_t contLen = 0;
12,906✔
1575
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
12,906!
1576
          mndTransSetRpcRsp(pTrans, pCont, contLen);
12,906✔
1577
        }
1578
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
20,439,907✔
1579
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
31,542✔
1580
        if (code != 0) {
31,542!
1581
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1582
        }
1583
      }
1584

1585
      if (pTrans->rpcRspLen != 0) {
30,877,919✔
1586
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
17,746,312✔
1587
        if (rpcCont != NULL) {
17,746,312!
1588
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
17,746,312!
1589
          rspMsg.pCont = rpcCont;
17,746,312✔
1590
          rspMsg.contLen = pTrans->rpcRspLen;
17,746,312✔
1591
        }
1592
      }
1593

1594
      tmsgSendRsp(&rspMsg);
30,877,919✔
1595

1596
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,877,919!
1597
            mndTransStr(pTrans->stage), pInfo->ahandle);
1598
    }
1599
  }
1600
  taosArrayClear(pTrans->pRpcArray);
31,908,571✔
1601
  taosWUnLockLatch(&pTrans->lockRpcArray);
31,908,571✔
1602
}
1603

1604
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
60,531,391✔
1605
  int32_t code = 0;
60,531,391✔
1606
  SMnode *pMnode = pRsp->info.node;
60,531,391✔
1607
#ifndef TD_ASTRA_32
1608
  int64_t signature = (int64_t)(pRsp->info.ahandle);
60,531,391✔
1609
  int32_t transId = (int32_t)(signature >> 32);
60,531,391✔
1610
  int32_t action = (int32_t)((signature << 32) >> 32);
60,531,391✔
1611
#else
1612
  int32_t transId = (int32_t)(pRsp->info.ahandle);
1613
  int32_t action = (int32_t)(pRsp->info.ahandleEx);
1614
#endif
1615
  STraceId* trace = &(pRsp->info.traceId);
60,531,391✔
1616
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
60,531,391✔
1617
  if (pTrans == NULL) {
60,531,391!
1618
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1619
    if (terrno != 0) code = terrno;
×
1620
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1621
    goto _OVER;
×
1622
  }
1623

1624
  SArray *pArray = NULL;
60,531,391✔
1625
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
60,531,391✔
1626
    pArray = pTrans->redoActions;
60,518,383✔
1627
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
13,008!
1628
    pArray = pTrans->undoActions;
13,008✔
1629
  } else {
1630
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1631
    goto _OVER;
×
1632
  }
1633

1634
  if (pArray == NULL) {
60,531,391!
1635
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1636
    goto _OVER;
×
1637
  }
1638

1639
  int32_t actionNum = taosArrayGetSize(pArray);
60,531,391✔
1640
  if (action < 0 || action >= actionNum) {
60,531,391!
1641
    mError("trans:%d, invalid action:%d", transId, action);
×
1642
    goto _OVER;
×
1643
  }
1644

1645
  STransAction *pAction = taosArrayGet(pArray, action);
60,531,391✔
1646
  if (pAction != NULL) {
60,531,391!
1647
    if (pAction->msgSent) {
60,531,391!
1648
      pAction->msgReceived = 1;
60,531,391✔
1649
      pAction->errCode = pRsp->code;
60,531,391✔
1650
      pAction->endTime = taosGetTimestampMs();
60,531,391✔
1651

1652
      // pTrans->lastErrorNo = pRsp->code;
1653
      mndSetTransLastAction(pTrans, pAction);
60,531,391✔
1654

1655
      mInfo("trans:%d, %s:%d response is received, msgType:%s, QID:0x%" PRIx64 ":0x%" PRIx64 ", received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
60,531,391!
1656
            mndTransStr(pAction->stage), action, TMSG_INFO(pAction->msgType), trace ? trace->rootId : 0, 
1657
              trace ? trace->msgId : 0, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1658
            tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1659
    } else {
1660
      mWarn("trans:%d, %s:%d response is received, but msgSent is false, code:0x%x(%s), accept:0x%x(%s) retry:0x%x", 
×
1661
            transId, mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code),
1662
            pAction->acceptableCode, tstrerror(pAction->acceptableCode), pAction->retryCode);
1663
    }
1664

1665
  } else {
1666
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1667
  }
1668

1669
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
60,531,391!
1670
  mndTransExecute(pMnode, pTrans, true);
60,531,391✔
1671

1672
_OVER:
60,531,391✔
1673
  mndReleaseTrans(pMnode, pTrans);
60,531,391✔
1674
  TAOS_RETURN(code);
60,531,391✔
1675
}
1676

1677
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
5,331,093✔
1678
  pAction->rawWritten = 0;
5,331,093✔
1679
  pAction->msgSent = 0;
5,331,093✔
1680
  pAction->msgReceived = 0;
5,331,093✔
1681
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
5,331,093!
1682
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
5,331,093!
1683
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
×
1684
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
×
1685
          pAction->id, pAction->epSet.inUse);
1686
  } else {
1687
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
5,331,093!
1688
  }
1689
  //  pAction->errCode = 0;
1690
}
5,331,093✔
1691

1692
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
3,252✔
1693
  int32_t numOfActions = taosArrayGetSize(pArray);
3,252✔
1694

1695
  for (int32_t action = 0; action < numOfActions; ++action) {
16,260✔
1696
    STransAction *pAction = taosArrayGet(pArray, action);
13,008✔
1697
    if (pAction->msgSent && pAction->msgReceived &&
13,008!
1698
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
13,008!
1699
      continue;
9,756✔
1700
    if (pAction->msgSent && !pAction->msgReceived) {
3,252!
1701
      int64_t timestamp = taosGetTimestampMs();
×
1702
      if (timestamp - pAction->startTime <= TRANS_ACTION_TIMEOUT) continue;
×
1703
    }
1704

1705
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
3,252!
1706

1707
    mndTransResetAction(pMnode, pTrans, pAction);
3,252✔
1708
    mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage), pAction->id,
3,252!
1709
          pAction->errCode, pAction->startTime);
1710
  }
1711
}
3,252✔
1712

1713
// execute in sync context
1714
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
446,116,314✔
1715
  if (pAction->rawWritten) return 0;
446,116,314✔
1716
  if (topHalf) {
248,694,118✔
1717
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
3,586✔
1718
  }
1719

1720
  if (pAction->pRaw->type >= SDB_MAX) {
248,690,532!
1721
    pAction->rawWritten = true;
×
1722
    pAction->errCode = 0;
×
1723
    mndSetTransLastAction(pTrans, pAction);
×
1724
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
1725
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1726
  }
1727

1728
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
248,690,532✔
1729
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
248,690,532!
1730
    pAction->rawWritten = true;
248,690,532✔
1731
    pAction->errCode = 0;
248,690,532✔
1732
    code = 0;
248,690,532✔
1733
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
248,690,532!
1734
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1735

1736
    mndSetTransLastAction(pTrans, pAction);
248,690,532✔
1737
  } else {
1738
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1739
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1740
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1741
    mndSetTransLastAction(pTrans, pAction);
×
1742
  }
1743

1744
  TAOS_RETURN(code);
248,690,532✔
1745
}
1746

1747
// execute in trans context
1748
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
285,152,398✔
1749
                                     bool notSend) {
1750
  if (pAction->msgSent) return 0;
285,152,398✔
1751
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
100,170,675✔
1752
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
39,098,590✔
1753
  }
1754

1755
  if (notSend) {
61,072,085✔
1756
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
17,886!
1757
    return 0;
17,886✔
1758
  }
1759

1760
#ifndef TD_ASTRA_32
1761
  int64_t signature = pTrans->id;
61,054,199✔
1762
  signature = (signature << 32);
61,054,199✔
1763
  signature += pAction->id;
61,054,199✔
1764

1765
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
61,054,199✔
1766
#else
1767
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1768
                    .contLen = pAction->contLen,
1769
                    .info.ahandle = (void *)pTrans->id,
1770
                    .info.ahandleEx = (void *)pAction->id};
1771
#endif
1772
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
61,054,199✔
1773
  if (rpcMsg.pCont == NULL) {
61,054,199!
1774
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1775
    return -1;
×
1776
  }
1777
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
61,054,199✔
1778
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
61,054,199✔
1779
  rpcMsg.info.notFreeAhandle = 1;
61,054,199✔
1780
  STraceId* trace = &(rpcMsg.info.traceId);
61,054,199✔
1781

1782
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
61,054,199!
1783

1784
  char    detail[1024] = {0};
61,054,199✔
1785
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
61,054,199!
1786
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
61,054,199✔
1787
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
131,050,213✔
1788
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
69,996,014✔
1789
                     pAction->epSet.eps[i].port);
69,996,014✔
1790
  }
1791

1792
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
61,054,199✔
1793
  if (code == 0) {
61,054,199!
1794
    pAction->msgSent = 1;
61,054,199✔
1795
    // pAction->msgReceived = 0;
1796
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
61,054,199✔
1797
    pAction->startTime = taosGetTimestampMs();
61,054,199✔
1798
    pAction->endTime = 0;
61,054,199✔
1799
    mInfo("trans:%d, %s:%d is sent, QID:0x%" PRIx64 ":0x%" PRIx64 ", %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, trace ? trace->rootId : 0, 
61,054,199!
1800
              trace ? trace->msgId : 0, detail);
1801

1802
    mndSetTransLastAction(pTrans, pAction);
61,054,199✔
1803
  } else {
1804
    pAction->msgSent = 0;
×
1805
    pAction->msgReceived = 0;
×
1806
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1807
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
×
1808
           detail);
1809

1810
    mndSetTransLastAction(pTrans, pAction);
×
1811
  }
1812

1813
  TAOS_RETURN(code);
61,054,199✔
1814
}
1815

1816
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1817
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1818
  pAction->rawWritten = 0;
×
1819
  pAction->errCode = 0;
×
1820
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1821

1822
  mndSetTransLastAction(pTrans, pAction);
×
1823
  return 0;
×
1824
}
1825

1826
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
731,268,712✔
1827
                                        bool notSend) {
1828
  if (pAction->actionType == TRANS_ACTION_RAW) {
731,268,712✔
1829
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
446,116,314✔
1830
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
285,152,398!
1831
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
285,152,398✔
1832
  } else {
1833
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1834
  }
1835
}
1836

1837
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
204,410,720✔
1838
  int32_t numOfActions = taosArrayGetSize(pArray);
204,410,720✔
1839
  int32_t code = 0;
204,410,720✔
1840

1841
  for (int32_t action = 0; action < numOfActions; ++action) {
835,953,306✔
1842
    STransAction *pAction = taosArrayGet(pArray, action);
661,902,099✔
1843
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
661,902,099✔
1844
    if (code != 0) {
661,902,099✔
1845
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
30,359,513!
1846
            numOfActions);
1847
      break;
30,359,513✔
1848
    }
1849
  }
1850

1851
  return code;
204,410,720✔
1852
}
1853

1854
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
245,852,752✔
1855
  int32_t numOfActions = taosArrayGetSize(pArray);
245,852,752✔
1856
  int32_t code = 0;
245,852,752✔
1857
  if (numOfActions == 0) return 0;
245,852,752✔
1858

1859
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
204,410,720✔
1860
    return code;
30,359,513✔
1861
  }
1862

1863
  int32_t       numOfExecuted = 0;
174,051,207✔
1864
  int32_t       errCode = 0;
174,051,207✔
1865
  STransAction *pErrAction = NULL;
174,051,207✔
1866
  for (int32_t action = 0; action < numOfActions; ++action) {
805,593,793✔
1867
    STransAction *pAction = taosArrayGet(pArray, action);
631,542,586✔
1868
    if (pAction->msgReceived || pAction->rawWritten) {
631,542,586✔
1869
      numOfExecuted++;
520,094,811✔
1870
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
520,094,811✔
1871
        errCode = pAction->errCode;
4,878✔
1872
        pErrAction = pAction;
4,878✔
1873
      }
1874
    } else {
1875
      pErrAction = pAction;
111,447,775✔
1876
    }
1877
  }
1878

1879
  mndSetTransLastAction(pTrans, pErrAction);
174,051,207✔
1880

1881
  if (numOfExecuted == numOfActions) {
174,051,207✔
1882
    if (errCode == 0) {
122,554,192✔
1883
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
122,550,940!
1884
      return 0;
122,550,940✔
1885
    } else {
1886
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
3,252!
1887
      mndTransResetActions(pMnode, pTrans, pArray);
3,252✔
1888
      terrno = errCode;
3,252✔
1889
      return errCode;
3,252✔
1890
    }
1891
  } else {
1892
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
51,497,015!
1893

1894
    for (int32_t action = 0; action < numOfActions; ++action) {
221,594,953✔
1895
      STransAction *pAction = taosArrayGet(pArray, action);
170,097,938✔
1896
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
170,097,938✔
1897
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1898
             pAction->acceptableCode, pAction->retryCode);
1899
      if (pAction->msgSent) {
170,097,938✔
1900
        bool reset = false;
170,080,052✔
1901
        if (pAction->msgReceived) {
170,080,052✔
1902
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
58,650,163✔
1903
        } else {
1904
          int64_t timestamp = taosGetTimestampMs();
111,429,889✔
1905
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
111,429,889!
1906
        }
1907
        if (reset) {
170,080,052✔
1908
          mndTransResetAction(pMnode, pTrans, pAction);
1,626✔
1909
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1,626!
1910
                pAction->id, pAction->errCode, pAction->startTime);
1911
        }
1912
      }
1913
    }
1914
    return TSDB_CODE_ACTION_IN_PROGRESS;
51,497,015✔
1915
  }
1916
}
1917

1918
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
147,403,341✔
1919
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
147,403,341✔
1920
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
147,403,341✔
1921
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
3,252!
1922
           mndStrExecutionContext(topHalf));
1923
  }
1924
  return code;
147,403,341✔
1925
}
1926

1927
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
22,764✔
1928
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf, notSend);
22,764✔
1929
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
22,764!
1930
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1931
           mndStrExecutionContext(topHalf));
1932
  }
1933
  return code;
22,764✔
1934
}
1935

1936
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
98,426,647✔
1937
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
98,426,647✔
1938
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
98,426,647!
1939
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1940
           mndStrExecutionContext(topHalf));
1941
  }
1942
  return code;
98,426,647✔
1943
}
1944

1945
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
21,295,573✔
1946
  int32_t code = 0;
21,295,573✔
1947
  int32_t numOfActions = taosArrayGetSize(pActions);
21,295,573✔
1948
  if (numOfActions == 0) return code;
21,295,573✔
1949

1950
  if (pTrans->actionPos >= numOfActions) {
21,289,507✔
1951
    return code;
1,351,613✔
1952
  }
1953

1954
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
19,937,894!
1955
        pTrans->actionPos, mndTransStr(pTrans->stage));
1956

1957
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
31,322,968✔
1958
    STransAction *pAction = taosArrayGet(pActions, action);
30,223,432✔
1959

1960
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
30,223,432✔
1961
      code = TSDB_CODE_ACTION_IN_PROGRESS;
21,534✔
1962
      break;
22,263✔
1963
    }
1964

1965
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
30,201,898!
1966
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
1967

1968
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
30,201,898✔
1969
    if (code == 0) {
30,201,898✔
1970
      if (pAction->msgSent) {
25,406,441✔
1971
        bool reset = false;
21,152,347✔
1972
        if (pAction->msgReceived) {
21,152,347✔
1973
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
9,393,646!
1974
            code = pAction->errCode;
5,184,430✔
1975
            reset = true;
5,184,430✔
1976
          } else {
1977
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,209,216!
1978
          }
1979
        } else {
1980
          int64_t timestamp = taosGetTimestampMs();
11,758,701✔
1981
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
11,758,701!
1982
          code = TSDB_CODE_ACTION_IN_PROGRESS;
11,758,701✔
1983
        }
1984
        if (reset) {
21,152,347✔
1985
          mndTransResetAction(pMnode, pTrans, pAction);
5,184,430✔
1986
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
5,184,430!
1987
                pAction->id, pAction->errCode, pAction->startTime);
1988
        }
1989
      } else if (pAction->rawWritten) {
4,254,094!
1990
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
4,254,094!
1991
          code = pAction->errCode;
×
1992
        } else {
1993
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,254,094!
1994
        }
1995
      } else {
1996
      }
1997
    }
1998

1999
    if (code == 0) {
30,201,898✔
2000
      pTrans->failedTimes = 0;
8,463,310✔
2001
    }
2002
    mndSetTransLastAction(pTrans, pAction);
30,201,898✔
2003
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
30,201,898✔
2004
      mInfo(
16,554,158!
2005
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2006
          "msgReceived:%d",
2007
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2008
          pAction->msgSent, pAction->msgReceived);
2009
    } else if (code != 0) {
13,647,740✔
2010
      mError(
5,184,430!
2011
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
2012
          "msgReceived:%d",
2013
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
2014
          pAction->msgSent, pAction->msgReceived);
2015
    }
2016

2017
    char str[200] = {0};
30,201,898✔
2018
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
30,201,898✔
2019
      pTrans->lastErrorNo = code;
7,058,123✔
2020
      pTrans->code = code;
7,058,123✔
2021
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
7,058,123!
2022
            action, str);
2023
      break;
7,058,123✔
2024
    }
2025

2026
    if (code == 0) {
23,143,775✔
2027
      pTrans->code = 0;
6,200,644✔
2028
      pTrans->actionPos++;
6,200,644✔
2029
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
6,200,644!
2030
            pAction->id);
2031
      (void)taosThreadMutexUnlock(&pTrans->mutex);
6,200,644✔
2032
      code = mndTransSync(pMnode, pTrans);
6,200,644✔
2033
      (void)taosThreadMutexLock(&pTrans->mutex);
6,200,644✔
2034
      if (code != 0) {
6,200,644!
2035
        pTrans->actionPos--;
×
2036
        pTrans->code = terrno;
×
2037
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2038
               mndTransStr(pAction->stage), pAction->id, terrstr());
2039
        break;
×
2040
      }
2041
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
16,943,131✔
2042
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
11,758,701!
2043
      break;
11,758,701✔
2044
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
5,184,430!
2045
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
×
2046
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
5,184,430!
2047
            code, tstrerror(code));
2048
      pTrans->lastErrorNo = code;
5,184,430✔
2049
      taosMsleep(300);
5,184,430✔
2050
      action--;
5,184,430✔
2051
      continue;
5,184,430✔
2052
    } else {
2053
      terrno = code;
×
2054
      pTrans->lastErrorNo = code;
×
2055
      pTrans->code = code;
×
2056
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2057
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2058
      break;
×
2059
    }
2060
  }
2061

2062
  return code;
19,937,894✔
2063
}
2064

2065
static int32_t mndTransExecuteActionsSerialGroup(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf,
12,078,230✔
2066
                                                 int32_t groupId, int32_t currentGroup, int32_t groupCount,
2067
                                                 bool notSend, SHashObj *pHash) {
2068
  int32_t code = 0;
12,078,230✔
2069
  int32_t numOfActions = taosArrayGetSize(pActions);
12,078,230✔
2070
  if (numOfActions == 0) return code;
12,078,230!
2071

2072
  if (groupId <= 0) {
12,078,230!
2073
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
2074
    return TSDB_CODE_INTERNAL_ERROR;
×
2075
  }
2076

2077
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &groupId, sizeof(int32_t));
12,078,230✔
2078
  if (actionPos == NULL) {
12,078,230!
2079
    mError("trans:%d, failed to execute action in serail group, actionPos is null at group %d", pTrans->id, groupId);
×
2080
    return TSDB_CODE_INTERNAL_ERROR;
×
2081
  }
2082

2083
  if (*actionPos >= numOfActions) {
12,078,230✔
2084
    mError("trans:%d, failed to execute action in serail group, actionPos:%d >= numOfActions:%d at group %d",
1,736,998!
2085
           pTrans->id, *actionPos, numOfActions, groupId);
2086
    return TSDB_CODE_INTERNAL_ERROR;
1,736,998✔
2087
  }
2088

2089
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
11,862,166✔
2090
    STransAction **ppAction = taosArrayGet(pActions, action);
11,280,566✔
2091
    STransAction  *pAction = *ppAction;
11,280,566✔
2092

2093
    if (notSend && !pAction->msgSent) {
11,280,566✔
2094
      mInfo(
1,723,688!
2095
          "trans:%d, %s:%d (%d/%d at group %d) skip to execute, curent state(actionType(1:msg,2:log):%d, "
2096
          "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2097
          pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2098
          pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2099
      code = TSDB_CODE_ACTION_IN_PROGRESS;
1,723,688✔
2100
      break;
1,724,156✔
2101
    }
2102

2103
    mInfo(
9,556,878!
2104
        "trans:%d, %s:%d (%d/%d at group %d) begin to execute, curent state(actionType(1:msg,2:log):%d, "
2105
        "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2106
        pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2107
        pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2108

2109
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
9,556,878✔
2110
    if (code == 0) {
9,556,878✔
2111
      if (pAction->msgSent) {
5,609,672✔
2112
        if (pAction->msgReceived) {
5,015,431✔
2113
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,348,948!
2114
            code = pAction->errCode;
141,785✔
2115
            mndTransResetAction(pMnode, pTrans, pAction);
141,785✔
2116
          } else {
2117
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
1,207,163!
2118
                  pAction->id, action, numOfActions, groupId);
2119
          }
2120
        } else {
2121
          code = TSDB_CODE_ACTION_IN_PROGRESS;
3,666,483✔
2122
        }
2123
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
5,015,431✔
2124
        if (msgSent != NULL) {
5,015,431!
2125
          *msgSent = pAction->msgSent;
5,015,431✔
2126
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
5,015,431!
2127
        } else {
2128
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2129
        }
2130
      } else if (pAction->rawWritten) {
594,241!
2131
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
594,241!
2132
          code = pAction->errCode;
×
2133
        } else {
2134
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
594,241!
2135
        }
2136
      } else {
2137
      }
2138
    }
2139

2140
    if (code == 0) {
9,556,878✔
2141
      pTrans->failedTimes = 0;
1,801,404✔
2142
    }
2143
    mndSetTransLastAction(pTrans, pAction);
9,556,878✔
2144

2145
    char str[200] = {0};
9,556,878✔
2146
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
9,556,878✔
2147
      pTrans->lastErrorNo = code;
4,367,619✔
2148
      pTrans->code = code;
4,367,619✔
2149
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
4,367,619✔
2150
        mInfo(
3,947,206!
2151
            "trans:%d, %s:%d (%d/%d at group %d) not able to execute since %s, current state("
2152
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2153
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2154
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2155
      } else if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
420,413!
2156
        mError(
×
2157
            "trans:%d, %s:%d (%d/%d at group %d) failed to execute since %s, current action("
2158
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2159
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2160
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2161
      }
2162
      mInfo("trans:%d, %s:%d (%d/%d at group %d) cannot execute next action, stop this group execution, %s", pTrans->id,
4,367,619!
2163
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2164
      break;
4,367,619✔
2165
    }
2166

2167
    if (code == 0) {
5,189,259✔
2168
      pTrans->code = 0;
1,380,991✔
2169
      pTrans->actionPos++;
1,380,991✔
2170
      (*actionPos)++;
1,380,991✔
2171
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
1,380,991!
2172
            pAction->id);
2173
      (void)taosThreadMutexUnlock(&pTrans->mutex);
1,380,991✔
2174
      code = mndTransSync(pMnode, pTrans);
1,380,991✔
2175
      (void)taosThreadMutexLock(&pTrans->mutex);
1,380,991✔
2176
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
1,380,991!
2177
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
38,004,754✔
2178
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
36,623,763✔
2179
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
36,623,763✔
2180
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
36,623,763✔
2181
          pTmpAction->msgSent = *msgSent;
1,894,865✔
2182
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
1,894,865!
2183
        }
2184
      }
2185
      if (code != 0) {
1,380,991✔
2186
        pTrans->actionPos--;
1,842✔
2187
        (*actionPos)--;
1,842✔
2188
        pTrans->code = terrno;
1,842✔
2189
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
1,842!
2190
               mndTransStr(pAction->stage), pAction->id, terrstr());
2191
        break;
1,842✔
2192
      }
2193
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
3,808,268✔
2194
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
3,666,483!
2195
            mndTransStr(pAction->stage), pAction->id);
2196
      break;
3,666,483✔
2197
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
141,785!
2198
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
×
2199
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
141,785!
2200
            code, tstrerror(code));
2201
      pTrans->lastErrorNo = code;
141,785✔
2202
      taosMsleep(300);
141,785✔
2203
      action--;
141,785✔
2204
      continue;
141,785✔
2205
    } else {
2206
      terrno = code;
×
2207
      pTrans->lastErrorNo = code;
×
2208
      pTrans->code = code;
×
2209
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2210
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2211
      break;
×
2212
    }
2213
  }
2214

2215
  return code;
10,341,232✔
2216
}
2217

2218
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
21,147,471✔
2219
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
21,147,471✔
2220
  (void)taosThreadMutexLock(&pTrans->mutex);
21,147,471✔
2221
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
21,147,471!
2222
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
21,147,471✔
2223
  }
2224
  (void)taosThreadMutexUnlock(&pTrans->mutex);
21,147,471✔
2225
  return code;
21,147,471✔
2226
}
2227

2228
static int32_t mndTransExecuteRedoActionGroup(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
3,396,579✔
2229
  int32_t total_code = TSDB_CODE_ACTION_IN_PROGRESS;
3,396,579✔
2230
  int32_t code = 0;
3,396,579✔
2231
  int32_t successCount = 0;
3,396,579✔
2232
  int32_t groupCount = taosHashGetSize(pTrans->redoGroupActions);
3,396,579✔
2233

2234
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
3,396,579✔
2235
  if(pHash == NULL){
3,396,579!
2236
    mError("trans:%d, failed to init hash since %s", pTrans->id, terrstr());
×
2237
    return -1;
×
2238
  }
2239
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
89,749,631✔
2240
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
86,353,052✔
2241
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
86,353,052✔
2242
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
86,353,052!
2243
  }
2244
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
3,396,579!
2245

2246
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
3,396,579!
2247
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
3,396,579✔
2248
  int32_t currentGroup = 1;
3,396,579✔
2249
  while (pIter) {
15,474,809✔
2250
    SArray **redoActions = pIter;
12,078,230✔
2251
    size_t   keyLen = 0;
12,078,230✔
2252
    int32_t *key = taosHashGetKey(pIter, &keyLen);
12,078,230✔
2253
    int32_t  actionCount = taosArrayGetSize(*redoActions);
12,078,230✔
2254
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
12,078,230!
2255
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2256
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
12,078,230✔
2257
                                             notSend, pHash);
2258
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
12,078,230✔
2259
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
3,947,206!
2260
            tstrerror(code));
2261
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
8,131,024✔
2262
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
5,390,171!
2263
    } else if (code != 0) {
2,740,853✔
2264
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,738,840!
2265
             tstrerror(code));
2266
    } else {
2267
      successCount++;
1,002,013✔
2268
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
1,002,013!
2269
    }
2270
    currentGroup++;
12,078,230✔
2271
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
12,078,230✔
2272
  }
2273

2274
  taosHashCleanup(pHash);
3,396,579✔
2275

2276
  if (successCount == groupCount) {
3,396,579✔
2277
    total_code = 0;
62,165✔
2278
  } else {
2279
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
3,334,414!
2280
  }
2281

2282
  return total_code;
3,396,579✔
2283
}
2284

2285
static int32_t mndTransExecuteRedoActionsParallel(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
3,993,473✔
2286
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
3,993,473✔
2287
  (void)taosThreadMutexLock(&pTrans->mutex);
3,993,473✔
2288

2289
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
3,993,473!
2290
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
3,993,473✔
2291
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
3,993,473!
2292
      code = 0;
448,792✔
2293
    } else {
2294
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
3,544,681✔
2295
      if (pAction != NULL && pAction->groupId == -1) {
3,544,681!
2296
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
148,102✔
2297
      } else {
2298
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
3,396,579✔
2299
        if (code == 0) {
3,396,579✔
2300
          if (pTrans->actionPos < numOfActions) {
62,165✔
2301
            code = TSDB_CODE_ACTION_IN_PROGRESS;
48,044✔
2302
          }
2303
        }
2304
      }
2305
    }
2306
  }
2307

2308
  (void)taosThreadMutexUnlock(&pTrans->mutex);
3,993,473✔
2309

2310
  return code;
3,993,473✔
2311
}
2312

2313
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
2314
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
2315
  (void)taosThreadMutexLock(&pTrans->mutex);
×
2316
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2317
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
2318
  }
2319
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
2320
  return code;
×
2321
}
2322

2323
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
53,873,165✔
2324
  bool    continueExec = true;
53,873,165✔
2325
  int32_t code = 0;
53,873,165✔
2326
  terrno = 0;
53,873,165✔
2327

2328
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
53,873,165✔
2329
  if (numOfActions == 0) goto _OVER;
53,873,165✔
2330

2331
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
20,813,741!
2332

2333
  for (int32_t action = 0; action < numOfActions; ++action) {
50,421,578✔
2334
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
29,607,837✔
2335
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
29,607,837✔
2336
    if (code != 0) {
29,607,837!
2337
      terrno = code;
×
2338
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
2339
             numOfActions, tstrerror(code));
2340
      return false;
×
2341
    }
2342
  }
2343

2344
_OVER:
20,813,741✔
2345
  pTrans->stage = TRN_STAGE_REDO_ACTION;
53,873,165✔
2346
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
53,873,165!
2347
  return continueExec;
53,873,165✔
2348
}
2349

2350
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
172,544,285✔
2351
  bool    continueExec = true;
172,544,285✔
2352
  int32_t code = 0;
172,544,285✔
2353
  terrno = 0;
172,544,285✔
2354

2355
  if (pTrans->exec == TRN_EXEC_SERIAL) {
172,544,285✔
2356
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
21,147,471✔
2357
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
151,396,814✔
2358
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
147,403,341✔
2359
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
3,993,473!
2360
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
3,993,473✔
2361
  } else {
2362
    code = TSDB_CODE_INTERNAL_ERROR;
×
2363
  }
2364

2365
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
172,547,537!
2366
      mndTransIsInSyncContext(topHalf)) {
3,252✔
2367
    pTrans->lastErrorNo = code;
×
2368
    pTrans->code = code;
×
2369
    mInfo(
×
2370
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2371
        "continueExec:%d, code:%s",
2372
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2373
    taosMsleep(100);
×
2374
    return true;
×
2375
  } else {
2376
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
172,544,285✔
2377
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
62,373,874!
2378
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2379
      return false;
62,373,874✔
2380
    }
2381
  }
2382
  terrno = code;
110,170,411✔
2383

2384
  if (code == 0) {
110,170,411✔
2385
    pTrans->code = 0;
44,944,529✔
2386
    pTrans->stage = TRN_STAGE_COMMIT;
44,944,529✔
2387
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
44,944,529!
2388
    continueExec = true;
44,944,529✔
2389
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
65,225,882!
2390
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
65,222,630!
2391
    continueExec = false;
65,222,630✔
2392
  } else {
2393
    pTrans->failedTimes++;
3,252✔
2394
    pTrans->code = terrno;
3,252✔
2395
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
3,252!
2396
      if (pTrans->lastAction != 0) {
3,252!
2397
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
3,252✔
2398
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
3,252!
2399
          if (pTrans->failedTimes < 6) {
×
2400
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2401
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
2402
            taosMsleep(1000);
×
2403
            continueExec = true;
×
2404
            return true;
×
2405
          }
2406
        }
2407
      }
2408

2409
      pTrans->stage = TRN_STAGE_ROLLBACK;
3,252✔
2410
      pTrans->actionPos = 0;
3,252✔
2411
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
3,252!
2412
             pTrans->actionPos);
2413
      continueExec = true;
3,252✔
2414
    } else {
2415
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2416
      continueExec = false;
×
2417
    }
2418
  }
2419

2420
  return continueExec;
110,170,411✔
2421
}
2422

2423
// execute in trans context
2424
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
44,944,529✔
2425
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
44,944,529!
2426

2427
  bool    continueExec = true;
44,944,529✔
2428
  int32_t code = mndTransCommit(pMnode, pTrans);
44,944,529✔
2429

2430
  if (code == 0) {
44,944,529✔
2431
    pTrans->code = 0;
44,930,035✔
2432
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
44,930,035✔
2433
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
44,930,035!
2434
    continueExec = true;
44,930,035✔
2435
  } else {
2436
    pTrans->code = terrno;
14,494✔
2437
    pTrans->failedTimes++;
14,494✔
2438
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
14,494!
2439
    continueExec = false;
14,494✔
2440
  }
2441

2442
  return continueExec;
44,944,529✔
2443
}
2444

2445
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
98,426,647✔
2446
  bool    continueExec = true;
98,426,647✔
2447
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
98,426,647✔
2448

2449
  if (code == 0) {
98,426,647!
2450
    pTrans->code = 0;
98,426,647✔
2451
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
98,426,647✔
2452
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
98,426,647!
2453
    continueExec = true;
98,426,647✔
2454
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
2455
    pTrans->code = 0;
×
2456
    pTrans->stage = TRN_STAGE_COMMIT;
×
2457
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
2458
    continueExec = true;
×
2459
  } else {
2460
    pTrans->code = terrno;
×
2461
    pTrans->failedTimes++;
×
2462
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2463
    continueExec = false;
×
2464
  }
2465

2466
  return continueExec;
98,426,647✔
2467
}
2468

2469
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
22,764✔
2470
  bool    continueExec = true;
22,764✔
2471
  int32_t code = 0;
22,764✔
2472

2473
  if (pTrans->exec == TRN_EXEC_SERIAL) {
22,764!
2474
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
2475
  } else {
2476
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
22,764✔
2477
  }
2478

2479
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
22,764✔
2480
  terrno = code;
19,512✔
2481

2482
  if (code == 0) {
19,512✔
2483
    pTrans->stage = TRN_STAGE_PRE_FINISH;
3,252✔
2484
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
3,252!
2485
    continueExec = true;
3,252✔
2486
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
16,260!
2487
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
16,260!
2488
    continueExec = false;
16,260✔
2489
  } else {
2490
    pTrans->failedTimes++;
×
2491
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2492
    continueExec = false;
×
2493
  }
2494

2495
  return continueExec;
19,512✔
2496
}
2497

2498
// in trans context
2499
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
3,252✔
2500
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
3,252!
2501

2502
  bool    continueExec = true;
3,252✔
2503
  int32_t code = mndTransRollback(pMnode, pTrans);
3,252✔
2504

2505
  if (code == 0) {
3,252!
2506
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
3,252✔
2507
    continueExec = true;
3,252✔
2508
  } else {
2509
    pTrans->failedTimes++;
×
2510
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2511
    continueExec = false;
×
2512
  }
2513

2514
  return continueExec;
3,252✔
2515
}
2516

2517
// excute in trans context
2518
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
3,252✔
2519
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
3,252!
2520

2521
  bool    continueExec = true;
3,252✔
2522
  int32_t code = mndTransPreFinish(pMnode, pTrans);
3,252✔
2523

2524
  if (code == 0) {
3,252!
2525
    pTrans->stage = TRN_STAGE_FINISH;
3,252✔
2526
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
3,252!
2527
    continueExec = true;
3,252✔
2528
  } else {
2529
    pTrans->failedTimes++;
×
2530
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2531
    continueExec = false;
×
2532
  }
2533

2534
  return continueExec;
3,252✔
2535
}
2536

2537
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
98,433,151✔
2538
  bool continueExec = false;
98,433,151✔
2539
  if (topHalf) return continueExec;
98,433,151✔
2540

2541
  SSdbRaw *pRaw = mndTransEncode(pTrans);
53,499,864✔
2542
  if (pRaw == NULL) {
53,499,864!
2543
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
2544
    return false;
×
2545
  }
2546
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
53,499,864!
2547

2548
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
53,499,864✔
2549
  if (code != 0) {
53,499,864!
2550
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
2551
  }
2552

2553
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
53,499,864!
2554
        pTrans->failedTimes, pTrans->createdTime);
2555
  return continueExec;
53,499,864✔
2556
}
2557

2558
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
226,063,661✔
2559
  bool continueExec = true;
226,063,661✔
2560

2561
  while (continueExec) {
640,441,541✔
2562
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
414,377,880!
2563
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2564
    pTrans->lastExecTime = taosGetTimestampMs();
414,377,880✔
2565
    switch (pTrans->stage) {
414,377,880!
2566
      case TRN_STAGE_PREPARE:
×
2567
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2568
        break;
×
2569
      case TRN_STAGE_REDO_ACTION:
172,544,285✔
2570
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
172,544,285✔
2571
        break;
172,544,285✔
2572
      case TRN_STAGE_COMMIT:
44,944,529✔
2573
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
44,944,529✔
2574
        break;
44,944,529✔
2575
      case TRN_STAGE_COMMIT_ACTION:
98,426,647✔
2576
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
98,426,647✔
2577
        break;
98,426,647✔
2578
      case TRN_STAGE_ROLLBACK:
3,252✔
2579
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
3,252✔
2580
        break;
3,252✔
2581
      case TRN_STAGE_UNDO_ACTION:
22,764✔
2582
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
22,764✔
2583
        break;
22,764✔
2584
      case TRN_STAGE_PRE_FINISH:
3,252✔
2585
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
3,252✔
2586
        break;
3,252✔
2587
      case TRN_STAGE_FINISH:
98,433,151✔
2588
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
98,433,151✔
2589
        break;
98,433,151✔
2590
      default:
×
2591
        continueExec = false;
×
2592
        break;
×
2593
    }
2594
  }
2595

2596
  mndTransSendRpcRsp(pMnode, pTrans);
226,063,661✔
2597
}
226,063,661✔
2598

2599
// start trans, pullup, receive rsp, kill
2600
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
110,300,117✔
2601
  bool topHalf = true;
110,300,117✔
2602
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
110,300,117✔
2603
}
110,300,117✔
2604

2605
// update trans
2606
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
115,763,544✔
2607
  bool topHalf = false;
115,763,544✔
2608
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
115,763,544✔
2609
}
115,763,544✔
2610

2611
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
43,778,072✔
2612
  mTrace("start to process trans timer");
43,778,072✔
2613
  mndTransPullup(pReq->info.node);
43,778,072✔
2614
  return 0;
43,778,072✔
2615
}
2616

2617
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
2618
  SArray *pArray = NULL;
×
2619
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
2620
    pArray = pTrans->redoActions;
×
2621
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2622
    pArray = pTrans->undoActions;
×
2623
  } else {
2624
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2625
  }
2626

2627
  if(!tsForceKillTrans){
×
2628
    if(pTrans->ableToBeKilled == false){
×
2629
      return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2630
    }
2631
  }
2632
  
2633
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
×
2634
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
2635
      STransAction *pAction = taosArrayGet(pArray, i);
×
2636
      mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
2637
            mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
2638
      pAction->msgSent = 1;
×
2639
      pAction->msgReceived = 1;
×
2640
      pAction->errCode = 0;
×
2641
    }
2642
  }
2643
  else if(pTrans->killMode == TRN_KILL_MODE_INTERUPT){
×
2644
    pTrans->stage = TRN_STAGE_PRE_FINISH;
×
2645
  }
2646
  else{
2647
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2648
  }
2649

2650
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
×
2651
  mndTransExecute(pMnode, pTrans, true);
×
2652
  return 0;
×
2653
}
2654

2655
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1,238✔
2656
  SMnode       *pMnode = pReq->info.node;
1,238✔
2657
  SKillTransReq killReq = {0};
1,238✔
2658
  int32_t       code = -1;
1,238✔
2659
  STrans       *pTrans = NULL;
1,238✔
2660

2661
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1,238!
2662
    code = TSDB_CODE_INVALID_MSG;
×
2663
    goto _OVER;
×
2664
  }
2665

2666
  mInfo("trans:%d, start to kill, force:%d", killReq.transId, tsForceKillTrans);
1,238!
2667
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1,238!
2668
    goto _OVER;
1,238✔
2669
  }
2670

2671
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
2672
  if (pTrans == NULL) {
×
2673
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
2674
    if (terrno != 0) code = terrno;
×
2675
    goto _OVER;
×
2676
  }
2677

2678
  code = mndKillTrans(pMnode, pTrans);
×
2679

2680
_OVER:
1,238✔
2681
  if (code != 0) {
1,238!
2682
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1,238!
2683
  }
2684

2685
  mndReleaseTrans(pMnode, pTrans);
1,238✔
2686
  TAOS_RETURN(code);
1,238✔
2687
}
2688

2689
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
2,727,701✔
2690

2691
void mndTransPullup(SMnode *pMnode) {
44,311,863✔
2692
  SSdb   *pSdb = pMnode->pSdb;
44,311,863✔
2693
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
44,311,863✔
2694
  if (pArray == NULL) return;
44,311,863!
2695

2696
  void *pIter = NULL;
44,311,863✔
2697
  while (1) {
4,774,341✔
2698
    STrans *pTrans = NULL;
49,086,204✔
2699
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
49,086,204✔
2700
    if (pIter == NULL) break;
49,086,204✔
2701
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
9,548,682!
2702
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2703
    }
2704
    sdbRelease(pSdb, pTrans);
4,774,341✔
2705
  }
2706

2707
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
44,311,863✔
2708

2709
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
49,086,204✔
2710
    int32_t *pTransId = taosArrayGet(pArray, i);
4,774,341✔
2711
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
4,774,341✔
2712
    if (pTrans != NULL) {
4,774,341!
2713
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
4,774,341!
2714
      mndTransExecute(pMnode, pTrans, false);
4,774,341✔
2715
    }
2716
    mndReleaseTrans(pMnode, pTrans);
4,774,341✔
2717
  }
2718
  taosArrayDestroy(pArray);
44,311,863✔
2719
}
2720

2721
static char *formatTimestamp(char *buf, int64_t val, int precision) {
48,683,268✔
2722
  time_t tt;
48,683,268✔
2723
  if (precision == TSDB_TIME_PRECISION_MICRO) {
48,683,268!
2724
    tt = (time_t)(val / 1000000);
×
2725
  }
2726
  if (precision == TSDB_TIME_PRECISION_NANO) {
48,683,268!
2727
    tt = (time_t)(val / 1000000000);
×
2728
  } else {
2729
    tt = (time_t)(val / 1000);
48,683,268✔
2730
  }
2731

2732
  struct tm tm;
48,683,268✔
2733
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
48,683,268!
2734
    mError("failed to get local time");
×
2735
    return NULL;
×
2736
  }
2737
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
48,683,268✔
2738

2739
  if (precision == TSDB_TIME_PRECISION_MICRO) {
48,683,268!
2740
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2741
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
48,683,268!
2742
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2743
  } else {
2744
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
48,683,268!
2745
  }
2746

2747
  return buf;
48,683,268✔
2748
}
2749

2750
static void mndTransLogAction(STrans *pTrans) {
959,896✔
2751
  char    detail[512] = {0};
959,896✔
2752
  int32_t len = 0;
959,896✔
2753
  int32_t index = 0;
959,896✔
2754

2755
  if (pTrans->stage == TRN_STAGE_PREPARE) {
959,896!
2756
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2757
      len = 0;
×
2758
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2759
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2760
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2761
                      sdbStatusName(pAction->pRaw->status));
×
2762
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2763
    }
2764
  }
2765

2766
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
959,896!
2767
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
32,612,452✔
2768
      len = 0;
31,652,556✔
2769
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
31,652,556✔
2770
      if (pAction->actionType == TRANS_ACTION_MSG) {
31,652,556✔
2771
        char bufStart[40] = {0};
24,267,954✔
2772
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
24,267,954✔
2773

2774
        char endStart[40] = {0};
24,267,954✔
2775
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
24,267,954✔
2776
        len += snprintf(detail + len, sizeof(detail) - len,
48,535,908!
2777
                        "action:%d, %s:%d msgType:%s,"
2778
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2779
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
48,535,908✔
2780
                        pAction->msgReceived, bufStart, endStart);
24,267,954✔
2781

2782
        SEpSet epset = pAction->epSet;
24,267,954✔
2783
        if (epset.numOfEps > 0) {
24,267,954!
2784
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
24,267,954!
2785
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
54,615,192✔
2786
            len +=
30,347,238✔
2787
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
30,347,238!
2788
          }
2789
        }
2790

2791
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
24,267,954!
2792
                        tstrerror(pAction->errCode));
2793
      } else {
2794
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
7,384,602!
2795
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
7,384,602✔
2796
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
7,384,602✔
2797
      }
2798
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
31,652,556!
2799
    }
2800
  }
2801

2802
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
959,896!
2803
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
×
2804
      len = 0;
×
2805
      STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
×
2806
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2807
                      mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type),
×
2808
                      sdbStatusName(pAction->pRaw->status));
×
2809
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2810
    }
2811

2812
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
×
2813
      len = 0;
×
2814
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
2815
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
2816
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
2817
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2818
        ;
2819
      } else {
2820
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2821
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2822
                        sdbStatusName(pAction->pRaw->status));
×
2823
      }
2824
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2825
    }
2826
  }
2827
}
959,896✔
2828

2829
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
1,015,442✔
2830
  SMnode *pMnode = pReq->info.node;
1,015,442✔
2831
  SSdb   *pSdb = pMnode->pSdb;
1,015,442✔
2832
  int32_t numOfRows = 0;
1,015,442✔
2833
  STrans *pTrans = NULL;
1,015,442✔
2834
  int32_t cols = 0;
1,015,442✔
2835
  int32_t code = 0;
1,015,442✔
2836
  int32_t lino = 0;
1,015,442✔
2837

2838
  while (numOfRows < rows) {
1,975,338!
2839
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
1,975,338✔
2840
    if (pShow->pIter == NULL) break;
1,975,338✔
2841

2842
    cols = 0;
959,896✔
2843

2844
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2845
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
959,896!
2846

2847
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2848
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
959,896!
2849
                        _OVER);
2850

2851
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
959,896✔
2852
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
959,896!
2853
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2854
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
959,896!
2855

2856
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
959,896✔
2857
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
959,896!
2858
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2859
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
959,896!
2860

2861
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
959,896✔
2862
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
959,896!
2863
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2864
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
959,896!
2865

2866
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
959,896✔
2867
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
959,896!
2868
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2869
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
959,896!
2870

2871
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
959,896!
2872
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
959,896✔
2873
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
959,896!
2874
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2875
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
959,896!
2876

2877
    /*
2878
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2879
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2880
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2881
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2882
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2883
    */
2884

2885
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2886
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
959,896!
2887
                        _OVER);
2888

2889
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2890
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
959,896!
2891
                        _OVER);
2892

2893
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
959,896✔
2894
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
959,896✔
2895
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
1,919,792✔
2896
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
1,919,792✔
2897
    SEpSet  epset = pTrans->lastEpset;
959,896✔
2898
    if (epset.numOfEps > 0) {
959,896✔
2899
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
1,894,576!
2900
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
1,894,576✔
2901
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
2,666,016✔
2902
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
1,718,728!
2903
      }
2904
    }
2905
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
959,896!
2906
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
959,896✔
2907
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
959,896!
2908

2909
    mndTransLogAction(pTrans);
959,896✔
2910

2911
    numOfRows++;
959,896✔
2912
    sdbRelease(pSdb, pTrans);
959,896✔
2913
  }
2914

2915
_OVER:
1,015,442✔
2916
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
1,015,442!
2917
  pShow->numOfRows += numOfRows;
1,015,442✔
2918
  return numOfRows;
1,015,442✔
2919
}
2920

2921
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
324,192✔
2922
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2923
  int32_t code = 0;
324,192✔
2924
  int32_t lino = 0;
324,192✔
2925
  int32_t len = 0;
324,192✔
2926

2927
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
324,192✔
2928
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&transactionId, false), &lino, _OVER);
324,192!
2929

2930
  char action[30 + 1] = {0};
324,192✔
2931
  if (curActionId == pAction->id) {
324,192✔
2932
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
1,842!
2933
                    mndTransTypeStr(pAction->actionType));
2934
  } else {
2935
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
322,350!
2936
                    mndTransTypeStr(pAction->actionType));
2937
  }
2938
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
324,192✔
2939
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
324,192!
2940
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
324,192✔
2941
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
324,192!
2942
_OVER:
324,192✔
2943
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
324,192!
2944
  return code;
324,192✔
2945
}
2946

2947
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
324,192✔
2948
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2949
  int32_t code = 0;
324,192✔
2950
  int32_t lino = 0;
324,192✔
2951
  int32_t len = 0;
324,192✔
2952
  int32_t cols = 0;
324,192✔
2953

2954
  cols = 0;
324,192✔
2955

2956
  if (mndShowTransCommonColumns(pShow, pBlock, pAction, transactionId, curActionId, numOfRows, &cols) != 0) return;
324,192!
2957

2958
  if (pAction->actionType == TRANS_ACTION_MSG) {
324,192✔
2959
    int32_t len = 0;
265,248✔
2960

2961
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
265,248✔
2962
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
265,248!
2963
                    pAction->msgSent, pAction->msgReceived);
265,248✔
2964
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
265,248✔
2965
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
265,248!
2966
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
265,248✔
2967
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
265,248!
2968

2969
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
265,248✔
2970
    len = 0;
265,248✔
2971
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
265,248!
2972
                    tstrerror(pAction->errCode));
2973
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
265,248✔
2974
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
265,248!
2975
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
265,248✔
2976
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
265,248!
2977

2978
    char target[TSDB_TRANS_TARGET_LEN] = {0};
265,248✔
2979
    len = 0;
265,248✔
2980
    SEpSet epset = pAction->epSet;
265,248✔
2981
    if (epset.numOfEps > 0) {
265,248!
2982
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
618,912✔
2983
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
353,664!
2984
      }
2985
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
265,248!
2986
    }
2987
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
265,248✔
2988
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
265,248!
2989
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
265,248✔
2990
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
265,248!
2991

2992
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
265,248✔
2993
    len = 0;
265,248✔
2994
    char bufStart[40] = {0};
265,248✔
2995
    if (pAction->startTime > 0) (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
265,248✔
2996
    char bufEnd[40] = {0};
265,248✔
2997
    if (pAction->endTime > 0) (void)formatTimestamp(bufEnd, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
265,248✔
2998
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
265,248!
2999
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
265,248✔
3000
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
265,248!
3001
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
265,248✔
3002
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
265,248!
3003

3004
  } else {
3005
    int32_t len = 0;
58,944✔
3006

3007
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
58,944✔
3008
    if (pAction->pRaw->type == SDB_VGROUP) {
58,944!
3009
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
58,944✔
3010
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
58,944✔
3011
      len += snprintf(objType + len, sizeof(objType) - len, "%s(%d)", sdbTableName(pAction->pRaw->type), pVgroup->vgId);
58,944!
3012
      taosMemoryFreeClear(pRow);
58,944!
3013
    } else {
3014
      strcpy(objType, sdbTableName(pAction->pRaw->type));
×
3015
    }
3016
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
58,944✔
3017
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
58,944!
3018
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
58,944✔
3019
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
58,944!
3020

3021
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
58,944✔
3022
    len = 0;
58,944✔
3023
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
58,944!
3024
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
58,944✔
3025
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
58,944!
3026
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
58,944✔
3027
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
58,944!
3028

3029
    char target[TSDB_TRANS_TARGET_LEN] = "";
58,944✔
3030
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
58,944✔
3031
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
58,944!
3032
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
58,944✔
3033
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
58,944!
3034

3035
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
58,944✔
3036
    len = 0;
58,944✔
3037
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
58,944!
3038
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
58,944✔
3039
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
58,944!
3040
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
58,944✔
3041
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
58,944!
3042
  }
3043

3044
_OVER:
324,192✔
3045
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
324,192!
3046
}
3047

3048
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
3,684✔
3049
  if (stage == TRN_STAGE_PREPARE) {
3,684!
3050
    return pTrans->prepareActions;
×
3051
  }
3052
  if (stage == TRN_STAGE_REDO_ACTION) {
3,684!
3053
    return pTrans->redoActions;
3,684✔
3054
  }
3055
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
3056
    return pTrans->commitActions;
×
3057
  }
3058
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
3059
    return pTrans->undoActions;
×
3060
  }
3061
  return NULL;
×
3062
}
3063

3064
typedef struct STransDetailIter {
3065
  void     *pIter;
3066
  STrans   *pTrans;
3067
  ETrnStage stage;
3068
  int32_t   num;
3069
} STransDetailIter;
3070

3071
static void mndTransShowActions(SSdb *pSdb, STransDetailIter *pShowIter, SShowObj *pShow, SSDataBlock *pBlock,
3,684✔
3072
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3073
  int32_t actionNum = taosArrayGetSize(pActions);
3,684✔
3074
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
3,684!
3075

3076
  for (int32_t i = start; i < actionNum; ++i) {
326,034✔
3077
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
324,192✔
3078
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
324,192✔
3079
    (*numOfRows)++;
324,192✔
3080
    if (*numOfRows >= rows) break;
324,192✔
3081
  }
3082

3083
  if (*numOfRows == end) {
3,684✔
3084
    sdbRelease(pSdb, pShowIter->pTrans);
1,842✔
3085
    pShowIter->pTrans = NULL;
1,842✔
3086
    pShowIter->num = 0;
1,842✔
3087
  } else {
3088
    pShowIter->pTrans = pShowIter->pTrans;
1,842✔
3089
    pShowIter->stage = pShowIter->pTrans->stage;
1,842✔
3090
    pShowIter->num += (*numOfRows);
1,842✔
3091
  }
3092
}
3,684✔
3093

3094
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
5,526✔
3095
  SMnode *pMnode = pReq->info.node;
5,526✔
3096
  SSdb   *pSdb = pMnode->pSdb;
5,526✔
3097
  int32_t numOfRows = 0;
5,526✔
3098

3099
  int32_t code = 0;
5,526✔
3100
  int32_t lino = 0;
5,526✔
3101

3102
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
5,526!
3103
        pShow->pIter);
3104

3105
  if (pShow->pIter == NULL) {
5,526✔
3106
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
1,842!
3107
    if (pShow->pIter == NULL) {
1,842!
3108
      mError("failed to malloc for pShow->pIter");
×
3109
      return 0;
×
3110
    }
3111
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
1,842!
3112
  }
3113

3114
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
5,526✔
3115

3116
  while (numOfRows < rows) {
5,526!
3117
    if (pShowIter->pTrans == NULL) {
5,526✔
3118
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
3,684✔
3119
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
3,684!
3120
      if (pShowIter->pIter == NULL) break;
3,684✔
3121
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
1,842!
3122
            pShowIter->pTrans->stage, pShowIter->num);
3123

3124
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
1,842✔
3125

3126
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
1,842✔
3127
      break;
1,842✔
3128
    } else {
3129
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
1,842!
3130
            pShowIter->num);
3131
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
1,842✔
3132

3133
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
1,842✔
3134
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
1,842✔
3135
      break;
1,842✔
3136
    }
3137
  }
3138

3139
_OVER:
×
3140
  pShow->numOfRows += numOfRows;
5,526✔
3141

3142
  if (code != 0) {
5,526!
3143
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
3144
  } else {
3145
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
5,526!
3146
  }
3147
  if (numOfRows == 0) {
5,526✔
3148
    taosMemoryFree(pShow->pIter);
1,842!
3149
    pShow->pIter = NULL;
1,842✔
3150
  }
3151
  return numOfRows;
5,526✔
3152
}
3153

3154
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
3155
  SSdb *pSdb = pMnode->pSdb;
×
3156
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
3157
}
×
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