ob_persistent_partition_table.cpp 37.3 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
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#define USING_LOG_PREFIX SHARE_PT

#include "ob_persistent_partition_table.h"

#include "lib/time/ob_time_utility.h"
#include "lib/string/ob_sql_string.h"
#include "lib/mysqlclient/ob_mysql_transaction.h"
#include "share/inner_table/ob_inner_table_schema.h"
#include "share/config/ob_server_config.h"
#include "share/ob_dml_sql_splicer.h"
#include "observer/ob_server_struct.h"
#include "storage/ob_partition_service.h"
#include "ob_partition_info.h"
#include "ob_replica_filter.h"
#include "ob_partition_table_proxy.h"
#include "observer/ob_server_struct.h"
namespace oceanbase {
namespace share {
using namespace common;
using namespace share;

ObPersistentPartitionTable::ObPersistentPartitionTable(ObIPartPropertyGetter& prop_getter)
    : ObIPartitionTable(prop_getter), inited_(false), sql_proxy_(nullptr), config_(nullptr)
{}

ObPersistentPartitionTable::~ObPersistentPartitionTable()
{}

int ObPersistentPartitionTable::init(ObISQLClient& sql_proxy, ObServerConfig* config)
{
  int ret = OB_SUCCESS;
  if (is_inited()) {
    ret = OB_INIT_TWICE;
    LOG_WARN("int twice", K(ret));
  } else {
    sql_proxy_ = &sql_proxy;
    config_ = config;
    inited_ = true;
  }
  return ret;
}

int ObPersistentPartitionTable::get(const uint64_t table_id, const int64_t partition_id,
56 57
    ObPartitionInfo& partition_info, const bool need_fetch_faillist, const int64_t cluster_id,
    const bool filter_flag_replica)
O
oceanbase-admin 已提交
58 59 60 61 62 63 64 65 66 67 68
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!is_valid_key(table_id, partition_id)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), KT(table_id), K(partition_id));
  } else if (NULL == partition_info.get_allocator()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("partition info's allocator must set", K(ret), K(partition_info.get_allocator()));
69 70 71
  } else if (OB_FAIL(get_partition_info(
                 table_id, partition_id, filter_flag_replica, partition_info, need_fetch_faillist, cluster_id))) {
    LOG_WARN("get_partition_info failed", K(cluster_id), KT(table_id), K(partition_id), K(filter_flag_replica), K(ret));
O
oceanbase-admin 已提交
72 73 74 75 76
  }
  return ret;
}

int ObPersistentPartitionTable::prefetch_by_table_id(const uint64_t tenant_id, const uint64_t start_table_id,
77 78
    const int64_t start_partition_id, ObIArray<ObPartitionInfo>& partition_infos, const bool need_fetch_faillist,
    const bool filter_flag_replica)
O
oceanbase-admin 已提交
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid tenant_id", K(tenant_id), K(ret));
  } else if (OB_INVALID_ID == start_table_id || OB_INVALID_INDEX == start_partition_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid start_table_id or invalid start_partition_id", K(start_table_id), K(start_partition_id), K(ret));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    ObPartitionTableProxy* proxy = NULL;
    int64_t fetch_count = GCONF.partition_table_scan_batch_count;
    if (OB_FAIL(factory.get_proxy(start_table_id, proxy))) {
      LOG_WARN("get partition table proxy failed", K(ret), K(start_table_id));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), KT(start_table_id));
    } else if (OB_FAIL(proxy->fetch_by_table_id(tenant_id,
                   start_table_id,
                   start_partition_id,
                   filter_flag_replica,
                   fetch_count,
                   partition_infos,
                   need_fetch_faillist))) {
      LOG_WARN("fetch_tenant_partition_infos failed",
          K(tenant_id),
          K(start_table_id),
          K(start_partition_id),
          K(filter_flag_replica),
          K(fetch_count),
          K(ret));
    }
  }
  return ret;
}

