ob_partition_log_service.cpp 322.9 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 170 171 172 173 174 175 176 177 178 179 180 181 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 214 215 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/**
 * 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 CLOG

#include "ob_partition_log_service.h"
#include <algorithm>
#include "share/ob_lease_struct.h"
#include "share/allocator/ob_tenant_mutil_allocator.h"
#include "lib/objectpool/ob_concurrency_objpool.h"
#include "lib/io/ob_io_manager.h"
#include "share/ob_cluster_version.h"
#include "share/ob_multi_cluster_util.h"
#include "election/ob_election.h"
#include "election/ob_election_mgr.h"
#include "election/ob_election_priority.h"
#include "storage/ob_storage_log_type.h"
#include "storage/transaction/ob_trans_log.h"
#include "storage/transaction/ob_trans_ctx.h"
#include "storage/transaction/ob_trans_log.h"
#include "observer/ob_server.h"
#include "ob_fetch_log_engine.h"
#include "ob_i_log_engine.h"
#include "ob_log_event_task_V2.h"
#include "ob_log_replay_engine_wrapper.h"
#include "ob_log_req.h"
#include "ob_remote_log_query_engine.h"
#include "common/ob_clock_generator.h"
#include "storage/transaction/ob_weak_read_util.h"
#include "archive/ob_archive_mgr.h"
#include "archive/ob_archive_restore_engine.h"
#include "observer/ob_server_event_history_table_operator.h"
#include "share/ob_server_blacklist.h"
#include "lib/resource/ob_resource_mgr.h"
#include "share/ob_bg_thread_monitor.h"

namespace oceanbase {
using namespace common;
using namespace share;
using namespace archive;
using namespace transaction;
using namespace election;
namespace clog {

void ObCandidateInfo::reset()
{
  role_ = INVALID_ROLE;
  is_tenant_active_ = false;
  is_dest_splitting_ = false;
  on_revoke_blacklist_ = false;
  on_loop_blacklist_ = false;
  replica_type_ = REPLICA_TYPE_MAX;
  server_status_ = share::RSS_INVALID;
  is_clog_disk_full_ = false;
  is_offline_ = false;
  is_need_rebuild_ = false;
  is_partition_candidate_ = false;
  is_disk_error_ = false;
  memstore_percent_ = -1;
}

int ObCursorArrayCache::init()
{
  int ret = OB_SUCCESS;
  for (int64_t i = 0; i < SCAN_THREAD_CNT; i++) {
    cursor_start_log_id_[i] = OB_INVALID_ID;
    cursor_end_log_id_[i] = OB_INVALID_ID;
  }
  return ret;
}

ObPartitionLogService::ObPartitionLogService()
    : is_inited_(false),
      lock_(ObLatchIds::PARTITION_LOG_LOCK),
      sw_(),
      ms_task_mgr_(),
      state_mgr_(),
      reconfirm_(),
      log_engine_(NULL),
      replay_engine_(NULL),
      election_(NULL),
      election_mgr_(NULL),
      partition_service_(NULL),
      partition_key_(),
      mm_(),
      cascading_mgr_(),
      restore_mgr_(),
      broadcast_info_mgr_(NULL),
      fetch_log_engine_(NULL),
      alloc_mgr_(NULL),
      event_scheduler_(NULL),
      self_(),
      checksum_(),
      saved_base_storage_info_(),
      zone_priority_(UINT64_MAX),
      is_candidate_(true),  // default true for recording change
      last_rebuild_time_(OB_INVALID_TIMESTAMP),
      ack_log_time_(OB_INVALID_TIMESTAMP),
      recv_child_next_ilog_ts_time_(OB_INVALID_TIMESTAMP),
      submit_log_mc_time_(OB_INVALID_TIMESTAMP),
      in_sync_check_time_(OB_INVALID_TIMESTAMP),
      update_next_replay_log_time_(OB_INVALID_TIMESTAMP),
      need_rebuild_(false),
      send_reject_msg_warn_time_(OB_INVALID_TIMESTAMP),
      leader_max_log_interval_(OB_INVALID_TIMESTAMP),
      cannot_slide_sw_interval_(OB_INVALID_TIMESTAMP),
      remote_log_query_engine_(NULL),
      archive_mgr_(NULL),
      log_archive_status_(),
      is_in_leader_member_list_(true),
      last_leader_keepalive_time_(OB_INVALID_TIMESTAMP),
      last_check_state_time_(OB_INVALID_TIMESTAMP),
      event_task_lock_(),
      has_event_task_(false),
      max_flushed_ilog_id_(0),
      scan_confirmed_log_cnt_(0),
      election_has_removed_(true),
      cursor_array_cache_(NULL)
{}

int ObPartitionLogService::init(ObILogEngine* log_engine, ObLogReplayEngineWrapper* replay_engine,
    ObIFetchLogEngine* fetch_log_engine, election::ObIElectionMgr* election_mgr,
    storage::ObPartitionService* partition_service, ObILogCallbackEngine* cb_engine, common::ObILogAllocator* alloc_mgr,
    ObLogEventScheduler* event_scheduler, const ObAddr& self, const ObVersion& data_version,
    const ObPartitionKey& partition_key, const ObReplicaType replica_type, const ObReplicaProperty replica_property,
    const ObBaseStorageInfo& base_storage_info, const int16_t archive_restore_state, const bool need_skip_mlist_check,
    ObRemoteLogQueryEngine* remote_log_query_engine, ObArchiveMgr* archive_mgr,
    ObArchiveRestoreEngine* archive_restore_engine, const enum ObCreatePlsType& create_pls_type)
{
  int ret = OB_SUCCESS;
  if (is_inited_) {
    ret = OB_INIT_TWICE;
  } else if (OB_ISNULL(log_engine) || OB_ISNULL(replay_engine) || OB_ISNULL(fetch_log_engine) ||
             OB_ISNULL(election_mgr) || OB_ISNULL(partition_service) || OB_ISNULL(cb_engine) || OB_ISNULL(alloc_mgr) ||
             OB_ISNULL(event_scheduler) || !self.is_valid() || !data_version.is_valid() || !partition_key.is_valid() ||
             OB_ISNULL(remote_log_query_engine) || OB_ISNULL(archive_mgr) || OB_ISNULL(archive_restore_engine)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(partition_key), KP(archive_mgr), KP(archive_restore_engine));
  } else if (OB_FAIL(checksum_.init(base_storage_info.get_last_replay_log_id() + 1,
                 base_storage_info.get_accumulate_checksum(),
                 partition_key))) {
    CLOG_LOG(WARN, "checksum init failed", K(partition_key), K(ret));
  } else if (OB_FAIL(
                 election_mgr->add_partition(partition_key, base_storage_info.get_replica_num(), this, election_))) {
    CLOG_LOG(WARN, "election_mgr->add_partition failed", K(partition_key), K(ret));
  } else if (OB_FAIL(state_mgr_.init(&sw_,
                 &reconfirm_,
                 log_engine,
                 &mm_,
                 &cascading_mgr_,
                 &restore_mgr_,
                 election_,
                 replay_engine,
                 partition_service,
                 alloc_mgr,
                 self,
                 base_storage_info.get_proposal_id(),
                 data_version,
                 partition_key,
                 create_pls_type))) {
    CLOG_LOG(WARN, "state_mgr init failed", K(partition_key), K(ret));
  } else if (OB_FAIL(ms_task_mgr_.init(log_engine,
                 &state_mgr_,
                 &mm_,
                 &cascading_mgr_,
                 cb_engine,
                 &restore_mgr_,
                 partition_service,
                 alloc_mgr,
                 self,
                 partition_key))) {
    CLOG_LOG(WARN, "ms_task_mgr init failed", K(partition_key), K(ret));
  } else if (OB_FAIL(reconfirm_.init(&sw_,
                 &state_mgr_,
                 &mm_,
                 &cascading_mgr_,
                 log_engine,
                 alloc_mgr,
                 partition_key,
                 self,
                 base_storage_info.get_submit_timestamp()))) {
    CLOG_LOG(WARN, "reconfirm init failed", K(partition_key), K(ret));
  } else if (OB_FAIL(sw_.init(replay_engine,
                 log_engine,
                 &state_mgr_,
                 &mm_,
                 &cascading_mgr_,
                 partition_service,
                 alloc_mgr,
                 &checksum_,
                 cb_engine,
                 &restore_mgr_,
                 self,
                 partition_key,
                 base_storage_info.get_epoch_id(),
                 base_storage_info.get_last_replay_log_id(),
                 base_storage_info.get_submit_timestamp(),
                 base_storage_info.get_accumulate_checksum()))) {
    CLOG_LOG(WARN, "sliding_window init failed", K(partition_key), K(ret));
  } else if (OB_FAIL(restore_mgr_.init(partition_service,
                 archive_restore_engine,
                 &sw_,
                 log_engine,
                 &mm_,
                 partition_key,
                 self,
                 archive_restore_state))) {
    CLOG_LOG(WARN, "restore_mgr init failed", K(partition_key), K(ret));
  } else if (OB_FAIL(mm_.init(base_storage_info.get_curr_member_list(),
                 base_storage_info.get_membership_timestamp(),
                 base_storage_info.get_membership_log_id(),
                 base_storage_info.get_ms_proposal_id(),
                 replica_type,
                 replica_property,
                 base_storage_info.get_replica_num(),
                 need_skip_mlist_check,
                 self,
                 partition_key,
                 &sw_,
                 &ms_task_mgr_,
                 &state_mgr_,
                 &cascading_mgr_,
                 cb_engine,
                 partition_service,
                 alloc_mgr))) {
    CLOG_LOG(WARN, "membership_mgr init failed", K(partition_key), K(base_storage_info), K(ret));
  } else if (OB_FAIL(
                 cascading_mgr_.init(self, partition_key, &sw_, &state_mgr_, &mm_, log_engine, partition_service))) {
    CLOG_LOG(WARN, "cascading_mgr_ init failed", K(partition_key), K(ret));
  } else if (OB_FAIL(saved_base_storage_info_.deep_copy(base_storage_info))) {
    CLOG_LOG(WARN, "base_storage_info deep_copy failed", K(partition_key), K(ret));
  } else if (REPLICA_TYPE_LOGONLY == replica_type && OB_FAIL(alloc_broadcast_info_mgr_(partition_key))) {
    CLOG_LOG(WARN, "alloc_broadcast_info_mgr_ failed", K(partition_key), K(ret));
  } else if (ASSIGN_PARTITION == create_pls_type && OB_FAIL(alloc_cursor_array_cache_(partition_key))) {
    CLOG_LOG(WARN, "alloc_cursor_array_cache_ failed", K(partition_key), K(ret));
  } else {
    log_engine_ = log_engine;
    replay_engine_ = replay_engine;
    election_mgr_ = election_mgr;
    partition_service_ = partition_service;
    partition_key_ = partition_key;
    fetch_log_engine_ = fetch_log_engine;
    alloc_mgr_ = alloc_mgr;
    event_scheduler_ = event_scheduler;
    self_ = self;
    remote_log_query_engine_ = remote_log_query_engine;
    archive_mgr_ = archive_mgr;
    max_flushed_ilog_id_ = 0;
    scan_confirmed_log_cnt_ = 0;
Y
yy0 已提交
259 260 261 262 263

    // priority of log replica include encrypted log replica should be initialize with UINT64_MAX,
    // while other replica types with 0.
    const uint64_t zone_priority = ObReplicaTypeCheck::is_log_replica(replica_type) ? UINT64_MAX : 0;
    ATOMIC_STORE(&zone_priority_, zone_priority);
O
oceanbase-admin 已提交
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
    is_inited_ = true;
    election_has_removed_ = false;
  }

  if (OB_SUCCESS != ret && !is_inited_) {
    // election obj need to be removed when pls init failed
    // state_mgr_ need to destroy before remove election, because it holds election pointer
    if (state_mgr_.is_inited()) {
      state_mgr_.destroy();
    }
    // remove election
    int tmp_ret = OB_SUCCESS;
    if (NULL != election_mgr && NULL != election_ &&
        OB_SUCCESS != (tmp_ret = election_mgr->remove_partition(partition_key))) {
      CLOG_LOG(WARN, "remove_partition from election_mgr failed", K(tmp_ret), K(partition_key));
    }
    // revert election
    if (NULL != election_mgr && NULL != election_) {
      election_mgr->revert_election(election_);
      election_ = NULL;
    } else {
      CLOG_LOG(WARN, "revert election to election_mgr failed", K_(partition_key), KP(election_mgr), KP_(election));
    }
    free_cursor_array_cache_();
    free_broadcast_info_mgr_();
  }
  FLOG_INFO("ObPartitionLogService init finished",
      K_(partition_key),
      K(ret),
      K(self),
      K(base_storage_info),
      K_(election_has_removed),
      K_(election),
      KP(this));
  return ret;
}

int ObPartitionLogService::get_log_id_timestamp(const int64_t base_timestamp, ObLogMeta& log_meta)
{
  int ret = OB_SUCCESS;
#ifdef ERRSIM
  static int64_t retry_count = 1;
  ret = E(EventTable::EN_BIG_ROW_REPLAY_FOR_MINORING) OB_SUCCESS;
  if (OB_SUCCESS != ret) {
    ATOMIC_FAA(&retry_count, 1);
    if (0 == (retry_count % 3)) {
      ret = OB_SUCCESS;
    } else {
      // ERRSIM: make retry times at least 3
      TRANS_LOG(WARN, "get log id and timestamp need retry", K(retry_count));
      return ret;
    }
  }
#endif
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (base_timestamp <= 0) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K(base_timestamp));
  } else if (!log_engine_->is_disk_space_enough()) {
    ret = OB_LOG_OUTOF_DISK_SPACE;
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(WARN, "log outof disk space", K(ret), K(partition_key_));
    }
  } else if (lock_.try_rdlock()) {
    uint64_t log_id = 0;
    int64_t timestamp = 0;

    if (OB_FAIL(state_mgr_.check_role_leader_and_state())) {
      CLOG_LOG(WARN, "check_role_leader_and_state false", K_(partition_key), K(ret));
    } else if (state_mgr_.is_switching_cluster_replica(partition_key_.get_table_id())) {
      // non-private table's leader is in switching state, return -4203 to trigger retry in transaction module
      ret = OB_EAGAIN;
    } else if (!state_mgr_.can_submit_log()) {
      ret = OB_NOT_MASTER;
      CLOG_LOG(WARN,
          "can_submit_log return false",
          K_(partition_key),
          K(ret),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state());
    } else if (!mm_.is_state_init() || state_mgr_.is_pre_member_changing()) {
      if (partition_reach_time_interval(2 * 1000, submit_log_mc_time_)) {
        CLOG_LOG(INFO,
            "member change is going on",
            K_(partition_key),
            K(mm_.is_state_init()),
            K(state_mgr_.is_pre_member_changing()));
      }
      ret = OB_EAGAIN;
    } else if (OB_FAIL(sw_.alloc_log_id(base_timestamp, log_id, timestamp))) {
      if (OB_EAGAIN == ret) {
        ret = OB_TOO_LARGE_LOG_ID;
      }
    } else if (OB_FAIL(log_meta.set(log_id, timestamp, state_mgr_.get_proposal_id()))) {
      CLOG_LOG(WARN, "set log meta failed", K(ret), K(log_id), K(timestamp), K(state_mgr_.get_proposal_id()));
    } else {
    }
    lock_.rdunlock();
  } else {
    ret = OB_EAGAIN;
  }

  return ret;
}

int ObPartitionLogService::submit_aggre_log(ObAggreBuffer* buffer, const int64_t base_timestamp)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!log_engine_->is_disk_space_enough()) {
    ret = OB_LOG_OUTOF_DISK_SPACE;
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(WARN, "log outof disk space", K(ret), K(partition_key_));
    }
  } else if (lock_.try_rdlock()) {
    if (OB_FAIL(state_mgr_.check_role_leader_and_state())) {
      CLOG_LOG(WARN, "check_role_leader_and_state false", K_(partition_key), K(ret));
    } else if (state_mgr_.is_switching_cluster_replica(partition_key_.get_table_id())) {
      // non-private table's leader is in switching state, return -4203 to trigger retry in transaction module
      ret = OB_EAGAIN;
    } else if (!state_mgr_.can_submit_log()) {
      ret = OB_NOT_MASTER;
      CLOG_LOG(WARN,
          "can_submit_log return false",
          K_(partition_key),
          K(ret),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state());
    } else if (!mm_.is_state_init() || state_mgr_.is_pre_member_changing()) {
      if (partition_reach_time_interval(2 * 1000, submit_log_mc_time_)) {
        CLOG_LOG(INFO,
            "member change is going on",
            K_(partition_key),
            K(mm_.is_state_init()),
            K(state_mgr_.is_pre_member_changing()));
      }
      ret = OB_EAGAIN;
    } else if (OB_FAIL(sw_.submit_aggre_log(buffer, base_timestamp))) {
      if (REACH_TIME_INTERVAL(1000 * 1000)) {
        CLOG_LOG(WARN,
            "submit_log failed",
            K(ret),
            K_(partition_key),
            "role",
            state_mgr_.get_role(),
            "state",
            state_mgr_.get_state());
      }
    }

    if (OB_ALLOCATE_MEMORY_FAILED == ret) {
      int tmp_ret = OB_SUCCESS;
      const uint32_t revoke_type = ObElection::RevokeType::SUBMIT_LOG_MEMORY_ALLOC_FAIL;
      if (OB_SUCCESS != (tmp_ret = election_mgr_->leader_revoke(partition_key_, revoke_type))) {
        CLOG_LOG(ERROR, "election_mgr_ leader_revoke failed", K(ret), K(tmp_ret), K(partition_key_), K(revoke_type));
      }
    }
    lock_.rdunlock();
  } else {
    ret = OB_EAGAIN;
  }
  return ret;
}

int ObPartitionLogService::submit_log(const char* buff, const int64_t size, const int64_t base_timestamp,
    ObISubmitLogCb* submit_cb, const bool is_trans_log, uint64_t& log_id, int64_t& log_timestamp)
{
  int ret = OB_SUCCESS;
  const ObLogType log_type = OB_LOG_SUBMIT;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_ISNULL(buff) || size > OB_MAX_LOG_ALLOWED_SIZE || base_timestamp < 0 ||
             base_timestamp > INT64_MAX / 2 || OB_ISNULL(submit_cb)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K_(partition_key), KP(buff), K(size), K(base_timestamp), KP(submit_cb));
  } else if (!log_engine_->is_disk_space_enough()) {
    ret = OB_LOG_OUTOF_DISK_SPACE;
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(WARN, "log outof disk space", K(ret), K(partition_key_));
    }
  } else if (lock_.try_rdlock()) {
    if (NULL != submit_cb) {
      submit_cb->base_class_reset();
    }

    if (OB_FAIL(state_mgr_.check_role_leader_and_state())) {
      CLOG_LOG(WARN, "check_role_leader_and_state false", K_(partition_key), K(ret));
    } else if (state_mgr_.is_switching_cluster_replica(partition_key_.get_table_id())) {
      // non-private table's leader is in switching state, return -4203 to trigger retry in transaction module
      ret = OB_EAGAIN;
    } else if (!state_mgr_.can_submit_log()) {
      ret = OB_NOT_MASTER;
      CLOG_LOG(WARN,
          "can_submit_log return false",
          K_(partition_key),
          K(ret),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state());
    } else if (!mm_.is_state_init() || state_mgr_.is_pre_member_changing()) {
      if (partition_reach_time_interval(2 * 1000, submit_log_mc_time_)) {
        CLOG_LOG(INFO,
            "member change is going on",
            K_(partition_key),
            K(mm_.is_state_init()),
            K(state_mgr_.is_pre_member_changing()));
      }
      ret = OB_EAGAIN;
    } else if (OB_FAIL(sw_.submit_log(
                   log_type, buff, size, base_timestamp, is_trans_log, submit_cb, log_id, log_timestamp))) {
      const int tmp_ret = ret;
      if (OB_ALLOCATE_MEMORY_FAILED != ret) {
        ret = OB_EAGAIN;
      }
      if (REACH_TIME_INTERVAL(1000 * 1000)) {
        CLOG_LOG(WARN,
            "submit_log failed",
            K(ret),
            K(tmp_ret),
            K_(partition_key),
            KP(buff),
            K(size),
            "role",
            state_mgr_.get_role(),
            "state",
            state_mgr_.get_state());
      }
    }

    if (OB_ALLOCATE_MEMORY_FAILED == ret) {
      int tmp_ret = OB_SUCCESS;
      const uint32_t revoke_type = ObElection::RevokeType::SUBMIT_LOG_MEMORY_ALLOC_FAIL;
      if (OB_SUCCESS != (tmp_ret = election_mgr_->leader_revoke(partition_key_, revoke_type))) {
        CLOG_LOG(ERROR, "election_mgr_ leader_revoke failed", K(ret), K(tmp_ret), K(partition_key_), K(revoke_type));
      }
    }
    lock_.rdunlock();
  } else {
    ret = OB_EAGAIN;
  }
  if (OB_FAIL(ret) && OB_NOT_INIT != ret && OB_INVALID_ARGUMENT != ret && OB_NOT_MASTER != ret && OB_EAGAIN != ret &&
      OB_ALLOCATE_MEMORY_FAILED != ret) {
    CLOG_LOG(ERROR,
        "submit_log failed",
        K(ret),
        K_(partition_key),
        KP(buff),
        K(size),
        K(base_timestamp),
        K(is_trans_log),
        "role",
        state_mgr_.get_role(),
        "state",
        state_mgr_.get_state());
  }
  return ret;
}

int ObPartitionLogService::get_role(common::ObRole& role) const
{
  // this func returns real role
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    role = FOLLOWER;
    if (!restore_mgr_.is_archive_restoring()) {
      int64_t unused_epoch = 0;
      ObTsWindows unused_windows;
      if (OB_SUCCESS == get_role_unlock(unused_epoch, unused_windows)) {
        // LEADER or STANDBY_LEADER or RESTORE_LEADER, and lease is valid
        role = state_mgr_.get_role();
      } else {
        // lease is expired or FOLLOWER role
        role = FOLLOWER;
      }
    } else {
      role = restore_mgr_.get_role();
    }
  }
  return ret;
}

int ObPartitionLogService::get_role_unsafe(int64_t& leader_epoch, ObTsWindows& changing_leader_windows) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!state_mgr_.is_leader_active() || state_mgr_.is_offline()) {
    ret = OB_NOT_MASTER;
  } else if (state_mgr_.is_changing_leader() && !check_election_leader_(leader_epoch, changing_leader_windows)) {
    // it is changing leader or lease is expired
    ret = OB_NOT_MASTER;
  } else {
    leader_epoch = state_mgr_.get_leader_epoch();
    if (!election::ObElection::is_valid_leader_epoch(leader_epoch)) {
      // leader_epoch is invalid
      ret = OB_NOT_MASTER;
      TRANS_LOG(WARN, "leader epoch not big than 0, unexpected error", K(ret), K(leader_epoch));
    }
  }
  return ret;
}

int ObPartitionLogService::get_role_unlock(int64_t& leader_epoch, ObTsWindows& changing_leader_windows) const
{
  // return -4038 when state is not ACTIVE or role is not LEADER, STANDBY_LEADER or RESTORE_LEADER
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_SUCCESS != (ret = state_mgr_.check_role_elect_leader_and_state())) {
    ret = OB_NOT_MASTER;
  } else if (!check_election_leader_(leader_epoch, changing_leader_windows)) {
    ret = OB_NOT_MASTER;
  } else {
    ret = OB_SUCCESS;
  }
  return ret;
}

int ObPartitionLogService::get_role_and_last_leader_active_time(common::ObRole& role, int64_t& timestamp) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    role = FOLLOWER;
    timestamp = OB_INVALID_TIMESTAMP;

    if (!restore_mgr_.is_archive_restoring()) {
      int64_t unused_epoch = 0;
      ObTsWindows unused_windows;
605
      if (OB_SUCC(get_role_unlock(unused_epoch, unused_windows))) {
O
oceanbase-admin 已提交
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 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 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
        role = state_mgr_.get_role();
      } else if (OB_NOT_MASTER == ret) {
        ret = OB_SUCCESS;
      } else {
        // do nothing
      }
      timestamp = state_mgr_.get_last_leader_active_time();
    } else {
      role = restore_mgr_.get_role();
      timestamp = restore_mgr_.get_last_takeover_time();
    }
  }
  return ret;
}

int ObPartitionLogService::get_role_and_leader_epoch(common::ObRole& role, int64_t& leader_epoch)
{
  int64_t unused_takeover_time = 0;
  return get_role_and_leader_epoch(role, leader_epoch, unused_takeover_time);
}

int ObPartitionLogService::get_role_and_leader_epoch(
    common::ObRole& role, int64_t& leader_epoch, int64_t& takeover_time)
{
  int ret = OB_SUCCESS;
  takeover_time = OB_INVALID_TIMESTAMP;

  if (IS_NOT_INIT) {
    ret = OB_SUCCESS;
  } else {
    RLockGuard guard(lock_);
    (void)(get_role_and_leader_epoch_unlock_(role, leader_epoch, takeover_time));
  }
  return ret;
}

int ObPartitionLogService::add_member(const ObMember& member, const int64_t quorum, obrpc::ObMCLogInfo& log_info)
{
  int ret = OB_SUCCESS;
  if (!member.is_valid() || quorum <= 0 || quorum > OB_MAX_MEMBER_NUMBER) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K(member));
  } else {
    ret = change_member_(member, true, quorum, log_info);
  }
  return ret;
}

int ObPartitionLogService::remove_member(const ObMember& member, const int64_t quorum, obrpc::ObMCLogInfo& log_info)
{
  int ret = OB_SUCCESS;
  if (!member.is_valid() || quorum <= 0 || quorum > OB_MAX_MEMBER_NUMBER) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K(member), K(quorum));
  } else {
    ret = change_member_(member, false, quorum, log_info);
  }
  return ret;
}

// for migrate to check if remove member is done
//------------------------------------
//     return value          |           meaning
//      OB_SUCCESS           |  remove member finished, can add replica
//      OB_EAGAIN            |  just retry check
// OB_MEMBER_CHANGE_FAILED   |  remove member failed, retry remove member
//  OB_INVALID_ARGUMENT      |  not invalid arguments
//       OTHER               |         bug!!!!
int ObPartitionLogService::is_member_change_done(const obrpc::ObMCLogInfo& log_info)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  ObLogTask* task = NULL;
  const int64_t* ref = NULL;
  uint64_t last_replay_log_id = OB_INVALID_ID;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!log_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K(partition_key_), K(log_info));
  } else if (state_mgr_.is_can_elect_standby_leader()) {
    // member change for standby cluster
    if (ms_task_mgr_.is_renew_ms_log_majority_success(log_info.log_id_, log_info.timestamp_)) {
      CLOG_LOG(INFO, "standby member change finished", K(ret), K(partition_key_), K(log_info));
    } else {
      ret = OB_EAGAIN;
    }
  } else {
    if (OB_FAIL(get_storage_last_replay_log_id_(last_replay_log_id))) {
      CLOG_LOG(WARN, "get_storage_last_replay_log_id_ failed", K(ret), K(partition_key_));
    } else if (OB_ERROR_OUT_OF_RANGE == (ret = sw_.get_log_task(log_info.log_id_, task, ref))) {
      ObLogCursorExt log_cursor;
      log_cursor.reset();
      ret = OB_SUCCESS;
      if (OB_FAIL(log_engine_->get_cursor(partition_key_, log_info.log_id_, log_cursor))) {
        CLOG_LOG(WARN, "not hit in cursor cache", K_(partition_key), K(ret), K(log_info));
        if ((OB_CURSOR_NOT_EXIST == ret && log_info.log_id_ > last_replay_log_id) || OB_ERR_OUT_OF_UPPER_BOUND == ret ||
            OB_NEED_RETRY == ret) {
          ret = OB_EAGAIN;
        }
      } else {
        ObLogEntry log_entry;
        ObReadParam read_param;
        read_param.file_id_ = log_cursor.get_file_id();
        read_param.offset_ = log_cursor.get_offset();
        read_param.read_len_ = log_cursor.get_size();
        ObReadBufGuard guard(ObModIds::OB_LOG_DIRECT_READER_CACHE_ID);
        ObReadBuf& rbuf = guard.get_read_buf();
        if (OB_UNLIKELY(!rbuf.is_valid())) {
          ret = OB_ALLOCATE_MEMORY_FAILED;
          CLOG_LOG(WARN, "failed to alloc read_buf", K_(partition_key), K(log_info), K(read_param), K(ret));
        } else if (OB_FAIL(log_engine_->read_log_by_location(read_param, rbuf, log_entry))) {
          CLOG_LOG(WARN, "read_log_by_location failed", K_(partition_key), K(ret), K(read_param));
        } else if (OB_LOG_MEMBERSHIP == log_entry.get_header().get_log_type() &&
                   log_info.timestamp_ == log_entry.get_header().get_submit_timestamp()) {
          CLOG_LOG(INFO, "member ship log is slide out", K_(partition_key), K(ret), K(log_entry));
          // SUCCESS
        } else {
          ret = OB_MEMBER_CHANGE_FAILED;
          CLOG_LOG(INFO, "log rewritten, member change failed", K_(partition_key), K(ret), K(log_info), K(log_entry));
        }
      }
    } else {
      RLockGuard guard(lock_);
      const uint64_t max_log_id = sw_.get_max_log_id();
      if (!state_mgr_.is_leader_active()) {
        ret = OB_EAGAIN;
      } else if (OB_ERR_NULL_VALUE == ret && log_info.log_id_ > max_log_id) {
        ret = OB_MEMBER_CHANGE_FAILED;
        CLOG_LOG(INFO,
            "leader active got null value, member change failed",
            K_(partition_key),
            K(ret),
            K(log_info),
            K(max_log_id));
      } else if (OB_ERR_NULL_VALUE == ret && log_info.log_id_ <= max_log_id) {
        ret = OB_EAGAIN;
        CLOG_LOG(INFO,
            "leader active got null value and log_id is less than max_log_id, need retry",
            K(ret),
            K(partition_key_),
            K(log_info),
            K(max_log_id));
      } else if (OB_SUCC(ret)) {
        task->lock();
        if (task->is_submit_log_exist()) {
          if (OB_LOG_MEMBERSHIP != task->get_log_type() || log_info.timestamp_ != task->get_submit_timestamp()) {
            ret = OB_MEMBER_CHANGE_FAILED;
            CLOG_LOG(INFO,
                "log in sliding window rewritten, member change failed",
                K_(partition_key),
                K(ret),
                K(log_info),
                K(task));
          } else {
            if (task->is_log_confirmed()) {
              CLOG_LOG(INFO,
                  "member ship log in sliding window confirmed, member change success",
                  K_(partition_key),
                  K(ret),
                  K(log_info),
                  K(task));
              // SUCCESS
            } else {
              ret = OB_EAGAIN;
              CLOG_LOG(TRACE,
                  "member ship log in sliding window not confirmed, need check again",
                  K_(partition_key),
                  K(ret),
                  K(log_info),
                  K(task));
            }
          }
        } else {
          ret = OB_MEMBER_CHANGE_FAILED;
          CLOG_LOG(INFO,
              "leader active just got confirm info, must not be member ship log",
              K_(partition_key),
              K(ret),
              K(log_info),
              K(task));
        }
        task->unlock();
      } else {
        CLOG_LOG(WARN, "get_log_task failed", K_(partition_key), K(ret), K(log_info));
      }
    }

    if (NULL != ref && OB_SUCCESS != (tmp_ret = sw_.revert_log_task(ref))) {
      CLOG_LOG(ERROR, "revert log task failed", K_(partition_key), K(tmp_ret), K(ref));
    } else {
      ref = NULL;
    }
  }

  // unexpected error code, please check it !
  if (OB_SUCCESS != ret && OB_EAGAIN != ret && OB_MEMBER_CHANGE_FAILED != ret && OB_INVALID_ARGUMENT != ret) {
    CLOG_LOG(
        ERROR, "is_member_change_done unexpected error code, please check it!", K(ret), K(partition_key_), K(log_info));
  }
  return ret;
}

int ObPartitionLogService::change_member_(
    const ObMember& member, const bool is_add_member, const int64_t quorum, obrpc::ObMCLogInfo& log_info)
{
  int ret = OB_SUCCESS;
  int64_t mc_timestamp = OB_INVALID_TIMESTAMP;
  ObMemberList member_list;
  ObProposalID proposal_id;
  bool is_standby_op = false;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!member.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(member), K(quorum), K(ret));
  } else {
    // ========== step 1 ===========
    // 1) check it can do chagne member
    // 2) get current membership_ts and member_list
    // 3) get current proposal_id
    //
    // step 1: both remove_member/add_member need exec
    do {
      RLockGuard guard(lock_);
      if (!state_mgr_.can_change_member()) {
        CLOG_LOG(WARN,
            "can not change_member",
            K_(partition_key),
            K(member),
            "role",
            state_mgr_.get_role(),
            "state",
            state_mgr_.get_state(),
            "is_changing_leader",
            state_mgr_.is_changing_leader(),
            K(is_add_member));
        ret = OB_STATE_NOT_MATCH;
      } else {
        mc_timestamp = mm_.get_timestamp();
        is_standby_op = (STANDBY_LEADER == state_mgr_.get_role() ? true : false);
        if (!is_standby_op) {
          proposal_id = state_mgr_.get_proposal_id();
        } else {
          proposal_id = mm_.get_ms_proposal_id();
        }
        if (OB_SUCCESS != (ret = member_list.deep_copy(mm_.get_curr_member_list()))) {
          CLOG_LOG(WARN, "member_list deep_copy failed", K_(partition_key), K(ret));
        } else {
          const int64_t member_num = member_list.get_member_number();
          if (is_add_member && quorum < (member_num + 1)) {
            // quorum is smaller than the number of new member_list
            ret = OB_NOT_SUPPORTED;
          } else if (!is_add_member && quorum < (member_num - 1)) {
            // quorum is smaller than the number of new member_list
            ret = OB_NOT_SUPPORTED;
          } else {
            // do nothing
          }
        }
      }
    } while (0);

    if (OB_SUCC(ret) && !is_add_member) {
      if (OB_FAIL(block_partition_split_())) {
        CLOG_LOG(WARN, "failed to block partition split", K(ret), K_(partition_key));
      }
    }

    // ========== step 2 ===========
    // check follower's state, it requires that follower's lags is behind leader's within 1000 entries
    //
    // step 2: both remove_member/add_member need exec
    if (OB_SUCCESS == ret && !check_remote_mc_ts_(member_list, member.get_server(), mc_timestamp)) {
      CLOG_LOG(WARN, "check_remote_mc_ts_ false", K_(partition_key), K(member_list), K(member), K(mc_timestamp));
      ret = OB_LOG_NOT_SYNC;
    }

    // ========== step 3 ===========
    // 1) Check that there are no ongoing membership changes
    // 2) stop submitting log
    //
    // step 3: only remove_member need exec
    if (OB_SUCC(ret) && !is_add_member) {
      WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
      if (OB_FAIL(ret)) {
        CLOG_LOG(
            WARN, "change_member wrlock failed", K(ret), K_(partition_key), K(member), K(is_add_member), K(quorum));
      } else if (state_mgr_.is_pre_member_changing()) {
        CLOG_LOG(WARN, "is_pre_member_changing true", K_(partition_key), K(member_list), K(member));
        ret = OB_STATE_NOT_MATCH;
      } else {
        state_mgr_.set_pre_member_changing();
      }
    }

    // ========== step 4 ===========
    // wait local logs confirmed, waiting 200ms at most without lock
    //
    // step 4: only remove_member need exec
    if (OB_SUCC(ret) && !is_add_member) {
      if (OB_FAIL(wait_log_confirmed_())) {
        CLOG_LOG(WARN, "wait_log_confirmed failed", K_(partition_key), K(ret), K(member_list), K(member));
        WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
        state_mgr_.reset_pre_member_changing();
        ret = OB_STATE_NOT_MATCH;
      }
    }

    // ========== step 5 ===========
    // check Follower' state, whether its logs are already confirmed by using sync RPC
    //
    // step 5: only remove_member need exec
    if (OB_SUCC(ret) && !is_add_member) {
      if (!check_remote_mc_ts_sync_(member_list, member.get_server(), mc_timestamp)) {
        CLOG_LOG(WARN, "check_remote_mc_ts_sync_ false", K_(partition_key), K(member_list), K(member), K(mc_timestamp));
        ret = OB_LOG_NOT_SYNC;
        WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
        state_mgr_.reset_pre_member_changing();
      }
    }

    // ========== step 6 ===========
    // 1) double check it can do member change
    // 2) double check current membership_ts is same with step 1
    // 3) double check current proposal_id is same with step 1
    // 4) execute member change
    //
    // step 6: both remove_member/add_member need exec
    if (OB_SUCC(ret)) {
      WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
      if (!state_mgr_.can_change_member()) {
        CLOG_LOG(WARN,
            "can not change_member",
            K_(partition_key),
            K(member),
            "role",
            state_mgr_.get_role(),
            "state",
            state_mgr_.get_state(),
            "is_changing_leader",
            state_mgr_.is_changing_leader(),
            K(is_add_member));
        ret = OB_STATE_NOT_MATCH;
      } else if (mc_timestamp != mm_.get_timestamp()) {
        CLOG_LOG(WARN,
            "timestamp not match",
            K_(partition_key),
            K(member),
            K(mc_timestamp),
            "current_timestamp",
            mm_.get_timestamp());
        ret = OB_STATE_NOT_MATCH;
      } else if (!is_standby_op && proposal_id != state_mgr_.get_proposal_id()) {
        ret = OB_STATE_NOT_MATCH;
        CLOG_LOG(WARN,
            "proposal_id not match",
            K(ret),
            K_(partition_key),
            K(member),
            K(proposal_id),
            "current_proposal_id",
            state_mgr_.get_proposal_id());
      } else if (is_standby_op && proposal_id != mm_.get_ms_proposal_id()) {
        ret = OB_STATE_NOT_MATCH;
        CLOG_LOG(WARN,
            "ms_proposal_id not match",
            K(ret),
            K_(partition_key),
            K(member),
            K(proposal_id),
            "current_proposal_id",
            mm_.get_ms_proposal_id());
      } else {
        if (is_add_member) {
          if (OB_SUCCESS != (ret = mm_.add_member(member, quorum, log_info))) {
            CLOG_LOG(WARN, "add member failed", K_(partition_key), K(ret), K(member_list), K(member));
          }
        } else {
          if (OB_SUCCESS != (ret = mm_.remove_member(member, quorum, log_info))) {
            CLOG_LOG(WARN, "remove member failed", K_(partition_key), K(ret), K(member_list), K(member));
          }
        }
      }
      if (!is_add_member) {
        // only remove_member need exec
        state_mgr_.reset_pre_member_changing();
      }
    }

    if (OB_SUCC(ret) && !is_add_member) {
      if (OB_FAIL(unblock_partition_split_())) {
        CLOG_LOG(WARN, "failed to block partition split", K(ret), K_(partition_key));
      }
    }
  }
  CLOG_LOG(INFO, "change_member function finished", K_(partition_key), K(ret), K(member), K(quorum), K(is_add_member));
  return ret;
}

int ObPartitionLogService::fill_base_storage_info(
    common::ObBaseStorageInfo& base_storage_info, uint64_t& sw_last_replay_log_id)
{
  int ret = OB_SUCCESS;
  uint64_t max_flushed_ilog_id = OB_INVALID_ID;

  if (OB_FAIL(query_max_flushed_ilog_id(max_flushed_ilog_id))) {
    CLOG_LOG(WARN, "query_max_flushed_ilog_id failed", K(ret), K(partition_key_));
  } else {
    // get max_flushed_ilog_id firstly
#ifdef ERRSIM
    if (ObServerConfig::get_instance()._ob_enable_rebuild_on_purpose) {
      // ERRSIM mode: if open _ob_enable_rebuild_on_purpose, skip checking max_flushed_ilog_id
      max_flushed_ilog_id = UINT64_MAX;
    }
#endif
    sw_last_replay_log_id = sw_.get_last_replay_log_id();
    const uint64_t last_replay_log_id = std::min(sw_last_replay_log_id, max_flushed_ilog_id);
    const int64_t last_submit_timestamp = 0;  // invalid timestamp
    const int64_t accumulate_checksum = 0;    // invalid checksum
    const int64_t epoch_id = sw_.get_epoch_id();
    const ObProposalID proposal_id = state_mgr_.get_proposal_id();

    int64_t replica_num = mm_.get_replica_num();
    int64_t ms_timestamp = mm_.get_timestamp();
    uint64_t mc_log_id = mm_.get_log_id();
    const ObProposalID ms_proposal_id = mm_.get_ms_proposal_id();

    common::ObMemberList member_list;
    if (mm_.get_curr_member_list().is_valid()) {
      if (OB_FAIL(member_list.deep_copy(mm_.get_curr_member_list()))) {
        CLOG_LOG(WARN, "member_list.deep_copy failed", K(ret), K_(partition_key));
      }
    } else if (restore_mgr_.is_standby_restore_state()) {
      // mock member_list for standby restoring replica
      ObAddr fake_server;
      if (OB_FAIL(fake_server.parse_from_cstring("127.0.0.1:12345"))) {
        STORAGE_LOG(WARN, "failed to parse fake server ip", K(ret), K_(partition_key));
      } else if (OB_FAIL(member_list.add_server(fake_server))) {
        STORAGE_LOG(WARN, "failed to add server", K(ret), K_(partition_key));
      } else {
        replica_num = 1;   // mock replica_num to 1
        ms_timestamp = 0;  // mock ms_timestamp to 0
      }
    }

    if (OB_SUCC(ret)) {
      ret = base_storage_info.init(epoch_id,
          proposal_id,
          last_replay_log_id,
          last_submit_timestamp,
          accumulate_checksum,
          replica_num,
          ms_timestamp,
          mc_log_id,
          member_list,
          ms_proposal_id);
    }
  }
  CLOG_LOG(INFO, "get_current_base_storage_info", K(ret), K_(partition_key), K(base_storage_info));
  return ret;
}

int ObPartitionLogService::get_base_storage_info(
    common::ObBaseStorageInfo& base_storage_info, uint64_t& sw_last_replay_log_id)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    if (OB_FAIL(fill_base_storage_info(base_storage_info, sw_last_replay_log_id))) {
      CLOG_LOG(WARN, "fill base storage info failed", K_(partition_key), K(ret));
    }
  }
  return ret;
}

common::ObPartitionKey ObPartitionLogService::get_partition_key() const
{
  return partition_key_;
}

int ObPartitionLogService::get_saved_base_storage_info(common::ObBaseStorageInfo& base_storage_info) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_FAIL(base_storage_info.deep_copy(saved_base_storage_info_))) {
    CLOG_LOG(WARN, "base_storage_info deep_copy failed", K_(partition_key), K(ret), K(saved_base_storage_info_));
  }
  CLOG_LOG(INFO, "get_saved_base_storage_info", K_(partition_key), K(ret), K(base_storage_info));
  return ret;
}

int ObPartitionLogService::change_leader(const common::ObAddr& leader, ObTsWindows& changing_leader_windows)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (leader == self_) {
    CLOG_LOG(INFO, "leader is self, do not need to change", K_(partition_key), K(leader), K_(self));
  } else {
    WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
    if (OB_FAIL(ret)) {
      CLOG_LOG(WARN, "change leader wrlock failed", K(ret), K_(partition_key), K(leader));
    } else if (!mm_.get_curr_member_list().contains(leader)) {
      ret = OB_ELECTION_WARN_NOT_CANDIDATE;
    } else if (!state_mgr_.can_change_leader()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "can not change leader",
          K(ret),
          K_(partition_key),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          "membership_mgr is_state_init",
          mm_.is_state_init(),
          "is_changing_leader",
          state_mgr_.is_changing_leader(),
          K_(self),
          "dst_server",
          leader);
    } else {
      ret = state_mgr_.change_leader_async(partition_key_, leader, changing_leader_windows);
    }
  }
  if (REACH_TIME_INTERVAL(10 * 1000)) {
    CLOG_LOG(INFO, "change leader async", K_(partition_key), K(ret), K_(self), K(leader));
  }
  return ret;
}

int ObPartitionLogService::change_restore_leader(const common::ObAddr& leader)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!leader.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K_(partition_key), K(leader));
  } else if (!restore_mgr_.is_archive_restoring() || RESTORE_LEADER != restore_mgr_.get_role()) {
    ret = OB_STATE_NOT_MATCH;
  } else if (OB_FAIL(restore_mgr_.change_restore_leader(leader))) {
    CLOG_LOG(WARN, "change_restore_leader failed", K(ret), K_(partition_key), K(leader));
  } else {
    CLOG_LOG(INFO, "change_restore_leader success", K(ret), K_(partition_key), K(leader));
  }
  return ret;
}

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
int ObPartitionLogService::report_physical_restore_unexpected_error_()
{
  int ret = OB_SUCCESS;
  int64_t job_id = 0;
  const uint64_t tenant_id = partition_key_.get_tenant_id();
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_FAIL(ObBackupInfoMgr::get_instance().get_restore_job_id(tenant_id, job_id))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      CLOG_LOG(WARN, "failed to get restore info", KR(ret), K(ret), K(tenant_id));
    } else {
      CLOG_LOG(INFO, "physical restore info not exist", K(tenant_id), K(ret));
    }
  } else if (OB_FAIL(ObRestoreFatalErrorReporter::get_instance().add_restore_error_task(
                 tenant_id, PHYSICAL_RESTORE_MOD_CLOG, OB_ERR_UNEXPECTED, job_id, MYADDR))) {
    CLOG_LOG(INFO, "failed to report restore error", KR(ret), K(tenant_id), KR(ret));
  } else {
  }
  return ret;
}

O
oceanbase-admin 已提交
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
int ObPartitionLogService::check_and_set_restore_progress()
{
  // responsible for switching state: RESTORE_LOG->NOT_RESTORE
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_SYS_TENANT_ID == partition_key_.get_tenant_id()) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "sys partitions do not need to check restore progress", K(ret), K(partition_key_));
  } else if (OB_ISNULL(partition_service_)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid partition_service_ is NULL", K(ret), K_(partition_key));
  } else if (restore_mgr_.is_archive_restoring_log()) {
    bool has_replayed = false;
    bool has_pending_task = false;
    // RESTORE_LOG->DUMP_MEMTABLE
    if (!restore_mgr_.has_finished_fetching_log()) {
      // do nothing
    } else if (OB_FAIL(mm_.check_follower_has_pending_entry(has_pending_task))) {
      CLOG_LOG(WARN, "failed to check_follower_has_pending_entry", K(ret), K_(partition_key));
    } else if (has_pending_task) {
    } else if (OB_FAIL(sw_.check_if_all_log_replayed(has_replayed))) {
      CLOG_LOG(WARN, "failed to check_if_all_log_replayed", K(ret), K_(partition_key));
    } else if (!has_replayed) {
    } else {
L
LIN 已提交
1210
      // check whether archived offline_log
O
oceanbase-admin 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
      if (!restore_mgr_.has_fetched_enough_log()) {
        uint64_t offline_log_id = OB_INVALID_ID;
        if (OB_FAIL(partition_service_->get_offline_log_id(partition_key_, offline_log_id))) {
          CLOG_LOG(WARN, "failed to get_offline_log_id ", K_(partition_key), KR(ret));
        } else if (offline_log_id != OB_INVALID_ID) {
          restore_mgr_.set_fetch_log_result(OB_ARCHIVE_FETCH_LOG_FINISH_AND_ENOUGH);
          CLOG_LOG(INFO, "partition has been dropped", K_(partition_key), K(offline_log_id));
        } else {
          ret = OB_ERR_UNEXPECTED;
          CLOG_LOG(ERROR, "log is not enough for restoring", K_(partition_key));
          SERVER_EVENT_ADD("clog_restore", "clog is not enough", "partition", partition_key_);
1222
          (void)report_physical_restore_unexpected_error_();
O
oceanbase-admin 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
        }
      }

      if (OB_SUCC(ret) && restore_mgr_.has_fetched_enough_log()) {
        if (OB_FAIL(set_restore_flag_after_restore_log_())) {
          CLOG_LOG(WARN, "failed to check_and_set_restore_flag_after_restore_log", KR(ret), K_(partition_key));
        }
      }
    }
  } else if (restore_mgr_.is_archive_restoring_mlist()) {
    // RESTORE_MEMBER_LIST->NOT_RESTORE
    int tmp_ret = OB_SUCCESS;
    uint64_t last_restore_log_id = OB_INVALID_ID;
D
dt0 已提交
1236
    int64_t last_restore_log_ts = OB_INVALID_TIMESTAMP;
O
oceanbase-admin 已提交
1237 1238 1239 1240 1241 1242
    int64_t unused_version = OB_INVALID_TIMESTAMP;
    const uint64_t last_slide_log_id = get_start_log_id_unsafe() - 1;
    const uint64_t ms_log_id = mm_.get_log_id();
    if (OB_SUCCESS != (tmp_ret = partition_service_->check_all_trans_in_trans_table_state(partition_key_)) &&
        (OB_ENTRY_NOT_EXIST != tmp_ret) && (OB_PARTITION_NOT_EXIST != tmp_ret)) {
      CLOG_LOG(WARN, "failed to wait_all_trans_clear", K_(partition_key), KR(tmp_ret));
1243 1244
    } else if (OB_SUCCESS != (tmp_ret = partition_service_->get_restore_replay_info(partition_key_,
            last_restore_log_id, last_restore_log_ts, unused_version))) {
O
oceanbase-admin 已提交
1245
      CLOG_LOG(WARN, "failed to get_restore_replay_info", K(tmp_ret), K_(partition_key), K(last_restore_log_id));
D
dt0 已提交
1246
    } else if (OB_INVALID_ID == last_restore_log_id || OB_INVALID_TIMESTAMP == last_restore_log_ts) {
1247
      CLOG_LOG(WARN, "unexpected last_restore_log_info", K_(partition_key), K(last_restore_log_id), K(last_restore_log_ts));
O
oceanbase-admin 已提交
1248 1249 1250 1251 1252 1253 1254 1255
    } else if (last_slide_log_id <= last_restore_log_id) {
      CLOG_LOG(INFO,
          "need wait start_working log slide out",
          K_(partition_key),
          K(last_slide_log_id),
          K(last_restore_log_id));
    } else if (ms_log_id > last_slide_log_id) {
      CLOG_LOG(INFO, "need wait membership_log_id slide out", K_(partition_key), K(last_slide_log_id), K(ms_log_id));
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
    } else if (OB_FAIL(restore_mgr_.check_last_restore_id_majority_equal(mm_.get_curr_member_list()))) {
      // all replicas need check majority's last_restore_log_id equal before update state to NOT_RESTORE.
      CLOG_LOG(INFO,
          "check_last_restore_id_majority_equal failed, need retry",
          K_(partition_key),
          K(last_restore_log_id),
          K(ret));
      if (OB_EAGAIN == ret) {
        // rewrite ret to SUCCESS
        ret = OB_SUCCESS;
      }
O
oceanbase-admin 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
    } else {
      ObReplicaRestoreStatus flag = ObReplicaRestoreStatus::REPLICA_NOT_RESTORE;
      if (OB_FAIL(partition_service_->set_restore_flag(partition_key_, flag))) {
        CLOG_LOG(WARN, "failed to set_restore_flag", K_(partition_key), K(flag), KR(ret));
      } else {
        CLOG_LOG(INFO, "success to set_restore_flag", K_(partition_key), K(flag), KR(ret));
      }
    }
  } else { /*do nothing*/
  }
  return ret;
}

int ObPartitionLogService::set_restore_fetch_log_finished(ObArchiveFetchLogResult fetch_log_result)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_FAIL(restore_mgr_.set_fetch_log_result(fetch_log_result))) {
    CLOG_LOG(WARN, "failed to set_fetch_log_result", K_(partition_key), K(fetch_log_result), KR(ret));
  } else { /*do nothing*/
  }
  return ret;
}

int ObPartitionLogService::process_restore_takeover_msg(const int64_t send_ts)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!restore_mgr_.is_archive_restoring()) {
    ret = OB_STATE_NOT_MATCH;
  } else if (ObTimeUtility::current_time() - send_ts > CASCAD_MSG_DELAY_TIME_THRESHOLD) {
    CLOG_LOG(WARN, "msg delay too much time, ignore", K_(partition_key), K(send_ts));
  } else if (OB_FAIL(restore_mgr_.leader_takeover())) {
    CLOG_LOG(WARN, "leader_takeover failed", K(ret), K_(partition_key));
  } else {
  }
  return ret;
}

int ObPartitionLogService::get_leader(common::ObAddr& leader) const
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    leader = state_mgr_.get_leader();
  }
  // CLOG_LOG(INFO, "get leader", K(ret), K_(partition_key), K(leader));
  return ret;
}

H
hf0 已提交
1322
int ObPartitionLogService::get_clog_parent_for_migration(common::ObAddr &parent, int64_t &cluster_id) const
O
oceanbase-admin 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  parent.reset();
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (restore_mgr_.is_archive_restoring()) {
    parent = restore_mgr_.get_restore_leader();
    cluster_id = state_mgr_.get_self_cluster_id();
  } else {
H
hf0 已提交
1333
    const bool is_paxos_replica = ObReplicaTypeCheck::is_paxos_replica(mm_.get_replica_type());
O
oceanbase-admin 已提交
1334
    const share::ObCascadMember parent_member = cascading_mgr_.get_parent();
H
hf0 已提交
1335 1336 1337
    const share::ObCascadMember prev_parent_member = cascading_mgr_.get_prev_parent();
    share::ObCascadMember cascad_leader;
    (void)state_mgr_.get_cascad_leader(cascad_leader);
O
oceanbase-admin 已提交
1338 1339 1340
    if (parent_member.is_valid()) {
      parent = parent_member.get_server();
      cluster_id = parent_member.get_cluster_id();
H
hf0 已提交
1341 1342 1343 1344 1345
    } else if (!is_paxos_replica && prev_parent_member.is_valid()) {
      // prev_parent is preferred choice for non-paxos replica
      parent = prev_parent_member.get_server();
      cluster_id = prev_parent_member.get_cluster_id();
    } else if (cascad_leader.is_valid()) {
O
oceanbase-admin 已提交
1346 1347 1348
      // returns corresponding leader when parent is invalid
      parent = cascad_leader.get_server();
      cluster_id = cascad_leader.get_cluster_id();
H
hf0 已提交
1349 1350 1351 1352 1353 1354
    } else if (prev_parent_member.is_valid()) {
      // at last return prev_parent
      parent = prev_parent_member.get_server();
      cluster_id = prev_parent_member.get_cluster_id();
    } else {
      // it does not have valid parent
O
oceanbase-admin 已提交
1355 1356 1357 1358 1359
    }
  }
  if (!parent.is_valid()) {
    ret = OB_NEED_RETRY;
  }
H
hf0 已提交
1360
  CLOG_LOG(INFO, "get clog parent", K(ret), K_(partition_key), K(parent), K(cluster_id));
O
oceanbase-admin 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
  return ret;
}

int ObPartitionLogService::get_leader_curr_member_list(common::ObMemberList& member_list) const
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!state_mgr_.can_get_leader_curr_member_list()) {
    CLOG_LOG(WARN, "it is not leader", K_(partition_key));
    ret = OB_STATE_NOT_MATCH;
  } else {
    ret = member_list.deep_copy(mm_.get_curr_member_list());
  }
  return ret;
}

int ObPartitionLogService::get_curr_member_list(common::ObMemberList& member_list) const
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    ret = member_list.deep_copy(mm_.get_curr_member_list());
  }
  return ret;
}

int ObPartitionLogService::get_curr_member_list_for_report(common::ObMemberList& member_list) const
{
  // used for reporting meta table
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (state_mgr_.has_valid_member_list()) {
    ret = member_list.deep_copy(mm_.get_curr_member_list());
    CLOG_LOG(DEBUG, "get_curr_member_list_for_report", K_(partition_key), K(member_list));
  } else {
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(INFO, "get_curr_member_list_for_report return empty", K_(partition_key));
    }
  }
  return ret;
}

int ObPartitionLogService::try_get_curr_member_list(common::ObMemberList& member_list) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (lock_.try_rdlock()) {
    ret = member_list.deep_copy(mm_.get_curr_member_list());
    lock_.rdunlock();
  } else {
    ret = OB_EAGAIN;
  }
  return ret;
}

int ObPartitionLogService::get_replica_num(int64_t& replica_num) const
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    replica_num = mm_.get_replica_num();
  }
  return ret;
}

int ObPartitionLogService::try_get_replica_num(int64_t& replica_num) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (lock_.try_rdlock()) {
    replica_num = mm_.get_replica_num();
    lock_.rdunlock();
  } else {
    ret = OB_EAGAIN;
  }
  return ret;
}

int ObPartitionLogService::receive_log(const ObLogEntry& log_entry, const ObAddr& server, const int64_t cluster_id,
    const ObProposalID& proposal_id, const ObPushLogMode push_mode, const ReceiveLogType type)
{
  int ret = OB_SUCCESS;
  const uint64_t log_id = log_entry.get_header().get_log_id();
  const ObLogType log_type = log_entry.get_header().get_log_type();
  uint32_t protection_level = 0;
  bool is_tenant_out_of_mem = false;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || (!proposal_id.is_valid() && !restore_mgr_.is_archive_restoring()) ||
             OB_ISNULL(log_engine_)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K_(partition_key), K(server), K(proposal_id));
  } else if (!log_entry.check_integrity() || log_entry.get_header().get_partition_key() != partition_key_) {
    ret = OB_INVALID_DATA;
    CLOG_LOG(ERROR, "receive_log invalid log", K_(partition_key), K(ret), K(log_entry), K(server));
  } else if (OB_FAIL(replay_engine_->is_tenant_out_of_memory(partition_key_, is_tenant_out_of_mem))) {
    CLOG_LOG(WARN, "is_tenant_out_of_memory failed", K(ret), K_(partition_key));
  } else if (is_tenant_out_of_mem) {
    ret = OB_TENANT_OUT_OF_MEM;
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(WARN, "tenant is out of memory, cannot receive log", K(ret), K_(partition_key));
    }
  } else if (OB_LOG_RENEW_MEMBERSHIP == log_type) {
    // it should not receive renew_ms_log here
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR,
        "receive_log encounter renew_ms_log, unexpected",
        K_(partition_key),
        K(ret),
        K(log_entry),
        K(server),
        K(proposal_id),
        K(type));
  } else if (proposal_id > state_mgr_.get_proposal_id()) {
    WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
    if (OB_FAIL(ret)) {
      CLOG_LOG(WARN,
          "receive log wrlock failed",
          K(ret),
          K_(partition_key),
          K(log_entry),
          K(server),
          K(proposal_id),
          K(type));
    } else if (!restore_mgr_.is_archive_restoring()) {
      if (state_mgr_.can_handle_prepare_rqst(proposal_id, cluster_id)) {
        ret = state_mgr_.handle_prepare_rqst(proposal_id, server, cluster_id);
      }
    } else {
      // update proposal_id directly during physical restoring
      if (OB_FAIL(state_mgr_.update_proposal_id(proposal_id))) {
        CLOG_LOG(WARN, "update_proposal_id failed", K_(partition_key), K(ret), K(log_entry));
      }
    }
  } else {
    // do nothing
  }

  if (OB_SUCC(ret)) {
    RLockGuard guard(lock_);
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
    // check src server
    const common::ObAddr parent = cascading_mgr_.get_parent_addr();
    bool is_valid_src = false;
    if (!parent.is_valid() || server == parent || cascading_mgr_.is_in_reregister_period()) {
      // When my parent is invalid(may be migrating dest) or src is my parent or
      // I'm in reregister period, I can receive log.
      is_valid_src = true;
    }

    if (!is_valid_src) {
      // sender is not valid src, cannot receive log
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "sender is an unexpected src, cannot receive log",
          K_(partition_key),
          K(ret),
          K(server),
          K(proposal_id),
          K(cluster_id),
          K(type),
          K(parent),
          "self_cluster_id",
          state_mgr_.get_self_cluster_id());
    } else if (cluster_id != state_mgr_.get_self_cluster_id() && STANDBY_LEADER != state_mgr_.get_role()) {
O
oceanbase-admin 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
      // only STANDBY_LEADER can receive logs from different cluster
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "receive diff cluster log in unexpected role",
          K_(partition_key),
          K(ret),
          K(server),
          K(proposal_id),
          K(cluster_id),
          K(type),
          "self_cluster_id",
          state_mgr_.get_self_cluster_id());
    } else if (OB_FAIL(state_mgr_.get_standby_protection_level(protection_level))) {
      CLOG_LOG(WARN, "get_standby_protection_level failed", K_(partition_key), K(ret));
    } else if (PUSH_LOG_ASYNC == push_mode && STANDBY_LEADER == state_mgr_.get_role() &&
               (MAXIMUM_PROTECTION_LEVEL == protection_level || RESYNCHRONIZATION_LEVEL == protection_level)) {
      // standby_leader cannot receive ASYNC logs in max protection_mode to keep data consistent
      // this may occur when primary cluster switches its protection mode
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "receive log in unexpected protection_level, ignore",
          K_(partition_key),
          K(ret),
          K(log_entry),
          K(server),
          K(proposal_id),
          K(push_mode),
          K(type),
          K(protection_level));
    } else {
    }
  }

  if (OB_SUCC(ret)) {
    bool follower_can_receive_log = true;
    ObProposalID pid_in_log = log_entry.get_header().get_proposal_id();
    if (OB_LOG_MEMBERSHIP != log_entry.get_header().get_log_type() &&
        OB_LOG_START_MEMBERSHIP != log_entry.get_header().get_log_type()) {
      RLockGuard guard(lock_);
      if (!log_engine_->is_disk_space_enough()) {
        ret = OB_LOG_OUTOF_DISK_SPACE;
        if (REACH_TIME_INTERVAL(1000 * 1000)) {
          CLOG_LOG(WARN, "log outof disk space", K_(partition_key), K(server), K(ret));
        }
      } else if (!restore_mgr_.is_archive_restoring() &&
                 !state_mgr_.can_receive_log(proposal_id, pid_in_log, cluster_id)) {
        ret = OB_STATE_NOT_MATCH;
        CLOG_LOG(WARN,
            "can not receive_log",
            K(ret),
            K_(partition_key),
            K(server),
            "log_type",
            log_entry.get_header().get_log_type(),
            "log_id",
            log_entry.get_header().get_log_id(),
            "role",
            state_mgr_.get_role(),
            "state",
            state_mgr_.get_state());
      } else if (state_mgr_.get_role() == LEADER && state_mgr_.get_state() == RECONFIRM &&
                 try_update_freeze_version_(log_entry) &&
                 OB_SUCCESS != (ret = reconfirm_.receive_log(log_entry, server))) {
        CLOG_LOG(WARN, "reconfirm receive log failed", K_(partition_key), K(ret), K(server));
        reconfirm_.reset();
      } else if (!state_mgr_.is_standby_leader_can_receive_log_id(log_id)) {
        // standby_leader cannot receive log/confirmed_info beyond max_log_id when it is doing member change
        ret = OB_STATE_NOT_MATCH;
        CLOG_LOG(WARN,
            "standby_leader is changing member, can not receive log larger than max_log_id",
            K(ret),
            K_(partition_key),
            K(server),
            K(log_id),
            "max_log_id",
            sw_.get_max_log_id());
      } else if (LEADER != state_mgr_.get_role() &&
                 (follower_can_receive_log = sw_.check_can_receive_larger_log(log_id)) &&
                 try_update_freeze_version_(log_entry) &&
                 OB_SUCCESS != (ret = sw_.receive_log(log_entry, server, cluster_id, type))) {
        CLOG_LOG(WARN, "follower active receive log failed", K_(partition_key), K(ret), K(server), K(log_entry));
      } else {
      }
    } else {
      WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
      if (OB_FAIL(ret)) {
        CLOG_LOG(WARN,
            "receive log wrlock failed",
            K(ret),
            K_(partition_key),
            K(log_entry),
            K(server),
            K(proposal_id),
            K(type));
      } else if (!log_engine_->is_disk_space_enough()) {
        ret = OB_LOG_OUTOF_DISK_SPACE;
        if (REACH_TIME_INTERVAL(1000 * 1000)) {
          CLOG_LOG(WARN, "log outof disk space", K_(partition_key), K(server), K(ret));
        }
      } else if (!restore_mgr_.is_archive_restoring() &&
                 !state_mgr_.can_receive_log(proposal_id, pid_in_log, cluster_id)) {
        ret = OB_STATE_NOT_MATCH;
        CLOG_LOG(WARN,
            "can not receive_log",
            K(ret),
            K_(partition_key),
            "log_type",
            log_entry.get_header().get_log_type(),
            "role",
            state_mgr_.get_role(),
            "state",
            state_mgr_.get_state());
      } else if (state_mgr_.get_role() == LEADER && state_mgr_.get_state() == RECONFIRM &&
                 try_update_freeze_version_(log_entry) &&
                 OB_SUCCESS != (ret = reconfirm_.receive_log(log_entry, server))) {
        CLOG_LOG(WARN, "reconfirm receive log failed", K_(partition_key), K(ret), K(server));
        reconfirm_.reset();
      } else if (!state_mgr_.is_standby_leader_can_receive_log_id(log_id)) {
        // standby_leader cannot receive log/confirmed_info beyond max_log_id when it is doing member change
        ret = OB_STATE_NOT_MATCH;
        CLOG_LOG(WARN,
            "standby_leader is changing member, can not receive log larger than max_log_id",
            K(ret),
            K_(partition_key),
            K(server),
            K(log_id),
            "max_log_id",
            sw_.get_max_log_id());
      } else if (LEADER != state_mgr_.get_role() &&
                 (follower_can_receive_log = sw_.check_can_receive_larger_log(log_id)) &&
                 try_update_freeze_version_(log_entry) &&
                 OB_SUCCESS != (ret = mm_.receive_log(log_entry, server, cluster_id, type))) {
        CLOG_LOG(WARN, "follower active receive log failed", K_(partition_key), K(ret), K(server), K(log_entry));
      } else {
      }
    }
  }
  return ret;
}

// used for receiving renew_ms_log by standby replica
int ObPartitionLogService::receive_renew_ms_log(const ObLogEntry& log_entry, const ObAddr& server,
    const int64_t cluster_id, const ObProposalID& proposal_id, const ReceiveLogType type)
{
  int ret = OB_SUCCESS;
  const ObProposalID& header_proposal_id = log_entry.get_header().get_proposal_id();

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || (!proposal_id.is_valid() && !restore_mgr_.is_archive_restoring()) ||
             OB_ISNULL(log_engine_)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K_(partition_key), K(server), K(proposal_id));
  } else if (!log_entry.check_integrity() || log_entry.get_header().get_partition_key() != partition_key_) {
    ret = OB_INVALID_DATA;
    CLOG_LOG(ERROR, "receive_log invalid log", K_(partition_key), K(ret), K(log_entry), K(server));
  } else if (header_proposal_id <= mm_.get_ms_proposal_id()) {
    // no need update proposal_id
  } else {
    WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
    if (OB_FAIL(ret)) {
      CLOG_LOG(WARN,
          "receive log wrlock failed",
          K(ret),
          K_(partition_key),
          K(log_entry),
          K(server),
          K(proposal_id),
          K(header_proposal_id),
          K(type));
    } else if (!restore_mgr_.is_archive_restoring()) {
      if (state_mgr_.can_handle_standby_prepare_rqst(header_proposal_id, cluster_id)) {
        // The new leader is subject to the proposal_id, because parent may not be leader when it transfer log to child
        ret = state_mgr_.handle_standby_prepare_rqst(header_proposal_id, server, cluster_id);
      } else {
        // update proposal_id
        if (OB_FAIL(mm_.update_ms_proposal_id(header_proposal_id))) {
          CLOG_LOG(WARN, "mm_ update_ms_proposal_id failed", K_(partition_key), K(ret), K(log_entry));
        }
      }
    } else {
      // physical_restoring state, update proposal_id
      if (OB_FAIL(mm_.update_ms_proposal_id(header_proposal_id))) {
        CLOG_LOG(WARN, "mm_ update_ms_proposal_id failed", K_(partition_key), K(ret), K(log_entry));
      }
    }
  }

  if (OB_SUCC(ret)) {
    ObProposalID pid_in_log = log_entry.get_header().get_proposal_id();
    WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
    if (OB_FAIL(ret)) {
      CLOG_LOG(WARN,
          "receive log wrlock failed",
          K(ret),
          K_(partition_key),
          K(log_entry),
          K(server),
          K(proposal_id),
          K(type));
    } else if (!log_engine_->is_disk_space_enough()) {
      ret = OB_LOG_OUTOF_DISK_SPACE;
      if (REACH_TIME_INTERVAL(1000 * 1000)) {
        CLOG_LOG(WARN, "log outof disk space", K_(partition_key), K(server), K(ret));
      }
    } else if (!state_mgr_.can_receive_renew_ms_log(proposal_id, pid_in_log, cluster_id)) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "can not receive_log",
          K(ret),
          K_(partition_key),
          "log_type",
          log_entry.get_header().get_log_type(),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state());
    } else if (try_update_freeze_version_(log_entry) &&
               OB_SUCCESS != (ret = mm_.receive_log(log_entry, server, cluster_id, type))) {
      CLOG_LOG(WARN, "follower active receive log failed", K_(partition_key), K(ret), K(server), K(log_entry));
    } else {
    }
  }
  return ret;
}

1762 1763
int ObPartitionLogService::receive_archive_log(const ObLogEntry& log_entry, const bool is_accum_checksum_valid,
    const int64_t accum_checksum, const bool is_batch_committed)
O
oceanbase-admin 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
{
  int ret = OB_SUCCESS;
  const ObAddr fake_server = self_;
  const ReceiveLogType fake_type = PUSH_LOG;
  const uint64_t log_id = log_entry.get_header().get_log_id();
  const ObProposalID proposal_id = log_entry.get_header().get_proposal_id();
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!log_entry.check_integrity() || log_entry.get_header().get_partition_key() != partition_key_) {
    ret = OB_INVALID_DATA;
    CLOG_LOG(ERROR, "receive_log invalid log", K_(partition_key), K(ret), K(log_entry));
  } else if (!restore_mgr_.is_archive_restoring() || state_mgr_.is_offline() || !state_mgr_.is_follower_active() ||
             RESTORE_LEADER != restore_mgr_.get_role()) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(WARN,
        "receive_archive_log in unexpected state",
        K_(partition_key),
        K(ret),
L
LIN 已提交
1782
        "is_archive_restoring",
O
oceanbase-admin 已提交
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
        restore_mgr_.is_archive_restoring(),
        "role",
        restore_mgr_.get_role(),
        "is_offline",
        state_mgr_.is_offline());
  } else if (proposal_id > state_mgr_.get_proposal_id()) {
    WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
    if (OB_FAIL(state_mgr_.update_proposal_id(proposal_id))) {
      CLOG_LOG(WARN, "update_proposal_id failed", K_(partition_key), K(ret), K(log_entry));
    }
  } else {
    // do nothing
  }
  if (OB_SUCC(ret)) {
    const int64_t self_cluster_id = state_mgr_.get_self_cluster_id();
    if (OB_LOG_MEMBERSHIP != log_entry.get_header().get_log_type() &&
        OB_LOG_START_MEMBERSHIP != log_entry.get_header().get_log_type()) {
      RLockGuard guard(lock_);
      if (!log_engine_->is_disk_space_enough()) {
        ret = OB_LOG_OUTOF_DISK_SPACE;
        if (REACH_TIME_INTERVAL(1000 * 1000)) {
          CLOG_LOG(WARN, "log outof disk space", K_(partition_key), K(ret));
        }
      } else if (state_mgr_.get_role() == FOLLOWER && sw_.check_can_receive_larger_log(log_id)) {
        if (try_update_freeze_version_(log_entry) &&
            OB_FAIL(sw_.receive_log(log_entry, fake_server, self_cluster_id, fake_type))) {
          CLOG_LOG(WARN, "follower active receive log failed", K_(partition_key), K(ret), K(log_entry));
1810 1811 1812 1813
        } else if (is_accum_checksum_valid
                    && OB_FAIL(sw_.set_log_archive_accum_checksum(log_id, accum_checksum))) {
          CLOG_LOG(WARN, "sw_ set_log_archive_accum_checksum failed", K(ret), K_(partition_key),
              K(is_accum_checksum_valid), K(accum_checksum), K(log_entry));
O
oceanbase-admin 已提交
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
        } else if (OB_FAIL(sw_.set_log_confirmed(log_id, is_batch_committed))) {
          if (OB_ERROR_OUT_OF_RANGE != ret) {
            CLOG_LOG(WARN, "sw_.set_log_confirmed failed", K_(partition_key), K(ret), K(log_entry));
          } else {
            // Its role may switch to LEADER during fetching log,
            // it may alredy received this log and slide it out, so we need avoid reporting error here
            ret = OB_SUCCESS;
          }
        } else {
          if (REACH_TIME_INTERVAL(1000 * 1000)) {
            CLOG_LOG(INFO, "receive_archive_log succ", K_(partition_key), K(ret), K(log_entry));
          }
        }
      } else {
        ret = OB_EAGAIN;
      }
    } else {
      WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
      if (OB_FAIL(ret)) {
        CLOG_LOG(WARN, "receive log wrlock failed", K(ret), K_(partition_key), K(log_entry));
      } else if (!log_engine_->is_disk_space_enough()) {
        ret = OB_LOG_OUTOF_DISK_SPACE;
        if (REACH_TIME_INTERVAL(1000 * 1000)) {
          CLOG_LOG(WARN, "log outof disk space", K_(partition_key), K(ret));
        }
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
      } else if (state_mgr_.get_role() == FOLLOWER
                 && sw_.check_can_receive_larger_log(log_id)) {
        if (try_update_freeze_version_(log_entry)
            && OB_FAIL(mm_.receive_log(log_entry, fake_server, self_cluster_id, fake_type))) {
          CLOG_LOG(WARN, "follower active receive log failed", K_(partition_key), K(ret),
                   K(log_entry));
        } else if (is_accum_checksum_valid
                    && OB_FAIL(sw_.set_log_archive_accum_checksum(log_id, accum_checksum))) {
          CLOG_LOG(WARN, "sw_ set_log_archive_accum_checksum failed", K(ret), K_(partition_key),
              K(is_accum_checksum_valid), K(accum_checksum), K(log_entry));
O
oceanbase-admin 已提交
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
        } else {
          int tmp_ret = OB_SUCCESS;
          if (OB_SUCCESS != (tmp_ret = sw_.set_log_confirmed(log_id, false))) {
            CLOG_LOG(WARN, "sw_.set_log_confirmed failed", K_(partition_key), K(tmp_ret), K(log_entry));
          }
          if (REACH_TIME_INTERVAL(1000 * 1000)) {
            CLOG_LOG(INFO, "receive_archive_log succ", K_(partition_key), K(ret), K(log_entry));
          }
        }
      } else {
        ret = OB_EAGAIN;
      }
    }
  }

  if (OB_ALLOCATE_MEMORY_FAILED == ret) {
    ret = OB_EAGAIN;
    if (REACH_TIME_INTERVAL(1 * 1000 * 1000L)) {
      CLOG_LOG(WARN, "failed because of lack of memory", K_(partition_key), K(ret), K(log_entry));
    }
  }
  return ret;
}

int ObPartitionLogService::process_archive_checkpoint(const uint64_t next_log_id, const int64_t next_log_ts)

{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_INVALID_ID == next_log_id || OB_INVALID_TIMESTAMP == next_log_ts) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K_(partition_key), K(next_log_id), K(next_log_ts));
  } else if (!restore_mgr_.is_archive_restoring() || state_mgr_.is_offline() || !state_mgr_.is_follower_active() ||
             RESTORE_LEADER != restore_mgr_.get_role()) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(WARN,
        "process_archive_checkpoint in unexpected state",
        K_(partition_key),
        K(ret),
        "is_archive_resotring",
        restore_mgr_.is_archive_restoring(),
        "role",
        restore_mgr_.get_role(),
        "is_offline",
        state_mgr_.is_offline());
  } else {
    uint64_t curr_leader_next_log_id = OB_INVALID_ID;
    int64_t curr_leader_next_log_ts = OB_INVALID_TIMESTAMP;
    sw_.get_next_replay_log_id_info(curr_leader_next_log_id, curr_leader_next_log_ts);
    if (next_log_id != curr_leader_next_log_id) {
      ret = OB_EAGAIN;
      if (REACH_TIME_INTERVAL(1 * 1000 * 1000)) {
        CLOG_LOG(WARN,
            "pre logs have not slide out",
            K(ret),
            K_(partition_key),
            K(next_log_id),
            K(next_log_ts),
            K(curr_leader_next_log_id));
      }
    } else if (OB_FAIL(sw_.follower_update_leader_next_log_info(next_log_id, next_log_ts))) {
      CLOG_LOG(WARN, "sw_.follower_update_leader_next_log_info failed", K(ret), K_(partition_key));
    } else {
      // do nothing
    }
  }

  return ret;
}

int ObPartitionLogService::get_restore_leader_info(bool& is_restore_leader, int64_t& leader_takeover_ts)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!restore_mgr_.is_archive_restoring()) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(WARN, "state not match", K(ret), K_(partition_key));
  } else {
    is_restore_leader = (RESTORE_LEADER == restore_mgr_.get_role());
    leader_takeover_ts = restore_mgr_.get_last_takeover_time();
  }
  return ret;
}

int ObPartitionLogService::fake_receive_log(
    const ObAddr& server, const uint64_t log_id, const ObProposalID& proposal_id)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || !proposal_id.is_valid() || OB_INVALID_ID == log_id) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K_(partition_key), K(server), K(proposal_id), K(log_id));
  } else {
    RLockGuard guard(lock_);
    if (proposal_id != state_mgr_.get_proposal_id()) {
      // proposal_id not match, skip
    } else if (state_mgr_.get_role() == FOLLOWER && ObReplicaTypeCheck::is_paxos_replica(mm_.get_replica_type()) &&
               state_mgr_.is_cluster_allow_vote()) {
      if (!log_engine_->is_disk_space_enough() || !sw_.check_can_receive_larger_log(log_id)) {
        // paxos replica need feedback to leader when it cannot receive log
        const ObProposalID self_proposal_id = state_mgr_.get_proposal_id();
        if (OB_FAIL(log_engine_->submit_fake_ack(server, partition_key_, log_id, self_proposal_id))) {
          CLOG_LOG(WARN, "notify_cannot_receive_log failed", K(ret), K_(partition_key), K(server), K(log_id));
        }
      } else {
        // follower need execute fetching immediately when it can receive log
        bool is_fetched = false;
        sw_.do_fetch_log(log_id, log_id + 1, POINT_FETCH_LOG_EXECUTE_TYPE, is_fetched);
      }
    }
  }
  return ret;
}

int ObPartitionLogService::ack_log(const ObAddr& server, const uint64_t log_id, const ObProposalID& proposal_id)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  bool is_in_curr_member_list = mm_.get_curr_member_list().contains(server);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!state_mgr_.can_receive_log_ack(proposal_id) || self_ == server) {
    if (partition_reach_time_interval(1000 * 1000, ack_log_time_)) {
      CLOG_LOG(WARN,
          "can not ack log",
          K_(partition_key),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          "current_proposal_id",
          state_mgr_.get_proposal_id(),
          K(server),
          K_(self),
          K(log_id),
          K(proposal_id));
    }
    ret = OB_STATE_NOT_MATCH;
  } else if (!is_in_curr_member_list) {
L
LIN 已提交
1994
    // if not paxos member, record ts and skip majority count
O
oceanbase-admin 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
  } else if (LEADER == state_mgr_.get_role() || STANDBY_LEADER == state_mgr_.get_role()) {
    // only leader need to count majority, other parent skip
    CLOG_LOG(DEBUG, "ack_log", K_(partition_key), K(server), K(log_id));
    bool majority = false;
    bool is_lease_valid = true;

    if (OB_SUCCESS != (ret = sw_.ack_log(log_id, server, majority))) {
      if (OB_CLOG_INVALID_ACK != ret) {
        CLOG_LOG(WARN, "sw ack_log failed", K_(partition_key), K(ret), K(log_id));
      }
    } else if (majority && is_lease_valid) {
      // this log has been majority
      if (state_mgr_.can_majority_cb(proposal_id)) {
        // LEADER executes majority_cb
        if (OB_SUCCESS != (ret = majority_cb_(log_id))) {
          CLOG_LOG(WARN, "majority cb failed", K_(partition_key), K(ret), K(log_id));
        }
      } else if (state_mgr_.can_standby_majority_cb(proposal_id)) {
        // STANDBY_LEADER executes standby_majority_cb
        share::ObCascadMember primary_leader = state_mgr_.get_primary_leader();
        if (!primary_leader.is_valid()) {
          // if current primary_leader is invalid, it can use prepared_primary_leader
          primary_leader = state_mgr_.get_prepared_primary_leader();
          if (primary_leader.is_valid() && primary_leader.get_server() != proposal_id.addr_) {
            CLOG_LOG(WARN,
                "prepared_primary_leader is not match with curr_proposal_id, ignore",
                K_(partition_key),
                K(ret),
                K(log_id),
                K(primary_leader),
                K(proposal_id));
            // reset it when it is not same with proposal_id
            primary_leader.reset();
          }
        }
        if (OB_FAIL(standby_majority_cb_(
                primary_leader.get_server(), primary_leader.get_cluster_id(), proposal_id, log_id))) {
          CLOG_LOG(WARN, "standby_majority_cb_ failed", K_(partition_key), K(ret), K(primary_leader), K(log_id));
        }
      } else {
        // do nothing
      }
    } else {
    }
  } else {
  }

  return ret;
}

int ObPartitionLogService::standby_ack_log(
    const ObAddr& server, const int64_t cluster_id, const uint64_t log_id, const ObProposalID& proposal_id)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (LEADER != state_mgr_.get_role()) {
    // self is not leader, ignore standby_ack
    // standby replica can be child of primary follower replica since 2.2.70
  } else if (!state_mgr_.can_receive_log_ack(proposal_id) || self_ == server) {
    ret = OB_STATE_NOT_MATCH;
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(WARN,
          "can not standby_ack_log",
          K(ret),
          K_(partition_key),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          "current_proposal_id",
          state_mgr_.get_proposal_id(),
          K(server),
          K_(self),
          K(log_id),
          K(proposal_id));
    }
  } else if (!GCTX.need_sync_to_standby() ||
             ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id())) {
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(INFO, "need ignore standby_ack", K_(partition_key), K(server), K(cluster_id), K(log_id));
    }
  } else {
    share::ObCascadMember sync_child = cascading_mgr_.get_sync_standby_child();
    if (sync_child.get_server() != server) {
      if (REACH_TIME_INTERVAL(100 * 1000)) {
        CLOG_LOG(WARN, "server is not curr sync_child, ignore", K_(partition_key), K(server), K(sync_child));
      }
    } else {
      bool majority = false;
      if (OB_SUCCESS != (ret = sw_.standby_ack_log(log_id, server, majority))) {
        if (OB_CLOG_INVALID_ACK != ret) {
          CLOG_LOG(WARN, "sw ack_log failed", K_(partition_key), K(ret), K(log_id));
        }
      } else if (majority && state_mgr_.can_majority_cb(proposal_id) && OB_SUCCESS != (ret = majority_cb_(log_id))) {
        CLOG_LOG(WARN, "majority cb failed", K_(partition_key), K(ret), K(log_id));
      } else {
      }
      CLOG_LOG(DEBUG, "standby_ack_log", K_(partition_key), K(server), K(cluster_id), K(log_id));
    }
  }

  return ret;
}

int ObPartitionLogService::fake_ack_log(const ObAddr& server, const uint64_t log_id, const ObProposalID& proposal_id)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  bool is_in_curr_member_list = mm_.get_curr_member_list().contains(server);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!state_mgr_.can_receive_log_ack(proposal_id) || self_ == server) {
    ret = OB_STATE_NOT_MATCH;
  } else if (LEADER != state_mgr_.get_role() || !is_in_curr_member_list) {
    // self is not leader or sender is not paxos memeber, ignore
  } else if (log_id != sw_.get_start_id()) {
    // log_id not match with current start_id, skip
  } else {
    const int64_t receive_ts = ObTimeUtility::current_time();
    if (OB_SUCCESS != (ret = sw_.fake_ack_log(log_id, server, receive_ts))) {
      CLOG_LOG(WARN, "sw fake_ack_log failed", K_(partition_key), K(ret), K(log_id));
    }
  }
  CLOG_LOG(DEBUG,
      "fake_ack_log finished",
      K(ret),
      K_(partition_key),
      K(server),
      K(proposal_id),
      K(log_id),
      "start_id",
      sw_.get_start_id());
  return ret;
}

int ObPartitionLogService::ack_renew_ms_log(
    const ObAddr& server, const uint64_t log_id, const int64_t submit_ts, const ObProposalID& ms_proposal_id)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  bool is_in_curr_member_list = mm_.get_curr_member_list().contains(server);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!state_mgr_.can_receive_renew_ms_log_ack(ms_proposal_id) || self_ == server) {
    if (partition_reach_time_interval(1000 * 1000, ack_log_time_)) {
      CLOG_LOG(WARN,
          "can not ack log",
          K_(partition_key),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          "curr_proposal_id",
          state_mgr_.get_proposal_id(),
          "curr_ms_proposal_id",
          mm_.get_ms_proposal_id(),
          K(server),
          K_(self),
          K(log_id),
          K(ms_proposal_id));
    }
    ret = OB_STATE_NOT_MATCH;
  } else if (!is_in_curr_member_list) {
    // if not paxos memeber, record ts and skip majority count
  } else {
    // only leader need to count majority, other parent skip
    bool majority = false;
    if (OB_SUCCESS != (ret = ms_task_mgr_.ack_renew_ms_log(log_id, submit_ts, ms_proposal_id, server, majority))) {
      if (OB_CLOG_INVALID_ACK != ret) {
        CLOG_LOG(WARN, "sw ack_log failed", K_(partition_key), K(ret), K(log_id), K(ms_proposal_id));
      }
    } else if (majority && OB_SUCCESS != (ret = renew_ms_log_majority_cb_(log_id, submit_ts, ms_proposal_id))) {
      CLOG_LOG(WARN, "majority cb failed", K_(partition_key), K(ret), K(log_id));
    } else {
    }
L
LIN 已提交
2174
    CLOG_LOG(INFO, "ack_renew_ms_log finished", K_(partition_key), K(server), K(log_id), K(ms_proposal_id), K(majority));
O
oceanbase-admin 已提交
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
  }

  return ret;
}

int ObPartitionLogService::resp_sw_info_when_get_log_(const ObAddr& server, const int64_t cluster_id)
{
  int ret = OB_SUCCESS;
  RLockGuard rdlock_guard(lock_);
  bool is_leader = (LEADER == state_mgr_.get_role() || STANDBY_LEADER == state_mgr_.get_role());
  if (is_leader || cascading_mgr_.is_valid_child(server)) {
    uint64_t next_log_id = OB_INVALID_ID;
    int64_t next_log_ts = OB_INVALID_TIMESTAMP;
    sw_.get_next_replay_log_id_info(next_log_id, next_log_ts);
    if (OB_FAIL(log_engine_->send_keepalive_msg(
            server, cluster_id, partition_key_, next_log_id, next_log_ts, 0 /* deliver_cnt */))) {
      CLOG_LOG(WARN, "send_keepalive_msg failed", K(ret), K_(partition_key), K(server));
    }
  }
  return ret;
}

bool ObPartitionLogService::is_primary_need_sync_to_standby_() const
{
  bool bool_ret = false;
  if (GCTX.is_primary_cluster() && GCTX.need_sync_to_standby() &&
      !ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id())) {
    bool_ret = true;
  }
  return bool_ret;
}

2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
int ObPartitionLogService::process_restore_check_req(
    const common::ObAddr& server, const int64_t cluster_id, const ObRestoreCheckType restore_type)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || OB_INVALID_CLUSTER_ID == cluster_id || OB_CHECK_UNKNOWN == restore_type) {
    ret = OB_INVALID_ARGUMENT;
  } else if (OB_CHECK_STANDBY_RESTORE == restore_type) {
    RLockGuard guard(lock_);
    if (restore_mgr_.is_standby_restore_state()) {
      if (REACH_TIME_INTERVAL(100 * 1000)) {
        CLOG_LOG(INFO, "self is still in restore state, ignore check restore msg", K_(partition_key), K(server));
      }
    } else if (STANDBY_LEADER == state_mgr_.get_role()) {
      // STANDBY_LEADER notify follower to exec restore
      bool is_in_member_list = mm_.get_curr_member_list().contains(server);
      const uint64_t fake_start_id = OB_INVALID_ID;
      if (OB_FAIL(log_engine_->notify_follower_log_missing(
              server, cluster_id, partition_key_, fake_start_id, is_in_member_list, OB_STANDBY_RESTORE_MSG))) {
        CLOG_LOG(WARN, "notify_follower_log_missing failed", K_(partition_key), K(server));
      } else {
        if (REACH_TIME_INTERVAL(100 * 1000)) {
          CLOG_LOG(INFO, "notify follower restore finished", K_(partition_key), K(server));
        }
      }
    } else {
      // other role, do nothing
    }
  } else if (OB_CHECK_RESTORE_END_ID == restore_type) {
    if (OB_FAIL(process_query_restore_end_id_req_(server, cluster_id))) {
      CLOG_LOG(WARN, "process_query_restore_end_id_req_ failed", K(ret), K_(partition_key), K(server));
    }
  } else {
  }
  return ret;
}

int ObPartitionLogService::process_query_restore_end_id_req_(const common::ObAddr& server, const int64_t cluster_id)
{
  int ret = OB_SUCCESS;
  uint64_t local_restore_end_id = OB_INVALID_ID;
  int64_t unused_log_ts = OB_INVALID_TIMESTAMP;
  int64_t unused_version = OB_INVALID_TIMESTAMP;

  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
2255 2256
  } else if (OB_FAIL(
                 partition_service_->get_restore_replay_info(partition_key_, local_restore_end_id, unused_log_ts, unused_version))) {
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
    CLOG_LOG(WARN, "failed to get_restore_replay_info", K(ret), K_(partition_key), K(local_restore_end_id));
  } else if (OB_INVALID_ID == local_restore_end_id) {
    CLOG_LOG(WARN, "local_restore_end_id is invalid, cannot response", K_(partition_key), K(local_restore_end_id));
  } else if (OB_FAIL(log_engine_->send_query_restore_end_id_resp(
                 server, cluster_id, partition_key_, local_restore_end_id))) {
    CLOG_LOG(
        WARN, "send_query_restore_end_id_resp failed", K(ret), K_(partition_key), K(server), K(local_restore_end_id));
  } else {
  }
  return ret;
}

int ObPartitionLogService::process_query_restore_end_id_resp(
    const common::ObAddr& server, const uint64_t last_restore_log_id)
{
  int ret = OB_SUCCESS;
  uint64_t local_restore_end_id = OB_INVALID_ID;
  int64_t unused_log_ts = OB_INVALID_TIMESTAMP;
  int64_t unused_version = OB_INVALID_TIMESTAMP;

  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || OB_INVALID_ID == last_restore_log_id) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K_(partition_key), K(server), K(last_restore_log_id));
  } else if (!restore_mgr_.is_archive_restoring_mlist()) {
    // self is not in restoring mlist state, ignore
  } else if (!mm_.get_curr_member_list().contains(server)) {
    CLOG_LOG(WARN, "server is not in member_list, ignore it", K_(partition_key), K(server), K(last_restore_log_id));
2287 2288
  } else if (OB_FAIL(
                 partition_service_->get_restore_replay_info(partition_key_, local_restore_end_id, unused_log_ts, unused_version))) {
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
    CLOG_LOG(WARN, "failed to get_restore_replay_info", K(ret), K_(partition_key), K(local_restore_end_id));
  } else if (last_restore_log_id != local_restore_end_id) {
    // last_restore_log_id is not match with local, unexpected error
    (void)report_physical_restore_unexpected_error_();
    SERVER_EVENT_ADD("clog_restore",
        "last_restore_log_id not match",
        "partition",
        partition_key_,
        "last_restore_log_id",
        local_restore_end_id);
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR,
        "last_restore_log_id is not match with local, unexpected",
        K(ret),
        K_(partition_key),
        K(local_restore_end_id),
        K(last_restore_log_id),
        K(server));
  } else if (OB_FAIL(restore_mgr_.process_restore_end_id_resp(server))) {
    CLOG_LOG(WARN, "process_restore_end_id_resp failed", K(ret), K_(partition_key), K(server), K(last_restore_log_id));
  } else {
  }
  return ret;
}

O
oceanbase-admin 已提交
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
int ObPartitionLogService::get_log(const common::ObAddr& server, const uint64_t start_log_id, const int64_t log_num,
    const ObFetchLogType fetch_type, const ObProposalID& proposal_id, const int64_t cluster_id,
    const common::ObReplicaType replica_type, const int64_t network_limit, const uint64_t max_confirmed_log_id)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_UNLIKELY(!server.is_valid()) || OB_UNLIKELY(start_log_id <= 0) || OB_UNLIKELY(log_num < 0) ||
             OB_FETCH_LOG_UNKNOWN == fetch_type || OB_ISNULL(log_engine_) || OB_ISNULL(fetch_log_engine_) ||
             OB_ISNULL(partition_service_) || network_limit < 0 || OB_INVALID_CLUSTER_ID == cluster_id) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN,
        "invalid arguments",
        K(ret),
        K_(partition_key),
        K(server),
        K(start_log_id),
        K(log_num),
        K(fetch_type),
        KP_(log_engine),
        KP_(fetch_log_engine),
        KP_(partition_service),
        K(network_limit),
        K(cluster_id));
  } else if (server == self_) {
    // ignore self_
  } else if (OB_FETCH_LOG_STANDBY_REPLICA == fetch_type && cluster_id == state_mgr_.get_self_cluster_id()) {
    CLOG_LOG(
        WARN, "receive same cluster_id standby fetch log req, unexpected", K_(partition_key), K(server), K(cluster_id));
  } else if (!state_mgr_.can_get_log(cluster_id)) {
    if (REACH_TIME_INTERVAL(100 * 1000)) {
      CLOG_LOG(WARN, "cannot get_log", K_(partition_key), K(server), K(cluster_id), K(fetch_type));
    }
  } else if (OB_FETCH_LOG_STANDBY_RESTORE == fetch_type) {
    RLockGuard guard(lock_);
    if (restore_mgr_.is_standby_restore_state()) {
      if (REACH_TIME_INTERVAL(100 * 1000)) {
        CLOG_LOG(INFO, "self is still in restore state, ignore check restore msg", K_(partition_key), K(server));
      }
    } else if (LEADER == state_mgr_.get_role()) {
      if (REACH_TIME_INTERVAL(100 * 1000)) {
        CLOG_LOG(INFO,
            "leader receive standby restore replica's fetch req, ignore",
            K_(partition_key),
            K(server),
            K(cluster_id));
      }
    } else if (STANDBY_LEADER == state_mgr_.get_role()) {
      bool is_in_member_list = mm_.get_curr_member_list().contains(server);
      if (OB_FAIL(log_engine_->notify_follower_log_missing(
              server, cluster_id, partition_key_, start_log_id, is_in_member_list, OB_STANDBY_RESTORE_MSG))) {
        CLOG_LOG(WARN, "notify_follower_log_missing failed", K_(partition_key), K(server));
      } else {
        if (REACH_TIME_INTERVAL(100 * 1000)) {
          CLOG_LOG(INFO, "notify follower restore finished", K_(partition_key), K(server));
        }
      }
    } else {
      // do nothing
    }
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
  } else if (OB_FETCH_LOG_LEADER_RECONFIRM == fetch_type &&
             (restore_mgr_.is_archive_restoring() || restore_mgr_.is_archive_restoring_mlist())) {
    // Restoring replica cannot response reconfirm fetch log req, because its last_restore_log_id maybe is
    // not match with current leader.
    // fix issue #35207594
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(INFO,
          "self is still in archive restore state, cannot response reconfirm fetch log req",
          K_(partition_key),
          K(server));
    }
O
oceanbase-admin 已提交
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
  } else {
    bool is_in_member_list = mm_.get_curr_member_list().contains(server);
    bool is_in_children_list = (cascading_mgr_.is_valid_child(server));
    if (OB_FETCH_LOG_FOLLOWER_ACTIVE == fetch_type || OB_FETCH_LOG_STANDBY_REPLICA == fetch_type) {
      RLockGuard guard(lock_);
      ret = cascading_mgr_.check_child_legal(server, cluster_id, replica_type);
    }
    // primary leader try set standby_ack according to standby_leader's max_confirmed_log_id
    // in leader reconfirm state
    if (OB_FETCH_LOG_STANDBY_REPLICA == fetch_type) {
      RLockGuard guard(lock_);
      ObCascadMember sync_child = cascading_mgr_.get_sync_standby_child();
      if (state_mgr_.is_leader_reconfirm() && is_primary_need_sync_to_standby_() &&
          OB_INVALID_ID != max_confirmed_log_id && server == sync_child.get_server() &&
          cluster_id == sync_child.get_cluster_id()) {
        int tmp_ret = OB_SUCCESS;
        uint64_t reconfirm_next_id = OB_INVALID_ID;
        if (OB_INVALID_ID == (reconfirm_next_id = reconfirm_.get_next_id())) {
          // reconfirm is initializing, skip
        } else if (OB_SUCCESS !=
                   (tmp_ret = sw_.process_sync_standby_max_confirmed_id(max_confirmed_log_id, reconfirm_next_id))) {
          CLOG_LOG(WARN,
              "process_sync_standby_max_confirmed_id failed",
              K(tmp_ret),
              K_(partition_key),
              K(server),
              K(cluster_id),
              K(max_confirmed_log_id),
              K(reconfirm_next_id));
        } else {
        }
      }
    }

    if (start_log_id > sw_.get_max_log_id() && OB_FETCH_LOG_LEADER_RECONFIRM != fetch_type) {
      // no log can be supplied for followers in this server, do nothing
      if (REACH_TIME_INTERVAL(5 * 1000 * 1000)) {
        CLOG_LOG(INFO,
            "wanted log are not supplied in this server",
            K(start_log_id),
            "sw_max_log_id",
            sw_.get_max_log_id(),
            K(server),
            K(fetch_type),
            K_(partition_key),
            K(is_in_member_list),
            K(is_in_children_list));
      }
    } else if (check_if_need_rebuild_(start_log_id, fetch_type)) {
      if (OB_FETCH_LOG_LEADER_RECONFIRM == fetch_type) {
        // new leader lagged behind too many, let the leader reconfirm fail
        // do nothing
        if (REACH_TIME_INTERVAL(2 * 1000 * 1000)) {
          CLOG_LOG(WARN,
              "new leader lagged behind too many",
              K_(partition_key),
              K(server),
              K(start_log_id),
              K(is_in_member_list),
              K(is_in_children_list));
        }
      } else {
        CLOG_LOG(INFO,
            "follower try to fetch too old log, need rebuild",
            K_(partition_key),
            K(server),
            K(start_log_id),
            K(is_in_member_list),
            K(is_in_children_list));
        (void)log_engine_->notify_follower_log_missing(
            server, cluster_id, partition_key_, start_log_id, is_in_member_list, OB_REBUILD_MSG);
      }
    } else if (log_num > 0) {
      uint64_t end_log_id = start_log_id + static_cast<uint64_t>(log_num);  // [start, end);
      const bool is_learner_fetching =
          (OB_FETCH_LOG_LEADER_RECONFIRM != fetch_type && !is_in_member_list && !is_in_children_list);
      const uint64_t end_limit_log_id =
          (is_learner_fetching ? (sw_.get_next_index_log_id()) : (sw_.get_max_log_id() + 1));
      if (OB_FETCH_LOG_LEADER_RECONFIRM != fetch_type) {
        end_log_id = MIN(end_log_id, end_limit_log_id);
      }
      uint64_t round_start_log_id = start_log_id;
      while (OB_SUCC(ret) && round_start_log_id < end_log_id) {
        uint64_t round_end_log_id = MIN(round_start_log_id + CLOG_ST_FETCH_LOG_COUNT, end_log_id);
        ObFetchLogTask* task = NULL;
        if (OB_UNLIKELY(NULL == (task = alloc_mgr_->alloc_fetch_log_task()))) {
          ret = OB_ALLOCATE_MEMORY_FAILED;
          CLOG_LOG(WARN, "alloc fetch_log_task failed", K(ret), K_(partition_key));
        } else if (OB_FAIL(task->init(partition_key_,
                       server,
                       cluster_id,
                       round_start_log_id,
                       round_end_log_id,
                       fetch_type,
                       proposal_id,
                       network_limit))) {
          CLOG_LOG(WARN,
              "init fetch log task error",
              K(ret),
              K_(partition_key),
              K(round_start_log_id),
              K(round_end_log_id),
              K(fetch_type),
              K(proposal_id));
        } else if (OB_FAIL(fetch_log_engine_->submit_fetch_log_task(task))) {
          CLOG_LOG(WARN, "submit fetch log task error", K(ret), K_(partition_key));
        } else {
          round_start_log_id = round_end_log_id;
          if (REACH_TIME_INTERVAL(50 * 1000)) {
            CLOG_LOG(INFO, "submit fetch log task succ", K_(partition_key), K(*task));
          }
        }
        if (OB_FAIL(ret) && NULL != task) {
          ob_slice_free_fetch_log_task(task);
          task = NULL;
        }
      }
    }
    if (!is_in_member_list && !is_in_children_list && cluster_version_before_2250_()) {
      (void)resp_sw_info_when_get_log_(server, cluster_id);
    }
    if (OB_FETCH_LOG_RESTORE_FOLLOWER == fetch_type) {
      int tmp_ret = OB_SUCCESS;
      if (start_log_id > sw_.get_max_log_id() && sw_.get_max_log_id() <= sw_.get_max_confirmed_log_id() &&
          restore_mgr_.is_restore_log_finished()) {
        if (OB_SUCCESS !=
            (tmp_ret = log_engine_->notify_restore_log_finished(server, cluster_id, partition_key_, start_log_id))) {
          CLOG_LOG(WARN, "notify_restore_log_finished failed", K(tmp_ret), K(partition_key_), K(server));
        } else {
          CLOG_LOG(DEBUG, "notify_restore_log_finished", K(partition_key_), K(server));
        }
      }
    }
  }
  return ret;
}

int ObPartitionLogService::async_get_log(const common::ObAddr& server, const int64_t cluster_id,
    const uint64_t start_log_id, const uint64_t end_log_id, const ObFetchLogType fetch_type,
    const common::ObProposalID& proposal_id, const int64_t network_limit)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    uint32_t log_attr = ObFetchLogAttr::NO_DEMAND;
    if (OB_FETCH_LOG_STANDBY_REPLICA == fetch_type) {
      // standby replica's fetch log request, need guarantees log has reach majority in local cluster
      log_attr = ObFetchLogAttr::NEED_MAJORITY;
    }

    bool need_send_confirm_info = false;
    if (OB_FETCH_LOG_FOLLOWER_ACTIVE == fetch_type || OB_FETCH_LOG_RESTORE_FOLLOWER == fetch_type ||
        OB_FETCH_LOG_STANDBY_REPLICA == fetch_type) {
      need_send_confirm_info = true;
    }

    switch (fetch_type) {
      case OB_FETCH_LOG_FOLLOWER_ACTIVE:
      case OB_FETCH_LOG_RESTORE_FOLLOWER:
      case OB_FETCH_LOG_STANDBY_REPLICA:
        ret = get_log_follower_active_(
            server, cluster_id, start_log_id, end_log_id, fetch_type, log_attr, need_send_confirm_info, network_limit);
        break;
      case OB_FETCH_LOG_LEADER_RECONFIRM:
        ret = get_log_leader_reconfirm_(
            server, start_log_id, end_log_id, log_attr, need_send_confirm_info, proposal_id, network_limit);
        break;
      default:
        CLOG_LOG(ERROR, "wrong fetch log type", K_(partition_key), K(fetch_type));
        ret = OB_ERR_UNEXPECTED;
        break;
    }
  }
  return ret;
}

int ObPartitionLogService::get_log_follower_active_(const common::ObAddr& server, const int64_t cluster_id,
    const uint64_t start_log_id, const uint64_t end_log_id, const ObFetchLogType& fetch_type, const uint32_t log_attr,
    const bool need_send_confirm_info, const int64_t network_limit)
{
  int ret = OB_SUCCESS;
  ObProposalID curr_proposal_id;
  bool can_get_log = false;

  do {
    RLockGuard guard(lock_);
    curr_proposal_id = state_mgr_.get_proposal_id();
    can_get_log = (state_mgr_.can_get_log(cluster_id) || restore_mgr_.is_archive_restoring() ||
                   cascading_mgr_.is_valid_child(server));
    //  need_log_confirmed = (state_mgr_.get_state() == RECONFIRM);
  } while (0);

  if (can_get_log) {
    ret = internal_fetch_log_(start_log_id,
        end_log_id,
        fetch_type,
        curr_proposal_id,
        server,
        cluster_id,
        log_attr,
        need_send_confirm_info,
        network_limit);
  }
  return ret;
}

int ObPartitionLogService::get_log_leader_reconfirm_(const common::ObAddr& server, const uint64_t start_log_id,
    const uint64_t end_log_id, const uint32_t log_attr, const bool need_send_confirm_info,
    const common::ObProposalID& proposal_id, const int64_t network_limit)
{
  int ret = OB_SUCCESS;
  ObProposalID curr_proposal_id;
  bool can_get_log = false;

  do {
    RLockGuard guard(lock_);
    curr_proposal_id = state_mgr_.get_proposal_id();
    can_get_log = state_mgr_.can_get_log_for_reconfirm(proposal_id);
  } while (0);

  if (can_get_log) {
    const int64_t cluster_id = obrpc::ObRpcNetHandler::CLUSTER_ID;
    ret = internal_fetch_log_(start_log_id,
        end_log_id,
        OB_FETCH_LOG_LEADER_RECONFIRM,
        curr_proposal_id,
        server,
        cluster_id,
        log_attr,
        need_send_confirm_info,
        network_limit);
  }

  return ret;
}

int ObPartitionLogService::internal_fetch_log_(const uint64_t start_log_id, const uint64_t end_log_id,
    const ObFetchLogType& fetch_type, const ObProposalID& proposal_id, const common::ObAddr& server,
    const int64_t cluster_id, const uint32_t log_attr, const bool need_send_confirm_info, const int64_t network_limit)
{
  int ret = OB_SUCCESS;
  ObLogCursorExt cursor_array[ObCursorArrayCache::CURSOR_ARRAY_SIZE];
  ObGetCursorResult result;
  result.csr_arr_ = cursor_array;
  result.arr_len_ = ObCursorArrayCache::CURSOR_ARRAY_SIZE;
  result.ret_len_ = 0;
  uint64_t cursor_start_log_id = OB_INVALID_ID;
  for (uint64_t log_id = start_log_id; OB_SUCC(ret) && log_id < end_log_id; log_id++) {
    if (log_id < sw_.get_start_id()) {
      if (OB_FAIL(fetch_log_from_ilog_storage_(log_id,
              fetch_type,
              proposal_id,
              server,
              cluster_id,
              need_send_confirm_info,
              network_limit,
              result,
              cursor_start_log_id))) {
        CLOG_LOG(WARN,
            "fetch_log_from_ilog_storage_ failed",
            K(ret),
            K(partition_key_),
            K(log_id),
            K(proposal_id),
            K(server),
            K(need_send_confirm_info));
      }
    } else {
      if (OB_FAIL(fetch_log_from_sw_(
              log_id, fetch_type, proposal_id, server, cluster_id, log_attr, need_send_confirm_info, network_limit)) &&
          OB_ERROR_OUT_OF_RANGE != ret) {
        CLOG_LOG(WARN,
            "fetch_log_from_sw_ failed",
            K(ret),
            K(partition_key_),
            K(log_id),
            K(proposal_id),
            K(server),
            K(need_send_confirm_info));
      } else if (OB_ERROR_OUT_OF_RANGE == ret) {
        if (OB_FAIL(fetch_log_from_ilog_storage_(log_id,
                fetch_type,
                proposal_id,
                server,
                cluster_id,
                need_send_confirm_info,
                network_limit,
                result,
                cursor_start_log_id))) {
          CLOG_LOG(WARN,
              "fetch_log_from_ilog_storage_ failed",
              K(ret),
              K(partition_key_),
              K(log_id),
              K(proposal_id),
              K(server),
              K(need_send_confirm_info));
        }
      } else {
        // do nothing
      }
    }
  }
  return ret;
}

int ObPartitionLogService::fetch_log_from_sw_(const uint64_t log_id, const ObFetchLogType& fetch_type,
    const ObProposalID& proposal_id, const common::ObAddr& server, const int64_t cluster_id, const uint32_t log_attr,
    const bool need_send_confirm_info, const int64_t network_limit)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (OB_INVALID_ID == log_id || OB_FETCH_LOG_UNKNOWN == fetch_type ||
             (!proposal_id.is_valid() && !restore_mgr_.is_archive_restoring()) || !server.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K(partition_key_), K(log_id), K(fetch_type), K(proposal_id), K(server));
  } else {
    ObLogCursor log_cursor;
    bool log_confirmed = false;
    int64_t accum_checksum = 0;
    bool batch_committed = false;

    if (OB_SUCCESS ==
        (ret = sw_.get_log(log_id, log_attr, log_confirmed, log_cursor, accum_checksum, batch_committed))) {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(ERROR, "get_log return value unexpected", K(ret), K(partition_key_), K(log_id));
    } else if (OB_LOG_MISSING == ret && log_cursor.is_valid()) {
      const bool need_send_confirm_info_tmp = (need_send_confirm_info && log_confirmed);
      ret = get_log_with_cursor_(server,
          cluster_id,
          fetch_type,
          proposal_id,
          need_send_confirm_info_tmp,
          log_confirmed,
          log_cursor,
          accum_checksum,
          batch_committed,
          network_limit);
    } else if (OB_LOG_MISSING == ret || OB_ERR_NULL_VALUE == ret) {
      if (REACH_TIME_INTERVAL(100 * 1000)) {
        CLOG_LOG(INFO,
            "getlog failed, log is not in sw",
            K(ret),
            K(partition_key_),
            K(log_id),
            K(fetch_type),
            K(proposal_id),
            K(server));
      }
      if (OB_FETCH_LOG_LEADER_RECONFIRM == fetch_type && OB_ERR_NULL_VALUE == ret) {
        ret = send_null_log_(server, cluster_id, log_id, proposal_id, network_limit);
      } else {
        // do nothing
        ret = OB_SUCCESS;
      }
    } else if (OB_EAGAIN == ret) {
      CLOG_LOG(INFO,
          "get log not confirmed or pinned",
          K(ret),
          K(partition_key_),
          K(log_id),
          K(fetch_type),
          K(proposal_id),
          K(server),
          K(log_attr),
          K(log_confirmed));
      ret = OB_SUCCESS;
    } else if (OB_ERROR_OUT_OF_RANGE == ret) {
      CLOG_LOG(INFO,
          "log is not in sw, error_out_of_range",
          K(ret),
          K(partition_key_),
          K(log_id),
          K(fetch_type),
          K(proposal_id),
          K(server));
    } else {
      CLOG_LOG(ERROR, "get log failed", K(ret), K(partition_key_), K(log_id), K(fetch_type), K(proposal_id), K(server));
    }
  }
  CLOG_LOG(TRACE,
      "fetch_log_from_sw_",
      K(ret),
      K(partition_key_),
      K(log_id),
      K(fetch_type),
      K(proposal_id),
      K(server),
      K(log_attr),
      K(need_send_confirm_info));
  return ret;
}

bool ObPartitionLogService::check_if_need_rebuild_(const uint64_t start_log_id, const ObFetchLogType fetch_type) const
{
  // caller guarantees (start_log_id <= max_log_id)
  bool need_rebuild = false;
  int ret = OB_SUCCESS;
  uint64_t last_replay_log_id = OB_INVALID_ID;
  file_id_t min_file_id = OB_INVALID_FILE_ID;
  file_id_t max_file_id = OB_INVALID_FILE_ID;
  ObLogCursorExt log_cursor_ext;
  const uint64_t start_id = sw_.get_start_id();
  UNUSED(fetch_type);

  if (OB_FAIL(get_storage_last_replay_log_id_(last_replay_log_id))) {
    CLOG_LOG(WARN, "get_storage_last_replay_log_id_ failed", K(ret), K(partition_key_));
  } else if (start_log_id >= start_id) {
    need_rebuild = false;
    CLOG_LOG(INFO,
        "wanted log_id is greater than start_id, no need rebuild",
        K(start_log_id),
        K(start_id),
        K_(partition_key));
  } else if (start_log_id > last_replay_log_id) {
    need_rebuild = false;
    CLOG_LOG(INFO,
        "wanted log_id is greater than last_replay_log_id, no need rebuild",
        K(start_log_id),
        K(last_replay_log_id),
        K(start_id),
        K_(partition_key));
#ifdef ERRSIM
  } else if (ObServerConfig::get_instance()._ob_enable_rebuild_on_purpose && start_log_id < last_replay_log_id) {
    need_rebuild = true;
    CLOG_LOG(INFO, "req server need rebuild", K(ret), K(start_log_id), K(last_replay_log_id));
#endif
  } else if (OB_FAIL(log_engine_->get_cursor(partition_key_, start_log_id, log_cursor_ext))) {
    if (OB_ERR_OUT_OF_UPPER_BOUND == ret && start_log_id < start_id) {
      // this log does not have ilog and log_id is smaller than sw->start_id, need rebuild
      need_rebuild = true;
      CLOG_LOG(INFO,
          "fetch log err out of upper bound, need rebuild",
          K(ret),
          K(need_rebuild),
          K(partition_key_),
          K(start_log_id),
          K(start_id));
    } else if (OB_CURSOR_NOT_EXIST == ret) {
      // log cursor does not exist, need rebuild
      need_rebuild = true;
      CLOG_LOG(INFO,
          "fetch log cursor not exist, need rebuild",
          K(ret),
          K(need_rebuild),
          K(partition_key_),
          K(start_log_id),
          K(start_id));
    } else {
      CLOG_LOG(WARN, "get_cursor failed", K(ret), K(need_rebuild), K(partition_key_), K(start_log_id), K(start_id));
    }
  } else {
    const int64_t data_lag_threshold = ObServerConfig::get_instance().rebuild_replica_data_lag_threshold;
    if (data_lag_threshold > 0) {
      const uint32_t CLOG_FILE_AHEAD = (uint32_t)(data_lag_threshold / CLOG_FILE_SIZE);
      // check if need rebuild when config value is set
      min_file_id = log_engine_->get_clog_min_file_id();
      max_file_id = log_engine_->get_clog_max_file_id();
      file_id_t start_file_id = log_cursor_ext.get_file_id();
      if (max_file_id < start_file_id) {
        CLOG_LOG(WARN, "invalid max_file_id", K(partition_key_), K(max_file_id));
      } else if (start_file_id < min_file_id) {
        need_rebuild = true;
        CLOG_LOG(INFO,
            "log file of start_log_id is already recycled, need rebuild",
            K(partition_key_),
            K(start_log_id),
            K(start_file_id),
            K(min_file_id));
      } else if (max_file_id - start_file_id < CLOG_FILE_AHEAD) {
        // not reach threshold
      } else {
        need_rebuild = true;
      }
    }
  }

#ifdef ERRSIM
  if (ObServerConfig::get_instance()._ob_enable_rebuild_on_purpose &&
      (start_log_id < sw_.get_start_id() || OB_FETCH_LOG_RESTORE_FOLLOWER == fetch_type)) {
    need_rebuild = true;
    CLOG_LOG(INFO, "req server need rebuild", K(ret), K(start_log_id));
  }
#endif
  return need_rebuild;
}

int ObPartitionLogService::fetch_log_from_ilog_storage_(const uint64_t log_id, const ObFetchLogType& fetch_type,
    const ObProposalID& proposal_id, const ObAddr& server, const int64_t cluster_id, const bool need_send_confirm_info,
    const int64_t network_limit, ObGetCursorResult& result, uint64_t& cursor_start_log_id)
{
  int ret = OB_SUCCESS;
  ObLogCursorExt log_cursor_ext;
  bool read_from_clog = true;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (OB_INVALID_ID == log_id || OB_FETCH_LOG_UNKNOWN == fetch_type ||
             (!proposal_id.is_valid() && !restore_mgr_.is_archive_restoring()) || !server.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN,
        "invalid arguments",
        K(ret),
        K(partition_key_),
        K(log_id),
        K(fetch_type),
        K(proposal_id),
        K(server),
        K(need_send_confirm_info));
  } else if (OB_FAIL(
                 log_engine_->get_cursor_batch(partition_key_, log_id, log_cursor_ext, result, cursor_start_log_id))) {
    CLOG_LOG(WARN,
        "log_engine get_cursor_batch failed",
        K(ret),
        K(partition_key_),
        K(log_id),
        K(fetch_type),
        K(proposal_id),
        K(server),
        K(need_send_confirm_info));
  } else {
    ObLogEntry log_entry;
    ObReadParam read_param;
    read_param.file_id_ = log_cursor_ext.get_file_id();
    read_param.offset_ = log_cursor_ext.get_offset();
    read_param.read_len_ = log_cursor_ext.get_size();
    const uint64_t acc_cksm = log_cursor_ext.get_accum_checksum();
    const bool batch_committed = log_cursor_ext.is_batch_committed();

    ObReadBufGuard guard(ObModIds::OB_LOG_DIRECT_READER_CACHE_ID);
    ObReadBuf& rbuf = guard.get_read_buf();
    if (OB_UNLIKELY(!rbuf.is_valid())) {
      ret = OB_ALLOCATE_MEMORY_FAILED;
      CLOG_LOG(WARN, "failed to alloc read_buf", K_(partition_key), K(log_id), K(read_param), K(ret));
    } else if (OB_FAIL(log_engine_->read_log_by_location(read_param, rbuf, log_entry))) {
      CLOG_LOG(WARN,
          "read log by cursor failed",
          K(ret),
          K(partition_key_),
          K(read_param),
          K(log_id),
          K(fetch_type),
          K(proposal_id),
          K(server),
          K(need_send_confirm_info));
      read_from_clog = false;
    } else if (OB_FETCH_LOG_STANDBY_REPLICA == fetch_type &&
               OB_FAIL(log_entry.update_header_proposal_id(proposal_id))) {
      // fix issue #29473286
      CLOG_LOG(WARN,
          "update_header_proposal_id failed",
          K(ret),
          K_(partition_key),
          K(log_id),
          K(fetch_type),
          K(proposal_id),
          K(log_entry));
    } else if (OB_FAIL(send_log_(server, cluster_id, log_entry, proposal_id, network_limit))) {
      CLOG_LOG(WARN,
          "send log to server failed",
          K(ret),
          K(partition_key_),
          K(log_id),
          K(fetch_type),
          K(proposal_id),
          K(server),
          K(need_send_confirm_info));
    } else if (need_send_confirm_info) {
      if (OB_FAIL(send_confirm_info_(server, cluster_id, log_entry, acc_cksm, batch_committed))) {
        CLOG_LOG(WARN,
            "send confirm info failed",
            K(ret),
            K(partition_key_),
            K(log_id),
            K(fetch_type),
            K(proposal_id),
            K(server),
            K(need_send_confirm_info));
      }
    }
  }

  if ((OB_CURSOR_NOT_EXIST == ret || !read_from_clog) && OB_FETCH_LOG_LEADER_RECONFIRM != fetch_type) {
    const bool is_in_member_list = mm_.get_curr_member_list().contains(server);
    CLOG_LOG(INFO, "this replica need rebuild", K(ret), K(server), K(partition_key_), K(log_id), K(read_from_clog));
    uint64_t last_replay_log_id = OB_INVALID_ID;
    if (OB_FAIL(get_storage_last_replay_log_id_(last_replay_log_id))) {
2977 2978 2979 2980 2981 2982 2983 2984 2985
      CLOG_LOG(WARN,
          "get_storage_last_replay_log_id_ failed",
          K(ret),
          K(partition_key_),
          K(log_id),
          K(fetch_type),
          K(proposal_id),
          K(server),
          K(need_send_confirm_info));
O
oceanbase-admin 已提交
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228
    } else if (log_id > last_replay_log_id) {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(ERROR,
          "log_id is larger than last_replay_log_id, unexpected",
          K(ret),
          K(partition_key_),
          K(log_id),
          K(last_replay_log_id));
    } else if (OB_FAIL(log_engine_->notify_follower_log_missing(
                   server, cluster_id, partition_key_, log_id, is_in_member_list, OB_REBUILD_MSG))) {
      CLOG_LOG(WARN,
          "notify follower log missing",
          K(ret),
          K(partition_key_),
          K(log_id),
          K(fetch_type),
          K(proposal_id),
          K(server),
          K(need_send_confirm_info),
          K(is_in_member_list));
    } else {
      ret = OB_LOG_NEED_REBUILD;
    }
  }
  CLOG_LOG(TRACE,
      "fetch_log_from_ilog_storage_",
      K(ret),
      K(partition_key_),
      K(log_id),
      K(fetch_type),
      K(proposal_id),
      K(server),
      K(need_send_confirm_info));
  return ret;
}

int ObPartitionLogService::get_log_with_cursor_(const common::ObAddr& server, const int64_t cluster_id,
    const ObFetchLogType& fetch_type, const common::ObProposalID& proposal_id, const bool need_send_confirm_info,
    const bool log_confirmed, const ObLogCursor& log_cursor, const int64_t accum_checksum, const bool batch_committed,
    const int64_t network_limit)
{
  int ret = OB_SUCCESS;
  ObLogEntry log_entry;
  ObReadParam read_param;
  read_param.file_id_ = log_cursor.file_id_;
  read_param.offset_ = log_cursor.offset_;
  read_param.read_len_ = log_cursor.size_;

  ObReadBufGuard guard(ObModIds::OB_LOG_DIRECT_READER_CACHE_ID);
  ObReadBuf& rbuf = guard.get_read_buf();
  if (OB_UNLIKELY(!rbuf.is_valid())) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    CLOG_LOG(WARN, "failed to alloc read_buf", K_(partition_key), K(log_cursor), K(ret));
  } else if (OB_FAIL(log_engine_->read_log_by_location(read_param, rbuf, log_entry))) {
    CLOG_LOG(WARN, "read_log_by_location failed", K_(partition_key), K(ret), K(log_cursor));
  } else if (log_entry.get_header().get_partition_key() != partition_key_) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(
        ERROR, "read_log_by_location wrong log", K_(partition_key), K(ret), K(log_entry), K(server), K(log_cursor));
  } else {
    bool is_reconfirm_allow_send = true;
    common::ObProposalID log_proposal_id = log_entry.get_header().get_proposal_id();
    if (!log_confirmed && state_mgr_.get_state() == RECONFIRM &&
        (reconfirm_.get_proposal_id() != proposal_id || log_proposal_id != proposal_id)) {
      is_reconfirm_allow_send = false;
    }
    if (!log_confirmed && !is_reconfirm_allow_send) {
      CLOG_LOG(WARN,
          "in reconfirm state, not allow send unconfirmed log with old proposal_id",
          K_(partition_key),
          K(server),
          "state_mgr proposal_id",
          proposal_id,
          "reconfirm proposal_id",
          reconfirm_.get_proposal_id(),
          K(log_proposal_id));
    } else if (OB_FETCH_LOG_STANDBY_REPLICA == fetch_type &&
               OB_FAIL(log_entry.update_header_proposal_id(proposal_id))) {
      // fix issue #29473286
      // Log header's proposal_id need be updated to current proposal_id when it is send by primary cluster to standby.
      CLOG_LOG(WARN,
          "update_header_proposal_id failed",
          K(ret),
          K_(partition_key),
          K(fetch_type),
          K(proposal_id),
          K(log_entry));
    } else if (OB_SUCCESS != (ret = send_log_(server, cluster_id, log_entry, proposal_id, network_limit))) {
      CLOG_LOG(WARN, "send_log failed", K_(partition_key), K(ret), K(server), K(proposal_id), K(log_cursor));
    } else if (need_send_confirm_info &&
               (OB_SUCCESS !=
                   (ret = send_confirm_info_(server, cluster_id, log_entry, accum_checksum, batch_committed)))) {
      CLOG_LOG(WARN, "send_confirm_info failed", K_(partition_key), K(ret), K(server), K(proposal_id), K(log_cursor));
    }
  }
  return ret;
}

// handle leader_prepare request
int ObPartitionLogService::get_max_log_id(
    const ObAddr& server, const int64_t cluster_id, const ObProposalID& proposal_id)
{
  int ret = OB_SUCCESS;
  WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
  if (OB_FAIL(ret)) {
    CLOG_LOG(WARN, "get_max_log_id wrlock failed", K(ret), K_(partition_key), K(server), K(proposal_id));
  } else if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || !proposal_id.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K_(partition_key), K(server), K(proposal_id));
  } else if (state_mgr_.can_handle_prepare_rqst(proposal_id, cluster_id)) {
    ret = state_mgr_.handle_prepare_rqst(proposal_id, server, cluster_id);
    CLOG_LOG(INFO, "handle_prepare_rqst finished", K(ret), K(partition_key_), K(server), K(cluster_id), K(proposal_id));
  }
  CLOG_LOG(INFO, "get_max_log_id finished", K(ret), K(partition_key_), K(server), K(proposal_id));
  return ret;
}

int ObPartitionLogService::receive_max_log_id(
    const ObAddr& server, const uint64_t log_id, const ObProposalID& proposal_id, const int64_t max_log_ts)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || !proposal_id.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
  } else if (!state_mgr_.can_receive_max_log_id(proposal_id)) {
    ret = OB_STATE_NOT_MATCH;
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(INFO, "stale prepare_resp, discard", K(ret), K_(partition_key), K(server), K(proposal_id));
    }
  } else {
    ret = reconfirm_.receive_max_log_id(server, log_id, max_log_ts);
  }
  CLOG_LOG(INFO, "receive_max_log_id", K_(partition_key), K(ret), K(server), K(log_id), K(proposal_id), K(max_log_ts));
  return ret;
}

int ObPartitionLogService::handle_standby_prepare_req(
    const ObAddr& server, const int64_t cluster_id, const ObProposalID& proposal_id)
{
  int ret = OB_SUCCESS;
  WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);

  if (OB_FAIL(ret)) {
    CLOG_LOG(WARN, "handle_standby_prepare_req wrlock failed", K(ret), K_(partition_key), K(server), K(proposal_id));
  } else if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || !proposal_id.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K_(partition_key), K(server), K(proposal_id));
  } else if (state_mgr_.can_handle_standby_prepare_rqst(proposal_id, cluster_id)) {
    ret = state_mgr_.handle_standby_prepare_rqst(proposal_id, server, cluster_id);
    CLOG_LOG(INFO,
        "handle_standby_prepare_rqst finished",
        K(ret),
        K(partition_key_),
        K(server),
        K(cluster_id),
        K(proposal_id));
  } else {
    // do nothing
  }

  CLOG_LOG(INFO, "handle_standby_prepare_req finished", K(ret), K(partition_key_), K(server), K(proposal_id));

  return ret;
}

int ObPartitionLogService::handle_standby_prepare_resp(const ObAddr& server, const ObProposalID& proposal_id,
    const uint64_t ms_log_id, const int64_t membership_version, const ObMemberList& member_list)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || !proposal_id.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K_(partition_key), K(server), K(proposal_id));
  } else if (!state_mgr_.can_handle_standby_prepare_resp(proposal_id)) {
    ret = OB_STATE_NOT_MATCH;
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(INFO, "stale standby_prepare_response, ignore", K(ret), K_(partition_key), K(server), K(proposal_id));
    }
  } else {
    ret = reconfirm_.handle_standby_prepare_resp(server, proposal_id, ms_log_id, membership_version, member_list);
  }
  CLOG_LOG(INFO,
      "handle_standby_prepare_resp finished",
      K_(partition_key),
      K(ret),
      K(server),
      K(proposal_id),
      K(ms_log_id),
      K(membership_version),
      K(member_list));
  return ret;
}

int ObPartitionLogService::handle_query_sync_start_id_req(
    const ObAddr& server, const int64_t cluster_id, const int64_t send_ts)
{
  int ret = OB_SUCCESS;
  const int64_t now = ObTimeUtility::current_time();
  int64_t sync_cluster_id = OB_INVALID_CLUSTER_ID;
  RLockGuard guard(lock_);

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || OB_INVALID_TIMESTAMP == send_ts) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K_(partition_key), K(server), K(cluster_id), K(send_ts));
  } else if (now - send_ts > CASCAD_MSG_DELAY_TIME_THRESHOLD) {
    CLOG_LOG(WARN, "msg delay too much time, ignore", K_(partition_key), K(send_ts), "delay", now - send_ts);
  } else if (state_mgr_.get_self_cluster_id() == cluster_id || !GCTX.need_sync_to_standby() ||
             ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id()) ||
             LEADER != state_mgr_.get_role()) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(WARN,
        "recv unexpected query sync_start_id req",
        K(ret),
        K_(partition_key),
        K(server),
        K(cluster_id),
        "role",
        state_mgr_.get_role(),
        "mode",
        GCTX.get_protection_mode());
  } else if (OB_FAIL(cascading_mgr_.get_sync_standby_cluster_id(sync_cluster_id))) {
    CLOG_LOG(WARN, "get_sync_standby_cluster_id failed", K(ret), K_(partition_key));
  } else if (sync_cluster_id != cluster_id) {
    CLOG_LOG(WARN,
        "arg server is not sync cluster, ignore",
        K(ret),
        K_(partition_key),
        K(sync_cluster_id),
        K(server),
        K(cluster_id));
  } else {
    ObCascadMember sync_child = cascading_mgr_.get_sync_standby_child();
    if (!sync_child.is_valid() || sync_child.get_server() != server) {
L
LIN 已提交
3229
      // update sync_standby_child
O
oceanbase-admin 已提交
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319
      (void)cascading_mgr_.update_sync_standby_child(server, cluster_id);
    }
    const uint64_t max_log_id = sw_.get_max_log_id();
    if (OB_FAIL(log_engine_->submit_sync_start_id_resp(server, cluster_id, partition_key_, send_ts, max_log_id))) {
      CLOG_LOG(
          WARN, "submit_sync_start_id_resp failed", K(ret), K_(partition_key), K(max_log_id), K(server), K(cluster_id));
    }
  }
  CLOG_LOG(INFO, "handle_query_sync_start_id_req finished", K_(partition_key), K(ret), K(server), K(cluster_id));

  return ret;
}

int ObPartitionLogService::handle_sync_start_id_resp(
    const ObAddr& server, const int64_t cluster_id, const int64_t original_send_ts, const uint64_t sync_start_id)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || OB_INVALID_TIMESTAMP == original_send_ts || OB_INVALID_ID == sync_start_id) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN,
        "invalid arguments",
        K(ret),
        K_(partition_key),
        K(server),
        K(cluster_id),
        K(original_send_ts),
        K(sync_start_id));
  } else if (OB_FAIL(state_mgr_.handle_sync_start_id_resp(server, cluster_id, original_send_ts, sync_start_id))) {
    CLOG_LOG(WARN,
        "handle_sync_start_id_resp failed",
        K_(partition_key),
        K(ret),
        K(server),
        K(original_send_ts),
        K(sync_start_id));
  } else {
  }

  return ret;
}

int ObPartitionLogService::standby_update_protection_level()
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (GCTX.is_primary_cluster() || ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id())) {
    // primary cluster or private tbl, skip
  } else if (OB_FAIL(state_mgr_.standby_update_protection_level())) {
    CLOG_LOG(INFO, "standby_update_protection_level failed", K_(partition_key), K(ret));
  } else {
  }

  return ret;
}

int ObPartitionLogService::get_standby_leader_protection_level(uint32_t& protection_level)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    int64_t unused_epoch = 0;
    ObTsWindows unused_windows;
    if (OB_FAIL(get_role_unlock(unused_epoch, unused_windows))) {  // check self is elected leader
      CLOG_LOG(WARN, "get_role failed or not master", K(ret), K(partition_key_));
    } else if (OB_FAIL(state_mgr_.get_standby_protection_level(protection_level))) {
      CLOG_LOG(WARN, "get_standby_protection_level failed", K_(partition_key), K(ret));
    } else {
      CLOG_LOG(DEBUG, "get_standby_protection_level success", K_(partition_key), K(protection_level));
    }
  }
  return ret;
}

int ObPartitionLogService::primary_process_protect_mode_switch()
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!GCTX.is_primary_cluster() ||
             ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id())) {
L
LIN 已提交
3320
    // skip primary or private replica
O
oceanbase-admin 已提交
3321 3322 3323 3324 3325
  } else {
    common::ObTimeGuard timeguard("primary_protect_mode_switch", 1000 * 1000);
    // need use wlock to protect role
    // Because it may be concurrent with standby_ack_log(), if sync_standby_child changes,
    // it will cause primary leader incorrectly advance the log_task state
3326
    WLockGuard guard(lock_);
O
oceanbase-admin 已提交
3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626
    timeguard.click();
    if (LEADER != state_mgr_.get_role()) {
      // skip
    } else if (OB_FAIL(cascading_mgr_.primary_process_protect_mode_switch())) {
      CLOG_LOG(WARN, "primary_process_protect_mode_switch failed", K_(partition_key), K(ret));
    } else if (OB_FAIL(state_mgr_.primary_process_protect_mode_switch())) {
      CLOG_LOG(WARN, "primary_process_protect_mode_switch failed", K_(partition_key), K(ret));
    } else {
      // do nothing
    }
  }
  CLOG_LOG(INFO, "primary_process_protect_mode_switch finished", K_(partition_key), K(ret));

  return ret;
}

int ObPartitionLogService::receive_confirmed_info(const common::ObAddr& server, const int64_t src_cluster_id,
    const uint64_t log_id, const ObConfirmedInfo& confirmed_info, const bool batch_committed)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (log_id <= 0) {
    ret = OB_INVALID_ARGUMENT;
  } else if (!log_engine_->is_disk_space_enough()) {
    ret = OB_LOG_OUTOF_DISK_SPACE;
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(WARN, "log outof disk space", K_(partition_key), K(server), K(ret));
    }
  } else if (!state_mgr_.can_receive_confirmed_info(src_cluster_id)) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(WARN,
        "can not receive_confirmed_info",
        K(ret),
        K_(partition_key),
        K(server),
        K(src_cluster_id),
        "role",
        state_mgr_.get_role(),
        "state",
        state_mgr_.get_state(),
        K(log_id),
        K(confirmed_info));
  } else if (!sw_.check_can_receive_larger_log(log_id)) {
    ret = OB_TOO_LARGE_LOG_ID;
  } else if (!state_mgr_.is_standby_leader_can_receive_log_id(log_id)) {
    // standby_leader is doing member change, cannot receive log/confirmed_info beyond max_log_id
  } else {
    ret = sw_.receive_confirmed_info(log_id, confirmed_info, batch_committed);
  }
  return ret;
}

int ObPartitionLogService::receive_renew_ms_log_confirmed_info(const common::ObAddr& server, const uint64_t log_id,
    const common::ObProposalID& ms_proposal_id, const ObConfirmedInfo& confirmed_info)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (log_id <= 0 || !ms_proposal_id.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
  } else if (!state_mgr_.can_receive_renew_ms_log_confirmed_info()) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(WARN,
        "can not receive_renew_ms_log_confirmed_info",
        K(ret),
        K_(partition_key),
        "role",
        state_mgr_.get_role(),
        "state",
        state_mgr_.get_state(),
        K(log_id),
        K(ms_proposal_id),
        K(confirmed_info));
  } else {
    ret = ms_task_mgr_.receive_renew_ms_log_confirmed_info(log_id, ms_proposal_id, confirmed_info);
  }

  CLOG_LOG(INFO,
      "receive_renew_ms_log_confirmed_info finished",
      K(ret),
      K_(partition_key),
      K(server),
      K(log_id),
      K(ms_proposal_id),
      K(confirmed_info));
  return ret;
}

int ObPartitionLogService::append_disk_log(
    const ObLogEntry& log, const ObLogCursor& log_cursor, const int64_t accum_checksum, const bool batch_committed)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!state_mgr_.can_append_disk_log()) {
    ret = OB_STATE_NOT_MATCH;
  } else {
    if ((OB_LOG_NOP == log.get_header().get_log_type()) ||
        (OB_LOG_START_MEMBERSHIP == log.get_header().get_log_type()) ||
        (OB_LOG_TRUNCATE == log.get_header().get_log_type()) ||
        (OB_LOG_RENEW_MEMBERSHIP == log.get_header().get_log_type()) ||
        (OB_LOG_SUBMIT == log.get_header().get_log_type()) || (OB_LOG_AGGRE == log.get_header().get_log_type()) ||
        (OB_LOG_MEMBERSHIP == log.get_header().get_log_type())) {
      uint64_t freeze_id = saved_base_storage_info_.get_last_replay_log_id();
      if (freeze_id != OB_INVALID_ID && log.get_header().get_log_id() <= freeze_id) {
        CLOG_LOG(DEBUG,
            "append disk log before freeze point, abort it",
            K_(partition_key),
            K(log.get_header()),
            K(freeze_id));
      } else if (OB_LOG_MEMBERSHIP != log.get_header().get_log_type() &&
                 OB_LOG_START_MEMBERSHIP != log.get_header().get_log_type() &&
                 OB_LOG_RENEW_MEMBERSHIP != log.get_header().get_log_type()) {
        // sw append_disk_log handles thread safety issues
        ret = sw_.append_disk_log(log, log_cursor, accum_checksum, batch_committed);
      } else {
        // ensure that the processing of multiple member changes are mutually exclusive
        WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
        ret = mm_.append_disk_log(log, log_cursor, accum_checksum, batch_committed);
      }
    } else if (OB_LOG_PREPARED == log.get_header().get_log_type()) {
      WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
      if (OB_SUCCESS != (tmp_ret = state_mgr_.update_proposal_id(log.get_header().get_proposal_id()))) {
        CLOG_LOG(INFO, "update_proposal_id failed", K_(partition_key), K(tmp_ret));
      }
    } else if (OB_LOG_STANDBY_PREPARED == log.get_header().get_log_type()) {
      WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
      if (OB_SUCCESS != (tmp_ret = mm_.update_ms_proposal_id(log.get_header().get_proposal_id()))) {
        CLOG_LOG(INFO, "update_ms_proposal_id failed", K_(partition_key), K(tmp_ret));
      }
    } else {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(ERROR, "unexpected log type", K(ret), "log_type", log.get_header().get_log_type());
    }

    if (OB_SUCC(ret) && log.get_header().get_log_id() < get_next_index_log_id()) {
      ATOMIC_INC(&scan_confirmed_log_cnt_);
    }
  }
  return ret;
}

int ObPartitionLogService::set_scan_disk_log_finished()
{
  int ret = OB_SUCCESS;
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    if (sw_.get_max_log_id() + 1 < sw_.get_next_index_log_id()) {
      CLOG_LOG(ERROR,
          "scan clog error",
          K_(partition_key),
          "start_id",
          sw_.get_start_id(),
          "max_log_id",
          sw_.get_max_log_id(),
          "next_index_log_id",
          sw_.get_next_index_log_id());
      ret = OB_ERR_UNEXPECTED;
    } else {
      ret = state_mgr_.set_scan_disk_log_finished();
      // record last_update_next_replay_log_id_info_ts_
      sw_.record_last_update_next_replay_log_id_info_ts();
      free_cursor_array_cache_();
    }
  }
  return ret;
}

int ObPartitionLogService::on_election_role_change()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_FAIL(add_role_change_task_())) {
    CLOG_LOG(WARN, "add_role_change_task_ failed", K(ret), K(partition_key_));
  } else {
    CLOG_LOG(TRACE, "add_role_change_task_ success", K(ret), K(partition_key_));
  }
  return ret;
}

int ObPartitionLogService::on_change_leader_retry(const common::ObAddr& server, ObTsWindows& changing_leader_windows)
{
  int ret = OB_SUCCESS;
  storage::ObIPartitionGroupGuard guard;
  storage::ObIPartitionGroup* partition = NULL;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (OB_FAIL(partition_service_->get_partition(partition_key_, guard)) ||
             NULL == (partition = guard.get_partition_group())) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(WARN, "this partition not existed", K(ret), K(partition_key_));
  } else if (OB_FAIL(partition->register_txs_change_leader(server, changing_leader_windows))) {
    CLOG_LOG(
        WARN, "register txs change leader failed", K(ret), K(partition_key_), K(server), K(changing_leader_windows));
  } else {
    CLOG_LOG(INFO,
        "partition on change leader fail and reregister windows",
        K(ret),
        K(partition_key_),
        K(server),
        K(changing_leader_windows));
  }

  return ret;
}

int ObPartitionLogService::switch_state()
{
  int ret = OB_SUCCESS;
  bool need_retry = false;
  ret = switch_state(need_retry);
  return ret;
}

int ObPartitionLogService::switch_state(bool& need_retry)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    bool state_changed = false;
    need_retry = false;
    do {
      RLockGuard guard(lock_);
      state_changed = state_mgr_.is_state_changed(need_retry);
    } while (0);
    if (state_changed) {
      WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
      if (OB_FAIL(ret)) {
        CLOG_LOG(WARN, "switch_state wrlock failed", K(ret), K_(partition_key));
      } else {
        ret = state_mgr_.switch_state(need_retry);
      }
    }
  }
  return ret;
}

int ObPartitionLogService::check_mc_and_sliding_window_state()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    check_state_();
  }
  return ret;
}

int ObPartitionLogService::check_state_()
{
  int ret = OB_SUCCESS;
  int mc_ret = OB_SUCCESS;
  int state_ret = OB_SUCCESS;
  const int64_t now = ObClockGenerator::getClock();
  if (now - last_check_state_time_ > CLOG_CHECK_STATE_INTERVAL) {
    bool state_changed = false;
    do {
      RLockGuard guard(lock_);
      state_changed = mm_.is_state_changed();
    } while (0);
    if (state_changed) {
      WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + now, ret);
      if (OB_FAIL(ret)) {
        CLOG_LOG(WARN, "check_sliding_window_state wrlock failed", K(ret), K_(partition_key));
      } else if (OB_SUCCESS != (mc_ret = mm_.switch_state())) {
        CLOG_LOG(WARN, "ObLogMembershipMgr switch state failed", K_(partition_key), K(mc_ret));
      } else {
      }
    }
    do {
      RLockGuard guard(lock_);
      state_changed = state_mgr_.check_sliding_window_state();
    } while (0);
    if (state_changed) {
      if (OB_SUCCESS != (state_ret = add_role_change_task_())) {
        CLOG_LOG(WARN, "add_state_change_event failed", K_(partition_key), K(state_ret));
      } else {
        CLOG_LOG(TRACE, "add_state_change_event succeed", K_(partition_key), K(state_ret));
      }
    }
    if (OB_SUCCESS != mc_ret || OB_SUCCESS != state_ret) {
      ret = OB_ERR_UNEXPECTED;
    }

    int tmp_ret = OB_SUCCESS;
    if (restore_mgr_.is_archive_restoring() && state_mgr_.is_follower_active()) {
      if (OB_SUCCESS != (tmp_ret = restore_mgr_.check_state())) {
        CLOG_LOG(WARN, "restore_mgr_.check_state failed", K_(partition_key), K(tmp_ret));
      }
    }
3627
    // update check time
O
oceanbase-admin 已提交
3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681
    last_check_state_time_ = now;
  }
  return ret;
}

int ObPartitionLogService::try_exec_standby_restore_(const share::ObCascadMember& src_member)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!src_member.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
  } else {
    if (ATOMIC_LOAD(&last_rebuild_time_) == OB_INVALID_TIMESTAMP ||
        ObTimeUtility::current_time() - ATOMIC_LOAD(&last_rebuild_time_) >= REBUILD_REPLICA_INTERVAL) {
      if (OB_FAIL(partition_service_->restore_standby_replica(
              partition_key_, src_member.get_server(), src_member.get_cluster_id()))) {
        CLOG_LOG(WARN, "restore_standby_replica failed", K(ret), K_(partition_key), K(src_member));
      } else {
        ATOMIC_STORE(&last_rebuild_time_, ObTimeUtility::current_time());
        CLOG_LOG(INFO, "restore_standby_replica success", K(ret), K_(partition_key), K(src_member));
      }
    }
  }
  return ret;
}

int ObPartitionLogService::check_cascading_state()
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    ret = cascading_mgr_.check_cascading_state();
  }
  return ret;
}

int ObPartitionLogService::flush_cb(const ObLogFlushCbArg& arg)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  ObAddr arg_leader = arg.leader_;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_ISNULL(log_engine_)) {
    ret = OB_INVALID_ARGUMENT;
  } else if (OB_LOG_PREPARED == arg.log_type_) {
    common::ObReplicaType replica_type = REPLICA_TYPE_MAX;
    ObProposalID proposal_id = arg.proposal_id_;
    uint64_t max_log_id = OB_INVALID_ID;
    int64_t max_log_ts = OB_INVALID_TIMESTAMP;
3682
    common::ObRole role = INVALID_ROLE;
O
oceanbase-admin 已提交
3683 3684 3685 3686
    do {
      RLockGuard guard(lock_);
      replica_type = mm_.get_replica_type();
      sw_.get_max_log_id_info(max_log_id, max_log_ts);
3687
      role = state_mgr_.get_role();
O
oceanbase-admin 已提交
3688 3689
    } while (0);

3690 3691
    if (arg_leader != self_ && arg_leader.is_valid() && FOLLOWER == role &&
        ObReplicaTypeCheck::is_paxos_replica_V2(replica_type)) {
O
oceanbase-admin 已提交
3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917
      tmp_ret = log_engine_->prepare_response(
          arg_leader, arg.cluster_id_, partition_key_, proposal_id, max_log_id, max_log_ts);
      CLOG_LOG(INFO,
          "update proposal id succ, submit prepare response",
          K_(partition_key),
          K(tmp_ret),
          K(arg_leader),
          K(proposal_id),
          K(replica_type),
          K(max_log_id),
          K(max_log_ts));
    } else {
      CLOG_LOG(INFO, "refuse to submit prepare response", K_(partition_key), K(arg_leader), K_(self), K(replica_type));
    }
  } else if (OB_LOG_STANDBY_PREPARED == arg.log_type_) {
    common::ObReplicaType replica_type = REPLICA_TYPE_MAX;
    ObProposalID proposal_id = arg.proposal_id_;
    do {
      RLockGuard guard(lock_);
      replica_type = mm_.get_replica_type();
    } while (0);

    if (arg_leader != self_ && arg_leader.is_valid() && ObReplicaTypeCheck::is_paxos_replica_V2(replica_type) &&
        state_mgr_.is_can_elect_standby_leader()) {
      // standby-active non-private table
      ObProposalID ms_proposal_id = mm_.get_ms_proposal_id();
      if (proposal_id == ms_proposal_id) {
        // reply response only when proposal_id and ms_proposal_id are same
        const uint64_t ms_log_id = mm_.get_log_id();
        const int64_t membership_version = mm_.get_timestamp();
        const ObMemberList member_list = mm_.get_curr_member_list();
        tmp_ret = log_engine_->standby_prepare_response(
            arg_leader, arg.cluster_id_, partition_key_, proposal_id, ms_log_id, membership_version, member_list);
        CLOG_LOG(INFO,
            "update proposal id succ, submit standby prepare response",
            K_(partition_key),
            K(tmp_ret),
            K(arg_leader),
            K(proposal_id),
            K(ms_proposal_id),
            K(membership_version),
            K(member_list));
      }
    } else {
      CLOG_LOG(INFO, "refuse to submit prepare response", K_(partition_key), K(arg_leader), K_(self), K(replica_type));
    }
  } else if (OB_LOG_RENEW_MEMBERSHIP == arg.log_type_) {
    bool majority = false;
    RLockGuard guard(lock_);
    const int64_t begin_time = ObClockGenerator::getClock();
    if (OB_SUCCESS != (ret = ms_task_mgr_.set_renew_ms_log_flushed_succ(
                           arg.log_id_, arg.submit_timestamp_, arg.proposal_id_, majority))) {
      CLOG_LOG(
          WARN, "set_renew_ms_log_flushed_succ failed", K_(partition_key), K(ret), K(arg.log_type_), K(arg.log_id_));
    } else if (majority && state_mgr_.can_majority_cb_for_renew_ms_log(arg.proposal_id_) &&
               (OB_SUCCESS !=
                   (ret = renew_ms_log_majority_cb_(arg.log_id_, arg.submit_timestamp_, arg.proposal_id_)))) {
      CLOG_LOG(WARN, "majority_cb failed", K_(partition_key), K(ret));
    } else if (state_mgr_.can_send_renew_ms_log_ack(arg.proposal_id_) && arg_leader.is_valid()) {
      if (OB_SUCCESS !=
          (tmp_ret = log_engine_->submit_renew_ms_log_ack(
               arg_leader, arg.cluster_id_, partition_key_, arg.log_id_, arg.submit_timestamp_, arg.proposal_id_))) {
        CLOG_LOG(WARN,
            "submit_renew_ms_log_ack failed",
            K(tmp_ret),
            K(arg_leader),
            K(arg.log_id_),
            K(arg.proposal_id_),
            K_(partition_key));
      } else {
        CLOG_LOG(INFO,
            "submit_renew_ms_log_ack success",
            K(tmp_ret),
            K(arg_leader),
            K(arg.log_id_),
            K(arg.proposal_id_),
            K_(partition_key));
      }
      const int64_t end_time = ObClockGenerator::getClock();
      if (end_time - begin_time >= CLOG_PERF_WARN_THRESHOLD) {
        CLOG_LOG(WARN,
            "flush_cb cost too much time",
            K(partition_key_),
            K(arg),
            K(end_time),
            K(begin_time),
            "cost_time",
            end_time - begin_time);
      }
    } else {
    }
  } else {
    bool majority = false;
    RLockGuard guard(lock_);
    const int64_t begin_time = ObClockGenerator::getClock();
    bool is_lease_valid = true;

    if (OB_SUCCESS != (ret = sw_.set_log_flushed_succ(
                           arg.log_id_, arg.proposal_id_, arg.log_cursor_, arg.after_consume_timestamp_, majority))) {
      CLOG_LOG(WARN, "sw set_log_flushed_succ failed", K_(partition_key), K(ret), K(arg.log_type_), K(arg.log_id_));
    } else if (majority && ((state_mgr_.can_majority_cb(arg.proposal_id_) && is_lease_valid)) &&
               (OB_SUCCESS != (ret = majority_cb_(arg.log_id_)))) {
      CLOG_LOG(WARN, "majority_cb failed", K_(partition_key), K(ret));
    } else if (majority && state_mgr_.can_standby_majority_cb(arg.proposal_id_) &&
               (OB_SUCCESS !=
                   (ret = standby_majority_cb_(arg_leader, arg.cluster_id_, arg.proposal_id_, arg.log_id_)))) {
      CLOG_LOG(WARN, "majority_cb failed", K_(partition_key), K(ret));
    } else if (state_mgr_.can_send_log_ack(arg.proposal_id_) && arg_leader.is_valid()) {
      if (OB_SUCCESS != (tmp_ret = log_engine_->submit_log_ack(
                             arg_leader, arg.cluster_id_, partition_key_, arg.log_id_, arg.proposal_id_))) {
        CLOG_LOG(WARN,
            "submit_log_ack failed",
            K(tmp_ret),
            K(arg_leader),
            K(arg.log_id_),
            K(arg.proposal_id_),
            K_(partition_key));
      }
    } else {
    }

    const int64_t end_time = ObClockGenerator::getClock();
    if (end_time - begin_time >= CLOG_PERF_WARN_THRESHOLD) {
      CLOG_LOG(WARN,
          "flush_cb cost too much time",
          K(partition_key_),
          K(arg),
          K(end_time),
          K(begin_time),
          "cost_time",
          end_time - begin_time);
    }
  }
  return ret;
}

int ObPartitionLogService::renew_ms_log_flush_cb(const ObMsInfoTask& arg)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  const int64_t begin_time = ObClockGenerator::getClock();
  ObAddr server = arg.get_server();
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_ISNULL(log_engine_)) {
    ret = OB_INVALID_ARGUMENT;
  } else {
    bool majority = false;
    RLockGuard guard(lock_);
    if (OB_FAIL(ms_task_mgr_.set_renew_ms_log_flushed_succ(
            arg.get_ms_log_id(), arg.get_mc_timestamp(), arg.get_ms_proposal_id(), majority))) {
      CLOG_LOG(WARN, "set_renew_ms_log_flushed_succ failed", K_(partition_key), K(ret), K(arg));
    } else if (majority && state_mgr_.can_majority_cb_for_renew_ms_log(arg.get_ms_proposal_id()) &&
               (OB_FAIL(
                   renew_ms_log_majority_cb_(arg.get_ms_log_id(), arg.get_mc_timestamp(), arg.get_ms_proposal_id())))) {
      CLOG_LOG(WARN, "majority_cb failed", K_(partition_key), K(ret));
    } else if (state_mgr_.can_send_renew_ms_log_ack(arg.get_ms_proposal_id()) && server.is_valid() && self_ != server) {
      if (OB_SUCCESS != (tmp_ret = log_engine_->submit_renew_ms_log_ack(server,
                             arg.get_cluster_id(),
                             partition_key_,
                             arg.get_ms_log_id(),
                             arg.get_mc_timestamp(),
                             arg.get_ms_proposal_id()))) {
        CLOG_LOG(WARN, "submit_renew_ms_log_ack failed", K(tmp_ret), K(server), K(arg), K_(partition_key));
      } else {
        CLOG_LOG(INFO, "submit_renew_ms_log_ack success", K(tmp_ret), K(server), K(arg), K_(partition_key));
      }
      const int64_t end_time = ObClockGenerator::getClock();
      if (end_time - begin_time >= CLOG_PERF_WARN_THRESHOLD) {
        CLOG_LOG(WARN,
            "renew_ms_log_flush_cb cost too much time",
            K(partition_key_),
            K(arg),
            "cost_time",
            end_time - begin_time);
      }
    } else {
    }
  }

  CLOG_LOG(INFO, "renew_ms_log_flush_cb finished", K(partition_key_), K(ret), K(arg));
  return ret;
}

int ObPartitionLogService::query_max_flushed_ilog_id(uint64_t& ret_max_ilog_id)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret));
  } else if (OB_FAIL(log_engine_->query_max_flushed_ilog_id(partition_key_, ret_max_ilog_id)) &&
             OB_PARTITION_NOT_EXIST != ret) {
    CLOG_LOG(ERROR, "query_max_flushed_ilog_id failed", K(ret), K(partition_key_));
  } else if (OB_PARTITION_NOT_EXIST == ret) {
    ret_max_ilog_id = 0;
    ret = OB_SUCCESS;
  }

  if (OB_SUCC(ret)) {
    ret_max_ilog_id = std::max(ret_max_ilog_id, saved_base_storage_info_.get_last_replay_log_id());
    ret_max_ilog_id = std::max(ret_max_ilog_id, ATOMIC_LOAD(&max_flushed_ilog_id_));
  }

  return ret;
}

// Because member change and switch leader are mutable ops, so this function can execute without lock
int ObPartitionLogService::on_get_election_priority(election::ObElectionPriority& priority)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_ISNULL(partition_service_)) {
    ret = OB_INVALID_ARGUMENT;
  } else {
    priority.reset();
    const uint64_t zone_priority = get_zone_priority();
    const bool is_need_rebuild = state_mgr_.is_need_rebuild();
    const bool is_partition_candidate = partition_service_->is_election_candidate(partition_key_);
    const bool is_leader_revoke_recently = state_mgr_.is_leader_revoke_recently();
    bool is_standby_restore_finished = true;
    if (GCTX.can_be_parent_cluster() && !ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id()) &&
        restore_mgr_.is_standby_restore_state()) {
L
LIN 已提交
3918
      // restoring replicas cannot be strong leader,
O
oceanbase-admin 已提交
3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932
      // because its last_submit_ts maybe invalid, which will lead to reconfirm failure
      is_standby_restore_finished = false;
    }
    bool is_candidate = partition_service_->is_tenant_active(partition_key_) &&
                        ObReplicaTypeCheck::is_can_elected_replica(mm_.get_replica_type()) &&
                        OBSERVER.get_gctx().rs_server_status_ == share::RSS_IS_WORKING && is_partition_candidate &&
                        is_standby_restore_finished;
#ifdef ERRSIM
    if (ObServerConfig::get_instance()._ob_enable_rebuild_on_purpose) {
      is_candidate = is_candidate && (!is_need_rebuild);
    }
#endif
    bool is_tenant_out_of_mem = is_tenant_out_of_memory_();
    bool is_data_disk_error = false;
3933 3934
    bool is_disk_space_enough = log_engine_->is_disk_space_enough();
    bool is_clog_disk_hang = log_engine_->is_clog_disk_hang();
3935
    const bool is_slog_disk_warning = SLOGGER.is_disk_warning();
O
oceanbase-admin 已提交
3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
    const ObReplicaProperty replica_property = mm_.get_replica_property();
    const uint64_t log_id = sw_.get_max_confirmed_log_id();
    if (OB_SUCCESS != (tmp_ret = ObIOManager::get_instance().is_disk_error(is_data_disk_error))) {
      CLOG_LOG(WARN, "is_data_disk_error failed", K(tmp_ret), K_(partition_key));
    }
    if (is_candidate_ != is_candidate) {
      is_candidate_ = is_candidate;
      CLOG_LOG(INFO,
          "whether is_candidate changed",
          K_(partition_key),
          K(is_candidate),
          K(partition_service_->is_tenant_active(partition_key_)),
          "replica_type",
          mm_.get_replica_type(),
          K(zone_priority),
          "rs_server_status",
          OBSERVER.get_gctx().rs_server_status_,
          "is_service_started",
          partition_service_->is_service_started(),
          K(is_partition_candidate));
    }
    if (OB_SUCCESS != (ret = priority.init(is_candidate, mm_.get_timestamp(), log_id, zone_priority))) {
      CLOG_LOG(WARN, "priority init error", K_(partition_key), K(ret));
    } else {
3960 3961 3962 3963 3964
      if (!is_disk_space_enough) {
        priority.set_system_disk_full();
      }
      if (is_clog_disk_hang) {
        priority.set_system_clog_disk_hang();
O
oceanbase-admin 已提交
3965 3966 3967 3968
      }
      if (is_data_disk_error) {
        priority.set_system_data_disk_error();
      }
3969 3970 3971
      if (is_slog_disk_warning) {
        priority.set_system_slog_disk_warning();
      }
O
oceanbase-admin 已提交
3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169
      if (is_tenant_out_of_mem) {
        priority.set_system_tenant_out_of_memory();
      }
      if (is_leader_revoke_recently) {
        priority.set_system_in_election_blacklist();
      }
      if (0 == replica_property.get_memstore_percent()) {
        priority.set_system_without_memstore();
      }
      if (!partition_service_->is_service_started()) {
        priority.set_system_service_not_started();
      }
      if (is_need_rebuild) {
        priority.set_system_need_rebuild();
      }
    }
  }
  return ret;
}

int ObPartitionLogService::majority_cb_(const uint64_t log_id)
{
  int ret = OB_SUCCESS;
  const bool batch_committed = false;
  const bool batch_first_participant = false;

  BG_MONITOR_GUARD_DEFAULT(1000 * 1000);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_SUCCESS != (ret = sw_.majority_cb(log_id, batch_committed, batch_first_participant))) {
    CLOG_LOG(WARN, "majority_cb failed", K_(partition_key), K(ret), K(log_id));
  } else if (OB_SUCCESS != (ret = sw_.set_log_confirmed(log_id, batch_committed))) {
    CLOG_LOG(WARN, "set_log_confirmed failed!", K_(partition_key), K(ret), K(log_id));
  } else {
  }
  return ret;
}

int ObPartitionLogService::standby_majority_cb_(const common::ObAddr& server, const int64_t cluster_id,
    const common::ObProposalID& proposal_id, const uint64_t log_id)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_INVALID_ID == log_id) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K(server), K(cluster_id), K(log_id));
  } else {
    // standby_leader send ack to primary parent
    int tmp_ret = OB_SUCCESS;
    if (server.is_valid() && OB_SUCCESS != (tmp_ret = log_engine_->submit_standby_log_ack(
                                                server, cluster_id, partition_key_, log_id, proposal_id))) {
      CLOG_LOG(WARN,
          "submit_standby_log_ack failed",
          K(tmp_ret),
          K(server),
          K(cluster_id),
          K(log_id),
          K(proposal_id),
          K_(partition_key));
    }
    const bool batch_committed = false;
    if (OB_SUCCESS != (tmp_ret = sw_.set_log_confirmed(log_id, batch_committed))) {
      if (OB_ERROR_OUT_OF_RANGE != tmp_ret) {
        // The standby leader has to wait for the confirmed info from primary to execute set_log_confirmed after
        // majority In a concurrent scenario, after this thread executes try_set_majority_finished, other threads may
        // execute set_log_confirmed, which triggers the log to slide out, so it is possible to encounter -4175 here
        ret = tmp_ret;
      }
      CLOG_LOG(WARN, "submit_confirm_log failed!", K_(partition_key), K(tmp_ret), K(ret), K(log_id));
    }
  }
  return ret;
}

int ObPartitionLogService::renew_ms_log_majority_cb_(
    const uint64_t log_id, const int64_t submit_timestamp, const common::ObProposalID& proposal_id)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_SUCCESS != (ret = ms_task_mgr_.renew_ms_log_majority_cb(log_id, submit_timestamp, proposal_id))) {
    CLOG_LOG(WARN, "majority_cb failed", K_(partition_key), K(ret), K(log_id), K(submit_timestamp));
  } else if (OB_SUCCESS != (ret = ms_task_mgr_.set_renew_ms_log_confirmed(log_id, submit_timestamp, proposal_id))) {
    CLOG_LOG(WARN, "set_renew_ms_log_confirmed failed", K_(partition_key), K(ret), K(log_id), K(submit_timestamp));
  } else {
    CLOG_LOG(INFO, "renew_ms_log_majority_cb_ success", K_(partition_key), K(ret), K(log_id), K(submit_timestamp));
  }
  return ret;
}

int ObPartitionLogService::stop_election()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    do {
      RLockGuard guard(lock_);
      ret = state_mgr_.stop_election();
    } while (false);
    while (OB_SUCC(ret) && state_mgr_.is_leader_active()) {
      if (OB_FAIL(switch_state())) {
        CLOG_LOG(WARN, "switch_state failed after stop election", K(ret), K_(partition_key));
      } else if (state_mgr_.is_leader_active()) {
        // election should return not running
        // should be leader active after one cycle
        // sleep for further modify
        // should not come to this branch
        CLOG_LOG(ERROR, "still leader active after stop election", K(ret), K_(partition_key));
        usleep(100);
      }
    }
  }
  return ret;
}

int ObPartitionLogService::remove_election()
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    // The reason why ret is not modified here is
    // According to the existing process of the upper layer, after the partition is deserialized from the file during
    // the recovery process There is a process of adding to pg_mgr without initialization And maybe before the
    // initialization, the partition is removed, and this interface is called at the same time At this time, if this
    // interface returns an error, the operation of removing the partition from the upper layer will fail
    CLOG_LOG(WARN, "pls not inited yet, just warn, return OB_SUCCESS", K_(partition_key));
  } else {
    // The reason for adding a write lock here is to hope that all read locks will be released when remove, and remove
    // when the old election is no longer used in other locations In order to provide a clear semantics to the outside
    // world That is, after remove_election, no one will hold an expired object Otherwise, it may happen that the same
    // pkey has a new object created, but the old object is still in use The outside may expect this interface to
    // provide the above-mentioned strict semantics, otherwise unexpected behavior may occur
    WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
    if (true == election_has_removed_) {
      CLOG_LOG(WARN, "election has been marked removed, should not call this again", K_(partition_key));
    } else if (NULL == election_mgr_) {
      // After the expected initialization, the election_mgr_ should not be empty when the function is called,
      // and the call stack needs to be checked when an incorrect calling method occurs
      CLOG_LOG(ERROR, "wrong way to call remove_election(), election_mgr_ is NULL", K_(partition_key));
    } else if (OB_FAIL(election_mgr_->remove_partition(partition_key_))) {
      CLOG_LOG(WARN, "remove partition from election_mgr failed", K(ret), K_(partition_key));
    } else {
      election_has_removed_ = true;
      FLOG_INFO(
          "remove partition from election_mgr success", K_(partition_key), K_(election_has_removed), K_(election));
    }
  }

  return ret;
}

void ObPartitionLogService::destroy()
{
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  if (is_inited_) {
    is_inited_ = false;
    int tmp_ret = OB_SUCCESS;
    if (sw_.is_inited()) {
      sw_.destroy();
    }
    // Election is not removed from mgr, it may be because the addition to pg_mgr failed
    if (!election_has_removed_) {
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = election_mgr_->remove_partition(partition_key_))) {
        CLOG_LOG(WARN, "remove_partition from election_mgr failed", K(tmp_ret), K_(partition_key));
      } else {
        election_has_removed_ = true;
        CLOG_LOG(WARN, "remove election in destroy(), probably add to pg_mgr failed", K(tmp_ret), K_(partition_key));
      }
    } else {
      FLOG_INFO("election has been removed", K_(partition_key), K_(election_has_removed), K_(election));
    }

    if (state_mgr_.is_inited()) {
      state_mgr_.destroy();
      if (NULL != election_) {
        if (NULL == election_mgr_) {
          CLOG_LOG(ERROR,
              "could not revert election, cause election_mgr_ is NULL",
              K_(partition_key),
              KP_(election_mgr),
              KP_(election));
        } else {
          election_mgr_->revert_election(election_);
          election_ = NULL;
        }
      } else {
        // do nothing
      }
    } else {
    }
    restore_mgr_.destroy();
    remote_log_query_engine_ = NULL;
    reconfirm_.reset();
    log_engine_ = NULL;
    replay_engine_ = NULL;
L
LIN 已提交
4170
    // partition_service_ is a global singleton obj, no need set NULL
O
oceanbase-admin 已提交
4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183
    // partition_service_ = NULL;
    if (mm_.is_inited()) {
      mm_.destroy();
    }
    if (cascading_mgr_.is_inited()) {
      cascading_mgr_.destroy();
    }
    fetch_log_engine_ = NULL;
    alloc_mgr_ = NULL;
    event_scheduler_ = NULL;
    self_.reset();
    // checksum_.destroy()
    saved_base_storage_info_.reset();
Y
yy0 已提交
4184
    zone_priority_ = 0;
O
oceanbase-admin 已提交
4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421
    is_candidate_ = false;
    free_cursor_array_cache_();
    free_broadcast_info_mgr_();
    FLOG_INFO(
        "ObPartitionLogService destroy finished", K_(partition_key), K(lbt()), K_(election_has_removed), K_(election));
  } else {
    CLOG_LOG(WARN, "ObPartitionLogService destroy, no need exec", K_(partition_key), KP(this), K(lbt()));
  }
}

int ObPartitionLogService::set_election_leader(const common::ObAddr& leader, const int64_t lease_start)
{
  int ret = OB_SUCCESS;
  WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
  if (OB_FAIL(ret)) {
    CLOG_LOG(WARN, "wrlock failed", K(ret), K_(partition_key));
  } else if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!leader.is_valid() || lease_start <= 0) {
    ret = OB_INVALID_ARGUMENT;
  } else if (!restore_mgr_.is_archive_restoring()) {
    ret = state_mgr_.set_election_leader(leader, lease_start);
  } else {
    if (self_ == leader) {
      ret = restore_mgr_.leader_takeover();
    }
  }
  return ret;
}

bool ObPartitionLogService::check_election_leader_(int64_t& leader_epoch, ObTsWindows& changing_leader_windows) const
{
  // It returns OB_SUCCESS when changing leader
  int ret = OB_SUCCESS;
  bool bool_ret = false;
  ObAddr leader;
  if (!ObReplicaTypeCheck::is_paxos_replica_V2(mm_.get_replica_type())) {
    ret = OB_NOT_MASTER;
  } else if (OB_SUCCESS != (ret = state_mgr_.get_election_leader(leader, leader_epoch, changing_leader_windows))) {
    CLOG_LOG(WARN, "get leader from election failed", K_(partition_key), K(ret));
  } else if (!leader.is_valid() || (self_ != leader) || (leader_epoch != state_mgr_.get_leader_epoch())) {
    CLOG_LOG(INFO,
        "now election has another leader",
        K_(partition_key),
        K(leader),
        K_(self),
        "current_leader_epoch",
        leader_epoch,
        "saved_leader_epoch",
        state_mgr_.get_leader_epoch());
  } else {
    bool_ret = true;
  }
  return bool_ret;
}

int ObPartitionLogService::set_next_index_log_id(const uint64_t log_id, const int64_t accum_checksum)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
    sw_.set_next_index_log_id(log_id, accum_checksum);
  }
  return ret;
}

int ObPartitionLogService::is_offline(bool& is_offline)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    is_offline = state_mgr_.is_offline();
  }
  return ret;
}

int ObPartitionLogService::set_offline()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
    state_mgr_.set_offline();
    const uint32_t reset_type = ObLogCascadingMgr::ResetPType::SELF_OFFLINE;
    cascading_mgr_.reset_parent(reset_type);
    share::ObCascadMemberList prev_children_list;
    int tmp_ret = OB_SUCCESS;
    if (OB_SUCCESS != (tmp_ret = cascading_mgr_.get_children_list(prev_children_list))) {
      CLOG_LOG(WARN, "get_children_list failed", K_(partition_key), K(tmp_ret));
    }
    cascading_mgr_.reset_children_list();
    cascading_mgr_.notify_change_parent(prev_children_list);
  }
  CLOG_LOG(INFO, "set_offline", K(ret), K_(partition_key));
  return ret;
}

int ObPartitionLogService::set_online(const ObBaseStorageInfo& base_storage_info, const ObVersion& memstore_version)
{
  int ret = OB_SUCCESS;
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!state_mgr_.is_offline()) {
    CLOG_LOG(INFO, "self is already online, skip", K(ret), K_(partition_key));
  } else {
    uint64_t next_replay_log_id = base_storage_info.get_last_replay_log_id() + 1;
    int64_t next_replay_log_ts = base_storage_info.get_submit_timestamp() + 1;
    sw_.set_next_replay_log_id_info(next_replay_log_id, next_replay_log_ts);
    if (base_storage_info.get_last_replay_log_id() + 1 >= sw_.get_start_id()) {
      // The migration logic guarantees that PartitionLogService must be offline when truncate_sw is executed
      // Partition in the offline state will not be elected as Leader, and will not submit_log
      // The thread that executes truncate is the thread of the migration task.
      // Since this operation may take a long time, the write lock needs to be released;
      lock_.wrunlock();
      if (OB_FAIL(truncate_first_stage_(base_storage_info))) {
        CLOG_LOG(WARN, "truncate_first_stage_ failed", K(ret), K_(partition_key), K(base_storage_info));
      }
      lock_.wrlock_with_retry(WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
      if (OB_SUCC(ret) && OB_FAIL(truncate_second_stage_(base_storage_info))) {
        CLOG_LOG(WARN, "truncate_second_stage_ failed", K(ret), K(partition_key_), K(base_storage_info));
      }
    } else {
      // do nothing
    }
    if (OB_SUCC(ret)) {
      (void)state_mgr_.update_freeze_version(memstore_version);
      state_mgr_.reset_need_rebuild();
      state_mgr_.set_online();
      in_sync_check_time_ = OB_INVALID_TIMESTAMP;
      common::ObAddr leader = state_mgr_.get_leader();
      if (!leader.is_valid()) {
        // leader is invalid, do nothing
      } else if (!cascading_mgr_.get_parent().is_valid() && FOLLOWER == state_mgr_.get_role()) {
        // request allocating parent when it is online
        int tmp_ret = OB_SUCCESS;
        const int64_t dst_cluster_id = state_mgr_.get_leader_cluster_id();
        if (OB_SUCCESS != (tmp_ret = cascading_mgr_.fetch_register_server(
                               leader, dst_cluster_id, state_mgr_.get_region(), state_mgr_.get_idc(), true, false))) {
          CLOG_LOG(WARN, "call fetch_register_server failed", K_(partition_key), K(tmp_ret), K(leader));
        } else {
          (void)cascading_mgr_.reset_last_request_state();
        }
      } else {
        // do nothing
      }
    }
  }
  CLOG_LOG(INFO, "set_online", K(ret), K_(partition_key), K(base_storage_info), K(memstore_version));
  return ret;
}

int ObPartitionLogService::is_in_sync(bool& is_sync) const
{
  // if there are too many partitions in one observer,
  // user can change max_safe_stale_time to avoid unsync ret
  int ret = OB_SUCCESS;
  is_sync = false;
  int64_t tenant_id = partition_key_.get_tenant_id();
  int64_t max_safe_stale_time = transaction::ObWeakReadUtil::max_stale_time_for_weak_consistency(tenant_id);
  max_safe_stale_time = std::min(max_safe_stale_time, MAX_SYNC_FALLBACK_INTERVAL);

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K_(partition_key), K(ret));
  } else if (state_mgr_.is_need_rebuild()) {
    ret = OB_LOG_NOT_SYNC;
    CLOG_LOG(WARN, "partition_log_service need rebuild", K(ret), K_(partition_key));
  } else {
    int64_t next_replay_log_timestamp = OB_INVALID_TIMESTAMP;
    if (OB_FAIL(sw_.get_next_replay_log_timestamp(next_replay_log_timestamp))) {
      CLOG_LOG(WARN, "sw get_next_replay_log_timestamp failed", K(ret), K_(partition_key));
    } else {
      is_sync = ((ObTimeUtility::current_time() - next_replay_log_timestamp) < max_safe_stale_time);
    }
  }
  return ret;
}

int ObPartitionLogService::is_log_sync_with_leader(bool& is_sync) const
{
  int ret = OB_SUCCESS;
  is_sync = false;
  ObAddr leader;
  int64_t dst_cluster_id = OB_INVALID_CLUSTER_ID;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    leader = state_mgr_.get_leader();
    dst_cluster_id = state_mgr_.get_leader_cluster_id();
  }

  if (OB_SUCC(ret)) {
    int64_t unused_timestamp = OB_INVALID_TIMESTAMP;
    uint64_t leader_max_confirmed_log_id = OB_INVALID_ID;
    bool unused_remote_replica_is_normal = false;
    if (OB_FAIL(is_in_sync(is_sync))) {
      CLOG_LOG(WARN, "is_in_sync failed", K(ret), K(partition_key_));
    } else if (is_sync) {
      // already sync, do nothing
    } else if (!leader.is_valid()) {
      // leader is invalid, return false
      is_sync = false;
    } else if (OB_FAIL(log_engine_->get_remote_membership_status(leader,
                   dst_cluster_id,
                   partition_key_,
                   unused_timestamp,
                   leader_max_confirmed_log_id,
                   unused_remote_replica_is_normal))) {
      // return OB_SUCCESS when rpc failed to avoid migrate failure, this may occur when local leader is out of date
      CLOG_LOG(WARN,
          "get_remote_membership_status from leader failed",
          K(ret),
          K(leader),
          K(dst_cluster_id),
          K(partition_key_),
          K(unused_timestamp),
          K(leader_max_confirmed_log_id));
      is_sync = false;
      ret = OB_SUCCESS;
    } else {
      // 1000 is to set aside a sufficient threshold to be consistent with the pre-check for member changes
      is_sync = (leader_max_confirmed_log_id <= (get_next_index_log_id() - 1 + 1000));
    }
  }
  return ret;
}

int ObPartitionLogService::is_log_sync_with_primary(const int64_t switchover_epoch, bool& is_sync) const
{
L
LIN 已提交
4422
  // caller guarantees self is non-private table
O
oceanbase-admin 已提交
4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688
  int ret = OB_SUCCESS;
  int64_t local_switchover_epoch = OB_INVALID_TIMESTAMP;
  uint64_t leader_max_log_id = OB_INVALID_ID;
  int64_t leader_next_log_ts = OB_INVALID_TIMESTAMP;
  is_sync = false;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K_(partition_key), K(ret));
  } else if (switchover_epoch <= 0) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K_(partition_key), K(ret), K(switchover_epoch));
  } else if (OB_FAIL(sw_.get_switchover_info(local_switchover_epoch, leader_max_log_id, leader_next_log_ts))) {
    CLOG_LOG(WARN, "sw_.get_switchover_info failed", K_(partition_key), K(ret));
  } else if (switchover_epoch != local_switchover_epoch) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(
        WARN, "switchover_epoch not match", K_(partition_key), K(ret), K(switchover_epoch), K(local_switchover_epoch));
  } else {
    int64_t local_max_confirmed_log_id = sw_.get_max_confirmed_log_id();
    uint64_t next_replay_log_id = OB_INVALID_ID;
    int64_t next_replay_log_ts = OB_INVALID_TIMESTAMP;
    sw_.get_next_replay_log_id_info(next_replay_log_id, next_replay_log_ts);
    if (local_max_confirmed_log_id != leader_max_log_id) {
      CLOG_LOG(WARN,
          "max_log_id not match with leader",
          K_(partition_key),
          K(ret),
          K(leader_max_log_id),
          K(local_max_confirmed_log_id));
    } else if (OB_INVALID_TIMESTAMP == next_replay_log_ts || next_replay_log_ts < leader_next_log_ts) {
      CLOG_LOG(WARN,
          "next_replay_log_ts not match with leader",
          K_(partition_key),
          K(ret),
          K(leader_next_log_ts),
          K(next_replay_log_ts),
          K(next_replay_log_id));
    } else {
      is_sync = true;
    }
  }

  if (OB_SUCC(ret) && !is_sync) {
    CLOG_LOG(INFO,
        "self is not sync with leader",
        K(ret),
        K_(partition_key),
        K(is_sync),
        "max_confirmed_log_id",
        sw_.get_max_confirmed_log_id(),
        K(leader_max_log_id));
  }
  return ret;
}

int ObPartitionLogService::is_need_rebuild(bool& need_rebuild) const
{
  int ret = OB_SUCCESS;
  need_rebuild = false;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K_(partition_key), K(ret));
  } else {
    need_rebuild = state_mgr_.is_need_rebuild();
  }
  return ret;
}

bool ObPartitionLogService::is_leader_active() const
{
  bool is_leader_active = false;
  RLockGuard guard(lock_);
  is_leader_active = state_mgr_.is_leader_active();
  return is_leader_active;
}

int ObPartitionLogService::check_self_is_election_leader_() const
{
  int ret = OB_SUCCESS;
  common::ObAddr election_leader;
  int64_t epoch = OB_INVALID_TIMESTAMP;
  if (OB_FAIL(state_mgr_.get_election_leader(election_leader, epoch))) {
    CLOG_LOG(WARN, "get_leader_from_election failed", K_(partition_key), K(ret));
  } else if (!election_leader.is_valid() || election_leader != self_ || epoch != state_mgr_.get_leader_epoch()) {
    // already not leader, do nothing
    ret = OB_NOT_MASTER;
  } else {
    // self is leader
  }

  return ret;
}

bool ObPartitionLogService::is_paxos_offline_replica_() const
{
  bool bool_ret = false;
  RLockGuard guard(lock_);
  if (ObReplicaTypeCheck::is_paxos_replica_V2(mm_.get_replica_type()) && state_mgr_.is_cluster_allow_vote() &&
      false == ATOMIC_LOAD(&is_in_leader_member_list_)) {
    bool_ret = true;
  }
  return bool_ret;
}

bool ObPartitionLogService::has_valid_member_list() const
{
  bool bool_ret = false;
  if (IS_NOT_INIT) {
  } else {
    RLockGuard guard(lock_);
    bool_ret = state_mgr_.has_valid_member_list();
  }
  return bool_ret;
}

bool ObPartitionLogService::has_valid_next_replay_log_ts() const
{
  // Whether the data replica has not generated memtable, mainly includes the following situations:
  // 1. The follower of D replica, returns false;
  // 2. The leader of D replica: the active state returns true,
  //        restoring status returns true,
  //        Restore has not started in other states, and returns false.
  int bool_ret = false;
  if (IS_NOT_INIT) {
  } else {
    RLockGuard guard(lock_);
    if (0 != mm_.get_replica_property().get_memstore_percent()) {
      bool_ret = true;
    } else if (FOLLOWER == state_mgr_.get_role()) {
      bool_ret = false;
    } else if (LEADER == state_mgr_.get_role()) {
      if (state_mgr_.is_leader_active()) {
        bool_ret = true;
      } else if (state_mgr_.is_restoring()) {
        bool_ret = true;
      } else {
        bool_ret = false;
      }
    }
  }
  return bool_ret;
}

int ObPartitionLogService::get_next_replay_info_for_data_replica_(
    uint64_t& next_replay_log_id, int64_t& next_replay_log_ts)
{
  int ret = OB_SUCCESS;
  if (LEADER == state_mgr_.get_role()) {
    if (state_mgr_.is_leader_active()) {
      // leader active state, return next_replay_ts
      (void)sw_.get_next_replay_log_id_info(next_replay_log_id, next_replay_log_ts);
    } else if (state_mgr_.is_restoring()) {
      // self is restoring, return last_replay_log_ts
      uint64_t last_replay_log_id = OB_INVALID_ID;
      int64_t last_replay_log_ts = OB_INVALID_TIMESTAMP;
      (void)sw_.get_last_replay_log(last_replay_log_id, last_replay_log_ts);
      if (OB_INVALID_ID == last_replay_log_id) {
      } else {
        next_replay_log_id = last_replay_log_id + 1;
        next_replay_log_ts = last_replay_log_ts + 1;
      }
    } else {
      // leader is not restoring, no need get from clog
    }
  } else {
    // empty pg or D replica
    (void)sw_.get_next_replay_log_id_info(next_replay_log_id, next_replay_log_ts);
  }
  return ret;
}

int ObPartitionLogService::get_next_replay_log_info(uint64_t& next_replay_log_id, int64_t& next_replay_log_timestamp)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    if (state_mgr_.is_offline()) {
      ret = OB_STATE_NOT_MATCH;
    } else if (0 == mm_.get_replica_property().get_memstore_percent()) {
      // If it is a replica of D, when obtaining the standby machine-readable timestamp, the following situations need
      // to be distinguished: 1) In the non-<leader, active> and non-restore state, the time stamp read by the standby
      // machine needs to take the right boundary of its static data; 2) During the restore process (only the leader
      // will execute), the read timestamp of the standby machine needs to be the submit_timestamp of the
      // last_replay_log that has been submitted for playback; 3) In the state of <leader, active>, take
      // next_replay_log_ts.
      if (OB_FAIL(get_next_replay_info_for_data_replica_(next_replay_log_id, next_replay_log_timestamp))) {
        CLOG_LOG(WARN,
            "get_next_replay_info_for_data_replica_ failed",
            K_(partition_key),
            K(ret),
            K(next_replay_log_id),
            K(next_replay_log_timestamp));
      }
    } else if (state_mgr_.is_restoring()) {
      // self is restoring, return last_replay_log_ts
      uint64_t last_replay_log_id = OB_INVALID_ID;
      int64_t last_replay_log_ts = OB_INVALID_TIMESTAMP;
      (void)sw_.get_last_replay_log(last_replay_log_id, last_replay_log_ts);
      if (OB_INVALID_ID == last_replay_log_id) {
      } else {
        next_replay_log_id = last_replay_log_id + 1;
        next_replay_log_timestamp = last_replay_log_ts + 1;
      }
    } else {
      // normal replica, take next_replay_log_ts.
      (void)sw_.get_next_replay_log_id_info(next_replay_log_id, next_replay_log_timestamp);
    }
    if (OB_FAIL(ret)) {
      if (EXECUTE_COUNT_PER_SEC(10)) {
        CLOG_LOG(DEBUG,
            "get_next_replay_log_info error",
            K(ret),
            K_(partition_key),
            "is_offline",
            state_mgr_.is_offline(),
            "role",
            state_mgr_.get_role(),
            "state",
            state_mgr_.get_state());
      }
    }
  }

  return ret;
}

int ObPartitionLogService::get_follower_log_delay(int64_t& log_delay)
{
  int ret = OB_SUCCESS;
  int64_t next_replay_log_timestamp = OB_INVALID_TIMESTAMP;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    if (state_mgr_.is_offline() || state_mgr_.is_need_rebuild() || FOLLOWER != state_mgr_.get_role()) {
      ret = OB_STATE_NOT_MATCH;
    } else if (OB_FAIL(sw_.get_next_replay_log_timestamp(next_replay_log_timestamp))) {
      CLOG_LOG(WARN, "get_next_replay_log_timestamp failed", K_(partition_key), K(ret), K(next_replay_log_timestamp));
    } else {
      log_delay = ObTimeUtility::current_time() - next_replay_log_timestamp;
    }
  }
  return ret;
}

int ObPartitionLogService::process_keepalive_msg(const common::ObAddr& server, const int64_t cluster_id,
    const uint64_t next_log_id, const int64_t next_log_ts_lb, const uint64_t deliver_cnt)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  const common::ObAddr parent = cascading_mgr_.get_parent_addr();
  const common::ObAddr restore_leader = restore_mgr_.get_restore_leader();
  const common::ObReplicaType replica_type = mm_.get_replica_type();

  bool is_valid_parent = false;
  if (server == parent || cascading_mgr_.is_in_reregister_period()) {
    // parent match or in reregister period, allow to process
    is_valid_parent = true;
  }

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
4689 4690
  } else if (LEADER == state_mgr_.get_role() || (parent.is_valid() && !is_valid_parent) ||
             state_mgr_.is_diff_cluster_req_in_disabled_state(cluster_id)) {
O
oceanbase-admin 已提交
4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701
    if (partition_reach_time_interval(30 * 1000 * 1000, send_reject_msg_warn_time_)) {
      CLOG_LOG(WARN,
          "sender is unexpected, need reject",
          K_(partition_key),
          K(server),
          K(cluster_id),
          K(parent),
          K(restore_leader),
          K(next_log_id),
          K(next_log_ts_lb),
          "replica_type",
4702 4703 4704
          replica_type,
          "self_cluster_id",
          state_mgr_.get_self_cluster_id());
O
oceanbase-admin 已提交
4705 4706 4707 4708
    }
    ObReplicaMsgType msg_type = OB_REPLICA_MSG_TYPE_NOT_CHILD;
    (void)cascading_mgr_.reject_server(server, cluster_id, msg_type);
  } else if (deliver_cnt > MAX_DELIVER_CNT) {
4709 4710 4711 4712 4713 4714 4715 4716
    // Because every cascading msg is async, a replica A is added to its new parent B's children_list firstly,
    // and then A is noticed to update parent to B. There is a time gap between these two events.
    // During this time gap, B may assign A as other replica C's parent, and the typo maybe is C->D->A before A update
    // parent. So in order to support smooth switching of parent, we need to accept short-term loop case and just break
    // it immediately.
    ObCascadMemberList children_list;
    (void)cascading_mgr_.get_children_list(children_list);
    CLOG_LOG(WARN,
O
oceanbase-admin 已提交
4717 4718 4719 4720 4721 4722 4723
        "deliver_cnt is larger than MAX_DELIVER_CNT, maybe loop appears",
        K(ret),
        K_(partition_key),
        K(next_log_id),
        K(next_log_ts_lb),
        K(deliver_cnt),
        "parent",
4724 4725
        cascading_mgr_.get_parent(),
        K(children_list));
O
oceanbase-admin 已提交
4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163
    // if predict there is loop, reset children_list to break it
    cascading_mgr_.reset_children_list();
  } else if (OB_FAIL(sw_.follower_update_leader_next_log_info(next_log_id, next_log_ts_lb))) {
    CLOG_LOG(WARN, "sw_.follower_update_leader_next_log_info failed", K(ret), K_(partition_key));
  } else if (!restore_mgr_.is_archive_restoring()) {
    // transfer keepalive to children in non-restoring state
    (void)send_keepalive_msg_to_children_(next_log_id, next_log_ts_lb, deliver_cnt);
    if (STANDBY_LEADER == state_mgr_.get_role()) {
      // standby_leader transfer keepalive to member_list
      (void)send_keepalive_msg_to_mlist_(next_log_id, next_log_ts_lb, deliver_cnt);
    }
  } else {
    // do nothing
  }
  return ret;
}

int ObPartitionLogService::send_keepalive_msg_to_mlist_(
    const uint64_t next_log_id, const int64_t next_log_ts_lb, const uint64_t deliver_cnt) const
{
  UNUSED(deliver_cnt);
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (LEADER == state_mgr_.get_role() || STANDBY_LEADER == state_mgr_.get_role()) {
    // send to curr_member_list and children_list
    const int64_t self_cluster_id = obrpc::ObRpcNetHandler::CLUSTER_ID;
    const common::ObMemberList member_list = mm_.get_curr_member_list();
    const int64_t member_cnt = member_list.get_member_number();
    for (int64_t i = 0; i < member_cnt && OB_SUCC(ret); ++i) {
      common::ObAddr server;
      if (OB_FAIL(member_list.get_server_by_index(i, server))) {
        CLOG_LOG(WARN, "get_server_by_index failed", K(ret), K_(partition_key), K(server));
      } else if (server == self_) {
        // skip self
      } else if (OB_FAIL(log_engine_->send_keepalive_msg(
                     server, self_cluster_id, partition_key_, next_log_id, next_log_ts_lb, 0 /* deliver_cnt */))) {
        CLOG_LOG(WARN, "send_keepalive_msg failed", K(ret), K_(partition_key), K(server));
      } else {
        // do nothing
      }
    }
  } else {
  }
  return ret;
}

int ObPartitionLogService::send_keepalive_msg_to_children_(
    const uint64_t next_log_id, const int64_t next_log_ts_lb, const uint64_t deliver_cnt) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    ObCascadMemberList children;
    if (OB_FAIL(cascading_mgr_.get_children_list(children))) {
      CLOG_LOG(WARN, "get_children_list failed", K_(partition_key), K(ret));
    } else {
      const int64_t child_cnt = children.get_member_number();
      for (int64_t i = 0; i < child_cnt; ++i) {
        share::ObCascadMember child;
        if (OB_FAIL(children.get_member_by_index(i, child))) {
          CLOG_LOG(WARN, "get_member_by_index failed", K(ret), K_(partition_key), K(child));
        } else if (!child.is_valid()) {
          CLOG_LOG(WARN, "invalid child", K(ret), K_(partition_key), K(child));
        } else if (OB_FAIL(log_engine_->send_keepalive_msg(child.get_server(),
                       child.get_cluster_id(),
                       partition_key_,
                       next_log_id,
                       next_log_ts_lb,
                       deliver_cnt + 1))) {
          CLOG_LOG(WARN, "send_keepalive_msg failed", K(ret), K_(partition_key), K(child));
        } else {
          // do nothing
        }
      }
    }
    if (OB_SUCC(ret)) {
      ObCascadMemberList async_standby_children;
      if (OB_FAIL(cascading_mgr_.get_async_standby_children(async_standby_children))) {
        CLOG_LOG(WARN, "get_async_standby_children failed", K_(partition_key), K(ret));
      } else {
        const int64_t child_cnt = async_standby_children.get_member_number();
        for (int64_t i = 0; i < child_cnt; ++i) {
          share::ObCascadMember child;
          if (OB_FAIL(async_standby_children.get_member_by_index(i, child))) {
            CLOG_LOG(WARN, "get_member_by_index failed", K(ret), K_(partition_key), K(child));
          } else if (!child.is_valid()) {
            CLOG_LOG(WARN, "invalid child", K(ret), K_(partition_key), K(child));
          } else if (OB_FAIL(log_engine_->send_keepalive_msg(child.get_server(),
                         child.get_cluster_id(),
                         partition_key_,
                         next_log_id,
                         next_log_ts_lb,
                         deliver_cnt + 1))) {
            CLOG_LOG(WARN, "send_keepalive_msg failed", K(ret), K_(partition_key), K(child));
          } else {
            // do nothing
          }
        }
      }
    }
  }
  return ret;
}

int ObPartitionLogService::process_sync_log_archive_progress_msg(
    const common::ObAddr& server, const int64_t cluster_id, const ObPGLogArchiveStatus& status)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  const common::ObReplicaType replica_type = mm_.get_replica_type();
  const common::ObAddr parent = cascading_mgr_.get_parent_addr();

  bool is_valid_parent = false;
  if (server == parent || cascading_mgr_.is_in_reregister_period()) {
    // parent match or in reregister period, allow to process
    is_valid_parent = true;
  }

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (LEADER == state_mgr_.get_role() || (!parent.is_valid()) || (parent.is_valid() && !is_valid_parent)) {
    if (partition_reach_time_interval(30 * 1000 * 1000, send_reject_msg_warn_time_)) {
      CLOG_LOG(WARN,
          "sender is not my leader",
          K_(partition_key),
          K(server),
          K(cluster_id),
          K(parent),
          K(status),
          "replica_type",
          replica_type);
    }
  } else if (OB_FAIL(follower_update_log_archive_progress_(status))) {
    CLOG_LOG(WARN, "sw_.follower_update_log_archive_progress failed", K(ret), K_(partition_key), K(status));
  } else if (OB_FAIL(send_archive_progress_msg_to_children_(status))) {
    CLOG_LOG(WARN, "failed to send_archive_progress_msg_to_children_", K(ret), K_(partition_key), K(status));
  } else { /*do nothing*/
  }
  return ret;
}

int ObPartitionLogService::follower_update_log_archive_progress_(const ObPGLogArchiveStatus& status)
{
  int ret = OB_SUCCESS;
  SpinWLockGuard wlock_guard(log_archive_status_rwlock_);
  if (log_archive_status_ < status) {
    log_archive_status_ = status;
  }
  return ret;
}

int ObPartitionLogService::process_leader_max_log_msg(const common::ObAddr& server, const int64_t switchover_epoch,
    const uint64_t leader_max_log_id, const int64_t leader_next_log_ts)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  const common::ObAddr parent = cascading_mgr_.get_parent_addr();
  const common::ObReplicaType replica_type = mm_.get_replica_type();

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    if (LEADER == state_mgr_.get_role() || server != parent) {
      if (partition_reach_time_interval(30 * 1000 * 1000, send_reject_msg_warn_time_)) {
        CLOG_LOG(WARN,
            "sender is not my parent",
            K_(partition_key),
            K(server),
            "parent",
            parent,
            K(leader_max_log_id),
            K(replica_type));
      }
    } else if (OB_FAIL(
                   sw_.follower_update_leader_max_log_info(switchover_epoch, leader_max_log_id, leader_next_log_ts))) {
      CLOG_LOG(WARN, "sw_.follower_update_leader_max_log_info failed", K(ret), K_(partition_key));
    } else {
      // do nothing
    }

    (void)send_max_log_msg_to_children_(switchover_epoch, leader_max_log_id, leader_next_log_ts);

    if (STANDBY_LEADER == state_mgr_.get_role()) {
      (void)send_max_log_msg_to_mlist_(switchover_epoch, leader_max_log_id, leader_next_log_ts);
    }
  }
  return ret;
}

int ObPartitionLogService::send_max_log_msg_to_children_(
    const int64_t switchover_epoch, const uint64_t leader_max_log_id, const int64_t next_log_ts) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    ObCascadMemberList children;
    if (OB_FAIL(cascading_mgr_.get_children_list(children))) {
      CLOG_LOG(WARN, "get_children_list failed", K_(partition_key), K(ret));
    } else {
      const int64_t child_cnt = children.get_member_number();
      for (int64_t i = 0; i < child_cnt; ++i) {
        share::ObCascadMember child;
        if (OB_FAIL(children.get_member_by_index(i, child))) {
          CLOG_LOG(WARN, "get_member_by_index failed", K(ret), K_(partition_key), K(child));
        } else if (!child.is_valid()) {
          CLOG_LOG(WARN, "invalid child", K(ret), K_(partition_key), K(child));
        } else if (OB_FAIL(log_engine_->send_leader_max_log_msg(child.get_server(),
                       child.get_cluster_id(),
                       partition_key_,
                       switchover_epoch,
                       leader_max_log_id,
                       next_log_ts))) {
          CLOG_LOG(WARN, "send_leader_max_log_msg failed", K(ret), K_(partition_key), K(child));
        } else {
          CLOG_LOG(DEBUG,
              "send_max_log_msg_to_children_ succ",
              K(ret),
              K_(partition_key),
              K(children),
              K(switchover_epoch),
              K(leader_max_log_id),
              K(next_log_ts));
        }
      }
    }

    if (OB_SUCC(ret)) {
      // send to sync standby child in max protection mode
      ObCascadMember sync_child = cascading_mgr_.get_sync_standby_child();
      if (sync_child.is_valid() && OB_FAIL(log_engine_->send_leader_max_log_msg(sync_child.get_server(),
                                       sync_child.get_cluster_id(),
                                       partition_key_,
                                       switchover_epoch,
                                       leader_max_log_id,
                                       next_log_ts))) {
        CLOG_LOG(WARN, "send_leader_max_log_msg failed", K(ret), K_(partition_key), K(sync_child));
      }
    }

    if (OB_SUCC(ret)) {
      // send to other standby children
      ObCascadMemberList async_standby_children;
      if (OB_FAIL(cascading_mgr_.get_async_standby_children(async_standby_children))) {
        CLOG_LOG(WARN, "get_async_standby_children failed", K_(partition_key), K(ret));
      } else {
        const int64_t child_cnt = async_standby_children.get_member_number();
        for (int64_t i = 0; i < child_cnt; ++i) {
          share::ObCascadMember child;
          if (OB_FAIL(async_standby_children.get_member_by_index(i, child))) {
            CLOG_LOG(WARN, "get_member_by_index failed", K(ret), K_(partition_key), K(child));
          } else if (!child.is_valid()) {
            CLOG_LOG(WARN, "invalid child", K(ret), K_(partition_key), K(child));
          } else if (OB_FAIL(log_engine_->send_leader_max_log_msg(child.get_server(),
                         child.get_cluster_id(),
                         partition_key_,
                         switchover_epoch,
                         leader_max_log_id,
                         next_log_ts))) {
            CLOG_LOG(WARN, "send_leader_max_log_msg failed", K(ret), K_(partition_key), K(child));
          } else {
            CLOG_LOG(DEBUG,
                "[HAOFAN] send_max_log_msg_to_children_ succ",
                K(ret),
                K_(partition_key),
                K(async_standby_children),
                K(switchover_epoch),
                K(leader_max_log_id),
                K(next_log_ts));
          }
        }
      }
    }
  }
  return ret;
}

int ObPartitionLogService::send_max_log_msg_to_mlist_(
    const int64_t switchover_epoch, const uint64_t leader_max_log_id, const int64_t next_log_ts) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    const int64_t self_cluster_id = obrpc::ObRpcNetHandler::CLUSTER_ID;
    // send to paxos member_list
    const ObMemberList member_list = mm_.get_curr_member_list();
    const int64_t member_cnt = member_list.get_member_number();
    for (int64_t i = 0; i < member_cnt && OB_SUCC(ret); ++i) {
      ObMember member;
      if (OB_FAIL(member_list.get_member_by_index(i, member))) {
        CLOG_LOG(WARN, "get_server_by_index failed", K(ret), K_(partition_key));
      } else if (!member.is_valid()) {
        CLOG_LOG(WARN, "invalid member", K(ret), K_(partition_key), K(member));
      } else if (member.get_server() == self_) {
        // skip self
      } else if (OB_FAIL(log_engine_->send_leader_max_log_msg(member.get_server(),
                     self_cluster_id,
                     partition_key_,
                     switchover_epoch,
                     leader_max_log_id,
                     next_log_ts))) {
        CLOG_LOG(WARN, "send_leader_max_log_msg failed", K(ret), K_(partition_key), K(member));
      } else {
        CLOG_LOG(DEBUG,
            "leader_send_max_log_msg_to_mlist_ succ",
            K(ret),
            K_(partition_key),
            K(member_list),
            K(switchover_epoch),
            K(leader_max_log_id),
            K(next_log_ts));
      }
    }
  }
  return ret;
}

int ObPartitionLogService::migrate_set_base_storage_info(const common::ObBaseStorageInfo& base_storage_info)
{
  int ret = OB_SUCCESS;
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!state_mgr_.is_offline()) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "not offline", K(ret), K_(partition_key));
  } else {
    // At this point, an empty memstore has been created using the parameter base_storage_info
    // In this case the dump must return base_storage_info based on this
    const uint64_t last_replay_log_id = base_storage_info.get_last_replay_log_id();
    const int64_t last_submit_timestamp = base_storage_info.get_submit_timestamp();
    const int64_t accum_checksum = base_storage_info.get_accumulate_checksum();
    sw_.set_last_replay_log(last_replay_log_id, last_submit_timestamp);
    sw_.set_saved_accum_checksum(accum_checksum);
    CLOG_LOG(INFO, "migrate_set_base_storage_info finished", K(ret), K_(partition_key), K(base_storage_info));
  }
  return ret;
}

int ObPartitionLogService::restore_replayed_log(const common::ObBaseStorageInfo& base_storage_info)
{
  int ret = OB_SUCCESS;
  CLOG_LOG(INFO, "restore_replayed_log begin", K_(partition_key), K(base_storage_info));
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());

  // At this point, an empty memstore has been created using the parameter base_storage_info
  // In this case the dump must return base_storage_info based on this
  sw_.set_last_replay_log(base_storage_info.get_last_replay_log_id(), base_storage_info.get_submit_timestamp());
  sw_.set_saved_accum_checksum(base_storage_info.get_accumulate_checksum());

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_FAIL(disable_replay_())) {
    CLOG_LOG(WARN, "disable_replay_ failed", K(ret), K_(partition_key));
  } else if (OB_FAIL(replay_engine_->reset_partition(partition_key_))) {
    CLOG_LOG(WARN, "reset_partition failed", K(ret), K_(partition_key));
  } else if (!state_mgr_.set_restoring()) {
    ret = OB_ERR_UNEXPECTED;
  } else if (OB_ISNULL(replay_engine_) || OB_ISNULL(log_engine_)) {
    ret = OB_INVALID_ARGUMENT;
  } else if (base_storage_info.get_last_replay_log_id() + 1 >= sw_.get_start_id()) {
    // The migration logic guarantees that PartitionLogService must be offline when truncate_sw is executed
    // Partition in the offline state will not be elected as Leader, and will not submit_log
    // The thread that executes truncate is the thread of the migration task. Since this operation may take a long time,
    // the write lock needs to be released;
    lock_.wrunlock();
    if (OB_FAIL(truncate_first_stage_(base_storage_info))) {
      CLOG_LOG(WARN, "truncate_first_stage_ failed", K(ret), K_(partition_key), K(base_storage_info));
    }
    lock_.wrlock_with_retry(WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
    if (OB_SUCC(ret) && OB_FAIL(truncate_second_stage_(base_storage_info))) {
      CLOG_LOG(WARN, "truncate_second_stage_ failed", K(ret), K(partition_key_), K(base_storage_info));
    }
  } else {
    uint64_t start_id = base_storage_info.get_last_replay_log_id() + 1;
    const uint64_t max_log_id_to_submit = sw_.get_start_id() - 1;
    lock_.wrunlock();  // Release locks when iterating data to avoid reading locks for a long time and blocking other
                       // processes

    const int64_t start_ts = ObTimeUtility::current_time();
    bool is_not_timeout = false;
    const int64_t RETRY_SLEEP_TIME = 1 * 1000 * 1000;           // 1s
    const int64_t MAX_RETRY_COST_TIME = 10 * 60 * 1000 * 1000;  // 10min
    ObReplicaReplayType replay_type = INVALID_REPLICA_REPLAY_TYPE;
    if (OB_FAIL(sw_.get_replica_replay_type(replay_type))) {
      CLOG_LOG(WARN, "failed to get replica type", K(ret), K(partition_key_));
    } else if (REPLICA_REPLAY_ALL_LOG == replay_type) {
      do {
        if (OB_EAGAIN == ret) {
          usleep(RETRY_SLEEP_TIME);
        }
        ObLogCursorExt log_cursor_ext;
        if (OB_FAIL(log_engine_->get_cursor(partition_key_, max_log_id_to_submit, log_cursor_ext))) {
          if (OB_CURSOR_NOT_EXIST == ret) {
            // The first log ilog written by Partition has not been flushed or loaded by ilog_storage,
            // if a minor freeze occurs at this time
            // When the content of this log needs to be read, log_engine will return OB_CURSOR_NOT_EXIST;
            // In this scenario, OB_EAGAIN is also returned, and the upper layer will try again to read this data
            ret = OB_EAGAIN;
            CLOG_LOG(WARN,
                "ilog_storage get invalid log, cursor not exist",
                K(ret),
                K(partition_key_),
                K(max_log_id_to_submit));
          } else if (OB_NEED_RETRY == ret || OB_ERR_OUT_OF_UPPER_BOUND == ret) {
            ret = OB_EAGAIN;
          } else {
            CLOG_LOG(WARN, "log_engine get_cursor failed", K(ret), K(partition_key_), K(max_log_id_to_submit));
          }
        } else if (OB_FAIL(replay_engine_->submit_replay_log_task_by_restore(
                       partition_key_, max_log_id_to_submit, log_cursor_ext.get_submit_timestamp()))) {
          if (OB_EAGAIN == ret) {
            if (REACH_TIME_INTERVAL(100 * 1000)) {
              CLOG_LOG(
                  WARN, "failed to submit replay task by restore", K(ret), K_(partition_key), K(max_log_id_to_submit));
            }
          } else {
            CLOG_LOG(
                WARN, "failed to submit replay task by restore", K(ret), K_(partition_key), K(max_log_id_to_submit));
          }
        } else {
          sw_.set_last_replay_log(max_log_id_to_submit, log_cursor_ext.get_submit_timestamp());
          sw_.set_saved_accum_checksum(log_cursor_ext.get_accum_checksum());
          CLOG_LOG(INFO,
              "succ to submit replay task by restore",
              K_(partition_key),
              K(max_log_id_to_submit),
              K(log_cursor_ext));
        }
      } while (OB_EAGAIN == ret && (is_not_timeout = (ObTimeUtility::current_time() - start_ts) < MAX_RETRY_COST_TIME));

    } else if (REPLICA_REPLAY_PARTITIAL_LOG == replay_type) {
      uint64_t cur_log_id = start_id;
      bool need_replay = false;
      int64_t log_submit_timestamp = OB_INVALID_TIMESTAMP;
5164
      int64_t next_replay_log_ts_for_rg = OB_INVALID_TIMESTAMP;
O
oceanbase-admin 已提交
5165 5166 5167 5168 5169 5170 5171
      int64_t accum_checksum = 0;
      do {
        if (OB_EAGAIN == ret) {
          usleep(RETRY_SLEEP_TIME);
        }
        bool is_meta_log = false;
        ObLogType log_type = OB_LOG_UNKNOWN;
G
gm 已提交
5172 5173
        if (OB_FAIL(sw_.get_log_meta_info(
                cur_log_id, is_meta_log, log_submit_timestamp, next_replay_log_ts_for_rg, accum_checksum, log_type))) {
O
oceanbase-admin 已提交
5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186
          if (OB_EAGAIN == ret) {
            if (REACH_TIME_INTERVAL(100 * 1000)) {
              CLOG_LOG(WARN, "failed to check is meta log", K(partition_key_), K(cur_log_id), K(ret));
            }
          } else {
            CLOG_LOG(WARN, "failed to check is meta log", K(partition_key_), K(cur_log_id), K(ret));
          }
        } else {
          need_replay = is_meta_log;
        }

        uint64_t last_replay_log_id = OB_INVALID_ID;
        if (OB_FAIL(ret)) {
G
gm 已提交
5187 5188 5189 5190 5191 5192
        } else if (OB_FAIL(replay_engine_->submit_replay_log_task_sequentially(partition_key_,
                       cur_log_id,
                       log_submit_timestamp,
                       need_replay,
                       log_type,
                       next_replay_log_ts_for_rg))) {
O
oceanbase-admin 已提交
5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705
          if (OB_EAGAIN == ret) {
            if (REACH_TIME_INTERVAL(100 * 1000)) {
              CLOG_LOG(WARN, "failed to submit replay task step by step", K(ret), K_(partition_key), K(cur_log_id));
            }
          } else {
            CLOG_LOG(WARN, "failed to submit replay task step by step", K(ret), K_(partition_key), K(cur_log_id));
          }
        } else {
          sw_.set_last_replay_log(cur_log_id, log_submit_timestamp);
          sw_.set_saved_accum_checksum(accum_checksum);
          cur_log_id++;
        }
      } while ((OB_SUCC(ret) || OB_EAGAIN == ret) && (cur_log_id <= max_log_id_to_submit) &&
               (is_not_timeout = (ObTimeUtility::current_time() - start_ts < MAX_RETRY_COST_TIME)));

      if (OB_SUCC(ret) && cur_log_id > max_log_id_to_submit) {
        CLOG_LOG(INFO,
            "finished submitting log to replay engine ",
            K(ret),
            K(partition_key_),
            K(replay_type),
            K(max_log_id_to_submit),
            K(is_not_timeout));
      } else if (OB_FAIL(ret) && OB_EAGAIN != ret) {
        // do nothing
      } else {
        int64_t tmp_ret = ret;
        ret = OB_REPLAY_EAGAIN_TOO_MUCH_TIME;
        CLOG_LOG(WARN,
            "waiting too much time before finishing submitting log to replay engine ",
            KR(tmp_ret),
            KR(ret),
            K(partition_key_),
            K(replay_type),
            K(max_log_id_to_submit),
            K(is_not_timeout));
      }
    } else {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(WARN, "unexpected replica type", K(ret), K(partition_key_), K(replay_type));
    }

    if (OB_SUCC(ret)) {
      // Wait for all log submission tasks to be completed
      bool is_submit_finished = false;
      do {
        if (OB_EAGAIN == ret) {
          // tenant maybe out of memory
          usleep(RETRY_SLEEP_TIME);
        }
        if (OB_FAIL(replay_engine_->is_submit_finished(partition_key_, is_submit_finished))) {
          CLOG_LOG(WARN, "failed to check is_submit_finished", K(ret), K_(partition_key), K(max_log_id_to_submit));
        } else if (!is_submit_finished) {
          ret = OB_EAGAIN;
          if (REACH_TIME_INTERVAL(1 * 1000 * 1000)) {
            CLOG_LOG(INFO, "wait submit finished", K(partition_key_), K(max_log_id_to_submit), K(ret));
          }
        } else { /*do nothing */
        }
      } while ((OB_SUCC(ret) || OB_EAGAIN == ret) && !is_submit_finished &&
               (is_not_timeout = ObTimeUtility::current_time() - start_ts < MAX_RETRY_COST_TIME));

      if (OB_SUCC(ret) && is_submit_finished) {
        CLOG_LOG(
            INFO, "finished submitting replay log task", K(partition_key_), K(replay_type), K(max_log_id_to_submit));
      } else if (OB_FAIL(ret) && OB_EAGAIN != ret) {
        // do nothing
      } else {
        int64_t tmp_ret = ret;
        ret = OB_REPLAY_EAGAIN_TOO_MUCH_TIME;
        CLOG_LOG(WARN,
            "waiting too much time before finishing submitting replay_task",
            K(ret),
            K(tmp_ret),
            K(partition_key_),
            K(replay_type),
            K(max_log_id_to_submit),
            K(is_not_timeout),
            K(is_submit_finished));
      }
    }

    lock_.wrlock_with_retry(WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  }
  int tmp_ret = OB_SUCCESS;
  if (OB_SUCCESS != (tmp_ret = enable_replay_())) {
    CLOG_LOG(ERROR, "enable_replay_ failed", K(tmp_ret), K_(partition_key));
  }
#ifdef ERRSIM
  if (OB_SUCC(ret)) {
    ret = E(EventTable::EN_CLOG_RESTORE_REPLAYED_LOG) OB_SUCCESS;
    if (OB_FAIL(ret)) {
      CLOG_LOG(ERROR, "fake restore_replayed_log", K_(partition_key), K(ret));
    }
  }
#endif
  if (0 == mm_.get_replica_property().get_memstore_percent() && state_mgr_.is_leader_taking_over() && OB_SUCC(ret)) {
    // When D replica performs restore in the leader_taking_over phase,
    // it needs to be delayed until the active phase and then reset
  } else {
    state_mgr_.reset_restoring();
  }
  CLOG_LOG(INFO, "restore_replayed_log finished", K_(partition_key), K(ret), K(base_storage_info));
  return ret;
}

int ObPartitionLogService::truncate_first_stage_(const common::ObBaseStorageInfo& base_storage_info)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_FAIL(sw_.truncate_first_stage(base_storage_info))) {
    CLOG_LOG(WARN, "sw truncate_first_stage failed", K(ret), K(partition_key_), K(base_storage_info));
  }
  CLOG_LOG(INFO, "truncate_first_stage_", K(ret), K(partition_key_), K(base_storage_info));
  return ret;
}

int ObPartitionLogService::truncate_second_stage_(const common::ObBaseStorageInfo& base_storage_info)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_SUCCESS != (ret = mm_.set_membership_info(base_storage_info.get_curr_member_list(),
                                base_storage_info.get_membership_timestamp(),
                                base_storage_info.get_membership_log_id(),
                                base_storage_info.get_replica_num(),
                                base_storage_info.get_ms_proposal_id()))) {
    CLOG_LOG(WARN, "set_membership_info failed", K_(partition_key), K(ret), K(base_storage_info));
  } else if (OB_SUCCESS != (ret = sw_.truncate_second_stage(base_storage_info))) {
    CLOG_LOG(WARN, "sw truncate_second_stage failed", K_(partition_key), K(ret), K(base_storage_info));
  } else if (OB_SUCCESS != (ret = saved_base_storage_info_.deep_copy(base_storage_info))) {
    CLOG_LOG(WARN, "base_storage_info deep_copy failed", K_(partition_key), K(ret), K(base_storage_info));
  } else {
    // do nothing
  }
  CLOG_LOG(INFO,
      "truncate_second_stage_",
      K_(partition_key),
      K(ret),
      "last_replay_log_id",
      base_storage_info.get_last_replay_log_id(),
      "start_id",
      sw_.get_start_id());
  return ret;
}

bool ObPartitionLogService::is_freeze_log_(const char* buff, const int64_t size) const
{
  bool bool_ret = false;
  int64_t log_type = storage::OB_LOG_UNKNOWN;
  int64_t pos = 0;
  if (OB_SUCCESS != (serialization::decode_i64(buff, size, pos, &log_type))) {
    CLOG_LOG(ERROR, "deserialize failed", K_(partition_key));
    bool_ret = true;  // if error, try update data version
  } else {
    bool_ret = storage::ObStorageLogTypeChecker::is_freeze_log(log_type);
  }
  return bool_ret;
}

bool ObPartitionLogService::try_update_freeze_version_(const ObLogEntry& log_entry)
{
  bool bool_ret = true;
  ObLogType log_type = log_entry.get_header().get_log_type();
  if (OB_LOG_SUBMIT == log_type) {
    if (is_freeze_log_(log_entry.get_buf(), log_entry.get_header().get_data_len())) {
      state_mgr_.update_freeze_version(log_entry.get_header().get_freeze_version());
    }
  } else if (OB_LOG_MEMBERSHIP == log_type || OB_LOG_START_MEMBERSHIP == log_type || OB_LOG_NOP == log_type) {
    state_mgr_.update_freeze_version(log_entry.get_header().get_freeze_version());
  }
  return bool_ret;
}

int64_t ObPartitionLogService::get_membership_timestamp() const
{
  RLockGuard guard(lock_);
  return mm_.get_timestamp();
}

bool ObPartitionLogService::internal_check_remote_mc_ts_(
    const ObMemberList& member_list, const ObAddr& addr, const int64_t mc_timestamp, const uint64_t follower_max_gap)
{
  int tmp_ret = OB_SUCCESS;
  int64_t valid_num = 0;
  if (!addr.is_valid() || OB_ISNULL(log_engine_)) {
    tmp_ret = OB_INVALID_ARGUMENT;
  } else {
    ObAddr server;
    for (int64_t i = 0; i < member_list.get_member_number(); ++i) {
      server.reset();
      int64_t timestamp = OB_INVALID_TIMESTAMP;
      uint64_t follower_max_confirmed_log_id = OB_INVALID_ID;
      bool remote_replica_is_normal = false;
      if (OB_SUCCESS != (tmp_ret = member_list.get_server_by_index(i, server))) {
        CLOG_LOG(ERROR, "get_server_by_index failed", K_(partition_key), K(tmp_ret));
      } else if (server == self_ || server == addr) {
        CLOG_LOG(INFO, "server do not need check", K_(partition_key), K(server), K(addr), K_(self));
      } else if (OB_SUCCESS != (tmp_ret = log_engine_->get_remote_membership_status(server,
                                    state_mgr_.get_self_cluster_id(),
                                    partition_key_,
                                    timestamp,
                                    follower_max_confirmed_log_id,
                                    remote_replica_is_normal))) {
        CLOG_LOG(WARN, "get_remote_membership_status failed", K_(partition_key), K(tmp_ret), K(server));
      } else if (mc_timestamp != timestamp) {
        CLOG_LOG(
            INFO, "timestamp do not match", K_(partition_key), K(tmp_ret), K(server), K(mc_timestamp), K(timestamp));
      } else if (sw_.get_max_log_id() > follower_max_confirmed_log_id + follower_max_gap) {
        CLOG_LOG(INFO,
            "follower is not in sync",
            K_(partition_key),
            K(tmp_ret),
            K(server),
            K(follower_max_confirmed_log_id),
            "leader_max_log_id",
            sw_.get_max_log_id());
      } else if (!remote_replica_is_normal) {
        CLOG_LOG(INFO, "remote replica may be out_of_memory or disk is not enough", K_(partition_key), K(server));
      } else {
        valid_num++;
      }
    }
  }
  CLOG_LOG(INFO,
      "check_remote_mc_ts_",
      K_(partition_key),
      K(tmp_ret),
      K(valid_num),
      K(addr),
      K(member_list),
      K(mc_timestamp));
  return valid_num >= (mm_.get_replica_num() / 2);
}

int ObPartitionLogService::try_replay(const bool need_async, bool& is_replayed, bool& is_replay_failed)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    ret = sw_.submit_replay_task(need_async, is_replayed, is_replay_failed);
    if (!state_mgr_.can_slide_sw()) {
      if (partition_reach_time_interval(5 * 1000 * 1000, cannot_slide_sw_interval_)) {
        CLOG_LOG(WARN,
            "can_slide_sw return false",
            K_(partition_key),
            "role",
            state_mgr_.get_role(),
            "state",
            state_mgr_.get_state(),
            "is_replay_enabled",
            state_mgr_.is_replay_enabled());
      }
    }
  }
  return ret;
}

uint64_t ObPartitionLogService::get_last_slide_log_id()
{
  uint64_t last_slide_log_id = OB_INVALID_ID;
  RLockGuard guard(lock_);
  if (state_mgr_.is_offline()) {
    last_slide_log_id = OB_INVALID_ID;
  } else {
    last_slide_log_id = sw_.get_start_id() - 1;
  }
  return last_slide_log_id;
}

void ObPartitionLogService::get_last_replay_log(uint64_t& log_id, int64_t& ts)
{
  RLockGuard guard(lock_);
  sw_.get_last_replay_log(log_id, ts);
}

int64_t ObPartitionLogService::get_last_submit_timestamp() const
{
  RLockGuard guard(lock_);
  return sw_.get_last_submit_timestamp();
}

uint64_t ObPartitionLogService::get_start_log_id()
{
  RLockGuard guard(lock_);
  return sw_.get_start_id();
}

uint64_t ObPartitionLogService::get_start_log_id_unsafe()
{
  return sw_.get_start_id();
}

bool ObPartitionLogService::is_svr_in_member_list(const ObAddr& server) const
{
  bool bool_ret = false;
  if (IS_NOT_INIT) {
    CLOG_LOG(WARN, "not init", K_(partition_key), K_(is_inited));
  } else if (!server.is_valid()) {
    CLOG_LOG(WARN, "invalid arguments", K_(partition_key), K(server));
  } else {
    RLockGuard guard(lock_);
    bool_ret = mm_.get_curr_member_list().contains(server);
  }
  return bool_ret;
}

int ObPartitionLogService::get_dst_leader_candidate(common::ObMemberList& member_list) const
{
  int ret = OB_SUCCESS;
  member_list.reset();
  ObAddr addr;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!ObReplicaTypeCheck::is_paxos_replica_V2(mm_.get_replica_type())) {
    ret = OB_NOT_MASTER;
  } else {
    RLockGuard guard(lock_);
    ObMemberList elect_valid_candidates;
    if (OB_SUCCESS != (ret = state_mgr_.get_election_valid_candidates(elect_valid_candidates))) {
      CLOG_LOG(WARN, "get_election_valid_candidates failed", K_(partition_key), K(ret));
    } else {
      const common::ObMemberList& curr_member_list = mm_.get_curr_member_list();
      for (int64_t i = 0; i < curr_member_list.get_member_number() && (OB_SUCC(ret)); ++i) {
        addr.reset();
        if (OB_FAIL(curr_member_list.get_server_by_index(i, addr))) {
          CLOG_LOG(ERROR, "get_server_by_index failed", K(ret), K_(partition_key), K(curr_member_list), K(i));
        } else if (addr != self_ && elect_valid_candidates.contains(addr)) {
          if (OB_FAIL(member_list.add_server(addr))) {
            // should not be entry exist
            CLOG_LOG(ERROR, "member_list add_server failed", K(ret), K_(partition_key), K(member_list));
          }
        } else {
          // do nothing
        }
      }
    }
  }

  if (OB_SUCC(ret)) {
    RLockGuard guard(lock_);
    if (!state_mgr_.can_change_leader()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "can not change leader",
          K(ret),
          K_(partition_key),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          "membership_mgr is_state_init",
          mm_.is_state_init(),
          "is_changing_leader",
          state_mgr_.is_changing_leader(),
          K_(self));
    }
  }
  return ret;
}

int ObPartitionLogService::get_max_data_version(ObVersion& max_data_version) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    max_data_version = state_mgr_.get_freeze_version();
  }
  return ret;
}

int ObPartitionLogService::set_follower_active()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    CLOG_LOG(INFO, "set_follower_active", K_(partition_key));
    WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
    state_mgr_.set_follower_active();
  }
  return ret;
}

int ObPartitionLogService::get_log_id_range(
    uint64_t& start_log_id, int64_t& start_ts, uint64_t& end_log_id, int64_t& end_ts)
{
  int ret = OB_SUCCESS;
  common::ObTimeGuard timeguard("get_log_id_range", 1000 * 1000);
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    start_log_id = saved_base_storage_info_.get_last_replay_log_id();
    start_ts = saved_base_storage_info_.get_submit_timestamp();
    sw_.get_last_replay_log(end_log_id, end_ts);
  }
  CLOG_LOG(
      TRACE, "get log id range", K(ret), K_(partition_key), K(start_log_id), K(start_ts), K(end_log_id), K(end_ts));
  return ret;
}

int ObPartitionLogService::get_sw_max_log_id_info(uint64_t& sw_max_log_id, int64_t& sw_max_log_ts)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    sw_.get_max_log_id_info(sw_max_log_id, sw_max_log_ts);
  }
  CLOG_LOG(TRACE, "get sw max_log_id_info", K(ret), K_(partition_key), K(sw_max_log_id), K(sw_max_log_ts));
  return ret;
}

int ObPartitionLogService::get_sw_max_log_id(uint64_t& sw_max_log_id)
{
  int64_t sw_max_log_ts = OB_INVALID_TIMESTAMP;
  return get_sw_max_log_id_info(sw_max_log_id, sw_max_log_ts);
}

ObClogVirtualStat* ObPartitionLogService::alloc_clog_virtual_stat()
{
  int ret = OB_SUCCESS;
  ObClogVirtualStat* vitual_stat = NULL;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (NULL == (vitual_stat = op_reclaim_alloc(ObClogVirtualStat))) {
    CLOG_LOG(WARN, "alloc memory for vitual_stat failed", K(ret));
    ret = OB_ALLOCATE_MEMORY_FAILED;
  } else if (OB_FAIL(vitual_stat->init(self_, partition_key_, &state_mgr_, &sw_, &mm_, &cascading_mgr_, this))) {
    CLOG_LOG(WARN, "vitual_stat init failed", K(ret), K_(partition_key));
    op_reclaim_free(vitual_stat);
    vitual_stat = NULL;
  } else {
  }
  return vitual_stat;
}

int ObPartitionLogService::revert_clog_virtual_stat(ObClogVirtualStat* virtual_stat)
{
  int ret = OB_SUCCESS;
  if (NULL == virtual_stat) {
    ret = OB_INVALID_ARGUMENT;
  } else {
    op_reclaim_free(virtual_stat);
    virtual_stat = NULL;
  }
  return ret;
}

int ObPartitionLogService::set_member_list(const ObMemberList& member_list, const int64_t replica_num,
    const common::ObAddr& assigned_leader, const int64_t lease_start)
{
  // used for creating tenant in standby cluster or physical restoring
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!member_list.is_valid() || member_list.get_member_number() > replica_num) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K_(partition_key), K(member_list));
  } else {
    WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
    if (GCTX.is_standby_cluster()) {
      int tmp_ret = OB_SUCCESS;
      ObAddr cur_leader;
      int64_t cur_leader_epoch = -1;
      if (OB_SUCCESS == (tmp_ret = state_mgr_.get_election_leader(cur_leader, cur_leader_epoch)) &&
          member_list.contains(cur_leader)) {
        // leader is valid, no need set
        CLOG_LOG(INFO,
            "leader is valid and in member_list, no need set election leader",
            K(ret),
            K_(partition_key),
            K(cur_leader),
            K(cur_leader_epoch),
            K(member_list));
      } else if (assigned_leader.is_valid() && lease_start >= 0) {
        // There is currently no leader, first set leader and then set member_list to avoid election devoting a new
        // leader The lease_start value may have expired when the retry is performed, and a check will be made in the
        // election Therefore, the set election leader may fail when retrying, and tmp_ret is used here to avoid
        // blocking the set_member_list later
        if (OB_SUCCESS != (tmp_ret = state_mgr_.standby_set_election_leader(assigned_leader, lease_start))) {
          CLOG_LOG(WARN,
              "standby_set_election_leader failed",
              K(tmp_ret),
              K_(partition_key),
              K(assigned_leader),
              K(lease_start));
        } else {
          CLOG_LOG(INFO,
              "standby_set_election_leader success",
              K(ret),
              K_(partition_key),
              K(assigned_leader),
              K(lease_start));
        }
      } else {
        // assigned_leader is invalid, skip
        CLOG_LOG(WARN,
            "assigned_leader is invlaid",
            K(ret),
            K_(partition_key),
            K(member_list),
            K(assigned_leader),
            K(lease_start));
      }
5706 5707 5708 5709 5710 5711 5712 5713 5714
    } else {
      // assigned_leader maybe invalid in physical restoring scenario, skip
      CLOG_LOG(WARN,
          "assigned_leader is invlaid",
          K(ret),
          K_(partition_key),
          K(member_list),
          K(assigned_leader),
          K(lease_start));
O
oceanbase-admin 已提交
5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(mm_.force_set_member_list(member_list, replica_num))) {
        CLOG_LOG(WARN, "mm_.force_set_member_list failed", K(ret), K_(partition_key), K(member_list), K(replica_num));
      } else if (OB_FAIL(state_mgr_.start_election())) {
        CLOG_LOG(WARN, "start_election failed", K_(partition_key), K(ret));
      } else if (OB_FAIL(state_mgr_.set_scan_disk_log_finished())) {
        CLOG_LOG(WARN, "set_scan_disk_log_finished failed", K_(partition_key), K(ret));
      } else {
        // do nothing
      }
    }
  }
  CLOG_LOG(INFO, "set_member_list finished", K(ret), K_(partition_key), K(member_list), K(replica_num));
  return ret;
}

int ObPartitionLogService::check_if_start_log_task_empty(bool& is_empty)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "not inited", K(ret), K_(partition_key));
  } else if (OB_FAIL(sw_.check_left_bound_empty(is_empty))) {
    if (OB_EAGAIN == ret) {
      if (REACH_TIME_INTERVAL(1 * 1000 * 1000)) {
        CLOG_LOG(WARN, "failed to check_left_bound_empty", K(ret), K_(partition_key));
      }
    } else {
      CLOG_LOG(WARN, "failed to check_left_bound_empty", K(ret), K_(partition_key));
    }
  } else { /*do nothing*/
  }
  return ret;
}

int ObPartitionLogService::try_freeze_aggre_buffer()
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  ret = sw_.try_freeze_aggre_buffer();
  return ret;
}

int ObPartitionLogService::get_last_archived_log_id(
    const int64_t incarnation, const int64_t archive_round, uint64_t& last_archived_log_id)
{
  int ret = OB_SUCCESS;
  SpinRLockGuard rlock_guard(log_archive_status_rwlock_);
  if (incarnation != log_archive_status_.archive_incarnation_ ||
      archive_round != log_archive_status_.log_archive_round_) {
    ret = OB_EAGAIN;
    CLOG_LOG(WARN,
        "incarnation or archive round is not the same, need retry",
        K(ret),
        K_(partition_key),
        K(incarnation),
        K(archive_round),
        K(log_archive_status_));
  } else {
    last_archived_log_id = log_archive_status_.last_archived_log_id_;
  }
  return ret;
}

int ObPartitionLogService::force_set_as_single_replica()
{
  int ret = OB_SUCCESS;
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  ret = mm_.force_set_as_single_replica();
  return ret;
}

int ObPartitionLogService::force_set_replica_num(const int64_t replica_num)
{
  int ret = OB_SUCCESS;
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  ret = mm_.force_set_replica_num(replica_num);
  return ret;
}

int ObPartitionLogService::force_set_parent(const common::ObAddr& new_parent)
{
  int ret = OB_SUCCESS;
  if (!new_parent.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
  } else {
    WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
    const uint32_t reset_type = ObLogCascadingMgr::ResetPType::ADMIN_CMD;
    cascading_mgr_.reset_parent(reset_type);
    const int64_t dst_cluster_id = state_mgr_.get_leader_cluster_id();
    int64_t next_replay_log_ts = OB_INVALID_TIMESTAMP;
    if (OB_FAIL(sw_.get_next_replay_log_timestamp(next_replay_log_ts))) {
      CLOG_LOG(WARN, "sw get_next_replay_log_timestamp failed", K(ret), K_(partition_key));
    } else if (OB_FAIL(log_engine_->fetch_register_server(new_parent,
                   dst_cluster_id,
                   partition_key_,
                   state_mgr_.get_region(),
                   state_mgr_.get_idc(),
                   mm_.get_replica_type(),
                   next_replay_log_ts,
                   false,
                   false))) {
      CLOG_LOG(WARN, "fetch_register_server failed", K_(partition_key), K(ret));
    } else {
      // do nothing
    }
  }
  return ret;
}

int ObPartitionLogService::force_reset_parent()
{
  int ret = OB_SUCCESS;
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  const uint32_t reset_type = ObLogCascadingMgr::ResetPType::ADMIN_CMD;
  cascading_mgr_.reset_parent(reset_type);
  return ret;
}

int ObPartitionLogService::force_set_server_list(const obrpc::ObServerList& server_list, const int64_t replica_num)
{
  int ret = OB_SUCCESS;
  int64_t unused_epoch = 0;
  ObTsWindows unused_windows;
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  CLOG_LOG(INFO, "force_set_server_list", K(partition_key_), K(server_list), K(replica_num));
  if (OB_NOT_MASTER != (ret = get_role_unlock(unused_epoch, unused_windows))) {
    ret = OB_NOT_SUPPORTED;
    CLOG_LOG(WARN,
        "this replica is Leader, not allow to force_set_server_list",
        K(ret),
        K(partition_key_),
        K(server_list),
        K(replica_num));
  } else {
    ret = mm_.force_set_server_list(server_list, replica_num);
  }
  return ret;
}

int ObPartitionLogService::send_log_(const common::ObAddr& server, const int64_t cluster_id,
    const ObLogEntry& log_entry, const common::ObProposalID& proposal_id, const int64_t network_limit)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || (!proposal_id.is_valid() && !restore_mgr_.is_archive_restoring()) ||
             OB_ISNULL(log_engine_) || OB_ISNULL(alloc_mgr_)) {
    ret = OB_INVALID_ARGUMENT;
  } else {
    ObPushLogMode push_mode = PUSH_LOG_ASYNC;
    if (GCTX.need_sync_to_standby() && LEADER == state_mgr_.get_role() &&
        !ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id()) &&
        cascading_mgr_.get_sync_standby_child().get_server() == server) {
      // in max_protection mode, all logs that primary leader send to standby are set PUSH_LOG_SYNC
      push_mode = PUSH_LOG_SYNC;
    }

    int64_t pos = 0;
    char* serialize_buff = NULL;
    int64_t serialize_size = log_entry.get_serialize_size();
    if (NULL == (serialize_buff = static_cast<char*>(alloc_mgr_->ge_alloc(serialize_size)))) {
      CLOG_LOG(ERROR, "alloc failed", K_(partition_key));
      ret = OB_ALLOCATE_MEMORY_FAILED;
    } else if (OB_SUCCESS != (ret = log_entry.serialize(serialize_buff, serialize_size, pos))) {
      CLOG_LOG(WARN, "submit_log serialize failed", K_(partition_key), K(ret));
    } else {
      ObLogNetTask net_task(proposal_id, serialize_buff, serialize_size);
      if (OB_SUCCESS != (ret = log_engine_->submit_fetch_log_resp(
                             server, cluster_id, partition_key_, network_limit, push_mode, &net_task))) {
        CLOG_LOG(WARN, "submit_fetch_log_resp failed", K_(partition_key), K(ret), K(log_entry));
      } else {
        if (REACH_TIME_INTERVAL(100 * 1000)) {
          CLOG_LOG(INFO,
              "submit_fetch_log_resp succ",
              K(server),
              K(partition_key_),
              K(network_limit),
              "log_id",
              log_entry.get_header().get_log_id(),
              K(push_mode));
        }
      }
    }
    if (NULL != serialize_buff) {
      alloc_mgr_->ge_free(serialize_buff);
      serialize_buff = NULL;
    }
  }
  return ret;
}

int ObPartitionLogService::send_null_log_(const common::ObAddr& server, const int64_t cluster_id, const uint64_t log_id,
    const common::ObProposalID& proposal_id, const int64_t network_limit)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || log_id <= 0 || (!proposal_id.is_valid() && !restore_mgr_.is_archive_restoring())) {
    ret = OB_INVALID_ARGUMENT;
  } else {
    ObLogEntryHeader header;
    ObLogEntry log_entry;
    ObProposalID invalid_proposal_id;
    const bool is_trans_log = false;
    if (OB_SUCCESS != (ret = header.generate_header(OB_LOG_NOT_EXIST,  // log_type
                           partition_key_,                             // partition_key
                           log_id,                                     // log_id
                           NULL,                                       // buf
                           0,                                          // data_len
                           ObTimeUtility::current_time(),              // generation_timestamp
                           OB_INVALID_TIMESTAMP,                       // epoch_id
                           invalid_proposal_id,                        // proposal_id
                           OB_INVALID_TIMESTAMP,                       // submit_timestamp
                           ObVersion(),
                           is_trans_log))) {
      CLOG_LOG(WARN, "generate_header failed", K_(partition_key), K(ret));
    } else if (OB_SUCCESS != (ret = log_entry.generate_entry(header, NULL))) {
      CLOG_LOG(WARN, "generate_entry failed", K_(partition_key), K(ret));
    } else if (OB_SUCCESS != (ret = send_log_(server, cluster_id, log_entry, proposal_id, network_limit))) {
      CLOG_LOG(WARN, "submit_log failed", K_(partition_key), K(ret), K(log_entry), K(proposal_id));
    } else {
    }
  }
  return ret;
}

int ObPartitionLogService::send_confirm_info_(const common::ObAddr& server, const int64_t cluster_id,
    const ObLogEntry& log_entry, const int64_t accum_checksum, const bool batch_committed)
{
  int ret = OB_SUCCESS;
  ObCascadMemberList list;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || OB_ISNULL(log_engine_)) {
    ret = OB_INVALID_ARGUMENT;
  } else if (OB_FAIL(list.add_member(ObCascadMember(server, cluster_id)))) {
    CLOG_LOG(WARN, "add_member failed", K_(partition_key), K(ret), K(server));
  } else {
    const uint64_t log_id = log_entry.get_header().get_log_id();
    ObConfirmedInfo confirmed_info;
R
rl0 已提交
5958
    if (OB_SUCCESS != (ret = confirmed_info.init(log_entry.get_header().get_data_checksum(),
5959 5960 5961
                           log_entry.get_header().get_epoch_id(),
                           accum_checksum,
                           log_entry.get_header().get_submit_timestamp()))) {
O
oceanbase-admin 已提交
5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000
      CLOG_LOG(WARN, "confirmed_info init failed", K_(partition_key), K(ret));
    } else if (OB_SUCCESS != (ret = log_engine_->submit_confirmed_info(
                                  list, partition_key_, log_id, confirmed_info, batch_committed))) {
      CLOG_LOG(WARN,
          "log_engine submit_confirmed_info",
          K_(partition_key),
          K(ret),
          K(list),
          K(log_id),
          K(confirmed_info),
          K(batch_committed));
    }
  }
  return ret;
}

int ObPartitionLogService::try_update_next_replay_log_ts_in_restore(const int64_t new_ts)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (restore_mgr_.can_update_next_replay_log_ts_in_restore()) {
    sw_.try_update_next_replay_log_info_on_leader(new_ts);
  } else { /*dp nothing*/
  }
  return ret;
}

int ObPartitionLogService::leader_update_next_replay_log_ts_under_lock()
{
  int ret = OB_SUCCESS;
  // lock to avoid leader switch
  RLockGuard guard(lock_);
  const int64_t now = ObClockGenerator::getClock();
  bool is_cluster_status_allow_update = false;
  if (!ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id())) {
    if (GCTX.is_primary_cluster() || GCTX.is_in_flashback_state() || GCTX.is_in_cleanup_state()) {
      // non-private leader only in flashback/cleanup state can advance next_log_ts
L
LIN 已提交
6001
      // it cannot do this in switching state, which may lead conflict with new primary cluster
O
oceanbase-admin 已提交
6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162
      is_cluster_status_allow_update = true;
    } else {
      is_cluster_status_allow_update = false;
    }
  } else {
    // cluster private table, allow leader update
    is_cluster_status_allow_update = true;
  }
  if (is_cluster_status_allow_update) {
    if (OB_FAIL(leader_update_next_replay_log_ts_(now))) {
      CLOG_LOG(WARN, "leader_update_next_replay_log_ts_ failed", K(ret), K(partition_key_), K(now));
    }
  }
  return ret;
}

int ObPartitionLogService::get_next_served_log_info_for_leader(
    uint64_t& next_served_log_id, int64_t& next_served_log_ts)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    // try update next_log_ts firstly
    int tmp_ret = leader_update_next_replay_log_ts_under_lock();
    if (OB_SUCCESS != tmp_ret) {
      CLOG_LOG(WARN,
          "update next replay log ts fail when get_next_served_log_info_for_leader",
          K(tmp_ret),
          K(partition_key_));
    }

    ret = sw_.get_next_served_log_info_by_next_replay_log_info(next_served_log_id, next_served_log_ts);
  }
  return ret;
}

int ObPartitionLogService::get_pls_epoch(int64_t& pls_epoch) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_FAIL(state_mgr_.get_pls_epoch(pls_epoch))) {
    CLOG_LOG(WARN, "get_pls_epoch failed", K(partition_key_), K(ret));
  } else {
    // do_nothing
  }
  return ret;
}

int ObPartitionLogService::allow_gc(bool& allowed)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_ISNULL(log_engine_)) {
    ret = OB_ERR_UNEXPECTED;
  } else if (state_mgr_.is_pre_member_changing()) {
    CLOG_LOG(INFO, "this partition is member changing , not allow gc", K(partition_key_));
    allowed = false;
  } else {
    allowed = true;
  }
  return ret;
}

int ObPartitionLogService::notify_log_missing(
    const common::ObAddr& src_server, const uint64_t start_log_id, const bool is_in_member_list, const int32_t msg_type)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K_(partition_key), K(ret));
  } else if (OB_INVALID_MSG_TYPE == msg_type) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN,
        "invalid argument",
        K_(partition_key),
        K(ret),
        K(src_server),
        K(start_log_id),
        K(is_in_member_list),
        K(msg_type));
  } else if (OB_REBUILD_MSG == msg_type) {
    ATOMIC_STORE(&is_in_leader_member_list_, is_in_member_list);
    const common::ObAddr parent = cascading_mgr_.get_parent_addr();
    const common::ObReplicaType replica_type = mm_.get_replica_type();
    do {
      WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
      if (OB_FAIL(ret)) {
        CLOG_LOG(WARN, "notify_log_missing wlock failed", K(ret));
      } else if (start_log_id != sw_.get_start_id()) {
        ret = OB_STATE_NOT_MATCH;
        CLOG_LOG(INFO,
            "notify_log_missing, log id not match",
            K(ret),
            K_(partition_key),
            K(start_log_id),
            K(sw_.get_start_id()));
      } else if ((!ObReplicaTypeCheck::is_paxos_replica_V2(replica_type) && !parent.is_valid()) ||
                 (parent.is_valid() && src_server != parent)) {
        ret = OB_STATE_NOT_MATCH;
        CLOG_LOG(
            INFO, "notify_log_missing, parent not match", K_(partition_key), K(src_server), K(parent), K(replica_type));
      } else {
        // do nothing
      }
      // Set the need_rebuild state must be under write lock protection, and the above check conditions are passed
      // Otherwise, it is possible that after the judgment condition is passed, before the set_need_rebuild is executed,
      // the log is received, sliding_cb is executed, and the rebuild is no longer needed Need to ensure mutual
      // exclusion between set_need_rebuild and sliding_cb
      if (OB_SUCC(ret)) {
        state_mgr_.set_need_rebuild();
      }
    } while (0);

    if (OB_SUCC(ret)) {
      bool need_exec_rebuild = true;
      if (ObReplicaTypeCheck::is_paxos_replica_V2(replica_type) && !is_in_member_list) {
        if (GCTX.is_standby_cluster() && !ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id())) {
          // non-private paxos replica in standby cluster, can do rebuild
        } else if (restore_mgr_.is_archive_restoring()) {
          // physical_restoring replica can do rebuild
        } else {
          // other paxos replicas that not in member_list, cannot do rebuild
          need_exec_rebuild = false;
          CLOG_LOG(INFO,
              "self is paxos replica, but not in leader's member_list, no need exec rebuild",
              K(ret),
              K_(partition_key),
              K_(self),
              K(replica_type),
              K(src_server),
              K(is_in_member_list));
        }
      }
      if (!need_exec_rebuild) {
        // no need exec rebuild
      } else if (ATOMIC_LOAD(&last_rebuild_time_) == OB_INVALID_TIMESTAMP ||
                 ObTimeUtility::current_time() - ATOMIC_LOAD(&last_rebuild_time_) >= REBUILD_REPLICA_INTERVAL) {
        if (OB_FAIL(partition_service_->handle_log_missing(partition_key_, self_))) {
          CLOG_LOG(WARN, "handle_log_missing failed", K(ret), K_(partition_key), K_(self));
        } else {
          ATOMIC_STORE(&last_rebuild_time_, ObTimeUtility::current_time());
          CLOG_LOG(INFO, "handle_log_missing success", K(ret), K_(partition_key), K_(self));
        }
      }
    }
    CLOG_LOG(INFO,
        "handle_log_missing finished",
        K(ret),
        K_(partition_key),
        K_(self),
        K(src_server),
        K(is_in_member_list),
        K(start_log_id),
        K(replica_type));
  } else if (OB_STANDBY_RESTORE_MSG == msg_type) {
    RLockGuard guard(lock_);
    const common::ObReplicaType replica_type = mm_.get_replica_type();
    if (!restore_mgr_.is_standby_restore_state()) {
L
LIN 已提交
6163
      CLOG_LOG(INFO, "self is not in restore state, ignore msg", K_(partition_key), K(src_server));
O
oceanbase-admin 已提交
6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194
    } else if (FOLLOWER != state_mgr_.get_role()) {
      CLOG_LOG(WARN, "self is not follower, igore msg", K_(partition_key), K(src_server));
    } else if (ObReplicaTypeCheck::is_paxos_replica_V2(replica_type) && !is_in_member_list) {
      CLOG_LOG(INFO,
          "self is paxos replica, but not in standby_leader's member_list, cannot exec restore",
          K(ret),
          K_(partition_key),
          K_(self),
          K(replica_type),
          K(src_server),
          K(is_in_member_list));
    } else {
      share::ObCascadMember leader_member = state_mgr_.get_leader_member();
      if (leader_member.is_valid()) {
        if (OB_FAIL(try_exec_standby_restore_(leader_member))) {
          CLOG_LOG(WARN, "try_exec_standby_restore_ failed", K_(partition_key), K(ret));
        }
      }
    }
  } else {
    // do nothing
  }
  return ret;
}

int ObPartitionLogService::try_update_leader_from_loc_cache()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216
    bool is_self_strong_leader = false;
    do {
      // It need RDLock here, because state_mgr_ may update role concurrently.
      RLockGuard guard(lock_);
      if (LEADER == state_mgr_.get_role()) {
        is_self_strong_leader = true;
      }
    } while (0);

    if (is_self_strong_leader) {
      // strong leader no need check location cache
    } else {
      // WRLock is needed here, because staet_mgr_ may update leader_ member.
      WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
      if (OB_FAIL(ret)) {
        CLOG_LOG(WARN, "wrlock failed", K(ret), K(partition_key_));
      } else if (state_mgr_.try_update_leader_from_loc_cache()) {
        CLOG_LOG(WARN, "try_update_leader_from_loc_cache failed", K_(partition_key), K(ret));
      } else {
      }
    }
    CLOG_LOG(DEBUG, "try_update_leader_from_loc_cache", K_(partition_key), K(is_self_strong_leader));
O
oceanbase-admin 已提交
6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423
  }
  return ret;
}

bool ObPartitionLogService::is_self_lag_behind_(const int64_t arg_next_replay_log_ts)
{
  bool bool_ret = false;
  int tmp_ret = OB_SUCCESS;
  bool is_sync = false;
  if (OB_SUCCESS == (tmp_ret = is_in_sync(is_sync)) && is_sync) {
    // self sync
  } else {
    // self unsync, compare next_replay_log_ts
    int64_t self_next_replay_log_ts = OB_INVALID_TIMESTAMP;
    if (OB_SUCCESS != (tmp_ret = sw_.get_next_replay_log_timestamp(self_next_replay_log_ts))) {
      CLOG_LOG(WARN, "sw get_next_replay_log_timestamp failed", K(tmp_ret), K_(partition_key));
    } else if (OB_INVALID_TIMESTAMP != self_next_replay_log_ts && self_next_replay_log_ts <= arg_next_replay_log_ts) {
      bool_ret = true;
      if (REACH_TIME_INTERVAL(100 * 1000)) {
        CLOG_LOG(WARN,
            "is_self_lag_behind_ return true",
            K(self_next_replay_log_ts),
            K(arg_next_replay_log_ts),
            K_(partition_key));
      }
    } else {
      // do nothing
    }
  }
  return bool_ret;
}

int ObPartitionLogService::fetch_register_server(const common::ObAddr& server, const common::ObReplicaType replica_type,
    const int64_t next_replay_log_ts, const bool is_request_leader, const bool is_need_force_register,
    const ObRegion& region, const int64_t cluster_id, const ObIDC& idc)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || region.is_empty() || !ObReplicaTypeCheck::is_replica_type_valid(replica_type)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN,
        "invalid argument",
        K_(partition_key),
        K(ret),
        K(server),
        K(replica_type),
        K(next_replay_log_ts),
        K(is_request_leader),
        K(is_need_force_register),
        K(region),
        K(cluster_id));
  } else if (server == self_) {
    // ignore self
  } else {
    const bool is_self_lag_behind = is_self_lag_behind_(next_replay_log_ts);
    const common::ObRegion self_region = state_mgr_.get_region();
    // lock to avoid role change
    RLockGuard guard(lock_);
    if (is_request_leader && LEADER != state_mgr_.get_role() && STANDBY_LEADER != state_mgr_.get_role()) {
      // self is not leader, ignore
    } else if (OB_FAIL(cascading_mgr_.process_fetch_reg_server_req(server,
                   replica_type,
                   is_self_lag_behind,
                   is_request_leader,
                   is_need_force_register,
                   region,
                   cluster_id,
                   idc,
                   self_region))) {
      CLOG_LOG(WARN,
          "process_fetch_reg_server_req failed",
          K_(partition_key),
          K(ret),
          K(server),
          K(idc),
          K(region),
          K(next_replay_log_ts),
          K(is_request_leader),
          K(is_need_force_register),
          K(cluster_id),
          K(self_region));
    } else {
      // do nothing
    }
  }
  return ret;
}

int ObPartitionLogService::fetch_register_server_resp_v2(const common::ObAddr& sender,
    const bool is_assign_parent_succeed, const ObCascadMemberList& candidate_list, const int32_t msg_type)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!sender.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K_(partition_key), K(ret), K(is_assign_parent_succeed), K(candidate_list));
  } else {
    const common::ObRegion self_region = state_mgr_.get_region();
    RLockGuard guard(lock_);
    if (OB_FAIL(cascading_mgr_.process_fetch_reg_server_resp(
            sender, is_assign_parent_succeed, candidate_list, msg_type, self_region))) {
      CLOG_LOG(WARN,
          "process_fetch_reg_server_resp failed",
          K_(partition_key),
          K(ret),
          K(is_assign_parent_succeed),
          K(candidate_list),
          K(msg_type),
          K(self_region));
    }
  }
  return ret;
}

int ObPartitionLogService::replace_sick_child(
    const common::ObAddr& sender, const int64_t cluster_id, const common::ObAddr& sick_child)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!sender.is_valid() || !sick_child.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K_(partition_key), K(ret), K(sender), K(sick_child));
  } else {
    RLockGuard guard(lock_);
    if (OB_FAIL(cascading_mgr_.try_replace_sick_child(sender, cluster_id, sick_child))) {
      CLOG_LOG(WARN, "try_replace_sick_child failed", K_(partition_key), K(ret), K(sender), K(sick_child));
    }
  }
  return ret;
}

int ObPartitionLogService::get_curr_leader_and_memberlist(common::ObAddr& leader, common::ObRole& role,
    common::ObMemberList& curr_member_list, common::ObChildReplicaList& children_list) const
{
  int ret = OB_SUCCESS;
  leader.reset();
  curr_member_list.reset();
  children_list.reset();
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K_(partition_key), K(ret));
  } else if (restore_mgr_.is_archive_restoring()) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(WARN, "self is archive restoring, cannot handle this req", K_(partition_key), K(ret));
  } else {
    RLockGuard guard(lock_);
    leader = state_mgr_.get_leader();
    role = state_mgr_.get_role();

    if (OB_FAIL(curr_member_list.deep_copy(mm_.get_curr_member_list()))) {
      CLOG_LOG(WARN, "copy curr_member_list failed", K(ret), K_(partition_key));
    } else if (OB_FAIL(cascading_mgr_.get_lower_level_replica_list(children_list))) {
      CLOG_LOG(WARN, "fail to deep copy member list", K(ret));
    } else {
    }
  }
  return ret;
}

bool ObPartitionLogService::check_remote_mc_ts_(
    const ObMemberList& member_list, const ObAddr& addr, const int64_t mc_timestamp)
{
  const uint64_t FOLLOWER_MAX_GAP = 1000;
  return internal_check_remote_mc_ts_(member_list, addr, mc_timestamp, FOLLOWER_MAX_GAP);
}

bool ObPartitionLogService::check_remote_mc_ts_sync_(
    const ObMemberList& member_list, const ObAddr& addr, const int64_t mc_timestamp)
{
  const uint64_t FOLLOWER_MAX_GAP = 0;
  return internal_check_remote_mc_ts_(member_list, addr, mc_timestamp, FOLLOWER_MAX_GAP);
}

int ObPartitionLogService::wait_log_confirmed_()
{
  int ret = OB_LOG_NOT_SYNC;
  const int64_t begin_time = ObTimeUtility::current_time();
  const int64_t MAX_WAIT_TIME = 400 * 1000;  // 400 ms, 200 ms for rpc, 200 ms for flush_log
  const int64_t MIN_WAIT_TIME = 100 * 1000;  // 100 ms
  while (OB_LOG_NOT_SYNC == ret) {
    if (sw_.get_next_index_log_id() >= sw_.get_max_log_id() + 1) {
      ret = OB_SUCCESS;
    } else if (ObTimeUtility::current_time() - begin_time >= MAX_WAIT_TIME) {
      break;
    } else {
      usleep(100);
    }
  }
  const int64_t wait_time = ObTimeUtility::current_time() - begin_time;
  if (wait_time < MIN_WAIT_TIME && wait_time >= 0) {
    usleep(static_cast<useconds_t>(MIN_WAIT_TIME - wait_time));
  }
  return ret;
}

int64_t ObPartitionLogService::get_zone_priority() const
{
  return ATOMIC_LOAD(&zone_priority_);
}

void ObPartitionLogService::set_zone_priority(const uint64_t zone_priority)
{
Y
yy0 已提交
6424 6425 6426
  const uint64_t final_zone_priority =
      ObReplicaTypeCheck::is_log_replica(mm_.get_replica_type()) ? UINT64_MAX : zone_priority;
  ATOMIC_STORE(&zone_priority_, final_zone_priority);
O
oceanbase-admin 已提交
6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531
}

int ObPartitionLogService::set_region(const common::ObRegion& region)
{
  int ret = OB_SUCCESS;
  bool is_changed = false;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (region.is_empty()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K_(partition_key), K(ret), K(region));
  } else {
    common::ObRegion old_region = state_mgr_.get_region();
    if (region != old_region) {
      if (OB_FAIL(state_mgr_.set_region(region, is_changed))) {
        CLOG_LOG(WARN, "state_mgr set_region failed", K_(partition_key), K(ret));
      } else if (is_changed) {
        // self region changed, need notify children
        RLockGuard guard(lock_);
        int tmp_ret = OB_SUCCESS;
        if (OB_SUCCESS != (tmp_ret = cascading_mgr_.process_region_change(region))) {
          CLOG_LOG(WARN, "process_region_change failed", K_(partition_key), K(tmp_ret));
        }
        if (REACH_TIME_INTERVAL(50 * 1000)) {
          CLOG_LOG(
              INFO, "in set_region, region changed", K_(partition_key), "old_region", old_region, "new_region", region);
        }
      } else {
        // do nothing
      }
    }
  }
  return ret;
}

const common::ObRegion& ObPartitionLogService::get_region() const
{
  return state_mgr_.get_region();
}

int ObPartitionLogService::set_idc(const common::ObIDC& idc)
{
  int ret = OB_SUCCESS;
  bool is_changed = false;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (idc.is_empty()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K_(partition_key), K(ret), K(idc));
  } else {
    common::ObIDC old_idc = state_mgr_.get_idc();
    if (idc != old_idc) {
      if (OB_FAIL(state_mgr_.set_idc(idc, is_changed))) {
        CLOG_LOG(WARN, "state_mgr set_idc failed", K_(partition_key), K(ret));
      } else if (is_changed) {
        if (REACH_TIME_INTERVAL(50 * 1000)) {
          CLOG_LOG(INFO, "in set_idc, idc changed", K_(partition_key), "old_idc", old_idc, "new_idc", idc);
        }
      } else {
        // do nothing
      }
    }
  }
  return ret;
}

const common::ObIDC& ObPartitionLogService::get_idc() const
{
  return state_mgr_.get_idc();
}

int ObPartitionLogService::process_replica_type_change_()
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    if (ObReplicaTypeCheck::is_paxos_replica_V2(mm_.get_replica_type())) {
      // non_paxos->paxos change, need start election
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = state_mgr_.start_election())) {
        CLOG_LOG(WARN, "start_election failed", K_(partition_key), K(tmp_ret));
      }
      // replica_type is changed to paxos, it need check whether its parent is leader
      const common::ObAddr leader = state_mgr_.get_leader();
      if (leader.is_valid() && ObReplicaTypeCheck::is_paxos_replica_V2(mm_.get_replica_type()) && leader != self_) {
        // paxos replica set parent to leader directly
        const int64_t cluster_id = obrpc::ObRpcNetHandler::CLUSTER_ID;
        (void)cascading_mgr_.set_parent(leader, cluster_id);
      }
    } else {
      // changed to non-paxos type, stop election and reset parent
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = state_mgr_.stop_election())) {
        CLOG_LOG(WARN, "stop_election failed", K_(partition_key), K(tmp_ret));
      }
      const uint32_t reset_type = ObLogCascadingMgr::ResetPType::REPLICA_TYPE_CHANGE;
      cascading_mgr_.reset_parent(reset_type);
    }
  }
  return ret;
}

int ObPartitionLogService::process_reject_msg(
6532
    const common::ObAddr& server, const int64_t cluster_id, const int32_t msg_type, const int64_t send_ts)
O
oceanbase-admin 已提交
6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544
{
  int ret = OB_SUCCESS;
  const int64_t now = ObTimeUtility::current_time();

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!server.is_valid() || OB_REPLICA_MSG_TYPE_UNKNOWN == msg_type) {
    ret = OB_INVALID_ARGUMENT;
  } else if (OB_INVALID_TIMESTAMP != send_ts && now - send_ts > CASCAD_MSG_DELAY_TIME_THRESHOLD) {
    CLOG_LOG(WARN, "msg is expired", K(ret), K_(partition_key), K(server), K(msg_type), K(send_ts));
  } else {
    RLockGuard guard(lock_);
6545
    ret = cascading_mgr_.process_reject_msg(server, cluster_id, msg_type);
O
oceanbase-admin 已提交
6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711
  }
  return ret;
}

int ObPartitionLogService::process_restore_log_finish_msg(const common::ObAddr& src_server, const uint64_t log_id)
{
  int ret = OB_SUCCESS;
  bool is_restore_log_finished = false;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (log_id < sw_.get_start_id()) {
    CLOG_LOG(
        WARN, "invalid argument", K(ret), K_(partition_key), K(src_server), K(log_id), "start_id", sw_.get_start_id());
  } else if (restore_mgr_.is_restore_log_finished()) {
    // already finished, skip
    is_restore_log_finished = true;
  } else {
    restore_mgr_.set_fetch_log_result(OB_ARCHIVE_FETCH_LOG_FINISH_AND_ENOUGH);
  }
  CLOG_LOG(INFO,
      "recv restore log finished msg",
      K(ret),
      K_(partition_key),
      K(src_server),
      K(log_id),
      K(is_restore_log_finished));
  return ret;
}

// TODO: to be removed by haofan
int ObPartitionLogService::process_restore_alive_msg(const common::ObAddr& server, const uint64_t start_log_id)
{
  UNUSED(start_log_id);
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!restore_mgr_.is_archive_restoring()) {
    // not in restoring state, ignore
  } else if (restore_mgr_.get_restore_leader() != server) {
    // sender is not restore_leader, ignore
  } else {
    (void)restore_mgr_.record_leader_resp_time(server);
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(INFO, "recv restore alive msg", K(ret), K_(partition_key), K(server), K(start_log_id));
    }
  }

  return ret;
}

int ObPartitionLogService::process_restore_alive_req(
    const common::ObAddr& server, const int64_t dst_cluster_id, const int64_t send_ts)
{
  int ret = OB_SUCCESS;
  const int64_t now = ObTimeUtility::current_time();

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_INVALID_TIMESTAMP == send_ts) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K_(partition_key), K(server), K(send_ts));
  } else if (now - send_ts > CASCAD_MSG_DELAY_TIME_THRESHOLD) {
    CLOG_LOG(WARN, "msg is expired", K(ret), K_(partition_key), K(server), K(send_ts));
  } else if (!restore_mgr_.is_archive_restoring()) {
    // not in restoring state, ignore
    CLOG_LOG(DEBUG, "self is not in archive restoring state", K(ret), K_(partition_key), K(server), K(send_ts));
  } else if (restore_mgr_.get_role() != RESTORE_LEADER) {
    // self is not restore_leader, ignore
    CLOG_LOG(DEBUG, "self is not restore leader", K(ret), K_(partition_key), K(server), K(send_ts));
  } else if (OB_FAIL(log_engine_->send_restore_alive_resp(server, dst_cluster_id, partition_key_))) {
    CLOG_LOG(WARN, "send_restore_alive_resp failed", K(ret), K_(partition_key), K(server), K(send_ts));
  } else {
    if (REACH_TIME_INTERVAL(100 * 1000)) {
      CLOG_LOG(INFO, "send_restore_alive_resp success", K_(partition_key), K(server));
    }
  }

  return ret;
}

int ObPartitionLogService::process_restore_alive_resp(const common::ObAddr& server, const int64_t send_ts)
{
  int ret = OB_SUCCESS;
  const int64_t now = ObTimeUtility::current_time();

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_INVALID_TIMESTAMP == send_ts) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K_(partition_key), K(server), K(send_ts));
  } else if (now - send_ts > CASCAD_MSG_DELAY_TIME_THRESHOLD) {
    CLOG_LOG(WARN, "msg is expired", K(ret), K_(partition_key), K(server), K(send_ts));
  } else if (!restore_mgr_.is_archive_restoring()) {
    // not in restoring state, ignore
    CLOG_LOG(DEBUG, "self is not in archive restoring state", K(ret), K_(partition_key), K(server), K(send_ts));
  } else if (restore_mgr_.get_restore_leader() != server) {
    // sender is not restore_leader, ignore
    CLOG_LOG(DEBUG, "server is not restore leader", K(ret), K_(partition_key), K(server), K(send_ts));
  } else {
    (void)restore_mgr_.record_leader_resp_time(server);
  }

  return ret;
}

int ObPartitionLogService::process_reregister_msg(
    const common::ObAddr& src_server, const share::ObCascadMember& new_leader, const int64_t send_ts)
{
  int ret = OB_SUCCESS;
  const int64_t now = ObTimeUtility::current_time();

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (!new_leader.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K_(partition_key), K(src_server), K(new_leader));
  } else if (OB_INVALID_TIMESTAMP != send_ts && now - send_ts > CASCAD_MSG_DELAY_TIME_THRESHOLD) {
    CLOG_LOG(WARN, "msg is expired", K(ret), K_(partition_key), K(src_server), K(new_leader), K(send_ts));
  } else {
    RLockGuard guard(lock_);
    if (src_server != cascading_mgr_.get_parent_addr()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN, "sender is not my parent", K(ret), K_(partition_key));
    } else if (OB_FAIL(cascading_mgr_.process_reregister_msg(new_leader))) {
      CLOG_LOG(WARN, "process_reregister_msg failed", K(ret), K_(partition_key));
    } else {
      // do nothing
    }
  }
  return ret;
}

enum ObReplicaType ObPartitionLogService::get_replica_type()
{
  ObReplicaType replica_type = (ObReplicaType)mm_.get_replica_type();
  return replica_type;
}

int ObPartitionLogService::set_replica_type(const enum ObReplicaType replica_type)
{
  int ret = OB_SUCCESS;
  WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
  ObReplicaType old_replica_type = get_replica_type();
  if (!ObReplicaTypeCheck::is_replica_type_valid(replica_type)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid replica type", K(ret), K_(partition_key), K(replica_type));
  } else if (old_replica_type == replica_type) {
    CLOG_LOG(WARN,
        "replica type already equal, no need change",
        K(ret),
        K_(partition_key),
        K(replica_type),
        K(old_replica_type));
  } else if (!state_mgr_.can_set_replica_type()) {
    ret = OB_STATE_NOT_MATCH;
    CLOG_LOG(WARN, "cannot set_replica_type now", K(ret), K(partition_key_));
  } else if (REPLICA_TYPE_LOGONLY == replica_type && OB_FAIL(alloc_broadcast_info_mgr_(partition_key_))) {
    CLOG_LOG(WARN, "alloc_broadcast_info_mgr_ failed", K(ret), K(partition_key_));
  } else if (OB_FAIL(mm_.set_replica_type(replica_type))) {
    CLOG_LOG(WARN, "membership_mgr set_replica_type failed", K(ret), K_(partition_key), K(replica_type));
    if (REPLICA_TYPE_LOGONLY != mm_.get_replica_type()) {
      free_broadcast_info_mgr_();
    }
  } else {
Y
yy0 已提交
6712 6713 6714
    if (ObReplicaTypeCheck::is_log_replica(replica_type)) {
      ATOMIC_STORE(&zone_priority_, UINT64_MAX);
    }
O
oceanbase-admin 已提交
6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734
    // change success
    if (REPLICA_TYPE_LOGONLY == mm_.get_replica_type()) {
      sw_.destroy_aggre_buffer();
    } else {
      free_broadcast_info_mgr_();
    }
    int tmp_ret = OB_SUCCESS;
    if (OB_SUCCESS != (tmp_ret = process_replica_type_change_())) {
      CLOG_LOG(WARN, "process_replica_type_change_ failed", K_(partition_key), K(tmp_ret));
    }
  }
  return ret;
}

int ObPartitionLogService::check_is_normal_partition(bool& is_normal_partition) const
{
  int ret = OB_SUCCESS;
  bool is_out_of_memory = false;
  bool is_disk_not_enough = false;
  bool is_disk_error = false;
6735
  bool is_clog_disk_hang = false;
6736
  bool is_archive_restoring = false;
O
oceanbase-admin 已提交
6737 6738 6739 6740 6741 6742 6743 6744
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (OB_FAIL(replay_engine_->is_tenant_out_of_memory(partition_key_, is_out_of_memory))) {
    CLOG_LOG(ERROR, "is_tenant_out_of_memory failed", K(ret), K(partition_key_));
  } else if (OB_FAIL(ObIOManager::get_instance().is_disk_error(is_disk_error))) {
    CLOG_LOG(ERROR, "is_disk_error failed", K(ret), K(partition_key_));
  } else {
6745
    is_clog_disk_hang = log_engine_->is_clog_disk_hang();
O
oceanbase-admin 已提交
6746
    is_disk_not_enough = !log_engine_->is_disk_space_enough();
6747 6748 6749 6750
    // physical restoring replica cannot participate in member change.
    // because its election module has not been started, it cannot vote.
    is_archive_restoring = restore_mgr_.is_archive_restoring();
    is_normal_partition =
6751
        !(is_disk_not_enough || is_out_of_memory || is_disk_error || is_clog_disk_hang || is_archive_restoring);
O
oceanbase-admin 已提交
6752 6753 6754 6755
  }
  return ret;
}

L
LIN 已提交
6756
//==================== batch change member begin ====================
O
oceanbase-admin 已提交
6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111
int ObPartitionLogService::pre_change_member(const int64_t quorum, const bool is_add_member, int64_t& mc_timestamp,
    ObMemberList& member_list, ObProposalID& proposal_id)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (quorum <= 0 || quorum > OB_MAX_MEMBER_NUMBER) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K(partition_key_), K(quorum));
  } else {
    RLockGuard guard(lock_);
    if (!state_mgr_.can_change_member()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "can not change member",
          K(ret),
          K(partition_key_),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          "is_changing_leader",
          state_mgr_.is_changing_leader());
    } else {
      mc_timestamp = mm_.get_timestamp();
      bool is_standby_op = (STANDBY_LEADER == state_mgr_.get_role() ? true : false);
      if (!is_standby_op) {
        proposal_id = state_mgr_.get_proposal_id();
      } else {
        // standby non-private talbe's member change, use ms_proposal_id
        proposal_id = mm_.get_ms_proposal_id();
      }
      if (OB_SUCCESS != (ret = member_list.deep_copy(mm_.get_curr_member_list()))) {
        CLOG_LOG(WARN, "member_list deep_copy failed", K(ret), K(partition_key_));
      } else {
        const int64_t member_num = member_list.get_member_number();
        if (is_add_member && quorum < (member_num + 1)) {
          ret = OB_NOT_SUPPORTED;
          CLOG_LOG(
              INFO, "not supported operation", K(ret), K(partition_key_), K(is_add_member), K(quorum), K(member_num));
        } else if (!is_add_member && quorum < (member_num - 1)) {
          ret = OB_NOT_SUPPORTED;
          CLOG_LOG(
              INFO, "not supported operation", K(ret), K(partition_key_), K(is_add_member), K(quorum), K(member_num));
        } else {
          // do nothing
        }
      }
    }
  }
  return ret;
}

int ObPartitionLogService::set_member_changing()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else {
    WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
    if (OB_FAIL(ret)) {
      CLOG_LOG(WARN, "set_member_changing wrlock failed", K(ret), K(partition_key_));
    } else if (state_mgr_.is_pre_member_changing()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN, "there is another member change operation in process", K(ret), K(partition_key_));
    } else {
      (void)state_mgr_.set_pre_member_changing();
    }
  }
  return ret;
}

int ObPartitionLogService::reset_member_changing()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else {
    WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
    if (!state_mgr_.is_pre_member_changing()) {
      // Maybe this member change failed in the pre-checking stage and did not enter the pre_member_changing state
      // So it is possible to come here
      CLOG_LOG(WARN, "wrong member changing state, maybe mc forward check failed", K(ret), K(partition_key_));
    } else {
      state_mgr_.reset_pre_member_changing();
    }
  }
  return ret;
}

int ObPartitionLogService::wait_log_confirmed(const int64_t begin_time, const int64_t max_wait_time)
{
  int ret = OB_LOG_NOT_SYNC;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else {
    while (OB_LOG_NOT_SYNC == ret) {
      if (sw_.get_next_index_log_id() >= sw_.get_max_log_id() + 1) {
        ret = OB_SUCCESS;
      } else if (ObClockGenerator::getClock() - begin_time >= max_wait_time) {
        break;
      } else {
        usleep(100);
      }
    }
  }
  return ret;
}

int ObPartitionLogService::batch_change_member(const common::ObMember& member, const int64_t quorum,
    const int64_t mc_timestamp, const common::ObProposalID& proposal_id, const bool is_add_member,
    obrpc::ObMCLogInfo& mc_log_info)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (!member.is_valid() || mc_timestamp <= 0 || !proposal_id.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K(partition_key_), K(member), K(mc_timestamp), K(proposal_id));
  } else {
    WLockGuardWithRetry guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time());
    bool is_standby_op = (STANDBY_LEADER == state_mgr_.get_role() ? true : false);
    if (!state_mgr_.can_change_member()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "can not change member",
          K(ret),
          K(partition_key_),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          "is_changing_leader",
          state_mgr_.is_changing_leader());
    } else if (mc_timestamp != mm_.get_timestamp()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "version not match",
          K(ret),
          K(partition_key_),
          K(member),
          K(is_add_member),
          K(mc_timestamp),
          "current_timestamp",
          mm_.get_timestamp());
    } else if (!is_standby_op && proposal_id != state_mgr_.get_proposal_id()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "proposal_id not match",
          K(ret),
          K(partition_key_),
          K(proposal_id),
          "current_proposal_id",
          state_mgr_.get_proposal_id());
    } else if (is_standby_op && proposal_id != mm_.get_ms_proposal_id()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "ms_proposal_id not match",
          K(ret),
          K(partition_key_),
          K(proposal_id),
          "current_proposal_id",
          mm_.get_ms_proposal_id());
    } else if (is_add_member) {
      if (OB_FAIL(mm_.add_member(member, quorum, mc_log_info))) {
        CLOG_LOG(WARN, "add_member failed", K(ret), K(partition_key_), K(member), K(quorum));
      }
    } else {
      if (OB_FAIL(mm_.remove_member(member, quorum, mc_log_info))) {
        CLOG_LOG(WARN, "remove_member failed", K(ret), K(partition_key_), K(member), K(quorum));
      }
    }
  }
  return ret;
}

int ObPartitionLogService::get_partition_max_log_id(uint64_t& partition_max_log_id)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "is_not_inited", K(ret), K(partition_key_));
  } else {
    partition_max_log_id = sw_.get_max_log_id();
  }
  return ret;
}

//==================== batch change member end ====================

bool ObPartitionLogService::need_skip_when_check_start_service() const
{
  return (is_paxos_offline_replica_() || is_no_update_next_replay_log_id_info_too_long_() ||
          restore_mgr_.is_archive_restoring());
}

bool ObPartitionLogService::is_no_update_next_replay_log_id_info_too_long_() const
{
  const int64_t TOO_LONG_TIME = 10 * 60 * 1000 * 1000;  // 10 minutes
  const int64_t last_update_next_replay_log_id_info_ts = sw_.get_last_update_next_replay_log_id_info_ts();
  return (last_update_next_replay_log_id_info_ts != OB_INVALID_TIMESTAMP) &&
         (ObTimeUtility::current_time() - last_update_next_replay_log_id_info_ts >= TOO_LONG_TIME);
}

int ObPartitionLogService::leader_update_next_replay_log_ts_(const int64_t new_ts)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_INVALID_TIMESTAMP == new_ts) {
    ret = OB_INVALID_ARGUMENT;
  } else if (state_mgr_.is_offline() || !state_mgr_.is_leader_active()) {
    ret = OB_STATE_NOT_MATCH;
  } else if (OB_FAIL(check_self_is_election_leader_())) {
    CLOG_LOG(WARN, "check_self_is_election_leader_ failed", K_(partition_key), K(ret));
  } else {
    sw_.try_update_next_replay_log_info_on_leader(new_ts);
  }

  return ret;
}

int ObPartitionLogService::leader_keepalive(const int64_t keepalive_interval)
{
  int ret = OB_SUCCESS;
  const int64_t now = ObClockGenerator::getClock();
  bool need_send_max_log_info = false;  // whether leader need send max_log_info to standby cluster

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else {
    // hold rdlock during keepAlive
    RLockGuard guard(lock_);
    if (state_mgr_.is_leader_active() && !state_mgr_.is_offline()) {
      bool is_cluster_status_allow_update = false;
      if (!ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id())) {
        if (GCTX.is_primary_cluster() || GCTX.is_in_flashback_state() || GCTX.is_in_cleanup_state()) {
          // non-private leader only in flashback/cleanup state can advance next_log_ts
          // it cannot do this in swithing state, which may lead conflict with new primary cluster
          is_cluster_status_allow_update = true;
        } else {
          is_cluster_status_allow_update = false;
          if (GCTX.is_in_standby_switching_state()) {
            // leader need send max_log info to standby replica during switching state
            need_send_max_log_info = true;
          }
        }
      } else {
        // cluster private table, allow leader update
        is_cluster_status_allow_update = true;
      }

      if (sw_.is_empty() && now - last_leader_keepalive_time_ < keepalive_interval) {
        // When the sliding window is empty, it will be sent every 100ms
      } else if (!sw_.is_empty() &&
                 (ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id()) ||
                     !GCTX.is_in_standby_switching_state()) &&
                 now - last_leader_keepalive_time_ < 10 * CLOG_LEADER_KEEPALIVE_INTERVAL) {
        // When the sliding window is not empty and it is private table or non-switching state,  it will be sent every
        // 1s
      } else {
        if (is_cluster_status_allow_update) {
          // advance next_log_ts
          const int64_t new_next_log_ts = now;
          if (OB_FAIL(leader_update_next_replay_log_ts_(new_next_log_ts))) {
            CLOG_LOG(WARN, "leader_update_next_replay_log_ts_ failed", K_(partition_key), K(ret));
          }
        }

        if (OB_SUCC(ret)) {
          uint64_t next_log_id = OB_INVALID_ID;
          int64_t next_log_ts = OB_INVALID_TIMESTAMP;
          sw_.get_next_replay_log_id_info(next_log_id, next_log_ts);

          // send to curr_member_list and children_list
          const int64_t self_cluster_id = obrpc::ObRpcNetHandler::CLUSTER_ID;
          const common::ObMemberList member_list = mm_.get_curr_member_list();
          const int64_t member_cnt = member_list.get_member_number();
          for (int64_t i = 0; i < member_cnt && OB_SUCC(ret); ++i) {
            common::ObAddr server;
            if (OB_FAIL(member_list.get_server_by_index(i, server))) {
              CLOG_LOG(WARN, "get_server_by_index failed", K(ret), K_(partition_key), K(server));
            } else if (server == self_) {
              // skip self
            } else if (OB_FAIL(log_engine_->send_keepalive_msg(
                           server, self_cluster_id, partition_key_, next_log_id, next_log_ts, 0 /* deliver_cnt */))) {
              CLOG_LOG(WARN, "send_keepalive_msg failed", K(ret), K_(partition_key), K(server));
            } else {
              // do nothing
            }
          }

          if (OB_SUCC(ret)) {
            ObCascadMember sync_child = cascading_mgr_.get_sync_standby_child();
            if (sync_child.is_valid() && OB_FAIL(log_engine_->send_keepalive_msg(sync_child.get_server(),
                                             sync_child.get_cluster_id(),
                                             partition_key_,
                                             next_log_id,
                                             next_log_ts,
                                             0 /* deliver_cnt */))) {
              CLOG_LOG(WARN, "send_keepalive_msg failed", K(ret), K_(partition_key), K(sync_child));
            }
          }

          if (OB_SUCC(ret)) {
            if (OB_FAIL(send_keepalive_msg_to_children_(next_log_id, next_log_ts, 0))) {
              CLOG_LOG(WARN, "send_keepalive_msg_to_children_ failed", K(ret), K_(partition_key));
            }
          }
        }

        if (OB_SUCC(ret)) {
          last_leader_keepalive_time_ = now;
        }
      }
    }
  }
  if (OB_SUCC(ret) && need_send_max_log_info) {
    // send max_log info to standby replica, which is used to check sync state
    int tmp_ret = OB_SUCCESS;
    if (OB_SUCCESS != (tmp_ret = leader_send_max_log_info())) {
      CLOG_LOG(WARN, "leader_send_max_log_info failed", K_(partition_key), K(tmp_ret));
    }
  }
  return ret;
}

// When primary cluster is switched to the switching state, the leader actively sends relevant information to the
// standby cluster, and the standby replica use this information to check whether the synchronization is completed
int ObPartitionLogService::leader_send_max_log_info()
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (!GCTX.is_in_standby_switching_state()) {
    // cluster not in switching state, skip
  } else if (ObMultiClusterUtil::is_cluster_private_table(partition_key_.get_table_id())) {
    // private table no need execute
  } else {
    bool is_online_leader = false;
    uint64_t max_log_id = OB_INVALID_ID;
    uint64_t next_log_id = OB_INVALID_ID;
    int64_t next_log_ts = OB_INVALID_TIMESTAMP;
    {
      RLockGuard guard(lock_);
      if (!state_mgr_.is_leader_active() || state_mgr_.is_offline()) {
L
LIN 已提交
7112
        // self is follower or offline, skip
O
oceanbase-admin 已提交
7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364
        is_online_leader = false;
      } else {
        is_online_leader = true;
      }
    }
    if (OB_SUCC(ret) && is_online_leader) {
      // Add a write lock to obtain timestamp.
      // The write lock can ensure that the value obtained is the last advance value in the switching state.
      // Because pushing the standby machine to read the timestamp is performed under the read lock.
      WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
      sw_.get_next_replay_log_id_info(next_log_id, next_log_ts);
      // The write lock of pls should also be added when obtaining max_log_id, which is used for mutual exclusion with
      // alloc_log_id Add a write lock to ensure that you can get the last allocated log_id
      max_log_id = sw_.get_max_log_id();
    }
    // double check self is leader
    {
      RLockGuard guard(lock_);
      const int64_t switchover_epoch = GCTX.get_pure_switch_epoch();
      if (OB_SUCC(ret) && is_online_leader && (state_mgr_.is_leader_active() && !state_mgr_.is_offline())) {
        if (OB_FAIL(check_self_is_election_leader_())) {
          CLOG_LOG(WARN, "check_self_is_election_leader_ failed", K_(partition_key), K(ret));
        } else if (OB_INVALID_ID == max_log_id) {
          ret = OB_ERR_UNEXPECTED;
          CLOG_LOG(ERROR, "max_log_id is invalid", K_(partition_key), K(ret), K(max_log_id));
        } else if (OB_FAIL(send_max_log_msg_to_mlist_(switchover_epoch, max_log_id, next_log_ts))) {
          CLOG_LOG(WARN, "send_max_log_msg_to_mlist_ failed", K(ret), K_(partition_key));
        } else if (OB_FAIL(send_max_log_msg_to_children_(switchover_epoch, max_log_id, next_log_ts))) {
          CLOG_LOG(WARN, "send_max_log_msg_to_children_ failed", K(ret), K_(partition_key));
        } else {
          // do nothing
        }
        if (partition_reach_time_interval(5 * 1000 * 1000, leader_max_log_interval_)) {
          CLOG_LOG(INFO,
              "leader_send_max_log_info finished",
              K(ret),
              K_(partition_key),
              K(switchover_epoch),
              K(max_log_id),
              K(next_log_ts));
        }
      }
    }
  }
  return ret;
}

int ObPartitionLogService::broadcast_info()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "is not inited", K(ret), K(partition_key_));
  } else if (GET_MIN_CLUSTER_VERSION() >= CLUSTER_VERSION_2260 || GCTX.is_primary_cluster()) {
    // Before the 226 version, the standby cluster did not have its own member_list,
    // and the logonly replica of the standby cluster can directly reclaim the logs according to the dump location,
    // so there is no need to broadcast
    // Starting from version 226, the standby cluster has member_list, which can be like the primary cluster
    RLockGuard guard(lock_);
    const ObMemberList member_list = mm_.get_curr_member_list();
    const ObReplicaType replica_type = mm_.get_replica_type();
    const uint64_t max_confirmed_log_id = sw_.get_max_confirmed_log_id();

    if (ObReplicaTypeCheck::is_paxos_replica_V2(replica_type) &&
        member_list.is_valid()  // standby replica's member_list maybe invalid
        && OB_FAIL(log_engine_->broadcast_info(member_list, partition_key_, replica_type, max_confirmed_log_id))) {
      CLOG_LOG(WARN,
          "broadcast_info failed",
          K(ret),
          K(partition_key_),
          K(member_list),
          K(replica_type),
          K(max_confirmed_log_id));
    }
  } else {
    // do nothing
  }
  return ret;
}

int ObPartitionLogService::archive_checkpoint(const int64_t archive_checkpoint_interval)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (OB_SYS_TENANT_ID == partition_key_.get_tenant_id()) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "sys partitions do not need to archive checkpoint", K(ret), K(partition_key_));
  } else {
    RLockGuard guard(lock_);
    const int64_t now = ObClockGenerator::getClock();
    int64_t last_archive_checkpoint_ts = sw_.get_last_update_archive_checkpoint_time();
    if (((now - last_archive_checkpoint_ts) >= archive_checkpoint_interval) && GCTX.is_primary_cluster() &&
        state_mgr_.is_leader_active() && !state_mgr_.is_offline()) {
      storage::ObIPartitionGroupGuard guard;
      int64_t checkpoint_ts = OB_INVALID_TIMESTAMP;
      // The error is generally that the archive mgr has not been initialized or the sending queue is full.
      // Wait a while and try again to avoid retrying too frequently.
      const int64_t max_delay_interval = 2 * 1000 * 1000LL;
      const int64_t wait_interval = std::min((archive_checkpoint_interval / 4), max_delay_interval);
      int64_t last_checkpoint_ts = sw_.get_last_archive_checkpoint_ts();
      ObPartitionLoopWorker* loop_worker = NULL;
      if ((OB_FAIL(partition_service_->get_partition(partition_key_, guard))) || NULL == guard.get_partition_group()) {
        CLOG_LOG(WARN, "invalid partition", K(ret), KP(partition_service_), K_(partition_key));
      } else if (!(guard.get_partition_group()->is_valid())) {
        ret = OB_INVALID_PARTITION;
        CLOG_LOG(WARN, "partition is invalid", K(ret), K_(partition_key));
      } else if (NULL == (loop_worker = guard.get_partition_group()->get_partition_loop_worker())) {
        ret = OB_INVALID_PARTITION;
        CLOG_LOG(WARN, "partition is invalid", K(ret), K_(partition_key));
      } else if (FALSE_IT(checkpoint_ts = loop_worker->get_last_checkpoint())) {
        CLOG_LOG(WARN, "failed to get_last_checkpoint", K(ret), K_(partition_key));
      } else if (0 >= checkpoint_ts) {
        if (REACH_TIME_INTERVAL(1 * 1000 * 1000)) {
          CLOG_LOG(WARN, "checkpoint_ts has not been generated", KR(ret), K(partition_key_), K(checkpoint_ts));
        }
        sw_.set_last_update_archive_checkpoint_time(last_archive_checkpoint_ts + wait_interval);
      } else if (checkpoint_ts > last_checkpoint_ts) {
        uint64_t max_log_id = OB_INVALID_ID;
        int64_t max_log_ts = OB_INVALID_TIMESTAMP;
        sw_.get_max_log_id_info(max_log_id, max_log_ts);
        if (sw_.is_empty() && max_log_id == sw_.get_last_archive_checkpoint_log_id()) {
          if (OB_FAIL(archive_mgr_->submit_checkpoint_task(partition_key_, max_log_id, max_log_ts, checkpoint_ts))) {
            if (OB_ENTRY_NOT_EXIST == ret || OB_LOG_ARCHIVE_NOT_RUNNING == ret || OB_EAGAIN == ret) {
              if (REACH_TIME_INTERVAL(1 * 1000 * 1000)) {
                CLOG_LOG(WARN, "failed to submit_checkpoint_task", KR(ret), K(partition_key_));
              }
            } else {
              CLOG_LOG(WARN, "failed to submit_checkpoint_task", KR(ret), K(partition_key_));
            }
            sw_.set_last_update_archive_checkpoint_time(last_archive_checkpoint_ts + wait_interval);
          } else {
            sw_.set_last_archive_checkpoint_ts(checkpoint_ts);
            sw_.set_last_update_archive_checkpoint_time(now);
          }
        } else {
          // need submit clog checkpoint log
          loop_worker->set_force_write_checkpoint(true);
          sw_.set_last_archive_checkpoint_log_id(max_log_id + 1);
          sw_.set_last_archive_checkpoint_ts(checkpoint_ts);
          sw_.set_last_update_archive_checkpoint_time(now);
        }
      } else {
        if (REACH_TIME_INTERVAL(5 * 1000 * 1000)) {
          CLOG_LOG(WARN,
              "checkpoint has not been pushed up",
              KR(ret),
              K(partition_key_),
              K(last_checkpoint_ts),
              K(checkpoint_ts));
        }
      }
    } else {
    }
  }
  return ret;
}

int ObPartitionLogService::sync_log_archive_progress()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (OB_SYS_TENANT_ID == partition_key_.get_tenant_id()) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "sys partitions do not need to sys log archive progress", K(ret), K(partition_key_));
  } else {
    RLockGuard guard(lock_);
    if (GCTX.is_primary_cluster() && state_mgr_.is_leader_active() && !state_mgr_.is_offline()) {
      bool need_sync = true;
      if (OB_FAIL(sync_local_archive_progress_(need_sync))) {
        CLOG_LOG(WARN, "sync_local_archive_progress_ failed", K(ret), K_(partition_key));
      } else if (need_sync) {
        const int64_t self_cluster_id = obrpc::ObRpcNetHandler::CLUSTER_ID;
        const common::ObMemberList member_list = mm_.get_curr_member_list();
        const int64_t member_cnt = member_list.get_member_number();
        for (int64_t i = 0; i < member_cnt && OB_SUCC(ret); ++i) {
          common::ObAddr server;
          if (OB_FAIL(member_list.get_server_by_index(i, server))) {
            CLOG_LOG(WARN, "sync_local_archive_progress_ failed", K(ret), K_(partition_key), K(server));
          } else if (server == self_) {
            // skip self
          } else if (OB_FAIL(log_engine_->send_sync_log_archive_progress_msg(
                         server, self_cluster_id, partition_key_, log_archive_status_))) {
            CLOG_LOG(WARN, "send_sync_log_archive_progress_msg failed", K(ret), K_(partition_key), K(server));
          } else { /*do nothing*/
          }
        }

        int tmp_ret = OB_SUCCESS;
        if (OB_SUCCESS != (tmp_ret = (send_archive_progress_msg_to_children_(log_archive_status_)))) {
          CLOG_LOG(WARN,
              "failed to send_archive_progress_msg_to_children_",
              K(tmp_ret),
              K_(partition_key),
              K(log_archive_status_));
          ret = (OB_SUCCESS == ret) ? tmp_ret : ret;
        }
      }
    }
  }
  return ret;
}

int ObPartitionLogService::send_archive_progress_msg_to_children_(const ObPGLogArchiveStatus& status) const
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    ObCascadMemberList children;
    if (OB_FAIL(cascading_mgr_.get_children_list(children))) {
      CLOG_LOG(WARN, "get_children_list failed", K_(partition_key), K(ret));
    } else {
      const int64_t child_cnt = children.get_member_number();
      int tmp_ret = OB_SUCCESS;
      for (int64_t i = 0; i < child_cnt; ++i) {
        share::ObCascadMember child;
        if (OB_SUCCESS != (tmp_ret = (children.get_member_by_index(i, child)))) {
          CLOG_LOG(WARN, "get_member_by_index failed", K(tmp_ret), K_(partition_key), K(child));
        } else if (!child.is_valid()) {
          tmp_ret = OB_ERR_UNEXPECTED;
          CLOG_LOG(WARN, "invalid child", K(tmp_ret), K_(partition_key), K(child));
        } else if (child.get_server() == self_) {
          // skip self
        } else if (OB_SUCCESS != (tmp_ret = (log_engine_->send_sync_log_archive_progress_msg(
                                      child.get_server(), child.get_cluster_id(), partition_key_, status)))) {
          CLOG_LOG(WARN, "send_sync_log_archive_progress_msg failed", K(tmp_ret), K_(partition_key), K(child));
        } else {
          // do nothing
        }
        ret = (OB_SUCCESS == ret) ? tmp_ret : ret;
      }
    }
  }
  return ret;
}

int ObPartitionLogService::get_log_archive_status(ObPGLogArchiveStatus& status)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "is not inited", K(ret), K(partition_key_));
  } else {
    SpinRLockGuard rlock_guard(log_archive_status_rwlock_);
    status = log_archive_status_;
  }
  return ret;
}
7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382

int ObPartitionLogService::check_log_exist(const uint64_t log_id, bool &exist)
{
  int ret = OB_SUCCESS;
  ObLogCursorExt log_cursor;
  exist = false;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "is not inited", K(ret), K(partition_key_));
  } else if (OB_UNLIKELY(OB_INVALID_ID == log_id)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid argument", K(ret), K(log_id), K(partition_key_));
  } else if (OB_FAIL(log_engine_->check_clog_exist(partition_key_, log_id, exist))) {
    CLOG_LOG(WARN, "check clog exist fail", K(ret), K(log_id), K(partition_key_));
  }
  return ret;
}

O
oceanbase-admin 已提交
7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281
int ObPartitionLogService::update_broadcast_info(
    const common::ObAddr& server, const common::ObReplicaType& replica_type, const uint64_t max_confirmed_log_id)
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "is not inited", K(ret), K(partition_key_));
  } else if (NULL == broadcast_info_mgr_) {
    // do nothing
  } else if (OB_FAIL(broadcast_info_mgr_->update_broadcast_info(server, replica_type, max_confirmed_log_id))) {
    CLOG_LOG(WARN,
        "update_broadcast_info failed",
        K(ret),
        K(partition_key_),
        K(server),
        K(replica_type),
        K(max_confirmed_log_id));
  }
  return ret;
}

int ObPartitionLogService::get_recyclable_log_id(uint64_t& log_id) const
{
  int ret = OB_SUCCESS;
  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "is not inited", K(ret), K(partition_key_));
  } else if (NULL == broadcast_info_mgr_) {
    log_id = 0;
  } else if (OB_FAIL(broadcast_info_mgr_->get_recyclable_log_id(log_id))) {
    CLOG_LOG(WARN, "get_recyclable_log_id failed", K(ret), K(partition_key_));
  }
  return ret;
}

int ObPartitionLogService::get_candidate_info(ObCandidateInfo& info)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    info.set_role(state_mgr_.get_role());
    info.set_tenant_active(partition_service_->is_tenant_active(partition_key_));
    info.set_on_revoke_blacklist(state_mgr_.is_leader_revoke_recently());
    info.set_replica_type(mm_.get_replica_type());
    info.set_server_status(OBSERVER.get_gctx().rs_server_status_);
    info.set_clog_disk_full(!log_engine_->is_disk_space_enough());
    info.set_offline(state_mgr_.is_offline());
    info.set_need_rebuild(state_mgr_.is_need_rebuild());
    info.set_is_partition_candidate(partition_service_->is_election_candidate(partition_key_));
    bool disk_error = false;
    int tmp_ret = OB_SUCCESS;
    if (OB_SUCCESS != (tmp_ret = ObIOManager::get_instance().is_disk_error(disk_error))) {
      CLOG_LOG(WARN, "is_disk_error failed", K(tmp_ret));
    }
    info.set_is_disk_error(disk_error);
    const ObReplicaProperty replica_property = mm_.get_replica_property();
    info.set_memstore_percent(replica_property.get_memstore_percent());
  }
  return ret;
}

int ObPartitionLogService::enable_replay_()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    CLOG_LOG(INFO, "enable_replay", K_(partition_key));
    state_mgr_.enable_replay();
  }
  return ret;
}

int ObPartitionLogService::disable_replay_()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    CLOG_LOG(INFO, "disable_replay", K_(partition_key));
    state_mgr_.disable_replay();
  }
  return ret;
}

int ObPartitionLogService::get_storage_last_replay_log_id_(uint64_t& last_replay_log_id) const
{
  int ret = OB_SUCCESS;
  storage::ObIPartitionGroupGuard guard;
  storage::ObIPartitionGroup* partition = NULL;
  int64_t unused = 0;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (OB_FAIL(partition_service_->get_partition(partition_key_, guard)) ||
             NULL == (partition = guard.get_partition_group())) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(WARN, "this partition not existed", K(ret), K(partition_key_));
  } else if (OB_FAIL(partition->get_saved_last_log_info(last_replay_log_id, unused))) {
    CLOG_LOG(ERROR, "partition get_last_replay_log_id failed", K(ret), K(partition_key_));
  } else if (OB_INVALID_ID == last_replay_log_id) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "last_replay_log_id is invalid", K(ret), K(partition_key_));
  } else {
    // do nothing
  }
  return ret;
}

int ObPartitionLogService::query_confirmed_log(
    const uint64_t log_id, transaction::ObTransID& trans_id, int64_t& submit_timestamp)
{
  int ret = OB_SUCCESS;

  ObTsWindows unused_windows;
  int64_t leader_epoch1 = 0;
  int64_t leader_epoch2 = 0;
  ObLogEntry log_entry;
  common::ObMemberList member_list;
  ObReadBufGuard guard(ObModIds::OB_LOG_DIRECT_READER_CACHE_ID);
  ObReadBuf& rbuf = guard.get_read_buf();
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (OB_INVALID_ID == log_id) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K(partition_key_), K(log_id));
  } else if (OB_UNLIKELY(!rbuf.is_valid())) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    CLOG_LOG(WARN, "failed to alloc read_buf", K_(partition_key), K(log_id), K(ret));
  } else if (OB_FAIL(try_get_curr_member_list(member_list))) {
    CLOG_LOG(WARN, "try_get_curr_member_list failed", K(ret), K(partition_key_), K(log_id));
  } else if (OB_FAIL(get_role_unlock(leader_epoch1, unused_windows))) {
    CLOG_LOG(WARN, "get_role failed or not master", K(ret), K(partition_key_), K(log_id));
  } else if (OB_SUCC(get_confirmed_log_from_sw_(log_id, rbuf, log_entry))) {
    uint64_t unused_log_id = OB_INVALID_ID;
    if (OB_FAIL(transaction::ObTransLogParseUtils::parse_redo_prepare_log(
            log_entry, unused_log_id, submit_timestamp, trans_id))) {
      CLOG_LOG(ERROR, "parse_redo_prepare_log failed", K(ret), K(partition_key_), K(log_id));
      ret = OB_ERR_UNEXPECTED;
    } else if (unused_log_id != log_id) {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(ERROR, "log_id not match", K(ret), K(partition_key_), K(log_id), K(unused_log_id));
    } else {
      CLOG_LOG(INFO,
          "get_confirmed_log_from_sw_ finished",
          K(ret),
          K(partition_key_),
          K(log_id),
          K(trans_id),
          K(submit_timestamp));
    }
  } else if (OB_EAGAIN == ret || OB_ENTRY_NOT_EXIST == ret) {
    // do nothing
    CLOG_LOG(INFO, "get_confirmed_log_from_sw_ eagain or not exist", K(ret), K(partition_key_), K(log_id));
  } else if (OB_ERROR_OUT_OF_RANGE == ret) {
    if (OB_FAIL(get_log_from_ilog_storage_(log_id, member_list, trans_id, submit_timestamp))) {
      CLOG_LOG(WARN, "get_log_from_ilog_storage_ failed", K(ret), K(partition_key_), K(log_id), K(submit_timestamp));
    } else {
      CLOG_LOG(INFO,
          "get_log_from_ilog_storage_ finished",
          K(ret),
          K(partition_key_),
          K(log_id),
          K(trans_id),
          K(submit_timestamp));
    }
  } else {
    CLOG_LOG(ERROR, "get_confirmed_log_from_sw_ failed", K(ret), K(partition_key_), K(log_id));
    ret = OB_ERR_UNEXPECTED;
  }

  if (OB_SUCCESS == ret || OB_EAGAIN == ret || OB_ENTRY_NOT_EXIST == ret) {
    int tmp_ret = OB_SUCCESS;
    if (OB_SUCCESS != (tmp_ret = get_role_unlock(leader_epoch2, unused_windows))) {
      CLOG_LOG(WARN, "get_role failed or not master", K(ret), K(partition_key_), K(log_id));
      ret = tmp_ret;
    } else if (leader_epoch1 != leader_epoch2) {
      CLOG_LOG(WARN, "leader epoch changed, not master", K(ret), K(partition_key_), K(log_id));
      ret = OB_EAGAIN;
    }
  }

  return ret;
}

int ObPartitionLogService::get_confirmed_log_from_sw_(const uint64_t log_id, ObReadBuf& rbuf, ObLogEntry& log_entry)
{
  int ret = OB_SUCCESS;
  const uint32_t log_attr = ObFetchLogAttr::NEED_CONFIRMED;
  bool log_confirmed = false;
  ObLogCursor log_cursor;
  int64_t accum_checksum = 0;
  bool batch_committed = false;
  ObReadParam read_param;

  if (OB_SUCCESS == (ret = sw_.get_log(log_id, log_attr, log_confirmed, log_cursor, accum_checksum, batch_committed))) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "get_log return value unexpected", K(ret), K(partition_key_), K(log_id));
  } else if (OB_EAGAIN == ret || OB_ERROR_OUT_OF_RANGE == ret) {
    // do nothing
  } else if (OB_ERR_NULL_VALUE == ret) {
    // When log_id is less than sw max_log_id, it means that this log_id has been allocated, so it needs to return
    // OB_EAGAIN Otherwise, you can safely return to OB_ENTRY_NOT_EXIST;
    if (log_id <= sw_.get_max_log_id()) {
      ret = OB_EAGAIN;
    } else {
      ret = OB_ENTRY_NOT_EXIST;
    }
  } else if (OB_LOG_MISSING == ret && log_cursor.is_valid()) {
    read_param.file_id_ = log_cursor.file_id_;
    read_param.offset_ = log_cursor.offset_;
    read_param.read_len_ = log_cursor.size_;
    if (OB_FAIL(log_engine_->read_log_by_location(read_param, rbuf, log_entry))) {
      CLOG_LOG(WARN, "read_log_by_location failed", K(ret), K(partition_key_), K(read_param));
    }
  } else {
    CLOG_LOG(ERROR,
        "sw_ get_log error, unexpected error",
        K(ret),
        K(partition_key_),
        K(log_id),
        K(log_entry),
        K(log_confirmed),
        K(log_cursor),
        K(accum_checksum));
    ret = OB_ERR_UNEXPECTED;
  }

  return ret;
}

int ObPartitionLogService::get_log_from_ilog_storage_(const uint64_t log_id, const common::ObMemberList& member_list,
    transaction::ObTransID& trans_id, int64_t& submit_timestamp)
{
  int ret = OB_SUCCESS;
  ObLogCursorExt log_cursor;
  uint64_t unused_log_id = OB_INVALID_ID;

  if (OB_FAIL(log_engine_->get_cursor(partition_key_, log_id, log_cursor))) {
    if (OB_CURSOR_NOT_EXIST == ret) {
      CLOG_LOG(WARN, "ilog_storage get invalid log, cursor not exist", K(ret), K(partition_key_), K(log_id));
      if (OB_FAIL(remote_log_query_engine_->get_log(partition_key_, log_id, member_list, trans_id, submit_timestamp))) {
        if (OB_EAGAIN != ret) {
          CLOG_LOG(
              WARN, "remote_log_query_engine_ get_log failed", K(ret), K(partition_key_), K(log_id), K(member_list));
        } else if (REACH_TIME_INTERVAL(1000 * 1000)) {
          CLOG_LOG(
              WARN, "remote_log_query_engine_ get_log failed", K(ret), K(partition_key_), K(log_id), K(member_list));
        }
      } else {
        CLOG_LOG(INFO,
            "remote_log_query_engine_ get_log finished",
            K(ret),
            K(partition_key_),
            K(log_id),
            K(trans_id),
            K(submit_timestamp));
      }
    } else if (OB_NEED_RETRY == ret || OB_ERR_OUT_OF_UPPER_BOUND == ret) {
      ret = OB_EAGAIN;
    } else {
      CLOG_LOG(WARN, "ilog_storage_ get_cursor failed", K(ret), K(partition_key_), K(log_id));
    }
  } else {
    ObReadParam read_param;
    read_param.file_id_ = log_cursor.get_file_id();
    read_param.offset_ = log_cursor.get_offset();
    read_param.read_len_ = log_cursor.get_size();
    ObLogEntry log_entry;
    ObReadBufGuard guard(ObModIds::OB_LOG_DIRECT_READER_CACHE_ID);
    ObReadBuf& rbuf = guard.get_read_buf();
    if (OB_UNLIKELY(!rbuf.is_valid())) {
      ret = OB_ALLOCATE_MEMORY_FAILED;
      CLOG_LOG(WARN, "failed to alloc read_buf", K_(partition_key), K(log_id), K(read_param), K(ret));
    } else if (OB_FAIL(log_engine_->read_log_by_location(read_param, rbuf, log_entry))) {
      CLOG_LOG(WARN, "read_log_by_location failed", K(ret), K(partition_key_), K(read_param));
    } else if (OB_FAIL(transaction::ObTransLogParseUtils::parse_redo_prepare_log(
                   log_entry, unused_log_id, submit_timestamp, trans_id))) {
      CLOG_LOG(ERROR, "parse_redo_prepare_log failed", K(ret), K(partition_key_), K(log_id));
      ret = OB_ERR_UNEXPECTED;
    } else if (unused_log_id != log_id) {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(ERROR, "log_id not match", K(ret), K(partition_key_), K(log_id), K(unused_log_id));
    } else {
      CLOG_LOG(DEBUG,
          "get_log_from_ilog_storage finished",
          K(ret),
          K(partition_key_),
          K(log_id),
          K(trans_id),
          K(submit_timestamp));
    }
  }
  return ret;
}

int ObPartitionLogService::add_role_change_task_()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (!need_add_event_task()) {
    // do nothing
    CLOG_LOG(TRACE, "no need_add_event_task", K(ret), K(partition_key_), K(has_event_task_));
  } else {
    ObLogStateEventTaskV2* task = NULL;
    while (NULL == (task = alloc_mgr_->alloc_log_event_task())) {
      if (REACH_TIME_INTERVAL(1000 * 1000)) {
        CLOG_LOG(ERROR, "alloc ObLogStateEventTaskV2 failed", K(ret), K(partition_key_));
      }
      usleep(1000);
    }
    if (OB_FAIL(task->init(partition_key_, event_scheduler_, partition_service_))) {
      CLOG_LOG(ERROR, "ObLogStateEventTaskV2 init failed", K(ret), K(partition_key_));
    } else if (OB_FAIL(event_scheduler_->add_state_change_event(task))) {
      CLOG_LOG(WARN, "add_state_change_event failed", K(ret), K(partition_key_));
    }
    if (OB_FAIL(ret)) {
      ob_slice_free_log_event_task(task);
      task = NULL;
    }
  }
  return ret;
}

bool ObPartitionLogService::need_add_event_task()
{
  bool bool_ret = false;
  if (IS_NOT_INIT) {
    bool_ret = false;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited");
  } else if (is_in_partition_service_()) {
    ObSpinLockGuard guard(event_task_lock_);
    if (has_event_task_) {
      bool_ret = false;
    } else {
      bool_ret = true;
      has_event_task_ = true;
    }
  }
  return bool_ret;
}

void ObPartitionLogService::reset_has_event_task()
{
  ObSpinLockGuard guard(event_task_lock_);
  has_event_task_ = false;
}

int ObPartitionLogService::backfill_log(
    const ObLogInfo& log_info, const ObLogCursor& log_cursor, const bool is_leader, ObISubmitLogCb* submit_cb)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_), K(log_info));
  } else if (!log_info.is_valid() || !log_cursor.is_valid() || (is_leader && OB_ISNULL(submit_cb)) ||
             (!is_leader && !OB_ISNULL(submit_cb))) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(
        WARN, "invalid arguments", K(ret), K(partition_key_), K(log_info), K(log_cursor), K(is_leader), KP(submit_cb));
  } else if (!is_leader && !sw_.check_can_receive_larger_log(log_info.get_log_id())) {
    ret = OB_TOO_LARGE_LOG_ID;
    if (REACH_TIME_INTERVAL(1000 * 1000)) {
      CLOG_LOG(WARN, "backfill_log too large log_id", K(ret), K(partition_key_), K(log_info));
    }
  } else {
    if (NULL != submit_cb) {
      submit_cb->base_class_reset();
    }
    const char* serialize_buff = log_info.get_buf();
    const int64_t serialize_size = log_info.get_size();
    const common::ObProposalID& proposal_id = log_info.get_proposal_id();

    RLockGuard guard(lock_);
    if (!state_mgr_.can_backfill_log(is_leader, proposal_id)) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "can_backfill_log failed",
          K(ret),
          K(partition_key_),
          K(is_leader),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          K(proposal_id),
          "curr_proposal_id",
          state_mgr_.get_proposal_id());
    } else if (OB_FAIL(sw_.backfill_log(log_info.get_log_id(),
                   proposal_id,
                   serialize_buff,
                   serialize_size,
                   log_cursor,
                   is_leader,
                   submit_cb))) {
      CLOG_LOG(WARN, "sw backfill_log failed", K(ret), K(partition_key_), K(log_info));
    }
    if (is_leader && OB_ALLOCATE_MEMORY_FAILED == ret) {
      int tmp_ret = OB_SUCCESS;
      const uint32_t revoke_type = ObElection::RevokeType::SUBMIT_LOG_MEMORY_ALLOC_FAIL;
      if (OB_SUCCESS != (tmp_ret = election_mgr_->leader_revoke(partition_key_, revoke_type))) {
        CLOG_LOG(ERROR, "election_mgr_ leader_revoke failed", K(ret), K(tmp_ret), K(partition_key_), K(revoke_type));
      }
    }
  }

  return ret;
}

int ObPartitionLogService::backfill_confirmed(const ObLogInfo& log_info, const bool batch_first_participant)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_), K(log_info));
  } else if (!log_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K(partition_key_), K(log_info));
  } else {
    const uint64_t log_id = log_info.get_log_id();
    const common::ObProposalID proposal_id = log_info.get_proposal_id();

    RLockGuard guard(lock_);
    if (!state_mgr_.can_backfill_confirmed(proposal_id)) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "can_backfill_confirmed failed",
          K(ret),
          K(partition_key_),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          K(proposal_id),
          "curr_proposal_id",
          state_mgr_.get_proposal_id());
    } else if (OB_FAIL(sw_.backfill_confirmed(log_id, batch_first_participant))) {
      CLOG_LOG(WARN, "sw backfill_confirmed failed", K(ret), K(partition_key_), K(log_id));
    }
  }

  return ret;
}

int ObPartitionLogService::resubmit_log(const ObLogInfo& log_info, ObISubmitLogCb* submit_cb)
{
  int ret = OB_SUCCESS;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_), K(log_info));
  } else if (OB_ISNULL(submit_cb) || !log_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K(partition_key_), K(log_info), KP(submit_cb));
  } else {
    if (NULL != submit_cb) {
      submit_cb->base_class_reset();
    }
    const common::ObProposalID proposal_id = log_info.get_proposal_id();

    RLockGuard guard(lock_);
    if (!state_mgr_.can_submit_with_assigned_id()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "can not submit log for assigned log_id",
          K(ret),
          K(partition_key_),
          "role",
          state_mgr_.get_role(),
          "is_offline",
          state_mgr_.is_offline(),
          "state",
          state_mgr_.get_state(),
          K(log_info));
    } else if (proposal_id != state_mgr_.get_proposal_id()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "proposal_id not match",
          K(ret),
          K(partition_key_),
          K(proposal_id),
          "curr_proposal_id",
          state_mgr_.get_proposal_id(),
          K(log_info));
    } else if (OB_FAIL(sw_.resubmit_log(log_info, submit_cb))) {
      CLOG_LOG(ERROR, "resubmit_log failed", K(ret), K(partition_key_), K(log_info));
    }

    if (OB_ALLOCATE_MEMORY_FAILED == ret) {
      int tmp_ret = OB_SUCCESS;
      const uint32_t revoke_type = ObElection::RevokeType::SUBMIT_LOG_MEMORY_ALLOC_FAIL;
      if (OB_SUCCESS != (tmp_ret = election_mgr_->leader_revoke(partition_key_, revoke_type))) {
        CLOG_LOG(ERROR, "election_mgr_ leader_revoke failed", K(ret), K(tmp_ret), K(partition_key_), K(revoke_type));
      }
    }
  }

  return ret;
}

int ObPartitionLogService::backfill_nop_log(const ObLogMeta& log_meta)
{
  int ret = OB_SUCCESS;

  static const int64_t BUF_SIZE = 2048;
  ObNopLog nop_log;
  char body_buffer[BUF_SIZE];
  int64_t body_pos = 0;
  common::ObVersion unused_version = ObVersion(1, 0);
  ObISubmitLogCb* submit_cb = NULL;
  ObLogEntryHeader header;
  const bool is_trans_log = false;

  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret));
  } else if (!log_meta.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K(partition_key_), K(log_meta));
  } else if (lock_.try_rdlock()) {
    if (!state_mgr_.can_submit_with_assigned_id()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "can not submit log for assigned log_id",
          K(ret),
          K(partition_key_),
          "role",
          state_mgr_.get_role(),
          "is_offline",
          state_mgr_.is_offline(),
          "state",
          state_mgr_.get_state(),
          K(log_meta));
    } else if (log_meta.get_proposal_id() != state_mgr_.get_proposal_id()) {
      ret = OB_STATE_NOT_MATCH;
      CLOG_LOG(WARN,
          "proposal_id not match",
          K(ret),
          K(partition_key_),
          "curr_proposal_id",
          state_mgr_.get_proposal_id(),
          K(log_meta));
    } else if (OB_FAIL(nop_log.serialize(body_buffer, BUF_SIZE, body_pos))) {
      CLOG_LOG(WARN, "serialize failed", K(ret), K(partition_key_));
    } else if (OB_FAIL(header.generate_header(OB_LOG_NOP,  // log_type
                   partition_key_,                         // partition_key
                   log_meta.get_log_id(),
                   body_buffer,                      // buff
                   body_pos,                         // data_len
                   ObClockGenerator::getClock(),     // generation_timestamp
                   log_meta.get_proposal_id().ts_,   // epoch_id,
                   log_meta.get_proposal_id(),       // proposal_id,
                   log_meta.get_submit_timestamp(),  // submit_timestamp
                   unused_version,                   // ObVersion
                   is_trans_log))) {
      CLOG_LOG(ERROR, "generate_header failed", K(ret), K(partition_key_));
    } else if (OB_FAIL(sw_.submit_log(header, body_buffer, submit_cb))) {
      CLOG_LOG(WARN, "submit_log failed", K(ret), K(partition_key_));
    }

    if (OB_ALLOCATE_MEMORY_FAILED == ret) {
      int tmp_ret = OB_SUCCESS;
      const uint32_t revoke_type = ObElection::RevokeType::SUBMIT_LOG_MEMORY_ALLOC_FAIL;
      if (OB_SUCCESS != (tmp_ret = election_mgr_->leader_revoke(partition_key_, revoke_type))) {
        CLOG_LOG(ERROR, "election_mgr_ leader_revoke failed", K(ret), K(tmp_ret), K(partition_key_), K(revoke_type));
      }
    }
    lock_.rdunlock();
  } else {
    ret = OB_EAGAIN;
  }

  return ret;
}

int ObPartitionLogService::is_valid_member(const common::ObAddr& addr, bool& is_valid) const
{
  int ret = OB_SUCCESS;

  RLockGuard guard(lock_);
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (!addr.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(ERROR, "invalid arguments", K(ret), K(partition_key_), K(addr));
  } else if (!state_mgr_.is_leader_active() && !state_mgr_.is_standby_leader_active()) {
    ret = OB_NOT_MASTER;
  } else {
    is_valid = mm_.get_curr_member_list().contains(addr);
  }

  return ret;
}

int ObPartitionLogService::change_quorum(const common::ObMemberList& curr_member_list, const int64_t curr_quorum,
    const int64_t new_quorum, obrpc::ObMCLogInfo& log_info)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (!curr_member_list.is_valid() || curr_quorum <= 0 || curr_quorum > OB_MAX_MEMBER_NUMBER ||
             new_quorum <= 0 || new_quorum > OB_MAX_MEMBER_NUMBER) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K(partition_key_), K(curr_member_list), K(curr_quorum), K(new_quorum));
  } else {
    WLockGuardWithTimeout guard(lock_, WRLOCK_TIMEOUT_US + ObTimeUtility::current_time(), ret);
    if (OB_FAIL(ret)) {
      CLOG_LOG(WARN, "change quorum wrlock failed", K(ret), K(partition_key_));
    } else if (!state_mgr_.can_change_member()) {
      CLOG_LOG(WARN,
          "can not change_member",
          K(ret),
          K(partition_key_),
          "role",
          state_mgr_.get_role(),
          "state",
          state_mgr_.get_state(),
          "is_changing_leader",
          state_mgr_.is_changing_leader());
    } else if (OB_FAIL(mm_.change_quorum(curr_member_list, curr_quorum, new_quorum, log_info))) {
      CLOG_LOG(
          WARN, "change quorum failed", K(ret), K(partition_key_), K(curr_member_list), K(curr_quorum), K(new_quorum));
    }
  }
  CLOG_LOG(INFO, "change_quorum finished", K(ret), K(partition_key_), K(curr_quorum), K(new_quorum), K(log_info));
  return ret;
}

int ObPartitionLogService::check_can_receive_batch_log(const uint64_t log_id)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(WARN, "ObPartitionLogService is not inited", K(ret), K(partition_key_));
  } else if (OB_INVALID_ID == log_id) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K(partition_key_), K(log_id));
  } else if (!sw_.check_can_receive_larger_log(log_id)) {
    ret = OB_TOO_LARGE_LOG_ID;
  } else {
    // do nothing
  }
  return ret;
}

bool ObPartitionLogService::is_in_partition_service_() const
{
  bool bool_ret = false;
  int ret = OB_SUCCESS;

  storage::ObIPartitionGroupGuard guard;
  ObIPartitionLogService* log_service = NULL;

  if (OB_FAIL(partition_service_->get_partition(partition_key_, guard))) {
    CLOG_LOG(WARN, "get_partition failed", K(ret), K(partition_key_));
  } else if (NULL == guard.get_partition_group()) {
    ret = OB_PARTITION_NOT_EXIST;
    CLOG_LOG(WARN, "partition not exist", K(ret), K(partition_key_));
  } else if (NULL == (log_service = guard.get_partition_group()->get_log_service())) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "get partition log service error", K(ret), K(partition_key_));
  } else {
    bool_ret = true;
  }

  return bool_ret;
}

int ObPartitionLogService::update_max_flushed_ilog_id(const uint64_t log_id)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "ObPartitionLogService is not inited", K(ret));
  } else if (!is_valid_log_id(log_id)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(ERROR, "invalid arguments", K(ret), K(partition_key_), K(log_id));
  } else {
    ATOMIC_STORE(&max_flushed_ilog_id_, log_id);
  }
  return ret;
}

int ObPartitionLogService::get_cursor_with_cache(
    const int64_t scan_thread_index, const uint64_t log_id, ObLogCursorExt& log_cursor)
{
  int ret = OB_SUCCESS;

  RLockGuard guard(lock_);
  ObCursorArrayCache* const c = cursor_array_cache_;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "ObPartitionLogService is not inited", K(ret));
  } else if (!is_valid_log_id(log_id) || scan_thread_index < 0 || scan_thread_index >= SCAN_THREAD_CNT) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(ERROR, "invalid arguments", K(ret), K(scan_thread_index), K(log_id));
  } else if (OB_ISNULL(cursor_array_cache_)) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "cursor_array_cache_ is NULL, unexpected", K(ret), K(partition_key_));
  } else if (log_id >= c->cursor_start_log_id_[scan_thread_index] &&
             log_id < c->cursor_end_log_id_[scan_thread_index]) {
    log_cursor = c->cursor_array_[scan_thread_index][log_id - c->cursor_start_log_id_[scan_thread_index]];
  } else {
    ObGetCursorResult result;
    result.csr_arr_ = c->cursor_array_[scan_thread_index];
    result.arr_len_ = ObCursorArrayCache::CURSOR_ARRAY_SIZE;
    result.ret_len_ = 0;
    if (OB_FAIL(log_engine_->get_cursor_batch_from_file(partition_key_, log_id, result))) {
      CSR_LOG(ERROR, "get_cursor_batch_from_file failed", K(ret), K(partition_key_), K(log_id), K(result));
    } else {
      c->cursor_start_log_id_[scan_thread_index] = log_id;
      c->cursor_end_log_id_[scan_thread_index] = log_id + result.ret_len_;
      log_cursor = c->cursor_array_[scan_thread_index][log_id - c->cursor_start_log_id_[scan_thread_index]];
    }
  }

  return ret;
}

int ObPartitionLogService::reset_has_pop_task()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "ObPartitionLogService is not inited", K(ret));
  } else {
    sw_.reset_has_pop_task();
  }
  return ret;
}

int ObPartitionLogService::process_check_rebuild_req(
    const common::ObAddr& server, const uint64_t start_log_id, const int64_t cluster_id)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else if (OB_UNLIKELY(!server.is_valid()) || OB_UNLIKELY(start_log_id <= 0)) {
    ret = OB_INVALID_ARGUMENT;
    CLOG_LOG(WARN, "invalid arguments", K(ret), K_(partition_key), K(server), K(start_log_id));
  } else if (server == self_) {
    // ignore self_
  } else {
    if (start_log_id > sw_.get_max_log_id()) {
      CLOG_LOG(INFO,
          "wanted log are not supplied in this server",
          K(start_log_id),
          "sw_max_log_id",
          sw_.get_max_log_id(),
          K(server),
          K_(partition_key));
    } else if (check_if_need_rebuild_(start_log_id, OB_FETCH_LOG_FOLLOWER_ACTIVE)) {
      bool is_in_member_list = mm_.get_curr_member_list().contains(server);
      CLOG_LOG(INFO,
          "follower try to fetch too old log, need rebuild",
          K_(partition_key),
          K(server),
          K(start_log_id),
          K(is_in_member_list));
      (void)log_engine_->notify_follower_log_missing(
          server, cluster_id, partition_key_, start_log_id, is_in_member_list, OB_REBUILD_MSG);
    } else {
      // do nothing
    }
  }
  return ret;
}

void ObPartitionLogService::get_max_majority_log(uint64_t& log_id, int64_t& log_ts) const
{
  sw_.get_max_majority_log(log_id, log_ts);
}

uint64_t ObPartitionLogService::get_max_confirmed_log_id() const
{
  return sw_.get_max_confirmed_log_id();
}

int ObPartitionLogService::block_partition_split_()
{
  int ret = OB_SUCCESS;

  storage::ObIPartitionGroupGuard guard;
  if (OB_FAIL(partition_service_->get_partition(partition_key_, guard))) {
    CLOG_LOG(WARN, "invalid partition", K(ret), K(partition_service_), K_(partition_key));
  } else if ((NULL == guard.get_partition_group()) || (!(guard.get_partition_group()->is_valid()))) {
    ret = OB_INVALID_PARTITION;
    CLOG_LOG(WARN, "partition is invalid", K(ret), K_(partition_key));
  } else if (OB_FAIL(guard.get_partition_group()->block_partition_split_by_mc())) {
    CLOG_LOG(WARN, "block partiton split failed", K(ret), K_(partition_key));
  } else {
    CLOG_LOG(INFO, "block partition split success", K_(partition_key));
  }

  return ret;
}

int ObPartitionLogService::unblock_partition_split_()
{
  int ret = OB_SUCCESS;

  storage::ObIPartitionGroupGuard guard;
  if (OB_FAIL(partition_service_->get_partition(partition_key_, guard))) {
    CLOG_LOG(WARN, "get partition failed", K(ret), K_(partition_key));
  } else if (NULL == guard.get_partition_group()) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(WARN, "partition is null", K(ret), K_(partition_key));
  } else if (OB_FAIL(guard.get_partition_group()->unblock_partition_split_by_mc())) {
    CLOG_LOG(WARN, "unblock partition split failed", K(ret), K_(partition_key));
  } else {
    CLOG_LOG(INFO, "unblock partition split success", K_(partition_key));
  }
  return ret;
}

int ObPartitionLogService::set_archive_restore_state(const int16_t archive_restore_state)
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    ret = restore_mgr_.set_archive_restore_state(archive_restore_state);
  }
  return ret;
}

int ObPartitionLogService::sync_local_archive_progress_(bool& need_sync)
{
  int ret = OB_SUCCESS;
  ObPGLogArchiveStatus status;
  common::ObRole role;
  int64_t archive_epoch = OB_INVALID_TIMESTAMP;
  int64_t leader_epoch = OB_INVALID_TIMESTAMP;
  int64_t unused_takeover_time = OB_INVALID_ARGUMENT;
  need_sync = false;
  if (OB_ISNULL(archive_mgr_)) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "archive_mgr is null", KR(ret), K_(partition_key));
  } else if (OB_FAIL(archive_mgr_->get_log_archive_status(partition_key_, status, archive_epoch))) {
    CLOG_LOG(WARN, "failed to get_log_archive_status", KR(ret), K_(partition_key));
  } else if (share::ObLogArchiveStatus::INVALID == status.status_) {
    // do nothing when returned status.status is invalid
  } else if (OB_FAIL(get_role_and_leader_epoch_unlock_(role, leader_epoch, unused_takeover_time))) {
    CLOG_LOG(WARN, "failed to get_role_and_leader_epoch", KR(ret), K_(partition_key), K(status), K(archive_epoch));
  } else if ((!is_strong_leader(role) || archive_epoch != leader_epoch) && !status.is_stopped() &&
             !status.is_interrupted()) {
    // skip it
  } else {
    SpinWLockGuard wlock_guard(log_archive_status_rwlock_);
    if (log_archive_status_ < status) {
      if (status.is_stopped() || status.is_interrupted()) {
        // If the synchronized state is stopped or interrupted, only incarnation, round, status are updated
        log_archive_status_.archive_incarnation_ = status.archive_incarnation_;
        log_archive_status_.log_archive_round_ = status.log_archive_round_;
        log_archive_status_.status_ = status.status_;
      } else {
        log_archive_status_ = status;
      }
    }
    need_sync = true;
  }
  return ret;
}

bool ObPartitionLogService::is_archive_restoring() const
{
  int bool_ret = false;
  if (IS_NOT_INIT) {
  } else {
    bool_ret = restore_mgr_.is_archive_restoring();
  }
  return bool_ret;
}

common::ObAddr ObPartitionLogService::get_restore_leader() const
{
  common::ObAddr restore_leader;
  if (IS_NOT_INIT) {
  } else {
    restore_leader = restore_mgr_.get_restore_leader();
  }
  return restore_leader;
}

int ObPartitionLogService::set_restore_flag_after_restore_log_()
{
  int ret = OB_SUCCESS;
  storage::ObIPartitionGroupGuard guard;
D
dt0 已提交
8282 8283 8284 8285
  ObIPartitionGroup *partition = NULL;
  uint64_t last_restore_log_id = OB_INVALID_ID;
  int64_t last_restore_log_ts = OB_INVALID_TIMESTAMP;
  sw_.get_max_log_id_info(last_restore_log_id, last_restore_log_ts);
O
oceanbase-admin 已提交
8286 8287 8288 8289 8290 8291 8292 8293
  if (OB_ISNULL(partition_service_)) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(WARN, "invalid partition_service_ is NULL", KR(ret), KP(partition_service_), K_(partition_key));
  } else if (OB_FAIL(partition_service_->get_partition(partition_key_, guard))) {
    CLOG_LOG(WARN, "failed to get_partition", KR(ret), KP(partition_service_), K_(partition_key));
  } else if (OB_ISNULL(partition = guard.get_partition_group()) || (!partition->is_valid())) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(WARN, "invalid partition", K(ret), K_(partition_key));
D
dt0 已提交
8294
  } else if (OB_FAIL(partition->clear_trans_after_restore_log(last_restore_log_id, last_restore_log_ts))) {
8295 8296
    CLOG_LOG(ERROR, "failed to clear_trans_after_restore_log", K(ret), K_(partition_key),
        K(last_restore_log_id), K(last_restore_log_ts));
O
oceanbase-admin 已提交
8297 8298 8299
  } else {
    ObReplicaRestoreStatus flag = ObReplicaRestoreStatus::REPLICA_RESTORE_DUMP_MEMTABLE;
    if (OB_FAIL(partition_service_->set_restore_flag(partition_key_, flag))) {
8300 8301
      CLOG_LOG(WARN, "failed to set_restore_flag", K_(partition_key), K(flag), K(last_restore_log_id),
          K(last_restore_log_ts), KR(ret));
O
oceanbase-admin 已提交
8302
    } else {
8303 8304
      CLOG_LOG(INFO, "success to set_restore_flag", K_(partition_key), K(flag), K(last_restore_log_id),
          K(last_restore_log_ts), KR(ret));
O
oceanbase-admin 已提交
8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352
    }
  }
  return ret;
}

int ObPartitionLogService::restore_leader_try_confirm_log()
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
  } else {
    RLockGuard guard(lock_);
    if (RESTORE_LEADER == restore_mgr_.get_role() && restore_mgr_.is_archive_restoring() &&
        restore_mgr_.is_restore_log_finished()) {
      (void)sw_.restore_leader_try_confirm_log();
    }
  }
  return ret;
}

bool ObPartitionLogService::is_standby_restore_state() const
{
  int bool_ret = false;
  if (IS_NOT_INIT) {
  } else {
    RLockGuard guard(lock_);
    bool_ret = restore_mgr_.is_standby_restore_state();
  }
  return bool_ret;
}

int ObPartitionLogService::check_and_try_leader_revoke(const ObElection::RevokeType& revoke_type)
{
  RLockGuard guard(lock_);
  int ret = OB_SUCCESS;
  bool need_revoke = false;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    CLOG_LOG(ERROR, "ObPartitionLogService not inited", K(ret), K(partition_key_));
  } else if (FOLLOWER == state_mgr_.get_role()) {
    // do nothing
  } else {
    // TODO:consider more suitation
    bool majority_is_clog_disk_full = false;
    if (OB_FAIL(check_majority_replica_clog_disk_full_(majority_is_clog_disk_full))) {
      CLOG_LOG(ERROR, "check_majority_replica_clog_disk_full_ failed", K(ret));
    } else {
      need_revoke = !majority_is_clog_disk_full;
8353
      CLOG_LOG(INFO, "partition may need revoke, ", "need revoke is ", need_revoke, "and revoke type is ", revoke_type);
O
oceanbase-admin 已提交
8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540
    }
  }

  if (need_revoke && OB_FAIL(election_mgr_->leader_revoke(partition_key_, revoke_type))) {
    CLOG_LOG(ERROR, "election_mgr_ leader_revoke failed", K(ret), K(partition_key_));
  }
  return ret;
}

int ObPartitionLogService::alloc_cursor_array_cache_(const common::ObPartitionKey& partition_key)
{
  int ret = OB_SUCCESS;

  if (NULL != cursor_array_cache_) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "cursor_array_cache_ is not NULL", K(ret), K(partition_key));
  } else if (NULL == (cursor_array_cache_ = (ObCursorArrayCache*)ob_malloc(
                          sizeof(ObCursorArrayCache), ObModIds::OB_PARTITION_LOG_SERVICE))) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    CLOG_LOG(ERROR, "alloc ObCursorArrayCache failed", K(ret), K(partition_key));
  } else {
    cursor_array_cache_ = new (cursor_array_cache_) ObCursorArrayCache();

    if (OB_FAIL(cursor_array_cache_->init())) {
      CLOG_LOG(ERROR, "cursor_array_cache_ init failed", K(ret), K(partition_key));
      free_cursor_array_cache_();
    }
  }

  return ret;
}

void ObPartitionLogService::free_cursor_array_cache_()
{
  if (cursor_array_cache_ != NULL) {
    cursor_array_cache_->~ObCursorArrayCache();
    ob_free(cursor_array_cache_);
    cursor_array_cache_ = NULL;
  }
}

int ObPartitionLogService::alloc_broadcast_info_mgr_(const common::ObPartitionKey& partition_key)
{
  int ret = OB_SUCCESS;

  if (NULL != broadcast_info_mgr_) {
    ret = OB_ERR_UNEXPECTED;
    CLOG_LOG(ERROR, "broadcast_info_mgr_ is not NULL", K(ret), K(partition_key));
  } else if (NULL == (broadcast_info_mgr_ = (ObLogBroadcastInfoMgr*)ob_malloc(
                          sizeof(ObLogBroadcastInfoMgr), ObModIds::OB_PARTITION_LOG_SERVICE))) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    CLOG_LOG(ERROR, "alloc ObLogBroadcastInfoMgr failed", K(ret), K(partition_key));
  } else {
    broadcast_info_mgr_ = new (broadcast_info_mgr_) ObLogBroadcastInfoMgr();

    if (OB_FAIL(broadcast_info_mgr_->init(partition_key, &mm_))) {
      CLOG_LOG(ERROR, "broadcast_info_mgr_ init failed", K(ret), K(partition_key));
      free_broadcast_info_mgr_();
    }
  }

  return ret;
}

void ObPartitionLogService::free_broadcast_info_mgr_()
{
  if (broadcast_info_mgr_ != NULL) {
    broadcast_info_mgr_->~ObLogBroadcastInfoMgr();
    ob_free(broadcast_info_mgr_);
    broadcast_info_mgr_ = NULL;
  }
}

int ObPartitionLogService::check_majority_replica_clog_disk_full_(bool& majority_is_clog_disk_full)
{
  int ret = OB_SUCCESS;
  const common::ObMemberList& member_list = mm_.get_curr_member_list();
  const int64_t member_number = member_list.get_member_number();
  const int64_t replica_num = mm_.get_replica_num();
  int64_t disk_full_cnt = 0;
  majority_is_clog_disk_full = false;
  for (int64_t i = 0; i < member_number && OB_SUCC(ret); i++) {
    common::ObAddr server;
    if (OB_FAIL(member_list.get_server_by_index(i, server))) {
      CLOG_LOG(ERROR, "get_server_by_index failed", K(ret), K(i), K(member_number));
    } else {
      ObCascadMember member(server, obrpc::ObRpcNetHandler::CLUSTER_ID);
      if (share::ObServerBlacklist::get_instance().is_clog_disk_full(member)) {
        disk_full_cnt++;
      }
    }
  }
  majority_is_clog_disk_full = (disk_full_cnt >= replica_num / 2 + 1);
  return ret;
}

// There are 3 conditions to judge if memory reached limit.
// 1. tenant's memory is not enough.
// 2. memory store's memory is not enough.
// 3. clog's memory is not enough.
// This function return true if any of conditions above is true.
bool ObPartitionLogService::is_tenant_out_of_memory_() const
{
  int ret = OB_SUCCESS;
  bool bool_ret = false;
  const uint64_t tenant_id = partition_key_.get_tenant_id();
  // expect at least 2MB memory for every reconfirm log
  constexpr int64_t NEED_MEMORY = RECONFIRM_LOG_ARRAY_LENGTH * OB_MAX_LOG_BUFFER_SIZE;
  constexpr double MAX_PERCENTAGE = 0.99;

  if (IS_NOT_INIT) {
    // do nothing
  } else {
    // 1. check if tenant's memory is not enough
    lib::ObTenantResourceMgrHandle resource_handle;
    if (OB_FAIL(lib::ObResourceMgr::get_instance().get_tenant_resource_mgr(tenant_id, resource_handle))) {
      CLOG_LOG(ERROR, "get_tenant_resource_mgr failed", KR(ret), K_(partition_key), K(tenant_id));
    } else if (!resource_handle.is_valid()) {
      ret = OB_ERR_UNEXPECTED;
      CLOG_LOG(ERROR, "resource_handle is invalid", K_(partition_key), K(tenant_id));
    } else {
      const int64_t limit = resource_handle.get_memory_mgr()->get_limit();
      const int64_t hold = resource_handle.get_memory_mgr()->get_sum_hold();
      if ((double(hold) / limit) > MAX_PERCENTAGE && limit - hold < NEED_MEMORY) {
        bool_ret = true;
        if (REACH_TIME_INTERVAL(1000 * 1000)) {
          CLOG_LOG(INFO, "tenant's memory not enough", K(limit), K(hold), K_(partition_key), K(tenant_id));
        }
      } else {
        // do nothing
      }
    }

    // 2. check if memory store's memory is not enough
    if (false == bool_ret) {
      bool is_tenant_out_of_mem = false;
      if (OB_FAIL(replay_engine_->is_tenant_out_of_memory(partition_key_, is_tenant_out_of_mem))) {
        CLOG_LOG(WARN, "is_tenant_out_of_memory failed", K(ret), K_(partition_key));
      } else if (true == is_tenant_out_of_mem) {
        bool_ret = is_tenant_out_of_mem;
        if (REACH_TIME_INTERVAL(1000 * 1000)) {
          CLOG_LOG(INFO, "memory store's memory not enough", K_(partition_key), K(tenant_id));
        }
      } else {
        // do nothing
      }
    }

    // 3. check if clog's memory is not enough
    if (false == bool_ret) {
      if (nullptr != alloc_mgr_) {
        const int64_t limit = alloc_mgr_->get_clog_blk_alloc_mgr().limit();
        const int64_t hold = alloc_mgr_->get_clog_blk_alloc_mgr().hold();
        if ((double(hold) / limit) > MAX_PERCENTAGE && limit - hold < NEED_MEMORY) {
          bool_ret = true;
          if (REACH_TIME_INTERVAL(1000 * 1000)) {
            CLOG_LOG(INFO, "clog's memory not enough", K(limit), K(hold), K_(partition_key), K(tenant_id));
          }
        } else {
          // do nothing
        }
      }
    }
  }

  return bool_ret;
}

int ObPartitionLogService::get_role_and_leader_epoch_unlock_(
    common::ObRole& role, int64_t& leader_epoch, int64_t& takeover_time)
{
  int ret = OB_SUCCESS;
  role = FOLLOWER;
  int64_t unused_epoch = 0;
  ObTsWindows unused_windows;
  if (OB_SUCC(get_role_unlock(unused_epoch, unused_windows))) {
    role = state_mgr_.get_role();
  } else if (OB_NOT_MASTER == ret) {
    ret = OB_SUCCESS;
  } else {
  }
  leader_epoch = state_mgr_.get_leader_epoch();
  takeover_time = state_mgr_.get_last_leader_active_time();

  return ret;
}

8541 8542
}  // namespace clog
}  // namespace oceanbase