mndTrans.c 16.9 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 20 21 22

#define SDB_TRANS_VER 1
#define TRN_DEFAULT_ARRAY_SIZE 8

S
Shengliang Guan 已提交
23 24 25
static SSdbRaw *mndTransActionEncode(STrans *pTrans);
static SSdbRow *mndTransActionDecode(SSdbRaw *pRaw);
static int32_t  mndTransActionInsert(SSdb *pSdb, STrans *pTrans);
S
Shengliang Guan 已提交
26
static int32_t  mndTransActionUpdate(SSdb *pSdb, STrans *OldTrans, STrans *pOldTrans);
S
Shengliang Guan 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
static int32_t  mndTransActionDelete(SSdb *pSdb, STrans *pTrans);

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) {
  int32_t rawDataLen = 16 * sizeof(int32_t);
S
Shengliang Guan 已提交
45 46 47 48 49 50
  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);

51
  for (int32_t i = 0; i < redoLogNum; ++i) {
S
Shengliang Guan 已提交
52
    SSdbRaw *pTmp = taosArrayGetP(pTrans->redoLogs, i);
S
Shengliang Guan 已提交
53 54 55
    rawDataLen += sdbGetRawTotalSize(pTmp);
  }

56
  for (int32_t i = 0; i < undoLogNum; ++i) {
S
Shengliang Guan 已提交
57
    SSdbRaw *pTmp = taosArrayGetP(pTrans->undoLogs, i);
S
Shengliang Guan 已提交
58 59 60
    rawDataLen += sdbGetRawTotalSize(pTmp);
  }

61
  for (int32_t i = 0; i < commitLogNum; ++i) {
S
Shengliang Guan 已提交
62
    SSdbRaw *pTmp = taosArrayGetP(pTrans->commitLogs, i);
S
Shengliang Guan 已提交
63 64 65 66 67
    rawDataLen += sdbGetRawTotalSize(pTmp);
  }

  SSdbRaw *pRaw = sdbAllocRaw(SDB_TRANS, SDB_TRANS_VER, rawDataLen);
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
68
    mError("trans:%d, failed to alloc raw since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
    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)

82
  for (int32_t i = 0; i < redoLogNum; ++i) {
S
Shengliang Guan 已提交
83
    SSdbRaw *pTmp = taosArrayGetP(pTrans->redoLogs, i);
S
Shengliang Guan 已提交
84 85 86 87 88
    int32_t  len = sdbGetRawTotalSize(pTmp);
    SDB_SET_INT32(pRaw, dataPos, len)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len)
  }

89
  for (int32_t i = 0; i < undoLogNum; ++i) {
S
Shengliang Guan 已提交
90
    SSdbRaw *pTmp = taosArrayGetP(pTrans->undoLogs, i);
S
Shengliang Guan 已提交
91 92 93 94 95
    int32_t  len = sdbGetRawTotalSize(pTmp);
    SDB_SET_INT32(pRaw, dataPos, len)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len)
  }

96
  for (int32_t i = 0; i < commitLogNum; ++i) {
S
Shengliang Guan 已提交
97
    SSdbRaw *pTmp = taosArrayGetP(pTrans->commitLogs, i);
S
Shengliang Guan 已提交
98 99 100 101 102
    int32_t  len = sdbGetRawTotalSize(pTmp);
    SDB_SET_INT32(pRaw, dataPos, len)
    SDB_SET_BINARY(pRaw, dataPos, (void *)pTmp, len)
  }

S
Shengliang Guan 已提交
103
  mTrace("trans:%d, encode to raw:%p, len:%d", pTrans->id, pRaw, dataPos);
S
Shengliang Guan 已提交
104 105 106
  return pRaw;
}

S
Shengliang Guan 已提交
107 108 109
static SSdbRow *mndTransActionDecode(SSdbRaw *pRaw) {
  int32_t code = 0;

S
Shengliang Guan 已提交
110 111 112 113 114 115 116 117 118 119 120 121
  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;
  }

S
Shengliang Guan 已提交
122 123
  SSdbRow *pRow = sdbAllocRow(sizeof(STrans));
  STrans  *pTrans = sdbGetRowObj(pRow);
S
Shengliang Guan 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136
  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) {
S
Shengliang Guan 已提交
137 138 139
    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 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  }

  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)

158
  for (int32_t i = 0; i < redoLogNum; ++i) {
S
Shengliang Guan 已提交
159 160 161 162 163
    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 已提交
164
    void *ret = taosArrayPush(pTrans->redoLogs, &pData);
S
Shengliang Guan 已提交
165 166
    if (ret == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
167
      goto TRANS_DECODE_OVER;
S
Shengliang Guan 已提交
168 169 170 171
      break;
    }
  }