int ObPersistentPartitionTable::prefetch(const uint64_t tenant_id, const uint64_t start_table_id,
    const int64_t start_partition_id, ObIArray<ObPartitionInfo>& partition_infos, bool ignore_row_checksum,
    const bool need_fetch_faillist)
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid tenant_id", K(tenant_id), K(ret));
  } else if (OB_INVALID_ID == start_table_id || OB_INVALID_INDEX == start_partition_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid start_table_id or invalid start_partition_id", K(start_table_id), K(start_partition_id), K(ret));
  } else {
    ObSEArray<ObPartitionTableProxy*, OB_DEFAULT_SE_ARRAY_COUNT> proxies;
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    const bool filter_flag_replica = false;
    if (OB_FAIL(factory.get_tenant_proxies(tenant_id, proxies))) {
      LOG_WARN("get_tenant_proxies failed", K(tenant_id), K(ret));
    } else {
      int64_t fetch_count = GCONF.partition_table_scan_batch_count;
      FOREACH_CNT_X(proxy, proxies, OB_SUCCESS == ret)
      {
        if (NULL == *proxy) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("NULL proxy", K(ret));
        } else if (OB_FAIL((*proxy)->fetch_partition_infos(tenant_id,
                       start_table_id,
                       start_partition_id,
                       filter_flag_replica,
                       fetch_count,
                       partition_infos,
                       ignore_row_checksum,
                       false,
                       need_fetch_faillist))) {
          LOG_WARN("fetch_tenant_partition_infos failed",
              K(tenant_id),
              K(start_table_id),
              K(start_partition_id),
              K(filter_flag_replica),
              K(fetch_count),
              K(ret));
        }
      }
    }
  }
  return ret;
}

int ObPersistentPartitionTable::prefetch(const uint64_t pt_table_id, const int64_t pt_partition_id,
    const uint64_t start_table_id, const int64_t start_partition_id, ObIArray<ObPartitionInfo>& partition_infos,
    const bool need_fetch_faillist)
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_INVALID_ID == pt_table_id || pt_partition_id < 0 || OB_INVALID_ID == start_table_id ||
             OB_INVALID_INDEX == start_partition_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(pt_table_id), K(pt_partition_id), K(start_table_id), K(start_partition_id), K(ret));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    ObPartitionTableProxy* proxy = NULL;
    int64_t fetch_count = GCONF.partition_table_scan_batch_count;
    if (OB_FAIL(factory.get_proxy_of_partition_table(pt_table_id, proxy))) {
      LOG_WARN("get partition table proxy failed", K(ret), KT(pt_table_id));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), KT(pt_table_id));
    } else if (OB_FAIL(proxy->fetch_partition_infos_pt(pt_table_id,
                   pt_partition_id,
                   start_table_id,
                   start_partition_id,
                   fetch_count,
                   partition_infos,
                   need_fetch_faillist))) {
      LOG_WARN("fetch_partition_infos failed",
          KT(pt_table_id),
          K(pt_partition_id),
          KT(start_table_id),
          K(start_partition_id),
          K(fetch_count),
          K(ret));
    }
  }

  return ret;
}

int ObPersistentPartitionTable::remove(const uint64_t table_id, const int64_t partition_id, const ObAddr& server)
{
  int ret = OB_SUCCESS;
  ObPartitionTableProxy* proxy = NULL;
  ObArenaAllocator allocator(ObModIds::OB_RS_PARTITION_TABLE_TEMP);
  ObPartitionInfo partition;
  partition.set_allocator(&allocator);
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!is_valid_key(table_id, partition_id) || !server.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), KT(table_id), K(partition_id), K(server));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(table_id, proxy))) {
      LOG_WARN("get partition table proxy failed", K(ret), KT(table_id));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), KT(table_id));
    } else {
      const bool filter_flag_replica = false;
      if (OB_FAIL(get_partition_info(table_id, partition_id, filter_flag_replica, partition))) {
        LOG_WARN("get partition info failed", K(ret), KT(table_id), K(partition_id), K(filter_flag_replica));
      } else {
        const ObPartitionReplica* replica = NULL;
        if (OB_FAIL(partition.find(server, replica))) {
          if (OB_ENTRY_NOT_EXIST == ret) {
            ret = OB_SUCCESS;
          } else {
            LOG_WARN("find server failed", K(ret), K(server));
          }
        } else {
          if (OB_FAIL(proxy->remove(table_id, partition_id, server))) {
            LOG_WARN("remove partition failed", K(ret), KT(table_id), K(partition_id));
          }
        }
      }
    }
  }
  LOG_INFO("remove partition", K(ret), KT(table_id), K(partition_id), K(server));
  return ret;
}

