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

taosdata / TDengine / #4674

18 Aug 2025 07:58AM UTC coverage: 59.821% (+0.1%) from 59.715%
#4674

push

travis-ci

web-flow
test: update case desc (#32551)

136937 of 292075 branches covered (46.88%)

Branch coverage included in aggregate %.

207916 of 284395 relevant lines covered (73.11%)

4553289.94 hits per line

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

63.86
/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; }
342,017✔
60

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

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

77
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
515,927✔
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) {
2,255✔
90
  SSdbTable table = {
2,255✔
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);
2,255✔
101
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
2,255✔
102

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

109
void mndCleanupTrans(SMnode *pMnode) {}
2,255✔
110

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

115
  for (int32_t i = 0; i < actionNum; ++i) {
1,863,679✔
116
    STransAction *pAction = taosArrayGet(pArray, i);
1,197,823✔
117
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,197,823✔
118
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
833,342✔
119
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
364,481!
120
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
364,481✔
121
    } else {
122
      // empty
123
    }
124
    rawDataLen += sizeof(int8_t);
1,197,823✔
125
  }
126

127
  return rawDataLen;
665,856✔
128
}
129

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

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

168
_OVER:
665,856✔
169
  *offset = dataPos;
665,856✔
170
  return ret;
665,856✔
171
}
172

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

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

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

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

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

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

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

222
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
166,464!
223
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
166,464!
224
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
166,464!
225
  if (pTrans->param != NULL) {
166,464!
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)
166,464!
230

231
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
166,464✔
232
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
166,464!
233
  void *pIter = NULL;
166,464✔
234
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
166,464✔
235
  while (pIter) {
166,536✔
236
    int32_t arbGroupId = *(int32_t *)pIter;
72✔
237
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
72!
238
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
72✔
239
  }
240

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

246
  if (sver > TRANS_VER2_NUMBER) {
166,464!
247
    int32_t groupNum = taosHashGetSize(pTrans->groupActionPos);
166,464✔
248
    SDB_SET_INT32(pRaw, dataPos, groupNum, _OVER)
166,464!
249
    void *pIter = NULL;
166,464✔
250
    pIter = taosHashIterate(pTrans->groupActionPos, NULL);
166,464✔
251
    while (pIter) {
171,764✔
252
      size_t   keysize = 0;
5,300✔
253
      int32_t *groupId = taosHashGetKey(pIter, &keysize);
5,300✔
254
      int32_t  groupPos = *(int32_t *)pIter;
5,300✔
255
      SDB_SET_INT32(pRaw, dataPos, *groupId, _OVER)
5,300!
256
      SDB_SET_INT32(pRaw, dataPos, groupPos, _OVER)
5,300!
257
      pIter = taosHashIterate(pTrans->groupActionPos, pIter);
5,300✔
258
    }
259
  }
260
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
166,464!
261
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
166,464!
262

263
  terrno = 0;
166,464✔
264

265
_OVER:
166,464✔
266
  if (terrno != 0) {
166,464!
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);
166,464✔
274
  return pRaw;
166,464✔
275
}
276

277
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
56,791✔
278
  if (pAction->groupId < 0) return 0;
56,791✔
279
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
54,347✔
280
  if (redoAction == NULL) {
54,347✔
281
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
10,688✔
282
    if (array != NULL) {
10,688!
283
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
10,688!
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));
10,688✔
289
  }
290
  if (redoAction != NULL) {
54,347!
291
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
108,694!
292
  }
293
  return 0;
54,347✔
294
}
295

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

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

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

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

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

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

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

437
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
312,708!
438

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

444
  pRow = sdbAllocRow(sizeof(STrans));
312,708✔
445
  if (pRow == NULL) goto _OVER;
312,708!
446

447
  pTrans = sdbGetRowObj(pRow);
312,708✔
448
  if (pTrans == NULL) goto _OVER;
312,708!
449

450
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
312,708!
451

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

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

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

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

497
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
312,708!
498
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
312,708✔
499
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
3,564!
500
                                      sver) < 0)
501
      goto _OVER;
×
502
  } else {
503
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
309,144!
504
  }
505
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
312,708!
506
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
312,708!
507

508
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
312,708!
509
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
312,708!
510
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
312,708!
511
  if (pTrans->paramLen != 0) {
312,708!
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);
312,708!
518

519
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
312,708!
520
  if (arbgroupIdNum > 0) {
312,708✔
521
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
110✔
522
    if (pTrans->arbGroupIds == NULL) goto _OVER;
110!
523
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
230✔
524
      int32_t arbGroupId = 0;
120✔
525
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
120!
526
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
120!
527
    }
528
  }
529

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

539
  if (sver > TRANS_VER2_NUMBER) {
312,708!
540
    int32_t groupNum = -1;
312,708✔
541
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
312,708!
542
    for (int32_t i = 0; i < groupNum; ++i) {
323,396✔
543
      int32_t groupId = -1;
10,688✔
544
      int32_t groupPos = -1;
10,688✔
545
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
10,688!
546
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
10,688!
547
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
10,688!
548
        goto _OVER;
×
549
    }
550
  }
551

552
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
312,708!
553

554
  terrno = 0;
312,708✔
555

556
_OVER:
312,708✔
557
  if (terrno != 0 && pTrans != NULL) {
312,708!
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) {
312,708!
566
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
312,708✔
567
  }
568
  return pRow;
312,708✔
569
}
570

571
static const char *mndTransStr(ETrnStage stage) {
2,134,232✔
572
  switch (stage) {
2,134,232!
573
    case TRN_STAGE_PREPARE:
164,387✔
574
      return "prepare";
164,387✔
575
    case TRN_STAGE_REDO_ACTION:
701,294✔
576
      return "redoAction";
701,294✔
577
    case TRN_STAGE_ROLLBACK:
10✔
578
      return "rollback";
10✔
579
    case TRN_STAGE_UNDO_ACTION:
42,078✔
580
      return "undoAction";
42,078✔
581
    case TRN_STAGE_COMMIT:
269,007✔
582
      return "commit";
269,007✔
583
    case TRN_STAGE_COMMIT_ACTION:
557,895✔
584
      return "commitAction";
557,895✔
585
    case TRN_STAGE_FINISH:
399,551✔
586
      return "finished";
399,551✔
587
    case TRN_STAGE_PRE_FINISH:
10✔
588
      return "pre-finish";
10✔
589
    default:
×
590
      return "invalid";
×
591
  }
592
}
593

594
static const char *mndTransTypeStr(ETrnAct actionType) {
×
595
  switch (actionType) {
×
596
    case TRANS_ACTION_MSG:
×
597
      return "msg";
×
598
    case TRANS_ACTION_RAW:
×
599
      return "sdb";
×
600
    default:
×
601
      return "invalid";
×
602
  }
603
}
604

605
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
604,609✔
606
  if (pAction != NULL) {
604,609✔
607
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
470,775✔
608
      pTrans->lastAction = pAction->id;
346,386✔
609
      pTrans->lastMsgType = pAction->msgType;
346,386✔
610
      pTrans->lastEpset = pAction->epSet;
346,386✔
611
      pTrans->lastErrorNo = pAction->errCode;
346,386✔
612
    }
613
  } else {
614
    pTrans->lastAction = 0;
133,834✔
615
    pTrans->lastMsgType = 0;
133,834✔
616
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
133,834✔
617
    pTrans->lastErrorNo = 0;
133,834✔
618
  }
619
}
604,609✔
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,966✔
630
  switch (ftype) {
1,966!
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:
983✔
636
      return mndRebCntInc;
983✔
637
    case TRANS_STOP_FUNC_MQ_REB:
983✔
638
      return mndRebCntDec;
983✔
639
    default:
×
640
      return NULL;
×
641
  }
642
}
643

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

648
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
59,589✔
649

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

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

663
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
59,589!
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) {
59,589!
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;
59,589✔
674
}
675

676
void mndTransDropData(STrans *pTrans) {
363,363✔
677
  if (pTrans->prepareActions != NULL) {
363,363!
678
    mndTransDropActions(pTrans->prepareActions);
363,363✔
679
    pTrans->prepareActions = NULL;
363,363✔
680
  }
681
  if (pTrans->redoActions != NULL) {
363,363!
682
    mndTransDropActions(pTrans->redoActions);
363,363✔
683
    pTrans->redoActions = NULL;
363,363✔
684
  }
685
  if (pTrans->undoActions != NULL) {
363,363!
686
    mndTransDropActions(pTrans->undoActions);
363,363✔
687
    pTrans->undoActions = NULL;
363,363✔
688
  }
689
  if (pTrans->commitActions != NULL) {
363,363!
690
    mndTransDropActions(pTrans->commitActions);
363,363✔
691
    pTrans->commitActions = NULL;
363,363✔
692
  }
693
  if (pTrans->redoGroupActions != NULL) {
363,363✔
694
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
54,219✔
695
    while (pIter) {
65,403✔
696
      SArray **redoActions = pIter;
11,184✔
697
      taosArrayDestroy(*redoActions);
11,184✔
698
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
11,184✔
699
    }
700

701
    taosHashCleanup(pTrans->redoGroupActions);
54,219✔
702
    pTrans->redoGroupActions = NULL;
54,219✔
703
  }
704
  if (pTrans->groupActionPos != NULL) {
363,363✔
705
    taosHashCleanup(pTrans->groupActionPos);
54,219✔
706
    pTrans->groupActionPos = NULL;
54,219✔
707
  }
708
  if (pTrans->arbGroupIds != NULL) {
363,363✔
709
    taosHashCleanup(pTrans->arbGroupIds);
50,765✔
710
  }
711
  if (pTrans->pRpcArray != NULL) {
363,363✔
712
    taosArrayDestroy(pTrans->pRpcArray);
50,655✔
713
    pTrans->pRpcArray = NULL;
50,655✔
714
  }
715
  if (pTrans->rpcRsp != NULL) {
363,363✔
716
    taosMemoryFree(pTrans->rpcRsp);
17,712!
717
    pTrans->rpcRsp = NULL;
17,712✔
718
    pTrans->rpcRspLen = 0;
17,712✔
719
  }
720
  if (pTrans->param != NULL) {
363,363!
721
    taosMemoryFree(pTrans->param);
×
722
    pTrans->param = NULL;
×
723
    pTrans->paramLen = 0;
×
724
  }
725
  (void)taosThreadMutexDestroy(&pTrans->mutex);
363,363✔
726
}
363,363✔
727

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

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

