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

taosdata / TDengine / #4192

30 May 2025 03:55AM UTC coverage: 63.023% (-0.2%) from 63.267%
#4192

push

travis-ci

web-flow
fix:defined col bind in interlace mode (#31246)

157832 of 318864 branches covered (49.5%)

Branch coverage included in aggregate %.

1 of 3 new or added lines in 1 file covered. (33.33%)

2934 existing lines in 172 files now uncovered.

243367 of 317732 relevant lines covered (76.6%)

17346426.62 hits per line

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

64.46
/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; }
446,188✔
60

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

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

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

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

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

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

100
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
2,189✔
101
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
2,189✔
102

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

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

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

115
  for (int32_t i = 0; i < actionNum; ++i) {
2,122,184✔
116
    STransAction *pAction = taosArrayGet(pArray, i);
1,420,596✔
117
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,420,596✔
118
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
916,846✔
119
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
503,750!
120
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
503,750✔
121
    } else {
122
      // empty
123
    }
124
    rawDataLen += sizeof(int8_t);
1,420,596✔
125
  }
126

127
  return rawDataLen;
701,588✔
128
}
129

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

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

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

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

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

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

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

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

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

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

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

229
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
175,397!
230

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

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

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

263
  terrno = 0;
175,397✔
264

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

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

277
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
66,782✔
278
  if (pAction->groupId < 0) return 0;
66,782✔
279
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
65,179✔
280
  if (redoAction == NULL) {
65,179✔
281
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
13,976✔
282
    if (array != NULL) {
13,976!
283
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
13,976!
UNCOV
284
        mInfo("failed put action into redo group actions");
×
UNCOV
285
        return TSDB_CODE_INTERNAL_ERROR;
×
286
      }
287
    }
288
    redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
13,976✔
289
  }
290
  if (redoAction != NULL) {
65,179!
291
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
130,358!
292
  }
293
  return 0;
65,179✔
294
}
295

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

517
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
328,404!
518

519
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
328,404✔
520

521
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
328,404!
522
  for (int32_t i = 0; i < arbgroupIdNum; ++i) {
328,464✔
523
    int32_t arbGroupId = 0;
60✔
524
    SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
60!
525
    if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
60!
526
  }
527

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

537
  if (sver > TRANS_VER2_NUMBER) {
328,404!
538
    int32_t groupNum = -1;
328,404✔
539
    SDB_GET_INT32(pRaw, dataPos, &groupNum, _OVER)
328,404!
540
    for (int32_t i = 0; i < groupNum; ++i) {
342,380✔
541
      int32_t groupId = -1;
13,976✔
542
      int32_t groupPos = -1;
13,976✔
543
      SDB_GET_INT32(pRaw, dataPos, &groupId, _OVER)
13,976!
544
      SDB_GET_INT32(pRaw, dataPos, &groupPos, _OVER)
13,976!
545
      if ((terrno = taosHashPut(pTrans->groupActionPos, &groupId, sizeof(int32_t), &groupPos, sizeof(int32_t))) != 0)
13,976!
UNCOV
546
        goto _OVER;
×
547
    }
548
  }
549

550
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
328,404!
551

552
  terrno = 0;
328,404✔
553

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

563
  if (pTrans != NULL) {
328,404!
564
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
328,404✔
565
  }
566
  return pRow;
328,404✔
567
}
568

569
static const char *mndTransStr(ETrnStage stage) {
2,562,837✔
570
  switch (stage) {
2,562,837!
571
    case TRN_STAGE_PREPARE:
170,601✔
572
      return "prepare";
170,601✔
573
    case TRN_STAGE_REDO_ACTION:
1,029,533✔
574
      return "redoAction";
1,029,533✔
575
    case TRN_STAGE_ROLLBACK:
10✔
576
      return "rollback";
10✔
577
    case TRN_STAGE_UNDO_ACTION:
52,718✔
578
      return "undoAction";
52,718✔
579
    case TRN_STAGE_COMMIT:
280,786✔
580
      return "commit";
280,786✔
581
    case TRN_STAGE_COMMIT_ACTION:
615,848✔
582
      return "commitAction";
615,848✔
583
    case TRN_STAGE_FINISH:
413,331✔
584
      return "finished";
413,331✔
585
    case TRN_STAGE_PRE_FINISH:
10✔
586
      return "pre-finish";
10✔
UNCOV
587
    default:
×
UNCOV
588
      return "invalid";
×
589
  }
590
}
591

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

603
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
766,560✔
604
  if (pAction != NULL) {
766,560✔
605
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
624,639✔
606
      pTrans->lastAction = pAction->id;
417,542✔
607
      pTrans->lastMsgType = pAction->msgType;
417,542✔
608
      pTrans->lastEpset = pAction->epSet;
417,542✔
609
      pTrans->lastErrorNo = pAction->errCode;
417,542✔
610
    }
611
  } else {
612
    pTrans->lastAction = 0;
141,921✔
613
    pTrans->lastMsgType = 0;
141,921✔
614
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
141,921✔
615
    pTrans->lastErrorNo = 0;
141,921✔
616
  }
617
}
766,560✔
618

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

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

627
static TransCbFp mndTransGetCbFp(ETrnFunc ftype) {
2,058✔
628
  switch (ftype) {
2,058!
UNCOV
629
    case TRANS_START_FUNC_TEST:
×
UNCOV
630
      return mndTransTestStartFunc;
×
UNCOV
631
    case TRANS_STOP_FUNC_TEST:
×
UNCOV
632
      return mndTransTestStopFunc;
×
633
    case TRANS_START_FUNC_MQ_REB:
1,029✔
634
      return mndRebCntInc;
1,029✔
635
    case TRANS_STOP_FUNC_MQ_REB:
1,029✔
636
      return mndRebCntDec;
1,029✔
UNCOV
637
    default:
×
UNCOV
638
      return NULL;
×
639
  }
640
}
641

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

646
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
62,068✔
647

