mndTrans.c 53.4 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
static bool    mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerfromFinishedStage(SMnode *pMnode, STrans *pTrans);
55
static bool    mndCantExecuteTransAction(SMnode *pMnode) { return !pMnode->deploy && !mndIsMaster(pMnode); }
S
Shengliang Guan 已提交
56

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;
S
Shengliang Guan 已提交
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;
S
Shengliang Guan 已提交
333
      // mTrace("raw:%p, is created", action.pRaw);
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;
S
Shengliang Guan 已提交
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

  if (pOld->stage == TRN_STAGE_COMMIT) {
    pOld->stage = TRN_STAGE_COMMIT_ACTION;
520
    mTrace("trans:%d, stage from commit to commitAction since perform update action", pNew->id);
521 522 523 524
  }

  if (pOld->stage == TRN_STAGE_ROLLBACK) {
    pOld->stage = TRN_STAGE_FINISHED;
525
    mTrace("trans:%d, stage from rollback to finished since perform update action", pNew->id);
526
  }
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 898 899 900 901

    pTrans->lastAction = pAction->id;
    pTrans->lastMsgType = pAction->msgType;
    pTrans->lastEpset = pAction->epSet;
    pTrans->lastErrorNo = 0;
902
  } else {
903
    pAction->errCode = (terrno != 0) ? terrno : code;
S
Shengliang Guan 已提交
904 905
    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));
906 907 908 909
    pTrans->lastAction = pAction->id;
    pTrans->lastMsgType = pAction->msgType;
    pTrans->lastEpset = pAction->epSet;
    pTrans->lastErrorNo = pAction->errCode;
910 911 912 913 914 915 916
  }

  return code;
}

static int32_t mndTransSendSingleMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
  if (pAction->msgSent) return 0;
917
  if (mndCantExecuteTransAction(pMnode)) return -1;
918 919 920 921 922 923 924 925 926 927 928 929 930

  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 已提交
931 932 933
  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 已提交
934
  for (int32_t i = 0; i < pAction->epSet.numOfEps; ++i) {
S
Shengliang Guan 已提交
935 936 937 938
    len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, pAction->epSet.eps[i].fqdn,
                    pAction->epSet.eps[i].port);
  }

939 940 941 942 943
  int32_t code = tmsgSendReq(&pAction->epSet, &rpcMsg);
  if (code == 0) {
    pAction->msgSent = 1;
    pAction->msgReceived = 0;
    pAction->errCode = 0;
S
Shengliang Guan 已提交
944
    mDebug("trans:%d, %s:%d is sent, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, detail);
945 946 947 948 949 950 951

    pTrans->lastAction = pAction->id;
    pTrans->lastMsgType = pAction->msgType;
    pTrans->lastEpset = pAction->epSet;
    if (pTrans->lastErrorNo == 0) {
      pTrans->lastErrorNo = TSDB_CODE_ACTION_IN_PROGRESS;
    }
952 953 954 955
  } else {
    pAction->msgSent = 0;
    pAction->msgReceived = 0;
    pAction->errCode = (terrno != 0) ? terrno : code;
S
Shengliang Guan 已提交
956 957
    mError("trans:%d, %s:%d not send since %s, %s", pTrans->id, mndTransStr(pAction->stage), pAction->id, terrstr(),
           detail);
958 959 960 961 962

    pTrans->lastAction = pAction->id;
    pTrans->lastMsgType = pAction->msgType;
    pTrans->lastEpset = pAction->epSet;
    pTrans->lastErrorNo = pAction->errCode;
963 964 965 966 967
  }

  return code;
}

968 969 970
static int32_t mndTransExecNullMsg(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
  pAction->rawWritten = 0;
  pAction->errCode = 0;
S
Shengliang Guan 已提交
971
  mDebug("trans:%d, %s:%d confirm action executed", pTrans->id, mndTransStr(pAction->stage), pAction->id);
972 973 974 975 976 977 978 979

  pTrans->lastAction = pAction->id;
  pTrans->lastMsgType = pAction->msgType;
  pTrans->lastEpset = pAction->epSet;
  pTrans->lastErrorNo == 0;
  return 0;
}