740
  mndTransDropData(pTrans);
186,141✔
741
  return 0;
186,141✔
742
}
743

744
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
268,356✔
745
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
793,985✔
746
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
525,629✔
747
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
525,629✔
748
    pOldAction->rawWritten = pNewAction->rawWritten;
525,629✔
749
    pOldAction->msgSent = pNewAction->msgSent;
525,629✔
750
    pOldAction->msgReceived = pNewAction->msgReceived;
525,629✔
751
    pOldAction->errCode = pNewAction->errCode;
525,629✔
752
  }
753
}
268,356✔
754

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

759
  if (pOld->createdTime != pNew->createdTime) {
67,089!
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);
67,089✔
769
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
67,089✔
770
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
67,089✔
771
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
67,089✔
772
  pOld->stage = pNew->stage;
67,089✔
773
  pOld->actionPos = pNew->actionPos;
67,089✔
774

775
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
67,089✔
776
  while (pIter != NULL) {
71,663✔
777
    int32_t newActionPos = *(int32_t *)pIter;
4,574✔
778

779
    size_t   keylen = 0;
4,574✔
780
    int32_t *groupId = taosHashGetKey(pIter, &keylen);
4,574✔
781

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

788
    pIter = taosHashIterate(pNew->groupActionPos, pIter);
4,574✔
789
  }
790

791
  if (pOld->stage == TRN_STAGE_COMMIT) {
67,089✔
792
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
59,456✔
793
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
59,456!
794
  }
795

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

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

806
  return 0;
67,089✔
807
}
808

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

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

823
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
50,655✔
824
                       const char *opername) {
825
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
50,655!
826
  if (pTrans == NULL) {
50,655!
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) {
50,655!
833
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
50,655✔
834
  }
835

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

858
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
50,655!
859
      pTrans->pRpcArray == NULL) {
50,655!
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) {
50,655✔
867
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
65,622!
868
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
869
      return NULL;
×
870
    }
871
    pTrans->originRpcType = pReq->msgType;
32,811✔
872
  }
873

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

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

880
static void mndTransDropActions(SArray *pArray) {
1,453,452✔
881
  int32_t size = taosArrayGetSize(pArray);
1,453,452✔
882
  for (int32_t i = 0; i < size; ++i) {
4,015,123✔
883
    STransAction *pAction = taosArrayGet(pArray, i);
2,561,671✔
884
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,561,671✔
885
      taosMemoryFreeClear(pAction->pRaw);
1,753,109!
886
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
808,562!
887
      taosMemoryFreeClear(pAction->pCont);
808,562!
888
    } else {
889
      // nothing
890
    }
891
  }
892

893
  taosArrayDestroy(pArray);
1,453,452✔
894
}
1,453,452✔
895

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

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

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

912
  return 0;
338,516✔
913
}
914

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

918
  SArray **redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
1,165✔
919
  if (redoAction == NULL) {
1,165✔
920
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
496✔
921
    if (array != NULL) {
496!
922
      if (taosHashPut(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
496!
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));
496✔
927
    }
928
  }
929
  if (redoAction != NULL) {
1,165!
930
    mInfo("trans:%d, append action into group %d, msgType:%s", pTrans->id, pAction->groupId,
1,165!
931
          TMSG_INFO(pAction->msgType));
932
    void *ptr = taosArrayPush(*redoAction, &pAction);
1,165✔
933
    if (ptr == NULL) {
1,165!
934
      TAOS_RETURN(terrno);
×
935
    }
936
  }
937

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

947
  return 0;
1,165✔
948
}
949

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

957
int32_t mndTransAppendGroupRedolog(STrans *pTrans, SSdbRaw *pRaw, int32_t groupId) {
987✔
958
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION,
987✔
959
                         .actionType = TRANS_ACTION_RAW,
960
                         .pRaw = pRaw,
961
                         .mTraceId = pTrans->mTraceId,
987✔
962
                         .groupId = groupId};
963
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
987!
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));
987!
968
  return mndTransAppendAction(pTrans->redoActions, &action);
987✔
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) {
13,767✔
991
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
13,767✔
992
  return mndTransAppendAction(pTrans->undoActions, &action);
13,767✔
993
}
994

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

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

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

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

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

1029
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
973✔
1030
  pTrans->startFunc = startFunc;
973✔
1031
  pTrans->stopFunc = stopFunc;
973✔
1032
  pTrans->param = param;
973✔
1033
  pTrans->paramLen = paramLen;
973✔
1034
}
973✔
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,608✔
1068
  if (dbname != NULL) {
26,608!
1069
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
26,608✔
1070
  }
1071
  if (stbname != NULL) {
26,608✔
1072
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
19,304✔
1073
  }
1074
}
26,608✔
1075

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

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

1087
void mndTransSetGroupParallel(STrans *pTrans) {
204✔
1088
  mInfo("trans:%d, set Group Parallel", pTrans->id);
204!
1089
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
204✔
1090
}
204✔
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) {
13✔
1100
  pTrans->ableToBeKilled = true; 
13✔
1101
  pTrans->killMode = killMode; 
13✔
1102
}
13✔
1103

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

1106
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
4,166✔
1107

1108
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
106,817✔
1109
  int32_t  code = 0;
106,817✔
1110
  SSdbRaw *pRaw = mndTransEncode(pTrans);
106,817✔
1111
  if (pRaw == NULL) {
106,817!
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));
106,817!
1118

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

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

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

1140
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
16,917✔
1141
  if (conflict[0] == 0) return false;
16,917✔
1142
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
16,914✔
1143
  return false;
16,805✔
1144
}
1145

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

1157
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
136,738✔
1158
  STrans *pTrans = NULL;
136,738✔
1159
  void   *pIter = NULL;
136,738✔
1160
  bool    conflict = false;
136,738✔
1161

1162
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
136,738✔
1163

1164
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
93,867✔
1165
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
93,867!
1166

1167
  while (1) {
1168
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
111,001✔
1169
    if (pIter == NULL) break;
111,001✔
1170

1171
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
17,134✔
1172

1173
    if (pNew->conflict == TRN_CONFLICT_DB) {
17,134✔
1174
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
4✔
1175
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4!
1176
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
3✔
1177
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
3✔
1178
      }
1179
    }
1180

1181
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
17,134✔
1182
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
16,921✔
1183
      if (pTrans->conflict == TRN_CONFLICT_DB) {
16,921✔
1184
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
214✔
1185
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
214✔
1186
      }
1187
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
16,921✔
1188
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
16,700✔
1189
      }
1190
    }
1191

1192
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
17,134✔
1193
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
1!
1194
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
1!
1195
        void *pGidIter = taosHashIterate(pNew->arbGroupIds, NULL);
×
1196
        while (pGidIter != NULL) {
×
1197
          int32_t groupId = *(int32_t *)pGidIter;
×
1198
          if (taosHashGet(pTrans->arbGroupIds, &groupId, sizeof(int32_t)) != NULL) {
×
1199
            taosHashCancelIterate(pNew->arbGroupIds, pGidIter);
×
1200
            mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1201
            break;
×
1202
          } else {
1203
            mndTransLogConflict(pNew, pTrans, false, &conflict);
×
1204
          }
1205
          pGidIter = taosHashIterate(pNew->arbGroupIds, pGidIter);
×
1206
        }
1207
      }
1208
    }
1209

1210
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
17,134!
1211
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
×
1212
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1213
      } else {
1214
        mndTransLogConflict(pNew, pTrans, false, &conflict);
×
1215
      }
1216
    }
1217

1218
    sdbRelease(pMnode->pSdb, pTrans);
17,134✔
1219
  }
1220

1221
  return conflict;
93,867✔
1222
}
1223

1224
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
136,738✔
1225
  int32_t code = 0;
136,738✔
1226
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
136,738✔
1227
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
71,053!
1228
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
1229
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
1230
      TAOS_RETURN(code);
×
1231
    }
1232
  }
1233

1234
  if (mndCheckTransConflict(pMnode, pTrans)) {
136,738✔
1235
    code = TSDB_CODE_MND_TRANS_CONFLICT;
543✔
1236
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
543!
1237
    TAOS_RETURN(code);
543✔
1238
  }
1239

1240
  TAOS_RETURN(code);
136,195✔
1241
}
1242

1243
int32_t mndTransCheckConflictWithCompact(SMnode *pMnode, STrans *pTrans) {
289✔
1244
  int32_t      code = 0;
289✔
1245
  void        *pIter = NULL;
289✔
1246
  bool         conflict = false;
289✔
1247
  SCompactObj *pCompact = NULL;
289✔
1248

1249
  while (1) {
4✔
1250
    bool thisConflict = false;
293✔
1251
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
293✔
1252
    if (pIter == NULL) break;
293✔
1253

1254
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
4✔
1255
      thisConflict = true;
2✔
1256
    }
1257
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4!
1258
      if (taosStrcasecmp(pTrans->dbname, pCompact->dbname) == 0) thisConflict = true;
2!
1259
    }
1260

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

1272
  if (conflict) {
289✔
1273
    code = TSDB_CODE_MND_TRANS_CONFLICT_COMPACT;
4✔
1274
    mError("trans:%d, failed to check tran conflict with compact since %s", pTrans->id, tstrerror(code));
4!
1275
    TAOS_RETURN(code);
4✔
1276
  }
