ob_partition_table_iterator.cpp 43.8 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/**
 * 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_PT

#include "ob_partition_table_iterator.h"

#include "lib/profile/ob_trace_id.h"
#include "lib/container/ob_array_iterator.h"
#include "share/schema/ob_schema_getter_guard.h"
#include "share/schema/ob_multi_version_schema_service.h"
#include "share/schema/ob_schema_getter_guard.h"
#include "share/schema/ob_part_mgr_util.h"
#include "share/schema/ob_schema_mgr.h"
#include "share/inner_table/ob_inner_table_schema.h"
#include "share/config/ob_server_config.h"
#include "ob_partition_table_operator.h"

namespace oceanbase {
namespace share {

using namespace common;
using namespace schema;

void ObPartIdAscIterator::reset()
{
  sorted_part_id_array_.reset();
  part_level_ = PARTITION_LEVEL_MAX;
  cur_idx_ = 0;
}

int ObPartIdAscIterator::build(const uint64_t partition_entity_id,
    const share::schema::ObPartitionSchema& partition_schema, const bool filter_dropped_schema)
{
  int ret = OB_SUCCESS;
  reset();
  ObPartitionKeyIter part_key_iter(partition_entity_id, partition_schema, !filter_dropped_schema);
  int64_t phy_part_id = -1;
  int64_t partition_num = part_key_iter.get_partition_num();
  if (OB_FAIL(sorted_part_id_array_.reserve(partition_num))) {
    LOG_WARN("Failed to reserve array", K(ret), K(partition_num));
  }
  while (OB_SUCC(ret) && OB_SUCC(part_key_iter.next_partition_id_v2(phy_part_id))) {
    if (OB_FAIL(sorted_part_id_array_.push_back(phy_part_id))) {
      LOG_WARN("fail to push back", K(ret));
    }
  }
  if (OB_ITER_END == ret) {
    ret = OB_SUCCESS;
  }
  if (OB_SUCC(ret)) {
    std::sort(sorted_part_id_array_.begin(), sorted_part_id_array_.end());
    part_level_ = partition_schema.get_part_level();
  }
  return ret;
}

int ObPartIdAscIterator::generate_next_part_id(int64_t& part_id) const
{
  int ret = OB_SUCCESS;
  if (cur_idx_ >= sorted_part_id_array_.count()) {
    ret = OB_ITER_END;
  } else {
    part_id = sorted_part_id_array_.at(cur_idx_);
  }
  return ret;
}

bool ObPartIdAscIterator::is_iter_end() const
{
  return cur_idx_ >= sorted_part_id_array_.count();
}

void ObPartIdAscIterator::inc_iter()
{
  ++cur_idx_;
}

int ObPartIdAscIterator::check_out_of_part_id_range(const int64_t partition_id, bool& out_of_range) const
{
  int ret = OB_SUCCESS;
  out_of_range = true;
  if (sorted_part_id_array_.count() <= 0) {
    out_of_range = true;
  } else {
    common::ObArray<int64_t>::const_iterator pos =
        std::lower_bound(sorted_part_id_array_.begin(), sorted_part_id_array_.end(), partition_id);
    if (pos != sorted_part_id_array_.end() && *pos == partition_id) {
      out_of_range = false;
    } else {
      out_of_range = true;
    }
  }
  return ret;
}

int ObTablePartitionIterator::ObPrefetchInfo::init(uint64_t table_id, ObPartitionTableOperator& pt_operator)
{
  int ret = OB_SUCCESS;
  if (OB_INVALID_ID == table_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(table_id));
  } else {
    table_id_ = table_id;
    pt_operator_ = &pt_operator;
  }
  return ret;
}
int ObTablePartitionIterator::ObPrefetchInfo::get(ObPartitionInfo& partition)
{
  int ret = OB_SUCCESS;
  if (need_prefetch()) {
    if (OB_FAIL(prefetch())) {
      LOG_WARN("fail to prefetch", K(ret), K_(table_id));
    }
  }
  if (OB_SUCC(ret)) {
    if (prefetch_idx_ >= prefetch_count()) {
      prefetch_iter_end_ = true;
      ret = OB_ITER_END;
    } else if (OB_FAIL(partition.assign(prefetch_partitions_.at(prefetch_idx_)))) {
      LOG_WARN("fail to assgign partition", K(ret));
    }
  }
  return ret;
}

int ObTablePartitionIterator::ObPrefetchInfo::next(ObPartitionInfo& partition)
{
  int ret = OB_SUCCESS;
  if (prefetch_idx_ >= prefetch_count()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid next", K(prefetch_idx_), K(prefetch_count()));
  } else if (OB_FAIL(partition.assign(prefetch_partitions_.at(prefetch_idx_)))) {
    LOG_WARN("fail to assign partition", K(ret), K(prefetch_idx_));
  } else {
    prefetch_idx_++;
  }
  return ret;
}

int ObTablePartitionIterator::ObPrefetchInfo::prefetch()
{
  int ret = OB_SUCCESS;
  if (prefetch_idx_ < prefetch_partitions_.count()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("no need to do prefetch", K(ret), K(prefetch_idx_), K(prefetch_partitions_.count()));
  } else if (OB_ISNULL(pt_operator_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid argument", K(ret), K(pt_operator_));
  } else {
    int64_t start_partition_id = 0;
    bool first_prefetch = true;
    if (prefetch_partitions_.count() > 0) {
      start_partition_id = prefetch_partitions_.at(prefetch_partitions_.count() - 1).get_partition_id();
      first_prefetch = false;
    }
    uint64_t tenant_id = extract_tenant_id(table_id_);
    prefetch_idx_ = 0;
    prefetch_partitions_.reuse();
170 171 172 173 174 175 176 177 178 179 180 181
    if (OB_FAIL(pt_operator_->prefetch_by_table_id(tenant_id,
            table_id_,
            start_partition_id,
            prefetch_partitions_,
            need_fetch_faillist_,
            filter_flag_replica_))) {
      LOG_WARN("fail to prefetch partitions",
          K(ret),
          K(table_id_),
          K(start_partition_id),
          K_(need_fetch_faillist),
          K_(filter_flag_replica));
O
oceanbase-admin 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    } else if (!first_prefetch) {
      prefetch_idx_++;  // the first partition is duplicated, need to be filtered
    }
  }
  return ret;
}
int64_t ObTablePartitionIterator::to_string(char* buf, const int64_t buf_len) const
{
  int64_t pos = 0;
  J_OBJ_START();
  J_KV(KT_(table_id));
  J_OBJ_END();
  return pos;
}

ObTablePartitionIterator::ObTablePartitionIterator()
    : inited_(false),
      table_id_(OB_INVALID_ID),
      part_level_(PARTITION_LEVEL_ZERO),
      asc_part_id_iterator_(),
      pt_operator_(NULL),
      allocator_(ObModIds::OB_RS_PARTITION_TABLE_TEMP),
      prefetch_info_(),
      filters_()
{}

ObTablePartitionIterator::~ObTablePartitionIterator()
{}

// check if we need to access the tenant level meta table by the mode
// when TablePartitionIterator::init is invoked
int ObTablePartitionIterator::init(
214 215
    const uint64_t table_id, ObSchemaGetterGuard& schema_guard, ObPartitionTableOperator& pt_operator,
    const bool filter_flag_replica /* = true*/)
