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

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndTrans.h"
S
Shengliang Guan 已提交
18 19 20 21 22
#include "trpc.h"

#define SDB_TRANS_VER 1
#define TRN_DEFAULT_ARRAY_SIZE 8

S
Shengliang Guan 已提交
23
SSdbRaw *mndTransActionEncode(STrans *pTrans) {
S
Shengliang Guan 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  int32_t rawDataLen = 10 * sizeof(int32_t);
  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);

  for (int32_t index = 0; index < redoLogNum; ++index) {
    SSdbRaw *pTmp = taosArrayGet(pTrans->redoLogs, index);
    rawDataLen += sdbGetRawTotalSize(pTmp);
  }

  for (int32_t index = 0; index < undoLogNum; ++index) {
    SSdbRaw *pTmp = taosArrayGet(pTrans->undoLogs, index);
    rawDataLen += sdbGetRawTotalSize(pTmp);
  }

  for (int32_t index = 0; index < commitLogNum; ++index) {
    SSdbRaw *pTmp = taosArrayGet(pTrans->commitLogs, index);
    rawDataLen += sdbGetRawTotalSize(pTmp);
  }

  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, SDB_TRANS_VER, rawDataLen);
  if (pRaw == NULL) {
    mError("trn:%d, failed to alloc raw since %s", pTrans->id, terrstr());
    return NULL;
  }

  int32_t dataPos = 0;
  SDB_SET_INT32(pRaw, dataPos, pTrans->id)
  SDB_SET_INT8(pRaw, dataPos, pTrans->stage)
  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)

  for (int32_t index = 0; index < redoLogNum; ++index) {
    SSdbRaw *pTmp = taosArrayGet(pTrans->redoLogs, index);
    int32_t  len = sdbGetRawTotalSize(pTmp);
    SDB_SET_INT32(pRaw, dataPos, len)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len)
  }

  for (int32_t index = 0; index < undoLogNum; ++index) {
    SSdbRaw *pTmp = taosArrayGet(pTrans->undoLogs, index);
    int32_t  len = sdbGetRawTotalSize(pTmp);
    SDB_SET_INT32(pRaw, dataPos, len)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len)
  }

  for (int32_t index = 0; index < commitLogNum; ++index) {
    SSdbRaw *pTmp = taosArrayGet(pTrans->commitLogs, index);
    int32_t  len = sdbGetRawTotalSize(pTmp);
    SDB_SET_INT32(pRaw, dataPos, len)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len)
  }

  mDebug("trn:%d, is encoded as raw:%p, len:%d", pTrans->id, pRaw, dataPos);
  return pRaw;
}

S
Shengliang Guan 已提交
87
SSdbRow *mndTransActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
    mError("failed to get soft ver from raw:%p since %s", pRaw, terrstr());
    return NULL;
  }

  if (sver != SDB_TRANS_VER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    mError("failed to get check soft ver from raw:%p since %s", pRaw, terrstr());
    return NULL;
  }

  SSdbRow   *pRow = sdbAllocRow(sizeof(STrans));
  STrans *pTrans = sdbGetRowObj(pRow);
  if (pTrans == NULL) {
    mError("failed to alloc trans from raw:%p since %s", pRaw, terrstr());
    return NULL;
  }

  pTrans->redoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
  pTrans->undoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
  pTrans->commitLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
  pTrans->redoActions = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
  pTrans->undoActions = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));

  if (pTrans->redoLogs == NULL || pTrans->undoLogs == NULL || pTrans->commitLogs == NULL ||
      pTrans->redoActions == NULL || pTrans->undoActions == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mDebug("trn:%d, failed to create array while parsed from raw:%p", pTrans->id, pRaw);
    return NULL;
  }

  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->stage)
  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)

  int32_t code = 0;
  for (int32_t index = 0; index < redoLogNum; ++index) {
    int32_t dataLen = 0;
    SDB_GET_INT32(pRaw, pRow, dataPos, &dataLen)

    char *pData = malloc(dataLen);
    SDB_GET_BINARY(pRaw, pRow, dataPos, pData, dataLen);
    void *ret = taosArrayPush(pTrans->redoLogs, pData);
    if (ret == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      break;
    }
  }

  if (code != 0) {
    terrno = code;
    mError("trn:%d, failed to parse from raw:%p since %s", pTrans->id, pRaw, terrstr());
S
Shengliang Guan 已提交
153
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
154 155 156 157 158 159 160
    return NULL;
  }

  mDebug("trn:%d, is parsed from raw:%p", pTrans->id, pRaw);
  return pRow;
}