1277

1278
  TAOS_RETURN(code);
285✔
1279
}
1280

1281
static bool mndTransActionsOfSameType(SArray *pActions) {
118,180✔
1282
  int32_t size = taosArrayGetSize(pActions);
118,180✔
1283
  ETrnAct lastActType = TRANS_ACTION_NULL;
118,180✔
1284
  bool    same = true;
118,180✔
1285
  for (int32_t i = 0; i < size; ++i) {
400,051✔
1286
    STransAction *pAction = taosArrayGet(pActions, i);
281,871✔
1287
    if (i > 0) {
281,871✔
1288
      if (lastActType != pAction->actionType) {
200,292!
1289
        same = false;
×
1290
        break;
×
1291
      }
1292
    }
1293
    lastActType = pAction->actionType;
281,871✔
1294
  }
1295
  return same;
118,180✔
1296
}
1297

1298
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
50,036✔
1299
  int32_t code = 0;
50,036✔
1300
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
50,036✔
1301
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
48,614!
1302
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1303
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1304
      TAOS_RETURN(code);
×
1305
    }
1306

1307
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
48,614✔
1308
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
19,530!
1309
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1310
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1311
        TAOS_RETURN(code);
×
1312
      }
1313
    }
1314
  }
1315

1316
  TAOS_RETURN(code);
50,036✔
1317
}
1318

1319
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
50,036✔
1320
  int32_t code = 0;
50,036✔
1321
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
50,036!
1322
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
×
1323
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
×
1324
    TAOS_RETURN(code);
×
1325
  }
1326
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
50,036!
1327
    code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1328
    mError("trans:%d, types of commit actions are not the same", pTrans->id);
×
1329
    TAOS_RETURN(code);
×
1330
  }
1331

1332
  TAOS_RETURN(code);
50,036✔
1333
}
1334

1335
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
50,046✔
1336
  int32_t code = 0;
50,046✔
1337
  if (pTrans == NULL) {
50,046!
1338
    return TSDB_CODE_INVALID_PARA;
×
1339
  }
1340

1341
  mInfo("trans:%d, action list:", pTrans->id);
50,046!
1342
  int32_t index = 0;
50,046✔
1343
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
74,963✔
1344
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
24,917✔
1345
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
24,917!
1346
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1347
  }
1348

1349
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
107,447✔
1350
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
57,401✔
1351
    if (pAction->actionType == TRANS_ACTION_MSG) {
57,401✔
1352
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
55,236!
1353
            TMSG_INFO(pAction->msgType));
1354
      ;
1355
    } else {
1356
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
2,165!
1357
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1358
    }
1359
  }
1360

1361
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
263,718✔
1362
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
213,672✔
1363
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
213,672!
1364
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1365
  }
1366

1367
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
92,038✔
1368
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
41,992✔
1369
    if (pAction->actionType == TRANS_ACTION_MSG) {
41,992✔
1370
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
28,225!
1371
            TMSG_INFO(pAction->msgType));
1372
      ;
1373
    } else {
1374
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
13,767!
1375
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1376
    }
1377
  }
1378

1379
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
50,046✔
1380

1381
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
50,036!
1382

1383
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
50,036!
1384

1385
  mInfo("trans:%d, prepare transaction", pTrans->id);
50,036!
1386
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
50,036!
1387
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
1388
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
×
1389
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
×
1390
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
×
1391
    TAOS_RETURN(code);
×
1392
  }
1393
  mInfo("trans:%d, prepare finished", pTrans->id);
50,036!
1394

1395
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
50,036✔
1396
  if (pNew == NULL) {
50,036!
1397
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1398
    if (terrno != 0) code = terrno;
×
1399
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, tstrerror(code));
×
1400
    TAOS_RETURN(code);
×
1401
  }
1402

1403
  pNew->pRpcArray = pTrans->pRpcArray;
50,036✔
1404
  pNew->rpcRsp = pTrans->rpcRsp;
50,036✔
1405
  pNew->rpcRspLen = pTrans->rpcRspLen;
50,036✔
1406
  pNew->mTraceId = pTrans->mTraceId;
50,036✔
1407
  pTrans->pRpcArray = NULL;
50,036✔
1408
  pTrans->rpcRsp = NULL;
50,036✔
1409
  pTrans->rpcRspLen = 0;
50,036✔
1410

1411
  mInfo("trans:%d, execute transaction in prepare", pTrans->id);
50,036!
1412
  mndTransExecute(pMnode, pNew, false);
50,036✔
1413
  mndReleaseTrans(pMnode, pNew);
50,036✔
1414
  // TDOD change to TAOS_RETURN(code);
1415
  return 0;
50,036✔
1416
}
1417

1418
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
50,030✔
1419
  int32_t code = 0;
50,030✔
1420
  mInfo("trans:%d, commit transaction", pTrans->id);
50,030!
1421
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
50,030✔
1422
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
5!
1423
    TAOS_RETURN(code);
5✔
1424
  }
1425
  mInfo("trans:%d, commit finished", pTrans->id);
50,025!
1426
  TAOS_RETURN(code);
50,025✔
1427
}
1428

1429
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
2✔
1430
  int32_t code = 0;
2✔
1431
  mInfo("trans:%d, rollback transaction", pTrans->id);
2!
1432
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
2!
1433
    mError("trans:%d, failed to rollback since %s", pTrans->id, tstrerror(code));
×
1434
    TAOS_RETURN(code);
×
1435
  }
1436
  mInfo("trans:%d, rollback finished", pTrans->id);
2!
1437
  TAOS_RETURN(code);
2✔
1438
}
1439

1440
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
2✔
1441
  int32_t code = 0;
2✔
1442
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
2!
1443
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
2!
1444
    mError("trans:%d, failed to pre-finish since %s", pTrans->id, tstrerror(code));
×
1445
    TAOS_RETURN(code);
×
1446
  }
1447
  mInfo("trans:%d, pre-finish finished", pTrans->id);
2!
1448
  TAOS_RETURN(code);
2✔
1449
}
1450

1451
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
239,162✔
1452
  bool    sendRsp = false;
239,162✔
1453
  int32_t code = pTrans->code;
239,162✔
1454

1455
  if (pTrans->stage == TRN_STAGE_FINISH) {
239,162✔
1456
    sendRsp = true;
109,495✔
1457
  }
1458

1459
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
239,162✔
1460
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
89,545!
1461
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
12!
1462
      sendRsp = true;
12✔
1463
    }
1464
  } else {
1465
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
149,617✔
1466
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
84,789!
1467
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1468
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1469
      } else {
1470
        if (pTrans->failedTimes > 6) sendRsp = true;
84,789✔
1471
      }
1472
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
84,789✔
1473
    }
1474
  }
1475

1476
  if (!sendRsp) {
239,162✔
1477
    return;
129,650✔
1478
  } else {
1479
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
109,512!
1480
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1481
  }
1482

1483
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
109,512!
1484
  taosWLockLatch(&pTrans->lockRpcArray);
109,512✔
1485
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
109,512✔
1486
  if (size <= 0) {
109,512✔
1487
    taosWUnLockLatch(&pTrans->lockRpcArray);
77,324✔
1488
    return;
77,324✔
1489
  }
1490

1491
  for (int32_t i = 0; i < size; ++i) {
64,376✔
1492
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
32,188✔
1493
    if (pInfo->handle != NULL) {
32,188✔
1494
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
30,823!
1495
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1496
      }
1497
      if (code == TSDB_CODE_SYN_TIMEOUT) {
30,823!
1498
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1499
      }
1500

1501
      if (i != 0 && code == 0) {
30,823!
1502
        code = TSDB_CODE_MNODE_NOT_FOUND;
×
1503
      }
1504
      mInfo("vgId:1, trans:%d, client:%d start to send rsp, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,823!
1505
            mndTransStr(pTrans->stage), pInfo->ahandle);
1506

1507
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
30,823✔
1508

1509
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
30,823✔
1510
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,163!
1511
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,163✔
1512
        if (pDb != NULL) {
4,163!
1513
          for (int32_t j = 0; j < 12; j++) {
5,134✔
1514
            bool ready = mndIsDbReady(pMnode, pDb);
5,129✔
1515
            if (!ready) {
5,129✔
1516
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
971!
1517
              taosMsleep(1000);
971✔
1518
            } else {
1519
              break;
4,158✔
1520
            }
1521
          }
1522
        }
1523
        mndReleaseDb(pMnode, pDb);
4,163✔
1524
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
26,660✔
1525
        void   *pCont = NULL;
7,259✔
1526
        int32_t contLen = 0;
7,259✔
1527
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
7,259✔
1528
          mndTransSetRpcRsp(pTrans, pCont, contLen);
7,257✔
1529
        }
1530
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
19,401✔
1531
        void   *pCont = NULL;
14✔
1532
        int32_t contLen = 0;
14✔
1533
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
14!
1534
          mndTransSetRpcRsp(pTrans, pCont, contLen);
14✔
1535
        }
1536
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
19,387✔
1537
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
34✔
1538
        if (code != 0) {
34!
1539
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1540
        }
1541
      }
1542

1543
      if (pTrans->rpcRspLen != 0) {
30,823✔
1544
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
17,712✔
1545
        if (rpcCont != NULL) {
17,712!
1546
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
17,712✔
1547
          rspMsg.pCont = rpcCont;
17,712✔
1548
          rspMsg.contLen = pTrans->rpcRspLen;
17,712✔
1549
        }
1550
      }
1551

1552
      tmsgSendRsp(&rspMsg);
30,823✔
1553

1554
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,823!
1555
            mndTransStr(pTrans->stage), pInfo->ahandle);
1556
    }
1557
  }
1558
  taosArrayClear(pTrans->pRpcArray);
32,188✔
1559
  taosWUnLockLatch(&pTrans->lockRpcArray);
