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

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndTrans.h"
S
Shengliang Guan 已提交
18
#include "mndSync.h"
S
Shengliang Guan 已提交
19

S
Shengliang Guan 已提交
20 21 22
#define TSDB_TRANS_VER 1
#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, 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) {
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
STrans *mndAcquireTrans(SMnode *pMnode, int32_t transId) {
  SSdb *pSdb = pMnode->pSdb;
  return sdbAcquire(pSdb, SDB_TRANS, &transId);
}

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 已提交
339
    case TRN_STAGE_OVER:
340
      return "over";
S
Shengliang Guan 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    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 已提交
357
STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, void *rpcHandle) {
S
Shengliang Guan 已提交
358 359 360 361 362 363 364
  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 已提交
365
  pTrans->id = sdbGetMaxId(pMnode->pSdb, SDB_TRANS);
S
Shengliang Guan 已提交
366 367 368
  pTrans->stage = TRN_STAGE_PREPARE;
  pTrans->policy = policy;
  pTrans->rpcHandle = rpcHandle;
S
Shengliang Guan 已提交
369 370 371
  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 已提交
372 373
  pTrans->redoActions = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(STransAction));
  pTrans->undoActions = taosArrayInit(TSDB_TRN_ARRAY_SIZE, sizeof(STransAction));
S
Shengliang Guan 已提交
374 375 376 377 378 379 380 381

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

382
  mDebug("trans:%d, is created", pTrans->id);
S
Shengliang Guan 已提交
383 384 385
  return pTrans;
}

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

  taosArrayDestroy(pArray);
}

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

  taosArrayDestroy(pArray);
}

S
Shengliang Guan 已提交
404
void mndTransDrop(STrans *pTrans) {
S
Shengliang Guan 已提交
405 406 407 408 409
  mndTransDropLogs(pTrans->redoLogs);
  mndTransDropLogs(pTrans->undoLogs);
  mndTransDropLogs(pTrans->commitLogs);
  mndTransDropActions(pTrans->redoActions);
  mndTransDropActions(pTrans->undoActions);
S
Shengliang Guan 已提交
410

411
  // mDebug("trans:%d, is dropped, data:%p", pTrans->id, pTrans);
S
Shengliang Guan 已提交
412 413 414
  tfree(pTrans);
}

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

S
Shengliang Guan 已提交
420
static int32_t mndTransAppendLog(SArray *pArray, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
421 422 423 424 425
  if (pArray == NULL || pRaw == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

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

  return 0;
}

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

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

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

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

  return 0;
}

S
Shengliang Guan 已提交
463 464
int32_t mndTransAppendRedoAction(STrans *pTrans, STransAction *pAction) {
  return mndTransAppendAction(pTrans->redoActions, pAction);
S
Shengliang Guan 已提交
465 466
}

S
Shengliang Guan 已提交
467 468
int32_t mndTransAppendUndoAction(STrans *pTrans, STransAction *pAction) {
  return mndTransAppendAction(pTrans->undoActions, pAction);
S
Shengliang Guan 已提交
469 470
}

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

S
Shengliang Guan 已提交
474
  SSdbRaw *pRaw = mndTransActionEncode(pTrans);
S
Shengliang Guan 已提交
475
  if (pRaw == NULL) {
S
Shengliang Guan 已提交
476
    mError("trans:%d, failed to decode trans since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
477 478
    return -1;
  }
S
Shengliang Guan 已提交
479
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
480

481
  mTrace("trans:%d, sync to other nodes", pTrans->id);
S
Shengliang Guan 已提交
482 483 484 485
  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 已提交
486 487 488
    return -1;
  }

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

S
Shengliang Guan 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503
  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;
  }

  mDebug("trans:%d, prepare finished", pNewTrans->id);
S
Shengliang Guan 已提交
504
  pNewTrans->rpcHandle = pTrans->rpcHandle;
S
Shengliang Guan 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
  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 已提交
520
  if (taosArrayGetSize(pTrans->commitLogs) != 0) {
521
    mTrace("trans:%d, sync to other nodes", pTrans->id);
S
Shengliang Guan 已提交
522 523 524 525 526 527
    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 已提交
528

S
Shengliang Guan 已提交
529 530 531 532 533 534
    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 已提交
535 536 537
  }

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

S
Shengliang Guan 已提交
541 542 543 544 545 546 547 548 549 550
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);

