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

taosdata / TDengine / #3873

21 Apr 2025 07:22AM UTC coverage: 63.063% (+0.1%) from 62.968%
#3873

push

travis-ci

GitHub
docs(opc): add perssit data support (#30783)

156631 of 316378 branches covered (49.51%)

Branch coverage included in aggregate %.

242184 of 316027 relevant lines covered (76.63%)

20271838.47 hits per line

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

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

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

28
#define TRANS_VER1_NUMBER  1
29
#define TRANS_VER2_NUMBER  2
30
#define TRANS_ARRAY_SIZE   8
31
#define TRANS_RESERVE_SIZE 42
32
#define TRANS_ACTION_TIMEOUT 1000 * 1000 * 60 * 15
33

34
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans);
35
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *OldTrans, STrans *pOld);
36
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc);
37

38
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw);
39
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction);
40
static void    mndTransDropLogs(SArray *pArray);
41
static void    mndTransDropActions(SArray *pArray);
42

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

58
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
568,292✔
59

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

67
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
593,650✔
68

69
static void    mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans);
70
static int32_t mndProcessTransTimer(SRpcMsg *pReq);
71
static int32_t mndProcessTtl(SRpcMsg *pReq);
72
static int32_t mndProcessKillTransReq(SRpcMsg *pReq);
73

74
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
75
static void    mndCancelGetNextTrans(SMnode *pMnode, void *pIter);
76
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
77
static int32_t tsMaxTransId = 0;
78

79
int32_t mndInitTrans(SMnode *pMnode) {
2,062✔
80
  SSdbTable table = {
2,062✔
81
      .sdbType = SDB_TRANS,
82
      .keyType = SDB_KEY_INT32,
83
      .encodeFp = (SdbEncodeFp)mndTransEncode,
84
      .decodeFp = (SdbDecodeFp)mndTransDecode,
85
      .insertFp = (SdbInsertFp)mndTransActionInsert,
86
      .updateFp = (SdbUpdateFp)mndTransActionUpdate,
87
      .deleteFp = (SdbDeleteFp)mndTransDelete,
88
  };
89

90
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
2,062✔
91
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
2,062✔
92

93
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndRetrieveTrans);
2,062✔
94
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndCancelGetNextTrans);
2,062✔
95
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANSACTION_DETAIL, mndRetrieveTransDetail);
2,062✔
96
  return sdbSetTable(pMnode->pSdb, table);
2,062✔
97
}
98

99
void mndCleanupTrans(SMnode *pMnode) {}
2,061✔
100

101
static int32_t mndTransGetActionsSize(SArray *pArray) {
697,264✔
102
  int32_t actionNum = taosArrayGetSize(pArray);
697,264✔
103
  int32_t rawDataLen = 0;
697,264✔
104

105
  for (int32_t i = 0; i < actionNum; ++i) {
2,096,964✔
106
    STransAction *pAction = taosArrayGet(pArray, i);
1,399,700✔
107
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,399,700✔
108
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
879,721✔
109
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
519,979!
110
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
519,979✔
111
    } else {
112
      // empty
113
    }
114
    rawDataLen += sizeof(int8_t);
1,399,700✔
115
  }
116

117
  return rawDataLen;
697,264✔
118
}
119

120
static int32_t mndTransEncodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionsNum) {
697,264✔
121
  int32_t code = 0;
697,264✔
122
  int32_t lino = 0;
697,264✔
123
  int32_t dataPos = *offset;
697,264✔
124
  int8_t  unused = 0;
697,264✔
125
  int32_t ret = -1;
697,264✔
126

127
  for (int32_t i = 0; i < actionsNum; ++i) {
2,096,964✔
128
    STransAction *pAction = taosArrayGet(pActions, i);
1,399,700✔
129
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
1,399,700!
130
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
1,399,700!
131
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
1,399,700!
132
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
1,399,700!
133
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
1,399,700!
134
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
1,399,700!
135
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
1,399,700!
136
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,399,700✔
137
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
879,721✔
138
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
879,721!
139
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
879,721!
140
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
879,721!
141
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
519,979!
142
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
519,979!
143
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
519,979!
144
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
519,979!
145
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
519,979!
146
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
519,979!
147
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
519,979!
148
    } else {
149
      // nothing
150
    }
151
  }
152
  ret = 0;
697,264✔
153

154
_OVER:
697,264✔
155
  *offset = dataPos;
697,264✔
156
  return ret;
697,264✔
157
}
158

159
SSdbRaw *mndTransEncode(STrans *pTrans) {
174,316✔
160
  int32_t code = 0;
174,316✔
161
  int32_t lino = 0;
174,316✔
162
  terrno = TSDB_CODE_INVALID_MSG;
174,316✔
163
  int8_t sver = TRANS_VER2_NUMBER;
174,316✔
164

165
  int32_t rawDataLen = sizeof(STrans) + TRANS_RESERVE_SIZE + pTrans->paramLen;
174,316✔
166
  rawDataLen += mndTransGetActionsSize(pTrans->prepareActions);
174,316✔
167
  rawDataLen += mndTransGetActionsSize(pTrans->redoActions);
174,316✔
168
  rawDataLen += mndTransGetActionsSize(pTrans->undoActions);
174,316✔
169
  rawDataLen += mndTransGetActionsSize(pTrans->commitActions);
174,316✔
170

171
  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, sver, rawDataLen);
174,316✔
172
  if (pRaw == NULL) {
174,316!
173
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
×
174
    return NULL;
×
175
  }
176

177
  int32_t dataPos = 0;
174,316✔
178
  SDB_SET_INT32(pRaw, dataPos, pTrans->id, _OVER)
174,316!
179
  SDB_SET_INT8(pRaw, dataPos, pTrans->stage, _OVER)
174,316!
180
  SDB_SET_INT8(pRaw, dataPos, pTrans->policy, _OVER)
174,316!
181
  SDB_SET_INT8(pRaw, dataPos, pTrans->conflict, _OVER)
174,316!
182
  SDB_SET_INT8(pRaw, dataPos, pTrans->exec, _OVER)
174,316!
183
  SDB_SET_INT8(pRaw, dataPos, pTrans->oper, _OVER)
174,316!
184
  SDB_SET_INT8(pRaw, dataPos, 0, _OVER)
174,316!
185
  SDB_SET_INT16(pRaw, dataPos, pTrans->originRpcType, _OVER)
174,316!
186
  SDB_SET_INT64(pRaw, dataPos, pTrans->createdTime, _OVER)
174,316!
187
  SDB_SET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
174,316!
188
  SDB_SET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
174,316!
189
  SDB_SET_INT32(pRaw, dataPos, pTrans->actionPos, _OVER)
174,316!
190

191
  int32_t prepareActionNum = taosArrayGetSize(pTrans->prepareActions);
174,316✔
192
  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
174,316✔
193
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
174,316✔
194
  int32_t commitActionNum = taosArrayGetSize(pTrans->commitActions);
174,316✔
195

196
  if (sver > TRANS_VER1_NUMBER) {
174,316!
197
    SDB_SET_INT32(pRaw, dataPos, prepareActionNum, _OVER)
174,316!
198
  }
199
  SDB_SET_INT32(pRaw, dataPos, redoActionNum, _OVER)
174,316!
200
  SDB_SET_INT32(pRaw, dataPos, undoActionNum, _OVER)
174,316!
201
  SDB_SET_INT32(pRaw, dataPos, commitActionNum, _OVER)
174,316!
202

203
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum) < 0) goto _OVER;
174,316!
204
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum) < 0) goto _OVER;
174,316!
205
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum) < 0) goto _OVER;
174,316!
206
  if (mndTransEncodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum) < 0) goto _OVER;
174,316!
207

208
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
174,316!
209
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
174,316!
210
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
174,316!
211
  if (pTrans->param != NULL) {
174,316!
212
    SDB_SET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER)
×
213
  }
214

215
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
174,316!
216

217
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
174,316✔
218
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
174,316!
219
  void *pIter = NULL;
174,316✔
220
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
174,316✔
221
  while (pIter) {
174,352✔
222
    int32_t arbGroupId = *(int32_t *)pIter;
36✔
223
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
36!
224
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
36✔
225
  }
226

227
  if (sver > TRANS_VER1_NUMBER) {
174,316!
228
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
174,316!
229
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
174,316!
230
  }
231

232
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
174,316!
233
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
174,316!
234

235
  terrno = 0;
174,316✔
236

237
_OVER:
174,316✔
238
  if (terrno != 0) {
174,316!
239
    mError("trans:%d, failed to encode to raw:%p maxlen:%d len:%d since %s", pTrans->id, pRaw, sdbGetRawTotalSize(pRaw),
×
240
           dataPos, terrstr());
241
    sdbFreeRaw(pRaw);
×
242
    return NULL;
×
243
  }
244

245
  mTrace("trans:%d, encode to raw:%p, row:%p len:%d", pTrans->id, pRaw, pTrans, dataPos);
174,316✔
246
  return pRaw;
174,316✔
247
}
248

249
static int32_t mndTransDecodeAction(SSdbRaw *pRaw, int32_t *offset, SArray *pActions, int32_t actionNum) {
1,295,776✔
250
  int32_t      code = 0;
1,295,776✔
251
  int32_t      lino = 0;
1,295,776✔
252
  STransAction action = {0};
1,295,776✔
253
  int32_t      dataPos = *offset;
1,295,776✔
254
  int8_t       unused = 0;
1,295,776✔
255
  int8_t       stage = 0;
1,295,776✔
256
  int8_t       actionType = 0;
1,295,776✔
257
  int32_t      dataLen = 0;
1,295,776✔
258
  int32_t      ret = -1;
1,295,776✔
259

260
  for (int32_t i = 0; i < actionNum; ++i) {
3,853,818✔
261
    memset(&action, 0, sizeof(action));
2,558,042✔
262
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
2,558,042!
263
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
2,558,042!
264
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
2,558,042!
265
    SDB_GET_INT32(pRaw, dataPos, &action.retryCode, _OVER)
2,558,042!
266
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
2,558,042!
267
    action.actionType = actionType;
2,558,042✔
268
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
2,558,042!
269
    action.stage = stage;
2,558,042✔
270
    SDB_GET_INT8(pRaw, dataPos, &action.reserved, _OVER)
2,558,042!
271
    if (action.actionType == TRANS_ACTION_RAW) {
2,558,042✔
272
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.rawWritten*/, _OVER)
1,574,299!
273
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
1,574,299!
274
      action.pRaw = taosMemoryMalloc(dataLen);
1,574,299!
275
      if (action.pRaw == NULL) goto _OVER;
1,574,299!
276
      mTrace("raw:%p, is created", action.pRaw);
1,574,299✔
277
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
1,574,299!
278
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
1,574,299!
279
      action.pRaw = NULL;
1,574,299✔
280
    } else if (action.actionType == TRANS_ACTION_MSG) {
983,743!
281
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
983,743!
282
      tmsgUpdateDnodeEpSet(&action.epSet);
983,743✔
283
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
983,743!
284
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgSent*/, _OVER)
983,743!
285
      SDB_GET_INT8(pRaw, dataPos, &unused /*&action.msgReceived*/, _OVER)
983,743!
286
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
983,743!
287
      action.pCont = taosMemoryMalloc(action.contLen);
983,743!
288
      if (action.pCont == NULL) goto _OVER;
983,743!
289
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
983,743!
290
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
983,743!
291
      action.pCont = NULL;
983,743✔
292
    } else {
293
      if (taosArrayPush(pActions, &action) == NULL) goto _OVER;
×
294
    }