O
oceanbase-admin 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
{
  int ret = OB_SUCCESS;
  const uint64_t tenant_id = extract_tenant_id(table_id);
  if (OB_INVALID_ID == table_id || OB_INVALID_ID == tenant_id || OB_INVALID_ID == extract_pure_id(table_id)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(table_id));
  } else if (OB_FAIL(prefetch_info_.init(table_id, pt_operator))) {
    LOG_WARN("fail to init prefetch info", K(ret));
  } else {
    share::schema::ObSchemaType schema_type =
        (is_new_tablegroup_id(table_id) ? ObSchemaType::TABLEGROUP_SCHEMA : ObSchemaType::TABLE_SCHEMA);
    const share::schema::ObPartitionSchema* partition_schema = nullptr;
    const ObSimpleTenantSchema* tenant = NULL;
    if (OB_FAIL(share::schema::ObPartMgrUtils::get_partition_schema(
            schema_guard, table_id, schema_type, partition_schema))) {
      LOG_WARN("fail to get partition schema", K(ret), "schema_id", table_id);
    } else if (OB_FAIL(schema_guard.get_tenant_info(tenant_id, tenant))) {
      LOG_WARN("fail to get tenant info", K(ret), K(tenant_id));
    } else if (OB_ISNULL(tenant)) {
      ret = OB_TENANT_NOT_EXIST;
      LOG_WARN("tenant not exist", K(ret), K(tenant_id));
    } else if (OB_FAIL(asc_part_id_iterator_.build(table_id, *partition_schema, tenant->is_restore()))) {
      LOG_WARN("fail to build asc part id iterator", K(ret));
    } else {
      table_id_ = table_id;
      pt_operator_ = &pt_operator;
      part_level_ = partition_schema->get_part_level();
      prefetch_info_.reset();
      prefetch_info_.set_need_fetch_faillist(need_fetch_faillist_);
245
      prefetch_info_.set_filter_flag_replica(filter_flag_replica);
O
oceanbase-admin 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 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 553 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 582 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 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 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 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 790 791 792 793 794 795 796 797 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 834 835 836 837 838 839 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 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 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
      allocator_.reuse();
      inited_ = true;
    }
  }
  return ret;
}

int ObTablePartitionIterator::next(ObPartitionInfo& partition)
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (NULL == partition.get_allocator()) {
    partition.set_allocator(&allocator_);
  }
  bool will_break = false;
  do {
    will_break = true;
    ObPartitionInfo tmp_partition;
    tmp_partition.set_allocator(&allocator_);
    bool prefetch_end = false;
    if (OB_SUCC(ret)) {
      if (OB_FAIL(prefetch_info_.get(tmp_partition))) {
        if (OB_ITER_END == ret) {
          prefetch_end = true;
          ret = OB_SUCCESS;
        } else {
          LOG_WARN("fail to get next prefetch partition info", K(ret));
        }
      }
    }
    if (OB_SUCC(ret)) {
      CheckType type = LEGAL_REPLICA;
      if (prefetch_end) {
        if (asc_part_id_iterator_.is_iter_end()) {
          ret = OB_ITER_END;
        } else {
          if (OB_FAIL(mock_next_partition(partition))) {
            LOG_WARN("fail to mock next partition", K(ret));
          }
        }
      } else if (OB_FAIL(check_replica(tmp_partition, type))) {
        LOG_WARN("fail to check replica", K(ret), K(tmp_partition));
      } else if (LEGAL_REPLICA == type || REDUNDANT_REPLICA == type) {
        if (OB_FAIL(prefetch_info_.next(partition))) {
          LOG_WARN("fail to get next partition", K(ret));
        }
      } else if (LOST_REPLICA == type) {
        if (OB_FAIL(mock_next_partition(partition))) {
          LOG_WARN("fail to mock next partition", K(ret));
        }
      } else if (OUTOF_SCHEMA_REPLICA == type) {
        // do not care ret, just let it iterate
        ret = prefetch_info_.next(tmp_partition);
        will_break = false;
      } else {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("unexpected replica type", K(type), K(ret));
      }
    }
  } while (OB_SUCC(ret) && !will_break);

  if (OB_SUCC(ret)) {
    if (OB_FAIL(partition.filter(filters_))) {
      LOG_WARN("filter replica failed", K(ret), K(partition));
    } else {
      asc_part_id_iterator_.inc_iter();
    }
  }

  return ret;
}

int ObTablePartitionIterator::mock_next_partition(ObPartitionInfo& info)
{
  int ret = OB_SUCCESS;
  int64_t next_partition_id = -1;
  if (OB_FAIL(asc_part_id_iterator_.generate_next_part_id(next_partition_id))) {
    LOG_WARN("fail to get next part id", K(ret));
  } else {
    info.set_table_id(table_id_);
    info.set_partition_id(next_partition_id);
  }
  return ret;
}

