ob_storage_struct.cpp 46.7 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/**
 * 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 STORAGE
#include "ob_storage_struct.h"
#include "share/ob_rpc_struct.h"
#include "ob_partition_split.h"
#include "storage/ob_tenant_file_struct.h"
#include "storage/ob_tenant_file_mgr.h"
#include "storage/transaction/ob_trans_ctx_mgr.h"

using namespace oceanbase;
using namespace storage;
using namespace common;
using namespace oceanbase::share::schema;
using namespace oceanbase::share;

OB_SERIALIZE_MEMBER(ObReportStatus, data_version_, row_count_, row_checksum_, data_checksum_, data_size_,
    required_size_, snapshot_version_);

OB_SERIALIZE_MEMBER(ObPGReportStatus, data_version_, data_size_, required_size_, snapshot_version_);

OB_SERIALIZE_MEMBER(ObPartitionStoreMeta, pkey_, is_restore_, replica_type_, saved_split_state_, migrate_status_,
    migrate_timestamp_, storage_info_, report_status_, multi_version_start_, data_table_id_, split_info_,
    replica_property_, create_timestamp_);

ObPartitionStoreMeta::ObPartitionStoreMeta()
{
  reset();
}

ObPartitionStoreMeta::~ObPartitionStoreMeta()
{
  reset();
}

bool ObPartitionStoreMeta::is_valid() const
{
  bool is_valid = true;
  if (!pkey_.is_valid()) {
    is_valid = false;
  } else {
    if (pkey_.is_pg()) {
      is_valid = multi_version_start_ > 0 ? true : false;
    } else {
      is_valid = inner_is_valid();
    }
  }
  return is_valid;
}

bool ObPartitionStoreMeta::inner_is_valid() const
{
  return (pkey_.is_valid() && is_restore_ >= 0 && ObReplicaTypeCheck::is_replica_type_valid(replica_type_) &&
          storage_info_.is_valid() && multi_version_start_ > 0 && replica_property_.is_valid() &&
          create_timestamp_ >= 0);
}

void ObPartitionStoreMeta::reset()
{
  pkey_.reset();
  is_restore_ = 0;
  replica_type_ = REPLICA_TYPE_MAX;
  saved_split_state_ = static_cast<int64_t>(UNKNOWN_SPLIT_STATE);
  storage_info_.reset();

  report_status_.reset();

  migrate_status_ = OB_MIGRATE_STATUS_NONE;
  migrate_timestamp_ = 0;
  multi_version_start_ = 0;
  data_table_id_ = 0;
  split_info_.reset();
  replica_property_.reset();
  create_timestamp_ = 0;
}

M
mw0 已提交
87
int ObPartitionStoreMeta::deep_copy(const ObPartitionStoreMeta &meta)
O
oceanbase-admin 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
  int ret = OB_SUCCESS;

  if (is_valid()) {
    ret = OB_INIT_TWICE;
    LOG_WARN("meta is valid, cannot overwrite", K(ret), K(*this));
  } else if (OB_FAIL(storage_info_.deep_copy(meta.storage_info_))) {
    LOG_WARN("failed to copy saved storage info", K(ret));
  } else if (OB_FAIL(split_info_.assign(meta.split_info_))) {
    STORAGE_LOG(WARN, "failed to assign split info", K(ret), K(meta.split_info_));
  } else {
    pkey_ = meta.pkey_;
    is_restore_ = meta.is_restore_;
    migrate_status_ = meta.migrate_status_;
    migrate_timestamp_ = meta.migrate_timestamp_;
    replica_type_ = meta.replica_type_;
    saved_split_state_ = meta.saved_split_state_;
    report_status_ = meta.report_status_;
    multi_version_start_ = meta.multi_version_start_;
    data_table_id_ = meta.data_table_id_;
    replica_property_ = meta.replica_property_;
    create_timestamp_ = meta.create_timestamp_;
  }

  return ret;
}

ObPGPartitionStoreMeta::ObPGPartitionStoreMeta()
{
  reset();
}

ObPGPartitionStoreMeta::~ObPGPartitionStoreMeta()
{
  reset();
}

void ObPGPartitionStoreMeta::reset()
{
  pkey_.reset();
  report_status_.reset();
  multi_version_start_ = 0;
  data_table_id_ = 0;
  create_schema_version_ = 0;
  create_timestamp_ = 0;
  replica_type_ = ObReplicaType::REPLICA_TYPE_MAX;
}

bool ObPGPartitionStoreMeta::is_valid() const
{
  return pkey_.is_valid() && multi_version_start_ > 0 && create_schema_version_ >= 0 && create_timestamp_ >= 0;
}

M
mw0 已提交
141
int ObPGPartitionStoreMeta::deep_copy(const ObPGPartitionStoreMeta &meta)
O
oceanbase-admin 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
{
  int ret = OB_SUCCESS;
  if (is_valid()) {
    ret = OB_INIT_TWICE;
    LOG_WARN("meta is valid, cannot overwrite", K(ret), K(*this));
  } else {
    pkey_ = meta.pkey_;
    report_status_ = meta.report_status_;
    multi_version_start_ = meta.multi_version_start_;
    data_table_id_ = meta.data_table_id_;
    create_schema_version_ = meta.create_schema_version_;
    replica_type_ = meta.replica_type_;
    create_timestamp_ = meta.create_timestamp_;
  }
  return ret;
}

M
mw0 已提交
159
int ObPGPartitionStoreMeta::copy_from_old_meta(const ObPartitionStoreMeta &meta)
O
oceanbase-admin 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
{
  int ret = OB_SUCCESS;
  if (is_valid()) {
    ret = OB_INIT_TWICE;
    LOG_WARN("meta is valid, cannot overwrite", K(ret), K(*this));
  } else {
    pkey_ = meta.pkey_;
    report_status_ = meta.report_status_;
    multi_version_start_ = meta.multi_version_start_;
    data_table_id_ = meta.data_table_id_;
    replica_type_ = meta.replica_type_;
  }
  return ret;
}

OB_SERIALIZE_MEMBER(ObPGPartitionStoreMeta, pkey_, report_status_, multi_version_start_, data_table_id_,
    create_schema_version_, create_timestamp_);

OB_SERIALIZE_MEMBER(ObPartitionGroupMeta, pg_key_, is_restore_, replica_type_, replica_property_, saved_split_state_,
    migrate_status_, migrate_timestamp_, storage_info_, split_info_, partitions_, report_status_,
    create_schema_version_, ddl_seq_num_, create_timestamp_, create_frozen_version_, last_restore_log_id_,
181
    restore_snapshot_version_, last_restore_log_ts_, restore_schema_version_);
O
oceanbase-admin 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

ObPartitionGroupMeta::ObPartitionGroupMeta()
{
  reset();
}

ObPartitionGroupMeta::~ObPartitionGroupMeta()
{
  reset();
}

bool ObPartitionGroupMeta::is_valid() const
{
  return pg_key_.is_valid() && (is_restore_ >= 0) && ObReplicaTypeCheck::is_replica_type_valid(replica_type_) &&
         replica_property_.is_valid() && storage_info_.is_valid() && create_schema_version_ >= 0 && ddl_seq_num_ >= 0 &&
         create_frozen_version_ >= 0 && restore_snapshot_version_ >= OB_INVALID_TIMESTAMP &&
198 199 200 201 202
         restore_schema_version_ >= OB_INVALID_TIMESTAMP &&
         (is_restore_ != REPLICA_RESTORE_LOG || restore_snapshot_version_ > 0) &&
         (is_inner_table(pg_key_.get_table_id()) ||
             (!is_inner_table(pg_key_.get_table_id()) &&
                 (is_restore_ != REPLICA_RESTORE_LOG || restore_schema_version_ > 0)));
O
oceanbase-admin 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
}

void ObPartitionGroupMeta::reset()
{
  pg_key_.reset();
  is_restore_ = 0;
  replica_type_ = REPLICA_TYPE_MAX;
  replica_property_.reset();
  saved_split_state_ = static_cast<int64_t>(UNKNOWN_SPLIT_STATE);
  storage_info_.reset();
  migrate_status_ = OB_MIGRATE_STATUS_NONE;
  migrate_timestamp_ = 0;
  split_info_.reset();
  partitions_.reset();
  create_schema_version_ = 0;
  ddl_seq_num_ = 0;
  create_timestamp_ = 0;
  create_frozen_version_ = 0;
  last_restore_log_id_ = OB_INVALID_ID;
  restore_snapshot_version_ = OB_INVALID_TIMESTAMP;
D
dt0 已提交
223
  last_restore_log_ts_ = OB_INVALID_TIMESTAMP;
224
  restore_schema_version_ = OB_INVALID_TIMESTAMP;
O
oceanbase-admin 已提交
225 226
}

M
mw0 已提交
227
int ObPartitionGroupMeta::deep_copy(const ObPartitionGroupMeta &meta)
O
oceanbase-admin 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
{
  int ret = OB_SUCCESS;

  if (is_valid()) {
    ret = OB_INIT_TWICE;
    LOG_WARN("meta is valid, cannot overwrite", K(ret), K(*this));
  } else if (this == &meta) {
    LOG_WARN("deep copy the same meta, no need to copy", K(meta), K(*this));
  } else if (OB_FAIL(storage_info_.deep_copy(meta.storage_info_))) {
    LOG_WARN("failed to copy saved storage info", K(ret), K(meta));
  } else if (OB_FAIL(partitions_.assign(meta.partitions_))) {
    LOG_WARN("failed to assign partitions", K(ret), K(meta));
  } else if (OB_FAIL(split_info_.assign(meta.split_info_))) {
    LOG_WARN("failed to assign split info", K(ret), K(meta));
  } else {
    pg_key_ = meta.pg_key_;
    is_restore_ = meta.is_restore_;
    migrate_status_ = meta.migrate_status_;
    migrate_timestamp_ = meta.migrate_timestamp_;
    replica_type_ = meta.replica_type_;
    replica_property_ = meta.replica_property_;
    saved_split_state_ = meta.saved_split_state_;
    report_status_ = meta.report_status_;
    create_schema_version_ = meta.create_schema_version_;
    ddl_seq_num_ = meta.ddl_seq_num_;
    create_timestamp_ = meta.create_timestamp_;
    create_frozen_version_ = meta.create_frozen_version_;
    last_restore_log_id_ = meta.last_restore_log_id_;
    restore_snapshot_version_ = meta.restore_snapshot_version_;
D
dt0 已提交
257
    last_restore_log_ts_ = meta.last_restore_log_ts_;
258
    restore_schema_version_ = meta.restore_schema_version_;
O
oceanbase-admin 已提交
259 260 261 262 263
  }

  return ret;
}

M
mw0 已提交
264
int ObPartitionGroupMeta::copy_from_store_meta(const ObPartitionStoreMeta &meta)
O
oceanbase-admin 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
{
  int ret = OB_SUCCESS;
  if (is_valid()) {
    ret = OB_INIT_TWICE;
    LOG_WARN("meta is valid, cannot overwrite", K(ret), K(*this));
  } else if (OB_FAIL(storage_info_.deep_copy(meta.storage_info_))) {
    LOG_WARN("failed to copy storage info", K(ret), K(meta));
  } else if (OB_FAIL(split_info_.assign(meta.split_info_))) {
    LOG_WARN("failed to assign split info", K(ret), K(meta));
  } else {
    pg_key_ = meta.pkey_;
    is_restore_ = meta.is_restore_;
    migrate_status_ = meta.migrate_status_;
    migrate_timestamp_ = meta.migrate_timestamp_;
    replica_type_ = meta.replica_type_;
    saved_split_state_ = meta.saved_split_state_;
    report_status_.data_size_ = meta.report_status_.data_size_;
    report_status_.data_version_ = meta.report_status_.data_version_;
    report_status_.required_size_ = meta.report_status_.required_size_;
    replica_property_ = meta.replica_property_;
    create_schema_version_ = meta.storage_info_.get_data_info().get_schema_version();
    create_timestamp_ = meta.create_timestamp_;
  }
  return ret;
}

M
mw0 已提交
291
int ObPartitionGroupMeta::get_recover_info_for_flashback(const int64_t, ObRecoverPoint &)
O
oceanbase-admin 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
{
  return OB_NOT_SUPPORTED;
}

int ObPartitionGroupMeta::clear_recover_points_for_physical_flashback(const int64_t)
{
  return OB_NOT_SUPPORTED;
}

int64_t ObPartitionGroupMeta::get_migrate_replay_log_ts() const
{
  int64_t replay_log_ts = 0;
  if (0 == partitions_.count() || !ObReplicaTypeCheck::is_replica_with_ssstore(replica_type_)) {
    replay_log_ts = storage_info_.get_clog_info().get_submit_timestamp();
  } else {
    replay_log_ts = storage_info_.get_data_info().get_last_replay_log_ts();
  }
  return replay_log_ts;
}

ObGetMergeTablesParam::ObGetMergeTablesParam()
    : merge_type_(INVALID_MERGE_TYPE),
      index_id_(OB_INVALID_ID),
      merge_version_(),
      trans_table_end_log_ts_(-1),
      trans_table_timestamp_(-1)
{}

bool ObGetMergeTablesParam::is_valid() const
{
  return OB_INVALID_ID != index_id_ && (merge_type_ > INVALID_MERGE_TYPE && merge_type_ < MERGE_TYPE_MAX) &&
         (!is_major_merge() || merge_version_.is_valid());
}

ObGetMergeTablesResult::ObGetMergeTablesResult()
    : version_range_(),
      handle_(),
      merge_version_(),
      base_schema_version_(-1),
      schema_version_(-1),
      create_snapshot_version_(-1),
      checksum_method_(-1),
      suggest_merge_type_(INVALID_MERGE_TYPE),
      create_sstable_for_large_snapshot_(false),
      log_ts_range_(),
      dump_memtable_timestamp_(0),
      read_base_version_(0)
{}

bool ObGetMergeTablesResult::is_valid() const
{
  return log_ts_range_.is_valid() && handle_.get_count() >= 1 && (merge_version_.is_valid() || 0 == merge_version_) &&
         base_schema_version_ >= 0 && schema_version_ >= 0 && create_snapshot_version_ >= 0 &&
         dump_memtable_timestamp_ >= 0 &&
         (suggest_merge_type_ > INVALID_MERGE_TYPE && suggest_merge_type_ < MERGE_TYPE_MAX);
}

void ObGetMergeTablesResult::reset()
{
  version_range_.reset();
  handle_.reset();
  base_handle_.reset();
  merge_version_.reset();
  base_schema_version_ = -1;
  schema_version_ = -1;
  create_snapshot_version_ = 0;
  suggest_merge_type_ = INVALID_MERGE_TYPE;
  create_sstable_for_large_snapshot_ = false;
  checksum_method_ = -1;
  log_ts_range_.reset();
  dump_memtable_timestamp_ = 0;
  read_base_version_ = 0;
}

M
mw0 已提交
366
int ObGetMergeTablesResult::deep_copy(const ObGetMergeTablesResult &src)
O
oceanbase-admin 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
{
  int ret = OB_SUCCESS;
  if (!src.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(src));
  } else if (OB_FAIL(handle_.assign(src.handle_))) {
    LOG_WARN("failed to copy handle", K(ret));
  } else if (OB_FAIL(base_handle_.assign(src.base_handle_))) {
    LOG_WARN("failed to copy base handle", K(ret));
  } else {
    version_range_ = src.version_range_;
    merge_version_ = src.merge_version_;
    base_schema_version_ = src.base_schema_version_;
    schema_version_ = src.schema_version_;
    create_snapshot_version_ = src.create_snapshot_version_;
    checksum_method_ = src.checksum_method_;
    suggest_merge_type_ = src.suggest_merge_type_;
    create_sstable_for_large_snapshot_ = src.create_sstable_for_large_snapshot_;
    log_ts_range_ = src.log_ts_range_;
    dump_memtable_timestamp_ = src.dump_memtable_timestamp_;
  }
  return ret;
}

AddTableParam::AddTableParam()
    : table_(NULL),
      max_kept_major_version_number_(-1),
      multi_version_start_(-1),
      in_slog_trans_(false),
      need_prewarm_(false),
      is_daily_merge_(false),
      complement_minor_sstable_(nullptr),
      schema_version_(0)
{}

bool AddTableParam::is_valid() const
{
  return (!is_daily_merge_ || (OB_NOT_NULL(table_) || OB_NOT_NULL(complement_minor_sstable_))) &&
M
mw0 已提交
405
         max_kept_major_version_number_ >= 0 && multi_version_start_ > ObVersionRange::MIN_VERSION;
O
oceanbase-admin 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
}

ObPartitionReadableInfo::ObPartitionReadableInfo()
    : min_log_service_ts_(0),
      min_trans_service_ts_(0),
      min_replay_engine_ts_(0),
      generated_ts_(0),
      max_readable_ts_(OB_INVALID_TIMESTAMP),
      force_(false)
{}

ObPartitionReadableInfo::~ObPartitionReadableInfo()
{}

bool ObPartitionReadableInfo::is_valid() const
{
  return min_replay_engine_ts_ > 0 && min_trans_service_ts_ > 0 && min_log_service_ts_ > 0 && max_readable_ts_ > 0;
}

void ObPartitionReadableInfo::calc_readable_ts()
{
  // ignore current transaction by subtract 1
  max_readable_ts_ = MIN(MIN(min_log_service_ts_, min_replay_engine_ts_), min_trans_service_ts_) - 1;
  generated_ts_ = ObTimeUtility::current_time();
}

void ObPartitionReadableInfo::reset()
{
  min_log_service_ts_ = 0;
  min_trans_service_ts_ = 0;
  min_replay_engine_ts_ = 0;
  generated_ts_ = 0;
  max_readable_ts_ = OB_INVALID_TIMESTAMP;
  force_ = false;
}

M
mw0 已提交
442
int ObMigrateStatusHelper::trans_replica_op(const ObReplicaOpType &op_type, ObMigrateStatus &migrate_status)
O
oceanbase-admin 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
{
  int ret = OB_SUCCESS;
  migrate_status = OB_MIGRATE_STATUS_MAX;

  if (!is_replica_op_valid(op_type)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret), K(op_type));
  } else {
    switch (op_type) {
      case ADD_REPLICA_OP: {
        migrate_status = OB_MIGRATE_STATUS_ADD;
        break;
      }
      case FAST_MIGRATE_REPLICA_OP:
      case MIGRATE_REPLICA_OP: {
        migrate_status = OB_MIGRATE_STATUS_MIGRATE;
        break;
      }
      case REBUILD_REPLICA_OP: {
        migrate_status = OB_MIGRATE_STATUS_REBUILD;
        break;
      }
      case CHANGE_REPLICA_OP: {
        migrate_status = OB_MIGRATE_STATUS_CHANGE;
        break;
      }
      case RESTORE_REPLICA_OP: {
        migrate_status = OB_MIGRATE_STATUS_RESTORE;
        break;
      }
      case COPY_GLOBAL_INDEX_OP: {
        migrate_status = OB_MIGRATE_STATUS_COPY_GLOBAL_INDEX;
        break;
      }
      case COPY_LOCAL_INDEX_OP: {
        migrate_status = OB_MIGRATE_STATUS_COPY_LOCAL_INDEX;
        break;
      }
      case RESTORE_FOLLOWER_REPLICA_OP: {
        migrate_status = OB_MIGRATE_STATUS_RESTORE_FOLLOWER;
        break;
      }
      case RESTORE_STANDBY_OP: {
        migrate_status = OB_MIGRATE_STATUS_RESTORE_STANDBY;
        break;
      }
      case LINK_SHARE_MAJOR_OP: {
        migrate_status = OB_MIGRATE_STATUS_LINK_MAJOR;
        break;
      }

      default: {
        ret = OB_INVALID_ARGUMENT;
        LOG_ERROR("unknown op type", K(ret), K(op_type));
      }
    }
  }

  return ret;
}

M
mw0 已提交
504
int ObMigrateStatusHelper::trans_fail_status(const ObMigrateStatus &cur_status, ObMigrateStatus &fail_status)
O
oceanbase-admin 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
{
  int ret = OB_SUCCESS;
  fail_status = OB_MIGRATE_STATUS_MAX;

  if (cur_status < OB_MIGRATE_STATUS_NONE || cur_status >= OB_MIGRATE_STATUS_MAX) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret), K(cur_status));
  } else {
    switch (cur_status) {
      case OB_MIGRATE_STATUS_ADD: {
        fail_status = OB_MIGRATE_STATUS_ADD_FAIL;
        break;
      }
      case OB_MIGRATE_STATUS_MIGRATE: {
        fail_status = OB_MIGRATE_STATUS_MIGRATE_FAIL;
        break;
      }
      case OB_MIGRATE_STATUS_REBUILD: {
        fail_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_CHANGE: {
        fail_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_RESTORE: {
O
obdev 已提交
531 532
        // allow observer self reentry
        fail_status = OB_MIGRATE_STATUS_NONE;
O
oceanbase-admin 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
        break;
      }
      case OB_MIGRATE_STATUS_COPY_GLOBAL_INDEX: {
        fail_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_COPY_LOCAL_INDEX: {
        fail_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_HOLD: {
        fail_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_RESTORE_FOLLOWER: {
O
obdev 已提交
548 549
        // allow observer self reentry
        fail_status = OB_MIGRATE_STATUS_NONE;
O
oceanbase-admin 已提交
550 551 552
        break;
      }
      case OB_MIGRATE_STATUS_RESTORE_STANDBY: {
O
obdev 已提交
553 554
        // allow observer self reentry
        fail_status = OB_MIGRATE_STATUS_NONE;
O
oceanbase-admin 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
        break;
      }
      case OB_MIGRATE_STATUS_LINK_MAJOR: {
        fail_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      default: {
        ret = OB_INVALID_ARGUMENT;
        LOG_ERROR("invalid cur status for fail", K(ret), K(cur_status));
      }
    }
  }
  return ret;
}

M
mw0 已提交
570
int ObMigrateStatusHelper::trans_reboot_status(const ObMigrateStatus &cur_status, ObMigrateStatus &reboot_status)
O
oceanbase-admin 已提交
571 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 600 601 602 603
{
  int ret = OB_SUCCESS;
  reboot_status = OB_MIGRATE_STATUS_MAX;

  if (cur_status < OB_MIGRATE_STATUS_NONE || cur_status >= OB_MIGRATE_STATUS_MAX) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret), K(cur_status));
  } else {
    switch (cur_status) {
      case OB_MIGRATE_STATUS_NONE: {
        reboot_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_ADD:
      case OB_MIGRATE_STATUS_ADD_FAIL: {
        reboot_status = OB_MIGRATE_STATUS_ADD_FAIL;
        break;
      }
      case OB_MIGRATE_STATUS_MIGRATE:
      case OB_MIGRATE_STATUS_MIGRATE_FAIL: {
        reboot_status = OB_MIGRATE_STATUS_MIGRATE_FAIL;
        break;
      }
      case OB_MIGRATE_STATUS_REBUILD: {
        reboot_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_CHANGE: {
        reboot_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_RESTORE:
      case OB_MIGRATE_STATUS_RESTORE_FOLLOWER:
O
obdev 已提交
604 605 606 607
      case OB_MIGRATE_STATUS_RESTORE_STANDBY: {
        reboot_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
O
oceanbase-admin 已提交
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 649 650
      case OB_MIGRATE_STATUS_RESTORE_FAIL: {
        reboot_status = OB_MIGRATE_STATUS_RESTORE_FAIL;
        break;
      }
      case OB_MIGRATE_STATUS_COPY_GLOBAL_INDEX: {
        reboot_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_COPY_LOCAL_INDEX: {
        reboot_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_HOLD: {
        reboot_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      case OB_MIGRATE_STATUS_LINK_MAJOR: {
        reboot_status = OB_MIGRATE_STATUS_NONE;
        break;
      }
      default: {
        ret = OB_INVALID_ARGUMENT;
        LOG_ERROR("invalid cur status for fail", K(ret), K(cur_status));
      }
    }
  }
  return ret;
}

ObCreatePGParam::ObCreatePGParam()
    : info_(),
      is_restore_(0),
      replica_type_(ObReplicaType::REPLICA_TYPE_MAX),
      replica_property_(),
      data_version_(0),
      write_slog_(false),
      split_info_(),
      split_state_(0),
      create_timestamp_(0),
      file_handle_(nullptr),
      file_mgr_(nullptr),
      create_frozen_version_(0),
      last_restore_log_id_(OB_INVALID_ID),
D
dt0 已提交
651
      last_restore_log_ts_(OB_INVALID_TIMESTAMP),
O
oceanbase-admin 已提交
652
      restore_snapshot_version_(OB_INVALID_TIMESTAMP),
653
      restore_schema_version_(OB_INVALID_TIMESTAMP),
O
oceanbase-admin 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
      migrate_status_(ObMigrateStatus::OB_MIGRATE_STATUS_NONE)
{}

void ObCreatePGParam::reset()
{
  info_.reset();
  is_restore_ = 0;
  replica_type_ = ObReplicaType::REPLICA_TYPE_MAX;
  replica_property_.reset();
  data_version_ = 0;
  write_slog_ = false;
  split_info_.reset();
  split_state_ = 0;
  create_timestamp_ = 0;
  file_handle_ = nullptr;
  file_mgr_ = nullptr;
  create_frozen_version_ = 0;
  last_restore_log_id_ = OB_INVALID_ID;
D
dt0 已提交
672
  last_restore_log_ts_ = OB_INVALID_TIMESTAMP;
O
oceanbase-admin 已提交
673
  restore_snapshot_version_ = OB_INVALID_TIMESTAMP;
674
  restore_schema_version_ = OB_INVALID_TIMESTAMP;
O
oceanbase-admin 已提交
675 676 677 678 679 680 681 682
  migrate_status_ = ObMigrateStatus::OB_MIGRATE_STATUS_NONE;
}

bool ObCreatePGParam::is_valid() const
{
  return info_.is_valid() && ObReplicaTypeCheck::is_replica_type_valid(replica_type_) && replica_property_.is_valid() &&
         (is_restore_ >= 0) && create_timestamp_ >= 0 && nullptr != file_handle_ && nullptr != file_mgr_ &&
         create_frozen_version_ >= 0 && restore_snapshot_version_ >= OB_INVALID_TIMESTAMP &&
683
         restore_schema_version_ >= OB_INVALID_TIMESTAMP && migrate_status_ < ObMigrateStatus::OB_MIGRATE_STATUS_MAX;
O
oceanbase-admin 已提交
684 685
}

M
mw0 已提交
686
int ObCreatePGParam::assign(const ObCreatePGParam &param)
O
oceanbase-admin 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
{
  int ret = OB_SUCCESS;
  if (!param.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("create pg param is invalid argument", K(ret), K(param));
  } else if (OB_FAIL(info_.deep_copy(param.info_))) {
    LOG_WARN("failed to copy saved storage info", K(ret), K(param));
  } else if (OB_FAIL(split_info_.assign(param.split_info_))) {
    LOG_WARN("failed to assign split info", K(ret), K(param));
  } else {
    is_restore_ = param.is_restore_;
    replica_type_ = param.replica_type_;
    replica_property_ = param.replica_property_;
    data_version_ = param.data_version_;
    write_slog_ = param.write_slog_;
    split_state_ = param.split_state_;
    create_timestamp_ = param.create_timestamp_;
    file_handle_ = param.file_handle_;
    file_mgr_ = param.file_mgr_;
    create_frozen_version_ = param.create_frozen_version_;
    last_restore_log_id_ = param.last_restore_log_id_;
D
dt0 已提交
708
    last_restore_log_ts_ = param.last_restore_log_ts_;
O
oceanbase-admin 已提交
709
    restore_snapshot_version_ = param.restore_snapshot_version_;
710
    restore_schema_version_ = param.restore_schema_version_;
O
oceanbase-admin 已提交
711 712 713 714 715
    migrate_status_ = param.migrate_status_;
  }
  return ret;
}

M
mw0 已提交
716
int ObCreatePGParam::set_storage_info(const ObSavedStorageInfoV2 &info)
O
oceanbase-admin 已提交
717 718 719 720 721 722 723 724
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(info_.deep_copy(info))) {
    LOG_WARN("failed to deep copy saved storage info", K(ret), K(info));
  }
  return ret;
}

M
mw0 已提交
725
int ObCreatePGParam::set_split_info(const ObPartitionSplitInfo &split_info)
O
oceanbase-admin 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(split_info_.assign(split_info))) {
    LOG_WARN("failed to assign split info", K(ret), K(split_info));
  }
  return ret;
}
void ObCreatePartitionMeta::reset()
{
  is_valid_ = false;
  table_id_ = OB_INVALID_ID;
  data_table_id_ = OB_INVALID_ID;
  progressive_merge_round_ = 0;
  is_global_index_table_ = false;
  table_type_ = ObTableType::USER_TABLE;
  table_mode_.reset();
  index_type_ = ObIndexType::INDEX_TYPE_IS_NOT;
  rowkey_column_num_ = 0;
  column_ids_.reset();
  id_hash_array_ = NULL;
}

M
mw0 已提交
748
int ObCreatePartitionMeta::extract_from(const ObTableSchema &table_schema)
O
oceanbase-admin 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!table_schema.is_valid())) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid arguments", K(ret), K(table_schema));
  } else if (OB_FAIL(table_schema.get_store_column_ids(column_ids_))) {
    LOG_WARN("failed to get_store_column_ids", K(ret), K(table_schema));
  } else {
    table_id_ = table_schema.get_table_id();
    data_table_id_ = table_schema.get_data_table_id();
    progressive_merge_round_ = table_schema.get_progressive_merge_round();
    is_global_index_table_ = table_schema.is_global_index_table();
    table_type_ = table_schema.get_table_type();
    table_mode_ = table_schema.get_table_mode_struct();
    index_type_ = table_schema.get_index_type();
    rowkey_column_num_ = table_schema.get_rowkey_column_num();
    id_hash_array_ = table_schema.get_id_hash_array();
    is_valid_ = true;
  }
  return ret;
}

M
mw0 已提交
771
const ObColumnSchemaV2 *ObCreatePartitionMeta::get_column_schema(const uint64_t column_id) const
O
oceanbase-admin 已提交
772
{
M
mw0 已提交
773
  ObColumnSchemaV2 *column = NULL;
O
oceanbase-admin 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
  if (NULL != id_hash_array_) {
    if (OB_SUCCESS != id_hash_array_->get_refactored(ObColumnIdKey(column_id), column)) {
      column = NULL;
    }
  }
  return column;
}

int ObCreatePartitionMeta::replace_tenant_id(const uint64_t new_tenant_id)
{
  int ret = OB_SUCCESS;
  if (!is_valid_) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "not inited", KR(ret), K(new_tenant_id), K(*this));
  } else if (new_tenant_id == extract_tenant_id(table_id_)) {
    // no need to replace
  } else {
    table_id_ = combine_id(new_tenant_id, table_id_);
    data_table_id_ = combine_id(new_tenant_id, data_table_id_);
  }
  return ret;
}

M
mw0 已提交
797
int ObCreatePartitionMeta::assign(const ObCreatePartitionMeta &other)
O
oceanbase-admin 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(column_ids_.assign(other.get_store_column_ids()))) {
    STORAGE_LOG(WARN, "failed to assign column_ids", KR(ret), K(other), K(*this));
  } else {
    is_valid_ = other.is_valid();
    table_id_ = other.get_table_id();
    data_table_id_ = other.get_data_table_id();
    progressive_merge_round_ = other.get_progressive_merge_round();
    is_global_index_table_ = other.is_global_index_table();
    table_type_ = other.get_table_type();
    table_mode_ = other.get_table_mode();
    index_type_ = other.get_index_type();
    rowkey_column_num_ = other.get_rowkey_column_num();
    id_hash_array_ = other.get_id_hash_array();
  }
  return ret;
}

void ObCreatePartitionParam::reset()
{
  is_valid_ = false;
  member_list_.reset();
  partition_key_.reset();
  need_create_sstable_ = false;
  ;
  schema_version_ = 0;
  memstore_version_ = 0;
  lease_start_ = 0;
  replica_type_ = common::REPLICA_TYPE_MAX;
  restore_ = share::REPLICA_NOT_RESTORE;
  frozen_timestamp_ = 0;
  pg_key_.reset();
  schemas_.reset();
}

M
mw0 已提交
834
int ObCreatePartitionParam::extract_from(const obrpc::ObCreatePartitionArg &arg)
O
oceanbase-admin 已提交
835 836 837 838
{
  int ret = OB_SUCCESS;
  const int64_t table_cnt = arg.table_schemas_.count();
  for (int64_t i = 0; OB_SUCC(ret) && i < table_cnt; ++i) {
M
mw0 已提交
839
    const ObTableSchema &table_schema = arg.table_schemas_.at(i);
O
oceanbase-admin 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
    ObCreatePartitionMeta partition_schema;
    if (OB_FAIL(partition_schema.extract_from(table_schema))) {
      STORAGE_LOG(WARN, "failed to extract partition_schema", KR(ret), K(table_schema));
    } else if (OB_FAIL(schemas_.push_back(partition_schema))) {
      STORAGE_LOG(WARN, "member list deep copy error", KR(ret), K(partition_schema));
    } else { /*do nothing*/
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(member_list_.deep_copy(arg.member_list_))) {
      STORAGE_LOG(WARN, "member list deep copy error", K(arg.member_list_));
    } else if (OB_FAIL(arg.check_need_create_sstable(need_create_sstable_))) {
      STORAGE_LOG(WARN, "failed to check_nened_create_sstable", K(arg));
    } else {
      partition_key_ = arg.partition_key_;
      schema_version_ = arg.schema_version_;
      memstore_version_ = arg.memstore_version_;
      lease_start_ = arg.lease_start_;
      replica_type_ = arg.replica_type_;
      restore_ = arg.restore_;
      frozen_timestamp_ = arg.frozen_timestamp_;
      pg_key_ = arg.pg_key_;
      is_valid_ = true;
    }
  }
  return ret;
}