S
Shengliang Guan 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  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);
    void *ret = taosArrayPush(pTrans->undoLogs, &pData);
    if (ret == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto TRANS_DECODE_OVER;
      break;
    }
  }

  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);
    void *ret = taosArrayPush(pTrans->commitLogs, &pData);
    if (ret == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto TRANS_DECODE_OVER;
      break;
    }
  }

TRANS_DECODE_OVER:
S
Shengliang Guan 已提交
201
  if (code != 0) {
S
Shengliang Guan 已提交
202
    mError("trans:%d, failed to parse from raw:%p since %s", pTrans->id, pRaw, tstrerror(errno));
S
Shengliang Guan 已提交
203
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
204
    terrno = code;
S
Shengliang Guan 已提交
205 206 207
    return NULL;
  }

S
Shengliang Guan 已提交
208
  mTrace("trans:%d, decode from raw:%p", pTrans->id, pRaw);
S
Shengliang Guan 已提交
209 210 211
  return pRow;
}

S
Shengliang Guan 已提交
212
static int32_t mndTransActionInsert(SSdb *pSdb, STrans *pTrans) {
S
Shengliang Guan 已提交
213 214
  mTrace("trans:%d, perform insert action, stage:%d", pTrans->id, pTrans->stage);

S
Shengliang Guan 已提交
215 216 217
  SArray *pArray = pTrans->redoLogs;
  int32_t arraySize = taosArrayGetSize(pArray);

218
  for (int32_t i = 0; i < arraySize; ++i) {
S
Shengliang Guan 已提交
219
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
220
    int32_t  code = sdbWrite(pSdb, pRaw);
S
Shengliang Guan 已提交
221
    if (code != 0) {
S
Shengliang Guan 已提交
222
      mError("trans:%d, failed to write raw:%p to sdb since %s", pTrans->id, pRaw, terrstr());
S
Shengliang Guan 已提交
223 224 225 226 227 228
      return code;
    }
  }
  return 0;
}

S
Shengliang Guan 已提交
229
static int32_t mndTransActionDelete(SSdb *pSdb, STrans *pTrans) {
S
Shengliang Guan 已提交
230 231 232
  mTrace("trans:%d, perform delete action, stage:%d", pTrans->id, pTrans->stage);

  SArray *pArray = pTrans->undoLogs;
S
Shengliang Guan 已提交
233 234
  int32_t arraySize = taosArrayGetSize(pArray);

235
  for (int32_t i = 0; i < arraySize; ++i) {
S
Shengliang Guan 已提交
236
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
237
    int32_t  code = sdbWrite(pSdb, pRaw);
S
Shengliang Guan 已提交
238
    if (code != 0) {
S
Shengliang Guan 已提交
239
      mError("trans:%d, failed to write raw:%p to sdb since %s", pTrans->id, pRaw, terrstr());
S
Shengliang Guan 已提交
240 241 242 243 244 245 246
      return code;
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
247 248
static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOldTrans, STrans *pNewTrans) {
  mTrace("trans:%d, perform update action, stage:%d", pOldTrans->id, pNewTrans->stage);
S
Shengliang Guan 已提交
249

S
Shengliang Guan 已提交
250
  SArray *pArray = pOldTrans->commitLogs;
S
Shengliang Guan 已提交
251 252
  int32_t arraySize = taosArrayGetSize(pArray);

253
  for (int32_t i = 0; i < arraySize; ++i) {
S
Shengliang Guan 已提交
254
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
255
    int32_t  code = sdbWrite(pSdb, pRaw);
S
Shengliang Guan 已提交
256
    if (code != 0) {
S
Shengliang Guan 已提交
257
      mError("trans:%d, failed to write raw:%p to sdb since %s", pOldTrans->id, pRaw, terrstr());
S
Shengliang Guan 已提交
258 259 260 261
      return code;
    }
  }

S
Shengliang Guan 已提交
262
  pOldTrans->stage = pNewTrans->stage;
S
Shengliang Guan 已提交
263 264 265
  return 0;
}

S
Shengliang Guan 已提交
266 267 268 269
static int32_t trnGenerateTransId() {
  static int32_t tmp = 0;
  return ++tmp;
}
S
Shengliang Guan 已提交
270

S
Shengliang Guan 已提交
271
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, void *rpcHandle) {
S
Shengliang Guan 已提交
272 273 274 275 276 277 278 279 280 281
  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;
S
Shengliang Guan 已提交
282
  pTrans->pMnode = pMnode;
S
Shengliang Guan 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296
  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;
  }

S
Shengliang Guan 已提交
297
  mDebug("trans:%d, data:%p is created", pTrans->id, pTrans);
S
Shengliang Guan 已提交
298 299 300 301
  return pTrans;
}

