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

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndTrans.h"
S
Shengliang Guan 已提交
18
#include "mndAuth.h"
L
Liu Jicong 已提交
19
#include "mndConsumer.h"
S
Shengliang Guan 已提交
20
#include "mndDb.h"
S
Shengliang Guan 已提交
21
#include "mndShow.h"
S
Shengliang Guan 已提交
22
#include "mndSync.h"
S
Shengliang Guan 已提交
23
#include "mndUser.h"
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25 26 27
#define TRANS_VER_NUMBER   1
#define TRANS_ARRAY_SIZE   8
#define TRANS_RESERVE_SIZE 64
S
Shengliang Guan 已提交
28

S
Shengliang Guan 已提交
29 30 31
static SSdbRaw *mndTransActionEncode(STrans *pTrans);
static SSdbRow *mndTransActionDecode(SSdbRaw *pRaw);
static int32_t  mndTransActionInsert(SSdb *pSdb, STrans *pTrans);
S
Shengliang Guan 已提交
32
static int32_t  mndTransActionUpdate(SSdb *pSdb, STrans *OldTrans, STrans *pOld);
33
static int32_t  mndTransActionDelete(SSdb *pSdb, STrans *pTrans, bool callFunc);
S
Shengliang Guan 已提交
34

S
Shengliang Guan 已提交
35
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw);
S
Shengliang Guan 已提交
36
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction);
S
Shengliang Guan 已提交
37 38
static void    mndTransDropLogs(SArray *pArray);
static void    mndTransDropActions(SArray *pArray);
39
static void    mndTransDropData(STrans *pTrans);
40
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray);
S
Shengliang Guan 已提交
41 42 43 44
static int32_t mndTransExecuteRedoLogs(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransExecuteUndoLogs(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans);
45
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans);
S
Shengliang Guan 已提交
46 47 48 49 50
static bool    mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerformRedoLogStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerformUndoLogStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans);
51
static bool    mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans);
S
Shengliang Guan 已提交
52 53 54 55
static bool    mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerfromFinishedStage(SMnode *pMnode, STrans *pTrans);

S
Shengliang Guan 已提交
56
static void    mndTransExecute(SMnode *pMnode, STrans *pTrans);
57
static void    mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans);
S
Shengliang Guan 已提交
58 59
static int32_t mndProcessTransReq(SRpcMsg *pReq);
static int32_t mndProcessKillTransReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
60

S
Shengliang Guan 已提交
61
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
62 63
static void    mndCancelGetNextTrans(SMnode *pMnode, void *pIter);

S
Shengliang Guan 已提交
64
int32_t mndInitTrans(SMnode *pMnode) {
S
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73
  SSdbTable table = {
      .sdbType = SDB_TRANS,
      .keyType = SDB_KEY_INT32,
      .encodeFp = (SdbEncodeFp)mndTransActionEncode,
      .decodeFp = (SdbDecodeFp)mndTransActionDecode,
      .insertFp = (SdbInsertFp)mndTransActionInsert,
      .updateFp = (SdbUpdateFp)mndTransActionUpdate,
      .deleteFp = (SdbDeleteFp)mndTransActionDelete,
  };
S
Shengliang Guan 已提交
74

S
Shengliang Guan 已提交
75
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS_TIMER, mndProcessTransReq);
S
Shengliang Guan 已提交
76
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRANS, mndProcessKillTransReq);
S
Shengliang Guan 已提交
77

S
Shengliang Guan 已提交
78
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndRetrieveTrans);
S
Shengliang Guan 已提交
79
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_TRANS, mndCancelGetNextTrans);
S
Shengliang Guan 已提交
80 81 82 83 84
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupTrans(SMnode *pMnode) {}

85 86 87
static int32_t mndTransGetActionsSize(SArray *pArray) {
  int32_t actionNum = taosArrayGetSize(pArray);
  int32_t rawDataLen = 0;
S
Shengliang Guan 已提交
88

89 90
  for (int32_t i = 0; i < actionNum; ++i) {
    STransAction *pAction = taosArrayGet(pArray, i);
91
    if (pAction->actionType == TRANS_ACTION_RAW) {
92
      rawDataLen += (sdbGetRawTotalSize(pAction->pRaw) + sizeof(int32_t));
93
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
94
      rawDataLen += (sizeof(STransAction) + pAction->contLen);
95 96
    } else {
      // empty
97
    }
98
    rawDataLen += sizeof(int8_t);
S
Shengliang Guan 已提交
99 100
  }

101 102
  return rawDataLen;
}
S
Shengliang Guan 已提交
103

104 105
static SSdbRaw *mndTransActionEncode(STrans *pTrans) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
106

107 108 109 110
  int32_t rawDataLen = sizeof(STrans) + TRANS_RESERVE_SIZE;
  rawDataLen += mndTransGetActionsSize(pTrans->redoActions);
  rawDataLen += mndTransGetActionsSize(pTrans->undoActions);
  rawDataLen += mndTransGetActionsSize(pTrans->commitActions);
S
Shengliang Guan 已提交
111

S
Shengliang Guan 已提交
112
  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, TRANS_VER_NUMBER, rawDataLen);
S
Shengliang Guan 已提交
113
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
114
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
115 116 117 118
    return NULL;
  }

  int32_t dataPos = 0;
119
  SDB_SET_INT32(pRaw, dataPos, pTrans->id, _OVER)
S
Shengliang Guan 已提交
120 121 122 123
  SDB_SET_INT8(pRaw, dataPos, pTrans->stage, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pTrans->policy, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pTrans->conflict, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pTrans->exec, _OVER)
124 125
  SDB_SET_INT64(pRaw, dataPos, pTrans->createdTime, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_DB_FNAME_LEN, _OVER)
126
  SDB_SET_INT32(pRaw, dataPos, pTrans->redoActionPos, _OVER)
127 128 129 130

  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);
  int32_t commitActionNum = taosArrayGetSize(pTrans->commitActions);
131 132
  SDB_SET_INT32(pRaw, dataPos, redoActionNum, _OVER)
  SDB_SET_INT32(pRaw, dataPos, undoActionNum, _OVER)
133
  SDB_SET_INT32(pRaw, dataPos, commitActionNum, _OVER)
S
Shengliang Guan 已提交
134

S
Shengliang Guan 已提交
135 136
  for (int32_t i = 0; i < redoActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
137 138 139
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
140
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
141
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
142
    if (pAction->actionType == TRANS_ACTION_RAW) {
143
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
144
      SDB_SET_INT8(pRaw, dataPos, pAction->rawWritten, _OVER)
145 146
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
147
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
148 149
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
150 151
      SDB_SET_INT8(pRaw, dataPos, pAction->msgSent, _OVER)
      SDB_SET_INT8(pRaw, dataPos, pAction->msgReceived, _OVER)
152 153
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
154 155
    } else {
      // nothing
156
    }
S
Shengliang Guan 已提交
157 158 159 160
  }

  for (int32_t i = 0; i < undoActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
161 162 163
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
164
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
165
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
166
    if (pAction->actionType == TRANS_ACTION_RAW) {
167
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
168
      SDB_SET_INT8(pRaw, dataPos, pAction->rawWritten, _OVER)
169 170
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
171
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
172 173
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
174 175
      SDB_SET_INT8(pRaw, dataPos, pAction->msgSent, _OVER)
      SDB_SET_INT8(pRaw, dataPos, pAction->msgReceived, _OVER)
176 177
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
178 179
    } else {
      // nothing
180 181 182 183 184
    }
  }

  for (int32_t i = 0; i < commitActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->commitActions, i);
185 186 187
    SDB_SET_INT32(pRaw, dataPos, pAction->id, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pAction->errCode, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, _OVER)
188
    SDB_SET_INT8(pRaw, dataPos, pAction->actionType, _OVER)
189
    SDB_SET_INT8(pRaw, dataPos, pAction->stage, _OVER)
190
    if (pAction->actionType == TRANS_ACTION_RAW) {
191
      int32_t len = sdbGetRawTotalSize(pAction->pRaw);
192
      SDB_SET_INT8(pRaw, dataPos, pAction->rawWritten, _OVER)
193 194
      SDB_SET_INT32(pRaw, dataPos, len, _OVER)
      SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pRaw, len, _OVER)