295
  }
296
  ret = 0;
1,295,776✔
297

298
_OVER:
1,295,776✔
299
  *offset = dataPos;
1,295,776✔
300
  taosMemoryFreeClear(action.pCont);
1,295,776!
301
  return ret;
1,295,776✔
302
}
303

304
SSdbRow *mndTransDecode(SSdbRaw *pRaw) {
323,944✔
305
  terrno = TSDB_CODE_INVALID_MSG;
323,944✔
306
  int32_t  code = 0;
323,944✔
307
  int32_t  lino = 0;
323,944✔
308
  SSdbRow *pRow = NULL;
323,944✔
309
  STrans  *pTrans = NULL;
323,944✔
310
  char    *pData = NULL;
323,944✔
311
  int32_t  dataLen = 0;
323,944✔
312
  int8_t   sver = 0;
323,944✔
313
  int32_t  prepareActionNum = 0;
323,944✔
314
  int32_t  redoActionNum = 0;
323,944✔
315
  int32_t  undoActionNum = 0;
323,944✔
316
  int32_t  commitActionNum = 0;
323,944✔
317
  int32_t  dataPos = 0;
323,944✔
318
  int32_t  arbgroupIdNum = 0;
323,944✔
319

320
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
323,944!
321

322
  if (sver > TRANS_VER2_NUMBER) {
323,944!
323
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
324
    goto _OVER;
×
325
  }
326

327
  pRow = sdbAllocRow(sizeof(STrans));
323,944✔
328
  if (pRow == NULL) goto _OVER;
323,944!
329

330
  pTrans = sdbGetRowObj(pRow);
323,944✔
331
  if (pTrans == NULL) goto _OVER;
323,944!
332

333
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
323,944!
334

335
  int8_t stage = 0;
323,944✔
336
  int8_t policy = 0;
323,944✔
337
  int8_t conflict = 0;
323,944✔
338
  int8_t exec = 0;
323,944✔
339
  int8_t oper = 0;
323,944✔
340
  int8_t reserved = 0;
323,944✔
341
  int8_t actionType = 0;
323,944✔
342
  SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
323,944!
343
  SDB_GET_INT8(pRaw, dataPos, &policy, _OVER)
323,944!
344
  SDB_GET_INT8(pRaw, dataPos, &conflict, _OVER)
323,944!
345
  SDB_GET_INT8(pRaw, dataPos, &exec, _OVER)
323,944!
346
  SDB_GET_INT8(pRaw, dataPos, &oper, _OVER)
323,944!
347
  SDB_GET_INT8(pRaw, dataPos, &reserved, _OVER)
323,944!
348
  pTrans->stage = stage;
323,944✔
349
  pTrans->policy = policy;
323,944✔
350
  pTrans->conflict = conflict;
323,944✔
351
  pTrans->exec = exec;
323,944✔
352
  pTrans->oper = oper;
323,944✔
353
  SDB_GET_INT16(pRaw, dataPos, &pTrans->originRpcType, _OVER)
323,944!
354
  SDB_GET_INT64(pRaw, dataPos, &pTrans->createdTime, _OVER)
323,944!
355
  SDB_GET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_TABLE_FNAME_LEN, _OVER)
323,944!
356
  SDB_GET_BINARY(pRaw, dataPos, pTrans->stbname, TSDB_TABLE_FNAME_LEN, _OVER)
323,944!
357
  SDB_GET_INT32(pRaw, dataPos, &pTrans->actionPos, _OVER)
323,944!
358

359
  if (sver > TRANS_VER1_NUMBER) {
323,944!
360
    SDB_GET_INT32(pRaw, dataPos, &prepareActionNum, _OVER)
323,944!
361
  }
362
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, _OVER)
323,944!
363
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, _OVER)
323,944!
364
  SDB_GET_INT32(pRaw, dataPos, &commitActionNum, _OVER)
323,944!
365

366
  pTrans->prepareActions = taosArrayInit(prepareActionNum, sizeof(STransAction));
323,944✔
367
  pTrans->redoActions = taosArrayInit(redoActionNum, sizeof(STransAction));
323,944✔
368
  pTrans->undoActions = taosArrayInit(undoActionNum, sizeof(STransAction));
323,944✔
369
  pTrans->commitActions = taosArrayInit(commitActionNum, sizeof(STransAction));
323,944✔
370

371
  if (pTrans->prepareActions == NULL) goto _OVER;
323,944!
372
  if (pTrans->redoActions == NULL) goto _OVER;
323,944!
373
  if (pTrans->undoActions == NULL) goto _OVER;
323,944!
374
  if (pTrans->commitActions == NULL) goto _OVER;
323,944!
375

376
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum) < 0) goto _OVER;
323,944!
377
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum) < 0) goto _OVER;
323,944!
378
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum) < 0) goto _OVER;
323,944!
379
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum) < 0) goto _OVER;
323,944!
380

381
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
323,944!
382
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
323,944!
383
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
323,944!
384
  if (pTrans->paramLen != 0) {
323,944!
385
    pTrans->param = taosMemoryMalloc(pTrans->paramLen);
×
386
    if (pTrans->param == NULL) goto _OVER;
×
387
    SDB_GET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER);
×
388
  }
389

390
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
323,944!
391

392
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
323,944✔
393

394
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
323,944!
395
  for (int32_t i = 0; i < arbgroupIdNum; ++i) {
324,004✔
396
    int32_t arbGroupId = 0;
60✔
397
    SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
60!
398
    if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
60!
399
  }
400

401
  int8_t ableKill = 0;
323,944✔
402
  int8_t killMode = 0;
323,944✔
403
  SDB_GET_INT8(pRaw, dataPos, &ableKill, _OVER)
323,944!
404
  SDB_GET_INT8(pRaw, dataPos, &killMode, _OVER)
323,944!
405
  pTrans->ableToBeKilled = ableKill;
323,944✔
406
  pTrans->killMode = killMode;
323,944✔
407

408
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
323,944!
409

410
  terrno = 0;
323,944✔
411

412
_OVER:
323,944✔
413
  if (terrno != 0 && pTrans != NULL) {
323,944!
414
    mError("trans:%d, failed to parse from raw:%p since %s", pTrans->id, pRaw, terrstr());
×
415
    mndTransDropData(pTrans);
×
416
    taosMemoryFreeClear(pRow);
×
417
    return NULL;
×
418
  }
419

420
  if (pTrans != NULL) {
323,944!
421
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
323,944✔
422
  }
423
  return pRow;
323,944✔
424
}
425

426
static const char *mndTransStr(ETrnStage stage) {
2,595,297✔
427
  switch (stage) {
2,595,297!
428
    case TRN_STAGE_PREPARE:
169,129✔
429
      return "prepare";
169,129✔
430
    case TRN_STAGE_REDO_ACTION:
1,095,639✔
431
      return "redoAction";
1,095,639✔
432
    case TRN_STAGE_ROLLBACK:
41✔
433
      return "rollback";
41✔
434
    case TRN_STAGE_UNDO_ACTION:
53,015✔
435
      return "undoAction";
53,015✔
436
    case TRN_STAGE_COMMIT:
278,763✔
437
      return "commit";
278,763✔
438
    case TRN_STAGE_COMMIT_ACTION:
588,856✔
439
      return "commitAction";
588,856✔
440
    case TRN_STAGE_FINISH:
409,817✔
441
      return "finished";
409,817✔
442
    case TRN_STAGE_PRE_FINISH:
37✔
443
      return "pre-finish";
37✔
444
    default:
×
445
      return "invalid";
×
446
  }
447
}
448

449
static const char *mndTransTypeStr(ETrnAct actionType) {
×
450
  switch (actionType) {
×
451
    case TRANS_ACTION_MSG:
×
452
      return "msg";
×
453
    case TRANS_ACTION_RAW:
×
454
      return "sdb";
×
455
    default:
×
456
      return "invalid";
×
457
  }
458
}
459

460
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
770,189✔
461
  if (pAction != NULL) {
770,189✔
462
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
628,257✔
463
      pTrans->lastAction = pAction->id;
409,721✔
464
      pTrans->lastMsgType = pAction->msgType;
409,721✔
465
      pTrans->lastEpset = pAction->epSet;
409,721✔
466
      pTrans->lastErrorNo = pAction->errCode;
409,721✔
467
    }
468
  } else {
469
    pTrans->lastAction = 0;
141,932✔
470
    pTrans->lastMsgType = 0;
141,932✔
471
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
141,932✔
472
    pTrans->lastErrorNo = 0;
141,932✔
473
  }
474
}
770,189✔
475

476
static void mndTransTestStartFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
477
  mInfo("test trans start, param:%s, len:%d", (char *)param, paramLen);
×
478
}
×
479

480
static void mndTransTestStopFunc(SMnode *pMnode, void *param, int32_t paramLen) {
×
481
  mInfo("test trans stop, param:%s, len:%d", (char *)param, paramLen);
×
482
}
×
483

484
static TransCbFp mndTransGetCbFp(ETrnFunc ftype) {
2,042✔
485
  switch (ftype) {
2,042!
486
    case TRANS_START_FUNC_TEST:
×
487
      return mndTransTestStartFunc;
×
488
    case TRANS_STOP_FUNC_TEST:
×
489
      return mndTransTestStopFunc;
×
490
    case TRANS_START_FUNC_MQ_REB:
1,021✔
491
      return mndRebCntInc;
1,021✔
492
    case TRANS_STOP_FUNC_MQ_REB:
1,021✔
493
      return mndRebCntDec;
1,021✔
494
    default:
×
495
      return NULL;
×
496
  }
497
}
498

499
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
61,221✔
500
  mInfo("trans:%d, perform insert action, row:%p stage:%s, callfunc:1, startFunc:%d", pTrans->id, pTrans,
61,221!
501
        mndTransStr(pTrans->stage), pTrans->startFunc);
502

503
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
61,221✔
504

505
  if (pTrans->startFunc > 0) {
61,221✔
506
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
1,021✔
507
    if (fp) {
1,021!
508
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,021✔
509
    }
510
    // pTrans->startFunc = 0;
511
  }
512

513
  if (pTrans->stage == TRN_STAGE_COMMIT) {
61,221✔
514
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
3✔
515
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pTrans->id);
3!
516
  }
517

518
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
61,221✔
519
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
1✔
520
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
1!
521
  }
522

523
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
61,221!
524
    pTrans->stage = TRN_STAGE_FINISH;
×
525
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pTrans->id);
×
526
  }
527

528
  return 0;
61,221✔
529
}
530