648
  if (pTrans->startFunc > 0) {
62,068✔
649
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
1,029✔
650
    if (fp) {
1,029!
651
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,029✔
652
    }
653
    // pTrans->startFunc = 0;
654
  }
655

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

661
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
62,068!
UNCOV
662
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
×
UNCOV
663
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
×
664
  }
665

666
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
62,068!
UNCOV
667
    pTrans->stage = TRN_STAGE_FINISH;
×
UNCOV
668
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pTrans->id);
×
669
  }
670

671
  return 0;
62,068✔
672
}
673

674
void mndTransDropData(STrans *pTrans) {
381,226✔
675
  if (pTrans->prepareActions != NULL) {
381,226!
676
    mndTransDropActions(pTrans->prepareActions);
381,226✔
677
    pTrans->prepareActions = NULL;
381,226✔
678
  }
679
  if (pTrans->redoActions != NULL) {
381,226!
680
    mndTransDropActions(pTrans->redoActions);
381,226✔
681
    pTrans->redoActions = NULL;
381,226✔
682
  }
683
  if (pTrans->undoActions != NULL) {
381,226!
684
    mndTransDropActions(pTrans->undoActions);
381,226✔
685
    pTrans->undoActions = NULL;
381,226✔
686
  }
687
  if (pTrans->commitActions != NULL) {
381,226!
688
    mndTransDropActions(pTrans->commitActions);
381,226✔
689
    pTrans->commitActions = NULL;
381,226✔
690
  }
691
  if (pTrans->redoGroupActions != NULL) {
381,226✔
692
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
57,161✔
693
    while (pIter) {
71,817✔
694
      SArray **redoActions = pIter;
14,656✔
695
      taosArrayDestroy(*redoActions);
14,656✔
696
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
14,656✔
697
    }
698

699
    taosHashCleanup(pTrans->redoGroupActions);
57,161✔
700
    pTrans->redoGroupActions = NULL;
57,161✔
701
  }
702
  if (pTrans->groupActionPos != NULL) {
381,226✔
703
    taosHashCleanup(pTrans->groupActionPos);
57,161✔
704
    pTrans->groupActionPos = NULL;
57,161✔
705
  }
706
  if (pTrans->arbGroupIds != NULL) {
381,226!
707
    taosHashCleanup(pTrans->arbGroupIds);
381,226✔
708
  }
709
  if (pTrans->pRpcArray != NULL) {
381,226✔
710
    taosArrayDestroy(pTrans->pRpcArray);
52,822✔
711
    pTrans->pRpcArray = NULL;
52,822✔
712
  }
713
  if (pTrans->rpcRsp != NULL) {
381,226✔
714
    taosMemoryFree(pTrans->rpcRsp);
15,875!
715
    pTrans->rpcRsp = NULL;
15,875✔
716
    pTrans->rpcRspLen = 0;
15,875✔
717
  }
718
  if (pTrans->param != NULL) {
381,226!
UNCOV
719
    taosMemoryFree(pTrans->param);
×
UNCOV
720
    pTrans->param = NULL;
×
UNCOV
721
    pTrans->paramLen = 0;
×
722
  }
723
  (void)taosThreadMutexDestroy(&pTrans->mutex);
381,226✔
724
}
381,226✔
725

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

730
  if (pTrans->stopFunc > 0 && callFunc) {
195,214✔
731
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
1,029✔
732
    if (fp) {
1,029!
733
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,029✔
734
    }
735
    // pTrans->stopFunc = 0;
736
  }
737

738
  mndTransDropData(pTrans);
195,214✔
739
  return 0;
195,214✔
740
}
741

742
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
285,208✔
743
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
887,165✔
744
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
601,957✔
745
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
601,957✔
746
    pOldAction->rawWritten = pNewAction->rawWritten;
601,957✔
747
    pOldAction->msgSent = pNewAction->msgSent;
601,957✔
748
    pOldAction->msgReceived = pNewAction->msgReceived;
601,957✔
749
    pOldAction->errCode = pNewAction->errCode;
601,957✔
750
  }
751
}
285,208✔
752

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

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

766
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
71,302✔
767
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
71,302✔
768
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
71,302✔
769
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
71,302✔
770
  pOld->stage = pNew->stage;
71,302✔
771
  pOld->actionPos = pNew->actionPos;
71,302✔
772

773
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
71,302✔
774
  while (pIter != NULL) {
77,220✔
775
    int32_t newActionPos = *(int32_t *)pIter;
5,918✔
776

777
    size_t   keylen = 0;
5,918✔
778
    int32_t *groupId = taosHashGetKey(pIter, &keylen);
5,918✔
779

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

786
    pIter = taosHashIterate(pNew->groupActionPos, pIter);
5,918✔
787
  }
788

789
  if (pOld->stage == TRN_STAGE_COMMIT) {
71,302✔
790
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
61,841✔
791
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
61,841!
792
  }
793

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

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

804
  return 0;
71,302✔
805
}
806

807
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
294,758✔
808
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
294,758✔
809
  if (pTrans == NULL) {
294,758✔
810
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
3,177✔
811
  }
812
  return pTrans;
294,758✔
813
}
814

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

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

830
  if (opername != NULL) {
52,822!
831
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
52,822✔
832
  }
833

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

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

864
  if (pReq != NULL) {
52,822✔
865
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
65,182!
UNCOV
866
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
867
      return NULL;
×
868
    }
869
    pTrans->originRpcType = pReq->msgType;
32,591✔
870
  }
871

872
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
52,822!
873

874
  mTrace("trans:%d, local object is created, data:%p", pTrans->id, pTrans);
52,822✔
875
  return pTrans;
52,822✔
876
}
877