32,188✔
1560
}
1561

1562
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
59,556✔
1563
  int32_t code = 0;
59,556✔
1564
  SMnode *pMnode = pRsp->info.node;
59,556✔
1565
#ifndef TD_ASTRA_32
1566
  int64_t signature = (int64_t)(pRsp->info.ahandle);
59,556✔
1567
  int32_t transId = (int32_t)(signature >> 32);
59,556✔
1568
  int32_t action = (int32_t)((signature << 32) >> 32);
59,556✔
1569
#else
1570
  int32_t transId = (int32_t)(pRsp->info.ahandle);
1571
  int32_t action = (int32_t)(pRsp->info.ahandleEx);
1572
#endif
1573
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
59,556✔
1574
  if (pTrans == NULL) {
59,556!
1575
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1576
    if (terrno != 0) code = terrno;
×
1577
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1578
    goto _OVER;
×
1579
  }
1580

1581
  SArray *pArray = NULL;
59,556✔
1582
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
59,556✔
1583
    pArray = pTrans->redoActions;
59,548✔
1584
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
8!
1585
    pArray = pTrans->undoActions;
8✔
1586
  } else {
1587
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1588
    goto _OVER;
×
1589
  }
1590

1591
  if (pArray == NULL) {
59,556!
1592
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1593
    goto _OVER;
×
1594
  }
1595

1596
  int32_t actionNum = taosArrayGetSize(pArray);
59,556✔
1597
  if (action < 0 || action >= actionNum) {
59,556!
1598
    mError("trans:%d, invalid action:%d", transId, action);
×
1599
    goto _OVER;
×
1600
  }
1601

1602
  STransAction *pAction = taosArrayGet(pArray, action);
59,556✔
1603
  if (pAction != NULL) {
59,556!
1604
    if (pAction->msgSent) {
59,556!
1605
      pAction->msgReceived = 1;
59,556✔
1606
      pAction->errCode = pRsp->code;
59,556✔
1607
      pAction->endTime = taosGetTimestampMs();
59,556✔
1608

1609
      // pTrans->lastErrorNo = pRsp->code;
1610
      mndSetTransLastAction(pTrans, pAction);
59,556✔
1611

1612
      mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
59,556!
1613
            mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1614
            tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1615
    } else {
1616
      mWarn("trans:%d, %s:%d response is received, but msgSent is false, code:0x%x(%s), accept:0x%x(%s) retry:0x%x", 
×
1617
            transId, mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code),
1618
            pAction->acceptableCode, tstrerror(pAction->acceptableCode), pAction->retryCode);
1619
    }
1620

1621
  } else {
1622
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1623
  }
1624

1625
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
59,556!
1626
  mndTransExecute(pMnode, pTrans, true);
59,556✔
1627

1628
_OVER:
59,556✔
1629
  mndReleaseTrans(pMnode, pTrans);
59,556✔
1630
  TAOS_RETURN(code);
59,556✔
1631
}
1632

1633
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
4,465✔
1634
  pAction->rawWritten = 0;
4,465✔
1635
  pAction->msgSent = 0;
4,465✔
1636
  pAction->msgReceived = 0;
4,465✔
1637
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
4,465!
1638
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
4,465✔
1639
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
5✔
1640
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
5!
1641
          pAction->id, pAction->epSet.inUse);
1642
  } else {
1643
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,460!
1644
  }
1645
  //  pAction->errCode = 0;
1646
}
4,465✔
1647

1648
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
2✔
1649
  int32_t numOfActions = taosArrayGetSize(pArray);
2✔
1650

1651
  for (int32_t action = 0; action < numOfActions; ++action) {
10✔
1652
    STransAction *pAction = taosArrayGet(pArray, action);
8✔
1653
    if (pAction->msgSent && pAction->msgReceived &&
8!
1654
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
8!
1655
      continue;
6✔
1656
    if (pAction->msgSent && !pAction->msgReceived) {
2!
1657
      int64_t timestamp = taosGetTimestampMs();
×
1658
      if (timestamp - pAction->startTime <= TRANS_ACTION_TIMEOUT) continue;
×
1659
    }
1660

1661
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
2!
1662

1663
    mndTransResetAction(pMnode, pTrans, pAction);
2✔
1664
    mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage), pAction->id,
2!
1665
          pAction->errCode, pAction->startTime);
1666
  }
1667
}
2✔
1668

1669
// execute in sync context
1670
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
483,043✔
1671
  if (pAction->rawWritten) return 0;
483,043✔
1672
  if (topHalf) {
267,291✔
1673
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
10✔
1674
  }
1675

1676
  if (pAction->pRaw->type >= SDB_MAX) {
267,281!
1677
    pAction->rawWritten = true;
×
1678
    pAction->errCode = 0;
×
1679
    mndSetTransLastAction(pTrans, pAction);
×
1680
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
1681
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1682
  }
1683

1684
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
267,281✔
1685
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
267,281!
1686
    pAction->rawWritten = true;
267,281✔
1687
    pAction->errCode = 0;
267,281✔
1688
    code = 0;
267,281✔
1689
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
267,281!
1690
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1691

1692
    mndSetTransLastAction(pTrans, pAction);
267,281✔
1693
  } else {
1694
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1695
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1696
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1697
    mndSetTransLastAction(pTrans, pAction);
×
1698
  }
1699

1700
  TAOS_RETURN(code);
267,281✔
1701
}
1702

1703
// execute in trans context
1704
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
261,942✔
1705
                                     bool notSend) {
1706
  if (pAction->msgSent) return 0;
261,942✔
1707
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
97,147✔
1708
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
37,574✔
1709
  }
1710

1711
  if (notSend) {
59,573✔
1712
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
12!
1713
    return 0;
12✔
1714
  }
1715

1716
#ifndef TD_ASTRA_32
1717
  int64_t signature = pTrans->id;
59,561✔
1718
  signature = (signature << 32);
59,561✔
1719
  signature += pAction->id;
59,561✔
1720

1721
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
59,561✔
1722
#else
1723
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1724
                    .contLen = pAction->contLen,
1725
                    .info.ahandle = (void *)pTrans->id,
1726
                    .info.ahandleEx = (void *)pAction->id};
1727
#endif
1728
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
59,561✔
1729
  if (rpcMsg.pCont == NULL) {
59,561!
1730
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1731
    return -1;
×
1732
  }
1733
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
59,561✔
1734
  rpcMsg.info.notFreeAhandle = 1;
59,561✔
1735

1736
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
59,561✔
1737

1738
  char    detail[1024] = {0};
59,561✔
1739
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
59,561!
1740
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
59,561✔
1741
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
126,727✔
1742
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
67,166✔
1743
                     pAction->epSet.eps[i].port);
67,166✔
1744
  }
1745

1746
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
59,561✔
1747
  if (code == 0) {
59,561✔
1748
    pAction->msgSent = 1;
59,560✔
1749
    // pAction->msgReceived = 0;
1750
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
59,560✔
1751
    pAction->startTime = taosGetTimestampMs();
59,560✔
1752
    pAction->endTime = 0;
59,560✔
1753
    mInfo("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
59,560!
1754

1755
    mndSetTransLastAction(pTrans, pAction);
59,560✔
1756
  } else {
1757
    pAction->msgSent = 0;
1✔
1758
    pAction->msgReceived = 0;
1✔
1759
    pAction->errCode = (terrno != 0) ? terrno : code;
1!
1760
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
1!
1761
           detail);
1762

1763
    mndSetTransLastAction(pTrans, pAction);
1✔
1764
  }
1765

1766
  TAOS_RETURN(code);
59,561✔
1767
}
1768

1769
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1770
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1771
  pAction->rawWritten = 0;
×
1772
  pAction->errCode = 0;
×
1773
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1774

1775
  mndSetTransLastAction(pTrans, pAction);
×
1776
  return 0;
×
1777
}
1778

1779
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
744,985✔
1780
                                        bool notSend) {
1781
  if (pAction->actionType == TRANS_ACTION_RAW) {
744,985✔
1782
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
483,043✔
1783
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
261,942!
1784
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
261,942✔
1785
  } else {
1786
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1787
  }
1788
}
1789

1790
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
215,072✔
1791
  int32_t numOfActions = taosArrayGetSize(pArray);
215,072✔
1792
  int32_t code = 0;
215,072✔
1793

1794
  for (int32_t action = 0; action < numOfActions; ++action) {
866,169✔
1795
    STransAction *pAction = taosArrayGet(pArray, action);
681,491✔
1796
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
681,491✔
1797
    if (code != 0) {
681,491✔
1798
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
30,394!
1799
            numOfActions);
1800
      break;
30,394✔
1801
    }
1802
  }
1803

1804
  return code;
215,072✔
1805
}
1806

1807
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
266,697✔
1808
  int32_t numOfActions = taosArrayGetSize(pArray);
266,697✔
1809
  int32_t code = 0;
266,697✔
1810
  if (numOfActions == 0) return 0;
266,697✔
1811

1812
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
215,072✔
1813
    return code;
30,394✔
1814
  }
1815

1816
  int32_t       numOfExecuted = 0;
184,678✔
1817
  int32_t       errCode = 0;
184,678✔
1818
  STransAction *pErrAction = NULL;
184,678✔
1819
  for (int32_t action = 0; action < numOfActions; ++action) {
835,775✔
1820
    STransAction *pAction = taosArrayGet(pArray, action);
651,097✔
1821
    if (pAction->msgReceived || pAction->rawWritten) {
651,097✔
1822
      numOfExecuted++;
549,180✔
1823
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
549,180✔
1824
        errCode = pAction->errCode;
4✔
1825
        pErrAction = pAction;
4✔
1826
      }
1827
    } else {
1828
      pErrAction = pAction;
101,917✔
1829
    }
1830
  }
1831

1832
  mndSetTransLastAction(pTrans, pErrAction);
184,678✔
1833