531
void mndTransDropData(STrans *pTrans) {
376,763✔
532
  if (pTrans->prepareActions != NULL) {
376,763!
533
    mndTransDropActions(pTrans->prepareActions);
376,763✔
534
    pTrans->prepareActions = NULL;
376,763✔
535
  }
536
  if (pTrans->redoActions != NULL) {
376,763!
537
    mndTransDropActions(pTrans->redoActions);
376,763✔
538
    pTrans->redoActions = NULL;
376,763✔
539
  }
540
  if (pTrans->undoActions != NULL) {
376,763!
541
    mndTransDropActions(pTrans->undoActions);
376,763✔
542
    pTrans->undoActions = NULL;
376,763✔
543
  }
544
  if (pTrans->commitActions != NULL) {
376,763!
545
    mndTransDropActions(pTrans->commitActions);
376,763✔
546
    pTrans->commitActions = NULL;
376,763✔
547
  }
548
  if (pTrans->arbGroupIds != NULL) {
376,763!
549
    taosHashCleanup(pTrans->arbGroupIds);
376,763✔
550
  }
551
  if (pTrans->pRpcArray != NULL) {
376,763✔
552
    taosArrayDestroy(pTrans->pRpcArray);
52,819✔
553
    pTrans->pRpcArray = NULL;
52,819✔
554
  }
555
  if (pTrans->rpcRsp != NULL) {
376,763✔
556
    taosMemoryFree(pTrans->rpcRsp);
15,503!
557
    pTrans->rpcRsp = NULL;
15,503✔
558
    pTrans->rpcRspLen = 0;
15,503✔
559
  }
560
  if (pTrans->param != NULL) {
376,763!
561
    taosMemoryFree(pTrans->param);
×
562
    pTrans->param = NULL;
×
563
    pTrans->paramLen = 0;
×
564
  }
565
  (void)taosThreadMutexDestroy(&pTrans->mutex);
376,763✔
566
}
376,763✔
567

568
static int32_t mndTransDelete(SSdb *pSdb, STrans *pTrans, bool callFunc) {
192,564✔
569
  mInfo("trans:%d, perform delete action, row:%p stage:%s callfunc:%d, stopFunc:%d", pTrans->id, pTrans,
192,564!
570
        mndTransStr(pTrans->stage), callFunc, pTrans->stopFunc);
571

572
  if (pTrans->stopFunc > 0 && callFunc) {
192,564✔
573
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
1,021✔
574
    if (fp) {
1,021!
575
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
1,021✔
576
    }
577
    // pTrans->stopFunc = 0;
578
  }
579

580
  mndTransDropData(pTrans);
192,564✔
581
  return 0;
192,564✔
582
}
583

584
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
281,480✔
585
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
871,492✔
586
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
590,012✔
587
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
590,012✔
588
    pOldAction->rawWritten = pNewAction->rawWritten;
590,012✔
589
    pOldAction->msgSent = pNewAction->msgSent;
590,012✔
590
    pOldAction->msgReceived = pNewAction->msgReceived;
590,012✔
591
    pOldAction->errCode = pNewAction->errCode;
590,012✔
592
  }
593
}
281,480✔
594

595
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) {
70,370✔
596
  mInfo("trans:%d, perform update action, old row:%p stage:%s create:%" PRId64 ", new row:%p stage:%s create:%" PRId64,
70,370!
597
        pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage), pNew->createdTime);
598

599
  if (pOld->createdTime != pNew->createdTime) {
70,370!
600
    mError("trans:%d, failed to perform update action since createTime not match, old row:%p stage:%s create:%" PRId64
×
601
           ", new row:%p stage:%s create:%" PRId64,
602
           pOld->id, pOld, mndTransStr(pOld->stage), pOld->createdTime, pNew, mndTransStr(pNew->stage),
603
           pNew->createdTime);
604
    // only occured while sync timeout
605
    TAOS_RETURN(TSDB_CODE_MND_TRANS_SYNC_TIMEOUT);
×
606
  }
607

608
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
70,370✔
609
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
70,370✔
610
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
70,370✔
611
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
70,370✔
612
  pOld->stage = pNew->stage;
70,370✔
613
  pOld->actionPos = pNew->actionPos;
70,370✔
614

615
  if (pOld->stage == TRN_STAGE_COMMIT) {
70,370✔
616
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
60,961✔
617
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
60,961!
618
  }
619

620
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
70,370✔
621
    pOld->stage = TRN_STAGE_UNDO_ACTION;
11✔
622
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pNew->id);
11!
623
  }
624

625
  if (pOld->stage == TRN_STAGE_PRE_FINISH) {
70,370✔
626
    pOld->stage = TRN_STAGE_FINISH;
11✔
627
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pNew->id);
11!
628
  }
629

630
  return 0;
70,370✔
631
}
632

633
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
301,283✔
634
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
301,283✔
635
  if (pTrans == NULL) {
301,283✔
636
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
4,147✔
637
  }
638
  return pTrans;
301,283✔
639
}
640

641
void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
297,137✔
642
  SSdb *pSdb = pMnode->pSdb;
297,137✔
643
  if (pTrans != NULL) mInfo("vgId:1, trans:%d, release transaction", pTrans->id);
297,137!
644
  sdbRelease(pSdb, pTrans);
297,137✔
645
}
297,137✔
646

647
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
52,819✔
648
                       const char *opername) {
649
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
52,819!
650
  if (pTrans == NULL) {
52,819!
651
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
652
    mError("failed to create transaction since %s", terrstr());
×
653
    return NULL;
×
654
  }
655

656
  if (opername != NULL) {
52,819!
657
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
52,819✔
658
  }
659

660
  int32_t sdbMaxId = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
52,819✔
661
  sdbReadLock(pMnode->pSdb, SDB_TRANS);
52,819✔
662
  pTrans->id = TMAX(sdbMaxId, tsMaxTransId + 1);
52,819✔
663
  sdbUnLock(pMnode->pSdb, SDB_TRANS);
52,819✔
664
  pTrans->stage = TRN_STAGE_PREPARE;
52,819✔
665
  pTrans->policy = policy;
52,819✔
666
  pTrans->conflict = conflict;
52,819✔
667
  pTrans->exec = TRN_EXEC_PARALLEL;
52,819✔
668
  pTrans->ableToBeKilled = false;
52,819✔
669
  pTrans->createdTime = taosGetTimestampMs();
52,819✔
670
  pTrans->prepareActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
52,819✔
671
  pTrans->redoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
52,819✔
672
  pTrans->undoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
52,819✔
673
  pTrans->commitActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
52,819✔
674
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
52,819✔
675
  pTrans->pRpcArray = taosArrayInit(1, sizeof(SRpcHandleInfo));
52,819✔
676
  pTrans->mTraceId = pReq ? TRACE_GET_ROOTID(&pReq->info.traceId) : tGenIdPI64();
52,819✔
677
  taosInitRWLatch(&pTrans->lockRpcArray);
52,819✔
678
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
52,819✔
679

680
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
52,819!
681
      pTrans->pRpcArray == NULL) {
52,819!
682
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
683
    mError("failed to create transaction since %s", terrstr());
×
684
    mndTransDrop(pTrans);
×
685
    return NULL;
×
686
  }
687

688
  if (pReq != NULL) {
52,819✔
689
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
65,624!
690
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
691
      return NULL;
×
692
    }
693
    pTrans->originRpcType = pReq->msgType;
32,812✔
694
  }
695

696
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
52,819!
697

698
  mTrace("trans:%d, local object is created, data:%p", pTrans->id, pTrans);
52,819✔
699
  return pTrans;
52,819✔
700
}
701

702
static void mndTransDropActions(SArray *pArray) {
1,507,052✔
703
  int32_t size = taosArrayGetSize(pArray);
1,507,052✔
704
  for (int32_t i = 0; i < size; ++i) {
4,474,514✔
705
    STransAction *pAction = taosArrayGet(pArray, i);
2,967,462✔
706
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,967,462✔
707
      taosMemoryFreeClear(pAction->pRaw);
1,845,170!
708
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,122,292!
709
      taosMemoryFreeClear(pAction->pCont);
1,122,292!
710
    } else {
711
      // nothing
712
    }
713
  }
714

715
  taosArrayDestroy(pArray);
1,507,052✔
716
}
1,507,052✔
717

718
void mndTransDrop(STrans *pTrans) {
53,939✔
719
  if (pTrans != NULL) {
53,939✔
720
    mndTransDropData(pTrans);
52,819✔
721
    mTrace("trans:%d, local object is freed, data:%p", pTrans->id, pTrans);
52,819✔
722
    taosMemoryFreeClear(pTrans);
52,819!
723
  }
724
}
53,939✔
725

726
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
409,420✔
727
  pAction->id = taosArrayGetSize(pArray);
409,420✔
728

729
  void *ptr = taosArrayPush(pArray, pAction);
409,420✔
730
  if (ptr == NULL) {
409,420!
731
    TAOS_RETURN(terrno);
×
732
  }
733

734
  return 0;
409,420✔
735
}
736

737
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
1,721✔
738
  STransAction action = {
1,721✔
739
      .stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw, .mTraceId = pTrans->mTraceId};
1,721✔
740
  return mndTransAppendAction(pTrans->redoActions, &action);
1,721✔
741
}
742

743
int32_t mndTransAppendNullLog(STrans *pTrans) {
×
744
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_NULL};
×
745
  return mndTransAppendAction(pTrans->redoActions, &action);
×
746
}
747

748
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
15,729✔
749
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
15,729✔
750
  return mndTransAppendAction(pTrans->undoActions, &action);
15,729✔
751
}
752

753
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
228,011✔
754
  STransAction action = {.stage = TRN_STAGE_COMMIT_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
228,011✔
755
  return mndTransAppendAction(pTrans->commitActions, &action);
228,011✔
756
}
757

758
int32_t mndTransAppendPrepareLog(STrans *pTrans, SSdbRaw *pRaw) {
25,410✔
759
  STransAction action = {
25,410✔
760
      .pRaw = pRaw, .stage = TRN_STAGE_PREPARE, .actionType = TRANS_ACTION_RAW, .mTraceId = pTrans->mTraceId};
25,410✔
761
  return mndTransAppendAction(pTrans->prepareActions, &action);
25,410✔
762
}
763

764
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
101,671✔
765
  pAction->stage = TRN_STAGE_REDO_ACTION;
101,671✔
766
  pAction->actionType = TRANS_ACTION_MSG;
101,671✔
767
  pAction->mTraceId = pTrans->mTraceId;
101,671✔
768
  return mndTransAppendAction(pTrans->redoActions, pAction);
101,671✔
769
}
770

771
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
36,878✔
772
  pAction->stage = TRN_STAGE_UNDO_ACTION;
36,878✔
773
  pAction->actionType = TRANS_ACTION_MSG;
36,878✔
774
  return mndTransAppendAction(pTrans->undoActions, pAction);
36,878✔
775
}
776

777
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
15,503✔
778
  pTrans->rpcRsp = pCont;
15,503✔
779
  pTrans->rpcRspLen = contLen;
15,503✔
780
}
15,503✔
781

782
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
1,009✔
783
  pTrans->startFunc = startFunc;
1,009✔
784
  pTrans->stopFunc = stopFunc;
1,009✔
785
  pTrans->param = param;
1,009✔
786
  pTrans->paramLen = paramLen;
1,009✔
787
}
1,009✔
788

789
int32_t mndSetRpcInfoForDbTrans(SMnode *pMnode, SRpcMsg *pMsg, EOperType oper, const char *dbname) {
×
790
  STrans *pTrans = NULL;
×
791
  void   *pIter = NULL;
×
792
  int32_t code = -1;
×
793

794
  while (1) {
795
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
×
796
    if (pIter == NULL) break;
×
797

798
    if (pTrans->oper == oper) {
×
799
      if (taosStrcasecmp(dbname, pTrans->dbname) == 0) {
×
800
        mInfo("trans:%d, db:%s oper:%d matched with input", pTrans->id, dbname, oper);
×
801
        taosWLockLatch(&pTrans->lockRpcArray);
×
802
        if (pTrans->pRpcArray == NULL) {
×
803
          pTrans->pRpcArray = taosArrayInit(4, sizeof(SRpcHandleInfo));
×
804
        }
805
        if (pTrans->pRpcArray != NULL && taosArrayPush(pTrans->pRpcArray, &pMsg->info) != NULL) {
×
806
          code = 0;
×
807
        }
808
        taosWUnLockLatch(&pTrans->lockRpcArray);
×
809

810
        sdbRelease(pMnode->pSdb, pTrans);
×
811
        break;
×
812
      }
813
    }
814

815
    sdbRelease(pMnode->pSdb, pTrans);
×
816
  }
817
  return code;
×
818
}
819