195
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
196 197
      SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), _OVER)
      SDB_SET_INT16(pRaw, dataPos, pAction->msgType, _OVER)
198 199
      SDB_SET_INT8(pRaw, dataPos, pAction->msgSent, _OVER)
      SDB_SET_INT8(pRaw, dataPos, pAction->msgReceived, _OVER)
200 201
      SDB_SET_INT32(pRaw, dataPos, pAction->contLen, _OVER)
      SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, _OVER)
202 203
    } else {
      // nothing
204
    }
205 206
  }

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

214 215
  SDB_SET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
216 217 218

  terrno = 0;

219
_OVER:
220 221 222 223
  if (terrno != 0) {
    mError("trans:%d, failed to encode to raw:%p len:%d since %s", pTrans->id, pRaw, dataPos, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
S
Shengliang Guan 已提交
224 225
  }

S
Shengliang Guan 已提交
226
  mTrace("trans:%d, encode to raw:%p, row:%p len:%d", pTrans->id, pRaw, pTrans, dataPos);
S
Shengliang Guan 已提交
227 228 229
  return pRaw;
}

S
Shengliang Guan 已提交
230
static SSdbRow *mndTransActionDecode(SSdbRaw *pRaw) {
231 232
  terrno = TSDB_CODE_OUT_OF_MEMORY;

233 234 235
  SSdbRow     *pRow = NULL;
  STrans      *pTrans = NULL;
  char        *pData = NULL;
236 237 238 239
  int32_t      dataLen = 0;
  int8_t       sver = 0;
  int32_t      redoActionNum = 0;
  int32_t      undoActionNum = 0;
240
  int32_t      commitActionNum = 0;
241 242 243
  int32_t      dataPos = 0;
  STransAction action = {0};

S
Shengliang Guan 已提交
244
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
245

S
Shengliang Guan 已提交
246
  if (sver != TRANS_VER_NUMBER) {
S
Shengliang Guan 已提交
247
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
248
    goto _OVER;
S
Shengliang Guan 已提交
249 250
  }

251
  pRow = sdbAllocRow(sizeof(STrans));
S
Shengliang Guan 已提交
252
  if (pRow == NULL) goto _OVER;
253 254

  pTrans = sdbGetRowObj(pRow);
S
Shengliang Guan 已提交
255
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
256

S
Shengliang Guan 已提交
257
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, _OVER)
S
Shengliang Guan 已提交
258

S
Shengliang Guan 已提交
259 260 261 262 263 264 265 266 267
  int8_t stage = 0;
  int8_t policy = 0;
  int8_t conflict = 0;
  int8_t exec = 0;
  int8_t actionType = 0;
  SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &policy, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &conflict, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &exec, _OVER)
S
Shengliang Guan 已提交
268
  pTrans->stage = stage;
269
  pTrans->policy = policy;
270 271
  pTrans->conflict = conflict;
  pTrans->exec = exec;
S
Shengliang Guan 已提交
272 273
  SDB_GET_INT64(pRaw, dataPos, &pTrans->createdTime, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_DB_FNAME_LEN, _OVER)
274
  SDB_GET_INT32(pRaw, dataPos, &pTrans->redoActionPos, _OVER)
S
Shengliang Guan 已提交
275 276
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, _OVER)
277
  SDB_GET_INT32(pRaw, dataPos, &commitActionNum, _OVER)
S
Shengliang Guan 已提交
278

S
Shengliang Guan 已提交
279 280
  pTrans->redoActions = taosArrayInit(redoActionNum, sizeof(STransAction));
  pTrans->undoActions = taosArrayInit(undoActionNum, sizeof(STransAction));
281
  pTrans->commitActions = taosArrayInit(commitActionNum, sizeof(STransAction));
S
Shengliang Guan 已提交
282

S
Shengliang Guan 已提交
283 284
  if (pTrans->redoActions == NULL) goto _OVER;
  if (pTrans->undoActions == NULL) goto _OVER;
285
  if (pTrans->commitActions == NULL) goto _OVER;
S
Shengliang Guan 已提交
286 287

  for (int32_t i = 0; i < redoActionNum; ++i) {
288 289 290
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
291 292
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
    action.actionType = actionType;
S
Shengliang Guan 已提交
293 294
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
    action.stage = stage;
295
    if (action.actionType == TRANS_ACTION_RAW) {
296
      SDB_GET_INT8(pRaw, dataPos, &action.rawWritten, _OVER)
297
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
298 299
      action.pRaw = taosMemoryMalloc(dataLen);
      if (action.pRaw == NULL) goto _OVER;
300
      mTrace("raw:%p, is created", pData);
301 302 303
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
      if (taosArrayPush(pTrans->redoActions, &action) == NULL) goto _OVER;
      action.pRaw = NULL;
304
    } else if (action.actionType == TRANS_ACTION_MSG) {
305 306
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
307 308
      SDB_GET_INT8(pRaw, dataPos, &action.msgSent, _OVER)
      SDB_GET_INT8(pRaw, dataPos, &action.msgReceived, _OVER)
309 310 311 312 313 314
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
      action.pCont = taosMemoryMalloc(action.contLen);
      if (action.pCont == NULL) goto _OVER;
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
      if (taosArrayPush(pTrans->redoActions, &action) == NULL) goto _OVER;
      action.pCont = NULL;
315 316
    } else {
      if (taosArrayPush(pTrans->redoActions, &action) == NULL) goto _OVER;
317
    }
S
Shengliang Guan 已提交
318 319 320
  }

  for (int32_t i = 0; i < undoActionNum; ++i) {
321 322 323
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
324 325
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
    action.actionType = actionType;
S
Shengliang Guan 已提交
326 327
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
    action.stage = stage;
328
    if (action.actionType == TRANS_ACTION_RAW) {
329
      SDB_GET_INT8(pRaw, dataPos, &action.rawWritten, _OVER)
330
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
331 332
      action.pRaw = taosMemoryMalloc(dataLen);
      if (action.pRaw == NULL) goto _OVER;
333
      mTrace("raw:%p, is created", pData);
334 335 336
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
      if (taosArrayPush(pTrans->undoActions, &action) == NULL) goto _OVER;
      action.pRaw = NULL;
337
    } else if (action.actionType == TRANS_ACTION_MSG) {
338 339
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
340 341
      SDB_GET_INT8(pRaw, dataPos, &action.msgSent, _OVER)
      SDB_GET_INT8(pRaw, dataPos, &action.msgReceived, _OVER)
342 343 344 345 346 347
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
      action.pCont = taosMemoryMalloc(action.contLen);
      if (action.pCont == NULL) goto _OVER;
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
      if (taosArrayPush(pTrans->undoActions, &action) == NULL) goto _OVER;
      action.pCont = NULL;
348 349
    } else {
      if (taosArrayPush(pTrans->redoActions, &action) == NULL) goto _OVER;
350 351 352 353
    }
  }

  for (int32_t i = 0; i < commitActionNum; ++i) {
354 355 356
    SDB_GET_INT32(pRaw, dataPos, &action.id, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &action.errCode, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, _OVER)
357 358
    SDB_GET_INT8(pRaw, dataPos, &actionType, _OVER)
    action.actionType = actionType;
S
Shengliang Guan 已提交
359 360
    SDB_GET_INT8(pRaw, dataPos, &stage, _OVER)
    action.stage = stage;
361
    if (action.actionType) {
362
      SDB_GET_INT8(pRaw, dataPos, &action.rawWritten, _OVER)
363
      SDB_GET_INT32(pRaw, dataPos, &dataLen, _OVER)
364 365
      action.pRaw = taosMemoryMalloc(dataLen);
      if (action.pRaw == NULL) goto _OVER;
366
      mTrace("raw:%p, is created", action.pRaw);
367 368 369
      SDB_GET_BINARY(pRaw, dataPos, (void *)action.pRaw, dataLen, _OVER);
      if (taosArrayPush(pTrans->commitActions, &action) == NULL) goto _OVER;
      action.pRaw = NULL;
370
    } else if (action.actionType == TRANS_ACTION_MSG) {
371 372
      SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), _OVER);
      SDB_GET_INT16(pRaw, dataPos, &action.msgType, _OVER)
