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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

69.53
/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; }
324,984✔
60

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

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

77
static inline char *mndStrExecutionContext(bool topHalf) { return topHalf ? "transContext" : "syncContext"; }
477,686✔
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) {
1,929✔
90
  SSdbTable table = {
1,929✔
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);
1,929✔
101
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
1,929✔
102

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

109
void mndCleanupTrans(SMnode *pMnode) {}
1,929✔
110

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

115
  for (int32_t i = 0; i < actionNum; ++i) {
1,770,899✔
116
    STransAction *pAction = taosArrayGet(pArray, i);
1,159,259✔
117
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,159,259✔
118
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
800,816✔
119
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
358,443!
120
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
358,443✔
121
    } else {
122
      // empty
123
    }
124
    rawDataLen += sizeof(int8_t);
1,159,259✔
125
  }
126

127
  return rawDataLen;
611,640✔
128
}
129

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

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

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

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

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

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

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

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

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

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

222
  SDB_SET_INT32(pRaw, dataPos, pTrans->startFunc, _OVER)
152,910!
223
  SDB_SET_INT32(pRaw, dataPos, pTrans->stopFunc, _OVER)
152,910!
224
  SDB_SET_INT32(pRaw, dataPos, pTrans->paramLen, _OVER)
152,910!
225
  if (pTrans->param != NULL) {
152,910!
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)
152,910!
230

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

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

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

263
  terrno = 0;
152,910✔
264

265
_OVER:
152,910✔
266
  if (terrno != 0) {
152,910!
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);
152,910✔
274
  return pRaw;
152,910✔
275
}
276

277
static int32_t mndTransDecodeGroupRedoAction(SHashObj *redoGroupActions, STransAction *pAction) {
57,687✔
278
  if (pAction->groupId < 0) return 0;
57,687✔
279
  SArray **redoAction = taosHashGet(redoGroupActions, &pAction->groupId, sizeof(int32_t));
55,944✔
280
  if (redoAction == NULL) {
55,944✔
281
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
10,642✔
282
    if (array != NULL) {
10,642!
283
      if (taosHashPut(redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
10,642!
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));
10,642✔
289
  }
290
  if (redoAction != NULL) {
55,944!
291
    if (taosArrayPush(*redoAction, &pAction) == NULL) return TSDB_CODE_INTERNAL_ERROR;
111,888!
292
  }
293
  return 0;
55,944✔
294
}
295

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

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

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

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

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

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

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

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

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

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

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

450
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
288,799!
451

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

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

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

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

497
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->prepareActions, prepareActionNum, sver) < 0) goto _OVER;
288,799!
498
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
288,799✔
499
    if (mndTransDecodeActionWithGroup(pRaw, &dataPos, pTrans->redoActions, redoActionNum, pTrans->redoGroupActions,
3,256!
500
                                      sver) < 0)
501
      goto _OVER;
×
502
  } else {
503
    if (mndTransDecodeAction(pRaw, &dataPos, pTrans->redoActions, redoActionNum, sver) < 0) goto _OVER;
285,543!
504
  }
505
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->undoActions, undoActionNum, sver) < 0) goto _OVER;
288,799!
506
  if (mndTransDecodeAction(pRaw, &dataPos, pTrans->commitActions, commitActionNum, sver) < 0) goto _OVER;
288,799!
507

508
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
288,799!
509
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
288,799!
510
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
288,799!
511
  if (pTrans->paramLen != 0) {
288,799!
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);
288,799!
518

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

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

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

552
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
288,799!
553

554
  terrno = 0;
288,799✔
555

556
_OVER:
288,799✔
557
  if (terrno != 0 && pTrans != NULL) {
288,799!
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) {
288,799!
566
    mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
288,799✔
567
  }
568
  return pRow;
288,799✔
569
}
570

571
static const char *mndTransStr(ETrnStage stage) {
2,034,356✔
572
  switch (stage) {
2,034,356!
573
    case TRN_STAGE_PREPARE:
154,928✔
574
      return "prepare";
154,928✔
575
    case TRN_STAGE_REDO_ACTION:
694,573✔
576
      return "redoAction";
694,573✔
577
    case TRN_STAGE_ROLLBACK:
10✔
578
      return "rollback";
10✔
579
    case TRN_STAGE_UNDO_ACTION:
40,625✔
580
      return "undoAction";
40,625✔
581
    case TRN_STAGE_COMMIT:
246,550✔
582
      return "commit";
246,550✔
583
    case TRN_STAGE_COMMIT_ACTION:
528,295✔
584
      return "commitAction";
528,295✔
585
    case TRN_STAGE_FINISH:
369,365✔
586
      return "finished";
369,365✔
587
    case TRN_STAGE_PRE_FINISH:
10✔
588
      return "pre-finish";
10✔
589
    default:
×
590
      return "invalid";
×
591
  }
592
}
593

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

605
static void mndSetTransLastAction(STrans *pTrans, STransAction *pAction) {
580,024✔
606
  if (pAction != NULL) {
580,024✔
607
    if (pAction->errCode != TSDB_CODE_ACTION_IN_PROGRESS) {
455,802✔
608
      pTrans->lastAction = pAction->id;
334,020✔
609
      pTrans->lastMsgType = pAction->msgType;
334,020✔
610
      pTrans->lastEpset = pAction->epSet;
334,020✔
611
      pTrans->lastErrorNo = pAction->errCode;
334,020✔
612
    }
613
  } else {
614
    pTrans->lastAction = 0;
124,222✔
615
    pTrans->lastMsgType = 0;
124,222✔
616
    memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
124,222✔
617
    pTrans->lastErrorNo = 0;
124,222✔
618
  }
619
}
580,024✔
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) {
1,534✔
630
  switch (ftype) {
1,534!
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:
767✔
636
      return mndRebCntInc;
767✔
637
    case TRANS_STOP_FUNC_MQ_REB:
767✔
638
      return mndRebCntDec;
767✔
639
    default:
×
640
      return NULL;
×
641
  }
642
}
643

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

648
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
54,916✔
649

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

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

663
  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
54,916!
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) {
54,916!
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;
54,916✔
674
}
675

676
void mndTransDropData(STrans *pTrans) {
334,920✔
677
  if (pTrans->prepareActions != NULL) {
334,920!
678
    mndTransDropActions(pTrans->prepareActions);
334,920✔
679
    pTrans->prepareActions = NULL;
334,920✔
680
  }
681
  if (pTrans->redoActions != NULL) {
334,920!
682
    mndTransDropActions(pTrans->redoActions);
334,920✔
683
    pTrans->redoActions = NULL;
334,920✔
684
  }
685
  if (pTrans->undoActions != NULL) {
334,920!
686
    mndTransDropActions(pTrans->undoActions);
334,920✔
687
    pTrans->undoActions = NULL;
334,920✔
688
  }
689
  if (pTrans->commitActions != NULL) {
334,920!
690
    mndTransDropActions(pTrans->commitActions);
334,920✔
691
    pTrans->commitActions = NULL;
334,920✔
692
  }
693
  if (pTrans->redoGroupActions != NULL) {
334,920✔
694
    void *pIter = taosHashIterate(pTrans->redoGroupActions, NULL);
49,377✔
695
    while (pIter) {
60,519✔
696
      SArray **redoActions = pIter;
11,142✔
697
      taosArrayDestroy(*redoActions);
11,142✔
698
      pIter = taosHashIterate(pTrans->redoGroupActions, pIter);
11,142✔
699
    }
700

701
    taosHashCleanup(pTrans->redoGroupActions);
49,377✔
702
    pTrans->redoGroupActions = NULL;
49,377✔
703
  }
704
  if (pTrans->groupActionPos != NULL) {
334,920✔
705
    taosHashCleanup(pTrans->groupActionPos);
49,377✔
706
    pTrans->groupActionPos = NULL;
49,377✔
707
  }
708
  if (pTrans->arbGroupIds != NULL) {
334,920✔
709
    taosHashCleanup(pTrans->arbGroupIds);
46,231✔
710
  }
711
  if (pTrans->pRpcArray != NULL) {
334,920✔
712
    taosArrayDestroy(pTrans->pRpcArray);
46,121✔
713
    pTrans->pRpcArray = NULL;
46,121✔
714
  }
715
  if (pTrans->rpcRsp != NULL) {
334,920✔
716
    taosMemoryFree(pTrans->rpcRsp);
17,541!
717
    pTrans->rpcRsp = NULL;
17,541✔
718
    pTrans->rpcRspLen = 0;
17,541✔
719
  }
720
  if (pTrans->param != NULL) {
334,920!
721
    taosMemoryFree(pTrans->param);
×
722
    pTrans->param = NULL;
×
723
    pTrans->paramLen = 0;
×
724
  }
725
  (void)taosThreadMutexDestroy(&pTrans->mutex);
334,920✔
726
}
334,920✔
727

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

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

