ob_backup_info_mgr.cpp 59.0 KB
Newer Older
O
oceanbase-admin 已提交
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
/**
 * 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.
 */

#define USING_LOG_PREFIX SHARE
#include "lib/thread/ob_thread_name.h"
#include "ob_backup_info_mgr.h"
#include "ob_log_archive_backup_info_mgr.h"
#include "ob_extern_backup_info_mgr.h"
#include "ob_physical_restore_table_operator.h"
#include "share/ob_cluster_version.h"
#include "ob_tenant_name_mgr.h"
#include "share/ob_force_print_log.h"
#include "share/ob_common_rpc_proxy.h"
#include "share/backup/ob_backup_lease_info_mgr.h"
#include "share/backup/ob_backup_operator.h"
#include "share/backup/ob_backup_manager.h"
#include "share/schema/ob_multi_version_schema_service.h"
27 28
#include "share/backup/ob_backup_backupset_operator.h"
#include "share/backup/ob_backup_backuppiece_operator.h"
O
oceanbase-admin 已提交
29
#include "lib/utility/utility.h"
30
#include "ob_backup_manager.h"
O
oceanbase-admin 已提交
31 32 33 34 35
using namespace oceanbase;
using namespace common;
using namespace share;
using namespace common;

36
ObLogArchiveSimpleInfo::ObLogArchiveSimpleInfo()
O
oceanbase-admin 已提交
37 38 39 40
    : update_ts_(0),
      checkpoint_ts_(0),
      start_ts_(0),
      status_(share::ObLogArchiveStatus::INVALID),
41 42 43 44 45 46
      tenant_id_(OB_INVALID_ID),
      cur_piece_id_(0),
      cur_piece_create_date_(0),
      is_piece_freezing_(false),
      prev_piece_id_(0),
      prev_piece_create_date_(0)
O
oceanbase-admin 已提交
47 48
{}

49
void ObLogArchiveSimpleInfo::reset()
O
oceanbase-admin 已提交
50 51 52 53
{
  update_ts_ = 0;
  checkpoint_ts_ = 0;
  start_ts_ = 0;
54
  status_ = ObLogArchiveStatus::INVALID;
O
oceanbase-admin 已提交
55
  tenant_id_ = OB_INVALID_ID;
56 57 58 59 60
  cur_piece_id_ = 0;
  cur_piece_create_date_ = 0;
  is_piece_freezing_ = false;
  prev_piece_id_ = 0;
  prev_piece_create_date_ = 0;
O
oceanbase-admin 已提交
61 62
}

63
bool ObLogArchiveSimpleInfo::is_valid() const
O
oceanbase-admin 已提交
64 65
{
  return update_ts_ > 0 && checkpoint_ts_ >= 0 && start_ts_ >= 0 && ObLogArchiveStatus::is_valid(status_) &&
66 67
         OB_INVALID_ID != tenant_id_ && cur_piece_id_ >= 0 && cur_piece_create_date_ >= 0 &&
         (!is_piece_freezing_ || (prev_piece_id_ > 0 && prev_piece_create_date_ > 0));
O
oceanbase-admin 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

ObLogArchiveInfoMgr::ObLogArchiveInfoMgr()
    : is_inited_(false),
      sql_proxy_(nullptr),
      lock_(ObLatchIds::BACKUP_INFO_MGR_LOCK),
      mutex_(ObLatchIds::BACKUP_INFO_MGR_LOCK),
      status_map_(),
      update_count_(0)
{}

ObLogArchiveInfoMgr::~ObLogArchiveInfoMgr()
{}

M
mw0 已提交
82
ObLogArchiveInfoMgr &ObLogArchiveInfoMgr::get_instance()
O
oceanbase-admin 已提交
83 84 85 86 87
{
  static ObLogArchiveInfoMgr mgr;
  return mgr;
}

M
mw0 已提交
88
int ObLogArchiveInfoMgr::init(common::ObMySQLProxy &sql_proxy)
O
oceanbase-admin 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
  int ret = OB_SUCCESS;
  lib::ObLabel label("BACKUP_LR_INFO");

  if (is_inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("cannot init twice", K(ret));
  } else if (OB_FAIL(status_map_.create(OB_MAX_SERVER_TENANT_CNT, label))) {
    LOG_WARN("failed to create status map", K(ret));
  } else {
    sql_proxy_ = &sql_proxy;
    is_inited_ = true;
  }
  return ret;
}

int ObLogArchiveInfoMgr::get_log_archive_status(
M
mw0 已提交
106
    const uint64_t tenant_id, const int64_t need_ts, ObLogArchiveSimpleInfo &status)
O
oceanbase-admin 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
{
  int ret = OB_SUCCESS;
  const int64_t CACHE_EXPIRED_TIME = 1 * 1000 * 1000;  // 1s
  bool need_renew = false;
  int64_t cur_ts = ObTimeUtility::current_time();

  status.reset();

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (tenant_id == OB_INVALID_ID || need_ts <= 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret), K(need_ts), K(tenant_id));
  } else {
    if (OB_FAIL(get_log_archive_status_(tenant_id, status))) {
      if (OB_HASH_NOT_EXIST == ret) {
        need_renew = true;
        ret = OB_SUCCESS;
      } else {
        LOG_WARN("failed to get log archive status", K(ret), K(tenant_id));
      }
    } else if (status.update_ts_ < need_ts || status.update_ts_ + CACHE_EXPIRED_TIME <= cur_ts) {
      need_renew = true;
    }
  }

  if (OB_SUCC(ret) && need_renew) {
    if (OB_FAIL(renew_log_archive_status_(tenant_id, status))) {
      LOG_WARN("failed to renew log archive status", K(ret), K(tenant_id));
    }
  }

  if (OB_SUCC(ret) && need_ts > status.update_ts_) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("invalid update ts", K(ret), K(need_ts), K(status));
  }

  return ret;
}

M
mw0 已提交
148
int ObLogArchiveInfoMgr::get_log_archive_status_(const uint64_t tenant_id, ObLogArchiveSimpleInfo &status)
O
oceanbase-admin 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
{
  int ret = OB_SUCCESS;

  SpinRLockGuard guard(lock_);
  if (OB_FAIL(status_map_.get_refactored(tenant_id, status))) {
    if (OB_HASH_NOT_EXIST != ret) {
      LOG_WARN("failed to get status", K(ret), K(tenant_id));
    }
  }

  return ret;
}

int ObLogArchiveInfoMgr::try_retire_status_()
{
  int ret = OB_SUCCESS;
  uint64_t del_tenant_id = 0;
  int64_t del_ts = INT64_MAX;

  if (status_map_.size() >= OB_MAX_SERVER_TENANT_CNT) {
    for (STATUS_MAP::const_iterator it = status_map_.begin(); OB_SUCC(ret) && it != status_map_.end(); ++it) {
      const uint64_t tenant_id = it->first;
M
mw0 已提交
171
      const ObLogArchiveSimpleInfo &status = it->second;
O
oceanbase-admin 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
      if (status.update_ts_ < del_ts) {
        del_tenant_id = tenant_id;
        del_ts = status.update_ts_;
      }
    }

    if (OB_SUCC(ret)) {
      SpinWLockGuard guard(lock_);
      if (OB_FAIL(status_map_.erase_refactored(del_tenant_id))) {
        LOG_WARN("failed to erase status", K(ret), K(del_tenant_id));
      }
    }

    if (OB_SUCC(ret)) {
      FLOG_INFO("retire log archive status", K(del_tenant_id));
    }
  }

  return ret;
}

M
mw0 已提交
193
int ObLogArchiveInfoMgr::renew_log_archive_status_(const uint64_t tenant_id, ObLogArchiveSimpleInfo &status)
O
oceanbase-admin 已提交
194 195 196 197
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfoMgr info_mgr;
  ObLogArchiveBackupInfo info;
198 199 200
  ObLogArchiveBackupInfo sys_backup_info;
  ObNonFrozenBackupPieceInfo sys_backup_piece;
  bool got_tenant_info = false;
O
oceanbase-admin 已提交
201 202 203
  status.reset();

  lib::ObMutexGuard mutex_guard(mutex_);
204 205
  status.update_ts_ = ObTimeUtility::current_time();
  status.tenant_id_ = tenant_id;
O
oceanbase-admin 已提交
206 207 208

  if (OB_FAIL(try_retire_status_())) {
    LOG_WARN("failed to try_retire_status_", K(ret));
209 210 211 212 213 214 215 216 217 218 219
  } else if (OB_FAIL(info_mgr.get_log_archive_backup_info_compatible(*sql_proxy_, tenant_id, info))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      LOG_WARN("failed to get log archive backup info", K(ret), K(tenant_id));
      usleep(100 * 1000);  // sleep 100ms
    } else {
      LOG_INFO("new tenant log archive info maybe not exist, treat is as beginning status", K(tenant_id));
      status.checkpoint_ts_ = 0;
      status.start_ts_ = 0;
      status.status_ = ObLogArchiveStatus::BEGINNING;
      ret = OB_SUCCESS;
    }
