ob_ctx_tx_data.cpp 14.7 KB
Newer Older
W
wangzelin.wzl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 87 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#include "storage/tx/ob_ctx_tx_data.h"
#include "storage/tx/ob_trans_ctx_mgr.h"
#include "storage/tx/ob_tx_data_define.h"
#include "storage/tx/ob_trans_define.h"

namespace oceanbase
{

using namespace storage;

namespace transaction
{

#define GET_TX_TABLE_(tx_table)                                                       \
  ObTxTableGuard table_guard;                                                         \
  if (OB_ISNULL(ctx_mgr_)) {                                                          \
    ret = OB_ERR_UNEXPECTED;                                                          \
    TRANS_LOG(WARN, "ctx_mgr is null when get_tx_table", K(ret));                     \
  } else if (OB_FAIL(ctx_mgr_->get_tx_table_guard(table_guard))) {                    \
    TRANS_LOG(WARN, "get tx table guard without check failed", KR(ret), K(*this));    \
  } else if (OB_ISNULL(tx_table = table_guard.get_tx_table())) {                      \
    ret = OB_ERR_UNEXPECTED;                                                          \
    TRANS_LOG(WARN, "tx table is null", KR(ret), K(ctx_mgr_->get_ls_id()), K(*this)); \
  }

int ObCtxTxData::init(ObLSTxCtxMgr *ctx_mgr, int64_t tx_id)
{
  int ret = OB_SUCCESS;
  ObTxTable *tx_table = nullptr;
  if (OB_ISNULL(ctx_mgr) || tx_id < 0) {
    ret = OB_INVALID_ARGUMENT;
    TRANS_LOG(WARN, "invalid argument", K(ret), KP(ctx_mgr), K(tx_id));
  } else {
    ctx_mgr_ = ctx_mgr;
    ObTxTable *tx_table = nullptr;
    if (OB_ISNULL(ctx_mgr_)) {
      ret = OB_ERR_UNEXPECTED;
      TRANS_LOG(WARN, "ctx_mgr is null when get_tx_table", K(ret));
    } else if (OB_ISNULL(tx_table = ctx_mgr_->get_tx_table())) {
      ret = OB_ERR_UNEXPECTED;
      TRANS_LOG(WARN, "tx table is null", KR(ret), K(ctx_mgr_->get_ls_id()), K(*this));
    } else if (OB_FAIL(tx_table->alloc_tx_data(tx_data_))) {
      TRANS_LOG(WARN, "get tx data failed", KR(ret), K(ctx_mgr_->get_ls_id()));
    } else if (OB_ISNULL(tx_data_)) {
      ret = OB_ERR_UNEXPECTED;
      TRANS_LOG(WARN, "tx data is unexpected null", KR(ret), K(ctx_mgr_->get_ls_id()));
    } else {
      tx_data_->tx_id_ = tx_id;
    }
  }
  return ret;
}

void ObCtxTxData::reset()
{
  ctx_mgr_ = nullptr;
  tx_data_ = nullptr;
  read_only_ = false;
  tx_commit_data_.reset();
}

void ObCtxTxData::destroy()
{
  int ret = OB_SUCCESS;
  if (tx_data_ != nullptr) {
    ObTxTable *tx_table = nullptr;
    GET_TX_TABLE_(tx_table);
    if (OB_FAIL(ret)) {

    } else if (OB_FAIL(free_tx_data_(tx_table, tx_data_))) {
      TRANS_LOG(WARN, "free tx data failed", K(ret), K(*this));

    } else {
      tx_data_ = nullptr;
    }
  }
}

int ObCtxTxData::insert_into_tx_table()
{
  int ret = OB_SUCCESS;
  common::ObTimeGuard tg("part_ctx::insert_into_tx_table", 100 * 1000);
  WLockGuard guard(lock_);
  tg.click();

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret));
  } else {
    tg.click();
    ObTxTable *tx_table = nullptr;
    GET_TX_TABLE_(tx_table)
    if (OB_FAIL(ret)) {
    } else {
      tg.click();
      tx_commit_data_ = *tx_data_;
      if (OB_FAIL(insert_tx_data_(tx_table, tx_data_))) {
        TRANS_LOG(WARN, "insert tx data failed", K(ret), K(*this));
      } else {
        tg.click();
        read_only_ = true;
        tx_data_ = NULL;
        tx_commit_data_.is_in_tx_data_table_ = true;
      }
    }
  }
  if (tg.get_diff() > 100000) {
    TRANS_LOG(INFO, "ObCtxData insert into tx table const too much time", K(tg));
  }

  return ret;
}

