ob_multi_version_table_store.cpp 51.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
/**
 * 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 "lib/oblog/ob_log_module.h"
#include "ob_multi_version_table_store.h"
#include "storage/ob_table_mgr.h"
#include "share/schema/ob_multi_version_schema_service.h"

using namespace oceanbase;
using namespace common;
using namespace storage;
using namespace memtable;

ObMultiVersionTableStore::ObMultiVersionTableStore()
    : is_inited_(false),
      pkey_(),
      table_id_(OB_INVALID_ID),
      head_(0),
      tail_(0),
      rand_(),
      freeze_info_mgr_(nullptr),
      pg_memtable_mgr_(nullptr),
      gc_sstable_count_(0),
      create_schema_version_(0),
      is_dropped_schema_(false),
      drop_schema_version_(0),
      drop_schema_refreshed_ts_(OB_INVALID_TIMESTAMP)
{
  MEMSET(table_stores_, 0, sizeof(table_stores_));
}

ObMultiVersionTableStore::~ObMultiVersionTableStore()
{
M
mw0 已提交
44
  ObTableStore *table_store = nullptr;
O
oceanbase-admin 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

  if (is_inited_) {
    while (head_ < tail_) {
      if (OB_ISNULL(table_store = get_store(head_))) {
        LOG_ERROR("table store must not null", K(pkey_), K(table_id_));
      } else {
        free_table_store(table_store);
        table_stores_[get_idx(head_)] = nullptr;
        ++head_;
      }
    }
    for (int64_t i = 0; i < gc_sstable_count_; ++i) {
      gc_sstable_infos_[i].sstable_->dec_ref();
      gc_sstable_infos_[i].reset();
    }
    gc_sstable_count_ = 0;
  }
  is_inited_ = false;
}

M
mw0 已提交
65 66
int ObMultiVersionTableStore::init(const common::ObPartitionKey &pkey, const uint64_t table_id,
    ObFreezeInfoSnapshotMgr *freeze_info_mgr, ObPGMemtableMgr *pg_memtable_mgr, const int64_t schema_version)
O
oceanbase-admin 已提交
67 68
{
  int ret = OB_SUCCESS;
M
mw0 已提交
69
  ObTableStore *new_store = nullptr;
O
oceanbase-admin 已提交
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

  if (is_inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("cannot init twice", K(ret), K(pkey), K(table_id));
  } else if (!pkey.is_valid() || OB_INVALID_ID == table_id || OB_ISNULL(freeze_info_mgr)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret), K(pkey), K(table_id), KP(freeze_info_mgr));
  } else {
    pkey_ = pkey;
    table_id_ = table_id;
    MEMSET(table_stores_, 0, sizeof(table_stores_));
    head_ = 0;
    tail_ = 0;
    freeze_info_mgr_ = freeze_info_mgr;
    pg_memtable_mgr_ = pg_memtable_mgr;
    gc_sstable_count_ = 0;
    create_schema_version_ = schema_version;

    if (OB_FAIL(alloc_table_store(new_store))) {
      LOG_WARN("failed to alloc_table_store", K(ret), K(pkey_), K(table_id_));
    } else if (OB_FAIL(new_store->init(pkey_, table_id_, freeze_info_mgr, pg_memtable_mgr))) {
      LOG_WARN("failed to init new store", K(ret), K(pkey_), K(table_id_));
    } else {
      table_stores_[get_idx(tail_)] = new_store;
      ++tail_;
      new_store = NULL;
    }
  }

  if (OB_SUCC(ret)) {
    is_inited_ = true;
  }

  if (NULL != new_store) {
    free_table_store(new_store);
  }
  return ret;
}

// used in logic split, reference tables from parent partition
int ObMultiVersionTableStore::set_reference_tables(
M
mw0 已提交
111
    ObTablesHandle &handle, ObTableStore *&new_table_store, const int64_t memtable_base_version, bool &need_update)
O
oceanbase-admin 已提交
112 113 114
{
  int ret = OB_SUCCESS;
  ObTablesHandle latest_tables_handle;
M
mw0 已提交
115
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
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
  new_table_store = NULL;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(alloc_table_store(new_table_store))) {
    LOG_WARN("failed to alloc_table_store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(new_table_store->init(pkey_, table_id_, freeze_info_mgr_, pg_memtable_mgr_))) {
    LOG_WARN("failed to init new store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->get_all_sstables(true /*need_complent*/, latest_tables_handle))) {
    LOG_WARN("failed to get_all_sstables", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(new_table_store->set_reference_tables(
                 handle, latest_tables_handle, memtable_base_version, need_update))) {
    LOG_WARN("failed to set reference tables", K(ret), K(pkey_), K(table_id_));
  }

  if (OB_FAIL(ret)) {
    if (NULL != new_table_store) {
      free_table_store(new_table_store);
      new_table_store = NULL;
    }
  }
  return ret;
}

// p0 Minor SSTable + memtable split and assign to p1
int ObMultiVersionTableStore::prepare_update_split_table_store(
M
mw0 已提交
146
    const bool is_major_split, ObTablesHandle &handle, bool &need_update, ObTableStore *&new_table_store)