878
static void mndTransDropActions(SArray *pArray) {
1,524,904✔
879
  int32_t size = taosArrayGetSize(pArray);
1,524,904✔
880
  for (int32_t i = 0; i < size; ++i) {
4,550,795✔
881
    STransAction *pAction = taosArrayGet(pArray, i);
3,025,891✔
882
    if (pAction->actionType == TRANS_ACTION_RAW) {
3,025,891✔
883
      taosMemoryFreeClear(pAction->pRaw);
1,928,252!
884
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,097,639!
885
      taosMemoryFreeClear(pAction->pCont);
1,097,639!
886
    } else {
887
      // nothing
888
    }
889
  }
890

891
  taosArrayDestroy(pArray);
1,524,904✔
892
}
1,524,904✔
893

894
void mndTransDrop(STrans *pTrans) {
54,033✔
895
  if (pTrans != NULL) {
54,033✔
896
    mndTransDropData(pTrans);
52,822✔
897
    mTrace("trans:%d, local object is freed, data:%p", pTrans->id, pTrans);
52,822✔
898
    taosMemoryFreeClear(pTrans);
52,822!
899
  }
900
}
54,033✔
901

902
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
414,242✔
903
  pAction->id = taosArrayGetSize(pArray);
414,242✔
904

905
  void *ptr = taosArrayPush(pArray, pAction);
414,242✔
906
  if (ptr == NULL) {
414,242!
UNCOV
907
    TAOS_RETURN(terrno);
×
908
  }
909

910
  return 0;
414,242✔
911
}
912

913
static int32_t mndTransAddActionToGroup(STrans *pTrans, STransAction *pAction) {
19,372✔
914
  if (pTrans->exec != TRN_EXEC_GROUP_PARALLEL) return 0;
19,372✔
915

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

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

945
  return 0;
1,210✔
946
}
947

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

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

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

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

988
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
15,780✔
989
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
15,780✔
990
  return mndTransAppendAction(pTrans->undoActions, &action);
15,780✔
991
}
992

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

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

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

1016
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
36,852✔
1017
  pAction->stage = TRN_STAGE_UNDO_ACTION;
36,852✔
1018
  pAction->actionType = TRANS_ACTION_MSG;
36,852✔
1019
  return mndTransAppendAction(pTrans->undoActions, pAction);
36,852✔
1020
}
1021

1022
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
15,875✔
1023
  pTrans->rpcRsp = pCont;
15,875✔
1024
  pTrans->rpcRspLen = contLen;
15,875✔
1025
}
15,875✔
1026

1027
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
1,019✔
1028
  pTrans->startFunc = startFunc;
1,019✔
1029
  pTrans->stopFunc = stopFunc;
1,019✔
1030
  pTrans->param = param;
1,019✔
1031
  pTrans->paramLen = paramLen;
1,019✔
1032
}
1,019✔
1033

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

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

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

UNCOV
1055
        sdbRelease(pMnode->pSdb, pTrans);
×
UNCOV
1056
        break;
×
1057
      }
1058
    }
1059

UNCOV
1060
    sdbRelease(pMnode->pSdb, pTrans);
×
1061
  }
UNCOV
1062
  return code;
×
1063
}
1064

1065
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
32,799✔
1066
  if (dbname != NULL) {
32,799!
1067
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
32,799✔
1068
  }
1069
  if (stbname != NULL) {
32,799✔
1070
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
24,327✔
1071
  }
1072
}
32,799✔
1073

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

1080
void mndTransSetSerial(STrans *pTrans) {
2,493✔
1081
  mInfo("trans:%d, set Serial", pTrans->id);
2,493!
1082
  pTrans->exec = TRN_EXEC_SERIAL;
2,493✔
1083
}
2,493✔
1084

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

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

UNCOV
1095
void mndTransSetBeKilled(STrans *pTrans, bool ableToBeKilled) { pTrans->ableToBeKilled = ableToBeKilled; }
×
1096

1097
void mndTransSetKillMode(STrans *pTrans, ETrnKillMode killMode) {
8✔
1098
  pTrans->ableToBeKilled = true; 
8✔
1099
  pTrans->killMode = killMode; 
8✔
1100
}
8✔
1101

1102
void mndTransSetChangeless(STrans *pTrans) { pTrans->changeless = true; }
34✔
1103

1104
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
4,759✔
1105

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

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

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

1132
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
6,193✔
1133
  if (conflict[0] == 0) return false;
6,193!
1134
  if (taosStrcasecmp(conflict, pTrans->dbname) == 0) return true;
6,193✔
1135
  return false;
5,909✔
1136
}
1137

1138
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
23,437✔
1139
  if (conflict[0] == 0) return false;
23,437✔
1140
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
21,656✔
1141
  return false;
21,547✔
1142
}
1143

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

1155
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
147,622✔
1156
  STrans *pTrans = NULL;
147,622✔
1157
  void   *pIter = NULL;
147,622✔
1158
  bool    conflict = false;
147,622✔
1159

1160
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
147,622✔
1161

1162
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
100,416✔
1163
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
100,416!
1164

1165
  while (1) {
1166
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
124,669✔
1167
    if (pIter == NULL) break;
124,669✔
1168

1169
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
24,253✔
1170

1171
    if (pNew->conflict == TRN_CONFLICT_DB) {
24,253✔
1172
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
4,398✔
1173
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4,398✔
1174
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
3,902✔
1175
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
3,902✔
1176
      }
1177
    }
1178

1179
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
24,253✔
1180
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
19,840✔
1181
      if (pTrans->conflict == TRN_CONFLICT_DB) {
19,840✔
1182
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
2,291✔
1183
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
2,291✔
1184
      }
1185
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
19,840✔
1186
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
17,244✔
1187
      }
1188
    }
1189

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

1208
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
24,253✔
1209
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
5!
UNCOV
1210
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1211
      } else {
1212
        mndTransLogConflict(pNew, pTrans, false, &conflict);
5✔
1213
      }
1214
    }
1215

1216
    sdbRelease(pMnode->pSdb, pTrans);
24,253✔
1217
  }
1218

1219
  return conflict;
100,416✔
1220
}
1221

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

1232
  if (mndCheckTransConflict(pMnode, pTrans)) {
147,622✔
1233
    code = TSDB_CODE_MND_TRANS_CONFLICT;
418✔
1234
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
418!
1235
    TAOS_RETURN(code);
418✔
1236
  }
