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

taosdata / TDengine / #4729

12 Sep 2025 02:34AM UTC coverage: 58.085% (-1.0%) from 59.125%
#4729

push

travis-ci

web-flow
docs: optimize taosd config parameters doc better (#32964)

133518 of 292959 branches covered (45.58%)

Branch coverage included in aggregate %.

201933 of 284559 relevant lines covered (70.96%)

5466318.3 hits per line

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

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

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

28
#define TRANS_VER1_NUMBER  1
29
#define TRANS_VER2_NUMBER  2
30
#define TRANS_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; }
324,812✔
60

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

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

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

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

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

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

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

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

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

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

115
  for (int32_t i = 0; i < actionNum; ++i) {
1,763,242✔
116
    STransAction *pAction = taosArrayGet(pArray, i);
1,154,570✔
117
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,154,570✔
118
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
796,784✔
119
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
357,786!
120
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
357,786✔
121
    } else {
122
      // empty
123
    }
124
    rawDataLen += sizeof(int8_t);
1,154,570✔
125
  }
126

127
  return rawDataLen;
608,672✔
128
}
129

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

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

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

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

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

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

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

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

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

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

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

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

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

263
  terrno = 0;
152,168✔
264

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

277
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
58,997✔
278
  if (pAction->groupId < 0) return 0;
58,997✔
279
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
57,142✔
280
  if (redoAction == NULL) {
57,142✔
281
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
11,196✔
282
    if (array != NULL) {
11,196!
283
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
11,196!
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));
11,196✔
289
  }
290
  if (redoAction != NULL) {
57,142!
291
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
114,284!
292
  }
293
  return 0;
57,142✔
294
}
295

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

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

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

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

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

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

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

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

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

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

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

450
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
287,422!
451

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

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

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

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

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

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

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

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

552
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
287,422!
553

554
  terrno = 0;
287,422✔
555

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

571
static const char *mndTransStr(ETrnStage stage) {
2,029,568✔
572
  switch (stage) {
2,029,568!
573
    case TRN_STAGE_PREPARE:
153,874✔
574
      return "prepare";
153,874✔
575
    case TRN_STAGE_REDO_ACTION:
697,757✔
576
      return "redoAction";
697,757✔
577
    case TRN_STAGE_ROLLBACK:
10✔
578
      return "rollback";
10✔
579
    case TRN_STAGE_UNDO_ACTION:
40,372✔
580
      return "undoAction";
40,372✔
581
    case TRN_STAGE_COMMIT:
245,133✔
582
      return "commit";
245,133✔
583
    case TRN_STAGE_COMMIT_ACTION:
525,132✔
584
      return "commitAction";
525,132✔
585
    case TRN_STAGE_FINISH:
367,280✔
586
      return "finished";
367,280✔
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) {
577,758✔
606
  if (pAction != NULL) {
577,758✔
607
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
454,319✔
608
      pTrans->lastAction = pAction->id;
332,652✔
609
      pTrans->lastMsgType = pAction->msgType;
332,652✔
610
      pTrans->lastEpset = pAction->epSet;
332,652✔
611
      pTrans->lastErrorNo = pAction->errCode;
332,652✔
612
    }
613
  } else {
614
    pTrans->lastAction = 0;
123,439✔
615
    pTrans->lastMsgType = 0;
123,439✔
616
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
123,439✔
617
    pTrans->lastErrorNo = 0;
123,439✔
618
  }
619
}
577,758✔
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,478✔
630
  switch (ftype) {
1,478!
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:
739✔
636
      return mndRebCntInc;
739✔
637
    case TRANS_STOP_FUNC_MQ_REB:
739✔
638
      return mndRebCntDec;
739✔
639
    default:
×
640
      return NULL;
×
641
  }
642
}
643

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

648
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
54,601✔
649

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

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

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

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

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

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

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

740
  mndTransDropData(pTrans);
170,998✔
741
  return 0;
170,998✔
742
}
743

744
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
247,728✔
745
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
758,414✔
746
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
510,686✔
747
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
510,686✔
748
    pOldAction->rawWritten = pNewAction->rawWritten;
510,686✔
749
    pOldAction->msgSent = pNewAction->msgSent;
510,686✔
750
    pOldAction->msgReceived = pNewAction->msgReceived;
510,686✔
751
    pOldAction->errCode = pNewAction->errCode;
510,686✔
752
  }
753
}
247,728✔
754

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

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

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

775
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
61,932✔
776
  while (pIter != NULL) {
66,729✔
777
    int32_t newActionPos = *(int32_t *)pIter;
4,797✔
778

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

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

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

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

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

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

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

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

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

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

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

866
  if (pReq != NULL) {
45,860✔
867
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
61,618!
868
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
869
      return NULL;
×
870
    }
871
    pTrans->originRpcType = pReq->msgType;
30,809✔
872
  }
873

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

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

880
static void mndTransDropActions(SArray *pArray) {
1,333,128✔
881
  int32_t size = taosArrayGetSize(pArray);
1,333,128✔
882
  for (int32_t i = 0; i < size; ++i) {
3,802,671✔
883
    STransAction *pAction = taosArrayGet(pArray, i);
2,469,543✔
884
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,469,543✔
885
      taosMemoryFreeClear(pAction->pRaw);
1,676,642!
886
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
792,901!
887
      taosMemoryFreeClear(pAction->pCont);
792,901!
888
    } else {
889
      // nothing
890
    }
891
  }
892

893
  taosArrayDestroy(pArray);
1,333,128✔
894
}
1,333,128✔
895

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

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

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

912
  return 0;
323,536✔
913
}
914

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

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

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

947
  return 0;
1,248✔
948
}
949

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

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

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

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

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

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

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

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

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

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

1029
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
727✔
1030
  pTrans->startFunc = startFunc;
727✔
1031
  pTrans->stopFunc = stopFunc;
727✔
1032
  pTrans->param = param;
727✔
1033
  pTrans->paramLen = paramLen;
727✔
1034
}
727✔
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) {
25,525✔
1068
  if (dbname != NULL) {
25,525!
1069
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
25,525✔
1070
  }
1071
  if (stbname != NULL) {
25,525✔
1072
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
18,579✔
1073
  }
1074
}
25,525✔
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,258✔
1083
  mInfo("trans:%d, set Serial", pTrans->id);
1,258!
1084
  pTrans->exec = TRN_EXEC_SERIAL;
1,258✔
1085
}
1,258✔
1086

1087
void mndTransSetGroupParallel(STrans *pTrans) {
200✔
1088
  mInfo("trans:%d, set Group Parallel", pTrans->id);
200!
1089
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
200✔
1090
}
200✔
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; }
3,969✔
1107

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

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

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

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

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

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

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

1165
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
125,967✔
1166

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

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

1174
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
17,741✔
1175

1176
    if (pNew->conflict == TRN_CONFLICT_DB) {
17,741✔
1177
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
1,068!
1178
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
1,068!
1179
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,068✔
1180
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,068✔
1181
      }
1182
    }
1183

1184
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
17,741✔
1185
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
16,643✔
1186
      if (pTrans->conflict == TRN_CONFLICT_DB) {
16,643✔
1187
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
310✔
1188
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
310✔
1189
      }
1190
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
16,643✔
1191
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
16,322✔
1192
      }
1193
    }
1194

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

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

1221
    sdbRelease(pMnode->pSdb, pTrans);
17,741✔
1222
  }
1223

1224
  return conflict;
89,675✔
1225
}
1226

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

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

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

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

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

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

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

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

1281
  TAOS_RETURN(code);
291✔
1282
}
1283

1284
static bool mndTransActionsOfSameType(SArray *pActions) {
106,449✔
1285
  int32_t size = taosArrayGetSize(pActions);
106,449✔
1286
  ETrnAct lastActType = TRANS_ACTION_NULL;
106,449✔
1287
  bool    same = true;
106,449✔
1288
  for (int32_t i = 0; i < size; ++i) {
374,414✔
1289
    STransAction *pAction = taosArrayGet(pActions, i);
267,965✔
1290
    if (i > 0) {
267,965✔
1291
      if (lastActType != pAction->actionType) {
192,187!
1292
        same = false;
×
1293
        break;
×
1294
      }
1295
    }
1296
    lastActType = pAction->actionType;
267,965✔
1297
  }
1298
  return same;
106,449✔
1299
}
1300