O
oceanbase-admin 已提交
220 221 222 223
  } else if (info.status_.tenant_id_ != tenant_id) {
    ret = OB_ERR_SYS;
    LOG_ERROR("invalid tennat_id", K(ret), K(tenant_id), K(info));
  } else {
224
    got_tenant_info = true;
O
oceanbase-admin 已提交
225 226 227
    status.checkpoint_ts_ = info.status_.checkpoint_ts_;
    status.start_ts_ = info.status_.start_ts_;
    status.status_ = info.status_.status_;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  }

  if (FAILEDx(ObBackupInfoMgr::fetch_sys_log_archive_backup_info_and_piece(
          *sql_proxy_, sys_backup_info, sys_backup_piece))) {
    LOG_WARN("Failed to fetch_sys_log_archive_backup_info_and_piece", K(ret));
    usleep(100 * 1000);  // sleep 100ms
  } else if (got_tenant_info && sys_backup_piece.cur_piece_info_.key_.round_id_ != info.status_.round_) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("round id not match", K(ret), K(info), K(sys_backup_piece), K(sys_backup_info));
  } else {
    status.cur_piece_id_ = sys_backup_piece.cur_piece_info_.key_.backup_piece_id_;
    status.cur_piece_create_date_ = sys_backup_piece.cur_piece_info_.create_date_;
    if (sys_backup_piece.has_prev_piece_info_) {
      status.is_piece_freezing_ = true;
      status.prev_piece_id_ = sys_backup_piece.prev_piece_info_.key_.backup_piece_id_;
      status.prev_piece_create_date_ = sys_backup_piece.prev_piece_info_.create_date_;
    } else {
      status.is_piece_freezing_ = false;
      status.prev_piece_id_ = -1;
      status.prev_piece_create_date_ = -1;
    }
O
oceanbase-admin 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    ++update_count_;
    FLOG_INFO("succeed to renew log archive status", K_(update_count), K(tenant_id), K(status));
    SpinWLockGuard guard(lock_);
    if (!status.is_valid()) {
      ret = OB_ERR_SYS;
      LOG_ERROR("invalid status", K(ret), K(status));
    } else if (OB_FAIL(status_map_.set_refactored(tenant_id, status, 1 /*overwrite*/))) {
      LOG_WARN("failed to set tenant_id", K(ret));
    }
  }
  return ret;
}

void ObBackupInfoMgr::ObBackupInfoUpdateTask::runTimerTask()
{
  int tmp_ret = OB_SUCCESS;
  if (OB_SUCCESS != (tmp_ret = ObBackupInfoMgr::get_instance().reload())) {
    LOG_WARN("failed to reload backup info", K(tmp_ret));
  }
}

M
mw0 已提交
270
ObBackupInfoMgr &ObBackupInfoMgr::get_instance()
O
oceanbase-admin 已提交
271 272 273 274 275 276 277 278 279 280 281
{
  static ObBackupInfoMgr backup_info;
  return backup_info;
}

ObBackupInfoMgr::ObBackupInfoMgr()
    : is_inited_(false),
      sql_proxy_(nullptr),
      timer_(),
      update_task_(),
      cur_backup_info_(nullptr),
282
      cur_backup_piece_(nullptr),
O
oceanbase-admin 已提交
283 284 285
      cur_restore_job_(nullptr),
      lock_(ObLatchIds::BACKUP_INFO_MGR_LOCK),
      mutex_(ObLatchIds::BACKUP_INFO_MGR_LOCK),
286 287 288 289
      is_backup_loaded_(false),
      is_restore_loaded_(false),
      backup_dest_detector_(nullptr),
      log_archive_checkpoint_interval_()
O
oceanbase-admin 已提交
290 291 292 293 294 295 296
{}

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

M
mw0 已提交
297
int ObBackupInfoMgr::init(common::ObMySQLProxy &sql_proxy, ObBackupDestDetector &backup_dest_detector)
O
oceanbase-admin 已提交
298 299 300 301 302 303 304
{
  int ret = OB_SUCCESS;
  if (is_inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("cannot init twice", K(ret));
  } else if (OB_FAIL(timer_.init("BackupInfoUpdate"))) {
    LOG_WARN("failed to init backup info update task", K(ret));
305 306
  } else if (OB_FAIL(update_log_archive_checkpoint_interval_())) {
    LOG_WARN("failed to update_log_archive_checkpoint_interval_", K(ret));
O
oceanbase-admin 已提交
307 308 309
  } else {
    sql_proxy_ = &sql_proxy;
    cur_backup_info_ = &backup_infos_[0];
310
    cur_backup_piece_ = &backup_pieces_[0];
O
oceanbase-admin 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    backup_dest_detector_ = &backup_dest_detector;
    is_inited_ = true;
  }
  return ret;
}

int ObBackupInfoMgr::start()
{
  int ret = OB_SUCCESS;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(timer_.schedule(update_task_, DEFAULT_UPDATE_INTERVAL_US, true /*repeate*/))) {
    LOG_WARN("Failed to schedule update task", K(ret));
  }
  return ret;
}

void ObBackupInfoMgr::stop()
{
  timer_.stop();
}

void ObBackupInfoMgr::wait()
{
  timer_.wait();
}

void ObBackupInfoMgr::destroy()
{
  FLOG_INFO("ObBackupInfoMgr::destroy");
  timer_.destroy();
  is_inited_ = false;
}

M
mw0 已提交
347
int ObBackupInfoMgr::get_log_archive_backup_info(ObLogArchiveBackupInfo &info)
348 349 350 351
{
  int ret = OB_SUCCESS;
  ObNonFrozenBackupPieceInfo backup_piece;
  if (OB_FAIL(get_log_archive_backup_info_and_piece(info, backup_piece))) {
352 353 354
    if (OB_EAGAIN != ret || REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL)) {
      LOG_WARN("failed to get_log_archive_backup_info_and_piece", K(ret));
    }
355 356 357 358 359
  }
  return ret;
}

int ObBackupInfoMgr::get_log_archive_backup_info_and_piece(
M
mw0 已提交
360
    ObLogArchiveBackupInfo &info, ObNonFrozenBackupPieceInfo &piece)
O
oceanbase-admin 已提交
361 362 363 364 365 366 367 368 369
{
  int ret = OB_SUCCESS;
  bool is_loaded = true;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else {
    SpinRLockGuard guard(lock_);
370
    is_loaded = is_backup_loaded_;
O
oceanbase-admin 已提交
371 372
    if (is_loaded) {
      info = *cur_backup_info_;
373
      piece = *cur_backup_piece_;
O
oceanbase-admin 已提交
374 375 376 377 378 379 380 381 382
    }
  }

  if (OB_SUCC(ret)) {
    if (!is_loaded) {
      ret = OB_EAGAIN;
      if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL)) {
        LOG_WARN("not loaded yet, try again", K(ret));
      }
383 384 385
    } else if (!info.is_valid() ||
               (ObLogArchiveStatus::STOP != info.status_.status_ &&
                   info.status_.compatible_ >= ObTenantLogArchiveStatus::COMPATIBLE_VERSION_2 && !piece.is_valid())) {
O
oceanbase-admin 已提交
386
      ret = OB_ERR_UNEXPECTED;
387
      LOG_WARN("invalid info", K(ret), K(info), K(piece));
O
oceanbase-admin 已提交
388
    } else if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL)) {
389
      FLOG_INFO("get_log_archive_backup_info", K(ret), K(info), K(piece));
O
oceanbase-admin 已提交
390 391 392 393 394
    }
  }
  return ret;
}

M
mw0 已提交
395
int ObBackupInfoMgr::get_restore_info_from_cache(const uint64_t tenant_id, ObSimplePhysicalRestoreJob &simple_job_info)
O
oceanbase-admin 已提交
396 397 398 399 400 401 402 403
{
  int ret = OB_ENTRY_NOT_EXIST;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else {
    SpinRLockGuard guard(lock_);
404
    if (is_restore_loaded_) {
O
oceanbase-admin 已提交
405
      for (int64_t i = 0; i < cur_restore_job_->count(); ++i) {
M
mw0 已提交
406
        ObPhysicalRestoreJob &cur_job = cur_restore_job_->at(i);
O
oceanbase-admin 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419
        if (cur_job.tenant_id_ == tenant_id) {
          if (OB_FAIL(cur_job.copy_to(simple_job_info))) {
            LOG_WARN("failed to copy to simple job info", K(ret), K(cur_job));
          }
          break;
        }
      }
    }
  }

  return ret;
}

M
mw0 已提交
420
int ObBackupInfoMgr::get_restore_status_from_cache(const uint64_t tenant_id, PhysicalRestoreStatus &status)
O
oceanbase-admin 已提交
421 422 423 424 425 426 427 428 429
{
  int ret = OB_ENTRY_NOT_EXIST;
  status = PHYSICAL_RESTORE_MAX_STATUS;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else {
    SpinRLockGuard guard(lock_);
430
    if (is_restore_loaded_) {
O
oceanbase-admin 已提交
431
      for (int64_t i = 0; i < cur_restore_job_->count(); ++i) {
M
mw0 已提交
432
        ObPhysicalRestoreJob &cur_job = cur_restore_job_->at(i);
O
oceanbase-admin 已提交
433 434
        if (cur_job.tenant_id_ == tenant_id) {
          status = cur_job.status_;
M
mz0 已提交
435
          ret = OB_SUCCESS;
O
oceanbase-admin 已提交
436 437 438 439 440 441 442 443 444
          break;
        }
      }
    }
  }

  return ret;
}