int ObPersistentPartitionTable::batch_execute(const ObIArray<ObPartitionReplica>& replicas)
{
  int ret = OB_SUCCESS;
  const int64_t EXECUTE_TIMEOUT_US = 2L * 1000 * 1000;
  int64_t start_time = ObTimeUtil::current_time();
  ObPartitionTableProxy* proxy = NULL;
  ObTimeoutCtx timeout_ctx;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_UNLIKELY(replicas.count() <= 0)) {
    // nothing to do
  } else {
    int64_t stmt_timeout = EXECUTE_TIMEOUT_US;
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(replicas.at(0).table_id_, proxy))) {
      LOG_WARN("get partition table proxy failed", K(ret), "table_id", replicas.at(0).table_id_);
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), K(replicas.at(0)));
    } else if (OB_FAIL(timeout_ctx.set_trx_timeout_us(stmt_timeout * (replicas.count() + 2)))) {
      // the start trans and end trans stmt are taken into consideration
      LOG_WARN("fail to set trx timeout", K(ret), K(replicas));
    } else if (OB_FAIL(timeout_ctx.set_timeout(stmt_timeout))) {
      LOG_WARN("set timeout context failed", K(ret));
    } else if (OB_FAIL(proxy->start_trans())) {
      LOG_WARN("fail to start trans", K(ret));
    } else {
      ObPartitionTableProxy* double_check_proxy = NULL;
      FOREACH_CNT_X(replica, replicas, OB_SUCC(ret))
      {
        if (OB_ISNULL(replica)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get invalid replica", K(ret), K(replica));
        } else if (OB_FAIL(factory.get_proxy(replica->table_id_, double_check_proxy))) {
          LOG_WARN("fail to get proxy", K(ret), K(replica));
        } else if (double_check_proxy != proxy) {
          ret = OB_ERR_UNEXPECTED;
          LOG_ERROR("error! get wrong proxy, should not be batch execute", K(ret), K(proxy), K(double_check_proxy));
        } else if (OB_FAIL(timeout_ctx.set_timeout(EXECUTE_TIMEOUT_US))) {
          LOG_WARN("set timeout context failed", K(ret));
        } else if (OB_FAIL(execute(proxy, *replica))) {
          LOG_WARN("fail to update replica", K(ret));
        }
      }
    }
    if (proxy != NULL && proxy->is_trans_started()) {
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = timeout_ctx.set_timeout(stmt_timeout))) {
        LOG_WARN("set timeout context failed", K(tmp_ret));
      }
      int trans_ret = proxy->end_trans(ret);
      if (OB_SUCCESS != trans_ret) {
        LOG_WARN("end trans failed", K(trans_ret));
        ret = OB_SUCCESS == ret ? trans_ret : ret;
      } else {
        LOG_DEBUG("batch execute end trans", K(ret), "end trans ret", trans_ret);
      }
    }
  }
  int64_t cost = ObTimeUtil::current_time() - start_time;
  LOG_DEBUG("batch execute", K(ret), K(cost), "batch count", replicas.count());
  return ret;
}

int ObPersistentPartitionTable::batch_report_partition_role(
    const common::ObIArray<share::ObPartitionReplica>& replica_array, const common::ObRole new_role)
{
  int ret = OB_SUCCESS;
  const int64_t EXECUTE_TIMEOUT_US = 2L * 1000 * 1000;
  ObPartitionTableProxy* proxy = nullptr;
  ObTimeoutCtx timeout_ctx;
  if (OB_UNLIKELY(!is_inited())) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", KR(ret));
  } else if (OB_UNLIKELY(nullptr == GCTX.par_ser_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("partition service ptr is null", KR(ret));
  } else if (OB_UNLIKELY(replica_array.count() <= 0)) {
    // bypass
  } else {
    const int64_t stmt_timeout = EXECUTE_TIMEOUT_US;
    const uint64_t table_id = replica_array.at(0).get_table_id();
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(table_id, proxy))) {
      LOG_WARN("get partition table proxy failed", KR(ret), K(table_id));
    } else if (OB_UNLIKELY(nullptr == proxy)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), K(table_id));
    } else if (OB_FAIL(timeout_ctx.set_trx_timeout_us(stmt_timeout * (replica_array.count() + 2)))) {
      LOG_WARN("fail to set trx timeout", KR(ret), K(replica_array));
    } else if (OB_FAIL(timeout_ctx.set_timeout(stmt_timeout))) {
      LOG_WARN("set timeout context failed", KR(ret));
    } else if (OB_FAIL(proxy->start_trans())) {
      LOG_WARN("fail to start trans", KR(ret));
    } else {
      ObPartitionTableProxy* double_check_proxy = nullptr;
      for (int64_t i = 0; OB_SUCC(ret) && i < replica_array.count(); ++i) {
        const share::ObPartitionReplica& replica = replica_array.at(i);
        if (OB_FAIL(factory.get_proxy(replica.get_table_id(), double_check_proxy))) {
          LOG_WARN("fail to get proxy", KR(ret), "table_id", replica.get_table_id());
        } else if (double_check_proxy != proxy) {
          ret = OB_ERR_UNEXPECTED;
          LOG_ERROR("error! get wrong proxy, should not be batch execute", KR(ret), K(proxy), K(double_check_proxy));
        }
      }
      if (OB_SUCC(ret)) {
        if (OB_FAIL(timeout_ctx.set_timeout(stmt_timeout * replica_array.count()))) {
          LOG_WARN("set timeout context failed", KR(ret));
        } else if (OB_FAIL(proxy->batch_report_partition_role(replica_array, new_role))) {
          LOG_WARN("fail to batch report role", KR(ret), K(replica_array));
        }
      }
    }
    if (proxy != nullptr && proxy->is_trans_started()) {
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = timeout_ctx.set_timeout(stmt_timeout))) {
        LOG_WARN("set timeout context failed", KR(tmp_ret));
      }
      int trans_ret = proxy->end_trans(ret);
      if (OB_SUCCESS != trans_ret) {
        LOG_WARN("end trans failed", K(trans_ret));
        ret = OB_SUCCESS == ret ? trans_ret : ret;
      }
    }
  }
  return ret;
}