373 374
      SDB_GET_INT8(pRaw, dataPos, &action.msgSent, _OVER)
      SDB_GET_INT8(pRaw, dataPos, &action.msgReceived, _OVER)
375 376 377 378 379 380
      SDB_GET_INT32(pRaw, dataPos, &action.contLen, _OVER)
      action.pCont = taosMemoryMalloc(action.contLen);
      if (action.pCont == NULL) goto _OVER;
      SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, _OVER);
      if (taosArrayPush(pTrans->commitActions, &action) == NULL) goto _OVER;
      action.pCont = NULL;
381 382
    } else {
      if (taosArrayPush(pTrans->redoActions, &action) == NULL) goto _OVER;
383
    }
S
Shengliang Guan 已提交
384 385
  }

S
Shengliang Guan 已提交
386 387 388
  SDB_GET_INT32(pRaw, dataPos, &pTrans->startFunc, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pTrans->stopFunc, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pTrans->paramLen, _OVER)
389 390
  if (pTrans->paramLen != 0) {
    pTrans->param = taosMemoryMalloc(pTrans->paramLen);
S
Shengliang Guan 已提交
391
    SDB_GET_BINARY(pRaw, dataPos, pTrans->param, pTrans->paramLen, _OVER);
392 393
  }

S
Shengliang Guan 已提交
394
  SDB_GET_RESERVE(pRaw, dataPos, TRANS_RESERVE_SIZE, _OVER)
395 396

  terrno = 0;
S
Shengliang Guan 已提交
397

S
Shengliang Guan 已提交
398
_OVER:
399 400 401
  if (terrno != 0) {
    mError("trans:%d, failed to parse from raw:%p since %s", pTrans->id, pRaw, terrstr());
    mndTransDropData(pTrans);
wafwerar's avatar
wafwerar 已提交
402 403
    taosMemoryFreeClear(pRow);
    taosMemoryFreeClear(action.pCont);
S
Shengliang Guan 已提交
404 405 406
    return NULL;
  }

S
Shengliang Guan 已提交
407
  mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
S
Shengliang Guan 已提交
408 409 410
  return pRow;
}

S
Shengliang Guan 已提交
411 412 413 414 415 416 417 418
static const char *mndTransStr(ETrnStage stage) {
  switch (stage) {
    case TRN_STAGE_PREPARE:
      return "prepare";
    case TRN_STAGE_REDO_ACTION:
      return "redoAction";
    case TRN_STAGE_ROLLBACK:
      return "rollback";
419 420 421 422 423 424
    case TRN_STAGE_UNDO_ACTION:
      return "undoAction";
    case TRN_STAGE_COMMIT:
      return "commit";
    case TRN_STAGE_COMMIT_ACTION:
      return "commitAction";
S
Shengliang Guan 已提交
425 426 427 428 429 430 431
    case TRN_STAGE_FINISHED:
      return "finished";
    default:
      return "invalid";
  }
}

432 433 434 435 436 437 438 439
static void mndTransTestStartFunc(SMnode *pMnode, void *param, int32_t paramLen) {
  mInfo("test trans start, param:%s, len:%d", (char *)param, paramLen);
}

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

S
Shengliang Guan 已提交
440
static TransCbFp mndTransGetCbFp(ETrnFunc ftype) {
441
  switch (ftype) {
S
Shengliang Guan 已提交
442
    case TRANS_START_FUNC_TEST:
443
      return mndTransTestStartFunc;
S
Shengliang Guan 已提交
444
    case TRANS_STOP_FUNC_TEST:
445
      return mndTransTestStopFunc;
S
Shengliang Guan 已提交
446
    case TRANS_START_FUNC_MQ_REB:
L
Liu Jicong 已提交
447
      return mndRebCntInc;
448
    case TRANS_STOP_FUNC_MQ_REB:
L
Liu Jicong 已提交
449
      return mndRebCntDec;
450 451 452 453 454
    default:
      return NULL;
  }
}

S
Shengliang Guan 已提交
455
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
S
Shengliang Guan 已提交
456
  mTrace("trans:%d, perform insert action, row:%p stage:%s", pTrans->id, pTrans, mndTransStr(pTrans->stage));
457 458 459 460 461 462 463 464

  if (pTrans->startFunc > 0) {
    TransCbFp fp = mndTransGetCbFp(pTrans->startFunc);
    if (fp) {
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
    }
  }

S
Shengliang Guan 已提交
465 466 467
  return 0;
}

468
static void mndTransDropData(STrans *pTrans) {
S
Shengliang Guan 已提交
469 470
  mndTransDropActions(pTrans->redoActions);
  mndTransDropActions(pTrans->undoActions);
471
  mndTransDropActions(pTrans->commitActions);
S
Shengliang Guan 已提交
472
  if (pTrans->rpcRsp != NULL) {
wafwerar's avatar
wafwerar 已提交
473
    taosMemoryFree(pTrans->rpcRsp);
S
Shengliang Guan 已提交
474 475 476
    pTrans->rpcRsp = NULL;
    pTrans->rpcRspLen = 0;
  }
477 478 479 480 481
  if (pTrans->param != NULL) {
    taosMemoryFree(pTrans->param);
    pTrans->param = NULL;
    pTrans->paramLen = 0;
  }
482
}
S
Shengliang Guan 已提交
483

484
static int32_t mndTransActionDelete(SSdb *pSdb, STrans *pTrans, bool callFunc) {
485
  mTrace("trans:%d, perform delete action, row:%p stage:%s callfunc:%d", pTrans->id, pTrans, mndTransStr(pTrans->stage),
486 487 488 489 490 491 492 493
         callFunc);
  if (pTrans->stopFunc > 0 && callFunc) {
    TransCbFp fp = mndTransGetCbFp(pTrans->stopFunc);
    if (fp) {
      (*fp)(pSdb->pMnode, pTrans->param, pTrans->paramLen);
    }
  }

494
  mndTransDropData(pTrans);
S
Shengliang Guan 已提交
495 496 497
  return 0;
}

498 499 500 501 502 503 504 505
static void mndTransUpdateActions(SArray *pOldArray, SArray *pNewArray) {
  for (int32_t i = 0; i < taosArrayGetSize(pOldArray); ++i) {
    STransAction *pOldAction = taosArrayGet(pOldArray, i);
    STransAction *pNewAction = taosArrayGet(pNewArray, i);
    pOldAction->rawWritten = pNewAction->rawWritten;
    pOldAction->msgSent = pNewAction->msgSent;
    pOldAction->msgReceived = pNewAction->msgReceived;
    pOldAction->errCode = pNewAction->errCode;
506
  }
507
}
508

509
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) {
S
Shengliang Guan 已提交
510 511
  mTrace("trans:%d, perform update action, old row:%p stage:%s, new row:%p stage:%s", pOld->id, pOld,
         mndTransStr(pOld->stage), pNew, mndTransStr(pNew->stage));
512 513 514
  mndTransUpdateActions(pOld->redoActions, pNew->redoActions);
  mndTransUpdateActions(pOld->undoActions, pNew->undoActions);
  mndTransUpdateActions(pOld->commitActions, pNew->commitActions);
S
Shengliang Guan 已提交
515
  pOld->stage = pNew->stage;
516
  pOld->redoActionPos = pNew->redoActionPos;
517 518 519 520 521 522 523 524 525 526

  if (pOld->stage == TRN_STAGE_COMMIT) {
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
    mTrace("trans:%d, stage from commit to commitAction", pNew->id);
  }

  if (pOld->stage == TRN_STAGE_ROLLBACK) {
    pOld->stage = TRN_STAGE_FINISHED;
    mTrace("trans:%d, stage from rollback to finished", pNew->id);
  }
S
Shengliang Guan 已提交
527 528 529
  return 0;
}

530
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
531
  STrans *pTrans = sdbAcquire(pMnode->pSdb, SDB_TRANS, &transId);
S
Shengliang Guan 已提交
532 533 534 535
  if (pTrans == NULL) {
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
  }
  return pTrans;
