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

taosdata / TDengine / #4658

09 Aug 2025 02:19PM UTC coverage: 59.866% (-1.3%) from 61.2%
#4658

push

travis-ci

GitHub
fix(stream)[TD-37079]: force close the last window in fill_history (#32436)

137251 of 291849 branches covered (47.03%)

Branch coverage included in aggregate %.

207628 of 284239 relevant lines covered (73.05%)

4861547.17 hits per line

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

69.5
/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; }
356,814✔
60

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

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

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

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

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

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

115
  for (int32_t i = 0; i < actionNum; ++i) {
1,996,683✔
116
    STransAction *pAction = taosArrayGet(pArray, i);
1,292,887✔
117
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,292,887✔
118
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
915,776✔
119
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
377,111!
120
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
377,111✔
121
    } else {
122
      // empty
123
    }
124
    rawDataLen += sizeof(int8_t);
1,292,887✔
125
  }
126

127
  return rawDataLen;
703,796✔
128
}
129

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

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

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

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

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

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

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

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

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

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

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

231
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
175,949✔
232
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
175,949!
233
  void *pIter = NULL;
175,949✔
234
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
175,949✔
235
  while (pIter) {
176,021✔
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) {
175,949!
242
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
175,949!
243
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
175,949!
244
  }
245

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

263
  terrno = 0;
175,949✔
264

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

277
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
68,209✔
278
  if (pAction->groupId < 0) return 0;
68,209✔
279
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
65,786✔
280
  if (redoAction == NULL) {
65,786✔
281
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
12,092✔
282
    if (array != NULL) {
12,092!
283
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
12,092!
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));
12,092✔
289
  }
290
  if (redoAction != NULL) {
65,786!
291
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
131,572!
292
  }
293
  return 0;
65,786✔
294
}
295

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

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

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

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

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

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

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

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

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

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

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

450
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
328,944!
451

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

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

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

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

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

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