int ObTablePartitionIterator::check_replica(ObPartitionInfo& info, CheckType& check_type)
{
  int ret = OB_SUCCESS;
  check_type = INVALID_REPLICA;

  bool out_of = false;
  int64_t expect_partition_id = 0;
  if (OB_FAIL(asc_part_id_iterator_.check_out_of_part_id_range(info.get_partition_id(), out_of))) {
    LOG_WARN("fail to check out of range", K(info.get_partition_id()), K(ret));
  } else if (out_of) {
    check_type = OUTOF_SCHEMA_REPLICA;
  } else if (asc_part_id_iterator_.is_iter_end()) {
    check_type = REDUNDANT_REPLICA;
  } else if (OB_FAIL(asc_part_id_iterator_.generate_next_part_id(expect_partition_id))) {
    LOG_WARN("fail to get next part id", K(ret));
  } else if (info.get_partition_id() == expect_partition_id) {
    check_type = LEGAL_REPLICA;
  } else if (info.get_partition_id() > expect_partition_id) {
    // expected <10>, got <11> or expected <1.5>, got <1.6>
    check_type = LOST_REPLICA;
  } else {
    // expected <18>, got <17>  or expected <1,6>, got <1,5>
    check_type = REDUNDANT_REPLICA;
  }
  return ret;
}

///////////////////

ObTenantPartitionIterator::ObTenantPartitionIterator()
    : inited_(false),
      pt_operator_(NULL),
      schema_service_(NULL),
      partition_entity_iterator_(),
      allocator_(ObModIds::OB_RS_PARTITION_TABLE_TEMP),
      filters_(),
      tenant_id_(OB_INVALID_ID),
      prefetch_partitions_(),
      prefetch_idx_(0),
      tenant_end_(false),
      partition_entity_id_(OB_INVALID_ID),
      asc_part_id_iterator_(),
      ignore_row_checksum_(true),
      partition_entity_filters_(),
      filter_dropped_schema_(false)
{}

ObTenantPartitionIterator::~ObTenantPartitionIterator()
{}

int ObTenantPartitionIterator::init(ObPartitionTableOperator& pt_operator, ObMultiVersionSchemaService& schema_service,
    const uint64_t tenant_id, bool ignore_row_checksum)
{
  int ret = OB_SUCCESS;
  // allow init twice
  const ObSimpleTenantSchema* tenant = NULL;
  if (OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid tenant_id", K(tenant_id), K(ret));
  } else if (OB_FAIL(filters_.set_valid_version())) {
    LOG_WARN("set valid version filter failed", K(ret));
  } else if (OB_FAIL(schema_service.get_tenant_schema_guard(tenant_id, schema_guard_))) {
    LOG_WARN("get schema guard failed", K(ret));
  } else if (OB_FAIL(schema_guard_.get_tenant_info(tenant_id, tenant))) {
    LOG_WARN("fail to get tenant info", K(ret), K(tenant_id));
  } else if (OB_ISNULL(tenant)) {
    ret = OB_TENANT_NOT_EXIST;
    LOG_WARN("tenant not exist", K(ret), K(tenant_id));
  } else if (OB_FAIL(partition_entity_iterator_.init(schema_guard_, tenant_id))) {
    LOG_WARN("table iterator init failed", K(tenant_id), K(ret));
  } else {
    pt_operator_ = &pt_operator;
    schema_service_ = &schema_service;
    tenant_id_ = tenant_id;
    prefetch_partitions_.reuse();
    prefetch_idx_ = 0;
    tenant_end_ = false;
    partition_entity_id_ = OB_INVALID_ID;
    ignore_row_checksum_ = ignore_row_checksum;
    filter_dropped_schema_ = tenant->is_restore();
    inited_ = true;
  }
  return ret;
}

int ObTenantPartitionIterator::next_partition_entity()
{
  int ret = OB_SUCCESS;
  uint64_t this_partition_entity_id = OB_INVALID_ID;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  }
  ObArray<share::ObZoneReplicaAttrSet> zone_locality;
  while (OB_SUCC(ret) && OB_SUCC(partition_entity_iterator_.next(this_partition_entity_id))) {
    ObSchema tmp_schema;
    ObPrimaryZone primary_zone_info(&tmp_schema);
    asc_part_id_iterator_.reset();
    zone_locality.reuse();
    const share::schema::ObPartitionSchema* partition_schema = nullptr;
    bool pass = true;
    const share::schema::ObSchemaType schema_type =
        (is_new_tablegroup_id(this_partition_entity_id) ? ObSchemaType::TABLEGROUP_SCHEMA : ObSchemaType::TABLE_SCHEMA);
    if (OB_FAIL(share::schema::ObPartMgrUtils::get_partition_schema(
            schema_guard_, this_partition_entity_id, schema_type, partition_schema))) {
      LOG_WARN("fail to get partition schema", K(ret), K(this_partition_entity_id));
    } else if (!partition_schema->has_self_partition()) {
      pass = false;  // has no partition
    } else if (filter_dropped_schema_ && partition_schema->is_dropped_schema()) {
      pass = false;  // filter dropped schema while restore
    } else if (OB_FAIL(partition_schema->get_primary_zone_inherit(schema_guard_, primary_zone_info))) {
      LOG_WARN("fail to get primary zone inherit", K(ret));
    } else if (OB_FAIL(partition_schema->get_zone_replica_attr_array_inherit(schema_guard_, zone_locality))) {
      LOG_WARN("fail to get zone replica attr array inherit", K(ret));
    }
    if (OB_SUCC(ret)) {
      if (!pass) {
        // not pass, iterate the next one
      } else if (OB_FAIL(partition_entity_filters_.check(primary_zone_info, zone_locality, pass))) {
        LOG_WARN("fail to check table", K(ret), K(this_partition_entity_id));
      } else if (!pass) {
        // nothing todo
      } else if (OB_UNLIKELY(nullptr == partition_schema)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("partition_schema ptr is null", K(ret));
      } else if (OB_FAIL(asc_part_id_iterator_.build(
                     this_partition_entity_id, *partition_schema, filter_dropped_schema_))) {
        LOG_WARN("fail to build asc part id iterator", K(ret));
      } else {
        partition_entity_id_ = this_partition_entity_id;
        break;
      }
    }
  }

  if (OB_ITER_END == ret) {
    ret = OB_SUCCESS;
  }

  return ret;
}