int ObCreatePartitionParam::replace_tenant_id(const uint64_t new_tenant_id)
{
  int ret = OB_SUCCESS;
  if (!is_valid_) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "not inited", KR(ret), K(new_tenant_id), K(*this));
  } else if (new_tenant_id == partition_key_.get_tenant_id()) {
    // no need to replace
  } else if (OB_FAIL(partition_key_.replace_tenant_id(new_tenant_id))) {
    STORAGE_LOG(WARN, "failed to replace_tenant_id of partition_key_", KR(ret), K(new_tenant_id), K(*this));
  } else if (OB_FAIL(pg_key_.replace_tenant_id(new_tenant_id))) {
    STORAGE_LOG(WARN, "failed to replace_tenant_id of pg_key_", KR(ret), K(new_tenant_id), K(*this));
  } else {
    const int64_t cnt = schemas_.count();
    for (int64_t i = 0; OB_SUCC(ret) && i < cnt; ++i) {
M
mw0 已提交
883
      ObCreatePartitionMeta &partition_schema = schemas_.at(i);
O
oceanbase-admin 已提交
884 885 886 887 888 889 890 891 892 893 894
      if (OB_FAIL(partition_schema.replace_tenant_id(new_tenant_id))) {
        STORAGE_LOG(WARN, "failed to replace_tenant_id of partition_scheam", KR(ret), K(*this));
      }
    }
  }
  return ret;
}