M
mw0 已提交
445
int ObBackupInfoMgr::get_restore_info(const uint64_t tenant_id, ObPhysicalRestoreInfo &info)
446 447 448 449 450
{
  return get_restore_info(false /*need_valid_restore_schema_version*/, tenant_id, info);
}

int ObBackupInfoMgr::get_restore_info(
M
mw0 已提交
451
    const bool need_valid_restore_schema_version, const uint64_t tenant_id, ObPhysicalRestoreInfo &info)
O
oceanbase-admin 已提交
452 453 454 455
{
  int ret = OB_SUCCESS;
  ObSimplePhysicalRestoreJob simple_job_info;

456
  bool need_reload = false;
O
oceanbase-admin 已提交
457 458 459 460 461 462
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      LOG_WARN("failed to get_restore_info_from_cache", K(ret), K(tenant_id));
463 464 465 466 467 468 469 470 471 472 473
    } else {
      ret = OB_SUCCESS;
      need_reload = true;
    }
  } else if (need_valid_restore_schema_version && (simple_job_info.restore_info_.restore_schema_version_ <= 0)) {
    need_reload = true;
    simple_job_info.reset();
  }

  if (OB_SUCC(ret) && need_reload) {
    if (OB_FAIL(reload())) {
O
oceanbase-admin 已提交
474 475 476 477 478 479 480
      LOG_WARN("failed to reload", K(ret));
    } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
      LOG_WARN("failed to get restore info from cache again", K(ret), K(tenant_id));
    }
  }

  if (OB_SUCC(ret)) {
481 482 483 484
    if (need_valid_restore_schema_version && simple_job_info.restore_info_.restore_schema_version_ <= 0) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid restore_info", K(ret), K(simple_job_info), K(need_valid_restore_schema_version));
    } else if (OB_FAIL(simple_job_info.copy_to(info))) {
O
oceanbase-admin 已提交
485 486 487 488
      LOG_WARN("failed to copy physical restore info", K(ret), K(simple_job_info));
    }

    if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL)) {
489
      FLOG_INFO("get_restore_info", K(ret), K(tenant_id), K(simple_job_info), K(need_valid_restore_schema_version));
O
oceanbase-admin 已提交
490 491 492 493 494
    }
  }
  return ret;
}

M
mw0 已提交
495
int ObBackupInfoMgr::get_restore_job_id(const uint64_t tenant_id, int64_t &job_id)
O
oceanbase-admin 已提交
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
{
  int ret = OB_SUCCESS;
  ObSimplePhysicalRestoreJob simple_job_info;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      LOG_WARN("failed to get_restore_info_from_cache", K(ret), K(tenant_id));
    } else if (OB_FAIL(reload())) {
      LOG_WARN("failed to reload", K(ret));
    } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
      LOG_WARN("failed to get restore info from cache again", K(ret), K(tenant_id));
    }
  }

  if (OB_SUCC(ret)) {
    job_id = simple_job_info.job_id_;
    if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL)) {
      FLOG_INFO("get_restore_job_id", K(ret), K(tenant_id), K(job_id), K(simple_job_info));
    }
  }
  return ret;
}
521 522

int ObBackupInfoMgr::get_restore_piece_list(
M
mw0 已提交
523
    const uint64_t tenant_id, common::ObIArray<share::ObSimpleBackupPiecePath> &piece_list)
524 525 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 552
{
  int ret = OB_SUCCESS;
  ObSimplePhysicalRestoreJob simple_job_info;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      LOG_WARN("failed to get_restore_info_from_cache", K(ret), K(tenant_id));
    } else if (OB_FAIL(reload())) {
      LOG_WARN("failed to reload", K(ret));
    } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
      LOG_WARN("failed to get restore info from cache again", K(ret), K(tenant_id));
    }
  }

  if (OB_SUCC(ret)) {
    if (OB_FAIL(piece_list.assign(simple_job_info.restore_info_.get_backup_piece_path_list()))) {
      LOG_WARN("failed to assign piece list", KR(ret));
    }
    if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL)) {
      FLOG_INFO("get_restore_job_id", K(ret), K(tenant_id), K(simple_job_info));
    }
  }
  return ret;
}

int ObBackupInfoMgr::get_restore_set_list(
M
mw0 已提交
553
    const uint64_t tenant_id, common::ObIArray<share::ObSimpleBackupSetPath> &set_list)
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
{
  int ret = OB_SUCCESS;
  ObSimplePhysicalRestoreJob simple_job_info;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      LOG_WARN("failed to get_restore_info_from_cache", K(ret), K(tenant_id));
    } else if (OB_FAIL(reload())) {
      LOG_WARN("failed to reload", K(ret));
    } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
      LOG_WARN("failed to get restore info from cache again", K(ret), K(tenant_id));
    }
  }

  if (OB_SUCC(ret)) {
    if (OB_FAIL(set_list.assign(simple_job_info.restore_info_.get_backup_set_path_list()))) {
      LOG_WARN("failed to assign set list", KR(ret));
    }
    if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL)) {
      FLOG_INFO("get_restore_job_id", K(ret), K(tenant_id), K(simple_job_info));
    }
  }
  return ret;
}

M
mw0 已提交
582
int ObBackupInfoMgr::get_restore_status(const uint64_t tenant_id, PhysicalRestoreStatus &status)
O
oceanbase-admin 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
{
  int ret = OB_SUCCESS;
  bool found = false;
  status = PHYSICAL_RESTORE_MAX_STATUS;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(get_restore_status_from_cache(tenant_id, status))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      LOG_WARN("failed to get_restore_status_from_cache", K(ret), K(tenant_id));
    } else if (OB_FAIL(reload())) {
      LOG_WARN("failed to reload", K(ret));
    } else if (OB_FAIL(get_restore_status_from_cache(tenant_id, status))) {
      LOG_WARN("failed to get restore info from cache again", K(ret), K(tenant_id));
    }
  }

  if (OB_SUCC(ret)) {
    if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL)) {
      FLOG_INFO("get_restore_status_from_cache", K(ret), K(tenant_id), K(status));
    }
  }
  return ret;
}

M
mw0 已提交
609
int ObBackupInfoMgr::get_backup_snapshot_version(int64_t &backup_snapshot_version)
O
oceanbase-admin 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfoMgr info_mgr;
  const uint64_t real_tenant_id = OB_SYS_TENANT_ID;
  const uint64_t cluster_version = GET_MIN_CLUSTER_VERSION();
  backup_snapshot_version = 0;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (cluster_version < CLUSTER_VERSION_2250) {
    backup_snapshot_version = 0;
    LOG_INFO("old cluster version, not check delay delete snapshot version", K(cluster_version));
  } else if (OB_FAIL(ObTenantBackupInfoOperation::get_backup_snapshot_version(
                 *sql_proxy_, real_tenant_id, backup_snapshot_version))) {
    LOG_WARN("failed to get backup snapshot_version", K(ret));
626
  } else if (REACH_TIME_INTERVAL(60 * 1000 * 1000)) {  // for each 60s
O
oceanbase-admin 已提交
627 628 629 630 631 632
    LOG_INFO("get_backup_snapshot_version", K(ret), K(real_tenant_id), K(backup_snapshot_version));
  }

  return ret;
}

M
mw0 已提交
633
int ObBackupInfoMgr::get_log_archive_checkpoint(int64_t &snapshot_version)
O
oceanbase-admin 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfoMgr info_mgr;
  const uint64_t real_tenant_id = OB_SYS_TENANT_ID;
  const uint64_t cluster_version = GET_MIN_CLUSTER_VERSION();
  snapshot_version = 0;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (cluster_version < CLUSTER_VERSION_2250) {
    snapshot_version = 0;
    LOG_INFO("old cluster version, not check delay delete snapshot version", K(cluster_version));
  } else if (OB_FAIL(info_mgr.get_log_archive_checkpoint(*sql_proxy_, real_tenant_id, snapshot_version))) {
    LOG_WARN("failed to get log archive checkpoint", K(ret));
  } else {
    LOG_INFO("get_log_archive_checkpoint", K(ret), K(real_tenant_id), K(snapshot_version));
  }

  return ret;
}

int ObBackupInfoMgr::get_delay_delete_schema_version(const uint64_t tenant_id,
M
mw0 已提交
657
    share::schema::ObMultiVersionSchemaService &schema_service, bool &is_backup, int64_t &reserved_schema_version)
