mndTrans.c 38.3 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 19
#include "mndAuth.h"
#include "mndShow.h"
S
Shengliang Guan 已提交
20
#include "mndSync.h"
S
Shengliang Guan 已提交
21
#include "mndUser.h"
S
Shengliang Guan 已提交
22

S
Shengliang Guan 已提交
23 24
#define MND_TRANS_VER_NUMBER   1
#define MND_TRANS_ARRAY_SIZE   8
S
Shengliang Guan 已提交
25
#define MND_TRANS_RESERVE_SIZE 64
S
Shengliang Guan 已提交
26

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

S
Shengliang Guan 已提交
33
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw);
S
Shengliang Guan 已提交
34
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction);
S
Shengliang Guan 已提交
35 36
static void    mndTransDropLogs(SArray *pArray);
static void    mndTransDropActions(SArray *pArray);
37
static void    mndTransDropData(STrans *pTrans);
S
Shengliang Guan 已提交
38
static int32_t mndTransExecuteLogs(SMnode *pMnode, SArray *pArray);
39
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray);
S
Shengliang Guan 已提交
40 41 42 43
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);
S
Shengliang Guan 已提交
44 45 46 47 48 49 50 51 52 53 54
static int32_t mndTransExecuteCommitLogs(SMnode *pMnode, STrans *pTrans);
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);
static bool    mndTransPerformCommitLogStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans);
static bool    mndTransPerfromFinishedStage(SMnode *pMnode, STrans *pTrans);

S
Shengliang Guan 已提交
55
static void    mndTransExecute(SMnode *pMnode, STrans *pTrans);
S
Shengliang Guan 已提交
56
static void    mndTransSendRpcRsp(STrans *pTrans);
S
Shengliang Guan 已提交
57
static int32_t mndProcessTransReq(SMnodeMsg *pMsg);
S
Shengliang Guan 已提交
58

S
Shengliang Guan 已提交
59 60 61 62
static int32_t mndGetTransMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta);
static int32_t mndRetrieveTrans(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows);
static void    mndCancelGetNextTrans(SMnode *pMnode, void *pIter);

S
Shengliang Guan 已提交
63 64 65 66 67 68 69 70 71
int32_t mndInitTrans(SMnode *pMnode) {
  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 已提交
72
  mndSetMsgHandle(pMnode, TDMT_MND_TRANS, mndProcessTransReq);
S
Shengliang Guan 已提交
73 74 75 76

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

void mndCleanupTrans(SMnode *pMnode) {}

static SSdbRaw *mndTransActionEncode(STrans *pTrans) {
83 84
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
85
  int32_t rawDataLen = sizeof(STrans) + MND_TRANS_RESERVE_SIZE;
S
Shengliang Guan 已提交
86 87 88 89 90 91
  int32_t redoLogNum = taosArrayGetSize(pTrans->redoLogs);
  int32_t undoLogNum = taosArrayGetSize(pTrans->undoLogs);
  int32_t commitLogNum = taosArrayGetSize(pTrans->commitLogs);
  int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions);
  int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions);

92
  for (int32_t i = 0; i < redoLogNum; ++i) {
S
Shengliang Guan 已提交
93
    SSdbRaw *pTmp = taosArrayGetP(pTrans->redoLogs, i);
S
Shengliang Guan 已提交
94
    rawDataLen += (sdbGetRawTotalSize(pTmp) + sizeof(int32_t));
S
Shengliang Guan 已提交
95 96
  }

97
  for (int32_t i = 0; i < undoLogNum; ++i) {
S
Shengliang Guan 已提交
98
    SSdbRaw *pTmp = taosArrayGetP(pTrans->undoLogs, i);
S
Shengliang Guan 已提交
99
    rawDataLen += (sdbGetRawTotalSize(pTmp) + sizeof(int32_t));
S
Shengliang Guan 已提交
100 101
  }

102
  for (int32_t i = 0; i < commitLogNum; ++i) {
S
Shengliang Guan 已提交
103
    SSdbRaw *pTmp = taosArrayGetP(pTrans->commitLogs, i);
S
Shengliang Guan 已提交
104
    rawDataLen += (sdbGetRawTotalSize(pTmp) + sizeof(int32_t));
S
Shengliang Guan 已提交
105 106
  }

S
Shengliang Guan 已提交
107 108
  for (int32_t i = 0; i < redoActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
S
Shengliang Guan 已提交
109
    rawDataLen += (sizeof(STransAction) + pAction->contLen);
S
Shengliang Guan 已提交
110 111 112 113
  }

  for (int32_t i = 0; i < undoActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
S
Shengliang Guan 已提交
114
    rawDataLen += (sizeof(STransAction) + pAction->contLen);
S
Shengliang Guan 已提交
115 116
  }

S
Shengliang Guan 已提交
117
  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, MND_TRANS_VER_NUMBER, rawDataLen);
S
Shengliang Guan 已提交
118
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
119
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
120 121 122 123
    return NULL;
  }

  int32_t dataPos = 0;
124 125
  SDB_SET_INT32(pRaw, dataPos, pTrans->id, TRANS_ENCODE_OVER)
  SDB_SET_INT8(pRaw, dataPos, pTrans->policy, TRANS_ENCODE_OVER)
126
  SDB_SET_INT8(pRaw, dataPos, pTrans->stage, TRANS_ENCODE_OVER)
S
Shengliang Guan 已提交
127 128 129 130
  SDB_SET_INT64(pRaw, dataPos, pTrans->createdTime, TRANS_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, pTrans->transType, TRANS_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pTrans->dbUid, TRANS_ENCODE_OVER)
  SDB_SET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_DB_FNAME_LEN, TRANS_ENCODE_OVER)
131 132 133 134 135
  SDB_SET_INT32(pRaw, dataPos, redoLogNum, TRANS_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, undoLogNum, TRANS_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, commitLogNum, TRANS_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, redoActionNum, TRANS_ENCODE_OVER)
  SDB_SET_INT32(pRaw, dataPos, undoActionNum, TRANS_ENCODE_OVER)
S
Shengliang Guan 已提交
136