/**********************ObRecoveryPointSchemaFilter***********************/
ObRecoveryPointSchemaFilter::ObRecoveryPointSchemaFilter()
    : is_inited_(false),
895
      is_restore_point_(false),
O
oceanbase-admin 已提交
896 897 898
      tenant_id_(OB_INVALID_ID),
      tenant_recovery_point_schema_version_(OB_INVALID_VERSION),
      tenant_current_schema_version_(OB_INVALID_VERSION),
899 900
      recovery_point_schema_guard_(share::schema::ObSchemaMgrItem::MOD_RECOVER_POINT),
      current_schema_guard_(share::schema::ObSchemaMgrItem::MOD_RECOVER_POINT),
O
oceanbase-admin 已提交
901 902 903 904 905 906 907 908 909 910 911
      is_schema_version_same_(false)
{}

ObRecoveryPointSchemaFilter::~ObRecoveryPointSchemaFilter()
{}

bool ObRecoveryPointSchemaFilter::is_inited() const
{
  return is_inited_;
}

M
mw0 已提交
912 913
int ObRecoveryPointSchemaFilter::init(const int64_t tenant_id, const bool is_restore_point,
    const int64_t tenant_recovery_point_schema_version, const int64_t tenant_current_schema_version)
O
oceanbase-admin 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(is_inited_)) {
    ret = OB_INIT_TWICE;
    STORAGE_LOG(WARN, "appender already inited", K(ret));
  } else if (OB_INVALID_ID == tenant_id || tenant_recovery_point_schema_version < OB_INVALID_VERSION) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN,
        "init backup schema checker get invalid argument",
        K(ret),
        K(tenant_id),
        K(tenant_recovery_point_schema_version),
        K(tenant_current_schema_version));
  } else {
    is_schema_version_same_ = (tenant_recovery_point_schema_version == tenant_current_schema_version);
M
mw0 已提交
929
    ObMultiVersionSchemaService &schema_service = ObMultiVersionSchemaService::get_instance();
O
oceanbase-admin 已提交
930 931 932
    if (OB_FAIL(ObBackupUtils::retry_get_tenant_schema_guard(
            tenant_id, schema_service, tenant_recovery_point_schema_version, recovery_point_schema_guard_))) {
      STORAGE_LOG(WARN,
933
          "failed to get tenant backup schema guard",
O
oceanbase-admin 已提交
934 935 936 937 938 939 940 941
          K(ret),
          K(tenant_id),
          K(tenant_recovery_point_schema_version));
    } else if (!is_schema_version_same_ &&
               OB_FAIL(ObBackupUtils::retry_get_tenant_schema_guard(
                   tenant_id, schema_service, tenant_current_schema_version, current_schema_guard_))) {
      STORAGE_LOG(WARN, "failed to get tenant current schema guard", K(ret), K(tenant_current_schema_version));
    } else {
942
      is_restore_point_ = is_restore_point;
O
oceanbase-admin 已提交
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
      tenant_id_ = tenant_id;
      tenant_recovery_point_schema_version_ = tenant_recovery_point_schema_version;
      tenant_current_schema_version_ = tenant_current_schema_version;
      is_inited_ = true;
      STORAGE_LOG(INFO,
          "init backup schema filter succ",
          K(tenant_id),
          K(tenant_recovery_point_schema_version),
          K(tenant_current_schema_version));
    }
  }

  return ret;
}