O
oceanbase-admin 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfoMgr info_mgr;
  int64_t tenant_name_backup_schema_version = 0;
  bool is_restore = false;
  const int64_t cluster_version = GET_MIN_CLUSTER_VERSION();
  reserved_schema_version = 0;
  is_backup = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (CLUSTER_VERSION_2250 > cluster_version) {
    LOG_INFO("skip get_delay_delete_schema_version for old cluster version", K(cluster_version));
  } else if (OB_FAIL(schema_service.check_tenant_is_restore(nullptr, tenant_id, is_restore))) {
    LOG_WARN("failed to check tenant is restore", K(ret), K(tenant_id), K(is_restore));
  } else if (is_restore) {
    LOG_TRACE(
        "get_delay_delete_schema_version for restore", K(ret), K(tenant_id), K(reserved_schema_version), K(is_backup));
  } else if (OB_FAIL(ObTenantBackupInfoOperation::get_backup_schema_version(
                 *sql_proxy_, tenant_id, reserved_schema_version))) {
    LOG_WARN("failed to get backup snapshot_version", K(ret));
680 681
  } else if (OB_FAIL(check_if_doing_backup(is_backup))) {
    LOG_WARN("failed to check_if_doing_backup", K(ret));
O
oceanbase-admin 已提交
682 683 684 685 686 687 688 689 690 691
  } else {
    if (OB_SYS_TENANT_ID == tenant_id) {
      if (OB_FAIL(ObTenantBackupInfoOperation::get_tenant_name_backup_schema_version(
              *sql_proxy_, tenant_name_backup_schema_version))) {
        LOG_WARN("failed to get_tenant_name_backup_schema_version", K(ret));
      } else if (tenant_name_backup_schema_version < reserved_schema_version || 0 == reserved_schema_version) {
        reserved_schema_version = tenant_name_backup_schema_version;
      }
    }

692
    LOG_INFO("get_delay_delete_schema_version",
O
oceanbase-admin 已提交
693 694 695 696 697 698 699 700 701 702
        K(ret),
        K(tenant_id),
        K(tenant_name_backup_schema_version),
        K(reserved_schema_version),
        K(is_backup));
  }

  return ret;
}

M
mw0 已提交
703
int ObBackupInfoMgr::check_if_doing_backup(bool &is_doing)
O
oceanbase-admin 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfoMgr info_mgr;
  ObLogArchiveBackupInfo log_archive_info;
  const uint64_t real_tenant_id = OB_SYS_TENANT_ID;
  const uint64_t cluster_version = GET_MIN_CLUSTER_VERSION();
  const bool for_update = false;
  ObBackupInfoItem item;
  item.name_ = "backup_status";
  is_doing = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (cluster_version < CLUSTER_VERSION_2250) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("old cluster version, should not call check_if_doing_backup", K(ret), K(cluster_version));
  } else if (OB_FAIL(ObTenantBackupInfoOperation::load_info_item(*sql_proxy_, real_tenant_id, item, for_update))) {
    LOG_WARN("failed to get backup status", K(ret));
  } else if (0 != STRCMP("STOP", item.get_value_ptr())) {
    is_doing = true;
    FLOG_INFO("doing base data backup", K(item));
726
  } else if (OB_FAIL(info_mgr.get_log_archive_backup_info_compatible(*sql_proxy_, real_tenant_id, log_archive_info))) {
O
oceanbase-admin 已提交
727 728 729 730 731 732 733 734 735 736
    LOG_WARN("failed to get log archive backup info", K(ret));
  } else if (log_archive_info.status_.status_ != ObLogArchiveStatus::STOP) {
    is_doing = true;
    FLOG_INFO("doing log archive", K(ret), K(log_archive_info));
  }

  FLOG_INFO("check_if_doing_backup", K(ret), K(is_doing), K(item), K(log_archive_info));
  return ret;
}

M
mw0 已提交
737
int ObBackupInfoMgr::check_if_doing_backup_backup(bool &is_doing)
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
{
  int ret = OB_SUCCESS;
  is_doing = false;
  ObBackupInfoManager manager;
  bool enable_backup_archivelog = false;
  ObArray<ObBackupBackupsetJobInfo> backup_set_jobs;
  ObArray<ObBackupBackupPieceJobInfo> backup_piece_jobs;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else {
    if (OB_FAIL(manager.init(OB_SYS_TENANT_ID, *sql_proxy_))) {
      LOG_WARN("failed to init backup info manager", KR(ret));
    } else if (OB_FAIL(manager.get_enable_auto_backup_archivelog(
                   OB_SYS_TENANT_ID, *sql_proxy_, enable_backup_archivelog))) {
      LOG_WARN("failed to update enable auto backup archivelog", KR(ret));
    } else if (OB_FAIL(ObBackupBackupsetOperator::get_one_task(backup_set_jobs, *sql_proxy_))) {
      LOG_WARN("failed to get all task items", K(ret));
    } else if (OB_FAIL(ObBackupBackupPieceJobOperator::get_one_job(*sql_proxy_, backup_piece_jobs))) {
      LOG_WARN("failed to get all job items", K(ret));
    }
    if (OB_SUCC(ret)) {
      is_doing = !backup_set_jobs.empty() || !backup_piece_jobs.empty() || enable_backup_archivelog;
    }
  }
  return ret;
}

int ObBackupInfoMgr::update_log_archive_checkpoint_interval_()
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  int64_t log_archive_checkpoint_interval = GCONF.log_archive_checkpoint_interval;
  ObBackupDestOpt backup_dest_opt;

  if (OB_SUCCESS != (tmp_ret = backup_dest_opt.init(false /*is_backup_backup*/))) {
    LOG_WARN("failed to get_backup_dest_opt", K(tmp_ret), K(lbt()));
  } else {
    log_archive_checkpoint_interval = backup_dest_opt.log_archive_checkpoint_interval_;
  }

  const int64_t cur_log_archive_checkpoint_interval = ATOMIC_LOAD(&log_archive_checkpoint_interval_);
  if (log_archive_checkpoint_interval != cur_log_archive_checkpoint_interval) {
    FLOG_INFO("update log_archive_checkpoint_interval",
        K(cur_log_archive_checkpoint_interval),
        K(log_archive_checkpoint_interval));
    ATOMIC_STORE(&log_archive_checkpoint_interval_, log_archive_checkpoint_interval);
  }
  return ret;
}

O
oceanbase-admin 已提交
790 791 792 793 794 795 796 797 798
int ObBackupInfoMgr::reload()
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfoMgr info_mgr;
  ObPhysicalRestoreTableOperator restore_operator;
  ObBackupInfoManager info_manager;
  const uint64_t tenant_id = OB_SYS_TENANT_ID;

  lib::ObMutexGuard mutex_guard(mutex_);
M
mw0 已提交
799 800 801
  ObLogArchiveBackupInfo *new_backup_info = &backup_infos_[0];
  ObNonFrozenBackupPieceInfo *new_backup_piece = &backup_pieces_[0];
  RestoreJobArray *new_restore_job = &restore_jobs_[0];
O
oceanbase-admin 已提交
802 803 804 805

  if (new_backup_info == cur_backup_info_) {
    new_backup_info = &backup_infos_[1];
  }
806 807 808 809
  if (new_backup_piece == cur_backup_piece_) {
    new_backup_piece = &backup_pieces_[1];
  }

O
oceanbase-admin 已提交
810 811 812 813
  if (new_restore_job == cur_restore_job_) {
    new_restore_job = &restore_jobs_[1];
  }

814
  // reload backup info
O
oceanbase-admin 已提交
815 816 817
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
818 819 820 821
  } else if (OB_FAIL(update_log_archive_checkpoint_interval_())) {
    LOG_WARN("failed to update_log_archive_checkpoint_interval_", K(ret));
  } else if (OB_FAIL(fetch_sys_log_archive_backup_info_and_piece(*sql_proxy_, *new_backup_info, *new_backup_piece))) {
    LOG_WARN("failed to get log archive backup info and piece", K(ret));
O
oceanbase-admin 已提交
822 823 824 825
  } else if (OB_FAIL(info_manager.init(tenant_id, *sql_proxy_))) {
    LOG_WARN("failed to init info manager", K(ret));
  } else if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL) ||
             new_backup_info->status_.status_ != cur_backup_info_->status_.status_ ||
826 827 828
             new_backup_info->status_.round_ != cur_backup_info_->status_.round_ ||
             new_backup_info->status_.backup_piece_id_ != cur_backup_info_->status_.backup_piece_id_) {
    FLOG_INFO("succeed to reload backup info", K(ret), K(*new_backup_info), K(*new_backup_piece));
O
oceanbase-admin 已提交
829 830 831 832 833
  }

  if (OB_SUCC(ret)) {
    SpinWLockGuard guard(lock_);
    cur_backup_info_ = new_backup_info;
834 835
    cur_backup_piece_ = new_backup_piece;
    is_backup_loaded_ = true;
O
oceanbase-admin 已提交
836 837 838 839 840 841 842
  }

  if (OB_SUCC(ret)) {
    if (OB_FAIL(backup_dest_detector_->update_backup_info(*cur_backup_info_))) {
      LOG_WARN("failed to update backup info", K(ret));
    }
  }
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862

  // reload restore info
  if (OB_SUCC(ret)) {
    if (OB_FAIL(restore_operator.init(sql_proxy_))) {
      LOG_WARN("failed to init restore operator", K(ret));
    } else if (OB_FAIL(restore_operator.get_jobs(*new_restore_job))) {
      LOG_WARN("failed to get new restore info", K(ret));
    } else {
      if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL) ||
          new_restore_job->count() != cur_restore_job_->count()) {
        FLOG_INFO("succeed to reload restore job", K(*new_restore_job));
      }
      SpinWLockGuard guard(lock_);
      cur_restore_job_ = new_restore_job;
      is_restore_loaded_ = true;
    }
  }
  return ret;
}