519
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
328,944!
520
  if (arbgroupIdNum > 0) {
328,944✔
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;
328,944✔
531
  int32_t killMode = 0;
328,944✔
532
  if (sver > TRANS_VER1_NUMBER) {
328,944!
533
    SDB_GET_INT8(pRaw, dataPos, &ableKill, _OVER)
328,944!
534
    SDB_GET_INT32(pRaw, dataPos, &killMode, _OVER)
328,944!
535
  }
536
  pTrans->ableToBeKilled = ableKill;
328,944✔
537
  pTrans->killMode = (int8_t)killMode;
328,944✔
538

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

552
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
328,944!
553

554
  terrno = 0;
328,944✔
555

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

571
static const char *mndTransStr(ETrnStage stage) {
2,272,917✔
572
  switch (stage) {
2,272,917!
573
    case TRN_STAGE_PREPARE:
172,966✔
574
      return "prepare";
172,966✔
575
    case TRN_STAGE_REDO_ACTION:
736,845✔
576
      return "redoAction";
736,845✔
577
    case TRN_STAGE_ROLLBACK:
10✔
578
      return "rollback";
10✔
579
    case TRN_STAGE_UNDO_ACTION:
43,803✔
580
      return "undoAction";
43,803✔
581
    case TRN_STAGE_COMMIT:
284,857✔
582
      return "commit";
284,857✔
583
    case TRN_STAGE_COMMIT_ACTION:
614,746✔
584
      return "commitAction";
614,746✔
585
    case TRN_STAGE_FINISH:
419,680✔
586
      return "finished";
419,680✔
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) {
352✔
595
  switch (actionType) {
352!
596
    case TRANS_ACTION_MSG:
288✔
597
      return "msg";
288✔
598
    case TRANS_ACTION_RAW:
64✔
599
      return "sdb";
64✔
600
    default:
×
601
      return "invalid";
×
602
  }
603
}
604

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

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

648
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
62,860✔
649

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

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

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

676
void mndTransDropData(STrans *pTrans) {
382,523✔
677
  if (pTrans->prepareActions != NULL) {
382,523!
678
    mndTransDropActions(pTrans->prepareActions);
382,523✔
679
    pTrans->prepareActions = NULL;
382,523✔
680
  }
681
  if (pTrans->redoActions != NULL) {
382,523!
682
    mndTransDropActions(pTrans->redoActions);
382,523✔
683
    pTrans->redoActions = NULL;
382,523✔
684
  }
685
  if (pTrans->undoActions != NULL) {
382,523!
686
    mndTransDropActions(pTrans->undoActions);
382,523✔
687
    pTrans->undoActions = NULL;
382,523✔
688
  }
689
  if (pTrans->commitActions != NULL) {
382,523!
690
    mndTransDropActions(pTrans->commitActions);
382,523✔
691
    pTrans->commitActions = NULL;
382,523✔
692
  }
693
  if (pTrans->redoGroupActions != NULL) {
382,523✔
694
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
57,317✔
695
    while (pIter) {
69,958✔
696
      SArray **redoActions = pIter;
12,641✔
697
      taosArrayDestroy(*redoActions);
12,641✔
698
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
12,641✔
699
    }
700

701
    taosHashCleanup(pTrans->redoGroupActions);
57,317✔
702
    pTrans->redoGroupActions = NULL;
57,317✔
703
  }
704
  if (pTrans->groupActionPos != NULL) {
382,523✔
705
    taosHashCleanup(pTrans->groupActionPos);
57,317✔
706
    pTrans->groupActionPos = NULL;
57,317✔
707
  }
708
  if (pTrans->arbGroupIds != NULL) {
382,523✔
709
    taosHashCleanup(pTrans->arbGroupIds);
53,689✔
710
  }
711
  if (pTrans->pRpcArray != NULL) {
382,523✔
712
    taosArrayDestroy(pTrans->pRpcArray);
53,579✔
713
    pTrans->pRpcArray = NULL;
53,579✔
714
  }
715
  if (pTrans->rpcRsp != NULL) {
382,523✔
716
    taosMemoryFree(pTrans->rpcRsp);
18,140!
717
    pTrans->rpcRsp = NULL;
18,140✔
718
    pTrans->rpcRspLen = 0;
18,140✔
719
  }
720
  if (pTrans->param != NULL) {
382,523!
721
    taosMemoryFree(pTrans->param);
×
722
    pTrans->param = NULL;
×
723
    pTrans->paramLen = 0;
×
724
  }
725
  (void)taosThreadMutexDestroy(&pTrans->mutex);
382,523✔
726
}
382,523✔
727

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

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

740
  mndTransDropData(pTrans);
195,890✔
741
  return 0;
195,890✔
742
}
743

744
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
281,296✔
745
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
841,777✔
746
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
560,481✔
747
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
560,481✔
748
    pOldAction->rawWritten = pNewAction->rawWritten;
560,481✔
749
    pOldAction->msgSent = pNewAction->msgSent;
560,481✔
750
    pOldAction->msgReceived = pNewAction->msgReceived;
560,481✔
751
    pOldAction->errCode = pNewAction->errCode;
560,481✔
752
  }
753
}
281,296✔
754

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

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

775
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
70,324✔
776
  while (pIter != NULL) {
75,531✔
777
    int32_t newActionPos = *(int32_t *)pIter;
5,207✔
778

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

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

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

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

796
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
70,324✔
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) {
70,324✔
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;
70,324✔
807
}
808

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

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

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

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

858
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
53,579!
859
      pTrans->pRpcArray == NULL) {
53,579!
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) {
53,579✔
867
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
66,062!
868
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
869
      return NULL;
×
870
    }
871
    pTrans->originRpcType = pReq->msgType;
33,031✔
872
  }
873

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

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

880
static void mndTransDropActions(SArray *pArray) {
1,530,092✔
881
  int32_t size = taosArrayGetSize(pArray);
1,530,092✔
882
  for (int32_t i = 0; i < size; ++i) {
4,282,087✔
883
    STransAction *pAction = taosArrayGet(pArray, i);
2,751,995✔
884
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,751,995✔
885
      taosMemoryFreeClear(pAction->pRaw);
1,918,269!
886
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
833,726!
887
      taosMemoryFreeClear(pAction->pCont);
833,726!
888
    } else {
889
      // nothing
890
    }
891
  }
892

893
  taosArrayDestroy(pArray);
1,530,092✔
894
}
1,530,092✔
895

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

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

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

912
  return 0;
368,099✔
913
}
914

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

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

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

947
  return 0;
