mndTrans.c 26.7 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 "mndSync.h"
S
Shengliang Guan 已提交
19

S
Shengliang Guan 已提交
20
#define TSDB_TRANS_VER_NUMBER 1
S
Shengliang Guan 已提交
21 22
#define TSDB_TRN_ARRAY_SIZE 8
#define TSDB_TRN_RESERVE_SIZE 64
S
Shengliang Guan 已提交
23

S
Shengliang Guan 已提交
24 25 26
static SSdbRaw *mndTransActionEncode(STrans *pTrans);
static SSdbRow *mndTransActionDecode(SSdbRaw *pRaw);
static int32_t  mndTransActionInsert(SSdb *pSdb, STrans *pTrans);
S
Shengliang Guan 已提交
27
static int32_t  mndTransActionUpdate(SSdb *pSdb, STrans *OldTrans, STrans *pOldTrans);
S
Shengliang Guan 已提交
28 29
static int32_t  mndTransActionDelete(SSdb *pSdb, STrans *pTrans);

S
Shengliang Guan 已提交
30 31
static void    mndTransSetRpcHandle(STrans *pTrans, void *rpcHandle);
static void    mndTransSendRpcRsp(STrans *pTrans, int32_t code);
S
Shengliang Guan 已提交
32
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw);
S
Shengliang Guan 已提交
33
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction);
S
Shengliang Guan 已提交
34 35 36
static void    mndTransDropLogs(SArray *pArray);
static void    mndTransDropActions(SArray *pArray);
static int32_t mndTransExecuteLogs(SMnode *pMnode, SArray *pArray);
37
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46 47 48
static int32_t mndTransExecuteRedoLogs(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransExecuteUndoLogs(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransExecuteCommitLogs(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransPerformExecuteStage(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans);
static int32_t mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans);
static void    mndTransExecute(SMnode *pMnode, STrans *pTrans);

S
Shengliang Guan 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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};

  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupTrans(SMnode *pMnode) {}

static SSdbRaw *mndTransActionEncode(STrans *pTrans) {
S
Shengliang Guan 已提交
64
  int32_t rawDataLen = sizeof(STrans) + TSDB_TRN_RESERVE_SIZE;
S
Shengliang Guan 已提交
65 66 67 68 69 70
  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);

71
  for (int32_t i = 0; i < redoLogNum; ++i) {
S
Shengliang Guan 已提交
72
    SSdbRaw *pTmp = taosArrayGetP(pTrans->redoLogs, i);
S
Shengliang Guan 已提交
73 74 75
    rawDataLen += sdbGetRawTotalSize(pTmp);
  }

76
  for (int32_t i = 0; i < undoLogNum; ++i) {
S
Shengliang Guan 已提交
77
    SSdbRaw *pTmp = taosArrayGetP(pTrans->undoLogs, i);
S
Shengliang Guan 已提交
78 79 80
    rawDataLen += sdbGetRawTotalSize(pTmp);
  }

81
  for (int32_t i = 0; i < commitLogNum; ++i) {
S
Shengliang Guan 已提交
82
    SSdbRaw *pTmp = taosArrayGetP(pTrans->commitLogs, i);
S
Shengliang Guan 已提交
83 84 85
    rawDataLen += sdbGetRawTotalSize(pTmp);
  }

S
Shengliang Guan 已提交
86 87
  for (int32_t i = 0; i < redoActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
S
Shengliang Guan 已提交
88
    rawDataLen += (sizeof(STransAction) + pAction->contLen);
S
Shengliang Guan 已提交
89 90 91 92
  }

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

S
Shengliang Guan 已提交
96
  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, TSDB_TRANS_VER_NUMBER, rawDataLen);
S
Shengliang Guan 已提交
97
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
98
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
99 100 101 102 103 104 105 106 107 108 109 110
    return NULL;
  }

  int32_t dataPos = 0;
  SDB_SET_INT32(pRaw, dataPos, pTrans->id)
  SDB_SET_INT8(pRaw, dataPos, pTrans->policy)
  SDB_SET_INT32(pRaw, dataPos, redoLogNum)
  SDB_SET_INT32(pRaw, dataPos, undoLogNum)
  SDB_SET_INT32(pRaw, dataPos, commitLogNum)
  SDB_SET_INT32(pRaw, dataPos, redoActionNum)
  SDB_SET_INT32(pRaw, dataPos, undoActionNum)

111
  for (int32_t i = 0; i < redoLogNum; ++i) {
S
Shengliang Guan 已提交
112
    SSdbRaw *pTmp = taosArrayGetP(pTrans->redoLogs, i);
S
Shengliang Guan 已提交
113 114 115 116 117
    int32_t  len = sdbGetRawTotalSize(pTmp);
    SDB_SET_INT32(pRaw, dataPos, len)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len)
  }