int ObTenantPartitionIterator::inner_next(ObPartitionInfo& partition)
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    if (asc_part_id_iterator_.is_iter_end()) {
      ret = OB_ITER_END;  // table partition iterator end
    } else if (prefetch_idx_ >= prefetch_partitions_.count()) {
      // prefetch partition iterator end
      if (!tenant_end_ && OB_FAIL(prefetch())) {
        if (OB_ITER_END != ret) {
          LOG_WARN("prefetch failed", K(ret));
        } else {
          ret = OB_SUCCESS;
          tenant_end_ = true;
        }
      }
    }
  }

  if (OB_SUCC(ret)) {
    // skip partition of table already deleted
    while (OB_SUCCESS == ret && !tenant_end_ &&
           partition_entity_id_ > prefetch_partitions_.at(prefetch_idx_).get_table_id()) {
      // table_id of the next table is greater then table invalid table_id prefetched before
      ++prefetch_idx_;
      if (prefetch_idx_ == prefetch_partitions_.count()) {
        // prefetch partition iterator end
        if (OB_FAIL(prefetch())) {
          if (OB_ITER_END != ret) {
            LOG_WARN("prefetch failed", K(ret));
          } else {
            ret = OB_SUCCESS;
            tenant_end_ = true;
          }
        }
      }
    }
    int64_t expect_partition_id = -1;
    if (OB_SUCC(ret)) {
      if (OB_FAIL(asc_part_id_iterator_.generate_next_part_id(expect_partition_id))) {
        LOG_WARN("fail to get next part id", K(ret));
      }
    }
    int64_t part_id_fetch = 0;
    uint64_t table_id_fetch = 0;
    if (OB_SUCC(ret) && !tenant_end_) {
      part_id_fetch = prefetch_partitions_.at(prefetch_idx_).get_partition_id();
      table_id_fetch = prefetch_partitions_.at(prefetch_idx_).get_table_id();
    }
    if (OB_FAIL(ret)) {
      // this is used to process the situation of null partition,
      // it depends on the successive increment of paritition_id_,
      // however the paritition_id_ is not successive increment for sub partition
    } else if (tenant_end_ || partition_entity_id_ < table_id_fetch ||
               (partition_entity_id_ == table_id_fetch && expect_partition_id < part_id_fetch)) {
      // 1. exist in schema, but not in meta_table
      // return empty partition here, do not set error ret.
      LOG_WARN("find hole in partition table",
          K_(tenant_end),
          K_(partition_entity_id),
          K(expect_partition_id),
          K(table_id_fetch),
          K(part_id_fetch));
      partition.set_table_id(partition_entity_id_);
      partition.set_partition_id(expect_partition_id);
      asc_part_id_iterator_.inc_iter();
    } else if (partition_entity_id_ == table_id_fetch && expect_partition_id > part_id_fetch) {
      // 2. not exist in schema, but exist in meta_table
      LOG_DEBUG("prefetched partition_id smaller than partition to iterated",
          KR(ret),
          K(partition_entity_id_),
          K(expect_partition_id),
          K(table_id_fetch),
          K(part_id_fetch));
      // 2.1 ignore the prefetch_partitions for table/tablegroup schemas do not exist
      while (OB_SUCC(ret) && prefetch_idx_ < prefetch_partitions_.count() &&
             partition_entity_id_ == prefetch_partitions_.at(prefetch_idx_).get_table_id() &&
             expect_partition_id > prefetch_partitions_.at(prefetch_idx_).get_partition_id()) {
        ++prefetch_idx_;
        if (prefetch_idx_ == prefetch_partitions_.count()) {
          // 2.2 prefetch ignore the partitions whose schema do not exist
          const uint64_t last_table_id = partition_entity_id_;
          const int64_t last_partition_id = expect_partition_id - 1;
          if (OB_FAIL(prefetch(last_table_id, last_partition_id))) {
            if (OB_ITER_END != ret) {
              LOG_WARN("prefetch failed", KR(ret), K(last_table_id), K(last_partition_id));
            } else {
              ret = OB_SUCCESS;
              tenant_end_ = true;
            }
          }
        }
      }
      if (OB_SUCC(ret)) {
        if (OB_FAIL(inner_next(partition))) {
          if (OB_ITER_END != ret) {
            LOG_WARN("inner next failed", KR(ret));
          }
        }
      }
    } else {
      if (OB_FAIL(partition.assign(prefetch_partitions_[prefetch_idx_]))) {
        LOG_WARN("failed to assign partition", K(ret));
      } else {
        ++prefetch_idx_;
        asc_part_id_iterator_.inc_iter();
      }
    }
  }
  return ret;
}

int ObTenantPartitionIterator::next(ObPartitionInfo& partition)
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    if (NULL == partition.get_allocator()) {
      partition.set_allocator(&allocator_);
    }
    if (OB_INVALID_ID == partition_entity_id_) {
      if (OB_FAIL(next_partition_entity())) {
        if (OB_ITER_END != ret) {
          LOG_WARN("switch to next table failed", K(ret));
        }
      }
    }
  }

  do {
    if (OB_SUCC(ret)) {
      partition.reuse();
      if (OB_FAIL(inner_next(partition))) {
        if (OB_ITER_END != ret) {
          LOG_WARN("inner_next failed", K(ret));
        } else {
          if (OB_FAIL(next_partition_entity())) {
            if (OB_ITER_END != ret) {
              LOG_WARN("switch to next table failed", K(ret));
            }
          } else if (OB_FAIL(inner_next(partition))) {
            if (OB_ITER_END != ret) {
              LOG_WARN("inner next failed", K(ret));
            }
          }
        }
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(partition.filter(filters_))) {
        LOG_WARN("filter replica failed", K(ret), K(partition));
      }
    }
  } while (OB_SUCCESS == ret && 0 == partition.replica_count() && filters_.skip_empty_partition());
  return ret;
}