1237

1238
  TAOS_RETURN(code);
147,204✔
1239
}
1240

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

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

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

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

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

1276
  TAOS_RETURN(code);
356✔
1277
}
1278

1279
static bool mndTransActionsOfSameType(SArray *pActions) {
120,779✔
1280
  int32_t size = taosArrayGetSize(pActions);
120,779✔
1281
  ETrnAct lastActType = TRANS_ACTION_NULL;
120,779✔
1282
  bool    same = true;
120,779✔
1283
  for (int32_t i = 0; i < size; ++i) {
469,423✔
1284
    STransAction *pAction = taosArrayGet(pActions, i);
348,644✔
1285
    if (i > 0) {
348,644✔
1286
      if (lastActType != pAction->actionType) {
259,994!
UNCOV
1287
        same = false;
×
UNCOV
1288
        break;
×
1289
      }
1290
    }
1291
    lastActType = pAction->actionType;
348,644✔
1292
  }
1293
  return same;
120,779✔
1294
}
1295

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

1305
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
49,640✔
1306
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
18,772!
UNCOV
1307
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
UNCOV
1308
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
UNCOV
1309
        TAOS_RETURN(code);
×
1310
      }
1311
    }
1312
  }
1313

1314
  TAOS_RETURN(code);
52,367✔
1315
}
1316

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

1330
  TAOS_RETURN(code);
52,367✔
1331
}
1332

1333
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
52,367✔
1334
  int32_t code = 0;
52,367✔
1335
  if (pTrans == NULL) {
52,367!
1336
    return TSDB_CODE_INVALID_PARA;
×
1337
  }
1338

1339
  mInfo("trans:%d, action list:", pTrans->id);
52,367!
1340
  int32_t index = 0;
52,367✔
1341
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
77,739✔
1342
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
25,372✔
1343
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
25,372!
1344
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1345
  }
1346

1347
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
148,323✔
1348
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
95,956✔
1349
    if (pAction->actionType == TRANS_ACTION_MSG) {
95,956✔
1350
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
94,547!
1351
            TMSG_INFO(pAction->msgType));
1352
      ;
1353
    } else {
1354
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
1,409!
1355
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1356
    }
1357
  }
1358

1359
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
292,187✔
1360
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
239,820✔
1361
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
239,820!
1362
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1363
  }
1364

1365
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
104,999✔
1366
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
52,632✔
1367
    if (pAction->actionType == TRANS_ACTION_MSG) {
52,632✔
1368
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
36,852!
1369
            TMSG_INFO(pAction->msgType));
1370
      ;
1371
    } else {
1372
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
15,780!
1373
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1374
    }
1375
  }
1376

1377
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
52,367!
1378

1379
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
52,367!
1380

1381
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
52,367!
1382

1383
  mInfo("trans:%d, prepare transaction", pTrans->id);
52,367!
1384
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
52,367✔
1385
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
10!
1386
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
10✔
1387
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
10✔
1388
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
10✔
1389
    TAOS_RETURN(code);
10✔
1390
  }
1391
  mInfo("trans:%d, prepare finished", pTrans->id);
52,357!
1392

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

1401
  pNew->pRpcArray = pTrans->pRpcArray;
52,357✔
1402
  pNew->rpcRsp = pTrans->rpcRsp;
52,357✔
1403
  pNew->rpcRspLen = pTrans->rpcRspLen;
52,357✔
1404
  pNew->mTraceId = pTrans->mTraceId;
52,357✔
1405
  pTrans->pRpcArray = NULL;
52,357✔
1406
  pTrans->rpcRsp = NULL;
52,357✔
1407
  pTrans->rpcRspLen = 0;
52,357✔
1408

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

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

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

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

1449
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
290,327✔
1450
  bool    sendRsp = false;
290,327✔
1451
  int32_t code = pTrans->code;
290,327✔
1452

1453
  if (pTrans->stage == TRN_STAGE_FINISH) {
290,327✔
1454
    sendRsp = true;
114,206✔
1455
  }
1456

1457
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
290,327✔
1458
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
89,193!
1459
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
12!
1460
      sendRsp = true;
12✔
1461
    }
1462
  } else {
1463
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
201,134✔
1464
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
129,472!
1465
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
UNCOV
1466
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1467
      } else {
1468
        if (pTrans->failedTimes > 6) sendRsp = true;
129,472✔
1469
      }
1470
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
129,472✔
1471
    }
1472
  }
1473

1474
  if (!sendRsp) {
290,327✔
1475
    return;
176,106✔
1476
  } else {
1477
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
114,221!
1478
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1479
  }
1480

1481
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
114,221!
1482
  taosWLockLatch(&pTrans->lockRpcArray);
114,221✔
1483
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
114,221✔
1484
  if (size <= 0) {
114,221✔
1485
    taosWUnLockLatch(&pTrans->lockRpcArray);
82,112✔
1486
    return;
82,112✔
1487
  }
1488

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

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

1505
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
30,619✔
1506

1507
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
30,619✔
1508
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,739!
1509
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,739✔
1510
        if (pDb != NULL) {
4,739!
1511
          for (int32_t j = 0; j < 12; j++) {
5,614✔
1512
            bool ready = mndIsDbReady(pMnode, pDb);
5,610✔
1513
            if (!ready) {
5,610✔
1514
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
875!
1515
              taosMsleep(1000);
875✔
1516
            } else {
1517
              break;
4,735✔
1518
            }
1519
          }
1520
        }
1521
        mndReleaseDb(pMnode, pDb);
4,739✔
1522
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
25,880✔
1523
        void   *pCont = NULL;
8,633✔
1524
        int32_t contLen = 0;
8,633✔
1525
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
8,633✔
1526
          mndTransSetRpcRsp(pTrans, pCont, contLen);
8,631✔
1527
        }
1528
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
17,247✔
1529
        void   *pCont = NULL;