1301
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
45,400✔
1302
  int32_t code = 0;
45,400✔
1303
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
45,400✔
1304
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
43,993!
1305
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1306
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1307
      TAOS_RETURN(code);
×
1308
    }
1309

1310
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
43,993✔
1311
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
17,056!
1312
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1313
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1314
        TAOS_RETURN(code);
×
1315
      }
1316
    }
1317
  }
1318

1319
  TAOS_RETURN(code);
45,400✔
1320
}
1321

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

1335
  TAOS_RETURN(code);
45,400✔
1336
}
1337

1338
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
45,410✔
1339
  int32_t code = 0;
45,410✔
1340
  if (pTrans == NULL) {
45,410!
1341
    return TSDB_CODE_INVALID_PARA;
×
1342
  }
1343

1344
  mInfo("trans:%d, action list:", pTrans->id);
45,410!
1345
  int32_t index = 0;
45,410✔
1346
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
69,907✔
1347
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
24,497✔
1348
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
24,497!
1349
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1350
  }
1351

1352
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
101,312✔
1353
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
55,902✔
1354
    if (pAction->actionType == TRANS_ACTION_MSG) {
55,902✔
1355
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
53,715!
1356
            TMSG_INFO(pAction->msgType));
1357
      ;
1358
    } else {
1359
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
2,187!
1360
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1361
    }
1362
  }
1363

1364
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
247,797✔
1365
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
202,387✔
1366
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
202,387!
1367
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1368
  }
1369

1370
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
85,696✔
1371
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
40,286✔
1372
    if (pAction->actionType == TRANS_ACTION_MSG) {
40,286✔
1373
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
27,034!
1374
            TMSG_INFO(pAction->msgType));
1375
      ;
1376
    } else {
1377
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
13,252!
1378
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1379
    }
1380
  }
1381

1382
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
45,410✔
1383

1384
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
45,400!
1385

1386
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
45,400!
1387

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

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

1406
  pNew->pRpcArray = pTrans->pRpcArray;
45,400✔
1407
  pNew->rpcRsp = pTrans->rpcRsp;
45,400✔
1408
  pNew->rpcRspLen = pTrans->rpcRspLen;
45,400✔
1409
  pNew->mTraceId = pTrans->mTraceId;
45,400✔
1410
  pTrans->pRpcArray = NULL;
45,400✔
1411
  pTrans->rpcRsp = NULL;
45,400✔
1412
  pTrans->rpcRspLen = 0;
45,400✔
1413

1414
  mInfo("trans:%d, execute transaction in prepare", pTrans->id);
45,400!
1415
  mndTransExecute(pMnode, pNew, false);
45,400✔
1416
  mndReleaseTrans(pMnode, pNew);
45,400✔
1417
  // TDOD change to TAOS_RETURN(code);
1418
  return 0;
45,400✔
1419
}
1420

1421
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
45,403✔
1422
  int32_t code = 0;
45,403✔
1423
  mInfo("trans:%d, commit transaction", pTrans->id);
45,403!
1424
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
45,403✔
1425
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
12!
1426
    TAOS_RETURN(code);
12✔
1427
  }
1428
  mInfo("trans:%d, commit finished", pTrans->id);
45,391!
1429
  TAOS_RETURN(code);
45,391✔
1430
}
1431

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

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

1454
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
222,735✔
1455
  bool    sendRsp = false;
222,735✔
1456
  int32_t code = pTrans->code;
222,735✔
1457

1458
  if (pTrans->stage == TRN_STAGE_FINISH) {
222,735✔
1459
    sendRsp = true;
99,860✔
1460
  }
1461

1462
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
222,735✔
1463
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
80,825!
1464
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
12!
1465
      sendRsp = true;
12✔
1466
    }
1467
  } else {
1468
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
141,910✔
1469
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
81,585!
1470
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1471
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1472
      } else {
1473
        if (pTrans->failedTimes > 6) sendRsp = true;
81,585!
1474
      }
1475
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
81,585✔
1476
    }
1477
  }
1478

1479
  if (!sendRsp) {
222,735✔
1480
    return;
122,863✔
1481
  } else {
1482
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
99,872!
1483
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1484
  }
1485

1486
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
99,872!
1487
  taosWLockLatch(&pTrans->lockRpcArray);
99,872✔
1488
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
99,872✔
1489
  if (size <= 0) {
99,872✔
1490
    taosWUnLockLatch(&pTrans->lockRpcArray);
69,538✔
1491
    return;
69,538✔
1492
  }
1493

1494
  for (int32_t i = 0; i < size; ++i) {
60,668✔
1495
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
30,334✔
1496
    if (pInfo->handle != NULL) {
30,334✔
1497
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
29,318!
1498
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1499
      }
1500
      if (code == TSDB_CODE_SYN_TIMEOUT) {
29,318!
1501
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1502
      }
1503

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

1510
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
29,318✔
1511

1512
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
29,318✔
1513
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
3,957!
1514
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
3,957✔
1515
        if (pDb != NULL) {
3,957!
1516
          for (int32_t j = 0; j < 12; j++) {
4,776✔
1517
            bool ready = mndIsDbReady(pMnode, pDb);
4,774✔
1518
            if (!ready) {
4,774✔
1519
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
819!
1520
              taosMsleep(1000);
819✔
1521
            } else {
1522
              break;
3,955✔
1523
            }
1524
          }
1525
        }
1526
        mndReleaseDb(pMnode, pDb);
3,957✔
1527
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
25,361✔
1528
        void   *pCont = NULL;
6,856✔
1529
        int32_t contLen = 0;
6,856✔
1530
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
6,856✔
1531
          mndTransSetRpcRsp(pTrans, pCont, contLen);
6,854✔
1532
        }
1533
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
18,505✔
1534
        void   *pCont = NULL;
10✔
1535
        int32_t contLen = 0;
10✔
1536
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
10!
1537
          mndTransSetRpcRsp(pTrans, pCont, contLen);
10✔
1538
        }
1539
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
18,495✔
1540
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
23✔
1541
        if (code != 0) {
23!
1542
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1543
        }
1544
      }
1545

1546
      if (pTrans->rpcRspLen != 0) {
29,318✔
1547
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
17,427✔
1548
        if (rpcCont != NULL) {
17,427!
1549
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
17,427✔
1550
          rspMsg.pCont = rpcCont;
17,427✔
1551
          rspMsg.contLen = pTrans->rpcRspLen;
17,427✔
1552
        }
1553
      }
1554

1555
      tmsgSendRsp(&rspMsg);
29,318✔
1556

1557
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
29,318!
1558
            mndTransStr(pTrans->stage), pInfo->ahandle);
1559
    }
1560
  }
1561
  taosArrayClear(pTrans->pRpcArray);
30,334✔
1562
  taosWUnLockLatch(&pTrans->lockRpcArray);
30,334✔
1563
}
1564

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

1585
  SArray *pArray = NULL;
58,092✔
1586
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
58,092✔
1587
    pArray = pTrans->redoActions;
58,084✔
1588
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
8!
1589
    pArray = pTrans->undoActions;
8✔
1590
  } else {
1591
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1592
    goto _OVER;
×
1593
  }
1594

1595
  if (pArray == NULL) {
58,092!
1596
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1597
    goto _OVER;
×
1598
  }
1599

1600
  int32_t actionNum = taosArrayGetSize(pArray);
58,092✔
1601
  if (action < 0 || action >= actionNum) {
58,092!
1602
    mError("trans:%d, invalid action:%d", transId, action);
×
1603
    goto _OVER;
×
1604
  }
1605

1606
  STransAction *pAction = taosArrayGet(pArray, action);