118
  for (int32_t i = 0; i < undoLogNum; ++i) {
S
Shengliang Guan 已提交
119
    SSdbRaw *pTmp = taosArrayGetP(pTrans->undoLogs, i);
S
Shengliang Guan 已提交
120 121 122 123 124
    int32_t  len = sdbGetRawTotalSize(pTmp);
    SDB_SET_INT32(pRaw, dataPos, len)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len)
  }

125
  for (int32_t i = 0; i < commitLogNum; ++i) {
S
Shengliang Guan 已提交
126
    SSdbRaw *pTmp = taosArrayGetP(pTrans->commitLogs, i);
S
Shengliang Guan 已提交
127 128 129 130 131
    int32_t  len = sdbGetRawTotalSize(pTmp);
    SDB_SET_INT32(pRaw, dataPos, len)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len)
  }

S
Shengliang Guan 已提交
132 133 134
  for (int32_t i = 0; i < redoActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->redoActions, i);
    SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet));
S
Shengliang Guan 已提交
135 136 137
    SDB_SET_INT8(pRaw, dataPos, pAction->msgType)
    SDB_SET_INT32(pRaw, dataPos, pAction->contLen)
    SDB_SET_BINARY(pRaw, dataPos, pAction->pCont, pAction->contLen);
S
Shengliang Guan 已提交
138 139 140 141 142
  }

  for (int32_t i = 0; i < undoActionNum; ++i) {
    STransAction *pAction = taosArrayGet(pTrans->undoActions, i);
    SDB_SET_BINARY(pRaw, dataPos, (void *)&pAction->epSet, sizeof(SEpSet));
S
Shengliang Guan 已提交
143 144 145
    SDB_SET_INT8(pRaw, dataPos, pAction->msgType)
    SDB_SET_INT32(pRaw, dataPos, pAction->contLen)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pAction->pCont, pAction->contLen);
S
Shengliang Guan 已提交
146 147
  }

S
Shengliang Guan 已提交
148 149
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_TRN_RESERVE_SIZE)
  SDB_SET_DATALEN(pRaw, dataPos);
S
Shengliang Guan 已提交
150
  mTrace("trans:%d, encode to raw:%p, len:%d", pTrans->id, pRaw, dataPos);
S
Shengliang Guan 已提交
151 152 153
  return pRaw;
}

S
Shengliang Guan 已提交
154 155 156
static SSdbRow *mndTransActionDecode(SSdbRaw *pRaw) {
  int32_t code = 0;

S
Shengliang Guan 已提交
157 158 159 160 161 162
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
    mError("failed to get soft ver from raw:%p since %s", pRaw, terrstr());
    return NULL;
  }

S
Shengliang Guan 已提交
163
  if (sver != TSDB_TRANS_VER_NUMBER) {
S
Shengliang Guan 已提交
164 165 166 167 168
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    mError("failed to get check soft ver from raw:%p since %s", pRaw, terrstr());
    return NULL;
  }

S
Shengliang Guan 已提交
169 170
  SSdbRow *pRow = sdbAllocRow(sizeof(STrans));
  STrans  *pTrans = sdbGetRowObj(pRow);
S
Shengliang Guan 已提交
171 172 173 174 175
  if (pTrans == NULL) {
    mError("failed to alloc trans from raw:%p since %s", pRaw, terrstr());
    return NULL;
  }

S
Shengliang Guan 已提交
176 177 178
  pTrans->redoLogs = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(void *));
  pTrans->undoLogs = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(void *));
  pTrans->commitLogs = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(void *));
S
Shengliang Guan 已提交
179 180
  pTrans->redoActions = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(STransAction));
  pTrans->undoActions = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(STransAction));
S
Shengliang Guan 已提交
181 182 183

  if (pTrans->redoLogs == NULL || pTrans->undoLogs == NULL || pTrans->commitLogs == NULL ||
      pTrans->redoActions == NULL || pTrans->undoActions == NULL) {
S
Shengliang Guan 已提交
184 185 186
    mDebug("trans:%d, failed to create array while parsed from raw:%p", pTrans->id, pRaw);
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  }

  int32_t redoLogNum = 0;
  int32_t undoLogNum = 0;
  int32_t commitLogNum = 0;
  int32_t redoActionNum = 0;
  int32_t undoActionNum = 0;

  int32_t dataPos = 0;
  SDB_GET_INT32(pRaw, pRow, dataPos, &pTrans->id)
  SDB_GET_INT8(pRaw, pRow, dataPos, (int8_t *)&pTrans->policy)
  SDB_GET_INT32(pRaw, pRow, dataPos, &redoLogNum)
  SDB_GET_INT32(pRaw, pRow, dataPos, &undoLogNum)
  SDB_GET_INT32(pRaw, pRow, dataPos, &commitLogNum)
  SDB_GET_INT32(pRaw, pRow, dataPos, &redoActionNum)
  SDB_GET_INT32(pRaw, pRow, dataPos, &undoActionNum)