980
static int32_t mndTransExecSingleAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
981
  if (pAction->actionType == TRANS_ACTION_RAW) {
982
    return mndTransWriteSingleLog(pMnode, pTrans, pAction);
983
  } else if (pAction->actionType == TRANS_ACTION_MSG) {
984
    return mndTransSendSingleMsg(pMnode, pTrans, pAction);
985
  } else {
986
    return mndTransExecNullMsg(pMnode, pTrans, pAction);
987 988 989
  }
}

990
static int32_t mndTransExecSingleActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
991
  int32_t numOfActions = taosArrayGetSize(pArray);
992
  int32_t code = 0;
993 994 995

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

1000
  return code;
S
Shengliang Guan 已提交
1001 1002 1003 1004 1005 1006
}

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

1007
  if (mndTransExecSingleActions(pMnode, pTrans, pArray) != 0) {
S
Shengliang Guan 已提交
1008 1009 1010
    return -1;
  }

1011 1012 1013
  int32_t       numOfExecuted = 0;
  int32_t       errCode = 0;
  STransAction *pErrAction = NULL;
1014 1015
  for (int32_t action = 0; action < numOfActions; ++action) {
    STransAction *pAction = taosArrayGet(pArray, action);
1016
    if (pAction->msgReceived || pAction->rawWritten) {
1017
      numOfExecuted++;
1018
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
S
Shengliang Guan 已提交
1019
        errCode = pAction->errCode;
1020
        pErrAction = pAction;
1021 1022 1023 1024
      }
    }
  }

1025
  if (numOfExecuted == numOfActions) {
S
Shengliang Guan 已提交
1026
    if (errCode == 0) {
1027 1028 1029
      pTrans->lastAction = 0;
      pTrans->lastMsgType = 0;
      memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
S
Shengliang Guan 已提交
1030
      pTrans->lastErrorNo = 0;
S
Shengliang Guan 已提交
1031 1032 1033
      mDebug("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
      return 0;
    } else {
S
Shengliang Guan 已提交
1034
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode & 0XFFFF);
1035
      if (pErrAction != NULL) {
1036
        pTrans->lastAction = pErrAction->id;
S
Shengliang Guan 已提交
1037
        pTrans->lastMsgType = pErrAction->msgType;
1038
        pTrans->lastEpset = pErrAction->epSet;
S
Shengliang Guan 已提交
1039
        pTrans->lastErrorNo = pErrAction->errCode;
1040
      }
S
Shengliang Guan 已提交
1041 1042 1043 1044
      mndTransResetActions(pMnode, pTrans, pArray);
      terrno = errCode;
      return errCode;
    }
1045
  } else {
1046
    mDebug("trans:%d, %d of %d actions executed", pTrans->id, numOfExecuted, numOfActions);
S
Shengliang Guan 已提交
1047
    return TSDB_CODE_ACTION_IN_PROGRESS;
1048
  }
S
Shengliang Guan 已提交
1049 1050
}

S
Shengliang Guan 已提交
1051
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans) {
1052
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions);
S
Shengliang Guan 已提交
1053
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1054
    mError("failed to execute redoActions since:%s, code:0x%x", terrstr(), terrno);
1055 1056
  }
  return code;
S
Shengliang Guan 已提交
1057
}
S
Shengliang Guan 已提交
1058

S
Shengliang Guan 已提交
1059
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans) {
1060
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions);
S
Shengliang Guan 已提交
1061
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1062 1063 1064
    mError("failed to execute undoActions since %s", terrstr());
  }
  return code;
S
Shengliang Guan 已提交
1065
}
S
Shengliang Guan 已提交
1066

1067
static int32_t mndTransExecuteCommitActions(SMnode *pMnode, STrans *pTrans) {
1068
  int32_t code = mndTransExecuteActions(pMnode, pTrans, pTrans->commitActions);
1069
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1070 1071 1072 1073 1074
    mError("failed to execute commitActions since %s", terrstr());
  }
  return code;
}