1,378✔
948
}
949

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

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

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

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

1006
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
57,136✔
1007
  pAction->stage = TRN_STAGE_REDO_ACTION;
57,136✔
1008
  pAction->actionType = TRANS_ACTION_MSG;
57,136✔
1009
  pAction->mTraceId = pTrans->mTraceId;
57,136✔
1010
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId == 0) {
57,136✔
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));
57,133!
1015
  return mndTransAppendAction(pTrans->redoActions, pAction);
57,133✔
1016
}
1017

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

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

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

1076
void mndTransAddArbGroupId(STrans *pTrans, int32_t groupId) {
24✔
1077
  if (taosHashPut(pTrans->arbGroupIds, &groupId, sizeof(int32_t), NULL, 0) != 0) {
24!
1078
    mError("trans:%d, failed to put groupid into hash, groupId:%d", pTrans->id, groupId);
×
1079
  }
1080
}
24✔
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) {
218✔
1088
  mInfo("trans:%d, set Group Parallel", pTrans->id);
218!
1089
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
218✔
1090
}
218✔
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,368✔
1107

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

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

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

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

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

1146
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
19,155✔
1147
  if (conflict) {
19,155✔
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,
329!
1149
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
1150
    *globalConflict = true;
329✔
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,
18,826!
1153
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
1154
  }
1155
}
19,155✔
1156

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

1162
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
143,389✔
1163

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

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

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

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

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

1192
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
17,976!
1193
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
×
1194
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
×
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,976!
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,976✔
1219
  }
1220

1221
  return conflict;
96,299✔
1222
}
1223

1224
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
143,389✔
1225
  int32_t code = 0;
143,389✔
1226
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
143,389✔
1227
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
85,340!
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)) {
143,389✔
1235
    code = TSDB_CODE_MND_TRANS_CONFLICT;
367✔
1236
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
367!
1237
    TAOS_RETURN(code);
367✔
1238
  }
1239

1240
  TAOS_RETURN(code);
143,022✔
1241
}
1242

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

1249
  while (1) {
4✔
1250
    bool thisConflict = false;
304✔
1251
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
304✔
1252
    if (pIter == NULL) break;
304✔
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) {
300✔
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);
296✔
1279
}
1280

1281
static bool mndTransActionsOfSameType(SArray *pActions) {
125,681✔
1282
  int32_t size = taosArrayGetSize(pActions);
125,681✔
1283
  ETrnAct lastActType = TRANS_ACTION_NULL;
125,681✔
1284
  bool    same = true;
125,681✔
1285
  for (int32_t i = 0; i < size; ++i) {
434,645✔
1286
    STransAction *pAction = taosArrayGet(pActions, i);
308,964✔
1287
    if (i > 0) {
308,964✔
1288
      if (lastActType != pAction->actionType) {
223,590!
1289
        same = false;
×
1290
        break;
×
1291
      }
1292
    }
1293
    lastActType = pAction->actionType;
308,964✔
1294
  }
1295
  return same;
125,681✔
1296
}
1297

1298
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
53,143✔
1299
  int32_t code = 0;
53,143✔
1300
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
53,143✔
1301
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
51,718!
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) {
51,718✔
1308
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
20,820!
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);
53,143✔
1317
}
1318

1319
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
53,143✔
1320
  int32_t code = 0;
53,143✔
1321
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
53,143!
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) {
53,143!
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);
53,143✔
1333
}
1334

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

1341
  mInfo("trans:%d, action list:", pTrans->id);
53,153!
1342
  int32_t index = 0;
53,153✔
1343
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
79,100✔
1344
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
25,947✔
1345
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
25,947!
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) {
112,184✔
1350
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
59,031✔
1351
    if (pAction->actionType == TRANS_ACTION_MSG) {
59,031✔
1352
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
56,863!
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,168!
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) {
292,020✔
1362
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
238,867✔
1363
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
238,867!
1364
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1365
  }
1366

1367
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
96,870✔
1368
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
43,717✔
1369
    if (pAction->actionType == TRANS_ACTION_MSG) {
43,717✔
1370
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
29,238!
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),
14,479!
1375
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1376
    }
1377
  }
1378

1379
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
53,153✔
1380

1381
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
53,143!
1382

1383
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
53,143!
1384