137
  for (int32_t i = 0; i < redoLogNum; ++i) {
S
Shengliang Guan 已提交
138
    SSdbRaw *pTmp = taosArrayGetP(pTrans->redoLogs, i);
S
Shengliang Guan 已提交
139
    int32_t  len = sdbGetRawTotalSize(pTmp);
140 141
    SDB_SET_INT32(pRaw, dataPos, len, TRANS_ENCODE_OVER)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len, TRANS_ENCODE_OVER)
S
Shengliang Guan 已提交
142 143
  }

144
  for (int32_t i = 0; i < undoLogNum; ++i) {
S
Shengliang Guan 已提交
145
    SSdbRaw *pTmp = taosArrayGetP(pTrans->undoLogs, i);
S
Shengliang Guan 已提交
146
    int32_t  len = sdbGetRawTotalSize(pTmp);
147 148
    SDB_SET_INT32(pRaw, dataPos, len, TRANS_ENCODE_OVER)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len, TRANS_ENCODE_OVER)
S
Shengliang Guan 已提交
149 150
  }

151
  for (int32_t i = 0; i < commitLogNum; ++i) {
S
Shengliang Guan 已提交
152
    SSdbRaw *pTmp = taosArrayGetP(pTrans->commitLogs, i);
S
Shengliang Guan 已提交
153
    int32_t  len = sdbGetRawTotalSize(pTmp);
154 155
    SDB_SET_INT32(pRaw, dataPos, len, TRANS_ENCODE_OVER)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len, TRANS_ENCODE_OVER)
S
Shengliang Guan 已提交
156 157
  }

S
Shengliang Guan 已提交
158 159
  for (int32_t i = 0; i < redoActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
160 161
    SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), TRANS_ENCODE_OVER)
    SDB_SET_INT16(pRaw, dataPos, pAction->msgType, TRANS_ENCODE_OVER)
162
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, TRANS_ENCODE_OVER)
163 164
    SDB_SET_INT32(pRaw, dataPos, pAction->contLen, TRANS_ENCODE_OVER)
    SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen, TRANS_ENCODE_OVER)
S
Shengliang Guan 已提交
165 166 167 168
  }

  for (int32_t i = 0; i < undoActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
169 170
    SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet), TRANS_ENCODE_OVER)
    SDB_SET_INT16(pRaw, dataPos, pAction->msgType, TRANS_ENCODE_OVER)
171
    SDB_SET_INT32(pRaw, dataPos, pAction->acceptableCode, TRANS_ENCODE_OVER)
172 173 174 175 176 177 178 179 180 181 182 183 184 185
    SDB_SET_INT32(pRaw, dataPos, pAction->contLen, TRANS_ENCODE_OVER)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pCont, pAction->contLen, TRANS_ENCODE_OVER)
  }

  SDB_SET_RESERVE(pRaw, dataPos, MND_TRANS_RESERVE_SIZE, TRANS_ENCODE_OVER)
  SDB_SET_DATALEN(pRaw, dataPos, TRANS_ENCODE_OVER)

  terrno = 0;

TRANS_ENCODE_OVER:
  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 已提交
186 187
  }

S
Shengliang Guan 已提交
188
  mTrace("trans:%d, encode to raw:%p, row:%p len:%d", pTrans->id, pRaw, pTrans, dataPos);
S
Shengliang Guan 已提交
189 190 191
  return pRaw;
}

S
Shengliang Guan 已提交
192
static SSdbRow *mndTransActionDecode(SSdbRaw *pRaw) {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  terrno = TSDB_CODE_OUT_OF_MEMORY;

  SSdbRow     *pRow = NULL;
  STrans      *pTrans = NULL;
  char        *pData = NULL;
  int32_t      dataLen = 0;
  int8_t       sver = 0;
  int32_t      redoLogNum = 0;
  int32_t      undoLogNum = 0;
  int32_t      commitLogNum = 0;
  int32_t      redoActionNum = 0;
  int32_t      undoActionNum = 0;
  int32_t      dataPos = 0;
  STransAction action = {0};

  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
209

S
Shengliang Guan 已提交
210
  if (sver != MND_TRANS_VER_NUMBER) {
S
Shengliang Guan 已提交
211
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
212
    goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
213 214
  }

215 216 217 218 219
  pRow = sdbAllocRow(sizeof(STrans));
  if (pRow == NULL) goto TRANS_DECODE_OVER;

  pTrans = sdbGetRowObj(pRow);
  if (pTrans == NULL) goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
220

S
Shengliang Guan 已提交
221 222 223 224 225
  pTrans->redoLogs = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(void *));
  pTrans->undoLogs = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(void *));
  pTrans->commitLogs = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(void *));
  pTrans->redoActions = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(STransAction));
  pTrans->undoActions = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(STransAction));
S
Shengliang Guan 已提交
226

227 228 229 230 231
  if (pTrans->redoLogs == NULL) goto TRANS_DECODE_OVER;
  if (pTrans->undoLogs == NULL) goto TRANS_DECODE_OVER;
  if (pTrans->commitLogs == NULL) goto TRANS_DECODE_OVER;
  if (pTrans->redoActions == NULL) goto TRANS_DECODE_OVER;
  if (pTrans->undoActions == NULL) goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
232

233 234
  SDB_GET_INT32(pRaw, dataPos, &pTrans->id, TRANS_DECODE_OVER)
  SDB_GET_INT8(pRaw, dataPos, (int8_t *)&pTrans->policy, TRANS_DECODE_OVER)
235
  SDB_GET_INT8(pRaw, dataPos, (int8_t *)&pTrans->stage, TRANS_DECODE_OVER)
S
Shengliang Guan 已提交
236 237 238 239
  SDB_GET_INT64(pRaw, dataPos, &pTrans->createdTime, TRANS_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &pTrans->transType, TRANS_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pTrans->dbUid, TRANS_DECODE_OVER)
  SDB_GET_BINARY(pRaw, dataPos, pTrans->dbname, TSDB_DB_FNAME_LEN, TRANS_DECODE_OVER)
240 241 242 243 244
  SDB_GET_INT32(pRaw, dataPos, &redoLogNum, TRANS_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &undoLogNum, TRANS_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &commitLogNum, TRANS_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &redoActionNum, TRANS_DECODE_OVER)
  SDB_GET_INT32(pRaw, dataPos, &undoActionNum, TRANS_DECODE_OVER)
S
Shengliang Guan 已提交
245

