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

taosdata / TDengine / #3572

02 Jan 2025 08:57AM UTC coverage: 63.077% (-0.2%) from 63.276%
#3572

push

travis-ci

web-flow
Merge pull request #29450 from taosdata/fix/TS-5651-skip-sync-heartbeat

fix:[TS-5651]skip-sync-heartbeat

139525 of 284348 branches covered (49.07%)

Branch coverage included in aggregate %.

217427 of 281548 relevant lines covered (77.23%)

18571459.85 hits per line

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

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

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

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

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

57
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
486,076✔
58

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

66
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
486,242✔
67

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

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

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

89
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
1,732✔
90
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
1,732✔
91

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

98
void mndCleanupTrans(SMnode *pMnode) {}
1,731✔
99

100
static int32_t mndTransGetActionsSize(SArray *pArray) {
559,740✔
101
  int32_t actionNum = taosArrayGetSize(pArray);
559,740✔
102
  int32_t rawDataLen = 0;
559,740✔
103

104
  for (int32_t i = 0; i < actionNum; ++i) {
1,759,486✔
105
    STransAction *pAction = taosArrayGet(pArray, i);
1,199,746✔
106
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,199,746✔
107
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
739,022✔
108
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
460,724!
109
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
460,724✔
110
    } else {
111
      // empty
112
    }
113
    rawDataLen += sizeof(int8_t);
1,199,746✔
114
  }
115

116
  return rawDataLen;
559,740✔
117
}
118

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

126
  for (int32_t i = 0; i < actionsNum; ++i) {
1,759,486✔
127
    STransAction *pAction = taosArrayGet(pActions, i);
1,199,746✔
128
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
1,199,746!
129
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
1,199,746!
130
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
1,199,746!
131
    SDB_SET_INT32(pRaw, dataPos, pAction->retryCode, _OVER)
1,199,746!
132
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
1,199,746!
133
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
1,199,746!
134
    SDB_SET_INT8(pRaw, dataPos, pAction->reserved, _OVER)
1,199,746!
135
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,199,746✔
136
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
739,022✔
137
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->rawWritten*/, _OVER)
739,022!
138
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
739,022!
139
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
739,022!
140
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
460,724!
141
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
460,724!
142
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
460,724!
143
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgSent*/, _OVER)
460,724!
144
      SDB_SET_INT8(pRaw, dataPos, unused /*pAction->msgReceived*/, _OVER)
460,724!
145
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
460,724!
146
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
460,724!
147
    } else {
148
      // nothing
149
    }
150
  }
151
  ret = 0;
559,740✔
152

153
_OVER:
559,740✔
154
  *offset = dataPos;
559,740✔
155
  return ret;
559,740✔
156
}
157

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

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

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

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

190
  int32_t prepareActionNum = taosArrayGetSize(pTrans->prepareActions);
139,935✔
191
  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
139,935✔
192
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
139,935✔
193
  int32_t commitActionNum = taosArrayGetSize(pTrans->commitActions);
139,935✔
194

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

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

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

214
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
139,935!
215

216
  int32_t arbGroupNum = taosHashGetSize(pTrans->arbGroupIds);
139,935✔
217
  SDB_SET_INT32(pRaw, dataPos, arbGroupNum, _OVER)
139,935!
218
  void *pIter = NULL;
139,935✔
219
  pIter = taosHashIterate(pTrans->arbGroupIds, NULL);
139,935✔
220
  while (pIter) {
139,953✔
221
    int32_t arbGroupId = *(int32_t *)pIter;
18✔
222
    SDB_SET_INT32(pRaw, dataPos, arbGroupId, _OVER)
18!
223
    pIter = taosHashIterate(pTrans->arbGroupIds, pIter);
18✔
224
  }
225

226
  if (sver > TRANS_VER1_NUMBER) {
139,935!
227
    SDB_SET_INT8(pRaw, dataPos, pTrans->ableToBeKilled, _OVER)
139,935!
228
    SDB_SET_INT32(pRaw, dataPos, pTrans->killMode, _OVER)
139,935!
229
  }
230

231
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
139,935!
232
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
139,935!
233

234
  terrno = 0;
139,935✔
235

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

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

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

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

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

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

319
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
265,217!
320

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

326
  pRow = sdbAllocRow(sizeof(STrans));
265,217✔
327
  if (pRow == NULL) goto _OVER;
265,217!
328

329
  pTrans = sdbGetRowObj(pRow);
265,217✔
330
  if (pTrans == NULL) goto _OVER;
265,217!
331

332
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
265,217!
333

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

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

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

370
  if (pTrans->prepareActions == NULL) goto _OVER;
265,217!
371
  if (pTrans->redoActions == NULL) goto _OVER;
265,217!
372
  if (pTrans->undoActions == NULL) goto _OVER;
265,217!
373
  if (pTrans->commitActions == NULL) goto _OVER;
265,217!
374

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

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

389
  SDB_GET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER);
265,217!
390

391
  pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
265,217✔
392

393
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
265,217!
394
  for (int32_t i = 0; i < arbgroupIdNum; ++i) {
265,247✔
395
    int32_t arbGroupId = 0;
30✔
396
    SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
30!
397
    if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
30!
398
  }
399

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

407
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
265,217!
408

409
  terrno = 0;
265,217✔
410

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

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

425
static const char *mndTransStr(ETrnStage stage) {
2,180,502✔
426
  switch (stage) {
2,180,502!
427
    case TRN_STAGE_PREPARE:
135,091✔
428
      return "prepare";
135,091✔
429
    case TRN_STAGE_REDO_ACTION:
963,448✔
430
      return "redoAction";
963,448✔
431
    case TRN_STAGE_ROLLBACK:
10✔
432
      return "rollback";
10✔
433
    case TRN_STAGE_UNDO_ACTION:
43,679✔
434
      return "undoAction";
43,679✔
435
    case TRN_STAGE_COMMIT:
220,961✔
436
      return "commit";
220,961✔
437
    case TRN_STAGE_COMMIT_ACTION:
487,362✔
438
      return "commitAction";
487,362✔
439
    case TRN_STAGE_FINISH:
329,941✔
440
      return "finished";
329,941✔
441
    case TRN_STAGE_PRE_FINISH:
10✔
442
      return "pre-finish";
10✔
443
    default:
×
444
      return "invalid";
×
445
  }
446
}
447

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

459
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
654,500✔
460
  if (pAction != NULL) {
654,500✔
461
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
540,477✔
462
      pTrans->lastAction = pAction->id;
351,556✔
463
      pTrans->lastMsgType = pAction->msgType;
351,556✔
464
      pTrans->lastEpset = pAction->epSet;
351,556✔
465
      pTrans->lastErrorNo = pAction->errCode;
351,556✔
466
    }
467
  } else {
468
    pTrans->lastAction = 0;
114,023✔
469
    pTrans->lastMsgType = 0;
114,023✔
470
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
114,023✔
471
    pTrans->lastErrorNo = 0;
114,023✔
472
  }
473
}
654,500✔
474

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

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

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

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

502
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
49,330✔
503

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

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

517
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
49,330!
518
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
×
519
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
×
520
  }
521

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

527
  return 0;
49,330✔
528
}
529

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

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

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

579
  mndTransDropData(pTrans);
157,257✔
580
  return 0;
157,257✔
581
}
582

583
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
235,184✔
584
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
762,490✔
585
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
527,306✔
586
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
527,306✔
587
    pOldAction->rawWritten = pNewAction->rawWritten;
527,306✔
588
    pOldAction->msgSent = pNewAction->msgSent;
527,306✔
589
    pOldAction->msgReceived = pNewAction->msgReceived;
