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

taosdata / TDengine / #4506

15 Jul 2025 12:33AM UTC coverage: 62.026% (-0.7%) from 62.706%
#4506

push

travis-ci

web-flow
docs: update stream docs (#31874)

155391 of 320094 branches covered (48.55%)

Branch coverage included in aggregate %.

240721 of 318525 relevant lines covered (75.57%)

6529048.03 hits per line

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

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

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

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

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

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

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

59
static inline bool mndTransIsInSyncContext(bool topHalf) { return !topHalf; }
391,329✔
60

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

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

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

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

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

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

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

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

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

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

115
  for (int32_t i = 0; i < actionNum; ++i) {
2,078,810✔
116
    STransAction *pAction = taosArrayGet(pArray, i);
1,407,106✔
117
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,407,106✔
118
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
948,203✔
119
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
458,903!
120
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
458,903✔
121
    } else {
122
      // empty
123
    }
124
    rawDataLen += sizeof(int8_t);
1,407,106✔
125
  }
126

127
  return rawDataLen;
671,704✔
128
}
129

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

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

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

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

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

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

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

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

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

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

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

229
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
167,926!
230

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

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

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

263
  terrno = 0;
167,926✔
264

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

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

277
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
91,483✔
278
  if (pAction->groupId < 0) return 0;
91,483✔
279
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
89,059✔
280
  if (redoAction == NULL) {
89,059✔
281
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
16,070✔
282
    if (array != NULL) {
16,070!
283
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
16,070!
284
        mInfo("failed put action into redo group actions");
×
285
        return TSDB_CODE_INTERNAL_ERROR;
×
286
      }
287
    }
288
    redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
16,070✔
289
  }
290
  if (redoAction != NULL) {
89,059!
291
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
178,118!
292
  }
293
  return 0;
89,059✔
294
}
295

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

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

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

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

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

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

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

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

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

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

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

450
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
320,638!
451

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

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

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

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

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

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

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

519
  SDB_GET_INT32(pRaw, dataPos, &arbgroupIdNum, _OVER)
320,638!
520
  if (arbgroupIdNum > 0) {
320,638✔
521
    pTrans->arbGroupIds = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
70✔
522
    if (pTrans->arbGroupIds == NULL) goto _OVER;
70!
523
    for (int32_t i = 0; i < arbgroupIdNum; ++i) {
160✔
524
      int32_t arbGroupId = 0;
90✔
525
      SDB_GET_INT32(pRaw, dataPos, &arbGroupId, _OVER)
90!
526
      if ((terrno = taosHashPut(pTrans->arbGroupIds, &arbGroupId, sizeof(int32_t), NULL, 0)) != 0) goto _OVER;
90!
527
    }
528
  }
529

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

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

552
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
320,638!
553

554
  terrno = 0;
320,638✔
555

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

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

571
static const char *mndTransStr(ETrnStage stage) {
2,484,027✔
572
  switch (stage) {
2,484,027!
573
    case TRN_STAGE_PREPARE:
164,581✔
574
      return "prepare";
164,581✔
575
    case TRN_STAGE_REDO_ACTION:
963,846✔
576
      return "redoAction";
963,846✔
577
    case TRN_STAGE_ROLLBACK:
37✔
578
      return "rollback";
37✔
579
    case TRN_STAGE_UNDO_ACTION:
47,740✔
580
      return "undoAction";
47,740✔
581
    case TRN_STAGE_COMMIT:
271,566✔
582
      return "commit";
271,566✔
583
    case TRN_STAGE_COMMIT_ACTION:
637,679✔
584
      return "commitAction";
637,679✔
585
    case TRN_STAGE_FINISH:
398,541✔
586
      return "finished";
398,541✔
587
    case TRN_STAGE_PRE_FINISH:
37✔
588
      return "pre-finish";
37✔
589
    default:
×
590
      return "invalid";
×
591
  }
592
}
593

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

605
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
712,147✔
606
  if (pAction != NULL) {
712,147✔
607
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
577,466✔
608
      pTrans->lastAction = pAction->id;
408,980✔
609
      pTrans->lastMsgType = pAction->msgType;
408,980✔
610
      pTrans->lastEpset = pAction->epSet;
408,980✔
611
      pTrans->lastErrorNo = pAction->errCode;
408,980✔
612
    }
613
  } else {
614
    pTrans->lastAction = 0;
134,681✔
615
    pTrans->lastMsgType = 0;
134,681✔
616
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
134,681✔
617
    pTrans->lastErrorNo = 0;
134,681✔
618
  }
619
}
712,147✔
620

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

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

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

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

648
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
61,116✔
649

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

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

663
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
61,116!
664
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
×
665
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pTrans->id);
×
666
  }
667

668
  if (pTrans->stage == TRN_STAGE_PRE_FINISH) {
61,116!
669
    pTrans->stage = TRN_STAGE_FINISH;
×
670
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pTrans->id);
×
671
  }
672

673
  return 0;
61,116✔
674
}
675