S
Shengliang Guan 已提交
536 537
}

538
void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
539 540 541 542
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pTrans);
}

543
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnConflct conflict, const SRpcMsg *pReq) {
wafwerar's avatar
wafwerar 已提交
544
  STrans *pTrans = taosMemoryCalloc(1, sizeof(STrans));
S
Shengliang Guan 已提交
545 546 547 548 549 550
  if (pTrans == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("failed to create transaction since %s", terrstr());
    return NULL;
  }

S
Shengliang Guan 已提交
551
  pTrans->id = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
S
Shengliang Guan 已提交
552 553
  pTrans->stage = TRN_STAGE_PREPARE;
  pTrans->policy = policy;
554 555
  pTrans->conflict = conflict;
  pTrans->exec = TRN_EXEC_PRARLLEL;
S
Shengliang Guan 已提交
556
  pTrans->createdTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
557 558
  pTrans->redoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
  pTrans->undoActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
559
  pTrans->commitActions = taosArrayInit(TRANS_ARRAY_SIZE, sizeof(STransAction));
S
Shengliang Guan 已提交
560

561
  if (pTrans->redoActions == NULL || pTrans->undoActions == NULL || pTrans->commitActions == NULL) {
S
Shengliang Guan 已提交
562 563 564 565 566
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("failed to create transaction since %s", terrstr());
    return NULL;
  }

567 568
  if (pReq != NULL) pTrans->rpcInfo = pReq->info;
  mTrace("trans:%d, local object is created, data:%p", pTrans->id, pTrans);
S
Shengliang Guan 已提交
569 570 571
  return pTrans;
}

S
Shengliang Guan 已提交
572
static void mndTransDropActions(SArray *pArray) {
S
Shengliang Guan 已提交
573 574
  int32_t size = taosArrayGetSize(pArray);
  for (int32_t i = 0; i < size; ++i) {
S
Shengliang Guan 已提交
575
    STransAction *pAction = taosArrayGet(pArray, i);
576
    if (pAction->actionType == TRANS_ACTION_RAW) {
577
      taosMemoryFreeClear(pAction->pRaw);
578
    } else if (pAction->actionType == TRANS_ACTION_MSG) {
579
      taosMemoryFreeClear(pAction->pCont);
580 581
    } else {
      // nothing
582
    }
S
Shengliang Guan 已提交
583 584 585 586 587
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
588
void mndTransDrop(STrans *pTrans) {
S
Shengliang 已提交
589 590
  if (pTrans != NULL) {
    mndTransDropData(pTrans);
591
    mTrace("trans:%d, local object is freed, data:%p", pTrans->id, pTrans);
wafwerar's avatar
wafwerar 已提交
592
    taosMemoryFreeClear(pTrans);
S
Shengliang 已提交
593
  }
S
Shengliang Guan 已提交
594 595
}

596
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
597 598
  pAction->id = taosArrayGetSize(pArray);

599
  void *ptr = taosArrayPush(pArray, pAction);
S
Shengliang Guan 已提交
600 601 602 603 604 605 606 607
  if (ptr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

608
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
609 610 611 612 613 614
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
  return mndTransAppendAction(pTrans->redoActions, &action);
}

int32_t mndTransAppendNullLog(STrans *pTrans) {
  STransAction action = {.stage = TRN_STAGE_REDO_ACTION, .actionType = TRANS_ACTION_NULL};
615 616
  return mndTransAppendAction(pTrans->redoActions, &action);
}
S
Shengliang Guan 已提交
617

618
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
619
  STransAction action = {.stage = TRN_STAGE_UNDO_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
620 621
  return mndTransAppendAction(pTrans->undoActions, &action);
}
S
Shengliang Guan 已提交
622

623
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
624
  STransAction action = {.stage = TRN_STAGE_COMMIT_ACTION, .actionType = TRANS_ACTION_RAW, .pRaw = pRaw};
625
  return mndTransAppendAction(pTrans->commitActions, &action);
S
Shengliang Guan 已提交
626 627
}

S
Shengliang Guan 已提交
628
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
629
  pAction->stage = TRN_STAGE_REDO_ACTION;
630
  pAction->actionType = TRANS_ACTION_MSG;
S
Shengliang Guan 已提交
631
  return mndTransAppendAction(pTrans->redoActions, pAction);
S
Shengliang Guan 已提交
632 633
}

S
Shengliang Guan 已提交
634
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
635
  pAction->stage = TRN_STAGE_UNDO_ACTION;
636
  pAction->actionType = TRANS_ACTION_MSG;
S
Shengliang Guan 已提交
637
  return mndTransAppendAction(pTrans->undoActions, pAction);
S
Shengliang Guan 已提交
638 639
}

S
Shengliang Guan 已提交
640 641 642 643 644
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
  pTrans->rpcRsp = pCont;
  pTrans->rpcRspLen = contLen;
}

S
Shengliang Guan 已提交
645
void mndTransSetCb(STrans *pTrans, ETrnFunc startFunc, ETrnFunc stopFunc, void *param, int32_t paramLen) {
646 647 648 649
  pTrans->startFunc = startFunc;
  pTrans->stopFunc = stopFunc;
  pTrans->param = param;
  pTrans->paramLen = paramLen;
650 651
}

652
void mndTransSetDbName(STrans *pTrans, const char *dbname) { memcpy(pTrans->dbname, dbname, TSDB_DB_FNAME_LEN); }
S
Shengliang Guan 已提交
653

654
void mndTransSetSerial(STrans *pTrans) { pTrans->exec = TRN_EXEC_SERIAL; }
655

S
Shengliang Guan 已提交
656
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
657
  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
S
Shengliang Guan 已提交
658
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
659
    mError("trans:%d, failed to encode while sync trans since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
660 661
    return -1;
  }
S
Shengliang Guan 已提交
662
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
663

664
  mDebug("trans:%d, sync to other mnodes", pTrans->id);
S
Shengliang Guan 已提交
665
  int32_t code = mndSyncPropose(pMnode, pRaw, pTrans->id);
S
Shengliang Guan 已提交
666 667 668
  if (code != 0) {
    mError("trans:%d, failed to sync since %s", pTrans->id, terrstr());
    sdbFreeRaw(pRaw);
S
Shengliang Guan 已提交
669 670 671
    return -1;
  }

672
  sdbFreeRaw(pRaw);
S
Shengliang Guan 已提交
673
  mDebug("trans:%d, sync finished", pTrans->id);
S
Shengliang Guan 已提交
674 675 676
  return 0;
}

677
static bool mndCheckTransConflict(SMnode *pMnode, STrans *pNew) {
S
Shengliang Guan 已提交
678
  STrans *pTrans = NULL;
679
  void   *pIter = NULL;
680
  bool    conflict = false;
681

682
  if (pNew->conflict == TRN_CONFLICT_NOTHING) return conflict;
S
Shengliang Guan 已提交
683 684 685 686 687

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

688 689 690 691 692 693 694 695 696
    if (pNew->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
    if (pNew->conflict == TRN_CONFLICT_DB) {
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
      if (pTrans->conflict == TRN_CONFLICT_DB && strcmp(pNew->dbname, pTrans->dbname) == 0) conflict = true;
      if (pTrans->conflict == TRN_CONFLICT_DB_INSIDE && strcmp(pNew->dbname, pTrans->dbname) == 0) conflict = true;
    }
    if (pNew->conflict == TRN_CONFLICT_DB_INSIDE) {
      if (pTrans->conflict == TRN_CONFLICT_GLOBAL) conflict = true;
      if (pTrans->conflict == TRN_CONFLICT_DB && strcmp(pNew->dbname, pTrans->dbname) == 0) conflict = true;
S
Shengliang Guan 已提交
697
    }
698
    mError("trans:%d, can't execute since conflict with trans:%d, db:%s", pNew->id, pTrans->id, pTrans->dbname);
S
Shengliang Guan 已提交
699 700 701
    sdbRelease(pMnode->pSdb, pTrans);
  }

702
  return conflict;
S
Shengliang Guan 已提交
703 704
}