M
mw0 已提交
958
int ObRecoveryPointSchemaFilter::check_partition_exist(const ObPartitionKey pkey, bool &is_exist)
O
oceanbase-admin 已提交
959 960 961 962
{
  int ret = OB_SUCCESS;
  bool check_dropped_partition = false;
  is_exist = false;
M
mw0 已提交
963
  ObSchemaGetterGuard *schema_guard = NULL;
O
oceanbase-admin 已提交
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
  bool is_exist_in_backup_schema = false;
  bool is_exist_in_delay_delete = false;
  bool is_exist_in_current_schema = false;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "backup schema filter is not init", K(ret), K(pkey));
  } else if (!pkey.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "check partition exist get invalid argument", K(ret), K(pkey));
  } else if (FALSE_IT(schema_guard = &recovery_point_schema_guard_)) {
  } else if (OB_FAIL(check_partition_exist_(pkey, check_dropped_partition, *schema_guard, is_exist_in_backup_schema))) {
    STORAGE_LOG(WARN, "failed to check partition exist", K(ret), K(pkey));
  } else if (FALSE_IT(check_dropped_partition = true)) {
  } else if (OB_FAIL(check_partition_exist_(pkey, check_dropped_partition, *schema_guard, is_exist_in_delay_delete))) {
    STORAGE_LOG(WARN, "failed to check partition exist", K(ret), K(pkey));
  } else if (is_schema_version_same_) {
    is_exist = is_exist_in_backup_schema;
  } else if (FALSE_IT(schema_guard = &current_schema_guard_)) {
  } else if (OB_FAIL(
                 check_partition_exist_(pkey, check_dropped_partition, *schema_guard, is_exist_in_current_schema))) {
    STORAGE_LOG(WARN, "failed to check partition exist", K(ret), K(pkey));
  } else if (is_exist_in_backup_schema && !is_exist_in_current_schema) {
    ret = OB_ERR_UNEXPECTED;
    STORAGE_LOG(WARN,
        "table exist in backup schema, but not exist in current schema",
        K(ret),
        K(tenant_recovery_point_schema_version_),
        K(tenant_current_schema_version_));
  } else if (!is_exist_in_backup_schema && is_exist_in_delay_delete) {
    is_exist = false;
  } else {
    is_exist = is_exist_in_backup_schema || is_exist_in_current_schema;
  }
  return ret;
}