527,306✔
590
    pOldAction->errCode = pNewAction->errCode;
527,306✔
591
  }
592
}
235,184✔
593

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

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

607
  mndTransUpdateActions(pOld->prepareActions, pNew->prepareActions);
58,796✔
608
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
58,796✔
609
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
58,796✔
610
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
58,796✔
611
  pOld->stage = pNew->stage;
58,796✔
612
  pOld->actionPos = pNew->actionPos;
58,796✔
613

614
  if (pOld->stage == TRN_STAGE_COMMIT) {
58,796✔
615
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
49,128✔
616
    mInfo("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
49,128!
617
  }
618

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

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

629
  return 0;
58,796✔
630
}
631

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

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

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

655
  if (opername != NULL) {
41,309!
656
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
41,309✔
657
  }
658

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

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

687
  if (pReq != NULL) {
41,309✔
688
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
55,412!
689
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
690
      return NULL;
×
691
    }
692
    pTrans->originRpcType = pReq->msgType;
27,706✔
693
  }
694

695
  mInfo("trans:%d, create transaction:%s, origin:%s", pTrans->id, pTrans->opername, opername);
41,309!
696

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

701
static void mndTransDropActions(SArray *pArray) {
1,226,104✔
702
  int32_t size = taosArrayGetSize(pArray);
1,226,104✔
703
  for (int32_t i = 0; i < size; ++i) {
3,798,409✔
704
    STransAction *pAction = taosArrayGet(pArray, i);
2,572,305✔
705
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,572,305✔
706
      taosMemoryFreeClear(pAction->pRaw);
1,564,470!
707
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,007,835!
708
      taosMemoryFreeClear(pAction->pCont);
1,007,835!
709
    } else {
710
      // nothing
711
    }
712
  }
713

714
  taosArrayDestroy(pArray);
1,226,104✔
715
}
1,226,104✔
716

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

725
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
341,203✔
726
  pAction->id = taosArrayGetSize(pArray);
341,203✔
727

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

733
  return 0;
341,203✔
734
}
735

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

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

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

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

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

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

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

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

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

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

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

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

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

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

819
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
29,283✔
820
  if (dbname != NULL) {
29,283!
821
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
29,283✔
822
  }
823
  if (stbname != NULL) {
29,283✔
824
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
22,183✔
825
  }
826
}
29,283✔
827

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

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

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

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

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

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

847
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
3,675✔
848

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

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

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

875
static bool mndCheckDbConflict(const char *conflict, STrans *pTrans) {
4,170✔
876
  if (conflict[0] == 0) return false;
4,170!
877
  if (strcasecmp(conflict, pTrans->dbname) == 0) return true;
4,170✔
878
  return false;
3,868✔
879
}
880

881
static bool mndCheckStbConflict(const char *conflict, STrans *pTrans) {
21,182✔
882
  if (conflict[0] == 0) return false;
21,182✔
883
  if (strcasecmp(conflict, pTrans->stbname) == 0) return true;
19,873✔
884
  return false;
19,846✔
885
}
886

887
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
25,352✔
888
  if (conflict) {
25,352✔
889
    mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with trans:%d db:%s stb:%s type:%d", pNew->id,
329!
890
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
891
    *globalConflict = true;
329✔
892
  } else {
893
    mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with trans:%d db:%s stb:%s type:%d", pNew->id, pNew->dbname,
25,023!
894
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
895
  }
896
}
25,352✔
897

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

903
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
119,879✔
904

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

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

912
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
23,024!
913

914
    if (pNew->conflict == TRN_CONFLICT_DB) {
23,024✔
915
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
3,599✔
916
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
3,599✔
917
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
2,467✔
918
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
2,467✔
919
      }
920
    }
921

922
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
23,024✔
923
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
19,425✔
924
      if (pTrans->conflict == TRN_CONFLICT_DB) {
19,425✔
925
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,703✔
926
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,703✔
927
      }
928
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
19,425✔
929
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
17,012✔
930
      }
931
    }
932

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

951
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
23,024!
952
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
×
953
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
954
      } else {
955
        mndTransLogConflict(pNew, pTrans, false, &conflict);
×
956
      }
957
    }
958

959
    sdbRelease(pMnode->pSdb, pTrans);
23,024✔
960
  }
961

962
  return conflict;
83,418✔
963
}
964

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

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

981
  TAOS_RETURN(code);
119,520✔
982
}
983

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

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

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

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

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

1019
  TAOS_RETURN(code);
300✔
1020
}
1021

1022
static bool mndTransActionsOfSameType(SArray *pActions) {
90,607✔
1023
  int32_t size = taosArrayGetSize(pActions);
90,607✔
1024
  ETrnAct lastActType = TRANS_ACTION_NULL;
90,607✔
1025
  bool    same = true;
90,607✔
1026
  for (int32_t i = 0; i < size; ++i) {
375,966✔
1027
    STransAction *pAction = taosArrayGet(pActions, i);
285,359✔
1028
    if (i > 0) {
285,359✔
1029
      if (lastActType != pAction->actionType) {
213,604!
1030
        same = false;
×
1031
        break;
×
1032
      }
1033
    }
1034
    lastActType = pAction->actionType;
285,359✔
1035
  }
1036
  return same;
90,607✔
1037
}
1038

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

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

1057
  TAOS_RETURN(code);
40,912✔
1058
}
1059

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

1073
  TAOS_RETURN(code);
40,912✔
1074
}
1075

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

1082
  mInfo("trans:%d, action list:", pTrans->id);
40,912!
1083
  int32_t index = 0;
40,912✔
1084
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
60,839✔
1085
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
19,927✔
1086
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
19,927!
1087
          mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1088
  }
1089

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

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

1102
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
84,517✔
1103
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
43,605✔
1104
    if(pAction->actionType == TRANS_ACTION_MSG){
43,605✔
1105
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index,
30,935!
1106
            mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));;
1107
    }
1108
    else{
1109
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index,
12,670!
1110
            mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1111
    }
1112
  }
1113

1114

1115
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
40,912!
1116

1117
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
40,912!
1118

1119
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
40,912!
1120

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

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

1139
  pNew->pRpcArray = pTrans->pRpcArray;
40,892✔
1140
  pNew->rpcRsp = pTrans->rpcRsp;
40,892✔
1141
  pNew->rpcRspLen = pTrans->rpcRspLen;
40,892✔
1142
  pNew->mTraceId = pTrans->mTraceId;
40,892✔
1143
  pTrans->pRpcArray = NULL;
40,892✔
1144
  pTrans->rpcRsp = NULL;
40,892✔
1145
  pTrans->rpcRspLen = 0;
40,892✔
1146

1147
  mndTransExecute(pMnode, pNew);
40,892✔
1148
  mndReleaseTrans(pMnode, pNew);
40,892✔
1149
  // TDOD change to TAOS_RETURN(code);
1150
  return 0;
40,892✔
1151
}
1152

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

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

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

1186
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
245,713✔
1187
  bool    sendRsp = false;
245,713✔
1188
  int32_t code = pTrans->code;
245,713✔
1189

1190
  if (pTrans->stage == TRN_STAGE_FINISH) {
245,713✔
1191
    sendRsp = true;
90,026✔
1192
  }
1193

1194
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
245,713✔
1195
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
62,006!
1196
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
10!
1197
      sendRsp = true;
10✔
1198
    }
1199
  } else {
1200
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
183,707✔
1201
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
121,187!
1202
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1203
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1204
      } else {
1205
        if (pTrans->failedTimes > 6) sendRsp = true;
121,187✔
1206
      }
1207
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
121,187✔
1208
    }
1209
  }
1210

1211
  if (!sendRsp) {
245,713✔
1212
    return;
155,675✔
1213
  } else {
1214
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
90,038!
1215
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1216
  }