551
  mTrace("trans:%d, sync to other nodes", pTrans->id);
S
Shengliang Guan 已提交
552 553 554 555 556 557 558 559 560 561 562 563
  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 已提交
564 565
  }

S
Shengliang Guan 已提交
566 567
  mDebug("trans:%d, rollback finished", pTrans->id);
  return 0;
S
Shengliang Guan 已提交
568
}
S
Shengliang Guan 已提交
569

S
Shengliang Guan 已提交
570 571 572 573 574 575 576
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 已提交
577 578 579
  }
}

S
Shengliang Guan 已提交
580 581 582 583
void mndTransApply(SMnode *pMnode, SSdbRaw *pRaw, STransMsg *pMsg, int32_t code) {
  // todo
}

584 585 586 587 588 589 590 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
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 已提交
628
static int32_t mndTransExecuteLogs(SMnode *pMnode, SArray *pArray) {
S
Shengliang Guan 已提交
629 630 631 632
  SSdb   *pSdb = pMnode->pSdb;
  int32_t arraySize = taosArrayGetSize(pArray);

  for (int32_t i = 0; i < arraySize; ++i) {
633
    SSdbRaw *pRaw = taosArrayGetP(pArray, i);
S
Shengliang Guan 已提交
634 635 636
    int32_t  code = sdbWriteNotFree(pSdb, pRaw);
    if (code != 0) {
      return code;
S
Shengliang Guan 已提交
637 638 639 640 641 642
    }
  }

  return 0;
}

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

  return code;
}

static int32_t mndTransExecuteUndoLogs(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
658 659
  int32_t code = 0;
  if (taosArrayGetSize(pTrans->undoLogs) != 0) {
S
Shengliang Guan 已提交
660
    code = mndTransExecuteLogs(pMnode, pTrans->undoLogs);
S
Shengliang Guan 已提交
661 662 663
    if (code != 0) {
      mError("trans:%d, failed to execute undo logs since %s", pTrans->id, terrstr())
    } else {
664
      mDebug("trans:%d, execute undo logs finished", pTrans->id)
S
Shengliang Guan 已提交
665
    }
S
Shengliang Guan 已提交
666 667 668 669 670 671
  }

  return code;
}

static int32_t mndTransExecuteCommitLogs(SMnode *pMnode, STrans *pTrans) {
S
Shengliang Guan 已提交
672 673
  int32_t code = 0;
  if (taosArrayGetSize(pTrans->commitLogs) != 0) {
S
Shengliang Guan 已提交
674
    code = mndTransExecuteLogs(pMnode, pTrans->commitLogs);
S
Shengliang Guan 已提交
675 676 677
    if (code != 0) {
      mError("trans:%d, failed to execute commit logs since %s", pTrans->id, terrstr())
    } else {
678
      mDebug("trans:%d, execute commit logs finished", pTrans->id)
S
Shengliang Guan 已提交
679
    }
S
Shengliang Guan 已提交
680
  }
S
Shengliang Guan 已提交
681

S
Shengliang Guan 已提交
682 683
  return code;
}
S
Shengliang Guan 已提交
684

685 686 687 688 689 690 691 692
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 已提交
693

694 695 696 697 698
    int64_t signature = pTrans->id;
    signature = (signature << 32);
    signature += action;

    SRpcMsg rpcMsg = {.msgType = pAction->msgType, .contLen = pAction->contLen, .ahandle = (void *)signature};
S
Shengliang Guan 已提交
699 700 701 702
    rpcMsg.pCont = rpcMallocCont(pAction->contLen);
    if (rpcMsg.pCont == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
S
Shengliang Guan 已提交
703
    }
S
Shengliang Guan 已提交
704
    memcpy(rpcMsg.pCont, pAction->pCont, pAction->contLen);
705 706 707 708 709 710

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

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

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
  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 已提交
734 735
}

S
Shengliang Guan 已提交
736
static int32_t mndTransExecuteRedoActions(SMnode *pMnode, STrans *pTrans) {
737
  return mndTransExecuteActions(pMnode, pTrans, pTrans->redoActions);
S
Shengliang Guan 已提交
738
}
S
Shengliang Guan 已提交
739

S
Shengliang Guan 已提交
740
static int32_t mndTransExecuteUndoActions(SMnode *pMnode, STrans *pTrans) {
741
  return mndTransExecuteActions(pMnode, pTrans, pTrans->undoActions);
S
Shengliang Guan 已提交
742
}
S
Shengliang Guan 已提交
743