456✔
1530
        int32_t contLen = 0;
456✔
1531
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
456!
1532
          mndTransSetRpcRsp(pTrans, pCont, contLen);
456✔
1533
        }
1534
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
16,791✔
1535
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
24✔
1536
        if (code != 0) {
24!
UNCOV
1537
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1538
        }
1539
      }
1540

1541
      if (pTrans->rpcRspLen != 0) {
30,619✔
1542
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
15,875✔
1543
        if (rpcCont != NULL) {
15,875!
1544
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
15,875✔
1545
          rspMsg.pCont = rpcCont;
15,875✔
1546
          rspMsg.contLen = pTrans->rpcRspLen;
15,875✔
1547
        }
1548
      }
1549

1550
      tmsgSendRsp(&rspMsg);
30,619✔
1551

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

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

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

1589
  if (pArray == NULL) {
99,392!
UNCOV
1590
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
UNCOV
1591
    goto _OVER;
×
1592
  }
1593

1594
  int32_t actionNum = taosArrayGetSize(pArray);
99,392✔
1595
  if (action < 0 || action >= actionNum) {
99,392!
UNCOV
1596
    mError("trans:%d, invalid action:%d", transId, action);
×
UNCOV
1597
    goto _OVER;
×
1598
  }
1599

1600
  STransAction *pAction = taosArrayGet(pArray, action);
99,392✔
1601
  if (pAction != NULL) {
99,392!
1602
    if (pAction->msgSent) {
99,392!
1603
      pAction->msgReceived = 1;
99,392✔
1604
      pAction->errCode = pRsp->code;
99,392✔
1605
      pAction->endTime = taosGetTimestampMs();
99,392✔
1606

1607
      // pTrans->lastErrorNo = pRsp->code;
1608
      mndSetTransLastAction(pTrans, pAction);
99,392✔
1609

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

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

1623
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
99,392!
1624
  mndTransExecute(pMnode, pTrans, true);
99,392✔
1625

1626
_OVER:
99,392✔
1627
  mndReleaseTrans(pMnode, pTrans);
99,392✔
1628
  TAOS_RETURN(code);
99,392✔
1629
}
1630

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

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

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

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

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

1667
// execute in sync context
1668
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
535,893✔
1669
  if (pAction->rawWritten) return 0;
535,893✔
1670
  if (topHalf) {
294,691✔
1671
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
10✔
1672
  }
1673

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

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

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

1698
  TAOS_RETURN(code);
294,681✔
1699
}
1700

1701
// execute in trans context
1702
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
631,607✔
1703
                                     bool notSend) {
1704
  if (pAction->msgSent) return 0;
631,607✔
1705
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
144,062✔
1706
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
44,417✔
1707
  }
1708

1709
  if (notSend) {
99,645✔
1710
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
185!
1711
    return 0;
185✔
1712
  }
1713

1714
#ifndef TD_ASTRA_32
1715
  int64_t signature = pTrans->id;
99,460✔
1716
  signature = (signature << 32);
99,460✔
1717
  signature += pAction->id;
99,460✔
1718

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

1734
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
99,460✔
1735

1736
  char    detail[1024] = {0};
99,460✔
1737
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
99,460!
1738
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
99,460✔
1739
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
204,293✔
1740
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
104,833✔
1741
                     pAction->epSet.eps[i].port);
104,833✔
1742
  }
1743

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

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

1761
    mndSetTransLastAction(pTrans, pAction);
1✔
1762
  }
1763

1764
  TAOS_RETURN(code);
99,460✔
1765
}
1766

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

UNCOV
1773
  mndSetTransLastAction(pTrans, pAction);
×
UNCOV
1774
  return 0;
×
1775
}
1776

1777
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
1,167,500✔
1778
                                        bool notSend) {
1779
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,167,500✔
1780
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
535,893✔
1781
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
631,607!
1782
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
631,607✔
1783
  } else {
UNCOV
1784
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1785
  }
1786
}
1787

1788
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
265,774✔
1789
  int32_t numOfActions = taosArrayGetSize(pArray);
265,774✔
1790
  int32_t code = 0;
265,774✔
1791

1792
  for (int32_t action = 0; action < numOfActions; ++action) {
1,326,718✔
1793
    STransAction *pAction = taosArrayGet(pArray, action);
1,095,108✔
1794
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
1,095,108✔
1795
    if (code != 0) {
1,095,108✔
1796
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
34,164!
1797
            numOfActions);
1798
      break;
34,164✔
1799
    }
1800
  }
1801

1802
  return code;
265,774✔
1803
}
1804

1805
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
312,464✔
1806
  int32_t numOfActions = taosArrayGetSize(pArray);
312,464✔
1807
  int32_t code = 0;
312,464✔
1808
  if (numOfActions == 0) return 0;
312,464✔
1809

1810
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
265,774✔
1811
    return code;
34,164✔
1812
  }
1813

1814
  int32_t       numOfExecuted = 0;
231,610✔
1815
  int32_t       errCode = 0;
231,610✔
1816
  STransAction *pErrAction = NULL;
231,610✔
1817
  for (int32_t action = 0; action < numOfActions; ++action) {
1,292,554✔
1818
    STransAction *pAction = taosArrayGet(pArray, action);
1,060,944✔
1819
    if (pAction->msgReceived || pAction->rawWritten) {
1,060,944✔
1820
      numOfExecuted++;
782,021✔
1821
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
782,021✔
1822
        errCode = pAction->errCode;
29✔
1823
        pErrAction = pAction;
29✔
1824
      }
1825
    } else {
1826
      pErrAction = pAction;
278,923✔
1827
    }
1828
  }
1829

1830
  mndSetTransLastAction(pTrans, pErrAction);
231,610✔
1831