int ObTenantPartitionIterator::prefetch()
{
  int ret = OB_SUCCESS;
  uint64_t last_table_id = OB_INVALID_ID;
  int64_t last_partition_id = OB_INVALID_INDEX;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (prefetch_idx_ != prefetch_partitions_.count()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("prefetched partition infos have not been iterated to end",
        K_(prefetch_idx),
        "prefetch count",
        prefetch_partitions_.count(),
        K(ret));
  } else if (OB_ISNULL(pt_operator_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("pt_operator is null", K(ret));
  } else {
    // intialize table_id and the start partition_id
    if (prefetch_partitions_.count() <= 0) {  // try to prefetch the first time
      last_table_id = 0;
      last_partition_id = 0;
    } else {  // prefetched before
      const int64_t last_idx = prefetch_partitions_.count() - 1;
      last_table_id = prefetch_partitions_.at(last_idx).get_table_id();
      last_partition_id = prefetch_partitions_.at(last_idx).get_partition_id();
    }

    // prefetch will assure that last partition info contains all replicas of that partition
    prefetch_idx_ = 0;
    prefetch_partitions_.reuse();
    if (OB_FAIL(pt_operator_->prefetch(
            tenant_id_, last_table_id, last_partition_id, prefetch_partitions_, ignore_row_checksum_))) {
      LOG_WARN("pt_operator prefetch failed", K_(tenant_id), KT(last_table_id), K(last_partition_id), K(ret));
    } else if (prefetch_partitions_.count() <= 0) {
      // if the prefetch result is empty, iter end
      ret = OB_ITER_END;
    }
  }
  return ret;
}

int ObTenantPartitionIterator::prefetch(const uint64_t last_table_id, const int64_t last_partition_id)
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (prefetch_idx_ != prefetch_partitions_.count()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("prefetched partition infos have not been iterated to end",
        K_(prefetch_idx),
        "prefetch count",
        prefetch_partitions_.count(),
        K(ret));
  } else if (OB_ISNULL(pt_operator_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("pt_operator is null", K(ret));
  } else {
    // prefetch will assure that last partition info contains all replicas of that partition
    prefetch_idx_ = 0;
    prefetch_partitions_.reuse();
    if (OB_FAIL(pt_operator_->prefetch(
            tenant_id_, last_table_id, last_partition_id, prefetch_partitions_, ignore_row_checksum_))) {
      LOG_WARN("pt_operator prefetch failed", K_(tenant_id), KT(last_table_id), K(last_partition_id), K(ret));
    } else if (prefetch_partitions_.count() <= 0) {
      // if the prefetch result is empty, iter end
      ret = OB_ITER_END;
    }
  }
  return ret;
}

int64_t ObTenantPartitionIterator::to_string(char* buf, const int64_t buf_len) const
{
  int64_t pos = 0;
  J_OBJ_START();

  J_KV(K_(inited),
      K_(partition_entity_iterator),
      K_(tenant_id),
      "prefetch count",
      prefetch_partitions_.count(),
      K_(prefetch_idx),
      K_(tenant_end),
      K_(partition_entity_id));
  J_OBJ_END();
  return pos;
}

ObPartitionTableIterator::ObPartitionTableIterator()
    : inited_(false),
      pt_operator_(NULL),
      schema_service_(NULL),
      tenant_partition_iter_(),
      tenant_iter_(),
      ignore_row_checksum_(true)
{}

ObPartitionTableIterator::~ObPartitionTableIterator()
{}

int ObPartitionTableIterator::init(
    ObPartitionTableOperator& pt_operator, ObMultiVersionSchemaService& schema_service, bool ignore_row_checksum)
{
  int ret = OB_SUCCESS;
  ObCurTraceId::TraceId* trace_id = ObCurTraceId::get_trace_id();
  if (OB_ISNULL(trace_id)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("get invalid trace id", K(ret));
  } else if (trace_id->is_invalid()) {
    ObCurTraceId::init(GCONF.self_addr_);
  }
  if (OB_SUCC(ret) && inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("init twice", K(ret));
  } else if (OB_FAIL(tenant_iter_.init(schema_service))) {
    LOG_WARN("tenant iterator init failed", K(ret));
  } else {
    pt_operator_ = &pt_operator;
    schema_service_ = &schema_service;
    ignore_row_checksum_ = ignore_row_checksum;
    inited_ = true;
  }
  return ret;
}

int ObPartitionTableIterator::next(ObPartitionInfo& partition)
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    if (!tenant_partition_iter_.is_inited()) {
      if (OB_FAIL(next_tenant())) {
        if (OB_ITER_END != ret) {
          LOG_WARN("next_tenant failed", K(ret));
        }
      }
    }

    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(tenant_partition_iter_.next(partition))) {
      if (OB_ITER_END != ret) {
        LOG_WARN("tenant partition iterate failed", K(ret));
      } else {
        while (OB_ITER_END == ret) {
          if (OB_FAIL(next_tenant())) {
            if (OB_ITER_END != ret) {
              LOG_WARN("next_tenant failed", K(ret));
            } else {
              break;
            }
          } else {
            if (OB_FAIL(tenant_partition_iter_.next(partition))) {
              if (OB_ITER_END != ret) {
                LOG_WARN("tenant partition iterate failed", K(ret));
              }
            }
          }
        }
      }
    }
  }

  return ret;
}

int64_t ObPartitionTableIterator::to_string(char* buf, const int64_t buf_len) const
{
  int64_t pos = 0;
  J_OBJ_START();

  J_KV(K_(inited), K_(tenant_partition_iter), K_(tenant_iter));
  J_OBJ_END();
  return pos;
}

int ObPartitionTableIterator::next_tenant()
{
  int ret = OB_SUCCESS;
  uint64_t tenant_id = OB_INVALID_ID;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_FAIL(tenant_iter_.next(tenant_id))) {
    if (OB_ITER_END != ret) {
      LOG_WARN("tenant iterate failed", K(ret));
    }
  } else if (OB_FAIL(tenant_partition_iter_.init(*pt_operator_, *schema_service_, tenant_id, ignore_row_checksum_))) {
    LOG_WARN("tenant partition iterator init failed", K(tenant_id), K(ret));
  }
  return ret;
}