S
Shengliang Guan 已提交
744 745
static int32_t mndTransPerformPrepareStage(SMnode *pMnode, STrans *pTrans) {
  int32_t code = mndTransExecuteRedoLogs(pMnode, pTrans);
S
Shengliang Guan 已提交
746

S
Shengliang Guan 已提交
747
  if (code == 0) {
S
Shengliang Guan 已提交
748
    pTrans->stage = TRN_STAGE_EXECUTE;
749
    mDebug("trans:%d, stage from prepare to execute", pTrans->id);
S
Shengliang Guan 已提交
750 751
  } else {
    pTrans->stage = TRN_STAGE_ROLLBACK;
S
Shengliang Guan 已提交
752
    mError("trans:%d, stage from prepare to rollback since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
753
  }
S
Shengliang Guan 已提交
754 755

  return 0;
S
Shengliang Guan 已提交
756 757
}

S
Shengliang Guan 已提交
758 759
static int32_t mndTransPerformExecuteStage(SMnode *pMnode, STrans *pTrans) {
  int32_t code = mndTransExecuteRedoActions(pMnode, pTrans);
S
Shengliang Guan 已提交
760 761 762

  if (code == 0) {
    pTrans->stage = TRN_STAGE_COMMIT;
763
    mDebug("trans:%d, stage from execute to commit", pTrans->id);
S
Shengliang Guan 已提交
764
  } else if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) {
765
    mDebug("trans:%d, stage keep on execute since %s", pTrans->id, tstrerror(code));
S
Shengliang Guan 已提交
766
    return code;
S
Shengliang Guan 已提交
767
  } else {
S
Shengliang Guan 已提交
768
    if (pTrans->policy == TRN_POLICY_ROLLBACK) {
S
Shengliang Guan 已提交
769
      pTrans->stage = TRN_STAGE_ROLLBACK;
S
Shengliang Guan 已提交
770 771
      mError("trans:%d, stage from execute to rollback since %s", pTrans->id, terrstr());
    } else {
772 773
      pTrans->stage = TRN_STAGE_EXECUTE;
      mError("trans:%d, stage keep on execute since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
774 775 776
    }
  }

S
Shengliang Guan 已提交
777
  return 0;
S
Shengliang Guan 已提交
778 779
}

S
Shengliang Guan 已提交
780
static int32_t mndTransPerformCommitStage(SMnode *pMnode, STrans *pTrans) {
781 782 783
  mndTransExecuteCommitLogs(pMnode, pTrans);
  pTrans->stage = TRN_STAGE_OVER;
  return 0;
S
Shengliang Guan 已提交
784 785
}

S
Shengliang Guan 已提交
786 787 788 789
static int32_t mndTransPerformRollbackStage(SMnode *pMnode, STrans *pTrans) {
  int32_t code = mndTransExecuteUndoActions(pMnode, pTrans);

  if (code == 0) {
790
    mDebug("trans:%d, rollbacked", pTrans->id);
S
Shengliang Guan 已提交
791 792
  } else {
    pTrans->stage = TRN_STAGE_ROLLBACK;
S
Shengliang Guan 已提交
793
    mError("trans:%d, stage keep on rollback since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
794 795
  }

S
Shengliang Guan 已提交
796 797
  return code;
}
S
Shengliang Guan 已提交
798

S
Shengliang Guan 已提交
799 800
static void mndTransExecute(SMnode *pMnode, STrans *pTrans) {
  int32_t code = 0;
S
Shengliang Guan 已提交
801

S
Shengliang Guan 已提交
802 803 804 805 806 807 808 809 810 811 812
  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) {
813
          mndTransPerformCommitStage(pMnode, pTrans);
S
Shengliang Guan 已提交
814 815 816 817 818 819 820 821
        }
        break;
      case TRN_STAGE_ROLLBACK:
        code = mndTransPerformRollbackStage(pMnode, pTrans);
        if (code == 0) {
          code = mndTransRollback(pMnode, pTrans);
        }
        break;
S
Shengliang Guan 已提交
822 823 824
      default:
        mndTransSendRpcRsp(pTrans, 0);
        return;
S
Shengliang Guan 已提交
825 826 827
    }
  }

S
Shengliang Guan 已提交
828 829
  mndTransSendRpcRsp(pTrans, code);
}