S
Shengliang Guan 已提交
705
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
706
  if (pTrans->conflict == TRN_CONFLICT_DB || pTrans->conflict == TRN_CONFLICT_DB_INSIDE) {
707 708 709 710 711 712 713
    if (strlen(pTrans->dbname) == 0) {
      terrno = TSDB_CODE_MND_TRANS_CONFLICT;
      mError("trans:%d, failed to prepare conflict db not set", pTrans->id);
      return -1;
    }
  }

714 715
  if (mndCheckTransConflict(pMnode, pTrans)) {
    terrno = TSDB_CODE_MND_TRANS_CONFLICT;
S
Shengliang Guan 已提交
716 717 718 719
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    return -1;
  }

720
  if (taosArrayGetSize(pTrans->commitActions) <= 0) {
721 722 723 724 725
    terrno = TSDB_CODE_MND_TRANS_CLOG_IS_NULL;
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
726 727 728 729 730 731 732
  mDebug("trans:%d, prepare transaction", pTrans->id);
  if (mndTransSync(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    return -1;
  }
  mDebug("trans:%d, prepare finished", pTrans->id);

S
Shengliang Guan 已提交
733 734
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
  if (pNew == NULL) {
S
Shengliang Guan 已提交
735
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
736 737 738
    return -1;
  }

S
Shengliang Guan 已提交
739
  pNew->rpcInfo = pTrans->rpcInfo;
S
Shengliang Guan 已提交
740 741 742 743 744
  pNew->rpcRsp = pTrans->rpcRsp;
  pNew->rpcRspLen = pTrans->rpcRspLen;
  pTrans->rpcRsp = NULL;
  pTrans->rpcRspLen = 0;

S
Shengliang Guan 已提交
745 746
  mndTransExecute(pMnode, pNew);
  mndReleaseTrans(pMnode, pNew);
S
Shengliang Guan 已提交
747 748 749
  return 0;
}

S
Shengliang Guan 已提交
750 751 752 753
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
  mDebug("trans:%d, commit transaction", pTrans->id);
  if (mndTransSync(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to commit since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
754
    return -1;
S
Shengliang Guan 已提交
755 756
  }
  mDebug("trans:%d, commit finished", pTrans->id);
S
Shengliang Guan 已提交
757 758 759
  return 0;
}

S
Shengliang Guan 已提交
760
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
761
  mDebug("trans:%d, rollback transaction", pTrans->id);
S
Shengliang Guan 已提交
762 763
  if (mndTransSync(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to rollback since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
764
    return -1;
S
Shengliang Guan 已提交
765
  }
S
Shengliang Guan 已提交
766 767
  mDebug("trans:%d, rollback finished", pTrans->id);
  return 0;
S
Shengliang Guan 已提交
768
}
S
Shengliang Guan 已提交
769

770
static void mndTransSendRpcRsp(SMnode *pMnode, STrans *pTrans) {
771 772
  bool    sendRsp = false;
  int32_t code = pTrans->code;
773 774 775 776 777 778

  if (pTrans->stage == TRN_STAGE_FINISHED) {
    sendRsp = true;
  }

  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
779
    if (pTrans->stage == TRN_STAGE_UNDO_ACTION || pTrans->stage == TRN_STAGE_ROLLBACK) {
780
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
781
      sendRsp = true;
782
    }
783
  } else {
784
    if (pTrans->stage == TRN_STAGE_REDO_ACTION && pTrans->failedTimes > 2) {
785
      if (code == 0) code = TSDB_CODE_MND_TRANS_UNKNOW_ERROR;
786
      sendRsp = true;
787
    }
S
Shengliang Guan 已提交
788
  }
789

S
Shengliang Guan 已提交
790
  if (sendRsp && pTrans->rpcInfo.handle != NULL) {
791 792
    mDebug("trans:%d, send rsp, code:0x%x stage:%s app:%p", pTrans->id, code, mndTransStr(pTrans->stage),
           pTrans->rpcInfo.ahandle);
793
    if (code == TSDB_CODE_RPC_NETWORK_UNAVAIL) {
794
      code = TSDB_CODE_MND_TRANS_NETWORK_UNAVAILL;
795 796 797 798 799 800 801 802 803 804 805 806 807
    }
    SRpcMsg rspMsg = {.code = code, .info = pTrans->rpcInfo};

    if (pTrans->rpcRspLen != 0) {
      void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
      if (rpcCont != NULL) {
        memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
        rspMsg.pCont = rpcCont;
        rspMsg.contLen = pTrans->rpcRspLen;
      }
      taosMemoryFree(pTrans->rpcRsp);
    }

S
shm  
Shengliang Guan 已提交
808
    tmsgSendRsp(&rspMsg);
S
Shengliang Guan 已提交
809
    pTrans->rpcInfo.handle = NULL;
S
Shengliang Guan 已提交
810 811
    pTrans->rpcRsp = NULL;
    pTrans->rpcRspLen = 0;
812
  }
S
Shengliang Guan 已提交
813 814
}

815
int32_t mndTransProcessRsp(SRpcMsg *pRsp) {
S
Shengliang Guan 已提交
816 817
  SMnode *pMnode = pRsp->info.node;
  int64_t signature = (int64_t)(pRsp->info.ahandle);
S
Shengliang Guan 已提交
818 819
  int32_t transId = (int32_t)(signature >> 32);
  int32_t action = (int32_t)((signature << 32) >> 32);
820 821 822 823

  STrans *pTrans = mndAcquireTrans(pMnode, transId);
  if (pTrans == NULL) {
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, terrstr());
S
Shengliang Guan 已提交
824
    goto _OVER;
825 826 827
  }

  SArray *pArray = NULL;
S
Shengliang Guan 已提交
828
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
829
    pArray = pTrans->redoActions;
S
Shengliang Guan 已提交
830
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
831 832
    pArray = pTrans->undoActions;
  } else {
S
Shengliang Guan 已提交
833
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
S
Shengliang Guan 已提交
834
    goto _OVER;
835 836 837
  }

  if (pArray == NULL) {
S
Shengliang Guan 已提交
838
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
S
Shengliang Guan 已提交
839
    goto _OVER;
840 841 842
  }

  int32_t actionNum = taosArrayGetSize(pTrans->redoActions);
S
Shengliang Guan 已提交
843
  if (action < 0 || action >= actionNum) {
844
    mError("trans:%d, invalid action:%d", transId, action);
S
Shengliang Guan 已提交
845
    goto _OVER;
846 847 848 849 850
  }

  STransAction *pAction = taosArrayGet(pArray, action);
  if (pAction != NULL) {
    pAction->msgReceived = 1;
S
Shengliang Guan 已提交
851
    pAction->errCode = pRsp->code;
852 853
  }

854 855
  mDebug("trans:%d, %s:%d response is received, code:0x%x, accept:0x%x", transId, mndTransStr(pAction->stage), action,
         pRsp->code, pAction->acceptableCode);
856 857
  mndTransExecute(pMnode, pTrans);

S
Shengliang Guan 已提交
858
_OVER:
859
  mndReleaseTrans(pMnode, pTrans);
860
  return 0;
861 862
}

S
Shengliang Guan 已提交
863 864 865 866 867
static void mndTransResetActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
  int32_t numOfActions = taosArrayGetSize(pArray);

  for (int32_t action = 0; action < numOfActions; ++action) {
    STransAction *pAction = taosArrayGet(pArray, action);
868 869 870 871
    if (pAction->msgSent && pAction->msgReceived &&
        (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode))
      continue;
    if (pAction->rawWritten && (pAction->errCode == 0 || pAction->errCode == pAction->acceptableCode)) continue;
S
Shengliang Guan 已提交
872

873
    pAction->rawWritten = 0;
S
Shengliang Guan 已提交
874 875
    pAction->msgSent = 0;
    pAction->msgReceived = 0;
876 877 878 879 880 881 882
    if (pAction->errCode == TSDB_CODE_RPC_REDIRECT) {
      pAction->epSet.inUse = (pAction->epSet.inUse + 1) % pAction->epSet.numOfEps;
      mDebug("trans:%d, %s:%d execute status is reset and set epset inuse:%d", pTrans->id, mndTransStr(pAction->stage),
             action, pAction->epSet.inUse);
    } else {
      mDebug("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), action);
    }
S
Shengliang Guan 已提交
883 884 885 886
    pAction->errCode = 0;
  }
}