246
  for (int32_t i = 0; i < redoLogNum; ++i) {
247 248 249
    SDB_GET_INT32(pRaw, dataPos, &dataLen, TRANS_DECODE_OVER)
    pData = malloc(dataLen);
    if (pData == NULL) goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
250
    mTrace("raw:%p, is created", pData);
251 252 253
    SDB_GET_BINARY(pRaw, dataPos, pData, dataLen, TRANS_DECODE_OVER);
    if (taosArrayPush(pTrans->redoLogs, &pData) == NULL) goto TRANS_DECODE_OVER;
    pData = NULL;
S
Shengliang Guan 已提交
254 255
  }

S
Shengliang Guan 已提交
256
  for (int32_t i = 0; i < undoLogNum; ++i) {
257 258 259
    SDB_GET_INT32(pRaw, dataPos, &dataLen, TRANS_DECODE_OVER)
    pData = malloc(dataLen);
    if (pData == NULL) goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
260
    mTrace("raw:%p, is created", pData);
261 262 263
    SDB_GET_BINARY(pRaw, dataPos, pData, dataLen, TRANS_DECODE_OVER);
    if (taosArrayPush(pTrans->undoLogs, &pData) == NULL) goto TRANS_DECODE_OVER;
    pData = NULL;
S
Shengliang Guan 已提交
264 265 266
  }

  for (int32_t i = 0; i < commitLogNum; ++i) {
267 268
    SDB_GET_INT32(pRaw, dataPos, &dataLen, TRANS_DECODE_OVER)
    pData = malloc(dataLen);
S
Shengliang Guan 已提交
269 270
    if (pData == NULL) goto TRANS_DECODE_OVER;
    mTrace("raw:%p, is created", pData);
271 272 273
    SDB_GET_BINARY(pRaw, dataPos, pData, dataLen, TRANS_DECODE_OVER);
    if (taosArrayPush(pTrans->commitLogs, &pData) == NULL) goto TRANS_DECODE_OVER;
    pData = NULL;
S
Shengliang Guan 已提交
274 275 276
  }

  for (int32_t i = 0; i < redoActionNum; ++i) {
277 278
    SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), TRANS_DECODE_OVER);
    SDB_GET_INT16(pRaw, dataPos, &action.msgType, TRANS_DECODE_OVER)
279
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, TRANS_DECODE_OVER)
280
    SDB_GET_INT32(pRaw, dataPos, &action.contLen, TRANS_DECODE_OVER)
S
Shengliang Guan 已提交
281
    action.pCont = malloc(action.contLen);
282 283 284 285
    if (action.pCont == NULL) goto TRANS_DECODE_OVER;
    SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, TRANS_DECODE_OVER);
    if (taosArrayPush(pTrans->redoActions, &action) == NULL) goto TRANS_DECODE_OVER;
    action.pCont = NULL;
S
Shengliang Guan 已提交
286 287 288
  }

  for (int32_t i = 0; i < undoActionNum; ++i) {
289 290
    SDB_GET_BINARY(pRaw, dataPos, (void *)&action.epSet, sizeof(SEpSet), TRANS_DECODE_OVER);
    SDB_GET_INT16(pRaw, dataPos, &action.msgType, TRANS_DECODE_OVER)
291
    SDB_GET_INT32(pRaw, dataPos, &action.acceptableCode, TRANS_DECODE_OVER)
292
    SDB_GET_INT32(pRaw, dataPos, &action.contLen, TRANS_DECODE_OVER)
S
Shengliang Guan 已提交
293
    action.pCont = malloc(action.contLen);
294 295 296 297
    if (action.pCont == NULL) goto TRANS_DECODE_OVER;
    SDB_GET_BINARY(pRaw, dataPos, action.pCont, action.contLen, TRANS_DECODE_OVER);
    if (taosArrayPush(pTrans->undoActions, &action) == NULL) goto TRANS_DECODE_OVER;
    action.pCont = NULL;
S
Shengliang Guan 已提交
298 299
  }

300 301 302
  SDB_GET_RESERVE(pRaw, dataPos, MND_TRANS_RESERVE_SIZE, TRANS_DECODE_OVER)

  terrno = 0;
S
Shengliang Guan 已提交
303

S
Shengliang Guan 已提交
304
TRANS_DECODE_OVER:
305 306 307 308 309 310
  if (terrno != 0) {
    mError("trans:%d, failed to parse from raw:%p since %s", pTrans->id, pRaw, terrstr());
    mndTransDropData(pTrans);
    tfree(pRow);
    tfree(pData);
    tfree(action.pCont);
S
Shengliang Guan 已提交
311 312 313
    return NULL;
  }

S
Shengliang Guan 已提交
314
  mTrace("trans:%d, decode from raw:%p, row:%p", pTrans->id, pRaw, pTrans);
S
Shengliang Guan 已提交
315 316 317
  return pRow;
}

S
Shengliang Guan 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
static const char *mndTransStr(ETrnStage stage) {
  switch (stage) {
    case TRN_STAGE_PREPARE:
      return "prepare";
    case TRN_STAGE_REDO_LOG:
      return "redoLog";
    case TRN_STAGE_REDO_ACTION:
      return "redoAction";
    case TRN_STAGE_COMMIT:
      return "commit";
    case TRN_STAGE_COMMIT_LOG:
      return "commitLog";
    case TRN_STAGE_UNDO_ACTION:
      return "undoAction";
    case TRN_STAGE_UNDO_LOG:
      return "undoLog";
    case TRN_STAGE_ROLLBACK:
      return "rollback";
    case TRN_STAGE_FINISHED:
      return "finished";
    default:
      return "invalid";
  }
}

S
Shengliang Guan 已提交
343 344 345 346 347 348 349 350 351
static const char *mndTransType(ETrnType type) {
  switch (type) {
    case TRN_TYPE_CREATE_DB:
      return "create-db";
    default:
      return "invalid";
  }
}

S
Shengliang Guan 已提交
352
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
S
Shengliang Guan 已提交
353 354
  // pTrans->stage = TRN_STAGE_PREPARE;
  mTrace("trans:%d, perform insert action, row:%p stage:%s", pTrans->id, pTrans, mndTransStr(pTrans->stage));
S
Shengliang Guan 已提交
355 356 357
  return 0;
}