S
Shengliang Guan 已提交
161
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
S
Shengliang Guan 已提交
162 163 164 165 166
  SArray *pArray = pTrans->redoLogs;
  int32_t arraySize = taosArrayGetSize(pArray);

  for (int32_t index = 0; index < arraySize; ++index) {
    SSdbRaw *pRaw = taosArrayGet(pArray, index);
S
Shengliang Guan 已提交
167
    int32_t  code = sdbWrite(pSdb, pRaw);
S
Shengliang Guan 已提交
168 169 170 171 172 173 174 175 176 177
    if (code != 0) {
      mError("trn:%d, failed to write raw:%p to sdb since %s", pTrans->id, pRaw, terrstr());
      return code;
    }
  }

  mDebug("trn:%d, write to sdb", pTrans->id);
  return 0;
}

S
Shengliang Guan 已提交
178
static int32_t mndTransActionDelete(SSdb *pSdb, STrans *pTrans) {
S
Shengliang Guan 已提交
179 180 181 182 183
  SArray *pArray = pTrans->redoLogs;
  int32_t arraySize = taosArrayGetSize(pArray);

  for (int32_t index = 0; index < arraySize; ++index) {
    SSdbRaw *pRaw = taosArrayGet(pArray, index);
S
Shengliang Guan 已提交
184
    int32_t  code = sdbWrite(pSdb, pRaw);
S
Shengliang Guan 已提交
185 186 187 188 189 190 191 192 193 194
    if (code != 0) {
      mError("trn:%d, failed to write raw:%p to sdb since %s", pTrans->id, pRaw, terrstr());
      return code;
    }
  }

  mDebug("trn:%d, delete from sdb", pTrans->id);
  return 0;
}

S
Shengliang Guan 已提交
195
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pTrans, STrans *pDstTrans) {
S
Shengliang Guan 已提交
196 197 198 199 200 201
  assert(true);
  SArray *pArray = pTrans->redoLogs;
  int32_t arraySize = taosArrayGetSize(pArray);

  for (int32_t index = 0; index < arraySize; ++index) {
    SSdbRaw *pRaw = taosArrayGet(pArray, index);
S
Shengliang Guan 已提交
202
    int32_t  code = sdbWrite(pSdb, pRaw);
S
Shengliang Guan 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215
    if (code != 0) {
      mError("trn:%d, failed to write raw:%p to sdb since %s", pTrans->id, pRaw, terrstr());
      return code;
    }
  }

  pTrans->stage = pDstTrans->stage;
  mDebug("trn:%d, update in sdb", pTrans->id);
  return 0;
}

static int32_t trnGenerateTransId() { return 1; }

S
Shengliang Guan 已提交
216
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, void *rpcHandle) {
S
Shengliang Guan 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  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;
  }

  pTrans->id = trnGenerateTransId();
  pTrans->stage = TRN_STAGE_PREPARE;
  pTrans->policy = policy;
  pTrans->rpcHandle = rpcHandle;
  pTrans->redoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
  pTrans->undoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
  pTrans->commitLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
  pTrans->redoActions = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));
  pTrans->undoActions = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *));

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

  mDebug("trn:%d, is created, %p", pTrans->id, pTrans);
  return pTrans;
}