int ObRecoveryPointSchemaFilter::check_partition_exist_(
M
mw0 已提交
1002
    const ObPartitionKey pkey, const bool check_dropped_partition, ObSchemaGetterGuard &schema_guard, bool &is_exist)
O
oceanbase-admin 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
{
  int ret = OB_SUCCESS;
  is_exist = false;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "backup schema filter is not init", K(ret), K(pkey));
  } else if (pkey.is_trans_table()) {
    is_exist = true;
  } else if (OB_FAIL(schema_guard.check_partition_exist(
                 pkey.get_table_id(), pkey.get_partition_id(), check_dropped_partition, is_exist))) {
    STORAGE_LOG(WARN, "schema guard check partition fail", K(ret), K(pkey));
  }
  return ret;
}

M
mw0 已提交
1018
int ObRecoveryPointSchemaFilter::check_table_exist(const uint64_t table_id, bool &is_exist)
O
oceanbase-admin 已提交
1019 1020 1021
{
  int ret = OB_SUCCESS;
  is_exist = false;
M
mw0 已提交
1022
  ObSchemaGetterGuard *schema_guard = NULL;
O
oceanbase-admin 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "backup schema filter is not init", K(ret), K(table_id));
  } else if (OB_INVALID_ID == table_id) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "check table exist get invalid argument", K(ret), K(table_id));
  } else if (FALSE_IT(schema_guard = &recovery_point_schema_guard_)) {
  } else if (OB_FAIL(check_table_exist_(table_id, *schema_guard, is_exist))) {
    STORAGE_LOG(WARN, "failed to check table exist", K(ret), K(table_id));
  } else if (is_exist) {
    // do nothing
  } else if (is_schema_version_same_) {
    // do nothing
  } else if (FALSE_IT(schema_guard = &current_schema_guard_)) {
  } else if (OB_FAIL(check_table_exist_(table_id, *schema_guard, is_exist))) {
    STORAGE_LOG(WARN, "failed to check table exist", K(ret), K(table_id));
  }
  return ret;
}