1385
  mInfo("trans:%d, prepare transaction", pTrans->id);
53,143!
1386
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
53,143!
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);
53,143!
1394

1395
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
53,143✔
1396
  if (pNew == NULL) {
53,143!
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;
53,143✔
1404
  pNew->rpcRsp = pTrans->rpcRsp;
53,143✔
1405
  pNew->rpcRspLen = pTrans->rpcRspLen;
53,143✔
1406
  pNew->mTraceId = pTrans->mTraceId;
53,143✔
1407
  pTrans->pRpcArray = NULL;
53,143✔
1408
  pTrans->rpcRsp = NULL;
53,143✔
1409
  pTrans->rpcRspLen = 0;
53,143✔
1410

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

1418
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
53,151✔
1419
  int32_t code = 0;
53,151✔
1420
  mInfo("trans:%d, commit transaction", pTrans->id);
53,151!
1421
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
53,151✔
1422
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
19!
1423
    TAOS_RETURN(code);
19✔
1424
  }
1425
  mInfo("trans:%d, commit finished", pTrans->id);
53,132!
1426
  TAOS_RETURN(code);
53,132✔
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) {
250,513✔
1452
  bool    sendRsp = false;
250,513✔
1453
  int32_t code = pTrans->code;
250,513✔
1454

1455
  if (pTrans->stage == TRN_STAGE_FINISH) {
250,513✔
1456
    sendRsp = true;
115,844✔
1457
  }
1458

1459
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
250,513✔
1460
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
93,806!
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) {
156,707✔
1466
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
88,084!
1467
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1468
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1469
      } else {
1470
        if (pTrans->failedTimes > 6) sendRsp = true;
88,084✔
1471
      }
1472
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
88,084✔
1473
    }
1474
  }
1475

1476
  if (!sendRsp) {
250,513✔
1477
    return;
134,653✔
1478
  } else {
1479
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
115,860!
1480
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1481
  }
1482

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

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

1501
      if (i != 0 && code == 0) {
31,295!
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,
31,295!
1505
            mndTransStr(pTrans->stage), pInfo->ahandle);
1506

1507
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
31,295✔
1508

1509
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
31,295✔
1510
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,351!
1511
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,351✔
1512
        if (pDb != NULL) {
4,351!
1513
          for (int32_t j = 0; j < 12; j++) {
5,205✔
1514
            bool ready = mndIsDbReady(pMnode, pDb);
5,201✔
1515
            if (!ready) {
5,201✔
1516
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
854!
1517
              taosMsleep(1000);
854✔
1518
            } else {
1519
              break;
4,347✔
1520
            }
1521
          }
1522
        }
1523
        mndReleaseDb(pMnode, pDb);
4,351✔
1524
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
26,944✔
1525
        void   *pCont = NULL;
7,379✔
1526
        int32_t contLen = 0;
7,379✔
1527
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
7,379✔
1528
          mndTransSetRpcRsp(pTrans, pCont, contLen);
7,377✔
1529
        }
1530
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
19,565✔
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,551✔
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) {
31,295✔
1544
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
18,140✔
1545
        if (rpcCont != NULL) {
18,140!
1546
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
18,140✔
1547
          rspMsg.pCont = rpcCont;
18,140✔
1548
          rspMsg.contLen = pTrans->rpcRspLen;
18,140✔
1549
        }
1550
      }
1551

1552
      tmsgSendRsp(&rspMsg);
31,295✔
1553

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

1562
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
61,338✔
1563
  int32_t code = 0;
61,338✔
1564
  SMnode *pMnode = pRsp->info.node;
61,338✔
1565
#ifndef TD_ASTRA_32
1566
  int64_t signature = (int64_t)(pRsp->info.ahandle);
61,338✔
1567
  int32_t transId = (int32_t)(signature >> 32);
61,338✔
1568
  int32_t action = (int32_t)((signature << 32) >> 32);
61,338✔
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);
61,338✔
1574
  if (pTrans == NULL) {
61,338!
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;
61,338✔
1582
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
61,338✔
1583
    pArray = pTrans->redoActions;
61,330✔
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) {
61,338!
1592
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1593
    goto _OVER;
×
1594
  }
1595

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

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

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

1612
      mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
61,338!
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);
61,338!
1626
  mndTransExecute(pMnode, pTrans, true);