1217

1218
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
90,038!
1219
  taosWLockLatch(&pTrans->lockRpcArray);
90,038✔
1220
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
90,038✔
1221
  if (size <= 0) {
90,038✔
1222
    taosWUnLockLatch(&pTrans->lockRpcArray);
62,759✔
1223
    return;
62,759✔
1224
  }
1225

1226
  for (int32_t i = 0; i < size; ++i) {
54,558✔
1227
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
27,279✔
1228
    if (pInfo->handle != NULL) {
27,279✔
1229
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
25,817!
1230
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1231
      }
1232
      if (code == TSDB_CODE_SYN_TIMEOUT) {
25,817!
1233
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1234
      }
1235

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

1242
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
25,817✔
1243

1244
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
25,817✔
1245
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
3,653!
1246
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
3,653✔
1247
        if (pDb != NULL) {
3,653!
1248
          for (int32_t j = 0; j < 12; j++) {
4,401✔
1249
            bool ready = mndIsDbReady(pMnode, pDb);
4,399✔
1250
            if (!ready) {
4,399✔
1251
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
748!
1252
              taosMsleep(1000);
748✔
1253
            } else {
1254
              break;
3,651✔
1255
            }
1256
          }
1257
        }
1258
        mndReleaseDb(pMnode, pDb);
3,653✔
1259
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
22,164✔
1260
        void   *pCont = NULL;
6,867✔
1261
        int32_t contLen = 0;
6,867✔
1262
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
6,867✔
1263
          mndTransSetRpcRsp(pTrans, pCont, contLen);
6,865✔
1264
        }
1265
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
15,297✔
1266
        void   *pCont = NULL;
481✔
1267
        int32_t contLen = 0;
481✔
1268
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
481!
1269
          mndTransSetRpcRsp(pTrans, pCont, contLen);
481✔
1270
        }
1271
      }
1272

1273
      if (pTrans->rpcRspLen != 0) {
25,817✔
1274
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
11,999✔
1275
        if (rpcCont != NULL) {
11,999!
1276
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
11,999✔
1277
          rspMsg.pCont = rpcCont;
11,999✔
1278
          rspMsg.contLen = pTrans->rpcRspLen;
11,999✔
1279
        }
1280
      }
1281

1282
      tmsgSendRsp(&rspMsg);
25,817✔
1283

1284
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
25,817!
1285
            mndTransStr(pTrans->stage), pInfo->ahandle);
1286
    }
1287
  }
1288
  taosArrayClear(pTrans->pRpcArray);
27,279✔
1289
  taosWUnLockLatch(&pTrans->lockRpcArray);
27,279✔
1290
}
1291

1292
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
92,205✔
1293
  int32_t code = 0;
92,205✔
1294
  SMnode *pMnode = pRsp->info.node;
92,205✔
1295
  int64_t signature = (int64_t)(pRsp->info.ahandle);
92,205✔
1296
  int32_t transId = (int32_t)(signature >> 32);
92,205✔
1297
  int32_t action = (int32_t)((signature << 32) >> 32);
92,205✔
1298

1299
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
92,205✔
1300
  if (pTrans == NULL) {
92,205!
1301
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1302
    if (terrno != 0) code = terrno;
×
1303
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1304
    goto _OVER;
×
1305
  }
1306

1307
  SArray *pArray = NULL;
92,205✔
1308
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
92,205✔
1309
    pArray = pTrans->redoActions;
92,197✔
1310
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
8!
1311
    pArray = pTrans->undoActions;
8✔
1312
  } else {
1313
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1314
    goto _OVER;
×
1315
  }
1316

1317
  if (pArray == NULL) {
92,205!
1318
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1319
    goto _OVER;
×
1320
  }
1321

1322
  int32_t actionNum = taosArrayGetSize(pArray);
92,205✔
1323
  if (action < 0 || action >= actionNum) {
92,205!
1324
    mError("trans:%d, invalid action:%d", transId, action);
×
1325
    goto _OVER;
×
1326
  }
1327

1328
  STransAction *pAction = taosArrayGet(pArray, action);
92,205✔
1329
  if (pAction != NULL) {
92,205!
1330
    pAction->msgReceived = 1;
92,205✔
1331
    pAction->errCode = pRsp->code;
92,205✔
1332
    pAction->endTime = taosGetTimestampMs();
92,205✔
1333

1334
    // pTrans->lastErrorNo = pRsp->code;
1335
    mndSetTransLastAction(pTrans, pAction);
92,205✔
1336

1337
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
92,205!
1338
          mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1339
          tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1340
  } else {
1341
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1342
  }
1343

1344
  mndTransExecute(pMnode, pTrans);
92,205✔
1345

1346
_OVER:
92,205✔
1347
  mndReleaseTrans(pMnode, pTrans);
92,205✔
1348
  TAOS_RETURN(code);
92,205✔
1349
}
1350

1351
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
5,294✔
1352
  pAction->rawWritten = 0;
5,294✔
1353
  pAction->msgSent = 0;
5,294✔
1354
  pAction->msgReceived = 0;
5,294✔
1355
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
5,294!
1356
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
5,294✔
1357
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
24✔
1358
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
24!
1359
          pAction->id, pAction->epSet.inUse);
1360
  } else {
1361
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
5,270!
1362
  }
1363
  pAction->errCode = 0;
5,294✔
1364
}
5,294✔
1365

1366
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
2✔
1367
  int32_t numOfActions = taosArrayGetSize(pArray);
2✔
1368

1369
  for (int32_t action = 0; action < numOfActions; ++action) {
10✔
1370
    STransAction *pAction = taosArrayGet(pArray, action);
8✔
1371
    if (pAction->msgSent && pAction->msgReceived &&
8!
1372
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
8!
1373
      continue;
6✔
1374
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
2!
1375

1376
    mndTransResetAction(pMnode, pTrans, pAction);
2✔
1377
  }
1378
}
2✔
1379

1380
// execute in sync context
1381
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
426,035✔
1382
  if (pAction->rawWritten) return 0;
426,035✔
1383
  if (topHalf) {
235,655!
1384
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
×
1385
  }
1386

1387
  if (pAction->pRaw->type >= SDB_MAX) {
235,655!
1388
    pAction->rawWritten = true;
×
1389
    pAction->errCode = 0;
×
1390
    mndSetTransLastAction(pTrans, pAction);
×
1391
    mInfo("skip sdb raw type:%d since it is not supported", pAction->pRaw->type);
×
1392
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
1393
  }
1394

1395
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
235,655✔
1396
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
235,655!
1397
    pAction->rawWritten = true;
235,655✔
1398
    pAction->errCode = 0;
235,655✔
1399
    code = 0;
235,655✔
1400
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
235,655!
1401
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1402

1403
    mndSetTransLastAction(pTrans, pAction);
235,655✔
1404
  } else {
1405
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1406
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1407
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1408
    mndSetTransLastAction(pTrans, pAction);
×
1409
  }
1410

1411
  TAOS_RETURN(code);
235,655✔
1412
}
1413

1414
// execute in trans context
1415
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
622,005✔
1416
  if (pAction->msgSent) return 0;
622,005✔
1417
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
130,106✔
1418
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
37,845✔
1419
  }
1420

1421
  int64_t signature = pTrans->id;
92,261✔
1422
  signature = (signature << 32);
92,261✔
1423
  signature += pAction->id;
92,261✔
1424

1425
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
92,261✔
1426
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
92,261✔
1427
  if (rpcMsg.pCont == NULL) {
92,261!
1428
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1429
    return -1;
×
1430
  }
1431
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
92,261✔
1432
  rpcMsg.info.notFreeAhandle = 1;