740
  mndTransDropData(pTrans);
171,843✔
741
  return 0;
171,843✔
742
}
743

744
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
248,588✔
745
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
760,024✔
746
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
511,436✔
747
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
511,436✔
748
    pOldAction->rawWritten = pNewAction->rawWritten;
511,436✔
749
    pOldAction->msgSent = pNewAction->msgSent;
511,436✔
750
    pOldAction->msgReceived = pNewAction->msgReceived;
511,436✔
751
    pOldAction->errCode = pNewAction->errCode;
511,436✔
752
  }
753
}
248,588✔
754

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

759
  if (pOld->createdTime != pNew->createdTime) {
62,147!
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);
62,147✔
769
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
62,147✔
770
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
62,147✔
771
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
62,147✔
772
  pOld->stage = pNew->stage;
62,147✔
773
  pOld->actionPos = pNew->actionPos;
62,147✔
774

775
  void *pIter = taosHashIterate(pNew->groupActionPos, NULL);
62,147✔
776
  while (pIter != NULL) {
66,705✔
777
    int32_t newActionPos = *(int32_t *)pIter;
4,558✔
778

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

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

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

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

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

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

806
  return 0;
62,147✔
807
}
808

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

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

823
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq,
46,121✔
824
                       const char *opername) {
825
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
46,121!
826
  if (pTrans == NULL) {
46,121!
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) {
46,121!
833
    tstrncpy(pTrans->opername, opername, TSDB_TRANS_OPER_LEN);
46,121✔
834
  }
835

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

858
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL ||
46,121!
859
      pTrans->pRpcArray == NULL) {
46,121!
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) {
46,121✔
867
    if (taosArrayPush(pTrans->pRpcArray, &pReq->info) == NULL) {
61,998!
868
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
869
      return NULL;
×
870
    }
871
    pTrans->originRpcType = pReq->msgType;
30,999✔
872
  }
873

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

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

880
static void mndTransDropActions(SArray *pArray) {
1,339,680✔
881
  int32_t size = taosArrayGetSize(pArray);
1,339,680✔
882
  for (int32_t i = 0; i < size; ++i) {
3,819,524✔
883
    STransAction *pAction = taosArrayGet(pArray, i);
2,479,844✔
884
    if (pAction->actionType == TRANS_ACTION_RAW) {
2,479,844✔
885
      taosMemoryFreeClear(pAction->pRaw);
1,685,116!
886
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
794,728!
887
      taosMemoryFreeClear(pAction->pCont);
794,728!
888
    } else {
889
      // nothing
890
    }
891
  }
892

893
  taosArrayDestroy(pArray);
1,339,680✔
894
}
1,339,680✔
895

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

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

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

912
  return 0;
325,589✔
913
}
914

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

918
  SArray **redoAction = taosHashGet(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t));
1,171✔
919
  if (redoAction == NULL) {
1,171✔
920
    SArray *array = taosArrayInit(4, sizeof(STransAction *));
500✔
921
    if (array != NULL) {
500!
922
      if (taosHashPut(pTrans->redoGroupActions, &pAction->groupId, sizeof(int32_t), &array, sizeof(SArray *)) < 0) {
500!
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));
500✔
927
    }
928
  }
929
  if (redoAction != NULL) {
1,171!
930
    mInfo("trans:%d, append action into group %d, msgType:%s", pTrans->id, pAction->groupId,
1,171!
931
          TMSG_INFO(pAction->msgType));
932
    void *ptr = taosArrayPush(*redoAction, &pAction);
1,171✔
933
    if (ptr == NULL) {
1,171!
934
      TAOS_RETURN(terrno);
×
935
    }
936
  }
937

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

947
  return 0;
1,171✔
948
}
949

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

957
int32_t mndTransAppendGroupRedolog(STrans *pTrans, SSdbRaw *pRaw, int32_t groupId) {
976✔
958
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION,
976✔
959
                         .actionType = TRANS_ACTION_RAW,
960
                         .pRaw = pRaw,
961
                         .mTraceId = pTrans->mTraceId,
976✔
962
                         .groupId = groupId};
963
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && action.groupId == 0) {
976!
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));
976!
968
  return mndTransAppendAction(pTrans->redoActions, &action);
976✔
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) {
13,382✔
991
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
13,382✔
992
  return mndTransAppendAction(pTrans->undoActions, &action);
13,382✔
993
}
994

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

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

1006
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
54,301✔
1007
  pAction->stage = TRN_STAGE_REDO_ACTION;
54,301✔
1008
  pAction->actionType = TRANS_ACTION_MSG;
54,301✔
1009
  pAction->mTraceId = pTrans->mTraceId;
54,301✔
1010
  if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId == 0) {
54,301✔
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));
54,298!
1015
  return mndTransAppendAction(pTrans->redoActions, pAction);
54,298✔
1016
}
1017

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

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

1029
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
755✔
1030
  pTrans->startFunc = startFunc;
755✔
1031
  pTrans->stopFunc = stopFunc;
755✔
1032
  pTrans->param = param;
755✔
1033
  pTrans->paramLen = paramLen;
755✔
1034
}
755✔
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) {
25,697✔
1068
  if (dbname != NULL) {
25,697!
1069
    tstrncpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN);
25,697✔
1070
  }
1071
  if (stbname != NULL) {
25,697✔
1072
    tstrncpy(pTrans->stbname, stbname, TSDB_TABLE_FNAME_LEN);
18,671✔
1073
  }
1074
}
25,697✔
1075

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

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

1087
void mndTransSetGroupParallel(STrans *pTrans) {
205✔
1088
  mInfo("trans:%d, set Group Parallel", pTrans->id);
205!
1089
  pTrans->exec = TRN_EXEC_GROUP_PARALLEL;
205✔
1090
}
205✔
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) {
13✔
1100
  pTrans->ableToBeKilled = true; 
13✔
1101
  pTrans->killMode = killMode; 
13✔
1102
}
13✔
1103

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

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

1108
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
97,922✔
1109
  int32_t  code = 0;
97,922✔
1110
  SSdbRaw *pRaw = mndTransEncode(pTrans);
97,922✔
1111
  if (pRaw == NULL) {
97,922!
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));
97,922!
1118

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

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

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

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

1146
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
19,160✔
1147
  if (conflict) {
19,160✔
1148
    mError("trans:%d, opername:%s db:%s stb:%s type:%d, can't execute since conflict with trans:%d, opername:%s db:%s "
352!
1149
      "stb:%s type:%d", 
1150
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1151
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1152
    *globalConflict = true;
352✔
1153
  } else {
1154
    mInfo("trans:%d, opername:%s db:%s stb:%s type:%d, not conflict with trans:%d, opername:%s db:%s stb:%s type:%d", 
18,808!
1155
      pNew->id, pNew->opername, pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->opername, 
1156
      pTrans->dbname, pTrans->stbname, pTrans->conflict);
1157
  }
1158
}
19,160✔
1159

1160
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
126,719✔
1161
  STrans *pTrans = NULL;
126,719✔
1162
  void   *pIter = NULL;
126,719✔
1163
  bool    conflict = false;
126,719✔
1164