int ObCtxTxData::recover_tx_data(const ObTxData *tmp_tx_data)
{
  int ret = OB_SUCCESS;
  WLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else if (OB_ISNULL(tmp_tx_data)) {
    ret = OB_INVALID_ARGUMENT;
    TRANS_LOG(WARN, "invalid argument", K(ret), KP(tmp_tx_data));
  } else {
    *tx_data_ = *tmp_tx_data;
  }

  return ret;
}

int ObCtxTxData::replace_tx_data(ObTxData *&tmp_tx_data)
{
  int ret = OB_SUCCESS;
  WLockGuard guard(lock_);

  ObTxTable *tx_table = nullptr;
  GET_TX_TABLE_(tx_table);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else if (OB_ISNULL(tmp_tx_data)) {
    ret = OB_INVALID_ARGUMENT;
    TRANS_LOG(WARN, "invalid argument", K(ret), KP(tmp_tx_data));
  } else if (OB_FAIL(free_tx_data_(tx_table, tx_data_))) {
    TRANS_LOG(WARN, "free tx data failed", K(ret), K(*this));
  } else {
    tx_data_ = tmp_tx_data;
    tmp_tx_data = nullptr;
  }

  return ret;
}

int ObCtxTxData::deep_copy_tx_data_out(ObTxData *&tmp_tx_data)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    ObTxTable *tx_table = nullptr;
    GET_TX_TABLE_(tx_table)
    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(deep_copy_tx_data_(tx_table, tmp_tx_data))) {
      TRANS_LOG(WARN, "deep copy tx data failed", K(ret), KPC(tmp_tx_data), K(*this));
    } else if (OB_ISNULL(tmp_tx_data)) {
      ret = OB_ERR_UNEXPECTED;
O
obdev 已提交
180
      TRANS_LOG(ERROR, "copied tmp tx data is null", KR(ret), K(*this));
W
wangzelin.wzl 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    }
  }

  return ret;
}

int ObCtxTxData::alloc_tmp_tx_data(storage::ObTxData *&tmp_tx_data)
{

  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    ObTxTable *tx_table = nullptr;
    GET_TX_TABLE_(tx_table)
    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(tx_table->alloc_tx_data(tmp_tx_data))) {
      TRANS_LOG(WARN, "alloc tx data failed", K(ret));
    }
  }

  return ret;
}

int ObCtxTxData::free_tmp_tx_data(ObTxData *&tmp_tx_data)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    ObTxTable *tx_table = nullptr;
    GET_TX_TABLE_(tx_table)
    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(free_tx_data_(tx_table, tmp_tx_data))) {
      TRANS_LOG(WARN, "free tx data failed", K(ret), KPC(tmp_tx_data), K(*this));
    } else {
      tmp_tx_data = nullptr;
    }
  }

  return ret;
}

int ObCtxTxData::insert_tmp_tx_data(ObTxData *&tmp_tx_data)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    ObTxTable *tx_table = nullptr;
    GET_TX_TABLE_(tx_table)
    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(insert_tx_data_(tx_table, tmp_tx_data))) {
      TRANS_LOG(WARN, "insert tx data failed", K(ret), KPC(tmp_tx_data), K(*this));
    } else {
      tmp_tx_data = nullptr;
    }
  }

  return ret;
}

void ObCtxTxData::get_tx_table(storage::ObTxTable *&tx_table)
{
  int ret = OB_SUCCESS;

  GET_TX_TABLE_(tx_table);

  if (OB_FAIL(ret)) {
    tx_table = nullptr;
  }
}

int ObCtxTxData::set_state(int32_t state)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    tx_data_->state_ = state;
  }

  return ret;
}

int ObCtxTxData::set_commit_version(int64_t commit_version)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    tx_data_->commit_version_ = commit_version;
  }

  return ret;
}

int ObCtxTxData::set_start_log_ts(int64_t start_ts)
{
  int ret = OB_SUCCESS;
  const int64_t tmp_start_ts = (OB_INVALID_TIMESTAMP == start_ts ? INT64_MAX : start_ts);
  RLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    tx_data_->start_log_ts_ = tmp_start_ts;
  }

  return ret;
}

int ObCtxTxData::set_end_log_ts(int64_t end_ts)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    tx_data_->end_log_ts_ = end_ts;
  }

  return ret;
}

int32_t ObCtxTxData::get_state() const
{
  RLockGuard guard(lock_);
  return (NULL != tx_data_ ? tx_data_->state_: tx_commit_data_.state_);
}

int64_t ObCtxTxData::get_commit_version() const
{
  RLockGuard guard(lock_);
  return (NULL != tx_data_ ? tx_data_->commit_version_ : tx_commit_data_.commit_version_);
}