M
mw0 已提交
863 864
int ObBackupInfoMgr::fetch_sys_log_archive_backup_info_and_piece(common::ObMySQLProxy &sql_proxy,
    ObLogArchiveBackupInfo &new_backup_info, ObNonFrozenBackupPieceInfo &new_backup_piece)
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 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
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfoMgr info_mgr;
  const bool for_update = false;
  bool need_retry = true;
  const uint64_t tenant_id = OB_SYS_TENANT_ID;
  int64_t retry_count = 0;
  const int64_t MAX_RETRY_COUNT = 3;

  while (OB_SUCC(ret) && need_retry) {
    new_backup_info.reset();
    new_backup_piece.reset();
    need_retry = false;

    if (OB_FAIL(info_mgr.get_log_archive_backup_info_compatible(sql_proxy, tenant_id, new_backup_info))) {
      LOG_WARN("failed to get log archive backup info", K(ret));
    } else if (ObLogArchiveStatus::STOP == new_backup_info.status_.status_) {
      LOG_INFO("log archive is stop, no need to get non frozen piece", K(ret), K(new_backup_info));
    } else if (new_backup_info.status_.compatible_ < ObTenantLogArchiveStatus::COMPATIBLE_VERSION_1) {
      LOG_INFO("old version log archive has no piece info", K(ret), "compatible", new_backup_info.status_.compatible_);
      if (OB_FAIL(new_backup_info.get_piece_key(new_backup_piece.cur_piece_info_.key_))) {
        LOG_WARN("Failed to get piece key", K(ret), K(new_backup_info));
      } else if (OB_FAIL(new_backup_piece.cur_piece_info_.backup_dest_.assign(new_backup_info.backup_dest_))) {
        LOG_WARN("failed to copy backup dest", K(ret), K(new_backup_info));
      } else {
        new_backup_piece.cur_piece_info_.status_ = ObBackupPieceStatus::BACKUP_PIECE_ACTIVE;
        new_backup_piece.cur_piece_info_.file_status_ = ObBackupFileStatus::BACKUP_FILE_AVAILABLE;
        new_backup_piece.cur_piece_info_.start_ts_ = new_backup_info.status_.start_ts_;
        new_backup_piece.cur_piece_info_.checkpoint_ts_ = new_backup_info.status_.checkpoint_ts_;
        new_backup_piece.cur_piece_info_.create_date_ = 0;
        new_backup_piece.cur_piece_info_.max_ts_ = INT64_MAX;
        new_backup_piece.cur_piece_info_.compatible_ = ObTenantLogArchiveStatus::NONE;
        new_backup_piece.cur_piece_info_.start_piece_id_ = 0;
      }
    } else if (OB_FAIL(
                   info_mgr.get_non_frozen_backup_piece(sql_proxy, for_update, new_backup_info, new_backup_piece))) {
      LOG_WARN("failed to get non frozen backup piece", K(ret), K(new_backup_info));
    } else if (ObLogArchiveStatus::STOPPING != new_backup_info.status_.status_ &&
               ObBackupPieceStatus::BACKUP_PIECE_ACTIVE != new_backup_piece.cur_piece_info_.status_) {
      ++retry_count;
      if (retry_count > MAX_RETRY_COUNT) {
        ret = OB_ERR_UNEXPECTED;
        LOG_ERROR("backup info and piece not match, expect retry will get matched info piece and once",
            K(ret),
            K(new_backup_info),
            K(new_backup_piece));
      } else {
        need_retry = true;
        FLOG_INFO(
            "backup info and piece not match, need retry", K(retry_count), K(new_backup_info), K(new_backup_piece));
      }
    } else {
      LOG_INFO("fetch_sys_log_archive_backup_info_and_piece", K(retry_count), K(new_backup_info), K(new_backup_piece));
    }
  }

O
oceanbase-admin 已提交
921 922 923
  return ret;
}

924 925 926 927 928
int64_t ObBackupInfoMgr::get_log_archive_checkpoint_interval() const
{
  return ATOMIC_LOAD(&log_archive_checkpoint_interval_);
}

M
mw0 已提交
929
int ObBackupInfoMgr::get_base_data_restore_schema_version(const uint64_t tenant_id, int64_t &schema_version)
O
oceanbase-admin 已提交
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
{
  int ret = OB_SUCCESS;
  ObSimplePhysicalRestoreJob simple_job_info;
  schema_version = 0;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      LOG_WARN("failed to get_restore_info_from_cache", K(ret), K(tenant_id));
    } else if (OB_FAIL(reload())) {
      LOG_WARN("failed to reload", K(ret));
    } else if (OB_FAIL(get_restore_info_from_cache(tenant_id, simple_job_info))) {
      LOG_WARN("failed to get restore info from cache again", K(ret), K(tenant_id));
    }
  }

  if (OB_SUCC(ret)) {
    schema_version = simple_job_info.schema_version_;
    if (REACH_TIME_INTERVAL(OB_DEFAULT_BACKUP_LOG_INTERVAL)) {
      FLOG_INFO("get_restore_info", K(ret), K(tenant_id), K(simple_job_info));
    }
  }
  return ret;
}

ObRestoreBackupInfoUtil::GetRestoreBackupInfoParam::GetRestoreBackupInfoParam()
    : backup_dest_(nullptr),
      backup_cluster_name_(nullptr),
      cluster_id_(0),
      incarnation_(0),
      backup_tenant_name_(nullptr),
      restore_timestamp_(0),
      passwd_array_(nullptr)
{}

967
int ObRestoreBackupInfoUtil::GetRestoreBackupInfoParam::get_largest_backup_set_path(
M
mw0 已提交
968
    share::ObSimpleBackupSetPath &simple_path) const
969 970 971 972 973 974
{
  int ret = OB_SUCCESS;
  simple_path.reset();
  int64_t idx = -1;
  int64_t largest_backup_set_id = -1;
  for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_path_list_.count(); ++i) {
M
mw0 已提交
975
    const share::ObSimpleBackupSetPath &path = backup_set_path_list_.at(i);
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
    if (path.backup_set_id_ > largest_backup_set_id) {
      largest_backup_set_id = path.backup_set_id_;
      idx = i;
    }
  }
  if (OB_SUCC(ret)) {
    if (idx >= 0) {
      simple_path = backup_set_path_list_.at(idx);
      LOG_INFO("largest backup set path", K(simple_path));
    } else {
      ret = OB_ENTRY_NOT_EXIST;
      LOG_INFO("backup set path not found", K(backup_set_path_list_));
    }
  }
  return ret;
}

int ObRestoreBackupInfoUtil::GetRestoreBackupInfoParam::get_smallest_backup_piece_path(
M
mw0 已提交
994
    share::ObSimpleBackupPiecePath &simple_path) const
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
{
  int ret = OB_SUCCESS;
  simple_path.reset();
  if (backup_piece_path_list_.empty()) {
    ret = OB_ERR_SYS;
    LOG_WARN("backup piece path list should not be empty", K(ret));
  } else {
    simple_path = backup_piece_path_list_.at(0);
  }
  return ret;
}

M
mw0 已提交
1007
int ObRestoreBackupInfoUtil::get_restore_backup_info(const GetRestoreBackupInfoParam &param, ObRestoreBackupInfo &info)
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
{
  int ret = OB_SUCCESS;
  bool is_cluster_level = param.backup_set_path_list_.empty();
  if (is_cluster_level) {
    if (OB_FAIL(get_restore_backup_info_v1_(param, info))) {
      LOG_WARN("failed to get restore backup info v1", KR(ret));
    }
  } else {
    if (OB_FAIL(get_restore_backup_info_v2_(param, info))) {
      LOG_WARN("failed to get restore backup info v1", KR(ret));
    }
  }
  return ret;
}

int ObRestoreBackupInfoUtil::get_restore_backup_info_v1_(
M
mw0 已提交
1024
    const GetRestoreBackupInfoParam &param, ObRestoreBackupInfo &info)
