ob_replay_status.cpp 47.0 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
/**
 * 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_replay_status.h"
#include "ob_log_replay_service.h"
#include "logservice/ob_log_base_type.h"
#include "logservice/palf/palf_env.h"
#include "lib/stat/ob_session_stat.h"
#include "share/ob_errno.h"

namespace oceanbase
{
using namespace common;
using namespace palf;
24
using namespace share;
W
wangzelin.wzl 已提交
25 26 27 28
namespace logservice
{
//---------------ObReplayServiceTask---------------//
ObReplayServiceTask::ObReplayServiceTask()
29
  : lock_(common::ObLatchIds::REPLAY_STATUS_TASK_LOCK),
W
wangzelin.wzl 已提交
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
    type_(ObReplayServiceTaskType::INVALID_LOG_TASK),
    replay_status_(NULL),
    lease_()
{
  reset();
}

ObReplayServiceTask::~ObReplayServiceTask()
{
  destroy();
}

void ObReplayServiceTask::reset()
{
  //考虑到reuse需求, lease不能重置, 否则可能出现同一个任务被push多次
  enqueue_ts_ = 0;
  err_info_.reset();
}

void ObReplayServiceTask::destroy()
{
  reset();
  type_ = ObReplayServiceTaskType::INVALID_LOG_TASK;
  replay_status_ = NULL;
}

void ObReplayServiceTask::TaskErrInfo::reset()
{
  has_fatal_error_ = false;
  ret_code_ = common::OB_SUCCESS;
  fail_ts_ = 0;
  fail_cost_ = 0;
}

void ObReplayServiceTask::clear_err_info(const int64_t cur_ts)
{
  err_info_.has_fatal_error_ = false;
  err_info_.ret_code_ = common::OB_SUCCESS;
  if (0 != err_info_.fail_ts_) {
    err_info_.fail_cost_ += (cur_ts - err_info_.fail_ts_);
    err_info_.fail_ts_ = 0;
  }
}

void ObReplayServiceTask::set_simple_err_info(const int ret_code,
                                              const int64_t fail_ts)
{
  err_info_.ret_code_ = ret_code;
  if (0 == err_info_.fail_ts_) {
    err_info_.fail_ts_ = fail_ts;
  }
}
void ObReplayServiceTask::set_fatal_err_info(const int ret_code,
                                             const int64_t fail_ts)
{
  err_info_.has_fatal_error_ = true;
  err_info_.ret_code_ = ret_code;
  err_info_.fail_ts_ = fail_ts;
}

bool ObReplayServiceTask::need_replay_immediately() const
{
  return (OB_SUCCESS == err_info_.ret_code_
          || OB_EAGAIN == err_info_.ret_code_);
}

//---------------ObReplayServiceSubmitTask---------------//
int ObReplayServiceSubmitTask::init(const palf::LSN &base_lsn,
98
                                    const SCN &base_scn,
W
wangzelin.wzl 已提交
99 100 101 102 103 104 105 106 107 108
                                    PalfHandle *palf_handle,
                                    ObReplayStatus *replay_status)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  if (OB_ISNULL(replay_status) || OB_ISNULL(palf_handle)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(type_), K(ret), K(replay_status), K(palf_handle));
  } else if (OB_FAIL(palf_handle->seek(base_lsn, iterator_))) {
    CLOG_LOG(WARN, "seek iterator failed", KR(ret), K(type_), K(palf_handle), K(base_lsn));
O
obdev 已提交
109
  } else if (OB_UNLIKELY(!base_scn.is_valid())) {
W
wangzelin.wzl 已提交
110
    ret = OB_ERR_UNEXPECTED;
O
obdev 已提交
111
    CLOG_LOG(ERROR, "base_scn is invalid", K(type_), K(base_lsn), K(base_scn), KR(ret));
W
wangzelin.wzl 已提交
112 113 114 115
  } else {
    replay_status_ = replay_status;
    committed_end_lsn_ = base_lsn;
    next_to_submit_lsn_ = base_lsn;
O
obdev 已提交
116
    next_to_submit_scn_.set_min();
W
wangzelin.wzl 已提交
117
    base_lsn_ = base_lsn;
O
obdev 已提交
118
    base_scn_ = base_scn;
W
wangzelin.wzl 已提交
119 120 121 122 123 124
    type_ = ObReplayServiceTaskType::SUBMIT_LOG_TASK;
    if (OB_SUCCESS != (tmp_ret = iterator_.next())) {
      // 在没有写入的情况下有可能已经到达边界
      CLOG_LOG(WARN, "iterator next failed", K(iterator_), K(tmp_ret));
    }
    CLOG_LOG(INFO, "submit log task init success", K(type_), K(next_to_submit_lsn_), K(committed_end_lsn_),
O
obdev 已提交
125
               K(next_to_submit_scn_), K(replay_status_), K(ret));
W
wangzelin.wzl 已提交
126 127 128 129 130 131 132 133 134
  }
  return ret;
}

void ObReplayServiceSubmitTask::reset()
{
  ObLockGuard<ObSpinLock> guard(lock_);
  next_to_submit_lsn_.reset();
  committed_end_lsn_.reset();
O
obdev 已提交
135
  next_to_submit_scn_.reset();
W
wangzelin.wzl 已提交
136
  base_lsn_.reset();
O
obdev 已提交
137
  base_scn_.reset();
W
wangzelin.wzl 已提交
138 139 140 141 142 143 144 145 146 147 148
  ObReplayServiceTask::reset();
}

void ObReplayServiceSubmitTask::destroy()
{
  reset();
  //iterator不支持reset语义,不能在后续可能重用的接口中调用destroy接口
  iterator_.destroy();
  ObReplayServiceTask::destroy();
}

O
obdev 已提交
149
int ObReplayServiceSubmitTask::get_next_to_submit_log_info(LSN &lsn, SCN &scn) const
W
wangzelin.wzl 已提交
150 151
{
  ObLockGuard<ObSpinLock> guard(lock_);
O
obdev 已提交
152
  return get_next_to_submit_log_info_(lsn, scn);
W
wangzelin.wzl 已提交
153 154
}

O
obdev 已提交
155
int ObReplayServiceSubmitTask::get_next_to_submit_log_info_(LSN &lsn, SCN &scn) const
W
wangzelin.wzl 已提交
156 157
{
  int ret = OB_SUCCESS;
O
obdev 已提交
158 159
  lsn.val_ = ATOMIC_LOAD(&next_to_submit_lsn_.val_);
  scn = next_to_submit_scn_.atomic_load();
W
wangzelin.wzl 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  return ret;
}

int ObReplayServiceSubmitTask::get_committed_end_lsn(LSN &lsn) const
{
  int ret = OB_SUCCESS;
  lsn = committed_end_lsn_;
  return ret;
}

int ObReplayServiceSubmitTask::get_base_lsn(LSN &lsn) const
{
  ObLockGuard<ObSpinLock> guard(lock_);
  return get_base_lsn_(lsn);
}

int ObReplayServiceSubmitTask::get_base_lsn_(LSN &lsn) const
{
  int ret = OB_SUCCESS;
  lsn = base_lsn_;
  return ret;
}

O
obdev 已提交
183
int ObReplayServiceSubmitTask::get_base_scn(SCN &scn) const
W
wangzelin.wzl 已提交
184 185
{
  ObLockGuard<ObSpinLock> guard(lock_);
O
obdev 已提交
186
  return get_base_scn_(scn);
W
wangzelin.wzl 已提交
187 188
}

O
obdev 已提交
189
int ObReplayServiceSubmitTask::get_base_scn_(SCN &scn) const
W
wangzelin.wzl 已提交
190 191
{
  int ret = OB_SUCCESS;
O
obdev 已提交
192
  scn = base_scn_;
W
wangzelin.wzl 已提交
193 194 195
  return ret;
}

O
obdev 已提交
196 197
bool ObReplayServiceSubmitTask::has_remained_submit_log(const SCN &replayable_point,
                                                        bool &iterate_end_by_replayable_point)
W
wangzelin.wzl 已提交
198
{
O
obdev 已提交
199 200
  // next接口只在submit任务中单线程调用,和reset接口通过replay status的大锁互斥
  // 故此处不需要submit task锁保护
W
wangzelin.wzl 已提交
201
  if (false == iterator_.is_valid()) {
O
obdev 已提交
202 203
    // maybe new logs is written after last check
    next_log(replayable_point, iterate_end_by_replayable_point);
W
wangzelin.wzl 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  }
  return iterator_.is_valid();
}

//只有padding日志的场景才能调用此接口
int ObReplayServiceSubmitTask::update_next_to_submit_lsn(const LSN &lsn)
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(update_next_to_submit_lsn_(lsn))) {
    CLOG_LOG(WARN, "failed to update_next_to_submit_lsn", KR(ret), K(lsn), K(next_to_submit_lsn_));
  } else {
    //do nothing
  }
  return ret;
}

O
obdev 已提交
220 221
int ObReplayServiceSubmitTask::update_submit_log_meta_info(const LSN &lsn,
                                                           const SCN &scn)
W
wangzelin.wzl 已提交
222 223 224 225
{
  ObLockGuard<ObSpinLock> guard(lock_);
  int ret = OB_SUCCESS;
  if (OB_FAIL(update_next_to_submit_lsn_(lsn))
O
obdev 已提交
226 227
      || OB_FAIL(update_next_to_submit_scn_(SCN::scn_inc(scn)))) {
    CLOG_LOG(ERROR, "failed to update_submit_log_meta_info", KR(ret), K(lsn), K(scn),
O
obdev 已提交
228
             K(next_to_submit_lsn_), K(next_to_submit_scn_));
W
wangzelin.wzl 已提交
229
  } else {
O
obdev 已提交
230 231
    CLOG_LOG(TRACE, "update_submit_log_meta_info", KR(ret), K(lsn), K(scn),
             K(next_to_submit_lsn_), K(next_to_submit_scn_), K(iterator_));
W
wangzelin.wzl 已提交
232 233 234 235
  }
  return ret;
}

O
obdev 已提交
236
int ObReplayServiceSubmitTask::update_committed_end_lsn(const LSN &lsn)
W
wangzelin.wzl 已提交
237 238 239 240 241 242 243 244 245
{
  return update_committed_end_lsn_(lsn);
}

int ObReplayServiceSubmitTask::update_committed_end_lsn_(const LSN &lsn)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(lsn <= committed_end_lsn_)) {
    ret = OB_INVALID_ARGUMENT;
O
obdev 已提交
246
    CLOG_LOG(WARN, "update_committed_end_lsn invalid argument", K(type_), K(lsn),
W
wangzelin.wzl 已提交
247 248 249 250 251 252 253
               K(committed_end_lsn_), K(ret));
  } else {
    committed_end_lsn_ = lsn;
  }
  return ret;
}

O
obdev 已提交
254 255 256 257 258 259 260
int ObReplayServiceSubmitTask::set_committed_end_lsn(const LSN &lsn)
{
  int ret = OB_SUCCESS;
  ATOMIC_SET(&committed_end_lsn_.val_, lsn.val_);
  return ret;
}

O
obdev 已提交
261
int ObReplayServiceSubmitTask::need_skip(const SCN &scn, bool &need_skip)
W
wangzelin.wzl 已提交
262 263
{
  int ret = OB_SUCCESS;
O
obdev 已提交
264
  if (OB_UNLIKELY(!base_scn_.is_valid())) {
W
wangzelin.wzl 已提交
265 266
    ret = OB_ERR_UNEXPECTED;
  } else {
O
obdev 已提交
267
    need_skip = scn < base_scn_;
W
wangzelin.wzl 已提交
268 269 270 271 272
  }
  return ret;
}


O
obdev 已提交
273
int ObReplayServiceSubmitTask::get_log(const char *&buffer, int64_t &nbytes, SCN &scn,
274
                                       palf::LSN &offset, bool &is_raw_write)
W
wangzelin.wzl 已提交
275
{
O
obdev 已提交
276
  return iterator_.get_entry(buffer, nbytes, scn, offset, is_raw_write);
W
wangzelin.wzl 已提交
277 278
}

O
obdev 已提交
279 280
int ObReplayServiceSubmitTask::next_log(const SCN &replayable_point,
                                        bool &iterate_end_by_replayable_point)
W
wangzelin.wzl 已提交
281
{
O
obdev 已提交
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
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  SCN next_min_scn;
  if (OB_SUCCESS != (tmp_ret = iterator_.next(replayable_point, next_min_scn,
                                              iterate_end_by_replayable_point))) {
    if (OB_ITER_END == tmp_ret) {
      if (next_min_scn == next_to_submit_scn_
          || !replayable_point.is_valid()) {
        // do nothing
      } else if (!next_min_scn.is_valid()) {
        // should only occurs when palf has no log
        CLOG_LOG(INFO, "next_min_scn is invalid", K(type_), K(replayable_point),
                 K(next_min_scn), K(next_to_submit_scn_), K(ret), K(iterator_));
      } else if (OB_UNLIKELY(next_min_scn < next_to_submit_scn_)) {
        ret = OB_ERR_UNEXPECTED;
        LSN unused_lsn;
        // updating next to submit log info is failed, set fatal error for replay status.
        replay_status_->set_err_info(unused_lsn, next_min_scn, ObLogBaseType::INVALID_LOG_BASE_TYPE,
                                     0, true, ObClockGenerator::getClock(), ret);
        CLOG_LOG(ERROR, "failed to update next_to_submit_scn_", K(type_), K(replayable_point),
                 K(next_min_scn), K(next_to_submit_scn_), K(ret), K(iterator_));
      } else {
        next_to_submit_scn_ = next_min_scn;
        CLOG_LOG(INFO, "update next_to_submit_scn_", K(type_), K(replayable_point),
                 K(next_min_scn), K(next_to_submit_scn_), K(ret), K(iterator_));
      }
    } else {
      // ignore other err ret of iterator
    }
  } else {
    // do nothing
  }
  return ret;
W
wangzelin.wzl 已提交
315 316
}

317
int ObReplayServiceSubmitTask::reset_iterator(PalfHandle &palf_handle, const LSN &begin_lsn)
W
wangzelin.wzl 已提交
318 319 320 321 322 323 324 325 326 327 328 329
{
  int ret = OB_SUCCESS;
  next_to_submit_lsn_ = std::max(next_to_submit_lsn_, begin_lsn);
  if (OB_FAIL(palf_handle.seek(next_to_submit_lsn_, iterator_))) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(WARN, "seek interator failed", K(type_), K(begin_lsn), K(ret));
  } else if (OB_FAIL(iterator_.next())) {
    CLOG_LOG(WARN, "iterator next failed", K(type_), K(begin_lsn), K(ret));
  }
  return ret;
}

O
obdev 已提交
330
int ObReplayServiceSubmitTask::update_next_to_submit_scn_(const SCN &scn)
W
wangzelin.wzl 已提交
331 332
{
  int ret = OB_SUCCESS;
O
obdev 已提交
333
  if (OB_UNLIKELY(scn <= next_to_submit_scn_)) {
W
wangzelin.wzl 已提交
334
    ret = OB_INVALID_ARGUMENT;
O
obdev 已提交
335 336
    CLOG_LOG(WARN, "update_next_to_submit_scn invalid argument", K(type_), K(scn),
               K(next_to_submit_scn_), K(ret));
W
wangzelin.wzl 已提交
337
  } else {
O
obdev 已提交
338
    next_to_submit_scn_ = scn;
W
wangzelin.wzl 已提交
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 373 374 375 376 377 378 379 380 381 382 383 384 385
  }
  return ret;
}

int ObReplayServiceSubmitTask::update_next_to_submit_lsn_(const LSN &lsn)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(lsn <= next_to_submit_lsn_)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "update_next_to_submit_lsn invalid argument", K(type_), K(lsn),
               K(next_to_submit_lsn_), K(ret));
  } else {
    next_to_submit_lsn_ = lsn;
  }
  return ret;
}

//---------------ObReplayServiceReplayTask---------------//
int ObReplayServiceReplayTask::init(ObReplayStatus *replay_status,
                                    const int64_t idx)
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(replay_status) || idx < 0) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(type_), KP(replay_status), K(ret), K(idx));
  } else {
    replay_status_ = replay_status;
    idx_ = idx;
    type_ = ObReplayServiceTaskType::REPLAY_LOG_TASK;
    CLOG_LOG(INFO, "ObReplayServiceReplayTask init success", K(type_), K(replay_status_), K(idx_));
  }
  return ret;
}

void ObReplayServiceReplayTask::reset()
{
  //attention: type_ and replay_status_ can not be reset
  ObLink *top_item = NULL;
  if (NULL != replay_status_) {
    ObLockGuard<ObSpinLock> guard(lock_);
    while (NULL != (top_item = pop_()))
    {
      ObLogReplayTask *replay_task = static_cast<ObLogReplayTask *>(top_item);
      //此处一定只能让引用计数归零的任务释放log_buff
      if (replay_task->is_pre_barrier_) {
        ObLogReplayBuffer *replay_buf = static_cast<ObLogReplayBuffer *>(replay_task->log_buf_);
        if (NULL == replay_buf) {
386
          CLOG_LOG_RET(ERROR, OB_ERR_UNEXPECTED, "replay_buf is NULL when reset", KPC(replay_task));
W
wangzelin.wzl 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
        } else if (0 == replay_buf->dec_replay_ref()) {
          replay_status_->free_replay_task_log_buf(replay_task);
          replay_status_->dec_pending_task(replay_task->log_size_);
        }
      } else {
        replay_status_->dec_pending_task(replay_task->log_size_);
      }
      replay_status_->free_replay_task(replay_task);
    };
  }
  idx_ = -1;
  ObReplayServiceTask::reset();
}

void ObReplayServiceReplayTask::destroy()
{
  reset();
  ObReplayServiceTask::destroy();
}

int64_t ObReplayServiceReplayTask::idx() const
{
  return idx_;
}

O
obdev 已提交
412 413 414 415 416 417 418 419
int ObReplayServiceReplayTask::get_min_unreplayed_log_info(LSN &lsn,
                                                           SCN &scn,
                                                           int64_t &replay_hint,
                                                           ObLogBaseType &log_type,
                                                           int64_t &first_handle_ts,
                                                           int64_t &replay_cost,
                                                           int64_t &retry_cost,
                                                           bool &is_queue_empty)
W
wangzelin.wzl 已提交
420 421 422 423 424 425 426
{
  int ret = OB_SUCCESS;
  ObLockGuard<ObSpinLock> guard(lock_);
  ObLogReplayTask *replay_task = NULL;
  ObLink *top_item = top();
  if (NULL != top_item && NULL != (replay_task = static_cast<ObLogReplayTask *>(top_item))) {
    lsn = replay_task->lsn_;
O
obdev 已提交
427
    scn = replay_task->scn_;
O
obdev 已提交
428 429 430 431 432
    replay_hint = replay_task->replay_hint_;
    log_type = replay_task->log_type_;
    first_handle_ts = replay_task->first_handle_ts_;
    replay_cost = replay_task->replay_cost_;
    retry_cost = replay_task->retry_cost_;
W
wangzelin.wzl 已提交
433 434 435 436 437 438 439
    is_queue_empty = false;
  } else {
    is_queue_empty = true;
  }
  return ret;
}

O
obdev 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
ObLink *ObReplayServiceReplayTask::pop()
{
  ObLockGuard<ObSpinLock> guard(lock_);
  return pop_();
}

void ObReplayServiceReplayTask::push(Link *p)
{
  need_batch_push_ = true;
  queue_.push(p);
}

bool ObReplayServiceReplayTask::need_batch_push()
{
  return need_batch_push_;
}

void ObReplayServiceReplayTask::set_batch_push_finish()
{
  need_batch_push_ = false;
}

W
wangzelin.wzl 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
//---------------ObLogReplayBuffer---------------//
void ObLogReplayBuffer::reset()
{
  ref_ = 0;
  log_buf_ = NULL;
}

int64_t ObLogReplayBuffer::dec_replay_ref()
{
  return ATOMIC_SAF(&ref_, 1);
}

void ObLogReplayBuffer::inc_replay_ref()
{
  ATOMIC_INC(&ref_);
}

int64_t ObLogReplayBuffer::get_replay_ref()
{
  return ATOMIC_LOAD(&ref_);
}

//---------------ObLogReplayTask---------------//
O
obdev 已提交
485
int ObLogReplayTask::init(void *log_buf)
W
wangzelin.wzl 已提交
486 487
{
  int ret = OB_SUCCESS;
O
obdev 已提交
488 489 490 491
  log_buf_ = log_buf;
  if (is_pre_barrier_) {
    ObLogReplayBuffer *replay_log_buffer = static_cast<ObLogReplayBuffer *>(log_buf);
    replay_log_buffer->ref_ = REPLAY_TASK_QUEUE_SIZE;
W
wangzelin.wzl 已提交
492
  }
O
obdev 已提交
493
  CLOG_LOG(TRACE, "ObLogReplayTask init success", KPC(this));
W
wangzelin.wzl 已提交
494 495 496 497 498 499
  return ret;
}

void ObLogReplayTask::reset()
{
  ls_id_.reset();
O
obdev 已提交
500
  scn_.reset();
W
wangzelin.wzl 已提交
501 502 503 504 505 506 507 508 509
  lsn_.reset();
  log_size_ = 0;
  is_pre_barrier_ = false;
  is_post_barrier_ = false;
  replay_hint_ = 0;
  log_type_ = ObLogBaseType::INVALID_LOG_BASE_TYPE;
  is_raw_write_ = false;
  first_handle_ts_ = common::OB_INVALID_TIMESTAMP;
  print_error_ts_ = common::OB_INVALID_TIMESTAMP;
510 511
  replay_cost_ = common::OB_INVALID_TIMESTAMP;
  retry_cost_ = common::OB_INVALID_TIMESTAMP;
W
wangzelin.wzl 已提交
512 513 514 515 516 517 518
  log_buf_ = NULL;
}

bool ObLogReplayTask::is_valid()
{
  bool ret = false;
  ret = ls_id_.is_valid()
O
obdev 已提交
519
        && scn_.is_valid()
W
wangzelin.wzl 已提交
520 521 522 523 524 525 526 527 528
        && lsn_.is_valid()
        && log_size_ > 0
        && NULL != log_buf_;
  return ret;
}

void ObLogReplayTask::shallow_copy(const ObLogReplayTask &other)
{
  ls_id_ = other.ls_id_;
O
obdev 已提交
529
  scn_ = other.scn_;
W
wangzelin.wzl 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
  lsn_ = other.lsn_;
  log_size_ = other.log_size_;
  is_pre_barrier_ = other.is_pre_barrier_;
  is_post_barrier_ = other.is_post_barrier_;
  replay_hint_ = other.replay_hint_;
  log_type_ = other.log_type_;
  is_raw_write_ = other.is_raw_write_;
  log_buf_ = other.log_buf_;
}

//---------------ObReplayFsCb---------------//
int ObReplayFsCb::update_end_lsn(int64_t id,
                                 const LSN &end_offset,
                                 const int64_t proposal_id)
{
  UNUSED(id);
  UNUSED(proposal_id);
  return replay_status_->update_end_offset(end_offset);
}

//---------------ObReplayStatus---------------//
ObReplayStatus::ObReplayStatus():
    is_inited_(false),
    is_enabled_(false),
    is_submit_blocked_(true),
    role_(FOLLOWER),
    ls_id_(),
    ref_cnt_(0),
    post_barrier_lsn_(),
    err_info_(),
    pending_task_count_(0),
    last_check_memstore_lsn_(),
562
    rwlock_(common::ObLatchIds::REPLAY_STATUS_LOCK),
O
obdev 已提交
563
    rolelock_(common::ObLatchIds::REPLAY_STATUS_LOCK),
W
wangzelin.wzl 已提交
564 565 566 567 568 569
    rp_sv_(NULL),
    submit_log_task_(),
    palf_env_(NULL),
    palf_handle_(),
    fs_cb_(),
    get_log_info_debug_time_(OB_INVALID_TIMESTAMP),
O
obdev 已提交
570 571
    try_wrlock_debug_time_(OB_INVALID_TIMESTAMP),
    check_enable_debug_time_(OB_INVALID_TIMESTAMP)
W
wangzelin.wzl 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
{
}

ObReplayStatus::~ObReplayStatus()
{
  destroy();
}

int ObReplayStatus::init(const share::ObLSID &id,
                         const common::ObReplicaType &replica_type,
                         PalfEnv *palf_env,
                         ObLogReplayService *rp_sv)
{
  //TODO: use replica type init need_replay
  UNUSED(replica_type);
  int ret = OB_SUCCESS;
  if (is_inited_) {
    ret = OB_INIT_TWICE;
    CLOG_LOG(WARN, "replay status has already been inited", K(ret));
  } else if (!id.is_valid() || OB_ISNULL(palf_env) || OB_ISNULL(rp_sv)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(id), K(rp_sv), KP(palf_env), K(ret));
  } else if (OB_FAIL(palf_env->open(id.id(), palf_handle_))) {
    CLOG_LOG(ERROR, "failed to open palf handle", K(palf_env), K(id));
  } else {
    ls_id_ = id;
    get_log_info_debug_time_ = OB_INVALID_TIMESTAMP;
    try_wrlock_debug_time_ = OB_INVALID_TIMESTAMP;
O
obdev 已提交
600
    check_enable_debug_time_ = OB_INVALID_TIMESTAMP;
W
wangzelin.wzl 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    palf_env_ = palf_env;
    rp_sv_ = rp_sv;
    fs_cb_ = ObReplayFsCb(this);
    is_inited_ = true;
    if (OB_FAIL(palf_handle_.register_file_size_cb(&fs_cb_))) {
      CLOG_LOG(ERROR, "failed to register cb", K(ret));
    } else {
      CLOG_LOG(INFO, "replay status init success", K(ret), KPC(this));
    }
  }
  if (OB_FAIL(ret) && (OB_INIT_TWICE != ret))
  {
    destroy();
  }
  return ret;
}

void ObReplayStatus::destroy()
{
  int ret = OB_SUCCESS;
  //注意: 虽然replay status的引用计数已归0, 但此时fs_cb_依然可能访问replay status,需要先调用unregister_file_size_cb
  if (OB_FAIL(palf_handle_.unregister_file_size_cb())) {
    CLOG_LOG(ERROR, "failed to unregister cb", K(ret));
  }
  WLockGuard wlock_guard(rwlock_);
  CLOG_LOG(INFO, "destuct replay status", KPC(this));
  // 析构前必须是disable状态
  if (is_enabled_) {
    CLOG_LOG(ERROR, "is_enable when destucting", K(this));
  } else {
    is_inited_ = false;
    if (palf_handle_.is_valid()) {
      palf_env_->close(palf_handle_);
    }
    submit_log_task_.destroy();
    for (int64_t i = 0; i < REPLAY_TASK_QUEUE_SIZE; ++i) {
      task_queues_[i].destroy();
    }
    is_submit_blocked_ = true;
    role_ = FOLLOWER;
    ls_id_.reset();
    post_barrier_lsn_.reset();
    err_info_.reset();
    last_check_memstore_lsn_.reset();
    pending_task_count_ = 0;
    fs_cb_.destroy();
    get_log_info_debug_time_ = OB_INVALID_TIMESTAMP;
    try_wrlock_debug_time_ = OB_INVALID_TIMESTAMP;
O
obdev 已提交
649
    check_enable_debug_time_ = OB_INVALID_TIMESTAMP;
W
wangzelin.wzl 已提交
650 651 652 653 654 655
    palf_env_ = NULL;
    rp_sv_ = NULL;
  }
}

//不可重入,如果失败需要外部手动disable
O
obdev 已提交
656
int ObReplayStatus::enable(const LSN &base_lsn, const SCN &base_scn)
W
wangzelin.wzl 已提交
657 658 659 660 661 662 663
{
  int ret = OB_SUCCESS;
  if (is_enabled()) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(WARN, "replay status already enable", K(ret));
  } else {
    WLockGuard wlock_guard(rwlock_);
O
obdev 已提交
664 665
    if (OB_FAIL(enable_(base_lsn, base_scn))) {
      CLOG_LOG(WARN, "enable replay status failed", K(ret), K(base_lsn), K(base_scn), K(ls_id_));
W
wangzelin.wzl 已提交
666
    } else {
O
obdev 已提交
667
      CLOG_LOG(INFO, "enable replay status success", K(ret), K(base_lsn), K(base_scn), K(ls_id_));
W
wangzelin.wzl 已提交
668 669 670 671 672 673
    }
  }
  return ret;
}

// 提交当前的submit_log_task并注册回调
O
obdev 已提交
674
int ObReplayStatus::enable_(const LSN &base_lsn, const SCN &base_scn)
W
wangzelin.wzl 已提交
675 676 677 678 679 680 681 682 683
{
  int ret = OB_SUCCESS;
  // 处理submit_task需要先设置enable状态
  is_enabled_ = true;
  is_submit_blocked_ = false;
  if (0 != pending_task_count_) {
    //针对reuse场景的防御检查
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(WARN, "remain pending task when enable replay status", K(ret), KPC(this));
O
obdev 已提交
684
  } else if (OB_FAIL(submit_log_task_.init(base_lsn, base_scn, &palf_handle_, this))) {
W
wangzelin.wzl 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    CLOG_LOG(WARN, "failed to init submit_log_task", K(ret), K(&palf_handle_));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < REPLAY_TASK_QUEUE_SIZE; ++i) {
      if (OB_FAIL(task_queues_[i].init(this, i))) {
        CLOG_LOG(WARN, "failed to init task_queue", K(ret));
      }
    }
    if (OB_SUCCESS == ret) {
      set_last_check_memstore_lsn(base_lsn);
      if (OB_FAIL(submit_task_to_replay_service_(submit_log_task_))) {
        CLOG_LOG(ERROR, "failed to submit submit_log_task to replay service", K(submit_log_task_),
                    KPC(this), K(ret));
      }
    }
  }
  if (OB_SUCCESS != ret) {
    disable_();
    is_submit_blocked_ = true;
  }
  return ret;
}

//可以重入
int ObReplayStatus::disable()
{
  int ret = OB_SUCCESS;
  if (!is_enabled()) {
    CLOG_LOG(INFO, "replay status already disable", K(ls_id_));
  } else {
    do {
O
obdev 已提交
715
      WLockGuard guard(rolelock_);
W
wangzelin.wzl 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
      is_submit_blocked_ = true;
    } while (0);
    int64_t abs_timeout_us = WRLOCK_TRY_THRESHOLD + ObTimeUtility::current_time();
    if (OB_SUCC(rwlock_.wrlock(abs_timeout_us))) {
      if (OB_FAIL(disable_())) {
        CLOG_LOG(WARN, "disable replay status failed", K(ls_id_));
      } else {
        CLOG_LOG(INFO, "disable replay status success", K(ls_id_));
      }
      rwlock_.unlock();
    } else {
      ret = OB_EAGAIN;
      if (palf_reach_time_interval(1000 * 1000, try_wrlock_debug_time_)) {
        CLOG_LOG(INFO, "try lock failed in disable", KPC(this), K(ret));
      }
    }
  }
  return ret;
}

int ObReplayStatus::disable_()
{
  int ret = OB_SUCCESS;
  is_enabled_ = false;
  submit_log_task_.reset();
  for (int64_t i = 0; i < REPLAY_TASK_QUEUE_SIZE; ++i) {
    task_queues_[i].reset();
  }
  err_info_.reset();
  last_check_memstore_lsn_.reset();
  get_log_info_debug_time_ = OB_INVALID_TIMESTAMP;
  ATOMIC_STORE(&post_barrier_lsn_.val_, LOG_INVALID_LSN_VAL);
  return ret;
}

bool ObReplayStatus::is_enabled() const
{
  RLockGuard rlock_guard(rwlock_);
  return is_replay_enabled_();
}

bool ObReplayStatus::is_enabled_without_lock() const
{
  return is_replay_enabled_();
}

762
void ObReplayStatus::block_submit()
W
wangzelin.wzl 已提交
763
{
O
obdev 已提交
764
  WLockGuard guard(rolelock_);
W
wangzelin.wzl 已提交
765
  is_submit_blocked_ = true;
766
  CLOG_LOG(INFO, "replay status block submit", KPC(this));
W
wangzelin.wzl 已提交
767 768
}

769
void ObReplayStatus::unblock_submit()
W
wangzelin.wzl 已提交
770
{
771
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
772
  do {
O
obdev 已提交
773
    WLockGuard guard(rolelock_);
W
wangzelin.wzl 已提交
774
    is_submit_blocked_ = false;
775
    CLOG_LOG(INFO, "replay status unblock submit", KPC(this));
W
wangzelin.wzl 已提交
776 777
  } while (0);

778 779 780 781
  RLockGuard rlock_guard(rwlock_);
  if (!is_enabled_) {
    // do nothing
  } else if (OB_FAIL(submit_task_to_replay_service_(submit_log_task_))) {
W
wangzelin.wzl 已提交
782 783 784 785 786 787 788 789 790 791 792 793
    CLOG_LOG(ERROR, "failed to submit submit_log_task to replay service", K(submit_log_task_),
             KPC(this), K(ret));
  }
}

bool ObReplayStatus::is_replay_enabled_() const
{
  return is_enabled_;
}

bool ObReplayStatus::need_submit_log() const
{
O
obdev 已提交
794
  RLockGuard guard(rolelock_);
W
wangzelin.wzl 已提交
795 796 797 798 799
  return (FOLLOWER == role_ && !is_submit_blocked_);
}

void ObReplayStatus::switch_to_leader()
{
O
obdev 已提交
800 801
  WLockGuard guard(rolelock_);
  role_ = LEADER;
W
wangzelin.wzl 已提交
802 803 804 805 806 807
  CLOG_LOG(INFO, "replay status switch_to_leader", KPC(this));
}

void ObReplayStatus::switch_to_follower(const palf::LSN &begin_lsn)
{
  int ret = OB_SUCCESS;
808 809 810 811
  // 1.switch role after reset iterator, or fscb may push submit task with
  //   old iterator, which will fetch logs smaller than max decided scn.
  // 2.submit task after switch role, or this task may be discarded if role
  //   still be leader, and no more submit task being submitted.
W
wangzelin.wzl 已提交
812
  do {
813 814 815 816 817 818 819 820 821
    WLockGuardWithRetryInterval wguard(rwlock_, WRLOCK_TRY_THRESHOLD, WRLOCK_RETRY_INTERVAL);
    if (!is_enabled_) {
      // do nothing
    } else {
      (void)submit_log_task_.reset_iterator(palf_handle_, begin_lsn);
    }
  } while (0);
  do {
    WLockGuard role_guard(rolelock_);
W
wangzelin.wzl 已提交
822 823
    role_ = FOLLOWER;
  } while (0);
824 825 826

  RLockGuard rguard(rwlock_);
  if (!is_enabled_) {
W
wangzelin.wzl 已提交
827
    // do nothing
828 829 830 831 832
  } else if (OB_FAIL(submit_task_to_replay_service_(submit_log_task_))) {
    CLOG_LOG(ERROR, "failed to submit submit_log_task to replay service", K(submit_log_task_),
                KPC(this), K(ret));
  } else {
    // success
W
wangzelin.wzl 已提交
833 834 835 836
  }
  CLOG_LOG(INFO, "replay status switch_to_follower", KPC(this), K(begin_lsn));
}

O
obdev 已提交
837 838 839 840 841
int ObReplayStatus::flashback()
{
  int ret = OB_SUCCESS;
  WLockGuardWithRetryInterval wguard(rwlock_, WRLOCK_TRY_THRESHOLD, WRLOCK_RETRY_INTERVAL);
  if (OB_FAIL(flashback_())) {
842
      CLOG_LOG(WARN, "replay status flashback failed", K(ret), KPC(this));
O
obdev 已提交
843 844 845 846 847 848 849
  } else {
    CLOG_LOG(INFO, "replay status flashback success", K(ret), KPC(this));
  }
  return ret;
}

int ObReplayStatus::flashback_()
W
wangzelin.wzl 已提交
850
{
O
obdev 已提交
851 852
  int ret = OB_SUCCESS;
  ObLogReplayTask *cache_task = NULL;
853 854 855 856 857 858
  // bugfix:
  LSN committed_end_lsn;
  if (OB_FAIL(palf_handle_.get_end_lsn(committed_end_lsn))) {
    CLOG_LOG(WARN, "get_end_lsn failed", K(ret), KPC(this));
  } else if (OB_FAIL(submit_log_task_.set_committed_end_lsn(committed_end_lsn))) {
    CLOG_LOG(WARN, "set_committed_end_lsn failed", K(ret), KPC(this), K(committed_end_lsn));
O
obdev 已提交
859 860 861 862
  } else {
    // do nothing
  }
  return ret;
W
wangzelin.wzl 已提交
863 864 865 866 867 868 869 870
}

bool ObReplayStatus::has_remained_replay_task() const
{
  int64_t count = 0;
  bool bool_ret = (0 != pending_task_count_);

  if (pending_task_count_ > PENDING_COUNT_THRESHOLD && REACH_TIME_INTERVAL(1000 * 1000)) {
871
    CLOG_LOG_RET(WARN, OB_ERR_UNEXPECTED, "too many pending replay task", K(count), KPC(this));
W
wangzelin.wzl 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884
  }
  return bool_ret;
}

int ObReplayStatus::is_replay_done(const LSN &end_lsn,
                                   bool &is_done)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "replay status has not been inited", K(ret));
  } else {
    RLockGuard rlock_guard(rwlock_);
O
obdev 已提交
885
    LSN min_unreplayed_lsn;
W
wangzelin.wzl 已提交
886 887 888
    if (!is_enabled_) {
      is_done = false;
      CLOG_LOG(INFO, "repaly is not enabled", K(end_lsn));
O
obdev 已提交
889 890 891
    } else if (OB_FAIL(get_min_unreplayed_lsn(min_unreplayed_lsn))) {
      CLOG_LOG(ERROR, "get_min_unreplayed_lsn failed", K(this), K(ret), K(min_unreplayed_lsn));
    } else if (!min_unreplayed_lsn.is_valid()) {
W
wangzelin.wzl 已提交
892
      ret = OB_ERR_UNEXPECTED;
O
obdev 已提交
893
      CLOG_LOG(ERROR, "min_unreplayed_lsn invalid", K(this), K(ret), K(end_lsn));
W
wangzelin.wzl 已提交
894
    } else {
O
obdev 已提交
895
      is_done = min_unreplayed_lsn >= end_lsn;
W
wangzelin.wzl 已提交
896 897 898
      //TODO: @keqing.llt 限流改为类内
      if (REACH_TIME_INTERVAL(10 * 1000 * 1000)) {
        if (is_done) {
O
obdev 已提交
899
          CLOG_LOG(INFO, "log stream finished replay", K(ls_id_), K(min_unreplayed_lsn), K(end_lsn));
W
wangzelin.wzl 已提交
900
        } else {
O
obdev 已提交
901
          CLOG_LOG(INFO, "log stream has not finished replay", K(ls_id_), K(min_unreplayed_lsn), K(end_lsn));
W
wangzelin.wzl 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
        }
      }
    }
  }
  return ret;
}

int ObReplayStatus::update_end_offset(const LSN &lsn)
{
  int ret = OB_SUCCESS;
  //check when log slide out
  RLockGuard rlock_guard(rwlock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "replay status is not init", K(ls_id_), K(lsn), K(ret));
  } else if (!is_enabled_) {
O
obdev 已提交
918 919 920
    if (palf_reach_time_interval(100 * 1000, check_enable_debug_time_)) {
      CLOG_LOG(INFO, "replay status is not enabled", K(this), K(ret), K(lsn));
    }
W
wangzelin.wzl 已提交
921 922 923 924 925
  } else if (OB_UNLIKELY(!lsn.is_valid())) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(ERROR, "invalid arguments", K(ls_id_), K(lsn), K(ret));
  } else if (!need_submit_log()) {
    // leader do nothing, keep submit_log_task recording last round status as follower
926
  } else if (OB_FAIL(submit_log_task_.update_committed_end_lsn(lsn))) {
W
wangzelin.wzl 已提交
927
    // update offset and submit submit_log_task
928 929 930 931
    CLOG_LOG(ERROR, "failed to update_apply_end_offset", KR(ret), K(ls_id_), K(lsn));
  } else if (OB_FAIL(submit_task_to_replay_service_(submit_log_task_))) {
    CLOG_LOG(ERROR, "failed to submit submit_log_task to replay Service", K(submit_log_task_),
             KPC(this), K(ret));
W
wangzelin.wzl 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
  }
  return ret;
}

int ObReplayStatus::get_ls_id(share::ObLSID &id)
{
  int ret = OB_SUCCESS;
  if (!ls_id_.is_valid()) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "ls_id_ is invalid", K(ret), K(ls_id_));
  } else {
    id = ls_id_;
  }
  return ret;
}

O
obdev 已提交
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
int ObReplayStatus::get_min_unreplayed_lsn(LSN &lsn)
{
  SCN unused_scn;
  int64_t unused_replay_hint = 0;
  ObLogBaseType unused_log_type = ObLogBaseType::INVALID_LOG_BASE_TYPE;
  int64_t unused_first_handle_ts = 0;
  int64_t unused_replay_cost = 0;
  int64_t unused_retry_cost = 0;
  return get_min_unreplayed_log_info(lsn, unused_scn, unused_replay_hint, unused_log_type,
                                     unused_first_handle_ts, unused_replay_cost, unused_retry_cost);
}

int ObReplayStatus::get_max_replayed_scn(SCN &scn)
{
  int ret = OB_SUCCESS;
  LSN unused_lsn;
  SCN min_unreplayed_scn;
  int64_t unused_replay_hint = 0;
  int64_t unused_first_handle_ts = 0;
  ObLogBaseType unused_log_type = ObLogBaseType::INVALID_LOG_BASE_TYPE;
  int64_t unused_replay_cost = 0;
  int64_t unused_retry_cost = 0;
  if (OB_FAIL(get_min_unreplayed_log_info(unused_lsn, min_unreplayed_scn, unused_replay_hint, unused_log_type,
                                          unused_first_handle_ts, unused_replay_cost, unused_retry_cost))) {
    CLOG_LOG(WARN, "get_min_unreplayed_log_info failed", K(ret), KPC(this));
  } else {
    scn = min_unreplayed_scn > SCN::base_scn() ? SCN::scn_dec(min_unreplayed_scn) : SCN::min_scn();
  }
  return ret;
}

O
obdev 已提交
979 980 981 982 983 984 985
int ObReplayStatus::get_min_unreplayed_log_info(LSN &lsn,
                                                SCN &scn,
                                                int64_t &replay_hint,
                                                ObLogBaseType &log_type,
                                                int64_t &first_handle_ts,
                                                int64_t &replay_cost,
                                                int64_t &retry_cost)
W
wangzelin.wzl 已提交
986 987
{
  int ret = OB_SUCCESS;
O
obdev 已提交
988
  SCN base_scn = SCN::min_scn();
O
obdev 已提交
989 990 991 992 993 994 995
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "replay status is not inited", K(ret));
  } else if (!is_enabled_) {
    ret = OB_STATE_NOT_MATCH;
    if (palf_reach_time_interval(1 * 1000 * 1000, get_log_info_debug_time_)) {
      CLOG_LOG(WARN, "replay status is not enabled", K(ret), KPC(this));
W
wangzelin.wzl 已提交
996
    }
O
obdev 已提交
997 998 999 1000
  } else if (OB_FAIL(submit_log_task_.get_next_to_submit_log_info(lsn, scn))) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "get_next_to_submit_scn failed", K(ret));
  } else if (OB_FAIL(submit_log_task_.get_base_scn(base_scn))) {
O
obdev 已提交
1001
    CLOG_LOG(ERROR, "get_base_scn failed", K(ret));
O
obdev 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  } else if (scn <= base_scn) {
    //拉到的日志尚未超过过滤点
    scn = base_scn;
    if (palf_reach_time_interval(5 * 1000 * 1000, get_log_info_debug_time_)) {
      CLOG_LOG(INFO, "get_min_unreplayed_log_info in skip state", K(lsn), K(scn), KPC(this));
    }
  } else {
    LSN queue_lsn;
    SCN queue_scn;
    bool is_queue_empty = true;
    for (int64_t i = 0; OB_SUCC(ret) && i < REPLAY_TASK_QUEUE_SIZE; ++i) {
      if (OB_FAIL(task_queues_[i].get_min_unreplayed_log_info(queue_lsn, queue_scn, replay_hint, log_type,
                                                first_handle_ts, replay_cost, retry_cost, is_queue_empty))) {
        CLOG_LOG(ERROR, "task_queue get_min_unreplayed_log_info failed", K(ret), K(task_queues_[i]));
      } else if (!is_queue_empty
                && queue_lsn < lsn
                && queue_scn < scn) {
        lsn = queue_lsn;
        scn = queue_scn;
1021
      }
O
obdev 已提交
1022
    }
O
obdev 已提交
1023 1024 1025 1026
    if (palf_reach_time_interval(5 * 1000 * 1000, get_log_info_debug_time_)) {
      CLOG_LOG(INFO, "get_min_unreplayed_log_info", K(lsn), K(scn), KPC(this));
    }
  }
O
obdev 已提交
1027
  if (OB_SUCC(ret) && !is_enabled_) {
O
obdev 已提交
1028 1029 1030 1031 1032
    //double check
    ret = OB_STATE_NOT_MATCH;
    if (palf_reach_time_interval(1 * 1000 * 1000, get_log_info_debug_time_)) {
      CLOG_LOG(WARN, "replay status is not enabled", K(ret), KPC(this));
    }
W
wangzelin.wzl 已提交
1033
  }
O
obdev 已提交
1034 1035 1036
  if (palf_reach_time_interval(5 * 1000 * 1000, get_log_info_debug_time_)) {
    CLOG_LOG(INFO, "get_min_unreplayed_log_info", K(lsn), K(scn), KPC(this), K(ret));
  }
W
wangzelin.wzl 已提交
1037 1038 1039 1040 1041 1042 1043 1044
  return ret;
}

int ObReplayStatus::get_replay_process(int64_t &replayed_log_size,
                                       int64_t &unreplayed_log_size)
{
  int ret = OB_SUCCESS;
  LSN base_lsn;
O
obdev 已提交
1045
  LSN min_unreplayed_lsn;
W
wangzelin.wzl 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
  LSN committed_end_lsn;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "replay status is not inited", K(ret));
  } else if (!is_enabled_) {
    replayed_log_size = 0;
    unreplayed_log_size = 0;
    CLOG_LOG(INFO, "replay status is not enabled", KPC(this));
  } else if (OB_FAIL(submit_log_task_.get_base_lsn(base_lsn))) {
    CLOG_LOG(WARN, "get_base_lsn failed", K(ret), KPC(this));
O
obdev 已提交
1056
  } else if (OB_FAIL(get_min_unreplayed_lsn(min_unreplayed_lsn))) {
W
wangzelin.wzl 已提交
1057 1058
    CLOG_LOG(WARN, "get_min_unreplayed_lsn failed", K(ret), KPC(this));
  } else if (!need_submit_log()) {
O
obdev 已提交
1059
    replayed_log_size = min_unreplayed_lsn.val_ - base_lsn.val_;
W
wangzelin.wzl 已提交
1060
    unreplayed_log_size = 0;
O
obdev 已提交
1061
    CLOG_LOG(INFO, "replay status is not follower", K(min_unreplayed_lsn), K(base_lsn), KPC(this));
W
wangzelin.wzl 已提交
1062 1063 1064
  } else if (OB_FAIL(palf_handle_.get_end_lsn(committed_end_lsn))) {
    CLOG_LOG(WARN, "get_end_lsn failed", K(ret), KPC(this));
  } else {
O
obdev 已提交
1065 1066
    replayed_log_size = min_unreplayed_lsn.val_ - base_lsn.val_;
    unreplayed_log_size = committed_end_lsn.val_ - min_unreplayed_lsn.val_;
W
wangzelin.wzl 已提交
1067
    if (replayed_log_size < 0 || unreplayed_log_size < 0) {
O
obdev 已提交
1068
      CLOG_LOG(WARN, "get_replay_process failed", K(committed_end_lsn), K(min_unreplayed_lsn), K(base_lsn), KPC(this));
W
wangzelin.wzl 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    }
  }
  return ret;
}

int ObReplayStatus::push_log_replay_task(ObLogReplayTask &task)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "replay service is NULL", K(task), K(ret));
  } else if (task.is_pre_barrier_) {
    //广播到所有队列, 分配多份内存时如果失败需要全部释放
    const int64_t task_size = sizeof(ObLogReplayTask);
    common::ObSEArray<ObLogReplayTask*, REPLAY_TASK_QUEUE_SIZE> broadcast_task_array;
    //入参任务本身占用一个槽位
    broadcast_task_array.push_back(&task);
    for (int64_t i = 1; OB_SUCC(ret) && i < REPLAY_TASK_QUEUE_SIZE; ++i) {
      void *task_buf = NULL;
      if (OB_UNLIKELY(NULL == (task_buf = rp_sv_->alloc_replay_task(task_size)))) {
        ret = OB_EAGAIN;
        if (REACH_TIME_INTERVAL(1 * 1000 * 1000)) {
1091
          CLOG_LOG(WARN, "failed to alloc replay task buf when broadcast pre barrier", K(i), K(ret));
W
wangzelin.wzl 已提交
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
        }
      } else {
        ObLogReplayTask *replay_task = new (task_buf) ObLogReplayTask();
        replay_task->shallow_copy(task);
        if (OB_FAIL(broadcast_task_array.push_back(replay_task))) {
          free_replay_task(replay_task);
          CLOG_LOG(ERROR, "broadcast_task_array push back replay_task failed", K(replay_task),
                   KPC(replay_task), K(i), K(ret));
        }
      }
    }
    if (OB_SUCC(ret)) {
      int index = 0;
      for (index = 0; OB_SUCC(ret) && index < REPLAY_TASK_QUEUE_SIZE; ++index) {
        ObLogReplayTask *replay_task = broadcast_task_array[index];
        task_queues_[index].push(replay_task);
        //失败后整体重试会导致此任务引用计数错乱, 必须原地重试
        int retry_count = 0;
        while (OB_FAIL(submit_task_to_replay_service_(task_queues_[index]))) {
          //print interval 100ms
          if (0 == retry_count) {
            CLOG_LOG(ERROR, "failed to push replay task queue to replay service", KPC(replay_task),
                     K(ret), KPC(this), K(index));
          }
          retry_count = (retry_count + 1) % 1000;
          ob_usleep(100);
        }
O
obdev 已提交
1119
        task_queues_[index].set_batch_push_finish();
W
wangzelin.wzl 已提交
1120
      }
O
obdev 已提交
1121
      CLOG_LOG(INFO, "broadcast push pre barrier log success", K(task));
W
wangzelin.wzl 已提交
1122
    } else {
1123
      for (int64_t i = 1; i < broadcast_task_array.count(); ++i) {
W
wangzelin.wzl 已提交
1124 1125 1126 1127 1128 1129 1130
        free_replay_task(broadcast_task_array[i]);
      }
    }
  } else {
    const uint64_t queue_idx = calc_replay_queue_idx(task.replay_hint_);
    ObReplayServiceReplayTask &task_queue = task_queues_[queue_idx];
    task_queue.push(&task);
O
obdev 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  }
  return ret;
}

//此接口不会失败
int ObReplayStatus::batch_push_all_task_queue()
{
  int ret = OB_SUCCESS;
  for (int i = 0; OB_SUCC(ret) && i < REPLAY_TASK_QUEUE_SIZE; ++i) {
    ObReplayServiceReplayTask &task_queue = task_queues_[i];
    if (!task_queue.need_batch_push()) {
W
wangzelin.wzl 已提交
1142
      // do nothing
O
obdev 已提交
1143 1144 1145 1146 1147 1148 1149
    } else if (OB_FAIL(submit_task_to_replay_service_(task_queue))) {
      CLOG_LOG(ERROR, "failed to push replay task queue to replay service", K(task_queue),
               K(ret), KPC(this));
    } else {
      task_queue.set_batch_push_finish();
      CLOG_LOG(TRACE, "push replay task queue to replay service", K(task_queue),
               K(ret), KPC(this));
W
wangzelin.wzl 已提交
1150 1151 1152 1153 1154 1155 1156 1157
    }
  }
  return ret;
}

void ObReplayStatus::inc_pending_task(const int64_t log_size)
{
  if (log_size < 0) {
1158
    CLOG_LOG_RET(ERROR, OB_INVALID_ERROR, "task is invalid", K(log_size), KPC(this));
W
wangzelin.wzl 已提交
1159
  } else if (OB_ISNULL(rp_sv_)) {
1160
    CLOG_LOG_RET(ERROR, OB_ERR_UNEXPECTED, "rp sv is NULL", K(log_size), KPC(this));
W
wangzelin.wzl 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169
  } else {
    ATOMIC_INC(&pending_task_count_);
    rp_sv_->inc_pending_task_size(log_size);
  }
}

void ObReplayStatus::dec_pending_task(const int64_t log_size)
{
  if (log_size < 0) {
1170
    CLOG_LOG_RET(ERROR, OB_INVALID_ERROR, "task is invalid", K(log_size), KPC(this));
W
wangzelin.wzl 已提交
1171
  } else if (OB_ISNULL(rp_sv_)) {
1172
    CLOG_LOG_RET(ERROR, OB_ERR_UNEXPECTED, "rp sv is NULL", K(log_size), KPC(this));
W
wangzelin.wzl 已提交
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 1222 1223 1224 1225 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 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  } else {
    ATOMIC_DEC(&pending_task_count_);
    rp_sv_->dec_pending_task_size(log_size);
  }
}

void ObReplayStatus::free_replay_task(ObLogReplayTask *task)
{
  rp_sv_->free_replay_task(task);
}

void ObReplayStatus::free_replay_task_log_buf(ObLogReplayTask *task)
{
  rp_sv_->free_replay_task_log_buf(task);
}

void ObReplayStatus::set_post_barrier_submitted(const palf::LSN &lsn)
{
  ATOMIC_STORE(&post_barrier_lsn_.val_, lsn.val_);
}

int ObReplayStatus::set_post_barrier_finished(const palf::LSN &lsn)
{
  int ret = OB_SUCCESS;
  offset_t post_barrier_lsn_val = ATOMIC_LOAD(&post_barrier_lsn_.val_);
  if (OB_UNLIKELY(post_barrier_lsn_val != lsn.val_)) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "post_barrier_lsn_ not match", K(post_barrier_lsn_val), K(lsn), K(ret));
  } else {
    ATOMIC_STORE(&post_barrier_lsn_.val_, LOG_INVALID_LSN_VAL);
  }
  return ret;
}

int ObReplayStatus::check_submit_barrier()
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "replay status not inited", K(ret));
  } else {
    offset_t post_barrier_lsn_val = ATOMIC_LOAD(&post_barrier_lsn_.val_);
    // 如果当前已经有后向barrier在队列中, 则需要此后向barrier日志回放完才能提交新任务
    if (LOG_INVALID_LSN_VAL == post_barrier_lsn_val) {
      ret = OB_SUCCESS;
    } else {
      ret = OB_EAGAIN;
    }
  }
  return ret;
}

//前向barrier日志只有引用计数减为0的线程需要回放
int ObReplayStatus::check_replay_barrier(ObLogReplayTask *replay_task,
                                         ObLogReplayBuffer *&replay_log_buf,
                                         bool &need_replay,
                                         const int64_t replay_queue_idx)
{
  int ret = OB_SUCCESS;
  int64_t replay_hint = replay_task->replay_hint_;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "replay status not inited", K(ret));
  } else if (NULL == replay_task
            || replay_queue_idx < 0) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(ERROR, "check_replay_barrier invalid argument", KP(replay_task), K(replay_queue_idx));
  } else if (replay_task->is_pre_barrier_) {
    int64_t nv = -1;
    if (NULL == (replay_log_buf = static_cast<ObLogReplayBuffer *>(replay_task->log_buf_))) {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(ERROR, "pre barrier log buff is NULL", K(ret), KPC(replay_task));
    } else if (NULL == replay_log_buf->log_buf_) {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(ERROR, "pre barrier log real log buff is NULL", K(ret), KPC(replay_task));
    } else if (replay_queue_idx == calc_replay_queue_idx(replay_hint)
               && 1 != replay_log_buf->get_replay_ref()) {
      ret = OB_EAGAIN;
      //某个事务内的前向barrier日志只能在此队列回放
      CLOG_LOG(TRACE, "skip dec pre barrier log ref", K(ret), K(replay_task), KPC(replay_task),
               K(nv), KPC(this));
    } else if ((0 == (nv = replay_log_buf->dec_replay_ref()))) {
      if (replay_queue_idx != calc_replay_queue_idx(replay_hint)) {
        ret = OB_ERR_UNEXPECTED;
        CLOG_LOG(ERROR, "pre barrier log need replay but replay_queue_idx not match", K(ret), K(replay_task),
                 KPC(replay_task), K(replay_queue_idx), KPC(this));
      } else {
        need_replay = true;
      }
    } else if (nv < 0) {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(ERROR, "dec pre barrier log ref less than 0", K(ret), K(replay_task), KPC(replay_task),
               K(nv), KPC(this));
    } else {
      //skip
      need_replay = false;
      CLOG_LOG(TRACE, "dec pre barrier log ref, skip replay", K(ret), K(replay_task), KPC(replay_task),
               K(nv), KPC(this));
    }
  } else {
    need_replay = true;
  }

  return ret;
}

O
obdev 已提交
1279 1280 1281 1282 1283
void ObReplayStatus::set_err_info(const palf::LSN &lsn,
                                  const SCN &scn,
                                  const ObLogBaseType &log_type,
                                  const int64_t replay_hint,
                                  const bool is_submit_err,
W
wangzelin.wzl 已提交
1284 1285 1286 1287
                                  const int64_t err_ts,
                                  const int err_ret)
{
  err_info_.lsn_ = lsn;
O
obdev 已提交
1288 1289 1290
  err_info_.scn_ = scn;
  err_info_.log_type_ = log_type;
  err_info_.is_submit_err_ = is_submit_err;
W
wangzelin.wzl 已提交
1291 1292 1293 1294 1295 1296 1297 1298 1299
  err_info_.err_ts_ = err_ts;
  err_info_.err_ret_ = err_ret;
}

bool ObReplayStatus::is_fatal_error(const int ret) const
{
  return (OB_SUCCESS != ret
          && OB_ALLOCATE_MEMORY_FAILED != ret
          && OB_EAGAIN != ret
1300 1301 1302
          && OB_NOT_RUNNING != ret
          // for temporary positioning issue
          && OB_IO_ERROR != ret
1303
          && OB_DISK_HUNG != ret);
W
wangzelin.wzl 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
}

int ObReplayStatus::submit_task_to_replay_service_(ObReplayServiceTask &task)
{
  int ret = OB_SUCCESS;
  if (task.acquire_lease()) {
    /* The thread that gets the lease is responsible for encapsulating the task  as a request
     * and placing it into replay Service*/
    inc_ref(); //Add the reference count first, if the task push fails, dec_ref() is required
    if (OB_FAIL(rp_sv_->submit_task(&task))) {
      CLOG_LOG(ERROR, "failed to submit task to replay service", KPC(this),
                 K(task), K(ret));
      dec_ref();
    }
  }
  return ret;
}