1834
  if (numOfExecuted == numOfActions) {
184,678✔
1835
    if (errCode == 0) {
133,836✔
1836
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
133,834!
1837
      return 0;
133,834✔
1838
    } else {
1839
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
2!
1840
      mndTransResetActions(pMnode, pTrans, pArray);
2✔
1841
      terrno = errCode;
2✔
1842
      return errCode;
2✔
1843
    }
1844
  } else {
1845
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
50,842!
1846

1847
    for (int32_t action = 0; action < numOfActions; ++action) {
203,067✔
1848
      STransAction *pAction = taosArrayGet(pArray, action);
152,225✔
1849
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
152,225✔
1850
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1851
             pAction->acceptableCode, pAction->retryCode);
1852
      if (pAction->msgSent) {
152,225✔
1853
        bool reset = false;
152,213✔
1854
        if (pAction->msgReceived) {
152,213✔
1855
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
50,308✔
1856
        } else {
1857
          int64_t timestamp = taosGetTimestampMs();
101,905✔
1858
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
101,905!
1859
        }
1860
        if (reset) {
152,213✔
1861
          mndTransResetAction(pMnode, pTrans, pAction);
2✔
1862
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2!
1863
                pAction->id, pAction->errCode, pAction->startTime);
1864
        }
1865
      }
1866
    }
1867
    return TSDB_CODE_ACTION_IN_PROGRESS;
50,842✔
1868
  }
1869
}
1870

1871
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
157,197✔
1872
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
157,197✔
1873
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
157,197✔
1874
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
2!
1875
           mndStrExecutionContext(topHalf));
1876
  }
1877
  return code;
157,197✔
1878
}
1879

1880
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
14✔
1881
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf, notSend);
14✔
1882
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
14!
1883
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1884
           mndStrExecutionContext(topHalf));
1885
  }
1886
  return code;
14✔
1887
}
1888

1889
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
109,486✔
1890
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
109,486✔
1891
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
109,486!
1892
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1893
           mndStrExecutionContext(topHalf));
1894
  }
1895
  return code;
109,486✔
1896
}
1897

1898
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
19,374✔
1899
  int32_t code = 0;
19,374✔
1900
  int32_t numOfActions = taosArrayGetSize(pActions);
19,374✔
1901
  if (numOfActions == 0) return code;
19,374✔
1902

1903
  if (pTrans->actionPos >= numOfActions) {
19,370✔
1904
    return code;
1,516✔
1905
  }
1906

1907
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
17,854!
1908
        pTrans->actionPos, mndTransStr(pTrans->stage));
1909

1910
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
27,852✔
1911
    STransAction *pAction = taosArrayGet(pActions, action);
26,613✔
1912

1913
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
26,613✔
1914
      code = TSDB_CODE_ACTION_IN_PROGRESS;
20✔
1915
      break;
16,615✔
1916
    }
1917

1918
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
26,593!
1919
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
1920

1921
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
26,593✔
1922
    if (code == 0) {
26,593✔
1923
      if (pAction->msgSent) {
22,279✔
1924
        bool reset = false;
18,064✔
1925
        if (pAction->msgReceived) {
18,064✔
1926
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
8,051!
1927
            code = pAction->errCode;
4,370✔
1928
            reset = true;
4,370✔
1929
          } else {
1930
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
3,681!
1931
          }
1932
        } else {
1933
          int64_t timestamp = taosGetTimestampMs();
10,013✔
1934
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
10,013!
1935
          code = TSDB_CODE_ACTION_IN_PROGRESS;
10,013✔
1936
        }
1937
        if (reset) {
18,064✔
1938
          mndTransResetAction(pMnode, pTrans, pAction);
4,370✔
1939
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
4,370!
1940
                pAction->id, pAction->errCode, pAction->startTime);
1941
        }
1942
      } else if (pAction->rawWritten) {
4,215!
1943
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
4,215!
1944
          code = pAction->errCode;
×
1945
        } else {
1946
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,215!
1947
        }
1948
      } else {
1949
      }
1950
    }
1951

1952
    if (code == 0) {
26,593✔
1953
      pTrans->failedTimes = 0;
7,896✔
1954
    }
1955
    mndSetTransLastAction(pTrans, pAction);
26,593✔
1956
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
26,593✔
1957
      mInfo(
14,326!
1958
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1959
          "msgReceived:%d",
1960
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1961
          pAction->msgSent, pAction->msgReceived);
1962
    } else if (code != 0) {
12,267✔
1963
      mError(
4,371!
1964
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1965
          "msgReceived:%d",
1966
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1967
          pAction->msgSent, pAction->msgReceived);
1968
    }
1969

1970
    char str[200] = {0};
26,593✔
1971
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
26,593✔
1972
      pTrans->lastErrorNo = code;
6,572✔
1973
      pTrans->code = code;
6,572✔
1974
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
6,572!
1975
            action, str);
1976
      break;
6,572✔
1977
    }
1978

1979
    if (code == 0) {
20,021✔
1980
      pTrans->code = 0;
5,637✔
1981
      pTrans->actionPos++;
5,637✔
1982
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
5,637!
1983
            pAction->id);
1984
      (void)taosThreadMutexUnlock(&pTrans->mutex);
5,637✔
1985
      code = mndTransSync(pMnode, pTrans);
5,637✔
1986
      (void)taosThreadMutexLock(&pTrans->mutex);
5,637✔
1987
      if (code != 0) {
5,637!
1988
        pTrans->actionPos--;
×
1989
        pTrans->code = terrno;
×
1990
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1991
               mndTransStr(pAction->stage), pAction->id, terrstr());
1992
        break;
×
1993
      }
1994
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
14,384✔
1995
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
10,013!
1996
      break;
10,013✔
1997
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,371!
1998
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
15✔
1999
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,361!
2000
            code, tstrerror(code));
2001
      pTrans->lastErrorNo = code;
4,361✔
2002
      taosMsleep(300);
4,361✔
2003
      action--;
4,361✔
2004
      continue;
4,361✔
2005
    } else {
2006
      terrno = code;
10✔
2007
      pTrans->lastErrorNo = code;
10✔
2008
      pTrans->code = code;
10✔
2009
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
10!
2010
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2011
      break;
10✔
2012
    }
2013
  }
2014

2015
  return code;
17,854✔
2016
}
2017

2018
static int32_t mndTransExecuteActionsSerialGroup(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf,
9,023✔
2019
                                                 int32_t groupId, int32_t currentGroup, int32_t groupCount,
2020
                                                 bool notSend, SHashObj *pHash) {
2021
  int32_t code = 0;
9,023✔
2022
  int32_t numOfActions = taosArrayGetSize(pActions);
9,023✔
2023
  if (numOfActions == 0) return code;
9,023!
2024

2025
  if (groupId <= 0) {
9,023!
2026
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
2027
    return TSDB_CODE_INTERNAL_ERROR;
×
2028
  }
2029

2030
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &groupId, sizeof(int32_t));
9,023✔
2031
  if (actionPos == NULL) {
9,023!
2032
    mError("trans:%d, failed to execute action in serail group, actionPos is null at group %d", pTrans->id, groupId);
×
2033
    return TSDB_CODE_INTERNAL_ERROR;
×
2034
  }
2035

2036
  if (*actionPos >= numOfActions) {
9,023✔
2037
    mError("trans:%d, failed to execute action in serail group, actionPos:%d >= numOfActions:%d at group %d",
1,683!
2038
           pTrans->id, *actionPos, numOfActions, groupId);
2039
    return TSDB_CODE_INTERNAL_ERROR;
1,683✔
2040
  }
2041

2042
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
8,541✔
2043
    STransAction **ppAction = taosArrayGet(pActions, action);
8,052✔
2044
    STransAction  *pAction = *ppAction;
8,052✔
2045

2046
    if (notSend && !pAction->msgSent) {
8,052✔
2047
      mInfo(
1,112!
2048
          "trans:%d, %s:%d (%d/%d at group %d) skip to execute, curent state(actionType(1:msg,2:log):%d, "
2049
          "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2050
          pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2051
          pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2052
      code = TSDB_CODE_ACTION_IN_PROGRESS;
1,112✔
2053
      break;
6,851✔
2054
    }
2055

2056
    mInfo(
6,940!
2057
        "trans:%d, %s:%d (%d/%d at group %d) begin to execute, curent state(actionType(1:msg,2:log):%d, "
2058
        "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2059
        pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2060
        pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2061

2062
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
6,940✔
2063
    if (code == 0) {
6,940✔
2064
      if (pAction->msgSent) {
4,063✔
2065
        if (pAction->msgReceived) {
3,644✔
2066
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,066!
2067
            code = pAction->errCode;
91✔
2068
            mndTransResetAction(pMnode, pTrans, pAction);
91✔
2069
          } else {
2070
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
975!
2071
                  pAction->id, action, numOfActions, groupId);
2072
          }
2073
        } else {
2074
          code = TSDB_CODE_ACTION_IN_PROGRESS;
2,578✔
2075
        }
2076
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
3,644✔
2077
        if (msgSent != NULL) {
3,644!
2078
          *msgSent = pAction->msgSent;
3,644✔
2079
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
3,644!
2080
        } else {
2081
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2082
        }
2083
      } else if (pAction->rawWritten) {
419!
2084
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
419!
2085
          code = pAction->errCode;
×
2086
        } else {
2087
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
419!
2088
        }
2089
      } else {
2090
      }
2091
    }
2092

2093
    if (code == 0) {
6,940✔
2094
      pTrans->failedTimes = 0;
1,394✔
2095
    }
2096
    mndSetTransLastAction(pTrans, pAction);
6,940✔
2097

2098
    char str[200] = {0};
6,940✔
2099
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
6,940✔
2100
      pTrans->lastErrorNo = code;
3,161✔
2101
      pTrans->code = code;
