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

taosdata / TDengine / #3545

02 Dec 2024 06:22AM UTC coverage: 60.839% (-0.04%) from 60.88%
#3545

push

travis-ci

web-flow
Merge pull request #28961 from taosdata/fix/refactor-vnode-management-open-vnode

fix/refactor-vnode-management-open-vnode

120592 of 253473 branches covered (47.58%)

Branch coverage included in aggregate %.

102 of 145 new or added lines in 3 files covered. (70.34%)

477 existing lines in 108 files now uncovered.

201840 of 276506 relevant lines covered (73.0%)

19392204.25 hits per line

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

70.97
/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

26
#define TRANS_VER1_NUMBER  1
27
#define TRANS_VER2_NUMBER  2
28
#define TRANS_ARRAY_SIZE   8
29
#define TRANS_RESERVE_SIZE 44
30

31
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans);
32
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *OldTrans, STrans *pOld);
33
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc);
34

35
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw);
36
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction);
37
static void    mndTransDropLogs(SArray *pArray);
38
static void    mndTransDropActions(SArray *pArray);
39

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

55
static bool mndCannotExecuteTransAction(SMnode *pMnode, bool topHalf) {
435,407✔
56
  return (!pMnode->deploy && !mndIsLeader(pMnode)) || !topHalf;
435,407✔
57
}
58

59
static void    mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans);
60
static int32_t mndProcessTransTimer(SRpcMsg *pReq);
61
static int32_t mndProcessTtl(SRpcMsg *pReq);
62
static int32_t mndProcessKillTransReq(SRpcMsg *pReq);
63

64
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
65
static void    mndCancelGetNextTrans(SMnode *pMnode, void *pIter);
66

67
static int32_t tsMaxTransId = 0;
68

69
int32_t mndInitTrans(SMnode *pMnode) {
2,012✔
70
  SSdbTable table = {
2,012✔
71
      .sdbType = SDB_TRANS,
72
      .keyType = SDB_KEY_INT32,
73
      .encodeFp = (SdbEncodeFp)mndTransEncode,
74
      .decodeFp = (SdbDecodeFp)mndTransDecode,
75
      .insertFp = (SdbInsertFp)mndTransActionInsert,
76
      .updateFp = (SdbUpdateFp)mndTransActionUpdate,
77
      .deleteFp = (SdbDeleteFp)mndTransDelete,
78
  };
79

80
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
2,012✔
81
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
2,012✔
82

83
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndRetrieveTrans);
2,012✔
84
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndCancelGetNextTrans);
2,012✔
85
  return sdbSetTable(pMnode->pSdb, table);
2,012✔
86
}
87

88
void mndCleanupTrans(SMnode *pMnode) {}
2,011✔
89

90
static int32_t mndTransGetActionsSize(SArray *pArray) {
596,892✔
91
  int32_t actionNum = taosArrayGetSize(pArray);
596,892✔
92
  int32_t rawDataLen = 0;
596,892✔
93

94
  for (int32_t i = 0; i < actionNum; ++i) {
1,538,435✔
95
    STransAction *pAction = taosArrayGet(pArray, i);
941,543✔
96
    if (pAction->actionType == TRANS_ACTION_RAW) {
941,543✔
97
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
433,176✔
98
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
508,367!
99
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
508,367✔
100
    } else {
101
      // empty
102
    }
103
    rawDataLen += sizeof(int8_t);
941,543✔
104
  }
105

106
  return rawDataLen;
596,892✔
107
}
108

109
static int32_t mndTransEncodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionsNum) {
596,892✔
110
  int32_t code = 0;
596,892✔
111
  int32_t lino = 0;
596,892✔
112
  int32_t dataPos = *offset;
596,892✔
113
  int8_t  unused = 0;
596,892✔
114
  int32_t ret = -1;
596,892✔
115

116
  for (int32_t i = 0; i < actionsNum; ++i) {
1,538,435✔
117
    STransAction *pAction = taosArrayGet(pActions, i);
941,543✔
118
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
941,543!
119
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
941,543!
120
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
941,543!
121
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
941,543!
122
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
941,543!
123
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
941,543!
124
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
941,543!
125
    if (pAction->actionType == TRANS_ACTION_RAW) {
941,543✔
126
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
433,176✔
127
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
433,176!
128
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
433,176!
129
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
433,176!
130
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
508,367!
131
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
508,367!
132
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
508,367!
133
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
508,367!
134
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
508,367!
135
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
508,367!
136
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
508,367!
137
    } else {
138
      // nothing
139
    }
140
  }
141
  ret = 0;
596,892✔
142

143
_OVER:
596,892✔
144
  *offset = dataPos;
596,892✔
145
  return ret;
596,892✔
146
}
147

148
SSdbRaw *mndTransEncode(STrans *pTrans) {
149,223✔
149
  int32_t code = 0;
149,223✔
150
  int32_t lino = 0;
149,223✔
151
  terrno = TSDB_CODE_INVALID_MSG;
149,223✔
152
  int8_t sver = taosArrayGetSize(pTrans->prepareActions) ? TRANS_VER2_NUMBER : TRANS_VER1_NUMBER;
149,223✔
153

154
  int32_t rawDataLen = sizeof(STrans) + TRANS_RESERVE_SIZE + pTrans->paramLen;
149,223✔
155
  rawDataLen += mndTransGetActionsSize(pTrans->prepareActions);
149,223✔
156
  rawDataLen += mndTransGetActionsSize(pTrans->redoActions);
149,223✔
157
  rawDataLen += mndTransGetActionsSize(pTrans->undoActions);
149,223✔
158
  rawDataLen += mndTransGetActionsSize(pTrans->commitActions);
149,223✔
159

160
  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, sver, rawDataLen);
149,223✔
161
  if (pRaw == NULL) {
149,223!
162
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
×
163
    return NULL;
×
164
  }
165

166
  int32_t dataPos = 0;
149,223✔
167
  SDB_SET_INT32(pRaw, dataPos, pTrans->id, _OVER)
149,223!
168
  SDB_SET_INT8(pRaw, dataPos, pTrans->stage, _OVER)
149,223!
169
  SDB_SET_INT8(pRaw, dataPos, pTrans->policy, _OVER)
149,223!
170
  SDB_SET_INT8(pRaw, dataPos, pTrans->conflict, _OVER)
149,223!
171
  SDB_SET_INT8(pRaw, dataPos, pTrans->exec, _OVER)
149,223!
172
  SDB_SET_INT8(pRaw, dataPos, pTrans->oper, _OVER)
149,223!
173
  SDB_SET_INT8(pRaw, dataPos, 0, _OVER)
149,223!
174
  SDB_SET_INT16(pRaw, dataPos, pTrans->originRpcType, _OVER)
149,223!
175
  SDB_SET_INT64(pRaw, dataPos, pTrans->createdTime, _OVER)
149,223!
176
  SDB_SET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
149,223!
177
  SDB_SET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
149,223!
178
  SDB_SET_INT32(pRaw, dataPos, pTrans->actionPos, _OVER)
149,223!
179

180
  int32_t prepareActionNum = taosArrayGetSize(pTrans->prepareActions);
149,223✔
181
  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
149,223✔
182
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
149,223✔
183
  int32_t commitActionNum = taosArrayGetSize(pTrans->commitActions);
149,223✔
184

185
  if (sver > TRANS_VER1_NUMBER) {
149,223✔
186
    SDB_SET_INT32(pRaw, dataPos, prepareActionNum, _OVER)
45,680!
187
  }
188
  SDB_SET_INT32(pRaw, dataPos, redoActionNum, _OVER)
149,223!
189
  SDB_SET_INT32(pRaw, dataPos, undoActionNum, _OVER)
149,223!
190
  SDB_SET_INT32(pRaw, dataPos, commitActionNum, _OVER)
149,223!
191

192
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum) < 0) goto _OVER;
149,223!
193
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum) < 0) goto _OVER;
149,223!
194
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum) < 0) goto _OVER;
149,223!
195
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum) < 0) goto _OVER;
149,223!
196

197
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
149,223!
198
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
149,223!
199
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
149,223!
200
  if (pTrans->param != NULL) {
149,223!
201
    SDB_SET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER)
×
202
  }
203

204
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
149,223!
205

206
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
149,223✔
207
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
149,223!
208
  void *pIter = NULL;
149,223✔
209
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
149,223✔
210
  while (pIter) {
149,241✔
211
    int32_t arbGroupId = *(int32_t *)pIter;
18✔
212
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
18!
213
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
18✔
214
  }
215

216
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
149,223!
217
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
149,223!
218

219
      terrno = 0;
149,223✔
220

221
_OVER:
149,223✔
222
  if (terrno != 0) {
149,223!
223
    mError("trans:%d, failed to encode to raw:%p maxlen:%d len:%d since %s", pTrans->id, pRaw, sdbGetRawTotalSize(pRaw),
×
224
           dataPos, terrstr());
225
    sdbFreeRaw(pRaw);
×
226
    return NULL;
×
227
  }
228

229
  mTrace("trans:%d, encode to raw:%p, row:%p len:%d", pTrans->id, pRaw, pTrans, dataPos);
149,223✔
230
  return pRaw;
149,223✔
231
}
232

