ob_tx_api.cpp 66.1 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
/**
 * 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 "ob_trans_service.h"
#include "common/storage/ob_sequence.h"
#include "ob_trans_part_ctx.h"
#include "wrs/ob_i_weak_read_service.h"
#include "storage/tx/ob_tx_log.h"
#include "storage/tx/wrs/ob_weak_read_service.h"
#include "storage/tx/wrs/ob_weak_read_util.h"
#include "ob_xa_service.h"
// ------------------------------------------------------------------------------------------
// Implimentation notes:
// there are two relation we need care:
// a) the relation between data: WAR, we need read data after writen
// b) the relation between operations:
//    i. write happened before rollback, to barrier write after rollback
//    ii. savepoint happened before following writes, to barrier write after savepoint
//
// thus, the interface use two logical clock to track these relations:
// 1. the Logical Clock for data relation, and
// 2. the In-Transaction-Clock for operation relation
//
// ::get_snapshots::
//   1. advance Logical Clock to establish data relation of Write-After-Read
// ::create savepoint::
//   1. advance Logical Clock to establish data relation of Write-After-SavePoint
//   2. advance In-Transaction-Clock to establish operation relation of Write-After-SavePoint
// ::rollback to savepoint::
//   1. advance In-Transaction-Clock to establish operation relation of Rollback-After-Write
// -------------------------------------------------------------------------------------------
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#define TXN_API_SANITY_CHECK_FOR_TXN_FREE_ROUTE \
  do {                                                                  \
    bool inv = false;                                                   \
    if (tx.is_xa_trans()) {                                             \
      inv = tx.xa_start_addr_ != self_;                                 \
    } else {                                                            \
      inv = tx.addr_ != self_;                                          \
    }                                                                   \
    if (inv) {                                                          \
      int ret = OB_TRANS_FREE_ROUTE_NOT_SUPPORTED;                      \
      TRANS_LOG(ERROR, "incorrect route of txn free route", K(ret), K(tx)); \
      return ret;                                                       \
    }                                                                   \
  } while (0);

W
wangzelin.wzl 已提交
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
namespace oceanbase {

using namespace share;

namespace transaction {

inline void ObTransService::init_tx_(ObTxDesc &tx, const uint32_t session_id)
{
  tx.tenant_id_ = tenant_id_;
  tx.addr_      = self_;
  tx.sess_id_   = session_id;
  tx.alloc_ts_  = ObClockGenerator::getClock();
  tx.expire_ts_ = INT64_MAX;
  tx.op_sn_     = 1;
  tx.state_     = ObTxDesc::State::IDLE;
}

int ObTransService::acquire_tx(ObTxDesc *&tx, const uint32_t session_id)
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(tx_desc_mgr_.alloc(tx))) {
    TRANS_LOG(WARN, "alloc tx fail", K(ret));
  } else {
    init_tx_(*tx, session_id);
  }
81
  TRANS_LOG(TRACE, "acquire tx", KPC(tx), K(session_id));
W
wangzelin.wzl 已提交
82 83
  if (OB_SUCC(ret)) {
    ObTransTraceLog &tlog = tx->get_tlog();
O
obdev 已提交
84
    REC_TRANS_TRACE_EXT(&tlog, acquire, OB_Y(ret),
W
wangzelin.wzl 已提交
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
                        OB_ID(addr), (void*)tx,
                        OB_ID(session), session_id,
                        OB_ID(ref), tx->get_ref(),
                        OB_ID(thread_id), GETTID());
  }
  return ret;
}

int ObTransService::finalize_tx_(ObTxDesc &tx)
{
  int ret = OB_SUCCESS;
  ObSpinLockGuard guard(tx.lock_);
  tx.flags_.RELEASED_ = true;
  if (tx.is_tx_active() && !tx.flags_.REPLICA_) {
    ret = OB_ERR_UNEXPECTED;
    TRANS_LOG(ERROR, "release tx when tx is active", K(ret), KPC(this), K(tx));
    tx.print_trace_();
  } else if (tx.is_committing()) {
    TRANS_LOG(WARN, "release tx when tx is committing", KPC(this), K(tx));
  }
  tx.cancel_commit_cb();
  if (tx.tx_id_.is_valid()) {
    tx_desc_mgr_.remove(tx);
  }
  return ret;
}

/*
 * release_tx - release the Tx object
 *
 * release tx is the final step for user operate on TxDesc.
 * generally, user should commit / rollback the tx before they release it.
 * the txDesc object should not been access anymore after release.
 *
 * - for tx in async committing
 *   the commit callback will not be called if not already called, and
 *   don't forget to call release_tx before release callback's memory
 * - for tx which is a shadow copy of original tx (started on another server)
 *   release just free its memory used
 */
int ObTransService::release_tx(ObTxDesc &tx)
{
  /*
   * for compatible with cross tenant session usage
   * we should switch tenant to prevent missmatch
   * eg.
   *    SYS tenant swith to Normal tenant execute SQL
   *    and then destory its session after switch back
   */
  int ret = OB_SUCCESS;
  TRANS_LOG(TRACE, "release tx", KPC(this), K(tx));
  if (tx.tenant_id_ != MTL_ID()) {
    MTL_SWITCH(tx.tenant_id_) {
      return MTL(ObTransService*)->release_tx(tx);
    }
  } else {
    ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
142
    REC_TRANS_TRACE_EXT(&tlog, release, OB_Y(ret),
W
wangzelin.wzl 已提交
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
                        OB_ID(ref), tx.get_ref(),
                        OB_ID(thread_id), GETTID());
    if (tx.flags_.SHADOW_) {
#ifndef NDEBUG
      if (tx.tx_id_.is_valid()) {
        tx.print_trace();
      }
#endif
      tx_desc_mgr_.free(&tx);
    } else {
      finalize_tx_(tx);
      tx_desc_mgr_.revert(tx);
    }
  }
  TRANS_LOG(TRACE, "release tx done", KP(&tx), KPC(this), K(lbt()));
  return ret;
}

int ObTransService::reuse_tx(ObTxDesc &tx)
{
  int ret = OB_SUCCESS;
  ObTransID orig_tx_id = tx.tx_id_;
  if (tx.is_in_tx() && !tx.is_tx_end()) {
    ret = OB_ERR_UNEXPECTED;
    TRANS_LOG(ERROR, "can not reuse tx which has active and not end yet", K(ret), K(tx.tx_id_));
  } else if (OB_FAIL(finalize_tx_(tx))) {
    TRANS_LOG(WARN, "finalize tx fail", K(ret), K(tx.tx_id_));
  } else {
    int cnt = 0;
    int final_ref_cnt = tx.commit_cb_lock_.self_locked() ? 2 : 1;
    while (tx.get_ref() > final_ref_cnt) {
      PAUSE();
      if (++cnt > 100) { ob_usleep(500); }
      if (cnt > 2000 && TC_REACH_TIME_INTERVAL(2 * 1000 * 1000)) {
177
        TRANS_LOG_RET(WARN, OB_ERR_TOO_MUCH_TIME, "blocking to wait tx referent quiescent cost too much time",
W
wangzelin.wzl 已提交
178 179 180 181 182 183 184 185 186 187 188
                  "tx_id", orig_tx_id, KP(&tx), K(final_ref_cnt), K(tx.get_ref()));
        tx.print_trace();
      }
    }
    // it is safe to operate tx without lock when not shared
    auto session_id = tx.sess_id_;
    tx.reset();
    init_tx_(tx, session_id);
  }
  TRANS_LOG(TRACE, "reuse tx", K(ret), K(orig_tx_id), K(tx));
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
189
  REC_TRANS_TRACE_EXT(&tlog, reuse, OB_Y(ret),
W
wangzelin.wzl 已提交
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
                      OB_ID(addr), (void*)&tx,
                      OB_ID(txid), orig_tx_id,
                      OB_ID(thread_id), GETTID());
  return ret;
}

int ObTransService::stop_tx(ObTxDesc &tx)
{
  int ret = OB_SUCCESS;
  tx.lock_.lock();
  TRANS_LOG(INFO, "stop_tx, print its trace as following", K(tx));
  tx.print_trace_();
  if (tx.state_ < ObTxDesc::State::IN_TERMINATE) {
    abort_tx_(tx, ObTxAbortCause::STOP, true);
  } else if (!tx.is_terminated()) {
    unregister_commit_retry_task_(tx);
    // arm callback arguments
    tx.commit_out_ = OB_TRANS_UNKNOWN;
    tx.state_ = ObTxDesc::State::COMMIT_UNKNOWN;
  }
  tx.lock_.unlock();
  // run callback after unlock
  tx.execute_commit_cb();
  return ret;
}

int ObTransService::start_tx(ObTxDesc &tx, const ObTxParam &tx_param)
{
  int ret = OB_SUCCESS;
  if (!tx_param.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    TRANS_LOG(WARN, "invalid tx param", K(ret), KR(ret), K(tx_param));
  } else {
    TX_STAT_START_INC
      ObSpinLockGuard guard(tx.lock_);
    tx.inc_op_sn();
    if (OB_FAIL(tx_desc_mgr_.add(tx))) {
      TRANS_LOG(WARN, "add tx to txMgr fail", K(ret), K(tx));
    } else {
      tx.cluster_version_ = GET_MIN_CLUSTER_VERSION();
      tx.cluster_id_      = tx_param.cluster_id_;
      tx.access_mode_     = tx_param.access_mode_;
      tx.isolation_       = tx_param.isolation_;
      tx.active_ts_       = ObClockGenerator::getClock();
      tx.timeout_us_      = tx_param.timeout_us_;
      tx.lock_timeout_us_ = tx_param.lock_timeout_us_;
      auto a = tx.timeout_us_ + tx.active_ts_;
      tx.expire_ts_       = a < 0 ? INT64_MAX : a;
      // start tx need reacquire snapshot
239
      tx.snapshot_version_.reset();
W
wangzelin.wzl 已提交
240 241 242
      // setup correct active_scn, whatever its used or not
      tx.active_scn_      = ObSequence::get_max_seq_no();
      tx.state_           = ObTxDesc::State::ACTIVE;
243
      tx.flags_.EXPLICIT_ = true;
W
wangzelin.wzl 已提交
244 245
    }
    ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
246
    REC_TRANS_TRACE_EXT(&tlog, start_tx, OB_Y(ret),
W
wangzelin.wzl 已提交
247 248 249 250 251 252 253 254
                        OB_ID(txid), tx.tx_id_,
                        OB_ID(isolation_level), (int)tx.isolation_,
                        OB_ID(ref), tx.get_ref(),
                        OB_ID(thread_id), GETTID());
  }
  if (OB_FAIL(ret)) {
    TRANS_LOG(WARN, "start tx failed", K(ret), K(tx));
  } else {
255
    tx.state_change_flags_.mark_all();
W
wangzelin.wzl 已提交
256 257 258 259 260 261 262 263 264
#ifndef NDEBUG
    TRANS_LOG(INFO, "start tx succeed", K(tx));
#endif
  }
  return ret;
}