int ObRecoveryPointSchemaFilter::check_table_exist_(
M
mw0 已提交
1045
    const uint64_t table_id, ObSchemaGetterGuard &schema_guard, bool &is_exist)
O
oceanbase-admin 已提交
1046 1047 1048
{
  int ret = OB_SUCCESS;
  is_exist = false;
M
mw0 已提交
1049
  const ObTableSchema *table_schema = NULL;
O
oceanbase-admin 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  bool need_skip = false;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "backup schema filter is not init", K(ret), K(table_id));
  } else if (is_trans_table_id(table_id)) {
    is_exist = true;
  } else if (OB_FAIL(schema_guard.check_table_exist(table_id, is_exist))) {
    STORAGE_LOG(WARN, "schema guard check table fail", K(ret), K(table_id));
  } else if (!is_exist) {
    // do nothing
  } else if (OB_FAIL(schema_guard.get_table_schema(table_id, table_schema))) {
    STORAGE_LOG(WARN, "failed to get table schema", K(ret), K(table_id));
M
mw0 已提交
1062 1063
  } else if (OB_FAIL(ObBackupRestoreTableSchemaChecker::check_backup_restore_need_skip_table(
                 table_schema, need_skip, is_restore_point_))) {
O
oceanbase-admin 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072
    LOG_WARN("failed to check backup restore need skip table", K(ret), K(table_id));
  } else if (!need_skip) {
    // do nothing
  } else {
    is_exist = false;
  }
  return ret;
}