204
  for (int32_t i = 0; i < redoLogNum; ++i) {
S
Shengliang Guan 已提交
205 206 207 208
    int32_t dataLen = 0;
    SDB_GET_INT32(pRaw, pRow, dataPos, &dataLen)
    char *pData = malloc(dataLen);
    SDB_GET_BINARY(pRaw, pRow, dataPos, pData, dataLen);
S
Shengliang Guan 已提交
209

S
Shengliang Guan 已提交
210
    void *ret = taosArrayPush(pTrans->redoLogs, &pData);
S
Shengliang Guan 已提交
211 212
    if (ret == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
213
      goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
214 215 216
    }
  }

S
Shengliang Guan 已提交
217 218 219 220 221
  for (int32_t i = 0; i < undoLogNum; ++i) {
    int32_t dataLen = 0;
    SDB_GET_INT32(pRaw, pRow, dataPos, &dataLen)
    char *pData = malloc(dataLen);
    SDB_GET_BINARY(pRaw, pRow, dataPos, pData, dataLen);
S
Shengliang Guan 已提交
222

S
Shengliang Guan 已提交
223 224 225 226 227 228 229 230 231 232 233 234
    void *ret = taosArrayPush(pTrans->undoLogs, &pData);
    if (ret == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto TRANS_DECODE_OVER;
    }
  }

  for (int32_t i = 0; i < commitLogNum; ++i) {
    int32_t dataLen = 0;
    SDB_GET_INT32(pRaw, pRow, dataPos, &dataLen)
    char *pData = malloc(dataLen);
    SDB_GET_BINARY(pRaw, pRow, dataPos, pData, dataLen);
S
Shengliang Guan 已提交
235

S
Shengliang Guan 已提交
236 237 238 239
    void *ret = taosArrayPush(pTrans->commitLogs, &pData);
    if (ret == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
240 241 242 243 244 245
    }
  }

  for (int32_t i = 0; i < redoActionNum; ++i) {
    STransAction action = {0};
    SDB_GET_BINARY(pRaw, pRow, dataPos, (void *)&action.epSet, sizeof(SEpSet));
S
Shengliang Guan 已提交
246 247 248 249
    SDB_GET_INT8(pRaw, pRow, dataPos, &action.msgType)
    SDB_GET_INT32(pRaw, pRow, dataPos, &action.contLen)
    action.pCont = malloc(action.contLen);
    if (action.pCont == NULL) {
S
Shengliang Guan 已提交
250 251 252
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto TRANS_DECODE_OVER;
    }
S
Shengliang Guan 已提交
253
    SDB_GET_BINARY(pRaw, pRow, dataPos, action.pCont, action.contLen);
S
Shengliang Guan 已提交
254 255 256 257 258 259 260 261 262 263 264

    void *ret = taosArrayPush(pTrans->redoActions, &action);
    if (ret == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto TRANS_DECODE_OVER;
    }
  }

  for (int32_t i = 0; i < undoActionNum; ++i) {
    STransAction action = {0};
    SDB_GET_BINARY(pRaw, pRow, dataPos, (void *)&action.epSet, sizeof(SEpSet));
S
Shengliang Guan 已提交
265 266 267 268
    SDB_GET_INT8(pRaw, pRow, dataPos, &action.msgType)
    SDB_GET_INT32(pRaw, pRow, dataPos, &action.contLen)
    action.pCont = malloc(action.contLen);
    if (action.pCont == NULL) {
S
Shengliang Guan 已提交
269 270 271
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto TRANS_DECODE_OVER;
    }
S
Shengliang Guan 已提交
272
    SDB_GET_BINARY(pRaw, pRow, dataPos, action.pCont, action.contLen);
S
Shengliang Guan 已提交
273 274 275 276 277

    void *ret = taosArrayPush(pTrans->undoActions, &action);
    if (ret == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
278 279 280
    }
  }

S
Shengliang Guan 已提交
281 282
  SDB_GET_RESERVE(pRaw, pRow, dataPos, TSDB_TRN_RESERVE_SIZE)

S
Shengliang Guan 已提交
283
TRANS_DECODE_OVER:
S
Shengliang Guan 已提交
284
  if (code != 0) {
S
Shengliang Guan 已提交
285
    mError("trans:%d, failed to parse from raw:%p since %s", pTrans->id, pRaw, tstrerror(errno));
S
Shengliang Guan 已提交
286
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
287
    terrno = code;
S
Shengliang Guan 已提交
288 289 290
    return NULL;
  }