676
void mndTransDropData(STrans *pTrans) {
370,999✔
677
  if (pTrans->prepareActions != NULL) {
370,999!
678
    mndTransDropActions(pTrans->prepareActions);
370,999✔
679
    pTrans->prepareActions = NULL;
370,999✔
680
  }
681
  if (pTrans->redoActions != NULL) {
370,999!
682
    mndTransDropActions(pTrans->redoActions);
370,999✔
683
    pTrans->redoActions = NULL;
370,999✔
684
  }
685
  if (pTrans->undoActions != NULL) {
370,999!
686
    mndTransDropActions(pTrans->undoActions);
370,999✔
687
    pTrans->undoActions = NULL;
370,999✔
688
  }
689
  if (pTrans->commitActions != NULL) {
370,999!
690
    mndTransDropActions(pTrans->commitActions);
370,999✔
691
    pTrans->commitActions = NULL;
370,999✔
692
  }
693
  if (pTrans->redoGroupActions != NULL) {
370,999✔
694
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
54,769✔
695
    while (pIter) {
71,425✔
696
      SArray **redoActions = pIter;
16,656✔
697
      taosArrayDestroy(*redoActions);
16,656✔
698
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
16,656✔
699
    }
700

701
    taosHashCleanup(pTrans->redoGroupActions);
54,769✔
702
    pTrans->redoGroupActions = NULL;
54,769✔
703
  }
704
  if (pTrans->groupActionPos != NULL) {
370,999✔
705
    taosHashCleanup(pTrans->groupActionPos);
54,769✔
706
    pTrans->groupActionPos = NULL;
54,769✔
707
  }
708
  if (pTrans->arbGroupIds != NULL) {
370,999✔
709
    taosHashCleanup(pTrans->arbGroupIds);
50,431✔
710
  }
711
  if (pTrans->pRpcArray != NULL) {
370,999✔
712
    taosArrayDestroy(pTrans->pRpcArray);
50,361✔
713
    pTrans->pRpcArray = NULL;
50,361✔
714
  }
715
  if (pTrans->rpcRsp != NULL) {
370,999✔
716
    taosMemoryFree(pTrans->rpcRsp);
14,767!
717
    pTrans->rpcRsp = NULL;
14,767✔
718
    pTrans->rpcRspLen = 0;
14,767✔
719
  }
720
  if (pTrans->param != NULL) {
370,999!
721
    taosMemoryFree(pTrans->param);
×
722
    pTrans->param = NULL;
×
723
    pTrans->paramLen = 0;
×
724
  }
725
  (void)taosThreadMutexDestroy(&pTrans->mutex);
370,999✔
726
}
370,999✔
727

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

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

740
  mndTransDropData(pTrans);
190,856✔
741
  return 0;
190,856✔
742
}
743

744
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
275,376✔
745
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
896,697✔
746
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
621,321✔
747
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
621,321✔
748
    pOldAction->rawWritten = pNewAction->rawWritten;
621,321✔
749
    pOldAction->msgSent = pNewAction->msgSent;
621,321✔
750
    pOldAction->msgReceived = pNewAction->msgReceived;
621,321✔
751
    pOldAction->errCode = pNewAction->errCode;
621,321✔
752
  }
753
}
275,376✔
754

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

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

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

775
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
68,844✔
776
  while (pIter != NULL) {
75,948✔
777
    int32_t newActionPos = *(int32_t *)pIter;
7,104✔
778

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

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

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

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

796
  if (pOld->stage == TRN_STAGE_ROLLBACK) {
68,844✔
797
    pOld->stage = TRN_STAGE_UNDO_ACTION;
11✔
798
    mInfo("trans:%d, stage from rollback to undoAction since perform update action", pNew->id);
11!
799
  }
800

801
  if (pOld->stage == TRN_STAGE_PRE_FINISH) {
68,844✔
802
    pOld->stage = TRN_STAGE_FINISH;
11✔
803
    mInfo("trans:%d, stage from pre-finish to finished since perform update action", pNew->id);
11!
804
  }
805

806
  return 0;
68,844✔
807
}
808

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

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

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

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

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

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

866
  if (pReq != NULL) {
50,361✔
867
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
58,966!
868
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
869
      return NULL;
×
870
    }
871
    pTrans->originRpcType = pReq->msgType;
29,483✔
872
  }
873

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

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

880
static void mndTransDropActions(SArray *pArray) {
1,483,996✔
881
  int32_t size = taosArrayGetSize(pArray);
1,483,996✔
882
  for (int32_t i = 0; i < size; ++i) {
4,503,830✔
883
    STransAction *pAction = taosArrayGet(pArray, i);
3,019,834✔
884
    if (pAction->actionType == TRANS_ACTION_RAW) {
3,019,834✔
885
      taosMemoryFreeClear(pAction->pRaw);
2,001,122!
886
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,018,712!
887
      taosMemoryFreeClear(pAction->pCont);
1,018,712!
888
    } else {
889
      // nothing
890
    }
891
  }
892

893
  taosArrayDestroy(pArray);
1,483,996✔
894
}
1,483,996✔
895

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

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

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

912
  return 0;
398,255✔
913
}
914

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

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

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

947
  return 0;
1,420✔
948
}
949

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1057
        sdbRelease(pMnode->pSdb, pTrans);
×
1058
        break;
×
1059
      }
1060
    }
1061

1062
    sdbRelease(pMnode->pSdb, pTrans);
×
1063
  }
1064
  return code;
×
1065
}
1066

1067
void mndTransSetDbName(STrans *pTrans, const char *dbname, const char *stbname) {
26,897✔
1068
  if (dbname != NULL) {
26,897!
1069
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
26,897✔
1070
  }
1071
  if (stbname != NULL) {
26,897✔
1072
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
18,792✔
1073
  }
1074
}
26,897✔
1075

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