int64_t ObCtxTxData::get_start_log_ts() const
{
  RLockGuard guard(lock_);
  int64_t ctx_log_ts = (NULL != tx_data_ ? tx_data_->start_log_ts_ : tx_commit_data_.start_log_ts_);
  if (INT64_MAX == ctx_log_ts) {
    ctx_log_ts = OB_INVALID_TIMESTAMP;
  }
  return ctx_log_ts;
}

int64_t ObCtxTxData::get_end_log_ts() const
{
  RLockGuard guard(lock_);
  int64_t ctx_log_ts = (NULL != tx_data_ ? tx_data_->end_log_ts_ : tx_commit_data_.end_log_ts_);
  if (INT64_MAX == ctx_log_ts) {
    ctx_log_ts = OB_INVALID_TIMESTAMP;
  }
  return ctx_log_ts;
}

ObTransID ObCtxTxData::get_tx_id() const
{
  RLockGuard guard(lock_);
  return (NULL != tx_data_ ? tx_data_->tx_id_ : tx_commit_data_.tx_id_);
}

O
obdev 已提交
355 356 357 358
int ObCtxTxData::prepare_add_undo_action(ObUndoAction &undo_action,
                                         storage::ObTxData *&tmp_tx_data,
                                         storage::ObUndoStatusNode *&tmp_undo_status)
{
W
wangzelin.wzl 已提交
359 360
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
O
obdev 已提交
361 362 363 364 365
  /*
   * alloc undo_status_node used on commit stage
   * alloc tx_data and add undo_action to it, which will be inserted
   *       into tx_data_table after RollbackSavepoint log sync success
   */
W
wangzelin.wzl 已提交
366 367 368 369 370 371
  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    ObTxTable *tx_table = nullptr;
    GET_TX_TABLE_(tx_table);
    if (OB_FAIL(ret)) {
O
obdev 已提交
372 373 374
    } else if (OB_FAIL(tx_table->get_tx_data_table()->alloc_undo_status_node(tmp_undo_status))) {
      TRANS_LOG(WARN, "alloc undo status fail", K(ret), KPC(this));
    } else if (OB_ISNULL(tmp_undo_status)) {
W
wangzelin.wzl 已提交
375
      ret = OB_ERR_UNEXPECTED;
O
obdev 已提交
376 377
      TRANS_LOG(ERROR, "undo status is null", KR(ret), KPC(this));
    } else if (OB_FAIL(tx_table->deep_copy_tx_data(tx_data_, tmp_tx_data))) {
W
wangzelin.wzl 已提交
378
      TRANS_LOG(WARN, "copy tx data fail", K(ret), KPC(this));
O
obdev 已提交
379
    } else if (OB_ISNULL(tmp_tx_data)) {
W
wangzelin.wzl 已提交
380
      ret = OB_ERR_UNEXPECTED;
O
obdev 已提交
381 382 383
      TRANS_LOG(ERROR, "copied tx_data is null", KR(ret), KPC(this));
    } else if (OB_FAIL(tmp_tx_data->add_undo_action(tx_table, undo_action))) {
      TRANS_LOG(WARN, "add undo action fail", K(ret), KPC(this));
W
wangzelin.wzl 已提交
384
    }
O
obdev 已提交
385

W
wangzelin.wzl 已提交
386
    if (OB_FAIL(ret)) {
O
obdev 已提交
387 388 389 390 391 392
      if (tmp_undo_status) {
        tx_table->get_tx_data_table()->free_undo_status_node(tmp_undo_status);
      }
      if (tmp_tx_data) {
        tx_table->free_tx_data(tmp_tx_data);
      }
W
wangzelin.wzl 已提交
393 394 395 396 397
    }
  }
  return ret;
}

O
obdev 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
int ObCtxTxData::cancel_add_undo_action(storage::ObTxData *tmp_tx_data, storage::ObUndoStatusNode *tmp_undo_status)
{
  int ret = OB_SUCCESS;
  ObTxTable *tx_table = nullptr;
  GET_TX_TABLE_(tx_table);
  if (OB_SUCC(ret)) {
    tx_table->free_tx_data(tmp_tx_data);
    ret = tx_table->get_tx_data_table()->free_undo_status_node(tmp_undo_status);
  }
  return ret;
}

int ObCtxTxData::commit_add_undo_action(ObUndoAction &undo_action, storage::ObUndoStatusNode &tmp_undo_status)
{
  return add_undo_action(undo_action, &tmp_undo_status);
}