S
Shengliang Guan 已提交
291
  mTrace("trans:%d, decode from raw:%p", pTrans->id, pRaw);
S
Shengliang Guan 已提交
292 293 294
  return pRow;
}

S
Shengliang Guan 已提交
295
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
S
Shengliang Guan 已提交
296
  pTrans->stage = TRN_STAGE_PREPARE;
S
Shengliang Guan 已提交
297
  mTrace("trans:%d, perform insert action, stage:%s", pTrans->id, mndTransStageStr(pTrans->stage));
S
Shengliang Guan 已提交
298 299 300
  return 0;
}

S
Shengliang Guan 已提交
301
static int32_t mndTransActionDelete(SSdb *pSdb, STrans *pTrans) {
S
Shengliang Guan 已提交
302
  mTrace("trans:%d, perform delete action, stage:%s", pTrans->id, mndTransStageStr(pTrans->stage));
S
Shengliang Guan 已提交
303

S
Shengliang Guan 已提交
304 305 306 307 308
  mndTransDropLogs(pTrans->redoLogs);
  mndTransDropLogs(pTrans->undoLogs);
  mndTransDropLogs(pTrans->commitLogs);
  mndTransDropActions(pTrans->redoActions);
  mndTransDropActions(pTrans->undoActions);
S
Shengliang Guan 已提交
309 310 311 312

  return 0;
}

S
Shengliang Guan 已提交
313
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOldTrans, STrans *pNewTrans) {
S
Shengliang Guan 已提交
314
  mTrace("trans:%d, perform update action, stage:%s", pOldTrans->id, mndTransStageStr(pNewTrans->stage));
S
Shengliang Guan 已提交
315
  pOldTrans->stage = pNewTrans->stage;
S
Shengliang Guan 已提交
316 317 318
  return 0;
}

S
Shengliang Guan 已提交
319
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
S
Shengliang Guan 已提交
320 321 322 323 324 325
  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 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
}

void mndReleaseTrans(SMnode *pMnode, STrans *pTrans) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pTrans);
}

char *mndTransStageStr(ETrnStage stage) {
  switch (stage) {
    case TRN_STAGE_PREPARE:
      return "prepare";
    case TRN_STAGE_EXECUTE:
      return "execute";
    case TRN_STAGE_COMMIT:
      return "commit";
    case TRN_STAGE_ROLLBACK:
      return "rollback";
S
Shengliang Guan 已提交
343
    case TRN_STAGE_OVER:
344
      return "over";
S
Shengliang Guan 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    default:
      return "undefined";
  }
}

char *mndTransPolicyStr(ETrnPolicy policy) {
  switch (policy) {
    case TRN_POLICY_ROLLBACK:
      return "prepare";
    case TRN_POLICY_RETRY:
      return "retry";
    default:
      return "undefined";
  }
}

S
Shengliang Guan 已提交
361
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, void *rpcHandle) {
S
Shengliang Guan 已提交
362 363 364 365 366 367 368
  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 已提交
369
  pTrans->id = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
S
Shengliang Guan 已提交
370 371 372
  pTrans->stage = TRN_STAGE_PREPARE;
  pTrans->policy = policy;
  pTrans->rpcHandle = rpcHandle;
S
Shengliang Guan 已提交
373 374 375
  pTrans->redoLogs = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(void *));
  pTrans->undoLogs = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(void *));
  pTrans->commitLogs = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(void *));
S
Shengliang Guan 已提交
376 377
  pTrans->redoActions = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(STransAction));
  pTrans->undoActions = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(STransAction));
S
Shengliang Guan 已提交
378 379 380 381 382 383 384 385

  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;
  }

386
  mDebug("trans:%d, is created", pTrans->id);
S
Shengliang Guan 已提交
387 388 389
  return pTrans;
}