1082
void mndTransSetSerial(STrans *pTrans) {
1,210✔
1083
  mInfo("trans:%d, set Serial", pTrans->id);
1,210!
1084
  pTrans->exec = TRN_EXEC_SERIAL;
1,210✔
1085
}
1,210✔
1086

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

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

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

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

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

1106
void mndTransSetOper(STrans *pTrans, EOperType oper) { pTrans->oper = oper; }
4,643✔
1107

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

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

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

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

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

1146
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
19,105✔
1147
  if (conflict) {
19,105✔
1148
    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,
331!
1149
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
1150
    *globalConflict = true;
331✔
1151
  } else {
1152
    mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with trans:%d db:%s stb:%s type:%d", pNew->id, pNew->dbname,
18,774!
1153
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
1154
  }
1155
}
19,105✔
1156

1157
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
138,478✔
1158
  STrans *pTrans = NULL;
138,478✔
1159
  void   *pIter = NULL;
138,478✔
1160
  bool    conflict = false;
138,478✔
1161

1162
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
138,478✔
1163

1164
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
92,611✔
1165
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
92,611!
1166

1167
  while (1) {
1168
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
111,307✔
1169
    if (pIter == NULL) break;
111,307✔
1170

1171
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
18,696✔
1172

1173
    if (pNew->conflict == TRN_CONFLICT_DB) {
18,696✔
1174
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
1,381✔
1175
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
1,381!
1176
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
923✔
1177
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
923✔
1178
      }
1179
    }
1180

1181
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
18,696✔
1182
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
17,290✔
1183
      if (pTrans->conflict == TRN_CONFLICT_DB) {
17,290✔
1184
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
262✔
1185
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
262✔
1186
      }
1187
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
17,290✔
1188
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
16,729✔
1189
      }
1190
    }
1191

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

1210
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
18,696✔
1211
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
6!
1212
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1213
      } else {
1214
        mndTransLogConflict(pNew, pTrans, false, &conflict);
6✔
1215
      }
1216
    }
1217

1218
    sdbRelease(pMnode->pSdb, pTrans);
18,696✔
1219
  }
1220

1221
  return conflict;
92,611✔
1222
}
1223

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

1234
  if (mndCheckTransConflict(pMnode, pTrans)) {
138,478✔
1235
    code = TSDB_CODE_MND_TRANS_CONFLICT;
368✔
1236
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
368!
1237
    TAOS_RETURN(code);
368✔
1238
  }
1239

1240
  TAOS_RETURN(code);
138,110✔
1241
}
1242

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

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

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

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

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

1278
  TAOS_RETURN(code);
288✔
1279
}
1280

1281
static bool mndTransActionsOfSameType(SArray *pActions) {
117,209✔
1282
  int32_t size = taosArrayGetSize(pActions);
117,209✔
1283
  ETrnAct lastActType = TRANS_ACTION_NULL;
117,209✔
1284
  bool    same = true;
117,209✔
1285
  for (int32_t i = 0; i < size; ++i) {
455,378✔
1286
    STransAction *pAction = taosArrayGet(pActions, i);
338,169✔
1287
    if (i > 0) {
338,169✔
1288
      if (lastActType != pAction->actionType) {
257,189!
1289
        same = false;
×
1290
        break;
×
1291
      }
1292
    }
1293
    lastActType = pAction->actionType;
338,169✔
1294
  }
1295
  return same;
117,209✔
1296
}
1297

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

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

1316
  TAOS_RETURN(code);
49,938✔
1317
}
1318

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

1332
  TAOS_RETURN(code);
49,938✔
1333
}
1334

1335
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
49,938✔
1336
  int32_t code = 0;
49,938✔
1337
  if (pTrans == NULL) {
49,938!
1338
    return TSDB_CODE_INVALID_PARA;
×
1339
  }
1340

1341
  mInfo("trans:%d, action list:", pTrans->id);
49,938!
1342
  int32_t index = 0;
49,938✔
1343
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
73,488✔
1344
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
23,550✔
1345
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
23,550!
1346
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1347
  }
1348

1349
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
125,339✔
1350
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
75,401✔
1351
    if (pAction->actionType == TRANS_ACTION_MSG) {
75,401✔
1352
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
73,657!
1353
            TMSG_INFO(pAction->msgType));
1354
      ;
1355
    } else {
1356
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
1,744!
1357
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1358
    }
1359
  }
1360

1361
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
301,308✔
1362
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
251,370✔
1363
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
251,370!
1364
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1365
  }
1366

1367
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
97,389✔
1368
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
47,451✔
1369
    if (pAction->actionType == TRANS_ACTION_MSG) {
47,451✔
1370
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
32,126!
1371
            TMSG_INFO(pAction->msgType));
1372
      ;
1373
    } else {
1374
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
15,325!
1375
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1376
    }
1377
  }
1378

1379
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
49,938!
1380

1381
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
49,938!
1382

1383
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
49,938!
1384

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

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

1403
  pNew->pRpcArray = pTrans->pRpcArray;
49,930✔
1404
  pNew->rpcRsp = pTrans->rpcRsp;
49,930✔
1405
  pNew->rpcRspLen = pTrans->rpcRspLen;
49,930✔
1406
  pNew->mTraceId = pTrans->mTraceId;
49,930✔
1407
  pTrans->pRpcArray = NULL;
49,930✔
1408
  pTrans->rpcRsp = NULL;
49,930✔
1409
  pTrans->rpcRspLen = 0;