O
oceanbase-admin 已提交
147 148
{
  int ret = OB_SUCCESS;
M
mw0 已提交
149
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  ObTablesHandle old_handle;
  bool is_complete = false;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->check_complete(is_complete))) {
    LOG_WARN("failed to check complete", K(ret), K(pkey_), K(table_id_));
  } else if (!is_complete) {
    need_update = false;
    LOG_INFO("table store is not complete, no need to update", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(alloc_table_store(new_table_store))) {
    LOG_WARN("failed to alloc_table_store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(new_table_store->init(pkey_, table_id_, freeze_info_mgr_, pg_memtable_mgr_))) {
    LOG_WARN("failed to init new store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->get_all_tables(false, /*include_active_memtable*/
                 false,                                  /*include_complement*/
                 old_handle))) {
    LOG_WARN("failed to get_all_tables", K(ret), K(pkey_), K(table_id_));
  } else if (is_major_split && OB_FAIL(new_table_store->build_major_split_store(old_handle, handle, need_update))) {
    LOG_WARN("failed to build split store", K(ret), K(pkey_), K(table_id_));
  } else if (!is_major_split && OB_FAIL(new_table_store->build_minor_split_store(old_handle, handle, need_update))) {
    LOG_WARN("failed to build split store", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

int ObMultiVersionTableStore::check_need_split(
M
mw0 已提交
181
    common::ObVersion &split_version, bool &need_split, bool &need_minor_split)
O
oceanbase-admin 已提交
182 183
{
  int ret = OB_SUCCESS;
M
mw0 已提交
184
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  ObTablesHandle old_handle;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->get_all_tables(false, /*include_active_memtable*/
                 false,                                  /*include_complement*/
                 old_handle))) {
    LOG_WARN("failed to get_all_tables", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->check_need_split(old_handle, split_version, need_split, need_minor_split))) {
    LOG_WARN("failed to check if need split", K(ret), K(pkey_), K(table_id_));
  }

  return ret;
}

M
mw0 已提交
203
int ObMultiVersionTableStore::check_can_migrate(bool &can_migrate)
O
oceanbase-admin 已提交
204 205
{
  int ret = OB_SUCCESS;
M
mw0 已提交
206
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
207 208 209 210 211 212 213 214 215 216 217 218
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->check_can_migrate(can_migrate))) {
    LOG_WARN("failed to check physical split finish", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
219
int ObMultiVersionTableStore::check_complete(bool &is_complete)
O
oceanbase-admin 已提交
220 221
{
  int ret = OB_SUCCESS;
M
mw0 已提交
222
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
223 224 225 226 227 228 229 230 231 232 233 234
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->check_complete(is_complete))) {
    LOG_WARN("failed to check physical split finish", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
235
int ObMultiVersionTableStore::is_physical_split_finished(bool &is_physical_split_finish)
O
oceanbase-admin 已提交
236 237
{
  int ret = OB_SUCCESS;
M
mw0 已提交
238
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  ObTablesHandle old_handle;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->get_all_tables(false, /*include_active_memtable*/
                 false,                                  /*include_complement*/
                 old_handle))) {
    LOG_WARN("failed to get_all_tables", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->is_physical_split_finished(old_handle, is_physical_split_finish))) {
    LOG_WARN("failed to check physical split finish", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
256
int ObMultiVersionTableStore::get_physical_split_info(ObVirtualPartitionSplitInfo &split_info)
O
oceanbase-admin 已提交
257 258
{
  int ret = OB_SUCCESS;
M
mw0 已提交
259
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->get_physical_split_info(split_info))) {
    LOG_WARN("Failed to get physical split info", K(ret), K(pkey_), K(table_id_));
  }

  return ret;
}

// add_sstable_with_prewarm is  used after merge
int ObMultiVersionTableStore::prepare_add_sstable(
M
mw0 已提交
276
    AddTableParam &param, ObTableStore *&new_table_store, bool &need_update)
O
oceanbase-admin 已提交
277 278 279 280 281
{
  int ret = OB_SUCCESS;
  need_update = true;
  bool is_equal = false;
  bool can_split = false;
M
mw0 已提交
282
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
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
  ObTablesHandle latest_tables_handle;
  new_table_store = NULL;
  bool need_check_split = (NULL != param.table_ && param.table_->get_partition_key() != pkey_) ? true : false;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (!param.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret), K(param));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (need_check_split && OB_FAIL(latest_store->check_can_split(can_split))) {
    LOG_WARN("failed to check can split", K(ret), K(pkey_));
  } else if (can_split) {
    need_update = false;
    LOG_INFO("split dest store has complete tables, no need to update", K(pkey_), K(table_id_));
  } else if (OB_FAIL(alloc_table_store(new_table_store))) {
    LOG_WARN("failed to alloc_table_store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(new_table_store->init(pkey_, table_id_, freeze_info_mgr_, pg_memtable_mgr_))) {
    LOG_WARN("failed to init new store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->get_all_tables(false, /*include_active_memtable*/
                 false,                                  /*include_complement*/
                 latest_tables_handle))) {
    LOG_WARN("failed to get_all_tables", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(add_complement_minor_sstable_if_needed_(param, *latest_store))) {
    LOG_WARN("failed to add_complement_minor_sstable_if_needed", K(ret), K_(pkey), K(table_id_), K(param));
  } else if (OB_FAIL(new_table_store->build_new_merge_store(param, latest_tables_handle))) {
    LOG_WARN("failed to build new merge store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(new_table_store->equals(*latest_store, is_equal))) {
    LOG_WARN("failed to check table store is equal", K(ret), K(pkey_), K(table_id_));
  }

  if (OB_SUCC(ret)) {
    if (!need_update) {
      // do nothing
    } else if (!is_equal) {
      need_update = true;
      LOG_INFO("prepare new merge store",
          K(head_),
          K(tail_),
          "multi_version_start",
          param.multi_version_start_,
          K(is_equal),
          K(latest_tables_handle));
    } else {
      need_update = false;
      LOG_INFO("prepare merge store, but new and old one are same",
          K(head_),
          K(tail_),
          "multi_version_start",
          param.multi_version_start_,
          K(is_equal),
          K(*new_table_store),
          K(latest_tables_handle),
          K(*latest_store));
    }
  }

  if (OB_FAIL(ret)) {
    if (NULL != new_table_store) {
      free_table_store(new_table_store);
    }
  }
  return ret;
}

int ObMultiVersionTableStore::prepare_remove_sstable(
M
mw0 已提交
352
    AddTableParam &param, ObTablesHandle &handle, ObTableStore *&new_table_store, bool &is_equal)
O
oceanbase-admin 已提交
353 354
{
  int ret = OB_SUCCESS;
M
mw0 已提交
355
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
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
  new_table_store = NULL;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (!param.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret), K(param));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(alloc_table_store(new_table_store))) {
    LOG_WARN("failed to alloc_table_store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(new_table_store->init(pkey_, table_id_, freeze_info_mgr_, pg_memtable_mgr_))) {
    LOG_WARN("failed to init new store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(new_table_store->build_new_merge_store(param, handle))) {
    LOG_WARN("failed to build new merge store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(new_table_store->equals(*latest_store, is_equal))) {
    LOG_WARN("failed to check table store is equal", K(ret), K(pkey_), K(table_id_));
  }

  if (OB_SUCC(ret)) {
    if (!is_equal) {
      LOG_INFO("prepare remove store",
          K(head_),
          K(tail_),
          K(param),
          K(is_equal),
          K(*new_table_store),
          K(handle),
          K(*latest_store));
    } else {
      LOG_INFO("prepare remove store, but new and old one are same",
          K(head_),
          K(tail_),
          K(param),
          K(is_equal),
          K(*new_table_store),
          K(handle),
          K(*latest_store));
    }
  }

  if (OB_FAIL(ret)) {
    if (NULL != new_table_store) {
      free_table_store(new_table_store);
    }
  }
  return ret;
}

M
mw0 已提交
407
int ObMultiVersionTableStore::alloc_table_store(ObTableStore *&new_table_store)
O
oceanbase-admin 已提交
408 409 410 411 412 413 414 415 416 417 418 419
{
  int ret = OB_SUCCESS;

  if (OB_ISNULL(new_table_store = op_alloc(ObTableStore))) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    LOG_WARN("failed to new store", K(ret), K(pkey_), K(table_id_));
  } else {
    LOG_DEBUG("succeed to alloc_table_store", KP(new_table_store));
  }
  return ret;
}

M
mw0 已提交
420
void ObMultiVersionTableStore::free_table_store(ObTableStore *&new_table_store)
O
oceanbase-admin 已提交
421 422 423 424 425 426 427 428
{
  if (NULL != new_table_store) {
    LOG_INFO("succeed to free_table_store", KP(new_table_store));
    op_free(new_table_store);
    new_table_store = NULL;
  }
}

M
mw0 已提交
429
int ObMultiVersionTableStore::enable_table_store(const bool need_prewarm, ObTableStore &new_table_store)
O
oceanbase-admin 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (get_idx(tail_ + 1) == get_idx(head_) && OB_FAIL(retire_head_store())) {
    LOG_WARN("failed to retire_store", K(ret), K(pkey_), K(table_id_));
  } else if (get_idx(tail_ + 1) == get_idx(head_) || NULL != get_store(tail_)) {
    ret = OB_ERR_SYS;
    LOG_ERROR("invalid head and tail", K(ret), K(tail_), K(head_), KP(get_store(tail_)));
  } else if (OB_SUCCESS != (tmp_ret = record_gc_minor_sstable(new_table_store))) {
    LOG_WARN("failed to record gc minor sstable", K(tmp_ret));
  }

  if (OB_SUCC(ret) && !need_prewarm) {
    while (OB_SUCC(ret) && head_ < tail_) {
M
mw0 已提交
448
      ObTableStore *table_store = get_store(head_);
O
oceanbase-admin 已提交
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
      if (NULL != table_store) {
        LOG_INFO("retire table store",
            K(pkey_),
            K(table_id_),
            "left_count",
            tail_ - head_ - 1,
            K(tail_),
            K(head_),
            K(*table_store));
        free_table_store(table_store);
        table_stores_[get_idx(head_)] = NULL;
        ++head_;
      }
    }
  }

  if (OB_SUCC(ret)) {
    table_stores_[get_idx(tail_)] = &new_table_store;
    ++tail_;
    LOG_DEBUG("enable new sstable store", "count", tail_ - head_, K(tail_), K(head_), K(new_table_store));
  }
  return ret;
}

int ObMultiVersionTableStore::get_read_tables(
M
mw0 已提交
474
    const int64_t snapshot_version, ObTablesHandle &handle, const bool allow_not_ready, const bool print_dropped_alert)
O
oceanbase-admin 已提交
475 476
{
  int ret = OB_SUCCESS;
M
mw0 已提交
477
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret), K(*this));
  } else if (is_dropped_schema_) {
    if (print_dropped_alert) {
      ret = OB_ERR_SYS;
      LOG_ERROR("should not read dropped table store", K(ret), K(pkey_), K(table_id_), K(is_dropped_schema_));
    } else {
      ret = OB_PARTITION_IS_REMOVED;
      LOG_WARN("cannot read dropped table store", K(ret), K(pkey_), K(table_id_), K(is_dropped_schema_));
    }
  } else if (OB_ISNULL(table_store = get_read_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("no table store exists", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_read_tables(snapshot_version, handle, allow_not_ready))) {
    LOG_WARN("failed to get read tables", K(ret));
  } else {
    handle.set_retire_check();
  }

  return ret;
}