1832
  if (numOfExecuted == numOfActions) {
231,610✔
1833
    if (errCode == 0) {
141,923✔
1834
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
141,921!
1835
      return 0;
141,921✔
1836
    } else {
1837
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
2!
1838
      mndTransResetActions(pMnode, pTrans, pArray);
2✔
1839
      terrno = errCode;
2✔
1840
      return errCode;
2✔
1841
    }
1842
  } else {
1843
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
89,687!
1844

1845
    for (int32_t action = 0; action < numOfActions; ++action) {
561,461✔
1846
      STransAction *pAction = taosArrayGet(pArray, action);
471,774✔
1847
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
471,774✔
1848
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1849
             pAction->acceptableCode, pAction->retryCode);
1850
      if (pAction->msgSent) {
471,774✔
1851
        bool reset = false;
471,589✔
1852
        if (pAction->msgReceived) {
471,589✔
1853
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
192,851✔
1854
        } else {
1855
          int64_t timestamp = taosGetTimestampMs();
278,738✔
1856
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
278,738!
1857
        }
1858
        if (reset) {
471,589✔
1859
          mndTransResetAction(pMnode, pTrans, pAction);
27✔
1860
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
27!
1861
                pAction->id, pAction->errCode, pAction->startTime);
1862
        }
1863
      }
1864
    }
1865
    return TSDB_CODE_ACTION_IN_PROGRESS;
89,687✔
1866
  }
1867
}
1868

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

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

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

1896
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
26,592✔
1897
  int32_t code = 0;
26,592✔
1898
  int32_t numOfActions = taosArrayGetSize(pActions);
26,592✔
1899
  if (numOfActions == 0) return code;
26,592✔
1900

1901
  if (pTrans->actionPos >= numOfActions) {
26,590✔
1902
    return code;
2,756✔
1903
  }
1904

1905
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
23,834!
1906
        pTrans->actionPos, mndTransStr(pTrans->stage));
1907

1908
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
35,721✔
1909
    STransAction *pAction = taosArrayGet(pActions, action);
33,243✔
1910

1911
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
33,243✔
1912
      code = TSDB_CODE_ACTION_IN_PROGRESS;
16✔
1913
      break;
21,356✔
1914
    }
1915

1916
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
33,227!
1917
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
1918

1919
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
33,227✔
1920
    if (code == 0) {
33,227✔
1921
      if (pAction->msgSent) {
26,641✔
1922
        bool reset = false;
23,802✔
1923
        if (pAction->msgReceived) {
23,802✔
1924
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
10,625!
1925
            code = pAction->errCode;
4,662✔
1926
            reset = true;
4,662✔
1927
          } else {
1928
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
5,963!
1929
          }
1930
        } else {
1931
          int64_t timestamp = taosGetTimestampMs();
13,177✔
1932
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
13,177!
1933
          code = TSDB_CODE_ACTION_IN_PROGRESS;
13,177✔
1934
        }
1935
        if (reset) {
23,802✔
1936
          mndTransResetAction(pMnode, pTrans, pAction);
4,662✔
1937
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
4,662!
1938
                pAction->id, pAction->errCode, pAction->startTime);
1939
        }
1940
      } else if (pAction->rawWritten) {
2,839!
1941
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
2,839!
UNCOV
1942
          code = pAction->errCode;
×
1943
        } else {
1944
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
2,839!
1945
        }
1946
      } else {
1947
      }
1948
    }
1949

1950
    if (code == 0) {
33,227✔
1951
      pTrans->failedTimes = 0;
8,802✔
1952
    }
1953
    mndSetTransLastAction(pTrans, pAction);
33,227✔
1954
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
33,227✔
1955
      mInfo(
19,763!
1956
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1957
          "msgReceived:%d",
1958
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1959
          pAction->msgSent, pAction->msgReceived);
1960
    } else if (code != 0) {
13,464✔
1961
      mError(
4,662!
1962
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1963
          "msgReceived:%d",
1964
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1965
          pAction->msgSent, pAction->msgReceived);
1966
    }
1967

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

1977
    if (code == 0) {
25,073✔
1978
      pTrans->code = 0;
7,234✔
1979
      pTrans->actionPos++;
7,234✔
1980
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
7,234!
1981
            pAction->id);
1982
      (void)taosThreadMutexUnlock(&pTrans->mutex);
7,234✔
1983
      code = mndTransSync(pMnode, pTrans);
7,234✔
1984
      (void)taosThreadMutexLock(&pTrans->mutex);
7,234✔
1985
      if (code != 0) {
7,234!
UNCOV
1986
        pTrans->actionPos--;
×
UNCOV
1987
        pTrans->code = terrno;
×
1988
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1989
               mndTransStr(pAction->stage), pAction->id, terrstr());
1990
        break;
×
1991
      }
1992
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
17,839✔
1993
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
13,177!
1994
      break;
13,177✔
1995
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,662✔
1996
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
21✔
1997
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,653!
1998
            code, tstrerror(code));
1999
      pTrans->lastErrorNo = code;
4,653✔
2000
      taosMsleep(300);
4,653✔
2001
      action--;
4,653✔
2002
      continue;
4,653✔
2003
    } else {
2004
      terrno = code;
9✔
2005
      pTrans->lastErrorNo = code;
9✔
2006
      pTrans->code = code;
9✔
2007
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
9!
2008
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2009
      break;
9✔
2010
    }
2011
  }
2012

2013
  return code;
23,834✔
2014
}
2015

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

2023
  if (groupId <= 0) {
10,902!
UNCOV
2024
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
UNCOV
2025
    return TSDB_CODE_INTERNAL_ERROR;
×
2026
  }
2027

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

2034
  if (*actionPos >= numOfActions) {
10,902✔
2035
    mError("trans:%d, failed to execute action in serail group, actionPos:%d >= numOfActions:%d at group %d",
2,314!
2036
           pTrans->id, *actionPos, numOfActions, groupId);
2037
    return TSDB_CODE_INTERNAL_ERROR;
2,314✔
2038
  }
2039

2040
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
9,887✔
2041
    STransAction **ppAction = taosArrayGet(pActions, action);
9,209✔
2042
    STransAction  *pAction = *ppAction;