233
static int32_t mndTransDecodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionNum) {
1,066,112✔
234
  int32_t      code = 0;
1,066,112✔
235
  int32_t      lino = 0;
1,066,112✔
236
  STransAction action = {0};
1,066,112✔
237
  int32_t      dataPos = *offset;
1,066,112✔
238
  int8_t       unused = 0;
1,066,112✔
239
  int8_t       stage = 0;
1,066,112✔
240
  int8_t       actionType = 0;
1,066,112✔
241
  int32_t      dataLen = 0;
1,066,112✔
242
  int32_t      ret = -1;
1,066,112✔
243

244
  for (int32_t i = 0; i < actionNum; ++i) {
2,792,752✔
245
    memset(&action, 0, sizeof(action));
1,726,640✔
246
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
1,726,640!
247
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
1,726,640!
248
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
1,726,640!
249
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
1,726,640!
250
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
1,726,640!
251
    action.actionType = actionType;
1,726,640✔
252
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
1,726,640!
253
    action.stage = stage;
1,726,640✔
254
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
1,726,640!
255
    if (action.actionType == TRANS_ACTION_RAW) {
1,726,640✔
256
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
775,942!
257
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
775,942!
258
      action.pRaw = taosMemoryMalloc(dataLen);
775,942✔
259
      if (action.pRaw == NULL) goto _OVER;
775,942!
260
      mTrace("raw:%p, is created", action.pRaw);
775,942✔
261
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
775,942!
262
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
775,942!
263
      action.pRaw = NULL;
775,942✔
264
    } else if (action.actionType == TRANS_ACTION_MSG) {
950,698!
265
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
950,698!
266
      tmsgUpdateDnodeEpSet(&action.epSet);
950,698✔
267
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
950,698!
268
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
950,698!
269
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
950,698!
270
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
950,698!
271
      action.pCont = taosMemoryMalloc(action.contLen);
950,698✔
272
      if (action.pCont == NULL) goto _OVER;
950,698!
273
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
950,698!
274
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
950,698!
275
      action.pCont = NULL;
950,698✔
276
    } else {
277
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
278
    }
279
  }
280
  ret = 0;
1,066,112✔
281

282
_OVER:
1,066,112✔
283
  *offset = dataPos;
1,066,112✔
284
  taosMemoryFreeClear(action.pCont);
1,066,112!
285
  return ret;
1,066,112✔
286
}
287

288
SSdbRow *mndTransDecode(SSdbRaw *pRaw) {
266,528✔
289
  terrno = TSDB_CODE_INVALID_MSG;
266,528✔
290
  int32_t code = 0;
266,528✔
291
  int32_t lino = 0;
266,528✔
292
  SSdbRow *pRow = NULL;
266,528✔
293
  STrans  *pTrans = NULL;
266,528✔
294
  char    *pData = NULL;
266,528✔
295
  int32_t  dataLen = 0;
266,528✔
296
  int8_t   sver = 0;
266,528✔
297
  int32_t  prepareActionNum = 0;
266,528✔
298
  int32_t  redoActionNum = 0;
266,528✔
299
  int32_t  undoActionNum = 0;
266,528✔
300
  int32_t  commitActionNum = 0;
266,528✔
301
  int32_t  dataPos = 0;
266,528✔
302
  int32_t  arbgroupIdNum = 0;
266,528✔
303

304
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
266,528!
305

306
  if (sver != TRANS_VER1_NUMBER && sver != TRANS_VER2_NUMBER) {
266,528!
307
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
308
    goto _OVER;
×
309
  }
310

311
  pRow = sdbAllocRow(sizeof(STrans));
266,528✔
312
  if (pRow == NULL) goto _OVER;
266,528!
313

314
  pTrans = sdbGetRowObj(pRow);
266,528✔
315
  if (pTrans == NULL) goto _OVER;
266,528!
316

317
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
266,528!
318

319
  int8_t stage = 0;
266,528✔
320
  int8_t policy = 0;
266,528✔
321
  int8_t conflict = 0;
266,528✔
322
  int8_t exec = 0;
266,528✔
323
  int8_t oper = 0;
266,528✔
324
  int8_t reserved = 0;
266,528✔
325
  int8_t actionType = 0;
266,528✔
326
  SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
266,528!
327
  SDB_GET_INT8(pRaw, dataPos, &policy, _OVER)
266,528!
328
  SDB_GET_INT8(pRaw, dataPos, &conflict, _OVER)
266,528!
329
  SDB_GET_INT8(pRaw, dataPos, &exec, _OVER)
266,528!
330
  SDB_GET_INT8(pRaw, dataPos, &oper, _OVER)
266,528!
331
  SDB_GET_INT8(pRaw, dataPos, &reserved, _OVER)
266,528!
332
  pTrans->stage = stage;
266,528✔
333
  pTrans->policy = policy;
266,528✔
334
  pTrans->conflict = conflict;
266,528✔
335
  pTrans->exec = exec;
266,528✔
336
  pTrans->oper = oper;
266,528✔
337
  SDB_GET_INT16(pRaw, dataPos, &pTrans->originRpcType, _OVER)
266,528!
338
  SDB_GET_INT64(pRaw, dataPos, &pTrans->createdTime, _OVER)
266,528!
339
  SDB_GET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
266,528!
340
  SDB_GET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
266,528!
341
  SDB_GET_INT32(pRaw, dataPos, &pTrans->actionPos, _OVER)
266,528!
342

343
  if (sver > TRANS_VER1_NUMBER) {
266,528✔
344
    SDB_GET_INT32(pRaw, dataPos, &prepareActionNum, _OVER)
79,439!
345
  }
346
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, _OVER)
266,528!
347
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, _OVER)
266,528!
348
  SDB_GET_INT32(pRaw, dataPos, &commitActionNum, _OVER)
266,528!
349

350
  pTrans->prepareActions = taosArrayInit(prepareActionNum, sizeof(STransAction));
266,528✔
351
  pTrans->redoActions = taosArrayInit(redoActionNum, sizeof(STransAction));
266,528✔
352
  pTrans->undoActions = taosArrayInit(undoActionNum, sizeof(STransAction));
266,528✔
353
  pTrans->commitActions = taosArrayInit(commitActionNum, sizeof(STransAction));
266,528✔
354

355
  if (pTrans->prepareActions == NULL) goto _OVER;
266,528!
356
  if (pTrans->redoActions == NULL) goto _OVER;
266,528!
357
  if (pTrans->undoActions == NULL) goto _OVER;
266,528!
358
  if (pTrans->commitActions == NULL) goto _OVER;
266,528!
359

360
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum) < 0) goto _OVER;
266,528!
361
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum) < 0) goto _OVER;
266,528!
362
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum) < 0) goto _OVER;
266,528!
363
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum) < 0) goto _OVER;
266,528!
364

365
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
266,528!
366
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
266,528!
367
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
266,528!
368
  if (pTrans->paramLen != 0) {
266,528!
369
    pTrans->param = taosMemoryMalloc(pTrans->paramLen);
×
370
    if (pTrans->param == NULL) goto _OVER;
×
371
    SDB_GET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER);
×
372
  }
373

374
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
266,528!
375

376
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
266,528✔
377

378
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
266,528!
379
  for (int32_t i = 0; i < arbgroupIdNum; ++i) {
266,558✔
380
    int32_t arbGroupId = 0;
30✔
381
    SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
30!
382
    if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
30!
383
  }
384

385
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
266,528!
386

387
  terrno = 0;
266,528✔
388

389
_OVER:
266,528✔
390
  if (terrno != 0 && pTrans != NULL) {
266,528!
391
    mError("trans:%d, failed to parse from raw:%p since %s", pTrans->id, pRaw, terrstr());
×
392
    mndTransDropData(pTrans);
×
393
    taosMemoryFreeClear(pRow);
×
394
    return NULL;
×
395
  }
396

397
  if (pTrans != NULL) {
266,528!
398
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
266,528✔
399
  }
400
  return pRow;
266,528✔
401
}
402

403
static const char *mndTransStr(ETrnStage stage) {
1,802,946✔
404
  switch (stage) {
1,802,946!
405
    case TRN_STAGE_PREPARE:
118,497✔
406
      return "prepare";
118,497✔
407
    case TRN_STAGE_REDO_ACTION:
919,834✔
408
      return "redoAction";
919,834✔
409
    case TRN_STAGE_ROLLBACK:
23✔
410
      return "rollback";
23✔
411
    case TRN_STAGE_UNDO_ACTION:
118✔
412
      return "undoAction";
118✔
413
    case TRN_STAGE_COMMIT:
233,589✔
414
      return "commit";
233,589✔
415
    case TRN_STAGE_COMMIT_ACTION:
183,719✔
416
      return "commitAction";
183,719✔
417
    case TRN_STAGE_FINISH:
347,147✔
418
      return "finished";
347,147✔
419
    case TRN_STAGE_PRE_FINISH:
19✔
420
      return "pre-finish";
19✔
421
    default:
×
422
      return "invalid";
×
423
  }
424
}
425

426
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
468,334✔
427
  if (pAction != NULL) {
468,334✔
428
    pTrans->lastAction = pAction->id;
346,557✔
429
    pTrans->lastMsgType = pAction->msgType;
346,557✔
430
    pTrans->lastEpset = pAction->epSet;
346,557✔
431
    pTrans->lastErrorNo = pAction->errCode;
346,557✔
432
  } else {
433
    pTrans->lastAction = 0;
121,777✔
434
    pTrans->lastMsgType = 0;
121,777✔
435
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
121,777✔
436
    pTrans->lastErrorNo = 0;
121,777✔
437
  }
438
}
468,334✔
439

440
static void mndTransTestStartFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
441
  mInfo("test trans start, param:%s, len:%d", (char *)param, paramLen);
×
442
}
×
443

444
static void mndTransTestStopFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
445
  mInfo("test trans stop, param:%s, len:%d", (char *)param, paramLen);
×
446
}
×
447

448
static TransCbFp mndTransGetCbFp(ETrnFunc ftype) {
2,162✔
449
  switch (ftype) {
2,162!
450
    case TRANS_START_FUNC_TEST:
×
451
      return mndTransTestStartFunc;
×
452
    case TRANS_STOP_FUNC_TEST:
×
453
      return mndTransTestStopFunc;
×
454
    case TRANS_START_FUNC_MQ_REB:
1,081✔
455
      return mndRebCntInc;
1,081✔
456
    case TRANS_STOP_FUNC_MQ_REB:
1,081✔
457
      return mndRebCntDec;
1,081✔
458
    default:
×
459
      return NULL;
×
460
  }
461
}
462