M
mw0 已提交
502
int ObMultiVersionTableStore::get_reference_tables(ObTablesHandle &handle)
O
oceanbase-admin 已提交
503 504
{
  int ret = OB_SUCCESS;
M
mw0 已提交
505
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("no table store exists", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_reference_tables(handle))) {
    LOG_WARN("failed to get reference tables", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
519
int ObMultiVersionTableStore::get_sample_read_tables(const common::SampleInfo &sample_info, ObTablesHandle &handle)
O
oceanbase-admin 已提交
520 521
{
  int ret = OB_SUCCESS;
M
mw0 已提交
522
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("no table store exists", K(ret), K(pkey_), K(table_id_));
    LOG_WARN("failed to get sample read tables", K(ret), K(sample_info));
  } else if (OB_FAIL(table_store->get_sample_read_tables(sample_info, handle))) {
  }

  return ret;
}

M
mw0 已提交
537
int ObMultiVersionTableStore::get_major_sstable(const ObVersion &version, ObTablesHandle &handle)
O
oceanbase-admin 已提交
538 539
{
  int ret = OB_SUCCESS;
M
mw0 已提交
540
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("no table store exists", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_major_sstable(version, handle))) {
    LOG_WARN("failed to get_major_sstable", K(ret), K(version));
  }

  return ret;
}