820
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
34,175✔
821
  if (dbname != NULL) {
34,175!
822
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
34,175✔
823
  }
824
  if (stbname != NULL) {
34,175✔
825
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
25,396✔
826
  }
827
}
34,175✔
828

829
void mndTransAddArbGroupId(STrans *pTrans, int32_t groupId) {
12✔
830
  if (taosHashPut(pTrans->arbGroupIds, &groupId, sizeof(int32_t), NULL, 0) != 0) {
12!
831
    mError("trans:%d, failed to put groupid into hash, groupId:%d", pTrans->id, groupId);
×
832
  }
833
}
12✔
834

835
void mndTransSetSerial(STrans *pTrans) { pTrans->exec = TRN_EXEC_SERIAL; }
2,920✔
836

837
void mndTransSetBeKilled(STrans *pTrans, bool ableToBeKilled) { pTrans->ableToBeKilled = ableToBeKilled; }
×
838

839
void mndTransSetKillMode(STrans *pTrans, ETrnKillMode killMode) {
8✔
840
  pTrans->ableToBeKilled = true; 
8✔
841
  pTrans->killMode = killMode; 
8✔
842
}
8✔
843

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

846
void mndTransSetChangeless(STrans *pTrans) { pTrans->changeless = true; }
64✔
847

848
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
4,705✔
849

850
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
112,980✔
851
  int32_t  code = 0;
112,980✔
852
  SSdbRaw *pRaw = mndTransEncode(pTrans);
112,980✔
853
  if (pRaw == NULL) {
112,980!
854
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
855
    if (terrno != 0) code = terrno;
×
856
    mError("trans:%d, failed to encode while sync trans since %s", pTrans->id, tstrerror(code));
×
857
    TAOS_RETURN(code);
×
858
  }
859
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_READY));
112,980!
860

861
  mInfo("trans:%d, sync to other mnodes, stage:%s createTime:%" PRId64, pTrans->id, mndTransStr(pTrans->stage),
112,980!
862
        pTrans->createdTime);
863
  code = mndSyncPropose(pMnode, pRaw, pTrans->id);
112,980✔
864
  if (code != 0) {
112,980✔
865
    mError("trans:%d, failed to sync, errno:%s code:0x%x createTime:%" PRId64 " saved trans:%d", pTrans->id,
23!
866
           tstrerror(code), code, pTrans->createdTime, pMnode->syncMgmt.transId);
867
    sdbFreeRaw(pRaw);
23✔
868
    TAOS_RETURN(code);
23✔
869
  }
870

871
  sdbFreeRaw(pRaw);
112,957✔
872
  mInfo("trans:%d, sync finished, createTime:%" PRId64, pTrans->id, pTrans->createdTime);
112,957!
873
  TAOS_RETURN(code);
112,957✔
874
}
875

876
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
3,602✔
877
  if (conflict[0] == 0) return false;
3,602!
878
  if (taosStrcasecmp(conflict, pTrans->dbname) == 0) return true;
3,602✔
879
  return false;
3,263✔
880
}
881

882
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
20,606✔
883
  if (conflict[0] == 0) return false;
20,606✔
884
  if (taosStrcasecmp(conflict, pTrans->stbname) == 0) return true;
18,908✔
885
  return false;
18,779✔
886
}
887

888
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
24,211✔
889
  if (conflict) {
24,211✔
890
    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,
468!
891
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
892
    *globalConflict = true;
468✔
893
  } else {
894
    mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with trans:%d db:%s stb:%s type:%d", pNew->id, pNew->dbname,
23,743!
895
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
896
  }
897
}
24,211✔
898

899
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
148,036✔
900
  STrans *pTrans = NULL;
148,036✔
901
  void   *pIter = NULL;
148,036✔
902
  bool    conflict = false;
148,036✔
903

904
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
148,036✔
905

906
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
100,105✔
907
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
100,105!
908

909
  while (1) {
910
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
122,478✔
911
    if (pIter == NULL) break;
122,478✔
912

913
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
22,373✔
914

915
    if (pNew->conflict == TRN_CONFLICT_DB) {
22,373✔
916
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
3,641✔
917
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
3,641✔
918
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
2,565✔
919
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
2,565✔
920
      }
921
    }
922

923
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
22,373✔
924
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
18,719✔
925
      if (pTrans->conflict == TRN_CONFLICT_DB) {
18,719✔
926
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,037✔
927
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,037✔
928
      }
929
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
18,719✔
930
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
17,004✔
931
      }
932
    }
933

934
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
22,373!
935
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
×
936
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
×
937
        void *pGidIter = taosHashIterate(pNew->arbGroupIds, NULL);
×
938
        while (pGidIter != NULL) {
×
939
          int32_t groupId = *(int32_t *)pGidIter;
×
940
          if (taosHashGet(pTrans->arbGroupIds, &groupId, sizeof(int32_t)) != NULL) {
×
941
            taosHashCancelIterate(pNew->arbGroupIds, pGidIter);
×
942
            mndTransLogConflict(pNew, pTrans, true, &conflict);
×
943
            break;
×
944
          } else {
945
            mndTransLogConflict(pNew, pTrans, false, &conflict);
×
946
          }
947
          pGidIter = taosHashIterate(pNew->arbGroupIds, pGidIter);
×
948
        }
949
      }
950
    }
951

952
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
22,373✔
953
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
3!
954
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
955
      } else {
956
        mndTransLogConflict(pNew, pTrans, false, &conflict);
3✔
957
      }
958
    }
959

960
    sdbRelease(pMnode->pSdb, pTrans);
22,373✔
961
  }
962

963
  return conflict;
100,105✔
964
}
965

966
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
148,036✔
967
  int32_t code = 0;
148,036✔
968
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
148,036✔
969
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
89,503!
970
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
971
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
972
      TAOS_RETURN(code);
×
973
    }
974
  }
975

976
  if (mndCheckTransConflict(pMnode, pTrans)) {
148,036✔
977
    code = TSDB_CODE_MND_TRANS_CONFLICT;
503✔
978
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
503!
979
    TAOS_RETURN(code);
503✔
980
  }
981

982
  TAOS_RETURN(code);
147,533✔
983
}
984

985
int32_t mndTransCheckConflictWithCompact(SMnode *pMnode, STrans *pTrans) {
358✔
986
  int32_t      code = 0;
358✔
987
  void        *pIter = NULL;
358✔
988
  bool         conflict = false;
358✔
989
  SCompactObj *pCompact = NULL;
358✔
990

991
  while (1) {
4✔
992
    bool thisConflict = false;
362✔
993
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
362✔
994
    if (pIter == NULL) break;
362✔
995

996
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL) {
4✔
997
      thisConflict = true;
2✔
998
    }
999
    if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
4!
1000
      if (taosStrcasecmp(pTrans->dbname, pCompact->dbname) == 0) thisConflict = true;
2!
1001
    }
1002

1003
    if (thisConflict) {
4!
1004
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
4!
1005
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1006
      conflict = true;
4✔
1007
    } else {
1008
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with compact:%d db:%s", pTrans->id, pTrans->dbname,
×
1009
            pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
1010
    }
1011
    sdbRelease(pMnode->pSdb, pCompact);
4✔
1012
  }
1013

1014
  if (conflict) {
358✔
1015
    code = TSDB_CODE_MND_TRANS_CONFLICT_COMPACT;
4✔
1016
    mError("trans:%d, failed to check tran conflict with compact since %s", pTrans->id, tstrerror(code));
4!
1017
    TAOS_RETURN(code);
4✔
1018
  }
1019

1020
  TAOS_RETURN(code);
354✔
1021
}
1022

1023
static bool mndTransActionsOfSameType(SArray *pActions) {
119,624✔
1024
  int32_t size = taosArrayGetSize(pActions);
119,624✔
1025
  ETrnAct lastActType = TRANS_ACTION_NULL;
119,624✔
1026
  bool    same = true;
119,624✔
1027
  for (int32_t i = 0; i < size; ++i) {
462,784✔
1028
    STransAction *pAction = taosArrayGet(pActions, i);
343,160✔
1029
    if (i > 0) {
343,160✔
1030
      if (lastActType != pAction->actionType) {
253,744!
1031
        same = false;
×
1032
        break;
×
1033
      }
1034
    }
1035
    lastActType = pAction->actionType;
343,160✔
1036
  }
1037
  return same;
119,624✔
1038
}
1039

1040
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
52,278✔
1041
  int32_t code = 0;
52,278✔
1042
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
52,278✔
1043
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
49,448!
1044
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1045
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1046
      TAOS_RETURN(code);
×
1047
    }
1048

1049
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
49,448✔
1050
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
17,898!
1051
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1052
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1053
        TAOS_RETURN(code);
×
1054
      }
1055
    }
1056
  }
1057

1058
  TAOS_RETURN(code);
52,278✔
1059
}
1060

1061
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
52,278✔
1062
  int32_t code = 0;
52,278✔
1063
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
52,278!
1064
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
×
1065
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
×
1066
    TAOS_RETURN(code);
×
1067
  }
1068
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
52,278!
1069
    code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1070
    mError("trans:%d, types of commit actions are not the same", pTrans->id);
×
1071
    TAOS_RETURN(code);
×
1072
  }
1073

1074
  TAOS_RETURN(code);
52,278✔
1075
}
1076

1077
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
52,278✔
1078
  int32_t code = 0;
52,278✔
1079
  if (pTrans == NULL) {
52,278!
1080
    return TSDB_CODE_INVALID_PARA;
×
1081
  }
1082

1083
  mInfo("trans:%d, action list:", pTrans->id);
52,278!
1084
  int32_t index = 0;
52,278✔
1085
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
77,665✔
1086
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
25,387✔
1087
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
25,387!
1088
          mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1089
  }
1090

1091
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
155,303✔
1092
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
103,025✔
1093
    mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
103,025!
1094
          mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1095
  }
1096

1097
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
280,205✔
1098
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
227,927✔
1099
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
227,927!
1100
          mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1101
  }
1102

1103
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
104,885✔
1104
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
52,607✔
1105
    if(pAction->actionType == TRANS_ACTION_MSG){
52,607✔
1106
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
36,878!
1107
            mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1108
    }
1109
    else{
1110
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
15,729!
1111
            mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1112
    }
1113
  }
1114

1115

1116
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
52,278!
1117

1118
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
52,278!
1119

1120
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
52,278!
1121

1122
  mInfo("trans:%d, prepare transaction", pTrans->id);
52,278!
1123
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
52,278✔
1124
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
11!
1125
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
11✔
1126
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
11✔
1127
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
11✔
1128
    TAOS_RETURN(code);
11✔
1129
  }
1130
  mInfo("trans:%d, prepare finished", pTrans->id);
52,267!
1131

1132
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
52,267✔
1133
  if (pNew == NULL) {
52,267!
1134
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1135
    if (terrno != 0) code = terrno;
×
1136
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, tstrerror(code));
×
1137
    TAOS_RETURN(code);
×
1138
  }
1139