M
mw0 已提交
1073
int ObRecoveryPointSchemaFilter::do_filter_tables(common::ObIArray<uint64_t> &table_ids)
O
oceanbase-admin 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
{
  int ret = OB_SUCCESS;
  ObArray<uint64_t> backup_tables;
  bool filtered = false;

  if (OB_UNLIKELY(!is_inited())) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "filter not inited", K(ret));
  } else {
    bool is_exist = false;
    for (int i = 0; OB_SUCC(ret) && i < table_ids.count(); ++i) {
      uint64_t table_id = table_ids.at(i);
      if (OB_FAIL(check_table_exist(table_id, is_exist))) {
        STORAGE_LOG(WARN, "backup filter check table failed", K(ret), K(table_id));
      } else if (is_exist) {
        if (OB_FAIL(backup_tables.push_back(table_id))) {
          STORAGE_LOG(WARN, "push back table id failed", K(ret), K(table_id));
        }
      } else {
        filtered = true;
        FLOG_INFO("backup table is not exist, no need backup", K(table_id));

#ifdef ERRSIM
        if (OB_SUCC(ret)) {
          ret = E(EventTable::EN_BACKUP_FILTER_TABLE_BY_SCHEMA) OB_SUCCESS;
        }
#endif
      }
    }
  }

  if (OB_SUCC(ret) && filtered) {
    table_ids.reuse();
    if (OB_FAIL(table_ids.assign(backup_tables))) {
      STORAGE_LOG(WARN, "assign table id failed", K(ret), K(backup_tables));
    }
  }
  return ret;
}

int ObRecoveryPointSchemaFilter::do_filter_pg_partitions(
M
mw0 已提交
1115
    const ObPartitionKey &pg_key, common::ObPartitionArray &partitions)
O
oceanbase-admin 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
{
  int ret = OB_SUCCESS;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "filter not inited", K(ret));
  } else {
    bool is_exist = false;
    bool filtered = false;
    ObPartitionArray exist_partitions;
    for (int i = 0; OB_SUCC(ret) && i < partitions.count(); ++i) {
M
mw0 已提交
1127
      const ObPartitionKey &pkey = partitions.at(i);
O
oceanbase-admin 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
      if (OB_FAIL(check_partition_exist(pkey, is_exist))) {
        STORAGE_LOG(WARN, "backup filter check partition failed", K(ret), K(pg_key), K(pkey));
      } else if (is_exist) {
        if (OB_FAIL(exist_partitions.push_back(pkey))) {
          STORAGE_LOG(WARN, "pusk back partition failed", K(ret), K(pkey));
        }
      } else {
        filtered = true;
        STORAGE_LOG(INFO, "backup partition is filtered", K(pkey));
      }
    }
    if (OB_SUCC(ret) && filtered) {
      partitions.reuse();
      if (OB_FAIL(partitions.assign(exist_partitions))) {
        STORAGE_LOG(WARN, "assign pg partitions failed", K(ret), K(exist_partitions));
      }
    }
  }

  return ret;
}

int ObRecoveryPointSchemaFilter::check_if_table_miss_by_schema(
M
mw0 已提交
1151
    const ObPartitionKey &pgkey, const hash::ObHashSet<uint64_t> &table_ids)
O
oceanbase-admin 已提交
1152 1153 1154 1155
{
  int ret = OB_SUCCESS;
  int hash_ret = OB_SUCCESS;
  ObArray<uint64_t> schema_tables;
M
mw0 已提交
1156
  ObSchemaGetterGuard *schema_guard = NULL;
O
oceanbase-admin 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172

  if (OB_UNLIKELY(!is_inited())) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "filter not inited", K(ret));
  } else if (FALSE_IT(schema_guard = &recovery_point_schema_guard_)) {
  } else if (OB_FAIL(check_if_table_miss_by_schema_(pgkey, table_ids, *schema_guard))) {
    STORAGE_LOG(WARN, "failed to check if table miss by schema", K(ret), K(pgkey));
  } else if (is_schema_version_same_) {
    // do nothing
  } else if (FALSE_IT(schema_guard = &current_schema_guard_)) {
  } else if (OB_FAIL(check_if_table_miss_by_schema_(pgkey, table_ids, *schema_guard))) {
    STORAGE_LOG(WARN, "failed to check if table miss by schema", K(ret), K(pgkey));
  }
  return ret;
}

M
mw0 已提交
1173 1174
int ObRecoveryPointSchemaFilter::check_if_table_miss_by_schema_(const ObPartitionKey &pgkey,
    const common::hash::ObHashSet<uint64_t> &table_ids, share::schema::ObSchemaGetterGuard &schema_guard)
O
oceanbase-admin 已提交
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
{
  int ret = OB_SUCCESS;
  int hash_ret = OB_SUCCESS;
  ObArray<uint64_t> schema_tables;

  if (OB_UNLIKELY(!is_inited())) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "filter not inited", K(ret));
  } else if (OB_FAIL(get_table_ids_in_pg_(pgkey, schema_tables, schema_guard))) {
    STORAGE_LOG(WARN, "get pg tables fail", K(ret), K(pgkey));
  } else {
    for (int i = 0; OB_SUCC(ret) && i < schema_tables.count(); ++i) {
      hash_ret = table_ids.exist_refactored(schema_tables.at(i));
      if (OB_HASH_EXIST == hash_ret) {
        // do nothing
      } else {
        ret = OB_SUCCESS == hash_ret ? OB_ERR_UNEXPECTED : hash_ret;
        STORAGE_LOG(
            WARN, "backup table is miss", K(ret), K(hash_ret), K(pgkey), K(table_ids), K(i), K(schema_tables.at(i)));
      }
    }
  }
  return ret;
}

M
mw0 已提交
1200 1201
int ObRecoveryPointSchemaFilter::get_table_ids_in_pg_(const ObPartitionKey &pgkey,
    common::ObIArray<uint64_t> &table_ids, share::schema::ObSchemaGetterGuard &schema_guard)