int ObPersistentPartitionTable::batch_report_with_optimization(
    const ObIArray<ObPartitionReplica>& replicas, const bool with_role)
{
  int ret = OB_SUCCESS;
  const int64_t EXECUTE_TIMEOUT_US = 2L * 1000 * 1000;
  int64_t start_time = ObTimeUtil::current_time();
  ObPartitionTableProxy* proxy = NULL;
  ObTimeoutCtx timeout_ctx;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_UNLIKELY(replicas.count() <= 0)) {
    // nothing to do
  } else {
    int64_t stmt_timeout = EXECUTE_TIMEOUT_US;
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(replicas.at(0).table_id_, proxy))) {
      LOG_WARN("get partition table proxy failed", K(ret), "table_id", replicas.at(0).table_id_);
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), K(replicas.at(0)));
    } else if (OB_FAIL(timeout_ctx.set_trx_timeout_us(stmt_timeout * (replicas.count() + 2)))) {
      // the start trans and end trans stmt are taken into consideration
      LOG_WARN("fail to set trx timeout", K(ret), K(replicas));
    } else if (OB_FAIL(timeout_ctx.set_timeout(stmt_timeout))) {
      LOG_WARN("set timeout context failed", K(ret));
    } else if (OB_FAIL(proxy->start_trans())) {
      LOG_WARN("fail to start trans", K(ret));
    } else {
      ObPartitionTableProxy* double_check_proxy = NULL;
      FOREACH_CNT_X(replica, replicas, OB_SUCC(ret))
      {
        if (OB_ISNULL(replica)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("get invalid replica", K(ret), K(replica));
        } else if (OB_ALL_CORE_TABLE_TID == extract_pure_id(replica->table_id_) ||
                   OB_ALL_ROOT_TABLE_TID == extract_pure_id(replica->table_id_) ||
                   (replica->is_leader_like() && (with_role || replica->need_force_full_report())) ||
                   replica->is_remove_) {
          ret = OB_INVALID_ARGUMENT;
          LOG_WARN("invalid replica", K(ret), K(replica));
        } else if (OB_FAIL(factory.get_proxy(replica->table_id_, double_check_proxy))) {
          LOG_WARN("fail to get proxy", K(ret), K(replica));
        } else if (double_check_proxy != proxy) {
          ret = OB_ERR_UNEXPECTED;
          LOG_ERROR("error! get wrong proxy, should not be batch execute", K(ret), K(proxy), K(double_check_proxy));
        }
      }
    }
    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(timeout_ctx.set_timeout(stmt_timeout * replicas.count()))) {
      LOG_WARN("set timeout context failed", K(ret));
    } else if (OB_FAIL(proxy->batch_report_with_optimization(replicas, with_role))) {
      LOG_WARN("fail to batch report follower", K(ret));
    }
    if (proxy != NULL && proxy->is_trans_started()) {
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = timeout_ctx.set_timeout(stmt_timeout))) {
        LOG_WARN("set timeout context failed", K(tmp_ret));
      }
      int trans_ret = proxy->end_trans(ret);
      if (OB_SUCCESS != trans_ret) {
        LOG_WARN("end trans failed", K(trans_ret));
        ret = OB_SUCCESS == ret ? trans_ret : ret;
      } else {
        LOG_DEBUG("batch execute end trans", K(ret), "end trans ret", trans_ret);
      }
    }
  }
  int64_t cost = ObTimeUtil::current_time() - start_time;
  LOG_DEBUG("batch report follower", K(ret), K(cost), "batch count", replicas.count());
  return ret;
}