49,930✔
1410

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

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

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

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

1451
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
264,793✔
1452
  bool    sendRsp = false;
264,793✔
1453
  int32_t code = pTrans->code;
264,793✔
1454

1455
  if (pTrans->stage == TRN_STAGE_FINISH) {
264,793✔
1456
    sendRsp = true;
110,827✔
1457
  }
1458

1459
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
264,793✔
1460
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
86,990!
1461
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
47✔
1462
      sendRsp = true;
47✔
1463
    }
1464
  } else {
1465
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
177,803✔
1466
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
109,831!
1467
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1468
        if (pTrans->failedTimes > 60) sendRsp = true;
5!
1469
      } else {
1470
        if (pTrans->failedTimes > 6) sendRsp = true;
109,826✔
1471
      }
1472
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
109,831✔
1473
    }
1474
  }
1475

1476
  if (!sendRsp) {
264,793✔
1477
    return;
153,915✔
1478
  } else {
1479
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
110,878!
1480
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1481
  }
1482

1483
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
110,878!
1484
  taosWLockLatch(&pTrans->lockRpcArray);
110,878✔
1485
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
110,878✔
1486
  if (size <= 0) {
110,878✔
1487
    taosWUnLockLatch(&pTrans->lockRpcArray);
81,851✔
1488
    return;
81,851✔
1489
  }
1490

1491
  for (int32_t i = 0; i < size; ++i) {
58,054✔
1492
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
29,027✔
1493
    if (pInfo->handle != NULL) {
29,027✔
1494
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
27,554!
1495
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1496
      }
1497
      if (code == TSDB_CODE_SYN_TIMEOUT) {
27,554✔
1498
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
1✔
1499
      }
1500

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

1507
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
27,554✔
1508

1509
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
27,554✔
1510
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,619!
1511
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,619✔
1512
        if (pDb != NULL) {
4,619!
1513
          for (int32_t j = 0; j < 12; j++) {
5,708✔
1514
            bool ready = mndIsDbReady(pMnode, pDb);
5,700✔
1515
            if (!ready) {
5,700✔
1516
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
1,089!
1517
              taosMsleep(1000);
1,089✔
1518
            } else {
1519
              break;
4,611✔
1520
            }
1521
          }
1522
        }
1523
        mndReleaseDb(pMnode, pDb);
4,619✔
1524
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
22,935✔
1525
        void   *pCont = NULL;
7,214✔
1526
        int32_t contLen = 0;
7,214✔
1527
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
7,214✔
1528
          mndTransSetRpcRsp(pTrans, pCont, contLen);
7,210✔
1529
        }
1530
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
15,721✔
1531
        void   *pCont = NULL;
14✔
1532
        int32_t contLen = 0;
14✔
1533
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
14!
1534
          mndTransSetRpcRsp(pTrans, pCont, contLen);
14✔
1535
        }
1536
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
15,707✔
1537
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
42✔
1538
        if (code != 0) {
42!
1539
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1540
        }
1541
      }
1542

1543
      if (pTrans->rpcRspLen != 0) {
27,554✔
1544
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
14,767✔
1545
        if (rpcCont != NULL) {
14,767!
1546
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
14,767✔
1547
          rspMsg.pCont = rpcCont;
14,767✔
1548
          rspMsg.contLen = pTrans->rpcRspLen;
14,767✔
1549
        }
1550
      }
1551

1552
      tmsgSendRsp(&rspMsg);
27,554✔
1553

1554
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
27,554!
1555
            mndTransStr(pTrans->stage), pInfo->ahandle);
1556
    }
1557
  }
1558
  taosArrayClear(pTrans->pRpcArray);
29,027✔
1559
  taosWUnLockLatch(&pTrans->lockRpcArray);
29,027✔
1560
}
1561

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

1581
  SArray *pArray = NULL;
79,434✔
1582
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
79,434✔
1583
    pArray = pTrans->redoActions;
79,412✔
1584
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
22!
1585
    pArray = pTrans->undoActions;
22✔
1586
  } else {
1587
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1588
    goto _OVER;
×
1589
  }
1590

1591
  if (pArray == NULL) {
79,434!
1592
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1593
    goto _OVER;
×
1594
  }
1595

1596
  int32_t actionNum = taosArrayGetSize(pArray);
79,434✔
1597
  if (action < 0 || action >= actionNum) {
79,434!
1598
    mError("trans:%d, invalid action:%d", transId, action);
×
1599
    goto _OVER;
×
1600
  }
1601

1602
  STransAction *pAction = taosArrayGet(pArray, action);
79,434✔
1603
  if (pAction != NULL) {
79,434!
1604
    if (pAction->msgSent) {
79,434!
1605
      pAction->msgReceived = 1;
79,434✔
1606
      pAction->errCode = pRsp->code;
79,434✔
1607
      pAction->endTime = taosGetTimestampMs();
79,434✔
1608

1609
      // pTrans->lastErrorNo = pRsp->code;
1610
      mndSetTransLastAction(pTrans, pAction);
79,434✔
1611

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

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

1625
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
79,434!
1626
  mndTransExecute(pMnode, pTrans, true);
79,434✔
1627

1628
_OVER:
79,434✔
1629
  mndReleaseTrans(pMnode, pTrans);
79,434✔
1630
  TAOS_RETURN(code);
79,434✔
1631
}
1632

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

1648
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
10✔
1649
  int32_t numOfActions = taosArrayGetSize(pArray);
10✔
1650

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

1661
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
10!
1662

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

1669
// execute in sync context
1670
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
561,056✔
1671
  if (pAction->rawWritten) return 0;
561,056✔
1672
  if (topHalf) {
307,994✔
1673
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
17✔
1674
  }
1675

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

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

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

1700
  TAOS_RETURN(code);
307,977✔
1701
}
1702