463
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
49,272✔
464
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
49,272!
465
        mndTransStr(pTrans->stage), pTrans->startFunc);
466

467
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
49,272✔
468

469
  if (pTrans->startFunc > 0) {
49,272✔
470
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
1,081✔
471
    if (fp) {
1,081!
472
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,081✔
473
    }
474
    // pTrans->startFunc = 0;
475
  }
476

477
  if (pTrans->stage == TRN_STAGE_COMMIT) {
49,272✔
478
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
4✔
479
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pTrans->id);
4!
480
  }
481

482
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
49,272✔
483
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1✔
484
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
1!
485
  }
486

487
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
49,272!
488
    pTrans->stage = TRN_STAGE_FINISH;
×
489
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pTrans->id);
×
490
  }
491

492
  return 0;
49,272✔
493
}
494

495
void mndTransDropData(STrans *pTrans) {
312,157✔
496
  if (pTrans->prepareActions != NULL) {
312,157!
497
    mndTransDropActions(pTrans->prepareActions);
312,157✔
498
    pTrans->prepareActions = NULL;
312,157✔
499
  }
500
  if (pTrans->redoActions != NULL) {
312,157!
501
    mndTransDropActions(pTrans->redoActions);
312,157✔
502
    pTrans->redoActions = NULL;
312,157✔
503
  }
504
  if (pTrans->undoActions != NULL) {
312,157!
505
    mndTransDropActions(pTrans->undoActions);
312,157✔
506
    pTrans->undoActions = NULL;
312,157✔
507
  }
508
  if (pTrans->commitActions != NULL) {
312,157!
509
    mndTransDropActions(pTrans->commitActions);
312,157✔
510
    pTrans->commitActions = NULL;
312,157✔
511
  }
512
  if (pTrans->arbGroupIds != NULL) {
312,157!
513
    taosHashCleanup(pTrans->arbGroupIds);
312,157✔
514
  }
515
  if (pTrans->pRpcArray != NULL) {
312,157✔
516
    taosArrayDestroy(pTrans->pRpcArray);
45,629✔
517
    pTrans->pRpcArray = NULL;
45,629✔
518
  }
519
  if (pTrans->rpcRsp != NULL) {
312,157✔
520
    taosMemoryFree(pTrans->rpcRsp);
13,902✔
521
    pTrans->rpcRsp = NULL;
13,902✔
522
    pTrans->rpcRspLen = 0;
13,902✔
523
  }
524
  if (pTrans->param != NULL) {
312,157!
525
    taosMemoryFree(pTrans->param);
×
526
    pTrans->param = NULL;
×
527
    pTrans->paramLen = 0;
×
528
  }
529
  (void)taosThreadMutexDestroy(&pTrans->mutex);
312,157✔
530
}
312,157✔
531

532
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc) {
157,886✔
533
  mInfo("trans:%d, perform delete action, row:%p stage:%s callfunc:%d, stopFunc:%d", pTrans->id, pTrans,
157,886!
534
        mndTransStr(pTrans->stage), callFunc, pTrans->stopFunc);
535

536
  if (pTrans->stopFunc > 0 && callFunc) {
157,886✔
537
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
1,081✔
538
    if (fp) {
1,081!
539
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,081✔
540
    }
541
    // pTrans->stopFunc = 0;
542
  }
543

544
  mndTransDropData(pTrans);
157,886✔
545
  return 0;
157,886✔
546
}
547

548
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
238,192✔
549
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
678,383✔
550
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
440,191✔
551
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
440,191✔
552
    pOldAction->rawWritten = pNewAction->rawWritten;
440,191✔
553
    pOldAction->msgSent = pNewAction->msgSent;
440,191✔
554
    pOldAction->msgReceived = pNewAction->msgReceived;
440,191✔
555
    pOldAction->errCode = pNewAction->errCode;
440,191✔
556
  }
557
}
238,192✔
558

559
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) {
59,548✔
560
  mInfo("trans:%d, perform update action, old row:%p stage:%s create:%" PRId64 ", new row:%p stage:%s create:%" PRId64,
59,548!
561
        pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage), pNew->createdTime);
562

563
  if (pOld->createdTime != pNew->createdTime) {
59,548!
564
    mError("trans:%d, failed to perform update action since createTime not match, old row:%p stage:%s create:%" PRId64
×
565
           ", new row:%p stage:%s create:%" PRId64,
566
           pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage),
567
           pNew->createdTime);
568
    // only occured while sync timeout
569
    TAOS_RETURN(TSDB_CODE_MND_TRANS_SYNC_TIMEOUT);
×
570
  }
571

572
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
59,548✔
573
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
59,548✔
574
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
59,548✔
575
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
59,548✔
576
  pOld->stage = pNew->stage;
59,548✔
577
  pOld->actionPos = pNew->actionPos;
59,548✔
578

579
  if (pOld->stage == TRN_STAGE_COMMIT) {
59,548✔
580
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
49,060✔
581
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
49,060!
582
  }
583

584
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
59,548✔
585
    pOld->stage = TRN_STAGE_UNDO_ACTION;
5✔
586
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pNew->id);
5!
587
  }
588

589
  if (pOld->stage == TRN_STAGE_PRE_FINISH) {
59,548✔
590
    pOld->stage = TRN_STAGE_FINISH;
5✔
591
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pNew->id);
5!
592
  }
593

594
  return 0;
59,548✔
595
}
596

597
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
264,589✔
598
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
264,589✔
599
  if (pTrans == NULL) {
264,589✔
600
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
4,168✔
601
  }
602
  return pTrans;
264,589✔
603
}
604

605
void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
260,422✔
606
  SSdb *pSdb = pMnode->pSdb;
260,422✔
607
  if (pTrans != NULL) mInfo("vgId:1, trans:%d, release transaction", pTrans->id);
260,422!
608
  sdbRelease(pSdb, pTrans);
260,422✔
609
}
260,422✔
610

611
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
45,629✔
612
                       const char *opername) {
613
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
45,629✔
614
  if (pTrans == NULL) {
45,629!
615
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
616
    mError("failed to create transaction since %s", terrstr());
×
617
    return NULL;
×
618
  }
619

620
  if (opername != NULL) {
45,629!
621
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
45,629✔
622
  }
623

624
  int32_t sdbMaxId = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
45,629✔
625
  sdbReadLock(pMnode->pSdb, SDB_TRANS);
45,629✔
626
  pTrans->id = TMAX(sdbMaxId, tsMaxTransId + 1);
45,629✔
627
  sdbUnLock(pMnode->pSdb, SDB_TRANS);
45,629✔
628
  pTrans->stage = TRN_STAGE_PREPARE;
45,629✔
629
  pTrans->policy = policy;
45,629✔
630
  pTrans->conflict = conflict;
45,629✔
631
  pTrans->exec = TRN_EXEC_PARALLEL;
45,629✔
632
  pTrans->createdTime = taosGetTimestampMs();
45,629✔
633
  pTrans->prepareActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
45,629✔
634
  pTrans->redoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
45,629✔
635
  pTrans->undoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
45,629✔
636
  pTrans->commitActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
45,629✔
637
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
45,629✔
638
  pTrans->pRpcArray = taosArrayInit(1, sizeof(SRpcHandleInfo));
45,629✔
639
  pTrans->mTraceId = pReq ? TRACE_GET_ROOTID(&pReq->info.traceId) : tGenIdPI64();
45,629✔
640
  taosInitRWLatch(&pTrans->lockRpcArray);
45,629✔
641
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
45,629✔
642

643
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
45,629!
644
      pTrans->pRpcArray == NULL) {
45,629!
645
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
646
    mError("failed to create transaction since %s", terrstr());
×
647
    mndTransDrop(pTrans);
×
648
    return NULL;
×
649
  }
650

651
  if (pReq != NULL) {
45,629✔
652
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
64,754!
653
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
654
      return NULL;
×
655
    }
656
    pTrans->originRpcType = pReq->msgType;
32,377✔
657
  }
658

659
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
45,629!
660

661
  mTrace("trans:%d, local object is created, data:%p", pTrans->id, pTrans);
45,629✔
662
  return pTrans;
45,629✔
663
}
664

665
static void mndTransDropActions(SArray *pArray) {
1,248,628✔
666
  int32_t size = taosArrayGetSize(pArray);
1,248,628✔
667
  for (int32_t i = 0; i < size; ++i) {
3,230,850✔
668
    STransAction *pAction = taosArrayGet(pArray, i);
1,982,222✔
669
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,982,222✔
670
      taosMemoryFreeClear(pAction->pRaw);
900,086!
671
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,082,136!
672
      taosMemoryFreeClear(pAction->pCont);
1,082,136!
673
    } else {
674
      // nothing
675
    }
676
  }
677

678
  taosArrayDestroy(pArray);
1,248,628✔
679
}
1,248,628✔
680

681
void mndTransDrop(STrans *pTrans) {
46,018✔
682
  if (pTrans != NULL) {
46,018✔
683
    mndTransDropData(pTrans);
45,629✔
684
    mTrace("trans:%d, local object is freed, data:%p", pTrans->id, pTrans);
45,629✔
685
    taosMemoryFreeClear(pTrans);
45,629!
686
  }
687
}
46,018✔
688

689
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
255,582✔
690
  pAction->id = taosArrayGetSize(pArray);
255,582✔
691

692
  void *ptr = taosArrayPush(pArray, pAction);
255,582✔
693
  if (ptr == NULL) {
255,582!
694
    TAOS_RETURN(terrno);
×
695
  }