int ObPersistentPartitionTable::execute(ObPartitionTableProxy* proxy, const ObPartitionReplica& replica)
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(proxy) || OB_ISNULL(GCTX.par_ser_)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(proxy), K(GCTX.par_ser_));
  } else {
    if (replica.is_remove_) {
      if (OB_FAIL(proxy->remove(replica.table_id_, replica.partition_id_, GCTX.self_addr_))) {
        LOG_WARN(
            "fail to remove partition", K(ret), "table_id", replica.table_id_, "paritition id", replica.partition_id_);
      }
    } else if (!replica.is_valid()) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invaid replica for update", K(ret), K(replica));
    } else {
      ObPartitionReplica new_replica;
      if (OB_FAIL(new_replica.assign(replica))) {
        LOG_WARN("failed to assign new_replica", K(ret));
      } else {
        ObRole new_role = INVALID_ROLE;
        if (replica.is_leader_like()) {
          if (replica.data_version_ < 0) {
            ret = OB_INVALID_ARGUMENT;
            LOG_WARN("data_version must > 0 for leader replica", K(ret), K(replica));
          } else if (replica.to_leader_time_ <= 0) {
            ret = OB_INVALID_ARGUMENT;
483 484 485
            LOG_WARN("change to leader time must > 0 for leader replica",
                     K(ret), K(replica));
          } else if (OB_FAIL(GCTX.par_ser_->get_role(replica.partition_key(), new_role))) {
O
oceanbase-admin 已提交
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
            ret = OB_SUCCESS;
            new_replica.role_ = FOLLOWER;
          } else if (is_follower(new_role)) {
            new_replica.role_ = FOLLOWER;
          } else {
          }
        }
      }
      if (OB_SUCC(ret)) {
        if (OB_FAIL(proxy->update_replica(new_replica))) {
          LOG_WARN("fail to update replica", K(ret), K(replica));
        }
      }
    }
  }
  return ret;
}

int ObPersistentPartitionTable::update(const ObPartitionReplica& replica)
{
  int ret = OB_SUCCESS;
  ObPartitionTableProxy* proxy = NULL;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!replica.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(replica));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(replica.table_id_, proxy))) {
      LOG_WARN("get partition table proxy failed", K(ret), "table_id", replica.table_id_);
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), "table_id", replica.table_id_);
    } else {
      ObPartitionInfo partition;
      ObArenaAllocator allocator(ObModIds::OB_RS_PARTITION_TABLE_TEMP);
      partition.set_allocator(&allocator);

      const bool lock_replica =
          (replica.is_leader_like() || OB_ALL_ROOT_TABLE_TID == extract_pure_id(replica.table_id_));
      if (lock_replica) {
        if (OB_FAIL(proxy->start_trans())) {
          LOG_WARN("start transaction failed", K(ret));
        }
      }

      const bool filter_flag_replica = false;
      if (OB_FAIL(ret)) {
      } else if (OB_FAIL(proxy->fetch_partition_info(
                     lock_replica, replica.table_id_, replica.partition_id_, filter_flag_replica, partition))) {
        LOG_WARN(
            "get partition failed", K(ret), K_(replica.table_id), K_(replica.partition_id), K(filter_flag_replica));
      } else {
        ObPartitionReplica new_replica;
        // replica's life cycle is larger than new_replica
        // get unit_id if replica exist
        const ObPartitionReplica* inner_replica = NULL;
        if (OB_FAIL(new_replica.assign(replica))) {
          LOG_WARN("failed to assign new_replica", K(ret));
        } else if (OB_FAIL(partition.find(replica.server_, inner_replica))) {
          if (OB_ENTRY_NOT_EXIST == ret) {
            ret = OB_SUCCESS;
          } else {
            LOG_WARN("find server replica failed", K(ret), "server", replica.server_);
          }
        } else if (NULL == inner_replica) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("NULL replica", K(ret));
        } else {
          new_replica.is_original_leader_ = inner_replica->is_original_leader_;
          if (is_follower(new_replica.role_)) {
            new_replica.to_leader_time_ = inner_replica->to_leader_time_;
          }
          if (OB_INVALID_ID == new_replica.unit_id_) {
            new_replica.unit_id_ = inner_replica->unit_id_;
          }
        }

        if (OB_SUCC(ret)) {
          if (new_replica.is_leader_like()) {
Z
zs0 已提交
568 569 570 571 572 573
            if (new_replica.data_version_ <= 0) {
              ret = OB_INVALID_ARGUMENT;
              LOG_WARN("data_version must > 0 for leader replica", K(ret), K(new_replica));
            } else if (new_replica.to_leader_time_ <= 0) {
              ret = OB_INVALID_ARGUMENT;
              LOG_WARN("change to leader time must > 0 for leader replica", K(ret), K(new_replica));
O
oceanbase-admin 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
            } else if (OB_FAIL(update_leader_replica(*proxy, partition, new_replica))) {
              LOG_WARN("update leader replica failed", K(ret), K(new_replica));
            }
          } else {
            if (OB_FAIL(update_follower_replica(*proxy, partition, new_replica))) {
              LOG_WARN("update follower replica failed", K(ret), K(new_replica));
            }
          }
        }
      }

      if (proxy->is_trans_started()) {
        int trans_ret = proxy->end_trans(ret);
        if (OB_SUCCESS != trans_ret) {
          LOG_WARN("end trans failed", K(trans_ret));
          ret = OB_SUCCESS == ret ? trans_ret : ret;
        }
      }
    }
  }
  return ret;
}