92,261✔
1433

1434
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
92,261✔
1435

1436
  char    detail[1024] = {0};
92,261✔
1437
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
92,261!
1438
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
92,261✔
1439
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
190,142✔
1440
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
97,881✔
1441
                     pAction->epSet.eps[i].port);
97,881✔
1442
  }
1443

1444
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
92,261✔
1445
  if (code == 0) {
92,261✔
1446
    pAction->msgSent = 1;
92,259✔
1447
    // pAction->msgReceived = 0;
1448
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
92,259✔
1449
    pAction->startTime = taosGetTimestampMs();
92,259✔
1450
    pAction->endTime = 0;
92,259✔
1451
    mInfo("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
92,259!
1452

1453
    mndSetTransLastAction(pTrans, pAction);
92,259✔
1454
  } else {
1455
    pAction->msgSent = 0;
2✔
1456
    pAction->msgReceived = 0;
2✔
1457
    pAction->errCode = (terrno != 0) ? terrno : code;
2!
1458
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
2!
1459
           detail);
1460

1461
    mndSetTransLastAction(pTrans, pAction);
2✔
1462
  }
1463

1464
  TAOS_RETURN(code);
92,261✔
1465
}
1466

1467
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1468
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1469
  pAction->rawWritten = 0;
×
1470
  pAction->errCode = 0;
×
1471
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1472

1473
  mndSetTransLastAction(pTrans, pAction);
×
1474
  return 0;
×
1475
}
1476

1477
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
1,048,040✔
1478
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,048,040✔
1479
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
426,035✔
1480
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
622,005!
1481
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf);
622,005✔
1482
  } else {
1483
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1484
  }
1485
}
1486

1487
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
225,482✔
1488
  int32_t numOfActions = taosArrayGetSize(pArray);
225,482✔
1489
  int32_t code = 0;
225,482✔
1490

1491
  for (int32_t action = 0; action < numOfActions; ++action) {
1,179,350✔
1492
    STransAction *pAction = taosArrayGet(pArray, action);
983,961✔
1493
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
983,961✔
1494
    if (code != 0) {
983,961✔
1495
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
30,093!
1496
            numOfActions);
1497
      break;
30,093✔
1498
    }
1499
  }
1500

1501
  return code;
225,482✔
1502
}
1503

1504
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
255,630✔
1505
  int32_t numOfActions = taosArrayGetSize(pArray);
255,630✔
1506
  int32_t code = 0;
255,630✔
1507
  if (numOfActions == 0) return 0;
255,630✔
1508

1509
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf)) != 0) {
225,482✔
1510
    return code;
30,093✔
1511
  }
1512

1513
  int32_t       numOfExecuted = 0;
195,389✔
1514
  int32_t       errCode = 0;
195,389✔
1515
  STransAction *pErrAction = NULL;
195,389✔
1516
  for (int32_t action = 0; action < numOfActions; ++action) {
1,149,257✔
1517
    STransAction *pAction = taosArrayGet(pArray, action);
953,868✔
1518
    if (pAction->msgReceived || pAction->rawWritten) {
953,868✔
1519
      numOfExecuted++;
674,538✔
1520
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
674,538✔
1521
        errCode = pAction->errCode;
4✔
1522
        pErrAction = pAction;
4✔
1523
      }
1524
    } else {
1525
      pErrAction = pAction;
279,330✔
1526
    }
1527
  }
1528

1529
  mndSetTransLastAction(pTrans, pErrAction);
195,389✔
1530

1531
  if (numOfExecuted == numOfActions) {
195,389✔
1532
    if (errCode == 0) {
114,025✔
1533
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
114,023!
1534
      return 0;
114,023✔
1535
    } else {
1536
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
2!
1537
      mndTransResetActions(pMnode, pTrans, pArray);
2✔
1538
      terrno = errCode;
2✔
1539
      return errCode;
2✔
1540
    }
1541
  } else {
1542
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
81,364!
1543

1544
    for (int32_t action = 0; action < numOfActions; ++action) {
558,119✔
1545
      STransAction *pAction = taosArrayGet(pArray, action);
476,755✔
1546
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
476,755✔
1547
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1548
             pAction->acceptableCode, pAction->retryCode);
1549
      if (pAction->msgSent) {
476,755!
1550
        if (pAction->msgReceived) {
476,755✔
1551
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
197,425✔
1552
            mndTransResetAction(pMnode, pTrans, pAction);
2✔
1553
            mInfo("trans:%d, %s:%d reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
2!
1554
          }
1555
        }
1556
      }
1557
    }
1558
    return TSDB_CODE_ACTION_IN_PROGRESS;
81,364✔
1559
  }
1560
}
1561

1562
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
165,597✔
1563
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf);
165,597✔
1564
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
165,597✔
1565
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, in %s", pTrans->id, terrstr(), terrno,
4!
1566
           mndStrExecutionContext(topHalf));
1567
  }
1568
  return code;
165,597✔
1569
}
1570

1571
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
12✔
1572
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf);
12✔
1573
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
12!
1574
    mError("trans:%d, failed to execute undoActions since %s. in %s", pTrans->id, terrstr(),
×
1575
           mndStrExecutionContext(topHalf));
1576
  }
1577
  return code;
12✔
1578
}
1579

1580
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
90,021✔
1581
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf);
90,021✔
1582
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
90,021!
1583
    mError("trans:%d, failed to execute commitActions since %s. in %s", pTrans->id, terrstr(),
×
1584
           mndStrExecutionContext(topHalf));
1585
  }
1586
  return code;
90,021✔
1587
}
1588

1589
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
30,975✔
1590
  int32_t code = 0;
30,975✔
1591
  int32_t numOfActions = taosArrayGetSize(pActions);
30,975✔
1592
  if (numOfActions == 0) return code;
30,975✔
1593

1594
  if (pTrans->actionPos >= numOfActions) {
30,971✔
1595
    return code;
3,142✔
1596
  }
1597

1598
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
27,829!
1599
        pTrans->actionPos, mndTransStr(pTrans->stage));
1600

1601
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
41,793✔
1602
    STransAction *pAction = taosArrayGet(pActions, action);
38,990✔
1603

1604
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", 
38,990!
1605
          pTrans->id, pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent,
1606
          pAction->msgReceived);
1607

1608
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
38,990✔
1609
    if (code == 0) {
38,990✔
1610
      if (pAction->msgSent) {
31,236✔
1611
        if (pAction->msgReceived) {
27,640✔
1612
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
12,341✔
1613
            code = pAction->errCode;
5,290✔
1614
            mndTransResetAction(pMnode, pTrans, pAction);
5,290✔
1615
          } else {
1616
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
7,051!
1617
          }
1618
        } else {
1619
          code = TSDB_CODE_ACTION_IN_PROGRESS;
15,299✔
1620
        }
1621
      } else if (pAction->rawWritten) {
3,596!
1622
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,596!
1623
          code = pAction->errCode;
×
1624
        } else {
1625
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,596!
1626
        }
1627
      } else {
1628
      }
1629
    }
1630

1631
    if (code == 0) {
38,990✔
1632
      pTrans->failedTimes = 0;
10,647✔
1633
    }
1634
    mndSetTransLastAction(pTrans, pAction);
38,990✔
1635

1636
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
38,990✔
1637
      pTrans->lastErrorNo = code;
9,709✔
1638
      pTrans->code = code;
9,709✔
1639
      mInfo("trans:%d, %s:%d, cannot execute next action in %s, code:%s", pTrans->id, mndTransStr(pAction->stage),
9,709!
1640
            action, mndStrExecutionContext(topHalf), tstrerror(code));
1641
      break;