O
oceanbase-admin 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
{
  int ret = OB_SUCCESS;
  common::ObArray<uint64_t> data_table_ids;
  common::ObArray<uint64_t> local_index_ids;
  ObArray<ObIndexTableStat> index_stats;
  table_ids.reuse();
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "backup schema filter is not init", K(ret), K(pgkey));
  } else if (pgkey.is_pg()) {
    if (OB_FAIL(schema_guard.get_table_ids_in_tablegroup(
            pgkey.get_tenant_id(), pgkey.get_tablegroup_id(), data_table_ids))) {
      STORAGE_LOG(WARN, "get pg table ids fail", K(ret), K(pgkey));
    }
  } else if (OB_FAIL(data_table_ids.push_back(pgkey.get_table_id()))) {
    STORAGE_LOG(WARN, "push back data table id fail", K(ret), K(pgkey));
  }

  if (OB_SUCC(ret)) {
    for (int64_t i = 0; OB_SUCC(ret) && i < data_table_ids.count(); ++i) {
      const uint64_t data_table_id = data_table_ids.at(i);
M
mw0 已提交
1223
      const ObTableSchema *table_schema = NULL;
O
oceanbase-admin 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238

      if (OB_FAIL(schema_guard.get_table_schema(data_table_id, table_schema))) {
        STORAGE_LOG(WARN, "failed to get table schema", K(ret), K(data_table_id));
      } else if (OB_ISNULL(table_schema)) {
        ret = OB_ERR_UNEXPECTED;
        STORAGE_LOG(WARN, "table schema should not be NULL", K(ret), KP(table_schema));
      } else if (table_schema->is_dropped_schema()) {
        // do nothing
        FLOG_INFO("table is dropped schema, skip check", K(data_table_id));
      } else if (OB_FAIL(table_ids.push_back(data_table_id))) {
        STORAGE_LOG(WARN, "failed to push data table id into array", K(ret), K(data_table_id));
      } else if (OB_FAIL(schema_guard.get_index_status(data_table_id, false /*with global index*/, index_stats))) {
        STORAGE_LOG(WARN, "get local index status fail", K(ret), K(pgkey));
      } else {
        for (int64_t j = 0; OB_SUCC(ret) && j < index_stats.count(); ++j) {
M
mw0 已提交
1239 1240
          const ObTableSchema *index_table_schema = NULL;
          const ObIndexTableStat &index_table_stat = index_stats.at(j);
O
oceanbase-admin 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
          if (INDEX_STATUS_AVAILABLE != index_table_stat.index_status_) {
            // filter unavailable index
          } else if (OB_FAIL(schema_guard.get_table_schema(index_table_stat.index_id_, index_table_schema))) {
            STORAGE_LOG(WARN, "failed to get table schema", K(ret), K(index_table_stat));
          } else if (OB_ISNULL(index_table_schema)) {
            ret = OB_ERR_UNEXPECTED;
            STORAGE_LOG(WARN, "index table schema should not be NULL", K(ret), KP(index_table_schema));
          } else if (index_table_schema->is_dropped_schema()) {
            // do nothing
            FLOG_INFO("index table is dropped schema, skip check", K(data_table_id), K(index_table_stat));
          } else if (OB_FAIL(table_ids.push_back(index_stats.at(j).index_id_))) {
            STORAGE_LOG(WARN, "push back index id fail", K(ret), K(pgkey));
          }
        }
      }
    }
  }
  return ret;
}

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
int ObRecoveryPointSchemaFilter::check_table_exist_in_recovery_schema(const uint64_t table_id, bool &is_exist)
{
  int ret = OB_SUCCESS;
  ObSchemaGetterGuard *schema_guard = NULL;
  is_exist = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("recovery point schema filter do not init", K(ret));
  } else if (OB_INVALID_ID == table_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("check table is local index get invalid argument");
  } else if (FALSE_IT(schema_guard = &recovery_point_schema_guard_)) {
  } else if (OB_FAIL(check_table_exist_(table_id, *schema_guard, is_exist))) {
    STORAGE_LOG(WARN, "failed to check table exist", K(ret), K(table_id));
  }
  return ret;
}

O
oceanbase-admin 已提交
1280 1281
/***********************ObBackupRestoreTableSchemaChecker***************************/
int ObBackupRestoreTableSchemaChecker::check_backup_restore_need_skip_table(
M
mw0 已提交
1282
    const share::schema::ObTableSchema *table_schema, bool &need_skip, const bool is_restore_point)
O
oceanbase-admin 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
{
  int ret = OB_SUCCESS;
  ObIndexStatus index_status;
  need_skip = true;
  int64_t table_id = 0;

  if (OB_ISNULL(table_schema)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("check backup restore need skip table get invalid argument", K(ret), KP(table_schema));
  } else if (FALSE_IT(table_id = table_schema->get_table_id())) {
  } else if (table_schema->is_dropped_schema()) {
    STORAGE_LOG(INFO, "table is dropped, skip it", K(table_id));
  } else if (FALSE_IT(index_status = table_schema->get_index_status())) {
M
mw0 已提交
1296 1297 1298 1299 1300 1301 1302 1303
  } else if (table_schema->is_index_table() &&
             (is_restore_point ? !is_final_index_status(index_status, table_schema->is_dropped_schema())
                               : ObIndexStatus::INDEX_STATUS_AVAILABLE != index_status)) {
    STORAGE_LOG(INFO,
        "restore table index is not expected status, skip it",
        K(is_restore_point),
        K(index_status),
        K(*table_schema));
O
oceanbase-admin 已提交
1304 1305 1306 1307 1308 1309
  } else {
    need_skip = false;
  }
  return ret;
}

M
mw0 已提交
1310
ObRebuildListener::ObRebuildListener(transaction::ObPartitionTransCtxMgr &mgr) : ob_partition_ctx_mgr_(mgr)
O
oceanbase-admin 已提交
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
{
  int tmp_ret = OB_SUCCESS;
  while (OB_SUCCESS != (tmp_ret = ob_partition_ctx_mgr_.lock_minor_merge_lock())) {
    STORAGE_LOG(ERROR, "lock minor merge lock failed, we need retry forever", K(tmp_ret));
  }
}

ObRebuildListener::~ObRebuildListener()
{
  int tmp_ret = OB_SUCCESS;
  while (OB_SUCCESS != (tmp_ret = ob_partition_ctx_mgr_.unlock_minor_merge_lock())) {
    STORAGE_LOG(ERROR, "unlock minor merge lock failed, we need retry forever", K(tmp_ret));
  }
}

bool ObRebuildListener::on_partition_rebuild()
{
  bool ret = false;

  if (ob_partition_ctx_mgr_.is_partition_stopped()) {
    STORAGE_LOG(INFO, "rebuild listener find rebuild is on doing");
    ret = true;
  }

  return ret;
}

int ObRestoreFakeMemberListHelper::fake_restore_member_list(
M
mw0 已提交
1339
    const int64_t replica_cnt, common::ObMemberList &fake_member_list)
O
oceanbase-admin 已提交
1340 1341 1342 1343
{
  int ret = OB_SUCCESS;
  fake_member_list.reset();

M
mw0 已提交
1344
  const char *fake_ip = "127.0.0.1";
O
oceanbase-admin 已提交
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
  int32_t fake_port = 10000;

  if (replica_cnt <= 0) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "fake restore member list get invalid argument", K(ret), K(replica_cnt));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < replica_cnt; ++i) {
      fake_port = fake_port + i;
      ObAddr fake_addr(ObAddr::IPV4, fake_ip, fake_port);
      ObMember fake_member(fake_addr, 0);
      if (!fake_member.is_valid()) {
        ret = OB_ERR_UNEXPECTED;
        STORAGE_LOG(WARN, "fake member is not valid", K(ret), K(fake_member));
      } else if (OB_FAIL(fake_member_list.add_member(fake_member))) {
        STORAGE_LOG(WARN, "failed to fake member list", K(ret), K(fake_member));
      }
    }
  }
  return ret;
}