1140
  pNew->pRpcArray = pTrans->pRpcArray;
52,267✔
1141
  pNew->rpcRsp = pTrans->rpcRsp;
52,267✔
1142
  pNew->rpcRspLen = pTrans->rpcRspLen;
52,267✔
1143
  pNew->mTraceId = pTrans->mTraceId;
52,267✔
1144
  pTrans->pRpcArray = NULL;
52,267✔
1145
  pTrans->rpcRsp = NULL;
52,267✔
1146
  pTrans->rpcRspLen = 0;
52,267✔
1147

1148
  mInfo("trans:%d, execute transaction in prepare", pTrans->id);
52,267!
1149
  mndTransExecute(pMnode, pNew, false);
52,267✔
1150
  mndReleaseTrans(pMnode, pNew);
52,267✔
1151
  // TDOD change to TAOS_RETURN(code);
1152
  return 0;
52,267✔
1153
}
1154

1155
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
52,278✔
1156
  int32_t code = 0;
52,278✔
1157
  mInfo("trans:%d, commit transaction", pTrans->id);
52,278!
1158
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
52,278✔
1159
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
11!
1160
    TAOS_RETURN(code);
11✔
1161
  }
1162
  mInfo("trans:%d, commit finished", pTrans->id);
52,267!
1163
  TAOS_RETURN(code);
52,267✔
1164
}
1165

1166
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
5✔
1167
  int32_t code = 0;
5✔
1168
  mInfo("trans:%d, rollback transaction", pTrans->id);
5!
1169
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
5✔
1170
    mError("trans:%d, failed to rollback since %s", pTrans->id, tstrerror(code));
1!
1171
    TAOS_RETURN(code);
1✔
1172
  }
1173
  mInfo("trans:%d, rollback finished", pTrans->id);
4!
1174
  TAOS_RETURN(code);
4✔
1175
}
1176

1177
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
5✔
1178
  int32_t code = 0;
5✔
1179
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
5!
1180
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
5!
1181
    mError("trans:%d, failed to pre-finish since %s", pTrans->id, tstrerror(code));
×
1182
    TAOS_RETURN(code);
×
1183
  }
1184
  mInfo("trans:%d, pre-finish finished", pTrans->id);
5!
1185
  TAOS_RETURN(code);
5✔
1186
}
1187

1188
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
295,741✔
1189
  bool    sendRsp = false;
295,741✔
1190
  int32_t code = pTrans->code;
295,741✔
1191

1192
  if (pTrans->stage == TRN_STAGE_FINISH) {
295,741✔
1193
    sendRsp = true;
113,246✔
1194
  }
1195

1196
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
295,741✔
1197
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
86,472✔
1198
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
67✔
1199
      sendRsp = true;
67✔
1200
    }
1201
  } else {
1202
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
209,269✔
1203
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
136,860!
1204
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1205
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1206
      } else {
1207
        if (pTrans->failedTimes > 6) sendRsp = true;
136,860✔
1208
      }
1209
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
136,860✔
1210
    }
1211
  }
1212

1213
  if (!sendRsp) {
295,741✔
1214
    return;
182,426✔
1215
  } else {
1216
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
113,315!
1217
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1218
  }
1219

1220
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
113,315!
1221
  taosWLockLatch(&pTrans->lockRpcArray);
113,315✔
1222
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
113,315✔
1223
  if (size <= 0) {
113,315✔
1224
    taosWUnLockLatch(&pTrans->lockRpcArray);
81,083✔
1225
    return;
81,083✔
1226
  }
1227

1228
  for (int32_t i = 0; i < size; ++i) {
64,464✔
1229
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
32,232✔
1230
    if (pInfo->handle != NULL) {
32,232✔
1231
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
30,696!
1232
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1233
      }
1234
      if (code == TSDB_CODE_SYN_TIMEOUT) {
30,696!
1235
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1236
      }
1237

1238
      if (i != 0 && code == 0) {
30,696!
1239
        code = TSDB_CODE_MNODE_NOT_FOUND;
×
1240
      }
1241
      mInfo("vgId:1, trans:%d, client:%d start to send rsp, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,696!
1242
            mndTransStr(pTrans->stage), pInfo->ahandle);
1243

1244
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
30,696✔
1245

1246
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
30,696✔
1247
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,676!
1248
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,676✔
1249
        if (pDb != NULL) {
4,676!
1250
          for (int32_t j = 0; j < 12; j++) {
5,459✔
1251
            bool ready = mndIsDbReady(pMnode, pDb);
5,456✔
1252
            if (!ready) {
5,456✔
1253
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
783!
1254
              taosMsleep(1000);
783✔
1255
            } else {
1256
              break;
4,673✔
1257
            }
1258
          }
1259
        }
1260
        mndReleaseDb(pMnode, pDb);
4,676✔
1261
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
26,020✔
1262
        void   *pCont = NULL;
8,475✔
1263
        int32_t contLen = 0;
8,475✔
1264
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
8,475✔
1265
          mndTransSetRpcRsp(pTrans, pCont, contLen);
8,470✔
1266
        }
1267
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
17,545✔
1268
        void   *pCont = NULL;
476✔
1269
        int32_t contLen = 0;
476✔
1270
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
476!
1271
          mndTransSetRpcRsp(pTrans, pCont, contLen);
476✔
1272
        }
1273
      }
1274

1275
      if (pTrans->rpcRspLen != 0) {
30,696✔
1276
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
15,503✔
1277
        if (rpcCont != NULL) {
15,503!
1278
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
15,503✔
1279
          rspMsg.pCont = rpcCont;
15,503✔
1280
          rspMsg.contLen = pTrans->rpcRspLen;
15,503✔
1281
        }
1282
      }
1283

1284
      tmsgSendRsp(&rspMsg);
30,696✔
1285

1286
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,696!
1287
            mndTransStr(pTrans->stage), pInfo->ahandle);
1288
    }
1289
  }
1290
  taosArrayClear(pTrans->pRpcArray);
32,232✔
1291
  taosWUnLockLatch(&pTrans->lockRpcArray);
32,232✔
1292
}
1293

1294
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
106,592✔
1295
  int32_t code = 0;
106,592✔
1296
  SMnode *pMnode = pRsp->info.node;
106,592✔
1297
#ifndef TD_ASTRA_32
1298
  int64_t signature = (int64_t)(pRsp->info.ahandle);
106,592✔
1299
  int32_t transId = (int32_t)(signature >> 32);
106,592✔
1300
  int32_t action = (int32_t)((signature << 32) >> 32);
106,592✔
1301
#else
1302
  int32_t transId = (int32_t)(pRsp->info.ahandle);
1303
  int32_t action = (int32_t)(pRsp->info.ahandleEx);
1304
#endif
1305
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
106,592✔
1306
  if (pTrans == NULL) {
106,592!
1307
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1308
    if (terrno != 0) code = terrno;
×
1309
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1310
    goto _OVER;
×
1311
  }
1312

1313
  SArray *pArray = NULL;
106,592✔
1314
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
106,592✔
1315
    pArray = pTrans->redoActions;
106,568✔
1316
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
24!
1317
    pArray = pTrans->undoActions;
24✔
1318
  } else {
1319
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1320
    goto _OVER;
×
1321
  }
1322

1323
  if (pArray == NULL) {
106,592!
1324
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1325
    goto _OVER;
×
1326
  }
1327

1328
  int32_t actionNum = taosArrayGetSize(pArray);
106,592✔
1329
  if (action < 0 || action >= actionNum) {
106,592!
1330
    mError("trans:%d, invalid action:%d", transId, action);
×
1331
    goto _OVER;
×
1332
  }
1333

1334
  STransAction *pAction = taosArrayGet(pArray, action);
106,592✔
1335
  if (pAction != NULL) {
106,592!
1336
    pAction->msgReceived = 1;
106,592✔
1337
    pAction->errCode = pRsp->code;
106,592✔
1338
    pAction->endTime = taosGetTimestampMs();
106,592✔
1339

1340
    // pTrans->lastErrorNo = pRsp->code;
1341
    mndSetTransLastAction(pTrans, pAction);
106,592✔
1342

1343
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
106,592!
1344
          mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1345
          tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1346
  } else {
1347
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1348
  }
1349

1350
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
106,592!
1351
  mndTransExecute(pMnode, pTrans, true);
106,592✔
1352

1353
_OVER:
106,592✔
1354
  mndReleaseTrans(pMnode, pTrans);
106,592✔
1355
  TAOS_RETURN(code);
106,592✔
1356
}
1357

1358
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
5,136✔
1359
  pAction->rawWritten = 0;
5,136✔
1360
  pAction->msgSent = 0;
5,136✔
1361
  pAction->msgReceived = 0;
5,136✔
1362
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
5,136!
1363
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
5,136✔
1364
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
11✔
1365
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
11!
1366
          pAction->id, pAction->epSet.inUse);
1367
  } else {
1368
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
5,125!
1369
  }
1370
  pAction->errCode = 0;
5,136✔
1371
}
5,136✔
1372

1373
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
8✔
1374
  int32_t numOfActions = taosArrayGetSize(pArray);
8✔
1375

1376
  for (int32_t action = 0; action < numOfActions; ++action) {
40✔
1377
    STransAction *pAction = taosArrayGet(pArray, action);
32✔
1378
    if (pAction->msgSent && pAction->msgReceived &&
32!
1379
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
32✔
1380
      continue;
24✔
1381
    if (pAction->msgSent && !pAction->msgReceived) {
8!
1382
      int64_t timestamp = taosGetTimestampMs();
×
1383
      if (timestamp - pAction->startTime <= TRANS_ACTION_TIMEOUT) continue;
×
1384
    }
1385

1386
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
8!
1387

1388
    mndTransResetAction(pMnode, pTrans, pAction);
8✔
1389
    mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage), pAction->id,
8!
1390
          pAction->errCode, pAction->startTime);
1391
  }
1392
}
8✔
1393

1394
// execute in sync context
1395
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
509,614✔
1396
  if (pAction->rawWritten) return 0;
509,614✔
1397
  if (topHalf) {
280,094✔
1398
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
10✔
1399
  }
1400

1401
  if (pAction->pRaw->type >= SDB_MAX) {
280,084!
1402
    pAction->rawWritten = true;
×
1403
    pAction->errCode = 0;
×
1404
    mndSetTransLastAction(pTrans, pAction);
×
1405
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
1406
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1407
  }
1408

1409
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
280,084✔
1410
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
280,084!
1411
    pAction->rawWritten = true;
280,084✔
1412
    pAction->errCode = 0;
280,084✔
1413
    code = 0;
280,084✔
1414
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
280,084!
1415
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1416

1417
    mndSetTransLastAction(pTrans, pAction);
280,084✔
1418
  } else {
1419
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1420
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1421
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1422
    mndSetTransLastAction(pTrans, pAction);
×
1423
  }
1424

1425
  TAOS_RETURN(code);
280,084✔
1426
}
1427

1428
// execute in trans context
1429
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
711,737✔
1430
                                     bool notSend) {
1431
  if (pAction->msgSent) return 0;
711,737✔
1432
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
149,094✔
1433
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
42,169✔
1434
  }
1435

1436
  if (notSend) {
106,925✔
1437
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
187!
1438
    return 0;
187✔
1439
  }
1440

1441
#ifndef TD_ASTRA_32
1442
  int64_t signature = pTrans->id;
106,738✔
1443
  signature = (signature << 32);
106,738✔
1444
  signature += pAction->id;
106,738✔
1445