S
Shengliang Guan 已提交
390
static void mndTransDropLogs(SArray *pArray) {
391
  for (int32_t i = 0; i < pArray->size; ++i) {
S
Shengliang Guan 已提交
392
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
393 394 395 396 397 398
    tfree(pRaw);
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
399
static void mndTransDropActions(SArray *pArray) {
S
Shengliang Guan 已提交
400 401
  for (int32_t i = 0; i < pArray->size; ++i) {
    STransAction *pAction = taosArrayGet(pArray, i);
S
Shengliang Guan 已提交
402
    free(pAction->pCont);
S
Shengliang Guan 已提交
403 404 405 406 407
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
408
void mndTransDrop(STrans *pTrans) {
S
Shengliang Guan 已提交
409 410 411 412 413
  mndTransDropLogs(pTrans->redoLogs);
  mndTransDropLogs(pTrans->undoLogs);
  mndTransDropLogs(pTrans->commitLogs);
  mndTransDropActions(pTrans->redoActions);
  mndTransDropActions(pTrans->undoActions);
S
Shengliang Guan 已提交
414

415
  // mDebug("trans:%d, is dropped, data:%p", pTrans->id, pTrans);
S
Shengliang Guan 已提交
416 417 418
  tfree(pTrans);
}

S
Shengliang Guan 已提交
419
static void mndTransSetRpcHandle(STrans *pTrans, void *rpcHandle) {
S
Shengliang Guan 已提交
420
  pTrans->rpcHandle = rpcHandle;
S
Shengliang Guan 已提交
421
  mTrace("trans:%d, set rpc handle:%p", pTrans->id, rpcHandle);
S
Shengliang Guan 已提交
422 423
}

S
Shengliang Guan 已提交
424
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
425 426 427 428 429
  if (pArray == NULL || pRaw == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
430
  void *ptr = taosArrayPush(pArray, &pRaw);
S
Shengliang Guan 已提交
431 432 433 434 435 436 437 438
  if (ptr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
439
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
440
  int32_t code = mndTransAppendLog(pTrans->redoLogs, pRaw);
S
Shengliang Guan 已提交
441
  mTrace("trans:%d, raw:%p append to redo logs, code:0x%x", pTrans->id, pRaw, code);
S
Shengliang Guan 已提交
442 443 444
  return code;
}

S
Shengliang Guan 已提交
445
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
446
  int32_t code = mndTransAppendLog(pTrans->undoLogs, pRaw);
S
Shengliang Guan 已提交
447
  mTrace("trans:%d, raw:%p append to undo logs, code:0x%x", pTrans->id, pRaw, code);
S
Shengliang Guan 已提交
448 449 450
  return code;
}

S
Shengliang Guan 已提交
451
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
452
  int32_t code = mndTransAppendLog(pTrans->commitLogs, pRaw);
S
Shengliang Guan 已提交
453
  mTrace("trans:%d, raw:%p append to commit logs, code:0x%x", pTrans->id, pRaw, code);
S
Shengliang Guan 已提交
454 455 456
  return code;
}

S
Shengliang Guan 已提交
457
static int32_t mndTransAppendAction(SArray *pArray, STransAction *pAction) {
458
  void *ptr = taosArrayPush(pArray, pAction);
S
Shengliang Guan 已提交
459 460 461 462 463 464 465 466
  if (ptr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
467
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
468 469 470
  int32_t code = mndTransAppendAction(pTrans->redoActions, pAction);
  mTrace("trans:%d, msg:%s append to redo actions, code:0x%x", pTrans->id, taosMsg[pAction->msgType], code);
  return code;
S
Shengliang Guan 已提交
471 472
}

S
Shengliang Guan 已提交
473
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
474 475 476
  int32_t code = mndTransAppendAction(pTrans->undoActions, pAction);
  mTrace("trans:%d, msg:%s append to undo actions, code:0x%x", pTrans->id, taosMsg[pAction->msgType], code);
  return code;
S
Shengliang Guan 已提交
477 478
}

S
Shengliang Guan 已提交
479
int32_t mndTransPrepare(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
480
  mDebug("trans:%d, prepare transaction", pTrans->id);
S
Shengliang Guan 已提交
481

S
Shengliang Guan 已提交
482
  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
S
Shengliang Guan 已提交
483
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
484
    mError("trans:%d, failed to decode trans since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
485 486
    return -1;
  }
S
Shengliang Guan 已提交
487
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
488

489
  mTrace("trans:%d, sync to other nodes", pTrans->id);
S
Shengliang Guan 已提交
490 491 492 493
  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 已提交
494 495 496
    return -1;
  }

S
Shengliang Guan 已提交
497
  mTrace("trans:%d, sync finished", pTrans->id);
S
Shengliang Guan 已提交
498

S
Shengliang Guan 已提交
499 500 501 502 503 504 505 506 507 508 509 510
  code = sdbWrite(pMnode->pSdb, pRaw);
  if (code != 0) {
    mError("trans:%d, failed to write sdb since %s", pTrans->id, terrstr());
    return -1;
  }

  STrans *pNewTrans = mndAcquireTrans(pMnode, pTrans->id);
  if (pNewTrans == NULL) {
    mError("trans:%d, failed to ready from sdb since %s", pTrans->id, terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
511
  pNewTrans->rpcHandle = pTrans->rpcHandle;
S
Shengliang Guan 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
  mndTransExecute(pMnode, pNewTrans);
  mndReleaseTrans(pMnode, pNewTrans);
  return 0;
}

int32_t mndTransCommit(SMnode *pMnode, STrans *pTrans) {
  mDebug("trans:%d, commit transaction", pTrans->id);

  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
  if (pRaw == NULL) {
    mError("trans:%d, failed to decode trans since %s", pTrans->id, terrstr());
    return -1;
  }
  sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);

S
Shengliang Guan 已提交
527
  if (taosArrayGetSize(pTrans->commitLogs) != 0) {
528
    mTrace("trans:%d, sync to other nodes", pTrans->id);
S
Shengliang Guan 已提交
529 530 531 532 533 534
    int32_t code = mndSyncPropose(pMnode, pRaw);
    if (code != 0) {
      mError("trans:%d, failed to sync since %s", pTrans->id, terrstr());
      sdbFreeRaw(pRaw);
      return -1;
    }
S
Shengliang Guan 已提交
535

S
Shengliang Guan 已提交
536 537 538 539 540 541
    mTrace("trans:%d, sync finished", pTrans->id);
    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 已提交
542 543 544
  }

  mDebug("trans:%d, commit finished", pTrans->id);
S
Shengliang Guan 已提交
545 546 547
  return 0;
}

S
Shengliang Guan 已提交
548 549 550 551 552 553 554 555 556 557
int32_t mndTransRollback(SMnode *pMnode, STrans *pTrans) {
  mDebug("trans:%d, rollback transaction", pTrans->id);

  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
  if (pRaw == NULL) {
    mError("trans:%d, failed to decode trans since %s", pTrans->id, terrstr());
    return -1;
  }
  sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);

558
  mTrace("trans:%d, sync to other nodes", pTrans->id);
S
Shengliang Guan 已提交
559 560 561 562 563 564 565 566 567 568 569 570
  int32_t code = mndSyncPropose(pMnode, pRaw);
  if (code != 0) {
    mError("trans:%d, failed to sync since %s", pTrans->id, terrstr());
    sdbFreeRaw(pRaw);
    return -1;
  }

  mTrace("trans:%d, sync finished", pTrans->id);
  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 已提交
571 572
  }

S
Shengliang Guan 已提交
573 574
  mDebug("trans:%d, rollback finished", pTrans->id);
  return 0;
S
Shengliang Guan 已提交
575
}
S
Shengliang Guan 已提交
576

S
Shengliang Guan 已提交
577 578 579 580 581 582 583
static void mndTransSendRpcRsp(STrans *pTrans, int32_t code) {
  if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) return;
  mDebug("trans:%d, send rpc rsp, RPC:%p code:0x%x", pTrans->id, pTrans->rpcHandle, code & 0xFFFF);

  if (pTrans->rpcHandle != NULL) {
    SRpcMsg rspMsg = {.handle = pTrans->rpcHandle, .code = code};
    rpcSendResponse(&rspMsg);
S
Shengliang Guan 已提交
584 585 586
  }
}