61,338✔
1627

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

1633
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
4,603✔
1634
  pAction->rawWritten = 0;
4,603✔
1635
  pAction->msgSent = 0;
4,603✔
1636
  pAction->msgReceived = 0;
4,603✔
1637
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
4,603!
1638
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
4,603✔
1639
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
12✔
1640
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
12!
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,591!
1644
  }
1645
  //  pAction->errCode = 0;
1646
}
4,603✔
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) {
534,798✔
1671
  if (pAction->rawWritten) return 0;
534,798✔
1672
  if (topHalf) {
293,880✔
1673
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
5✔
1674
  }
1675

1676
  if (pAction->pRaw->type >= SDB_MAX) {
293,875!
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);
293,875✔
1685
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
293,875!
1686
    pAction->rawWritten = true;
293,875✔
1687
    pAction->errCode = 0;
293,875✔
1688
    code = 0;
293,875✔
1689
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
293,875!
1690
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1691

1692
    mndSetTransLastAction(pTrans, pAction);
293,875✔
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);
293,875✔
1701
}
1702

1703
// execute in trans context
1704
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
284,476✔
1705
                                     bool notSend) {
1706
  if (pAction->msgSent) return 0;
284,476✔
1707
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
100,129✔
1708
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
38,698✔
1709
  }
1710

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

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

1721
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
61,421✔
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);
61,421✔
1729
  if (rpcMsg.pCont == NULL) {
61,421!
1730
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1731
    return -1;
×
1732
  }
1733
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
61,421✔
1734
  rpcMsg.info.notFreeAhandle = 1;
61,421✔
1735

1736
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
61,421✔
1737

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

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

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

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

1766
  TAOS_RETURN(code);
61,421✔
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,
819,274✔
1780
                                        bool notSend) {
1781
  if (pAction->actionType == TRANS_ACTION_RAW) {
819,274✔
1782
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
534,798✔
1783
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
284,476!
1784
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
284,476✔
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) {
224,305✔
1791
  int32_t numOfActions = taosArrayGetSize(pArray);
224,305✔
1792
  int32_t code = 0;
224,305✔
1793

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

1804
  return code;
224,305✔
1805
}
1806

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

1812
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
224,305✔
1813
    return code;
31,105✔
1814
  }
1815

1816
  int32_t       numOfExecuted = 0;
193,200✔
1817
  int32_t       errCode = 0;
193,200✔
1818
  STransAction *pErrAction = NULL;
193,200✔
1819
  for (int32_t action = 0; action < numOfActions; ++action) {
915,748✔
1820
    STransAction *pAction = taosArrayGet(pArray, action);
722,548✔
1821
    if (pAction->msgReceived || pAction->rawWritten) {
722,548✔
1822
      numOfExecuted++;
610,503✔
1823
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
610,503✔
1824
        errCode = pAction->errCode;
3✔
1825
        pErrAction = pAction;
3✔
1826
      }
1827
    } else {
1828
      pErrAction = pAction;
112,045✔
1829
    }
1830
  }
1831

1832
  mndSetTransLastAction(pTrans, pErrAction);
193,200✔
1833

1834
  if (numOfExecuted == numOfActions) {
193,200✔
1835
    if (errCode == 0) {
140,747✔
1836
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
140,745!
1837
      return 0;
140,745✔
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);
52,453!
1846

1847
    for (int32_t action = 0; action < numOfActions; ++action) {
224,001✔
1848
      STransAction *pAction = taosArrayGet(pArray, action);
171,548✔
1849
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
171,548✔
1850
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1851
             pAction->acceptableCode, pAction->retryCode);
1852
      if (pAction->msgSent) {
171,548✔
1853
        bool reset = false;
171,538✔
1854
        if (pAction->msgReceived) {
171,538✔
1855
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
59,503✔
1856
        } else {
1857
          int64_t timestamp = taosGetTimestampMs();
112,035✔
1858
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
112,035!
1859
        }
1860
        if (reset) {
171,538✔
1861
          mndTransResetAction(pMnode, pTrans, pAction);
1✔
1862
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1!
1863
                pAction->id, pAction->errCode, pAction->startTime);
1864
        }
1865
      }
1866
    }