int ObPersistentPartitionTable::set_unit_id(
    const uint64_t table_id, const int64_t partition_id, const ObAddr& server, const uint64_t unit_id)
{
  int ret = OB_SUCCESS;
  ObPartitionTableProxy* proxy = NULL;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!is_valid_key(table_id, partition_id) || !server.is_valid() || OB_INVALID_ID == unit_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", KT(table_id), K(partition_id), K(server), K(unit_id), K(ret));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(table_id, proxy))) {
      LOG_WARN("get partition table failed", K(ret), KT(table_id));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), KT(table_id));
    } else if (OB_FAIL(proxy->set_unit_id(table_id, partition_id, server, unit_id))) {
      LOG_WARN("set unit failed", K(ret), KT(table_id), K(partition_id), K(server), K(unit_id));
    }
  }
  return ret;
}

int ObPersistentPartitionTable::update_rebuild_flag(
    const uint64_t table_id, const int64_t partition_id, const common::ObAddr& server, const bool rebuild)
{
  int ret = OB_SUCCESS;
  ObPartitionTableProxy* proxy = NULL;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!is_valid_key(table_id, partition_id) || !server.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", KT(table_id), K(partition_id), K(server), K(ret));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(table_id, proxy))) {
      LOG_WARN("get partition table failed", K(ret), KT(table_id));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), KT(table_id));
    } else if (OB_FAIL(proxy->update_rebuild_flag(table_id, partition_id, server, rebuild))) {
      LOG_WARN("update rebuild flag failed", K(ret), KT(table_id), K(partition_id), K(server), K(rebuild));
    }
  }
  return ret;
}

int ObPersistentPartitionTable::update_fail_list(const uint64_t table_id, const int64_t partition_id,
    const common::ObAddr& server, const ObPartitionReplica::FailList& fail_list)
{
  int ret = OB_SUCCESS;
  ObPartitionTableProxy* proxy = NULL;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!is_valid_key(table_id, partition_id) || !server.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", KT(table_id), K(partition_id), K(server), K(ret));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(table_id, proxy))) {
      LOG_WARN("get partition table failed", K(ret), KT(table_id));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), KT(table_id));
    } else if (OB_FAIL(proxy->update_fail_list(table_id, partition_id, server, fail_list))) {
      LOG_WARN("update rebuild flag failed", K(ret), KT(table_id), K(partition_id), K(server), K(fail_list));
    }
  }
  return ret;
}