O
oceanbase-admin 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
{
  int ret = OB_SUCCESS;
  ObClusterBackupDest dest;
  ObExternBackupInfoMgr backup_info_mgr;
  ObExternTenantInfoMgr tenant_info_mgr;
  ObExternTenantInfo tenant_info;
  ObExternBackupInfo backup_info;
  ObExternLogArchiveBackupInfo log_archive_backup_info;
  ObLogArchiveBackupInfoMgr log_archive_backup_info_mgr;
  ObTenantLogArchiveStatus log_archive_status;
  ObExternTenantLocalityInfoMgr tenant_locality_info_mgr;
  ObExternTenantLocalityInfo tenant_locality_info;
  const int64_t cluster_version = ObClusterVersion::get_instance().get_cluster_version();
  ObTenantNameSimpleMgr tenant_name_mgr;
  ObExternPGListMgr pg_list_mgr;
  uint64_t backup_tenant_id = 0;
M
mw0 已提交
1041 1042
  const char *backup_dest = param.backup_dest_;
  const char *backup_cluster_name = param.backup_cluster_name_;
O
oceanbase-admin 已提交
1043 1044
  const int64_t cluster_id = param.cluster_id_;
  const int64_t incarnation = param.incarnation_;
M
mw0 已提交
1045
  const char *backup_tenant_name = param.backup_tenant_name_;
O
oceanbase-admin 已提交
1046
  const int64_t restore_timestamp = param.restore_timestamp_;
M
mw0 已提交
1047
  const char *passwd_array = param.passwd_array_;
O
oceanbase-admin 已提交
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 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
  ObFakeBackupLeaseService fake_backup_lease;

  if (OB_ISNULL(backup_dest) || OB_ISNULL(backup_cluster_name) || cluster_id <= 0 || incarnation < 0 ||
      OB_ISNULL(backup_tenant_name) || restore_timestamp <= 0 || OB_ISNULL(passwd_array)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get restore backup info get invalid argument",
        K(ret),
        KP(backup_dest),
        KP(backup_cluster_name),
        K(cluster_id),
        KP(backup_tenant_name),
        K(restore_timestamp),
        KP(passwd_array));
  } else if (strlen(backup_dest) >= share::OB_MAX_BACKUP_DEST_LENGTH ||
             strlen(backup_cluster_name) >= OB_MAX_CLUSTER_NAME_LENGTH) {
    ret = OB_SIZE_OVERFLOW;
    LOG_WARN("backup dest or backup cluster name size over flow", K(ret), K(backup_dest), K(backup_cluster_name));
  } else if (OB_FAIL(dest.set(backup_dest, backup_cluster_name, cluster_id, incarnation))) {
    LOG_WARN(
        "failed to set backup dest", K(ret), K(backup_dest), K(backup_cluster_name), K(cluster_id), K(incarnation));
  } else if (OB_FAIL(tenant_name_mgr.init())) {
    LOG_WARN("faiuiled to init tenant_name mgr", K(ret));
  } else if (OB_FAIL(tenant_name_mgr.read_backup_file(dest))) {
    LOG_WARN("failed to read backup tenant name mgr", K(ret), K(dest));
  } else if (OB_FAIL(tenant_name_mgr.get_tenant_id(backup_tenant_name, restore_timestamp, backup_tenant_id))) {
    LOG_WARN("failed to backup tenant id", K(ret), K(backup_tenant_name), K(restore_timestamp));
  } else if (OB_FAIL(tenant_info_mgr.init(dest, fake_backup_lease))) {
    LOG_WARN("failed to init tenant info mgr", K(ret), K(dest));
  } else if (OB_FAIL(tenant_info_mgr.find_tenant_info(backup_tenant_id, tenant_info))) {
    LOG_WARN("failed to find tenant info", K(ret), K(backup_tenant_id));
  } else if (OB_FAIL(backup_info_mgr.init(tenant_info.tenant_id_, dest, fake_backup_lease))) {
    LOG_WARN("failed to init backup info mgr", K(ret), K(dest), K(tenant_info));
  } else if (OB_FAIL(backup_info_mgr.find_backup_info(restore_timestamp, passwd_array, backup_info))) {
    LOG_WARN("failed to find backup info", K(ret), K(restore_timestamp), K(tenant_info));
  } else if (OB_FAIL(log_archive_backup_info_mgr.read_extern_log_archive_backup_info(
                 dest, tenant_info.tenant_id_, log_archive_backup_info))) {
    LOG_WARN("failed to read extern log archive backup info", K(ret), K(dest), K(tenant_info));
  } else if (OB_FAIL(log_archive_backup_info.get_log_archive_status(restore_timestamp, log_archive_status))) {
    LOG_WARN("failed to get log_archive_status", K(ret), K(restore_timestamp));
  } else if (backup_info.backup_snapshot_version_ < log_archive_status.start_ts_) {
    ret = OB_ISOLATED_BACKUP_SET;
    LOG_WARN("log archive status is not continues with backup info", K(ret), K(backup_info));
  } else if (OB_FAIL(tenant_locality_info_mgr.init(tenant_info.tenant_id_,
                 backup_info.full_backup_set_id_,
                 backup_info.inc_backup_set_id_,
                 dest,
1094 1095
                 backup_info.date_,
                 backup_info.compatible_,
O
oceanbase-admin 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
                 fake_backup_lease))) {
    LOG_WARN("failed to init tenant locality info mgr", K(ret), K(tenant_info));
  } else if (OB_FAIL(tenant_locality_info_mgr.get_extern_tenant_locality_info(tenant_locality_info))) {
    LOG_WARN("failed to find tenant locality info", K(ret), K(tenant_info));
  } else if (backup_info.cluster_version_ > cluster_version) {
    ret = OB_NOT_SUPPORTED;
    LOG_ERROR("cannot restore from newer cluster to older cluster", K(ret), K(cluster_version), K(backup_info));
  } else if (OB_FAIL(pg_list_mgr.init(backup_tenant_id,
                 backup_info.full_backup_set_id_,
                 backup_info.inc_backup_set_id_,
                 dest,
1107 1108
                 backup_info.date_,
                 backup_info.compatible_,
O
oceanbase-admin 已提交
1109 1110 1111 1112
                 fake_backup_lease))) {
    LOG_WARN("failed to init pg list mgr", K(ret), K(backup_info), K(dest));
  } else if (OB_FAIL(pg_list_mgr.get_sys_pg_list(info.sys_pg_key_list_))) {
    LOG_WARN("failed to get sys pg list", K(ret), K(backup_info), K(dest));
1113 1114 1115
  } else if (info.sys_pg_key_list_.empty()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("sys pg key list should not be empty", K(ret), K(backup_info), K(dest));
O
oceanbase-admin 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
  } else {
    info.compat_mode_ = tenant_info.compat_mode_;
    info.frozen_data_version_ = backup_info.backup_data_version_;
    info.frozen_schema_version_ = backup_info.frozen_schema_version_;
    info.frozen_snapshot_version_ = backup_info.frozen_snapshot_version_;
    info.schema_version_ = backup_info.backup_schema_version_;
    info.snapshot_version_ = backup_info.backup_snapshot_version_;
    STRCPY(info.locality_, tenant_locality_info.locality_.ptr());
    STRCPY(info.primary_zone_, tenant_locality_info.primary_zone_.ptr());
    STRCPY(info.physical_restore_info_.backup_dest_, backup_dest);
    STRCPY(info.physical_restore_info_.cluster_name_, backup_cluster_name);
    info.physical_restore_info_.cluster_id_ = cluster_id;
    info.physical_restore_info_.full_backup_set_id_ = backup_info.full_backup_set_id_;
    info.physical_restore_info_.inc_backup_set_id_ = backup_info.inc_backup_set_id_;
    info.physical_restore_info_.incarnation_ = incarnation;
    info.physical_restore_info_.tenant_id_ = tenant_info.tenant_id_;
    info.physical_restore_info_.log_archive_round_ = log_archive_status.round_;
    info.physical_restore_info_.compatible_ = backup_info.compatible_;
    info.physical_restore_info_.cluster_version_ = backup_info.cluster_version_;
1135
    info.physical_restore_info_.backup_date_ = backup_info.date_;
O
oceanbase-admin 已提交
1136 1137 1138 1139 1140
    FLOG_INFO("get_restore_backup_info", K(info), K(log_archive_status), K(backup_info));
  }
  return ret;
}