1867
    return TSDB_CODE_ACTION_IN_PROGRESS;
52,453✔
1868
  }
1869
}
1870

1871
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
165,199✔
1872
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
165,199✔
1873
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
165,199✔
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;
165,199✔
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) {
115,836✔
1890
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
115,836✔
1891
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
115,836!
1892
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1893
           mndStrExecutionContext(topHalf));
1894
  }
1895
  return code;
115,836✔
1896
}
1897

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

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

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

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

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

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

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

1952
    if (code == 0) {
26,595✔
1953
      pTrans->failedTimes = 0;
7,792✔
1954
    }
1955
    mndSetTransLastAction(pTrans, pAction);
26,595✔
1956
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
26,595✔
1957
      mInfo(
14,313!
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,282✔
1963
      mError(
4,490!
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,595✔
1971
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
26,595✔
1972
      pTrans->lastErrorNo = code;
6,503✔
1973
      pTrans->code = code;
6,503✔
1974
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
6,503!
1975
            action, str);
1976
      break;
6,503✔
1977
    }
1978

1979
    if (code == 0) {
20,092✔
1980
      pTrans->code = 0;
5,564✔
1981
      pTrans->actionPos++;
5,564✔
1982
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
5,564!
1983
            pAction->id);
1984
      (void)taosThreadMutexUnlock(&pTrans->mutex);
5,564✔
1985
      code = mndTransSync(pMnode, pTrans);
5,564✔
1986
      (void)taosThreadMutexLock(&pTrans->mutex);
5,564✔
1987
      if (code != 0) {
5,564!
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,528✔
1995
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
10,038!
1996
      break;
10,038✔
1997
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,490✔
1998
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
21✔
1999
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,481!
2000
            code, tstrerror(code));
2001
      pTrans->lastErrorNo = code;
4,481✔
2002
      taosMsleep(300);
4,481✔
2003
      action--;
4,481✔
2004
      continue;
4,481✔
2005
    } else {
2006
      terrno = code;
9✔
2007
      pTrans->lastErrorNo = code;
9✔
2008
      pTrans->code = code;
9✔
2009
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
9!
2010
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2011
      break;
9✔
2012
    }
2013
  }
2014

2015
  return code;
17,800✔
2016
}
2017

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

2025
  if (groupId <= 0) {
10,182!
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));
10,182✔
2031
  if (actionPos == NULL) {
10,182!
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) {
10,182✔
2037
    mError("trans:%d, failed to execute action in serail group, actionPos:%d >= numOfActions:%d at group %d",
1,794!
2038
           pTrans->id, *actionPos, numOfActions, groupId);
2039
    return TSDB_CODE_INTERNAL_ERROR;
1,794✔
2040
  }
2041

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

2046
    if (notSend && !pAction->msgSent) {
9,145✔
2047
      mInfo(
1,267!
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,267✔
2053
      break;
7,862✔
2054
    }
2055

2056
    mInfo(
7,878!
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);
7,878✔
2063
    if (code == 0) {
7,878✔
2064
      if (pAction->msgSent) {
4,480✔
2065
        if (pAction->msgReceived) {
3,913✔
2066
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,134!
2067
            code = pAction->errCode;
111✔
2068
            mndTransResetAction(pMnode, pTrans, pAction);
111✔
2069
          } else {
2070
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
1,023!
2071
                  pAction->id, action, numOfActions, groupId);
2072
          }
2073
        } else {
2074
          code = TSDB_CODE_ACTION_IN_PROGRESS;
2,779✔
2075
        }
2076
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
3,913✔
2077
        if (msgSent != NULL) {
3,913!
2078
          *msgSent = pAction->msgSent;
3,913✔
2079
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
3,913!
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) {
567!
2084
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
567!
2085
          code = pAction->errCode;
×
2086
        } else {
2087
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
567!
2088
        }
2089
      } else {
2090
      }
2091
    }
2092

2093
    if (code == 0) {
7,878✔
2094
      pTrans->failedTimes = 0;
1,590✔
2095
    }
2096
    mndSetTransLastAction(pTrans, pAction);
7,878✔
2097

2098
    char str[200] = {0};
7,878✔
2099
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
7,878✔
2100
      pTrans->lastErrorNo = code;
3,739✔
2101
      pTrans->code = code;