9,709✔
1642
    }
1643

1644
    if (code == 0) {
29,281✔
1645
      pTrans->code = 0;
8,692✔
1646
      pTrans->actionPos++;
8,692✔
1647
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
8,692!
1648
            pAction->id);
1649
      (void)taosThreadMutexUnlock(&pTrans->mutex);
8,692✔
1650
      code = mndTransSync(pMnode, pTrans);
8,692✔
1651
      (void)taosThreadMutexLock(&pTrans->mutex);
8,692✔
1652
      if (code != 0) {
8,692!
1653
        pTrans->actionPos--;
×
1654
        pTrans->code = terrno;
×
1655
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1656
               mndTransStr(pAction->stage), pAction->id, terrstr());
1657
        break;
×
1658
      }
1659
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
20,589✔
1660
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
15,299!
1661
      break;
15,299✔
1662
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
5,290!
1663
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
42✔
1664
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
5,272!
1665
            code, tstrerror(code));
1666
      pTrans->lastErrorNo = code;
5,272✔
1667
      taosMsleep(300);
5,272✔
1668
      action--;
5,272✔
1669
      continue;
5,272✔
1670
    } else {
1671
      terrno = code;
18✔
1672
      pTrans->lastErrorNo = code;
18✔
1673
      pTrans->code = code;
18✔
1674
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
18!
1675
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1676
      break;
18✔
1677
    }
1678
  }
1679

1680
  return code;
27,829✔
1681
}
1682

1683
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
30,975✔
1684
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
30,975✔
1685
  (void)taosThreadMutexLock(&pTrans->mutex);
30,975✔
1686
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
30,975!
1687
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
30,975✔
1688
  }
1689
  (void)taosThreadMutexUnlock(&pTrans->mutex);
30,975✔
1690
  return code;
30,975✔
1691
}
1692

1693
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
1694
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1695
  (void)taosThreadMutexLock(&pTrans->mutex);
×
1696
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1697
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
1698
  }
1699
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
1700
  return code;
×
1701
}
1702

1703
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
49,163✔
1704
  bool    continueExec = true;
49,163✔
1705
  int32_t code = 0;
49,163✔
1706
  terrno = 0;
49,163✔
1707

1708
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
49,163✔
1709
  if (numOfActions == 0) goto _OVER;
49,163✔
1710

1711
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
16,066!
1712

1713
  for (int32_t action = 0; action < numOfActions; ++action) {
41,155✔
1714
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
25,089✔
1715
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
25,089✔
1716
    if (code != 0) {
25,089!
1717
      terrno = code;
×
1718
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
1719
             numOfActions, tstrerror(code));
1720
      return false;
×
1721
    }
1722
  }
1723

1724
_OVER:
16,066✔
1725
  pTrans->stage = TRN_STAGE_REDO_ACTION;
49,163✔
1726
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
49,163!
1727
  return continueExec;
49,163✔
1728
}
1729

1730
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
196,572✔
1731
  bool    continueExec = true;
196,572✔
1732
  int32_t code = 0;
196,572✔
1733
  terrno = 0;
196,572✔
1734

1735
  if (pTrans->exec == TRN_EXEC_SERIAL) {
196,572✔
1736
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
30,975✔
1737
  } else {
1738
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf);
165,597✔
1739
  }
1740

1741
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && mndTransIsInSyncContext(topHalf)) {
196,572!
1742
    pTrans->lastErrorNo = code;
×
1743
    pTrans->code = code;
×
1744
    mInfo(
×
1745
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
1746
        "continueExec:%d, code:%s",
1747
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1748
    taosMsleep(100);
×
1749
    return true;
×
1750
  } else {
1751
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
196,572✔
1752
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
58,995!
1753
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
1754
      return false;
58,995✔
1755
    }
1756
  }
1757
  terrno = code;
137,577✔
1758

1759
  if (code == 0) {
137,577✔
1760
    pTrans->code = 0;
40,900✔
1761
    pTrans->stage = TRN_STAGE_COMMIT;
40,900✔
1762
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
40,900!
1763
    continueExec = true;
40,900✔
1764
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
96,677!
1765
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
96,655!
1766
    continueExec = false;
96,655✔
1767
  } else {
1768
    pTrans->failedTimes++;
22✔
1769
    pTrans->code = terrno;
22✔
1770
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
22✔
1771
      if (pTrans->lastAction != 0) {
2!
1772
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
1773
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
1774
          if (pTrans->failedTimes < 6) {
×
1775
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
1776
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
1777
            taosMsleep(1000);
×
1778
            continueExec = true;
×
1779
            return true;
×
1780
          }
1781
        }
1782
      }
1783

1784
      pTrans->stage = TRN_STAGE_ROLLBACK;
2✔
1785
      pTrans->actionPos = 0;
2✔
1786
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
2!
1787
             pTrans->actionPos);
1788
      continueExec = true;
2✔
1789
    } else {
1790
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
20!
1791
      continueExec = false;
20✔
1792
    }
1793
  }
1794

1795
  return continueExec;
137,577✔
1796
}
1797

1798
// execute in trans context
1799
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
40,900✔
1800
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
40,900!
1801

1802
  bool    continueExec = true;
40,900✔
1803
  int32_t code = mndTransCommit(pMnode, pTrans);
40,900✔
1804

1805
  if (code == 0) {
40,900✔
1806
    pTrans->code = 0;
40,892✔
1807
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
40,892✔
1808
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
40,892!
1809
    continueExec = true;
40,892✔
1810
  } else {
1811
    pTrans->code = terrno;
8✔
1812
    pTrans->failedTimes++;
8✔
1813
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
8!
1814
    continueExec = false;
8✔
1815
  }
1816

1817
  return continueExec;
40,900✔
1818
}
1819

1820
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
90,021✔
1821
  bool    continueExec = true;
90,021✔
1822
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
90,021✔
1823

1824
  if (code == 0) {
90,021!
1825
    pTrans->code = 0;
90,021✔
1826
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
90,021✔
1827
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
90,021!
1828
    continueExec = true;
90,021✔
1829
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
1830
    pTrans->code = 0;
×
1831
    pTrans->stage = TRN_STAGE_COMMIT;
×
1832
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
1833
    continueExec = true;
×
1834
  } else {
1835
    pTrans->code = terrno;
×
1836
    pTrans->failedTimes++;
×
1837
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1838
    continueExec = false;
×
1839
  }
1840

1841
  return continueExec;
90,021✔
1842
}
1843

1844
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
12✔
1845
  bool    continueExec = true;
12✔
1846
  int32_t code = 0;
12✔
1847

1848
  if (pTrans->exec == TRN_EXEC_SERIAL) {
12!
1849
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
1850
  } else {
1851
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf);
12✔
1852
  }
1853

1854
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
12✔
1855
  terrno = code;
10✔
1856

1857
  if (code == 0) {
10✔
1858
    pTrans->stage = TRN_STAGE_PRE_FINISH;
2✔
1859
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
2!
1860
    continueExec = true;
2✔
1861
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
8!
1862
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
8!
1863
    continueExec = false;
8✔
1864
  } else {
1865
    pTrans->failedTimes++;
×
1866
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1867
    continueExec = false;
×
1868
  }
1869

1870
  return continueExec;
10✔
1871
}
1872

1873
// in trans context
1874
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
2✔
1875
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
2!
1876

1877
  bool    continueExec = true;
2✔
1878
  int32_t code = mndTransRollback(pMnode, pTrans);
2✔
1879

1880
  if (code == 0) {
2!
1881
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
2✔
1882
    continueExec = true;
2✔
1883
  } else {
1884
    pTrans->failedTimes++;
×
1885
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1886
    continueExec = false;
×
1887
  }