M
mw0 已提交
555
int ObMultiVersionTableStore::get_all_tables(TableSet &table_set, ObTablesHandle &handle)
O
oceanbase-admin 已提交
556 557 558
{
  int ret = OB_SUCCESS;
  int hash_ret = OB_SUCCESS;
M
mw0 已提交
559
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else {
    const bool is_single_table_store = (1 == (tail_ - head_));
    for (int64_t pos = head_; OB_SUCC(ret) && pos < tail_; ++pos) {
      ObTablesHandle tmp_handle;
      if (OB_ISNULL(table_store = get_store(pos))) {
        ret = OB_ERR_SYS;
        LOG_ERROR("table store must not null", K(ret), K(pkey_), K(table_id_));
      } else if (OB_FAIL(table_store->get_all_tables(true, /*include_active_memtable*/
                     true,                                 /*include_complement_minor_sstable*/
                     tmp_handle))) {
        LOG_WARN("failed to get all tables", K(ret), K(pkey_), K(table_id_));
      } else {
        for (int64_t i = 0; OB_SUCC(ret) && i < tmp_handle.get_count(); ++i) {
M
mw0 已提交
577
          ObITable *table = tmp_handle.get_table(i);
O
oceanbase-admin 已提交
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
          int64_t table_addr = reinterpret_cast<int64_t>(table);
          if (OB_ISNULL(table)) {
            ret = OB_ERR_SYS;
            LOG_ERROR("table must not null", K(ret), K(pkey_), K(table_id_));
          } else if (!is_single_table_store || table->is_memtable()) {
            if (OB_HASH_EXIST == (hash_ret = table_set.exist_refactored(table_addr))) {
              // pass
            } else if (OB_HASH_NOT_EXIST != hash_ret) {
              ret = hash_ret;
              LOG_WARN("failed to check if table exists", K(ret), K(pkey_), K(table_id_));
            } else if (OB_FAIL(table_set.set_refactored(table_addr))) {
              LOG_WARN("failed to set table addr", K(ret));
            } else if (OB_FAIL(handle.add_table(table))) {
              LOG_WARN("failed to add table", K(ret), K(pkey_), K(table_id_));
            }
          } else {
            // table_set is passed from caller due to performance consideration.
            // Different ObMultiVersionTableStore won't have same sstable, so we can skip
            // check table_set when there's only one table store.
            if (OB_FAIL(handle.add_table(table))) {
              LOG_WARN("failed to add table", K(ret), K(pkey_), K(table_id_));
            }
          }
        }
      }
    }
  }

  return ret;
}

M
mw0 已提交
609
int ObMultiVersionTableStore::check_memtable_merged(const ObMemtable &memtable, bool &all_merged, bool &can_release)
O
oceanbase-admin 已提交
610 611
{
  int ret = OB_SUCCESS;
M
mw0 已提交
612
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
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

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else {
    all_merged = false;
    can_release = true;
    for (int64_t pos = head_; OB_SUCC(ret) && pos < tail_; ++pos) {
      bool need_merge = false;
      if (OB_ISNULL(table_store = get_store(pos))) {
        ret = OB_ERR_SYS;
        LOG_ERROR("table store must not null", K(ret), K(pkey_), K(table_id_));
      } else if (OB_FAIL(table_store->is_memtable_need_merge(memtable, need_merge))) {
        LOG_WARN("failed to check memtable need merge", K(ret), K(*table_store), K(memtable), K(need_merge));
      } else if (need_merge) {
        can_release = false;
      } else {
        all_merged = true;
      }
    }
    if (OB_SUCC(ret)) {
      can_release = can_release && all_merged;
    }
  }

  return ret;
}

M
mw0 已提交
641
int ObMultiVersionTableStore::get_effective_tables(const bool include_active_memtable, ObTablesHandle &handle)
O
oceanbase-admin 已提交
642 643
{
  int ret = OB_SUCCESS;
M
mw0 已提交
644
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
    // get_effective_tables no need to get buffer minor sstable now
    // TODO  remove this interface
  } else if (OB_FAIL(table_store->get_all_tables(include_active_memtable,
                 false, /*include_complement*/
                 handle))) {
    LOG_WARN("failed to get latest sstable", K(ret), K(pkey_), K(table_id_));
  }

  return ret;
}

M
mw0 已提交
663
int ObMultiVersionTableStore::get_latest_major_sstable(ObTableHandle &handle)
O
oceanbase-admin 已提交
664 665
{
  int ret = OB_SUCCESS;
M
mw0 已提交
666
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_latest_major_sstable(handle))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      LOG_WARN("failed to get latest sstable", K(ret), K(pkey_), K(table_id_));
    }
  }

  return ret;
}

M
mw0 已提交
683
int ObMultiVersionTableStore::get_sstable_schema_version(int64_t &schema_version)
O
oceanbase-admin 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
{
  int ret = OB_SUCCESS;
  ObTableHandle handle;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_FAIL(get_latest_major_sstable(handle))) {
    if (OB_ENTRY_NOT_EXIST == ret) {
      ret = OB_SUCCESS;
    } else {
      LOG_WARN("fail to get table handle", K(ret));
    }
  } else if (OB_FAIL(handle.get_sstable_schema_version(schema_version))) {
    LOG_WARN("fail to get sstable schema version", K(ret));
  }

  return ret;
}

M
mw0 已提交
703
int ObMultiVersionTableStore::get_replay_tables(ObIArray<ObITable::TableKey> &replay_tables)
O
oceanbase-admin 已提交
704 705
{
  int ret = OB_SUCCESS;
M
mw0 已提交
706
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_replay_tables(replay_tables))) {
    LOG_WARN("failed to get_replay_tables", K(ret), K(pkey_), K(table_id_));
  }

  return ret;
}

M
mw0 已提交
721
int ObMultiVersionTableStore::get_oldest_read_tables(const int64_t snapshot_version, ObTablesHandle &handle)
O
oceanbase-admin 已提交
722 723
{
  int ret = OB_SUCCESS;
M
mw0 已提交
724
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_oldest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("no table store exists", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_read_tables(snapshot_version, handle))) {
    LOG_WARN("failed to get read tables", K(ret));
  } else {
    handle.set_retire_check();
  }

  return ret;
}

int ObMultiVersionTableStore::get_merge_tables(
M
mw0 已提交
742
    const ObGetMergeTablesParam &param, const int64_t multi_version_start, ObGetMergeTablesResult &result)
O
oceanbase-admin 已提交
743 744
{
  int ret = OB_SUCCESS;
M
mw0 已提交
745
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
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

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (!param.is_valid() || table_id_ != param.index_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid args", K(ret), K(param), K(table_id_));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (MAJOR_MERGE == param.merge_type_) {
    if (OB_FAIL(table_store->get_major_merge_tables(param, result))) {
      LOG_WARN("failed to get major merge tables", K(ret), K(param), K(*this));
    }
  } else if (MINI_MINOR_MERGE == param.merge_type_) {
    if (OB_FAIL(table_store->get_mini_minor_merge_tables(param, multi_version_start, result))) {
      LOG_WARN("failed to get minor merge tables", K(ret), K(param), K(*this));
    }
  } else if (MINI_MERGE == param.merge_type_) {
    if (OB_FAIL(table_store->get_mini_merge_tables(param, multi_version_start, result))) {
      LOG_WARN("failed to get minor merge tables", K(ret), K(param), K(*this));
    }
  } else if (HISTORY_MINI_MINOR_MERGE == param.merge_type_) {
    if (OB_FAIL(table_store->get_hist_minor_merge_tables(param, multi_version_start, result))) {
      LOG_WARN("failed to get history mini minor merge tables", K(ret), K(param), K(*this));
    }
  } else {
    ret = OB_NOT_SUPPORTED;
    LOG_WARN("not support merge type", K(ret), K(param), K(*this));
  }
  return ret;
}