int ObTransService::rollback_tx(ObTxDesc &tx)
{
265
  TXN_API_SANITY_CHECK_FOR_TXN_FREE_ROUTE
W
wangzelin.wzl 已提交
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
  int ret = OB_SUCCESS;
  TX_STAT_ROLLBACK_INC
  ObSpinLockGuard guard(tx.lock_);
  tx.inc_op_sn();
  switch(tx.state_) {
  case ObTxDesc::State::ABORTED:
    TX_STAT_ROLLBACK_INC
    tx.state_ = ObTxDesc::State::ROLLED_BACK;
    break;
  case ObTxDesc::State::ROLLED_BACK:
    ret = OB_TRANS_ROLLBACKED;
    TRANS_LOG(WARN, "tx rollbacked", K(ret), K(tx));
    break;
  case ObTxDesc::State::COMMITTED:
    ret = OB_TRANS_COMMITED;
    TRANS_LOG(WARN, "tx committed", K(ret), K(tx));
    break;
  case ObTxDesc::State::IN_TERMINATE:
  case ObTxDesc::State::COMMIT_TIMEOUT:
  case ObTxDesc::State::COMMIT_UNKNOWN:
    ret = OB_TRANS_HAS_DECIDED;
    TRANS_LOG(WARN, "tx in terminating", K(ret), K(tx));
    break;
  case ObTxDesc::State::ACTIVE:
  case ObTxDesc::State::IMPLICIT_ACTIVE:
    TX_STAT_ROLLBACK_INC
    tx.state_ = ObTxDesc::State::IN_TERMINATE;
    tx.abort_cause_ = OB_TRANS_ROLLBACKED;
    abort_participants_(tx);
  case ObTxDesc::State::IDLE:
    TX_STAT_ROLLBACK_INC
    tx.state_ = ObTxDesc::State::ROLLED_BACK;
    tx.finish_ts_ = ObClockGenerator::getClock();
    tx_post_terminate_(tx);
    break;
  default:
    ret = OB_TRANS_INVALID_STATE;
    TRANS_LOG(WARN, "invalid state", K(ret), K_(tx.state), K(tx));
  }
  TRANS_LOG(INFO, "rollback tx", K(ret), K(*this), K(tx));
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
307
  REC_TRANS_TRACE_EXT(&tlog, rollback_tx, OB_Y(ret),
W
wangzelin.wzl 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  return ret;
}

// impl note
// abort tx should invalidate registered snapshot
// savepoint not invalidate, they were invalidate
// when do explicit rollback
int ObTransService::abort_tx(ObTxDesc &tx, int cause)
{
  int ret = OB_SUCCESS;
  ObSpinLockGuard guard(tx.lock_);
  tx.inc_op_sn();
  if (tx.state_ != ObTxDesc::State::ABORTED) {
    ret = abort_tx_(tx, cause);
  }
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
326
  REC_TRANS_TRACE_EXT(&tlog, abort_tx, OB_Y(ret),
W
wangzelin.wzl 已提交
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
                      OB_ID(arg), cause,
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  tx.print_trace_();
  return ret;
}

namespace {
  struct SyncTxCommitCb : public ObITxCallback
  {
    public:
    void callback(int ret) { cond_.notify(ret); }
    int wait(const int64_t time_us, int &ret) {
      return cond_.wait(time_us, ret);
    }
    ObTransCond cond_;
  };
}

int ObTransService::commit_tx(ObTxDesc &tx, const int64_t expire_ts, const ObString *trace_info)
{
  int ret = OB_SUCCESS;
  auto start_ts = ObTimeUtility::current_time();
  SyncTxCommitCb cb;
  if (OB_SUCC(submit_commit_tx(tx, expire_ts, cb, trace_info))) {
    int result = 0;
    // plus 10s to wait callback, if callback leaky, wakeup self
    int64_t wait_us = MAX(expire_ts - ObTimeUtility::current_time(), 0) + 10 * 1000 * 1000L;
    if (OB_FAIL(cb.wait(wait_us, result))) {
      TRANS_LOG(WARN, "wait commit fail", K(ret), K(expire_ts), K(wait_us), K(tx));
      /* NOTE: must cancel callback before release it */
      tx.cancel_commit_cb();
      ret = ret == OB_TIMEOUT ? OB_TRANS_STMT_TIMEOUT : ret;
    } else {
      ret = result;
    }
  }
  auto elapsed_us = ObTimeUtility::current_time() - start_ts;
#ifndef NDEBUG
  TRANS_LOG(INFO, "sync commit tx", K(ret), K(tx), K(expire_ts));
#else
  if (OB_FAIL(ret)) {
    TRANS_LOG(WARN, "sync commit tx fail", K(ret), K(tx), K(expire_ts));
  }
#endif
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
373
  REC_TRANS_TRACE_EXT(&tlog, commit_tx, OB_Y(ret), OB_Y(expire_ts),
W
wangzelin.wzl 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
                      OB_ID(time_used), elapsed_us,
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  if (OB_FAIL(ret)) {
    tx.print_trace();
  }
  return ret;
}

// impl note
// imediately succeed cases:
//   1. idle state
//   2. empty valid part
// imediately fail cases:
//   1. aborted state
//   2. incorrect state
//   3. tx-timeout state
// on commit finish:
//   1. release savepoints
// on commit fail:
//   1. invalid registered snapshot
int ObTransService::submit_commit_tx(ObTxDesc &tx,
                                     const int64_t expire_ts,
                                     ObITxCallback &cb,
                                     const ObString *trace_info)
{
400
  TXN_API_SANITY_CHECK_FOR_TXN_FREE_ROUTE
W
wangzelin.wzl 已提交
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 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
  int ret = OB_SUCCESS;
  tx.lock_.lock();
  if (tx.commit_ts_ <= 0) {
    tx.commit_ts_ = ObClockGenerator::getClock();
  }
  tx.inc_op_sn();
  switch(tx.state_) {
  case ObTxDesc::State::IDLE:
    TRANS_LOG(TRACE, "commit a dummy tx", K(tx), KP(&cb));
    tx.set_commit_cb(&cb);
    handle_tx_commit_result_(tx, OB_SUCCESS);
    ret = OB_SUCCESS;
    break;
  case ObTxDesc::State::ABORTED:
    handle_tx_commit_result_(tx, OB_TRANS_ROLLBACKED);
    ret = OB_TRANS_ROLLBACKED;
    break;
  case ObTxDesc::State::ROLLED_BACK:
    ret = OB_TRANS_ROLLBACKED;
    TRANS_LOG(WARN, "insane tx action", K(ret), K(tx));
    break;
  case ObTxDesc::State::COMMITTED:
    ret = OB_TRANS_COMMITED;
    TRANS_LOG(WARN, "insane tx action", K(ret), K(tx));
    break;
  case ObTxDesc::State::IN_TERMINATE:
  case ObTxDesc::State::COMMIT_TIMEOUT:
  case ObTxDesc::State::COMMIT_UNKNOWN:
    ret = OB_TRANS_HAS_DECIDED;
    TRANS_LOG(WARN, "insane tx action", K(ret), K(tx));
    break;
  case ObTxDesc::State::ACTIVE:
  case ObTxDesc::State::IMPLICIT_ACTIVE:
    if (tx.expire_ts_ <= ObClockGenerator::getClock()) {
      TX_STAT_TIMEOUT_INC
      TRANS_LOG(WARN, "tx has timeout, it has rollbacked internally", K_(tx.expire_ts), K(tx));
      ret = OB_TRANS_ROLLBACKED;
      handle_tx_commit_result_(tx, OB_TRANS_ROLLBACKED);
    } else if (tx.flags_.PARTS_INCOMPLETE_) {
      TRANS_LOG(WARN, "txn participants set incomplete, can not commit", K(ret), K(tx));
      abort_tx_(tx, ObTxAbortCause::PARTICIPANTS_SET_INCOMPLETE);
      handle_tx_commit_result_(tx, OB_TRANS_ROLLBACKED);
      ret = OB_TRANS_ROLLBACKED;
    } else if (tx.flags_.PART_EPOCH_MISMATCH_) {
      TRANS_LOG(WARN, "txn participant state incomplete, can not commit", K(ret), K(tx));
      abort_tx_(tx, ObTxAbortCause::PARTICIPANT_STATE_INCOMPLETE);
      handle_tx_commit_result_(tx, OB_TRANS_ROLLBACKED);
      ret = OB_TRANS_ROLLBACKED;
    } else {
      int clean = true;
      ARRAY_FOREACH_X(tx.parts_, i, cnt, clean) {
        clean = tx.parts_[i].is_clean();
      }
      if (clean) {
        // explicit savepoint rollback cause empty valid-part-set
        tx.set_commit_cb(&cb);
        abort_participants_(tx);                  // let part ctx quit
        handle_tx_commit_result_(tx, OB_SUCCESS); // commit success
        ret = OB_SUCCESS;
      }
    }
    break;
  default:
    TRANS_LOG(WARN, "anormaly tx state", K(tx));
    abort_tx_(tx, ObTxAbortCause::IN_CONSIST_STATE);
    handle_tx_commit_result_(tx, OB_TRANS_ROLLBACKED);
    ret = OB_TRANS_ROLLBACKED;
  }
  // normal path, commit cont.
  if (OB_SUCC(ret) && (
      tx.state_ == ObTxDesc::State::ACTIVE ||
      tx.state_ == ObTxDesc::State::IMPLICIT_ACTIVE)) {
    auto state0 = tx.state_;
    tx.state_ = ObTxDesc::State::IN_TERMINATE;
    // record trace_info
    if (OB_NOT_NULL(trace_info) &&
        OB_FAIL(tx.trace_info_.set_app_trace_info(*trace_info))) {
      TRANS_LOG(WARN, "set trace_info failed", K(ret), KPC(trace_info));
    }
480
    SCN commit_version;
W
wangzelin.wzl 已提交
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
    if (OB_SUCC(ret) &&
        OB_FAIL(do_commit_tx_(tx, expire_ts, cb, commit_version))) {
      TRANS_LOG(WARN, "try to commit tx fail, tx will be aborted",
                K(ret), K(expire_ts), K(tx), KP(&cb));
      // the error may caused by txn has terminated
      handle_tx_commit_result_(tx, ret, commit_version);
    }
    // if txn not terminated, it can be choice to abort
    if (OB_FAIL(ret) && tx.state_ == ObTxDesc::State::IN_TERMINATE) {
      tx.state_ = state0;
      abort_tx_(tx, ret);
      handle_tx_commit_result_(tx, OB_TRANS_ROLLBACKED);
      ret = OB_TRANS_ROLLBACKED;
    }
  }

  /* NOTE:
   * to prevent potential deadlock, distinguish the commit
   * completed by current thread from other cases
   */
  bool committed = tx.state_ == ObTxDesc::State::COMMITTED;
  // if tx committed, we should callback immediately
  //
  // NOTE: this must defer to final current function
  // in order to assure there is no access to tx, because
  // after calling the commit_cb, the tx object may be
  // released or reused
  DEFER({
      tx.lock_.unlock();
      if (OB_SUCC(ret) && committed) {
        tx.execute_commit_cb();
      }
    });
#ifndef NDEBUG
  TRANS_LOG(INFO, "submit commit tx", K(ret),
            K(committed), KPC(this), K(tx), K(expire_ts), KP(&cb));
#else
  if (OB_FAIL(ret)) {
    TRANS_LOG(INFO, "submit commit tx fail", K(ret),
              K(committed), KPC(this), K(tx), K(expire_ts), KP(&cb));
  }
#endif
  ObTransTraceLog &tlog = tx.get_tlog();
  const char *trace_info_str = (trace_info == NULL ? NULL : trace_info->ptr());
O
obdev 已提交
525
  REC_TRANS_TRACE_EXT(&tlog, submit_commit_tx, OB_Y(ret), OB_Y(expire_ts),
W
wangzelin.wzl 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
                      OB_ID(tag1), committed,
                      OB_ID(tag2), trace_info_str,
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  return ret;
}

int ObTransService::get_read_snapshot(ObTxDesc &tx,
                                      const ObTxIsolationLevel iso_level,
                                      const int64_t expire_ts,
                                      ObTxReadSnapshot &snapshot)
{
  int ret = OB_SUCCESS;
  ObSpinLockGuard guard(tx.lock_);
  ObTxIsolationLevel isolation = iso_level;
  if (OB_SUCC(tx_sanity_check_(tx))) {
    if (tx.is_in_tx() && isolation != tx.isolation_) {
      //use txn's isolation if txn is active
      isolation = tx.isolation_;
    }
  }
  if (OB_FAIL(ret)) {
  } else if (isolation == ObTxIsolationLevel::SERIAL ||
             isolation == ObTxIsolationLevel::RR) {
      // only acquire snapshot once in these isolation level
    if (tx.isolation_ != isolation /*change isolation*/ ||
552
        !tx.snapshot_version_.is_valid()/*version invalid*/) {
553
      SCN version;
554
      int64_t uncertain_bound = 0;
W
wangzelin.wzl 已提交
555 556
      if (OB_FAIL(sync_acquire_global_snapshot_(tx, expire_ts, version, uncertain_bound))) {
        TRANS_LOG(WARN, "acquire global snapshot fail", K(ret), K(tx));
557 558
      } else if (!tx.tx_id_.is_valid() && OB_FAIL(tx_desc_mgr_.add(tx))) {
        TRANS_LOG(WARN, "add tx to mgr fail", K(ret), K(tx));
W
wangzelin.wzl 已提交
559 560 561 562
      } else {
        tx.snapshot_version_ = version;
        tx.snapshot_uncertain_bound_ = uncertain_bound;
        tx.snapshot_scn_ = ObSequence::get_max_seq_no() + 1;
563
        tx.state_change_flags_.EXTRA_CHANGED_ = true;
W
wangzelin.wzl 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576
      }
    }
    if (OB_SUCC(ret)) {
      tx.isolation_ = isolation;
      snapshot.core_.version_ = tx.snapshot_version_;
      snapshot.uncertain_bound_ = tx.snapshot_uncertain_bound_;
    }
  } else if (OB_FAIL(sync_acquire_global_snapshot_(tx,
                                                   expire_ts,
                                                   snapshot.core_.version_,
                                                   snapshot.uncertain_bound_))) {
    TRANS_LOG(WARN, "acquire global snapshot fail", K(ret), K(tx));
  } else {
577
    if (tx.is_can_elr() && MTL_IS_PRIMARY_TENANT()) {
W
wangzelin.wzl 已提交
578 579 580 581 582 583
      snapshot.core_.version_ = std::max(snapshot.core_.version_, tx_version_mgr_.get_max_commit_ts(true));
    }
  }
  if (OB_SUCC(ret)) {
    snapshot.source_ = ObTxReadSnapshot::SRC::GLOBAL;
    snapshot.parts_.reset();
O
obdev 已提交
584 585
    // If tx id is valid , record tx_id and scn
    if (tx.tx_id_.is_valid()) {
W
wangzelin.wzl 已提交
586 587
      snapshot.core_.tx_id_ = tx.tx_id_;
      snapshot.core_.scn_ = ObSequence::get_max_seq_no();
O
obdev 已提交
588 589
    }
    if (tx.state_ != ObTxDesc::State::IDLE) {
W
wangzelin.wzl 已提交
590 591 592 593 594 595 596 597 598 599
      ARRAY_FOREACH(tx.parts_, i) {
        if (!tx.parts_[i].is_clean() &&
            OB_FAIL(snapshot.parts_.push_back(ObTxLSEpochPair(tx.parts_[i].id_, tx.parts_[i].epoch_)))) {
          TRANS_LOG(WARN, "push snapshot parts fail", K(ret), K(tx), K(snapshot));
        }
      }
    }
    snapshot.valid_ = true;
  }
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
600
  REC_TRANS_TRACE_EXT(&tlog, get_read_snapshot, OB_Y(ret), OB_Y(expire_ts),
601
                      OB_ID(txid), tx.tx_id_,
W
wangzelin.wzl 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
                      OB_ID(isolation_level), (int)isolation,
                      OB_ID(snapshot_source), (int)snapshot.source_,
                      OB_ID(snapshot_version), snapshot.core_.version_,
                      OB_ID(snapshot_txid), snapshot.core_.tx_id_.get_id(),
                      OB_ID(snapshot_scn), snapshot.core_.scn_,
                      OB_ID(trace_id), ObCurTraceId::get_trace_id_str(),
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  return ret;
}

int ObTransService::get_ls_read_snapshot(ObTxDesc &tx,
                                         const ObTxIsolationLevel iso_level,
                                         const share::ObLSID &lsid,
                                         const int64_t expire_ts,
                                         ObTxReadSnapshot &snapshot)
{
  int ret = OB_SUCCESS;
  bool fallback_get_global_snapshot = false;
  // if txn is active use txn's isolation instead
  ObTxIsolationLevel isolation = tx.is_in_tx() ? tx.isolation_ : iso_level;
  if (isolation == ObTxIsolationLevel::SERIAL ||
      isolation == ObTxIsolationLevel::RR) {
    ret = get_read_snapshot(tx, isolation, expire_ts, snapshot);
  } else if (!lsid.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    TRANS_LOG(WARN, "invalid lsid", K(ret), K(lsid));
  } else {
    ObSpinLockGuard guard(tx.lock_);
    if (OB_FAIL(tx_sanity_check_(tx))) {
    } else if (OB_SUCC(acquire_local_snapshot_(lsid, snapshot.core_.version_))) {
      snapshot.source_ = ObTxReadSnapshot::SRC::LS;
      snapshot.snapshot_lsid_ = lsid;
      snapshot.uncertain_bound_ = 0;
      snapshot.parts_.reset();
O
obdev 已提交
637 638
      // If tx id is valid , record tx_id and scn
      if (tx.tx_id_.is_valid()) {
W
wangzelin.wzl 已提交
639 640
        snapshot.core_.tx_id_ = tx.tx_id_;
        snapshot.core_.scn_ = ObSequence::get_max_seq_no();
O
obdev 已提交
641 642
      }
      if (tx.state_ != ObTxDesc::State::IDLE) {
W
wangzelin.wzl 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
        ARRAY_FOREACH(tx.parts_, i) {
          if (tx.parts_[i].id_ == lsid && !tx.parts_[i].is_clean()) {
            if (OB_FAIL(snapshot.parts_.push_back(ObTxLSEpochPair(lsid, tx.parts_[i].epoch_)))) {
              TRANS_LOG(WARN, "push lsid to snapshot.parts fail", K(ret), K(lsid), K(tx));
            }
          }
        }
      }
      snapshot.valid_ = true;
    } else {
      // XXX In standby cluster mode, the failure to call acquire_local_snapshot_ is an
      // normal situation, no error log needs to be printed
      // TRANS_LOG(WARN, "acquire local snapshot fail, fallback gts", K(ret), K(lsid));
      fallback_get_global_snapshot = true;
    }
  }
  if (fallback_get_global_snapshot) {
    ret = get_read_snapshot(tx, isolation, expire_ts, snapshot);
  }
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
663
  REC_TRANS_TRACE_EXT(&tlog, get_ls_read_snapshot, OB_Y(ret), OB_Y(expire_ts),
W
wangzelin.wzl 已提交
664 665 666 667 668 669
                      OB_ID(ls_id), lsid.id(),
                      OB_ID(isolation_level), (int)isolation,
                      OB_ID(snapshot_source), (int)snapshot.source_,
                      OB_ID(snapshot_version), snapshot.core_.version_,
                      OB_ID(snapshot_txid), snapshot.core_.tx_id_.get_id(),
                      OB_ID(snapshot_scn), snapshot.core_.scn_,
670
                      OB_ID(trace_id), ObCurTraceId::get_trace_id_str(),
W
wangzelin.wzl 已提交
671 672 673 674 675 676
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  return ret;
}

int ObTransService::get_read_snapshot_version(const int64_t expire_ts,
677
                                              SCN &snapshot_version)
W
wangzelin.wzl 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690
{
  int ret = OB_SUCCESS;
  int64_t uncertain_bound = 0;

  ret = acquire_global_snapshot__(expire_ts,
                                  0,
                                  snapshot_version,
                                  uncertain_bound,
                                  []() -> bool { return false; });
  return ret;
}

int ObTransService::get_ls_read_snapshot_version(const share::ObLSID &local_ls_id,
691
                                                 SCN &snapshot_version)
W
wangzelin.wzl 已提交
692 693 694 695 696 697
{
  int ret = OB_SUCCESS;
  ret = acquire_local_snapshot_(local_ls_id, snapshot_version);
  return ret;
}

698 699
int ObTransService::get_weak_read_snapshot_version(const int64_t max_read_stale_time,
                                                   SCN &snapshot)
W
wangzelin.wzl 已提交
700 701
{
  int ret = OB_SUCCESS;
O
obdev 已提交
702 703 704 705 706 707 708 709 710
  bool monotinic_read = true;;
    // server weak read version
  if (!ObWeakReadUtil::enable_monotonic_weak_read(tenant_id_)) {
    if (OB_FAIL(GCTX.weak_read_service_->get_server_version(tenant_id_, snapshot))) {
      TRANS_LOG(WARN, "get server read snapshot fail", K(ret), KPC(this));
    }
    monotinic_read = false;
    // wrs cluster version
  } else if (OB_FAIL(GCTX.weak_read_service_->get_cluster_version(tenant_id_, snapshot))) {
W
wangzelin.wzl 已提交
711 712
    TRANS_LOG(WARN, "get weak read snapshot fail", K(ret), KPC(this));
  } else {
713 714 715 716
    // do nothing
  }
  if (OB_SUCC(ret)) {
    const int64_t snapshot_barrier = ObTimeUtility::current_time() - max_read_stale_time;
O
obdev 已提交
717
    if (snapshot.convert_to_ts() < snapshot_barrier) {
W
wangzelin.wzl 已提交
718
      TRANS_LOG(WARN, "weak read snapshot too stale", K(snapshot),
O
obdev 已提交
719
                K(snapshot_barrier), "delta", (snapshot_barrier - snapshot.convert_to_ts()));
W
wangzelin.wzl 已提交
720 721 722
      ret = OB_REPLICA_NOT_READABLE;
    }
  }
O
obdev 已提交
723
  TRANS_LOG(TRACE, "get weak-read snapshot", K(ret), K(snapshot), K(monotinic_read));
W
wangzelin.wzl 已提交
724 725 726 727 728 729
  return ret;
}

int ObTransService::release_snapshot(ObTxDesc &tx)
{
  int ret = OB_SUCCESS;
730
  SCN snapshot;
W
wangzelin.wzl 已提交
731 732 733 734 735 736
  ObSpinLockGuard guard(tx.lock_);
  tx.inc_op_sn();
  if (tx.state_ != ObTxDesc::State::IDLE) {
    ret = OB_NOT_SUPPORTED;
  } else if (ObTxIsolationLevel::SERIAL == tx.isolation_ ||
             ObTxIsolationLevel::RR == tx.isolation_) {
737 738 739
    snapshot = tx.snapshot_version_;
    if (snapshot.is_valid()) {
      tx.snapshot_version_.reset();
W
wangzelin.wzl 已提交
740 741 742 743 744
      tx.snapshot_uncertain_bound_ = 0;
    }
  }
  TRANS_LOG(TRACE, "release snapshot", K(tx), K(snapshot));
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
745
  REC_TRANS_TRACE_EXT(&tlog, release_snapshot, OB_Y(ret), OB_ID(thread_id), GETTID());
W
wangzelin.wzl 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759
  return ret;
}

int ObTransService::register_tx_snapshot_verify(ObTxReadSnapshot &snapshot)
{
  int ret = OB_SUCCESS;
  const auto &tx_id = snapshot.core_.tx_id_;
  if (tx_id.is_valid()) {
    ObTxDesc *tx = NULL;
    if (OB_SUCC(tx_desc_mgr_.get(tx_id, tx))) {
      ObTxSavePoint sp;
      sp.init(&snapshot);
      ObSpinLockGuard guard(tx->lock_);
      if (OB_FAIL(tx_sanity_check_(*tx))) {
760 761
      } else if (!tx->is_in_tx()) {
        // skip register if txn not active
W
wangzelin.wzl 已提交
762 763 764 765 766
      } else if (OB_FAIL(tx->savepoints_.push_back(sp))) {
        TRANS_LOG(WARN, "push back snapshot fail", K(ret),
                  K(snapshot), KPC(tx));
      }
      ObTransTraceLog &tlog = tx->get_tlog();
O
obdev 已提交
767
      REC_TRANS_TRACE_EXT(&tlog, register_snapshot, OB_Y(ret),
W
wangzelin.wzl 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
                          OB_ID(arg), (void*)&snapshot,
                          OB_ID(snapshot_version), snapshot.core_.version_,
                          OB_ID(snapshot_scn), snapshot.core_.scn_,
                          OB_ID(ref), tx->get_ref(),
                          OB_ID(thread_id), GETTID());
    } else if (ret != OB_ENTRY_NOT_EXIST) {
      TRANS_LOG(WARN, "get tx fail", K(tx_id), K(snapshot));
    } else {
      ret = OB_SUCCESS;
    }
    if (OB_NOT_NULL(tx)) {
      tx_desc_mgr_.revert(*tx);
    }
  }
  TRANS_LOG(TRACE, "register snapshot", K(ret), K(snapshot));
  return ret;
}

void ObTransService::unregister_tx_snapshot_verify(ObTxReadSnapshot &snapshot)
{
  int ret = OB_SUCCESS;
  const auto &tx_id = snapshot.core_.tx_id_;
  if (tx_id.is_valid()) {
    ObTxDesc *tx = NULL;
    if (OB_SUCC(tx_desc_mgr_.get(tx_id, tx))) {
      ObSpinLockGuard guard(tx->lock_);
      ARRAY_FOREACH_N(tx->savepoints_, i, cnt) {
        ObTxSavePoint &it = tx->savepoints_[cnt - 1 - i];
        if (it.is_snapshot() && it.snapshot_ == &snapshot) {
          it.release();
          break;
        }
      }
      ObTransTraceLog &tlog = tx->get_tlog();
O
obdev 已提交
802
      REC_TRANS_TRACE_EXT(&tlog, unregister_snapshot, OB_Y(ret),
W
wangzelin.wzl 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
                          OB_ID(arg), (void*)&snapshot,
                          OB_ID(snapshot_version), snapshot.core_.version_,
                          OB_ID(snapshot_scn), snapshot.core_.scn_,
                          OB_ID(ref), tx->get_ref(),
                          OB_ID(thread_id), GETTID());
    }
    if (OB_NOT_NULL(tx)) {
      tx_desc_mgr_.revert(*tx);
    }
  }
  TRANS_LOG(TRACE, "unreigster snapshot", K(ret), K(snapshot));
}

int ObTransService::create_implicit_savepoint(ObTxDesc &tx,
                                              const ObTxParam &tx_param,
                                              int64_t &savepoint,
                                              const bool release)
{
  int ret = OB_SUCCESS;
  ObSpinLockGuard guard(tx.lock_);
  if (!tx_param.is_valid()) {
    // NOTE: tx_param only required for create global implicit_savepoint when txn in IDLE state
    // TODO: rework this interface, allow skip pass tx_param if not required
    ret = OB_INVALID_ARGUMENT;
    TRANS_LOG(WARN, "tx param invalid", K(ret), K(tx_param), K(tx));
  } else if (OB_FAIL(tx_sanity_check_(tx))) {
  } else if (tx.state_ >= ObTxDesc::State::IN_TERMINATE) {
    ret = OB_TRANS_INVALID_STATE;
    TRANS_LOG(WARN, "create implicit savepoint but tx terminated", K(ret), K(tx));
  } else if (tx.flags_.SHADOW_) {
    ret = create_local_implicit_savepoint_(tx, savepoint);
  } else {
    ret = create_global_implicit_savepoint_(tx, tx_param, savepoint, release);
  }
  return ret;
}

int ObTransService::create_local_implicit_savepoint_(ObTxDesc &tx,
                                                     int64_t &savepoint)
{
  int ret = OB_SUCCESS;
  savepoint = ObSequence::inc_and_get_max_seq_no();
  TRANS_LOG(TRACE, "create local implicit savepoint", K(ret), K(savepoint), K(tx));
  ObTransTraceLog &tlog = tx.get_tlog();
  REC_TRANS_TRACE_EXT(&tlog, create_local_implicit_savepoint,
O
obdev 已提交
848
                      OB_Y(ret), OB_Y(savepoint), OB_ID(opid), tx.op_sn_,
W
wangzelin.wzl 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  return ret;
}

int ObTransService::create_global_implicit_savepoint_(ObTxDesc &tx,
                                                      const ObTxParam &tx_param,
                                                      int64_t &savepoint,
                                                      const bool release)
{
  int ret = OB_SUCCESS;
  if (tx.state_ == ObTxDesc::State::IDLE) {
    tx.cluster_version_ = GET_MIN_CLUSTER_VERSION();
    tx.cluster_id_      = tx_param.cluster_id_;
    tx.access_mode_     = tx_param.access_mode_;
    tx.timeout_us_      = tx_param.timeout_us_;
    if (tx.isolation_ != tx_param.isolation_) {
      tx.isolation_ = tx_param.isolation_;
867
      tx.snapshot_version_.reset(); // invalidate previouse snapshot
W
wangzelin.wzl 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    }
  }
  // NOTE: the lock_timeout_us_ can be changed even tx active
  tx.lock_timeout_us_ = tx_param.lock_timeout_us_;

  tx.inc_op_sn();
  savepoint = ObSequence::inc_and_get_max_seq_no();
  if (tx.state_ == ObTxDesc::State::IDLE && !tx.tx_id_.is_valid()) {
    if (tx.has_implicit_savepoint()) {
      ret = OB_TRANS_INVALID_STATE;
      TRANS_LOG(WARN, "has implicit savepoint, but tx_id is invalid", K(ret), K(tx));
    } else if (OB_FAIL(tx_desc_mgr_.add(tx))) {
      TRANS_LOG(WARN, "failed to register with txMgr", K(ret), K(tx));
    }
  }
  if (OB_SUCC(ret)) {
    if (release) {
      tx.release_all_implicit_savepoint();
    }
    tx.add_implicit_savepoint(savepoint);
  }
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
890
  REC_TRANS_TRACE_EXT(&tlog, create_global_implicit_savepoint, OB_Y(ret),
W
wangzelin.wzl 已提交
891
                      OB_ID(txid), tx.tx_id_,
O
obdev 已提交
892
                      OB_Y(savepoint), OB_Y(release),
W
wangzelin.wzl 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
                      OB_ID(opid), tx.op_sn_,
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  TRANS_LOG(TRACE, "create global implicit savepoint", K(ret), K(tx), K(tx_param), K(savepoint));
  return ret;
}

// impl note
// if tx aborted reject with need_rollback
// if tx terminated reject with COMMITTED / ABORTED
// if tx in-commtting reject with has_decided
// if tx in idle:
//    abort tx and reset tx [1]
// if tx in active:
//    normal rollback [2]
// if tx in implicit_active:
//    if tx.active_scn > savepoint:
//       abort tx and reset tx [1]
//    else
//       normal rollback [2]
// [1] abort tx and reset tx
//     re-register with new tx-id
//     state = IDLE
// [2] normal rollback:
//     if rollback failed: abort tx
int ObTransService::rollback_to_implicit_savepoint(ObTxDesc &tx,
                                                   const int64_t savepoint,
                                                   const int64_t expire_ts,
                                                   const share::ObLSArray *extra_touched_ls)
{
  int ret = OB_SUCCESS;
  ObSpinLockGuard guard(tx.lock_);
  if (OB_FAIL(tx_sanity_check_(tx))) {
  } else if (tx.flags_.SHADOW_) {
    if (OB_NOT_NULL(extra_touched_ls)) {
      ret = OB_NOT_SUPPORTED;
      TRANS_LOG(WARN, "rollback on remote only suport collected tx parts",
                K(ret), K(savepoint), K(tx));
    } else {
      ret = rollback_to_local_implicit_savepoint_(tx, savepoint, expire_ts);
    }
  } else {
    ret = rollback_to_global_implicit_savepoint_(tx,
                                                 savepoint,
                                                 expire_ts,
                                                 extra_touched_ls);
  }
  return ret;
}

int ObTransService::rollback_to_local_implicit_savepoint_(ObTxDesc &tx,
                                                          const int64_t savepoint,
                                                          const int64_t expire_ts)
{
  int ret = OB_SUCCESS;
  ObTxPartRefList parts;
  auto start_ts = ObTimeUtility::current_time();
  if (OB_FAIL(find_parts_after_sp_(tx, parts, savepoint))) {
    TRANS_LOG(WARN, "find rollback parts fail", K(ret), K(savepoint), K(tx));
  } else {
    ARRAY_FOREACH(parts, i) {
      ObPartTransCtx *ctx = NULL;
      ObTxPart &p = parts[i];
      if (OB_FAIL(get_tx_ctx_(p.id_, tx.tx_id_, ctx))) {
        TRANS_LOG(WARN, "get tx ctx fail", K(ret), K_(p.id), K(tx));
      } else if (p.epoch_ != ctx->epoch_) {
        ret = OB_TRANS_CTX_NOT_EXIST; // FIXME more decent errno
      } else if (OB_FAIL(ls_sync_rollback_savepoint__(ctx, savepoint, tx.op_sn_, expire_ts))) {
        TRANS_LOG(WARN, "LS rollback savepoint fail", K(ret), K(savepoint), K(tx));
      } else {
        p.last_scn_ = savepoint;
      }
      if (OB_NOT_NULL(ctx)) {
        revert_tx_ctx_(ctx);
      }
    }
  }
  auto elapsed_us = ObTimeUtility::current_time() - start_ts;
#ifndef NDEBUG
  TRANS_LOG(INFO, "rollback local implicit savepoint", K(ret), K(savepoint));
#else
  if (OB_FAIL(ret)) {
    TRANS_LOG(WARN, "rollback local implicit savepoint fail", K(ret), K(savepoint));
  }
#endif
  ObTransTraceLog &tlog = tx.get_tlog();
  REC_TRANS_TRACE_EXT(&tlog, rollback_local_implicit_savepoint,
O
obdev 已提交
980
                      OB_Y(ret), OB_Y(savepoint), OB_Y(expire_ts),
W
wangzelin.wzl 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
                      OB_ID(time_used) , elapsed_us,
                      OB_ID(opid), tx.op_sn_,
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  return ret;
}

int ObTransService::rollback_to_global_implicit_savepoint_(ObTxDesc &tx,
                                                           const int64_t savepoint,
                                                           const int64_t expire_ts,
                                                           const share::ObLSArray *extra_touched_ls)
{
  int ret = OB_SUCCESS;
  auto start_ts = ObTimeUtility::current_time();
  tx.inc_op_sn();
  bool reset_tx = false, normal_rollback = false;
  // merge extra touched ls
O
obdev 已提交
998
  if (OB_NOT_NULL(extra_touched_ls) && !extra_touched_ls->empty()) {
W
wangzelin.wzl 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
    if (OB_FAIL(tx.update_parts(*extra_touched_ls))) {
      TRANS_LOG(WARN, "add tx part with extra_touched_ls fail", K(ret), K(tx), KPC(extra_touched_ls));
      abort_tx_(tx, ret);
    } else {
      TRANS_LOG(INFO, "add tx part with extra_touched_ls", KPC(extra_touched_ls), K_(tx.tx_id));
    }
  }

  if (OB_SUCC(ret)) {
    switch(tx.state_) {
    case ObTxDesc::State::IDLE:
      tx.release_implicit_savepoint(savepoint);
      ret = OB_SUCCESS;
      break;
    case ObTxDesc::State::ACTIVE:
      tx.release_implicit_savepoint(savepoint);
      normal_rollback = true;
      break;
    case ObTxDesc::State::IMPLICIT_ACTIVE:
      tx.release_implicit_savepoint(savepoint);
      if (!tx.has_implicit_savepoint() && tx.active_scn_ >= savepoint) {
        reset_tx = true;
      } else {
        normal_rollback = true;
      }
      break;
    default:
      ret = OB_TRANS_INVALID_STATE; // FIXME, better error code
    }
  }

  if (normal_rollback) {
    ObTxPartRefList parts;
    if (OB_FAIL(ret)) {
    } else if (tx.flags_.PART_EPOCH_MISMATCH_) {
      ret = OB_TRANS_NEED_ROLLBACK;
      TRANS_LOG(WARN, "some participant born epoch mismatch, txn will rollback internally", K(ret));
    } else if (tx.flags_.PARTS_INCOMPLETE_) {
      ret = OB_TRANS_NEED_ROLLBACK;
      TRANS_LOG(WARN, "txn participants set incomplete, txn will rollback internally", K(ret));
    } else if (OB_FAIL(find_parts_after_sp_(tx, parts, savepoint))) {
      TRANS_LOG(WARN, "find rollback parts fail", K(ret), K(tx), K(savepoint));
    } else if (OB_FAIL(rollback_savepoint_(tx,
                                           parts,
                                           savepoint,
                                           expire_ts))) {
      TRANS_LOG(WARN, "do savepoint rollback fail", K(ret));
    }
    if (OB_FAIL(ret)) {
      TRANS_LOG(WARN, "rollback savepoint fail, abort tx",
                K(ret), K(savepoint), KP(extra_touched_ls), K(parts), K(tx));
      abort_tx_(tx, ObTxAbortCause::SAVEPOINT_ROLLBACK_FAIL);
    } else {
      /*
       * advance txn op_seqence to barrier duplicate rollback msg
       * otherwise, rollback may erase following write
       */
      tx.inc_op_sn();
    }
  }
  /*
   * reset tx state from IMPLICIT_ACTIVE to IDLE
   * in progress tx was cleaned up via abort
   * but resources hold before beginning of tx
   * were reserved
   */
  if (reset_tx) {
    if (OB_FAIL(abort_tx_(tx, ObTxAbortCause::IMPLICIT_ROLLBACK,
              false /*don't cleanup resource*/))) {
      TRANS_LOG(WARN, "internal abort tx fail", K(ret), K(tx));
    } else if (OB_FAIL(start_epoch_(tx))) {
      TRANS_LOG(WARN, "switch tx to idle fail", K(ret), K(tx));
    }
  }
  auto elapsed_us = ObTimeUtility::current_time() - start_ts;
#ifndef NDEBUG
  TRANS_LOG(INFO, "rollback to implicit savepoint", K(ret), K(savepoint), K(elapsed_us), K(tx));
#else
  if (OB_FAIL(ret)) {
    TRANS_LOG(WARN, "rollback to implicit savepoint fail", K(ret), K(savepoint), K(elapsed_us), K(tx));
  }
#endif
  ObTransTraceLog &tlog = tx.get_tlog();
  REC_TRANS_TRACE_EXT(&tlog, rollback_global_implicit_savepoint,
O
obdev 已提交
1083
                      OB_Y(ret), OB_Y(savepoint), OB_Y(expire_ts),
W
wangzelin.wzl 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
                      OB_ID(time_used), elapsed_us,
                      OB_ID(arg), (void*)extra_touched_ls,
                      OB_ID(opid), tx.op_sn_,
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  return ret;
}

int ObTransService::ls_sync_rollback_savepoint__(ObPartTransCtx *part_ctx,
                                                 const int64_t savepoint,
                                                 const int64_t op_sn,
                                                 const int64_t expire_ts)
{
  int ret = OB_SUCCESS;
  int64_t retry_cnt = 0;
  bool blockable = expire_ts > 0;
  const int64_t from_scn = ObSequence::inc_and_get_max_seq_no();
  do {
    ret = part_ctx->rollback_to_savepoint(op_sn, from_scn, savepoint);
    if (OB_NEED_RETRY == ret && blockable) {
      if (ObTimeUtility::current_time() >= expire_ts) {
        ret = OB_TIMEOUT;
        TRANS_LOG(WARN, "can not retry rollback_to because of timeout", K(ret), K(retry_cnt));
      } else {
        if (retry_cnt % 5 == 0) {
          TRANS_LOG(WARN, "retry rollback_to savepoint in ctx", K(ret), K(retry_cnt));
        }
        retry_cnt += 1;
        ob_usleep(50 * 1000);
      }
    }
  } while (OB_NEED_RETRY == ret && blockable);
#ifndef NDEBUG
  TRANS_LOG(INFO, "rollback to savepoint sync", K(ret),
            K(part_ctx->get_trans_id()), K(part_ctx->get_ls_id()), K(retry_cnt),
            K(op_sn), K(savepoint), K(expire_ts));
#else
  if (OB_FAIL(ret)) {
    TRANS_LOG(WARN, "rollback to savepoint sync fail", K(ret),
              K(part_ctx->get_trans_id()), K(part_ctx->get_ls_id()), K(retry_cnt),
              K(op_sn), K(savepoint), K(expire_ts));
  }
#endif
  return ret;
}

int ObTransService::create_explicit_savepoint(ObTxDesc &tx, const ObString &savepoint)
{
  int ret = OB_SUCCESS;
  int64_t scn = 0;
  ObSpinLockGuard guard(tx.lock_);
  tx.inc_op_sn();
  scn = ObSequence::inc_and_get_max_seq_no();
  ObTxSavePoint sp;
  if (OB_SUCC(sp.init(scn, savepoint))) {
    if (OB_FAIL(tx.savepoints_.push_back(sp))) {
      TRANS_LOG(WARN, "push savepoint failed", K(ret));
1141 1142 1143
    } else if (!tx.tx_id_.is_valid() && OB_FAIL(tx_desc_mgr_.add(tx))) {
      TRANS_LOG(WARN, "add tx to mgr failed", K(ret), K(tx));
      tx.savepoints_.pop_back();
W
wangzelin.wzl 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    } else {
      // impl move semantic of savepoint
      ARRAY_FOREACH_X(tx.savepoints_, i, cnt, i != cnt - 1) {
        ObTxSavePoint &it = tx.savepoints_.at(cnt - 2 - i);
        if (it.is_stash()) { break; }
        if (it.is_savepoint() && it.name_ == savepoint) {
          TRANS_LOG(TRACE, "move savepoint", K(savepoint), "from", it.scn_, "to", scn, K(tx));
          it.release();
          break; // assume only one if exist
        }
      }
    }
  }
1157
  tx.state_change_flags_.EXTRA_CHANGED_ = true;
W
wangzelin.wzl 已提交
1158 1159
  TRANS_LOG(TRACE, "normal savepoint", K(ret), K(savepoint), K(scn), K(tx));
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
1160
  REC_TRANS_TRACE_EXT(&tlog, create_explicit_savepoint, OB_Y(ret),
W
wangzelin.wzl 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
                      OB_ID(savepoint), savepoint,
                      OB_ID(seq_no), scn,
                      OB_ID(opid), tx.op_sn_,
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  return ret;
}

// impl note
// 1. find savepoint Node
// 3. do rollback savepoint by savepoint No.
// 2. invalidate savepoint and snapshot after the savepoint Node.
int ObTransService::rollback_to_explicit_savepoint(ObTxDesc &tx,
                                                   const ObString &savepoint,
                                                   const int64_t expire_ts)
{
  int ret = OB_SUCCESS;
  auto start_ts = ObTimeUtility::current_time();
  int64_t sp_scn = -1;
  ObSpinLockGuard guard(tx.lock_);
  if (OB_SUCC(tx_sanity_check_(tx))) {
    tx.inc_op_sn();
    ARRAY_FOREACH_N(tx.savepoints_, i, cnt) {
      const ObTxSavePoint &it = tx.savepoints_.at(cnt - 1 - i);
      TRANS_LOG(TRACE, "sp iterate:", K(it));
      if (it.is_stash()) { break; }
      if (it.is_savepoint() && it.name_ == savepoint) {
        sp_scn = it.scn_;
        break;
      }
    }
    if (sp_scn == -1) {
      ret = OB_SAVEPOINT_NOT_EXIST;
      TRANS_LOG(WARN, "savepoint not exist", K(ret), K(savepoint), K_(tx.savepoints));
    }
  }
  if (OB_SUCC(ret)) {
    ObTxPartRefList parts;
    if (OB_FAIL(find_parts_after_sp_(tx, parts, sp_scn))) {
      TRANS_LOG(WARN, "find rollback parts fail", K(ret), K(tx), K(savepoint));
    } else if (OB_FAIL(rollback_savepoint_(tx,
                                           parts,
                                           sp_scn,
                                           expire_ts))) {
      TRANS_LOG(WARN, "do savepoint rollback fail", K(ret));
    }
    if (OB_FAIL(ret)) {
      TRANS_LOG(WARN, "rollback savepoint fail, abort tx",
                K(ret), K(savepoint), K(sp_scn), K(parts), K(tx));
      abort_tx_(tx, ObTxAbortCause::SAVEPOINT_ROLLBACK_FAIL);
    }
  }
  if (OB_SUCC(ret)) {
    // rollback savepoints > sp (note, current savepoint with sp won't be released)
    ARRAY_FOREACH_N(tx.savepoints_, i, cnt) {
      ObTxSavePoint &it = tx.savepoints_.at(cnt - 1 - i);
      if (it.scn_ > sp_scn) {
        it.rollback();
      }
    }
  }
1222
  tx.state_change_flags_.EXTRA_CHANGED_ = true;
W
wangzelin.wzl 已提交
1223 1224
  auto elapsed_us = ObTimeUtility::current_time() - start_ts;
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
1225
  REC_TRANS_TRACE_EXT(&tlog, rollback_explicit_savepoint, OB_Y(ret),
W
wangzelin.wzl 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
                      OB_ID(id), savepoint,
                      OB_ID(savepoint), sp_scn,
                      OB_ID(time_used), elapsed_us,
                      OB_ID(opid), tx.op_sn_,
                      OB_ID(ref), tx.get_ref(),
                      OB_ID(thread_id), GETTID());
  return ret;
}

// impl note
// registered snapshot keep valid
int ObTransService::release_explicit_savepoint(ObTxDesc &tx, const ObString &savepoint)
{
  int ret = OB_SUCCESS;
  bool hit = false;
  int64_t sp_id = 0;
  ObSpinLockGuard guard(tx.lock_);
  if (OB_SUCC(tx_sanity_check_(tx))) {
    tx.inc_op_sn();
    ARRAY_FOREACH_N(tx.savepoints_, i, cnt) {
      ObTxSavePoint &it = tx.savepoints_.at(cnt - 1 - i);
      if (it.is_savepoint() && it.name_ == savepoint) {
        hit = true;
        sp_id = it.scn_;
        break;
      }
      if (it.is_stash()) { break; }
    }
    if (!hit) {
      ret = OB_SAVEPOINT_NOT_EXIST;
      TRANS_LOG(WARN, "release savepoint fail", K(ret), K(savepoint), K(tx));
    } else {
      ARRAY_FOREACH_N(tx.savepoints_, i, cnt) {
        ObTxSavePoint &it = tx.savepoints_.at(cnt - 1 - i);
        if (it.is_savepoint() && it.scn_ >= sp_id) {
          it.release();
        }
      }
      TRANS_LOG(TRACE, "release savepoint", K(savepoint), K(sp_id), K(tx));
    }
  }
1267
  tx.state_change_flags_.EXTRA_CHANGED_ = true;
W
wangzelin.wzl 已提交
1268
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
1269
  REC_TRANS_TRACE_EXT(&tlog, release_explicit_savepoint, OB_Y(ret),
W
wangzelin.wzl 已提交
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
                      OB_ID(savepoint), savepoint,
                      OB_ID(seq_no), sp_id,
                      OB_ID(opid), tx.op_sn_);
  return ret;
}

int ObTransService::create_stash_savepoint(ObTxDesc &tx, const ObString &name)
{
  int ret = OB_SUCCESS;
  ObSpinLockGuard guard(tx.lock_);
  tx.inc_op_sn();
  auto seq_no = ObSequence::inc_and_get_max_seq_no();
  ObTxSavePoint sp;
  if (OB_SUCC(sp.init(seq_no, name, true))) {
    if (OB_FAIL(tx.savepoints_.push_back(sp))) {
      TRANS_LOG(WARN, "push savepoint failed", K(ret));
    }
  }
  TRANS_LOG(TRACE, "create stash savepoint", K(ret), K(seq_no), K(name), K(tx));
O
obdev 已提交
1289
  REC_TRANS_TRACE_EXT(&tx.tlog_, create_stash_savepoint, OB_Y(ret),
W
wangzelin.wzl 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
                      OB_ID(savepoint), name,
                      OB_ID(seq_no), seq_no,
                      OB_ID(opid), tx.op_sn_);
  return ret;
}

// impl note
// fastpath:
//  local call, if NOT_MASTER, fallback slowpath
// slowpath:
//  post msg,
//  abort tx if rollback failed
int ObTransService::rollback_savepoint_(ObTxDesc &tx,
                                        ObTxPartRefList &parts,
                                        const int64_t savepoint,
                                        int64_t expire_ts)
{
  int ret = OB_SUCCESS;
  bool slowpath = true;
  expire_ts = std::min(expire_ts, tx.get_expire_ts());
  if (parts.count() == 0) {
    slowpath = false;
    TRANS_LOG(INFO, "empty rollback participant set", K(tx), K(savepoint));
  } else if (parts.count() == 1 && parts[0].addr_ == self_) {
    slowpath = false;
    ObTxPart &p = parts[0];
    int64_t born_epoch = 0;
    if (OB_FAIL(ls_rollback_to_savepoint_(tx.tx_id_,
                                          p.id_,
                                          p.epoch_,
                                          tx.op_sn_,
                                          savepoint,
                                          born_epoch,
1323
                                          &tx,
W
wangzelin.wzl 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
                                          expire_ts))) {
      if (OB_NOT_MASTER == ret) {
        slowpath = true;
        TRANS_LOG(INFO, "fallback to msg driven rollback", K(ret), K(savepoint), K(p), K(tx));
      } else {
        TRANS_LOG(WARN, "rollback savepoint fail", K(ret), K(savepoint), K(p), K(tx));
      }
    } else {
      if (p.epoch_ <= 0) { p.epoch_ = born_epoch; }
      TRANS_LOG(TRACE, "succ to rollback on participant", K(p), K(tx), K(savepoint));
    }
  }
  if (slowpath &&
      OB_FAIL(rollback_savepoint_slowpath_(tx,
                                           parts,
                                           savepoint,
                                           expire_ts))) {
    TRANS_LOG(WARN, "rollback slowpath fail", K(ret),
              K(parts), K(savepoint), K(expire_ts), K(tx));
  }
  if (OB_TIMEOUT == ret && ObTimeUtility::current_time() >= tx.get_expire_ts()) {
     ret = OB_TRANS_TIMEOUT;
  }
  if (OB_SUCC(ret)) {
    ARRAY_FOREACH(parts, i) {
      parts[i].last_scn_ = savepoint;
    }
  }
  return ret;
}

/**
 * ls_rollback_to_savepoint - rollback savepoint on LogStream
 * @verify_epoch:             used verify tx_ctx's epoch, prevent
 *                            create-crash-recreate
 * @op_sn:                    the operator sequence inner transaction
 *                            used to keep operation order correctly
 * @ctx_born_epoch:           return the ctx's born epoch if created(born)
 * @tx:                       tnx descriptor used to create participant txn ctx
 * @expire_ts:                an expire_ts used if retry was required
 *                            -1 if non-blocking desired
 *
 * Impl Note:
 * if verify_epoch = -1, which means first time to touch the LS
 *    if the tx_ctx not exist, one will be created, and its epoch returned
 * otherwise, tx_ctx's epoch was verified with @verify_epoch
 */
int ObTransService::ls_rollback_to_savepoint_(const ObTransID &tx_id,
                                              const share::ObLSID &ls,
                                              const int64_t verify_epoch,
                                              const int64_t op_sn,
                                              const int64_t savepoint,
                                              int64_t &ctx_born_epoch,
1377
                                              const ObTxDesc *tx,
W
wangzelin.wzl 已提交
1378 1379 1380 1381 1382 1383 1384 1385
                                              int64_t expire_ts)
{
  int ret = OB_SUCCESS;
  int64_t retry_cnt = 0;
  ObPartTransCtx *ctx = NULL;
  if (OB_FAIL(get_tx_ctx_(ls, tx_id, ctx))) {
    if (OB_NOT_MASTER == ret) {
    } else if (OB_TRANS_CTX_NOT_EXIST == ret && verify_epoch <= 0) {
1386 1387
      if (OB_FAIL(create_tx_ctx_(ls, *tx, ctx))) {
        TRANS_LOG(WARN, "create tx ctx fail", K(ret), K(ls), KPC(tx));
W
wangzelin.wzl 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
      }
    } else {
      TRANS_LOG(WARN, "get transaction context error", K(ret), K(tx_id), K(ls));
    }
  }
  if (OB_SUCC(ret)) {
    if (verify_epoch > 0 && ctx->epoch_ != verify_epoch) {
      ret = OB_TRANS_CTX_NOT_EXIST;
      TRANS_LOG(WARN, "current ctx illegal, born epoch not match", K(ret), K(ls), K(tx_id),
                K(verify_epoch), KPC(ctx));
    } else if(OB_FAIL(ls_sync_rollback_savepoint__(ctx, savepoint, op_sn, expire_ts))){
      TRANS_LOG(WARN, "LS rollback to savepoint fail", K(ret), K(tx_id), K(ls), K(op_sn), K(savepoint), KPC(ctx));
    } else if (verify_epoch <= 0) {
      ctx_born_epoch = ctx->epoch_;
    }
  }
  if (OB_NOT_NULL(ctx)) {
    revert_tx_ctx_(ctx);
  }
  return ret;
}

inline int ObTransService::rollback_savepoint_slowpath_(ObTxDesc &tx,
                                                        const ObTxPartRefList &parts,
                                                        const int64_t savepoint,
                                                        const int64_t expire_ts)
{
  int ret = OB_SUCCESS;
  int64_t max_retry_intval = GCONF._ob_trans_rpc_timeout;
  ObSEArray<ObTxLSEpochPair, 4> targets;
  if (OB_FAIL(targets.reserve(parts.count()))) {
    TRANS_LOG(WARN, "reserve space fail", K(ret), K(parts), K(tx));
  } else {
    ARRAY_FOREACH(parts, i) {
      targets.push_back(ObTxLSEpochPair(parts[i].id_, parts[i].epoch_));
    }
    tx.brpc_mask_set_.reset();
    if (OB_FAIL(tx.brpc_mask_set_.init(&targets))) {
      TRANS_LOG(WARN, "init rpc mask set fail", K(ret), K(tx));
    }
  }
  ObTxRollbackSPMsg msg;
  msg.cluster_version_ = tx.cluster_version_;
  msg.tenant_id_ = tx.tenant_id_;
  msg.sender_addr_ = self_;
  msg.sender_ = SCHEDULER_LS;
  msg.cluster_id_ = tx.cluster_id_;
  msg.tx_id_ = tx.tx_id_;
  msg.savepoint_ = savepoint;
  msg.op_sn_ = tx.op_sn_;
  msg.epoch_ = -1;
  msg.request_id_ = tx.op_sn_;
1440 1441 1442 1443 1444 1445
  // prepare msg.tx_ptr_ if required
  // TODO(yunxing.cyx) : in 4.1 rework here, won't serialize txDesc
  ObTxDesc *tmp_tx_desc = NULL;
  ARRAY_FOREACH_NORET(parts, i) {
    if (parts[i].epoch_ <= 0) {
      int64_t len = tx.get_serialize_size() + sizeof(ObTxDesc);
1446
      char *buf = (char*)ob_malloc(len);
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
      int64_t pos = sizeof(ObTxDesc);
      if (OB_FAIL(tx.serialize(buf, len, pos))) {
        TRANS_LOG(WARN, "serialize tx fail", KR(ret), K(tx));
        ob_free(buf);
      } else {
        tmp_tx_desc = new(buf)ObTxDesc();
        pos = sizeof(ObTxDesc);
        if (OB_FAIL(tmp_tx_desc->deserialize(buf, len, pos))) {
          TRANS_LOG(WARN, "deserialize tx fail", KR(ret));
        } else {
          tmp_tx_desc->parts_.reset();
          msg.tx_ptr_ = tmp_tx_desc;
        }
      }
      break;
    }
  }
W
wangzelin.wzl 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
  int64_t start_ts = ObTimeUtility::current_time();
  int retries = 0;
  if (OB_SUCC(ret)) {
    // setup state before release lock
    auto save_state = tx.state_;
    tx.state_ = ObTxDesc::State::ROLLBACK_SAVEPOINT;
    tx.flags_.BLOCK_ = true;
    // release lock before blocking
    tx.lock_.unlock();
    ret = sync_rollback_savepoint__(tx, msg, tx.brpc_mask_set_,
                                    expire_ts, max_retry_intval, retries);
    tx.lock_.lock();
    tx.flags_.BLOCK_ = false;
    // restore state
    tx.state_ = save_state;
    // mask_set need clear
    tx.brpc_mask_set_.reset();
    // clear interrupt flag
    tx.flags_.INTERRUPTED_ = false;
  }
1484 1485 1486 1487 1488
  if (OB_NOT_NULL(tmp_tx_desc)) {
    msg.tx_ptr_ = NULL;
    tmp_tx_desc->~ObTxDesc();
    ob_free(tmp_tx_desc);
  }
W
wangzelin.wzl 已提交
1489 1490 1491 1492 1493
  auto elapsed_us = ObTimeUtility::current_time() - start_ts;
  TRANS_LOG(INFO, "rollback savepoint slowpath", K(ret),
            K_(tx.tx_id), K(start_ts), K(retries),
            K(savepoint), K(expire_ts), K(tx), K(parts.count()));
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
1494 1495
  REC_TRANS_TRACE_EXT(&tlog, rollback_savepoint_slowpath, OB_Y(ret),
                      OB_Y(savepoint), OB_Y(expire_ts),
W
wangzelin.wzl 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
                      OB_ID(retry_cnt), retries,
                      OB_ID(time_used), elapsed_us);
  return ret;
}

inline int ObTransService::sync_rollback_savepoint__(ObTxDesc &tx,
                                                     ObTxRollbackSPMsg &msg,
                                                     const ObTxDesc::MaskSet &mask_set,
                                                     int64_t expire_ts,
                                                     const int64_t max_retry_intval,
                                                     int &retries)
{
  const int64_t MIN_WAIT_TIME = 200 * 1000; // 200 ms
  int ret = OB_SUCCESS;
  int64_t start_ts = ObClockGenerator::getClock();
  retries = 0;
  int64_t min_retry_intval = max_retry_intval / 3;
  expire_ts = std::max(ObTimeUtility::current_time() + MIN_WAIT_TIME, expire_ts);
  while (OB_SUCC(ret)) {
    int64_t retry_intval = std::min(min_retry_intval * (1 + retries), max_retry_intval);
    auto waittime = std::min(expire_ts - ObTimeUtility::current_time(), retry_intval);
    if (waittime <=0) {
      ret = OB_TIMEOUT;
      TRANS_LOG(WARN, "tx rpc wait result timeout", K(ret), K(expire_ts), K(retries));
    } else {
      ObSEArray<ObTxLSEpochPair, 4> remain;
      mask_set.get_not_mask(remain);
      auto remain_cnt = remain.count();
      TRANS_LOG(DEBUG, "unmasked parts", K(remain), K(tx), K(retries));
      if (remain_cnt) {
        tx.rpc_cond_.reset(); /* reset rpc_cond */
        if (OB_FAIL(batch_post_tx_msg_(msg, remain))) {
          TRANS_LOG(WARN, "batch post tx msg fail", K(msg), K(remain), K(retries));
        }
        // wait result
        int rpc_ret = OB_SUCCESS;
        if (OB_FAIL(tx.rpc_cond_.wait(waittime, rpc_ret))) {
          TRANS_LOG(WARN, "tx rpc condition wakeup", K(ret),
                    K(waittime), K(rpc_ret), K(expire_ts), K(remain), K(remain_cnt), K(retries));
          ret = OB_SUCCESS;
        }
        if (OB_SUCCESS != rpc_ret) {
          TRANS_LOG(WARN, "tx rpc fail", K(rpc_ret), K_(tx.tx_id), K(waittime), K(remain), K(remain_cnt), K(retries));
          if (rpc_ret == OB_TRANS_CTX_NOT_EXIST) {
            // participant has quit, may be txn is timeout or other failure occured
            // txn need abort
            ret = tx.is_tx_timeout() ? OB_TRANS_TIMEOUT : OB_TRANS_KILLED;
          } else {
            ret = rpc_ret;
          }
        }
      }
      if (OB_SUCC(ret) && mask_set.is_all_mask()) {
        TRANS_LOG(INFO, "all savepoint rollback succeed", K_(tx.tx_id),
                  K(remain_cnt), K(waittime), K(retries));
        break;
      }
      if (tx.flags_.INTERRUPTED_) {
        ret = OB_ERR_INTERRUPTED;
        TRANS_LOG(WARN, "rollback was interrupted", K_(tx.tx_id),
                  K(remain_cnt), K(waittime), K(retries));
      }
    }
    ++retries;
  }
  return ret;
}

int ObTransService::merge_tx_state(ObTxDesc &to, const ObTxDesc &from)
{
  TRANS_LOG(TRACE, "merge_tx_state", K(to), K(from));
  int ret = to.merge_exec_info_with(from);
  ObTransTraceLog &tlog = to.get_tlog();
O
obdev 已提交
1569
  REC_TRANS_TRACE_EXT(&tlog, merge_tx_state, OB_Y(ret),
W
wangzelin.wzl 已提交
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
                      OB_ID(to), (void*)&to,
                      OB_ID(from), (void*)&from,
                      OB_ID(opid), to.op_sn_,
                      OB_ID(thread_id), GETTID());
  return ret;
}
int ObTransService::get_tx_exec_result(ObTxDesc &tx, ObTxExecResult &exec_info)
{
  TRANS_LOG(TRACE, "get_tx_exec_result", K(tx), K(exec_info));
  int ret = tx.get_inc_exec_info(exec_info);
  return ret;
}
int ObTransService::add_tx_exec_result(ObTxDesc &tx, const ObTxExecResult &exec_info)
{
  TRANS_LOG(TRACE, "add_tx_exec_result", K(tx), K(exec_info));
  int ret = tx.add_exec_info(exec_info);
  ObTransTraceLog &tlog = tx.get_tlog();
  REC_TRANS_TRACE_EXT(&tlog, add_tx_exec_result, OB_ID(opid), tx.op_sn_,
                      OB_ID(num), exec_info.parts_.count(),
                      OB_ID(flag), exec_info.incomplete_,
                      OB_ID(thread_id), GETTID());
  return ret;
}

/*
 * tx_post_terminate - cleanup resource after tx terminated
 *
 * after tx committed/aborted/rollbacked
 * tx resource need to be released, we do it here
 */
void ObTransService::tx_post_terminate_(ObTxDesc &tx)
{
  // invalid registered snapshot
  if (tx.state_ == ObTxDesc::State::ABORTED ||
      tx.is_commit_unsucc()
      // FIXME: (yunxing.cyx)
      // bellow line is temproary, when storage layer support
      // record txn-id in SSTable's MvccRow, we can remove this
      // (the support was planed in v4.1)
      || tx.state_ == ObTxDesc::State::COMMITTED
      ) {
    invalid_registered_snapshot_(tx);
  } else if (tx.state_ == ObTxDesc::State::COMMITTED) {
    // cleanup snapshot's participant info, so that they will skip
    // verify participant txn ctx, which cause false negative,
    // because txn ctx has quit when txn committed.
    registered_snapshot_clear_part_(tx);
  }
  // statistic
  if (tx.is_tx_end()) {
    if (tx.is_committed()) {
      TX_STAT_COMMIT_INC;
      TX_STAT_COMMIT_TIME_USED(tx.finish_ts_ - tx.commit_ts_);
    }
    else if (tx.is_rollbacked()) {
      TX_STAT_ROLLBACK_INC;
      if (tx.commit_ts_ > 0) {
        TX_STAT_ROLLBACK_TIME_USED(MAX(0, tx.finish_ts_ - tx.commit_ts_));
      }
    }
    else {
      // TODO: COMMIT_UNKNOWN, COMMIT_TIMEOUT
    }
    switch(tx.parts_.count()) {
    case 0:  TX_STAT_READONLY_INC break;
    case 1:  TX_STAT_LOCAL_INC break;
    default: TX_STAT_DIST_INC;
    }
    if (tx.active_ts_ > 0) { // skip txn has not active
      if (tx.finish_ts_ <= 0) {
1640
        TRANS_LOG_RET(WARN, OB_ERR_UNEXPECTED, "tx finish ts is unset", K(tx));
W
wangzelin.wzl 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649
      } else if (tx.finish_ts_ > tx.active_ts_) {
        TX_STAT_TIME_USED(tx.finish_ts_ - tx.active_ts_);
      }
    }
  }
  // release all savepoints
  tx.min_implicit_savepoint_ = INT64_MAX;
  tx.savepoints_.reset();
  // reset snapshot
1650
  tx.snapshot_version_.reset();
W
wangzelin.wzl 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
  tx.snapshot_scn_ = 0;
}

int ObTransService::start_epoch_(ObTxDesc &tx)
{
  int ret = OB_SUCCESS;
  if (!tx.is_terminated()) {
    ret = OB_TRANS_INVALID_STATE;
    TRANS_LOG(WARN, "unexpected tx state to start new epoch", K(ret), K(tx));
  } else {
    tx.inc_op_sn();
    if (tx.flags_.RELEASED_) {
      ret = OB_ERR_UNEXPECTED;
      TRANS_LOG(ERROR, "tx released, cannot start new epoch", K(ret), K(tx));
    } else if (OB_FAIL(tx_desc_mgr_.remove(tx))) {
      TRANS_LOG(WARN, "unregister tx fail", K(ret), K(tx));
    } else if (OB_FAIL(tx.switch_to_idle())) {
      TRANS_LOG(WARN, "switch to idlefail", K(ret), K(tx));
    }
    TRANS_LOG(INFO, "tx start new epoch", K(ret), K(tx));
  }
  ObTransTraceLog &tlog = tx.get_tlog();
O
obdev 已提交
1673
  REC_TRANS_TRACE_EXT(&tlog, start_epoch, OB_Y(ret), OB_ID(opid), tx.op_sn_);
W
wangzelin.wzl 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
  return ret;
}

int ObTransService::release_tx_ref(ObTxDesc &tx)
{
  return tx_desc_mgr_.release_tx_ref(&tx);
}

OB_INLINE int ObTransService::tx_sanity_check_(const ObTxDesc &tx)
{
  int ret = OB_SUCCESS;
  if (tx.expire_ts_ <= ObClockGenerator::getClock()) {
    TX_STAT_TIMEOUT_INC
    ret = OB_TRANS_TIMEOUT;
  } else if (tx.flags_.BLOCK_) {
    ret = OB_NOT_SUPPORTED;
    TRANS_LOG(WARN, "tx is blocked in other busy work", K(ret), K(tx));
  } else {
    switch(tx.state_) {
    case ObTxDesc::State::IDLE:
    case ObTxDesc::State::ACTIVE:
    case ObTxDesc::State::IMPLICIT_ACTIVE:

      break;
    case ObTxDesc::State::ABORTED:
      ret = tx.abort_cause_ < 0 ? tx.abort_cause_ : OB_TRANS_NEED_ROLLBACK;
      break;
    case ObTxDesc::State::COMMITTED:
      ret = OB_TRANS_COMMITED;
      break;
    case ObTxDesc::State::ROLLED_BACK:
      ret = OB_TRANS_ROLLBACKED;
      break;
    case ObTxDesc::State::COMMIT_TIMEOUT:
    case ObTxDesc::State::COMMIT_UNKNOWN:
      ret = OB_TRANS_HAS_DECIDED;
      break;
    default:
      ret = OB_NOT_SUPPORTED; // FIXME: refine errno
    }
  }
  if (OB_FAIL(ret)) {
    TRANS_LOG(WARN, "tx state insanity", K(ret), K(tx));
    tx.print_trace_();
  }
  return ret;
}

int ObTransService::is_tx_active(const ObTransID &tx_id, bool &active)
{
  int ret = OB_SUCCESS;
  ObTxDesc *tx = NULL;
  if (OB_SUCC(tx_desc_mgr_.get(tx_id, tx))) {
    if (tx->state_ == ObTxDesc::State::IDLE ||
        tx->state_ == ObTxDesc::State::IMPLICIT_ACTIVE ||
        tx->state_ == ObTxDesc::State::ACTIVE) {
      active = true;
    } else {
      active = false;
    }
  } else if (OB_ENTRY_NOT_EXIST == ret) {
    ret = OB_SUCCESS;
    active = false;
  }
  if (OB_NOT_NULL(tx)) {
    tx_desc_mgr_.revert(*tx);
  }
  return ret;
}
1743
int ObTransService::sql_stmt_start_hook(const ObXATransID &xid, ObTxDesc &tx, const uint32_t session_id)
W
wangzelin.wzl 已提交
1744 1745 1746
{
  int ret = OB_SUCCESS;
  if (tx.is_xa_trans()) {
1747 1748 1749 1750 1751 1752 1753 1754 1755
    // xa txn execute stmt on non xa-start node
    // register tx before execute stmt
    bool registed = false;
    if (tx.xa_start_addr_ != self_ && tx.addr_ != self_) {
      if (OB_FAIL(tx_desc_mgr_.add_with_txid(tx.tx_id_, tx))) {
        TRANS_LOG(WARN, "register tx fail", K(ret), K_(tx.tx_id), K(xid), KP(&tx));
      } else { registed = true; }
    }
    if (OB_SUCC(ret) && OB_FAIL(MTL(ObXAService*)->start_stmt(xid, tx))) {
W
wangzelin.wzl 已提交
1756
      TRANS_LOG(WARN, "xa trans start stmt failed", K(ret), K_(tx.xid), K(xid));
O
obdev 已提交
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
      ObGlobalTxType global_tx_type = tx.get_global_tx_type(xid);
      if (ObGlobalTxType::DBLINK_TRANS == global_tx_type && OB_TRANS_XA_BRANCH_FAIL == ret) {
        // if dblink trans, change errno (branch fail) to the errno of plain trans
        if (OB_FAIL(tx_sanity_check_(tx))) {
          TRANS_LOG(WARN, "tx state insanity", K(ret), K(global_tx_type), K(xid));
        } else {
          // if success, set ret to rollback
          ret = OB_TRANS_NEED_ROLLBACK;
          TRANS_LOG(WARN, "need rollback", K(ret), K(global_tx_type), K(xid));
        }
      }
1768 1769
    } else {
      tx.set_sessid(session_id);
W
wangzelin.wzl 已提交
1770
    }
1771 1772 1773
    if (OB_FAIL(ret) && registed) {
      tx_desc_mgr_.remove(tx);
    }
1774
    TRANS_LOG(INFO, "xa trans start stmt", K_(tx.xid), K(xid), K_(tx.sess_id), K(session_id));
W
wangzelin.wzl 已提交
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
  }
  return ret;
}

int ObTransService::sql_stmt_end_hook(const ObXATransID &xid, ObTxDesc &tx)
{
  int ret = OB_SUCCESS;
  if (tx.is_xa_trans()) {
    // need xid from session
    if (OB_FAIL(MTL(ObXAService*)->end_stmt(xid, tx))) {
      TRANS_LOG(WARN, "xa trans end stmt failed", K(ret), K_(tx.xid), K(xid));
O
obdev 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
      ObGlobalTxType global_tx_type = tx.get_global_tx_type(xid);
      if (ObGlobalTxType::DBLINK_TRANS == global_tx_type && OB_TRANS_XA_BRANCH_FAIL == ret) {
        // if dblink trans, change errno (branch fail) to the errno of plain trans
        if (OB_FAIL(tx_sanity_check_(tx))) {
          TRANS_LOG(WARN, "tx state insanity", K(ret), K(global_tx_type), K(xid));
        } else {
          // if success, set ret to rollback
          ret = OB_TRANS_NEED_ROLLBACK;
          TRANS_LOG(WARN, "need rollback", K(ret), K(global_tx_type), K(xid));
        }
      }
W
wangzelin.wzl 已提交
1797
    }
1798 1799 1800 1801 1802
    // deregister from tx_desc_mgr to prevent
    // conflict with resumed xa branch execute
    if (tx.xa_start_addr_ != self_ && tx.addr_ != self_ ) {
      tx_desc_mgr_.remove(tx);
    }
W
wangzelin.wzl 已提交
1803 1804 1805 1806 1807 1808
    TRANS_LOG(INFO, "xa trans end stmt", K_(tx.xid), K(xid));
  }
  return ret;
}
} // transaction
} // namespace
1809
#undef TXN_API_SANITY_CHECK_FOR_TXN_FREE_ROUTE