358
static void mndTransDropData(STrans *pTrans) {
S
Shengliang Guan 已提交
359 360 361 362 363
  mndTransDropLogs(pTrans->redoLogs);
  mndTransDropLogs(pTrans->undoLogs);
  mndTransDropLogs(pTrans->commitLogs);
  mndTransDropActions(pTrans->redoActions);
  mndTransDropActions(pTrans->undoActions);
S
Shengliang Guan 已提交
364
  if (pTrans->rpcRsp != NULL) {
365
    free(pTrans->rpcRsp);
S
Shengliang Guan 已提交
366 367 368
    pTrans->rpcRsp = NULL;
    pTrans->rpcRspLen = 0;
  }
369
}
S
Shengliang Guan 已提交
370

371
static int32_t mndTransActionDelete(SSdb *pSdb, STrans *pTrans) {
S
Shengliang Guan 已提交
372
  mTrace("trans:%d, perform delete action, row:%p stage:%s", pTrans->id, pTrans, mndTransStr(pTrans->stage));
373
  mndTransDropData(pTrans);
S
Shengliang Guan 已提交
374 375 376
  return 0;
}

S
Shengliang Guan 已提交
377
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) {
S
Shengliang Guan 已提交
378 379 380 381
  if (pNew->stage == TRN_STAGE_COMMIT) {
    pNew->stage = TRN_STAGE_COMMIT_LOG;
    mTrace("trans:%d, stage from %s to %s", pNew->id, mndTransStr(TRN_STAGE_COMMIT), mndTransStr(TRN_STAGE_COMMIT_LOG));
  }
382

S
Shengliang Guan 已提交
383 384
  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));
S
Shengliang Guan 已提交
385
  pOld->stage = pNew->stage;
S
Shengliang Guan 已提交
386 387 388
  return 0;
}

S
Shengliang Guan 已提交
389
static STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
S
Shengliang Guan 已提交
390 391 392 393 394 395
  SSdb   *pSdb = pMnode->pSdb;
  STrans *pTrans = sdbAcquire(pSdb, SDB_TRANS, &transId);
  if (pTrans == NULL) {
    terrno = TSDB_CODE_MND_TRANS_NOT_EXIST;
  }
  return pTrans;
S
Shengliang Guan 已提交
396 397
}

S
Shengliang Guan 已提交
398
static void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
399 400 401 402
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pTrans);
}

S
Shengliang Guan 已提交
403
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, const SRpcMsg *pReq) {
S
Shengliang Guan 已提交
404 405 406 407 408 409 410
  STrans *pTrans = calloc(1, sizeof(STrans));
  if (pTrans == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("failed to create transaction since %s", terrstr());
    return NULL;
  }

S
Shengliang Guan 已提交
411
  pTrans->id = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
S
Shengliang Guan 已提交
412 413
  pTrans->stage = TRN_STAGE_PREPARE;
  pTrans->policy = policy;
S
Shengliang Guan 已提交
414 415
  pTrans->rpcHandle = pReq->handle;
  pTrans->rpcAHandle = pReq->ahandle;
S
Shengliang Guan 已提交
416 417 418 419 420
  pTrans->redoLogs = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(void *));
  pTrans->undoLogs = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(void *));
  pTrans->commitLogs = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(void *));
  pTrans->redoActions = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(STransAction));
  pTrans->undoActions = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(STransAction));
S
Shengliang Guan 已提交
421 422 423 424 425 426 427 428

  if (pTrans->redoLogs == NULL || pTrans->undoLogs == NULL || pTrans->commitLogs == NULL ||
      pTrans->redoActions == NULL || pTrans->undoActions == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("failed to create transaction since %s", terrstr());
    return NULL;
  }

S
Shengliang Guan 已提交
429
  mDebug("trans:%d, is created, data:%p", pTrans->id, pTrans);
S
Shengliang Guan 已提交
430 431 432
  return pTrans;
}