58,092✔
1607
  if (pAction != NULL) {
58,092!
1608
    if (pAction->msgSent) {
58,092!
1609
      pAction->msgReceived = 1;
58,092✔
1610
      pAction->errCode = pRsp->code;
58,092✔
1611
      pAction->endTime = taosGetTimestampMs();
58,092✔
1612

1613
      // pTrans->lastErrorNo = pRsp->code;
1614
      mndSetTransLastAction(pTrans, pAction);
58,092✔
1615

1616
      mInfo("trans:%d, %s:%d response is received, msgType:%s, QID:0x%" PRIx64 ":0x%" PRIx64 ", received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
58,092!
1617
            mndTransStr(pAction->stage), action, TMSG_INFO(pAction->msgType), trace ? trace->rootId : 0, 
1618
              trace ? trace->msgId : 0, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1619
            tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1620
    } else {
1621
      mWarn("trans:%d, %s:%d response is received, but msgSent is false, code:0x%x(%s), accept:0x%x(%s) retry:0x%x", 
×
1622
            transId, mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code),
1623
            pAction->acceptableCode, tstrerror(pAction->acceptableCode), pAction->retryCode);
1624
    }
1625

1626
  } else {
1627
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1628
  }
1629

1630
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
58,092!
1631
  mndTransExecute(pMnode, pTrans, true);
58,092✔
1632

1633
_OVER:
58,092✔
1634
  mndReleaseTrans(pMnode, pTrans);
58,092✔
1635
  TAOS_RETURN(code);
58,092✔
1636
}
1637

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

1653
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
2✔
1654
  int32_t numOfActions = taosArrayGetSize(pArray);
2✔
1655

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

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

1668
    mndTransResetAction(pMnode, pTrans, pAction);
2✔
1669
    mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage), pAction->id,
2!
1670
          pAction->errCode, pAction->startTime);
1671
  }
1672
}
2✔
1673

1674
// execute in sync context
1675
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
459,480✔
1676
  if (pAction->rawWritten) return 0;
459,480✔
1677
  if (topHalf) {
255,015✔
1678
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
7✔
1679
  }
1680

1681
  if (pAction->pRaw->type >= SDB_MAX) {
255,008!
1682
    pAction->rawWritten = true;
×
1683
    pAction->errCode = 0;
×
1684
    mndSetTransLastAction(pTrans, pAction);
×
1685
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
1686
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1687
  }
1688

1689
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
255,008✔
1690
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
255,008!
1691
    pAction->rawWritten = true;
255,008✔
1692
    pAction->errCode = 0;
255,008✔
1693
    code = 0;
255,008✔
1694
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
255,008!
1695
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1696

1697
    mndSetTransLastAction(pTrans, pAction);
255,008✔
1698
  } else {
1699
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1700
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1701
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1702
    mndSetTransLastAction(pTrans, pAction);
×
1703
  }
1704

1705
  TAOS_RETURN(code);
255,008✔
1706
}
1707

1708
// execute in trans context
1709
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
271,264✔
1710
                                     bool notSend) {
1711
  if (pAction->msgSent) return 0;
271,264✔
1712
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
94,954✔
1713
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
36,826✔
1714
  }
1715

1716
  if (notSend) {
58,128✔
1717
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
13!
1718
    return 0;
13✔
1719
  }
1720

1721
#ifndef TD_ASTRA_32
1722
  int64_t signature = pTrans->id;
58,115✔
1723
  signature = (signature << 32);
58,115✔
1724
  signature += pAction->id;
58,115✔
1725

1726
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
58,115✔
1727
#else
1728
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1729
                    .contLen = pAction->contLen,
1730
                    .info.ahandle = (void *)pTrans->id,
1731
                    .info.ahandleEx = (void *)pAction->id};
1732
#endif
1733
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
58,115✔
1734
  if (rpcMsg.pCont == NULL) {
58,115!
1735
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1736
    return -1;
×
1737
  }
1738
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
58,115✔
1739
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
58,115✔
1740
  rpcMsg.info.notFreeAhandle = 1;
58,115✔
1741
  STraceId* trace = &(rpcMsg.info.traceId);
58,115✔
1742

1743
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
58,115✔
1744

1745
  char    detail[1024] = {0};
58,115✔
1746
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
58,115!
1747
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
58,115✔
1748
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
123,809✔
1749
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
65,694✔
1750
                     pAction->epSet.eps[i].port);
65,694✔
1751
  }
1752

1753
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
58,115✔
1754
  if (code == 0) {
58,115✔
1755
    pAction->msgSent = 1;
58,114✔
1756
    // pAction->msgReceived = 0;
1757
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
58,114✔
1758
    pAction->startTime = taosGetTimestampMs();
58,114✔
1759
    pAction->endTime = 0;
58,114✔
1760
    mInfo("trans:%d, %s:%d is sent, QID:0x%" PRIx64 ":0x%" PRIx64 ", %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, trace ? trace->rootId : 0, 
58,114!
1761
              trace ? trace->msgId : 0, detail);
1762

1763
    mndSetTransLastAction(pTrans, pAction);
58,114✔
1764
  } else {
1765
    pAction->msgSent = 0;
1✔
1766
    pAction->msgReceived = 0;
1✔
1767
    pAction->errCode = (terrno != 0) ? terrno : code;
1!
1768
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
1!
1769
           detail);
1770

1771
    mndSetTransLastAction(pTrans, pAction);
1✔
1772
  }
1773

1774
  TAOS_RETURN(code);
58,115✔
1775
}
1776

1777
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1778
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1779
  pAction->rawWritten = 0;
×
1780
  pAction->errCode = 0;
×
1781
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1782

1783
  mndSetTransLastAction(pTrans, pAction);
×
1784
  return 0;
×
1785
}
1786

1787
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
730,744✔
1788
                                        bool notSend) {
1789
  if (pAction->actionType == TRANS_ACTION_RAW) {
730,744✔
1790
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
459,480✔
1791
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
271,264!
1792
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
271,264✔
1793
  } else {
1794
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1795
  }
1796
}
1797

1798
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
202,280✔
1799
  int32_t numOfActions = taosArrayGetSize(pArray);
202,280✔
1800
  int32_t code = 0;
202,280✔
1801

1802
  for (int32_t action = 0; action < numOfActions; ++action) {
840,108✔
1803
    STransAction *pAction = taosArrayGet(pArray, action);
667,412✔
1804
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
667,412✔
1805
    if (code != 0) {
667,412✔
1806
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
29,584!
1807
            numOfActions);
1808
      break;
29,584✔
1809
    }
1810
  }
1811

1812
  return code;
202,280✔
1813
}
1814

1815
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
245,951✔
1816
  int32_t numOfActions = taosArrayGetSize(pArray);
245,951✔
1817
  int32_t code = 0;
245,951✔
1818
  if (numOfActions == 0) return 0;
245,951✔
1819

1820
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
202,280✔
1821
    return code;
29,584✔
1822
  }
1823

1824
  int32_t       numOfExecuted = 0;
172,696✔
1825
  int32_t       errCode = 0;
172,696✔
1826
  STransAction *pErrAction = NULL;
172,696✔
1827
  for (int32_t action = 0; action < numOfActions; ++action) {
810,524✔
1828
    STransAction *pAction = taosArrayGet(pArray, action);
637,828✔
1829
    if (pAction->msgReceived || pAction->rawWritten) {
637,828✔
1830
      numOfExecuted++;
531,187✔
1831
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
531,187✔
1832
        errCode = pAction->errCode;
4✔
1833
        pErrAction = pAction;
4✔
1834
      }
1835
    } else {
1836
      pErrAction = pAction;
106,641✔
1837
    }
1838
  }
1839

1840
  mndSetTransLastAction(pTrans, pErrAction);
172,696✔
1841