M
mw0 已提交
779
int ObMultiVersionTableStore::get_split_tables(const bool is_major_split, ObTablesHandle &handle)
O
oceanbase-admin 已提交
780 781
{
  int ret = OB_SUCCESS;
M
mw0 已提交
782
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (is_major_split) {
    if (OB_FAIL(table_store->get_major_split_tables(handle))) {
      LOG_WARN("failed to get major split tables", K(ret), K(*this));
    }
  } else {
    if (OB_FAIL(table_store->get_minor_split_tables(handle))) {
      LOG_WARN("failed to get minor split tables", K(ret), K(*this));
    }
  }
  return ret;
}

int ObMultiVersionTableStore::retire_prewarm_store(const int64_t duration, const int64_t minor_deferred_gc_time)
{
  int ret = OB_SUCCESS;
M
mw0 已提交
805
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
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
  const int64_t retire_ts = duration <= 0 ? INT64_MAX : ObTimeUtility::current_time() - duration;
  const int64_t retire_gc_sstbale_ts =
      minor_deferred_gc_time <= 0 ? INT64_MAX : ObTimeUtility::current_time() - minor_deferred_gc_time;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else {
    while (OB_SUCC(ret) && head_ < tail_ - 1) {
      if (OB_ISNULL(table_store = get_store(head_))) {
        ret = OB_ERR_SYS;
        LOG_ERROR("table store must not null", K(ret), K(pkey_), K(table_id_));
      } else if (table_store->get_uptime() < retire_ts) {
        LOG_INFO("retire table store",
            K(pkey_),
            K(table_id_),
            K(duration),
            K(retire_ts),
            "left_count",
            tail_ - head_ - 1,
            K(*table_store));
        free_table_store(table_store);
        table_stores_[get_idx(head_)] = NULL;
        ++head_;
      } else {
        break;
      }
    }

    for (int64_t i = gc_sstable_count_ - 1; OB_SUCC(ret) && i >= 0; --i) {
      if (gc_sstable_infos_[i].retired_ts_ < retire_gc_sstbale_ts) {
M
mw0 已提交
837
        ObGCSSTableInfo &gc_sstable_info = gc_sstable_infos_[i];
O
oceanbase-admin 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
        LOG_INFO("retire gc sstable", K(gc_sstable_count_), K(minor_deferred_gc_time), K(gc_sstable_info));
        gc_sstable_info.sstable_->dec_ref();
        gc_sstable_info.reset();
        --gc_sstable_count_;
      } else {
        break;
      }
    }
  }

  return ret;
}

int ObMultiVersionTableStore::retire_head_store()
{
  int ret = OB_SUCCESS;
M
mw0 已提交
854
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (tail_ - head_ <= 1) {
    ret = OB_ERR_SYS;
    LOG_ERROR("only one table store exist, cannot retire", K(ret), K(*this));
  } else if (OB_ISNULL(table_store = get_store(head_))) {
    ret = OB_ERR_SYS;
    LOG_ERROR("table store must not null", K(ret), K(pkey_), K(table_id_));
  } else {
    LOG_INFO("retire_head_store", K(pkey_), K(table_id_), K(head_), K(tail_), K(*table_store));
    free_table_store(table_store);
    table_stores_[get_idx(head_)] = NULL;
    ++head_;
  }

  return ret;
}