1703
// execute in trans context
1704
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
474,000✔
1705
                                     bool notSend) {
1706
  if (pAction->msgSent) return 0;
474,000✔
1707
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
120,488✔
1708
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
40,628✔
1709
  }
1710

1711
  if (notSend) {
79,860✔
1712
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
337!
1713
    return 0;
337✔
1714
  }
1715

1716
#ifndef TD_ASTRA_32
1717
  int64_t signature = pTrans->id;
79,523✔
1718
  signature = (signature << 32);
79,523✔
1719
  signature += pAction->id;
79,523✔
1720

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

1736
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
79,523✔
1737

1738
  char    detail[1024] = {0};
79,523✔
1739
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
79,523!
1740
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
79,523✔
1741
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
166,713✔
1742
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
87,190✔
1743
                     pAction->epSet.eps[i].port);
87,190✔
1744
  }
1745

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

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

1763
    mndSetTransLastAction(pTrans, pAction);
3✔
1764
  }
1765

1766
  TAOS_RETURN(code);
79,523✔
1767
}
1768

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

1775
  mndSetTransLastAction(pTrans, pAction);
×
1776
  return 0;
×
1777
}
1778

1779
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
1,035,056✔
1780
                                        bool notSend) {
1781
  if (pAction->actionType == TRANS_ACTION_RAW) {
1,035,056✔
1782
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
561,056✔
1783
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
474,000!
1784
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
474,000✔
1785
  } else {
1786
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1787
  }
1788
}
1789

1790
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
236,714✔
1791
  int32_t numOfActions = taosArrayGetSize(pArray);
236,714✔
1792
  int32_t code = 0;
236,714✔
1793

1794
  for (int32_t action = 0; action < numOfActions; ++action) {
1,170,021✔
1795
    STransAction *pAction = taosArrayGet(pArray, action);
964,853✔
1796
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
964,853✔
1797
    if (code != 0) {
964,853✔
1798
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
31,546!
1799
            numOfActions);
1800
      break;
31,546✔
1801
    }
1802
  }
1803

1804
  return code;
236,714✔
1805
}
1806

1807
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
289,190✔
1808
  int32_t numOfActions = taosArrayGetSize(pArray);
289,190✔
1809
  int32_t code = 0;
289,190✔
1810
  if (numOfActions == 0) return 0;
289,190✔
1811

1812
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
236,714✔
1813
    return code;
31,546✔
1814
  }
1815

1816
  int32_t       numOfExecuted = 0;
205,168✔
1817
  int32_t       errCode = 0;
205,168✔
1818
  STransAction *pErrAction = NULL;
205,168✔
1819
  for (int32_t action = 0; action < numOfActions; ++action) {
1,138,475✔
1820
    STransAction *pAction = taosArrayGet(pArray, action);
933,307✔
1821
    if (pAction->msgReceived || pAction->rawWritten) {
933,307✔
1822
      numOfExecuted++;
730,658✔
1823
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
730,658✔
1824
        errCode = pAction->errCode;
53✔
1825
        pErrAction = pAction;
53✔
1826
      }
1827
    } else {
1828
      pErrAction = pAction;
202,649✔
1829
    }
1830
  }
1831

1832
  mndSetTransLastAction(pTrans, pErrAction);
205,168✔
1833

1834
  if (numOfExecuted == numOfActions) {
205,168✔
1835
    if (errCode == 0) {
134,691✔
1836
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
134,681!
1837
      return 0;
134,681✔
1838
    } else {
1839
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
10!
1840
      mndTransResetActions(pMnode, pTrans, pArray);
10✔
1841
      terrno = errCode;
10✔
1842
      return errCode;
10✔
1843
    }
1844
  } else {
1845
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
70,477!
1846

1847
    for (int32_t action = 0; action < numOfActions; ++action) {
408,362✔
1848
      STransAction *pAction = taosArrayGet(pArray, action);
337,885✔
1849
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
337,885✔
1850
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1851
             pAction->acceptableCode, pAction->retryCode);
1852
      if (pAction->msgSent) {
337,885✔
1853
        bool reset = false;
337,548✔
1854
        if (pAction->msgReceived) {
337,548✔
1855
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
135,236✔
1856
        } else {
1857
          int64_t timestamp = taosGetTimestampMs();
202,312✔
1858
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
202,312!
1859
        }
1860
        if (reset) {
337,548✔
1861
          mndTransResetAction(pMnode, pTrans, pAction);
43✔
1862
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
43!
1863
                pAction->id, pAction->errCode, pAction->startTime);
1864
        }
1865
      }
1866
    }
1867
    return TSDB_CODE_ACTION_IN_PROGRESS;
70,477✔
1868
  }
1869
}
1870

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

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

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

1898
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
21,663✔
1899
  int32_t code = 0;
21,663✔
1900
  int32_t numOfActions = taosArrayGetSize(pActions);
21,663✔
1901
  if (numOfActions == 0) return code;
21,663✔
1902

1903
  if (pTrans->actionPos >= numOfActions) {
21,661✔
1904
    return code;
1,496✔
1905
  }