9,209✔
2043

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

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

2060
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
8,190✔
2061
    if (code == 0) {
8,190✔
2062
      if (pAction->msgSent) {
4,512✔
2063
        if (pAction->msgReceived) {
4,110✔
2064
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,189!
2065
            code = pAction->errCode;
99✔
2066
            mndTransResetAction(pMnode, pTrans, pAction);
99✔
2067
          } else {
2068
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
1,090!
2069
                  pAction->id, action, numOfActions, groupId);
2070
          }
2071
        } else {
2072
          code = TSDB_CODE_ACTION_IN_PROGRESS;
2,921✔
2073
        }
2074
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
4,110✔
2075
        if (msgSent != NULL) {
4,110!
2076
          *msgSent = pAction->msgSent;
4,110✔
2077
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
4,110!
2078
        } else {
UNCOV
2079
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2080
        }
2081
      } else if (pAction->rawWritten) {
402!
2082
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
402!
2083
          code = pAction->errCode;
×
2084
        } else {
2085
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
402!
2086
        }
2087
      } else {
2088
      }
2089
    }
2090

2091
    if (code == 0) {
8,190✔
2092
      pTrans->failedTimes = 0;
1,492✔
2093
    }
2094
    mndSetTransLastAction(pTrans, pAction);
8,190✔
2095

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

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

2166
  return code;
8,588✔
2167
}
2168

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

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

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

2197
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
3,115!
2198
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
3,115✔
2199
  int32_t currentGroup = 1;
3,115✔
2200
  while (pIter) {
14,017✔
2201
    SArray **redoActions = pIter;
10,902✔
2202
    size_t   keyLen = 0;
10,902✔
2203
    int32_t *key = taosHashGetKey(pIter, &keyLen);
10,902✔
2204
    int32_t  actionCount = taosArrayGetSize(*redoActions);
10,902✔
2205
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
10,902!
2206
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2207
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
10,902✔
2208
                                             notSend, pHash);
2209
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
10,902✔
2210
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
3,678!
2211
            tstrerror(code));
2212
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
7,224✔
2213
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
3,940!
2214
    } else if (code != 0) {
3,284✔
2215
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
2,314!
2216
             tstrerror(code));
2217
    } else {
2218
      successCount++;
970✔
2219
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
970!
2220
    }
2221
    currentGroup++;
10,902✔
2222
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
10,902✔
2223
  }
2224

2225
  taosHashCleanup(pHash);
3,115✔
2226

2227
  if (successCount == groupCount) {
3,115✔
2228
    total_code = 0;
42✔
2229
  } else {
2230
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
3,073!
2231
  }
2232

2233
  return total_code;
3,115✔
2234
}
2235

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

2240
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
3,766!
2241
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
3,766✔
2242
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
3,766!
2243
      code = 0;
515✔
2244
    } else {
2245
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
3,251✔
2246
      if (pAction != NULL && pAction->groupId == -1) {
3,251!
2247
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
136✔
2248
      } else {
2249
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
3,115✔
2250
        if (code == 0) {
3,115✔
2251
          if (pTrans->actionPos < numOfActions) {
42✔
2252
            code = TSDB_CODE_ACTION_IN_PROGRESS;
34✔
2253
          }
2254
        }
2255
      }
2256
    }
2257
  }
2258

2259
  (void)taosThreadMutexUnlock(&pTrans->mutex);
3,766✔
2260

2261
  return code;
3,766✔
2262
}
2263

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

2274
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
61,887✔
2275
  bool    continueExec = true;
61,887✔
2276
  int32_t code = 0;
61,887✔
2277
  terrno = 0;
61,887✔
2278

2279
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
61,887✔
2280
  if (numOfActions == 0) goto _OVER;
61,887✔
2281

2282
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
19,734!
2283

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

2295
_OVER:
19,734✔
2296
  pTrans->stage = TRN_STAGE_REDO_ACTION;
61,887✔
2297
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
61,887!
2298
  return continueExec;
61,887✔
2299
}
2300

2301
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
228,470✔
2302
  bool    continueExec = true;
228,470✔
2303
  int32_t code = 0;
228,470✔
2304
  terrno = 0;
228,470✔
2305

2306
  if (pTrans->exec == TRN_EXEC_SERIAL) {
228,470✔
2307
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
26,456✔
2308
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
202,014✔
2309
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
198,248✔
2310
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
3,766!
2311
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
3,766✔
2312
  } else {
UNCOV
2313
    code = TSDB_CODE_INTERNAL_ERROR;
×
2314
  }
2315

2316
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
228,482!
2317
      mndTransIsInSyncContext(topHalf)) {
12✔
UNCOV
2318
    pTrans->lastErrorNo = code;
×
UNCOV
2319
    pTrans->code = code;
×
UNCOV
2320
    mInfo(
×
2321
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2322
        "continueExec:%d, code:%s",
2323
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
UNCOV
2324
    taosMsleep(100);
×
UNCOV
2325
    return true;
×
2326
  } else {
2327
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
228,470✔
2328
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
71,527!
2329
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2330
      return false;
71,527✔
2331
    }
2332
  }
2333
  terrno = code;
156,943✔
2334

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

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

2371
  return continueExec;
156,943✔
2372
}
2373

2374
// execute in trans context
2375
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
52,367✔
2376
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
52,367✔
2377

2378
  bool    continueExec = true;
52,366✔
2379
  int32_t code = mndTransCommit(pMnode, pTrans);
52,366✔
2380

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

2393
  return continueExec;
52,366✔
2394
}
2395

2396
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
114,202✔
2397
  bool    continueExec = true;
114,202✔
2398
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
114,202✔
2399

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

2417
  return continueExec;
114,202✔
2418
}
2419

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

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

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

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

2446
  return continueExec;
12✔
2447
}
2448

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

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

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

2465
  return continueExec;
2✔
2466
}
2467

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

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

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

2485
  return continueExec;
2✔
2486
}
2487

2488
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
114,205✔
2489
  bool continueExec = false;