1141
int ObRestoreBackupInfoUtil::get_restore_backup_info_v2_(
M
mw0 已提交
1142
    const GetRestoreBackupInfoParam &param, ObRestoreBackupInfo &info)
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
{
  int ret = OB_SUCCESS;
  ObFakeBackupLeaseService fake_backup_lease;
  ObExternSingleBackupSetInfoMgr backup_info_mgr;
  ObLogArchiveBackupInfoMgr log_archive_backup_info_mgr;
  ObExternTenantLocalityInfoMgr tenant_locality_info_mgr;
  ObExternPGListMgr pg_list_mgr;
  ObSimpleBackupSetPath simple_set_path;      // largest backup set path
  ObSimpleBackupPiecePath simple_piece_path;  // smallest backup piece path
  ObBackupPath piece_backup_path;
M
mw0 已提交
1153 1154
  const char *backup_dest = param.backup_dest_;
  const char *backup_cluster_name = param.backup_cluster_name_;
1155 1156 1157
  const int64_t cluster_id = param.cluster_id_;
  const int64_t incarnation = param.incarnation_;
  const int64_t restore_timestamp = param.restore_timestamp_;
M
mw0 已提交
1158
  const char *passwd_array = param.passwd_array_;
1159 1160 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 1222 1223 1224 1225
  const int64_t cluster_version = ObClusterVersion::get_instance().get_cluster_version();

  ObExternLogArchiveBackupInfo log_archive_backup_info;
  ObTenantLogArchiveStatus log_archive_status;
  ObExternTenantLocalityInfo tenant_locality_info;
  ObBackupPieceInfo piece_info;
  ObBackupSetFileInfo backup_set_info;

  if (OB_FAIL(param.get_smallest_backup_piece_path(simple_piece_path))) {
    LOG_WARN("failed to get smallest simple backup piece path", K(ret));
  } else if (OB_FAIL(param.get_largest_backup_set_path(simple_set_path))) {
    LOG_WARN("failed to get smallest largest backup set path", K(ret));
  } else if (OB_FAIL(backup_info_mgr.init(simple_set_path, fake_backup_lease))) {
    LOG_WARN("failed to init backup info mgr", K(ret), K(simple_set_path));
  } else if (OB_FAIL(backup_info_mgr.get_extern_backup_set_file_info(passwd_array, backup_set_info))) {
    LOG_WARN("failed to find backup info", K(ret), K(restore_timestamp));
  } else if (OB_FAIL(piece_backup_path.init(simple_piece_path.get_simple_path()))) {
    LOG_WARN("failed to init piece backup path", K(ret));
  } else if (OB_FAIL(piece_backup_path.join(OB_STR_TENANT_CLOG_SINGLE_BACKUP_PIECE_INFO))) {
    LOG_WARN("failed to join single backup piece info");
  } else if (OB_FAIL(log_archive_backup_info_mgr.read_external_single_backup_piece_info(
                 piece_backup_path, simple_piece_path.get_storage_info(), piece_info, fake_backup_lease))) {
    LOG_WARN("failed to read external single backup piece info", K(ret), K(piece_backup_path), K(simple_piece_path));
  } else if (backup_set_info.snapshot_version_ < piece_info.start_ts_) {
    ret = OB_ISOLATED_BACKUP_SET;
    LOG_WARN("log archive status is not continues with backup info", K(ret), K(backup_set_info));
  } else if (OB_FAIL(tenant_locality_info_mgr.init(simple_set_path, fake_backup_lease))) {
    LOG_WARN("failed to init tenant locality info mgr", K(ret), K(simple_set_path));
  } else if (OB_FAIL(tenant_locality_info_mgr.get_extern_tenant_locality_info(tenant_locality_info))) {
    LOG_WARN("failed to find tenant locality info", K(ret));
  } else if (backup_set_info.cluster_version_ > cluster_version) {
    ret = OB_NOT_SUPPORTED;
    LOG_ERROR("cannot restore from newer cluster to older cluster", K(ret), K(cluster_version), K(backup_set_info));
  } else if (OB_FAIL(pg_list_mgr.init(simple_set_path, fake_backup_lease))) {
    LOG_WARN("failed to get sys pg list", K(ret), K(backup_set_info), K(simple_set_path));
  } else if (OB_FAIL(pg_list_mgr.get_sys_pg_list(info.sys_pg_key_list_))) {
    LOG_WARN("failed to get sys pg list", K(ret), K(backup_set_info));
  } else if (info.sys_pg_key_list_.empty()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("sys pg key list should not be empty", K(ret), K(backup_set_info));
  } else {
    info.compat_mode_ = tenant_locality_info.compat_mode_;
    info.frozen_data_version_ = backup_set_info.backup_data_version_;
    info.frozen_schema_version_ = backup_set_info.backup_schema_version_;
    info.frozen_snapshot_version_ = backup_set_info.snapshot_version_;
    info.schema_version_ = backup_set_info.backup_schema_version_;
    info.snapshot_version_ = backup_set_info.snapshot_version_;
    STRCPY(info.locality_, tenant_locality_info.locality_.ptr());
    STRCPY(info.primary_zone_, tenant_locality_info.primary_zone_.ptr());
    STRCPY(info.physical_restore_info_.backup_dest_, backup_dest);
    STRCPY(info.physical_restore_info_.cluster_name_, backup_cluster_name);
    info.physical_restore_info_.cluster_id_ = cluster_id;
    info.physical_restore_info_.full_backup_set_id_ = ObBackupType::FULL_BACKUP == backup_set_info.backup_type_.type_
                                                          ? backup_set_info.backup_set_id_
                                                          : backup_set_info.prev_full_backup_set_id_;
    info.physical_restore_info_.inc_backup_set_id_ = backup_set_info.backup_set_id_;
    info.physical_restore_info_.incarnation_ = incarnation;
    info.physical_restore_info_.tenant_id_ = tenant_locality_info.tenant_id_;
    info.physical_restore_info_.log_archive_round_ = piece_info.key_.round_id_;
    info.physical_restore_info_.compatible_ = backup_set_info.compatible_;
    info.physical_restore_info_.cluster_version_ = backup_set_info.cluster_version_;
    info.physical_restore_info_.backup_date_ = backup_set_info.date_;
    FLOG_INFO("get_restore_backup_info", K(info), K(piece_info), K(backup_set_info));
  }
  return ret;
}

O
oceanbase-admin 已提交
1226
int ObRestoreBackupInfoUtil::get_restore_sys_table_ids(
M
mw0 已提交
1227
    const ObPhysicalRestoreInfo &info, common::ObIArray<common::ObPartitionKey> &pkey_list)
O
oceanbase-admin 已提交
1228 1229 1230 1231 1232
{
  UNUSEDx(info, pkey_list);
  return OB_SUCCESS;
}

M
mw0 已提交
1233
ObRestoreFatalErrorReporter &ObRestoreFatalErrorReporter::get_instance()
O
oceanbase-admin 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
{
  static ObRestoreFatalErrorReporter reporter;
  return reporter;
}

ObRestoreFatalErrorReporter::ObRestoreFatalErrorReporter()
    : is_inited_(false),
      rpc_proxy_(nullptr),
      rs_mgr_(nullptr),
      mutex_(ObLatchIds::BACKUP_INFO_MGR_LOCK),
      report_results_()
{}

ObRestoreFatalErrorReporter::~ObRestoreFatalErrorReporter()
{}

M
mw0 已提交
1250
int ObRestoreFatalErrorReporter::init(obrpc::ObCommonRpcProxy &rpc_proxy, share::ObRsMgr &rs_mgr)
O
oceanbase-admin 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
{
  int ret = OB_SUCCESS;

  if (is_inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("cannot init twice", K(ret));
  } else {
    rpc_proxy_ = &rpc_proxy;
    rs_mgr_ = &rs_mgr;
    is_inited_ = true;
  }
  return ret;
}

M
mw0 已提交
1265 1266
int ObRestoreFatalErrorReporter::add_restore_error_task(const uint64_t tenant_id, const PhysicalRestoreMod &mod,
    const int32_t result, const int64_t job_id, const common::ObAddr &addr)
O
oceanbase-admin 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 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 1323 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
{
  int ret = OB_SUCCESS;
  bool found = false;

  obrpc::ObPhysicalRestoreResult restore_result;
  restore_result.tenant_id_ = tenant_id;
  restore_result.mod_ = mod;
  restore_result.return_ret_ = result;
  restore_result.job_id_ = job_id;
  restore_result.trace_id_ = *ObCurTraceId::get_trace_id();
  restore_result.addr_ = addr;

  lib::ObMutexGuard mutex_guard(mutex_);
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (!restore_result.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret));
  }

  for (int64_t i = 0; OB_SUCC(ret) && i < report_results_.count(); ++i) {
    if (report_results_.at(i).tenant_id_ == tenant_id) {
      found = true;
      break;
    }
  }

  if (OB_SUCC(ret) && !found) {
    if (OB_FAIL(report_results_.push_back(restore_result))) {
      LOG_WARN("failed to add report item", K(ret));
    } else {
      LOG_INFO("succeed to add report item", K(restore_result));
    }
  }
  return ret;
}

int ObRestoreFatalErrorReporter::start()
{
  int ret = OB_SUCCESS;

  FLOG_INFO("ObRestoreFatalErrorReporter start");
  if (OB_FAIL(ObThreadPool::start())) {
    LOG_INFO("ObRestoreFatalErrorReporter start fail", K(ret));
  }
  return ret;
}

void ObRestoreFatalErrorReporter::stop()
{
  FLOG_INFO("ObRestoreFatalErrorReporter stop");
  ObThreadPool::stop();
}

void ObRestoreFatalErrorReporter::wait()
{
  FLOG_INFO("ObRestoreFatalErrorReporter begin wait");
  ObThreadPool::wait();
  FLOG_INFO("ObRestoreFatalErrorReporter finish wait");
}

void ObRestoreFatalErrorReporter::run1()
{
  int tmp_ret = OB_SUCCESS;
  const int64_t REPORT_INTERVAL_US = 10 * 1000 * 1000;
  lib::set_thread_name("RestoreReporter");
  ObCurTraceId::init(GCONF.self_addr_);

  while (!has_set_stop()) {
    if (OB_SUCCESS != (tmp_ret = report_restore_errors())) {
      LOG_WARN("failed to report restore errors", K(tmp_ret));
    }
    if (REACH_TIME_INTERVAL(600 * 1000 * 1000)) {  // per 600s
      LOG_INFO("finish report_restore_errors", K(tmp_ret));
    }
    usleep(REPORT_INTERVAL_US);
  }
}

int ObRestoreFatalErrorReporter::report_restore_errors()
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  common::ObSEArray<obrpc::ObPhysicalRestoreResult, 16> report_results;
  bool remove_report_task = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else {
    lib::ObMutexGuard mutex_guard(mutex_);
    if (OB_FAIL(report_results.assign(report_results_))) {
      LOG_WARN("failed to copy report items", K(ret));
    }
  }

  if (OB_SUCC(ret) && report_results.count() > 0) {
    for (int64_t i = 0; !has_set_stop() && i < report_results.count(); ++i) {  // ignore ret
M
mw0 已提交
1366
      obrpc::ObPhysicalRestoreResult &result = report_results.at(i);
O
oceanbase-admin 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
      if (OB_SUCCESS != (tmp_ret = report_restore_error_(result))) {
        ret = OB_SUCC(ret) ? tmp_ret : ret;
        LOG_WARN("failed to report restore error", K(tmp_ret), K(ret), K(result));
      } else if (OB_SUCCESS != (tmp_ret = remove_restore_error_task_(result))) {
        ret = OB_SUCC(ret) ? tmp_ret : ret;
        LOG_WARN("failed to remove report result", K(tmp_ret), K(result));
      }
    }
  }
  return ret;
}