696

697
  return 0;
255,582✔
698
}
699

700
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
2,143✔
701
  STransAction action = {
2,143✔
702
      .stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw, .mTraceId = pTrans->mTraceId};
2,143✔
703
  return mndTransAppendAction(pTrans->redoActions, &action);
2,143✔
704
}
705

706
int32_t mndTransAppendNullLog(STrans *pTrans) {
×
707
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_NULL};
×
708
  return mndTransAppendAction(pTrans->redoActions, &action);
×
709
}
710

711
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
15,645✔
712
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
15,645✔
713
  return mndTransAppendAction(pTrans->undoActions, &action);
15,645✔
714
}
715

716
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
83,120✔
717
  STransAction action = {.stage = TRN_STAGE_COMMIT_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
83,120✔
718
  return mndTransAppendAction(pTrans->commitActions, &action);
83,120✔
719
}
720

721
int32_t mndTransAppendPrepareLog(STrans *pTrans, SSdbRaw *pRaw) {
23,236✔
722
  STransAction action = {
23,236✔
723
      .pRaw = pRaw, .stage = TRN_STAGE_PREPARE, .actionType = TRANS_ACTION_RAW, .mTraceId = pTrans->mTraceId};
23,236✔
724
  return mndTransAppendAction(pTrans->prepareActions, &action);
23,236✔
725
}
726

727
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
93,904✔
728
  pAction->stage = TRN_STAGE_REDO_ACTION;
93,904✔
729
  pAction->actionType = TRANS_ACTION_MSG;
93,904✔
730
  pAction->mTraceId = pTrans->mTraceId;
93,904✔
731
  return mndTransAppendAction(pTrans->redoActions, pAction);
93,904✔
732
}
733

734
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
37,534✔
735
  pAction->stage = TRN_STAGE_UNDO_ACTION;
37,534✔
736
  pAction->actionType = TRANS_ACTION_MSG;
37,534✔
737
  return mndTransAppendAction(pTrans->undoActions, pAction);
37,534✔
738
}
739

740
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
13,902✔
741
  pTrans->rpcRsp = pCont;
13,902✔
742
  pTrans->rpcRspLen = contLen;
13,902✔
743
}
13,902✔
744

745
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
1,071✔
746
  pTrans->startFunc = startFunc;
1,071✔
747
  pTrans->stopFunc = stopFunc;
1,071✔
748
  pTrans->param = param;
1,071✔
749
  pTrans->paramLen = paramLen;
1,071✔
750
}
1,071✔
751

752
int32_t mndSetRpcInfoForDbTrans(SMnode *pMnode, SRpcMsg *pMsg, EOperType oper, const char *dbname) {
×
753
  STrans *pTrans = NULL;
×
754
  void   *pIter = NULL;
×
755
  int32_t code = -1;
×
756

757
  while (1) {
758
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
×
759
    if (pIter == NULL) break;
×
760

761
    if (pTrans->oper == oper) {
×
762
      if (strcasecmp(dbname, pTrans->dbname) == 0) {
×
763
        mInfo("trans:%d, db:%s oper:%d matched with input", pTrans->id, dbname, oper);
×
764
        taosWLockLatch(&pTrans->lockRpcArray);
×
765
        if (pTrans->pRpcArray == NULL) {
×
766
          pTrans->pRpcArray = taosArrayInit(4, sizeof(SRpcHandleInfo));
×
767
        }
768
        if (pTrans->pRpcArray != NULL && taosArrayPush(pTrans->pRpcArray, &pMsg->info) != NULL) {
×
769
          code = 0;
×
770
        }
771
        taosWUnLockLatch(&pTrans->lockRpcArray);
×
772

773
        sdbRelease(pMnode->pSdb, pTrans);
×
774
        break;
×
775
      }
776
    }
777

778
    sdbRelease(pMnode->pSdb, pTrans);
×
779
  }
780
  return code;
×
781
}
782

783
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
32,528✔
784
  if (dbname != NULL) {
32,528!
785
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
32,528✔
786
  }
787
  if (stbname != NULL) {
32,528✔
788
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
23,720✔
789
  }
790
}
32,528✔
791

792
void mndTransAddArbGroupId(STrans *pTrans, int32_t groupId) {
6✔
793
  if (taosHashPut(pTrans->arbGroupIds, &groupId, sizeof(int32_t), NULL, 0) != 0) {
6!
794
    mError("trans:%d, failed to put groupid into hash, groupId:%d", pTrans->id, groupId);
×
795
  }
796
}
6✔
797

798
void mndTransSetSerial(STrans *pTrans) { pTrans->exec = TRN_EXEC_SERIAL; }
2,996✔
799

800
void mndTransSetParallel(STrans *pTrans) { pTrans->exec = TRN_EXEC_PARALLEL; }
×
801

802
void mndTransSetChangeless(STrans *pTrans) { pTrans->changeless = true; }
66✔
803

804
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
4,632✔
805

806
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
99,829✔
807
  int32_t  code = 0;
99,829✔
808
  SSdbRaw *pRaw = mndTransEncode(pTrans);
99,829✔
809
  if (pRaw == NULL) {
99,829!
810
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
811
    if (terrno != 0) code = terrno;
×
812
    mError("trans:%d, failed to encode while sync trans since %s", pTrans->id, tstrerror(code));
×
813
    TAOS_RETURN(code);
×
814
  }
815
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_READY));
99,829!
816

817
  mInfo("trans:%d, sync to other mnodes, stage:%s createTime:%" PRId64, pTrans->id, mndTransStr(pTrans->stage),
99,829!
818
        pTrans->createdTime);
819
  code = mndSyncPropose(pMnode, pRaw, pTrans->id);
99,829✔
820
  if (code != 0) {
99,829✔
821
    mError("trans:%d, failed to sync, errno:%s code:0x%x createTime:%" PRId64 " saved trans:%d", pTrans->id,
17!
822
           tstrerror(code), code, pTrans->createdTime, pMnode->syncMgmt.transId);
823
    sdbFreeRaw(pRaw);
17✔
824
    TAOS_RETURN(code);
17✔
825
  }
826

827
  sdbFreeRaw(pRaw);
99,812✔
828
  mInfo("trans:%d, sync finished, createTime:%" PRId64, pTrans->id, pTrans->createdTime);
99,812!
829
  TAOS_RETURN(code);
99,812✔
830
}
831

832
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
4,539✔
833
  if (conflict[0] == 0) return false;
4,539!
834
  if (strcasecmp(conflict, pTrans->dbname) == 0) return true;
4,539✔
835
  return false;
4,231✔
836
}
837

838
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
22,380✔
839
  if (conflict[0] == 0) return false;
22,380✔
840
  if (strcasecmp(conflict, pTrans->stbname) == 0) return true;
20,773✔
841
  return false;
20,714✔
842
}
843

844
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
26,919✔
845
  if (conflict) {
26,919✔
846
    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,
367!
847
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
848
    *globalConflict = true;
367✔
849
  } else {
850
    mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with trans:%d db:%s stb:%s type:%d", pNew->id, pNew->dbname,
26,552!
851
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
852
  }
853
}
26,919✔
854

855
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
127,326✔
856
  STrans *pTrans = NULL;
127,326✔
857
  void   *pIter = NULL;
127,326✔
858
  bool    conflict = false;
127,326✔
859

860
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
127,326✔
861

862
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
91,103✔
863
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
91,103!
864

865
  while (1) {
866
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
115,348✔
867
    if (pIter == NULL) break;
115,348✔
868

869
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
24,245✔
870

871
    if (pNew->conflict == TRN_CONFLICT_DB) {
24,245✔
872
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
4,012✔
873
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4,012✔
874
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
2,888✔
875
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
2,888✔
876
      }
877
    }
878

879
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
24,245✔
880
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
20,222✔
881
      if (pTrans->conflict == TRN_CONFLICT_DB) {
20,222✔
882
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,651✔
883
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,651✔
884
      }
885
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
20,222✔
886
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
17,841✔
887
      }
888
    }
889

890
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
24,245!
891
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
×
892
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
×
893
        void* pGidIter = taosHashIterate(pNew->arbGroupIds, NULL);
×
894
        while (pGidIter != NULL) {
×
895
          int32_t groupId = *(int32_t *)pGidIter;
×
896
          if (taosHashGet(pTrans->arbGroupIds, &groupId, sizeof(int32_t)) != NULL) {
×
897
            taosHashCancelIterate(pNew->arbGroupIds, pGidIter);
×
898
            mndTransLogConflict(pNew, pTrans, true, &conflict);
×
899
            break;
×
900
          } else {
901
            mndTransLogConflict(pNew, pTrans, false, &conflict);
×
902
          }
903
          pGidIter = taosHashIterate(pNew->arbGroupIds, pGidIter);
×
904
        }
905
      }
906
    }
907

908
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
24,245!
UNCOV
909
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
×
910
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
911
      } else {
UNCOV
912
        mndTransLogConflict(pNew, pTrans, false, &conflict);
×
913
      }
914
    }
915

916
    sdbRelease(pMnode->pSdb, pTrans);
24,245✔
917
  }
918

919
  return conflict;
91,103✔
920
}
921

922
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
127,326✔
923
  int32_t code = 0;
127,326✔
924
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
127,326✔
925
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
80,749!
926
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
927
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
928
      TAOS_RETURN(code);
×
929
    }
930
  }
931

932
  if (mndCheckTransConflict(pMnode, pTrans)) {
127,326✔
933
    code = TSDB_CODE_MND_TRANS_CONFLICT;
416✔
934
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
416!
935
    TAOS_RETURN(code);
416✔
936
  }
937

938
  TAOS_RETURN(code);
126,910✔
939
}
940