1165
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
126,719✔
1166

1167
  int32_t size = sdbGetSize(pMnode->pSdb, SDB_TRANS);
90,279✔
1168
  mInfo("trans:%d, trans hash size %d", pNew->id, size);
90,279!
1169

1170
  while (1) {
1171
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
108,070✔
1172
    if (pIter == NULL) break;
108,070✔
1173

1174
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
17,791✔
1175

1176
    if (pNew->conflict == TRN_CONFLICT_DB) {
17,791✔
1177
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
1,107!
1178
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
1,107!
1179
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,107✔
1180
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,107✔
1181
      }
1182
    }
1183

1184
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
17,791✔
1185
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
16,654✔
1186
      if (pTrans->conflict == TRN_CONFLICT_DB) {
16,654✔
1187
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
298✔
1188
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
298✔
1189
      }
1190
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
16,654✔
1191
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
16,350✔
1192
      }
1193
    }
1194

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

1213
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
17,791!
1214
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
×
1215
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
1216
      } else {
1217
        mndTransLogConflict(pNew, pTrans, false, &conflict);
×
1218
      }
1219
    }
1220

1221
    sdbRelease(pMnode->pSdb, pTrans);
17,791✔
1222
  }
1223

1224
  return conflict;
90,279✔
1225
}
1226

1227
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
126,719✔
1228
  int32_t code = 0;
126,719✔
1229
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
126,719✔
1230
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
81,623!
1231
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
1232
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
1233
      TAOS_RETURN(code);
×
1234
    }
1235
  }
1236

1237
  if (mndCheckTransConflict(pMnode, pTrans)) {
126,719✔
1238
    code = TSDB_CODE_MND_TRANS_CONFLICT;
388✔
1239
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
388!
1240
    TAOS_RETURN(code);
388✔
1241
  }
1242

1243
  TAOS_RETURN(code);
126,331✔
1244
}
1245

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

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

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

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

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

1281
  TAOS_RETURN(code);
288✔
1282
}
1283

1284
static bool mndTransActionsOfSameType(SArray *pActions) {
107,066✔
1285
  int32_t size = taosArrayGetSize(pActions);
107,066✔
1286
  ETrnAct lastActType = TRANS_ACTION_NULL;
107,066✔
1287
  bool    same = true;
107,066✔
1288
  for (int32_t i = 0; i < size; ++i) {
376,756✔
1289
    STransAction *pAction = taosArrayGet(pActions, i);
269,690✔
1290
    if (i > 0) {
269,690✔
1291
      if (lastActType != pAction->actionType) {
193,402!
1292
        same = false;
×
1293
        break;
×
1294
      }
1295
    }
1296
    lastActType = pAction->actionType;
269,690✔
1297
  }
1298
  return same;
107,066✔
1299
}
1300

1301
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
45,662✔
1302
  int32_t code = 0;
45,662✔
1303
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
45,662✔
1304
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
44,269!
1305
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1306
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1307
      TAOS_RETURN(code);
×
1308
    }
1309

1310
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
44,269✔
1311
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
17,135!
1312
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1313
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1314
        TAOS_RETURN(code);
×
1315
      }
1316
    }
1317
  }
1318

1319
  TAOS_RETURN(code);
45,662✔
1320
}
1321

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

1335
  TAOS_RETURN(code);
45,662✔
1336
}
1337

1338
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
45,672✔
1339
  int32_t code = 0;
45,672✔
1340
  if (pTrans == NULL) {
45,672!
1341
    return TSDB_CODE_INVALID_PARA;
×
1342
  }
1343

1344
  mInfo("trans:%d, action list:", pTrans->id);
45,672!
1345
  int32_t index = 0;
45,672✔
1346
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->prepareActions); ++i, ++index) {
70,373✔
1347
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, i);
24,701✔
1348
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
24,701!
1349
          pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1350
  }
1351

1352
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
101,848✔
1353
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
56,176✔
1354
    if (pAction->actionType == TRANS_ACTION_MSG) {
56,176✔
1355
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
54,034!
1356
            TMSG_INFO(pAction->msgType));
1357
      ;
1358
    } else {
1359
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
2,142!
1360
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1361
    }
1362
  }
1363

1364
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->commitActions); ++i, ++index) {
249,328✔
1365
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
203,656✔
1366
    mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage), i,
203,656!
1367
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1368
  }
1369

1370
  for (int32_t i = 0; i < taosArrayGetSize(pTrans->undoActions); ++i, ++index) {
86,211✔
1371
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
40,539✔
1372
    if (pAction->actionType == TRANS_ACTION_MSG) {
40,539✔
1373
      mInfo("trans:%d, action:%d, %s:%d msgType:%s", pTrans->id, index, mndTransStr(pAction->stage), pAction->id,
27,157!
1374
            TMSG_INFO(pAction->msgType));
1375
      ;
1376
    } else {
1377
      mInfo("trans:%d, action:%d, %s:%d sdbType:%s, sdbStatus:%s", pTrans->id, index, mndTransStr(pAction->stage),
13,382!
1378
            pAction->id, sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1379
    }
1380
  }
1381

1382
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
45,672✔
1383

1384
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
45,662!
1385

1386
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
45,662!
1387

1388
  mInfo("trans:%d, prepare transaction", pTrans->id);
45,662!
1389
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
45,662!
1390
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
×
1391
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
×
1392
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
×
1393
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
×
1394
    TAOS_RETURN(code);
×
1395
  }
1396
  mInfo("trans:%d, prepare finished", pTrans->id);
45,662!
1397

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

1406
  pNew->pRpcArray = pTrans->pRpcArray;
45,662✔
1407
  pNew->rpcRsp = pTrans->rpcRsp;
45,662✔
1408
  pNew->rpcRspLen = pTrans->rpcRspLen;
45,662✔
1409
  pNew->mTraceId = pTrans->mTraceId;
45,662✔
1410
  pTrans->pRpcArray = NULL;
45,662✔
1411
  pTrans->rpcRsp = NULL;
45,662✔
1412
  pTrans->rpcRspLen = 0;
45,662✔
1413

1414
  mInfo("trans:%d, execute transaction in prepare", pTrans->id);
45,662!
1415
  mndTransExecute(pMnode, pNew, false);
45,662✔
1416
  mndReleaseTrans(pMnode, pNew);
45,662✔
1417
  // TDOD change to TAOS_RETURN(code);
1418
  return 0;
45,662✔
1419
}
1420

1421
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
45,664✔
1422
  int32_t code = 0;
45,664✔
1423
  mInfo("trans:%d, commit transaction", pTrans->id);
45,664!
1424
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
45,664✔
1425
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
11!
1426
    TAOS_RETURN(code);
11✔
1427
  }
1428
  mInfo("trans:%d, commit finished", pTrans->id);
45,653!
1429
  TAOS_RETURN(code);
45,653✔
1430
}
1431

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

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

1454
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
223,643✔
1455
  bool    sendRsp = false;
223,643✔
1456
  int32_t code = pTrans->code;
223,643✔
1457

1458
  if (pTrans->stage == TRN_STAGE_FINISH) {
223,643✔
1459
    sendRsp = true;
100,436✔
1460
  }
1461

1462
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
223,643✔
1463
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
81,031!
1464
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
12!
1465
      sendRsp = true;
12✔
1466
    }
1467
  } else {
1468
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
142,612✔
1469
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
81,851!
1470
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1471
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1472
      } else {
1473
        if (pTrans->failedTimes > 6) sendRsp = true;
81,851!
1474
      }
1475
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
81,851✔
1476
    }
1477
  }
1478

1479
  if (!sendRsp) {
223,643✔
1480
    return;
123,195✔
1481
  } else {
1482
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
100,448!
1483
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1484
  }
1485

1486
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
100,448!
1487
  taosWLockLatch(&pTrans->lockRpcArray);
100,448✔
1488
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
100,448✔
1489
  if (size <= 0) {
100,448✔
1490
    taosWUnLockLatch(&pTrans->lockRpcArray);
69,923✔
1491
    return;
69,923✔
1492
  }
1493

1494
  for (int32_t i = 0; i < size; ++i) {
61,050✔
1495
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
30,525✔
1496
    if (pInfo->handle != NULL) {
30,525✔
1497
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
29,469!
1498
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
×
1499
      }
1500
      if (code == TSDB_CODE_SYN_TIMEOUT) {
29,469!
1501
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1502
      }
1503

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

1510
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
29,469✔
1511

1512
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
29,469✔
1513
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,000!
1514
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,000✔
1515
        if (pDb != NULL) {
4,000!
1516
          for (int32_t j = 0; j < 12; j++) {
4,801✔
1517
            bool ready = mndIsDbReady(pMnode, pDb);
4,799✔
1518
            if (!ready) {
4,799✔
1519
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
801!
1520
              taosMsleep(1000);
801✔
1521
            } else {
1522
              break;
3,998✔
1523
            }
1524
          }
1525
        }
1526
        mndReleaseDb(pMnode, pDb);
4,000✔
1527
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
25,469✔
1528
        void   *pCont = NULL;
6,909✔
1529
        int32_t contLen = 0;
6,909✔
1530
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
6,909✔
1531
          mndTransSetRpcRsp(pTrans, pCont, contLen);
6,907✔
1532
        }
1533
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
18,560✔
1534
        void   *pCont = NULL;
10✔
1535
        int32_t contLen = 0;
10✔
1536
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
10!
1537
          mndTransSetRpcRsp(pTrans, pCont, contLen);
10✔
1538
        }
1539
      } else if (pTrans->originRpcType == TDMT_MND_DROP_DNODE) {
18,550✔
1540
        int32_t code = mndRefreshUserIpWhiteList(pMnode);
23✔
1541
        if (code != 0) {
23!
1542
          mWarn("failed to refresh user ip white list since %s", tstrerror(code));
×
1543
        }
1544
      }
1545

1546
      if (pTrans->rpcRspLen != 0) {
29,469✔
1547
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
17,541✔
1548
        if (rpcCont != NULL) {
17,541!
1549
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
17,541✔
1550
          rspMsg.pCont = rpcCont;
17,541✔
1551
          rspMsg.contLen = pTrans->rpcRspLen;
17,541✔
1552
        }
1553
      }
1554

1555
      tmsgSendRsp(&rspMsg);
29,469✔
1556

1557
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
29,469!
1558
            mndTransStr(pTrans->stage), pInfo->ahandle);
1559
    }
1560
  }
1561
  taosArrayClear(pTrans->pRpcArray);
30,525✔
1562
  taosWUnLockLatch(&pTrans->lockRpcArray);
30,525✔
1563
}
1564

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

1584
  SArray *pArray = NULL;
58,290✔
1585
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
58,290✔
1586
    pArray = pTrans->redoActions;
58,282✔
1587
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
8!
1588
    pArray = pTrans->undoActions;
8✔
1589
  } else {
1590
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1591
    goto _OVER;
×
1592
  }
1593

1594
  if (pArray == NULL) {
58,290!
1595
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1596
    goto _OVER;
×
1597
  }
1598

1599
  int32_t actionNum = taosArrayGetSize(pArray);
58,290✔
1600
  if (action < 0 || action >= actionNum) {
58,290!
1601
    mError("trans:%d, invalid action:%d", transId, action);
×
1602
    goto _OVER;
×
1603
  }
1604

1605
  STransAction *pAction = taosArrayGet(pArray, action);
58,290✔
1606
  if (pAction != NULL) {
58,290!
1607
    if (pAction->msgSent) {
58,290!
1608
      pAction->msgReceived = 1;
58,290✔
1609
      pAction->errCode = pRsp->code;
58,290✔
1610
      pAction->endTime = taosGetTimestampMs();
58,290✔
1611

1612
      // pTrans->lastErrorNo = pRsp->code;
1613
      mndSetTransLastAction(pTrans, pAction);
58,290✔
1614

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

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

1628
  mInfo("trans:%d, execute transaction in process response", pTrans->id);
58,290!
1629
  mndTransExecute(pMnode, pTrans, true);
58,290✔
1630

1631
_OVER:
58,290✔
1632
  mndReleaseTrans(pMnode, pTrans);
58,290✔
1633
  TAOS_RETURN(code);
58,290✔
1634
}
1635

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

1651
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
2✔
1652
  int32_t numOfActions = taosArrayGetSize(pArray);
2✔
1653

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

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

1666
    mndTransResetAction(pMnode, pTrans, pAction);
2✔
1667
    mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage), pAction->id,
2!
1668
          pAction->errCode, pAction->startTime);
1669
  }
1670
}
2✔
1671

1672
// execute in sync context
1673
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
462,248✔
1674
  if (pAction->rawWritten) return 0;
462,248✔
1675
  if (topHalf) {
256,556✔
1676
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
16✔
1677
  }
1678

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

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

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

1703
  TAOS_RETURN(code);
256,540✔
1704
}
1705

1706
// execute in trans context
1707
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
272,115✔
1708
                                     bool notSend) {
1709
  if (pAction->msgSent) return 0;
272,115✔
1710
  if (mndCannotExecuteTrans(pMnode, topHalf)) {
95,274✔
1711
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
36,956✔
1712
  }
1713

1714
  if (notSend) {
58,318✔
1715
    mInfo("trans:%d, action:%d skip to execute msg action", pTrans->id, pAction->id);
9!
1716
    return 0;
9✔
1717
  }
1718

1719
#ifndef TD_ASTRA_32
1720
  int64_t signature = pTrans->id;
58,309✔
1721
  signature = (signature << 32);
58,309✔
1722
  signature += pAction->id;
58,309✔
1723

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

1739
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
58,309✔
1740

1741
  char    detail[1024] = {0};
58,309✔
1742
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
58,309!
1743
                          pAction->epSet.numOfEps, pAction->epSet.inUse);
58,309✔
1744
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
124,200✔
1745
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
65,891✔
1746
                     pAction->epSet.eps[i].port);
65,891✔
1747
  }
1748

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

1758
    mndSetTransLastAction(pTrans, pAction);
58,308✔
1759
  } else {
1760
    pAction->msgSent = 0;
1✔
1761
    pAction->msgReceived = 0;
1✔
1762
    pAction->errCode = (terrno != 0) ? terrno : code;
1!
1763
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
1!
1764
           detail);
1765

1766
    mndSetTransLastAction(pTrans, pAction);
1✔
1767
  }
1768

1769
  TAOS_RETURN(code);
58,309✔
1770
}
1771

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

1778
  mndSetTransLastAction(pTrans, pAction);
×
1779
  return 0;
×
1780
}
1781

1782
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf,
734,363✔
1783
                                        bool notSend) {
1784
  if (pAction->actionType == TRANS_ACTION_RAW) {
734,363✔
1785
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
462,248✔
1786
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
272,115!
1787
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf, notSend);
272,115✔
1788
  } else {
1789
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1790
  }
1791
}
1792

1793
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
203,757✔
1794
  int32_t numOfActions = taosArrayGetSize(pArray);
203,757✔
1795
  int32_t code = 0;
203,757✔
1796

1797
  for (int32_t action = 0; action < numOfActions; ++action) {
845,529✔
1798
    STransAction *pAction = taosArrayGet(pArray, action);
671,626✔
1799
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, notSend);
671,626✔
1800
    if (code != 0) {
671,626✔
1801
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
29,854!
1802
            numOfActions);
1803
      break;
29,854✔
1804
    }
1805
  }
1806

1807
  return code;
203,757✔
1808
}
1809

1810
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf, bool notSend) {
247,557✔
1811
  int32_t numOfActions = taosArrayGetSize(pArray);
247,557✔
1812
  int32_t code = 0;
247,557✔
1813
  if (numOfActions == 0) return 0;
247,557✔
1814

1815
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf, notSend)) != 0) {
203,757✔
1816
    return code;
29,854✔
1817
  }
1818

1819
  int32_t       numOfExecuted = 0;
173,903✔
1820
  int32_t       errCode = 0;
173,903✔
1821
  STransAction *pErrAction = NULL;
173,903✔
1822
  for (int32_t action = 0; action < numOfActions; ++action) {
815,675✔
1823
    STransAction *pAction = taosArrayGet(pArray, action);
641,772✔
1824
    if (pAction->msgReceived || pAction->rawWritten) {
641,772✔
1825
      numOfExecuted++;
534,538✔
1826
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
534,538✔
1827
        errCode = pAction->errCode;
3✔
1828
        pErrAction = pAction;
3✔
1829
      }
1830
    } else {
1831
      pErrAction = pAction;
107,234✔
1832
    }
1833
  }
1834

1835
  mndSetTransLastAction(pTrans, pErrAction);
173,903✔
1836

1837
  if (numOfExecuted == numOfActions) {
173,903✔
1838
    if (errCode == 0) {
124,224✔
1839
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
124,222!
1840
      return 0;
124,222✔
1841
    } else {
1842
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
2!
1843
      mndTransResetActions(pMnode, pTrans, pArray);
2✔
1844
      terrno = errCode;
2✔
1845
      return errCode;
2✔
1846
    }
1847
  } else {
1848
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
49,679!
1849

1850
    for (int32_t action = 0; action < numOfActions; ++action) {
214,214✔
1851
      STransAction *pAction = taosArrayGet(pArray, action);
164,535✔
1852
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x", pTrans->id,
164,535✔
1853
             mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived, pAction->errCode,
1854
             pAction->acceptableCode, pAction->retryCode);
1855
      if (pAction->msgSent) {
164,535✔
1856
        bool reset = false;
164,526✔
1857
        if (pAction->msgReceived) {
164,526✔
1858
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) reset = true;
57,301✔
1859
        } else {
1860
          int64_t timestamp = taosGetTimestampMs();
107,225✔
1861
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
107,225!
1862
        }
1863
        if (reset) {
164,526✔
1864
          mndTransResetAction(pMnode, pTrans, pAction);
1✔
1865
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
1!
1866
                pAction->id, pAction->errCode, pAction->startTime);
1867
        }
1868
      }
1869
    }
1870
    return TSDB_CODE_ACTION_IN_PROGRESS;
49,679✔
1871
  }
1872
}
1873

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

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

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

1901
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
18,864✔
1902
  int32_t code = 0;
18,864✔
1903
  int32_t numOfActions = taosArrayGetSize(pActions);
18,864✔
1904
  if (numOfActions == 0) return code;
18,864✔
1905

1906
  if (pTrans->actionPos >= numOfActions) {
18,862✔
1907
    return code;
1,456✔
1908
  }
1909

1910
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
17,406!
1911
        pTrans->actionPos, mndTransStr(pTrans->stage));
1912

1913
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
27,283✔
1914
    STransAction *pAction = taosArrayGet(pActions, action);
26,072✔
1915

1916
    if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL && pAction->groupId > 0) {
26,072✔
1917
      code = TSDB_CODE_ACTION_IN_PROGRESS;
15✔
1918
      break;
16,195✔
1919
    }
1920

1921
    mInfo("trans:%d, current action:%d, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, msgReceived:%d", pTrans->id,
26,057!
1922
          pTrans->actionPos, mndTransStr(pAction->stage), pAction->actionType, pAction->msgSent, pAction->msgReceived);
1923

1924
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
26,057✔
1925
    if (code == 0) {
26,057✔
1926
      if (pAction->msgSent) {
21,860✔
1927
        bool reset = false;
17,727✔
1928
        if (pAction->msgReceived) {
17,727✔
1929
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
7,945!
1930
            code = pAction->errCode;
4,314✔
1931
            reset = true;
4,314✔
1932
          } else {
1933
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), pAction->id);
3,631!
1934
          }
1935
        } else {
1936
          int64_t timestamp = taosGetTimestampMs();
9,782✔
1937
          if (timestamp - pAction->startTime > TRANS_ACTION_TIMEOUT) reset = true;
9,782!
1938
          code = TSDB_CODE_ACTION_IN_PROGRESS;
9,782✔
1939
        }
1940
        if (reset) {
17,727✔
1941
          mndTransResetAction(pMnode, pTrans, pAction);
4,314✔
1942
          mInfo("trans:%d, %s:%d reset, errCode:%d, startTime:%" PRId64, pTrans->id, mndTransStr(pAction->stage),
4,314!
1943
                pAction->id, pAction->errCode, pAction->startTime);
1944
        }
1945
      } else if (pAction->rawWritten) {
4,133!
1946
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
4,133!
1947
          code = pAction->errCode;
×
1948
        } else {
1949
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
4,133!
1950
        }
1951
      } else {
1952
      }
1953
    }
1954

1955
    if (code == 0) {
26,057✔
1956
      pTrans->failedTimes = 0;
7,764✔
1957
    }
1958
    mndSetTransLastAction(pTrans, pAction);
26,057✔
1959
    if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH || code == TSDB_CODE_ACTION_IN_PROGRESS) {
26,057✔
1960
      mInfo(
13,978!
1961
          "trans:%d, not able to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1962
          "msgReceived:%d",
1963
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1964
          pAction->msgSent, pAction->msgReceived);
1965
    } else if (code != 0) {
12,079✔
1966
      mError(
4,315!
1967
          "trans:%d, failed to execute current action:%d since %s, stage:%s, actionType(1:msg,2:log):%d, msgSent:%d, "
1968
          "msgReceived:%d",
1969
          pTrans->id, pTrans->actionPos, tstrerror(code), mndTransStr(pAction->stage), pAction->actionType,
1970
          pAction->msgSent, pAction->msgReceived);
1971
    }
1972

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

1982
    if (code == 0) {
19,662✔
1983
      pTrans->code = 0;
5,565✔
1984
      pTrans->actionPos++;
5,565✔
1985
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
5,565!
1986
            pAction->id);
1987
      (void)taosThreadMutexUnlock(&pTrans->mutex);
5,565✔
1988
      code = mndTransSync(pMnode, pTrans);
5,565✔
1989
      (void)taosThreadMutexLock(&pTrans->mutex);
5,565✔
1990
      if (code != 0) {
5,565!
1991
        pTrans->actionPos--;
×
1992
        pTrans->code = terrno;
×
1993
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1994
               mndTransStr(pAction->stage), pAction->id, terrstr());
1995
        break;
×
1996
      }
1997
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
14,097✔
1998
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
9,782!
1999
      break;
9,782✔
2000
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
4,315!
2001
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
5✔
2002
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
4,312!
2003
            code, tstrerror(code));
2004
      pTrans->lastErrorNo = code;
4,312✔
2005
      taosMsleep(300);
4,312✔
2006
      action--;
4,312✔
2007
      continue;
4,312✔
2008
    } else {
2009
      terrno = code;
3✔
2010
      pTrans->lastErrorNo = code;
3✔
2011
      pTrans->code = code;
3✔
2012
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
3!
2013
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
2014
      break;
3✔
2015
    }
2016
  }
2017

2018
  return code;
17,406✔
2019
}
2020

2021
static int32_t mndTransExecuteActionsSerialGroup(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf,
8,889✔
2022
                                                 int32_t groupId, int32_t currentGroup, int32_t groupCount,
2023
                                                 bool notSend, SHashObj *pHash) {
2024
  int32_t code = 0;
8,889✔
2025
  int32_t numOfActions = taosArrayGetSize(pActions);
8,889✔
2026
  if (numOfActions == 0) return code;
8,889!
2027

2028
  if (groupId <= 0) {
8,889!
2029
    mError("trans:%d, failed to execute action in serail group, %d", pTrans->id, groupId);
×
2030
    return TSDB_CODE_INTERNAL_ERROR;
×
2031
  }
2032

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

2039
  if (*actionPos >= numOfActions) {
8,889✔
2040
    mError("trans:%d, failed to execute action in serail group, actionPos:%d >= numOfActions:%d at group %d",
1,519!
2041
           pTrans->id, *actionPos, numOfActions, groupId);
2042
    return TSDB_CODE_INTERNAL_ERROR;
1,519✔
2043
  }
2044

2045
  for (int32_t action = *actionPos; action < numOfActions; ++action) {
8,477✔
2046
    STransAction **ppAction = taosArrayGet(pActions, action);
7,993✔
2047
    STransAction  *pAction = *ppAction;
7,993✔
2048

2049
    if (notSend && !pAction->msgSent) {
7,993✔
2050
      mInfo(
1,068!
2051
          "trans:%d, %s:%d (%d/%d at group %d) skip to execute, curent state(actionType(1:msg,2:log):%d, "
2052
          "msgSent:%d, msgReceived:%d), transaction(action pos:%d)",
2053
          pTrans->id, mndTransStr(pTrans->stage), pAction->id, action, numOfActions, groupId, pAction->actionType,
2054
          pAction->msgSent, pAction->msgReceived, pTrans->actionPos);
2055
      code = TSDB_CODE_ACTION_IN_PROGRESS;
1,068✔
2056
      break;
6,886✔
2057
    }
2058

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

2065
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf, false);
6,925✔
2066
    if (code == 0) {
6,925✔
2067
      if (pAction->msgSent) {
4,003✔
2068
        if (pAction->msgReceived) {
3,537✔
2069
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
985!
2070
            code = pAction->errCode;
80✔
2071
            mndTransResetAction(pMnode, pTrans, pAction);
80✔
2072
          } else {
2073
            mInfo("trans:%d, %s:%d (%d/%d at group %d) suceed to exeute", pTrans->id, mndTransStr(pAction->stage),
905!
2074
                  pAction->id, action, numOfActions, groupId);
2075
          }
2076
        } else {
2077
          code = TSDB_CODE_ACTION_IN_PROGRESS;
2,552✔
2078
        }
2079
        int8_t *msgSent = taosHashGet(pHash, &pAction->id, sizeof(int32_t));
3,537✔
2080
        if (msgSent != NULL) {
3,537!
2081
          *msgSent = pAction->msgSent;
3,537✔
2082
          mInfo("trans:%d, action:%d, set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
3,537!
2083
        } else {
2084
          mWarn("trans:%d, action:%d, failed set tmp msgSent:%d", pTrans->id, pAction->id, pAction->msgSent);
×
2085
        }
2086
      } else if (pAction->rawWritten) {
466!
2087
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
466!
2088
          code = pAction->errCode;
×
2089
        } else {
2090
          mInfo("trans:%d, %s:%d was already written", pTrans->id, mndTransStr(pAction->stage), pAction->id);
466!
2091
        }
2092
      } else {
2093
      }
2094
    }
2095

2096
    if (code == 0) {
6,925✔
2097
      pTrans->failedTimes = 0;
1,371✔
2098
    }
2099
    mndSetTransLastAction(pTrans, pAction);
6,925✔
2100

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

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

2171
  return code;
7,370✔
2172
}
2173

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

2184
static int32_t mndTransExecuteRedoActionGroup(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
2,512✔
2185
  int32_t total_code = TSDB_CODE_ACTION_IN_PROGRESS;
2,512✔
2186
  int32_t code = 0;
2,512✔
2187
  int32_t successCount = 0;
2,512✔
2188
  int32_t groupCount = taosHashGetSize(pTrans->redoGroupActions);
2,512✔
2189

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

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

2230
  taosHashCleanup(pHash);
2,512✔
2231

2232
  if (successCount == groupCount) {
2,512✔
2233
    total_code = 0;
49✔
2234
  } else {
2235
    mInfo("trans:%d, redo action group is executed, %d of %d groups is executed", pTrans->id, successCount, groupCount);
2,463!
2236
  }
2237

2238
  return total_code;
2,512✔
2239
}
2240

2241
static int32_t mndTransExecuteRedoActionsParallel(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
3,004✔
2242
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
3,004✔
2243
  (void)taosThreadMutexLock(&pTrans->mutex);
3,004✔
2244

2245
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
3,004!
2246
    int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
3,004✔
2247
    if (numOfActions == 0 || pTrans->actionPos >= numOfActions) {
3,004!
2248
      code = 0;
363✔
2249
    } else {
2250
      STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->actionPos);
2,641✔
2251
      if (pAction != NULL && pAction->groupId == -1) {
2,641!
2252
        code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
129✔
2253
      } else {
2254
        code = mndTransExecuteRedoActionGroup(pMnode, pTrans, topHalf, notSend);
2,512✔
2255
        if (code == 0) {
2,512✔
2256
          if (pTrans->actionPos < numOfActions) {
49✔
2257
            code = TSDB_CODE_ACTION_IN_PROGRESS;
36✔
2258
          }
2259
        }
2260
      }
2261
    }
2262
  }
2263

2264
  (void)taosThreadMutexUnlock(&pTrans->mutex);
3,004✔
2265

2266
  return code;
3,004✔
2267
}
2268

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

2279
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
54,808✔
2280
  bool    continueExec = true;
54,808✔
2281
  int32_t code = 0;
54,808✔
2282
  terrno = 0;
54,808✔
2283

2284
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
54,808✔
2285
  if (numOfActions == 0) goto _OVER;
54,808✔
2286

2287
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
20,382!
2288

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

2300
_OVER:
20,382✔
2301
  pTrans->stage = TRN_STAGE_REDO_ACTION;
54,808✔
2302
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
54,808!
2303
  return continueExec;
54,808✔
2304
}
2305

2306
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
168,850✔
2307
  bool    continueExec = true;
168,850✔
2308
  int32_t code = 0;
168,850✔
2309
  terrno = 0;
168,850✔
2310

2311
  if (pTrans->exec == TRN_EXEC_SERIAL) {
168,850✔
2312
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
18,735✔
2313
  } else if (pTrans->exec == TRN_EXEC_PARALLEL) {
150,115✔
2314
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf, notSend);
147,111✔
2315
  } else if (pTrans->exec == TRN_EXEC_GROUP_PARALLEL) {
3,004!
2316
    code = mndTransExecuteRedoActionsParallel(pMnode, pTrans, topHalf, notSend);
3,004✔
2317
  } else {
2318
    code = TSDB_CODE_INTERNAL_ERROR;
×
2319
  }
2320

2321
  if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH && code != TSDB_CODE_ACTION_IN_PROGRESS &&
168,855!
2322
      mndTransIsInSyncContext(topHalf)) {
5✔
2323
    pTrans->lastErrorNo = code;
×
2324
    pTrans->code = code;
×
2325
    mInfo(
×
2326
        "trans:%d, failed to execute, will retry redo action stage in 100 ms , in %s, "
2327
        "continueExec:%d, code:%s",
2328
        pTrans->id, mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2329
    taosMsleep(100);
×
2330
    return true;
×
2331
  } else {
2332
    if (mndCannotExecuteTrans(pMnode, topHalf)) {
168,850✔
2333
      mInfo("trans:%d, cannot continue to execute redo action stage in %s, continueExec:%d, code:%s", pTrans->id,
62,284!
2334
            mndStrExecutionContext(topHalf), continueExec, tstrerror(code));
2335
      return false;
62,284✔
2336
    }
2337
  }
2338
  terrno = code;
106,566✔
2339

2340
  if (code == 0) {
106,566✔
2341
    pTrans->code = 0;
45,664✔
2342
    pTrans->stage = TRN_STAGE_COMMIT;
45,664✔
2343
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
45,664!
2344
    continueExec = true;
45,664✔
2345
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
60,902!
2346
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
60,897!
2347
    continueExec = false;
60,897✔
2348
  } else {
2349
    pTrans->failedTimes++;
5✔
2350
    pTrans->code = terrno;
5✔
2351
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
5✔
2352
      if (pTrans->lastAction != 0) {
2!
2353
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
2✔
2354
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
2!
2355
          if (pTrans->failedTimes < 6) {
×
2356
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
2357
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
2358
            taosMsleep(1000);
×
2359
            continueExec = true;
×
2360
            return true;
×
2361
          }
2362
        }
2363
      }
2364

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

2376
  return continueExec;
106,566✔
2377
}
2378

2379
// execute in trans context
2380
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
45,665✔
2381
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
45,665✔
2382

2383
  bool    continueExec = true;
45,664✔
2384
  int32_t code = mndTransCommit(pMnode, pTrans);
45,664✔
2385

2386
  if (code == 0) {
45,664✔
2387
    pTrans->code = 0;
45,653✔
2388
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
45,653✔
2389
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
45,653!
2390
    continueExec = true;
45,653✔
2391
  } else {
2392
    pTrans->code = terrno;
11✔
2393
    pTrans->failedTimes++;
11✔
2394
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
11!
2395
    continueExec = false;
11✔
2396
  }
2397

2398
  return continueExec;
45,664✔
2399
}
2400

2401
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
100,432✔
2402
  bool    continueExec = true;
100,432✔
2403
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
100,432✔
2404

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

2422
  return continueExec;
100,432✔
2423
}
2424

2425
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
14✔
2426
  bool    continueExec = true;
14✔
2427
  int32_t code = 0;
14✔
2428

2429
  if (pTrans->exec == TRN_EXEC_SERIAL) {
14!
2430
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
2431
  } else {
2432
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf, notSend);
14✔
2433
  }
2434

2435
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
14✔
2436
  terrno = code;
12✔
2437

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

2451
  return continueExec;
12✔
2452
}
2453

2454
// in trans context
2455
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
2✔
2456
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
2!
2457

2458
  bool    continueExec = true;
2✔
2459
  int32_t code = mndTransRollback(pMnode, pTrans);
2✔
2460

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

2470
  return continueExec;
2✔
2471
}
2472

2473
// excute in trans context
2474
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
2✔
2475
  if (mndCannotExecuteTrans(pMnode, topHalf)) return false;
2!
2476

2477
  bool    continueExec = true;
2✔
2478
  int32_t code = mndTransPreFinish(pMnode, pTrans);
2✔
2479

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

2490
  return continueExec;
2✔
2491
}
2492

2493
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
100,435✔
2494
  bool continueExec = false;
100,435✔
2495
  if (topHalf) return continueExec;
100,435✔
2496

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

2504
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
54,780✔
2505
  if (code != 0) {
54,780!
2506
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
2507
  }
2508

2509
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
54,780!
2510
        pTrans->failedTimes, pTrans->createdTime);
2511
  return continueExec;
54,780✔
2512
}
2513

2514
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf, bool notSend) {
223,643✔
2515
  bool continueExec = true;
223,643✔
2516

2517
  while (continueExec) {
639,043✔
2518
    mInfo("trans:%d, continue to execute stage:%s in %s, createTime:%" PRId64, pTrans->id,
415,400!
2519
          mndTransStr(pTrans->stage), mndStrExecutionContext(topHalf), pTrans->createdTime);
2520
    pTrans->lastExecTime = taosGetTimestampMs();
415,400✔
2521
    switch (pTrans->stage) {
415,400!
2522
      case TRN_STAGE_PREPARE:
×
2523
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
2524
        break;
×
2525
      case TRN_STAGE_REDO_ACTION:
168,850✔
2526
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf, notSend);
168,850✔
2527
        break;
168,850✔
2528
      case TRN_STAGE_COMMIT:
45,665✔
2529
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
45,665✔
2530
        break;
45,665✔
2531
      case TRN_STAGE_COMMIT_ACTION:
100,432✔
2532
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
100,432✔
2533
        break;
100,432✔
2534
      case TRN_STAGE_ROLLBACK:
2✔
2535
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
2✔
2536
        break;
2✔
2537
      case TRN_STAGE_UNDO_ACTION:
14✔
2538
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf, notSend);
14✔
2539
        break;
14✔
2540
      case TRN_STAGE_PRE_FINISH:
2✔
2541
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
2✔
2542
        break;
2✔
2543
      case TRN_STAGE_FINISH:
100,435✔
2544
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
100,435✔
2545
        break;
100,435✔
2546
      default:
×
2547
        continueExec = false;
×
2548
        break;
×
2549
    }
2550
  }
2551

2552
  mndTransSendRpcRsp(pMnode, pTrans);
223,643✔
2553
}
223,643✔
2554

2555
// start trans, pullup, receive rsp, kill
2556
void mndTransExecute(SMnode *pMnode, STrans *pTrans, bool notSend) {
106,687✔
2557
  bool topHalf = true;
106,687✔
2558
  mndTransExecuteImp(pMnode, pTrans, topHalf, notSend);
106,687✔
2559
}
106,687✔
2560

2561
// update trans
2562
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
116,956✔
2563
  bool topHalf = false;
116,956✔
2564
  mndTransExecuteImp(pMnode, pTrans, topHalf, false);
116,956✔
2565
}
116,956✔
2566

2567
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
28,114✔
2568
  mTrace("start to process trans timer");
28,114✔
2569
  mndTransPullup(pReq->info.node);
28,114✔
2570
  return 0;
28,114✔
2571
}
2572

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

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

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

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

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

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

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

2634
  code = mndKillTrans(pMnode, pTrans);
×
2635

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

2641
  mndReleaseTrans(pMnode, pTrans);
1✔
2642
  TAOS_RETURN(code);
1✔
2643
}
2644

2645
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
69✔
2646

2647
void mndTransPullup(SMnode *pMnode) {
28,617✔
2648
  SSdb   *pSdb = pMnode->pSdb;
28,617✔
2649
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
28,617✔
2650
  if (pArray == NULL) return;
28,617!
2651

2652
  void *pIter = NULL;
28,617✔
2653
  while (1) {
2,735✔
2654
    STrans *pTrans = NULL;
31,352✔
2655
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
31,352✔
2656
    if (pIter == NULL) break;
31,352✔
2657
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
5,470!
2658
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
2659
    }
2660
    sdbRelease(pSdb, pTrans);
2,735✔
2661
  }
2662

2663
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
28,617✔
2664

2665
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
31,352✔
2666
    int32_t *pTransId = taosArrayGet(pArray, i);
2,735✔
2667
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
2,735✔
2668
    if (pTrans != NULL) {
2,735!
2669
      mInfo("trans:%d, execute transaction in trans pullup", pTrans->id);
2,735!
2670
      mndTransExecute(pMnode, pTrans, false);
2,735✔
2671
    }
2672
    mndReleaseTrans(pMnode, pTrans);
2,735✔
2673
  }
2674
  taosArrayDestroy(pArray);
28,617✔
2675
}
2676

2677
static char *formatTimestamp(char *buf, int64_t val, int precision) {
38,736✔
2678
  time_t tt;
2679
  if (precision == TSDB_TIME_PRECISION_MICRO) {
38,736!
2680
    tt = (time_t)(val / 1000000);
×
2681
  }
2682
  if (precision == TSDB_TIME_PRECISION_NANO) {
38,736!
2683
    tt = (time_t)(val / 1000000000);
×
2684
  } else {
2685
    tt = (time_t)(val / 1000);
38,736✔
2686
  }
2687

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

2695
  if (precision == TSDB_TIME_PRECISION_MICRO) {
38,736!
2696
    sprintf(buf + pos, ".%06d", (int)(val % 1000000));
×
2697
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
38,736!
2698
    sprintf(buf + pos, ".%09d", (int)(val % 1000000000));
×
2699
  } else {
2700
    sprintf(buf + pos, ".%03d", (int)(val % 1000));
38,736✔
2701
  }
2702

2703
  return buf;
38,736✔
2704
}
2705

2706
static void mndTransLogAction(STrans *pTrans) {
769✔
2707
  char    detail[512] = {0};
769✔
2708
  int32_t len = 0;
769✔
2709
  int32_t index = 0;
769✔
2710

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

2722
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
769!
2723
    for (int32_t i = 0; i < taosArrayGetSize(pTrans->redoActions); ++i, ++index) {
26,247✔
2724
      len = 0;
25,478✔
2725
      STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
25,478✔
2726
      if (pAction->actionType == TRANS_ACTION_MSG) {
25,478✔
2727
        char bufStart[40] = {0};
19,345✔
2728
        (void)formatTimestamp(bufStart, pAction->startTime, TSDB_TIME_PRECISION_MILLI);
19,345✔
2729

2730
        char endStart[40] = {0};
19,345✔
2731
        (void)formatTimestamp(endStart, pAction->endTime, TSDB_TIME_PRECISION_MILLI);
19,345✔
2732
        len += snprintf(detail + len, sizeof(detail) - len,
38,690!
2733
                        "action:%d, %s:%d msgType:%s,"
2734
                        "sent:%d, received:%d, startTime:%s, endTime:%s, ",
2735
                        index, mndTransStr(pAction->stage), pAction->id, TMSG_INFO(pAction->msgType), pAction->msgSent,
19,345✔
2736
                        pAction->msgReceived, bufStart, endStart);
19,345✔
2737

2738
        SEpSet epset = pAction->epSet;
19,345✔
2739
        if (epset.numOfEps > 0) {
19,345!
2740
          len += snprintf(detail + len, sizeof(detail) - len, "numOfEps:%d inUse:%d ", epset.numOfEps, epset.inUse);
19,345✔
2741
          for (int32_t i = 0; i < epset.numOfEps; ++i) {
43,422✔
2742
            len +=
24,077✔
2743
                snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
24,077✔
2744
          }
2745
        }
2746

2747
        len += snprintf(detail + len, sizeof(detail) - len, ", errCode:0x%x(%s)\n", pAction->errCode & 0xFFFF,
19,345✔
2748
                        tstrerror(pAction->errCode));
2749
      } else {
2750
        len += snprintf(detail + len, sizeof(detail) - len, "action:%d, %s:%d sdbType:%s, sdbStatus:%s, written:%d\n",
6,133✔
2751
                        index, mndTransStr(pAction->stage), pAction->id, sdbTableName(pAction->pRaw->type),
6,133✔
2752
                        sdbStatusName(pAction->pRaw->status), pAction->rawWritten);
6,133✔
2753
      }
2754
      mDebug("trans:%d, show tran action, detail:%s", pTrans->id, detail);
25,478!
2755
    }
2756
  }
2757

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

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

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

2794
  while (numOfRows < rows) {
1,585!
2795
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
1,585✔
2796
    if (pShow->pIter == NULL) break;
1,585✔
2797

2798
    cols = 0;
769✔
2799

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

2803
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
769✔
2804
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
769!
2805
                        _OVER);
2806

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

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

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

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

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

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

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

2845
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
769✔
2846
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
769!
2847
                        _OVER);
2848

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

2865
    mndTransLogAction(pTrans);
769✔
2866

2867
    numOfRows++;
769✔
2868
    sdbRelease(pSdb, pTrans);
769✔
2869
  }
2870

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

2877
static int32_t mndShowTransCommonColumns(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction,
352✔
2878
                                         int32_t transactionId, int32_t curActionId, int32_t numOfRows, int32_t *cols) {
2879
  int32_t code = 0;
352✔
2880
  int32_t lino = 0;
352✔
2881
  int32_t len = 0;
352✔
2882

2883
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, (*cols)++);
352✔
2884
  TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&transactionId, false), &lino, _OVER);
352!
2885

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

2903
static void mndShowTransAction(SShowObj *pShow, SSDataBlock *pBlock, STransAction *pAction, int32_t transactionId,
352✔
2904
                               int32_t curActionId, int32_t rows, int32_t numOfRows) {
2905
  int32_t code = 0;
352✔
2906
  int32_t lino = 0;
352✔
2907
  int32_t len = 0;
352✔
2908
  int32_t cols = 0;
352✔
2909

2910
  cols = 0;
352✔
2911

2912
  if (mndShowTransCommonColumns(pShow, pBlock, pAction, transactionId, curActionId, numOfRows, &cols) != 0) return;
352!
2913

2914
  if (pAction->actionType == TRANS_ACTION_MSG) {
352✔
2915
    int32_t len = 0;
288✔
2916

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

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

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

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

2960
  } else {
2961
    int32_t len = 0;
64✔
2962

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

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

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

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

3000
_OVER:
352✔
3001
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
352!
3002
}
3003

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

3020
typedef struct STransDetailIter {
3021
  void     *pIter;
3022
  STrans   *pTrans;
3023
  ETrnStage stage;
3024
  int32_t   num;
3025
} STransDetailIter;
3026

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

3032
  for (int32_t i = start; i < actionNum; ++i) {
354✔
3033
    STransAction *pAction = taosArrayGet(pShowIter->pTrans->redoActions, i);
352✔
3034
    mndShowTransAction(pShow, pBlock, pAction, pShowIter->pTrans->id, pShowIter->pTrans->lastAction, rows, *numOfRows);
352✔
3035
    (*numOfRows)++;
352✔
3036
    if (*numOfRows >= rows) break;
352✔
3037
  }
3038

3039
  if (*numOfRows == end) {
4✔
3040
    sdbRelease(pSdb, pShowIter->pTrans);
2✔
3041
    pShowIter->pTrans = NULL;
2✔
3042
    pShowIter->num = 0;
2✔
3043
  } else {
3044
    pShowIter->pTrans = pShowIter->pTrans;
2✔
3045
    pShowIter->stage = pShowIter->pTrans->stage;
2✔
3046
    pShowIter->num += (*numOfRows);
2✔
3047
  }
3048
}
4✔
3049

3050
static int32_t mndRetrieveTransDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
6✔
3051
  SMnode *pMnode = pReq->info.node;
6✔
3052
  SSdb   *pSdb = pMnode->pSdb;
6✔
3053
  int32_t numOfRows = 0;
6✔
3054

3055
  int32_t code = 0;
6✔
3056
  int32_t lino = 0;
6✔
3057

3058
  mInfo("start to mndRetrieveTransDetail, rows:%d, pShow->numOfRows:%d, pShow->pIter:%p", rows, pShow->numOfRows,
6!
3059
        pShow->pIter);
3060

3061
  if (pShow->pIter == NULL) {
6✔
3062
    pShow->pIter = taosMemoryMalloc(sizeof(STransDetailIter));
2!
3063
    if (pShow->pIter == NULL) {
2!
3064
      mError("failed to malloc for pShow->pIter");
×
3065
      return 0;
×
3066
    }
3067
    memset(pShow->pIter, 0, sizeof(STransDetailIter));
2✔
3068
  }
3069

3070
  STransDetailIter *pShowIter = (STransDetailIter *)pShow->pIter;
6✔
3071

3072
  while (numOfRows < rows) {
6!
3073
    if (pShowIter->pTrans == NULL) {
6✔
3074
      pShowIter->pIter = sdbFetch(pSdb, SDB_TRANS, pShowIter->pIter, (void **)&(pShowIter->pTrans));
4✔
3075
      mDebug("retrieve trans detail from fetch, pShow->pIter:%p, pTrans:%p", pShowIter->pIter, pShowIter->pTrans);
4!
3076
      if (pShowIter->pIter == NULL) break;
4✔
3077
      mInfo("retrieve trans detail from fetch, id:%d, trans stage:%d, IterNum:%d", pShowIter->pTrans->id,
2!
3078
            pShowIter->pTrans->stage, pShowIter->num);
3079

3080
      SArray *pActions = mndTransGetAction(pShowIter->pTrans, pShowIter->pTrans->stage);
2✔
3081

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

3089
      mndTransShowActions(pSdb, pShowIter, pShow, pBlock, rows, &numOfRows, pActions,
4✔
3090
                          taosArrayGetSize(pActions) - pShowIter->num, pShowIter->num);
2✔
3091
      break;
2✔
3092
    }
3093
  }
3094

3095
_OVER:
×
3096
  pShow->numOfRows += numOfRows;
6✔
3097

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

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