1446
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
106,738✔
1447
#else
1448
  SRpcMsg rpcMsg = {.msgType = pAction->msgType,
1449
                    .contLen = pAction->contLen,
1450
                    .info.ahandle = (void *)pTrans->id,
1451
                    .info.ahandleEx = (void *)pAction->id};
1452
#endif
1453
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
106,738✔
1454
  if (rpcMsg.pCont == NULL) {
106,738!
1455
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1456
    return -1;
×
1457
  }
1458
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
106,738✔
1459
  rpcMsg.info.notFreeAhandle = 1;
106,738✔
1460

1461
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
106,738✔
1462

1463
  char    detail[1024] = {0};
106,738✔
1464
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
106,738!
1465
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
106,738✔
1466
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
218,194✔
1467
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
111,456✔
1468
                     pAction->epSet.eps[i].port);
111,456✔
1469
  }
1470

1471
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
106,738✔
1472
  if (code == 0) {
106,738✔
1473
    pAction->msgSent = 1;
106,726✔
1474
    // pAction->msgReceived = 0;
1475
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
106,726✔
1476
    pAction->startTime = taosGetTimestampMs();
106,726✔
1477
    pAction->endTime = 0;
106,726✔
1478
    mInfo("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
106,726!
1479

1480
    mndSetTransLastAction(pTrans, pAction);
106,726✔
1481
  } else {
1482
    pAction->msgSent = 0;
12✔
1483
    pAction->msgReceived = 0;
12✔
1484
    pAction->errCode = (terrno != 0) ? terrno : code;
12!
1485
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
12!
1486
           detail);
1487

1488
    mndSetTransLastAction(pTrans, pAction);
12✔
1489
  }
1490

1491
  TAOS_RETURN(code);
106,738✔
1492
}
1493

1494
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1495
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1496
  pAction->rawWritten = 0;
×
1497
  pAction->errCode = 0;
×
1498
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1499

1500
  mndSetTransLastAction(pTrans, pAction);
×
1501
  return 0;
×
1502
}
1503

1504
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
1,221,351✔
1505
                                        bool notSend) {
1506
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,221,351✔
1507
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
509,614✔
1508
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
711,737!
1509
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
711,737✔
1510
  } else {
1511
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1512
  }
1513
}
1514

1515
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
273,471✔
1516
  int32_t numOfActions = taosArrayGetSize(pArray);
273,471✔
1517
  int32_t code = 0;
273,471✔
1518

1519
  for (int32_t action = 0; action < numOfActions; ++action) {
1,391,833✔
1520
    STransAction *pAction = taosArrayGet(pArray, action);
1,152,977✔
1521
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
1,152,977✔
1522
    if (code != 0) {
1,152,977✔
1523
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
34,615!
1524
            numOfActions);
1525
      break;
34,615✔
1526
    }
1527
  }
1528

1529
  return code;
273,471✔
1530
}
1531

1532
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
317,637✔
1533
  int32_t numOfActions = taosArrayGetSize(pArray);
317,637✔
1534
  int32_t code = 0;
317,637✔
1535
  if (numOfActions == 0) return 0;
317,637✔
1536

1537
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
273,471✔
1538
    return code;
34,615✔
1539
  }
1540

1541
  int32_t       numOfExecuted = 0;
238,856✔
1542
  int32_t       errCode = 0;
238,856✔
1543
  STransAction *pErrAction = NULL;
238,856✔
1544
  for (int32_t action = 0; action < numOfActions; ++action) {
1,357,218✔
1545
    STransAction *pAction = taosArrayGet(pArray, action);
1,118,362✔
1546
    if (pAction->msgReceived || pAction->rawWritten) {
1,118,362✔
1547
      numOfExecuted++;
797,798✔
1548
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
797,798✔
1549
        errCode = pAction->errCode;
33✔
1550
        pErrAction = pAction;
33✔
1551
      }
1552
    } else {
1553
      pErrAction = pAction;
320,564✔
1554
    }
1555
  }
1556

1557
  mndSetTransLastAction(pTrans, pErrAction);
238,856✔
1558

1559
  if (numOfExecuted == numOfActions) {
238,856✔
1560
    if (errCode == 0) {
141,940✔
1561
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
141,932!
1562
      return 0;
141,932✔
1563
    } else {
1564
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
8!
1565
      mndTransResetActions(pMnode, pTrans, pArray);
8✔
1566
      terrno = errCode;
8✔
1567
      return errCode;
8✔
1568
    }
1569
  } else {
1570
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
96,916!
1571

1572
    for (int32_t action = 0; action < numOfActions; ++action) {
645,007✔
1573
      STransAction *pAction = taosArrayGet(pArray, action);
548,091✔
1574
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
548,091✔
1575
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1576
             pAction->acceptableCode, pAction->retryCode);
1577
      if (pAction->msgSent) {
548,091✔
1578
        bool reset = false;
547,904✔
1579
        if (pAction->msgReceived) {
547,904✔
1580
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
227,527✔
1581
        } else {
1582
          int64_t timestamp = taosGetTimestampMs();
320,377✔
1583
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
320,377!
1584
        }
1585
        if (reset) {
547,904✔
1586
          mndTransResetAction(pMnode, pTrans, pAction);
25✔
1587
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
25!
1588
                pAction->id, pAction->errCode, pAction->startTime);
1589
        }
1590
      }
1591
    }
1592
    return TSDB_CODE_ACTION_IN_PROGRESS;
96,916✔
1593
  }
1594
}
1595

1596
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
204,335✔
1597
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf, notSend);
204,335✔
1598
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
204,335✔
1599
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
16!
1600
           mndStrExecutionContext(topHalf));
1601
  }
1602
  return code;
204,335✔
1603
}
1604

1605
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
71✔
1606
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf, notSend);
71✔
1607
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
71✔
1608
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
4!
1609
           mndStrExecutionContext(topHalf));
1610
  }
1611
  return code;
71✔
1612
}
1613

1614
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
113,231✔
1615
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf, true);
113,231✔
1616
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
113,231!
1617
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1618
           mndStrExecutionContext(topHalf));
1619
  }
1620
  return code;
113,231✔
1621
}
1622

1623
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
30,364✔
1624
  int32_t code = 0;
30,364✔
1625
  int32_t numOfActions = taosArrayGetSize(pActions);
30,364✔
1626
  if (numOfActions == 0) return code;
30,364✔
1627

1628
  if (pTrans->actionPos >= numOfActions) {
30,362✔
1629
    return code;
3,115✔
1630
  }
1631

1632
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
27,247!
1633
        pTrans->actionPos, mndTransStr(pTrans->stage));
1634

1635
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
40,747✔
1636
    STransAction *pAction = taosArrayGet(pActions, action);
37,919✔
1637

1638
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", 
37,919!
1639
          pTrans->id, pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent,
1640
          pAction->msgReceived);
1641

1642
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
37,919✔
1643
    if (code == 0) {
37,919✔
1644
      if (pAction->msgSent) {
30,343✔
1645
        bool reset = false;
26,836✔
1646
        if (pAction->msgReceived) {
26,836✔
1647
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
11,921✔
1648
            code = pAction->errCode;
5,103✔
1649
            reset = true;
5,103✔
1650
          } else {
1651
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
6,818!
1652
          }
1653
        } else {
1654
          int64_t timestamp = taosGetTimestampMs();
14,915✔
1655
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
14,915!
1656
          code = TSDB_CODE_ACTION_IN_PROGRESS;
14,915✔
1657
        }
1658
        if (reset) {
26,836✔
1659
          mndTransResetAction(pMnode, pTrans, pAction);
5,103✔
1660
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
5,103!
1661
                pAction->id, pAction->errCode, pAction->startTime);
1662
        }
1663
      } else if (pAction->rawWritten) {
3,507!
1664
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,507!
1665
          code = pAction->errCode;
×
1666
        } else {
1667
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,507!
1668
        }
1669
      } else {
1670
      }
1671
    }
1672

1673
    if (code == 0) {
37,919✔
1674
      pTrans->failedTimes = 0;
10,325✔
1675
    }
1676
    mndSetTransLastAction(pTrans, pAction);
37,919✔
1677

1678
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
37,919✔
1679
      pTrans->lastErrorNo = code;
9,487✔
1680
      pTrans->code = code;
9,487✔
1681
      mInfo("trans:%d, %s:%d, cannot execute next action in %s, code:%s", pTrans->id, mndTransStr(pAction->stage),
9,487!
1682
            action, mndStrExecutionContext(topHalf), tstrerror(code));
1683
      break;
9,487✔
1684
    }
1685

1686
    if (code == 0) {
28,432✔
1687
      pTrans->code = 0;
8,414✔
1688
      pTrans->actionPos++;
8,414✔
1689
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
8,414!
1690
            pAction->id);
1691
      (void)taosThreadMutexUnlock(&pTrans->mutex);
8,414✔
1692
      code = mndTransSync(pMnode, pTrans);
8,414✔
1693
      (void)taosThreadMutexLock(&pTrans->mutex);
8,414✔
1694
      if (code != 0) {
8,414!
1695
        pTrans->actionPos--;
×
1696
        pTrans->code = terrno;
×
1697
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1698
               mndTransStr(pAction->stage), pAction->id, terrstr());
1699
        break;
×
1700
      }
1701
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
20,018✔
1702
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
14,915!
1703
      break;
14,915✔
1704
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
5,103✔
1705
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
28✔
1706
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
5,086!
1707
            code, tstrerror(code));
1708
      pTrans->lastErrorNo = code;
5,086✔
1709
      taosMsleep(300);
5,086✔
1710
      action--;
5,086✔
1711
      continue;
5,086✔
1712
    } else {
1713
      terrno = code;
17✔
1714
      pTrans->lastErrorNo = code;
17✔
1715
      pTrans->code = code;
17✔
1716
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
17!
1717
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1718
      break;
17✔
1719
    }
1720
  }
1721

1722
  return code;
27,247✔
1723
}
1724

1725
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
30,364✔
1726
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
30,364✔
1727
  (void)taosThreadMutexLock(&pTrans->mutex);
30,364✔
1728
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
30,364!
1729
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
30,364✔
1730
  }
1731
  (void)taosThreadMutexUnlock(&pTrans->mutex);
30,364✔
1732
  return code;
30,364✔
1733
}
1734

1735
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
1736
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1737
  (void)taosThreadMutexLock(&pTrans->mutex);
×
1738
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1739
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
1740
  }
1741
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
1742
  return code;
×
1743
}
1744

1745
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
61,009✔
1746
  bool    continueExec = true;
61,009✔
1747
  int32_t code = 0;
61,009✔
1748
  terrno = 0;
61,009✔
1749

1750
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
61,009✔
1751
  if (numOfActions == 0) goto _OVER;
61,009✔
1752

1753
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
19,447!
1754

1755
  for (int32_t action = 0; action < numOfActions; ++action) {
49,902✔
1756
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
30,455✔
1757
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, true);
30,455✔
1758
    if (code != 0) {
30,455!
1759
      terrno = code;
×
1760
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
1761
             numOfActions, tstrerror(code));
1762
      return false;
×
1763
    }
1764
  }
1765

1766
_OVER:
19,447✔
1767
  pTrans->stage = TRN_STAGE_REDO_ACTION;
61,009✔
1768
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
61,009!
1769
  return continueExec;
61,009✔
1770
}
1771

1772
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
234,699✔
1773
  bool    continueExec = true;
234,699✔
1774
  int32_t code = 0;