941
int32_t mndTransCheckConflictWithCompact(SMnode *pMnode, STrans *pTrans) {
403✔
942
  int32_t      code = 0;
403✔
943
  void        *pIter = NULL;
403✔
944
  bool         conflict = false;
403✔
945
  SCompactObj *pCompact = NULL;
403✔
946

947
  while (1) {
4✔
948
    bool thisConflict = false;
407✔
949
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
407✔
950
    if (pIter == NULL) break;
407✔
951

952
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
4✔
953
      thisConflict = true;
2✔
954
    }
955
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4!
956
      if (strcasecmp(pTrans->dbname, pCompact->dbname) == 0) thisConflict = true;
2!
957
    }
958

959
    if (thisConflict) {
4!
960
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
4!
961
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
962
      conflict = true;
4✔
963
    } else {
964
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with compact:%d db:%s", pTrans->id, pTrans->dbname,
×
965
            pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
966
    }
967
    sdbRelease(pMnode->pSdb, pCompact);
4✔
968
  }
969

970
  if (conflict) {
403✔
971
    code = TSDB_CODE_MND_TRANS_CONFLICT_COMPACT;
4✔
972
    mError("trans:%d, failed to check tran conflict with compact since %s", pTrans->id, tstrerror(code));
4!
973
    TAOS_RETURN(code);
4✔
974
  }
975

976
  TAOS_RETURN(code);
399✔
977
}
978

979
static bool mndTransActionsOfSameType(SArray *pActions) {
101,913✔
980
  int32_t size = taosArrayGetSize(pActions);
101,913✔
981
  ETrnAct lastActType = TRANS_ACTION_NULL;
101,913✔
982
  bool    same = true;
101,913✔
983
  for (int32_t i = 0; i < size; ++i) {
293,170✔
984
    STransAction *pAction = taosArrayGet(pActions, i);
191,257✔
985
    if (i > 0) {
191,257✔
986
      if (lastActType != pAction->actionType) {
109,569!
987
        same = false;
×
988
        break;
×
989
      }
990
    }
991
    lastActType = pAction->actionType;
191,257✔
992
  }
993
  return same;
101,913✔
994
}
995

996
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
45,163✔
997
  int32_t code = 0;
45,163✔
998
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
45,163✔
999
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
42,268!
1000
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1001
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1002
      TAOS_RETURN(code);
×
1003
    }
1004

1005
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
42,268✔
1006
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
14,482!
1007
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1008
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1009
        TAOS_RETURN(code);
×
1010
      }
1011
    }
1012
  }
1013

1014
  TAOS_RETURN(code);
45,163✔
1015
}
1016

1017
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
45,163✔
1018
  int32_t code = 0;
45,163✔
1019
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
45,163!
1020
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
×
1021
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
×
1022
    TAOS_RETURN(code);
×
1023
  }
1024
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
45,163!
1025
    code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1026
    mError("trans:%d, types of commit actions are not the same", pTrans->id);
×
1027
    TAOS_RETURN(code);
×
1028
  }
1029

1030
  TAOS_RETURN(code);
45,163✔
1031
}
1032

1033
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
45,163✔
1034
  int32_t code = 0;
45,163✔
1035
  if (pTrans == NULL) {
45,163!
1036
      return TSDB_CODE_INVALID_PARA;
×
1037
  }
1038

1039
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
45,163!
1040

1041
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
45,163!
1042

1043
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
45,163!
1044

1045
  mInfo("trans:%d, prepare transaction", pTrans->id);
45,163!
1046
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
45,163✔
1047
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
8!
1048
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
8✔
1049
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
8✔
1050
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
8✔
1051
    TAOS_RETURN(code);
8✔
1052
  }
1053
  mInfo("trans:%d, prepare finished", pTrans->id);
45,155!
1054

1055
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
45,155✔
1056
  if (pNew == NULL) {
45,155!
1057
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1058
    if (terrno != 0) code = terrno;
×
1059
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, tstrerror(code));
×
1060
    TAOS_RETURN(code);
×
1061
  }
1062

1063
  pNew->pRpcArray = pTrans->pRpcArray;
45,155✔
1064
  pNew->rpcRsp = pTrans->rpcRsp;
45,155✔
1065
  pNew->rpcRspLen = pTrans->rpcRspLen;
45,155✔
1066
  pNew->mTraceId = pTrans->mTraceId;
45,155✔
1067
  pTrans->pRpcArray = NULL;
45,155✔
1068
  pTrans->rpcRsp = NULL;
45,155✔
1069
  pTrans->rpcRspLen = 0;
45,155✔
1070

1071
  mndTransExecute(pMnode, pNew);
45,155✔
1072
  mndReleaseTrans(pMnode, pNew);
45,155✔
1073
  // TDOD change to TAOS_RETURN(code);
1074
  return 0;
45,155✔
1075
}
1076

1077
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
45,155✔
1078
  int32_t code = 0;
45,155✔
1079
  mInfo("trans:%d, commit transaction", pTrans->id);
45,155!
1080
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
45,155✔
1081
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
8!
1082
    TAOS_RETURN(code);
8✔
1083
  }
1084
  mInfo("trans:%d, commit finished", pTrans->id);
45,147!
1085
  TAOS_RETURN(code);
45,147✔
1086
}
1087

1088
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
3✔
1089
  int32_t code = 0;
3✔
1090
  mInfo("trans:%d, rollback transaction", pTrans->id);
3!
1091
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
3✔
1092
    mError("trans:%d, failed to rollback since %s", pTrans->id, tstrerror(code));
1!
1093
    TAOS_RETURN(code);
1✔
1094
  }
1095
  mInfo("trans:%d, rollback finished", pTrans->id);
2!
1096
  TAOS_RETURN(code);
2✔
1097
}
1098

1099
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
3✔
1100
  int32_t code = 0;
3✔
1101
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
3!
1102
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
3!
1103
    mError("trans:%d, failed to pre-finish since %s", pTrans->id, tstrerror(code));
×
1104
    TAOS_RETURN(code);
×
1105
  }
1106
  mInfo("trans:%d, pre-finish finished", pTrans->id);
3!
1107
  TAOS_RETURN(code);
3✔
1108
}
1109

1110
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
258,771✔
1111
  bool    sendRsp = false;
258,771✔
1112
  int32_t code = pTrans->code;
258,771✔
1113

1114
  if (pTrans->stage == TRN_STAGE_FINISH) {
258,771✔
1115
    sendRsp = true;
94,217✔
1116
  }
1117

1118
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
258,771✔
1119
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
75,754✔
1120
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
19✔
1121
      sendRsp = true;
19✔
1122
    }
1123
  } else {
1124
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
183,017✔
1125
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
122,118!
1126
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
UNCOV
1127
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1128
      } else {
1129
        if (pTrans->failedTimes > 6) sendRsp = true;
122,118✔
1130
      }
1131
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
122,118✔
1132
    }
1133
  }
1134

1135
  if (!sendRsp) {
258,771✔
1136
    return;
164,533✔
1137
  } else {
1138
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
94,238!
1139
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1140
  }
1141

1142
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
94,238!
1143
  taosWLockLatch(&pTrans->lockRpcArray);
94,238✔
1144
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
94,238✔
1145
  if (size <= 0) {
94,238✔
1146
    taosWUnLockLatch(&pTrans->lockRpcArray);
62,356✔
1147
    return;
62,356✔
1148
  }
1149

1150
  for (int32_t i = 0; i < size; ++i) {
63,764✔
1151
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
31,882✔
1152
    if (pInfo->handle != NULL) {
31,882✔
1153
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
30,295!
1154
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1155
      }
1156
      if (code == TSDB_CODE_SYN_TIMEOUT) {
30,295!
1157
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1158
      }
1159

1160
      if (i != 0 && code == 0) {
30,295!
1161
        code = TSDB_CODE_MNODE_NOT_FOUND;
×
1162
      }
1163
      mInfo("vgId:1, trans:%d, client:%d start to send rsp, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,295!
1164
            mndTransStr(pTrans->stage), pInfo->ahandle);
1165

1166
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
30,295✔
1167

1168
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
30,295✔
1169
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,610!
1170
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,610✔
1171
        if (pDb != NULL) {
4,610!
1172
          for (int32_t j = 0; j < 12; j++) {
5,438✔
1173
            bool ready = mndIsDbReady(pMnode, pDb);
5,436✔
1174
            if (!ready) {
5,436✔
1175
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
828!
1176
              taosMsleep(1000);
828✔
1177
            } else {
1178
              break;
4,608✔
1179
            }
1180
          }
1181
        }
1182
        mndReleaseDb(pMnode, pDb);
4,610✔
1183
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
25,685✔
1184
        void   *pCont = NULL;
8,989✔
1185
        int32_t contLen = 0;
8,989✔
1186
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
8,989✔
1187
          mndTransSetRpcRsp(pTrans, pCont, contLen);
8,986✔
1188
        }
1189
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
16,696✔
1190
        void   *pCont = NULL;
488✔
1191
        int32_t contLen = 0;
488✔
1192
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
488!
1193
          mndTransSetRpcRsp(pTrans, pCont, contLen);
488✔
1194
        }
1195
      }
1196

1197
      if (pTrans->rpcRspLen != 0) {
30,295✔
1198
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
13,902✔
1199
        if (rpcCont != NULL) {
13,902!
1200
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
13,902✔
1201
          rspMsg.pCont = rpcCont;
13,902✔
1202
          rspMsg.contLen = pTrans->rpcRspLen;
13,902✔
1203
        }
1204
      }
1205

1206
      tmsgSendRsp(&rspMsg);
30,295✔
1207

1208
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,295!
1209
            mndTransStr(pTrans->stage), pInfo->ahandle);
1210
    }
1211
  }
1212
  taosArrayClear(pTrans->pRpcArray);
31,882✔
1213
  taosWUnLockLatch(&pTrans->lockRpcArray);