1888

1889
  return continueExec;
2✔
1890
}
1891

1892
// excute in trans context
1893
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
2✔
1894
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
2!
1895

1896
  bool    continueExec = true;
2✔
1897
  int32_t code = mndTransPreFinish(pMnode, pTrans);
2✔
1898

1899
  if (code == 0) {
2!
1900
    pTrans->stage = TRN_STAGE_FINISH;
2✔
1901
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
2!
1902
    continueExec = true;
2✔
1903
  } else {
1904
    pTrans->failedTimes++;
×
1905
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1906
    continueExec = false;
×
1907
  }
1908

1909
  return continueExec;
2✔
1910
}
1911

1912
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
90,025✔
1913
  bool continueExec = false;
90,025✔
1914
  if (topHalf) return continueExec;
90,025✔
1915

1916
  SSdbRaw *pRaw = mndTransEncode(pTrans);
49,131✔
1917
  if (pRaw == NULL) {
49,131!
1918
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
1919
    return false;
×
1920
  }
1921
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
49,131!
1922

1923
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
49,131✔
1924
  if (code != 0) {
49,131!
1925
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
1926
  }
1927

1928
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
49,131!
1929
        pTrans->failedTimes, pTrans->createdTime);
1930
  return continueExec;
49,131✔
1931
}
1932

1933
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf) {
245,713✔
1934
  bool continueExec = true;
245,713✔
1935

1936
  while (continueExec) {
663,247✔
1937
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64 "", pTrans->id,
417,534!
1938
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
1939
    pTrans->lastExecTime = taosGetTimestampMs();
417,534✔
1940
    switch (pTrans->stage) {
417,534!
1941
      case TRN_STAGE_PREPARE:
×
1942
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1943
        break;
×
1944
      case TRN_STAGE_REDO_ACTION:
196,572✔
1945
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf);
196,572✔
1946
        break;
196,572✔
1947
      case TRN_STAGE_COMMIT:
40,900✔
1948
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
40,900✔
1949
        break;
40,900✔
1950
      case TRN_STAGE_COMMIT_ACTION:
90,021✔
1951
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
90,021✔
1952
        break;
90,021✔
1953
      case TRN_STAGE_ROLLBACK:
2✔
1954
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
2✔
1955
        break;
2✔
1956
      case TRN_STAGE_UNDO_ACTION:
12✔
1957
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf);
12✔
1958
        break;
12✔
1959
      case TRN_STAGE_PRE_FINISH:
2✔
1960
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
2✔
1961
        break;
2✔
1962
      case TRN_STAGE_FINISH:
90,025✔
1963
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
90,025✔
1964
        break;
90,025✔
1965
      default:
×
1966
        continueExec = false;
×
1967
        break;
×
1968
    }
1969
  }
1970

1971
  mndTransSendRpcRsp(pMnode, pTrans);
245,713✔
1972
}
245,713✔
1973

1974
// start trans, pullup, receive rsp, kill
1975
void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
137,753✔
1976
  bool topHalf = true;
137,753✔
1977
  mndTransExecuteImp(pMnode, pTrans, topHalf);
137,753✔
1978
}
137,753✔
1979

1980
// update trans
1981
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
107,960✔
1982
  bool topHalf = false;
107,960✔
1983
  mndTransExecuteImp(pMnode, pTrans, topHalf);
107,960✔
1984
}
107,960✔
1985

1986
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
30,973✔
1987
  mTrace("start to process trans timer");
30,973✔
1988
  mndTransPullup(pReq->info.node);
30,973✔
1989
  return 0;
30,973✔
1990
}
1991

1992
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
1993
  SArray *pArray = NULL;
×
1994
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
1995
    pArray = pTrans->redoActions;
×
1996
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1997
    pArray = pTrans->undoActions;
×
1998
  } else {
1999
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
2000
  }
2001

2002
  if(pTrans->ableToBeKilled == false){
×
2003
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2004
  }
2005
  
2006
  if(pTrans->killMode == TRN_KILL_MODE_SKIP){
×
2007
    for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
2008
      STransAction *pAction = taosArrayGet(pArray, i);
×
2009
      mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
2010
            mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
2011
      pAction->msgSent = 1;
×
2012
      pAction->msgReceived = 1;
×
2013
      pAction->errCode = 0;
×
2014
    }
2015
  }
2016
  else if(pTrans->killMode == TRN_KILL_MODE_INTERUPT){
×
2017
    pTrans->stage = TRN_STAGE_PRE_FINISH;
×
2018
  }
2019
  else{
2020
    return TSDB_CODE_MND_TRANS_NOT_ABLE_TO_kILLED;
×
2021
  }
2022

2023
  mndTransExecute(pMnode, pTrans);
×
2024
  return 0;
×
2025
}
2026

2027
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1✔
2028
  SMnode       *pMnode = pReq->info.node;
1✔
2029
  SKillTransReq killReq = {0};
1✔
2030
  int32_t       code = -1;
1✔
2031
  STrans       *pTrans = NULL;
1✔
2032

2033
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
2034
    code = TSDB_CODE_INVALID_MSG;
×
2035
    goto _OVER;
×
2036
  }
2037

2038
  mInfo("trans:%d, start to kill", killReq.transId);
1!
2039
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1!
2040
    goto _OVER;
1✔
2041
  }
2042

2043
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
2044
  if (pTrans == NULL) {
×
2045
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
2046
    if (terrno != 0) code = terrno;
×
2047
    goto _OVER;
×
2048
  }
2049

2050
  code = mndKillTrans(pMnode, pTrans);
×
2051

2052
_OVER:
1✔
2053
  if (code != 0) {
1!
2054
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1!
2055
  }
2056

2057
  mndReleaseTrans(pMnode, pTrans);
1✔
2058
  TAOS_RETURN(code);
1✔
2059
}
2060

2061
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
231✔
2062

2063
void mndTransPullup(SMnode *pMnode) {
31,457✔
2064
  SSdb   *pSdb = pMnode->pSdb;
31,457✔
2065
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
31,457✔
2066
  if (pArray == NULL) return;
31,457!
2067

2068
  void *pIter = NULL;
31,457✔
2069
  while (1) {
4,656✔
2070
    STrans *pTrans = NULL;
36,113✔
2071
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
36,113✔
2072
    if (pIter == NULL) break;
36,113✔
2073
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
9,312!
2074
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2075
    }
2076
    sdbRelease(pSdb, pTrans);
4,656✔
2077
  }
2078

2079
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
31,457✔
2080

2081
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
36,113✔
2082
    int32_t *pTransId = taosArrayGet(pArray, i);
4,656✔
2083
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
4,656✔
2084
    if (pTrans != NULL) {
4,656!
2085
      mndTransExecute(pMnode, pTrans);
4,656✔
2086
    }
2087
    mndReleaseTrans(pMnode, pTrans);
4,656✔
2088
  }
2089
  taosArrayDestroy(pArray);
31,457✔
2090
}
2091

2092
static char *formatTimestamp(char *buf, int64_t val, int precision) {
15,018✔
2093
  time_t tt;
2094
  if (precision == TSDB_TIME_PRECISION_MICRO) {
15,018!
2095
    tt = (time_t)(val / 1000000);
×
2096
  }
2097
  if (precision == TSDB_TIME_PRECISION_NANO) {
15,018!
2098
    tt = (time_t)(val / 1000000000);
×
2099
  } else {
2100
    tt = (time_t)(val / 1000);
15,018✔
2101
  }
2102

2103
  struct tm tm;
2104
  if (taosLocalTime(&tt, &tm, NULL, 0, NULL) == NULL) {
15,018!
2105
    mError("failed to get local time");
×
2106
    return NULL;
×
2107
  }
2108
  size_t pos = taosStrfTime(buf, 32, "%Y-%m-%d %H:%M:%S", &tm);
15,018✔
2109

2110
  if (precision == TSDB_TIME_PRECISION_MICRO) {
15,018!
2111
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2112
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
15,018!
2113
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2114
  } else {
2115
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
15,018✔
2116
  }
2117

2118
  return buf;
15,018✔
2119
}
2120