ObPTPartPartitionIterator::ObPTPartPartitionIterator()
    : inited_(false),
      pt_operator_(NULL),
      pt_table_id_(OB_INVALID_ID),
      pt_partition_id_(OB_INVALID_INDEX),
      prefetch_partitions_(),
      prefetch_idx_(0),
      allocator_(ObModIds::OB_RS_PARTITION_TABLE_TEMP),
      filters_()
{}

ObPTPartPartitionIterator::~ObPTPartPartitionIterator()
{}

int ObPTPartPartitionIterator::init(
    ObPartitionTableOperator& pt_operator, const uint64_t pt_table_id, const int64_t pt_partition_id)
{
  int ret = OB_SUCCESS;
  // allow init twice
  if (OB_INVALID_ID == pt_table_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid pt_table_id", K(pt_table_id), K(ret));
  } else if (pt_partition_id < 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid pt_partition_id", K(pt_partition_id), K(ret));
  } else {
    pt_operator_ = &pt_operator;
    pt_table_id_ = pt_table_id;
    pt_partition_id_ = pt_partition_id;
    prefetch_partitions_.reuse();
    prefetch_idx_ = 0;
    inited_ = true;
  }
  return ret;
}

int ObPTPartPartitionIterator::next(ObPartitionInfo& partition)
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    partition.reuse();
    if (NULL == partition.get_allocator()) {
      partition.set_allocator(&allocator_);
    }

    if (prefetch_idx_ < prefetch_partitions_.count()) {
      if (OB_FAIL(partition.assign(prefetch_partitions_[prefetch_idx_]))) {
        LOG_WARN("failed to assign partition", K(ret));
      } else {
        ++prefetch_idx_;
      }
    } else if (OB_FAIL(prefetch())) {
      if (OB_ITER_END != ret) {
        LOG_WARN("prefetch failed", K(ret));
      }
    } else {
      if (prefetch_idx_ >= prefetch_partitions_.count()) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("unexpected prefetch idx", K(ret), K_(prefetch_idx), "partition_count", prefetch_partitions_.count());
      } else {
        if (OB_FAIL(partition.assign(prefetch_partitions_[prefetch_idx_]))) {
          LOG_WARN("failed to assign partition", K(ret));
        } else {
          ++prefetch_idx_;
        }
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(partition.filter(filters_))) {
        LOG_WARN("filter replica failed", K(partition), K(ret));
      }
    }
  }
  return ret;
}