static void trnDropArray(SArray *pArray) {
302
  for (int32_t i = 0; i < pArray->size; ++i) {
S
Shengliang Guan 已提交
303
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
304 305 306 307 308 309
    tfree(pRaw);
  }

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
310
void mndTransDrop(STrans *pTrans) {
S
Shengliang Guan 已提交
311 312 313 314 315 316
  trnDropArray(pTrans->redoLogs);
  trnDropArray(pTrans->undoLogs);
  trnDropArray(pTrans->commitLogs);
  trnDropArray(pTrans->redoActions);
  trnDropArray(pTrans->undoActions);

S
Shengliang Guan 已提交
317
  mDebug("trans:%d, data:%p is dropped", pTrans->id, pTrans);
S
Shengliang Guan 已提交
318 319 320
  tfree(pTrans);
}

S
Shengliang Guan 已提交
321
void mndTransSetRpcHandle(STrans *pTrans, void *rpcHandle) {
S
Shengliang Guan 已提交
322
  pTrans->rpcHandle = rpcHandle;
S
Shengliang Guan 已提交
323
  mTrace("trans:%d, set rpc handle:%p", pTrans->id, rpcHandle);
S
Shengliang Guan 已提交
324 325
}

S
Shengliang Guan 已提交
326
static int32_t mndTransAppendArray(SArray *pArray, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
327 328 329 330 331
  if (pArray == NULL || pRaw == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
332
  void *ptr = taosArrayPush(pArray, &pRaw);
S
Shengliang Guan 已提交
333 334 335 336 337 338 339 340
  if (ptr == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
341 342
int32_t mndTransAppendRedolog(STrans *pTrans, SSdbRaw *pRaw) {
  int32_t code = mndTransAppendArray(pTrans->redoLogs, pRaw);
S
Shengliang Guan 已提交
343
  mTrace("trans:%d, raw:%p append to redo logs, code:%d", pTrans->id, pRaw, code);
S
Shengliang Guan 已提交
344 345 346
  return code;
}

S
Shengliang Guan 已提交
347 348
int32_t mndTransAppendUndolog(STrans *pTrans, SSdbRaw *pRaw) {
  int32_t code = mndTransAppendArray(pTrans->undoLogs, pRaw);
S
Shengliang Guan 已提交
349
  mTrace("trans:%d, raw:%p append to undo logs, code:%d", pTrans->id, pRaw, code);
S
Shengliang Guan 已提交
350 351 352
  return code;
}

S
Shengliang Guan 已提交
353 354
int32_t mndTransAppendCommitlog(STrans *pTrans, SSdbRaw *pRaw) {
  int32_t code = mndTransAppendArray(pTrans->commitLogs, pRaw);
S
Shengliang Guan 已提交
355
  mTrace("trans:%d, raw:%p append to commit logs, code:%d", pTrans->id, pRaw, code);
S
Shengliang Guan 已提交
356 357 358
  return code;
}

S
Shengliang Guan 已提交
359 360
int32_t mndTransAppendRedoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {
  int32_t code = mndTransAppendArray(pTrans->redoActions, pMsg);
S
Shengliang Guan 已提交
361
  mTrace("trans:%d, msg:%p append to redo actions", pTrans->id, pMsg);
S
Shengliang Guan 已提交
362 363 364
  return code;
}

S
Shengliang Guan 已提交
365 366
int32_t mndTransAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {
  int32_t code = mndTransAppendArray(pTrans->undoActions, pMsg);
S
Shengliang Guan 已提交
367
  mTrace("trans:%d, msg:%p append to undo actions", pTrans->id, pMsg);
S
Shengliang Guan 已提交
368 369 370
  return code;
}

S
Shengliang Guan 已提交
371 372
int32_t mndTransPrepare(STrans *pTrans) {
  mDebug("trans:%d, prepare transaction", pTrans->id);
S
Shengliang Guan 已提交
373

S
Shengliang Guan 已提交
374
  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
S
Shengliang Guan 已提交
375
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
376
    mError("trans:%d, failed to decode trans since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
377 378 379 380
    return -1;
  }
  sdbSetRawStatus(pRaw, SDB_STATUS_CREATING);

S
Shengliang Guan 已提交
381 382
  if (sdbWriteNotFree(pTrans->pMnode->pSdb, pRaw) != 0) {
    mError("trans:%d, failed to write trans since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
383 384 385
    return -1;
  }

S
Shengliang Guan 已提交
386 387 388 389 390 391 392 393 394
  STransMsg *pMsg = calloc(1, sizeof(STransMsg));
  pMsg->id = pTrans->id;
  pMsg->rpcHandle = pTrans->rpcHandle;

  mDebug("trans:%d, start sync, RPC:%p pMsg:%p", pTrans->id, pTrans->rpcHandle, pMsg);
  if (mndSyncPropose(pTrans->pMnode, pRaw, pMsg) != 0) {
    mError("trans:%d, failed to sync since %s", pTrans->id, terrstr());
    free(pMsg);
    sdbFreeRaw(pRaw);
S
Shengliang Guan 已提交
395 396 397
    return -1;
  }

S
Shengliang Guan 已提交
398
  sdbFreeRaw(pRaw);
S
Shengliang Guan 已提交
399 400 401
  return 0;
}

S
Shengliang Guan 已提交
402 403 404 405
static void trnSendRpcRsp(STransMsg *pMsg, int32_t code) {
  mDebug("trans:%d, send rpc rsp, RPC:%p code:0x%x pMsg:%p", pMsg->id, pMsg->rpcHandle, code & 0xFFFF, pMsg);
  if (pMsg->rpcHandle != NULL) {
    SRpcMsg rspMsg = {.handle = pMsg->rpcHandle, .code = code};
S
Shengliang Guan 已提交
406 407 408
    rpcSendResponse(&rspMsg);
  }

S
Shengliang Guan 已提交
409 410
  free(pMsg);
}
S
Shengliang Guan 已提交
411

S
Shengliang Guan 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
void mndTransApply(SMnode *pMnode, SSdbRaw *pRaw, STransMsg *pMsg, int32_t code) {
  if (code == 0) {
    mDebug("trans:%d, commit transaction", pMsg->id);
    sdbSetRawStatus(pRaw, SDB_STATUS_READY);
    if (sdbWrite(pMnode->pSdb, pRaw) != 0) {
      code = terrno;
      mError("trans:%d, failed to write sdb while commit since %s", pMsg->id, terrstr());
    }
    trnSendRpcRsp(pMsg, code);
  } else {
    mDebug("trans:%d, rollback transaction", pMsg->id);
    sdbSetRawStatus(pRaw, SDB_STATUS_DROPPED);
    if (sdbWrite(pMnode->pSdb, pRaw) != 0) {
      mError("trans:%d, failed to write sdb while rollback since %s", pMsg->id, terrstr());
    }
    trnSendRpcRsp(pMsg, code);
S
Shengliang Guan 已提交
428 429 430
  }
}

S
Shengliang Guan 已提交
431
static int32_t trnExecuteArray(SMnode *pMnode, SArray *pArray) {
432 433
  for (int32_t i = 0; i < pArray->size; ++i) {
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
434
    if (sdbWrite(pMnode->pSdb, pRaw) != 0) {
S
Shengliang Guan 已提交
435 436 437 438 439 440 441
      return -1;
    }
  }

  return 0;
}

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

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

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

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

S
Shengliang Guan 已提交
450
static int32_t trnExecuteUndoActions(STrans *pTrans) { return trnExecuteArray(pTrans->pMnode, pTrans->undoActions); }
S
Shengliang Guan 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

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 已提交
510
int32_t mndTransExecute(SSdb *pSdb, int32_t tranId) {
S
Shengliang Guan 已提交
511 512
  int32_t code = 0;

S
Shengliang Guan 已提交
513
  STrans *pTrans = sdbAcquire(pSdb, SDB_TRANS, &tranId);
S
Shengliang Guan 已提交
514 515 516 517 518 519
  if (pTrans == NULL) {
    return -1;
  }

  if (pTrans->stage == TRN_STAGE_PREPARE) {
    if (trnPerformPrepareStage(pTrans) != 0) {
S
Shengliang Guan 已提交
520
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
521 522 523 524 525 526
      return -1;
    }
  }

  if (pTrans->stage == TRN_STAGE_EXECUTE) {
    if (trnPerformExecuteStage(pTrans) != 0) {
S
Shengliang Guan 已提交
527
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
528 529 530 531 532 533
      return -1;
    }
  }

  if (pTrans->stage == TRN_STAGE_COMMIT) {
    if (trnPerformCommitStage(pTrans) != 0) {
S
Shengliang Guan 已提交
534
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
535 536 537 538 539 540
      return -1;
    }
  }

  if (pTrans->stage == TRN_STAGE_ROLLBACK) {
    if (trnPerformRollbackStage(pTrans) != 0) {
S
Shengliang Guan 已提交
541
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
542 543 544 545 546 547
      return -1;
    }
  }

  if (pTrans->stage == TRN_STAGE_RETRY) {
    if (trnPerformRetryStage(pTrans) != 0) {
S
Shengliang Guan 已提交
548
      sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
549 550 551 552
      return -1;
    }
  }

S
Shengliang Guan 已提交
553
  sdbRelease(pSdb, pTrans);
S
Shengliang Guan 已提交
554 555
  return 0;
}