S
Shengliang Guan 已提交
433
static void mndTransDropLogs(SArray *pArray) {
S
Shengliang Guan 已提交
434 435
  int32_t size = taosArrayGetSize(pArray);
  for (int32_t i = 0; i < size; ++i) {
S
Shengliang Guan 已提交
436
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
437
    sdbFreeRaw(pRaw);
S
Shengliang Guan 已提交
438 439 440 441 442
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
443
static void mndTransDropActions(SArray *pArray) {
S
Shengliang Guan 已提交
444 445
  int32_t size = taosArrayGetSize(pArray);
  for (int32_t i = 0; i < size; ++i) {
S
Shengliang Guan 已提交
446
    STransAction *pAction = taosArrayGet(pArray, i);
S
Shengliang Guan 已提交
447
    tfree(pAction->pCont);
S
Shengliang Guan 已提交
448 449 450 451 452
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
453
void mndTransDrop(STrans *pTrans) {
S
Shengliang 已提交
454 455 456 457 458
  if (pTrans != NULL) {
    mndTransDropData(pTrans);
    mDebug("trans:%d, is dropped, data:%p", pTrans->id, pTrans);
    tfree(pTrans);
  }
S
Shengliang Guan 已提交
459 460
}

S
Shengliang Guan 已提交
461
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
462 463 464 465 466
  if (pArray == NULL || pRaw == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
467
  void *ptr = taosArrayPush(pArray, &pRaw);
S
Shengliang Guan 已提交
468 469 470 471 472 473 474 475
  if (ptr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
476
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) { return mndTransAppendLog(pTrans->redoLogs, pRaw); }
S
Shengliang Guan 已提交
477

S
Shengliang Guan 已提交
478
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) { return mndTransAppendLog(pTrans->undoLogs, pRaw); }
S
Shengliang Guan 已提交
479

S
Shengliang Guan 已提交
480
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) { return mndTransAppendLog(pTrans->commitLogs, pRaw); }
S
Shengliang Guan 已提交
481

S
Shengliang Guan 已提交
482
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
483
  void *ptr = taosArrayPush(pArray, pAction);
S
Shengliang Guan 已提交
484 485 486 487 488 489 490 491
  if (ptr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
492
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
S
Shengliang Guan 已提交
493
  return mndTransAppendAction(pTrans->redoActions, pAction);
S
Shengliang Guan 已提交
494 495
}

S
Shengliang Guan 已提交
496
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
S
Shengliang Guan 已提交
497
  return mndTransAppendAction(pTrans->undoActions, pAction);
S
Shengliang Guan 已提交
498 499
}

S
Shengliang Guan 已提交
500 501 502 503 504
void mndTransSetRpcRsp(STrans *pTrans, void *pCont, int32_t contLen) {
  pTrans->rpcRsp = pCont;
  pTrans->rpcRspLen = contLen;
}

S
Shengliang Guan 已提交
505
static int32_t mndTransSync(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
506
  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
S
Shengliang Guan 已提交
507
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
508
    mError("trans:%d, failed to encode while sync trans since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
509 510
    return -1;
  }
S
Shengliang Guan 已提交
511
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
512

S
Shengliang Guan 已提交
513
  mDebug("trans:%d, sync to other nodes", pTrans->id);
S
Shengliang Guan 已提交
514 515 516 517
  int32_t code = mndSyncPropose(pMnode, pRaw);
  if (code != 0) {
    mError("trans:%d, failed to sync since %s", pTrans->id, terrstr());
    sdbFreeRaw(pRaw);
S
Shengliang Guan 已提交
518 519 520
    return -1;
  }

S
Shengliang Guan 已提交
521
  mDebug("trans:%d, sync finished", pTrans->id);
S
Shengliang Guan 已提交
522

S
Shengliang Guan 已提交
523 524 525 526 527 528
  code = sdbWrite(pMnode->pSdb, pRaw);
  if (code != 0) {
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
529 530 531 532 533 534 535 536 537 538 539
  return 0;
}

int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
  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 已提交
540 541
  STrans *pNew = mndAcquireTrans(pMnode, pTrans->id);
  if (pNew == NULL) {
S
Shengliang Guan 已提交
542
    mError("trans:%d, failed to read from sdb since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
543 544 545
    return -1;
  }

S
Shengliang Guan 已提交
546 547
  pNew->rpcHandle = pTrans->rpcHandle;
  pNew->rpcAHandle = pTrans->rpcAHandle;
S
Shengliang Guan 已提交
548 549 550 551 552
  pNew->rpcRsp = pTrans->rpcRsp;
  pNew->rpcRspLen = pTrans->rpcRspLen;
  pTrans->rpcRsp = NULL;
  pTrans->rpcRspLen = 0;

S
Shengliang Guan 已提交
553 554
  mndTransExecute(pMnode, pNew);
  mndReleaseTrans(pMnode, pNew);
S
Shengliang Guan 已提交
555 556 557
  return 0;
}

S
Shengliang Guan 已提交
558 559
static int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
  if (taosArrayGetSize(pTrans->commitLogs) == 0 && taosArrayGetSize(pTrans->redoActions) == 0) return 0;
S
Shengliang Guan 已提交
560

S
Shengliang Guan 已提交
561 562 563
  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 已提交
564
    return -1;
S
Shengliang Guan 已提交
565 566
  }
  mDebug("trans:%d, commit finished", pTrans->id);
S
Shengliang Guan 已提交
567 568 569
  return 0;
}

S
Shengliang Guan 已提交
570
static int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
571
  mDebug("trans:%d, rollback transaction", pTrans->id);
S
Shengliang Guan 已提交
572 573
  if (mndTransSync(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to rollback since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
574
    return -1;
S
Shengliang Guan 已提交
575
  }
S
Shengliang Guan 已提交
576 577
  mDebug("trans:%d, rollback finished", pTrans->id);
  return 0;
S
Shengliang Guan 已提交
578
}
S
Shengliang Guan 已提交
579

S
Shengliang Guan 已提交
580
static void mndTransSendRpcRsp(STrans *pTrans) {
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  bool sendRsp = false;

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

  if (pTrans->policy == TRN_POLICY_ROLLBACK) {
    if (pTrans->stage == TRN_STAGE_UNDO_LOG || pTrans->stage == TRN_STAGE_UNDO_ACTION ||
        pTrans->stage == TRN_STAGE_ROLLBACK) {
      sendRsp = true;
    }
  }

  if (pTrans->policy == TRN_POLICY_RETRY) {
    if (pTrans->stage == TRN_STAGE_REDO_ACTION && pTrans->failedTimes > 0) {
      sendRsp = true;
597
    }
S
Shengliang Guan 已提交
598
  }
599 600

  if (sendRsp && pTrans->rpcHandle != NULL) {
S
Shengliang Guan 已提交
601 602 603 604 605 606
    void *rpcCont = rpcMallocCont(pTrans->rpcRspLen);
    if (rpcCont != NULL) {
      memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen);
    }
    free(pTrans->rpcRsp);

607 608
    mDebug("trans:%d, send rsp, code:0x%x stage:%d app:%p", pTrans->id, pTrans->code & 0xFFFF, pTrans->stage,
           pTrans->rpcAHandle);
S
Shengliang Guan 已提交
609 610 611
    SRpcMsg rspMsg = {.handle = pTrans->rpcHandle,
                      .code = pTrans->code,
                      .ahandle = pTrans->rpcAHandle,
S
Shengliang Guan 已提交
612
                      .pCont = rpcCont,
S
Shengliang Guan 已提交
613
                      .contLen = pTrans->rpcRspLen};
614 615
    rpcSendResponse(&rspMsg);
    pTrans->rpcHandle = NULL;
S
Shengliang Guan 已提交
616 617
    pTrans->rpcRsp = NULL;
    pTrans->rpcRspLen = 0;
618
  }
S
Shengliang Guan 已提交
619 620
}