887
static int32_t mndTransWriteSingleLog(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
888 889
  if (pAction->rawWritten) return 0;

890
  int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pAction->pRaw);
891 892 893 894
  if (code == 0 || terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
    pAction->rawWritten = true;
    pAction->errCode = 0;
    code = 0;
S
Shengliang Guan 已提交
895 896
    mDebug("trans:%d, %s:%d write to sdb, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
           sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
897
  } else {
898
    pAction->errCode = (terrno != 0) ? terrno : code;
S
Shengliang Guan 已提交
899 900
    mError("trans:%d, %s:%d failed to write sdb since %s, type:%s status:%s", pTrans->id, mndTransStr(pAction->stage),
           pAction->id, terrstr(), sdbTableName(pAction->pRaw->type), sdbStatusName(pAction->pRaw->status));
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
  }

  return code;
}

static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
  if (pAction->msgSent) return 0;
  if (!pMnode->deploy && !mndIsMaster(pMnode)) return -1;

  int64_t signature = pTrans->id;
  signature = (signature << 32);
  signature += pAction->id;

  SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .info.ahandle = (void *)signature};
  rpcMsg.pCont = rpcMallocCont(pAction->contLen);
  if (rpcMsg.pCont == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);

S
Shengliang Guan 已提交
922 923 924
  char    detail[1024] = {0};
  int32_t len = snprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", TMSG_INFO(pAction->msgType),
                         pAction->epSet.numOfEps, pAction->epSet.inUse);
S
Shengliang Guan 已提交
925
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
S
Shengliang Guan 已提交
926 927 928 929
    len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
                    pAction->epSet.eps[i].port);
  }

930 931 932 933 934
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
  if (code == 0) {
    pAction->msgSent = 1;
    pAction->msgReceived = 0;
    pAction->errCode = 0;
S
Shengliang Guan 已提交
935
    mDebug("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
936 937 938 939
  } else {
    pAction->msgSent = 0;
    pAction->msgReceived = 0;
    pAction->errCode = (terrno != 0) ? terrno : code;
S
Shengliang Guan 已提交
940 941
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
           detail);
942 943 944 945 946 947
  }

  return code;
}

static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
948
  if (pAction->actionType == TRANS_ACTION_RAW) {
949
    return mndTransWriteSingleLog(pMnode, pTrans, pAction);
950
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
951
    return mndTransSendSingleMsg(pMnode, pTrans, pAction);
952 953 954 955 956
  } else {
    pAction->rawWritten = 0;
    pAction->errCode = 0;
    mDebug("trans:%d, %s:%d null action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
    return 0;
957 958 959
  }
}

960
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
961
  int32_t numOfActions = taosArrayGetSize(pArray);
962
  int32_t code = 0;
963 964 965

  for (int32_t action = 0; action < numOfActions; ++action) {
    STransAction *pAction = taosArrayGet(pArray, action);
966
    code = mndTransExecSingleAction(pMnode, pTrans, pAction);
967
    if (code != 0) break;
S
Shengliang Guan 已提交
968 969
  }

970
  return code;
S
Shengliang Guan 已提交
971 972 973 974 975 976
}

static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
  int32_t numOfActions = taosArrayGetSize(pArray);
  if (numOfActions == 0) return 0;

977
  if (mndTransExecSingleActions(pMnode, pTrans, pArray) != 0) {
S
Shengliang Guan 已提交
978 979 980
    return -1;
  }

981 982 983
  int32_t       numOfExecuted = 0;
  int32_t       errCode = 0;
  STransAction *pErrAction = NULL;
984 985
  for (int32_t action = 0; action < numOfActions; ++action) {
    STransAction *pAction = taosArrayGet(pArray, action);
986
    if (pAction->msgReceived || pAction->rawWritten) {
987
      numOfExecuted++;
988
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
S
Shengliang Guan 已提交
989
        errCode = pAction->errCode;
990
        pErrAction = pAction;
991 992 993 994
      }
    }
  }

995
  if (numOfExecuted == numOfActions) {
S
Shengliang Guan 已提交
996
    if (errCode == 0) {
997 998 999 1000
      pTrans->lastErrorAction = 0;
      pTrans->lastErrorNo = 0;
      pTrans->lastErrorMsgType = 0;
      memset(&pTrans->lastErrorEpset, 0, sizeof(pTrans->lastErrorEpset));
S
Shengliang Guan 已提交
1001 1002 1003
      mDebug("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
      return 0;
    } else {
S
Shengliang Guan 已提交
1004
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1005 1006 1007 1008 1009 1010
      if (pErrAction != NULL) {
        pTrans->lastErrorMsgType = pErrAction->msgType;
        pTrans->lastErrorAction = pErrAction->id;
        pTrans->lastErrorNo = pErrAction->errCode;
        pTrans->lastErrorEpset = pErrAction->epSet;
      }
S
Shengliang Guan 已提交
1011 1012 1013 1014
      mndTransResetActions(pMnode, pTrans, pArray);
      terrno = errCode;
      return errCode;
    }
1015
  } else {
1016
    mDebug("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
S
Shengliang Guan 已提交
1017
    return TSDB_CODE_ACTION_IN_PROGRESS;
1018
  }
S
Shengliang Guan 已提交
1019 1020
}

S
Shengliang Guan 已提交
1021
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans) {
1022
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions);
S
Shengliang Guan 已提交
1023
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1024
    mError("failed to execute redoActions since:%s, code:0x%x", terrstr(), terrno);
1025 1026
  }
  return code;
S
Shengliang Guan 已提交
1027
}
S
Shengliang Guan 已提交
1028

S
Shengliang Guan 已提交
1029
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans) {
1030
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions);
S
Shengliang Guan 已提交
1031
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1032 1033 1034
    mError("failed to execute undoActions since %s", terrstr());
  }
  return code;
S
Shengliang Guan 已提交
1035
}
S
Shengliang Guan 已提交
1036

1037
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans) {
1038
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions);
1039
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1040 1041 1042 1043 1044
    mError("failed to execute commitActions since %s", terrstr());
  }
  return code;
}

1045
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans) {
1046 1047 1048 1049
  int32_t code = 0;
  int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
  if (numOfActions == 0) return code;
  if (pTrans->redoActionPos >= numOfActions) return code;
S
Shengliang Guan 已提交
1050

1051 1052
  for (int32_t action = pTrans->redoActionPos; action < numOfActions; ++action) {
    STransAction *pAction = taosArrayGet(pTrans->redoActions, pTrans->redoActionPos);
S
Shengliang Guan 已提交
1053

1054 1055 1056 1057 1058 1059
    code = mndTransExecSingleAction(pMnode, pTrans, pAction);
    if (code == 0) {
      if (pAction->msgSent) {
        if (pAction->msgReceived) {
          if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
            code = pAction->errCode;
1060 1061 1062
            pAction->msgSent = 0;
            pAction->msgReceived = 0;
            mDebug("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), action);
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
          }
        } else {
          code = TSDB_CODE_ACTION_IN_PROGRESS;
        }
      }
      if (pAction->rawWritten) {
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
          code = pAction->errCode;
        }
      }
    }
1074

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    if (code == 0) {
      pTrans->lastErrorAction = 0;
      pTrans->lastErrorNo = 0;
      pTrans->lastErrorMsgType = 0;
      memset(&pTrans->lastErrorEpset, 0, sizeof(pTrans->lastErrorEpset));
    } else {
      pTrans->lastErrorMsgType = pAction->msgType;
      pTrans->lastErrorAction = action;
      pTrans->lastErrorNo = pAction->errCode;
      pTrans->lastErrorEpset = pAction->epSet;
    }

1087
    if (code == 0) {
1088 1089
      if (!pMnode->deploy && !mndIsMaster(pMnode)) break;

1090
      pTrans->code = 0;
1091 1092 1093 1094 1095
      pTrans->redoActionPos++;
      mDebug("trans:%d, %s:%d is executed and need sync to other mnodes", pTrans->id, mndTransStr(pAction->stage),
             pAction->id);
      code = mndTransSync(pMnode, pTrans);
      if (code != 0) {
1096 1097 1098
        pTrans->code = terrno;
        mError("trans:%d, %s:%d is executed and failed to sync to other mnodes since %s", pTrans->id,
               mndTransStr(pAction->stage), pAction->id, terrstr());
1099 1100 1101 1102 1103 1104
        break;
      }
    } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
      mDebug("trans:%d, %s:%d is in progress and wait it finish", pTrans->id, mndTransStr(pAction->stage), pAction->id);
      break;
    } else {
1105 1106
      terrno = code;
      pTrans->code = code;
1107 1108 1109 1110
      mError("trans:%d, %s:%d failed to execute since %s", pTrans->id, mndTransStr(pAction->stage), pAction->id,
             terrstr());
      break;
    }