114,205✔
2490
  if (topHalf) return continueExec;
114,205✔
2491

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

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

2504
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
61,844!
2505
        pTrans->failedTimes, pTrans->createdTime);
2506
  return continueExec;
61,844✔
2507
}
2508

2509
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
290,327✔
2510
  bool continueExec = true;
290,327✔
2511

2512
  while (continueExec) {
799,589✔
2513
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
509,262!
2514
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2515
    pTrans->lastExecTime = taosGetTimestampMs();
509,262✔
2516
    switch (pTrans->stage) {
509,262!
UNCOV
2517
      case TRN_STAGE_PREPARE:
×
UNCOV
2518
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
UNCOV
2519
        break;
×
2520
      case TRN_STAGE_REDO_ACTION:
228,470✔
2521
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
228,470✔
2522
        break;
228,470✔
2523
      case TRN_STAGE_COMMIT:
52,367✔
2524
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
52,367✔
2525
        break;
52,367✔
2526
      case TRN_STAGE_COMMIT_ACTION:
114,202✔
2527
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
114,202✔
2528
        break;
114,202✔
2529
      case TRN_STAGE_ROLLBACK:
2✔
2530
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
2✔
2531
        break;
2✔
2532
      case TRN_STAGE_UNDO_ACTION:
14✔
2533
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
14✔
2534
        break;
14✔
2535
      case TRN_STAGE_PRE_FINISH:
2✔
2536
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
2✔
2537
        break;
2✔
2538
      case TRN_STAGE_FINISH:
114,205✔
2539
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
114,205✔
2540
        break;
114,205✔
UNCOV
2541
      default:
×
UNCOV
2542
        continueExec = false;
×
UNCOV
2543
        break;
×
2544
    }
2545
  }
2546

2547
  mndTransSendRpcRsp(pMnode, pTrans);
290,327✔
2548
}
290,327✔
2549

2550
// start trans, pullup, receive rsp, kill
2551
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
157,137✔
2552
  bool topHalf = true;
157,137✔
2553
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
157,137✔
2554
}
157,137✔
2555

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

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

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

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

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

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

2612
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
UNCOV
2613
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2614
    goto _OVER;
×
2615
  }
2616

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

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

UNCOV
2629
  code = mndKillTrans(pMnode, pTrans);
×
2630

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

2636
  mndReleaseTrans(pMnode, pTrans);
1✔
2637
  TAOS_RETURN(code);
1✔
2638
}
2639

2640
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
236✔
2641

2642
void mndTransPullup(SMnode *pMnode) {
36,276✔
2643
  SSdb   *pSdb = pMnode->pSdb;
36,276✔
2644
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
36,276✔
2645
  if (pArray == NULL) return;
36,276!
2646

2647
  void *pIter = NULL;
36,276✔
2648
  while (1) {
5,388✔
2649
    STrans *pTrans = NULL;
41,664✔
2650
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
41,664✔
2651
    if (pIter == NULL) break;
41,664✔
2652
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
10,776!
UNCOV
2653
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2654
    }
2655
    sdbRelease(pSdb, pTrans);
5,388✔
2656
  }
2657

2658
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
36,276✔
2659

2660
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
41,664✔
2661
    int32_t *pTransId = taosArrayGet(pArray, i);
5,388✔
2662
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
5,388✔
2663
    if (pTrans != NULL) {
5,388!
2664
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
5,388!
2665
      mndTransExecute(pMnode, pTrans, false);
5,388✔
2666
    }
2667
    mndReleaseTrans(pMnode, pTrans);
5,388✔
2668
  }
2669
  taosArrayDestroy(pArray);
36,276✔
2670
}
2671

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

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

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

2698
  return buf;
6,290✔
2699
}
2700

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

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

2717
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
370✔
2718
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
4,165✔
2719
      len = 0;
3,797✔
2720
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
3,797✔
2721
      if (pAction->actionType == TRANS_ACTION_MSG) {
3,797✔
2722
        char bufStart[40] = {0};
3,147✔
2723
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
3,147✔
2724

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

2733
        SEpSet epset = pAction->epSet;
3,147✔
2734
        if (epset.numOfEps > 0) {
3,147!
2735
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
3,147✔
2736
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
6,994✔
2737
            len +=
3,847✔
2738
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
3,847✔
2739
          }
2740
        }
2741

2742
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
3,147✔
2743
                        tstrerror(pAction->errCode));
2744
      } else {
2745
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
650✔
2746
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
650✔
2747
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
650✔
2748
      }
2749
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
3,797✔
2750
    }
2751
  }
2752

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

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

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

2789
  while (numOfRows < rows) {
7,761!
2790
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
7,761✔
2791
    if (pShow->pIter == NULL) break;
7,764✔
2792

2793
    cols = 0;
369✔
2794

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

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

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

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

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

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

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

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

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

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

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

2860
    mndTransLogAction(pTrans);
370✔
2861

2862
    numOfRows++;
370✔
2863
    sdbRelease(pSdb, pTrans);
370✔
2864
  }
2865

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

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

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

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

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

UNCOV
2905
  cols = 0;
×
2906

UNCOV
2907
  if (mndShowTransCommonColumns(pShow, pBlock, pAction, transactionId, curActionId, numOfRows, &cols) != 0) return;
×
2908

UNCOV
2909
  if (pAction->actionType == TRANS_ACTION_MSG) {
×
UNCOV
2910
    int32_t len = 0;
×
2911

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

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

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

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

2955
  } else {
UNCOV
2956
    int32_t len = 0;
×
2957

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

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

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

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

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

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

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

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

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

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

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

UNCOV
3050
  int32_t code = 0;
×
UNCOV
3051
  int32_t lino = 0;
×
3052

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

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

UNCOV
3065
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
×
3066

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

UNCOV
3075
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
×
3076

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

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

UNCOV
3090
_OVER:
×
UNCOV
3091
  pShow->numOfRows += numOfRows;
×
3092

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

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