S
Shengliang Guan 已提交
587 588 589 590
void mndTransApply(SMnode *pMnode, SSdbRaw *pRaw, STransMsg *pMsg, int32_t code) {
  // todo
}

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
void mndTransHandleActionRsp(SMnodeMsg *pMsg) {
  SMnode *pMnode = pMsg->pMnode;
  int64_t sig = (int64_t)(pMsg->rpcMsg.ahandle);
  int32_t transId = (int32_t)(sig >> 32);
  int32_t action = (int32_t)((sig << 32) >> 32);

  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;
  if (pTrans->stage == TRN_STAGE_EXECUTE) {
    pArray = pTrans->redoActions;
  } else if (pTrans->stage == TRN_STAGE_ROLLBACK) {
    pArray = pTrans->undoActions;
  } else {
  }

  if (pArray == NULL) {
    mError("trans:%d, invalid trans stage:%s", transId, mndTransStageStr(pTrans->stage));
    goto HANDLE_ACTION_RSP_OVER;
  }

  int32_t actionNum = taosArrayGetSize(pTrans->redoActions);
  if (action < 0 || action > actionNum) {
    mError("trans:%d, invalid action:%d", transId, action);
    goto HANDLE_ACTION_RSP_OVER;
  }

  STransAction *pAction = taosArrayGet(pArray, action);
  if (pAction != NULL) {
    pAction->msgReceived = 1;
    pAction->errCode = pMsg->code;
  }

  mDebug("trans:%d, action:%d response is received, code:0x%x", transId, action, pMsg->code);
  mndTransExecute(pMnode, pTrans);

HANDLE_ACTION_RSP_OVER:
  mndReleaseTrans(pMnode, pTrans);
}