3,161✔
2102
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
3,161✔
2103
        mInfo(
2,877!
2104
            "trans:%d, %s:%d (%d/%d at group %d) not able to execute since %s, current state("
2105
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2106
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2107
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2108
      } else if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
284!
2109
        mError(
×
2110
            "trans:%d, %s:%d (%d/%d at group %d) failed to execute since %s, current action("
2111
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2112
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2113
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2114
      }
2115
      mInfo("trans:%d, %s:%d (%d/%d at group %d) cannot execute next action, stop this group execution, %s", pTrans->id,
3,161!
2116
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2117
      break;
3,161✔
2118
    }
2119

2120
    if (code == 0) {
3,779✔
2121
      pTrans->code = 0;
1,110✔
2122
      pTrans->actionPos++;
1,110✔
2123
      (*actionPos)++;
1,110✔
2124
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
1,110!
2125
            pAction->id);
2126
      (void)taosThreadMutexUnlock(&pTrans->mutex);
1,110✔
2127
      code = mndTransSync(pMnode, pTrans);
1,110✔
2128
      (void)taosThreadMutexLock(&pTrans->mutex);
1,110✔
2129
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
1,110!
2130
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
24,746✔
2131
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
23,636✔
2132
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
23,636✔
2133
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
23,636✔
2134
          pTmpAction->msgSent = *msgSent;
1,318✔
2135
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
1,318!
2136
        }
2137
      }
2138
      if (code != 0) {
1,110!
2139
        pTrans->actionPos--;
×
2140
        (*actionPos)--;
×
2141
        pTrans->code = terrno;
×
2142
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2143
               mndTransStr(pAction->stage), pAction->id, terrstr());
2144
        break;
×
2145
      }
2146
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,669✔
2147
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
2,578!
2148
            mndTransStr(pAction->stage), pAction->id);
2149
      break;
2,578✔
2150
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
91!
2151
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
×
2152
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
91!
2153
            code, tstrerror(code));
2154
      pTrans->lastErrorNo = code;
91✔
2155
      taosMsleep(300);
91✔
2156
      action--;
91✔
2157
      continue;
91✔
2158
    } else {
2159
      terrno = code;
×
2160
      pTrans->lastErrorNo = code;
×
2161
      pTrans->code = code;
×
2162
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2163
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2164
      break;
×
2165
    }
2166
  }
2167

2168
  return code;
7,340✔
2169
}
2170

2171
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
19,197✔
2172
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
19,197✔
2173
  (void)taosThreadMutexLock(&pTrans->mutex);
19,197✔
2174
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
19,197!
2175
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
19,197✔
2176
  }
2177
  (void)taosThreadMutexUnlock(&pTrans->mutex);
19,197✔
2178
  return code;
19,197✔
2179
}
2180

2181
static int32_t mndTransExecuteRedoActionGroup(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
2,746✔
2182
  int32_t total_code = TSDB_CODE_ACTION_IN_PROGRESS;
2,746✔
2183
  int32_t code = 0;
2,746✔
2184
  int32_t successCount = 0;
2,746✔
2185
  int32_t groupCount = taosHashGetSize(pTrans->redoGroupActions);
2,746✔
2186

2187
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
2,746✔
2188
  if(pHash == NULL){
2,746!
2189
    mError("trans:%d, failed to init hash since %s", pTrans->id, terrstr());
×
2190
    return -1;
×
2191
  }
2192
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
59,317✔
2193
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
56,571✔
2194
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
56,571✔
2195
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
56,571!
2196
  }
2197
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
2,746!
2198

2199
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
2,746!
2200
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
2,746✔
2201
  int32_t currentGroup = 1;
2,746✔
2202
  while (pIter) {
11,769✔
2203
    SArray **redoActions = pIter;
9,023✔
2204
    size_t   keyLen = 0;
9,023✔
2205
    int32_t *key = taosHashGetKey(pIter, &keyLen);
9,023✔
2206
    int32_t  actionCount = taosArrayGetSize(*redoActions);
9,023✔
2207
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
9,023!
2208
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2209
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
9,023✔
2210
                                             notSend, pHash);
2211
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
9,023✔
2212
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
2,877!
2213
            tstrerror(code));
2214
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
6,146✔
2215
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
3,690!
2216
    } else if (code != 0) {
2,456✔
2217
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,683!
2218
             tstrerror(code));
2219
    } else {
2220
      successCount++;
773✔
2221
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
773!
2222
    }
2223
    currentGroup++;
9,023✔
2224
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
9,023✔
2225
  }
2226

2227
  taosHashCleanup(pHash);
2,746✔
2228

2229
  if (successCount == groupCount) {
2,746✔
2230
    total_code = 0;
63✔
2231
  } else {
2232
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
2,683!
2233
  }
2234

2235
  return total_code;
2,746✔
2236
}
2237

2238
static int32_t mndTransExecuteRedoActionsParallel(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
3,293✔
2239
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
3,293✔
2240
  (void)taosThreadMutexLock(&pTrans->mutex);
3,293✔
2241

2242
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
3,293!
2243
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
3,293✔
2244
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
3,293!
2245
      code = 0;
370✔
2246
    } else {
2247
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
2,923✔
2248
      if (pAction != NULL && pAction->groupId == -1) {
2,923!
2249
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
177✔
2250
      } else {
2251
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
2,746✔
2252
        if (code == 0) {
2,746✔
2253
          if (pTrans->actionPos < numOfActions) {
63✔
2254
            code = TSDB_CODE_ACTION_IN_PROGRESS;
46✔
2255
          }
2256
        }
2257
      }
2258
    }
2259
  }
2260

2261
  (void)taosThreadMutexUnlock(&pTrans->mutex);
3,293✔
2262

2263
  return code;
3,293✔
2264
}
2265

2266
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
2267
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
2268
  (void)taosThreadMutexLock(&pTrans->mutex);
×
2269
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2270
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
2271
  }
2272
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
2273
  return code;
×
2274
}
2275

2276
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
59,473✔
2277
  bool    continueExec = true;
59,473✔
2278
  int32_t code = 0;
59,473✔
2279
  terrno = 0;
59,473✔
2280

2281
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
59,473✔
2282
  if (numOfActions == 0) goto _OVER;
59,473✔
2283

2284
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
20,359!
2285

2286
  for (int32_t action = 0; action < numOfActions; ++action) {
50,320✔
2287
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
29,961✔
2288
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
29,961✔
2289
    if (code != 0) {
29,961!
2290
      terrno = code;
×
2291
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
2292
             numOfActions, tstrerror(code));
2293
      return false;
×
2294
    }
2295
  }
2296

2297
_OVER:
20,359✔
2298
  pTrans->stage = TRN_STAGE_REDO_ACTION;
59,473✔
2299
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
59,473!
2300
  return continueExec;
59,473✔
2301
}
2302

2303
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
179,687✔
2304
  bool    continueExec = true;
179,687✔
2305
  int32_t code = 0;
179,687✔
2306
  terrno = 0;
179,687✔
2307

2308
  if (pTrans->exec == TRN_EXEC_SERIAL) {
179,687✔
2309
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
19,197✔
2310
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
160,490✔
2311
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
157,197✔
2312
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
3,293!
2313
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
3,293✔
2314
  } else {
2315
    code = TSDB_CODE_INTERNAL_ERROR;
×
2316
  }
2317

2318
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
179,699!
2319
      mndTransIsInSyncContext(topHalf)) {
12✔
2320
    pTrans->lastErrorNo = code;
×
2321
    pTrans->code = code;
×
2322
    mInfo(
×
2323
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2324
        "continueExec:%d, code:%s",
2325
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2326
    taosMsleep(100);
×
2327
    return true;
×
2328
  } else {
2329
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
179,687✔
2330
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
67,214!
2331
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2332
      return false;
67,214✔
2333
    }
2334
  }
2335
  terrno = code;
112,473✔
2336

2337
  if (code == 0) {
112,473✔
2338
    pTrans->code = 0;
50,030✔
2339
    pTrans->stage = TRN_STAGE_COMMIT;
50,030✔
2340
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
50,030!
2341
    continueExec = true;
50,030✔
2342
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
62,443!
2343
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
62,431!
2344
    continueExec = false;
62,431✔
2345
  } else {
2346
    pTrans->failedTimes++;
12✔
2347
    pTrans->code = terrno;
12✔
2348
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
12✔
2349
      if (pTrans->lastAction != 0) {
2!
2350
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
2351
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
2352
          if (pTrans->failedTimes < 6) {
×
2353
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2354
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
2355
            taosMsleep(1000);
×
2356
            continueExec = true;
×
2357
            return true;
×
2358
          }
2359
        }
2360
      }
2361

2362
      pTrans->stage = TRN_STAGE_ROLLBACK;
2✔
2363
      pTrans->actionPos = 0;
2✔
2364
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
2!
2365
             pTrans->actionPos);
2366
      continueExec = true;
2✔
2367
    } else {
2368
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
10!
2369
      continueExec = false;
10✔
2370
    }
2371
  }
2372

2373
  return continueExec;
112,473✔
2374
}
2375

2376
// execute in trans context
2377
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
50,030✔
2378
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
50,030!
2379

2380
  bool    continueExec = true;
50,030✔
2381
  int32_t code = mndTransCommit(pMnode, pTrans);
50,030✔
2382

2383
  if (code == 0) {
50,030✔
2384
    pTrans->code = 0;
50,025✔
2385
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
50,025✔
2386
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
50,025!
2387
    continueExec = true;
50,025✔
2388
  } else {
2389
    pTrans->code = terrno;
5✔
2390
    pTrans->failedTimes++;
5✔
2391
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
5!
2392
    continueExec = false;
5✔
2393
  }
2394

2395
  return continueExec;
50,030✔
2396
}
2397

2398
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
109,486✔
2399
  bool    continueExec = true;
109,486✔
2400
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
109,486✔
2401