234,699✔
1775
  terrno = 0;
234,699✔
1776

1777
  if (pTrans->exec == TRN_EXEC_SERIAL) {
234,699✔
1778
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
30,364✔
1779
  } else {
1780
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
204,335✔
1781
  }
1782

1783
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
234,699!
1784
    pTrans->lastErrorNo = code;
×
1785
    pTrans->code = code;
×
1786
    mInfo(
×
1787
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
1788
        "continueExec:%d, code:%s",
1789
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1790
    taosMsleep(100);
×
1791
    return true;
×
1792
  } else {
1793
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
234,699✔
1794
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
70,607!
1795
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1796
      return false;
70,607✔
1797
    }
1798
  }
1799
  terrno = code;
164,092✔
1800

1801
  if (code == 0) {
164,092✔
1802
    pTrans->code = 0;
52,278✔
1803
    pTrans->stage = TRN_STAGE_COMMIT;
52,278✔
1804
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
52,278!
1805
    continueExec = true;
52,278✔
1806
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
111,814!
1807
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
111,781!
1808
    continueExec = false;
111,781✔
1809
  } else {
1810
    pTrans->failedTimes++;
33✔
1811
    pTrans->code = terrno;
33✔
1812
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
33✔
1813
      if (pTrans->lastAction != 0) {
5✔
1814
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
1815
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
1816
          if (pTrans->failedTimes < 6) {
×
1817
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
1818
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
1819
            taosMsleep(1000);
×
1820
            continueExec = true;
×
1821
            return true;
×
1822
          }
1823
        }
1824
      }
1825

1826
      pTrans->stage = TRN_STAGE_ROLLBACK;
5✔
1827
      pTrans->actionPos = 0;
5✔
1828
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
5!
1829
             pTrans->actionPos);
1830
      continueExec = true;
5✔
1831
    } else {
1832
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
28!
1833
      continueExec = false;
28✔
1834
    }
1835
  }
1836

1837
  return continueExec;
164,092✔
1838
}
1839

1840
// execute in trans context
1841
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
52,280✔
1842
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
52,280✔
1843

1844
  bool    continueExec = true;
52,278✔
1845
  int32_t code = mndTransCommit(pMnode, pTrans);
52,278✔
1846

1847
  if (code == 0) {
52,278✔
1848
    pTrans->code = 0;
52,267✔
1849
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
52,267✔
1850
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
52,267!
1851
    continueExec = true;
52,267✔
1852
  } else {
1853
    pTrans->code = terrno;
11✔
1854
    pTrans->failedTimes++;
11✔
1855
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
11!
1856
    continueExec = false;
11✔
1857
  }
1858

1859
  return continueExec;
52,278✔
1860
}
1861

1862
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
113,231✔
1863
  bool    continueExec = true;
113,231✔
1864
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
113,231✔
1865

1866
  if (code == 0) {
113,231✔
1867
    pTrans->code = 0;
113,229✔
1868
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
113,229✔
1869
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
113,229!
1870
    continueExec = true;
113,229✔
1871
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
2!
1872
    pTrans->code = 0;
2✔
1873
    pTrans->stage = TRN_STAGE_COMMIT;
2✔
1874
    mInfo("trans:%d, back to commit stage", pTrans->id);
2!
1875
    continueExec = true;
2✔
1876
  } else {
1877
    pTrans->code = terrno;
×
1878
    pTrans->failedTimes++;
×
1879
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1880
    continueExec = false;
×
1881
  }
1882

1883
  return continueExec;
113,231✔
1884
}
1885

1886
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
71✔
1887
  bool    continueExec = true;
71✔
1888
  int32_t code = 0;
71✔
1889

1890
  if (pTrans->exec == TRN_EXEC_SERIAL) {
71!
1891
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
1892
  } else {
1893
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
71✔
1894
  }
1895

1896
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
71✔
1897
  terrno = code;
59✔
1898

1899
  if (code == 0) {
59✔
1900
    pTrans->stage = TRN_STAGE_PRE_FINISH;
5✔
1901
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
5!
1902
    continueExec = true;
5✔
1903
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
54!
1904
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
50!
1905
    continueExec = false;
50✔
1906
  } else {
1907
    pTrans->failedTimes++;
4✔
1908
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
4!
1909
    continueExec = false;
4✔
1910
  }
1911

1912
  return continueExec;
59✔
1913
}
1914

1915
// in trans context
1916
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
5✔
1917
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
5!
1918

1919
  bool    continueExec = true;
5✔
1920
  int32_t code = mndTransRollback(pMnode, pTrans);
5✔
1921

1922
  if (code == 0) {
5✔
1923
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
4✔
1924
    continueExec = true;
4✔
1925
  } else {
1926
    pTrans->failedTimes++;
1✔
1927
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
1!
1928
    continueExec = false;
1✔
1929
  }
1930

1931
  return continueExec;
5✔
1932
}
1933

1934
// excute in trans context
1935
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
5✔
1936
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
5!
1937

1938
  bool    continueExec = true;
5✔
1939
  int32_t code = mndTransPreFinish(pMnode, pTrans);
5✔
1940

1941
  if (code == 0) {
5!
1942
    pTrans->stage = TRN_STAGE_FINISH;
5✔
1943
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
5!
1944
    continueExec = true;
5✔
1945
  } else {
1946
    pTrans->failedTimes++;
×
1947
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1948
    continueExec = false;
×
1949
  }
1950

1951
  return continueExec;
5✔
1952
}
1953

1954
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
113,245✔
1955
  bool continueExec = false;
113,245✔
1956
  if (topHalf) return continueExec;
113,245✔
1957

1958
  SSdbRaw *pRaw = mndTransEncode(pTrans);
60,973✔
1959
  if (pRaw == NULL) {
60,973!
1960
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
1961
    return false;
×
1962
  }
1963
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
60,973!
1964

1965
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
60,973✔
1966
  if (code != 0) {
60,973!
1967
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
1968
  }
1969

1970
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
60,973!
1971
        pTrans->failedTimes, pTrans->createdTime);
1972
  return continueExec;
60,973✔
1973
}
1974

1975
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
295,741✔
1976
  bool continueExec = true;
295,741✔
1977

1978
  while (continueExec) {
809,277✔
1979
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
513,536!
1980
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1981
    pTrans->lastExecTime = taosGetTimestampMs();
513,536✔
1982
    switch (pTrans->stage) {
513,536!
1983
      case TRN_STAGE_PREPARE:
×
1984
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1985
        break;
×
1986
      case TRN_STAGE_REDO_ACTION:
234,699✔
1987
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
234,699✔
1988
        break;
234,699✔
1989
      case TRN_STAGE_COMMIT:
52,280✔
1990
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
52,280✔
1991
        break;
52,280✔
1992
      case TRN_STAGE_COMMIT_ACTION:
113,231✔
1993
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
113,231✔
1994
        break;
113,231✔
1995
      case TRN_STAGE_ROLLBACK:
5✔
1996
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
5✔
1997
        break;
5✔
1998
      case TRN_STAGE_UNDO_ACTION:
71✔
1999
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
71✔
2000
        break;
71✔
2001
      case TRN_STAGE_PRE_FINISH:
5✔
2002
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
5✔
2003
        break;
5✔
2004
      case TRN_STAGE_FINISH:
113,245✔
2005
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
113,245✔
2006
        break;
113,245✔
2007
      default:
×
2008
        continueExec = false;
×
2009
        break;
×
2010
    }
2011
  }
2012

2013
  mndTransSendRpcRsp(pMnode, pTrans);
295,741✔
2014
}
295,741✔
2015

2016
// start trans, pullup, receive rsp, kill
2017
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
164,361✔
2018
  bool topHalf = true;
164,361✔
2019
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
164,361✔
2020
}
164,361✔
2021

2022
// update trans
2023
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
131,380✔
2024
  bool topHalf = false;
131,380✔
2025
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
131,380✔
2026
}
131,380✔
2027

2028
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
44,588✔
2029
  mTrace("start to process trans timer");
44,588✔
2030
  mndTransPullup(pReq->info.node);
44,588✔
2031
  return 0;
44,588✔
2032
}
2033

2034
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
2035
  SArray *pArray = NULL;
×
2036
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
2037
    pArray = pTrans->redoActions;
×
2038
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
2039
    pArray = pTrans->undoActions;
×
2040
  } else {
2041
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2042
  }
2043

2044
  if(pTrans->ableToBeKilled == false){
×
2045
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2046
  }
2047
  
2048
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
×
2049
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
2050
      STransAction *pAction = taosArrayGet(pArray, i);
×
2051
      mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
2052
            mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
2053
      pAction->msgSent = 1;
×
2054
      pAction->msgReceived = 1;
×
2055
      pAction->errCode = 0;
×
2056
    }
2057
  }
2058
  else if(pTrans->killMode == TRN_KILL_MODE_INTERUPT){
×
2059
    pTrans->stage = TRN_STAGE_PRE_FINISH;
×
2060
  }
2061
  else{
2062
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2063
  }
2064

2065
  mInfo("trans:%d, execute transaction in kill trans", pTrans->id);
×
2066
  mndTransExecute(pMnode, pTrans, true);
×
2067
  return 0;
×
2068
}
2069

2070
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1✔
2071
  SMnode       *pMnode = pReq->info.node;
1✔
2072
  SKillTransReq killReq = {0};
1✔
2073
  int32_t       code = -1;
1✔
2074
  STrans       *pTrans = NULL;
1✔
2075

2076
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
2077
    code = TSDB_CODE_INVALID_MSG;
×
2078
    goto _OVER;
×
2079
  }
2080

2081
  mInfo("trans:%d, start to kill", killReq.transId);
1!
2082
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1!
2083
    goto _OVER;
1✔
2084
  }
2085

2086
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
2087
  if (pTrans == NULL) {
×
2088
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
2089
    if (terrno != 0) code = terrno;
×
2090
    goto _OVER;
×
2091
  }
2092

2093
  code = mndKillTrans(pMnode, pTrans);
×
2094

2095
_OVER:
1✔
2096
  if (code != 0) {
1!
2097
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1!
2098
  }
2099

2100
  mndReleaseTrans(pMnode, pTrans);
1✔
2101
  TAOS_RETURN(code);
1✔
2102
}
2103

2104
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
357✔
2105

2106
void mndTransPullup(SMnode *pMnode) {
45,097✔
2107
  SSdb   *pSdb = pMnode->pSdb;
45,097✔
2108
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
45,097✔
2109
  if (pArray == NULL) return;
45,097!
2110

2111
  void *pIter = NULL;
45,097✔
2112
  while (1) {
5,502✔
2113
    STrans *pTrans = NULL;
50,599✔
2114
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
50,599✔
2115
    if (pIter == NULL) break;
50,599✔
2116
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
11,004!
2117
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2118
    }
2119
    sdbRelease(pSdb, pTrans);
5,502✔
2120
  }
2121

2122
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
45,097✔
2123

2124
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
50,599✔
2125
    int32_t *pTransId = taosArrayGet(pArray, i);
5,502✔
2126
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
5,502✔
2127
    if (pTrans != NULL) {
5,502!
2128
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
5,502!
2129
      mndTransExecute(pMnode, pTrans, false);
5,502✔
2130
    }
2131
    mndReleaseTrans(pMnode, pTrans);
5,502✔
2132
  }
2133
  taosArrayDestroy(pArray);
45,097✔
2134
}
2135