S
Shengliang Guan 已提交
635
static int32_t mndTransExecuteLogs(SMnode *pMnode, SArray *pArray) {
S
Shengliang Guan 已提交
636 637 638 639
  SSdb   *pSdb = pMnode->pSdb;
  int32_t arraySize = taosArrayGetSize(pArray);

  for (int32_t i = 0; i < arraySize; ++i) {
640
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
641 642 643
    int32_t  code = sdbWriteNotFree(pSdb, pRaw);
    if (code != 0) {
      return code;
S
Shengliang Guan 已提交
644 645 646 647 648 649
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
650
static int32_t mndTransExecuteRedoLogs(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
651 652
  int32_t code = 0;
  if (taosArrayGetSize(pTrans->redoLogs) != 0) {
S
Shengliang Guan 已提交
653
    code = mndTransExecuteLogs(pMnode, pTrans->redoLogs);
S
Shengliang Guan 已提交
654 655 656
    if (code != 0) {
      mError("trans:%d, failed to execute redo logs since %s", pTrans->id, terrstr())
    } else {
657
      mDebug("trans:%d, execute redo logs finished", pTrans->id)
S
Shengliang Guan 已提交
658
    }
S
Shengliang Guan 已提交
659 660 661 662 663 664
  }

  return code;
}

static int32_t mndTransExecuteUndoLogs(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
665 666
  int32_t code = 0;
  if (taosArrayGetSize(pTrans->undoLogs) != 0) {
S
Shengliang Guan 已提交
667
    code = mndTransExecuteLogs(pMnode, pTrans->undoLogs);
S
Shengliang Guan 已提交
668 669 670
    if (code != 0) {
      mError("trans:%d, failed to execute undo logs since %s", pTrans->id, terrstr())
    } else {
671
      mDebug("trans:%d, execute undo logs finished", pTrans->id)
S
Shengliang Guan 已提交
672
    }
S
Shengliang Guan 已提交
673 674 675 676 677 678
  }

  return code;
}

static int32_t mndTransExecuteCommitLogs(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
679 680
  int32_t code = 0;
  if (taosArrayGetSize(pTrans->commitLogs) != 0) {
S
Shengliang Guan 已提交
681
    code = mndTransExecuteLogs(pMnode, pTrans->commitLogs);
S
Shengliang Guan 已提交
682 683 684
    if (code != 0) {
      mError("trans:%d, failed to execute commit logs since %s", pTrans->id, terrstr())
    } else {
685
      mDebug("trans:%d, execute commit logs finished", pTrans->id)
S
Shengliang Guan 已提交
686
    }
S
Shengliang Guan 已提交
687
  }
S
Shengliang Guan 已提交
688

S
Shengliang Guan 已提交
689 690
  return code;
}
S
Shengliang Guan 已提交
691

692 693 694 695 696 697 698 699
static int32_t mndTransExecuteActions(SMnode *pMnode, STrans *pTrans, SArray *pArray) {
  int32_t numOfActions = taosArrayGetSize(pArray);
  if (numOfActions == 0) return 0;

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

701 702 703 704 705
    int64_t signature = pTrans->id;
    signature = (signature << 32);
    signature += action;

    SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .ahandle = (void *)signature};
S
Shengliang Guan 已提交
706 707 708 709
    rpcMsg.pCont = rpcMallocCont(pAction->contLen);
    if (rpcMsg.pCont == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
S
Shengliang Guan 已提交
710
    }
S
Shengliang Guan 已提交
711
    memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
712 713 714 715 716 717

    pAction->msgSent = 1;
    pAction->msgReceived = 0;
    pAction->errCode = 0;

    mDebug("trans:%d, action:%d is sent", pTrans->id, action);
S
Shengliang Guan 已提交
718
    mndSendMsgToDnode(pMnode, &pAction->epSet, &rpcMsg);
S
Shengliang Guan 已提交
719 720
  }

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
  int32_t numOfReceivedMsgs = 0;
  int32_t errorCode = 0;
  for (int32_t action = 0; action < numOfActions; ++action) {
    STransAction *pAction = taosArrayGet(pArray, action);
    if (pAction == NULL) continue;
    if (pAction->msgSent && pAction->msgReceived) {
      numOfReceivedMsgs++;
      if (pAction->errCode != 0) {
        errorCode = pAction->errCode;
      }
    }
  }

  if (numOfReceivedMsgs == numOfActions) {
    mDebug("trans:%d, all %d actions executed, code:0x%x", pTrans->id, numOfActions, errorCode);
    terrno = errorCode;
    return errorCode;
  } else {
    return TSDB_CODE_MND_ACTION_IN_PROGRESS;
  }
S
Shengliang Guan 已提交
741 742
}

S
Shengliang Guan 已提交
743
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans) {
744
  return mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions);