1906

1907
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
20,165!
1908
        pTrans->actionPos, mndTransStr(pTrans->stage));
1909

1910
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
31,154✔
1911
    STransAction *pAction = taosArrayGet(pActions, action);
29,953✔
1912

1913
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
29,953✔
1914
      code = TSDB_CODE_ACTION_IN_PROGRESS;
27✔
1915
      break;
18,964✔
1916
    }
1917

1918
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
29,926!
1919
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
1920

1921
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
29,926✔
1922
    if (code == 0) {
29,926✔
1923
      if (pAction->msgSent) {
25,343✔
1924
        bool reset = false;
21,950✔
1925
        if (pAction->msgReceived) {
21,950✔
1926
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
9,462!
1927
            code = pAction->errCode;
5,556✔
1928
            reset = true;
5,556✔
1929
          } else {
1930
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
3,906!
1931
          }
1932
        } else {
1933
          int64_t timestamp = taosGetTimestampMs();
12,488✔
1934
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
12,488!
1935
          code = TSDB_CODE_ACTION_IN_PROGRESS;
12,488✔
1936
        }
1937
        if (reset) {
21,950✔
1938
          mndTransResetAction(pMnode, pTrans, pAction);
5,556✔
1939
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
5,556!
1940
                pAction->id, pAction->errCode, pAction->startTime);
1941
        }
1942
      } else if (pAction->rawWritten) {
3,393!
1943
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,393!
1944
          code = pAction->errCode;
×
1945
        } else {
1946
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
3,393!
1947
        }
1948
      } else {
1949
      }
1950
    }
1951

1952
    if (code == 0) {
29,926✔
1953
      pTrans->failedTimes = 0;
7,299✔
1954
    }
1955
    mndSetTransLastAction(pTrans, pAction);
29,926✔
1956
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
29,926✔
1957
      mInfo(
17,071!
1958
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1959
          "msgReceived:%d",
1960
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1961
          pAction->msgSent, pAction->msgReceived);
1962
    } else if (code != 0) {
12,855✔
1963
      mError(
5,556!
1964
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1965
          "msgReceived:%d",
1966
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1967
          pAction->msgSent, pAction->msgReceived);
1968
    }
1969

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

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

2015
  return code;
20,165✔
2016
}
2017

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

2025
  if (groupId <= 0) {
13,505!
2026
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
2027
    return TSDB_CODE_INTERNAL_ERROR;
×
2028
  }
2029

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

2036
  if (*actionPos >= numOfActions) {
13,505✔
2037
    mError("trans:%d, failed to execute action in serail group, actionPos:%d >= numOfActions:%d at group %d",
2,820!
2038
           pTrans->id, *actionPos, numOfActions, groupId);
2039
    return TSDB_CODE_INTERNAL_ERROR;
2,820✔
2040
  }
2041

2042
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
12,208✔
2043
    STransAction **ppAction = taosArrayGet(pActions, action);
11,626✔
2044
    STransAction  *pAction = *ppAction;
11,626✔
2045

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

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

2062
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
10,119✔
2063
    if (code == 0) {
10,119✔
2064
      if (pAction->msgSent) {
5,600✔
2065
        if (pAction->msgReceived) {
4,986✔
2066
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
1,349!
2067
            code = pAction->errCode;
123✔
2068
            mndTransResetAction(pMnode, pTrans, pAction);
123✔
2069
          } else {
2070
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
1,226!
2071
                  pAction->id, action, numOfActions, groupId);
2072
          }
2073
        } else {
2074
          code = TSDB_CODE_ACTION_IN_PROGRESS;
3,637✔
2075
        }
2076
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
4,986✔
2077
        if (msgSent != NULL) {
4,986!
2078
          *msgSent = pAction->msgSent;
4,986✔
2079
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
4,986!
2080
        } else {
2081
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2082
        }
2083
      } else if (pAction->rawWritten) {
614!
2084
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
614!
2085
          code = pAction->errCode;
×
2086
        } else {
2087
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
614!
2088
        }
2089
      } else {
2090
      }
2091
    }
2092

2093
    if (code == 0) {
10,119✔
2094
      pTrans->failedTimes = 0;
1,840✔
2095
    }
2096
    mndSetTransLastAction(pTrans, pAction);
10,119✔
2097

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

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

2168
  return code;
10,685✔
2169
}
2170

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

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

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

2199
  mInfo("trans:%d, redo action group begin to execute, total group count:%d", pTrans->id, groupCount);
3,509!
2200
  void   *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
3,509✔
2201
  int32_t currentGroup = 1;
3,509✔
2202
  while (pIter) {
17,014✔
2203
    SArray **redoActions = pIter;
13,505✔
2204
    size_t   keyLen = 0;
13,505✔
2205
    int32_t *key = taosHashGetKey(pIter, &keyLen);
13,505✔
2206
    int32_t  actionCount = taosArrayGetSize(*redoActions);
13,505✔
2207
    mInfo("trans:%d, group:%d/%d(%d) begin to execute, current group(action count:%d) transaction(action pos:%d)",
13,505!
2208
          pTrans->id, currentGroup, groupCount, *key, actionCount, pTrans->actionPos);
2209
    code = mndTransExecuteActionsSerialGroup(pMnode, pTrans, *redoActions, topHalf, *key, currentGroup, groupCount,
13,505✔
2210
                                             notSend, pHash);
2211
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
13,505✔
2212
      mInfo("trans:%d, group:%d/%d(%d) not able to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
4,519!
2213
            tstrerror(code));
2214
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
8,986✔
2215
      mInfo("trans:%d, group:%d/%d(%d) is executed and still in progress", pTrans->id, currentGroup, groupCount, *key);
5,144!
2216
    } else if (code != 0) {
3,842✔
2217
      mError("trans:%d, group:%d/%d(%d) failed to execute, code:%s", pTrans->id, currentGroup, groupCount, *key,
2,820!
2218
             tstrerror(code));
2219
    } else {
2220
      successCount++;
1,022✔
2221
      mInfo("trans:%d, group:%d/%d(%d) is finished", pTrans->id, currentGroup, groupCount, *key);
1,022!
2222
    }