M
mw0 已提交
875
int ObMultiVersionTableStore::get_latest_table_count(int64_t &latest_table_count)
O
oceanbase-admin 已提交
876 877
{
  int ret = OB_SUCCESS;
M
mw0 已提交
878
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_table_count(latest_table_count))) {
    LOG_WARN("failed to get table count", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
892
int ObMultiVersionTableStore::check_latest_table_count_safe(bool &is_safe)
O
oceanbase-admin 已提交
893 894
{
  int ret = OB_SUCCESS;
M
mw0 已提交
895
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->check_table_count_safe(is_safe))) {
    LOG_WARN("failed to check table count safe", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
909
int ObMultiVersionTableStore::latest_has_major_sstable(bool &has_major)
O
oceanbase-admin 已提交
910 911
{
  int ret = OB_SUCCESS;
M
mw0 已提交
912
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->has_major_sstable(has_major))) {
    LOG_WARN("fail to check has major sstable", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
926
int64_t ObMultiVersionTableStore::to_string(char *buf, const int64_t buf_len) const
O
oceanbase-admin 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
{
  int64_t pos = 0;
  if (OB_ISNULL(buf) || buf_len <= 0) {
    // do nothing
  } else {
    J_OBJ_START();
    J_KV(K_(pkey),
        K_(table_id),
        K_(create_schema_version),
        K_(is_dropped_schema),
        "count",
        tail_ - head_,
        K_(tail),
        K_(head));
    J_ARRAY_START();
    for (int64_t tmp_pos = head_; tmp_pos < tail_; ++tmp_pos) {
M
mw0 已提交
943
      const ObTableStore *table_store = table_stores_[get_idx(tmp_pos)];
O
oceanbase-admin 已提交
944 945 946 947 948
      J_KV(K(tmp_pos), K(table_store));
    }
    J_ARRAY_END();
    J_ARRAY_START();
    for (int64_t i = 0; i < gc_sstable_count_; ++i) {
M
mw0 已提交
949
      const ObGCSSTableInfo &gc_info = gc_sstable_infos_[i];
O
oceanbase-admin 已提交
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
      if (gc_info.sstable_ != NULL) {
        J_KV("retired_ts", gc_info.retired_ts_, "table_key", gc_info.sstable_->get_key());
      } else {
        J_KV("retired_ts", gc_info.retired_ts_, "table_key", "null");
      }
    }
    J_ARRAY_END();
    J_OBJ_END();
  }
  return pos;
}

int ObMultiVersionTableStore::finish_replay(const int64_t multi_version_start)
{
  int ret = OB_SUCCESS;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (tail_ - head_ > 1 || OB_ISNULL(get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("invalid store status", K(ret), K(*this));
  } else if (OB_FAIL(get_latest_store()->finish_replay(multi_version_start))) {
    LOG_WARN("failed to finish_replay", K(ret));
  }
  return ret;
}

M
mw0 已提交
978
int ObMultiVersionTableStore::check_ready_for_read(bool &is_ready)
O
oceanbase-admin 已提交
979 980
{
  int ret = OB_SUCCESS;
M
mw0 已提交
981
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else {
    is_ready = table_store->is_ready_for_read();
  }

  return ret;
}

M
mw0 已提交
996 997
int ObMultiVersionTableStore::need_remove_old_table(const common::ObVersion &kept_min_version,
    const int64_t multi_version_start, int64_t &real_kept_major_num, bool &need_remove)
O
oceanbase-admin 已提交
998 999
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1000
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
1001 1002 1003 1004 1005 1006 1007 1008

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->need_remove_old_table(
M
mw0 已提交
1009
                 kept_min_version, multi_version_start, real_kept_major_num, need_remove))) {
O
oceanbase-admin 已提交
1010 1011 1012 1013 1014
    LOG_WARN("fail to check need remove old table", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
1015
int ObMultiVersionTableStore::set_table_store(const ObTableStore &table_store)
O
oceanbase-admin 已提交
1016 1017
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1018
  ObTableStore *new_table_store = NULL;
O
oceanbase-admin 已提交
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
  const bool need_prewarm = false;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (tail_ - head_ > 1) {
    ret = OB_ERR_SYS;
    LOG_ERROR("during replay, table store must not more than one", K(ret), K(*this));
  } else if (OB_FAIL(alloc_table_store(new_table_store))) {
    LOG_WARN("failed to alloc_table_store", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(new_table_store->init(table_store, freeze_info_mgr_, pg_memtable_mgr_))) {
    LOG_WARN("failed to init new store", K(ret), K(table_store));
  } else if (OB_FAIL(enable_table_store(need_prewarm, *new_table_store))) {
    LOG_WARN("Failed to enable table store", K(ret));
  } else {
    new_table_store = NULL;
  }

  if (NULL != new_table_store) {
    free_table_store(new_table_store);
  }
  return ret;
}

int ObMultiVersionTableStore::check_need_minor_merge(
M
mw0 已提交
1044
    const bool is_follower_data_rep, const ObMergeType merge_type, bool &need_merge)
O
oceanbase-admin 已提交
1045 1046
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1047
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (MINI_MINOR_MERGE == merge_type) {
    if (OB_FAIL(table_store->check_need_mini_minor_merge(is_follower_data_rep, need_merge))) {
      LOG_WARN("failed to check_need_mini_minor_merge", K(ret), K(pkey_), K(table_id_));
    }
  } else if (HISTORY_MINI_MINOR_MERGE == merge_type) {
    if (OB_FAIL(table_store->check_need_hist_minor_merge(need_merge))) {
      LOG_WARN("failed to check_need_hist_minor_merge", K(ret), K(pkey_), K(table_id_));
    }
  } else {
    ret = OB_ERR_SYS;
    LOG_ERROR("Unsupported merge type", K(ret), K(merge_type));
  }
  return ret;
}

ObMultiVersionTableStore::ObGCSSTableInfo::ObGCSSTableInfo() : sstable_(nullptr), retired_ts_(0)
{}

ObMultiVersionTableStore::ObGCSSTableInfo::~ObGCSSTableInfo()
{
  sstable_ = nullptr;
  retired_ts_ = 0;
}

void ObMultiVersionTableStore::ObGCSSTableInfo::reset()
{
  sstable_ = nullptr;
  retired_ts_ = 0;
}

M
mw0 已提交
1085
int64_t ObMultiVersionTableStore::ObGCSSTableInfo::to_string(char *buf, const int64_t buf_len) const
O
oceanbase-admin 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
{
  int64_t pos = 0;
  if (OB_ISNULL(buf) || buf_len <= 0) {
    // do nothing
  } else {
    J_OBJ_START();
    if (sstable_ != NULL) {
      J_KV("retired_ts", retired_ts_, "table_key", sstable_->get_key());
    } else {
      J_KV("retired_ts", retired_ts_, "table_key", "null");
    }
    J_OBJ_END();
  }
  return pos;
}

M
mw0 已提交
1102
int ObMultiVersionTableStore::record_gc_minor_sstable(ObTableStore &new_table_store)
O
oceanbase-admin 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
{
  int ret = OB_SUCCESS;
  int hash_ret = OB_SUCCESS;
  const int64_t set_bucket_num = common::OB_MAX_VERSION_COUNT * 2;
  const int64_t minor_deferred_gc_level = GCONF._minor_deferred_gc_level;
  const int64_t minor_deferred_gc_time = GCONF.minor_deferred_gc_time;
  TableSet new_table_set;
  ObTablesHandle new_tables_handle;
  ObTablesHandle old_tables_handle;
  int64_t cur_time = ObTimeUtility::current_time();

  if (OB_FAIL(new_table_set.create(set_bucket_num))) {
    LOG_WARN("failed to create new table set", K(ret));
  } else if (OB_FAIL(new_table_store.get_all_sstables(false /*need_complent*/, new_tables_handle))) {
    LOG_WARN("failed to get_all_sstables", K(ret));
  } else if (NULL != get_latest_store()) {
    if (OB_FAIL(get_latest_store()->get_all_sstables(false /*need_complent*/, old_tables_handle))) {
      LOG_WARN("Failed to get old sstables", K(ret));
    }
  }

  if (OB_SUCC(ret) && old_tables_handle.get_count() > 0 && minor_deferred_gc_time > 0) {
    for (int64_t i = 0; OB_SUCC(ret) && i < new_tables_handle.get_count(); ++i) {
M
mw0 已提交
1126
      ObITable *table = new_tables_handle.get_table(i);
O
oceanbase-admin 已提交
1127 1128 1129 1130 1131 1132
      int64_t table_addr = reinterpret_cast<int64_t>(table);
      if (OB_FAIL(new_table_set.set_refactored(table_addr))) {
        LOG_WARN("failed to set table addr", K(ret));
      }
    }
    for (int64_t i = 0; OB_SUCC(ret) && i < old_tables_handle.get_count(); ++i) {
M
mw0 已提交
1133
      ObITable *table = old_tables_handle.get_table(i);
O
oceanbase-admin 已提交
1134 1135 1136 1137 1138 1139 1140 1141
      int64_t table_addr = reinterpret_cast<int64_t>(table);
      hash_ret = new_table_set.exist_refactored(table_addr);
      if (OB_ISNULL(table)) {
        ret = OB_ERR_SYS;
        LOG_ERROR("table must not null", K(ret));
      } else if (table->is_mini_minor_sstable() || (table->is_minor_sstable() && 1 == minor_deferred_gc_level)) {
        if (OB_HASH_NOT_EXIST == hash_ret) {
          if (OB_FAIL(add_gc_minor_sstable(
M
mw0 已提交
1142
                  cur_time, static_cast<ObSSTable *>(table), MAX_DEFERRED_GC_MINOR_SSTABLE_COUNT))) {
O
oceanbase-admin 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
            LOG_WARN("failed to add gc minor sstable", K(ret));
          }
        } else if (OB_HASH_EXIST == hash_ret) {
          // do nothing;
        } else {
          ret = OB_ERR_SYS;
          LOG_WARN("failed to check hash_set exist", K(ret), K(hash_ret));
        }
      }
    }
  }
  return ret;
}

int ObMultiVersionTableStore::add_gc_minor_sstable(
M
mw0 已提交
1158
    const int64_t gc_ts, ObSSTable *table, const int64_t max_deferred_gc_count)
O
oceanbase-admin 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
{
  int ret = OB_SUCCESS;
  int64_t find_pos = -1;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table)) {
    ret = OB_ERR_SYS;
    LOG_WARN("table must not null", K(ret));
  } else {
    for (int64_t i = 0; i < gc_sstable_count_; ++i) {
      if (gc_sstable_infos_[i].sstable_ == table) {
        find_pos = i;
        break;
      }
    }
  }

  if (OB_SUCC(ret)) {
    if (-1 == find_pos) {  // add new
      if (gc_sstable_count_ == max_deferred_gc_count) {
M
mw0 已提交
1181
        ObGCSSTableInfo &last_info = gc_sstable_infos_[gc_sstable_count_ - 1];
O
oceanbase-admin 已提交
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
        last_info.sstable_->dec_ref();
        last_info.reset();
        --gc_sstable_count_;
      }

      for (int64_t i = gc_sstable_count_; i >= 1; --i) {
        gc_sstable_infos_[i] = gc_sstable_infos_[i - 1];
      }
      gc_sstable_infos_[0].sstable_ = table;
      gc_sstable_infos_[0].retired_ts_ = gc_ts;
      table->inc_ref();
      ++gc_sstable_count_;
      ObArrayWrap<ObGCSSTableInfo> new_gc_sstables(gc_sstable_infos_, gc_sstable_count_);
      LOG_INFO("add_gc_minor_sstable", K_(gc_sstable_count), K(new_gc_sstables));
    } else {  // update ts
      ObGCSSTableInfo tmp_info = gc_sstable_infos_[find_pos];
      tmp_info.retired_ts_ = gc_ts;
      for (int64_t i = find_pos; i >= 1; --i) {
        gc_sstable_infos_[i] = gc_sstable_infos_[i - 1];
      }
      gc_sstable_infos_[0] = tmp_info;
      ObArrayWrap<ObGCSSTableInfo> new_gc_sstables(gc_sstable_infos_, gc_sstable_count_);
      LOG_INFO("update add_gc_minor_sstable", K_(gc_sstable_count), K(new_gc_sstables));
    }
  }

  return ret;
}

M
mw0 已提交
1211
int ObMultiVersionTableStore::get_gc_sstables(ObTablesHandle &handle)
O
oceanbase-admin 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
{
  int ret = OB_SUCCESS;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < gc_sstable_count_; ++i) {
      if (OB_FAIL(handle.add_table(gc_sstable_infos_[i].sstable_))) {
        LOG_WARN("Failed to get gc sstable", K(ret), K(i));
      }
    }
    ObArrayWrap<ObGCSSTableInfo> gc_sstables(gc_sstable_infos_, gc_sstable_count_);
    LOG_INFO("get_gc_sstables", K_(gc_sstable_count), K(gc_sstables), K(handle));
  }
  return ret;
}