S
Shengliang Guan 已提交
745
}
S
Shengliang Guan 已提交
746

S
Shengliang Guan 已提交
747
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans) {
748
  return mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions);
S
Shengliang Guan 已提交
749
}
S
Shengliang Guan 已提交
750

S
Shengliang Guan 已提交
751 752
static int32_t mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans) {
  int32_t code = mndTransExecuteRedoLogs(pMnode, pTrans);
S
Shengliang Guan 已提交
753

S
Shengliang Guan 已提交
754
  if (code == 0) {
S
Shengliang Guan 已提交
755
    pTrans->stage = TRN_STAGE_EXECUTE;
756
    mDebug("trans:%d, stage from prepare to execute", pTrans->id);
S
Shengliang Guan 已提交
757 758
  } else {
    pTrans->stage = TRN_STAGE_ROLLBACK;
S
Shengliang Guan 已提交
759
    mError("trans:%d, stage from prepare to rollback since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
760
  }
S
Shengliang Guan 已提交
761 762

  return 0;
S
Shengliang Guan 已提交
763 764
}

S
Shengliang Guan 已提交
765 766
static int32_t mndTransPerformExecuteStage(SMnode *pMnode, STrans *pTrans) {
  int32_t code = mndTransExecuteRedoActions(pMnode, pTrans);
S
Shengliang Guan 已提交
767 768 769

  if (code == 0) {
    pTrans->stage = TRN_STAGE_COMMIT;
770
    mDebug("trans:%d, stage from execute to commit", pTrans->id);
S
Shengliang Guan 已提交
771
  } else if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) {
772
    mDebug("trans:%d, stage keep on execute since %s", pTrans->id, tstrerror(code));
S
Shengliang Guan 已提交
773
    return code;
S
Shengliang Guan 已提交
774
  } else {
S
Shengliang Guan 已提交
775
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
S
Shengliang Guan 已提交
776
      pTrans->stage = TRN_STAGE_ROLLBACK;
S
Shengliang Guan 已提交
777 778
      mError("trans:%d, stage from execute to rollback since %s", pTrans->id, terrstr());
    } else {
779 780
      pTrans->stage = TRN_STAGE_EXECUTE;
      mError("trans:%d, stage keep on execute since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
781 782 783
    }
  }

S
Shengliang Guan 已提交
784
  return 0;
S
Shengliang Guan 已提交
785 786
}

S
Shengliang Guan 已提交
787
static int32_t mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans) {
788 789 790
  mndTransExecuteCommitLogs(pMnode, pTrans);
  pTrans->stage = TRN_STAGE_OVER;
  return 0;
S
Shengliang Guan 已提交
791 792
}

S
Shengliang Guan 已提交
793 794 795 796
static int32_t mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans) {
  int32_t code = mndTransExecuteUndoActions(pMnode, pTrans);

  if (code == 0) {
797
    mDebug("trans:%d, rollbacked", pTrans->id);
S
Shengliang Guan 已提交
798 799
  } else {
    pTrans->stage = TRN_STAGE_ROLLBACK;
S
Shengliang Guan 已提交
800
    mError("trans:%d, stage keep on rollback since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
801 802
  }

S
Shengliang Guan 已提交
803 804
  return code;
}
S
Shengliang Guan 已提交
805

S
Shengliang Guan 已提交
806 807
static void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
  int32_t code = 0;
S
Shengliang Guan 已提交
808

S
Shengliang Guan 已提交
809 810 811 812 813 814 815 816 817 818 819
  while (code == 0) {
    switch (pTrans->stage) {
      case TRN_STAGE_PREPARE:
        code = mndTransPerformPrepareStage(pMnode, pTrans);
        break;
      case TRN_STAGE_EXECUTE:
        code = mndTransPerformExecuteStage(pMnode, pTrans);
        break;
      case TRN_STAGE_COMMIT:
        code = mndTransCommit(pMnode, pTrans);
        if (code == 0) {
820
          mndTransPerformCommitStage(pMnode, pTrans);
S
Shengliang Guan 已提交
821 822 823 824 825 826 827 828
        }
        break;
      case TRN_STAGE_ROLLBACK:
        code = mndTransPerformRollbackStage(pMnode, pTrans);
        if (code == 0) {
          code = mndTransRollback(pMnode, pTrans);
        }
        break;
S
Shengliang Guan 已提交
829 830 831
      default:
        mndTransSendRpcRsp(pTrans, 0);
        return;
S
Shengliang Guan 已提交
832 833 834
    }
  }

S
Shengliang Guan 已提交
835 836
  mndTransSendRpcRsp(pTrans, code);
}