31,882✔
1214
}
1215

1216
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
99,600✔
1217
  int32_t code = 0;
99,600✔
1218
  SMnode *pMnode = pRsp->info.node;
99,600✔
1219
  int64_t signature = (int64_t)(pRsp->info.ahandle);
99,600✔
1220
  int32_t transId = (int32_t)(signature >> 32);
99,600✔
1221
  int32_t action = (int32_t)((signature << 32) >> 32);
99,600✔
1222

1223
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
99,600✔
1224
  if (pTrans == NULL) {
99,600!
1225
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1226
    if (terrno != 0) code = terrno;
×
1227
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1228
    goto _OVER;
×
1229
  }
1230

1231
  SArray *pArray = NULL;
99,600✔
1232
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
99,600✔
1233
    pArray = pTrans->redoActions;
99,588✔
1234
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
12!
1235
    pArray = pTrans->undoActions;
12✔
1236
  } else {
1237
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1238
    goto _OVER;
×
1239
  }
1240

1241
  if (pArray == NULL) {
99,600!
1242
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1243
    goto _OVER;
×
1244
  }
1245

1246
  int32_t actionNum = taosArrayGetSize(pArray);
99,600✔
1247
  if (action < 0 || action >= actionNum) {
99,600!
1248
    mError("trans:%d, invalid action:%d", transId, action);
×
1249
    goto _OVER;
×
1250
  }
1251

1252
  STransAction *pAction = taosArrayGet(pArray, action);
99,600✔
1253
  if (pAction != NULL) {
99,600!
1254
    pAction->msgReceived = 1;
99,600✔
1255
    pAction->errCode = pRsp->code;
99,600✔
1256
    pTrans->lastErrorNo = pRsp->code;
99,600✔
1257

1258
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
99,600!
1259
          mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1260
          tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1261
  } else {
1262
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1263
  }
1264

1265
  mndTransExecute(pMnode, pTrans);
99,600✔
1266

1267
_OVER:
99,600✔
1268
  mndReleaseTrans(pMnode, pTrans);
99,600✔
1269
  TAOS_RETURN(code);
99,600✔
1270
}
1271

1272
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
5,931✔
1273
  pAction->rawWritten = 0;
5,931✔
1274
  pAction->msgSent = 0;
5,931✔
1275
  pAction->msgReceived = 0;
5,931✔
1276
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
5,931!
1277
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
5,931✔
1278
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
22✔
1279
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
22!
1280
          pAction->id, pAction->epSet.inUse);
1281
  } else {
1282
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
5,909!
1283
  }
1284
  pAction->errCode = 0;
5,931✔
1285
}
5,931✔
1286

1287
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
2✔
1288
  int32_t numOfActions = taosArrayGetSize(pArray);
2✔
1289

1290
  for (int32_t action = 0; action < numOfActions; ++action) {
10✔
1291
    STransAction *pAction = taosArrayGet(pArray, action);
8✔
1292
    if (pAction->msgSent && pAction->msgReceived &&
8!
1293
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
8!
1294
      continue;
6✔
1295
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
2!
1296

1297
    mndTransResetAction(pMnode, pTrans, pAction);
2✔
1298
  }
1299
}
2✔
1300

1301
// execute in sync context
1302
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
201,189✔
1303
  if (pAction->rawWritten) return 0;
201,189✔
1304
  if (topHalf) {
116,106!
1305
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
×
1306
  }
1307

1308
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
116,106✔
1309
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
116,106!
1310
    pAction->rawWritten = true;
116,106✔
1311
    pAction->errCode = 0;
116,106✔
1312
    code = 0;
116,106✔
1313
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
116,106!
1314
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1315

1316
    mndSetTransLastAction(pTrans, pAction);
116,106✔
1317
  } else {
1318
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1319
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1320
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1321
    mndSetTransLastAction(pTrans, pAction);
×
1322
  }
1323

1324
  TAOS_RETURN(code);
116,106✔
1325
}
1326

1327
// execute in trans context
1328
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
643,847✔
1329
  if (pAction->msgSent) return 0;
643,847✔
1330
  if (mndCannotExecuteTransAction(pMnode, topHalf)) {
137,469✔
1331
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
37,738✔
1332
  }
1333

1334
  int64_t signature = pTrans->id;
99,731✔
1335
  signature = (signature << 32);
99,731✔
1336
  signature += pAction->id;
99,731✔
1337

1338
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
99,731✔
1339
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
99,731✔
1340
  if (rpcMsg.pCont == NULL) {
99,731!
1341
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1342
    return -1;
×
1343
  }
1344
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
99,731✔
1345
  rpcMsg.info.notFreeAhandle = 1;
99,731✔
1346

1347
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
99,731✔
1348

1349
  char    detail[1024] = {0};
99,731✔
1350
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
99,731!
1351
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
99,731✔
1352
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
206,010✔
1353
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
106,279✔
1354
                    pAction->epSet.eps[i].port);
106,279✔
1355
  }
1356

1357
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
99,731✔
1358
  if (code == 0) {
99,731✔
1359
    pAction->msgSent = 1;
99,721✔
1360
    // pAction->msgReceived = 0;
1361
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
99,721✔
1362
    mInfo("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
99,721!
1363

1364
    mndSetTransLastAction(pTrans, pAction);
99,721✔
1365
  } else {
1366
    pAction->msgSent = 0;
10✔
1367
    pAction->msgReceived = 0;
10✔
1368
    pAction->errCode = (terrno != 0) ? terrno : code;
10!
1369
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
10!
1370
           detail);
1371

1372
    mndSetTransLastAction(pTrans, pAction);
10✔
1373
  }
1374

1375
  TAOS_RETURN(code);
99,731✔
1376
}
1377

1378
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1379
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1380
  pAction->rawWritten = 0;
×
1381
  pAction->errCode = 0;
×
1382
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1383

1384
  mndSetTransLastAction(pTrans, pAction);
×
1385
  return 0;
×
1386
}
1387

1388
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
845,036✔
1389
  if (pAction->actionType == TRANS_ACTION_RAW) {
845,036✔
1390
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
201,189✔
1391
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
643,847!
1392
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf);
643,847✔
1393
  } else {
1394
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1395
  }
1396
}
1397

1398
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
238,796✔
1399
  int32_t numOfActions = taosArrayGetSize(pArray);
238,796✔
1400
  int32_t code = 0;
238,796✔
1401

1402
  for (int32_t action = 0; action < numOfActions; ++action) {
987,152✔
1403
    STransAction *pAction = taosArrayGet(pArray, action);
777,725✔
1404
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
777,725✔
1405
    if (code != 0) {
777,725✔
1406
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
29,369!
1407
            numOfActions);
1408
      break;
29,369✔
1409
    }
1410
  }
1411

1412
  return code;
238,796✔
1413
}
1414

1415
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
270,135✔
1416
  int32_t numOfActions = taosArrayGetSize(pArray);
270,135✔
1417
  int32_t code = 0;
270,135✔
1418
  if (numOfActions == 0) return 0;
270,135✔
1419

1420
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf)) != 0) {
238,796✔
1421
    return code;
29,369✔
1422
  }
1423

1424
  int32_t       numOfExecuted = 0;
209,427✔
1425
  int32_t       errCode = 0;
209,427✔
1426
  STransAction *pErrAction = NULL;
209,427✔
1427
  for (int32_t action = 0; action < numOfActions; ++action) {
957,783✔
1428
    STransAction *pAction = taosArrayGet(pArray, action);
748,356✔
1429
    if (pAction->msgReceived || pAction->rawWritten) {
748,356✔
1430
      numOfExecuted++;
459,440✔
1431
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
459,440✔
1432
        errCode = pAction->errCode;
3✔
1433
        pErrAction = pAction;
3✔
1434
      }
1435
    } else {
1436
      pErrAction = pAction;
288,916✔
1437
    }
1438
  }
1439

1440
  mndSetTransLastAction(pTrans, pErrAction);
209,427✔
1441

1442
  if (numOfExecuted == numOfActions) {
209,427✔
1443
    if (errCode == 0) {
121,779✔
1444
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
121,777!
1445
      return 0;
121,777✔
1446
    } else {
1447
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
2!
1448
      mndTransResetActions(pMnode, pTrans, pArray);
2✔
1449
      terrno = errCode;
2✔
1450
      return errCode;
2✔
1451
    }
1452
  } else {
1453
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
87,648!
1454

1455
    for (int32_t action = 0; action < numOfActions; ++action) {
577,048✔
1456
      STransAction *pAction = taosArrayGet(pArray, action);
489,400✔
1457
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x",
489,400✔
1458
              pTrans->id, mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived,
1459
              pAction->errCode, pAction->acceptableCode, pAction->retryCode);
1460
      if (pAction->msgSent) {
489,400!
1461
        if (pAction->msgReceived) {
489,400✔
1462
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
200,484✔
1463
            mndTransResetAction(pMnode, pTrans, pAction);
1✔
1464
            mInfo("trans:%d, %s:%d reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
1!
1465
          }
1466
        }
1467
      }
1468
    }
1469
    return TSDB_CODE_ACTION_IN_PROGRESS;
87,648✔
1470
  }
1471
}
1472

1473
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
175,906✔
1474
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf);
175,906✔
1475
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
175,906✔
1476
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, topHalf(TransContext):%d", pTrans->id,
12!
1477
           terrstr(), terrno, topHalf);
1478
  }
1479
  return code;
175,906✔
1480
}
1481

1482
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
21✔
1483
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf);
21✔
1484
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
21!
1485
    mError("trans:%d, failed to execute undoActions since %s. topHalf(TransContext):%d", pTrans->id, terrstr(),
×
1486
           topHalf);
1487
  }
1488
  return code;
21✔
1489
}
1490