int ObReplayStatus::stat(LSReplayStat &stat) const
{
  int ret = OB_SUCCESS;
  RLockGuard rlock_guard(rwlock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    stat.ls_id_ = ls_id_.id();
    stat.role_ = role_;
    stat.enabled_ = is_enabled_;
    stat.pending_cnt_ = pending_task_count_;
    if (OB_FAIL(submit_log_task_.get_next_to_submit_log_info(stat.unsubmitted_lsn_,
O
obdev 已提交
1334
                                                             stat.unsubmitted_scn_))) {
W
wangzelin.wzl 已提交
1335 1336 1337 1338 1339 1340 1341 1342
      CLOG_LOG(WARN, "get_next_to_submit_log_info failed", KPC(this), K(ret));
    } else if (OB_FAIL(submit_log_task_.get_committed_end_lsn(stat.end_lsn_))) {
      CLOG_LOG(WARN, "get_committed_end_lsn failed", KPC(this), K(ret));
    }
  }
  return ret;
}

O
obdev 已提交
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 1377 1378 1379 1380 1381 1382
int ObReplayStatus::diagnose(ReplayDiagnoseInfo &diagnose_info)
{
  int ret = OB_SUCCESS;
  RLockGuard rlock_guard(rwlock_);
  LSN min_unreplayed_lsn;
  SCN min_unreplayed_scn;
  int64_t replay_hint = 0;
  ObLogBaseType log_type = ObLogBaseType::INVALID_LOG_BASE_TYPE;
  char log_type_str[common::MAX_SERVICE_TYPE_BUF_LENGTH];
  int64_t first_handle_time = 0;
  int64_t replay_cost = 0;
  int64_t retry_cost = 0;
  int replay_ret = OB_SUCCESS;
  bool is_submit_err = false;
  diagnose_info.diagnose_str_.reset();
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!is_enabled_) {
    ret = OB_STATE_NOT_MATCH;
  } else if (OB_FAIL(get_min_unreplayed_log_info(min_unreplayed_lsn, min_unreplayed_scn, replay_hint,
                                                 log_type, first_handle_time, replay_cost, retry_cost))) {
    CLOG_LOG(WARN, "get_min_unreplayed_log_info failed", KPC(this), K(ret));
  } else if (FALSE_IT(diagnose_info.max_replayed_lsn_ = min_unreplayed_lsn) ||
             FALSE_IT(diagnose_info.max_replayed_scn_ = SCN::minus(min_unreplayed_scn, 1))) {
  } else if (OB_FAIL(log_base_type_to_string(log_type, log_type_str, common::MAX_SERVICE_TYPE_BUF_LENGTH))) {
    CLOG_LOG(WARN, "log_base_type_to_string failed", K(ret), K(log_type));
  } else if (OB_SUCCESS != err_info_.err_ret_) {
    // 发生过不可重试的错误, 此场景不需要诊断最小未回放位日志
    min_unreplayed_lsn = err_info_.lsn_;
    min_unreplayed_scn = err_info_.scn_;
    replay_hint = err_info_.replay_hint_;
    log_type = err_info_.log_type_;
    first_handle_time = err_info_.err_ts_;
    replay_cost = 0;
    retry_cost = 0;
    replay_ret = err_info_.err_ret_;
    is_submit_err = err_info_.is_submit_err_;
  } else if (0 < retry_cost || 0 < replay_cost) {
    replay_ret = OB_EAGAIN;
  }