M
mw0 已提交
1379
int ObRestoreFatalErrorReporter::remove_restore_error_task_(const obrpc::ObPhysicalRestoreResult &result)
O
oceanbase-admin 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
{
  int ret = OB_SUCCESS;

  lib::ObMutexGuard mutex_guard(mutex_);
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  }

  for (int64_t i = 0; OB_SUCC(ret) && i < report_results_.count(); ++i) {
    if (report_results_.at(i).tenant_id_ == result.tenant_id_) {
      if (OB_FAIL(report_results_.remove(i))) {
        LOG_WARN("failed to remove restore report task", K(ret), K(i), K(result));
      } else {
        LOG_INFO("succeed to remove restore report task", K(ret), K(i), K(result));
      }
      break;
    }
  }
  return ret;
}

M
mw0 已提交
1402
int ObRestoreFatalErrorReporter::report_restore_error_(const obrpc::ObPhysicalRestoreResult &result)
O
oceanbase-admin 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
{
  int ret = OB_SUCCESS;
  common::ObAddr rs_addr;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(rs_mgr_->get_master_root_server(rs_addr))) {
    STORAGE_LOG(WARN, "get master root service failed", K(ret));
  } else if (OB_FAIL(rpc_proxy_->to(rs_addr).send_physical_restore_result(result))) {
    LOG_WARN("failed to send_physical_restore_result", K(ret), K(rs_addr), K(result));
    if (OB_ENTRY_NOT_EXIST == ret) {
      ret = OB_SUCCESS;
      LOG_INFO("restore task not exist, skip report", K(result));
    }
  }
  FLOG_INFO("finish report_restore_error_", K(ret), K(rs_addr), K(result));
  return ret;
}

M
mw0 已提交
1423
ObBackupDestDetector &ObBackupDestDetector::get_instance()
O
oceanbase-admin 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
{
  static ObBackupDestDetector detector;
  return detector;
}

int ObBackupDestDetector::start()
{
  int ret = OB_SUCCESS;

  FLOG_INFO("ObBackupDestDetector start");
  if (OB_FAIL(ObThreadPool::start())) {
    LOG_INFO("ObBackupDestDetector start fail", K(ret));
  }
  return ret;
}

void ObBackupDestDetector::stop()
{
  FLOG_INFO("ObBackupDestDetector stop");
  ObThreadPool::stop();
  wakeup();
}

void ObBackupDestDetector::wait()
{
  FLOG_INFO("ObBackupDestDetector begin wait");
  ObThreadPool::wait();
  FLOG_INFO("ObBackupDestDetector finish wait");
}

ObBackupDestDetector::ObBackupDestDetector()
    : is_inited_(false), lock_(ObLatchIds::BACKUP_INFO_MGR_LOCK), info_(), is_bad_(false), wakeup_count_(0)
{}

ObBackupDestDetector::~ObBackupDestDetector()
{}

int ObBackupDestDetector::init()
{
  int ret = OB_SUCCESS;

  if (is_inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("cannot init twice", K(ret));
  } else if (OB_FAIL(cond_.init(ObWaitEventIds::BACKUP_INFO_MGR_LOCK_WAIT))) {
    LOG_WARN("failed to init cond", K(ret));
  } else {
    is_bad_ = false;
    wakeup_count_ = 0;
    is_inited_ = true;
  }
  return ret;
}

void ObBackupDestDetector::run1()
{
  int tmp_ret = OB_SUCCESS;
  lib::set_thread_name("BackupDestDetect");
  ObCurTraceId::init(GCONF.self_addr_);

  while (!has_set_stop()) {
    if (OB_SUCCESS != (tmp_ret = check_backup_dest())) {
      LOG_WARN("failed to check_backup_dest", K(tmp_ret));
    }
    if (REACH_TIME_INTERVAL(600 * 1000 * 1000)) {  // per 600s
      LOG_INFO("[BACKUP_MOUNT_FILE]finish check_backup_dest", K_(is_bad), K_(info));
    }
    idle();
  }
}

void ObBackupDestDetector::idle()
{
  const int32_t IDLE_TIME_MS = 10 * 1000;
  const int32_t PER_IDLE_TIME_MS = 1000;
  int32_t left_time_ms = IDLE_TIME_MS;
  ObThreadCondGuard guard(cond_);
  while (!has_set_stop() && wakeup_count_ == 0) {
    const int64_t wait_time_ms = MIN(PER_IDLE_TIME_MS, left_time_ms);
    if (wait_time_ms <= 0) {
      break;
    } else {
      left_time_ms -= wait_time_ms;
      (void)cond_.wait(wait_time_ms);  // ignore ret
    }
  }
  wakeup_count_ = 0;
}

void ObBackupDestDetector::wakeup()
{
  ObThreadCondGuard guard(cond_);
  wakeup_count_++;
  (void)cond_.broadcast();  // ignore ret
}

M
mw0 已提交
1520
int ObBackupDestDetector::get_is_backup_dest_bad(const int64_t round_id, bool &is_bad)
O
oceanbase-admin 已提交
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
{
  int ret = OB_SUCCESS;
  is_bad = false;

  SpinRLockGuard guard(lock_);
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (round_id <= 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret), K(round_id));
  } else if (!is_bad_) {
    // not bad
  } else if (info_.status_.round_ != round_id) {
    if (REACH_TIME_INTERVAL(10 * 1000 * 1000)) {  // per 10s
      LOG_INFO("[BACKUP_MOUNT_FILE]round not match, treat it as not bad", K(round_id), K_(is_bad), K_(info));
    }
  } else if (ObLogArchiveStatus::BEGINNING != info_.status_.status_ &&
             ObLogArchiveStatus::DOING != info_.status_.status_) {
    if (REACH_TIME_INTERVAL(10 * 1000 * 1000)) {  // per 10s
      LOG_INFO("[BACKUP_MOUNT_FILE]log archive status is not beginning and doing, treat it as not bad",
          K(round_id),
          K_(is_bad),
          K_(info));
    }
  } else {
    is_bad = is_bad_;
    if (REACH_TIME_INTERVAL(10 * 1000 * 1000)) {  // per 10s
      LOG_INFO("backup dest maybe is not mount properly", K(round_id), K_(is_bad), K_(info));
    }
  }

  return ret;
}

M
mw0 已提交
1556
int ObBackupDestDetector::update_backup_info(const ObLogArchiveBackupInfo &info)
O
oceanbase-admin 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 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
{
  int ret = OB_SUCCESS;
  bool is_changed = false;

  {
    SpinWLockGuard guard(lock_);
    if (!is_inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("not inited", K(ret));
    } else if (!info.is_valid()) {
      ret = OB_INVALID_ARGUMENT;
      LOG_WARN("invalid args", K(ret), K(info));
    } else if (0 != STRNCMP(info.backup_dest_, info_.backup_dest_, sizeof(info_.backup_dest_)) ||
               info.status_.round_ != info_.status_.round_) {
      is_changed = true;
      FLOG_INFO("[BACKUP_MOUNT_FILE]backup dest or round changed, clear backup mount file bad flag", K(info_), K(info));
      is_bad_ = false;
    }

    if (OB_SUCC(ret)) {
      info_ = info;
    }
  }

  if (is_changed) {
    wakeup();
  }
  return ret;
}

int ObBackupDestDetector::check_backup_dest()
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfo info;
  bool is_bad;

  {
    SpinRLockGuard guard(lock_);
    if (!is_inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("not inited", K(ret));
    } else {
      info = info_;
      is_bad = is_bad_;
    }
  }

  if (OB_SUCC(ret) && info.is_valid() && ObLogArchiveStatus::STOP != info.status_.status_) {
    if (is_bad) {
      LOG_ERROR("backup dest is bad, skip check backup dest", K(info));
    } else if (OB_FAIL(check_backup_dest_(info, is_bad))) {
      LOG_WARN("failed to check backup dest", K(ret), K(info));
    } else if (is_bad) {
      SpinWLockGuard guard(lock_);
      if (0 == STRNCMP(info.backup_dest_, info_.backup_dest_, sizeof(info_.backup_dest_)) &&
          info.status_.round_ == info_.status_.round_) {
        is_bad_ = true;
        LOG_ERROR("[BACKUP_MOUNT_FILE]backup mount file not file, mark backup dest bad", K(info));
      } else {
        FLOG_WARN("[BACKUP_MOUNT_FILE]backup info is changed, cannot set bad", K(info), K(info_));
      }
    }
  }

  return ret;
}

M
mw0 已提交
1624
int ObBackupDestDetector::check_backup_dest_(ObLogArchiveBackupInfo &backup_info, bool &is_bad)
O
oceanbase-admin 已提交
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
{
  int ret = OB_SUCCESS;
  bool need_check = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(ObBackupMountFile::need_check_mount_file(backup_info, need_check))) {
    LOG_WARN("failed to need check mount file", K(ret), K(backup_info));
  } else if (!need_check || !backup_info.status_.is_mount_file_created_) {
    // no need check
  } else if (OB_FAIL(ObBackupMountFile::check_mount_file(backup_info))) {
    if (OB_BACKUP_MOUNT_FILE_NOT_VALID == ret) {
      LOG_ERROR(
          "[BACKUP_MOUNT_FILE]backup mount file is not valid, maybe nfs is not mount properly", K(ret), K(backup_info));
      ret = OB_SUCCESS;
      is_bad = true;
    } else {
      LOG_WARN("failed to check backup mount file", K(ret), K(backup_info));
    }
  }
  return ret;
}