1842
  if (numOfExecuted == numOfActions) {
172,696✔
1843
    if (errCode == 0) {
123,441✔
1844
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
123,439!
1845
      return 0;
123,439✔
1846
    } else {
1847
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
2!
1848
      mndTransResetActions(pMnode, pTrans, pArray);
2✔
1849
      terrno = errCode;
2✔
1850
      return errCode;
2✔
1851
    }
1852
  } else {
1853
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
49,255!
1854

1855
    for (int32_t action = 0; action < numOfActions; ++action) {
212,863✔
1856
      STransAction *pAction = taosArrayGet(pArray, action);
163,608✔
1857
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
163,608✔
1858
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1859
             pAction->acceptableCode, pAction->retryCode);
1860
      if (pAction->msgSent) {
163,608✔
1861
        bool reset = false;
163,595✔
1862
        if (pAction->msgReceived) {
163,595✔
1863
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
56,967✔
1864
        } else {
1865
          int64_t timestamp = taosGetTimestampMs();
106,628✔
1866
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
106,628!
1867
        }
1868
        if (reset) {
163,595✔
1869
          mndTransResetAction(pMnode, pTrans, pAction);
2✔
1870
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
2!
1871
                pAction->id, pAction->errCode, pAction->startTime);
1872
        }
1873
      }
1874
    }
1875
    return TSDB_CODE_ACTION_IN_PROGRESS;
49,255✔
1876
  }
1877
}
1878

1879
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
146,083✔
1880
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
146,083✔
1881
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
146,083✔
1882
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
2!
1883
           mndStrExecutionContext(topHalf));
1884
  }
1885
  return code;
146,083✔
1886
}
1887

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

1897
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
99,854✔
1898
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
99,854✔
1899
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
99,854!
1900
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1901
           mndStrExecutionContext(topHalf));
1902
  }
1903
  return code;
99,854✔
1904
}
1905

1906
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
19,216✔
1907
  int32_t code = 0;
19,216✔
1908
  int32_t numOfActions = taosArrayGetSize(pActions);
19,216✔
1909
  if (numOfActions == 0) return code;
19,216✔
1910

1911
  if (pTrans->actionPos >= numOfActions) {
19,212✔
1912
    return code;
1,465✔
1913
  }
1914

1915
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
17,747!
1916
        pTrans->actionPos, mndTransStr(pTrans->stage));
1917

1918
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
27,870✔
1919
    STransAction *pAction = taosArrayGet(pActions, action);
26,648✔
1920

1921
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
26,648✔
1922
      code = TSDB_CODE_ACTION_IN_PROGRESS;
15✔
1923
      break;
16,525✔
1924
    }
1925

1926
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
26,633!
1927
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
1928

1929
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
26,633✔
1930
    if (code == 0) {
26,633✔
1931
      if (pAction->msgSent) {
22,401✔
1932
        bool reset = false;
18,193✔
1933
        if (pAction->msgReceived) {
18,193✔
1934
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
8,151!
1935
            code = pAction->errCode;
4,478✔
1936
            reset = true;
4,478✔
1937
          } else {
1938
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
3,673!
1939
          }
1940
        } else {
1941
          int64_t timestamp = taosGetTimestampMs();
10,042✔
1942
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
10,042!
1943
          code = TSDB_CODE_ACTION_IN_PROGRESS;
10,042✔
1944
        }
1945
        if (reset) {
18,193✔
1946
          mndTransResetAction(pMnode, pTrans, pAction);
4,478✔
1947
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
4,478!
1948
                pAction->id, pAction->errCode, pAction->startTime);
1949
        }
1950
      } else if (pAction->rawWritten) {
4,208!
1951
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
4,208!
1952
          code = pAction->errCode;
×
1953
        } else {
1954
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,208!
1955
        }
1956
      } else {
1957
      }
1958
    }
1959

1960
    if (code == 0) {
26,633✔
1961
      pTrans->failedTimes = 0;
7,881✔
1962
    }
1963
    mndSetTransLastAction(pTrans, pAction);
26,633✔
1964
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
26,633✔
1965
      mInfo(
14,273!
1966
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1967
          "msgReceived:%d",
1968
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1969
          pAction->msgSent, pAction->msgReceived);
1970
    } else if (code != 0) {
12,360✔
1971
      mError(
4,479!
1972
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1973
          "msgReceived:%d",
1974
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1975
          pAction->msgSent, pAction->msgReceived);
1976
    }
1977

1978
    char str[200] = {0};
26,633✔
1979
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
26,633✔
1980
      pTrans->lastErrorNo = code;
6,466✔
1981
      pTrans->code = code;
6,466✔
1982
      mInfo("trans:%d, %s:%d cannot execute next action, stop execution, %s", pTrans->id, mndTransStr(pAction->stage),
6,466!
1983
            action, str);
1984
      break;
6,466✔
1985
    }
1986

1987
    if (code == 0) {
20,167✔
1988
      pTrans->code = 0;
5,646✔
1989
      pTrans->actionPos++;
5,646✔
1990
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
5,646!
1991
            pAction->id);
1992
      (void)taosThreadMutexUnlock(&pTrans->mutex);
5,646✔
1993
      code = mndTransSync(pMnode, pTrans);
5,646✔
1994
      (void)taosThreadMutexLock(&pTrans->mutex);
5,646✔
1995
      if (code != 0) {
5,646!
1996
        pTrans->actionPos--;
×
1997
        pTrans->code = terrno;
×
1998
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1999
               mndTransStr(pAction->stage), pAction->id, terrstr());
2000
        break;
×
2001
      }
2002
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
14,521✔
2003
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
10,042!
2004
      break;
10,042✔
2005
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,479!
2006
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
4✔
2007
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,477!
2008
            code, tstrerror(code));
2009
      pTrans->lastErrorNo = code;
4,477✔
2010
      taosMsleep(300);
4,477✔
2011
      action--;
4,477✔
2012
      continue;
4,477✔
2013
    } else {
2014
      terrno = code;
2✔
2015
      pTrans->lastErrorNo = code;
2✔
2016
      pTrans->code = code;
2✔
2017
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
2!
2018
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2019
      break;
2✔
2020
    }
2021
  }
2022

2023
  return code;
17,747✔
2024
}
2025

2026
static int32_t mndTransExecuteActionsSerialGroup(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf,
9,413✔
2027
                                                 int32_t groupId, int32_t currentGroup, int32_t groupCount,
2028
                                                 bool notSend, SHashObj *pHash) {
2029
  int32_t code = 0;
9,413✔
2030
  int32_t numOfActions = taosArrayGetSize(pActions);
9,413✔
2031
  if (numOfActions == 0) return code;
9,413!
2032

2033
  if (groupId <= 0) {
9,413!
2034
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
2035
    return TSDB_CODE_INTERNAL_ERROR;
×
2036
  }
2037

2038
  int32_t *actionPos = taosHashGet(pTrans->groupActionPos, &groupId, sizeof(int32_t));
9,413✔
2039
  if (actionPos == NULL) {
9,413!
2040
    mError("trans:%d, failed to execute action in serail group, actionPos is null at group %d", pTrans->id, groupId);
×
2041
    return TSDB_CODE_INTERNAL_ERROR;
×
2042
  }
2043

2044
  if (*actionPos >= numOfActions) {
9,413✔
2045
    mError("trans:%d, failed to execute action in serail group, actionPos:%d >= numOfActions:%d at group %d",
1,766!
2046
           pTrans->id, *actionPos, numOfActions, groupId);
2047
    return TSDB_CODE_INTERNAL_ERROR;
1,766✔
2048
  }
2049

2050
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
8,784✔
2051
    STransAction **ppAction = taosArrayGet(pActions, action);
8,278✔
2052
    STransAction  *pAction = *ppAction;
8,278✔
2053

2054
    if (notSend && !pAction->msgSent) {
8,278✔
2055
      mInfo(
1,064!
2056
          "trans:%d, %s:%d (%d/%d at group %d) skip to execute, curent state(actionType(1:msg,2:log):%d, "
2057
          "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2058
          pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2059
          pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2060
      code = TSDB_CODE_ACTION_IN_PROGRESS;
1,064✔
2061
      break;
7,141✔
2062
    }
2063

2064
    mInfo(
7,214!
2065
        "trans:%d, %s:%d (%d/%d at group %d) begin to execute, curent state(actionType(1:msg,2:log):%d, "
2066
        "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2067
        pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2068
        pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2069

2070
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
7,214✔
2071
    if (code == 0) {
7,214✔
2072
      if (pAction->msgSent) {
4,196✔
2073
        if (pAction->msgReceived) {
3,707✔
2074
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,010!
2075
            code = pAction->errCode;
83✔
2076
            mndTransResetAction(pMnode, pTrans, pAction);
83✔
2077
          } else {
2078
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
927!
2079
                  pAction->id, action, numOfActions, groupId);
2080
          }
2081
        } else {
2082
          code = TSDB_CODE_ACTION_IN_PROGRESS;
2,697✔
2083
        }
2084
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
3,707✔
2085
        if (msgSent != NULL) {
3,707!
2086
          *msgSent = pAction->msgSent;
3,707✔
2087
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
3,707!
2088
        } else {
2089
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2090
        }
2091
      } else if (pAction->rawWritten) {
489!
2092
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
489!
2093
          code = pAction->errCode;
×
2094
        } else {
2095
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
489!
2096
        }
2097
      } else {
2098
      }
2099
    }