int ObMultiVersionTableStore::set_replay_sstables(
M
mw0 已提交
1231
    const bool is_replay_old, const common::ObIArray<ObSSTable *> &sstables)
O
oceanbase-admin 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObMultiVersionTableStore has not been inited", K(ret));
  } else if (tail_ - head_ > 1 || OB_ISNULL(get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("invalid store status", K(ret), K(*this));
  } else if (OB_FAIL(get_latest_store()->set_replay_sstables(is_replay_old, sstables))) {
    LOG_WARN("failed to finish_replay", K(ret));
  }
  return ret;
}

M
mw0 已提交
1246
int ObMultiVersionTableStore::get_multi_version_start(int64_t &multi_version_start)
O
oceanbase-admin 已提交
1247 1248
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1249
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_multi_version_start(multi_version_start))) {
    LOG_WARN("failed to get multi_version_start", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
1263
int ObMultiVersionTableStore::get_max_major_sstable_snapshot(int64_t &max_snapshot_version)
O
oceanbase-admin 已提交
1264 1265
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1266
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
  ObTableHandle handle;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_latest_major_sstable(handle))) {
    if (OB_ENTRY_NOT_EXIST == ret) {
      ret = OB_SUCCESS;
      max_snapshot_version = INT64_MIN;
    } else {
      LOG_WARN("failed to get multi_version_start", K(ret), K(pkey_), K(table_id_));
    }
  } else {
    max_snapshot_version = handle.get_table()->get_snapshot_version();
  }
  return ret;
}

M
mw0 已提交
1287
int ObMultiVersionTableStore::get_min_max_major_version(int64_t &min_version, int64_t &max_version)
O
oceanbase-admin 已提交
1288 1289
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1290
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_min_max_major_version(min_version, max_version))) {
    LOG_WARN("failed to get multi_version_start", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
1304
int ObMultiVersionTableStore::get_latest_minor_sstables(ObTablesHandle &handle)
O
oceanbase-admin 已提交
1305 1306
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1307
  ObTableStore *table_store = nullptr;
O
oceanbase-admin 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("latest_store can not be null", K(ret), K(*this));
  } else if (OB_FAIL(table_store->get_minor_sstables(handle))) {
    LOG_WARN("failed to get_minor_sstables", K(ret), K(*this));
  }
  return ret;
}