1075
static int32_t mndTransExecuteRedoActionsSerial(SMnode *pMnode, STrans *pTrans) {
1076 1077 1078 1079
  int32_t code = 0;
  int32_t numOfActions = taosArrayGetSize(pTrans->redoActions);
  if (numOfActions == 0) return code;
  if (pTrans->redoActionPos >= numOfActions) return code;
S
Shengliang Guan 已提交
1080

S
Shengliang Guan 已提交
1081 1082
  int32_t retryTimes = 0;

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

1086 1087 1088 1089 1090 1091
    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;
1092 1093 1094
            pAction->msgSent = 0;
            pAction->msgReceived = 0;
            mDebug("trans:%d, %s:%d execute status is reset", pTrans->id, mndTransStr(pAction->stage), action);
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
          }
        } else {
          code = TSDB_CODE_ACTION_IN_PROGRESS;
        }
      }
      if (pAction->rawWritten) {
        if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
          code = pAction->errCode;
        }
      }
    }
1106

1107
    if (code == 0) {
S
Shengliang Guan 已提交
1108
      pTrans->lastAction = action;
1109
      pTrans->lastMsgType = 0;
S
Shengliang Guan 已提交
1110
      pTrans->lastErrorNo = 0;
1111
      memset(&pTrans->lastEpset, 0, sizeof(pTrans->lastEpset));
1112
    } else {
1113
      pTrans->lastAction = action;
S
Shengliang Guan 已提交
1114
      pTrans->lastMsgType = pAction->msgType;
1115 1116
      pTrans->lastErrorNo = code;
      pTrans->lastEpset = pAction->epSet;
1117 1118
    }

1119
    if (mndCantExecuteTransAction(pMnode)) break;
1120

1121
    if (code == 0) {
1122
      pTrans->code = 0;
1123 1124 1125 1126 1127
      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) {
1128 1129 1130
        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());
S
Shengliang Guan 已提交
1131 1132 1133 1134
        if (retryTimes >= 3)
          break;
        else
          continue;
1135
      }
S
Shengliang Guan 已提交
1136
      retryTimes = 0;
1137 1138 1139 1140
    } 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 {
1141 1142
      terrno = code;
      pTrans->code = code;
S
Shengliang Guan 已提交
1143 1144 1145 1146 1147 1148 1149 1150
      pAction->epSet.inUse++;
      if (pAction->epSet.inUse >= pAction->epSet.numOfEps) pAction->epSet.inUse = 0;
      mError("trans:%d, %s:%d failed to execute since %s, inUse set to %d", pTrans->id, mndTransStr(pAction->stage),
             pAction->id, terrstr(), pAction->epSet.inUse);
      if (retryTimes >= 3)
        break;
      else
        continue;
1151
    }
S
Shengliang Guan 已提交
1152
  }
S
Shengliang Guan 已提交
1153

1154 1155 1156 1157 1158 1159 1160
  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 已提交
1161
  return continueExec;
S
Shengliang Guan 已提交
1162 1163
}

S
Shengliang Guan 已提交
1164 1165
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans) {
  bool    continueExec = true;
1166 1167
  int32_t code = 0;

1168
  if (pTrans->exec == TRN_EXEC_SERIAL) {
1169
    code = mndTransExecuteRedoActionsSerial(pMnode, pTrans);
1170 1171 1172
  } else {
    code = mndTransExecuteRedoActions(pMnode, pTrans);
  }
S
Shengliang Guan 已提交
1173

1174 1175
  if (mndCantExecuteTransAction(pMnode)) return false;

S
Shengliang Guan 已提交
1176
  if (code == 0) {
S
Shengliang Guan 已提交
1177
    pTrans->code = 0;
S
Shengliang Guan 已提交
1178
    pTrans->stage = TRN_STAGE_COMMIT;
S
Shengliang Guan 已提交
1179 1180
    mDebug("trans:%d, stage from redoAction to commit", pTrans->id);
    continueExec = true;
S
Shengliang Guan 已提交
1181
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1182 1183
    mDebug("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
    continueExec = false;
S
Shengliang Guan 已提交
1184
  } else {
S
Shengliang Guan 已提交
1185
    pTrans->code = terrno;
S
Shengliang Guan 已提交
1186
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
S
Shengliang Guan 已提交
1187 1188 1189
      pTrans->stage = TRN_STAGE_UNDO_ACTION;
      mError("trans:%d, stage from redoAction to undoAction since %s", pTrans->id, terrstr());
      continueExec = true;
S
Shengliang Guan 已提交
1190
    } else {
S
Shengliang Guan 已提交
1191 1192 1193
      pTrans->failedTimes++;
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
      continueExec = false;
S
Shengliang Guan 已提交
1194 1195 1196
    }
  }

S
Shengliang Guan 已提交
1197
  return continueExec;
S
Shengliang Guan 已提交
1198 1199
}