2402
  if (code == 0) {
109,486!
2403
    pTrans->code = 0;
109,486✔
2404
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
109,486✔
2405
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
109,486!
2406
    continueExec = true;
109,486✔
2407
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
2408
    pTrans->code = 0;
×
2409
    pTrans->stage = TRN_STAGE_COMMIT;
×
2410
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
2411
    continueExec = true;
×
2412
  } else {
2413
    pTrans->code = terrno;
×
2414
    pTrans->failedTimes++;
×
2415
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2416
    continueExec = false;
×
2417
  }
2418

2419
  return continueExec;
109,486✔
2420
}
2421

2422
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
14✔
2423
  bool    continueExec = true;
14✔
2424
  int32_t code = 0;
14✔
2425

2426
  if (pTrans->exec == TRN_EXEC_SERIAL) {
14!
2427
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
2428
  } else {
2429
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
14✔
2430
  }
2431

2432
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
14✔
2433
  terrno = code;
12✔
2434

2435
  if (code == 0) {
12✔
2436
    pTrans->stage = TRN_STAGE_PRE_FINISH;
2✔
2437
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
2!
2438
    continueExec = true;
2✔
2439
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
10!
2440
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
10!
2441
    continueExec = false;
10✔
2442
  } else {
2443
    pTrans->failedTimes++;
×
2444
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2445
    continueExec = false;
×
2446
  }
2447

2448
  return continueExec;
12✔
2449
}
2450

2451
// in trans context
2452
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
2✔
2453
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
2!
2454

2455
  bool    continueExec = true;
2✔
2456
  int32_t code = mndTransRollback(pMnode, pTrans);
2✔
2457

2458
  if (code == 0) {
2!
2459
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
2✔
2460
    continueExec = true;
2✔
2461
  } else {
2462
    pTrans->failedTimes++;
×
2463
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2464
    continueExec = false;
×
2465
  }
2466

2467
  return continueExec;
2✔
2468
}
2469

2470
// excute in trans context
2471
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
2✔
2472
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
2!
2473

2474
  bool    continueExec = true;
2✔
2475
  int32_t code = mndTransPreFinish(pMnode, pTrans);
2✔
2476

2477
  if (code == 0) {
2!
2478
    pTrans->stage = TRN_STAGE_FINISH;
2✔
2479
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
2!
2480
    continueExec = true;
2✔
2481
  } else {
2482
    pTrans->failedTimes++;
×
2483
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2484
    continueExec = false;
×
2485
  }
2486

2487
  return continueExec;
2✔
2488
}
2489

2490
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
109,490✔
2491
  bool continueExec = false;
109,490✔
2492
  if (topHalf) return continueExec;
109,490✔
2493

2494
  SSdbRaw *pRaw = mndTransEncode(pTrans);
59,463✔
2495
  if (pRaw == NULL) {
59,463!
2496
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
2497
    return false;
×
2498
  }
2499
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
59,463!
2500

2501
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
59,463✔
2502
  if (code != 0) {
59,463!
2503
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
2504
  }
2505

2506
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
59,463!
2507
        pTrans->failedTimes, pTrans->createdTime);
2508
  return continueExec;
59,463✔
2509
}
2510

2511
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
239,162✔
2512
  bool continueExec = true;
239,162✔
2513

2514
  while (continueExec) {
687,873✔
2515
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
448,711!
2516
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2517
    pTrans->lastExecTime = taosGetTimestampMs();
448,711✔
2518
    switch (pTrans->stage) {
448,711!
2519
      case TRN_STAGE_PREPARE:
×
2520
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2521
        break;
×
2522
      case TRN_STAGE_REDO_ACTION:
179,687✔
2523
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
179,687✔
2524
        break;
179,687✔
2525
      case TRN_STAGE_COMMIT:
50,030✔
2526
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
50,030✔
2527
        break;
50,030✔
2528
      case TRN_STAGE_COMMIT_ACTION:
109,486✔
2529
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
109,486✔
2530
        break;
109,486✔
2531
      case TRN_STAGE_ROLLBACK:
2✔
2532
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
2✔
2533
        break;
2✔
2534
      case TRN_STAGE_UNDO_ACTION:
14✔
2535
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
14✔
2536
        break;
14✔
2537
      case TRN_STAGE_PRE_FINISH:
2✔
2538
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
2✔
2539
        break;
2✔
2540
      case TRN_STAGE_FINISH:
109,490✔
2541
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
109,490✔
2542
        break;
109,490✔
2543
      default:
×
2544
        continueExec = false;
×
2545
        break;
×
2546
    }
2547
  }
2548

2549
  mndTransSendRpcRsp(pMnode, pTrans);
239,162✔
2550
}
239,162✔
2551

2552
// start trans, pullup, receive rsp, kill
2553
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
112,595✔
2554
  bool topHalf = true;
112,595✔
2555
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
112,595✔
2556
}
112,595✔
2557

2558
// update trans
2559
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
126,567✔
2560
  bool topHalf = false;
126,567✔
2561
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
126,567✔
2562
}
126,567✔
2563

2564
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
29,576✔
2565
  mTrace("start to process trans timer");
29,576✔
2566
  mndTransPullup(pReq->info.node);
29,576✔
2567
  return 0;
29,576✔
2568
}
2569

2570
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
2571
  SArray *pArray = NULL;
×
2572
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
2573
    pArray = pTrans->redoActions;
×
2574
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2575
    pArray = pTrans->undoActions;
×
2576
  } else {
2577
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2578
  }
2579

2580
  if(!tsForceKillTrans){
×
2581
    if(pTrans->ableToBeKilled == false){
×
2582
      return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2583
    }
2584
  }
2585
  
2586
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
×
2587
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
2588
      STransAction *pAction = taosArrayGet(pArray, i);
×
2589
      mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
2590
            mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
2591
      pAction->msgSent = 1;
×
2592
      pAction->msgReceived = 1;
×
2593
      pAction->errCode = 0;
×
2594
    }
2595
  }
2596
  else if(pTrans->killMode == TRN_KILL_MODE_INTERUPT){
×
2597
    pTrans->stage = TRN_STAGE_PRE_FINISH;
×
2598
  }
2599
  else{
2600
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2601
  }
2602

2603
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
×
2604
  mndTransExecute(pMnode, pTrans, true);
×
2605
  return 0;
×
2606
}
2607

2608
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
2✔
2609
  SMnode       *pMnode = pReq->info.node;
2✔
2610
  SKillTransReq killReq = {0};
2✔
2611
  int32_t       code = -1;
2✔
2612
  STrans       *pTrans = NULL;
2✔
2613

2614
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
2!
2615
    code = TSDB_CODE_INVALID_MSG;
×
2616
    goto _OVER;
×
2617
  }
2618

2619
  mInfo("trans:%d, start to kill, force:%d", killReq.transId, tsForceKillTrans);
2!
2620
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
2!
2621
    goto _OVER;
2✔
2622
  }
2623

2624
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
2625
  if (pTrans == NULL) {
×
2626
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
2627
    if (terrno != 0) code = terrno;
×
2628
    goto _OVER;
×
2629
  }
2630

2631
  code = mndKillTrans(pMnode, pTrans);
×
2632

2633
_OVER:
2✔
2634
  if (code != 0) {
2!
2635
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
2!
2636
  }
2637

2638
  mndReleaseTrans(pMnode, pTrans);
2✔
2639
  TAOS_RETURN(code);
2✔
2640
}
2641

2642
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
2✔
2643

2644
void mndTransPullup(SMnode *pMnode) {
30,170✔
2645
  SSdb   *pSdb = pMnode->pSdb;
30,170✔
2646
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
30,170✔
2647
  if (pArray == NULL) return;
30,170!
2648

2649
  void *pIter = NULL;
30,170✔
2650
  while (1) {
3,003✔
2651
    STrans *pTrans = NULL;
33,173✔
2652
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
33,173✔
2653
    if (pIter == NULL) break;
33,173✔
2654
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
6,006!
2655
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2656
    }
2657
    sdbRelease(pSdb, pTrans);
3,003✔
2658
  }
2659

2660
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
30,170✔
2661

2662
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
33,173✔
2663
    int32_t *pTransId = taosArrayGet(pArray, i);
3,003✔
2664
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
3,003✔
2665
    if (pTrans != NULL) {
3,003!
2666
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
3,003!
2667
      mndTransExecute(pMnode, pTrans, false);
3,003✔
2668
    }
2669
    mndReleaseTrans(pMnode, pTrans);
3,003✔
2670
  }
2671
  taosArrayDestroy(pArray);
30,170✔
2672
}
2673

2674
static char *formatTimestamp(char *buf, int64_t val, int precision) {
41,202✔
2675
  time_t tt;
2676
  if (precision == TSDB_TIME_PRECISION_MICRO) {
41,202!
2677
    tt = (time_t)(val / 1000000);
×
2678
  }
2679
  if (precision == TSDB_TIME_PRECISION_NANO) {
41,202!
2680
    tt = (time_t)(val / 1000000000);
×
2681
  } else {
2682
    tt = (time_t)(val / 1000);
41,202✔
2683
  }
2684

2685
  struct tm tm;
2686
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
41,202!
2687
    mError("failed to get local time");
×
2688
    return NULL;
×
2689
  }
2690
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
41,202✔
2691

2692
  if (precision == TSDB_TIME_PRECISION_MICRO) {
41,202!
2693
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2694
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
41,202!
2695
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2696
  } else {
2697
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
41,202✔
2698
  }
2699

2700
  return buf;
41,202✔
2701
}
2702

2703
static void mndTransLogAction(STrans *pTrans) {
829✔
2704
  char    detail[512] = {0};
829✔
2705
  int32_t len = 0;
829✔
2706
  int32_t index = 0;
829✔
2707

2708
  if (pTrans->stage == TRN_STAGE_PREPARE) {
829!
2709
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2710
      len = 0;
×
2711
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2712
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2713
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2714
                      sdbStatusName(pAction->pRaw->status));
×
2715
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2716
    }