M
mw0 已提交
1320
int ObMultiVersionTableStore::add_complement_minor_sstable_if_needed_(AddTableParam &param, ObTableStore &store)
O
oceanbase-admin 已提交
1321 1322 1323 1324 1325 1326 1327 1328
{
  int ret = OB_SUCCESS;
  if (nullptr == param.complement_minor_sstable_) {
    param.complement_minor_sstable_ = store.get_complement_minor_sstable();
  }
  return ret;
}

M
mw0 已提交
1329
int ObMultiVersionTableStore::get_latest_continue_tables(ObTablesHandle &handle)
O
oceanbase-admin 已提交
1330 1331
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1332
  ObTableStore *latest_store = NULL;
O
oceanbase-admin 已提交
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(latest_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("latest store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(latest_store->get_continue_tables(handle))) {
    LOG_WARN("failed to get continue tables", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
1345
int ObMultiVersionTableStore::get_min_schema_version(int64_t &min_schema_version)
O
oceanbase-admin 已提交
1346 1347 1348
{
  int ret = OB_SUCCESS;
  min_schema_version = 0;
M
mw0 已提交
1349
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_min_schema_version(min_schema_version))) {
    LOG_WARN("failed to get schema version", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
1363
int ObMultiVersionTableStore::get_flashback_major_tables(const int64_t flashback_scn, ObTablesHandle &handle)
O
oceanbase-admin 已提交
1364 1365
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1366
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_flashback_major_sstable(flashback_scn, handle))) {
    LOG_WARN("failed to get latest sstable", K(ret), K(pkey_), K(table_id_));
  }
  return ret;
}

int ObMultiVersionTableStore::get_mark_deletion_tables(
M
mw0 已提交
1381
    const int64_t end_log_ts, const int64_t snapshot_version, ObTablesHandle &handle)
O
oceanbase-admin 已提交
1382 1383
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1384
  ObTableStore *table_store = nullptr;
O
oceanbase-admin 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObMultiVersionTableStore is not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_mark_deletion_tables(end_log_ts, snapshot_version, handle))) {
    LOG_WARN("failed to get_mark_deletion_tables", K(ret), K(table_id_));
  }
  return ret;
}

int ObMultiVersionTableStore::clear_complement_minor_sstable()
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1401
  ObTableStore *table_store = nullptr;
O
oceanbase-admin 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObMultiVersionTableStore is not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->clear_complement_minor_sstable())) {
    LOG_WARN("failed to clear_complement_minor_sstable", K(ret), K(table_id_));
  }
  return ret;
}

M
mw0 已提交
1415
int ObMultiVersionTableStore::get_schema_version(int64_t &schema_version)
O
oceanbase-admin 已提交
1416 1417
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1418
  ObTableStore *table_store = nullptr;
O
oceanbase-admin 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
  int64_t table_count = 0;
  schema_version = INT64_MAX;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObMultiVersionTableStore is not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("table store must not null", K(ret), K_(pkey), K_(table_id));
  } else if (OB_FAIL(table_store->get_table_count(table_count))) {
    LOG_WARN("failed to get_table_count", K(ret));
  } else if (table_count > 0) {
    if (OB_FAIL(table_store->get_schema_version(schema_version))) {
      LOG_WARN("failed to get_schema_version", K(ret));
    }
  } else {
    schema_version = create_schema_version_;
  }
  return ret;
}

int ObMultiVersionTableStore::get_needed_local_tables_for_migrate(
M
mw0 已提交
1441
    const ObMigrateRemoteTableInfo &remote_table_info, ObTablesHandle &handle)
O
oceanbase-admin 已提交
1442 1443
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1444
  ObTableStore *table_store;
O
oceanbase-admin 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    LOG_WARN("multi version table store is inited", K(ret));
  } else if (pkey_.is_trans_table()) {
    handle.reset();
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_WARN("table store should not be null", K(ret));
  } else if (OB_FAIL(table_store->get_needed_local_tables_for_migrate(remote_table_info, handle))) {
    LOG_WARN("failed to get_needed_local_tables_for_migrate", K(ret), K(remote_table_info));
  }
  return ret;
}

M
mw0 已提交
1459
int ObMultiVersionTableStore::get_migrate_tables(ObTablesHandle &handle, bool &is_ready_for_read)
O
oceanbase-admin 已提交
1460 1461
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1462
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
  is_ready_for_read = false;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_all_sstables(false /*need_complent*/, handle))) {
    LOG_WARN("failed to get latest sstable", K(ret), K(pkey_), K(table_id_));
  } else {
    is_ready_for_read = table_store->is_ready_for_read();
  }

  return ret;
}

int ObMultiVersionTableStore::set_drop_schema_info(const int64_t drop_schema_version)
{
  int ret = OB_SUCCESS;
  drop_schema_version_ = drop_schema_version;
  drop_schema_refreshed_ts_ = ObTimeUtility::current_time();
  return ret;
}

M
mw0 已提交
1488
int ObMultiVersionTableStore::get_drop_schema_info(int64_t &drop_schema_version, int64_t &drop_schema_refreshed_ts)
O
oceanbase-admin 已提交
1489 1490 1491 1492 1493 1494 1495
{
  int ret = OB_SUCCESS;
  drop_schema_version = drop_schema_version_;
  drop_schema_refreshed_ts = drop_schema_refreshed_ts_;
  return ret;
}

M
mw0 已提交
1496
int ObMultiVersionTableStore::get_recovery_point_tables(const int64_t snapshot_version, ObTablesHandle &handle)
O
oceanbase-admin 已提交
1497 1498
{
  int ret = OB_SUCCESS;
M
mw0 已提交
1499
  ObTableStore *table_store = NULL;
O
oceanbase-admin 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
  int64_t multi_version_start = 0;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not inited", K(ret), K(*this));
  } else if (OB_ISNULL(table_store = get_latest_store())) {
    ret = OB_ERR_SYS;
    LOG_ERROR("store must not null", K(ret), K(pkey_), K(table_id_));
  } else if (OB_FAIL(table_store->get_recovery_point_tables(snapshot_version, handle))) {
    LOG_WARN("failed to get recovery point tables", K(ret), K(snapshot_version));
  }
  return ret;
}