S
Shengliang Guan 已提交
1200
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans) {
1201 1202
  if (mndCantExecuteTransAction(pMnode)) return false;

S
Shengliang Guan 已提交
1203 1204 1205 1206 1207
  bool    continueExec = true;
  int32_t code = mndTransCommit(pMnode, pTrans);

  if (code == 0) {
    pTrans->code = 0;
1208 1209
    pTrans->stage = TRN_STAGE_COMMIT_ACTION;
    mDebug("trans:%d, stage from commit to commitAction", pTrans->id);
S
Shengliang Guan 已提交
1210 1211 1212 1213
    continueExec = true;
  } else {
    pTrans->code = terrno;
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
1214 1215
      pTrans->stage = TRN_STAGE_UNDO_ACTION;
      mError("trans:%d, stage from commit to undoAction since %s, failedTimes:%d", pTrans->id, terrstr(),
S
Shengliang Guan 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
             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 已提交
1226 1227
}

1228
static bool mndTransPerformCommitActionStage(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
1229
  bool    continueExec = true;
1230
  int32_t code = mndTransExecuteCommitActions(pMnode, pTrans);
S
Shengliang Guan 已提交
1231 1232

  if (code == 0) {
S
Shengliang Guan 已提交
1233 1234
    pTrans->code = 0;
    pTrans->stage = TRN_STAGE_FINISHED;
1235
    mDebug("trans:%d, stage from commitAction to finished", pTrans->id);
S
Shengliang Guan 已提交
1236
    continueExec = true;
S
Shengliang Guan 已提交
1237
  } else {
S
Shengliang Guan 已提交
1238 1239
    pTrans->code = terrno;
    pTrans->failedTimes++;
1240
    mError("trans:%d, stage keep on commitAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
S
Shengliang Guan 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    continueExec = false;
  }

  return continueExec;
}

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

1251 1252
  if (mndCantExecuteTransAction(pMnode)) return false;

S
Shengliang Guan 已提交
1253
  if (code == 0) {
1254 1255
    pTrans->stage = TRN_STAGE_ROLLBACK;
    mDebug("trans:%d, stage from undoAction to rollback", pTrans->id);
S
Shengliang Guan 已提交
1256
    continueExec = true;
S
Shengliang Guan 已提交
1257
  } else if (code == TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1258
    mDebug("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
S
Shengliang Guan 已提交
1259 1260 1261
    continueExec = false;
  } else {
    pTrans->failedTimes++;
1262
    mError("trans:%d, stage keep on undoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
S
Shengliang Guan 已提交
1263 1264 1265 1266 1267 1268 1269
    continueExec = false;
  }

  return continueExec;
}

static bool mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans) {
1270 1271
  if (mndCantExecuteTransAction(pMnode)) return false;

S
Shengliang Guan 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280
  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++;
1281
    mError("trans:%d, stage keep on rollback since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
S
Shengliang Guan 已提交
1282
    continueExec = false;
S
Shengliang Guan 已提交
1283 1284
  }

S
Shengliang Guan 已提交
1285 1286 1287 1288 1289 1290 1291 1292
  return continueExec;
}

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

  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
1293
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
1294 1295 1296 1297 1298 1299 1300 1301
  }
  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());
  }