S
Shengliang Guan 已提交
1111
  }
S
Shengliang Guan 已提交
1112

1113 1114 1115 1116 1117 1118 1119
  return code;
}

static bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans) {
  bool continueExec = true;
  pTrans->stage = TRN_STAGE_REDO_ACTION;
  mDebug("trans:%d, stage from prepare to redoAction", pTrans->id);
S
Shengliang Guan 已提交
1120
  return continueExec;
S
Shengliang Guan 已提交
1121 1122
}

S
Shengliang Guan 已提交
1123 1124
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans) {
  bool    continueExec = true;
1125 1126
  int32_t code = 0;

1127
  if (pTrans->exec == TRN_EXEC_SERIAL) {
1128
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans);
1129 1130 1131
  } else {
    code = mndTransExecuteRedoActions(pMnode, pTrans);
  }
S
Shengliang Guan 已提交
1132 1133

  if (code == 0) {
S
Shengliang Guan 已提交
1134
    pTrans->code = 0;
S
Shengliang Guan 已提交
1135
    pTrans->stage = TRN_STAGE_COMMIT;
S
Shengliang Guan 已提交
1136 1137
    mDebug("trans:%d, stage from redoAction to commit", pTrans->id);
    continueExec = true;
S
Shengliang Guan 已提交
1138
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1139 1140
    mDebug("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
    continueExec = false;
S
Shengliang Guan 已提交
1141
  } else {
S
Shengliang Guan 已提交
1142
    pTrans->code = terrno;
S
Shengliang Guan 已提交
1143
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
S
Shengliang Guan 已提交
1144 1145 1146
      pTrans->stage = TRN_STAGE_UNDO_ACTION;
      mError("trans:%d, stage from redoAction to undoAction since %s", pTrans->id, terrstr());
      continueExec = true;
S
Shengliang Guan 已提交
1147
    } else {
S
Shengliang Guan 已提交
1148 1149 1150
      pTrans->failedTimes++;
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
      continueExec = false;
S
Shengliang Guan 已提交
1151 1152 1153
    }
  }

S
Shengliang Guan 已提交
1154
  return continueExec;
S
Shengliang Guan 已提交
1155 1156
}

S
Shengliang Guan 已提交
1157 1158 1159 1160 1161 1162
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans) {
  bool    continueExec = true;
  int32_t code = mndTransCommit(pMnode, pTrans);

  if (code == 0) {
    pTrans->code = 0;
1163 1164
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
    mDebug("trans:%d, stage from commit to commitAction", pTrans->id);
S
Shengliang Guan 已提交
1165 1166 1167 1168
    continueExec = true;
  } else {
    pTrans->code = terrno;
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
1169 1170
      pTrans->stage = TRN_STAGE_UNDO_ACTION;
      mError("trans:%d, stage from commit to undoAction since %s, failedTimes:%d", pTrans->id, terrstr(),
S
Shengliang Guan 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
             pTrans->failedTimes);
      continueExec = true;
    } else {
      pTrans->failedTimes++;
      mError("trans:%d, stage keep on commit since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
      continueExec = false;
    }
  }

  return continueExec;
S
Shengliang Guan 已提交
1181 1182
}

1183
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
1184
  bool    continueExec = true;
1185
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans);
S
Shengliang Guan 已提交
1186 1187

  if (code == 0) {
S
Shengliang Guan 已提交
1188 1189
    pTrans->code = 0;
    pTrans->stage = TRN_STAGE_FINISHED;
1190
    mDebug("trans:%d, stage from commitAction to finished", pTrans->id);
S
Shengliang Guan 已提交
1191
    continueExec = true;
S
Shengliang Guan 已提交
1192
  } else {
S
Shengliang Guan 已提交
1193 1194
    pTrans->code = terrno;
    pTrans->failedTimes++;
1195
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
S
Shengliang Guan 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
    continueExec = false;
  }

  return continueExec;
}

static bool mndTransPerformUndoActionStage(SMnode *pMnode, STrans *pTrans) {
  bool    continueExec = true;
  int32_t code = mndTransExecuteUndoActions(pMnode, pTrans);

  if (code == 0) {
1207 1208
    pTrans->stage = TRN_STAGE_ROLLBACK;
    mDebug("trans:%d, stage from undoAction to rollback", pTrans->id);
S
Shengliang Guan 已提交
1209
    continueExec = true;
S
Shengliang Guan 已提交
1210
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1211
    mDebug("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
S
Shengliang Guan 已提交
1212 1213 1214
    continueExec = false;
  } else {
    pTrans->failedTimes++;
1215
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
S
Shengliang Guan 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
    continueExec = false;
  }

  return continueExec;
}

static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans) {
  bool    continueExec = true;
  int32_t code = mndTransRollback(pMnode, pTrans);

  if (code == 0) {
    pTrans->stage = TRN_STAGE_FINISHED;
    mDebug("trans:%d, stage from rollback to finished", pTrans->id);
    continueExec = true;
  } else {
    pTrans->failedTimes++;
1232
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
S
Shengliang Guan 已提交
1233
    continueExec = false;
S
Shengliang Guan 已提交
1234 1235
  }

S
Shengliang Guan 已提交
1236 1237 1238 1239 1240 1241 1242 1243
  return continueExec;
}

static bool mndTransPerfromFinishedStage(SMnode *pMnode, STrans *pTrans) {
  bool continueExec = false;

  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
1244
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
1245 1246 1247 1248 1249 1250 1251 1252
  }
  sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);

  int32_t code = sdbWrite(pMnode->pSdb, pRaw);
  if (code != 0) {
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
  }

1253
  mDebug("trans:%d, execute finished, code:0x%x, failedTimes:%d", pTrans->id, pTrans->code, pTrans->failedTimes);
S
Shengliang Guan 已提交
1254
  return continueExec;
S
Shengliang Guan 已提交
1255
}
S
Shengliang Guan 已提交
1256

S
Shengliang Guan 已提交
1257
static void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
1258
  bool continueExec = true;
S
Shengliang Guan 已提交
1259

S
Shengliang Guan 已提交
1260
  while (continueExec) {
S
Shengliang Guan 已提交
1261
    pTrans->lastExecTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
1262 1263
    switch (pTrans->stage) {
      case TRN_STAGE_PREPARE:
S
Shengliang Guan 已提交
1264 1265 1266 1267 1268
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans);
        break;
      case TRN_STAGE_REDO_ACTION:
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans);
        break;
1269 1270 1271 1272 1273
      case TRN_STAGE_COMMIT:
        continueExec = mndTransPerformCommitStage(pMnode, pTrans);
        break;
      case TRN_STAGE_COMMIT_ACTION:
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans);
S
Shengliang Guan 已提交
1274
        break;
S
Shengliang Guan 已提交
1275 1276 1277
      case TRN_STAGE_UNDO_ACTION:
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans);
        break;
S
Shengliang Guan 已提交
1278
      case TRN_STAGE_ROLLBACK:
S
Shengliang Guan 已提交
1279 1280 1281 1282
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans);
        break;
      case TRN_STAGE_FINISHED:
        continueExec = mndTransPerfromFinishedStage(pMnode, pTrans);
S
Shengliang Guan 已提交
1283
        break;
S
Shengliang Guan 已提交
1284
      default:
S
Shengliang Guan 已提交
1285 1286
        continueExec = false;
        break;
S
Shengliang Guan 已提交
1287 1288 1289
    }
  }

1290
  mndTransSendRpcRsp(pMnode, pTrans);
S
Shengliang Guan 已提交
1291
}
S
Shengliang Guan 已提交
1292

S
Shengliang Guan 已提交
1293 1294
static int32_t mndProcessTransReq(SRpcMsg *pReq) {
  mndTransPullup(pReq->info.node);
S
Shengliang Guan 已提交
1295 1296 1297
  return 0;
}

1298
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
  SArray *pArray = NULL;
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
    pArray = pTrans->redoActions;
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
    pArray = pTrans->undoActions;
  } else {
    terrno = TSDB_CODE_MND_TRANS_INVALID_STAGE;
    return -1;
  }

1309
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
S
Shengliang Guan 已提交
1310 1311
    STransAction *pAction = taosArrayGet(pArray, i);
    if (pAction->errCode != 0) {
1312 1313
      mInfo("trans:%d, %s:%d set processed for kill msg received, errCode from %s to success", pTrans->id,
            mndTransStr(pAction->stage), i, tstrerror(pAction->errCode));
S
Shengliang Guan 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
      pAction->msgSent = 1;
      pAction->msgReceived = 1;
      pAction->errCode = 0;
    }
  }

  mndTransExecute(pMnode, pTrans);
  return 0;
}

S
Shengliang Guan 已提交
1324 1325
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1326
  SKillTransReq killReq = {0};
S
Shengliang Guan 已提交
1327
  int32_t       code = -1;
1328 1329
  SUserObj     *pUser = NULL;
  STrans       *pTrans = NULL;
S
Shengliang Guan 已提交
1330

S
Shengliang Guan 已提交
1331
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
S
Shengliang Guan 已提交
1332
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
1333
    goto _OVER;
S
Shengliang Guan 已提交
1334 1335 1336 1337
  }

  mInfo("trans:%d, start to kill", killReq.transId);

S
Shengliang Guan 已提交
1338
  pUser = mndAcquireUser(pMnode, pReq->conn.user);
S
Shengliang Guan 已提交
1339
  if (pUser == NULL) {
S
Shengliang Guan 已提交
1340
    goto _OVER;
S
Shengliang Guan 已提交
1341 1342
  }

S
Shengliang Guan 已提交
1343
  if (mndCheckTransAuth(pUser) != 0) {
S
Shengliang Guan 已提交
1344
    goto _OVER;
S
Shengliang Guan 已提交
1345 1346
  }

S
Shengliang Guan 已提交
1347
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
S
Shengliang Guan 已提交
1348
  if (pTrans == NULL) {
1349
    goto _OVER;
S
Shengliang Guan 已提交
1350 1351
  }

S
Shengliang Guan 已提交
1352 1353
  code = mndKillTrans(pMnode, pTrans);

S
Shengliang Guan 已提交
1354
_OVER:
1355
  if (code != 0) {
S
Shengliang Guan 已提交
1356 1357 1358
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
  }

1359
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
1360
  mndReleaseTrans(pMnode, pTrans);
S
Shengliang Guan 已提交
1361
  return code;
S
Shengliang Guan 已提交
1362 1363
}

1364 1365
static int32_t mndCompareTransId(int32_t *pTransId1, int32_t *pTransId2) { return *pTransId1 >= *pTransId2 ? 1 : 0; }

S
Shengliang Guan 已提交
1366
void mndTransPullup(SMnode *pMnode) {
1367 1368 1369
  SSdb   *pSdb = pMnode->pSdb;
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
  if (pArray == NULL) return;
S
Shengliang Guan 已提交
1370

1371
  void *pIter = NULL;
S
Shengliang Guan 已提交
1372
  while (1) {
1373
    STrans *pTrans = NULL;
S
Shengliang Guan 已提交
1374 1375
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
    if (pIter == NULL) break;
1376 1377 1378
    taosArrayPush(pArray, &pTrans->id);
    sdbRelease(pSdb, pTrans);
  }
S
Shengliang Guan 已提交
1379

1380 1381 1382 1383 1384 1385 1386 1387 1388
  taosArraySort(pArray, (__compar_fn_t)mndCompareTransId);

  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
    int32_t *pTransId = taosArrayGet(pArray, i);
    STrans  *pTrans = mndAcquireTrans(pMnode, *pTransId);
    if (pTrans != NULL) {
      mndTransExecute(pMnode, pTrans);
    }
    mndReleaseTrans(pMnode, pTrans);
S
Shengliang Guan 已提交
1389
  }
S
Shengliang Guan 已提交
1390

S
Shengliang Guan 已提交
1391 1392 1393 1394
  SSnapshotMeta sMeta = {0};
  if (syncGetSnapshotMeta(pMnode->syncMgmt.sync, &sMeta) == 0) {
    sdbSetCurConfig(pMnode->pSdb, sMeta.lastConfigIndex);
  }
S
Shengliang Guan 已提交
1395
  sdbWriteFile(pMnode->pSdb);
1396
  taosArrayDestroy(pArray);
1397
}
S
Shengliang Guan 已提交
1398

S
Shengliang Guan 已提交
1399 1400
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode *pMnode = pReq->info.node;
1401
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1402 1403 1404
  int32_t numOfRows = 0;
  STrans *pTrans = NULL;
  int32_t cols = 0;
1405
  char   *pWrite;
S
Shengliang Guan 已提交
1406 1407 1408 1409 1410 1411 1412

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_TRANS, pShow->pIter, (void **)&pTrans);
    if (pShow->pIter == NULL) break;

    cols = 0;

S
Shengliang Guan 已提交
1413 1414
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pTrans->id, false);
S
Shengliang Guan 已提交
1415

S
Shengliang Guan 已提交
1416 1417
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pTrans->createdTime, false);
S
Shengliang Guan 已提交
1418

S
Shengliang Guan 已提交
1419
    char stage[TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE] = {0};
1420
    STR_WITH_MAXSIZE_TO_VARSTR(stage, mndTransStr(pTrans->stage), pShow->pMeta->pSchemas[cols].bytes);
S
Shengliang Guan 已提交
1421 1422
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)stage, false);
S
Shengliang Guan 已提交
1423

S
Shengliang Guan 已提交
1424
    char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1425
    STR_WITH_MAXSIZE_TO_VARSTR(dbname, mndGetDbStr(pTrans->dbname), pShow->pMeta->pSchemas[cols].bytes);
S
Shengliang Guan 已提交
1426 1427
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)dbname, false);
S
Shengliang Guan 已提交
1428

1429 1430 1431
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false);

S
Shengliang Guan 已提交
1432 1433
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pTrans->lastExecTime, false);
S
Shengliang Guan 已提交
1434

S
Shengliang Guan 已提交
1435
    char lastError[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
1436 1437 1438 1439 1440 1441 1442 1443
    char detail[TSDB_TRANS_ERROR_LEN] = {0};
    if (pTrans->lastErrorNo != 0) {
      int32_t len = snprintf(detail, sizeof(detail), "action:%d errno:0x%x(%s) ", pTrans->lastErrorAction,
                             pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
      SEpSet  epset = pTrans->lastErrorEpset;
      if (epset.numOfEps > 0) {
        len += snprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
                        TMSG_INFO(pTrans->lastErrorMsgType), epset.numOfEps, epset.inUse);
S
Shengliang Guan 已提交
1444 1445 1446
        for (int32_t i = 0; i < pTrans->lastErrorEpset.numOfEps; ++i) {
          len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
        }
1447 1448 1449
      }
    }
    STR_WITH_MAXSIZE_TO_VARSTR(lastError, detail, pShow->pMeta->pSchemas[cols].bytes);
S
Shengliang Guan 已提交
1450 1451
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)lastError, false);
S
Shengliang Guan 已提交
1452 1453 1454 1455 1456

    numOfRows++;
    sdbRelease(pSdb, pTrans);
  }

1457
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1458 1459 1460 1461 1462 1463 1464
  return numOfRows;
}

static void mndCancelGetNextTrans(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}