int ObPersistentPartitionTable::handover_partition(
    const common::ObPGKey& pg_key, const common::ObAddr& src_addr, const common::ObAddr& dest_addr)
{
  int ret = OB_SUCCESS;
  ObPartitionTableProxy* proxy = NULL;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!pg_key.is_valid() || !src_addr.is_valid() || !dest_addr.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(pg_key), K(src_addr), K(dest_addr));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(pg_key.get_table_id(), proxy))) {
      LOG_WARN("get partition table failed", K(ret), K(pg_key));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), K(pg_key));
    } else if (OB_FAIL(proxy->handover_partition(pg_key, src_addr, dest_addr))) {
      LOG_WARN("handover partition failed", K(ret), K(pg_key), K(src_addr), K(dest_addr));
    }
  }
  return ret;
}

int ObPersistentPartitionTable::replace_partition(
    const ObPartitionReplica& replica, const common::ObAddr& src_addr, const common::ObAddr& dest_addr)
{
  int ret = OB_SUCCESS;
  ObPartitionTableProxy* proxy = NULL;
  const ObPGKey pg_key = replica.partition_key();
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!replica.is_valid() || !src_addr.is_valid() || !dest_addr.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(pg_key), K(replica), K(src_addr), K(dest_addr));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(pg_key.get_table_id(), proxy))) {
      LOG_WARN("get partition table failed", K(ret), K(pg_key));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), K(pg_key));
    } else if (OB_FAIL(proxy->replace_partition(replica, src_addr, dest_addr))) {
      LOG_WARN("handover partition failed", K(ret), K(pg_key), K(replica), K(src_addr), K(dest_addr));
    }
  }
  return ret;
}

int ObPersistentPartitionTable::set_original_leader(
    const uint64_t table_id, const int64_t partition_id, const bool is_original_leader)
{
  int ret = OB_SUCCESS;
  ObPartitionTableProxy* proxy = NULL;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!is_valid_key(table_id, partition_id)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), KT(table_id), K(partition_id), K(is_original_leader));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(table_id, proxy))) {
      LOG_WARN("get partition table failed", K(ret), KT(table_id));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), KT(table_id));
    } else if OB_FAIL (proxy->set_original_leader(table_id, partition_id, is_original_leader)) {
      LOG_WARN("update original leader flag failed", K(ret), KT(table_id), K(partition_id), K(is_original_leader));
    }
  }
  return ret;
}

int ObPersistentPartitionTable::update_follower_replica(
    ObPartitionTableProxy& pt_proxy, ObPartitionInfo& partition, const ObPartitionReplica& replica)
{
  int ret = OB_SUCCESS;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!partition.is_valid() || !replica.is_valid() || !is_follower(replica.role_)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(partition), K(replica));
  } else if (OB_FAIL(update_replica(pt_proxy, partition, replica))) {
    LOG_WARN("update replica failed", K(ret), K(partition), K(replica));
  }
  return ret;
}

int ObPersistentPartitionTable::update_replica(
    ObPartitionTableProxy& pt_proxy, ObPartitionInfo& partition, const ObPartitionReplica& replica)
{
  int ret = OB_SUCCESS;
  bool replace = false;
  const ObPartitionReplica* old = NULL;
  // FIXME : remove replace flag, because log replica always first inserted
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!partition.is_valid() || !replica.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(partition), K(replica));
  } else if (OB_FAIL(partition.find(replica.server_, old))) {
    if (OB_ENTRY_NOT_EXIST != ret) {
      LOG_WARN("find server replica failed", K(ret), "server", replica.server_);
    } else {
      LOG_INFO("insert new replica", K(replica));
      replace = !replica.is_flag_replica();
      if (OB_FAIL(pt_proxy.update_replica(replica, replace))) {
        LOG_WARN("insert replica failed", K(ret), K(replica));
      }
    }
  } else if (NULL == old) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("NULL replica", K(ret));
  } else {
    bool need_update = false;
    if (replica.is_flag_replica()) {
      LOG_WARN("log replica not the first inserted replica, ignore", K(ret), K(replica));
    } else if (OB_FAIL(check_need_update(*old, replica, need_update))) {
      LOG_WARN("check need update failed", K(ret), "old", *old, K(replica));
    } else if (need_update) {
      LOG_INFO("update replica", "old_replica", *old, K(replica));
      replace = true;
      if (OB_FAIL(partition.add_replica(replica))) {
        LOG_WARN("partition add replica failed", K(ret), K(partition), K(replica));
      } else if (OB_FAIL(pt_proxy.update_replica(replica, replace))) {
        LOG_WARN("update replica failed", K(ret), K(replica));
      }
    }
  }
  return ret;
}