int ObPTPartPartitionIterator::prefetch()
{
  int ret = OB_SUCCESS;
  uint64_t last_table_id = 0;
  int64_t last_partition_id = 0;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (prefetch_idx_ != prefetch_partitions_.count()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("prefetched partition infos have not been iterated to end",
        K_(prefetch_idx),
        "prefetch count",
        prefetch_partitions_.count(),
        K(ret));
  } else if (OB_ISNULL(pt_operator_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("pt_operator is null", K(ret));
  } else {
    if (prefetch_partitions_.count() > 0) {
      const int64_t last_idx = prefetch_partitions_.count() - 1;
      last_table_id = prefetch_partitions_.at(last_idx).get_table_id();
      last_partition_id = prefetch_partitions_.at(last_idx).get_partition_id();
    }
    prefetch_idx_ = 0;
    prefetch_partitions_.reuse();
    const int64_t start = ObTimeUtility::current_time();
    if (OB_FAIL(pt_operator_->prefetch(pt_table_id_,
            pt_partition_id_,
            last_table_id,
            last_partition_id,
            prefetch_partitions_,
            need_fetch_faillist_))) {
      LOG_WARN("pt_operator prefetch failed",
          KT_(pt_table_id),
          K_(pt_partition_id),
          KT(last_table_id),
          K(last_partition_id),
          K(ret));
    } else if (prefetch_partitions_.count() <= 0) {
      ret = OB_ITER_END;
    }
    const int64_t cost = ObTimeUtility::current_time() - start;
    LOG_DEBUG("prefetch cost", K(cost));
  }
  return ret;
}

ObPartitionTableIdIterator::ObPartitionTableIdIterator()
    : inited_(false),
      schema_service_(NULL),
      tenant_iter_(),
      pt_tables_(),
      pt_table_id_(OB_INVALID_ID),
      pt_partition_id_(OB_INVALID_INDEX)
{}

ObPartitionTableIdIterator::~ObPartitionTableIdIterator()
{}

int ObPartitionTableIdIterator::init(ObMultiVersionSchemaService& schema_service)
{
  int ret = OB_SUCCESS;
  if (inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("init twice", K(ret));
  } else if (OB_FAIL(tenant_iter_.init(schema_service))) {
    LOG_WARN("tenant iterator init failed", K(ret));
  } else {
    pt_tables_[0] = OB_ALL_VIRTUAL_CORE_META_TABLE_TID;
    pt_tables_[1] = OB_ALL_CORE_TABLE_TID;
    pt_tables_[2] = OB_ALL_ROOT_TABLE_TID;
    pt_tables_[3] = OB_ALL_TENANT_META_TABLE_TID;
    schema_service_ = &schema_service;
    inited_ = true;
  }
  return ret;
}

int ObPartitionTableIdIterator::get_next_partition(uint64_t& pt_table_id, int64_t& pt_partition_id)
{
  int ret = OB_SUCCESS;

  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if ((OB_INVALID_ID != pt_table_id_ && OB_INVALID_INDEX == pt_partition_id_) ||
             (OB_INVALID_ID == pt_table_id_ && OB_INVALID_INDEX != pt_partition_id_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid pt_table_id or pt_partition_id", K(ret), K(pt_table_id_), K(pt_partition_id_));
  } else if (OB_INVALID_ID == pt_table_id_ && OB_INVALID_INDEX == pt_partition_id_) {
    pt_table_id_ = combine_id(OB_SYS_TENANT_ID, OB_ALL_VIRTUAL_CORE_META_TABLE_TID);
    pt_partition_id_ = 0;
  } else {
    int64_t part_num = 0;
    if (OB_FAIL(get_part_num(pt_table_id_, part_num))) {
      LOG_WARN("get_part_num failed", K(pt_table_id_), K(ret));
    } else if (pt_partition_id_ < part_num - 1) {
      // @note: all inner tables are non range partitions,
      // no need to process for range partitions
      // since the range partition id are successively incremental
      pt_partition_id_++;
    } else {
      int64_t index = -1;
      for (int64_t i = 0; i < ARRAYSIZEOF(pt_tables_); ++i) {
        if (extract_pure_id(pt_tables_[i]) == extract_pure_id(pt_table_id_)) {
          index = i;
        }
      }
      if (index < 0 || index >= ARRAYSIZEOF(pt_tables_)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("invalid index", K(ret), K(index), "pt_tables_count", ARRAYSIZEOF(pt_tables_));
      } else {
        uint64_t tenant_id = OB_INVALID_TENANT_ID;
        if (index == ARRAYSIZEOF(pt_tables_) - 1) {
          if (OB_ALL_TENANT_META_TABLE_TID == extract_pure_id(pt_tables_[index])) {
            // for __all_tenant_meta_table,need to get the next tenant
            if (OB_FAIL(tenant_iter_.next(tenant_id))) {
              if (OB_ITER_END != ret) {
                LOG_WARN("tenant iterate failed", K(ret));
              }
            } else if (OB_INVALID_TENANT_ID != tenant_id) {
              pt_table_id_ = combine_id(tenant_id, pt_tables_[index]);
              pt_partition_id_ = 0;
            } else {
              ret = OB_ERR_UNEXPECTED;
              LOG_WARN("tenant_id is invalid", K(ret), K(tenant_id));
            }
          }
        } else {
          if (OB_ALL_TENANT_META_TABLE_TID != extract_pure_id(pt_tables_[index + 1])) {
            pt_table_id_ = combine_id(OB_SYS_TENANT_ID, pt_tables_[index + 1]);
            pt_partition_id_ = 0;
          } else {
            if (OB_FAIL(tenant_iter_.next(tenant_id))) {
              // at lease sys tenant is iterated out
              LOG_WARN("should have at least one tenant", K(ret));
            } else if (OB_INVALID_TENANT_ID != tenant_id) {
              pt_table_id_ = combine_id(tenant_id, pt_tables_[index + 1]);
              pt_partition_id_ = 0;
            } else {
              ret = OB_ERR_UNEXPECTED;
              LOG_WARN("tenant_id is invalid", K(ret), K(tenant_id));
            }
          }
        }
      }
    }
  }
  if (OB_SUCC(ret)) {
    pt_table_id = pt_table_id_;
    pt_partition_id = pt_partition_id_;
  }
  return ret;
}

int ObPartitionTableIdIterator::get_part_num(const uint64_t table_id, int64_t& part_num)
{
  int ret = OB_SUCCESS;
  ObSchemaGetterGuard guard;
  const ObSimpleTableSchemaV2* table = NULL;
  const uint64_t tenant_id = extract_tenant_id(table_id);
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_INVALID_ID == table_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid table_id", KT(table_id), K(ret));
  } else if (OB_FAIL(schema_service_->get_tenant_schema_guard(tenant_id, guard))) {
1084
    LOG_WARN("get_schema_guard failed", K(ret));
O
oceanbase-admin 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 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 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 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
  } else if (OB_FAIL(guard.get_table_schema(table_id, table))) {
    LOG_WARN("fail to get table schema", K(ret), K(table_id));
  } else if (NULL == table) {
    ret = OB_TABLE_NOT_EXIST;
    LOG_WARN("table not exist", KT(table_id), K(ret));
  } else if (table->has_self_partition()) {
    part_num = table->get_all_part_num();
  } else {
    // for all_virtual_core_meta_table
    part_num = 1;
  }
  return ret;
}

ObFullPartitionTableIterator::ObFullPartitionTableIterator()
    : inited_(false), pt_operator_(NULL), part_iter_(), pt_part_iter_()
{}

ObFullPartitionTableIterator::~ObFullPartitionTableIterator()
{}

// iteration sequence:__all_virtual_core_meta_table
//                    -> __all_core_table
//                    -> __all_root_table
//                    -> __all_tenant_meta_table
int ObFullPartitionTableIterator::init(
    ObPartitionTableOperator& pt_operator, ObMultiVersionSchemaService& schema_service)
{
  int ret = OB_SUCCESS;
  if (inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("init twice", K(ret));
  } else if (OB_FAIL(pt_part_iter_.init(schema_service))) {
    LOG_WARN("pt_part_iterator init failed", K(ret));
  } else {
    pt_operator_ = &pt_operator;
    inited_ = true;
  }
  return ret;
}

int ObFullPartitionTableIterator::next(ObPartitionInfo& partition)
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    if (!part_iter_.is_inited()) {
      if (OB_FAIL(next_partition())) {
        if (OB_ITER_END != ret) {
          LOG_WARN("next_partition failed", K(ret));
        }
      }
    }

    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(part_iter_.next(partition))) {
      if (OB_ITER_END != ret) {
        LOG_WARN("partition iterate failed", K(ret));
      } else {
        while (OB_ITER_END == ret) {
          if (OB_FAIL(next_partition())) {
            if (OB_ITER_END != ret) {
              LOG_WARN("next_partition failed", K(ret));
            } else {
              break;
            }
          } else if (OB_FAIL(part_iter_.next(partition))) {
            if (OB_ITER_END != ret) {
              LOG_WARN("partition iterate failed", K(ret));
            }
          }
        }
      }
    }
  }
  return ret;
}