2136
static char *formatTimestamp(char *buf, int64_t val, int precision) {
12,782✔
2137
  time_t tt;
2138
  if (precision == TSDB_TIME_PRECISION_MICRO) {
12,782!
2139
    tt = (time_t)(val / 1000000);
×
2140
  }
2141
  if (precision == TSDB_TIME_PRECISION_NANO) {
12,782!
2142
    tt = (time_t)(val / 1000000000);
×
2143
  } else {
2144
    tt = (time_t)(val / 1000);
12,782✔
2145
  }
2146

2147
  struct tm tm;
2148
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
12,782!
2149
    mError("failed to get local time");
×
2150
    return NULL;
×
2151
  }
2152
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
12,782✔
2153

2154
  if (precision == TSDB_TIME_PRECISION_MICRO) {
12,782!
2155
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2156
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
12,782!
2157
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2158
  } else {
2159
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
12,782✔
2160
  }
2161

2162
  return buf;
12,782✔
2163
}
2164

2165
static void mndTransLogAction(STrans *pTrans) {
194✔
2166
  char    detail[512] = {0};
194✔
2167
  int32_t len = 0;
194✔
2168
  int32_t index = 0;
194✔
2169

2170
  if (pTrans->stage == TRN_STAGE_PREPARE) {
194!
2171
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2172
      len = 0;
×
2173
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2174
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2175
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2176
                      sdbStatusName(pAction->pRaw->status));
×
2177
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2178
    }
2179
  }
2180

2181
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
194!
2182
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
8,080✔
2183
      len = 0;
7,886✔
2184
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
7,886✔
2185
      if (pAction->actionType == TRANS_ACTION_MSG) {
7,886✔
2186
        char bufStart[40] = {0};
6,391✔
2187
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
6,391✔
2188

2189
        char endStart[40] = {0};
6,391✔
2190
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
6,391✔
2191
        len += snprintf(detail + len, sizeof(detail) - len,
12,782!
2192
                        "action:%d, %s:%d msgType:%s,"
2193
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2194
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
6,391✔
2195
                        pAction->msgReceived, bufStart, endStart);
6,391✔
2196

2197
        SEpSet epset = pAction->epSet;
6,391✔
2198
        if (epset.numOfEps > 0) {
6,391!
2199
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
6,391✔
2200
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
14,821✔
2201
            len +=
8,430✔
2202
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
8,430✔
2203
          }
2204
        }
2205

2206
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
6,391✔
2207
                        tstrerror(pAction->errCode));
2208
      } else {
2209
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
1,495✔
2210
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,495✔
2211
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,495✔
2212
      }
2213
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
7,886✔
2214
    }
2215
  }
2216

2217
  if (pTrans->stage == TRN_STAGE_COMMIT_ACTION) {
194!
2218
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
×
2219
      len = 0;
×
2220
      STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
×
2221
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2222
                      mndTransStr(pAction->stage), i, sdbTableName(pAction->pRaw->type),
×
2223
                      sdbStatusName(pAction->pRaw->status));
×
2224
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2225
    }
2226

2227
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
×
2228
      len = 0;
×
2229
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
2230
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
2231
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
2232
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2233
        ;
2234
      } else {
2235
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2236
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2237
                        sdbStatusName(pAction->pRaw->status));
×
2238
      }
2239
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2240
    }
2241
  }
2242
}
194✔
2243

2244
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
7,466✔
2245
  SMnode *pMnode = pReq->info.node;
7,466✔
2246
  SSdb   *pSdb = pMnode->pSdb;
7,466✔
2247
  int32_t numOfRows = 0;
7,466✔
2248
  STrans *pTrans = NULL;
7,466✔
2249
  int32_t cols = 0;
7,466✔
2250
  int32_t code = 0;
7,466✔
2251
  int32_t lino = 0;
7,466✔
2252

2253
  while (numOfRows < rows) {
7,659!
2254
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
7,659✔
2255
    if (pShow->pIter == NULL) break;
7,668✔
2256

2257
    cols = 0;
194✔
2258

2259
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2260
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
194!
2261

2262
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2263
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
194!
2264
                        _OVER);
2265

2266
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
194✔
2267
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
194✔
2268
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2269
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
194!
2270

2271
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
194✔
2272
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
194✔
2273
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2274
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
194!
2275

2276
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
194✔
2277
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
194✔
2278
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2279
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
194!
2280

2281
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
194✔
2282
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
194✔
2283
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2284
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
194!
2285

2286
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
194!
2287
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
194✔
2288
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
194✔
2289
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2290
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
194!
2291

2292
    /*
2293
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2294
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2295
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2296
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2297
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2298
    */
2299

2300
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2301
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
194!
2302
                        _OVER);
2303

2304
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2305
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
194!
2306
                        _OVER);
2307

2308
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
194✔
2309
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
194✔
2310
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
582✔
2311
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
194✔
2312
    SEpSet  epset = pTrans->lastEpset;
194✔
2313
    if (epset.numOfEps > 0) {
194✔
2314
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
336!
2315
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
336✔
2316
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
466✔
2317
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
298✔
2318
      }
2319
    }
2320
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
194✔
2321
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
194✔
2322
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
194!
2323

2324
    mndTransLogAction(pTrans);
194✔
2325

2326
    numOfRows++;
194✔
2327
    sdbRelease(pSdb, pTrans);
194✔
2328
  }
2329

2330
_OVER:
×
2331
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
7,474!
2332
  pShow->numOfRows += numOfRows;
7,474✔
2333
  return numOfRows;
7,474✔
2334
}
2335

2336
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
×
2337
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2338
  int32_t code = 0;
×
2339
  int32_t lino = 0;
×
2340
  int32_t len = 0;
×
2341

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

2345
  char action[30 + 1] = {0};
×
2346
  if (curActionId == pAction->id) {
×
2347
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
×
2348
                    mndTransTypeStr(pAction->actionType));
2349
  } else {
2350
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
×
2351
                    mndTransTypeStr(pAction->actionType));
2352
  }
2353
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
×
2354
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
×
2355
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
2356
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
×
2357
_OVER:
×
2358
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2359
  return code;
×
2360
}
2361

2362
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
×
2363
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2364
  int32_t code = 0;
×
2365
  int32_t lino = 0;
×
2366
  int32_t len = 0;
×
2367
  int32_t cols = 0;
×
2368

2369
  cols = 0;
×
2370

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

2373
  if (pAction->actionType == TRANS_ACTION_MSG) {
×
2374
    int32_t len = 0;
×
2375

2376
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2377
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
×
2378
                    pAction->msgSent, pAction->msgReceived);
×
2379
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2380
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2381
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2382
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2383

2384
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2385
    len = 0;
×
2386
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
×
2387
                    tstrerror(pAction->errCode));
2388
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2389
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2390
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2391
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2392

2393
    char target[TSDB_TRANS_TARGET_LEN] = {0};
×
2394
    len = 0;
×
2395
    SEpSet epset = pAction->epSet;
×
2396
    if (epset.numOfEps > 0) {
×
2397
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
×
2398
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
×
2399
      }
2400
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
×
2401
    }
2402
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2403
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2404
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2405
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2406

2407
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2408
    len = 0;
×
2409
    char bufStart[40] = {0};
×
2410
    if (pAction->startTime > 0) (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
×
2411
    char bufEnd[40] = {0};
×
2412
    if (pAction->endTime > 0) (void)formatTimestamp(bufEnd, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
×
2413
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
×
2414
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2415
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2416
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2417
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2418

2419
  } else {
2420
    int32_t len = 0;
×
2421

2422
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2423
    if (pAction->pRaw->type == SDB_VGROUP) {
×
2424
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
×
2425
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
×
2426
      len += snprintf(objType + len, sizeof(objType) - len, "%s(%d)", sdbTableName(pAction->pRaw->type), pVgroup->vgId);
×
2427
      taosMemoryFreeClear(pRow);
×
2428
    } else {
2429
      strcpy(objType, sdbTableName(pAction->pRaw->type));
×
2430
    }
2431
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2432
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2433
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2434
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2435

2436
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2437
    len = 0;
×
2438
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
×
2439
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2440
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2441
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2442
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2443

2444
    char target[TSDB_TRANS_TARGET_LEN] = "";
×
2445
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2446
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2447
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2448
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2449

2450
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2451
    len = 0;
×
2452
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
×
2453
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2454
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2455
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2456
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2457
  }
2458

2459
_OVER:
×
2460
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2461
}
2462

2463
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
×
2464
  if (stage == TRN_STAGE_PREPARE) {
×
2465
    return pTrans->prepareActions;
×
2466
  }
2467
  if (stage == TRN_STAGE_REDO_ACTION) {
×
2468
    return pTrans->redoActions;
×
2469
  }
2470
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
2471
    return pTrans->commitActions;
×
2472
  }
2473
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
2474
    return pTrans->undoActions;
×
2475
  }
2476
  return NULL;
×
2477
}
2478

2479
typedef struct STransDetailIter {
2480
  void     *pIter;
2481
  STrans   *pTrans;
2482
  ETrnStage stage;
2483
  int32_t   num;
2484
} STransDetailIter;
2485

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

2491
  for (int32_t i = start; i < actionNum; ++i) {
×
2492
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
×
2493
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
×
2494
    (*numOfRows)++;
×
2495
    if (*numOfRows >= rows) break;
×
2496
  }
2497

2498
  if (*numOfRows == end) {
×
2499
    sdbRelease(pSdb, pShowIter->pTrans);
×
2500
    pShowIter->pTrans = NULL;
×
2501
    pShowIter->num = 0;
×
2502
  } else {
2503
    pShowIter->pTrans = pShowIter->pTrans;
×
2504
    pShowIter->stage = pShowIter->pTrans->stage;
×
2505
    pShowIter->num += (*numOfRows);
×
2506
  }
2507
}
×
2508

2509
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2510
  SMnode *pMnode = pReq->info.node;
×
2511
  SSdb   *pSdb = pMnode->pSdb;
×
2512
  int32_t numOfRows = 0;
×
2513

2514
  int32_t code = 0;
×
2515
  int32_t lino = 0;
×
2516

2517
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
×
2518
        pShow->pIter);
2519

2520
  if (pShow->pIter == NULL) {
×
2521
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
×
2522
    if (pShow->pIter == NULL) {
×
2523
      mError("failed to malloc for pShow->pIter");
×
2524
      return 0;
×
2525
    }
2526
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
×
2527
  }
2528

2529
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
×
2530

2531
  while (numOfRows < rows) {
×
2532
    if (pShowIter->pTrans == NULL) {
×
2533
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
×
2534
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
×
2535
      if (pShowIter->pIter == NULL) break;
×
2536
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
×
2537
            pShowIter->pTrans->stage, pShowIter->num);
2538

2539
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
×
2540

2541
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
×
2542
      break;
×
2543
    } else {
2544
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
×
2545
            pShowIter->num);
2546
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
×
2547

2548
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
×
2549
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
×
2550
      break;
×
2551
    }
2552
  }
2553

2554
_OVER:
×
2555
  pShow->numOfRows += numOfRows;
×
2556

2557
  if (code != 0) {
×
2558
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2559
  } else {
2560
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
×
2561
  }
2562
  if (numOfRows == 0) {
×
2563
    taosMemoryFree(pShow->pIter);
×
2564
    pShow->pIter = NULL;
×
2565
  }
2566
  return numOfRows;
×
2567
}
2568

2569
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
2570
  SSdb *pSdb = pMnode->pSdb;
×
2571
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
2572
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc