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

taosdata / TDengine / #3541

26 Nov 2024 03:56AM UTC coverage: 60.776% (-0.07%) from 60.846%
#3541

push

travis-ci

web-flow
Merge pull request #28920 from taosdata/fix/TD-33008-3.0

fix(query)[TD-33008]. fix error handling in tsdbCacheRead

120076 of 252763 branches covered (47.51%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

1395 existing lines in 154 files now uncovered.

200995 of 275526 relevant lines covered (72.95%)

19612328.37 hits per line

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

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

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

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

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

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

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

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

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

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

67
static int32_t tsMaxTransId = 0;
68

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

80
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransTimer);
1,996✔
81
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
1,996✔
82

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

88
void mndCleanupTrans(SMnode *pMnode) {}
1,995✔
89

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

94
  for (int32_t i = 0; i < actionNum; ++i) {
1,523,844✔
95
    STransAction *pAction = taosArrayGet(pArray, i);
930,236✔
96
    if (pAction->actionType == TRANS_ACTION_RAW) {
930,236✔
97
      rawDataLen += (sizeof(STransAction) + sdbGetRawTotalSize(pAction->pRaw));
428,874✔
98
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
501,362!
99
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
501,362✔
100
    } else {
101
      // empty
102
    }
103
    rawDataLen += sizeof(int8_t);
930,236✔
104
  }
105

106
  return rawDataLen;
593,608✔
107
}
108

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

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

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

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

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

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

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

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

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

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

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

204
  SDB_SET_BINARY(pRaw, dataPos, pTrans->opername, TSDB_TRANS_OPER_LEN, _OVER)
148,402!
205

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

216
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
148,402!
217
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
148,402!
218

219
      terrno = 0;
148,402✔
220

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

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

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

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

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

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

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

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

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

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

317
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
264,616!
318

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

343
  if (sver > TRANS_VER1_NUMBER) {
264,616✔
344
    SDB_GET_INT32(pRaw, dataPos, &prepareActionNum, _OVER)
78,797!
345
  }
346
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, _OVER)
264,616!
347
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, _OVER)
264,616!
348
  SDB_GET_INT32(pRaw, dataPos, &commitActionNum, _OVER)
264,616!
349

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

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

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

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

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

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

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

385
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
264,616!
386

387
  terrno = 0;
264,616✔
388

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

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

403
static const char *mndTransStr(ETrnStage stage) {
1,797,024✔
404
  switch (stage) {
1,797,024!
405
    case TRN_STAGE_PREPARE:
117,816✔
406
      return "prepare";
117,816✔
407
    case TRN_STAGE_REDO_ACTION:
919,094✔
408
      return "redoAction";
919,094✔
409
    case TRN_STAGE_ROLLBACK:
37✔
410
      return "rollback";
37✔
411
    case TRN_STAGE_UNDO_ACTION:
278✔
412
      return "undoAction";
278✔
413
    case TRN_STAGE_COMMIT:
232,359✔
414
      return "commit";
232,359✔
415
    case TRN_STAGE_COMMIT_ACTION:
182,421✔
416
      return "commitAction";
182,421✔
417
    case TRN_STAGE_FINISH:
344,982✔
418
      return "finished";
344,982✔
419
    case TRN_STAGE_PRE_FINISH:
37✔
420
      return "pre-finish";
37✔
421
    default:
×
422
      return "invalid";
×
423
  }
424
}
425

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

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

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

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

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

467
  (void)taosThreadMutexInit(&pTrans->mutex, NULL);
48,928✔
468

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

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

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

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

492
  return 0;
48,928✔
493
}
494

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

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

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

544
  mndTransDropData(pTrans);
156,754✔
545
  return 0;
156,754✔
546
}
547

548
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
236,400✔
549
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
667,838✔
550
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
431,438✔
551
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
431,438✔
552
    pOldAction->rawWritten = pNewAction->rawWritten;
431,438✔
553
    pOldAction->msgSent = pNewAction->msgSent;
431,438✔
554
    pOldAction->msgReceived = pNewAction->msgReceived;
431,438✔
555
    pOldAction->errCode = pNewAction->errCode;
431,438✔
556
  }
557
}
236,400✔
558

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

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

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

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

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

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

594
  return 0;
59,100✔
595
}
596

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

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

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

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

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

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

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

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

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

665
static void mndTransDropActions(SArray *pArray) {
1,239,892✔
666
  int32_t size = taosArrayGetSize(pArray);
1,239,892✔
667
  for (int32_t i = 0; i < size; ++i) {
3,194,830✔
668
    STransAction *pAction = taosArrayGet(pArray, i);
1,954,938✔
669
    if (pAction->actionType == TRANS_ACTION_RAW) {
1,954,938✔
670
      taosMemoryFreeClear(pAction->pRaw);
890,499!
671
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
1,064,439!
672
      taosMemoryFreeClear(pAction->pCont);
1,064,439!
673
    } else {
674
      // nothing
675
    }
676
  }
677

678
  taosArrayDestroy(pArray);
1,239,892✔
679
}
1,239,892✔
680

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

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

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

697
  return 0;
254,391✔
698
}
699

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

844
static void mndTransLogConflict(STrans *pNew, STrans *pTrans, bool conflict, bool *globalConflict) {
25,975✔
845
  if (conflict) {
25,975✔
846
    mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with trans:%d db:%s stb:%s type:%d", pNew->id,
281!
847
           pNew->dbname, pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
848
    *globalConflict = true;
281✔
849
  } else {
850
    mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with trans:%d db:%s stb:%s type:%d", pNew->id, pNew->dbname,
25,694!
851
          pNew->stbname, pNew->conflict, pTrans->id, pTrans->dbname, pTrans->stbname, pTrans->conflict);
852
  }
853
}
25,975✔
854

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

860
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
126,590✔
861

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

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

869
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
23,199✔
870
    if (pNew->conflict == TRN_CONFLICT_DB) {
23,199✔
871
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
3,790!
872
      if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
3,790✔
873
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
2,694✔
874
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
2,694✔
875
      }
876
    }
877
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
23,199✔
878
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
19,399✔
879
      if (pTrans->conflict == TRN_CONFLICT_DB) {
19,399✔
880
        mndTransLogConflict(pNew, pTrans, mndCheckDbConflict(pNew->dbname, pTrans), &conflict);
1,862✔
881
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);
1,862✔
882
      }
883
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
19,399✔
884
        mndTransLogConflict(pNew, pTrans, mndCheckStbConflict(pNew->stbname, pTrans), &conflict);  // for stb
16,863✔
885
      }
886
    }
887

888
//    if (pNew->conflict == TRN_CONFLICT_TOPIC) {
889
//      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
890
//      if (pTrans->conflict == TRN_CONFLICT_TOPIC || pTrans->conflict == TRN_CONFLICT_TOPIC_INSIDE) {
891
//        if (strcasecmp(pNew->dbname, pTrans->dbname) == 0) conflict = true;
892
//      }
893
//    }
894
//    if (pNew->conflict == TRN_CONFLICT_TOPIC_INSIDE) {
895
//      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
896
//      if (pTrans->conflict == TRN_CONFLICT_TOPIC) {
897
//        if (strcasecmp(pNew->dbname, pTrans->dbname) == 0) conflict = true;
898
//      }
899
//      if (pTrans->conflict == TRN_CONFLICT_TOPIC_INSIDE) {
900
//        if (strcasecmp(pNew->dbname, pTrans->dbname) == 0 && strcasecmp(pNew->stbname, pTrans->stbname) == 0)
901
//          conflict = true;
902
//      }
903
//    }
904
    if (pNew->conflict == TRN_CONFLICT_ARBGROUP) {
23,199!
905
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
×
906
      if (pTrans->conflict == TRN_CONFLICT_ARBGROUP) {
×
907
        void* pGidIter = taosHashIterate(pNew->arbGroupIds, NULL);
×
908
        while (pGidIter != NULL) {
×
909
          int32_t groupId = *(int32_t *)pGidIter;
×
910
          if (taosHashGet(pTrans->arbGroupIds, &groupId, sizeof(int32_t)) != NULL) {
×
911
            taosHashCancelIterate(pNew->arbGroupIds, pGidIter);
×
912
            mndTransLogConflict(pNew, pTrans, true, &conflict);
×
913
            break;
×
914
          } else {
915
            mndTransLogConflict(pNew, pTrans, false, &conflict);
×
916
          }
917
          pGidIter = taosHashIterate(pNew->arbGroupIds, pGidIter);
×
918
        }
919
      }
920
    }
921

922
    if (pNew->conflict == TRN_CONFLICT_TSMA) {
23,199!
UNCOV
923
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_TSMA) {
×
924
        mndTransLogConflict(pNew, pTrans, true, &conflict);
×
925
      } else {
UNCOV
926
        mndTransLogConflict(pNew, pTrans, false, &conflict);
×
927
      }
928
    }
929

930
    sdbRelease(pMnode->pSdb, pTrans);
23,199✔
931
  }
932

933
  return conflict;
90,277✔
934
}
935

936
int32_t mndTransCheckConflict(SMnode *pMnode, STrans *pTrans) {
126,590✔
937
  int32_t code = 0;
126,590✔
938
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
126,590✔
939
    if (strlen(pTrans->dbname) == 0 && strlen(pTrans->stbname) == 0) {
80,020!
940
      code = TSDB_CODE_MND_TRANS_CONFLICT;
×
941
      mError("trans:%d, failed to check tran conflict since db not set", pTrans->id);
×
942
      TAOS_RETURN(code);
×
943
    }
944
  }
945

946
  if (mndCheckTransConflict(pMnode, pTrans)) {
126,590✔
947
    code = TSDB_CODE_MND_TRANS_CONFLICT;
319✔
948
    mError("trans:%d, failed to check tran conflict since %s", pTrans->id, tstrerror(code));
319!
949
    TAOS_RETURN(code);
319✔
950
  }
951

952
  TAOS_RETURN(code);
126,271✔
953
}
954

955
int32_t mndTransCheckConflictWithCompact(SMnode *pMnode, STrans *pTrans) {
73✔
956
  int32_t      code = 0;
73✔
957
  void        *pIter = NULL;
73✔
958
  bool         conflict = false;
73✔
959
  SCompactObj *pCompact = NULL;
73✔
960

961
  while (1) {
×
962
    bool thisConflict = false;
73✔
963
    pIter = sdbFetch(pMnode->pSdb, SDB_COMPACT, pIter, (void **)&pCompact);
73✔
964
    if (pIter == NULL) break;
73!
965

966
    if (pTrans->conflict == TRN_CONFLICT_GLOBAL || pTrans->conflict == TRN_CONFLICT_DB ||
×
967
        pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
×
968
      if (strcasecmp(pTrans->dbname, pCompact->dbname) == 0) thisConflict = true;
×
969
    }
970

971
    if (thisConflict) {
×
972
      mError("trans:%d, db:%s stb:%s type:%d, can't execute since conflict with compact:%d db:%s", pTrans->id,
×
973
             pTrans->dbname, pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
974
      conflict = true;
×
975
    } else {
976
      mInfo("trans:%d, db:%s stb:%s type:%d, not conflict with compact:%d db:%s", pTrans->id, pTrans->dbname,
×
977
            pTrans->stbname, pTrans->conflict, pCompact->compactId, pCompact->dbname);
978
    }
979
    sdbRelease(pMnode->pSdb, pCompact);
×
980
  }
981

982
  if (conflict) {
73!
983
    code = TSDB_CODE_MND_TRANS_CONFLICT_COMPACT;
×
984
    mError("trans:%d, failed to check tran conflict with compact since %s", pTrans->id, tstrerror(code));
×
985
    TAOS_RETURN(code);
×
986
  }
987

988
  TAOS_RETURN(code);
73✔
989
}
990

991
static bool mndTransActionsOfSameType(SArray *pActions) {
101,466✔
992
  int32_t size = taosArrayGetSize(pActions);
101,466✔
993
  ETrnAct lastActType = TRANS_ACTION_NULL;
101,466✔
994
  bool    same = true;
101,466✔
995
  for (int32_t i = 0; i < size; ++i) {
291,986✔
996
    STransAction *pAction = taosArrayGet(pActions, i);
190,520✔
997
    if (i > 0) {
190,520✔
998
      if (lastActType != pAction->actionType) {
109,209!
999
        same = false;
×
1000
        break;
×
1001
      }
1002
    }
1003
    lastActType = pAction->actionType;
190,520✔
1004
  }
1005
  return same;
101,466✔
1006
}
1007

1008
static int32_t mndTransCheckParallelActions(SMnode *pMnode, STrans *pTrans) {
44,980✔
1009
  int32_t code = 0;
44,980✔
1010
  if (pTrans->exec == TRN_EXEC_PARALLEL) {
44,980✔
1011
    if (mndTransActionsOfSameType(pTrans->redoActions) == false) {
42,080!
1012
      code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1013
      mError("trans:%d, types of parallel redo actions are not the same", pTrans->id);
×
1014
      TAOS_RETURN(code);
×
1015
    }
1016

1017
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
42,080✔
1018
      if (mndTransActionsOfSameType(pTrans->undoActions) == false) {
14,406!
1019
        code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1020
        mError("trans:%d, types of parallel undo actions are not the same", pTrans->id);
×
1021
        TAOS_RETURN(code);
×
1022
      }
1023
    }
1024
  }
1025

1026
  TAOS_RETURN(code);
44,980✔
1027
}
1028

1029
static int32_t mndTransCheckCommitActions(SMnode *pMnode, STrans *pTrans) {
44,980✔
1030
  int32_t code = 0;
44,980✔
1031
  if (!pTrans->changeless && taosArrayGetSize(pTrans->commitActions) <= 0) {
44,980!
1032
    code = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
×
1033
    mError("trans:%d, commit actions of non-changeless trans are empty", pTrans->id);
×
1034
    TAOS_RETURN(code);
×
1035
  }
1036
  if (mndTransActionsOfSameType(pTrans->commitActions) == false) {
44,980!
1037
    code = TSDB_CODE_MND_TRANS_INVALID_STAGE;
×
1038
    mError("trans:%d, types of commit actions are not the same", pTrans->id);
×
1039
    TAOS_RETURN(code);
×
1040
  }
1041

1042
  TAOS_RETURN(code);
44,980✔
1043
}
1044

1045
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
44,980✔
1046
  int32_t code = 0;
44,980✔
1047
  if (pTrans == NULL) {
44,980!
1048
      return TSDB_CODE_INVALID_PARA;
×
1049
  }
1050

1051
  TAOS_CHECK_RETURN(mndTransCheckConflict(pMnode, pTrans));
44,980!
1052

1053
  TAOS_CHECK_RETURN(mndTransCheckParallelActions(pMnode, pTrans));
44,980!
1054

1055
  TAOS_CHECK_RETURN(mndTransCheckCommitActions(pMnode, pTrans));
44,980!
1056

1057
  mInfo("trans:%d, prepare transaction", pTrans->id);
44,980!
1058
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
44,980✔
1059
    mError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
14!
1060
    sdbWriteLock(pMnode->pSdb, SDB_TRANS);
14✔
1061
    tsMaxTransId = TMAX(pTrans->id, tsMaxTransId);
14✔
1062
    sdbUnLock(pMnode->pSdb, SDB_TRANS);
14✔
1063
    TAOS_RETURN(code);
14✔
1064
  }
1065
  mInfo("trans:%d, prepare finished", pTrans->id);
44,966!
1066

1067
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
44,966✔
1068
  if (pNew == NULL) {
44,966!
1069
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1070
    if (terrno != 0) code = terrno;
×
1071
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, tstrerror(code));
×
1072
    TAOS_RETURN(code);
×
1073
  }
1074

1075
  pNew->pRpcArray = pTrans->pRpcArray;
44,966✔
1076
  pNew->rpcRsp = pTrans->rpcRsp;
44,966✔
1077
  pNew->rpcRspLen = pTrans->rpcRspLen;
44,966✔
1078
  pNew->mTraceId = pTrans->mTraceId;
44,966✔
1079
  pTrans->pRpcArray = NULL;
44,966✔
1080
  pTrans->rpcRsp = NULL;
44,966✔
1081
  pTrans->rpcRspLen = 0;
44,966✔
1082

1083
  mndTransExecute(pMnode, pNew);
44,966✔
1084
  mndReleaseTrans(pMnode, pNew);
44,966✔
1085
  // TDOD change to TAOS_RETURN(code);
1086
  return 0;
44,966✔
1087
}
1088

1089
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
44,976✔
1090
  int32_t code = 0;
44,976✔
1091
  mInfo("trans:%d, commit transaction", pTrans->id);
44,976!
1092
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
44,976✔
1093
    mError("trans:%d, failed to commit since %s", pTrans->id, tstrerror(code));
19!
1094
    TAOS_RETURN(code);
19✔
1095
  }
1096
  mInfo("trans:%d, commit finished", pTrans->id);
44,957!
1097
  TAOS_RETURN(code);
44,957✔
1098
}
1099

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

1111
static int32_t mndTransPreFinish(SMnode *pMnode, STrans *pTrans) {
5✔
1112
  int32_t code = 0;
5✔
1113
  mInfo("trans:%d, pre-finish transaction", pTrans->id);
5!
1114
  if ((code = mndTransSync(pMnode, pTrans)) != 0) {
5!
1115
    mError("trans:%d, failed to pre-finish since %s", pTrans->id, tstrerror(code));
×
1116
    TAOS_RETURN(code);
×
1117
  }
1118
  mInfo("trans:%d, pre-finish finished", pTrans->id);
5!
1119
  TAOS_RETURN(code);
5✔
1120
}
1121

1122
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
258,008✔
1123
  bool    sendRsp = false;
258,008✔
1124
  int32_t code = pTrans->code;
258,008✔
1125

1126
  if (pTrans->stage == TRN_STAGE_FINISH) {
258,008✔
1127
    sendRsp = true;
93,692✔
1128
  }
1129

1130
  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
258,008✔
1131
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
75,206!
1132
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
44!
1133
      sendRsp = true;
44✔
1134
    }
1135
  } else {
1136
    if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
182,802✔
1137
      if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_APP_IS_STARTING ||
122,096!
1138
          code == TSDB_CODE_SYN_PROPOSE_NOT_READY) {
1139
        if (pTrans->failedTimes > 60) sendRsp = true;
×
1140
      } else {
1141
        if (pTrans->failedTimes > 6) sendRsp = true;
122,096✔
1142
      }
1143
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
122,096✔
1144
    }
1145
  }
1146

1147
  if (!sendRsp) {
258,008✔
1148
    return;
164,270✔
1149
  } else {
1150
    mInfo("vgId:1, trans:%d, start to send rsp, stage:%s failedTimes:%d code:0x%x", pTrans->id,
93,738!
1151
          mndTransStr(pTrans->stage), pTrans->failedTimes, code);
1152
  }
1153

1154
  mInfo("vgId:1, trans:%d, start to lock rpc array", pTrans->id);
93,738!
1155
  taosWLockLatch(&pTrans->lockRpcArray);
93,738✔
1156
  int32_t size = taosArrayGetSize(pTrans->pRpcArray);
93,738✔
1157
  if (size <= 0) {
93,738✔
1158
    taosWUnLockLatch(&pTrans->lockRpcArray);
62,060✔
1159
    return;
62,060✔
1160
  }
1161

1162
  for (int32_t i = 0; i < size; ++i) {
63,356✔
1163
    SRpcHandleInfo *pInfo = taosArrayGet(pTrans->pRpcArray, i);
31,678✔
1164
    if (pInfo->handle != NULL) {
31,678✔
1165
      if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
30,080!
1166
        code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
1✔
1167
      }
1168
      if (code == TSDB_CODE_SYN_TIMEOUT) {
30,080!
1169
        code = TSDB_CODE_MND_TRANS_SYNC_TIMEOUT;
×
1170
      }
1171

1172
      if (i != 0 && code == 0) {
30,080!
1173
        code = TSDB_CODE_MNODE_NOT_FOUND;
×
1174
      }
1175
      mInfo("vgId:1, trans:%d, client:%d start to send rsp, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,080!
1176
            mndTransStr(pTrans->stage), pInfo->ahandle);
1177

1178
      SRpcMsg rspMsg = {.code = code, .info = *pInfo};
30,080✔
1179

1180
      if (pTrans->originRpcType == TDMT_MND_CREATE_DB) {
30,080✔
1181
        mInfo("trans:%d, origin msgtype:%s", pTrans->id, TMSG_INFO(pTrans->originRpcType));
4,569!
1182
        SDbObj *pDb = mndAcquireDb(pMnode, pTrans->dbname);
4,569✔
1183
        if (pDb != NULL) {
4,569!
1184
          for (int32_t j = 0; j < 12; j++) {
5,408✔
1185
            bool ready = mndIsDbReady(pMnode, pDb);
5,402✔
1186
            if (!ready) {
5,402✔
1187
              mInfo("trans:%d, db:%s not ready yet, wait %d times", pTrans->id, pTrans->dbname, j);
839!
1188
              taosMsleep(1000);
839✔
1189
            } else {
1190
              break;
4,563✔
1191
            }
1192
          }
1193
        }
1194
        mndReleaseDb(pMnode, pDb);
4,569✔
1195
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_STB) {
25,511✔
1196
        void   *pCont = NULL;
8,894✔
1197
        int32_t contLen = 0;
8,894✔
1198
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
8,894✔
1199
          mndTransSetRpcRsp(pTrans, pCont, contLen);
8,890✔
1200
        }
1201
      } else if (pTrans->originRpcType == TDMT_MND_CREATE_INDEX) {
16,617✔
1202
        void   *pCont = NULL;
494✔
1203
        int32_t contLen = 0;
494✔
1204
        if (0 == mndBuildSMCreateStbRsp(pMnode, pTrans->dbname, pTrans->stbname, &pCont, &contLen)) {
494!
1205
          mndTransSetRpcRsp(pTrans, pCont, contLen);
494✔
1206
        }
1207
      }
1208

1209
      if (pTrans->rpcRspLen != 0) {
30,080✔
1210
        void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
13,805✔
1211
        if (rpcCont != NULL) {
13,805!
1212
          memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
13,805✔
1213
          rspMsg.pCont = rpcCont;
13,805✔
1214
          rspMsg.contLen = pTrans->rpcRspLen;
13,805✔
1215
        }
1216
      }
1217

1218
      tmsgSendRsp(&rspMsg);
30,080✔
1219

1220
      mInfo("vgId:1, trans:%d, client:%d send rsp finished, code:0x%x stage:%s app:%p", pTrans->id, i, code,
30,080!
1221
            mndTransStr(pTrans->stage), pInfo->ahandle);
1222
    }
1223
  }
1224
  taosArrayClear(pTrans->pRpcArray);
31,678✔
1225
  taosWUnLockLatch(&pTrans->lockRpcArray);
31,678✔
1226
}
1227

1228
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
99,807✔
1229
  int32_t code = 0;
99,807✔
1230
  SMnode *pMnode = pRsp->info.node;
99,807✔
1231
  int64_t signature = (int64_t)(pRsp->info.ahandle);
99,807✔
1232
  int32_t transId = (int32_t)(signature >> 32);
99,807✔
1233
  int32_t action = (int32_t)((signature << 32) >> 32);
99,807✔
1234

1235
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
99,807✔
1236
  if (pTrans == NULL) {
99,807!
1237
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1238
    if (terrno != 0) code = terrno;
×
1239
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, tstrerror(code));
×
1240
    goto _OVER;
×
1241
  }
1242

1243
  SArray *pArray = NULL;
99,807✔
1244
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
99,807✔
1245
    pArray = pTrans->redoActions;
99,786✔
1246
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
21!
1247
    pArray = pTrans->undoActions;
21✔
1248
  } else {
1249
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
×
1250
    goto _OVER;
×
1251
  }
1252

1253
  if (pArray == NULL) {
99,807!
1254
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
×
1255
    goto _OVER;
×
1256
  }
1257

1258
  int32_t actionNum = taosArrayGetSize(pArray);
99,807✔
1259
  if (action < 0 || action >= actionNum) {
99,807!
1260
    mError("trans:%d, invalid action:%d", transId, action);
×
1261
    goto _OVER;
×
1262
  }
1263

1264
  STransAction *pAction = taosArrayGet(pArray, action);
99,807✔
1265
  if (pAction != NULL) {
99,807!
1266
    pAction->msgReceived = 1;
99,807✔
1267
    pAction->errCode = pRsp->code;
99,807✔
1268
    pTrans->lastErrorNo = pRsp->code;
99,807✔
1269

1270
    mInfo("trans:%d, %s:%d response is received, received code:0x%x(%s), accept:0x%x(%s) retry:0x%x(%s)", transId,
99,807!
1271
          mndTransStr(pAction->stage), action, pRsp->code, tstrerror(pRsp->code), pAction->acceptableCode,
1272
          tstrerror(pAction->acceptableCode), pAction->retryCode, tstrerror(pAction->retryCode));
1273
  } else {
1274
    mInfo("trans:%d, invalid action, index:%d, code:0x%x", transId, action, pRsp->code);
×
1275
  }
1276

1277
  mndTransExecute(pMnode, pTrans);
99,807✔
1278

1279
_OVER:
99,807✔
1280
  mndReleaseTrans(pMnode, pTrans);
99,807✔
1281
  TAOS_RETURN(code);
99,807✔
1282
}
1283

1284
static void mndTransResetAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
6,383✔
1285
  pAction->rawWritten = 0;
6,383✔
1286
  pAction->msgSent = 0;
6,383✔
1287
  pAction->msgReceived = 0;
6,383✔
1288
  if (pAction->errCode == TSDB_CODE_SYN_NEW_CONFIG_ERROR || pAction->errCode == TSDB_CODE_SYN_INTERNAL_ERROR ||
6,383!
1289
      pAction->errCode == TSDB_CODE_SYN_NOT_LEADER) {
6,383✔
1290
    pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
21✔
1291
    mInfo("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
21!
1292
          pAction->id, pAction->epSet.inUse);
1293
  } else {
1294
    mInfo("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
6,362!
1295
  }
1296
  pAction->errCode = 0;
6,383✔
1297
}
6,383✔
1298

1299
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
6✔
1300
  int32_t numOfActions = taosArrayGetSize(pArray);
6✔
1301

1302
  for (int32_t action = 0; action < numOfActions; ++action) {
30✔
1303
    STransAction *pAction = taosArrayGet(pArray, action);
24✔
1304
    if (pAction->msgSent && pAction->msgReceived &&
24!
1305
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
24✔
1306
      continue;
18✔
1307
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
6!
1308

1309
    mndTransResetAction(pMnode, pTrans, pAction);
6✔
1310
  }
1311
}
6✔
1312

1313
// execute in sync context
1314
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
199,750✔
1315
  if (pAction->rawWritten) return 0;
199,750✔
1316
  if (topHalf) {
115,146!
1317
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
×
1318
  }
1319

1320
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
115,146✔
1321
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
115,146!
1322
    pAction->rawWritten = true;
115,146✔
1323
    pAction->errCode = 0;
115,146✔
1324
    code = 0;
115,146✔
1325
    mInfo("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
115,146!
1326
          sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1327

1328
    mndSetTransLastAction(pTrans, pAction);
115,146✔
1329
  } else {
1330
    pAction->errCode = (terrno != 0) ? terrno : code;
×
1331
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
×
1332
           pAction->id, tstrerror(code), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
1333
    mndSetTransLastAction(pTrans, pAction);
×
1334
  }
1335

1336
  TAOS_RETURN(code);
115,146✔
1337
}
1338

1339
// execute in trans context
1340
static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
643,325✔
1341
  if (pAction->msgSent) return 0;
643,325✔
1342
  if (mndCannotExecuteTransAction(pMnode, topHalf)) {
137,225✔
1343
    TAOS_RETURN(TSDB_CODE_MND_TRANS_CTX_SWITCH);
37,373✔
1344
  }
1345

1346
  int64_t signature = pTrans->id;
99,852✔
1347
  signature = (signature << 32);
99,852✔
1348
  signature += pAction->id;
99,852✔
1349

1350
  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
99,852✔
1351
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
99,852✔
1352
  if (rpcMsg.pCont == NULL) {
99,852!
1353
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1354
    return -1;
×
1355
  }
1356
  rpcMsg.info.traceId.rootId = pTrans->mTraceId;
99,852✔
1357
  rpcMsg.info.notFreeAhandle = 1;
99,852✔
1358

1359
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
99,852✔
1360

1361
  char    detail[1024] = {0};
99,852✔
1362
  int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
99,852!
1363
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
99,852✔
1364
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
206,071✔
1365
    len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
106,219✔
1366
                    pAction->epSet.eps[i].port);
106,219✔
1367
  }
1368

1369
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
99,852✔
1370
  if (code == 0) {
99,852✔
1371
    pAction->msgSent = 1;
99,851✔
1372
    // pAction->msgReceived = 0;
1373
    pAction->errCode = TSDB_CODE_ACTION_IN_PROGRESS;
99,851✔
1374
    mInfo("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
99,851!
1375

1376
    mndSetTransLastAction(pTrans, pAction);
99,851✔
1377
  } else {
1378
    pAction->msgSent = 0;
1✔
1379
    pAction->msgReceived = 0;
1✔
1380
    pAction->errCode = (terrno != 0) ? terrno : code;
1!
1381
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
1!
1382
           detail);
1383

1384
    mndSetTransLastAction(pTrans, pAction);
1✔
1385
  }
1386

1387
  TAOS_RETURN(code);
99,852✔
1388
}
1389

1390
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
×
1391
  if (!topHalf) return TSDB_CODE_MND_TRANS_CTX_SWITCH;
×
1392
  pAction->rawWritten = 0;
×
1393
  pAction->errCode = 0;
×
1394
  mInfo("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
×
1395

1396
  mndSetTransLastAction(pTrans, pAction);
×
1397
  return 0;
×
1398
}
1399

1400
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction, bool topHalf) {
843,075✔
1401
  if (pAction->actionType == TRANS_ACTION_RAW) {
843,075✔
1402
    return mndTransWriteSingleLog(pMnode, pTrans, pAction, topHalf);
199,750✔
1403
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
643,325!
1404
    return mndTransSendSingleMsg(pMnode, pTrans, pAction, topHalf);
643,325✔
1405
  } else {
1406
    return mndTransExecNullMsg(pMnode, pTrans, pAction, topHalf);
×
1407
  }
1408
}
1409

1410
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
237,622✔
1411
  int32_t numOfActions = taosArrayGetSize(pArray);
237,622✔
1412
  int32_t code = 0;
237,622✔
1413

1414
  for (int32_t action = 0; action < numOfActions; ++action) {
983,800✔
1415
    STransAction *pAction = taosArrayGet(pArray, action);
775,249✔
1416
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
775,249✔
1417
    if (code != 0) {
775,249✔
1418
      mInfo("trans:%d, action:%d not executed since %s. numOfActions:%d", pTrans->id, action, tstrerror(code),
29,071!
1419
            numOfActions);
1420
      break;
29,071✔
1421
    }
1422
  }
1423

1424
  return code;
237,622✔
1425
}
1426

1427
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray, bool topHalf) {
268,833✔
1428
  int32_t numOfActions = taosArrayGetSize(pArray);
268,833✔
1429
  int32_t code = 0;
268,833✔
1430
  if (numOfActions == 0) return 0;
268,833✔
1431

1432
  if ((code = mndTransExecSingleActions(pMnode, pTrans, pArray, topHalf)) != 0) {
237,622✔
1433
    return code;
29,071✔
1434
  }
1435

1436
  int32_t       numOfExecuted = 0;
208,551✔
1437
  int32_t       errCode = 0;
208,551✔
1438
  STransAction *pErrAction = NULL;
208,551✔
1439
  for (int32_t action = 0; action < numOfActions; ++action) {
954,729✔
1440
    STransAction *pAction = taosArrayGet(pArray, action);
746,178✔
1441
    if (pAction->msgReceived || pAction->rawWritten) {
746,178✔
1442
      numOfExecuted++;
457,798✔
1443
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
457,798✔
1444
        errCode = pAction->errCode;
8✔
1445
        pErrAction = pAction;
8✔
1446
      }
1447
    } else {
1448
      pErrAction = pAction;
288,380✔
1449
    }
1450
  }
1451

1452
  mndSetTransLastAction(pTrans, pErrAction);
208,551✔
1453

1454
  if (numOfExecuted == numOfActions) {
208,551✔
1455
    if (errCode == 0) {
121,135✔
1456
      mInfo("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
121,129!
1457
      return 0;
121,129✔
1458
    } else {
1459
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
6!
1460
      mndTransResetActions(pMnode, pTrans, pArray);
6✔
1461
      terrno = errCode;
6✔
1462
      return errCode;
6✔
1463
    }
1464
  } else {
1465
    mInfo("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
87,416!
1466

1467
    for (int32_t action = 0; action < numOfActions; ++action) {
576,047✔
1468
      STransAction *pAction = taosArrayGet(pArray, action);
488,631✔
1469
      mDebug("trans:%d, %s:%d Sent:%d, Received:%d, errCode:0x%x, acceptableCode:0x%x, retryCode:0x%x",
488,631✔
1470
              pTrans->id, mndTransStr(pAction->stage), pAction->id, pAction->msgSent, pAction->msgReceived,
1471
              pAction->errCode, pAction->acceptableCode, pAction->retryCode);
1472
      if (pAction->msgSent) {
488,631!
1473
        if (pAction->msgReceived) {
488,631✔
1474
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
200,251✔
1475
            mndTransResetAction(pMnode, pTrans, pAction);
2✔
1476
            mInfo("trans:%d, %s:%d reset", pTrans->id, mndTransStr(pAction->stage), pAction->id);
2!
1477
          }
1478
        }
1479
      }
1480
    }
1481
    return TSDB_CODE_ACTION_IN_PROGRESS;
87,416✔
1482
  }
1483
}
1484

1485
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
175,112✔
1486
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions, topHalf);
175,112✔
1487
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
175,112✔
1488
    mError("trans:%d, failed to execute redoActions since:%s, code:0x%x, topHalf(TransContext):%d", pTrans->id,
6!
1489
           terrstr(), terrno, topHalf);
1490
  }
1491
  return code;
175,112✔
1492
}
1493

1494
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
49✔
1495
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions, topHalf);
49✔
1496
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
49✔
1497
    mError("trans:%d, failed to execute undoActions since %s. topHalf(TransContext):%d", pTrans->id, terrstr(),
1!
1498
           topHalf);
1499
  }
1500
  return code;
49✔
1501
}
1502

1503
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans, bool topHalf) {
93,672✔
1504
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions, topHalf);
93,672✔
1505
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
93,672!
1506
    mError("trans:%d, failed to execute commitActions since %s. topHalf(TransContext):%d", pTrans->id, terrstr(),
×
1507
           topHalf);
1508
  }
1509
  return code;
93,672✔
1510
}
1511

1512
static int32_t mndTransExecuteActionsSerial(SMnode *pMnode, STrans *pTrans, SArray *pActions, bool topHalf) {
34,126✔
1513
  int32_t code = 0;
34,126✔
1514
  int32_t numOfActions = taosArrayGetSize(pActions);
34,126✔
1515
  if (numOfActions == 0) return code;
34,126✔
1516

1517
  if (pTrans->actionPos >= numOfActions) {
34,124✔
1518
    return code;
3,227✔
1519
  }
1520

1521
  mInfo("trans:%d, execute %d actions serial, begin at action:%d, stage:%s", pTrans->id, numOfActions,
30,897!
1522
        pTrans->actionPos, mndTransStr(pTrans->stage));
1523

1524
  for (int32_t action = pTrans->actionPos; action < numOfActions; ++action) {
46,647✔
1525
    STransAction *pAction = taosArrayGet(pActions, action);
43,749✔
1526

1527
    mInfo("trans:%d, current action:%d, stage:%s, actionType(0:log,1:msg):%d", pTrans->id, pTrans->actionPos,
43,749!
1528
          mndTransStr(pAction->stage), pAction->actionType);
1529

1530
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
43,749✔
1531
    if (code == 0) {
43,749✔
1532
      if (pAction->msgSent) {
35,446✔
1533
        if (pAction->msgReceived) {
31,543✔
1534
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
13,972✔
1535
            code = pAction->errCode;
6,375✔
1536
            mndTransResetAction(pMnode, pTrans, pAction);
6,375✔
1537
          } else {
1538
            mInfo("trans:%d, %s:%d execute successfully", pTrans->id, mndTransStr(pAction->stage), action);
7,597!
1539
          }
1540
        } else {
1541
          code = TSDB_CODE_ACTION_IN_PROGRESS;
17,571✔
1542
        }
1543
      } else if (pAction->rawWritten) {
3,903!
1544
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
3,903!
1545
          code = pAction->errCode;
×
1546
        } else {
1547
          mInfo("trans:%d, %s:%d write successfully", pTrans->id, mndTransStr(pAction->stage), action);
3,903!
1548
        }
1549
      } else {
1550
      }
1551
    }
1552

1553
    if (code == 0) {
43,749✔
1554
      pTrans->failedTimes = 0;
11,500✔
1555
    }
1556
    mndSetTransLastAction(pTrans, pAction);
43,749✔
1557

1558
    if (mndCannotExecuteTransAction(pMnode, topHalf)) {
43,749✔
1559
      pTrans->lastErrorNo = code;
10,410✔
1560
      pTrans->code = code;
10,410✔
1561
      mInfo("trans:%d, %s:%d, topHalf(TransContext):%d, not execute next action, code:%s", pTrans->id,
10,410!
1562
            mndTransStr(pAction->stage), action, topHalf, tstrerror(code));
1563
      break;
10,410✔
1564
    }
1565

1566
    if (code == 0) {
33,339✔
1567
      pTrans->code = 0;
9,393✔
1568
      pTrans->actionPos++;
9,393✔
1569
      mInfo("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
9,393!
1570
            pAction->id);
1571
      (void)taosThreadMutexUnlock(&pTrans->mutex);
9,393✔
1572
      code = mndTransSync(pMnode, pTrans);
9,393✔
1573
      (void)taosThreadMutexLock(&pTrans->mutex);
9,393✔
1574
      if (code != 0) {
9,393!
1575
        pTrans->actionPos--;
×
1576
        pTrans->code = terrno;
×
1577
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
×
1578
               mndTransStr(pAction->stage), pAction->id, terrstr());
1579
        break;
×
1580
      }
1581
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
23,946✔
1582
      mInfo("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
17,571!
1583
      break;
17,571✔
1584
    } else if (code == pAction->retryCode || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
6,375✔
1585
               code == TSDB_CODE_SYN_RESTORING || code == TSDB_CODE_SYN_NOT_LEADER) {
38✔
1586
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and retry", pTrans->id, mndTransStr(pAction->stage), pAction->id,
6,357!
1587
            code, tstrerror(code));
1588
      pTrans->lastErrorNo = code;
6,357✔
1589
      taosMsleep(300);
6,357✔
1590
      action--;
6,357✔
1591
      continue;
6,357✔
1592
    } else {
1593
      terrno = code;
18✔
1594
      pTrans->lastErrorNo = code;
18✔
1595
      pTrans->code = code;
18✔
1596
      mInfo("trans:%d, %s:%d receive code:0x%x(%s) and wait another schedule, failedTimes:%d", pTrans->id,
18!
1597
            mndTransStr(pAction->stage), pAction->id, code, tstrerror(code), pTrans->failedTimes);
1598
      break;
18✔
1599
    }
1600
  }
1601

1602
  return code;
30,897✔
1603
}
1604

1605
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
34,126✔
1606
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
34,126✔
1607
  (void)taosThreadMutexLock(&pTrans->mutex);
34,126✔
1608
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
34,126!
1609
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->redoActions, topHalf);
34,126✔
1610
  }
1611
  (void)taosThreadMutexUnlock(&pTrans->mutex);
34,126✔
1612
  return code;
34,126✔
1613
}
1614

1615
static int32_t mndTransExecuteUndoActionsSerial(SMnode *pMnode, STrans *pTrans, bool topHalf) {
×
1616
  int32_t code = TSDB_CODE_ACTION_IN_PROGRESS;
×
1617
  (void)taosThreadMutexLock(&pTrans->mutex);
×
1618
  if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1619
    code = mndTransExecuteActionsSerial(pMnode, pTrans, pTrans->undoActions, topHalf);
×
1620
  }
1621
  (void)taosThreadMutexUnlock(&pTrans->mutex);
×
1622
  return code;
×
1623
}
1624

1625
bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
48,759✔
1626
  bool    continueExec = true;
48,759✔
1627
  int32_t code = 0;
48,759✔
1628
  terrno = 0;
48,759✔
1629

1630
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
48,759✔
1631
  if (numOfActions == 0) goto _OVER;
48,759✔
1632

1633
  mInfo("trans:%d, execute %d prepare actions.", pTrans->id, numOfActions);
13,302!
1634

1635
  for (int32_t action = 0; action < numOfActions; ++action) {
37,379✔
1636
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
24,077✔
1637
    code = mndTransExecSingleAction(pMnode, pTrans, pAction, topHalf);
24,077✔
1638
    if (code != 0) {
24,077!
1639
      terrno = code;
×
1640
      mError("trans:%d, failed to execute prepare action:%d, numOfActions:%d, since %s", pTrans->id, action,
×
1641
             numOfActions, tstrerror(code));
1642
      return false;
×
1643
    }
1644
  }
1645

1646
_OVER:
13,302✔
1647
  pTrans->stage = TRN_STAGE_REDO_ACTION;
48,759✔
1648
  mInfo("trans:%d, stage from prepare to redoAction", pTrans->id);
48,759!
1649
  return continueExec;
48,759✔
1650
}
1651

1652
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
209,238✔
1653
  bool    continueExec = true;
209,238✔
1654
  int32_t code = 0;
209,238✔
1655
  terrno = 0;
209,238✔
1656

1657
  if (pTrans->exec == TRN_EXEC_SERIAL) {
209,238✔
1658
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans, topHalf);
34,126✔
1659
  } else {
1660
    code = mndTransExecuteRedoActions(pMnode, pTrans, topHalf);
175,112✔
1661
  }
1662

1663
  if (mndCannotExecuteTransAction(pMnode, topHalf)) {
209,238✔
1664
    pTrans->lastErrorNo = code;
59,283✔
1665
    pTrans->code = code;
59,283✔
1666
    bool continueExec = true;
59,283✔
1667
    if (code != 0 && code != TSDB_CODE_MND_TRANS_CTX_SWITCH) {
59,283!
1668
      taosMsleep(100);
×
1669
      continueExec = true;
×
1670
    } else {
1671
      continueExec = false;
59,283✔
1672
    }
1673
    mInfo("trans:%d, cannot execute redo action stage, topHalf(TransContext):%d, continueExec:%d, code:%s", pTrans->id,
59,283!
1674
          topHalf, continueExec, tstrerror(code));
1675

1676
    return continueExec;
59,283✔
1677
  }
1678
  terrno = code;
149,955✔
1679

1680
  if (code == 0) {
149,955✔
1681
    pTrans->code = 0;
44,976✔
1682
    pTrans->stage = TRN_STAGE_COMMIT;
44,976✔
1683
    mInfo("trans:%d, stage from redoAction to commit", pTrans->id);
44,976!
1684
    continueExec = true;
44,976✔
1685
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
104,979!
1686
    mInfo("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
104,955!
1687
    continueExec = false;
104,955✔
1688
  } else {
1689
    pTrans->failedTimes++;
24✔
1690
    pTrans->code = terrno;
24✔
1691
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
24✔
1692
      if (pTrans->lastAction != 0) {
5✔
1693
        STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->lastAction);
4✔
1694
        if (pAction->retryCode != 0 && pAction->retryCode == pAction->errCode) {
4!
1695
          if (pTrans->failedTimes < 6) {
×
1696
            mError("trans:%d, stage keep on redoAction since action:%d code:0x%x not 0x%x, failedTimes:%d", pTrans->id,
×
1697
                   pTrans->lastAction, pTrans->code, pAction->retryCode, pTrans->failedTimes);
1698
            taosMsleep(1000);
×
1699
            continueExec = true;
×
1700
            return true;
×
1701
          }
1702
        }
1703
      }
1704

1705
      pTrans->stage = TRN_STAGE_ROLLBACK;
5✔
1706
      pTrans->actionPos = 0;
5✔
1707
      mError("trans:%d, stage from redoAction to rollback since %s, and set actionPos to %d", pTrans->id, terrstr(),
5!
1708
             pTrans->actionPos);
1709
      continueExec = true;
5✔
1710
    } else {
1711
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
19!
1712
      continueExec = false;
19✔
1713
    }
1714
  }
1715

1716
  return continueExec;
149,955✔
1717
}
1718

1719
// in trans context
1720
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
44,976✔
1721
  if (mndCannotExecuteTransAction(pMnode, topHalf)) return false;
44,976!
1722

1723
  bool    continueExec = true;
44,976✔
1724
  int32_t code = mndTransCommit(pMnode, pTrans);
44,976✔
1725

1726
  if (code == 0) {
44,976✔
1727
    pTrans->code = 0;
44,957✔
1728
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
44,957✔
1729
    mInfo("trans:%d, stage from commit to commitAction", pTrans->id);
44,957!
1730
    continueExec = true;
44,957✔
1731
  } else {
1732
    pTrans->code = terrno;
19✔
1733
    pTrans->failedTimes++;
19✔
1734
    mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
19!
1735
    continueExec = false;
19✔
1736
  }
1737

1738
  return continueExec;
44,976✔
1739
}
1740

1741
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
93,672✔
1742
  bool    continueExec = true;
93,672✔
1743
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans, topHalf);
93,672✔
1744

1745
  if (code == 0) {
93,672!
1746
    pTrans->code = 0;
93,672✔
1747
    pTrans->stage = TRN_STAGE_FINISH;  // TRN_STAGE_PRE_FINISH is not necessary
93,672✔
1748
    mInfo("trans:%d, stage from commitAction to finished", pTrans->id);
93,672!
1749
    continueExec = true;
93,672✔
1750
  } else if (code == TSDB_CODE_MND_TRANS_CTX_SWITCH && topHalf) {
×
1751
    pTrans->code = 0;
×
1752
    pTrans->stage = TRN_STAGE_COMMIT;
×
1753
    mInfo("trans:%d, back to commit stage", pTrans->id);
×
1754
    continueExec = true;
×
1755
  } else {
1756
    pTrans->code = terrno;
×
1757
    pTrans->failedTimes++;
×
1758
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1759
    continueExec = false;
×
1760
  }
1761

1762
  return continueExec;
93,672✔
1763
}
1764

1765
static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
49✔
1766
  bool    continueExec = true;
49✔
1767
  int32_t code = 0;
49✔
1768

1769
  if (pTrans->exec == TRN_EXEC_SERIAL) {
49!
1770
    code = mndTransExecuteUndoActionsSerial(pMnode, pTrans, topHalf);
×
1771
  } else {
1772
    code = mndTransExecuteUndoActions(pMnode, pTrans, topHalf);
49✔
1773
  }
1774

1775
  if (mndCannotExecuteTransAction(pMnode, topHalf)) return false;
49✔
1776
  terrno = code;
38✔
1777

1778
  if (code == 0) {
38✔
1779
    pTrans->stage = TRN_STAGE_PRE_FINISH;
5✔
1780
    mInfo("trans:%d, stage from undoAction to pre-finish", pTrans->id);
5!
1781
    continueExec = true;
5✔
1782
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS || code == TSDB_CODE_MND_TRANS_CTX_SWITCH) {
33!
1783
    mInfo("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
32!
1784
    continueExec = false;
32✔
1785
  } else {
1786
    pTrans->failedTimes++;
1✔
1787
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
1!
1788
    continueExec = false;
1✔
1789
  }
1790

1791
  return continueExec;
38✔
1792
}
1793

1794
// in trans context
1795
static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
5✔
1796
  if (mndCannotExecuteTransAction(pMnode, topHalf)) return false;
5!
1797

1798
  bool    continueExec = true;
5✔
1799
  int32_t code = mndTransRollback(pMnode, pTrans);
5✔
1800

1801
  if (code == 0) {
5!
1802
    pTrans->stage = TRN_STAGE_UNDO_ACTION;
5✔
1803
    continueExec = true;
5✔
1804
  } else {
UNCOV
1805
    pTrans->failedTimes++;
×
UNCOV
1806
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
UNCOV
1807
    continueExec = false;
×
1808
  }
1809

1810
  return continueExec;
5✔
1811
}
1812

1813
static bool mndTransPerformPreFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
5✔
1814
  if (mndCannotExecuteTransAction(pMnode, topHalf)) return false;
5!
1815

1816
  bool    continueExec = true;
5✔
1817
  int32_t code = mndTransPreFinish(pMnode, pTrans);
5✔
1818

1819
  if (code == 0) {
5!
1820
    pTrans->stage = TRN_STAGE_FINISH;
5✔
1821
    mInfo("trans:%d, stage from pre-finish to finish", pTrans->id);
5!
1822
    continueExec = true;
5✔
1823
  } else {
1824
    pTrans->failedTimes++;
×
1825
    mError("trans:%d, stage keep on pre-finish since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
×
1826
    continueExec = false;
×
1827
  }
1828

1829
  return continueExec;
5✔
1830
}
1831

1832
static bool mndTransPerformFinishStage(SMnode *pMnode, STrans *pTrans, bool topHalf) {
93,688✔
1833
  bool continueExec = false;
93,688✔
1834
  if (topHalf) return continueExec;
93,688✔
1835

1836
  SSdbRaw *pRaw = mndTransEncode(pTrans);
48,726✔
1837
  if (pRaw == NULL) {
48,726!
1838
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
×
1839
    return false;
×
1840
  }
1841
  TAOS_CHECK_RETURN(sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED));
48,726!
1842

1843
  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
48,726✔
1844
  if (code != 0) {
48,726!
1845
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
×
1846
  }
1847

1848
  mInfo("trans:%d, execute finished, code:0x%x, failedTimes:%d createTime:%" PRId64, pTrans->id, pTrans->code,
48,726!
1849
        pTrans->failedTimes, pTrans->createdTime);
1850
  return continueExec;
48,726✔
1851
}
1852

1853
void mndTransExecuteImp(SMnode *pMnode, STrans *pTrans, bool topHalf) {
258,008✔
1854
  bool continueExec = true;
258,008✔
1855

1856
  while (continueExec) {
699,641✔
1857
    mInfo("trans:%d, continue to execute, stage:%s createTime:%" PRId64 " topHalf(TransContext):%d", pTrans->id,
441,633!
1858
          mndTransStr(pTrans->stage), pTrans->createdTime, topHalf);
1859
    pTrans->lastExecTime = taosGetTimestampMs();
441,633✔
1860
    switch (pTrans->stage) {
441,633!
1861
      case TRN_STAGE_PREPARE:
×
1862
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans, topHalf);
×
1863
        break;
×
1864
      case TRN_STAGE_REDO_ACTION:
209,238✔
1865
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans, topHalf);
209,238✔
1866
        break;
209,238✔
1867
      case TRN_STAGE_COMMIT:
44,976✔
1868
        continueExec = mndTransPerformCommitStage(pMnode, pTrans, topHalf);
44,976✔
1869
        break;
44,976✔
1870
      case TRN_STAGE_COMMIT_ACTION:
93,672✔
1871
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans, topHalf);
93,672✔
1872
        break;
93,672✔
1873
      case TRN_STAGE_ROLLBACK:
5✔
1874
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans, topHalf);
5✔
1875
        break;
5✔
1876
      case TRN_STAGE_UNDO_ACTION:
49✔
1877
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans, topHalf);
49✔
1878
        break;
49✔
1879
      case TRN_STAGE_PRE_FINISH:
5✔
1880
        continueExec = mndTransPerformPreFinishStage(pMnode, pTrans, topHalf);
5✔
1881
        break;
5✔
1882
      case TRN_STAGE_FINISH:
93,688✔
1883
        continueExec = mndTransPerformFinishStage(pMnode, pTrans, topHalf);
93,688✔
1884
        break;
93,688✔
1885
      default:
×
1886
        continueExec = false;
×
1887
        break;
×
1888
    }
1889
  }
1890

1891
  mndTransSendRpcRsp(pMnode, pTrans);
258,008✔
1892
}
258,008✔
1893

1894
// start trans, pullup, receive rsp, kill
1895
void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
150,146✔
1896
  bool topHalf = true;
150,146✔
1897
  mndTransExecuteImp(pMnode, pTrans, topHalf);
150,146✔
1898
}
150,146✔
1899

1900
// update trans
1901
void mndTransRefresh(SMnode *pMnode, STrans *pTrans) {
107,862✔
1902
  bool topHalf = false;
107,862✔
1903
  mndTransExecuteImp(pMnode, pTrans, topHalf);
107,862✔
1904
}
107,862✔
1905

1906
static int32_t mndProcessTransTimer(SRpcMsg *pReq) {
32,936✔
1907
  mTrace("start to process trans timer");
32,936✔
1908
  mndTransPullup(pReq->info.node);
32,936✔
1909
  return 0;
32,936✔
1910
}
1911

1912
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
×
1913
  SArray *pArray = NULL;
×
1914
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
×
1915
    pArray = pTrans->redoActions;
×
1916
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
×
1917
    pArray = pTrans->undoActions;
×
1918
  } else {
1919
    TAOS_RETURN(TSDB_CODE_MND_TRANS_INVALID_STAGE);
×
1920
  }
1921

1922
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
×
1923
    STransAction *pAction = taosArrayGet(pArray, i);
×
1924
    mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
×
1925
          mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
1926
    pAction->msgSent = 1;
×
1927
    pAction->msgReceived = 1;
×
1928
    pAction->errCode = 0;
×
1929
  }
1930

1931
  mndTransExecute(pMnode, pTrans);
×
1932
  return 0;
×
1933
}
1934

1935
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
1✔
1936
  SMnode       *pMnode = pReq->info.node;
1✔
1937
  SKillTransReq killReq = {0};
1✔
1938
  int32_t       code = -1;
1✔
1939
  STrans       *pTrans = NULL;
1✔
1940

1941
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
1!
1942
    code = TSDB_CODE_INVALID_MSG;
×
1943
    goto _OVER;
×
1944
  }
1945

1946
  mInfo("trans:%d, start to kill", killReq.transId);
1!
1947
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_KILL_TRANS)) != 0) {
1!
1948
    goto _OVER;
1✔
1949
  }
1950

1951
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
×
1952
  if (pTrans == NULL) {
×
1953
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
1954
    if (terrno != 0) code = terrno;
×
1955
    goto _OVER;
×
1956
  }
1957

1958
  code = mndKillTrans(pMnode, pTrans);
×
1959

1960
_OVER:
1✔
1961
  if (code != 0) {
1!
1962
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
1!
1963
  }
1964

1965
  mndReleaseTrans(pMnode, pTrans);
1✔
1966
  TAOS_RETURN(code);
1✔
1967
}
1968

1969
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }
217✔
1970

1971
void mndTransPullup(SMnode *pMnode) {
33,442✔
1972
  SSdb   *pSdb = pMnode->pSdb;
33,442✔
1973
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
33,442✔
1974
  if (pArray == NULL) return;
33,442!
1975

1976
  void *pIter = NULL;
33,442✔
1977
  while (1) {
5,373✔
1978
    STrans *pTrans = NULL;
38,815✔
1979
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
38,815✔
1980
    if (pIter == NULL) break;
38,815✔
1981
    if (taosArrayPush(pArray, &pTrans->id) == NULL) {
10,746!
1982
      mError("failed to put trans into array, trans:%d, but pull up will continute", pTrans->id);
×
1983
    }
1984
    sdbRelease(pSdb, pTrans);
5,373✔
1985
  }
1986

1987
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);
33,442✔
1988

1989
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
38,815✔
1990
    int32_t *pTransId = taosArrayGet(pArray, i);
5,373✔
1991
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
5,373✔
1992
    if (pTrans != NULL) {
5,373!
1993
      mndTransExecute(pMnode, pTrans);
5,373✔
1994
    }
1995
    mndReleaseTrans(pMnode, pTrans);
5,373✔
1996
  }
1997
  taosArrayDestroy(pArray);
33,442✔
1998
}
1999

2000
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
7,419✔
2001
  SMnode *pMnode = pReq->info.node;
7,419✔
2002
  SSdb   *pSdb = pMnode->pSdb;
7,419✔
2003
  int32_t numOfRows = 0;
7,419✔
2004
  STrans *pTrans = NULL;
7,419✔
2005
  int32_t cols = 0;
7,419✔
2006
  int32_t code = 0;
7,419✔
2007
  int32_t lino = 0;
7,419✔
2008

2009
  while (numOfRows < rows) {
8,224!
2010
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
8,225✔
2011
    if (pShow->pIter == NULL) break;
8,229✔
2012

2013
    cols = 0;
805✔
2014

2015
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
805✔
2016
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->id, false), pTrans, &lino, _OVER);
805!
2017

2018
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
805✔
2019
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false), pTrans, &lino,
805!
2020
                        _OVER);
2021

2022
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
805✔
2023
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
805✔
2024
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
805✔
2025
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stage, false), pTrans, &lino, _OVER);
805!
2026

2027
    char opername[TSDB_TRANS_OPER_LEN + VARSTR_HEADER_SIZE] = {0};
805✔
2028
    STR_WITH_MAXSIZE_TO_VARSTR(opername, pTrans->opername, pShow->pMeta->pSchemas[cols].bytes);
805✔
2029
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
805✔
2030
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)opername, false), pTrans, &lino, _OVER);
805!
2031

2032
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
805✔
2033
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
805✔
2034
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
805✔
2035
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)dbname, false), pTrans, &lino, _OVER);
805!
2036

2037
    char stbname[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
805✔
2038
    STR_WITH_MAXSIZE_TO_VARSTR(stbname, mndGetDbStr(pTrans->stbname), pShow->pMeta->pSchemas[cols].bytes);
805✔
2039
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
805✔
2040
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)stbname, false), pTrans, &lino, _OVER);
805!
2041

2042
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
805✔
2043
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false), pTrans, &lino,
805!
2044
                        _OVER);
2045

2046
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
805✔
2047
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false), pTrans, &lino,
805!
2048
                        _OVER);
2049

2050
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
805✔
2051
    char    detail[TSDB_TRANS_ERROR_LEN + 1] = {0};
805✔
2052
    int32_t len = tsnprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
2,415✔
2053
                           pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
805✔
2054
    SEpSet  epset = pTrans->lastEpset;
805✔
2055
    if (epset.numOfEps > 0) {
805!
2056
      len += tsnprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
1,610!
2057
                      TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
1,610✔
2058
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
2,233✔
2059
        len += tsnprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
1,428✔
2060
      }
2061
    }
2062
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
805✔
2063
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
805✔
2064
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)lastInfo, false), pTrans, &lino, _OVER);
805!
2065

2066
    numOfRows++;
805✔
2067
    sdbRelease(pSdb, pTrans);
805✔
2068
  }
2069

2070
_OVER:
×
2071
  if (code != 0) mError("failed to retrieve at line:%d, since %s", lino, tstrerror(code));
7,423!
2072
  pShow->numOfRows += numOfRows;
7,423✔
2073
  return numOfRows;
7,423✔
2074
}
2075

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

© 2025 Coveralls, Inc