int ObFullPartitionTableIterator::next_partition()
{
  int ret = OB_SUCCESS;
  uint64_t pt_table_id = OB_INVALID_ID;
  int64_t pt_partition_id = OB_INVALID_INDEX;

  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_FAIL(pt_part_iter_.get_next_partition(pt_table_id, pt_partition_id))) {
    if (OB_ITER_END != ret) {
      LOG_WARN("fail to get next partition", K(ret), K(pt_table_id), K(pt_partition_id));
    }
  } else if (OB_FAIL(part_iter_.init(*pt_operator_, pt_table_id, pt_partition_id))) {
    LOG_WARN("ObPTPartPartitionIterator init failed", KT(pt_table_id), K(pt_partition_id), K(ret));
  }
  return ret;
}

ObFullMetaTableIterator::ObFullMetaTableIterator()
    : inited_(false),
      pt_operator_(NULL),
      schema_service_(NULL),
      part_iter_(),
      tenant_iter_(),
      pt_table_(OB_INVALID_INDEX)
{}

ObFullMetaTableIterator::~ObFullMetaTableIterator()
{}

int ObFullMetaTableIterator::init(ObPartitionTableOperator& pt_operator, ObMultiVersionSchemaService& schema_service)
{
  int ret = OB_SUCCESS;
  if (inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("init twice", K(ret));
  } else if (OB_FAIL(tenant_iter_.init(schema_service))) {
    LOG_WARN("tenant iterator init failed", K(ret));
  } else {
    pt_table_ = OB_ALL_TENANT_META_TABLE_TID;
    pt_operator_ = &pt_operator;
    schema_service_ = &schema_service;
    inited_ = true;
  }
  return ret;
}

int ObFullMetaTableIterator::next(ObPartitionInfo& partition)
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    if (!part_iter_.is_inited()) {
      if (OB_FAIL(next_partition())) {
        if (OB_ITER_END != ret) {
          LOG_WARN("next_partition failed", K(ret));
        }
      }
    }

    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(part_iter_.next(partition))) {
      if (OB_ITER_END != ret) {
        LOG_WARN("partition iterate failed", K(ret));
      } else {
        while (OB_ITER_END == ret) {
          if (OB_FAIL(next_partition())) {
            if (OB_ITER_END != ret) {
              LOG_WARN("next_partition failed", K(ret));
            } else {
              break;
            }
          } else if (OB_FAIL(part_iter_.next(partition))) {
            if (OB_ITER_END != ret) {
              LOG_WARN("partition iterate failed", K(ret));
            }
          }
        }
      }
    }
  }
  return ret;
}

int ObFullMetaTableIterator::next_partition()
{
  int ret = OB_SUCCESS;
  uint64_t pt_table_id = OB_INVALID_ID;
  int64_t pt_partition_id = OB_INVALID_INDEX;

  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_ALL_TENANT_META_TABLE_TID != pt_table_) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("pt_table_ is invalid", K(ret), K(pt_table_));
  } else {
    uint64_t tenant_id = OB_INVALID_TENANT_ID;
    if (!part_iter_.is_inited()) {
      if (OB_ALL_TENANT_META_TABLE_TID == extract_pure_id(pt_table_)) {
        if (OB_FAIL(tenant_iter_.next(tenant_id))) {
          LOG_WARN("at least has one tenant", K(ret));
        } else if (OB_INVALID_TENANT_ID != tenant_id) {
          pt_table_id = combine_id(tenant_id, pt_table_);
          pt_partition_id = 0;
        } else {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("tenant_id is invalid", K(ret), K(tenant_id));
        }
      }

    } else {
      int64_t part_num = 0;
      if (OB_FAIL(get_part_num(part_iter_.get_pt_table_id(), part_num))) {
        LOG_WARN("get_part_num failed", "pt_table_id", part_iter_.get_pt_table_id(), K(ret));
      } else if (part_iter_.get_pt_partition_id() < part_num - 1) {
        pt_table_id = part_iter_.get_pt_table_id();
        // @note: all inner tables are non range partitions,
        // no need to process for range partitions
        // since the range partition id are successively incremental
        pt_partition_id = part_iter_.get_pt_partition_id() + 1;
      } else {
        if (OB_ALL_TENANT_META_TABLE_TID == extract_pure_id(pt_table_)) {
          // for __all_tenant_meta_table,need to iterate the next tenant
          if (OB_FAIL(tenant_iter_.next(tenant_id))) {
            if (OB_ITER_END != ret) {
              LOG_WARN("tenant iterate failed", K(ret));
            }
          } else if (OB_INVALID_TENANT_ID != tenant_id) {
            pt_table_id = combine_id(tenant_id, pt_table_);
            pt_partition_id = 0;
          } else {
            ret = OB_ERR_UNEXPECTED;
            LOG_WARN("tenant_id is invalid", K(ret), K(tenant_id));
          }
        }
      }
    }
  }

  if (OB_SUCC(ret)) {
    if (OB_FAIL(part_iter_.init(*pt_operator_, pt_table_id, pt_partition_id))) {
      LOG_WARN("ObPTPartPartitionIterator init failed", KT(pt_table_id), K(pt_partition_id), K(ret));
    }
  }
  return ret;
}

int ObFullMetaTableIterator::get_part_num(const uint64_t table_id, int64_t& part_num)
{
  int ret = OB_SUCCESS;
  ObSchemaGetterGuard guard;
1320
  const ObSimpleTableSchemaV2* table = NULL;
O
oceanbase-admin 已提交
1321 1322 1323 1324 1325 1326 1327 1328
  const uint64_t tenant_id = extract_tenant_id(table_id);
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_INVALID_ID == table_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid table_id", KT(table_id), K(ret));
  } else if (OB_FAIL(schema_service_->get_tenant_schema_guard(tenant_id, guard))) {
1329
    LOG_WARN("get_schema_guard failed", K(ret));
O
oceanbase-admin 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
  } else if (OB_FAIL(guard.get_table_schema(table_id, table))) {
    LOG_WARN("fail to get table schema", K(ret), K(table_id));
  } else if (NULL == table) {
    ret = OB_TABLE_NOT_EXIST;
    LOG_WARN("table not exist", KT(table_id), K(ret));
  } else if (table->has_self_partition()) {
    part_num = table->get_all_part_num();
  }
  return ret;
}

}  // end namespace share
}  // end namespace oceanbase