int ObPersistentPartitionTable::get_partition_info(const uint64_t table_id, const int64_t partition_id,
    const bool filter_flag_replica, ObPartitionInfo& partition_info, const bool need_fetch_faillist,
    const int64_t cluster_id)
{
  int ret = OB_SUCCESS;
  const bool lock_replica = false;
  ObPartitionTableProxy* proxy = NULL;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!is_valid_key(table_id, partition_id) || NULL == partition_info.get_allocator()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN(
        "invalid argument", K(ret), KT(table_id), K(partition_id), "allocator", OB_P(partition_info.get_allocator()));
  } else {
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(table_id, proxy))) {
      LOG_WARN("get partition table proxy failed", K(ret), KT(table_id));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), KT(table_id));
    } else if (OB_FAIL(proxy->fetch_partition_info(lock_replica,
                   table_id,
                   partition_id,
                   filter_flag_replica,
                   partition_info,
                   need_fetch_faillist,
                   cluster_id))) {
      LOG_WARN(
          "get partition table failed", K(ret), K(cluster_id), KT(table_id), K(partition_id), K(filter_flag_replica));
    }
  }
  return ret;
}

int ObPersistentPartitionTable::batch_fetch_partition_infos(const common::ObIArray<common::ObPartitionKey>& keys,
    common::ObIAllocator& allocator, common::ObArray<ObPartitionInfo*>& partitions, const int64_t cluster_id)
{
  int ret = OB_SUCCESS;
  ObPartitionTableProxy* proxy = NULL;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (keys.count() <= 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid key cnt", K(ret));
  } else {
    uint64_t table_id = keys.at(0).get_table_id();
    ObPartitionTableProxyFactory factory(*sql_proxy_, merge_error_cb_, config_);
    if (OB_FAIL(factory.get_proxy(table_id, proxy))) {
      LOG_WARN("get partition table proxy failed", K(ret), KT(table_id));
    } else if (NULL == proxy) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("NULL proxy", K(ret), KT(table_id));
    } else if (OB_FAIL(proxy->batch_fetch_partition_infos(keys, allocator, partitions, cluster_id))) {
      LOG_WARN("batch get partition table failed", K(ret), K(cluster_id), "cnt", keys.count());
    }
  }
  return ret;
}

int ObPersistentPartitionTable::update_leader_replica(
    ObPartitionTableProxy& pt_proxy, ObPartitionInfo& partition, const ObPartitionReplica& replica)
{
  int ret = OB_SUCCESS;
  ObSEArray<ObAddr, ObPartitionReplica::DEFAULT_REPLICA_COUNT> members;
  if (!is_inited()) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (!partition.is_valid() || !replica.is_valid() || !replica.is_leader_like()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(partition), K(replica));
  } else if (!replica.is_restore_leader() &&
             OB_FAIL(prop_getter_->get_leader_member(replica.partition_key(), members))) {
    LOG_WARN("get leader member failed", K(ret), K(replica));
    ObPartitionReplica follower_replica;
    if (OB_FAIL(follower_replica.assign(replica))) {
      LOG_WARN("failed to assign follower_replica", K(ret));
    } else {
      follower_replica.role_ = FOLLOWER;
      // reset ret to avoid infinity retry.
      // TODO : only NOT_LEADER error can do this?
      if (OB_FAIL(update_follower_replica(pt_proxy, partition, follower_replica))) {
        LOG_WARN("update follower replica failed", K(ret), K(replica));
      }
    }
  } else {
    ObPartitionInfo::ReplicaArray& all_replicas = partition.get_replicas_v2();
    // replicas not in leader member
    ObSEArray<ObAddr, ObPartitionReplica::DEFAULT_REPLICA_COUNT> to_remove;
    for (int64_t i = 0; OB_SUCC(ret) && i < all_replicas.count(); ++i) {
      if (partition.is_leader_like(i) && all_replicas.at(i).server_ != replica.server_) {
        LOG_INFO("update replica role to FOLLOWER", "replica", all_replicas.at(i));
        if (OB_FAIL(
                pt_proxy.set_to_follower_role(replica.table_id_, replica.partition_id_, all_replicas.at(i).server_))) {
          LOG_WARN("update replica role to follower failed", K(ret), "replica", all_replicas.at(i));
        }
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(update_replica(pt_proxy, partition, replica))) {
        LOG_WARN("add replica failed", K(ret), K(replica));
      }
    }
  }
  return ret;
}
}  // end namespace share
}  // end namespace oceanbase