2121
static void mndTransLogAction(STrans *pTrans) {
246✔
2122
  char    detail[512] = {0};
246✔
2123
  int32_t len = 0;
246✔
2124
  int32_t index = 0;
246✔
2125

2126
  if (pTrans->stage == TRN_STAGE_PREPARE) {
246!
2127
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
×
2128
      len = 0;
×
2129
      STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
×
2130
      len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2131
                      mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2132
                      sdbStatusName(pAction->pRaw->status));
×
2133
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2134
    }
2135
  }
2136

2137
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
246✔
2138
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
9,618✔
2139
      len = 0;
9,374✔
2140
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
9,374✔
2141
      if (pAction->actionType == TRANS_ACTION_MSG) {
9,374✔
2142
        char bufStart[40] = {0};
7,509✔
2143
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
7,509✔
2144

2145
        char endStart[40] = {0};
7,509✔
2146
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
7,509✔
2147
        len += snprintf(detail + len, sizeof(detail) - len,
15,018!
2148
                        "action:%d, %s:%d msgType:%s,"
2149
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2150
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
7,509✔
2151
                        pAction->msgReceived, bufStart, endStart);
7,509✔
2152

2153
        SEpSet epset = pAction->epSet;
7,509✔
2154
        if (epset.numOfEps > 0) {
7,509!
2155
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
7,509✔
2156
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
17,337✔
2157
            len +=
9,828✔
2158
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
9,828✔
2159
          }
2160
        }
2161

2162
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
7,509✔
2163
                        tstrerror(pAction->errCode));
2164
      } else {
2165
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
1,865✔
2166
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,865✔
2167
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,865✔
2168
      }
2169
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
9,374✔
2170
    }
2171
  }
2172

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

2183
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
×
2184
      len = 0;
×
2185
      STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
×
2186
      if (pAction->actionType == TRANS_ACTION_MSG) {
×
2187
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d msgType:%s\n", index,
×
2188
                        mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType));
×
2189
        ;
2190
      } else {
2191
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s\n", index,
×
2192
                        mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
×
2193
                        sdbStatusName(pAction->pRaw->status));
×
2194
      }
2195
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
×
2196
    }
2197
  }
2198
}
246✔
2199

2200
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
7,545✔
2201
  SMnode *pMnode = pReq->info.node;
7,545✔
2202
  SSdb   *pSdb = pMnode->pSdb;
7,545✔
2203
  int32_t numOfRows = 0;
7,545✔
2204
  STrans *pTrans = NULL;
7,545✔
2205
  int32_t cols = 0;
7,545✔
2206
  int32_t code = 0;
7,545✔
2207
  int32_t lino = 0;
7,545✔
2208

2209
  while (numOfRows < rows) {
7,790!
2210
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
7,790✔
2211
    if (pShow->pIter == NULL) break;
7,793✔
2212

2213
    cols = 0;
246✔
2214

2215
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2216
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
246!
2217

2218
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2219
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
246!
2220
                        _OVER);
2221

2222
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
246✔
2223
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
246✔
2224
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2225
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
246!
2226

2227
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
246✔
2228
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
246✔
2229
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2230
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
246!
2231

2232
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
246✔
2233
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
246✔
2234
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2235
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
246!
2236

2237
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
246✔
2238
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
246✔
2239
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2240
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
246!
2241

2242
    const char *killableStr = pTrans->ableToBeKilled ? "yes" : "no";
246!
2243
    char        killableVstr[10 + VARSTR_HEADER_SIZE] = {0};
246✔
2244
    STR_WITH_MAXSIZE_TO_VARSTR(killableVstr, killableStr, 10 + VARSTR_HEADER_SIZE);
246✔
2245
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2246
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killableVstr, false), pTrans, &lino, _OVER);
246!
2247

2248
    /*
2249
    const char *killModeStr = pTrans->killMode == TRN_KILL_MODE_SKIP ? "skip" : "interrupt";
2250
    char        killModeVstr[10 + VARSTR_HEADER_SIZE] = {0};
2251
    STR_WITH_MAXSIZE_TO_VARSTR(killModeVstr, killModeStr, 24);
2252
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2253
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)killModeVstr, false), pTrans, &lino, _OVER);
2254
    */
2255

2256
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2257
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
246!
2258
                        _OVER);
2259

2260
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2261
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
246!
2262
                        _OVER);
2263

2264
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
246✔
2265
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
246✔
2266
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
738✔
2267
                            pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
246✔
2268
    SEpSet  epset = pTrans->lastEpset;
246✔
2269
    if (epset.numOfEps > 0) {
246✔
2270
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
460!
2271
                       TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
460✔
2272
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
644✔
2273
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
414✔
2274
      }
2275
    }
2276
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
246✔
2277
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
246✔
2278
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
246!
2279

2280
    mndTransLogAction(pTrans);
246✔
2281

2282
    numOfRows++;
246✔
2283
    sdbRelease(pSdb, pTrans);
246✔
2284
  }
2285

2286
_OVER:
×
2287
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
7,547!
2288
  pShow->numOfRows += numOfRows;
7,546✔
2289
  return numOfRows;
7,546✔
2290
}
2291

2292
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
×
2293
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2294
  int32_t code = 0;
×
2295
  int32_t lino = 0;
×
2296
  int32_t len = 0;
×
2297

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

2301
  char action[30 + 1] = {0};
×
2302
  if (curActionId == pAction->id) {
×
2303
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)<-last", mndTransStr(pAction->stage), pAction->id,
×
2304
                    mndTransTypeStr(pAction->actionType));
2305
  } else {
2306
    len += snprintf(action + len, sizeof(action) - len, "%s:%d(%s)", mndTransStr(pAction->stage), pAction->id,
×
2307
                    mndTransTypeStr(pAction->actionType));
2308
  }
2309
  char actionVStr[30 + VARSTR_HEADER_SIZE] = {0};
×
2310
  STR_WITH_MAXSIZE_TO_VARSTR(actionVStr, action, pShow->pMeta->pSchemas[*cols].bytes);
×
2311
  pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
×
2312
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)actionVStr, false), &lino, _OVER);
×
2313
_OVER:
×
2314
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2315
  return code;
×
2316
}
2317

2318
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
×
2319
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2320
  int32_t code = 0;
×
2321
  int32_t lino = 0;
×
2322
  int32_t len = 0;
×
2323
  int32_t cols = 0;
×
2324

2325
  cols = 0;
×
2326

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