2223
    currentGroup++;
13,505✔
2224
    pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
13,505✔
2225
  }
2226

2227
  taosHashCleanup(pHash);
3,509✔
2228

2229
  if (successCount == groupCount) {
3,509✔
2230
    total_code = 0;
68✔
2231
  } else {
2232
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
3,441!
2233
  }
2234

2235
  return total_code;
3,509✔
2236
}
2237

2238
static int32_t mndTransExecuteRedoActionsParallel(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
4,022✔
2239
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
4,022✔
2240
  (void)taosThreadMutexLock(&pTrans->mutex);
4,022✔
2241

2242
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
4,022!
2243
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
4,022✔
2244
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
4,022!
2245
      code = 0;
348✔
2246
    } else {
2247
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
3,674✔
2248
      if (pAction != NULL && pAction->groupId == -1) {
3,674!
2249
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
165✔
2250
      } else {
2251
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
3,509✔
2252
        if (code == 0) {
3,509✔
2253
          if (pTrans->actionPos < numOfActions) {
68✔
2254
            code = TSDB_CODE_ACTION_IN_PROGRESS;
56✔
2255
          }
2256
        }
2257
      }
2258
    }
2259
  }
2260

2261
  (void)taosThreadMutexUnlock(&pTrans->mutex);
4,022✔
2262

2263
  return code;
4,022✔
2264
}
2265

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

2276
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
60,935✔
2277
  bool    continueExec = true;
60,935✔
2278
  int32_t code = 0;
60,935✔
2279
  terrno = 0;
60,935✔
2280

2281
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
60,935✔
2282
  if (numOfActions == 0) goto _OVER;
60,935✔
2283

2284
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
19,376!
2285

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

2297
_OVER:
19,376✔
2298
  pTrans->stage = TRN_STAGE_REDO_ACTION;
60,935✔
2299
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
60,935!
2300
  return continueExec;
60,935✔
2301
}
2302

2303
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
203,849✔
2304
  bool    continueExec = true;
203,849✔
2305
  int32_t code = 0;
203,849✔
2306
  terrno = 0;
203,849✔
2307

2308
  if (pTrans->exec == TRN_EXEC_SERIAL) {
203,849✔
2309
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
21,498✔
2310
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
182,351✔
2311
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
178,329✔
2312
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
4,022!
2313
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
4,022✔
2314
  } else {
2315
    code = TSDB_CODE_INTERNAL_ERROR;
×
2316
  }
2317

2318
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
203,867!
2319
      mndTransIsInSyncContext(topHalf)) {
18✔
2320
    pTrans->lastErrorNo = code;
×
2321
    pTrans->code = code;
×
2322
    mInfo(
×
2323
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2324
        "continueExec:%d, code:%s",
2325
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2326
    taosMsleep(100);
×
2327
    return true;
×
2328
  } else {
2329
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
203,849✔
2330
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
69,052!
2331
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2332
      return false;
69,052✔
2333
    }
2334
  }
2335
  terrno = code;
134,797✔
2336

2337
  if (code == 0) {
134,797✔
2338
    pTrans->code = 0;
49,932✔
2339
    pTrans->stage = TRN_STAGE_COMMIT;
49,932✔
2340
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
49,932!
2341
    continueExec = true;
49,932✔
2342
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
84,865!
2343
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
84,847!
2344
    continueExec = false;
84,847✔
2345
  } else {
2346
    pTrans->failedTimes++;
18✔
2347
    pTrans->code = terrno;
18✔
2348
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
18✔
2349
      if (pTrans->lastAction != 0) {
5✔
2350
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
4✔
2351
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
4!
2352
          if (pTrans->failedTimes < 6) {
×
2353
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2354
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
2355
            taosMsleep(1000);
×
2356
            continueExec = true;
×
2357
            return true;
×
2358
          }
2359
        }
2360
      }
2361

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

2373
  return continueExec;
134,797✔
2374
}
2375

2376
// execute in trans context
2377
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
49,933✔
2378
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
49,933✔
2379

2380
  bool    continueExec = true;
49,932✔
2381
  int32_t code = mndTransCommit(pMnode, pTrans);
49,932✔
2382

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

2395
  return continueExec;
49,932✔
2396
}
2397

2398
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
110,809✔
2399
  bool    continueExec = true;
110,809✔
2400
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
110,809✔
2401

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

2419
  return continueExec;
110,809✔
2420
}
2421

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

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

2432
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
52✔
2433
  terrno = code;
37✔
2434

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

2448
  return continueExec;
37✔
2449
}
2450

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

2455
  bool    continueExec = true;
5✔
2456
  int32_t code = mndTransRollback(pMnode, pTrans);