3,739✔
2102
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
3,739✔
2103
        mInfo(
3,323!
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) {
416!
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,739!
2116
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2117
      break;
3,739✔
2118
    }
2119

2120
    if (code == 0) {
4,139✔
2121
      pTrans->code = 0;
1,174✔
2122
      pTrans->actionPos++;
1,174✔
2123
      (*actionPos)++;
1,174✔
2124
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
1,174!
2125
            pAction->id);
2126
      (void)taosThreadMutexUnlock(&pTrans->mutex);
1,174✔
2127
      code = mndTransSync(pMnode, pTrans);
1,174✔
2128
      (void)taosThreadMutexLock(&pTrans->mutex);
1,174✔
2129
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
1,174!
2130
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
30,286✔
2131
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
29,112✔
2132
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
29,112✔
2133
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
29,112✔
2134
          pTmpAction->msgSent = *msgSent;
1,586✔
2135
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
1,586!
2136
        }
2137
      }
2138
      if (code != 0) {
1,174!
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,965✔
2147
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
2,779!
2148
            mndTransStr(pAction->stage), pAction->id);
2149
      break;
2,779✔
2150
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
186✔
2151
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
77!
2152
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
109!
2153
            code, tstrerror(code));
2154
      pTrans->lastErrorNo = code;
109✔
2155
      taosMsleep(300);
109✔
2156
      action--;
109✔
2157
      continue;
109✔
2158
    } else {
2159
      terrno = code;
77✔
2160
      pTrans->lastErrorNo = code;
77✔
2161
      pTrans->code = code;
77✔
2162
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
77!
2163
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2164
      break;
77✔
2165
    }
2166
  }
2167

2168
  return code;
8,388✔
2169
}
2170

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

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

2187
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
2,893✔
2188
  if(pHash == NULL){
2,893!
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) {
69,164✔
2193
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
66,271✔
2194
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
66,271✔
2195
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
66,271!
2196
  }
2197
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
2,893!
2198

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

2227
  taosHashCleanup(pHash);
2,893✔
2228

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

2235
  return total_code;
2,893✔
2236
}
2237

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

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

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

2263
  return code;
3,449✔
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) {
62,726✔
2277
  bool    continueExec = true;
62,726✔
2278
  int32_t code = 0;
62,726✔
2279
  terrno = 0;
62,726✔
2280

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

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

2286
  for (int32_t action = 0; action < numOfActions; ++action) {
52,153✔
2287
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
31,148✔
2288
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
31,148✔
2289
    if (code != 0) {
31,148!
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:
21,005✔
2298
  pTrans->stage = TRN_STAGE_REDO_ACTION;
62,726✔
2299
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
62,726!
2300
  return continueExec;
62,726✔
2301
}
2302

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

2308
  if (pTrans->exec == TRN_EXEC_SERIAL) {
187,795✔
2309
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
19,147✔
2310
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
168,648✔
2311
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
165,199✔
2312
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
3,449!
2313
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
3,449✔
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 &&
187,806!
2319
      mndTransIsInSyncContext(topHalf)) {
11✔
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)) {
187,795✔
2330
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
70,482!
2331
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2332
      return false;
70,482✔
2333
    }
2334
  }
2335
  terrno = code;
117,313✔
2336

2337
  if (code == 0) {
117,313✔
2338
    pTrans->code = 0;
53,151✔
2339
    pTrans->stage = TRN_STAGE_COMMIT;
53,151✔
2340
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
53,151!
2341
    continueExec = true;
53,151✔
2342
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
64,162!
2343
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
64,151!
2344
    continueExec = false;
64,151✔
2345
  } else {
2346
    pTrans->failedTimes++;
11✔
2347
    pTrans->code = terrno;
11✔
2348
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
11✔
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);
9!
2369
      continueExec = false;
9✔
2370
    }
2371
  }
2372

2373
  return continueExec;
117,313✔
2374
}
2375

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

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

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

2395
  return continueExec;
53,151✔
2396
}
2397

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

2402
  if (code == 0) {
115,836!
2403
    pTrans->code = 0;
115,836✔
2404
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
115,836✔
2405
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
115,836!
2406
    continueExec = true;
115,836✔
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;
115,836✔
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) {
115,840✔
2491
  bool continueExec = false;
115,840✔
2492
  if (topHalf) return continueExec;
115,840✔
2493

2494
  SSdbRaw *pRaw = mndTransEncode(pTrans);
62,706✔
2495
  if (pRaw == NULL) {
62,706!
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));
62,706!
2500