static void trnDropArray(SArray *pArray) {
  for (int32_t index = 0; index < pArray->size; ++index) {
    SSdbRaw *pRaw = taosArrayGet(pArray, index);
    tfree(pRaw);
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
254
void mndTransDrop(STrans *pTrans) {
S
Shengliang Guan 已提交
255 256 257 258 259 260 261 262 263 264
  trnDropArray(pTrans->redoLogs);
  trnDropArray(pTrans->undoLogs);
  trnDropArray(pTrans->commitLogs);
  trnDropArray(pTrans->redoActions);
  trnDropArray(pTrans->undoActions);

  mDebug("trn:%d, is dropped, %p", pTrans->id, pTrans);
  tfree(pTrans);
}

S
Shengliang Guan 已提交
265
void mndTransSetRpcHandle(STrans *pTrans, void *rpcHandle) {
S
Shengliang Guan 已提交
266 267 268 269
  pTrans->rpcHandle = rpcHandle;
  mTrace("trn:%d, set rpc handle:%p", pTrans->id, rpcHandle);
}

S
Shengliang Guan 已提交
270
static int32_t mndTransAppendArray(SArray *pArray, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284
  if (pArray == NULL || pRaw == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  void *ptr = taosArrayPush(pArray, pRaw);
  if (ptr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
285 286
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
  int32_t code = mndTransAppendArray(pTrans->redoLogs, pRaw);
S
Shengliang Guan 已提交
287 288 289 290
  mTrace("trn:%d, raw:%p append to redo logs, code:%d", pTrans->id, pRaw, code);
  return code;
}

S
Shengliang Guan 已提交
291 292
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
  int32_t code = mndTransAppendArray(pTrans->undoLogs, pRaw);
S
Shengliang Guan 已提交
293 294 295 296
  mTrace("trn:%d, raw:%p append to undo logs, code:%d", pTrans->id, pRaw, code);
  return code;
}

S
Shengliang Guan 已提交
297 298
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
  int32_t code = mndTransAppendArray(pTrans->commitLogs, pRaw);
S
Shengliang Guan 已提交
299 300 301 302
  mTrace("trn:%d, raw:%p append to commit logs, code:%d", pTrans->id, pRaw, code);
  return code;
}

S
Shengliang Guan 已提交
303 304
int32_t mndTransAppendRedoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {
  int32_t code = mndTransAppendArray(pTrans->redoActions, pMsg);
S
Shengliang Guan 已提交
305 306 307 308
  mTrace("trn:%d, msg:%p append to redo actions", pTrans->id, pMsg);
  return code;
}

S
Shengliang Guan 已提交
309 310
int32_t mndTransAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {
  int32_t code = mndTransAppendArray(pTrans->undoActions, pMsg);
S
Shengliang Guan 已提交
311 312 313 314
  mTrace("trn:%d, msg:%p append to undo actions", pTrans->id, pMsg);
  return code;
}

S
Shengliang Guan 已提交
315
int32_t mndInitTrans(SMnode *pMnode) {
S
Shengliang Guan 已提交
316 317
  SSdbTable table = {.sdbType = SDB_TRANS,
                     .keyType = SDB_KEY_INT32,
S
Shengliang Guan 已提交
318 319 320 321 322
                     .encodeFp = (SdbEncodeFp)mndTransActionEncode,
                     .decodeFp = (SdbDecodeFp)mndTransActionDecode,
                     .insertFp = (SdbInsertFp)mndTransActionInsert,
                     .updateFp = (SdbUpdateFp)mndTransActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndTransActionDelete};
S
Shengliang Guan 已提交
323

S
Shengliang Guan 已提交
324
  return sdbSetTable(pMnode->pSdb, table);
S
Shengliang Guan 已提交
325 326
}

S
Shengliang Guan 已提交
327
void mndCleanupTrans(SMnode *pMnode) {}
S
Shengliang Guan 已提交
328

S
Shengliang Guan 已提交
329
int32_t mndTransPrepare(STrans *pTrans, int32_t (*syncfp)(SSdbRaw *pRaw, void *pData)) {
S
Shengliang Guan 已提交
330 331
  if (syncfp == NULL) return -1;

S
Shengliang Guan 已提交
332
  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
S
Shengliang Guan 已提交
333 334 335 336 337 338
  if (pRaw == NULL) {
    mError("trn:%d, failed to decode trans since %s", pTrans->id, terrstr());
    return -1;
  }
  sdbSetRawStatus(pRaw, SDB_STATUS_CREATING);

S
Shengliang Guan 已提交
339
  if (sdbWrite(pTrans->pMnode->pSdb, pRaw) != 0) {
S
Shengliang Guan 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    mError("trn:%d, failed to write trans since %s", pTrans->id, terrstr());
    return -1;
  }

  if ((*syncfp)(pRaw, pTrans->rpcHandle) != 0) {
    mError("trn:%d, failed to sync trans since %s", pTrans->id, terrstr());
    return -1;
  }

  return 0;
}

static void trnSendRpcRsp(void *rpcHandle, int32_t code) {
  if (rpcHandle != NULL) {
    SRpcMsg rspMsg = {.handle = rpcHandle, .code = terrno};
    rpcSendResponse(&rspMsg);
  }
}

S
Shengliang Guan 已提交
359
int32_t mndTransApply(SMnode *pMnode, SSdbRaw *pRaw, void *pData, int32_t code) {
S
Shengliang Guan 已提交
360 361 362 363 364
  if (code != 0) {
    trnSendRpcRsp(pData, terrno);
    return 0;
  }

S
Shengliang Guan 已提交
365
  if (sdbWrite(pMnode->pSdb, pData) != 0) {
S
Shengliang Guan 已提交
366 367 368 369 370 371 372 373 374
    code = terrno;
    trnSendRpcRsp(pData, code);
    terrno = code;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
375
static int32_t trnExecuteArray(SMnode *pMnode, SArray *pArray) {
S
Shengliang Guan 已提交
376 377
  for (int32_t index = 0; index < pArray->size; ++index) {
    SSdbRaw *pRaw = taosArrayGetP(pArray, index);
S
Shengliang Guan 已提交
378
    if (sdbWrite(pMnode->pSdb, pRaw) != 0) {
S
Shengliang Guan 已提交
379 380 381 382 383 384 385
      return -1;
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
386
static int32_t trnExecuteRedoLogs(STrans *pTrans) { return trnExecuteArray(pTrans->pMnode, pTrans->redoLogs); }
S
Shengliang Guan 已提交
387

S
Shengliang Guan 已提交
388
static int32_t trnExecuteUndoLogs(STrans *pTrans) { return trnExecuteArray(pTrans->pMnode, pTrans->undoLogs); }
S
Shengliang Guan 已提交
389

S
Shengliang Guan 已提交
390
static int32_t trnExecuteCommitLogs(STrans *pTrans) { return trnExecuteArray(pTrans->pMnode, pTrans->commitLogs); }
S
Shengliang Guan 已提交
391

S
Shengliang Guan 已提交
392
static int32_t trnExecuteRedoActions(STrans *pTrans) { return trnExecuteArray(pTrans->pMnode, pTrans->redoActions); }
S
Shengliang Guan 已提交
393

S
Shengliang Guan 已提交
394
static int32_t trnExecuteUndoActions(STrans *pTrans) { return trnExecuteArray(pTrans->pMnode, pTrans->undoActions); }
S
Shengliang Guan 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

static int32_t trnPerformPrepareStage(STrans *pTrans) {
  if (trnExecuteRedoLogs(pTrans) == 0) {
    pTrans->stage = TRN_STAGE_EXECUTE;
    return 0;
  } else {
    pTrans->stage = TRN_STAGE_ROLLBACK;
    return -1;
  }
}

static int32_t trnPerformExecuteStage(STrans *pTrans) {
  int32_t code = trnExecuteRedoActions(pTrans);

  if (code == 0) {
    pTrans->stage = TRN_STAGE_COMMIT;
    return 0;
  } else if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) {
    return -1;
  } else {
    if (pTrans->policy == TRN_POLICY_RETRY) {
      pTrans->stage = TRN_STAGE_RETRY;
    } else {
      pTrans->stage = TRN_STAGE_ROLLBACK;
    }
    return 0;
  }
}

static int32_t trnPerformCommitStage(STrans *pTrans) {
  if (trnExecuteCommitLogs(pTrans) == 0) {
    pTrans->stage = TRN_STAGE_EXECUTE;
    return 0;
  } else {
    pTrans->stage = TRN_STAGE_ROLLBACK;
    return -1;
  }
}

static int32_t trnPerformRollbackStage(STrans *pTrans) {
  if (trnExecuteCommitLogs(pTrans) == 0) {
    pTrans->stage = TRN_STAGE_EXECUTE;
    return 0;
  } else {
    pTrans->stage = TRN_STAGE_ROLLBACK;
    return -1;
  }
}

static int32_t trnPerformRetryStage(STrans *pTrans) {
  if (trnExecuteCommitLogs(pTrans) == 0) {
    pTrans->stage = TRN_STAGE_EXECUTE;
    return 0;
  } else {
    pTrans->stage = TRN_STAGE_ROLLBACK;
    return -1;
  }
}

S
Shengliang Guan 已提交
454
int32_t mndTransExecute(SSdb *pSdb, int32_t tranId) {
S
Shengliang Guan 已提交
455 456
  int32_t code = 0;

S
Shengliang Guan 已提交
457
  STrans *pTrans = sdbAcquire(pSdb, SDB_TRANS, &tranId);
S
Shengliang Guan 已提交
458 459 460 461 462 463
  if (pTrans == NULL) {
    return -1;
  }

  if (pTrans->stage == TRN_STAGE_PREPARE) {
    if (trnPerformPrepareStage(pTrans) != 0) {
S
Shengliang Guan 已提交
464
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
465 466 467 468 469 470
      return -1;
    }
  }

  if (pTrans->stage == TRN_STAGE_EXECUTE) {
    if (trnPerformExecuteStage(pTrans) != 0) {
S
Shengliang Guan 已提交
471
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
472 473 474 475 476 477
      return -1;
    }
  }

  if (pTrans->stage == TRN_STAGE_COMMIT) {
    if (trnPerformCommitStage(pTrans) != 0) {
S
Shengliang Guan 已提交
478
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
479 480 481 482 483 484
      return -1;
    }
  }

  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
    if (trnPerformRollbackStage(pTrans) != 0) {
S
Shengliang Guan 已提交
485
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
486 487 488 489 490 491
      return -1;
    }
  }

  if (pTrans->stage == TRN_STAGE_RETRY) {
    if (trnPerformRetryStage(pTrans) != 0) {
S
Shengliang Guan 已提交
492
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
493 494 495 496
      return -1;
    }
  }

S
Shengliang Guan 已提交
497
  sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
498 499
  return 0;
}