int ObCtxTxData::add_undo_action(ObUndoAction &undo_action, storage::ObUndoStatusNode *tmp_undo_status)
W
wangzelin.wzl 已提交
416 417 418 419 420 421 422 423 424 425 426
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (OB_FAIL(check_tx_data_writable_())) {
    TRANS_LOG(WARN, "tx data is not writeable", K(ret), K(*this));
  } else {
    ObTxTable *tx_table = nullptr;
    GET_TX_TABLE_(tx_table);
    if (OB_FAIL(ret)) {
      // do nothing
O
obdev 已提交
427 428
    } else if (OB_FAIL(tx_data_->add_undo_action(tx_table, undo_action, tmp_undo_status))) {
      TRANS_LOG(WARN, "add undo action failed", K(ret), K(undo_action), KP(tmp_undo_status), K(*this));
W
wangzelin.wzl 已提交
429 430 431 432 433 434 435 436 437
    };
  }

  return ret;
}

int ObCtxTxData::Guard::get_tx_data(const ObTxData *&tx_data) const
{
  int ret = OB_SUCCESS;
C
chinaxing 已提交
438 439
  auto tmp_tx_data = host_.tx_data_;
  if (NULL == tmp_tx_data) {
W
wangzelin.wzl 已提交
440 441
    ret = OB_TRANS_CTX_NOT_EXIST;
  } else {
C
chinaxing 已提交
442
    tx_data = tmp_tx_data;
W
wangzelin.wzl 已提交
443 444 445 446 447 448 449 450 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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
  }
  return ret;
}

int ObCtxTxData::get_tx_commit_data(const ObTxCommitData *&tx_commit_data) const
{
  int ret = OB_SUCCESS;
  tx_commit_data = &tx_commit_data_;
  return ret;
}

int ObCtxTxData::check_tx_data_writable_()
{
  int ret = OB_SUCCESS;

  if (OB_ISNULL(tx_data_)) {
    ret = OB_ERR_UNEXPECTED;
    TRANS_LOG(WARN, "tx_data_ is not valid", K(this));
  } else if (tx_data_->is_in_tx_data_table_) {
    ret = OB_ERR_UNEXPECTED;
    TRANS_LOG(ERROR, "tx_data has inserted into tx table", K(ret), K(this));
  } else if (read_only_) {
    ret = OB_ERR_UNEXPECTED;
    TRANS_LOG(ERROR, "try to write a read-only tx_data", K(ret), K(this));
  }
  return ret;
}

int ObCtxTxData::insert_tx_data_(ObTxTable *tx_table, ObTxData *&tx_data)
{
  int ret = OB_SUCCESS;

  if (OB_ISNULL(tx_table)) {
    ret = OB_INVALID_ARGUMENT;
    TRANS_LOG(WARN, "invalid argument", K(ret), KP(tx_table));
  } else if (OB_ISNULL(tx_data)) {
    TRANS_LOG(INFO, "tx_data is nullptr, no need to insert", KP(tx_data), K(*this));
    // no need to insert, do nothing
  } else if (OB_FAIL(tx_table->insert(tx_data))) {
    TRANS_LOG(WARN, "insert into tx_table failed", K(ret), KPC(tx_data));
  } else {
    tx_data = nullptr;
  }

  return ret;
}

int ObCtxTxData::free_tx_data_(ObTxTable *tx_table, ObTxData *&tx_data)
{
  int ret = OB_SUCCESS;

  if (OB_ISNULL(tx_table)) {
    ret = OB_INVALID_ARGUMENT;
    TRANS_LOG(WARN, "invalid argument", K(ret), KP(tx_table));
  } else if (OB_ISNULL(tx_data)) {
    TRANS_LOG(INFO, "tx_data is nullptr, no need to free", KP(tx_data), K(*this));
    // no need to free, do nothing
  } else {
    tx_table->free_tx_data(tx_data);
    tx_data = nullptr;
  }
  return ret;
}

int ObCtxTxData::deep_copy_tx_data_(ObTxTable *tx_table, storage::ObTxData *&tx_data)
{
  int ret = OB_SUCCESS;

  if (OB_ISNULL(tx_table)) {
    ret = OB_INVALID_ARGUMENT;
    TRANS_LOG(WARN, "invalid argument", K(ret), KP(tx_table));
  } else if (OB_ISNULL(tx_data_)) {
    TRANS_LOG(INFO, "tx_data_ is nullptr, no need to deep copy tx data", K(*this));
    // no need to free, do nothing
  } else if (OB_FAIL(tx_table->deep_copy_tx_data(tx_data_, tx_data))) {
    TRANS_LOG(WARN, "deep copy tx data failed", K(ret), KPC(tx_data), K(*this));
  }
  return ret;
}

} // namespace transaction

} // namespace oceanbase