2100

2101
    if (code == 0) {
7,214✔
2102
      pTrans->failedTimes = 0;
1,416✔
2103
    }
2104
    mndSetTransLastAction(pTrans, pAction);
7,214✔
2105

2106
    char str[200] = {0};
7,214✔
2107
    if (mndCannotExecuteTransWithInfo(pMnode, topHalf, str, 200)) {
7,214✔
2108
      pTrans->lastErrorNo = code;
3,380✔
2109
      pTrans->code = code;
3,380✔
2110
      if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
3,380✔
2111
        mInfo(
3,018!
2112
            "trans:%d, %s:%d (%d/%d at group %d) not able to execute since %s, current state("
2113
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2114
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2115
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2116
      } else if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
362!
2117
        mError(
×
2118
            "trans:%d, %s:%d (%d/%d at group %d) failed to execute since %s, current action("
2119
            "actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d)",
2120
            pTrans->id, mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, tstrerror(code),
2121
            pAction->actionType, pAction->msgSent, pAction->msgReceived);
2122
      }
2123
      mInfo("trans:%d, %s:%d (%d/%d at group %d) cannot execute next action, stop this group execution, %s", pTrans->id,
3,380!
2124
            mndTransStr(pAction->stage), pAction->id, action, numOfActions, groupId, str);
2125
      break;
3,380✔
2126
    }
2127

2128
    if (code == 0) {
3,834✔
2129
      pTrans->code = 0;
1,054✔
2130
      pTrans->actionPos++;
1,054✔
2131
      (*actionPos)++;
1,054✔
2132
      mInfo("trans:%d, %s:%d is finished and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
1,054!
2133
            pAction->id);
2134
      (void)taosThreadMutexUnlock(&pTrans->mutex);
1,054✔
2135
      code = mndTransSync(pMnode, pTrans);
1,054✔
2136
      (void)taosThreadMutexLock(&pTrans->mutex);
1,054✔
2137
      mInfo("trans:%d, try to reset all action msgSent except:%d", pTrans->id, pAction->id);
1,054!
2138
      for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
26,396✔
2139
        STransAction *pTmpAction = taosArrayGet(pTrans->redoActions, i);
25,342✔
2140
        int8_t       *msgSent = taosHashGet(pHash, &pTmpAction->id, sizeof(int32_t));
25,342✔
2141
        if (pTmpAction->id != pAction->id && pTmpAction->msgSent != *msgSent) {
25,342✔
2142
          pTmpAction->msgSent = *msgSent;
1,484✔
2143
          mInfo("trans:%d, action:%d, reset msgSent:%d", pTrans->id, pTmpAction->id, *msgSent);
1,484!
2144
        }
2145
      }
2146
      if (code != 0) {
1,054!
2147
        pTrans->actionPos--;
×
2148
        (*actionPos)--;
×
2149
        pTrans->code = terrno;
×
2150
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
2151
               mndTransStr(pAction->stage), pAction->id, terrstr());
2152
        break;
×
2153
      }
2154
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
2,780✔
2155
      mInfo("trans:%d, %s:%d is executed and still in progress and wait it finish", pTrans->id,
2,697!
2156
            mndTransStr(pAction->stage), pAction->id);
2157
      break;
2,697✔
2158
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
83!
2159
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
×
2160
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
83!
2161
            code, tstrerror(code));
2162
      pTrans->lastErrorNo = code;
83✔
2163
      taosMsleep(300);
83✔
2164
      action--;
83✔
2165
      continue;
83✔
2166
    } else {
2167
      terrno = code;
×
2168
      pTrans->lastErrorNo = code;
×
2169
      pTrans->code = code;
×
2170
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
×
2171
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2172
      break;
×
2173
    }
2174
  }
2175

2176
  return code;
7,647✔
2177
}
2178

2179
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
18,952✔
2180
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
18,952✔
2181
  (void)taosThreadMutexLock(&pTrans->mutex);
18,952✔
2182
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
18,952!
2183
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
18,952✔
2184
  }
2185
  (void)taosThreadMutexUnlock(&pTrans->mutex);
18,952✔
2186
  return code;
18,952✔
2187
}
2188

2189
static int32_t mndTransExecuteRedoActionGroup(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
2,591✔
2190
  int32_t total_code = TSDB_CODE_ACTION_IN_PROGRESS;
2,591✔
2191
  int32_t code = 0;
2,591✔
2192
  int32_t successCount = 0;
2,591✔
2193
  int32_t groupCount = taosHashGetSize(pTrans->redoGroupActions);
2,591✔
2194

2195
  SHashObj *pHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
2,591✔
2196
  if(pHash == NULL){
2,591!
2197
    mError("trans:%d, failed to init hash since %s", pTrans->id, terrstr());
×
2198
    return -1;
×
2199
  }
2200
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i) {
60,249✔
2201
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
57,658✔
2202
    int32_t       code = taosHashPut(pHash, &pAction->id, sizeof(int32_t), &pAction->msgSent, sizeof(int8_t));
57,658✔
2203
    if (code != 0) mError("trans:%d, failed to put hash since %s", pTrans->id, tstrerror(code));
57,658!
2204
  }
2205
  mTrace("trans:%d, temp save all action msgSent", pTrans->id);
2,591!
2206

2207
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
2,591!
2208
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
2,591✔
2209
  int32_t currentGroup = 1;
2,591✔
2210
  while (pIter) {
12,004✔
2211
    SArray **redoActions = pIter;
9,413✔
2212
    size_t   keyLen = 0;
9,413✔
2213
    int32_t *key = taosHashGetKey(pIter, &keyLen);
9,413✔
2214
    int32_t  actionCount = taosArrayGetSize(*redoActions);
9,413✔
2215
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
9,413!
2216
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2217
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
9,413✔
2218
                                             notSend, pHash);
2219
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
9,413✔
2220
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
3,018!
2221
            tstrerror(code));
2222
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
6,395✔
2223
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
3,761!
2224
    } else if (code != 0) {
2,634✔
2225
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
1,766!
2226
             tstrerror(code));
2227
    } else {
2228
      successCount++;
868✔
2229
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
868!
2230
    }
2231
    currentGroup++;
9,413✔
2232
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
9,413✔
2233
  }
2234

2235
  taosHashCleanup(pHash);
2,591✔
2236

2237
  if (successCount == groupCount) {
2,591✔
2238
    total_code = 0;
48✔
2239
  } else {
2240
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
2,543!
2241
  }
2242

2243
  return total_code;
2,591✔
2244
}
2245