2329
  if (pAction->actionType == TRANS_ACTION_MSG) {
×
2330
    int32_t len = 0;
×
2331

2332
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2333
    len += snprintf(objType + len, sizeof(objType) - len, "%s(s:%d,r:%d)", TMSG_INFO(pAction->msgType),
×
2334
                    pAction->msgSent, pAction->msgReceived);
×
2335
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2336
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2337
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2338
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2339

2340
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2341
    len = 0;
×
2342
    len += snprintf(result + len, sizeof(result) - len, "errCode:0x%x(%s)", pAction->errCode & 0xFFFF,
×
2343
                    tstrerror(pAction->errCode));
2344
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2345
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2346
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2347
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2348

2349
    char target[TSDB_TRANS_TARGET_LEN] = {0};
×
2350
    len = 0;
×
2351
    SEpSet epset = pAction->epSet;
×
2352
    if (epset.numOfEps > 0) {
×
2353
      for (int32_t i = 0; i < epset.numOfEps; ++i) {
×
2354
        len += snprintf(target + len, sizeof(target) - len, "ep:%d-%s:%u,", i, epset.eps[i].fqdn, epset.eps[i].port);
×
2355
      }
2356
      len += snprintf(target + len, sizeof(target) - len, "(%d:%d) ", epset.numOfEps, epset.inUse);
×
2357
    }
2358
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2359
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2360
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2361
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2362

2363
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2364
    len = 0;
×
2365
    char bufStart[40] = {0};
×
2366
    if (pAction->startTime > 0) (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
×
2367
    char bufEnd[40] = {0};
×
2368
    if (pAction->endTime > 0) (void)formatTimestamp(bufEnd, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
×
2369
    len += snprintf(detail + len, sizeof(detail) - len, "startTime:%s, endTime:%s, ", bufStart, bufEnd);
×
2370
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2371
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2372
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2373
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2374

2375
  } else {
2376
    int32_t len = 0;
×
2377

2378
    char objType[TSDB_TRANS_OBJTYPE_LEN + 1] = {0};
×
2379
    if (pAction->pRaw->type == SDB_VGROUP) {
×
2380
      SSdbRow *pRow = mndVgroupActionDecode(pAction->pRaw);
×
2381
      SVgObj  *pVgroup = sdbGetRowObj(pRow);
×
2382
      len += snprintf(objType + len, sizeof(objType) - len, "%s(%d)", sdbTableName(pAction->pRaw->type), pVgroup->vgId);
×
2383
      taosMemoryFreeClear(pRow);
×
2384
    } else {
2385
      strcpy(objType, sdbTableName(pAction->pRaw->type));
×
2386
    }
2387
    char objTypeVStr[TSDB_TRANS_OBJTYPE_LEN + VARSTR_HEADER_SIZE] = {0};
×
2388
    STR_WITH_MAXSIZE_TO_VARSTR(objTypeVStr, objType, pShow->pMeta->pSchemas[cols].bytes);
×
2389
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2390
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)objTypeVStr, false), &lino, _OVER);
×
2391

2392
    char result[TSDB_TRANS_RESULT_LEN + 1] = {0};
×
2393
    len = 0;
×
2394
    len += snprintf(result + len, sizeof(result) - len, "rawWritten:%d", pAction->rawWritten);
×
2395
    char resultVStr[TSDB_TRANS_RESULT_LEN + VARSTR_HEADER_SIZE] = {0};
×
2396
    STR_WITH_MAXSIZE_TO_VARSTR(resultVStr, result, pShow->pMeta->pSchemas[cols].bytes);
×
2397
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2398
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)resultVStr, false), &lino, _OVER);
×
2399

2400
    char target[TSDB_TRANS_TARGET_LEN] = "";
×
2401
    char targetVStr[TSDB_TRANS_TARGET_LEN + VARSTR_HEADER_SIZE] = {0};
×
2402
    STR_WITH_MAXSIZE_TO_VARSTR(targetVStr, target, pShow->pMeta->pSchemas[cols].bytes);
×
2403
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2404
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)targetVStr, false), &lino, _OVER);
×
2405

2406
    char detail[TSDB_TRANS_DETAIL_LEN] = {0};
×
2407
    len = 0;
×
2408
    len += snprintf(detail + len, sizeof(detail) - len, "sdbStatus:%s", sdbStatusName(pAction->pRaw->status));
×
2409
    char detailVStr[TSDB_TRANS_DETAIL_LEN + VARSTR_HEADER_SIZE] = {0};
×
2410
    STR_WITH_MAXSIZE_TO_VARSTR(detailVStr, detail, pShow->pMeta->pSchemas[cols].bytes);
×
2411
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
2412
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)detailVStr, false), &lino, _OVER);
×
2413
  }
2414

2415
_OVER:
×
2416
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2417
}
2418

2419
static SArray *mndTransGetAction(STrans *pTrans, ETrnStage stage) {
×
2420
  if (stage == TRN_STAGE_PREPARE) {
×
2421
    return pTrans->prepareActions;
×
2422
  }
2423
  if (stage == TRN_STAGE_REDO_ACTION) {
×
2424
    return pTrans->redoActions;
×
2425
  }
2426
  if (stage == TRN_STAGE_COMMIT_ACTION) {
×
2427
    return pTrans->commitActions;
×
2428
  }
2429
  if (stage == TRN_STAGE_UNDO_ACTION) {
×
2430
    return pTrans->undoActions;
×
2431
  }
2432
  return NULL;
×
2433
}
2434

2435
typedef struct STransDetailIter {
2436
  void     *pIter;
2437
  STrans   *pTrans;
2438
  ETrnStage stage;
2439
  int32_t   num;
2440
} STransDetailIter;
2441

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

2447
  for (int32_t i = start; i < actionNum; ++i) {
×
2448
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
×
2449
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
×
2450
    (*numOfRows)++;
×
2451
    if (*numOfRows >= rows) break;
×
2452
  }
2453

2454
  if (*numOfRows == end) {
×
2455
    sdbRelease(pSdb, pShowIter->pTrans);
×
2456
    pShowIter->pTrans = NULL;
×
2457
    pShowIter->num = 0;
×
2458
  } else {
2459
    pShowIter->pTrans = pShowIter->pTrans;
×
2460
    pShowIter->stage = pShowIter->pTrans->stage;
×
2461
    pShowIter->num += (*numOfRows);
×
2462
  }
2463
}
×
2464

2465
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
2466
  SMnode *pMnode = pReq->info.node;
×
2467
  SSdb   *pSdb = pMnode->pSdb;
×
2468
  int32_t numOfRows = 0;
×
2469

2470
  int32_t code = 0;
×
2471
  int32_t lino = 0;
×
2472

2473
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
×
2474
        pShow->pIter);
2475

2476
  if (pShow->pIter == NULL) {
×
2477
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
×
2478
    if (pShow->pIter == NULL) {
×
2479
      mError("failed to malloc for pShow->pIter");
×
2480
      return 0;
×
2481
    }
2482
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
×
2483
  }
2484

2485
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
×
2486

2487
  while (numOfRows < rows) {
×
2488
    if (pShowIter->pTrans == NULL) {
×
2489
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
×
2490
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
×
2491
      if (pShowIter->pIter == NULL) break;
×
2492
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
×
2493
            pShowIter->pTrans->stage, pShowIter->num);
2494

2495
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
×
2496

2497
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions, taosArrayGetSize(pActions), 0);
×
2498
      break;
×
2499
    } else {
2500
      mInfo("retrieve trans detail from iter, id:%d, iterStage:%d, IterNum:%d", pShowIter->pTrans->id, pShowIter->stage,
×
2501
            pShowIter->num);
2502
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->stage);
×
2503

2504
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
×
2505
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
×
2506
      break;
×
2507
    }
2508
  }
2509

2510
_OVER:
×
2511
  pShow->numOfRows += numOfRows;
×
2512

2513
  if (code != 0) {
×
2514
    mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
×
2515
  } else {
2516
    mInfo("retrieve trans detail, numOfRows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows)
×
2517
  }
2518
  if (numOfRows == 0) {
×
2519
    taosMemoryFree(pShow->pIter);
×
2520
    pShow->pIter = NULL;
×
2521
  }
2522
  return numOfRows;
×
2523
}
2524

2525
static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
×
2526
  SSdb *pSdb = pMnode->pSdb;
×
2527
  sdbCancelFetchByType(pSdb, pIter, SDB_TRANS);
×
2528
}
×
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