S
Shengliang Guan 已提交
621 622 623
void mndTransProcessRsp(SMnodeMsg *pRsp) {
  SMnode *pMnode = pRsp->pMnode;
  int64_t signature = (int64_t)(pRsp->rpcMsg.ahandle);
S
Shengliang Guan 已提交
624 625
  int32_t transId = (int32_t)(signature >> 32);
  int32_t action = (int32_t)((signature << 32) >> 32);
626 627 628 629 630 631 632 633

  STrans *pTrans = mndAcquireTrans(pMnode, transId);
  if (pTrans == NULL) {
    mError("trans:%d, failed to get transId from vnode rsp since %s", transId, terrstr());
    goto HANDLE_ACTION_RSP_OVER;
  }

  SArray *pArray = NULL;
S
Shengliang Guan 已提交
634
  if (pTrans->stage == TRN_STAGE_REDO_ACTION) {
635
    pArray = pTrans->redoActions;
S
Shengliang Guan 已提交
636
  } else if (pTrans->stage == TRN_STAGE_UNDO_ACTION) {
637 638
    pArray = pTrans->undoActions;
  } else {
S
Shengliang Guan 已提交
639 640
    mError("trans:%d, invalid trans stage:%d while recv action rsp", pTrans->id, pTrans->stage);
    goto HANDLE_ACTION_RSP_OVER;
641 642 643
  }

  if (pArray == NULL) {
S
Shengliang Guan 已提交
644
    mError("trans:%d, invalid trans stage:%d", transId, pTrans->stage);
645 646 647 648
    goto HANDLE_ACTION_RSP_OVER;
  }

  int32_t actionNum = taosArrayGetSize(pTrans->redoActions);
S
Shengliang Guan 已提交
649
  if (action < 0 || action >= actionNum) {
650 651 652 653 654 655 656
    mError("trans:%d, invalid action:%d", transId, action);
    goto HANDLE_ACTION_RSP_OVER;
  }

  STransAction *pAction = taosArrayGet(pArray, action);
  if (pAction != NULL) {
    pAction->msgReceived = 1;
S
Shengliang Guan 已提交
657
    pAction->errCode = pRsp->rpcMsg.code;
658 659
  }

S
Shengliang Guan 已提交
660
  mDebug("trans:%d, action:%d response is received, code:0x%x, accept:0x%x", transId, action, pRsp->rpcMsg.code,
661
         pAction->acceptableCode);
662 663 664 665 666 667
  mndTransExecute(pMnode, pTrans);

HANDLE_ACTION_RSP_OVER:
  mndReleaseTrans(pMnode, pTrans);
}

S
Shengliang Guan 已提交
668
static int32_t mndTransExecuteLogs(SMnode *pMnode, SArray *pArray) {
S
Shengliang Guan 已提交
669 670 671
  SSdb   *pSdb = pMnode->pSdb;
  int32_t arraySize = taosArrayGetSize(pArray);

S
Shengliang Guan 已提交
672 673
  if (arraySize == 0) return 0;

S
Shengliang Guan 已提交
674
  for (int32_t i = 0; i < arraySize; ++i) {
675
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
676 677 678
    int32_t  code = sdbWriteNotFree(pSdb, pRaw);
    if (code != 0) {
      return code;
S
Shengliang Guan 已提交
679 680 681 682 683 684
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
685
static int32_t mndTransExecuteRedoLogs(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
686
  return mndTransExecuteLogs(pMnode, pTrans->redoLogs);
S
Shengliang Guan 已提交
687 688 689
}

static int32_t mndTransExecuteUndoLogs(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
690
  return mndTransExecuteLogs(pMnode, pTrans->undoLogs);
S
Shengliang Guan 已提交
691 692 693
}

static int32_t mndTransExecuteCommitLogs(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
694
  return mndTransExecuteLogs(pMnode, pTrans->commitLogs);
S
Shengliang Guan 已提交
695
}
S
Shengliang Guan 已提交
696

S
Shengliang Guan 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
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);
    if (pAction == NULL) continue;
    if (pAction->msgSent && pAction->msgReceived && pAction->errCode == 0) continue;

    pAction->msgSent = 0;
    pAction->msgReceived = 0;
    pAction->errCode = 0;
    mDebug("trans:%d, action:%d is reset and will be re-executed", pTrans->id, action);
  }
}

static int32_t mndTransSendActionMsg(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
713 714 715 716 717
  int32_t numOfActions = taosArrayGetSize(pArray);

  for (int32_t action = 0; action < numOfActions; ++action) {
    STransAction *pAction = taosArrayGet(pArray, action);
    if (pAction == NULL) continue;
S
Shengliang Guan 已提交
718
    if (pAction->msgSent) continue;
S
Shengliang Guan 已提交
719

720 721 722 723 724
    int64_t signature = pTrans->id;
    signature = (signature << 32);
    signature += action;

    SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .ahandle = (void *)signature};
S
Shengliang Guan 已提交
725 726 727 728
    rpcMsg.pCont = rpcMallocCont(pAction->contLen);
    if (rpcMsg.pCont == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
S
Shengliang Guan 已提交
729
    }
S
Shengliang Guan 已提交
730
    memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
731

S
Shengliang Guan 已提交
732 733 734 735 736 737
    if (mndSendReqToDnode(pMnode, &pAction->epSet, &rpcMsg) == 0) {
      mDebug("trans:%d, action:%d is sent", pTrans->id, action);
      pAction->msgSent = 1;
      pAction->msgReceived = 0;
      pAction->errCode = 0;
    } else {
738
      mDebug("trans:%d, action:%d not send since %s", pTrans->id, action, terrstr());
S
Shengliang Guan 已提交
739 740
      return -1;
    }
S
Shengliang Guan 已提交
741 742
  }

S
Shengliang Guan 已提交
743 744 745 746 747 748 749 750 751 752 753
  return 0;
}

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

  if (mndTransSendActionMsg(pMnode, pTrans, pArray) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
754 755
  int32_t numOfReceived = 0;
  int32_t errCode = 0;
756 757 758 759
  for (int32_t action = 0; action < numOfActions; ++action) {
    STransAction *pAction = taosArrayGet(pArray, action);
    if (pAction == NULL) continue;
    if (pAction->msgSent && pAction->msgReceived) {
S
Shengliang Guan 已提交
760
      numOfReceived++;
761
      if (pAction->errCode != 0 && pAction->errCode != pAction->acceptableCode) {
S
Shengliang Guan 已提交
762
        errCode = pAction->errCode;
763 764 765 766
      }
    }
  }

S
Shengliang Guan 已提交
767
  if (numOfReceived == numOfActions) {
S
Shengliang Guan 已提交
768 769 770 771 772 773 774 775 776
    if (errCode == 0) {
      mDebug("trans:%d, all %d actions execute successfully", pTrans->id, numOfActions);
      return 0;
    } else {
      mError("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errCode);
      mndTransResetActions(pMnode, pTrans, pArray);
      terrno = errCode;
      return errCode;
    }
777
  } else {
S
Shengliang Guan 已提交
778
    mDebug("trans:%d, %d of %d actions executed, code:0x%x", pTrans->id, numOfReceived, numOfActions, errCode);
779 780
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
  }
S
Shengliang Guan 已提交
781 782
}

S
Shengliang Guan 已提交
783
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans) {
784
  return mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions);