2246
static int32_t mndTransExecuteRedoActionsParallel(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
3,223✔
2247
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
3,223✔
2248
  (void)taosThreadMutexLock(&pTrans->mutex);
3,223✔
2249

2250
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
3,223!
2251
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
3,223✔
2252
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
3,223!
2253
      code = 0;
368✔
2254
    } else {
2255
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
2,855✔
2256
      if (pAction != NULL && pAction->groupId == -1) {
2,855!
2257
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
264✔
2258
      } else {
2259
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
2,591✔
2260
        if (code == 0) {
2,591✔
2261
          if (pTrans->actionPos < numOfActions) {
48✔
2262
            code = TSDB_CODE_ACTION_IN_PROGRESS;
36✔
2263
          }
2264
        }
2265
      }
2266
    }
2267
  }
2268

2269
  (void)taosThreadMutexUnlock(&pTrans->mutex);
3,223✔
2270

2271
  return code;
3,223✔
2272
}
2273

2274
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
2275
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
2276
  (void)taosThreadMutexLock(&pTrans->mutex);
×
2277
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2278
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
2279
  }
2280
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
2281
  return code;
×
2282
}
2283

2284
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
54,490✔
2285
  bool    continueExec = true;
54,490✔
2286
  int32_t code = 0;
54,490✔
2287
  terrno = 0;
54,490✔
2288

2289
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
54,490✔
2290
  if (numOfActions == 0) goto _OVER;
54,490✔
2291

2292
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
20,203!
2293

2294
  for (int32_t action = 0; action < numOfActions; ++action) {
49,688✔
2295
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
29,485✔
2296
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
29,485✔
2297
    if (code != 0) {
29,485!
2298
      terrno = code;
×
2299
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
2300
             numOfActions, tstrerror(code));
2301
      return false;
×
2302
    }
2303
  }
2304

2305
_OVER:
20,203✔
2306
  pTrans->stage = TRN_STAGE_REDO_ACTION;
54,490✔
2307
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
54,490!
2308
  return continueExec;
54,490✔
2309
}
2310

2311
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
168,258✔
2312
  bool    continueExec = true;
168,258✔
2313
  int32_t code = 0;
168,258✔
2314
  terrno = 0;
168,258✔
2315

2316
  if (pTrans->exec == TRN_EXEC_SERIAL) {
168,258✔
2317
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
18,952✔
2318
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
149,306✔
2319
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
146,083✔
2320
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
3,223!
2321
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
3,223✔
2322
  } else {
2323
    code = TSDB_CODE_INTERNAL_ERROR;
×
2324
  }
2325

2326
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
168,262!
2327
      mndTransIsInSyncContext(topHalf)) {
4✔
2328
    pTrans->lastErrorNo = code;
×
2329
    pTrans->code = code;
×
2330
    mInfo(
×
2331
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2332
        "continueExec:%d, code:%s",
2333
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2334
    taosMsleep(100);
×
2335
    return true;
×
2336
  } else {
2337
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
168,258✔
2338
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
62,066!
2339
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2340
      return false;
62,066✔
2341
    }
2342
  }
2343
  terrno = code;
106,192✔
2344

2345
  if (code == 0) {
106,192✔
2346
    pTrans->code = 0;
45,403✔
2347
    pTrans->stage = TRN_STAGE_COMMIT;
45,403✔
2348
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
45,403!
2349
    continueExec = true;
45,403✔
2350
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
60,789!
2351
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
60,785!
2352
    continueExec = false;
60,785✔
2353
  } else {
2354
    pTrans->failedTimes++;
4✔
2355
    pTrans->code = terrno;
4✔
2356
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
4✔
2357
      if (pTrans->lastAction != 0) {
2!
2358
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
2359
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
2360
          if (pTrans->failedTimes < 6) {
×
2361
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2362
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
2363
            taosMsleep(1000);
×
2364
            continueExec = true;
×
2365
            return true;
×
2366
          }
2367
        }
2368
      }
2369

2370
      pTrans->stage = TRN_STAGE_ROLLBACK;
2✔
2371
      pTrans->actionPos = 0;
2✔
2372
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
2!
2373
             pTrans->actionPos);
2374
      continueExec = true;
2✔
2375
    } else {
2376
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
2!
2377
      continueExec = false;
2✔
2378
    }
2379
  }
2380

2381
  return continueExec;
106,192✔
2382
}
2383

2384
// execute in trans context
2385
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
45,403✔
2386
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
45,403!
2387

2388
  bool    continueExec = true;
45,403✔
2389
  int32_t code = mndTransCommit(pMnode, pTrans);
45,403✔
2390

2391
  if (code == 0) {
45,403✔
2392
    pTrans->code = 0;
45,391✔
2393
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
45,391✔
2394
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
45,391!
2395
    continueExec = true;
45,391✔
2396
  } else {
2397
    pTrans->code = terrno;
12✔
2398
    pTrans->failedTimes++;
12✔
2399
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
12!
2400
    continueExec = false;
12✔
2401
  }
2402

2403
  return continueExec;
45,403✔
2404
}
2405

2406
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
99,854✔
2407
  bool    continueExec = true;
99,854✔
2408
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
99,854✔
2409

2410
  if (code == 0) {
99,854!
2411
    pTrans->code = 0;
99,854✔
2412
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
99,854✔
2413
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
99,854!
2414
    continueExec = true;
99,854✔
2415
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
2416
    pTrans->code = 0;
×
2417
    pTrans->stage = TRN_STAGE_COMMIT;
×
2418
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
2419
    continueExec = true;
×
2420
  } else {
2421
    pTrans->code = terrno;
×
2422
    pTrans->failedTimes++;
×
2423
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2424
    continueExec = false;
×
2425
  }
2426

2427
  return continueExec;
99,854✔
2428
}
2429

2430
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
14✔
2431
  bool    continueExec = true;
14✔
2432
  int32_t code = 0;
14✔
2433

2434
  if (pTrans->exec == TRN_EXEC_SERIAL) {
14!
2435
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
2436
  } else {
2437
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
14✔
2438
  }
2439

2440
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
14✔
2441
  terrno = code;
12✔
2442

2443
  if (code == 0) {
12✔
2444
    pTrans->stage = TRN_STAGE_PRE_FINISH;
2✔
2445
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
2!
2446
    continueExec = true;
2✔
2447
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
10!
2448
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
10!
2449
    continueExec = false;
10✔
2450
  } else {
2451
    pTrans->failedTimes++;
×
2452
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2453
    continueExec = false;
×
2454
  }
2455

2456
  return continueExec;
12✔
2457
}
2458

2459
// in trans context
2460
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
2✔
2461
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
2!
2462

2463
  bool    continueExec = true;
2✔
2464
  int32_t code = mndTransRollback(pMnode, pTrans);
2✔
2465

2466
  if (code == 0) {
2!
2467
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
2✔
2468
    continueExec = true;
2✔
2469
  } else {
2470
    pTrans->failedTimes++;
×
2471
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2472
    continueExec = false;
×
2473
  }
2474

2475
  return continueExec;
2✔
2476
}
2477

2478
// excute in trans context
2479
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
2✔
2480
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
2!
2481

2482
  bool    continueExec = true;
2✔
2483
  int32_t code = mndTransPreFinish(pMnode, pTrans);
2✔
2484

2485
  if (code == 0) {
2!
2486
    pTrans->stage = TRN_STAGE_FINISH;
2✔
2487
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
2!
2488
    continueExec = true;
2✔
2489
  } else {
2490
    pTrans->failedTimes++;
×
2491
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
2492
    continueExec = false;
×
2493
  }
2494

2495
  return continueExec;
2✔
2496
}
2497

2498
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
99,858✔
2499
  bool continueExec = false;
99,858✔
2500
  if (topHalf) return continueExec;
99,858✔
2501

2502
  SSdbRaw *pRaw = mndTransEncode(pTrans);
54,465✔
2503
  if (pRaw == NULL) {
54,465!
2504
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
2505
    return false;
×
2506
  }
2507
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
54,465!
2508

2509
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
54,465✔
2510
  if (code != 0) {
54,465!
2511
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
2512
  }
2513

2514
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
54,465!
2515
        pTrans->failedTimes, pTrans->createdTime);
2516
  return continueExec;
54,465✔
2517
}
2518

2519
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
222,735✔
2520
  bool continueExec = true;
222,735✔
2521

2522
  while (continueExec) {
636,126✔
2523
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
413,391!
2524
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2525
    pTrans->lastExecTime = taosGetTimestampMs();
413,391✔
2526
    switch (pTrans->stage) {
413,391!
2527
      case TRN_STAGE_PREPARE:
×
2528
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2529
        break;
×
2530
      case TRN_STAGE_REDO_ACTION:
168,258✔
2531
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
168,258✔
2532
        break;
168,258✔
2533
      case TRN_STAGE_COMMIT:
45,403✔
2534
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
45,403✔
2535
        break;
45,403✔
2536
      case TRN_STAGE_COMMIT_ACTION:
99,854✔
2537
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
99,854✔
2538
        break;
99,854✔
2539
      case TRN_STAGE_ROLLBACK:
2✔
2540
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
2✔
2541
        break;
2✔
2542
      case TRN_STAGE_UNDO_ACTION:
14✔
2543
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
14✔
2544
        break;
14✔
2545
      case TRN_STAGE_PRE_FINISH:
2✔
2546
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
2✔
2547
        break;
2✔
2548
      case TRN_STAGE_FINISH:
99,858✔
2549
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
99,858✔
2550
        break;
99,858✔
2551
      default:
×
2552
        continueExec = false;
×
2553
        break;
×
2554
    }
2555
  }
2556

2557
  mndTransSendRpcRsp(pMnode, pTrans);
222,735✔
2558
}
222,735✔
2559

2560
// start trans, pullup, receive rsp, kill
2561
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
106,311✔
2562
  bool topHalf = true;
106,311✔
2563
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
106,311✔
2564
}
106,311✔
2565

2566
// update trans
2567
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
116,424✔
2568
  bool topHalf = false;
116,424✔
2569
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
116,424✔
2570
}
116,424✔
2571

2572
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
28,084✔
2573
  mTrace("start to process trans timer");
28,084✔
2574
  mndTransPullup(pReq->info.node);
28,084✔
2575
  return 0;
28,084✔
2576
}
2577

2578
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
2579
  SArray *pArray = NULL;
×
2580
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
2581
    pArray = pTrans->redoActions;
×
2582
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2583
    pArray = pTrans->undoActions;
×
2584
  } else {
2585
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2586
  }
2587

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

2611
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
×
2612
  mndTransExecute(pMnode, pTrans, true);
×
2613
  return 0;
×
2614
}
2615

2616
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1✔
2617
  SMnode       *pMnode = pReq->info.node;
1✔
2618
  SKillTransReq killReq = {0};
1✔
2619
  int32_t       code = -1;
1✔
2620
  STrans       *pTrans = NULL;
1✔
2621

2622
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
2623
    code = TSDB_CODE_INVALID_MSG;
×
2624
    goto _OVER;
×
2625
  }
2626

2627
  mInfo("trans:%d, start to kill, force:%d", killReq.transId, tsForceKillTrans);
1!
2628
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1!
2629
    goto _OVER;
1✔
2630
  }
2631

2632
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
2633
  if (pTrans == NULL) {
×
2634
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
2635
    if (terrno != 0) code = terrno;
×
2636
    goto _OVER;
×
2637
  }
2638

2639
  code = mndKillTrans(pMnode, pTrans);
×
2640

2641
_OVER:
1✔
2642
  if (code != 0) {
1!
2643
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1!
2644
  }
2645

2646
  mndReleaseTrans(pMnode, pTrans);
1✔
2647
  TAOS_RETURN(code);
1✔
2648
}
2649

2650
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
79✔
2651

2652
void mndTransPullup(SMnode *pMnode) {
28,588✔
2653
  SSdb   *pSdb = pMnode->pSdb;
28,588✔
2654
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
28,588✔
2655
  if (pArray == NULL) return;
28,588!
2656

2657
  void *pIter = NULL;
28,588✔
2658
  while (1) {
2,819✔
2659
    STrans *pTrans = NULL;
31,407✔
2660
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
31,407✔
2661
    if (pIter == NULL) break;
31,407✔
2662
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
5,638!
2663
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2664
    }
2665
    sdbRelease(pSdb, pTrans);
2,819✔
2666
  }
2667

2668
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
28,588✔
2669

2670
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
31,407✔
2671
    int32_t *pTransId = taosArrayGet(pArray, i);
2,819✔
2672
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
2,819✔
2673
    if (pTrans != NULL) {
2,819!
2674
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
2,819!
2675
      mndTransExecute(pMnode, pTrans, false);
2,819✔
2676
    }
2677
    mndReleaseTrans(pMnode, pTrans);
2,819✔
2678
  }
2679
  taosArrayDestroy(pArray);
28,588✔
2680
}
2681

2682
static char *formatTimestamp(char *buf, int64_t val, int precision) {
41,470✔
2683
  time_t tt;
2684
  if (precision == TSDB_TIME_PRECISION_MICRO) {
41,470!
2685
    tt = (time_t)(val / 1000000);
×
2686
  }
2687
  if (precision == TSDB_TIME_PRECISION_NANO) {
41,470!
2688
    tt = (time_t)(val / 1000000000);
×
2689
  } else {
2690
    tt = (time_t)(val / 1000);
41,470✔
2691
  }
2692

2693
  struct tm tm;
2694
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
41,470!
2695
    mError("failed to get local time");
×
2696
    return NULL;
×
2697
  }
2698
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
41,470✔
2699

2700
  if (precision == TSDB_TIME_PRECISION_MICRO) {
41,470!
2701
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2702
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
41,470!
2703
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2704
  } else {
2705
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
41,470✔
2706
  }
2707

2708
  return buf;
41,470✔
2709
}
2710

2711
static void mndTransLogAction(STrans *pTrans) {
826✔
2712
  char    detail[512] = {0};
826✔
2713
  int32_t len = 0;
826✔
2714
  int32_t index = 0;
826✔
2715

2716
  if (pTrans->stage == TRN_STAGE_PREPARE) {
826!
2717
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2718
      len = 0;
×
2719
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2720
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2721
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2722
                      sdbStatusName(pAction->pRaw->status));
×
2723
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2724
    }
2725
  }
2726

2727
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
826!
2728
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
28,090✔
2729
      len = 0;
27,264✔
2730
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
27,264✔
2731
      if (pAction->actionType == TRANS_ACTION_MSG) {
27,264✔
2732
        char bufStart[40] = {0};
20,713✔
2733
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
20,713✔
2734

2735
        char endStart[40] = {0};
20,713✔
2736
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
20,713✔
2737
        len += snprintf(detail + len, sizeof(detail) - len,
41,426!
2738
                        "action:%d, %s:%d msgType:%s,"
2739
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2740
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
20,713✔
2741
                        pAction->msgReceived, bufStart, endStart);
20,713✔
2742

2743
        SEpSet epset = pAction->epSet;
20,713✔
2744
        if (epset.numOfEps > 0) {
20,713!
2745
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
20,713✔
2746
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
46,532✔
2747
            len +=
25,819✔
2748
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
25,819✔
2749
          }
2750
        }
2751

2752
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
20,713✔
2753
                        tstrerror(pAction->errCode));
2754
      } else {
2755
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
6,551✔
2756
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
6,551✔
2757
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
6,551✔
2758
      }
2759
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
27,264!
2760
    }
2761
  }
2762

2763
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
826!
2764
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
×
2765
      len = 0;
×
2766
      STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
×
2767
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2768
                      mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type),
×
2769
                      sdbStatusName(pAction->pRaw->status));
×
2770
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2771
    }
2772

2773
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
×
2774
      len = 0;
×
2775
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
2776
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
2777
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
2778
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2779
        ;
2780
      } else {
2781
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2782
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2783
                        sdbStatusName(pAction->pRaw->status));
×
2784
      }
2785
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2786
    }
2787
  }
2788
}
826✔
2789

2790
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
873✔
2791
  SMnode *pMnode = pReq->info.node;
873✔
2792
  SSdb   *pSdb = pMnode->pSdb;
873✔
2793
  int32_t numOfRows = 0;
873✔
2794
  STrans *pTrans = NULL;
873✔
2795
  int32_t cols = 0;
873✔
2796
  int32_t code = 0;
873✔
2797
  int32_t lino = 0;
873✔
2798

2799
  while (numOfRows < rows) {
1,699!
2800
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
1,699✔
2801
    if (pShow->pIter == NULL) break;
1,699✔
2802

2803
    cols = 0;
826✔
2804

2805
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
826✔
2806
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
826!
2807

2808
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
826✔
2809
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
826!
2810
                        _OVER);
2811

2812
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
826✔
2813
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
826✔
2814
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
826✔
2815
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
826!
2816

2817
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
826✔
2818
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
826✔
2819
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
826✔
2820
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
826!
2821

2822
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
826✔
2823
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
826✔
2824
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
826✔
2825
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
826!
2826

2827
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
826✔
2828
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
826✔
2829
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
826✔
2830
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
826!
2831

2832
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
826!
2833
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
826✔
2834
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
826✔
2835
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
826✔
2836
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
826!
2837

2838
    /*
2839
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2840
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2841
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2842
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2843
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2844
    */
2845

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

2850
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
826✔
2851
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
826!
2852
                        _OVER);
2853

2854
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
826✔
2855
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
826✔
2856
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
2,478✔
2857
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
826✔
2858
    SEpSet  epset = pTrans->lastEpset;
826✔
2859
    if (epset.numOfEps > 0) {
826✔
2860
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
1,634!
2861
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
1,634✔
2862
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
2,306✔
2863
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
1,489✔
2864
      }
2865
    }
2866
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
826✔
2867
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
826✔
2868
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
826!
2869

2870
    mndTransLogAction(pTrans);
826✔
2871

2872
    numOfRows++;
826✔
2873
    sdbRelease(pSdb, pTrans);
826✔
2874
  }
2875

2876
_OVER:
×
2877
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
873!
2878
  pShow->numOfRows += numOfRows;
873✔
2879
  return numOfRows;
873✔
2880
}
2881

2882
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
352✔
2883
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2884
  int32_t code = 0;
352✔
2885
  int32_t lino = 0;
352✔
2886
  int32_t len = 0;
352✔
2887

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

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

2908
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
352✔
2909
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2910
  int32_t code = 0;
352✔
2911
  int32_t lino = 0;
352✔
2912
  int32_t len = 0;
352✔
2913
  int32_t cols = 0;
352✔
2914

2915
  cols = 0;
352✔
2916

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

2919
  if (pAction->actionType == TRANS_ACTION_MSG) {
352✔
2920
    int32_t len = 0;
288✔
2921

2922
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
288✔
2923
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
288!
2924
                    pAction->msgSent, pAction->msgReceived);
288✔
2925
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
288✔
2926
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
288✔
2927
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
288✔
2928
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
288!
2929

2930
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
288✔
2931
    len = 0;
288✔
2932
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
288✔
2933
                    tstrerror(pAction->errCode));
2934
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
288✔
2935
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
288✔
2936
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
288✔
2937
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
288!
2938

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

2953
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
288✔
2954
    len = 0;
288✔
2955
    char bufStart[40] = {0};
288✔
2956
    if (pAction->startTime > 0) (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
288✔
2957
    char bufEnd[40] = {0};
288✔
2958
    if (pAction->endTime > 0) (void)formatTimestamp(bufEnd, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
288✔
2959
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
288✔
2960
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
288✔
2961
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
288✔
2962
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
288✔
2963
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
288!
2964

2965
  } else {
2966
    int32_t len = 0;
64✔
2967

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

2982
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
64✔
2983
    len = 0;
64✔
2984
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
64✔
2985
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
64✔
2986
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
64✔
2987
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
64✔
2988
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
64!
2989

2990
    char target[TSDB_TRANS_TARGET_LEN] = "";
64✔
2991
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
64✔
2992
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
64✔
2993
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
64✔
2994
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
64!
2995

2996
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
64✔
2997
    len = 0;
64✔
2998
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
64✔
2999
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
64✔
3000
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
64✔
3001
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
64✔
3002
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
64!
3003
  }
3004

3005
_OVER:
352✔
3006
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
352!
3007
}
3008

3009
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
4✔
3010
  if (stage == TRN_STAGE_PREPARE) {
4!
3011
    return pTrans->prepareActions;
×
3012
  }
3013
  if (stage == TRN_STAGE_REDO_ACTION) {
4!
3014
    return pTrans->redoActions;
4✔
3015
  }
3016
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
3017
    return pTrans->commitActions;
×
3018
  }
3019
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
3020
    return pTrans->undoActions;
×
3021
  }
3022
  return NULL;
×
3023
}
3024

3025
typedef struct STransDetailIter {
3026
  void     *pIter;
3027
  STrans   *pTrans;
3028
  ETrnStage stage;
3029
  int32_t   num;
3030
} STransDetailIter;
3031

3032
static void mndTransShowActions(SSdb *pSdb, STransDetailIter *pShowIter, SShowObj *pShow, SSDataBlock *pBlock,
4✔
3033
                                int32_t rows, int32_t *numOfRows, SArray *pActions, int32_t end, int32_t start) {
3034
  int32_t actionNum = taosArrayGetSize(pActions);
4✔
3035
  mInfo("stage:%s, Actions num:%d", mndTransStr(pShowIter->stage), actionNum);
4!
3036

3037
  for (int32_t i = start; i < actionNum; ++i) {
354✔
3038
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
352✔
3039
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
352✔
3040
    (*numOfRows)++;
352✔
3041
    if (*numOfRows >= rows) break;
352✔
3042
  }
3043

3044
  if (*numOfRows == end) {
4✔
3045
    sdbRelease(pSdb, pShowIter->pTrans);
2✔
3046
    pShowIter->pTrans = NULL;
2✔
3047
    pShowIter->num = 0;
2✔
3048
  } else {
3049
    pShowIter->pTrans = pShowIter->pTrans;
2✔
3050
    pShowIter->stage = pShowIter->pTrans->stage;
2✔
3051
    pShowIter->num += (*numOfRows);
2✔
3052
  }
3053
}
4✔
3054

3055
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
6✔
3056
  SMnode *pMnode = pReq->info.node;
6✔
3057
  SSdb   *pSdb = pMnode->pSdb;
6✔
3058
  int32_t numOfRows = 0;
6✔
3059

3060
  int32_t code = 0;
6✔
3061
  int32_t lino = 0;
6✔
3062

3063
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
6!
3064
        pShow->pIter);
3065

3066
  if (pShow->pIter == NULL) {
6✔
3067
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
2!
3068
    if (pShow->pIter == NULL) {
2!
3069
      mError("failed to malloc for pShow->pIter");
×
3070
      return 0;
×
3071
    }
3072
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
2✔
3073
  }
3074

3075
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
6✔
3076

3077
  while (numOfRows < rows) {
6!
3078
    if (pShowIter->pTrans == NULL) {
6✔
3079
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
4✔
3080
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
4!
3081
      if (pShowIter->pIter == NULL) break;
4✔
3082
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
2!
3083
            pShowIter->pTrans->stage, pShowIter->num);
3084

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

3087
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
2✔
3088
      break;
2✔
3089
    } else {
3090
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
2!
3091
            pShowIter->num);
3092
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
2✔
3093

3094
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
4✔
3095
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
2✔
3096
      break;
2✔
3097
    }
3098
  }
3099

3100
_OVER:
×
3101
  pShow->numOfRows += numOfRows;
6✔
3102

3103
  if (code != 0) {
6!
3104
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
3105
  } else {
3106
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
6!
3107
  }
3108
  if (numOfRows == 0) {
6✔
3109
    taosMemoryFree(pShow->pIter);
2!
3110
    pShow->pIter = NULL;
2✔
3111
  }
3112
  return numOfRows;
6✔
3113
}
3114

3115
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
3116
  SSdb *pSdb = pMnode->pSdb;
×
3117
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
3118
}
×
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