1302
  mDebug("trans:%d, execute finished, code:0x%x, failedTimes:%d", pTrans->id, pTrans->code, pTrans->failedTimes);
S
Shengliang Guan 已提交
1303
  return continueExec;
S
Shengliang Guan 已提交
1304
}
S
Shengliang Guan 已提交
1305

1306
void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
1307
  bool continueExec = true;
S
Shengliang Guan 已提交
1308

S
Shengliang Guan 已提交
1309
  while (continueExec) {
1310
    mDebug("trans:%d, continue to execute, stage:%s", pTrans->id, mndTransStr(pTrans->stage));
S
Shengliang Guan 已提交
1311
    pTrans->lastExecTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
1312 1313
    switch (pTrans->stage) {
      case TRN_STAGE_PREPARE:
S
Shengliang Guan 已提交
1314 1315 1316 1317 1318
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans);
        break;
      case TRN_STAGE_REDO_ACTION:
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans);
        break;
1319 1320 1321 1322 1323
      case TRN_STAGE_COMMIT:
        continueExec = mndTransPerformCommitStage(pMnode, pTrans);
        break;
      case TRN_STAGE_COMMIT_ACTION:
        continueExec = mndTransPerformCommitActionStage(pMnode, pTrans);
S
Shengliang Guan 已提交
1324
        break;
S
Shengliang Guan 已提交
1325 1326 1327
      case TRN_STAGE_UNDO_ACTION:
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans);
        break;
S
Shengliang Guan 已提交
1328
      case TRN_STAGE_ROLLBACK:
S
Shengliang Guan 已提交
1329 1330 1331 1332
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans);
        break;
      case TRN_STAGE_FINISHED:
        continueExec = mndTransPerfromFinishedStage(pMnode, pTrans);
S
Shengliang Guan 已提交
1333
        break;
S
Shengliang Guan 已提交
1334
      default:
S
Shengliang Guan 已提交
1335 1336
        continueExec = false;
        break;
S
Shengliang Guan 已提交
1337 1338 1339
    }
  }

1340
  mndTransSendRpcRsp(pMnode, pTrans);
S
Shengliang Guan 已提交
1341
}
S
Shengliang Guan 已提交
1342

S
Shengliang Guan 已提交
1343 1344
static int32_t mndProcessTransReq(SRpcMsg *pReq) {
  mndTransPullup(pReq->info.node);
S
Shengliang Guan 已提交
1345 1346 1347
  return 0;
}

1348
int32_t mndKillTrans(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
  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;
  }

1359
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
S
Shengliang Guan 已提交
1360
    STransAction *pAction = taosArrayGet(pArray, i);
S
Shengliang Guan 已提交
1361 1362 1363 1364 1365
    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));
    pAction->msgSent = 1;
    pAction->msgReceived = 1;
    pAction->errCode = 0;
S
Shengliang Guan 已提交
1366 1367 1368 1369 1370 1371
  }

  mndTransExecute(pMnode, pTrans);
  return 0;
}

S
Shengliang Guan 已提交
1372 1373
static int32_t mndProcessKillTransReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1374
  SKillTransReq killReq = {0};
S
Shengliang Guan 已提交
1375
  int32_t       code = -1;
1376
  STrans       *pTrans = NULL;
S
Shengliang Guan 已提交
1377

S
Shengliang Guan 已提交
1378
  if (tDeserializeSKillTransReq(pReq->pCont, pReq->contLen, &killReq) != 0) {
S
Shengliang Guan 已提交
1379
    terrno = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
1380
    goto _OVER;
S
Shengliang Guan 已提交
1381 1382 1383 1384
  }

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

1385
  if (mndCheckOperAuth(pMnode, pReq->conn.user, MND_OPER_KILL_TRANS) != 0) {
S
Shengliang Guan 已提交
1386
    goto _OVER;
S
Shengliang Guan 已提交
1387 1388
  }

S
Shengliang Guan 已提交
1389
  pTrans = mndAcquireTrans(pMnode, killReq.transId);
S
Shengliang Guan 已提交
1390
  if (pTrans == NULL) {
1391
    goto _OVER;
S
Shengliang Guan 已提交
1392 1393
  }