S
Shengliang Guan 已提交
785
}
S
Shengliang Guan 已提交
786

S
Shengliang Guan 已提交
787
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans) {
788
  return mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions);
S
Shengliang Guan 已提交
789
}
S
Shengliang Guan 已提交
790

S
Shengliang Guan 已提交
791 792 793 794 795 796 797 798 799
static bool mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans) {
  bool continueExec = true;
  pTrans->stage = TRN_STAGE_REDO_LOG;
  mDebug("trans:%d, stage from prepare to redoLog", pTrans->id);
  return continueExec;
}

static bool mndTransPerformRedoLogStage(SMnode *pMnode, STrans *pTrans) {
  bool    continueExec = true;
S
Shengliang Guan 已提交
800
  int32_t code = mndTransExecuteRedoLogs(pMnode, pTrans);
S
Shengliang Guan 已提交
801

S
Shengliang Guan 已提交
802
  if (code == 0) {
S
Shengliang Guan 已提交
803 804 805
    pTrans->code = 0;
    pTrans->stage = TRN_STAGE_REDO_ACTION;
    mDebug("trans:%d, stage from redoLog to redoAction", pTrans->id);
S
Shengliang Guan 已提交
806
  } else {
S
Shengliang Guan 已提交
807 808
    pTrans->code = terrno;
    pTrans->stage = TRN_STAGE_UNDO_LOG;
809
    mError("trans:%d, stage from redoLog to undoLog since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
810
  }
S
Shengliang Guan 已提交
811 812

  return continueExec;
S
Shengliang Guan 已提交
813 814
}

S
Shengliang Guan 已提交
815 816
static bool mndTransPerformRedoActionStage(SMnode *pMnode, STrans *pTrans) {
  bool    continueExec = true;
S
Shengliang Guan 已提交
817
  int32_t code = mndTransExecuteRedoActions(pMnode, pTrans);
S
Shengliang Guan 已提交
818 819

  if (code == 0) {
S
Shengliang Guan 已提交
820
    pTrans->code = 0;
S
Shengliang Guan 已提交
821
    pTrans->stage = TRN_STAGE_COMMIT;
S
Shengliang Guan 已提交
822 823
    mDebug("trans:%d, stage from redoAction to commit", pTrans->id);
    continueExec = true;
S
Shengliang Guan 已提交
824
  } else if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
825 826
    mDebug("trans:%d, stage keep on redoAction since %s", pTrans->id, tstrerror(code));
    continueExec = false;
S
Shengliang Guan 已提交
827
  } else {
S
Shengliang Guan 已提交
828
    pTrans->code = terrno;
S
Shengliang Guan 已提交
829
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
S
Shengliang Guan 已提交
830 831 832
      pTrans->stage = TRN_STAGE_UNDO_ACTION;
      mError("trans:%d, stage from redoAction to undoAction since %s", pTrans->id, terrstr());
      continueExec = true;
S
Shengliang Guan 已提交
833
    } else {
S
Shengliang Guan 已提交
834 835 836
      pTrans->failedTimes++;
      mError("trans:%d, stage keep on redoAction since %s, failedTimes:%d", pTrans->id, terrstr(), pTrans->failedTimes);
      continueExec = false;
S
Shengliang Guan 已提交
837 838 839
    }
  }

S
Shengliang Guan 已提交
840
  return continueExec;
S
Shengliang Guan 已提交
841 842
}

S
Shengliang Guan 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
static bool mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans) {
  bool    continueExec = true;
  int32_t code = mndTransCommit(pMnode, pTrans);

  if (code == 0) {
    pTrans->code = 0;
    pTrans->stage = TRN_STAGE_COMMIT_LOG;
    mDebug("trans:%d, stage from commit to commitLog", pTrans->id);
    continueExec = true;
  } else {
    pTrans->code = terrno;
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
      pTrans->stage = TRN_STAGE_REDO_ACTION;
      mError("trans:%d, stage from commit to redoAction since %s, failedTimes:%d", pTrans->id, terrstr(),
             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 已提交
867 868
}

S
Shengliang Guan 已提交
869 870 871
static bool mndTransPerformCommitLogStage(SMnode *pMnode, STrans *pTrans) {
  bool    continueExec = true;
  int32_t code = mndTransExecuteCommitLogs(pMnode, pTrans);
S
Shengliang Guan 已提交
872 873

  if (code == 0) {
S
Shengliang Guan 已提交
874 875 876 877
    pTrans->code = 0;
    pTrans->stage = TRN_STAGE_FINISHED;
    mDebug("trans:%d, stage from commitLog to finished", pTrans->id);
    continueExec = true;
S
Shengliang Guan 已提交
878
  } else {
S
Shengliang Guan 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892
    pTrans->code = terrno;
    pTrans->failedTimes++;
    mError("trans:%d, stage keep on commitLog since %s", pTrans->id, terrstr());
    continueExec = false;
  }

  return continueExec;
}

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

  if (code == 0) {
S
Shengliang Guan 已提交
893
    pTrans->stage = TRN_STAGE_ROLLBACK;
S
Shengliang Guan 已提交
894 895 896
    mDebug("trans:%d, stage from undoLog to rollback", pTrans->id);
    continueExec = true;
  } else {
S
Shengliang Guan 已提交
897
    mError("trans:%d, stage keep on undoLog since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
898 899 900 901 902 903 904 905 906 907 908
    continueExec = false;
  }

  return continueExec;
}

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

  if (code == 0) {
909
    pTrans->stage = TRN_STAGE_UNDO_LOG;
S
Shengliang Guan 已提交
910 911 912
    mDebug("trans:%d, stage from undoAction to undoLog", pTrans->id);
    continueExec = true;
  } else if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
913
    mDebug("trans:%d, stage keep on undoAction since %s", pTrans->id, tstrerror(code));
S
Shengliang Guan 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
    continueExec = false;
  } else {
    pTrans->failedTimes++;
    mError("trans:%d, stage keep on undoAction since %s", pTrans->id, terrstr());
    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++;
S
Shengliang Guan 已提交
934
    mError("trans:%d, stage keep on rollback since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
935
    continueExec = false;
S
Shengliang Guan 已提交
936 937
  }

S
Shengliang Guan 已提交
938 939 940 941 942 943 944 945
  return continueExec;
}

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

  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
946
    mError("trans:%d, failed to encode while finish trans since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
947 948 949 950 951 952 953 954
  }
  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());
  }