5✔
2457

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

2467
  return continueExec;
5✔
2468
}
2469

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

2474
  bool    continueExec = true;
5✔
2475
  int32_t code = mndTransPreFinish(pMnode, pTrans);
5✔
2476

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

2487
  return continueExec;
5✔
2488
}
2489

2490
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
110,824✔
2491
  bool continueExec = false;
110,824✔
2492
  if (topHalf) return continueExec;
110,824✔
2493

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

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

2506
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
60,896!
2507
        pTrans->failedTimes, pTrans->createdTime);
2508
  return continueExec;
60,896✔
2509
}
2510

2511
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
264,793✔
2512
  bool continueExec = true;
264,793✔
2513

2514
  while (continueExec) {
740,270✔
2515
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
475,477!
2516
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2517
    pTrans->lastExecTime = taosGetTimestampMs();
475,477✔
2518
    switch (pTrans->stage) {
475,477!
2519
      case TRN_STAGE_PREPARE:
×
2520
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2521
        break;
×
2522
      case TRN_STAGE_REDO_ACTION:
203,849✔
2523
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
203,849✔
2524
        break;
203,849✔
2525
      case TRN_STAGE_COMMIT:
49,933✔
2526
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
49,933✔
2527
        break;
49,933✔
2528
      case TRN_STAGE_COMMIT_ACTION:
110,809✔
2529
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
110,809✔
2530
        break;
110,809✔
2531
      case TRN_STAGE_ROLLBACK:
5✔
2532
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
5✔
2533
        break;
5✔
2534
      case TRN_STAGE_UNDO_ACTION:
52✔
2535
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
52✔
2536
        break;
52✔
2537
      case TRN_STAGE_PRE_FINISH:
5✔
2538
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
5✔
2539
        break;
5✔
2540
      case TRN_STAGE_FINISH:
110,824✔
2541
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
110,824✔
2542
        break;
110,824✔
2543
      default:
×
2544
        continueExec = false;
×
2545
        break;
×
2546
    }
2547
  }
2548

2549
  mndTransSendRpcRsp(pMnode, pTrans);
264,793✔
2550
}
264,793✔
2551

2552
// start trans, pullup, receive rsp, kill
2553
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
135,011✔
2554
  bool topHalf = true;
135,011✔
2555
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
135,011✔
2556
}
135,011✔
2557

2558
// update trans
2559
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
129,782✔
2560
  bool topHalf = false;
129,782✔
2561
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
129,782✔
2562
}
129,782✔
2563

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

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

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

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

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

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

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

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

2631
  code = mndKillTrans(pMnode, pTrans);
×
2632

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

2638
  mndReleaseTrans(pMnode, pTrans);
1✔
2639
  TAOS_RETURN(code);
1✔
2640
}
2641

2642
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
194✔
2643

2644
void mndTransPullup(SMnode *pMnode) {
31,169✔
2645
  SSdb   *pSdb = pMnode->pSdb;
31,169✔
2646
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
31,169✔
2647
  if (pArray == NULL) return;
31,169!
2648

2649
  void *pIter = NULL;
31,169✔
2650
  while (1) {
5,647✔
2651
    STrans *pTrans = NULL;
36,816✔
2652
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
36,816✔
2653
    if (pIter == NULL) break;
36,816✔
2654
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
11,294!
2655
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2656
    }
2657
    sdbRelease(pSdb, pTrans);
5,647✔
2658
  }
2659

2660
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
31,169✔
2661

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

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

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

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

2700
  return buf;
9,400✔
2701
}
2702

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

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

2719
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
185!
2720
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
6,172✔
2721
      len = 0;
5,987✔
2722
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
5,987✔
2723
      if (pAction->actionType == TRANS_ACTION_MSG) {
5,987✔
2724
        char bufStart[40] = {0};
4,700✔
2725
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
4,700✔
2726

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

2735
        SEpSet epset = pAction->epSet;
4,700✔
2736
        if (epset.numOfEps > 0) {
4,700!
2737
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
4,700✔
2738
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
10,694✔
2739
            len +=
5,994✔
2740
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
5,994✔
2741
          }
2742
        }
2743

2744
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
4,700✔
2745
                        tstrerror(pAction->errCode));
2746
      } else {
2747
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
1,287✔
2748
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
1,287✔
2749
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
1,287✔
2750
      }
2751
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
5,987!
2752
    }
2753
  }
2754

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

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

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

2791
  while (numOfRows < rows) {
402!
2792
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
402✔
2793
    if (pShow->pIter == NULL) break;
402✔
2794

2795
    cols = 0;
185✔
2796

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

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

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

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

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

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

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

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

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

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

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

2862
    mndTransLogAction(pTrans);
185✔
2863

2864
    numOfRows++;
185✔
2865
    sdbRelease(pSdb, pTrans);
185✔
2866
  }
2867

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

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

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

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

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

2907
  cols = 0;
×
2908

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

2911
  if (pAction->actionType == TRANS_ACTION_MSG) {
×
2912
    int32_t len = 0;
×
2913

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

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

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

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

2957
  } else {
2958
    int32_t len = 0;
×
2959

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

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

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

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

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

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

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

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

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

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

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

3052
  int32_t code = 0;
×
3053
  int32_t lino = 0;
×
3054

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

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

3067
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
×
3068

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

3077
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
×
3078

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

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

3092
_OVER:
×
3093
  pShow->numOfRows += numOfRows;
×
3094

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

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