1491
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
94,208✔
1492
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf);
94,208✔
1493
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
94,208!
1494
    mError("trans:%d, failed to execute commitActions since %s. topHalf(TransContext):%d", pTrans->id, terrstr(),
×
1495
           topHalf);
1496
  }
1497
  return code;
94,208✔
1498
}
1499

1500
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
33,780✔
1501
  int32_t code = 0;
33,780✔
1502
  int32_t numOfActions = taosArrayGetSize(pActions);
33,780✔
1503
  if (numOfActions == 0) return code;
33,780✔
1504

1505
  if (pTrans->actionPos >= numOfActions) {
33,778✔
1506
    return code;
3,229✔
1507
  }
1508

1509
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
30,549!
1510
        pTrans->actionPos, mndTransStr(pTrans->stage));
1511

1512
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
45,963✔
1513
    STransAction *pAction = taosArrayGet(pActions, action);
43,070✔
1514

1515
    mInfo("trans:%d, current action:%d, stage:%s, actionType(0:log,1:msg):%d", pTrans->id, pTrans->actionPos,
43,070!
1516
          mndTransStr(pAction->stage), pAction->actionType);
1517

1518
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
43,070✔
1519
    if (code == 0) {
43,070✔
1520
      if (pAction->msgSent) {
34,691✔
1521
        if (pAction->msgReceived) {
30,722✔
1522
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
13,604✔
1523
            code = pAction->errCode;
5,928✔
1524
            mndTransResetAction(pMnode, pTrans, pAction);
5,928✔
1525
          } else {
1526
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
7,676!
1527
          }
1528
        } else {
1529
          code = TSDB_CODE_ACTION_IN_PROGRESS;
17,118✔
1530
        }
1531
      } else if (pAction->rawWritten) {
3,969!
1532
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,969!
1533
          code = pAction->errCode;
×
1534
        } else {
1535
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,969!
1536
        }
1537
      } else {
1538
      }
1539
    }
1540

1541
    if (code == 0) {
43,070✔
1542
      pTrans->failedTimes = 0;
11,645✔
1543
    }
1544
    mndSetTransLastAction(pTrans, pAction);
43,070✔
1545

1546
    if (mndCannotExecuteTransAction(pMnode, topHalf)) {
43,070✔
1547
      pTrans->lastErrorNo = code;
10,519✔
1548
      pTrans->code = code;
10,519✔
1549
      mInfo("trans:%d, %s:%d, topHalf(TransContext):%d, not execute next action, code:%s", pTrans->id,
10,519!
1550
            mndTransStr(pAction->stage), action, topHalf, tstrerror(code));
1551
      break;
10,519✔
1552
    }
1553

1554
    if (code == 0) {
32,551✔
1555
      pTrans->code = 0;
9,505✔
1556
      pTrans->actionPos++;
9,505✔
1557
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
9,505!
1558
            pAction->id);
1559
      (void)taosThreadMutexUnlock(&pTrans->mutex);
9,505✔
1560
      code = mndTransSync(pMnode, pTrans);
9,505✔
1561
      (void)taosThreadMutexLock(&pTrans->mutex);
9,505✔
1562
      if (code != 0) {
9,505!
1563
        pTrans->actionPos--;
×
1564
        pTrans->code = terrno;
×
1565
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1566
               mndTransStr(pAction->stage), pAction->id, terrstr());
1567
        break;
×
1568
      }
1569
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
23,046✔
1570
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
17,118!
1571
      break;
17,118✔
1572
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
5,928!
1573
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
41✔
1574
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
5,909!
1575
            code, tstrerror(code));
1576
      pTrans->lastErrorNo = code;
5,909✔
1577
      taosMsleep(300);
5,909✔
1578
      action--;
5,909✔
1579
      continue;
5,909✔
1580
    } else {
1581
      terrno = code;
19✔
1582
      pTrans->lastErrorNo = code;
19✔
1583
      pTrans->code = code;
19✔
1584
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
19!
1585
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1586
      break;
19✔
1587
    }
1588
  }
1589

1590
  return code;
30,549✔
1591
}
1592

1593
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
33,780✔
1594
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
33,780✔
1595
  (void)taosThreadMutexLock(&pTrans->mutex);
33,780✔
1596
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
33,780!
1597
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
33,780✔
1598
  }
1599
  (void)taosThreadMutexUnlock(&pTrans->mutex);
33,780✔
1600
  return code;
33,780✔
1601
}
1602

1603
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
1604
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1605
  (void)taosThreadMutexLock(&pTrans->mutex);
×
1606
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1607
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
1608
  }
1609
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
1610
  return code;
×
1611
}
1612

1613
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
49,093✔
1614
  bool    continueExec = true;
49,093✔
1615
  int32_t code = 0;
49,093✔
1616
  terrno = 0;
49,093✔
1617

1618
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
49,093✔
1619
  if (numOfActions == 0) goto _OVER;
49,093✔
1620

1621
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
13,397!
1622

1623
  for (int32_t action = 0; action < numOfActions; ++action) {
37,638✔
1624
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
24,241✔
1625
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
24,241✔
1626
    if (code != 0) {
24,241!
1627
      terrno = code;
×
1628
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
1629
             numOfActions, tstrerror(code));
1630
      return false;
×
1631
    }
1632
  }
1633

1634
_OVER:
13,397✔
1635
  pTrans->stage = TRN_STAGE_REDO_ACTION;
49,093✔
1636
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
49,093!
1637
  return continueExec;
49,093✔
1638
}
1639

1640
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
209,686✔
1641
  bool    continueExec = true;
209,686✔
1642
  int32_t code = 0;
209,686✔
1643
  terrno = 0;
209,686✔
1644

1645
  if (pTrans->exec == TRN_EXEC_SERIAL) {
209,686✔
1646
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
33,780✔
1647
  } else {
1648
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf);
175,906✔
1649
  }
1650

1651
  if (mndCannotExecuteTransAction(pMnode, topHalf)) {
209,686✔
1652
    pTrans->lastErrorNo = code;
59,746✔
1653
    pTrans->code = code;
59,746✔
1654
    bool continueExec = true;
59,746✔
1655
    if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
59,746!
1656
      taosMsleep(100);
×
1657
      continueExec = true;
×
1658
    } else {
1659
      continueExec = false;
59,746✔
1660
    }
1661
    mInfo("trans:%d, cannot execute redo action stage, topHalf(TransContext):%d, continueExec:%d, code:%s", pTrans->id,
59,746!
1662
          topHalf, continueExec, tstrerror(code));
1663

1664
    return continueExec;
59,746✔
1665
  }
1666
  terrno = code;
149,940✔
1667

1668
  if (code == 0) {
149,940✔
1669
    pTrans->code = 0;
45,155✔
1670
    pTrans->stage = TRN_STAGE_COMMIT;
45,155✔
1671
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
45,155!
1672
    continueExec = true;
45,155✔
1673
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
104,785!
1674
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
104,754!
1675
    continueExec = false;
104,754✔
1676
  } else {
1677
    pTrans->failedTimes++;
31✔
1678
    pTrans->code = terrno;
31✔
1679
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
31✔
1680
      if (pTrans->lastAction != 0) {
3✔
1681
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
1682
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
1683
          if (pTrans->failedTimes < 6) {
×
1684
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
1685
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
1686
            taosMsleep(1000);
×
1687
            continueExec = true;
×
1688
            return true;
×
1689
          }
1690
        }
1691
      }
1692

1693
      pTrans->stage = TRN_STAGE_ROLLBACK;
3✔
1694
      pTrans->actionPos = 0;
3✔
1695
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
3!
1696
             pTrans->actionPos);
1697
      continueExec = true;
3✔
1698
    } else {
1699
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
28!
1700
      continueExec = false;
28✔
1701
    }
1702
  }
1703

1704
  return continueExec;
149,940✔
1705
}
1706

1707
// in trans context
1708
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
45,155✔
1709
  if (mndCannotExecuteTransAction(pMnode, topHalf)) return false;
45,155!
1710

1711
  bool    continueExec = true;
45,155✔
1712
  int32_t code = mndTransCommit(pMnode, pTrans);
45,155✔
1713

1714
  if (code == 0) {
45,155✔
1715
    pTrans->code = 0;
45,147✔
1716
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
45,147✔
1717
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
45,147!
1718
    continueExec = true;
45,147✔
1719
  } else {
1720
    pTrans->code = terrno;
8✔
1721
    pTrans->failedTimes++;
8✔
1722
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
8!
1723
    continueExec = false;
8✔
1724
  }
1725

1726
  return continueExec;
45,155✔
1727
}
1728

1729
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
94,208✔
1730
  bool    continueExec = true;
94,208✔
1731
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
94,208✔
1732

1733
  if (code == 0) {
94,208!
1734
    pTrans->code = 0;
94,208✔
1735
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
94,208✔
1736
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
94,208!
1737
    continueExec = true;
94,208✔
1738
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
1739
    pTrans->code = 0;
×
1740
    pTrans->stage = TRN_STAGE_COMMIT;
×
1741
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
1742
    continueExec = true;
×
1743
  } else {
1744
    pTrans->code = terrno;
×
1745
    pTrans->failedTimes++;
×
1746
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1747
    continueExec = false;
×
1748
  }
1749

1750
  return continueExec;
94,208✔
1751
}
1752

1753
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
21✔
1754
  bool    continueExec = true;
21✔
1755
  int32_t code = 0;
21✔
1756

1757
  if (pTrans->exec == TRN_EXEC_SERIAL) {
21!
1758
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
1759
  } else {
1760
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf);
21✔
1761
  }
1762

1763
  if (mndCannotExecuteTransAction(pMnode, topHalf)) return false;
21✔
1764
  terrno = code;
15✔
1765