2501
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
62,706✔
2502
  if (code != 0) {
62,706!
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,
62,706!
2507
        pTrans->failedTimes, pTrans->createdTime);
2508
  return continueExec;
62,706✔
2509
}
2510

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

2514
  while (continueExec) {
723,153✔
2515
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
472,640!
2516
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2517
    pTrans->lastExecTime = taosGetTimestampMs();
472,640✔
2518
    switch (pTrans->stage) {
472,640!
2519
      case TRN_STAGE_PREPARE:
×
2520
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2521
        break;
×
2522
      case TRN_STAGE_REDO_ACTION:
187,795✔
2523
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
187,795✔
2524
        break;
187,795✔
2525
      case TRN_STAGE_COMMIT:
53,151✔
2526
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
53,151✔
2527
        break;
53,151✔
2528
      case TRN_STAGE_COMMIT_ACTION:
115,836✔
2529
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
115,836✔
2530
        break;
115,836✔
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:
115,840✔
2541
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
115,840✔
2542
        break;
115,840✔
2543
      default:
×
2544
        continueExec = false;
×
2545
        break;
×
2546
    }
2547
  }
2548

2549
  mndTransSendRpcRsp(pMnode, pTrans);
250,513✔
2550
}
250,513✔
2551

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

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

2564
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
29,796✔
2565
  mTrace("start to process trans timer");
29,796✔
2566
  mndTransPullup(pReq->info.node);
29,796✔
2567
  return 0;
29,796✔
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; }
112✔
2643

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

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

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

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

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

2685
  struct tm tm;
2686
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
40,550!
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);
40,550✔
2691

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

2700
  return buf;
40,550✔
2701
}
2702

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

2708
  if (pTrans->stage == TRN_STAGE_PREPARE) {
796!
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) {
796!
2720
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
27,413✔
2721
      len = 0;
26,617✔
2722
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
26,617✔
2723
      if (pAction->actionType == TRANS_ACTION_MSG) {
26,617✔
2724
        char bufStart[40] = {0};
20,253✔
2725
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
20,253✔
2726

2727
        char endStart[40] = {0};
20,253✔
2728
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
20,253✔
2729
        len += snprintf(detail + len, sizeof(detail) - len,
40,506!
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,253✔
2733
                        pAction->msgReceived, bufStart, endStart);
20,253✔
2734

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

2744
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
20,253✔
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,364✔
2748
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
6,364✔
2749
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
6,364✔
2750
      }
2751
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
26,617!
2752
    }
2753
  }
2754

2755
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
796!
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
}
796✔
2781

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

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

2795
    cols = 0;
796✔
2796

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

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

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

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

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

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

2824
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
796!
2825
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
796✔
2826
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
796✔
2827
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
796✔
2828
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
796!
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++);
796✔
2839
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
796!
2840
                        _OVER);
2841

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

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

2862
    mndTransLogAction(pTrans);
796✔
2863

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

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

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

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

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

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

2907
  cols = 0;
352✔
2908

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

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

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

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

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

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

2957
  } else {
2958
    int32_t len = 0;
64✔
2959

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

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

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

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

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

3001
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
4✔
3002
  if (stage == TRN_STAGE_PREPARE) {
4!
3003
    return pTrans->prepareActions;
×
3004
  }
3005
  if (stage == TRN_STAGE_REDO_ACTION) {
4!
3006
    return pTrans->redoActions;
4✔
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,
4✔
3025
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3026
  int32_t actionNum = taosArrayGetSize(pActions);
4✔
3027
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
4!
3028

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

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

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

3052
  int32_t code = 0;
6✔
3053
  int32_t lino = 0;
6✔
3054

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

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

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

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

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

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

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

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

3095
  if (code != 0) {
6!
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)
6!
3099
  }
3100
  if (numOfRows == 0) {
6✔
3101
    taosMemoryFree(pShow->pIter);
2!
3102
    pShow->pIter = NULL;
2✔
3103
  }
3104
  return numOfRows;
6✔
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