S
Shengliang Guan 已提交
955
  mDebug("trans:%d, finished, code:0x%x, failedTimes:%d", pTrans->id, pTrans->code, pTrans->failedTimes);
S
Shengliang Guan 已提交
956
  return continueExec;
S
Shengliang Guan 已提交
957
}
S
Shengliang Guan 已提交
958

S
Shengliang Guan 已提交
959
static void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
960
  bool continueExec = true;
S
Shengliang Guan 已提交
961

S
Shengliang Guan 已提交
962
  while (continueExec) {
S
Shengliang Guan 已提交
963 964
    switch (pTrans->stage) {
      case TRN_STAGE_PREPARE:
S
Shengliang Guan 已提交
965 966 967 968 969 970 971 972 973 974
        continueExec = mndTransPerformPrepareStage(pMnode, pTrans);
        break;
      case TRN_STAGE_REDO_LOG:
        continueExec = mndTransPerformRedoLogStage(pMnode, pTrans);
        break;
      case TRN_STAGE_REDO_ACTION:
        continueExec = mndTransPerformRedoActionStage(pMnode, pTrans);
        break;
      case TRN_STAGE_UNDO_LOG:
        continueExec = mndTransPerformUndoLogStage(pMnode, pTrans);
S
Shengliang Guan 已提交
975
        break;
S
Shengliang Guan 已提交
976 977 978 979 980
      case TRN_STAGE_UNDO_ACTION:
        continueExec = mndTransPerformUndoActionStage(pMnode, pTrans);
        break;
      case TRN_STAGE_COMMIT_LOG:
        continueExec = mndTransPerformCommitLogStage(pMnode, pTrans);
S
Shengliang Guan 已提交
981 982
        break;
      case TRN_STAGE_COMMIT:
S
Shengliang Guan 已提交
983
        continueExec = mndTransPerformCommitStage(pMnode, pTrans);
S
Shengliang Guan 已提交
984 985
        break;
      case TRN_STAGE_ROLLBACK:
S
Shengliang Guan 已提交
986 987 988 989
        continueExec = mndTransPerformRollbackStage(pMnode, pTrans);
        break;
      case TRN_STAGE_FINISHED:
        continueExec = mndTransPerfromFinishedStage(pMnode, pTrans);
S
Shengliang Guan 已提交
990
        break;
S
Shengliang Guan 已提交
991
      default:
S
Shengliang Guan 已提交
992 993
        continueExec = false;
        break;
S
Shengliang Guan 已提交
994 995 996
    }
  }

997
  mndTransSendRpcRsp(pTrans);
S
Shengliang Guan 已提交
998
}
S
Shengliang Guan 已提交
999

S
Shengliang Guan 已提交
1000 1001
static int32_t mndProcessTransReq(SMnodeMsg *pReq) {
  mndTransPullup(pReq->pMnode);
S
Shengliang Guan 已提交
1002 1003 1004 1005
  return 0;
}

void mndTransPullup(SMnode *pMnode) {
S
Shengliang Guan 已提交
1006
  STrans *pTrans = NULL;
S
Shengliang Guan 已提交
1007
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1008 1009 1010 1011 1012 1013 1014 1015

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

    mndTransExecute(pMnode, pTrans);
    sdbRelease(pMnode->pSdb, pTrans);
  }
S
Shengliang Guan 已提交
1016 1017

  sdbWriteFile(pMnode->pSdb);
1018
}
S
Shengliang Guan 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044

static int32_t mndGetTransMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) {
  SMnode *pMnode = pReq->pMnode;
  SSdb   *pSdb = pMnode->pSdb;

  int32_t  cols = 0;
  SSchema *pSchema = pMeta->pSchemas;

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "id");
  pSchema[cols].bytes = pShow->bytes[cols];
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
  strcpy(pSchema[cols].name, "create_time");
  pSchema[cols].bytes = pShow->bytes[cols];
  cols++;

  pShow->bytes[cols] = TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "stage");
  pSchema[cols].bytes = pShow->bytes[cols];
  cols++;

S
Shengliang Guan 已提交
1045
  pShow->bytes[cols] = TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE;
S
Shengliang Guan 已提交
1046 1047 1048 1049 1050
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "db");
  pSchema[cols].bytes = pShow->bytes[cols];
  cols++;

S
Shengliang Guan 已提交
1051
  pShow->bytes[cols] = TSDB_TRANS_DESC_LEN + VARSTR_HEADER_SIZE;
S
Shengliang Guan 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "type");
  pSchema[cols].bytes = pShow->bytes[cols];
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
  strcpy(pSchema[cols].name, "last_exec_time");
  pSchema[cols].bytes = pShow->bytes[cols];
  cols++;

S
Shengliang Guan 已提交
1063
  pShow->bytes[cols] = TSDB_TRANS_ERROR_LEN + VARSTR_HEADER_SIZE;
S
Shengliang Guan 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "last_error");
  pSchema[cols].bytes = pShow->bytes[cols];
  cols++;

  pMeta->numOfColumns = cols;
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }

  pShow->numOfRows = sdbGetSize(pSdb, SDB_TRANS);
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
  return 0;
}

static int32_t mndRetrieveTrans(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) {
  SMnode *pMnode = pReq->pMnode;
  SSdb   *pSdb = pMnode->pSdb;
  int32_t numOfRows = 0;
  STrans *pTrans = NULL;
  int32_t cols = 0;
  char   *pWrite;

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

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int32_t *)pWrite = pTrans->id;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pTrans->createdTime;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, mndTransStr(pTrans->stage));
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, pTrans->dbname);
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, mndTransType(pTrans->transType));
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pTrans->lastExecTime;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_TO_VARSTR(pWrite, pTrans->lastError);
    cols++;

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

  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

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