1766
  if (code == 0) {
15✔
1767
    pTrans->stage = TRN_STAGE_PRE_FINISH;
3✔
1768
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
3!
1769
    continueExec = true;
3✔
1770
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
12!
1771
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
12!
1772
    continueExec = false;
12✔
1773
  } else {
1774
    pTrans->failedTimes++;
×
1775
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1776
    continueExec = false;
×
1777
  }
1778

1779
  return continueExec;
15✔
1780
}
1781

1782
// in trans context
1783
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
3✔
1784
  if (mndCannotExecuteTransAction(pMnode, topHalf)) return false;
3!
1785

1786
  bool    continueExec = true;
3✔
1787
  int32_t code = mndTransRollback(pMnode, pTrans);
3✔
1788

1789
  if (code == 0) {
3✔
1790
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
2✔
1791
    continueExec = true;
2✔
1792
  } else {
1793
    pTrans->failedTimes++;
1✔
1794
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
1!
1795
    continueExec = false;
1✔
1796
  }
1797

1798
  return continueExec;
3✔
1799
}
1800

1801
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
3✔
1802
  if (mndCannotExecuteTransAction(pMnode, topHalf)) return false;
3!
1803

1804
  bool    continueExec = true;
3✔
1805
  int32_t code = mndTransPreFinish(pMnode, pTrans);
3✔
1806

1807
  if (code == 0) {
3!
1808
    pTrans->stage = TRN_STAGE_FINISH;
3✔
1809
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
3!
1810
    continueExec = true;
3✔
1811
  } else {
1812
    pTrans->failedTimes++;
×
1813
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1814
    continueExec = false;
×
1815
  }
1816

1817
  return continueExec;
3✔
1818
}
1819

1820
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
94,216✔
1821
  bool continueExec = false;
94,216✔
1822
  if (topHalf) return continueExec;
94,216✔
1823

1824
  SSdbRaw *pRaw = mndTransEncode(pTrans);
49,066✔
1825
  if (pRaw == NULL) {
49,066!
1826
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
1827
    return false;
×
1828
  }
1829
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
49,066!
1830

1831
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
49,066✔
1832
  if (code != 0) {
49,066!
1833
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
1834
  }
1835

1836
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
49,066!
1837
        pTrans->failedTimes, pTrans->createdTime);
1838
  return continueExec;
49,066✔
1839
}
1840

1841
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf) {
258,771✔
1842
  bool continueExec = true;
258,771✔
1843

1844
  while (continueExec) {
702,063✔
1845
    mInfo("trans:%d, continue to execute, stage:%s createTime:%" PRId64 " topHalf(TransContext):%d", pTrans->id,
443,292!
1846
          mndTransStr(pTrans->stage), pTrans->createdTime, topHalf);
1847
    pTrans->lastExecTime = taosGetTimestampMs();
443,292✔
1848
    switch (pTrans->stage) {
443,292!
1849
      case TRN_STAGE_PREPARE:
×
1850
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1851
        break;
×
1852
      case TRN_STAGE_REDO_ACTION:
209,686✔
1853
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf);
209,686✔
1854
        break;
209,686✔
1855
      case TRN_STAGE_COMMIT:
45,155✔
1856
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
45,155✔
1857
        break;
45,155✔
1858
      case TRN_STAGE_COMMIT_ACTION:
94,208✔
1859
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
94,208✔
1860
        break;
94,208✔
1861
      case TRN_STAGE_ROLLBACK:
3✔
1862
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
3✔
1863
        break;
3✔
1864
      case TRN_STAGE_UNDO_ACTION:
21✔
1865
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf);
21✔
1866
        break;
21✔
1867
      case TRN_STAGE_PRE_FINISH:
3✔
1868
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
3✔
1869
        break;
3✔
1870
      case TRN_STAGE_FINISH:
94,216✔
1871
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
94,216✔
1872
        break;
94,216✔
1873
      default:
×
1874
        continueExec = false;
×
1875
        break;
×
1876
    }
1877
  }
1878

1879
  mndTransSendRpcRsp(pMnode, pTrans);
258,771✔
1880
}
258,771✔
1881

1882
// start trans, pullup, receive rsp, kill
1883
void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
150,129✔
1884
  bool topHalf = true;
150,129✔
1885
  mndTransExecuteImp(pMnode, pTrans, topHalf);
150,129✔
1886
}
150,129✔
1887

1888
// update trans
1889
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
108,642✔
1890
  bool topHalf = false;
108,642✔
1891
  mndTransExecuteImp(pMnode, pTrans, topHalf);
108,642✔
1892
}
108,642✔
1893

1894
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
32,582✔
1895
  mTrace("start to process trans timer");
32,582✔
1896
  mndTransPullup(pReq->info.node);
32,582✔
1897
  return 0;
32,582✔
1898
}
1899

1900
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
1901
  SArray *pArray = NULL;
×
1902
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
1903
    pArray = pTrans->redoActions;
×
1904
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1905
    pArray = pTrans->undoActions;
×
1906
  } else {
1907
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
1908
  }
1909

1910
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
1911
    STransAction *pAction = taosArrayGet(pArray, i);
×
1912
    mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
1913
          mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
1914
    pAction->msgSent = 1;
×
1915
    pAction->msgReceived = 1;
×
1916
    pAction->errCode = 0;
×
1917
  }
1918

1919
  mndTransExecute(pMnode, pTrans);
×
1920
  return 0;
×
1921
}
1922

1923
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1✔
1924
  SMnode       *pMnode = pReq->info.node;
1✔
1925
  SKillTransReq killReq = {0};
1✔
1926
  int32_t       code = -1;
1✔
1927
  STrans       *pTrans = NULL;
1✔
1928

1929
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
1930
    code = TSDB_CODE_INVALID_MSG;
×
1931
    goto _OVER;
×
1932
  }
1933

1934
  mInfo("trans:%d, start to kill", killReq.transId);
1!
1935
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1!
1936
    goto _OVER;
1✔
1937
  }
1938

1939
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
1940
  if (pTrans == NULL) {
×
1941
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1942
    if (terrno != 0) code = terrno;
×
1943
    goto _OVER;
×
1944
  }
1945

1946
  code = mndKillTrans(pMnode, pTrans);
×
1947

1948
_OVER:
1✔
1949
  if (code != 0) {
1!
1950
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1!
1951
  }
1952

1953
  mndReleaseTrans(pMnode, pTrans);
1✔
1954
  TAOS_RETURN(code);
1✔
1955
}
1956

1957
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
225✔
1958

1959
void mndTransPullup(SMnode *pMnode) {
33,089✔
1960
  SSdb   *pSdb = pMnode->pSdb;
33,089✔
1961
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
33,089✔
1962
  if (pArray == NULL) return;
33,089!
1963

1964
  void *pIter = NULL;
33,089✔
1965
  while (1) {
5,374✔
1966
    STrans *pTrans = NULL;
38,463✔
1967
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
38,463✔
1968
    if (pIter == NULL) break;
38,463✔
1969
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
10,748!
1970
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
1971
    }
1972
    sdbRelease(pSdb, pTrans);
5,374✔
1973
  }
1974

1975
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
33,089✔
1976

1977
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
38,463✔
1978
    int32_t *pTransId = taosArrayGet(pArray, i);
5,374✔
1979
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
5,374✔
1980
    if (pTrans != NULL) {
5,374!
1981
      mndTransExecute(pMnode, pTrans);
5,374✔
1982
    }
1983
    mndReleaseTrans(pMnode, pTrans);
5,374✔
1984
  }
1985
  taosArrayDestroy(pArray);
33,089✔
1986
}
1987

1988
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
7,608✔
1989
  SMnode *pMnode = pReq->info.node;
7,608✔
1990
  SSdb   *pSdb = pMnode->pSdb;
7,608✔
1991
  int32_t numOfRows = 0;
7,608✔
1992
  STrans *pTrans = NULL;
7,608✔
1993
  int32_t cols = 0;
7,608✔
1994
  int32_t code = 0;
7,608✔
1995
  int32_t lino = 0;
7,608✔
1996

1997
  while (numOfRows < rows) {
8,603✔
1998
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
8,602✔
1999
    if (pShow->pIter == NULL) break;
8,608✔
2000

2001
    cols = 0;
995✔
2002

2003
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
995✔
2004
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
995!
2005

2006
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
995✔
2007
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
995!
2008
                        _OVER);
2009

2010
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
995✔
2011
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
995✔
2012
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
995✔
2013
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
995!
2014

2015
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
995✔
2016
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
995✔
2017
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
995✔
2018
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
995!
2019

2020
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
995✔
2021
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
995✔
2022
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
995✔
2023
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
995!
2024

2025
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
995✔
2026
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
995✔
2027
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
995✔
2028
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
995!
2029

2030
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
995✔
2031
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
995!
2032
                        _OVER);
2033

2034
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
995✔
2035
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
995!
2036
                        _OVER);
2037

2038
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
995✔
2039
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
995✔
2040
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
2,985✔
2041
                           pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
995✔
2042
    SEpSet  epset = pTrans->lastEpset;
995✔
2043
    if (epset.numOfEps > 0) {
995!
2044
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
1,990!
2045
                      TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
1,990✔
2046
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
2,784✔
2047
        len += tsnprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
1,789✔
2048
      }
2049
    }
2050
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
995✔
2051
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
995✔
2052
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
995!
2053

2054
    numOfRows++;
995✔
2055
    sdbRelease(pSdb, pTrans);
995✔
2056
  }
2057

2058
_OVER:
1✔
2059
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
7,614!
2060
  pShow->numOfRows += numOfRows;
7,611✔
2061
  return numOfRows;
7,611✔
2062
}
2063

2064
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
2065
  SSdb *pSdb = pMnode->pSdb;
×
2066
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
2067
}
×
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

© 2025 Coveralls, Inc