2717
  }
2718

2719
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
829!
2720
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
27,992✔
2721
      len = 0;
27,163✔
2722
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
27,163✔
2723
      if (pAction->actionType == TRANS_ACTION_MSG) {
27,163✔
2724
        char bufStart[40] = {0};
20,601✔
2725
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
20,601✔
2726

2727
        char endStart[40] = {0};
20,601✔
2728
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
20,601✔
2729
        len += snprintf(detail + len, sizeof(detail) - len,
41,202!
2730
                        "action:%d, %s:%d msgType:%s,"
2731
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2732
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
20,601✔
2733
                        pAction->msgReceived, bufStart, endStart);
20,601✔
2734

2735
        SEpSet epset = pAction->epSet;
20,601✔
2736
        if (epset.numOfEps > 0) {
20,601!
2737
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
20,601✔
2738
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
46,286✔
2739
            len +=
25,685✔
2740
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
25,685✔
2741
          }
2742
        }
2743

2744
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
20,601✔
2745
                        tstrerror(pAction->errCode));
2746
      } else {
2747
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
6,562✔
2748
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
6,562✔
2749
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
6,562✔
2750
      }
2751
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
27,163!
2752
    }
2753
  }
2754

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

2765
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
×
2766
      len = 0;
×
2767
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
2768
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
2769
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
2770
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2771
        ;
2772
      } else {
2773
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2774
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2775
                        sdbStatusName(pAction->pRaw->status));
×
2776
      }
2777
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2778
    }
2779
  }
2780
}
829✔
2781

2782
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
891✔
2783
  SMnode *pMnode = pReq->info.node;
891✔
2784
  SSdb   *pSdb = pMnode->pSdb;
891✔
2785
  int32_t numOfRows = 0;
891✔
2786
  STrans *pTrans = NULL;
891✔
2787
  int32_t cols = 0;
891✔
2788
  int32_t code = 0;
891✔
2789
  int32_t lino = 0;
891✔
2790

2791
  while (numOfRows < rows) {
1,720!
2792
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
1,720✔
2793
    if (pShow->pIter == NULL) break;
1,720✔
2794

2795
    cols = 0;
829✔
2796

2797
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2798
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
829!
2799

2800
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2801
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
829!
2802
                        _OVER);
2803

2804
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
829✔
2805
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
829✔
2806
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2807
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
829!
2808

2809
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
829✔
2810
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
829✔
2811
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2812
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
829!
2813

2814
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
829✔
2815
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
829✔
2816
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2817
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
829!
2818

2819
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
829✔
2820
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
829✔
2821
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2822
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
829!
2823

2824
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
829!
2825
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
829✔
2826
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
829✔
2827
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2828
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
829!
2829

2830
    /*
2831
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2832
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2833
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2834
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2835
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2836
    */
2837

2838
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2839
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
829!
2840
                        _OVER);
2841

2842
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2843
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
829!
2844
                        _OVER);
2845

2846
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
829✔
2847
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
829✔
2848
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
2,487✔
2849
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
829✔
2850
    SEpSet  epset = pTrans->lastEpset;
829✔
2851
    if (epset.numOfEps > 0) {
829✔
2852
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
1,646!
2853
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
1,646✔
2854
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
2,336✔
2855
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
1,513✔
2856
      }
2857
    }
2858
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
829✔
2859
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
829✔
2860
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
829!
2861

2862
    mndTransLogAction(pTrans);
829✔
2863

2864
    numOfRows++;
829✔
2865
    sdbRelease(pSdb, pTrans);
829✔
2866
  }
2867

2868
_OVER:
×
2869
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
891!
2870
  pShow->numOfRows += numOfRows;
891✔
2871
  return numOfRows;
891✔
2872
}
2873

2874
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
×
2875
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2876
  int32_t code = 0;
×
2877
  int32_t lino = 0;
×
2878
  int32_t len = 0;
×
2879

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

2883
  char action[30 + 1] = {0};
×
2884
  if (curActionId == pAction->id) {
×
2885
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
×
2886
                    mndTransTypeStr(pAction->actionType));
2887
  } else {
2888
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
×
2889
                    mndTransTypeStr(pAction->actionType));
2890
  }
2891
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
×
2892
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
×
2893
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
2894
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
×
2895
_OVER:
×
2896
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2897
  return code;
×
2898
}
2899

2900
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
×
2901
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2902
  int32_t code = 0;
×
2903
  int32_t lino = 0;
×
2904
  int32_t len = 0;
×
2905
  int32_t cols = 0;
×
2906

2907
  cols = 0;
×
2908

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

2911
  if (pAction->actionType == TRANS_ACTION_MSG) {
×
2912
    int32_t len = 0;
×
2913

2914
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2915
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
×
2916
                    pAction->msgSent, pAction->msgReceived);
×
2917
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2918
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2919
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2920
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2921

2922
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2923
    len = 0;
×
2924
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
×
2925
                    tstrerror(pAction->errCode));
2926
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2927
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2928
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2929
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2930

2931
    char target[TSDB_TRANS_TARGET_LEN] = {0};
×
2932
    len = 0;
×
2933
    SEpSet epset = pAction->epSet;
×
2934
    if (epset.numOfEps > 0) {
×
2935
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
×
2936
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
×
2937
      }
2938
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
×
2939
    }
2940
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2941
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2942
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2943
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2944

2945
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2946
    len = 0;
×
2947
    char bufStart[40] = {0};
×
2948
    if (pAction->startTime > 0) (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
×
2949
    char bufEnd[40] = {0};
×
2950
    if (pAction->endTime > 0) (void)formatTimestamp(bufEnd, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
×
2951
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
×
2952
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2953
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2954
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2955
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2956

2957
  } else {
2958
    int32_t len = 0;
×
2959

2960
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2961
    if (pAction->pRaw->type == SDB_VGROUP) {
×
2962
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
×
2963
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
×
2964
      len += snprintf(objType + len, sizeof(objType) - len, "%s(%d)", sdbTableName(pAction->pRaw->type), pVgroup->vgId);
×
2965
      taosMemoryFreeClear(pRow);
×
2966
    } else {
2967
      strcpy(objType, sdbTableName(pAction->pRaw->type));
×
2968
    }
2969
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2970
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2971
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2972
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2973

2974
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2975
    len = 0;
×
2976
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
×
2977
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2978
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2979
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2980
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2981

2982
    char target[TSDB_TRANS_TARGET_LEN] = "";
×
2983
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2984
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2985
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2986
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2987

2988
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2989
    len = 0;
×
2990
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
×
2991
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2992
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2993
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2994
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2995
  }
2996

2997
_OVER:
×
2998
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2999
}
3000

3001
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
×
3002
  if (stage == TRN_STAGE_PREPARE) {
×
3003
    return pTrans->prepareActions;
×
3004
  }
3005
  if (stage == TRN_STAGE_REDO_ACTION) {
×
3006
    return pTrans->redoActions;
×
3007
  }
3008
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
3009
    return pTrans->commitActions;
×
3010
  }
3011
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
3012
    return pTrans->undoActions;
×
3013
  }
3014
  return NULL;
×
3015
}
3016

3017
typedef struct STransDetailIter {
3018
  void     *pIter;
3019
  STrans   *pTrans;
3020
  ETrnStage stage;
3021
  int32_t   num;
3022
} STransDetailIter;
3023

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

3029
  for (int32_t i = start; i < actionNum; ++i) {
×
3030
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
×
3031
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
×
3032
    (*numOfRows)++;
×
3033
    if (*numOfRows >= rows) break;
×
3034
  }
3035

3036
  if (*numOfRows == end) {
×
3037
    sdbRelease(pSdb, pShowIter->pTrans);
×
3038
    pShowIter->pTrans = NULL;
×
3039
    pShowIter->num = 0;
×
3040
  } else {
3041
    pShowIter->pTrans = pShowIter->pTrans;
×
3042
    pShowIter->stage = pShowIter->pTrans->stage;
×
3043
    pShowIter->num += (*numOfRows);
×
3044
  }
3045
}
×
3046

3047
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
3048
  SMnode *pMnode = pReq->info.node;
×
3049
  SSdb   *pSdb = pMnode->pSdb;
×
3050
  int32_t numOfRows = 0;
×
3051

3052
  int32_t code = 0;
×
3053
  int32_t lino = 0;
×
3054

3055
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
×
3056
        pShow->pIter);
3057

3058
  if (pShow->pIter == NULL) {
×
3059
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
×
3060
    if (pShow->pIter == NULL) {
×
3061
      mError("failed to malloc for pShow->pIter");
×
3062
      return 0;
×
3063
    }
3064
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
×
3065
  }
3066

3067
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
×
3068

3069
  while (numOfRows < rows) {
×
3070
    if (pShowIter->pTrans == NULL) {
×
3071
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
×
3072
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
×
3073
      if (pShowIter->pIter == NULL) break;
×
3074
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
×
3075
            pShowIter->pTrans->stage, pShowIter->num);
3076

3077
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
×
3078

3079
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
×
3080
      break;
×
3081
    } else {
3082
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
×
3083
            pShowIter->num);
3084
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
×
3085

3086
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
×
3087
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
×
3088
      break;
×
3089
    }
3090
  }
3091

3092
_OVER:
×
3093
  pShow->numOfRows += numOfRows;
×
3094

3095
  if (code != 0) {
×
3096
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
3097
  } else {
3098
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
×
3099
  }
3100
  if (numOfRows == 0) {
×
3101
    taosMemoryFree(pShow->pIter);
×
3102
    pShow->pIter = NULL;
×
3103
  }
3104
  return numOfRows;
×
3105
}
3106

3107
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
3108
  SSdb *pSdb = pMnode->pSdb;
×
3109
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
3110
}
×
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