1383 1384 1385 1386
  if (OB_SUCC(ret) || OB_STATE_NOT_MATCH == ret) {
    ret = OB_SUCCESS;
    if (OB_FAIL(diagnose_info.diagnose_str_.append_fmt("is_enabled:%s"
                                                       "ret:%d; "
O
obdev 已提交
1387 1388 1389 1390 1391 1392 1393
                                                       "min_unreplayed_lsn:%ld; "
                                                       "min_unreplayed_scn:%lu; "
                                                       "replay_hint:%ld; "
                                                       "log_type:%s; "
                                                       "replay_cost:%ld; "
                                                       "retry_cost:%ld; "
                                                       "first_handle_time:%ld;" ,
1394
                                                       is_enabled_? "true" : "false",
O
obdev 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
                                                       replay_ret, min_unreplayed_lsn.val_,
                                                       min_unreplayed_scn.get_val_for_inner_table_field(), replay_hint,
                                                       is_submit_err ? "REPLAY_SUBMIT" : log_type_str,
                                                       replay_cost, retry_cost, first_handle_time))) {
      CLOG_LOG(WARN, "append diagnose str failed", K(ret), K(replay_ret), K(min_unreplayed_lsn), K(min_unreplayed_scn),
               K(replay_hint), K(is_submit_err), K(replay_cost), K(retry_cost), K(first_handle_time));
    }
  }
  return ret;
}

O
obdev 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
int ObReplayStatus::trigger_fetch_log()
{
  int ret = OB_SUCCESS;
  RLockGuard rlock_guard(rwlock_);
  if (is_enabled_) {
    if (OB_FAIL(submit_task_to_replay_service_(submit_log_task_))) {
      CLOG_LOG(ERROR, "failed to submit submit_log_task to replay service", K(submit_log_task_),
                  KPC(this), K(ret));
    }
  } else {
    // do nothing
  }
  return ret;
}
W
wangzelin.wzl 已提交
1420 1421
} // namespace logservice
}