S
Shengliang Guan 已提交
1394 1395
  code = mndKillTrans(pMnode, pTrans);

S
Shengliang Guan 已提交
1396
_OVER:
1397
  if (code != 0) {
S
Shengliang Guan 已提交
1398 1399 1400
    mError("trans:%d, failed to kill since %s", killReq.transId, terrstr());
  }

S
Shengliang Guan 已提交
1401
  mndReleaseTrans(pMnode, pTrans);
S
Shengliang Guan 已提交
1402
  return code;
S
Shengliang Guan 已提交
1403 1404
}

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

S
Shengliang Guan 已提交
1407
void mndTransPullup(SMnode *pMnode) {
1408 1409 1410
  SSdb   *pSdb = pMnode->pSdb;
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_TRANS), sizeof(int32_t));
  if (pArray == NULL) return;
S
Shengliang Guan 已提交
1411

1412
  void *pIter = NULL;
S
Shengliang Guan 已提交
1413
  while (1) {
1414
    STrans *pTrans = NULL;
S
Shengliang Guan 已提交
1415 1416
    pIter = sdbFetch(pMnode->pSdb, SDB_TRANS, pIter, (void **)&pTrans);
    if (pIter == NULL) break;
1417 1418 1419
    taosArrayPush(pArray, &pTrans->id);
    sdbRelease(pSdb, pTrans);
  }
S
Shengliang Guan 已提交
1420

1421 1422 1423 1424 1425 1426 1427 1428 1429
  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 已提交
1430
  }
S
Shengliang Guan 已提交
1431

S
Shengliang Guan 已提交
1432 1433 1434 1435
  SSnapshotMeta sMeta = {0};
  if (syncGetSnapshotMeta(pMnode->syncMgmt.sync, &sMeta) == 0) {
    sdbSetCurConfig(pMnode->pSdb, sMeta.lastConfigIndex);
  }
S
Shengliang Guan 已提交
1436
  sdbWriteFile(pMnode->pSdb);
1437
  taosArrayDestroy(pArray);
1438
}
S
Shengliang Guan 已提交
1439

S
Shengliang Guan 已提交
1440 1441
static int32_t mndRetrieveTrans(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode *pMnode = pReq->info.node;
1442
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
1443 1444 1445
  int32_t numOfRows = 0;
  STrans *pTrans = NULL;
  int32_t cols = 0;
1446
  char   *pWrite;
S
Shengliang Guan 已提交
1447 1448 1449 1450 1451 1452 1453

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

    cols = 0;

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

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

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

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

1470 1471 1472
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pTrans->failedTimes, false);

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

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
    char    lastInfo[TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE] = {0};
    char    detail[TSDB_TRANS_ERROR_LEN] = {0};
    int32_t len = snprintf(detail, sizeof(detail), "action:%d code:0x%x(%s) ", pTrans->lastAction,
                           pTrans->lastErrorNo & 0xFFFF, tstrerror(pTrans->lastErrorNo));
    SEpSet  epset = pTrans->lastEpset;
    if (epset.numOfEps > 0) {
      len += snprintf(detail + len, sizeof(detail) - len, "msgType:%s numOfEps:%d inUse:%d ",
                      TMSG_INFO(pTrans->lastMsgType), epset.numOfEps, epset.inUse);
      for (int32_t i = 0; i < pTrans->lastEpset.numOfEps; ++i) {
        len += snprintf(detail + len, sizeof(detail) - len, "ep:%d-%s:%u ", i, epset.eps[i].fqdn, epset.eps[i].port);
1486 1487
      }
    }
1488
    STR_WITH_MAXSIZE_TO_VARSTR(lastInfo, detail, pShow->pMeta->pSchemas[cols].bytes);
S
Shengliang Guan 已提交
1489
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1490
    colDataAppend(pColInfo, numOfRows, (const char *)lastInfo, false);
S
Shengliang Guan 已提交
1491 1492 1493 1494 1495

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

1496
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
1497 1498 1499 1500 1501 1502 1503
  return numOfRows;
}

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