“b2c99e3c70d77fb194df5aa1642030080d28ea48”上不存在“arch/x86/kernel/efi_32.c”
ob_basic_session_info.cpp 162.2 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
/**
 * 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 SQL_SESSION

#include "sql/session/ob_basic_session_info.h"

#include "lib/string/ob_sql_string.h"
#include "lib/timezone/ob_oracle_format_models.h"
#include "common/sql_mode/ob_sql_mode_utils.h"
#include "common/ob_smart_call.h"
#include "share/config/ob_server_config.h"
#include "share/schema/ob_schema_getter_guard.h"
#include "share/schema/ob_sys_variable_mgr.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/plan_cache/ob_plan_cache_util.h"
#include "sql/plan_cache/ob_prepare_stmt_struct.h"
#include "sql/ob_sql_trans_util.h"
#include "observer/ob_server_struct.h"
#include "sql/engine/ob_physical_plan.h"
#include "storage/transaction/ob_weak_read_util.h"  //ObWeakReadUtil
#include "observer/omt/ob_tenant_timezone_mgr.h"
32
#include "observer/omt/ob_tenant_config_mgr.h"
O
oceanbase-admin 已提交
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

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

namespace oceanbase {
namespace sql {

ObBasicSessionInfo::ObBasicSessionInfo()
    : query_mutex_(),
      thread_data_mutex_(),
      is_valid_(true),
      is_deserialized_(false),
      tenant_id_(OB_INVALID_ID),
      effective_tenant_id_(OB_INVALID_ID),
      rpc_tenant_id_(0),
      is_changed_to_temp_tenant_(false),
      user_id_(OB_INVALID_ID),
      client_version_(),
      driver_version_(),
      sessid_(0),
      master_sessid_(INVALID_SESSID),
      version_(0),
      proxy_sessid_(VALID_PROXY_SESSID),
      //      in_transaction_(false),
      variables_last_modify_time_(0),
      global_vars_version_(0),
      sys_var_base_version_(OB_INVALID_VERSION),
      des_sys_var_base_version_(OB_INVALID_VERSION),
      trans_desc_(),
      trans_consistency_type_(transaction::ObTransConsistencyType::UNKNOWN),
      // minus 32 is used for alignment of ObMalloc, preventing memory allocation more than 8K.
      block_allocator_(
          SMALL_BLOCK_SIZE, common::OB_MALLOC_NORMAL_BLOCK_SIZE - 32, ObMalloc(ObModIds::OB_SQL_SESSION_SBLOCK)),
      ps_session_info_allocator_(
          sizeof(ObPsSessionInfo), common::OB_MALLOC_NORMAL_BLOCK_SIZE - 32, ObMalloc("PsSessionInfo")),
      name_pool_(ObModIds::OB_SQL_SESSION, OB_MALLOC_NORMAL_BLOCK_SIZE),
      base_sys_var_alloc_(ObModIds::OB_SQL_SESSION, OB_MALLOC_NORMAL_BLOCK_SIZE),
      trans_flags_(),
      bucket_allocator_wrapper_(&block_allocator_),
      user_var_val_map_(SMALL_BLOCK_SIZE, ObWrapperAllocator(&block_allocator_)),
      influence_plan_var_indexs_(),
      is_first_gen_(true),
      sys_var_fac_(),
      consistency_level_(INVALID_CONSISTENCY),
      tz_info_wrap_(),
      next_tx_read_only_(-1),
      next_tx_isolation_(transaction::ObTransIsolation::UNKNOWN),
      log_id_level_map_valid_(false),
      cur_phy_plan_(NULL),
      capability_(),
      proxy_capability_(),
      client_mode_(OB_MIN_CLIENT_MODE),
      changed_sys_vars_(),
      changed_user_vars_(),
      changed_var_pool_(ObModIds::OB_SQL_SESSION, OB_MALLOC_NORMAL_BLOCK_SIZE),
      is_database_changed_(false),
      feedback_manager_(),
      trans_spec_status_(TRANS_SPEC_NOT_SET),
      debug_sync_actions_(),
      partition_hit_(),
      magic_num_(0x13572468),
      current_execution_id_(-1),
      database_id_(OB_INVALID_ID),
      retry_info_(),
      last_query_trace_id_(),
      nested_count_(-1),
      safe_weak_read_snapshot_(0),
      weak_read_snapshot_source_(0),
      curr_trans_last_stmt_end_time_(0),
      read_snapshot_version_(OB_INVALID_VERSION),
      check_sys_variable_(true),
      is_foreign_key_cascade_(false),
      is_foreign_key_check_exist_(false),
      acquire_from_pool_(false),
      release_to_pool_(true),
      is_tenant_killed_(0),
      reused_count_(0),
      first_stmt_type_(stmt::T_NONE),
      exec_min_cluster_version_(GET_MIN_CLUSTER_VERSION()),
      stmt_type_(stmt::T_NONE),
      thread_id_(0),
      is_password_expired_(false)
{
  MEMSET(sys_vars_, 0, sizeof(sys_vars_));
  log_id_level_map_.reset_level();
  CHAR_CARRAY_INIT(tenant_);
  CHAR_CARRAY_INIT(effective_tenant_);
  CHAR_CARRAY_INIT(trace_id_buff_);
  ssl_cipher_buff_[0] = '\0';
}

ObBasicSessionInfo::~ObBasicSessionInfo()
{
  destroy();
}

bool ObBasicSessionInfo::is_server_status_in_transaction() const
{
133 134 135 136 137 138 139 140 141 142 143 144
  /*!
   * readonly sql not in transaction, for compatible, we also need send in trans flag to proxy.
   * for now,
   * we use ob_proxy_readonly_transaction_routing_policy parameter decide to send in trans or not.
   */
  bool result = get_in_transaction();
  if (!result
      && !get_local_autocommit()
      && trans_desc_.get_standalone_stmt_desc().is_snapshot_version_valid()) {
    omt::ObTenantConfigGuard tenant_config(TENANT_CONF(get_effective_tenant_id()));
    result = is_isolation_serializable() || tenant_config->ob_proxy_readonly_transaction_routing_policy;
  }
O
oceanbase-admin 已提交
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
  return result;
}

// for test
int ObBasicSessionInfo::test_init(
    uint32_t version, uint32_t sessid, uint64_t proxy_sessid, common::ObIAllocator* bucket_allocator)
{
  int ret = OB_SUCCESS;
  if (NULL != bucket_allocator) {
    bucket_allocator_wrapper_.set_alloc(bucket_allocator);
  }

  ret = user_var_val_map_.init(1024 * 1024 * 2,
      256,  // # of user variables
      (NULL == bucket_allocator ? NULL : &bucket_allocator_wrapper_));
  if (OB_FAIL(ret)) {
    LOG_WARN("fail to init user_var_val_map", K(ret));
  } else if (OB_FAIL(debug_sync_actions_.init(SMALL_BLOCK_SIZE, bucket_allocator_wrapper_))) {
    LOG_WARN("fail to init debug sync actions", K(ret));
  } else if (OB_FAIL(set_session_state(SESSION_INIT))) {
    LOG_WARN("fail to set session stat", K(ret));
  } else if (OB_FAIL(trans_desc_.test_init())) {
    LOG_WARN("transaction desc init error", K(ret));
  } else if (OB_FAIL(set_time_zone(ObString("+8:00"), is_oracle_compatible(), true /* check_timezone_valid */))) {
    LOG_WARN("fail to set time zone", K(ret));
  } else {
    // tz_info_wrap_.set_tz_info_map(GCTX.tz_info_mgr_->get_tz_info_map());

    version_ = version;
    sessid_ = sessid;
    proxy_sessid_ = proxy_sessid;
  }
  return ret;
}

bool ObBasicSessionInfo::is_use_inner_allocator() const
{
  return bucket_allocator_wrapper_.get_alloc() == &block_allocator_;
}

int ObBasicSessionInfo::init(uint32_t version, uint32_t sessid, uint64_t proxy_sessid,
    common::ObIAllocator* bucket_allocator, const ObTZInfoMap* tz_info)
{
  int ret = OB_SUCCESS;
  ObWrapperAllocator* user_var_allocator_wrapper = NULL;
  if (is_acquire_from_pool()) {
    reused_count_++;
    if (OB_NOT_NULL(bucket_allocator) || !is_use_inner_allocator()) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("session from pool must use inner allocator", K(ret));
    }
  } else {
    if (NULL != bucket_allocator) {
      bucket_allocator_wrapper_.set_alloc(bucket_allocator);
      user_var_allocator_wrapper = &bucket_allocator_wrapper_;
    }
  }
  if (OB_FAIL(ret)) {
  } else if (!is_acquire_from_pool() &&
             OB_FAIL(user_var_val_map_.init(1024 * 1024 * 2, 256, user_var_allocator_wrapper))) {
    LOG_WARN("fail to init user_var_val_map", K(ret));
  } else if (!is_acquire_from_pool() &&
             OB_FAIL(debug_sync_actions_.init(SMALL_BLOCK_SIZE, bucket_allocator_wrapper_))) {
    LOG_WARN("fail to init debug sync actions", K(ret));
  } else if (OB_FAIL(set_session_state(SESSION_INIT))) {
    LOG_WARN("fail to set session stat", K(ret));
  } else if (OB_FAIL(trans_desc_.init(this))) {
    LOG_WARN("transaction desc init error", K(ret));
  } else if (FALSE_IT(trans_result_.set_trans_desc(&trans_desc_))) {
  } else {
    version_ = version;
    sessid_ = sessid;
    proxy_sessid_ = proxy_sessid;
    uint64_t tenant_id = tenant_id_;
    if (OB_ISNULL(tz_info)) {
      ObTZMapWrap tz_map_wrap;
      if (OB_FAIL(OTTZ_MGR.get_tenant_tz(tenant_id, tz_map_wrap))) {
        LOG_WARN("get tenant timezone map failed", K(ret));
      } else {
        tz_info_wrap_.set_tz_info_map(tz_map_wrap.get_tz_map());
      }
    } else {
      tz_info_wrap_.set_tz_info_map(tz_info);
    }
  }
  return ret;
}

int ObBasicSessionInfo::update_safe_weak_read_snapshot(
    const uint64_t tenant_id, const int64_t& safe_weak_read_snapshot, const int64_t& safe_weak_read_snapshot_source)
{
  int ret = OB_SUCCESS;

  if (!ObWeakReadUtil::enable_monotonic_weak_read(tenant_id)) {
    LOG_WARN("observer not support causal order slave read", K(safe_weak_read_snapshot));
  } else if (safe_weak_read_snapshot < 0) {
    TRANS_LOG(WARN, "invalid argument", K(safe_weak_read_snapshot));
    ret = OB_INVALID_ARGUMENT;
  } else {
    if (safe_weak_read_snapshot_ < safe_weak_read_snapshot) {
      safe_weak_read_snapshot_ = safe_weak_read_snapshot;
      weak_read_snapshot_source_ = safe_weak_read_snapshot_source;
    }
  }

  return ret;
}

void ObBasicSessionInfo::destroy()
{
  if (magic_num_ != 0x13572468) {
    LOG_ERROR("ObBasicSessionInfo may be double free!!!", K(magic_num_));
  }
  trans_desc_.destroy();
  trans_result_.reset();
  trans_consistency_type_ = transaction::ObTransConsistencyType::UNKNOWN;
  magic_num_ = 0x86427531;
  if (thread_data_.cur_query_ != nullptr) {
    ob_free(thread_data_.cur_query_);
    thread_data_.cur_query_ = nullptr;
    thread_data_.cur_query_buf_len_ = 0;
  }
  total_stmt_tables_.reset();
  cur_stmt_tables_.reset();
}

void ObBasicSessionInfo::clean_status()
{
  //  in_transaction_ = false;
  trans_flags_.reset();
  trans_spec_status_ = TRANS_SPEC_NOT_SET;
  // trans_desc_.reset();
  trans_consistency_type_ = transaction::ObTransConsistencyType::UNKNOWN;
  set_valid(true);
  thread_data_.cur_query_start_time_ = 0;
  thread_data_.cur_query_len_ = 0;
  thread_data_.last_active_time_ = 0;
  reset_session_changed_info();
}

void ObBasicSessionInfo::reset(bool skip_sys_var)
{
  set_valid(false);
  is_deserialized_ = false;
  CHAR_CARRAY_INIT(tenant_);
  tenant_id_ = OB_INVALID_ID;
  CHAR_CARRAY_INIT(effective_tenant_);
  effective_tenant_id_ = OB_INVALID_ID;
  is_changed_to_temp_tenant_ = false;
  user_id_ = OB_INVALID_ID;
  client_version_.reset();
  driver_version_.reset();
  sessid_ = 0;
  master_sessid_ = INVALID_SESSID;
  version_ = 0;
  proxy_sessid_ = VALID_PROXY_SESSID;
  variables_last_modify_time_ = 0;
  global_vars_version_ = 0;
  des_sys_var_base_version_ = OB_INVALID_VERSION;

  trans_desc_.reset();
  trans_consistency_type_ = transaction::ObTransConsistencyType::UNKNOWN;
  trans_result_.reset();
  total_stmt_tables_.reset();
  cur_stmt_tables_.reset();
  // reset() of user_var_val_map_ and debug_sync_actions_ will keep some memory
  // allocated from block_allocator_ / bucket_allocator_wrapper_, so we skip
  // reset() of block_allocator_ and bucket_allocator_wrapper_.
  // block_allocator_.reset();
  ps_session_info_allocator_.reset();
  trans_flags_.reset();
  // bucket_allocator_wrapper_.reset();
  user_var_val_map_.reuse();
  if (!skip_sys_var) {
    memset(sys_vars_, 0, sizeof(sys_vars_));
    influence_plan_var_indexs_.reset();
  } else {
    const SysVarIds& all_sys_var_ids = sys_var_inc_info_.get_all_sys_var_ids();
    for (int i = 0; i < all_sys_var_ids.count(); i++) {
      int ret = OB_SUCCESS;
      int64_t store_idx = -1;
      ObSysVarClassType sys_var_id = all_sys_var_ids.at(i);
      OZ(ObSysVarFactory::calc_sys_var_store_idx(sys_var_id, store_idx));
      OV(0 <= store_idx && store_idx < ObSysVarFactory::ALL_SYS_VARS_COUNT);
      OV(OB_NOT_NULL(sys_vars_[store_idx]));
      OX(sys_vars_[store_idx]->clean_inc_value());
    }
    sys_var_inc_info_.reset();
    reset_timezone();
  }
  sys_var_in_pc_str_.reset();
  is_first_gen_ = true;
  CHAR_CARRAY_INIT(trace_id_buff_);
  // consistency_level_ = INVALID_CONSISTENCY;
  next_tx_read_only_ = -1;
  next_tx_isolation_ = transaction::ObTransIsolation::UNKNOWN;
  log_id_level_map_valid_ = false;
  log_id_level_map_.reset_level();
  cur_phy_plan_ = NULL;
  capability_.capability_ = 0;
  proxy_capability_.capability_ = 0;
  client_mode_ = OB_MIN_CLIENT_MODE;
  reset_session_changed_info();
  trans_spec_status_ = TRANS_SPEC_NOT_SET;
  debug_sync_actions_.reset();
  partition_hit_.reset();
  // magic_num_ = 0x86427531;
  current_execution_id_ = -1;
  last_trace_id_.reset();
354
  curr_trace_id_.reset();
O
oceanbase-admin 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
  app_trace_id_.reset();
  database_id_ = OB_INVALID_ID;
  retry_info_.reset();
  last_query_trace_id_.reset();
  thread_data_.reset();
  nested_count_ = -1;
  if (!skip_sys_var) {
    sys_vars_cache_.reset();
  } else {
    sys_vars_cache_.clean_inc();
  }
  safe_weak_read_snapshot_ = 0;
  weak_read_snapshot_source_ = 0;
  curr_trans_last_stmt_end_time_ = 0;
  read_snapshot_version_ = OB_INVALID_VERSION;
  check_sys_variable_ = true;
  is_foreign_key_cascade_ = false;
  is_foreign_key_check_exist_ = false;
  acquire_from_pool_ = false;
  // skip release_to_pool_, see comments in .h file.
  is_tenant_killed_ = 0;
  first_stmt_type_ = stmt::T_NONE;
  exec_min_cluster_version_ = GET_MIN_CLUSTER_VERSION();
  thread_id_ = 0;
  is_password_expired_ = false;
  // release all allocators at last, otherwise some members may keep invalid pointers,
  // and for_each_session() of session mgr may core if access these invalid pointers.
  name_pool_.reset();
  if (!skip_sys_var) {
    base_sys_var_alloc_.reset();
    sys_var_fac_.destroy();
  }
}

void ObBasicSessionInfo::reset_timezone()
{
  int ret = OB_SUCCESS;
  ObObj tmp_obj1;
  if (OB_FAIL(get_sys_variable(SYS_VAR_TIME_ZONE, tmp_obj1))) {
    LOG_WARN("get sys var failed", K(ret));
  } else if (OB_FAIL(process_session_time_zone_value(tmp_obj1, false))) {
    LOG_WARN("set time zone failed", K(ret));
  }

  ObObj tmp_obj2;
  if (OB_FAIL(get_sys_variable(SYS_VAR_ERROR_ON_OVERLAP_TIME, tmp_obj2))) {
    LOG_WARN("get sys var failed", K(ret));
  } else if (OB_FAIL(process_session_overlap_time_value(tmp_obj2))) {
    LOG_WARN("process session overlap time value failed", K(ret), K(tmp_obj2));
  }
}

int ObBasicSessionInfo::init_tenant(const ObString& tenant_name, const uint64_t tenant_id)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!is_valid_tenant_id(tenant_id))) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid tenant id", K(tenant_id), K(ret));
  } else if (tenant_name.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("tenant name is empty", K(tenant_name), K(ret));
  } else if (tenant_name.length() > OB_MAX_TENANT_NAME_LENGTH) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("tenant name too long", K(tenant_name), K(ret));
  } else if (OB_FAIL(ob_cstrcopy(tenant_, sizeof(tenant_), tenant_name))) {
    LOG_WARN("failed to copy tenant name", K(tenant_name), K(ret));
  } else if (OB_FAIL(ob_cstrcopy(effective_tenant_, sizeof(effective_tenant_), tenant_name))) {
    LOG_WARN("failed to copy effective tenant name", K(tenant_name), K(ret));
  } else {
    ObTZMapWrap tz_map_wrap;
    if (OB_FAIL(OTTZ_MGR.get_tenant_tz(tenant_id, tz_map_wrap))) {
      LOG_WARN("get tenant timezone map failed", K(ret));
    } else {
      tz_info_wrap_.set_tz_info_map(tz_map_wrap.get_tz_map());
      tenant_id_ = tenant_id;
      effective_tenant_id_ = tenant_id;
      LOG_DEBUG("init session tenant", K(tenant_name), K(tenant_id));
    }
  }
  return ret;
}

int ObBasicSessionInfo::set_tenant(const common::ObString& tenant_name, const uint64_t tenant_id)
{
  int ret = OB_SUCCESS;
  if (!is_valid_tenant_id(tenant_id)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid tenant id", K(tenant_id), K(ret));
  } else if (tenant_name.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("tenant name is empty", K(tenant_name), K(ret));
  } else if (tenant_name.length() > OB_MAX_TENANT_NAME_LENGTH) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("tenant name too long", K(tenant_name), K(ret));
  } else if (OB_FAIL(ob_cstrcopy(tenant_, sizeof(tenant_), tenant_name))) {
    LOG_WARN("tenant name too long", K(tenant_name));
  } else {
    tenant_id_ = tenant_id;
    LOG_TRACE("set tenant", K(tenant_name), K(tenant_id));
  }
  return ret;
}

int ObBasicSessionInfo::set_tenant(const common::ObString& tenant_name, const uint64_t tenant_id, char* ori_tenant_name,
    const uint64_t length, uint64_t& ori_tenant_id)
{
  int ret = OB_SUCCESS;
  if (!is_valid_tenant_id(tenant_id)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid tenant id", K(tenant_id), K(ret));
  } else if (OB_ISNULL(ori_tenant_name)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("ori_tenant_name is NULL", K(ret));
  } else if (length < OB_MAX_TENANT_NAME_LENGTH + 1) {
    ret = OB_INVALID_ARGUMENT_FOR_LENGTH;
    LOG_WARN("tenant_name length is not enough ");
  } else if (tenant_name.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("tenant name is empty", K(tenant_name), K(ret));
  } else if (tenant_name.length() > OB_MAX_TENANT_NAME_LENGTH) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("tenant name too long", K(tenant_name), K(ret));
  } else if (OB_FAIL(ob_cstrcopy(ori_tenant_name, length, tenant_, strlen(tenant_)))) {
    LOG_ERROR("tenant_name is longer than ori_tenant_name", K(length), K(strlen(tenant_)));
  } else if (OB_FAIL(ob_cstrcopy(tenant_, sizeof(tenant_), tenant_name))) {
    LOG_ERROR("tenant name too long", K(tenant_name));
  } else {
    ori_tenant_id = tenant_id_;
    tenant_id_ = tenant_id;
  }
  return ret;
}

int ObBasicSessionInfo::switch_tenant(uint64_t effective_tenant_id)
{
  int ret = OB_SUCCESS;
  if (OB_SYS_TENANT_ID != tenant_id_) {
    ret = OB_NOT_SUPPORTED;
    LOG_WARN("only support sys tenant switch tenant", K(ret), K(tenant_id_), K(effective_tenant_id_));
  } else if (effective_tenant_id <= 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid tenant_id", K(ret), K(effective_tenant_id));
  } else {
#ifndef NDEBUG
    if (effective_tenant_id_ != effective_tenant_id) {
      LOG_INFO(
          "switch tenant", K(effective_tenant_id), K(effective_tenant_id_), "priv_tenant_id", tenant_id_, K(lbt()));
    }
#endif
    effective_tenant_id_ = effective_tenant_id;
  }
  return ret;
}

const common::ObString ObBasicSessionInfo::get_tenant_name() const
{
  return ObString::make_string(tenant_);
}

const common::ObString ObBasicSessionInfo::get_effective_tenant_name() const
{
  return ObString::make_string(effective_tenant_);
}

int ObBasicSessionInfo::set_user(const ObString& user_name, const ObString& host_name, const uint64_t user_id)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(user_name.length() > common::OB_MAX_USER_NAME_LENGTH) ||
      OB_UNLIKELY(host_name.length() > common::OB_MAX_HOST_NAME_LENGTH)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("name length invalid_", K(user_name), K(host_name), K(ret));
  } else {
    char tmp_buf[common::OB_MAX_USER_NAME_LENGTH + common::OB_MAX_HOST_NAME_LENGTH + 2] = {};
    snprintf(tmp_buf,
        sizeof(tmp_buf),
        "%.*s@%.*s",
        user_name.length(),
        user_name.ptr(),
        host_name.length(),
        host_name.ptr());
    ObString tmp_string(tmp_buf);
    LockGuard lock_guard(thread_data_mutex_);
    if (OB_FAIL(name_pool_.write_string(user_name, &thread_data_.user_name_))) {
      LOG_WARN("fail to write username to string_buf_", K(user_name), K(ret));
    } else if (OB_FAIL(name_pool_.write_string(host_name, &thread_data_.host_name_))) {
      LOG_WARN("fail to write hostname to string_buf_", K(host_name), K(ret));
    } else if (OB_FAIL(name_pool_.write_string(tmp_string, &thread_data_.user_at_host_name_))) {
      LOG_WARN("fail to write user_at_host_name to string_buf_", K(tmp_string), K(ret));
    } else {
      user_id_ = user_id;
    }
  }
  return ret;
}

int ObBasicSessionInfo::set_real_client_ip(const common::ObString& client_ip)
{
  int ret = OB_SUCCESS;
  char tmp_buf[common::OB_MAX_USER_NAME_LENGTH + common::OB_MAX_HOST_NAME_LENGTH + 2] = {};
  snprintf(tmp_buf,
      sizeof(tmp_buf),
      "%.*s@%.*s",
      thread_data_.user_name_.length(),
      thread_data_.user_name_.ptr(),
      client_ip.length(),
      client_ip.ptr());
  ObString tmp_string(tmp_buf);
  LockGuard lock_guard(thread_data_mutex_);
  if (OB_FAIL(name_pool_.write_string(client_ip, &thread_data_.client_ip_))) {
    LOG_WARN("fail to write client_ip to string_buf_", K(client_ip), K(ret));
  } else if (OB_FAIL(name_pool_.write_string(tmp_string, &thread_data_.user_at_client_ip_))) {
    LOG_WARN("fail to write user_at_host_name to string_buf_", K(tmp_string), K(ret));
  } else {
    thread_data_.user_client_addr_.set_ip_addr(client_ip, 0);
  }
  return ret;
}

int ObBasicSessionInfo::check_and_init_retry_info(const ObCurTraceId::TraceId& cur_trace_id, const ObString& sql)
{
  int ret = OB_SUCCESS;
  if (last_query_trace_id_.equals(cur_trace_id)) {  // is retrying.
    if (OB_UNLIKELY(!retry_info_.is_inited())) {
      LOG_ERROR("is retry packet, but retry info is not inited, will init it",
          K(last_query_trace_id_),
          K(cur_trace_id),
          K(retry_info_),
          K(get_sessid()),
          K(sql));
      if (OB_FAIL(retry_info_.init())) {
        LOG_WARN("fail to init retry info", K(ret), K(retry_info_), K(sql));
      }
    }
  } else {
    if (OB_UNLIKELY(retry_info_.is_inited())) {
      retry_info_.reset();
    }
    if (OB_FAIL(retry_info_.init())) {
      LOG_WARN("fail to init retry info", K(ret), K(retry_info_), K(sql));
    } else {
      last_query_trace_id_.set(cur_trace_id);
    }
  }
  return ret;
}

void ObBasicSessionInfo::check_and_reset_retry_info(const ObCurTraceId::TraceId& cur_trace_id, bool is_packet_retry)
{
  if (!is_packet_retry) {
    retry_info_.reset();
  }
  last_query_trace_id_.set(cur_trace_id);
}

const ObLogIdLevelMap* ObBasicSessionInfo::get_log_id_level_map() const
{
  return (log_id_level_map_valid_ ? (&log_id_level_map_) : NULL);
}

int ObBasicSessionInfo::set_client_version(const ObString& client_version)
{
  int ret = OB_SUCCESS;
  if (client_version.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("client version is empty", K(client_version), K(ret));
  } else if (OB_FAIL(name_pool_.write_string(client_version, &client_version_))) {
    LOG_WARN("failed to write client_version to string_buf_", K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::set_driver_version(const ObString& driver_version)
{
  int ret = OB_SUCCESS;
  if (driver_version.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("driver version is empty", K(driver_version), K(ret));
  } else if (OB_FAIL(name_pool_.write_string(driver_version, &driver_version_))) {
    LOG_WARN("failed to write driver_version to string_buf_", K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::set_default_database(
    const ObString& database_name, const ObCollationType coll_type /*= CS_TYPE_INVALID */)
{
  int ret = OB_SUCCESS;
644
  if (database_name.length() > OB_MAX_DATABASE_NAME_LENGTH * OB_MAX_CHAR_LEN) {
O
oceanbase-admin 已提交
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
    ret = OB_INVALID_ARGUMENT_FOR_LENGTH;
    LOG_WARN("invalid length for database_name", K(database_name), K(ret));
  } else {
    if (CS_TYPE_INVALID != coll_type) {
      const int64_t coll_val = static_cast<int64_t>(coll_type);
      if (OB_FAIL(update_sys_variable(SYS_VAR_CHARACTER_SET_DATABASE, coll_val))) {
        LOG_WARN("failed to update variable", K(ret));
      } else if (OB_FAIL(update_sys_variable(SYS_VAR_COLLATION_DATABASE, coll_val))) {
        LOG_WARN("failed to update variable", K(ret));
      } else {
      }
    }

    if (OB_SUCC(ret)) {
      LockGuard lock_guard(thread_data_mutex_);
      MEMCPY(thread_data_.database_name_, database_name.ptr(), database_name.length());
      thread_data_.database_name_[database_name.length()] = '\0';
      if (is_track_session_info()) {
        is_database_changed_ = true;
      }
    }
  }
  return ret;
}

int ObBasicSessionInfo::update_database_variables(ObSchemaGetterGuard* schema_guard)
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(schema_guard)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid schema guard is NULL", K(ret));
  } else {
    if ('\0' == thread_data_.database_name_[0]) {
      // no default database
      ObObj val;
      if (OB_FAIL(get_sys_variable(SYS_VAR_CHARACTER_SET_SERVER, val))) {
        LOG_WARN("failed to get sys variable", K(ret));
      } else if (OB_FAIL(update_sys_variable(SYS_VAR_CHARACTER_SET_DATABASE, val))) {
        LOG_WARN("failed to update sys variable", K(ret));
      } else if (OB_FAIL(get_sys_variable(SYS_VAR_COLLATION_SERVER, val))) {
        LOG_WARN("failed to get sys variable", K(ret));
      } else if (OB_FAIL(update_sys_variable(SYS_VAR_COLLATION_DATABASE, val))) {
        LOG_WARN("failed to update sys variable", K(ret));
      } else {
      }
    } else {
      const share::schema::ObDatabaseSchema* db_schema = NULL;
      ObString db_name(thread_data_.database_name_);
      if (OB_FAIL(schema_guard->get_database_schema(effective_tenant_id_, db_name, db_schema))) {
        LOG_WARN("get database schema failed", K(effective_tenant_id_), K(db_name), K(ret));
      } else if (NULL == db_schema) {
        ret = OB_ERR_BAD_DATABASE;
        LOG_WARN("database not exist", K(effective_tenant_id_), K(db_name), K(ret));
        LOG_USER_ERROR(OB_ERR_BAD_DATABASE, db_name.length(), db_name.ptr());
      } else {
        const int64_t db_coll = static_cast<int64_t>(db_schema->get_collation_type());
        if (OB_FAIL(update_sys_variable(SYS_VAR_CHARACTER_SET_DATABASE, db_coll))) {
          LOG_WARN("failed to update sys variable", K(ret));
        } else if (OB_FAIL(update_sys_variable(SYS_VAR_COLLATION_DATABASE, db_coll))) {
          LOG_WARN("failed to update sys variable", K(ret));
        } else {
        }
      }
    }
  }

  return ret;
}

int ObBasicSessionInfo::update_max_packet_size()
{
  int ret = OB_SUCCESS;
  int64_t max_allowed_pkt = 0;
  int64_t net_buffer_len = 0;
  if (OB_FAIL(get_max_allowed_packet(max_allowed_pkt))) {
    LOG_WARN("fail to get_max_allowed_packet", K(ret));
  } else if (OB_FAIL(get_net_buffer_length(net_buffer_len))) {
    LOG_WARN("fail to get_net_buffer_length", K(ret));
  } else {
    thread_data_.max_packet_size_ = std::max(max_allowed_pkt, net_buffer_len);
  }
  return ret;
}

const ObString ObBasicSessionInfo::get_database_name() const
{
  ObString str_ret;
  str_ret.assign_ptr(
      const_cast<char*>(thread_data_.database_name_), static_cast<int32_t>(strlen(thread_data_.database_name_)));
  return str_ret;
}

//// FIXME:
// int ObBasicSessionInfo::get_database_id(
//    ObSchemaGetterGuard *schema_guard,
//    uint64_t &db_id) const
//{
//  int ret = OB_SUCCESS;
//  db_id = OB_INVALID_ID;
//  if (get_database_name().empty()) {
//    //do nothing
//  } else if (OB_UNLIKELY(NULL == schema_guard)) {
//    ret = OB_INVALID_ARGUMENT;
//    LOG_WARN("Schema guard should not be NULL", K(ret));
//  } else if (OB_FAIL(schema_guard->get_database_id(get_effective_tenant_id(), get_database_name(),
//                                                     db_id))) {
//    db_id = OB_INVALID_ID;
//    LOG_WARN("failed to get database id", K(db_id), K(ret));
//  } else { }//do nothing
//  return ret;
//}

////////////////////////////////////////////////////////////////
int ObBasicSessionInfo::get_global_sys_variable(
    const ObBasicSessionInfo* session, ObIAllocator& calc_buf, const ObString& var_name, ObObj& val)
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(session)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("session is NULL", K(ret), K(var_name));
  } else {
    // const ObDataTypeCastParams dtc_params(session->get_timezone_info(),
    //                                      session->get_local_nls_formats(),
    //                                      session->get_nls_collation(),
    //                                      session->get_nls_collation_nation());
    ObDataTypeCastParams dtc_params = session->get_dtc_params();
    if (OB_FAIL(get_global_sys_variable(session->get_effective_tenant_id(), calc_buf, dtc_params, var_name, val))) {
      LOG_WARN("fail to get global sys variable", K(ret), K(var_name));
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_global_sys_variable(const uint64_t actual_tenant_id,  // tenant may be switched
    ObIAllocator& calc_buf, const ObDataTypeCastParams& dtc_params, const ObString& var_name, ObObj& val)
{
  int ret = OB_SUCCESS;
  ObSchemaGetterGuard schema_guard;
  const ObTenantSchema* tenant_schema = NULL;
  const ObSysVarSchema* sysvar_schema = NULL;
  const ObSysVariableSchema* sys_variable_schema = NULL;
  if (OB_UNLIKELY(!is_valid_tenant_id(actual_tenant_id))) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid tenant id", K(actual_tenant_id), K(ret));
  } else if (OB_ISNULL(GCTX.schema_service_)) {
    ret = OB_INVALID_ARGUMENT;
    OB_LOG(WARN, "invalid argument", K(GCTX.schema_service_));
  } else if (OB_FAIL(GCTX.schema_service_->get_tenant_schema_guard(actual_tenant_id, schema_guard))) {
    ret = OB_SCHEMA_ERROR;
    OB_LOG(WARN, "fail get schema guard", K(ret));
  } else if (OB_FAIL(schema_guard.get_tenant_info(actual_tenant_id, tenant_schema))) {
    LOG_WARN("get tenant info failed", K(ret), K(actual_tenant_id));
  } else if (OB_ISNULL(tenant_schema)) {
    ret = OB_SCHEMA_ERROR;
    LOG_WARN("tenant_schema is NULL", K(ret));
  } else if (OB_FAIL(schema_guard.get_sys_variable_schema(actual_tenant_id, sys_variable_schema))) {
    LOG_WARN("get sys variable schema failed", K(ret));
  } else if (OB_ISNULL(sys_variable_schema)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("sys variable schema is null", K(ret));
  } else if (OB_FAIL(sys_variable_schema->get_sysvar_schema(var_name, sysvar_schema))) {
    ret = OB_ERR_SYS_VARIABLE_UNKNOWN;
    LOG_WARN("failed to get sysvar", K(ret), K(var_name));
  } else if (OB_ISNULL(sysvar_schema)) {
    ret = OB_SCHEMA_ERROR;
    LOG_WARN("tenant_schema is NULL", K(ret));
  } else if (OB_FAIL(sysvar_schema->get_value(&calc_buf, dtc_params, val))) {
    LOG_WARN("failed to get value", K(ret), K(var_name));
  } else if (OB_FAIL(ObBasicSessionInfo::change_value_for_special_sys_var(var_name, val, val))) {
    LOG_ERROR("fail to change value for special sys var", K(ret), K(var_name), K(val));
  } else {
    LOG_DEBUG("get global sysvar", K(var_name), K(val));
  }
  return ret;
}

const ObBasicSysVar* ObBasicSessionInfo::get_sys_var(const int64_t idx) const
{
  const ObBasicSysVar* var = NULL;
  if (idx >= 0 && idx < ObSysVarFactory::ALL_SYS_VARS_COUNT) {
    var = sys_vars_[idx];
  }
  return var;
}

int ObBasicSessionInfo::init_system_variables(const bool print_info_log, const bool is_sys_tenant)
{
  int ret = OB_SUCCESS;
  ObString name;
  ObObj type;
  ObObj value;
  ObObj min_val;
  ObObj max_val;
  ObObjType var_type = ObNullType;
  int64_t var_flag = ObSysVarFlag::NONE;
  int64_t var_amount = ObSysVariables::get_amount();
  ObObj casted_value;
  ObArenaAllocator calc_buf(ObModIds::OB_SQL_SESSION);
  ObCastCtx cast_ctx(&calc_buf, NULL, CM_NONE, ObCharset::get_system_collation());
  for (int64_t i = 0; OB_SUCC(ret) && i < var_amount; ++i) {
    name.assign_ptr(const_cast<char*>(ObSysVariables::get_name(i).ptr()),
        static_cast<ObString::obstr_size_t>(strlen(ObSysVariables::get_name(i).ptr())));
    bool is_exist = false;
    if (OB_FAIL(sys_variable_exists(name, is_exist))) {
      LOG_WARN("failed to check if sys variable exists", K(name), K(ret));
    } else if (!is_exist) {
      var_type = ObSysVariables::get_type(i);
      var_flag = ObSysVariables::get_flags(i);
      value.set_varchar(ObSysVariables::get_value(i));
      value.set_collation_type(ObCharset::get_system_collation());
      min_val.set_varchar(ObSysVariables::get_min(i));
      min_val.set_collation_type(ObCharset::get_system_collation());
      max_val.set_varchar(ObSysVariables::get_max(i));
      max_val.set_collation_type(ObCharset::get_system_collation());
      type.set_type(var_type);
      if (is_sys_tenant) {
        if (OB_FAIL(process_variable_for_tenant(name, value))) {
          LOG_WARN("process system variable for tenant error", K(name), K(value), K(ret));
        }
      }
      if (OB_SUCC(ret)) {
        if (OB_FAIL(load_sys_variable(name, type, value, min_val, max_val, var_flag))) {
          LOG_WARN("fail to load default system variable", K(name), K(ret));
        } else if (OB_NOT_NULL(sys_vars_[i]) && sys_vars_[i]->is_influence_plan() &&
                   OB_FAIL(influence_plan_var_indexs_.push_back(i))) {
          LOG_WARN("fail to add influence plan sys var", K(name), K(ret));
        } else if (print_info_log) {
          LOG_INFO("load default system variable", name.ptr(), value.get_string().ptr());
        }
      }
    }
  }  // end for

  if (OB_SUCC(ret)) {
    if (OB_FAIL(gen_sys_var_in_pc_str())) {
      LOG_INFO("fail to generate system variables in pc str");
    } else {
      global_vars_version_ = 0;
      sys_var_base_version_ = 0;
    }
  }
  return ret;
}

int ObBasicSessionInfo::update_query_sensitive_system_variable(ObSchemaGetterGuard& schema_guard)
{
  int ret = OB_SUCCESS;
  const ObSysVarSchema* sysvar = NULL;
  int64_t schema_version = -1;
  const ObSimpleSysVariableSchema* sys_variable_schema = NULL;
  const uint64_t tenant_id = get_effective_tenant_id();
  int64_t refreshed_schema_version = OB_INVALID_VERSION;
  if (!check_sys_variable_) {
    // skip to avoid circular dependency.
  } else if (OB_FAIL(schema_guard.get_schema_version(tenant_id, refreshed_schema_version))) {
    LOG_WARN("fail to get tenant schema version", K(ret), K(tenant_id));
  } else if (OB_CORE_SCHEMA_VERSION >= refreshed_schema_version) {
    // if in tenant creation process or the tenant creation fails or the local schema is not refreshed,
    // the system variables can not be obtained, skip this step.
  } else if (OB_FAIL(schema_guard.get_sys_variable_schema(tenant_id, sys_variable_schema))) {
    LOG_WARN("get tenant schema version failed", K(ret), K(tenant_id));
  } else if (OB_ISNULL(sys_variable_schema)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("sys variable schema should not be null", K(ret), K(tenant_id));
  } else if (FALSE_IT(schema_version = sys_variable_schema->get_schema_version())) {
    ret = OB_ERR_UNEXPECTED;
  } else if (schema_version > get_global_vars_version() && schema_version > OB_CORE_SCHEMA_VERSION) {
    const ObTenantSchema* tenant_info = NULL;
    bool need_update_version = false;
    const ObSysVariableSchema* sys_variable_schema = NULL;
    if (OB_FAIL(schema_guard.get_tenant_info(get_effective_tenant_id(), tenant_info))) {
      LOG_WARN("get tenant info from schema guard failed", K(ret));
    } else if (OB_FAIL(schema_guard.get_sys_variable_schema(get_effective_tenant_id(), sys_variable_schema))) {
      if (OB_TENANT_NOT_EXIST == ret) {
        LOG_INFO("tenant maybe creating, just skip", K(ret), K(ret));
        ret = OB_SUCCESS;
      } else {
        LOG_WARN("get sys variable schema failed", K(ret));
      }
    } else if (OB_ISNULL(sys_variable_schema)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("sys variable schema is null", K(ret));
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < sys_variable_schema->get_sysvar_count(); ++i) {
        sysvar = sys_variable_schema->get_sysvar_schema(i);
        if (sysvar != NULL && sysvar->is_query_sensitive()) {
          if (OB_FAIL(update_sys_variable(sysvar->get_name(), sysvar->get_value()))) {
            if (OB_ERR_SYS_VARIABLE_UNKNOWN == ret) {
              // this system variable may come from a higher version,
              // which is not available locally, ignore it.
              ret = OB_SUCCESS;
            } else {
              LOG_WARN("update system variable failed", K(ret), K(*sysvar));
            }
          } else {
            need_update_version = true;
          }
        }
      }
      if (OB_SUCC(ret) && need_update_version) {
        set_global_vars_version(schema_version);
      }
    }
  }
  return ret;
}

952
// used for bootstrap, in which we can not get system variables from inner table.
O
oceanbase-admin 已提交
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
int ObBasicSessionInfo::load_default_sys_variable(const bool print_info_log, const bool is_sys_tenant)
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(init_system_variables(print_info_log, is_sys_tenant))) {
    LOG_WARN("Init system variables failed !", K(ret));
  } else {
    variables_last_modify_time_ = ObTimeUtility::current_time();
  }
  return ret;
}

// used for session deserialization from lower version observer,
// and some system variables may be supported in current version.
int ObBasicSessionInfo::load_default_sys_variable(int64_t var_idx)
{
  int ret = OB_SUCCESS;
  ObString name;
  ObObj type;
  ObObj value;
  ObObj min_val;
  ObObj max_val;
  ObObjType var_type = ObNullType;
  int64_t var_flag = ObSysVarFlag::NONE;
  if (var_idx < 0 || var_idx >= ObSysVarFactory::ALL_SYS_VARS_COUNT) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("the value of var_idx is unexpected", K(ret));
  } else {
    name.assign_ptr(const_cast<char*>(ObSysVariables::get_name(var_idx).ptr()),
        static_cast<ObString::obstr_size_t>(strlen(ObSysVariables::get_name(var_idx).ptr())));
    var_type = ObSysVariables::get_type(var_idx);
    var_flag = ObSysVariables::get_flags(var_idx);
    value.set_varchar(ObSysVariables::get_value(var_idx));
    value.set_collation_type(ObCharset::get_system_collation());
    min_val.set_varchar(ObSysVariables::get_min(var_idx));
    min_val.set_collation_type(ObCharset::get_system_collation());
    max_val.set_varchar(ObSysVariables::get_max(var_idx));
    max_val.set_collation_type(ObCharset::get_system_collation());
    type.set_type(var_type);
    if (OB_FAIL(load_sys_variable(name, type, value, min_val, max_val, var_flag))) {
      LOG_WARN("fail to load default system variable", K(name), K(ret));
    }
  }
  return ret;
}

int ObBasicSessionInfo::process_variable_for_tenant(const ObString& var, ObObj& val)
{
  int ret = OB_SUCCESS;
  if (0 == var.compare(OB_SV_LOWER_CASE_TABLE_NAMES)) {
    val.set_varchar("2");
    val.set_collation_type(ObCharset::get_system_collation());
  }
  return ret;
}

int ObBasicSessionInfo::create_sys_var(ObSysVarClassType sys_var_id, int64_t store_idx, ObBasicSysVar*& sys_var)
{
  int ret = OB_SUCCESS;
  OV(0 <= store_idx && store_idx < ObSysVarFactory::ALL_SYS_VARS_COUNT, OB_ERR_UNEXPECTED, sys_var_id, store_idx);
  if (OB_NOT_NULL(sys_vars_[store_idx])) {
    OV(sys_vars_[store_idx]->get_type() == sys_var_id,
        OB_ERR_UNEXPECTED,
        sys_var_id,
        store_idx,
        sys_vars_[store_idx]->get_type());
    OX(sys_var = sys_vars_[store_idx]);
  } else {
    OZ(sys_var_fac_.create_sys_var(sys_var_id, sys_var), sys_var_id);
    OV(OB_NOT_NULL(sys_var), OB_ERR_UNEXPECTED, sys_var_id, store_idx);
    OX(sys_vars_[store_idx] = sys_var);
  }
  return ret;
}

int ObBasicSessionInfo::inner_get_sys_var(const ObString& sys_var_name, ObBasicSysVar*& sys_var) const
{
  int ret = OB_SUCCESS;
  ObSysVarClassType sys_var_id = SYS_VAR_INVALID;
  int64_t store_idx = -1;
  if (OB_UNLIKELY(SYS_VAR_INVALID == (sys_var_id = ObSysVarFactory::find_sys_var_id_by_name(sys_var_name)))) {
    ret = OB_ERR_SYS_VARIABLE_UNKNOWN;
    LOG_WARN("fail to find sys var id by name", K(ret), K(sys_var_name), K(lbt()));
  } else if (OB_FAIL(ObSysVarFactory::calc_sys_var_store_idx(sys_var_id, store_idx))) {
    LOG_WARN("fail to calc sys var store idx", K(ret), K(sys_var_id), K(sys_var_name), K(lbt()));
  } else if (OB_UNLIKELY(store_idx < 0) || OB_UNLIKELY(store_idx >= ObSysVarFactory::ALL_SYS_VARS_COUNT)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("got store_idx is invalid", K(ret), K(store_idx));
  } else if (OB_ISNULL(sys_vars_[store_idx])) {
    ret = OB_ENTRY_NOT_EXIST;
Q
qianchanger 已提交
1042
    LOG_WARN("sys var is NULL", K(ret), K(store_idx), K(sys_var_name));
O
oceanbase-admin 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
  } else {
    sys_var = sys_vars_[store_idx];
  }
  return ret;
}

int ObBasicSessionInfo::inner_get_sys_var(const ObSysVarClassType sys_var_id, ObBasicSysVar*& sys_var) const
{
  int ret = OB_SUCCESS;
  int64_t store_idx = -1;
  if (OB_UNLIKELY(SYS_VAR_INVALID == sys_var_id)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_ERROR("invalid sys var id", K(ret), K(sys_var_id), K(lbt()));
  } else if (OB_FAIL(ObSysVarFactory::calc_sys_var_store_idx(sys_var_id, store_idx))) {
    LOG_WARN("fail to calc sys var store idx", K(ret), K(sys_var_id), K(lbt()));
  } else if (OB_UNLIKELY(store_idx < 0) || OB_UNLIKELY(store_idx >= ObSysVarFactory::ALL_SYS_VARS_COUNT)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("got store_idx is invalid", K(ret), K(store_idx));
  } else if (OB_ISNULL(sys_vars_[store_idx])) {
    ret = OB_ENTRY_NOT_EXIST;
Q
qianchanger 已提交
1063
    LOG_WARN("sys var is NULL", K(ret), K(store_idx));
O
oceanbase-admin 已提交
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 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 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 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 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
  } else {
    sys_var = sys_vars_[store_idx];
  }
  return ret;
}

int ObBasicSessionInfo::change_value_for_special_sys_var(
    const ObString& sys_var_name, const ObObj& ori_val, ObObj& new_val)
{
  int ret = OB_SUCCESS;
  ObSysVarClassType sys_var_id = ObSysVarFactory::find_sys_var_id_by_name(sys_var_name);
  if (OB_UNLIKELY(SYS_VAR_INVALID == sys_var_id)) {
    LOG_WARN("fail to find sys var id by name", K(ret), K(sys_var_name));
  } else if (OB_FAIL(ObBasicSessionInfo::change_value_for_special_sys_var(sys_var_id, ori_val, new_val))) {
    LOG_WARN("fail to change value for special sys var", K(ret), K(sys_var_name), K(sys_var_id), K(ori_val));
  }
  return ret;
}

int ObBasicSessionInfo::change_value_for_special_sys_var(
    const ObSysVarClassType sys_var_id, const ObObj& ori_val, ObObj& new_val)
{
  int ret = OB_SUCCESS;
  int64_t sys_var_store_idx = -1;
  if (SYS_VAR_VERSION_COMMENT == sys_var_id || SYS_VAR_VERSION == sys_var_id ||
      SYS_VAR_SYSTEM_TIME_ZONE == sys_var_id) {
    if (OB_FAIL(ObSysVarFactory::calc_sys_var_store_idx(sys_var_id, sys_var_store_idx))) {
      LOG_WARN("fail to calc sys var store idx", K(ret), K(sys_var_id));
    } else {
      new_val = ObSysVariables::get_default_value(sys_var_store_idx);
    }
  } else {
    new_val = ori_val;
  }
  return ret;
}

int ObBasicSessionInfo::load_sys_variable(const ObString& name, const ObObj& type, const ObObj& value,
    const ObObj& min_val, const ObObj& max_val, const int64_t flags, bool is_from_sys_table /*= false*/)
{
  int ret = OB_SUCCESS;
  ObObj casted_cell;
  ObBasicSysVar* sys_var = NULL;
  ObSysVarClassType var_id = SYS_VAR_INVALID;
  ObObj real_val;
  ObObj val_ptr;
  ObObj min_ptr;
  ObObj max_ptr;
  ObObj val_type;
  ObObj tmp_type;
  int64_t store_idx = -1;
  if (SYS_VAR_INVALID == (var_id = ObSysVarFactory::find_sys_var_id_by_name(name, is_from_sys_table))) {
    if (is_from_sys_table) {
      ret = OB_SUCCESS;
    } else {
      ret = OB_ERR_SYS_VARIABLE_UNKNOWN;
      LOG_ERROR("failed to find system variable", K(ret), K(name));
    }
  } else if (OB_FAIL(ObSysVarFactory::calc_sys_var_store_idx(var_id, store_idx))) {
    LOG_WARN("fail to calc sys var store idx", K(ret), K(var_id));
  } else if (OB_FAIL(cast_sys_variable(false, var_id, type, value, flags, val_type, val_ptr))) {
    LOG_WARN("fail to cast system variable", K(var_id), K(type), K(value), K(flags), K(val_ptr), K(ret));
  } else if (OB_FAIL(cast_sys_variable(true, var_id, type, min_val, flags, tmp_type, min_ptr))) {
    LOG_WARN("fail to cast system variable", K(var_id), K(type), K(min_val), K(flags), K(min_val), K(min_ptr), K(ret));
  } else if (OB_FAIL(cast_sys_variable(true, var_id, type, max_val, flags, tmp_type, max_ptr))) {
    LOG_WARN("fail to cast system variable", K(var_id), K(type), K(max_val), K(flags), K(max_ptr), K(ret));
  } else if (OB_FAIL(create_sys_var(var_id, store_idx, sys_var))) {
    LOG_WARN("fail to create sys var", K(name), K(value), K(ret));
  } else if (OB_ISNULL(sys_var)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("sys var is NULL", K(name), K(value), K(ret));
  } else if (OB_FAIL(ObBasicSessionInfo::change_value_for_special_sys_var(var_id, val_ptr, real_val))) {
    LOG_WARN("fail to change value for special sys var", K(ret), K(var_id), K(val_ptr));
  } else if (OB_FAIL(sys_var->init(real_val, min_ptr, max_ptr, val_type.get_type(), flags))) {
    LOG_WARN("fail to init sys var", K(ret), K(sys_var->get_type()), K(real_val), K(name), K(value));
  } else if (OB_FAIL(process_session_variable(var_id, real_val, false, false /*check_timezone_valid*/))) {
    LOG_WARN("process system variable error", K(name), K(type), K(real_val), K(value), K(ret));
  } else {
    variables_last_modify_time_ = ObTimeUtility::current_time();
  }
  return ret;
}

int ObBasicSessionInfo::load_sys_variable(const ObString& name, const int64_t dtype, const ObString& value_str,
    const ObString& min_val_str, const ObString& max_val_str, const int64_t flags, bool is_from_sys_table /*= false*/)
{
  int ret = OB_SUCCESS;
  ObObj value, min_val, max_val;
  ObObj otype;
  value.set_varchar(value_str);
  value.set_collation_type(ObCharset::get_system_collation());
  min_val.set_varchar(min_val_str);
  min_val.set_collation_type(ObCharset::get_system_collation());
  max_val.set_varchar(max_val_str);
  max_val.set_collation_type(ObCharset::get_system_collation());
  otype.set_type(static_cast<ObObjType>(dtype));
  if (OB_FAIL(load_sys_variable(name, otype, value, min_val, max_val, flags, is_from_sys_table))) {
    LOG_WARN("fail to load system variable",
        K(name),
        K(otype),
        K(value),
        K(min_val),
        K(max_val),
        K(flags),
        K(is_from_sys_table),
        K(ret));
  }
  return ret;
}

int ObBasicSessionInfo::cast_sys_variable(bool is_range_value, const ObSysVarClassType sys_var_id, const ObObj& type,
    const ObObj& value, int64_t flags, ObObj& out_type, ObObj& out_value)
{
  UNUSED(sys_var_id);
  int ret = OB_SUCCESS;
  ObObj casted_cell;
  ObArenaAllocator calc_buf(ObModIds::OB_SQL_SESSION);
  if (ObVarcharType != value.get_type()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid type", K(value), K(ret));
  } else {
    if (is_range_value && value.get_varchar() == ObString::make_string(ObBasicSysVar::EMPTY_STRING)) {
      out_value.set_null();
    } else if (ObBasicSysVar::is_null_value(value.get_varchar(), flags)) {
      out_value.set_null();
    } else {
      ObDataTypeCastParams dtc_params = get_dtc_params();
      ObCastCtx cast_ctx(&calc_buf, &dtc_params, CM_NONE, ObCharset::get_system_collation());
      if (OB_FAIL(
              ObObjCaster::to_type(type.get_type(), ObCharset::get_system_collation(), cast_ctx, value, casted_cell))) {
        _LOG_WARN("failed to cast object, cell=%s from_type=%s to_type=%s ret=%d ",
            to_cstring(value),
            ob_obj_type_str(value.get_type()),
            ob_obj_type_str(type.get_type()),
            ret);
      } else if (OB_FAIL(base_sys_var_alloc_.write_obj(casted_cell, &out_value))) {
        LOG_WARN("fail to store variable value", K(casted_cell), K(value), K(ret));
      } else {
        if (ob_is_string_type(out_value.get_type())) {
          out_value.set_collation_level(CS_LEVEL_SYSCONST);
          if (CS_TYPE_INVALID == out_value.get_collation_type()) {
            out_value.set_collation_type(ObCharset::get_system_collation());
          }
        }
      }
    }
    if (OB_SUCC(ret)) {
      out_type.set_type(type.get_type());
    }
  }
  return ret;
}

bool sys_var_compare(const SysVarNameVal& left, const SysVarNameVal& right)
{
  return left.name_ < right.name_;
}

/*
 *the following system variables affect plan cache:
 *read_only
 *ob_enable_transformation
 *binlog_row_image
 *collation_connection
 *sql_auto_is_null
 *div_precision_increment
 *ob_enable_aggregation_pushdown
 *ob_enable_index_direct_select
 *sql_mode
 *ob_max_parallel_degree
 *ob_route_policy
 *ob_read_consistency
 */
int ObBasicSessionInfo::get_influence_plan_sys_var(ObSysVarInPC& sys_vars) const
{
  int ret = OB_SUCCESS;
  // get influence_plan_sys_var from sys_var_val_map
  int64_t index = 0;
  for (int64_t i = 0; OB_SUCC(ret) && i < get_influence_plan_var_count(); ++i) {
    index = influence_plan_var_indexs_.at(i);
    if (index >= ObSysVarFactory::ALL_SYS_VARS_COUNT) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("influence plan system var indexs out of range", K(i), K(ret));
    } else if (NULL == sys_vars_[index]) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("influence plan system var is NULL", K(i), K(ret));
    } else if (OB_FAIL(sys_vars.push_back(sys_vars_[index]->get_value()))) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("influence plan system variables push failed", K(ret));
    }
  }
  return ret;
}

int ObBasicSessionInfo::gen_sys_var_in_pc_str()
{
  int ret = OB_SUCCESS;
  const int64_t MAX_SYS_VARS_STR_SIZE = 1024;
  ObSysVarInPC sys_vars;
  char* buf = NULL;
  int64_t pos = 0;
  if (is_first_gen_) {
    if (NULL == (buf = (char*)name_pool_.alloc(MAX_SYS_VARS_STR_SIZE))) {
      ret = OB_ALLOCATE_MEMORY_FAILED;
      LOG_WARN("fail to allocator memory", K(ret), K(MAX_SYS_VARS_STR_SIZE));
    }
    is_first_gen_ = false;
  } else {
    buf = sys_var_in_pc_str_.ptr();
    MEMSET(buf, 0, sys_var_in_pc_str_.length());
    sys_var_in_pc_str_.reset();
  }

  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(get_influence_plan_sys_var(sys_vars))) {
    LOG_WARN("fail to get influence plan system variables", K(ret));
  } else if (OB_FAIL(sys_vars.serialize_sys_vars(buf, MAX_SYS_VARS_STR_SIZE, pos))) {
    LOG_WARN("fail to serialize system vars");
  } else {
    (void)sys_var_in_pc_str_.assign(buf, int32_t(pos));
  }

  return ret;
}

int ObBasicSessionInfo::update_sys_variable_by_name(const ObString& var, const ObObj& val)
{
  int ret = OB_SUCCESS;
  ObSysVarClassType var_id = SYS_VAR_INVALID;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(val), K(ret));
  } else if (SYS_VAR_INVALID == (var_id = ObSysVarFactory::find_sys_var_id_by_name(var))) {
    ret = OB_ERR_SYS_VARIABLE_UNKNOWN;
    LOG_WARN("unknown variable", K(var), K(val), K(ret));
  } else if (OB_FAIL(update_sys_variable(var_id, val))) {
    LOG_WARN("failed to update sys variable", K(var), K(val), K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::update_sys_variable_by_name(const common::ObString& var, int64_t val)
{
  ObObj obj;
  obj.set_int(val);
  return update_sys_variable_by_name(var, obj);
}

int ObBasicSessionInfo::update_sys_variable(const ObSysVarClassType sys_var_id, const ObString& val)
{
  ObObj obj;
  obj.set_varchar(val);
  obj.set_collation_type(ObCharset::get_system_collation());
  return update_sys_variable(sys_var_id, obj);
}

int ObBasicSessionInfo::update_sys_variable(const ObSysVarClassType sys_var_id, const int64_t val)
{
  ObObj obj;
  obj.set_int(val);
  return update_sys_variable(sys_var_id, obj);
}

int ObBasicSessionInfo::update_sys_variable(const ObString& var, const ObString& val)
{
  int ret = OB_SUCCESS;
  ObSysVarClassType sys_var_id = SYS_VAR_INVALID;
  ObBasicSysVar* sys_var = NULL;
  if (OB_UNLIKELY(SYS_VAR_INVALID == (sys_var_id = ObSysVarFactory::find_sys_var_id_by_name(var)))) {
    ret = OB_ERR_SYS_VARIABLE_UNKNOWN;
    LOG_WARN("unknown variable", K(var), K(val), K(ret));
  } else if (OB_FAIL(inner_get_sys_var(sys_var_id, sys_var))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(var), K(val));
  } else if (OB_ISNULL(sys_var)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret), K(var));
  } else {
    // trim quotation marks
    ObString tmp_val;
    if (val.length() > 1 &&
        (('\'' == val[0] && '\'' == val[val.length() - 1]) || ('\"' == val[0] && '\"' == val[val.length() - 1]))) {
      tmp_val.assign_ptr(val.ptr() + 1, val.length() - 2);
    } else {
      tmp_val.assign_ptr(val.ptr(), val.length());
    }
    ObObj in_obj;
    in_obj.set_varchar(tmp_val);
    in_obj.set_collation_type(ObCharset::get_system_collation());
    const ObObj* out_obj = NULL;
    ObObj buf_obj;
    ObArenaAllocator calc_buf(ObModIds::OB_SQL_SESSION);
    ObDataTypeCastParams dtc_params = get_dtc_params();
    ObCastCtx cast_ctx(&calc_buf, &dtc_params, CM_NONE, ObCharset::get_system_collation());
    if (OB_FAIL(ObObjCaster::to_type(sys_var->get_data_type(), cast_ctx, in_obj, buf_obj, out_obj))) {
      LOG_WARN("failed to cast obj", "expected type", sys_var->get_meta_type(), K(ret), K(var), K(in_obj));
    } else if (OB_ISNULL(out_obj)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("casted success, but out_obj is NULL", K(in_obj), K(ret));
    } else if (OB_FAIL(update_sys_variable(sys_var_id, *out_obj))) {
      LOG_WARN("fail to update sys variable", K(ret), K(var), K(*out_obj));
    }
  }
  return ret;
}

int ObBasicSessionInfo::update_sys_variable(const ObSysVarClassType sys_var_id, const ObObj& val)
{
  int ret = OB_SUCCESS;
  ObObj obj;
  ObBasicSysVar* sys_var = NULL;
  if (SYS_VAR_INVALID == sys_var_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid sys_var_id", K(sys_var_id), K(ret));
  } else if (OB_FAIL(inner_get_sys_var(sys_var_id, sys_var))) {
    LOG_WARN("failed to inner get sys var", K(ret), K(sys_var_id), K(val));
  } else if (OB_ISNULL(sys_var)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("failed to inner get sys var, sys var is null", K(ret), K(sys_var_id), K(val));
  } else if (is_track_session_info()) {
    if (OB_FAIL(track_sys_var(sys_var_id, sys_var->get_value()))) {
      LOG_WARN("failed to track sys var", K(ret), K(sys_var_id), K(val));
    } else {
      variables_last_modify_time_ = ObTimeUtility::current_time();
      LOG_DEBUG("succ to track system variable", K(ret), K(sys_var_id), K(val), K(sys_var->get_value()));
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(deep_copy_sys_variable(sys_var_id, val, &obj))) {
      LOG_WARN("fail to update system variable", K(sys_var_id), K(val), K(ret));
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(process_session_variable(sys_var_id, val, true, false /*check_timezone_valid*/))) {
      LOG_WARN("process system variable error", K(sys_var_id), K(val), K(ret));
    } else if (OB_FAIL(sys_var_inc_info_.add_sys_var_id(sys_var_id))) {
      LOG_WARN("add sys var id error", K(sys_var_id), K(ret));
    } else {
      if (SYS_VAR_TIME_ZONE == sys_var_id && !tz_info_wrap_.is_position_class()) {
        const int64_t buf_len = 16;
        char tmp_buf[buf_len] = {0};
        int64_t pos = 0;
        ObObj tmp_obj = val;
        if (OB_ISNULL(tz_info_wrap_.get_time_zone_info())) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("time zone info is null", K(ret));
        } else if (OB_FAIL(tz_info_wrap_.get_time_zone_info()->timezone_to_str(tmp_buf, buf_len, pos))) {
          LOG_WARN("timezone to str failed", K(ret));
        } else {
          tmp_obj.set_common_value(ObString(pos, tmp_buf));
          if (OB_FAIL(deep_copy_sys_variable(sys_var_id, tmp_obj, &obj))) {
            LOG_WARN("deep copy sys var failed", K(ret));
          }
        }
      }
      if (OB_SUCC(ret)) {
        if (ob_is_string_type(obj.get_type())) {
          obj.set_collation_level(CS_LEVEL_SYSCONST);
          if (CS_TYPE_INVALID == obj.get_collation_type()) {
            obj.set_collation_type(ObCharset::get_default_collation(ObCharset::get_default_charset()));
          }
        }
        sys_var->set_value(obj);
      }
    }
  }
  if (OB_SUCC(ret) && !is_deserialized_ && sys_var->is_influence_plan() && OB_FAIL(gen_sys_var_in_pc_str())) {
    LOG_ERROR("fail to gen sys var in pc str", K(ret));
  }
  return ret;
}

int ObBasicSessionInfo::deep_copy_trace_id_var(const ObObj& src_val, ObObj* dest_val_ptr)
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(dest_val_ptr)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("dest val ptr is NULL", K(ret));
  } else {
    *dest_val_ptr = src_val;
    ObString new_str;
    ObString src_str;
    const int64_t STR_BUFF_LEN = sizeof(trace_id_buff_);
    int64_t src_str_len = 0;
    if (OB_FAIL(src_val.get_varchar(src_str))) {
      LOG_WARN("fail to get varchar", K(src_val), K(ret));
    } else if (OB_UNLIKELY((src_str_len = src_str.length()) == 0)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid string", K(src_str), K(ret));
    } else if (STR_BUFF_LEN <= src_str_len) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("invalid src str len", K(src_str_len), K(ret));
    } else {
      MEMCPY(trace_id_buff_, src_str.ptr(), src_str_len);
      trace_id_buff_[src_str_len] = '\0';
      new_str.assign_ptr(trace_id_buff_, static_cast<ObString::obstr_size_t>(src_str_len));
      dest_val_ptr->set_varchar(new_str);
    }
  }
  return ret;
}

// name_pool_ is an allocator that only alloc but not free,
// now we optimize for trace_id only, which will be accessed frequently.
int ObBasicSessionInfo::deep_copy_sys_variable(
    const ObSysVarClassType sys_var_id, const ObObj& src_val, ObObj* dest_val_ptr)
{
  int ret = OB_SUCCESS;
  if (sys_var_id == SYS_VAR_OB_STATEMENT_TRACE_ID) {
    if (OB_FAIL(deep_copy_trace_id_var(src_val, dest_val_ptr))) {
      LOG_WARN("fail to deep copy trace id", K(ret));
    }
  } else if (OB_FAIL(name_pool_.write_obj(src_val, dest_val_ptr))) {
    LOG_WARN("fail to write obj", K(src_val), K(ret));
  }
  return ret;
}

int ObBasicSessionInfo::update_session_sys_variable(ObExecContext& ctx, const ObString& var, const ObObj& val)
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* sys_var = NULL;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(ret), K(var));
  } else if (OB_FAIL(inner_get_sys_var(var, sys_var))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(var), K(val));
  } else if (OB_ISNULL(sys_var)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret), K(var), K(val));
  } else {
    ObMySQLProxy sql_proxy;
    ObSetVar set_var(var, ObSetVar::SET_SCOPE_SESSION, false, OB_INVALID_ID, ctx.get_allocator(), sql_proxy);
    if (OB_FAIL(sys_var->session_update(ctx, set_var, val))) {
      LOG_WARN("session update system variable failed", K(ret), K(set_var));
    } else if (OB_FAIL(sys_var->update(ctx, set_var, val))) {
      LOG_WARN("fail to update sys var", K(ret), K(var), K(val));
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_sys_variable_by_name(const ObString& var, ObBasicSysVar*& val) const
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(var.empty())) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(ret));
  } else if (OB_FAIL(inner_get_sys_var(var, val))) {
    LOG_WARN("fail to inner get sys var", K(var), K(ret));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret), K(var));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::get_sys_variable_by_name(const ObString& var, ObObj& val) const
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* sys_var = NULL;
  if (OB_UNLIKELY(var.empty())) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(ret));
  } else if (OB_FAIL(inner_get_sys_var(var, sys_var))) {
    LOG_WARN("fail to get sys var", K(ret), K(var));
  } else if (OB_ISNULL(sys_var)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to get sys var, but sys var is NULL", K(ret), K(var));
  } else {
    val = sys_var->get_value();
  }
  return ret;
}

int ObBasicSessionInfo::get_sys_variable_by_name(const common::ObString& var, int64_t& val) const
{
  int ret = OB_SUCCESS;
  ObObj obj;
  if (OB_FAIL(get_sys_variable_by_name(var, obj))) {
  } else if (OB_FAIL(obj.get_int(val))) {
    LOG_WARN("wrong obj type for system variable", K(var), K(obj), K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::get_sys_variable(const ObSysVarClassType sys_var_id, common::ObObj& val) const
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* var = NULL;
  if (OB_UNLIKELY(SYS_VAR_INVALID == sys_var_id)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid sys_var_id", K(ret), K(sys_var_id));
  } else if (OB_FAIL(inner_get_sys_var(sys_var_id, var))) {
    LOG_WARN("fail to get sys var", K(ret), K(var));
  } else if (OB_ISNULL(var)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to get sys var, but sys var is NULL", K(ret), K(var));
  } else {
    val = var->get_value();
  }
  return ret;
}

int ObBasicSessionInfo::get_sys_variable(const ObSysVarClassType sys_var_id, common::ObString& val) const
{
  int ret = OB_SUCCESS;
  ObObj obj;
  if (SYS_VAR_INVALID == sys_var_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid sys_var_id", K(sys_var_id), K(ret));
  } else if (OB_FAIL(get_sys_variable(sys_var_id, obj))) {
    LOG_WARN("failed to get system variable", K(sys_var_id), K(ret));
  } else if (OB_FAIL(obj.get_varchar(val))) {
    LOG_WARN("wrong obj type for system variable", K(sys_var_id), K(obj), K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::get_sys_variable(const ObSysVarClassType sys_var_id, int64_t& val) const
{
  int ret = OB_SUCCESS;
  ObObj obj;
  if (OB_FAIL(get_sys_variable(sys_var_id, obj))) {
    LOG_WARN("failed to get system variable", K(sys_var_id), K(obj), K(ret));
  } else if (OB_FAIL(obj.get_int(val))) {
    LOG_WARN("wrong obj type for system variable", K(sys_var_id), K(obj), K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::get_sys_variable(const ObSysVarClassType sys_var_id, uint64_t& val) const
{
  int ret = OB_SUCCESS;
  ObObj obj;
  if (OB_FAIL(get_sys_variable(sys_var_id, obj))) {
    LOG_WARN("failed to get system variable", K(sys_var_id), K(obj), K(ret));
  } else if (OB_FAIL(obj.get_uint64(val))) {
    LOG_WARN("wrong obj type for system variable", K(ret), K(sys_var_id), K(obj));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::sys_variable_exists(const ObString& var, bool& is_exists) const
{
  int ret = OB_SUCCESS;
  is_exists = false;
  ObSysVarClassType sys_var_id = SYS_VAR_INVALID;
  int64_t store_idx = -1;
  if (SYS_VAR_INVALID == (sys_var_id = ObSysVarFactory::find_sys_var_id_by_name(var))) {
    LOG_DEBUG("sys var is not exist", K(var), K(ret));
  } else if (OB_FAIL(ObSysVarFactory::calc_sys_var_store_idx(sys_var_id, store_idx))) {
    LOG_WARN("fail to calc sys var store idx", K(sys_var_id), K(var), K(ret));
  } else if (store_idx < 0 || store_idx >= ObSysVarFactory::ALL_SYS_VARS_COUNT) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("got store_idx is invalid", K(store_idx), K(ret));
  } else {
    is_exists = (NULL != sys_vars_[store_idx]);
  }
  return ret;
}

ObPhysicalPlan* ObBasicSessionInfo::get_cur_phy_plan() const
{
  return cur_phy_plan_;
}

int ObBasicSessionInfo::set_cur_phy_plan(ObPhysicalPlan* cur_phy_plan)
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(cur_phy_plan)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("current physical plan is NULL", K(lbt()), K(ret));
  } else {
    cur_phy_plan_ = cur_phy_plan;
1644
    MEMCPY(sql_id_, cur_phy_plan->stat_.sql_id_, common::OB_MAX_SQL_ID_LENGTH + 1);
O
oceanbase-admin 已提交
1645 1646 1647 1648 1649 1650 1651 1652 1653
  }
  return ret;
}

void ObBasicSessionInfo::reset_cur_phy_plan_to_null()
{
  cur_phy_plan_ = NULL;
}

1654 1655 1656 1657 1658 1659 1660 1661 1662
void ObBasicSessionInfo::get_cur_sql_id(char *sql_id_buf, int64_t sql_id_buf_size) const
{
  if (common::OB_MAX_SQL_ID_LENGTH + 1 <= sql_id_buf_size) {
    MEMCPY(sql_id_buf, sql_id_, common::OB_MAX_SQL_ID_LENGTH + 1);
  } else {
    sql_id_buf[0] = '\0';
  }
}

O
oceanbase-admin 已提交
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 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 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 1810 1811 1812 1813 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 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 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 1994 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 2174 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 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 2255 2256 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 2287 2288 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 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 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 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 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
ObObjType ObBasicSessionInfo::get_sys_variable_type(const ObString& var_name) const
{
  int ret = OB_SUCCESS;
  ObObjType obj_type = ObMaxType;
  ObBasicSysVar* val = NULL;
  if (OB_FAIL(inner_get_sys_var(var_name, val))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(var_name));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret), K(var_name));
  } else {
    obj_type = val->get_data_type();
  }
  return obj_type;
}

ObObjType ObBasicSessionInfo::get_sys_variable_meta_type(const ObString& var_name) const
{
  int ret = OB_SUCCESS;
  ObObjType obj_type = ObMaxType;
  ObBasicSysVar* val = NULL;
  if (OB_FAIL(inner_get_sys_var(var_name, val))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(var_name));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret), K(var_name));
  } else {
    obj_type = val->get_meta_type();
  }
  return obj_type;
}

#define PROCESS_SESSION_INT_VARIABLE(sys_var)                       \
  do {                                                              \
    ObArenaAllocator allocator(ObModIds::OB_SQL_SESSION);           \
    int64_t val_int = 0;                                            \
    ObCastCtx cast_ctx(&allocator, NULL, CM_NONE, CS_TYPE_INVALID); \
    EXPR_GET_INT64_V2(val, val_int);                                \
    if (OB_SUCC(ret)) {                                             \
      sys_var = val_int;                                            \
    }                                                               \
  } while (0)

OB_INLINE int ObBasicSessionInfo::process_session_variable(
    ObSysVarClassType var, const ObObj& val, bool is_inc, const bool check_timezone_valid /*true*/)
{
  int ret = OB_SUCCESS;
  switch (var) {
    case SYS_VAR_OB_LOG_LEVEL: {
      OZ(process_session_log_level(val), val);
      break;
    }
    case SYS_VAR_OB_COMPATIBILITY_MODE: {
      OZ(process_session_compatibility_mode_value(val, is_inc), val);
      break;
    }
    case SYS_VAR_SQL_MODE: {
      OZ(process_session_sql_mode_value(val, is_inc), val);
      break;
    }
    case SYS_VAR_TIME_ZONE: {
      OZ(process_session_time_zone_value(val, check_timezone_valid));
      break;
    }
    case SYS_VAR_ERROR_ON_OVERLAP_TIME: {
      OZ(process_session_overlap_time_value(val));
      break;
    }
    case SYS_VAR_WAIT_TIMEOUT: {
      LockGuard lock_guard(thread_data_mutex_);
      PROCESS_SESSION_INT_VARIABLE(thread_data_.wait_timeout_);
      break;
    }
    case SYS_VAR_DEBUG_SYNC: {
      const bool is_global = false;
      ret = process_session_debug_sync(val, is_global);
      break;
    }
    case SYS_VAR_OB_GLOBAL_DEBUG_SYNC: {
      const bool is_global = true;
      ret = process_session_debug_sync(val, is_global);
      break;
    }
    case SYS_VAR_OB_READ_CONSISTENCY: {
      int64_t consistency = 0;
      PROCESS_SESSION_INT_VARIABLE(consistency);
      if (OB_SUCC(ret)) {
        consistency_level_ = static_cast<ObConsistencyLevel>(consistency);
      }
      break;
    }
    case SYS_VAR_AUTO_INCREMENT_INCREMENT: {
      uint64_t uint_val = 0;
      OZ(val.get_uint64(uint_val), val);
      OX(sys_vars_cache_.set_auto_increment_increment(uint_val, is_inc));
      break;
    }
    case SYS_VAR_SQL_THROTTLE_CURRENT_PRIORITY: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_sql_throttle_current_priority(int_val, is_inc));
      break;
    }
    case SYS_VAR_OB_LAST_SCHEMA_VERSION: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_last_schema_version(int_val, is_inc));
      break;
    }
    case SYS_VAR_SQL_SELECT_LIMIT: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_sql_select_limit(int_val, is_inc));
      break;
    }
    case SYS_VAR_AUTO_INCREMENT_OFFSET: {
      uint64_t uint_val = 0;
      OZ(val.get_uint64(uint_val), val);
      OX(sys_vars_cache_.set_auto_increment_offset(uint_val, is_inc));
      break;
    }
    case SYS_VAR_LAST_INSERT_ID: {
      uint64_t uint_val = 0;
      OZ(val.get_uint64(uint_val), val);
      OX(sys_vars_cache_.set_last_insert_id(uint_val, is_inc));
      break;
    }
    case SYS_VAR_BINLOG_ROW_IMAGE: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_binlog_row_image(int_val, is_inc));
      break;
    }
    case SYS_VAR_FOREIGN_KEY_CHECKS: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_foreign_key_checks(int_val, is_inc));
      break;
    }
    case SYS_VAR_TX_ISOLATION: {
      ObString str_val;
      int32_t isolation = ObTransIsolation::UNKNOWN;
      OZ(val.get_string(str_val));
      OX(isolation = ObTransIsolation::get_level(str_val));
      OV(isolation != ObTransIsolation::UNKNOWN, OB_ERR_UNEXPECTED, isolation);
      OX(sys_vars_cache_.set_tx_isolation(isolation, is_inc));
      break;
    }
    case SYS_VAR_TX_READ_ONLY: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_tx_read_only(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR_OB_ENABLE_PLAN_CACHE: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_enable_plan_cache(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR_OPTIMIZER_USE_SQL_PLAN_BASELINES: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_optimizer_use_sql_plan_baselines(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR_OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_optimizer_capture_sql_plan_baselines(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR_IS_RESULT_ACCURATE: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_is_result_accurate(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR__OB_USE_PARALLEL_EXECUTION: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set__ob_use_parallel_execution(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR__OPTIMIZER_ADAPTIVE_CURSOR_SHARING: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set__optimizer_adaptive_cursor_sharing(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR_CHARACTER_SET_RESULTS: {
      int64_t coll_int64 = 0;
      if (val.is_null()) {
        OX(sys_vars_cache_.set_character_set_results(CHARSET_INVALID, is_inc));
      } else if (OB_FAIL(val.get_int(coll_int64))) {
        LOG_WARN("fail to get int from value", K(val), K(ret));
      } else if (OB_UNLIKELY(false == ObCharset::is_valid_collation(coll_int64))) {
        // do not set error code.
        LOG_WARN("invalid collation", K(coll_int64), K(val));
        OX(sys_vars_cache_.set_character_set_results(CHARSET_INVALID, is_inc));
      } else {
        OX(sys_vars_cache_.set_character_set_results(
            ObCharset::charset_type_by_coll(static_cast<ObCollationType>(coll_int64)), is_inc));
      }
      break;
    }
    case SYS_VAR_OB_ENABLE_SQL_AUDIT: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_enable_sql_audit(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR_NLS_LENGTH_SEMANTICS: {
      ObString str_val;
      ObLengthSemantics nls_length_semantics = LS_BYTE;
      OZ(val.get_string(str_val));
      OX(nls_length_semantics = get_length_semantics(str_val));
      OV(nls_length_semantics != LS_INVALIED, OB_ERR_UNEXPECTED, nls_length_semantics);
      OX(sys_vars_cache_.set_nls_length_semantics(nls_length_semantics, is_inc));
      break;
    }
    case SYS_VAR_AUTOCOMMIT: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_autocommit(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR_OB_ENABLE_TRACE_LOG: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_enable_trace_log(int_val != 0, is_inc));
      break;
    }
    case SYS_VAR_OB_ORG_CLUSTER_ID: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_org_cluster_id(int_val, is_inc));
      break;
    }
    case SYS_VAR_OB_QUERY_TIMEOUT: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_query_timeout(int_val, is_inc));
      break;
    }
    case SYS_VAR_OB_PL_BLOCK_TIMEOUT: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_pl_block_timeout(int_val, is_inc));
      break;
    }
    case SYS_VAR_OB_TRX_TIMEOUT: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_trx_timeout(int_val, is_inc));
      break;
    }
    case SYS_VAR_OB_TRX_IDLE_TIMEOUT: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_trx_idle_timeout(int_val, is_inc));
      break;
    }
    case SYS_VAR_OB_TRX_LOCK_TIMEOUT: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_ob_trx_lock_timeout(int_val, is_inc));
      break;
    }
    case SYS_VAR_COLLATION_CONNECTION: {
      int64_t int_val = 0;
      OZ(val.get_int(int_val), val);
      OX(sys_vars_cache_.set_collation_connection(int_val, is_inc));
      break;
    }
    case SYS_VAR_TIMESTAMP: {
      // the unit of set timestamp operation is second,
      // but we store it in microsecond, after a round operation.
      if (val.get_number().is_zero()) {
        sys_vars_cache_.set_timestamp(0, is_inc);
      } else {
        const ObObj* res_obj = NULL;
        ObArenaAllocator allocator(common::ObModIds::OB_SQL_EXPR_CALC);
        number::ObNumber value;
        number::ObNumber unit;
        if (OB_FAIL(unit.from(static_cast<int64_t>(USECS_PER_SEC * 10), allocator))) {
          LOG_WARN("failed to get the number", K(ret));
        } else if (OB_FAIL(val.get_number().mul(unit, value, allocator))) {
          LOG_WARN("failed to get the result of timestamp to microsecond", K(ret));
        } else {
          ObObj param_obj;
          param_obj.set_number(value);
          ObCollationType cast_coll_type = static_cast<ObCollationType>(get_local_collation_connection());
          // const ObDataTypeCastParams dtc_params(TZ_INFO(this), GET_NLS_FORMATS(this), get_nls_collation(),
          // get_nls_collation_nation());;
          ObDataTypeCastParams dtc_params = get_dtc_params();
          ObCastCtx cast_ctx(&allocator,
              &dtc_params,
              0, /*number_int useless*/
              CM_NONE,
              cast_coll_type,
              NULL /* time zone info */);
          EXPR_CAST_OBJ_V2(ObIntType, param_obj, res_obj);
          if (OB_SUCC(ret)) {
            if (OB_ISNULL(res_obj)) {
              ret = OB_ERR_UNEXPECTED;
              LOG_WARN("result obj is null");
            } else {
              sys_vars_cache_.set_timestamp((res_obj->get_int() + 5) / 10, is_inc);
            }
          } else {
            LOG_WARN("failed to convert the number to int", K(ret));
          }
        }
      }
      break;
    }
    case SYS_VAR_NLS_DATE_FORMAT:
    case SYS_VAR_NLS_TIMESTAMP_FORMAT:
    case SYS_VAR_NLS_TIMESTAMP_TZ_FORMAT: {
      ObString format;
      if (OB_FAIL(val.get_string(format))) {
        LOG_WARN("fail to get nls_date_format str value", K(ret), K(var));
      } else {
        int64_t nls_enum = ObNLSFormatEnum::NLS_DATE;
        ObDTMode mode = DT_TYPE_DATETIME;
        if (SYS_VAR_NLS_TIMESTAMP_FORMAT == var) {
          mode |= DT_TYPE_ORACLE;
          nls_enum = ObNLSFormatEnum::NLS_TIMESTAMP;
        } else if (SYS_VAR_NLS_TIMESTAMP_TZ_FORMAT == var) {
          mode |= DT_TYPE_ORACLE;
          mode |= DT_TYPE_TIMEZONE;
          nls_enum = ObNLSFormatEnum::NLS_TIMESTAMP_TZ;
        }
        ObSEArray<ObDFMElem, ObDFMUtil::COMMON_ELEMENT_NUMBER> dfm_elems;
        ObBitSet<ObDFMFlag::MAX_FLAG_NUMBER> elem_flags;
        // 1. parse and check semantic of format string
        if (OB_FAIL(ObDFMUtil::parse_datetime_format_string(format, dfm_elems))) {
          LOG_WARN("fail to parse oracle datetime format string", K(ret), K(format));
        } else if (OB_FAIL(ObDFMUtil::check_semantic(dfm_elems, elem_flags, mode))) {
          LOG_WARN("check semantic of format string failed", K(ret), K(format));
        } else {
          switch (nls_enum) {
            case ObNLSFormatEnum::NLS_DATE:
              sys_vars_cache_.set_nls_date_format(format, is_inc);
              break;
            case ObNLSFormatEnum::NLS_TIMESTAMP:
              sys_vars_cache_.set_nls_timestamp_format(format, is_inc);
              break;
            case ObNLSFormatEnum::NLS_TIMESTAMP_TZ:
              sys_vars_cache_.set_nls_timestamp_tz_format(format, is_inc);
              break;
            default:
              break;
          }
        }
        LOG_DEBUG("succ to set SYS_VAR_NLS_FORMAT", K(ret), K(var), K(nls_enum), K(format));
      }
      break;
    }
    case SYS_VAR_NLS_NCHAR_CHARACTERSET:
    case SYS_VAR_NLS_CHARACTERSET: {
      ObString str_val;
      ObCharsetType charset = CHARSET_INVALID;
      ObCollationType collation = CS_TYPE_INVALID;
      OZ(val.get_string(str_val), val);
      OX(charset = ObCharset::charset_type_by_name_oracle(str_val));
      OX(collation = ObCharset::get_default_collation_oracle(charset));
      OV(ObCharset::is_valid_charset(charset) && ObCharset::is_valid_collation(collation),
          OB_ERR_INVALID_CHARACTER_STRING,
          str_val,
          charset,
          collation);
      if (var == SYS_VAR_NLS_CHARACTERSET) {
        OX(sys_vars_cache_.set_nls_collation(collation, is_inc));
      } else {
        OX(sys_vars_cache_.set_nls_nation_collation(collation, is_inc));
      }
      break;
    }
    case SYS_VAR_OB_TRACE_INFO: {
      ObString trace_info;
      if (OB_FAIL(val.get_string(trace_info))) {
        LOG_WARN("fail to get str value", K(ret), K(var));
      } else {
        sys_vars_cache_.set_ob_trace_info(trace_info, is_inc);
      }
      break;
    }
    default: {
      // do nothing
    }
  }
  return ret;
}

int ObBasicSessionInfo::process_session_variable_fast()
{
  int ret = OB_SUCCESS;
  int64_t store_idx = -1;
  // SYS_VAR_OB_LOG_LEVEL
  OZ(ObSysVarFactory::calc_sys_var_store_idx(SYS_VAR_OB_LOG_LEVEL, store_idx));
  OV(ObSysVarFactory::is_valid_sys_var_store_idx(store_idx));
  OZ(process_session_log_level(sys_vars_[store_idx]->get_value()));
  // SYS_VAR_DEBUG_SYNC
  OZ(ObSysVarFactory::calc_sys_var_store_idx(SYS_VAR_DEBUG_SYNC, store_idx));
  OV(ObSysVarFactory::is_valid_sys_var_store_idx(store_idx));
  OZ(process_session_debug_sync(sys_vars_[store_idx]->get_value(), false));
  // SYS_VAR_OB_GLOBAL_DEBUG_SYNC
  OZ(ObSysVarFactory::calc_sys_var_store_idx(SYS_VAR_OB_GLOBAL_DEBUG_SYNC, store_idx));
  OV(ObSysVarFactory::is_valid_sys_var_store_idx(store_idx));
  OZ(process_session_debug_sync(sys_vars_[store_idx]->get_value(), true));
  // SYS_VAR_OB_READ_CONSISTENCY
  OZ(ObSysVarFactory::calc_sys_var_store_idx(SYS_VAR_OB_READ_CONSISTENCY, store_idx));
  OV(ObSysVarFactory::is_valid_sys_var_store_idx(store_idx));
  if (OB_SUCC(ret)) {
    const ObObj& val = sys_vars_[store_idx]->get_value();
    int64_t consistency = 0;
    PROCESS_SESSION_INT_VARIABLE(consistency);
    OX(consistency_level_ = static_cast<ObConsistencyLevel>(consistency));
  }
  // SYS_VAR_WAIT_TIMEOUT
  {
    LockGuard lock_guard(thread_data_mutex_);
    get_int64_sys_var(SYS_VAR_WAIT_TIMEOUT, thread_data_.wait_timeout_);
  }
  return ret;
}

int ObBasicSessionInfo::process_session_sql_mode_value(const ObObj& value, bool is_inc)
{
  int ret = OB_SUCCESS;
  ObSQLMode sql_mode = 0;
  if (value.is_string_type()) {
    const ObString& sql_mode_str = value.get_string();
    if (OB_FAIL(ob_str_to_sql_mode(sql_mode_str, sql_mode))) {
      LOG_WARN("failed to get sql mode", K(sql_mode_str), K(value), K(ret));
    }
  } else if (ObUInt64Type == value.get_type()) {
    sql_mode = value.get_uint64();
  } else if (ObIntType == value.get_type()) {
    sql_mode = static_cast<ObSQLMode>(value.get_int());
  } else {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid sql mode val type", K(value.get_type()), K(value), K(ret));
  }
  if (OB_FAIL(ret)) {
  } else if (!is_sql_mode_supported(sql_mode)) {
    ret = OB_NOT_SUPPORTED;
    LOG_USER_ERROR(OB_NOT_SUPPORTED, "Value for sql_mode");
    LOG_WARN("invalid sql mode val", K(value.get_type()), K(value), K(ret));
  } else {
    set_sql_mode(sql_mode, is_inc);
  }
  return ret;
}

int ObBasicSessionInfo::check_compatibility_mode() const
{
  int ret = OB_SUCCESS;
  ObCompatibilityMode mode = get_compatibility_mode();
  if (ORACLE_MODE == mode && is_oracle_mode()) {
  } else if (MYSQL_MODE == mode && is_mysql_mode()) {
  } else {
    // sometimes mode is OCEANBASE_MODE because mode is not assigned, we can accept that,
    // but at least they must be consistent in oracle mode.
    if (ORACLE_MODE == mode && !is_oracle_mode()) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("compatibility mode is not match", K(mode), K(is_oracle_mode()), K(lbt()), K(ret));
    }
  }
  return ret;
}

int ObBasicSessionInfo::process_session_compatibility_mode_value(const ObObj& value, bool is_inc)
{
  int ret = OB_SUCCESS;
  ObCompatibilityMode comp_mode = ObCompatibilityMode::OCEANBASE_MODE;
  if (value.is_string_type()) {
    const ObString& comp_mode_str = value.get_string();
    if (comp_mode_str.case_compare("ORACLE")) {
      comp_mode = ObCompatibilityMode::ORACLE_MODE;
    } else if (comp_mode_str.case_compare("MYSQL")) {
      comp_mode = ObCompatibilityMode::MYSQL_MODE;
    } else {
      ret = OB_NOT_SUPPORTED;
      LOG_WARN("not supported sql mode", K(ret), K(value), K(comp_mode_str));
    }
  } else if (ObUInt64Type == value.get_type()) {
    comp_mode = static_cast<ObCompatibilityMode>(value.get_uint64());
  } else if (ObIntType == value.get_type()) {
    comp_mode = static_cast<ObCompatibilityMode>(value.get_int());
  } else {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid sql mode val type", K(value.get_type()), K(value), K(ret));
  }

  if (OB_SUCC(ret)) {
    set_compatibility_mode(comp_mode, is_inc);
    LOG_DEBUG("set compatibility mode", K(ret), K(comp_mode), K(value), K(get_compatibility_mode()), K(lbt()));
  }
  return ret;
}

int ObBasicSessionInfo::process_session_time_zone_value(const ObObj& value, const bool check_timezone_valid)
{
  int ret = OB_SUCCESS;
  ObString str_val;
  const bool is_oralce_mode = is_oracle_compatible();
  if (OB_FAIL(value.get_string(str_val))) {
    LOG_WARN("fail to get string value", K(value), K(ret));
  } else if (OB_FAIL(set_time_zone(str_val, is_oralce_mode, check_timezone_valid))) {
    LOG_WARN("failed to set time zone",
        K(str_val),
        K(is_oralce_mode),
        "is_oracle_compatible",
        is_oracle_compatible(),
        K(ret));
  }
  return ret;
}

int ObBasicSessionInfo::process_session_overlap_time_value(const ObObj& value)
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(tz_info_wrap_.set_error_on_overlap_time(value.get_bool()))) {
    LOG_WARN("fail to set error on overlap time", K(value), K(ret));
  }
  return ret;
}

int ObBasicSessionInfo::process_session_log_level(const ObObj& val)
{
  int ret = OB_SUCCESS;
  int32_t valid_length = 0;
  ObString val_str;
  if (OB_SUCC(val.get_varchar(val_str))) {
    if (0 == val_str.case_compare("disabled")) {
      log_id_level_map_valid_ = false;
      log_id_level_map_.reset_level();
    } else if (OB_FAIL(OB_LOGGER.parse_set(val_str.ptr(), val_str.length(), valid_length, log_id_level_map_))) {
      LOG_WARN("Failed to parse set log_level", K(ret), "log_level", val_str);
    } else {
      log_id_level_map_valid_ = true;
    }
  }
  return ret;
}

int ObBasicSessionInfo::process_session_debug_sync(const ObObj& val, const bool is_global)
{
  int ret = OB_SUCCESS;
  if (OB_SYS_TENANT_ID == tenant_id_ && GCONF.is_debug_sync_enabled()) {
    ObString debug_sync;
    if (OB_FAIL(val.get_varchar(debug_sync))) {
      LOG_WARN("varchar expected", K(ret));
    } else {
      if (!debug_sync.empty()) {
        if (OB_FAIL(GDS.add_debug_sync(debug_sync, is_global, debug_sync_actions_))) {
          LOG_WARN("set debug sync string failed", K(debug_sync), K(ret));
        }
      }
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_int64_sys_var(const ObSysVarClassType sys_var_id, int64_t& int64_val) const
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* val = NULL;
  if (OB_FAIL(inner_get_sys_var(sys_var_id, val))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(sys_var_id));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret), K(sys_var_id));
  } else {
    int64_t int_val = 0;
    if (OB_FAIL(val->get_value().get_int(int_val))) {
      LOG_WARN("fail to get int from value", K(*val), K(ret));
    } else {
      int64_val = int_val;
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_uint64_sys_var(const ObSysVarClassType sys_var_id, uint64_t& uint64_val) const
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* val = NULL;
  if (OB_FAIL(inner_get_sys_var(sys_var_id, val))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(sys_var_id));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret), K(sys_var_id));
  } else {
    uint64_t uint_val = 0;
    if (OB_FAIL(val->get_value().get_uint64(uint_val))) {
      LOG_ERROR("fail to get uint64 from value", K(*val), K(ret));
    } else {
      uint64_val = uint_val;
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_bool_sys_var(const ObSysVarClassType sys_var_id, bool& bool_val) const
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* val = NULL;
  if (OB_FAIL(inner_get_sys_var(sys_var_id, val))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(sys_var_id));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret), K(sys_var_id));
  } else {
    int64_t int_val = 0;
    if (OB_SUCCESS != (ret = val->get_value().get_int(int_val))) {
      LOG_ERROR("fail to get int from value", K(*val), K(ret));
    } else {
      bool_val = (0 != int_val);
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_charset_sys_var(const ObSysVarClassType sys_var_id, ObCharsetType& cs_type) const
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* val = NULL;
  if (OB_FAIL(inner_get_sys_var(sys_var_id, val))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(sys_var_id));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret), K(sys_var_id));
  } else {
    int64_t coll_int64 = 0;
    if (val->get_value().is_null()) {
      cs_type = CHARSET_INVALID;
    } else if (OB_FAIL(val->get_value().get_int(coll_int64))) {
      LOG_ERROR("fail to get int from value", K(*val), K(ret));
    } else if (OB_UNLIKELY(false == ObCharset::is_valid_collation(coll_int64))) {
      LOG_ERROR("invalid collation", K(sys_var_id), K(coll_int64), K(*val));
    } else {
      cs_type = ObCharset::charset_type_by_coll(static_cast<ObCollationType>(coll_int64));
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_collation_sys_var(ObSysVarClassType sys_var_id, ObCollationType& coll_type) const
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* val = NULL;
  if (OB_FAIL(inner_get_sys_var(sys_var_id, val))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(sys_var_id));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get sys var, but sys var is NULL", K(ret));
  } else {
    int64_t coll_int64 = 0;
    if (OB_FAIL(val->get_value().get_int(coll_int64))) {
      LOG_ERROR("fail to get int from value", K(ret), K(*val));
    } else if (OB_UNLIKELY(false == ObCharset::is_valid_collation(coll_int64))) {
      LOG_ERROR("invalid collation", K(sys_var_id), K(coll_int64), K(*val));
    } else {
      coll_type = static_cast<ObCollationType>(coll_int64);
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_string_sys_var(ObSysVarClassType sys_var_id, ObString& str) const
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* val = NULL;
  if (OB_FAIL(inner_get_sys_var(sys_var_id, val))) {
    LOG_WARN("fail to inner get sys var", K(ret), K(sys_var_id));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("succ to inner get says var, but sys var is NULL", K(ret));
  } else {
    if (OB_FAIL(val->get_value().get_string(str))) {
      LOG_ERROR("fail to get int from value", K(ret), K(*val));
    }
  }
  return ret;
}

int ObBasicSessionInfo::if_aggr_pushdown_allowed(bool& aggr_pushdown_allowed) const
{
  return get_bool_sys_var(SYS_VAR_OB_ENABLE_AGGREGATION_PUSHDOWN, aggr_pushdown_allowed);
}

int ObBasicSessionInfo::is_use_trace_log(bool& use_trace_log) const
{
  use_trace_log = sys_vars_cache_.get_ob_enable_trace_log();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::is_use_transmission_checksum(bool& use_transmission_checksum) const
{
  return get_bool_sys_var(SYS_VAR_OB_ENABLE_TRANSMISSION_CHECKSUM, use_transmission_checksum);
}

int ObBasicSessionInfo::get_name_case_mode(ObNameCaseMode& case_mode) const
{
  int ret = OB_SUCCESS;
  int64_t int64_val = -1;
  if (OB_FAIL(get_sys_variable(SYS_VAR_LOWER_CASE_TABLE_NAMES, int64_val))) {
    LOG_WARN("failed to load sys param", "var_name", OB_SV_LOWER_CASE_TABLE_NAMES, K(ret));
  } else {
    int32_t value = static_cast<int32_t>(int64_val);
    if (value <= OB_NAME_CASE_INVALID || value >= OB_NAME_CASE_MAX) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("invalid value", "var_name", OB_SV_LOWER_CASE_TABLE_NAMES, K(value), K(ret));
    } else {
      case_mode = static_cast<ObNameCaseMode>(value);
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_init_connect(ObString& str) const
{
  return get_string_sys_var(SYS_VAR_INIT_CONNECT, str);
}

int ObBasicSessionInfo::is_transformation_enabled(bool& transformation_enabled) const
{
  return get_bool_sys_var(SYS_VAR_OB_ENABLE_TRANSFORMATION, transformation_enabled);
}

int ObBasicSessionInfo::is_select_index_enabled(bool& select_index_enabled) const
{
  return get_bool_sys_var(SYS_VAR_OB_ENABLE_INDEX_DIRECT_SELECT, select_index_enabled);
}

int ObBasicSessionInfo::is_create_table_strict_mode(bool& strict_mode) const
{
  return get_bool_sys_var(SYS_VAR_OB_CREATE_TABLE_STRICT_MODE, strict_mode);
}

void ObBasicSessionInfo::set_autocommit(bool autocommit)
{
  sys_vars_cache_.set_autocommit(autocommit, true);
}

int ObBasicSessionInfo::get_autocommit(bool& autocommit) const
{
  autocommit = sys_vars_cache_.get_autocommit();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_explicit_defaults_for_timestamp(bool& explicit_defaults_for_timestamp) const
{
  return get_bool_sys_var(SYS_VAR_EXPLICIT_DEFAULTS_FOR_TIMESTAMP, explicit_defaults_for_timestamp);
}

int ObBasicSessionInfo::get_sql_auto_is_null(bool& sql_auto_is_null) const
{
  return get_bool_sys_var(SYS_VAR_SQL_AUTO_IS_NULL, sql_auto_is_null);
}

int ObBasicSessionInfo::get_is_result_accurate(bool& is_result_accurate) const
{
  is_result_accurate = sys_vars_cache_.get_is_result_accurate();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_div_precision_increment(int64_t& div_precision_increment) const
{
  int ret = get_int64_sys_var(SYS_VAR_DIV_PRECISION_INCREMENT, div_precision_increment);
  return ret;
}

int ObBasicSessionInfo::get_tx_timeout(int64_t& tx_timeout) const
{
  tx_timeout = sys_vars_cache_.get_ob_trx_timeout();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_query_timeout(int64_t& query_timeout) const
{
  query_timeout = sys_vars_cache_.get_ob_query_timeout();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_sql_throttle_current_priority(int64_t& sql_throttle_current_priority)
{
  sql_throttle_current_priority = sys_vars_cache_.get_sql_throttle_current_priority();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_ob_last_schema_version(int64_t& ob_last_schema_version)
{
  ob_last_schema_version = sys_vars_cache_.get_ob_last_schema_version();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_pl_block_timeout(int64_t& pl_block_timeout) const
{
  pl_block_timeout = sys_vars_cache_.get_ob_pl_block_timeout();
  return OB_SUCCESS;
}

int64_t ObBasicSessionInfo::get_trx_lock_timeout() const
{
  return sys_vars_cache_.get_ob_trx_lock_timeout();
}

int ObBasicSessionInfo::get_binlog_row_image(int64_t& binlog_row_image) const
{
  binlog_row_image = sys_vars_cache_.get_binlog_row_image();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_ob_read_consistency(int64_t& ob_read_consistency) const
{
  return get_int64_sys_var(SYS_VAR_OB_READ_CONSISTENCY, ob_read_consistency);
}

int ObBasicSessionInfo::get_sql_select_limit(int64_t& sql_select_limit) const
{
  sql_select_limit = sys_vars_cache_.get_sql_select_limit();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_ob_stmt_parallel_degree(int64_t& ob_stmt_parallel_degree) const
{
  return get_int64_sys_var(SYS_VAR_OB_STMT_PARALLEL_DEGREE, ob_stmt_parallel_degree);
}

int ObBasicSessionInfo::use_parallel_execution(bool& v) const
{
  v = sys_vars_cache_.get__ob_use_parallel_execution();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_ob_stmt_parallel_degree_min_max(
    int64_t& min_ob_stmt_parallel_degree, int64_t& max_ob_stmt_parallel_degree) const
{
  int ret = OB_SUCCESS;
  ObBasicSysVar* val = NULL;
  if (OB_FAIL(inner_get_sys_var(SYS_VAR_OB_STMT_PARALLEL_DEGREE, val))) {
    LOG_WARN("fail to inner get sys var", K(ret));
  } else if (OB_ISNULL(val)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("succ to inner get sys var, but sys var is NULL", K(ret));
  } else if (OB_FAIL(val->get_min_val().get_int(min_ob_stmt_parallel_degree))) {
    LOG_WARN("fail to get int from value", K(*val), K(ret));
  } else if (OB_FAIL(val->get_max_val().get_int(max_ob_stmt_parallel_degree))) {
    LOG_WARN("fail to get int from value", K(*val), K(ret));
  }
  return ret;
}

int ObBasicSessionInfo::get_group_concat_max_len(uint64_t& group_concat_max_len) const
{
  return get_uint64_sys_var(SYS_VAR_GROUP_CONCAT_MAX_LEN, group_concat_max_len);
}

int ObBasicSessionInfo::get_ob_interm_result_mem_limit(int64_t& ob_interm_result_mem_limit) const
{
  return get_int64_sys_var(SYS_VAR_OB_INTERM_RESULT_MEM_LIMIT, ob_interm_result_mem_limit);
}

int ObBasicSessionInfo::get_ob_org_cluster_id(int64_t& ob_org_cluster_id) const
{
  ob_org_cluster_id = sys_vars_cache_.get_ob_org_cluster_id();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_max_allowed_packet(int64_t& max_allowed_pkt) const
{
  return get_int64_sys_var(SYS_VAR_MAX_ALLOWED_PACKET, max_allowed_pkt);
}

int ObBasicSessionInfo::get_net_buffer_length(int64_t& net_buffer_len) const
{
  return get_int64_sys_var(SYS_VAR_NET_BUFFER_LENGTH, net_buffer_len);
}

////////////////////////////////////////////////////////////////
int ObBasicSessionInfo::replace_user_variables(const ObSessionValMap& user_var_map)
{
  int ret = OB_SUCCESS;
  if (user_var_map.size() > 0) {
    const sql::ObSessionValMap::VarNameValMap& new_map = user_var_map.get_val_map();
    for (sql::ObSessionValMap::VarNameValMap::const_iterator iter = new_map.begin();
         OB_SUCC(ret) && iter != new_map.end();
         iter++) {
      const common::ObString& key = iter->first;
      const sql::ObSessionVariable& value = iter->second;
      if (OB_FAIL(replace_user_variable(key, value))) {
        LOG_WARN("fail to replace user var", K(ret), K(key), K(value));
      }
    }
  }
  return ret;
}

int ObBasicSessionInfo::replace_user_variable(const ObString& var, const ObSessionVariable& val)
{
  int ret = OB_SUCCESS;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid var name", K(var), K(ret));
  } else if (OB_FAIL(user_var_val_map_.set_refactored(var, val))) {
    LOG_ERROR("fail to add variable", K(var), K(ret));
  } else {
    if (is_track_session_info()) {
      if (OB_FAIL(track_user_var(var))) {
        LOG_WARN("fail to track user var", K(var), K(ret));
      }
    }
    if (OB_SUCC(ret)) {
      variables_last_modify_time_ = ObTimeUtility::current_time();
    }
  }
  return ret;
}

int ObBasicSessionInfo::remove_user_variable(const ObString& var)
{
  int ret = OB_SUCCESS;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(ret));
  } else if (OB_SUCCESS != user_var_val_map_.erase_refactored(var)) {
    ret = OB_ERR_USER_VARIABLE_UNKNOWN;
    LOG_WARN("unknown variable", K(var), K(ret));
  } else {
    variables_last_modify_time_ = ObTimeUtility::current_time();
  }
  return ret;
}

int ObBasicSessionInfo::get_user_variable(const ObString& var, ObSessionVariable& val) const
{
  int ret = OB_SUCCESS;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(ret));
  } else if (OB_SUCCESS != user_var_val_map_.get_refactored(var, val)) {
    ret = OB_ERR_USER_VARIABLE_UNKNOWN;
    LOG_WARN("unknown user variable", K(var), K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::get_user_variable_value(const ObString& var, common::ObObj& val) const
{
  int ret = OB_SUCCESS;
  ObSessionVariable sess_var;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(ret));
  } else if (OB_SUCCESS != user_var_val_map_.get_refactored(var, sess_var)) {
    ret = OB_ERR_USER_VARIABLE_UNKNOWN;
    LOG_WARN("unknown user variable", K(var), K(ret));
  } else {
    val = sess_var.value_;
  }
  return ret;
}

int ObBasicSessionInfo::get_user_variable_meta(const ObString& var, ObObjMeta& meta) const
{
  int ret = OB_SUCCESS;
  ObSessionVariable val;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(ret));
  } else if (OB_SUCCESS != user_var_val_map_.get_refactored(var, val)) {
    ret = OB_ERR_USER_VARIABLE_UNKNOWN;
    LOG_WARN("unknown user variable", K(var), K(ret));
  } else {
    meta = val.meta_;
  }
  return ret;
}

bool ObBasicSessionInfo::user_variable_exists(const ObString& var) const
{
  ObSessionVariable val;
  bool exist = false;
  int ret = OB_SUCCESS;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(ret));
  } else {
    exist = (OB_SUCCESS == user_var_val_map_.get_refactored(var, val));
  }
  return exist;
}

const ObSessionVariable* ObBasicSessionInfo::get_user_variable(const common::ObString& var) const
{
  const ObSessionVariable* sess_var = NULL;
  int ret = OB_SUCCESS;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(ret));
  } else {
    sess_var = user_var_val_map_.get(var);
  }
  return sess_var;
}

const common::ObObj* ObBasicSessionInfo::get_user_variable_value(const ObString& var) const
{
  const ObObj* val_obj = NULL;
  const ObSessionVariable* sess_var = NULL;
  int ret = OB_SUCCESS;
  if (var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid variable name", K(var), K(ret));
  } else if (NULL != (sess_var = user_var_val_map_.get(var))) {
    val_obj = &(sess_var->value_);
  } else {
  }  // just return NULL
  return val_obj;
}

////////////////////////////////////////////////////////////////
int64_t ObBasicSessionInfo::to_string(char* buf, const int64_t buf_len) const
{
  int64_t pos = 0;
  J_KV(N_TENANT,
      get_tenant_name(),
      N_EFFECTIVE_TENANT,
      get_effective_tenant_name(),
      N_DATABASE,
      get_database_name(),
      N_USER,
      (lib::is_oracle_mode() ? get_user_name() : get_user_at_host()),
      "consistency_level",
      consistency_level_,
      "consistency_type",
      trans_consistency_type_,
      "safe_weak_snapshot_version",
      safe_weak_read_snapshot_,
      "weak_read_snapshot_source",
      weak_read_snapshot_source_,
      "weak_read_snapshot_source_str",
      source_to_string(weak_read_snapshot_source_));
  return pos;
}

int ObBasicSessionInfo::calc_need_serialize_vars(
    ObIArray<ObSysVarClassType>& sys_var_ids, ObIArray<ObString>& user_var_names, int64_t ser_version) const
{
  int ret = OB_SUCCESS;
  sys_var_ids.reset();
  user_var_names.reset();
  if (ser_version == CUR_SER_VERSION) {
    if (OB_FAIL(sys_var_ids.assign(sys_var_inc_info_.get_all_sys_var_ids()))) {
      LOG_WARN("fail to assign ser_sys_var_ids", K(ret));
    }
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < ObSysVariables::get_all_sys_var_count(); ++i) {
      bool can_serialize = true;
      if (exec_min_cluster_version_ < CLUSTER_VERSION_2230) {
        can_serialize = true;
      } else {
        can_serialize = !(is_mysql_mode() && (ObSysVariables::get_flags(i) & ObSysVarFlag::ORACLE_ONLY));
      }
      if (((ObSysVariables::get_flags(i) & ObSysVarFlag::NEED_SERIALIZE) ||
              (ObSysVariables::get_flags(i) & ObSysVarFlag::QUERY_SENSITIVE)) &&
          can_serialize) {
        if (OB_FAIL(sys_var_ids.push_back(ObSysVariables::get_sys_var_id(i)))) {
          LOG_WARN("fail to push back sys var id", K(i), K(ObSysVariables::get_sys_var_id(i)), K(sys_var_ids), K(ret));
        }
      }
    }
  }

  CK(OB_NOT_NULL(cur_phy_plan_));
  if (OB_SUCC(ret)) {
    // process user variables and system variables that need to be serialized for current query.
    const ObIArray<ObVarInfo>& extra_serialize_vars = cur_phy_plan_->get_vars();
    for (int64_t i = 0; OB_SUCC(ret) && i < extra_serialize_vars.count(); ++i) {
      const ObVarInfo& var_info = extra_serialize_vars.at(i);
      if (USER_VAR == var_info.type_) {
        if (OB_FAIL(user_var_names.push_back(var_info.name_))) {
          LOG_WARN("fail to push user var name", K(var_info), K(user_var_names), K(ret));
        }
      } else if (SYS_VAR == var_info.type_) {
        ObSysVarClassType sys_var_id = ObSysVarFactory::find_sys_var_id_by_name(var_info.name_);
        if (SYS_VAR_INVALID == sys_var_id) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("invalid sys var id", K(sys_var_id), K(var_info), K(ret));
        } else {
          bool sys_var_exist = false;
          for (int64_t j = 0; OB_SUCC(ret) && !sys_var_exist && j < sys_var_ids.count(); ++j) {
            if (sys_var_id == sys_var_ids.at(j)) {
              sys_var_exist = true;
            }
          }
          if (OB_SUCCESS == ret && !sys_var_exist) {
            if (OB_FAIL(sys_var_ids.push_back(sys_var_id))) {
              LOG_WARN("fail to push back sys var id", K(sys_var_id), K(var_info), K(sys_var_ids), K(ret));
            }
          }
        }
      } else {
        ret = OB_ERR_UNEXPECTED;
        LOG_ERROR("invalid var info type", K(var_info.type_), K(var_info), K(ret));
      }
    }
  }
  return ret;
}

OB_SERIALIZE_MEMBER(ObBasicSessionInfo::TableStmtType, table_id_, stmt_type_);

OB_DEF_SERIALIZE(ObBasicSessionInfo::SysVarsCacheData)
{
  int ret = OB_SUCCESS;
  LST_DO_CODE(OB_UNIS_ENCODE,
      autocommit_,
      ob_enable_trace_log_,
      ob_org_cluster_id_,
      ob_query_timeout_,
      ob_trx_timeout_,
      collation_connection_,
      sql_mode_,
      ob_trx_idle_timeout_,
      nls_collation_,
      nls_nation_collation_,
      ob_enable_sql_audit_,
      nls_length_semantics_,
      nls_formats_[NLS_DATE],
      nls_formats_[NLS_TIMESTAMP],
      nls_formats_[NLS_TIMESTAMP_TZ],
      ob_trx_lock_timeout_,
      ob_trace_info_);
  return ret;
}

OB_DEF_DESERIALIZE(ObBasicSessionInfo::SysVarsCacheData)
{
  int ret = OB_SUCCESS;
  LST_DO_CODE(OB_UNIS_DECODE,
      autocommit_,
      ob_enable_trace_log_,
      ob_org_cluster_id_,
      ob_query_timeout_,
      ob_trx_timeout_,
      collation_connection_,
      sql_mode_,
      ob_trx_idle_timeout_,
      nls_collation_,
      nls_nation_collation_,
      ob_enable_sql_audit_,
      nls_length_semantics_,
      nls_formats_[NLS_DATE],
      nls_formats_[NLS_TIMESTAMP],
      nls_formats_[NLS_TIMESTAMP_TZ],
      ob_trx_lock_timeout_,
      ob_trace_info_);
  set_nls_date_format(nls_formats_[NLS_DATE]);
  set_nls_timestamp_format(nls_formats_[NLS_TIMESTAMP]);
  set_nls_timestamp_tz_format(nls_formats_[NLS_TIMESTAMP_TZ]);
  set_ob_trace_info(ob_trace_info_);
  return ret;
}

OB_DEF_SERIALIZE_SIZE(ObBasicSessionInfo::SysVarsCacheData)
{
  int64_t len = 0;
  LST_DO_CODE(OB_UNIS_ADD_LEN,
      autocommit_,
      ob_enable_trace_log_,
      ob_org_cluster_id_,
      ob_query_timeout_,
      ob_trx_timeout_,
      collation_connection_,
      sql_mode_,
      ob_trx_idle_timeout_,
      nls_collation_,
      nls_nation_collation_,
      ob_enable_sql_audit_,
      nls_length_semantics_,
      nls_formats_[NLS_DATE],
      nls_formats_[NLS_TIMESTAMP],
      nls_formats_[NLS_TIMESTAMP_TZ],
      ob_trx_lock_timeout_,
      ob_trace_info_);
  return len;
}

int64_t ObBasicSessionInfo::get_ser_version(uint64_t tenant_id, uint64_t eff_tenant_id) const
{
  return (GET_UNIS_CLUSTER_VERSION() < CLUSTER_VERSION_2250 || tenant_id == OB_SYS_TENANT_ID ||
             eff_tenant_id == OB_SYS_TENANT_ID)
             ? PRE_SER_VERSION
             : CUR_SER_VERSION;
}

OB_DEF_SERIALIZE(ObBasicSessionInfo)
{
  int ret = OB_SUCCESS;
  int64_t ser_version = get_ser_version(tenant_id_, effective_tenant_id_);
  ObTimeZoneInfo tmp_tz_info;  // for compatibility with old version
  bool in_transaction = trans_flags_.is_in_transaction();
  // To be compatible with old version which store sql_mode and compatibility mode in ObSQLModeManager;
  int64_t compatibility_mode_index = 0;
  if (OB_FAIL(compatibility_mode2index(get_compatibility_mode(), compatibility_mode_index))) {
    LOG_WARN("convert compatibility mode to index failed", K(ret));
  }

  LST_DO_CODE(OB_UNIS_ENCODE,
      in_transaction,
      trans_desc_,
      consistency_level_,
      compatibility_mode_index,
      tmp_tz_info,
      // NOTE: rpc_tenant_id may cause compatability problem,
      // But only in diagnose tenant, so keep the stupid hack as it is.
      tenant_id_ | (rpc_tenant_id_ << 32),
      effective_tenant_id_,
      is_changed_to_temp_tenant_,
      user_id_,
      is_master_session() ? sessid_ : master_sessid_,
      capability_.capability_,
      thread_data_.database_name_);
  // serialize user variables and system variables.
  ObSEArray<ObSysVarClassType, 128> sys_var_ids;
  ObSEArray<ObString, 32> user_var_names;
  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(calc_need_serialize_vars(sys_var_ids, user_var_names, ser_version))) {
    LOG_WARN("fail to calc need serialize vars", K(ret));
  } else {
    ObSEArray<std::pair<ObString, ObSessionVariable>, 32> actual_ser_user_vars;
    ObSessionVariable user_var_val;
    for (int64_t i = 0; OB_SUCC(ret) && i < user_var_names.count(); ++i) {
      user_var_val.reset();
      const ObString& user_var_name = user_var_names.at(i);
      ret = user_var_val_map_.get_refactored(user_var_name, user_var_val);
      if (OB_SUCCESS != ret && OB_HASH_NOT_EXIST != ret) {
        LOG_WARN("fail to get user var from session user var map",
            K(i),
            K(user_var_name),
            K(user_var_val_map_.size()),
            K(ret));
      } else {
        if (OB_SUCCESS == ret && OB_FAIL(actual_ser_user_vars.push_back(std::make_pair(user_var_name, user_var_val)))) {
          LOG_WARN("fail to push back pair(user_var_name, user_var_val)",
              K(buf_len),
              K(pos),
              K(user_var_name),
              K(user_var_val),
              K(ret));
        } else {
          ret = OB_SUCCESS;
        }
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(serialization::encode(buf, buf_len, pos, actual_ser_user_vars.count()))) {
        LOG_WARN("fail to serialize user var count", K(ret), K(buf_len), K(pos), K(actual_ser_user_vars.count()));
      } else {
        for (int64_t i = 0; OB_SUCC(ret) && i < actual_ser_user_vars.count(); ++i) {
          const ObString& user_var_name = actual_ser_user_vars.at(i).first;
          const ObSessionVariable& user_var_val = actual_ser_user_vars.at(i).second;
          if (OB_FAIL(serialization::encode(buf, buf_len, pos, user_var_name))) {
            LOG_WARN("fail to serialize user var name", K(buf_len), K(pos), K(user_var_name), K(ret));
          } else if (OB_FAIL(serialization::encode(buf, buf_len, pos, user_var_val.meta_))) {
            LOG_WARN("fail to serialize user var val meta", K(buf_len), K(pos), K(user_var_val.meta_), K(ret));
          } else if (OB_FAIL(serialization::encode(buf, buf_len, pos, user_var_val.value_))) {
            LOG_WARN("fail to serialize user var val value", K(buf_len), K(pos), K(user_var_val.value_), K(ret));
          } else {
          }
        }
      }
    }
  }

  if (OB_SUCC(ret)) {
    if (ser_version == CUR_SER_VERSION) {
      OB_UNIS_ENCODE(sys_var_base_version_);
      OB_UNIS_ENCODE(sys_var_inc_info_);
    }
    if (OB_FAIL(serialization::encode(buf, buf_len, pos, sys_var_ids.count()))) {
      LOG_WARN("fail to serialize sys var count", K(buf_len), K(pos), K(sys_var_ids.count()), K(ret));
    }
    for (int64_t i = 0; OB_SUCC(ret) && i < sys_var_ids.count(); ++i) {
      int64_t sys_var_idx = -1;
      if (OB_FAIL(ObSysVarFactory::calc_sys_var_store_idx(sys_var_ids.at(i), sys_var_idx))) {
        LOG_WARN("fail to calc sys var store idx", K(i), K(sys_var_idx), K(sys_var_ids.at(i)), K(ret));
      } else if (sys_var_idx < 0 || get_sys_var_count() <= sys_var_idx) {
        ret = OB_ERR_UNEXPECTED;
        LOG_ERROR("sys var idx is invalid", K(sys_var_idx), K(get_sys_var_count()), K(ret));
      } else if (OB_ISNULL(sys_vars_[sys_var_idx])) {
        ret = OB_ERR_UNEXPECTED;
        LOG_ERROR("sys var is NULL", K(ret), K(sys_var_idx), K(get_sys_var_count()));
      } else {
        int16_t sys_var_id = static_cast<int16_t>(sys_vars_[sys_var_idx]->get_type());
        if (OB_FAIL(serialization::encode(buf, buf_len, pos, sys_var_id))) {
          LOG_ERROR("fail to serialize sys var id", K(buf_len), K(pos), K(sys_var_id), K(ret));
        } else if (OB_FAIL(sys_vars_[sys_var_idx]->serialize(buf, buf_len, pos))) {
          LOG_ERROR("fail to serialize sys var",
              K(buf_len),
              K(pos),
              K(i),
              K(sys_var_idx),
              K(*sys_vars_[sys_var_idx]),
              K(ret));
        }
      }
    }
  }

  if (OB_FAIL(ret)) {
    LOG_WARN("fail to encode session info", K(ret));
  } else {
    bool tx_read_only = get_tx_read_only();
    if (OB_FAIL(serialization::encode_bool(buf, buf_len, pos, tx_read_only))) {
      LOG_WARN("fail to encode tx_read_only", K(ret));
    }
  }

  // for compatibility with old version
  bool unused_literal_query = false;
  int64_t unused_inner_safe_weak_read_snapshot = safe_weak_read_snapshot_;

  bool need_serial_exec = trans_flags_.need_serial_exec();
O
obdev 已提交
2995
  uint64_t sql_scope_flags = 0;
O
oceanbase-admin 已提交
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
  LST_DO_CODE(OB_UNIS_ENCODE,
      sys_vars_cache_.inc_data_,
      trans_consistency_type_,
      safe_weak_read_snapshot_,
      unused_inner_safe_weak_read_snapshot,
      unused_literal_query,
      tz_info_wrap_,
      app_trace_id_,
      proxy_capability_.capability_,
      client_mode_,
      proxy_sessid_,
      nested_count_,
      thread_data_.user_name_,
      next_tx_isolation_,
      read_snapshot_version_,
      check_sys_variable_,
      weak_read_snapshot_source_,
      database_id_,
      thread_data_.user_at_host_name_,
      thread_data_.user_at_client_ip_,
      current_execution_id_,
      total_stmt_tables_,
      cur_stmt_tables_,
      is_foreign_key_cascade_,
      sys_var_in_pc_str_,
      is_foreign_key_check_exist_,
O
obdev 已提交
3022 3023 3024
      need_serial_exec,
      sql_scope_flags,
      stmt_type_);
O
oceanbase-admin 已提交
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
  return ret;
}

OB_DEF_DESERIALIZE(ObBasicSessionInfo)
{
  int ret = OB_SUCCESS;
  ObTimeZoneInfo tmp_tz_info;  // for compatibility with old version
  bool in_transaction = false;
  int64_t compatibility_mode_index = 0;
  is_deserialized_ = true;
  LST_DO_CODE(OB_UNIS_DECODE,
      in_transaction,
      trans_desc_,
      consistency_level_,
      compatibility_mode_index,
      tmp_tz_info,
      tenant_id_,
      effective_tenant_id_,
      is_changed_to_temp_tenant_,
      user_id_,
      master_sessid_,
      capability_.capability_,
      thread_data_.database_name_);
  int64_t ser_version = get_ser_version(tenant_id_, effective_tenant_id_);
  trans_flags_.set_is_in_transaction(in_transaction);
  rpc_tenant_id_ = (tenant_id_ >> 32);
  tenant_id_ = (tenant_id_ << 32 >> 32);

  // deserialize user variables and system variables.
  int64_t deserialize_user_var_count = 0;
  int64_t deserialize_sys_var_count = 0;

  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(serialization::decode(buf, data_len, pos, deserialize_user_var_count))) {
    LOG_WARN("fail to deserialize user var count", K(data_len), K(pos), K(ret));
  } else {
    ObSessionVariable user_var_val;
    for (int64_t i = 0; OB_SUCC(ret) && i < deserialize_user_var_count; ++i) {
      ObString user_var_name;
      user_var_val.reset();
      if (OB_FAIL(serialization::decode(buf, data_len, pos, user_var_name))) {
        LOG_WARN("fail to deserialize user var name", K(i), K(data_len), K(pos), K(ret));
      } else if (OB_FAIL(serialization::decode(buf, data_len, pos, user_var_val.meta_))) {
        LOG_WARN("fail to deserialize user var val meta", K(i), K(data_len), K(pos), K(ret));
      } else if (OB_FAIL(serialization::decode(buf, data_len, pos, user_var_val.value_))) {
        LOG_WARN("fail to deserialize user var val value", K(i), K(data_len), K(pos), K(ret));
      } else {
        if (OB_FAIL(user_var_val_map_.set_refactored(user_var_name, user_var_val))) {
          LOG_WARN("Insert value into map failed", K(user_var_name), K(user_var_val), K(ret));
        } else {
          ret = OB_SUCCESS;
        }
      }
    }
  }

  if (ser_version == CUR_SER_VERSION) {
    OB_UNIS_DECODE(des_sys_var_base_version_);
    OB_UNIS_DECODE(sys_var_inc_info_);
    OV(des_sys_var_base_version_ > OB_INVALID_VERSION);
    if (des_sys_var_base_version_ == sys_var_base_version_) {
      // skip.
    } else if (des_sys_var_base_version_ > 0) {
      ObSchemaGetterGuard schema_guard;
      OZ(GCTX.schema_service_->get_tenant_schema_guard(effective_tenant_id_, schema_guard, des_sys_var_base_version_));
      OZ(load_all_sys_vars(schema_guard));
      if (OB_FAIL(ret) && is_schema_error(ret)) {
        ret = OB_ERR_WAIT_REMOTE_SCHEMA_REFRESH;
      }
    } else {
      OZ(load_all_sys_vars_default());
    }
  }
  if (OB_SUCC(ret)) {
    ObTZMapWrap tz_map_wrap;
    if (OB_FAIL(OTTZ_MGR.get_tenant_tz(tenant_id_, tz_map_wrap))) {
      LOG_WARN("get tenant timezone map failed", K(ret));
    } else {
      tz_info_wrap_.set_tz_info_map(tz_map_wrap.get_tz_map());
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(serialization::decode(buf, data_len, pos, deserialize_sys_var_count))) {
      LOG_WARN("fail to deserialize sys var count", K(data_len), K(pos), K(ret));
    } else {
      const bool check_timezone_valid = false;
      for (int64_t i = 0; OB_SUCC(ret) && i < deserialize_sys_var_count; ++i) {
        ObObj tmp_val;
        ObBasicSysVar* sys_var = NULL;
        ObSysVarClassType sys_var_id = SYS_VAR_INVALID;
        int16_t tmp_sys_var_id = -1;
        int64_t store_idx = -1;
        if (OB_FAIL(serialization::decode(buf, data_len, pos, tmp_sys_var_id))) {
          LOG_WARN("fail to deserialize sys var id", K(data_len), K(pos), K(ret));
        } else if (FALSE_IT(sys_var_id = static_cast<ObSysVarClassType>(tmp_sys_var_id))) {
        } else if (ser_version == PRE_SER_VERSION && OB_FAIL(sys_var_inc_info_.add_sys_var_id(sys_var_id))) {
          LOG_WARN("fail to add sys var id", K(sys_var_id), K(ret));
        } else if (OB_FAIL(ObSysVarFactory::calc_sys_var_store_idx(sys_var_id, store_idx))) {
          if (OB_SYS_VARS_MAYBE_DIFF_VERSION == ret) {
            // observer of higher version serialized some system variables which are
            // not supported in current version, just skip this data and continue.
            ret = OB_SUCCESS;
            int64_t sys_var_version = 0;
            int64_t sys_var_len = 0;
            OB_UNIS_DECODEx(sys_var_version);
            OB_UNIS_DECODEx(sys_var_len);
            if (OB_SUCC(ret)) {
              pos += sys_var_len;
              LOG_WARN("invalid sys var id, maybe version is different, skip it", K(sys_var_id));
            }
          } else {
            LOG_ERROR("invalid sys var id", K(sys_var_id), K(ret));
          }
        } else if (OB_FAIL(create_sys_var(sys_var_id, store_idx, sys_var))) {
          LOG_WARN("fail to create sys var", K(sys_var_id), K(ret));
        } else if (OB_ISNULL(sys_var)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("create sys var is NULL", K(ret));
        } else if (OB_FAIL(sys_var->deserialize(buf, data_len, pos))) {
          LOG_WARN("fail to deserialize sys var", K(data_len), K(pos), K(sys_var_id), K(ret));
        } else if (OB_FAIL(deep_copy_sys_variable(sys_var_id, sys_var->get_value(), &tmp_val))) {
          LOG_WARN("fail to update system variable", K(sys_var_id), K(sys_var->get_value()), K(ret));
        } else if (FALSE_IT(sys_var->set_value(tmp_val))) {
        } else if (OB_FAIL(process_session_variable(sys_var_id, sys_var->get_value(), true, check_timezone_valid))) {
          LOG_ERROR("process system variable error", K(ret), K(*sys_var));
        }
      }
    }

    if (OB_SUCC(ret)) {
      bool tx_read_only = false;
      if (OB_FAIL(serialization::decode_bool(buf, data_len, pos, &tx_read_only))) {
        LOG_WARN("fail to decode tx_read_only", K(ret));
      } else {
        // direct assignment because in this case we won't track trans_specified_status
        next_tx_read_only_ = tx_read_only;
      }
    }
  }

  int64_t unused_inner_safe_weak_read_snapshot = 0;
  bool unused_literal_query = false;
  bool need_serial_exec = false;
O
obdev 已提交
3168
  uint64_t sql_scope_flags = 0;
O
oceanbase-admin 已提交
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

  // sys_var_in_pc_str_ may be set when deserialize system variables,
  // so reset it before deserialization of itself.
  sys_var_in_pc_str_.reset();
  const ObTZInfoMap* tz_info_map = tz_info_wrap_.get_tz_info_offset().get_tz_info_map();
  LST_DO_CODE(OB_UNIS_DECODE,
      sys_vars_cache_.inc_data_,
      trans_consistency_type_,
      safe_weak_read_snapshot_,
      unused_inner_safe_weak_read_snapshot,
      unused_literal_query,
      tz_info_wrap_,
      app_trace_id_,
      proxy_capability_.capability_,
      client_mode_,
      proxy_sessid_,
      nested_count_,
      thread_data_.user_name_,
      next_tx_isolation_,
      read_snapshot_version_,
      check_sys_variable_,
      weak_read_snapshot_source_,
      database_id_,
      thread_data_.user_at_host_name_,
      thread_data_.user_at_client_ip_,
      current_execution_id_,
      total_stmt_tables_,
      cur_stmt_tables_,
      is_foreign_key_cascade_,
      sys_var_in_pc_str_,
      is_foreign_key_check_exist_,
O
obdev 已提交
3200 3201 3202
      need_serial_exec,
      sql_scope_flags,
      stmt_type_);
O
oceanbase-admin 已提交
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 3229 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 3320 3321 3322 3323 3324 3325 3326 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
  trans_flags_.set_need_serial_exec(need_serial_exec);
  is_deserialized_ = true;
  tz_info_wrap_.set_tz_info_map(tz_info_map);
  if (OB_SUCC(ret)) {
    if (unused_inner_safe_weak_read_snapshot > safe_weak_read_snapshot_) {
      LOG_INFO("ObBasicSessionInfo::deserialize: use inner safe weak read snapshot, "
               "which comes from cluster before 2.2",
          K(unused_inner_safe_weak_read_snapshot),
          K(safe_weak_read_snapshot_),
          K(*this));
      safe_weak_read_snapshot_ = unused_inner_safe_weak_read_snapshot;
    }
  }

  if (OB_SUCC(ret) && GET_MIN_CLUSTER_VERSION() < CLUSTER_CURRENT_VERSION) {
    for (int64_t i = 0; OB_SUCC(ret) && i < get_sys_var_count(); ++i) {
      if ((ObSysVariables::get_flags(i) & ObSysVarFlag::NEED_SERIALIZE) && OB_ISNULL(sys_vars_[i])) {
        if (OB_FAIL(load_default_sys_variable(i))) {
          LOG_WARN("fail to load default sys variable", K(ret), K(i));
        }
      }
    }
  }
  release_to_pool_ = OB_SUCC(ret);
  return ret;
}

OB_DEF_SERIALIZE(ObBasicSessionInfo::SysVarIncInfo)
{
  int ret = OB_SUCCESS;
  OB_UNIS_ENCODE(all_sys_var_ids_);
  return ret;
}

OB_DEF_DESERIALIZE(ObBasicSessionInfo::SysVarIncInfo)
{
  int ret = OB_SUCCESS;
  OB_UNIS_DECODE(all_sys_var_ids_);
  return ret;
}

OB_DEF_SERIALIZE_SIZE(ObBasicSessionInfo::SysVarIncInfo)
{
  int64_t len = 0;
  OB_UNIS_ADD_LEN(all_sys_var_ids_);
  return len;
}

ObBasicSessionInfo::SysVarIncInfo::SysVarIncInfo() : all_sys_var_ids_()
{}

ObBasicSessionInfo::SysVarIncInfo::~SysVarIncInfo()
{}

int ObBasicSessionInfo::SysVarIncInfo::add_sys_var_id(ObSysVarClassType sys_var_id)
{
  int ret = OB_SUCCESS;
  OZ(add_var_to_array_no_dup(all_sys_var_ids_, sys_var_id), all_sys_var_ids_, sys_var_id);
  return ret;
}

int ObBasicSessionInfo::SysVarIncInfo::get_des_sys_var_id(int64_t idx, ObSysVarClassType& sys_var_id) const
{
  int ret = OB_SUCCESS;
  OV(0 <= idx && idx < all_sys_var_ids_.count());
  OX(sys_var_id = all_sys_var_ids_.at(idx));
  return ret;
}

bool ObBasicSessionInfo::SysVarIncInfo::all_has_sys_var_id(ObSysVarClassType sys_var_id) const
{
  return has_exist_in_array(all_sys_var_ids_, sys_var_id);
}

int64_t ObBasicSessionInfo::SysVarIncInfo::all_count() const
{
  return all_sys_var_ids_.count();
}

const ObBasicSessionInfo::SysVarIds& ObBasicSessionInfo::SysVarIncInfo::get_all_sys_var_ids() const
{
  return all_sys_var_ids_;
}

int ObBasicSessionInfo::SysVarIncInfo::assign(const SysVarIncInfo& other)
{
  int ret = OB_SUCCESS;
  if (this != &other) {
    OZ(all_sys_var_ids_.assign(other.all_sys_var_ids_));
  }
  return ret;
}

int ObBasicSessionInfo::SysVarIncInfo::reset()
{
  int ret = OB_SUCCESS;
  OX(all_sys_var_ids_.reset());
  return ret;
}

int ObBasicSessionInfo::load_all_sys_vars_default()
{
  int ret = OB_SUCCESS;
  OZ(clean_all_sys_vars());
  OZ(load_default_sys_variable(false, false));
  return ret;
}

int ObBasicSessionInfo::clean_all_sys_vars()
{
  int ret = OB_SUCCESS;
  for (int64_t i = 0; OB_SUCC(ret) && i < get_sys_var_count(); ++i) {
    if (OB_NOT_NULL(sys_vars_[i])) {
      OX(sys_vars_[i]->clean_value());
    }
  }
  OX(base_sys_var_alloc_.reset());
  return ret;
}

int ObBasicSessionInfo::load_all_sys_vars(ObSchemaGetterGuard& schema_guard)
{
  int ret = OB_SUCCESS;
  const ObSysVariableSchema* sys_var_schema = NULL;
  OZ(schema_guard.get_sys_variable_schema(effective_tenant_id_, sys_var_schema));
  OV(OB_NOT_NULL(sys_var_schema));
  OZ(load_all_sys_vars(*sys_var_schema));
  return ret;
}

int ObBasicSessionInfo::load_all_sys_vars(const ObSysVariableSchema& sys_var_schema)
{
  int ret = OB_SUCCESS;
  if (sys_var_base_version_ == sys_var_schema.get_schema_version()) {
    OZ(process_session_variable_fast());
  } else {
    OZ(clean_all_sys_vars());
    OX(influence_plan_var_indexs_.reset());
    for (int64_t i = 0; OB_SUCC(ret) && i < get_sys_var_count(); i++) {
      ObSysVarClassType sys_var_id = ObSysVariables::get_sys_var_id(i);
      const ObSysVarSchema* sys_var = NULL;
      OZ(sys_var_schema.get_sysvar_schema(sys_var_id, sys_var));
      OV(OB_NOT_NULL(sys_var));
      OZ(load_sys_variable(sys_var->get_name(),
          sys_var->get_data_type(),
          sys_var->get_value(),
          sys_var->get_min_val(),
          sys_var->get_max_val(),
          sys_var->get_flags(),
          true));
      if (OB_NOT_NULL(sys_vars_[i]) && sys_vars_[i]->is_influence_plan()) {
        OZ(influence_plan_var_indexs_.push_back(i));
      }
    }
    OX(sys_var_base_version_ = sys_var_schema.get_schema_version());
  }
  if (!is_deserialized_) {
    OZ(gen_sys_var_in_pc_str());
  }
  return ret;
}

OB_DEF_SERIALIZE_SIZE(ObBasicSessionInfo)
{
  int64_t len = 0;
  int64_t ser_version = get_ser_version(tenant_id_, effective_tenant_id_);
  ObTimeZoneInfo tmp_tz_info;  // for compatibility with old version
  int ret = OB_SUCCESS;
  bool in_transaction = trans_flags_.is_in_transaction();
  // To be compatible with old version which store sql_mode and compatibility mode in ObSQLModeManager;
  int64_t compatibility_mode_index = 0;
  if (OB_FAIL(compatibility_mode2index(get_compatibility_mode(), compatibility_mode_index))) {
    LOG_WARN("convert compatibility mode to index failed", K(ret));
  }
  LST_DO_CODE(OB_UNIS_ADD_LEN,
      in_transaction,
      trans_desc_,
      consistency_level_,
      compatibility_mode_index,
      tmp_tz_info,
      tenant_id_ | (rpc_tenant_id_ << 32),
      effective_tenant_id_,
      is_changed_to_temp_tenant_,
      user_id_,
      is_master_session() ? sessid_ : master_sessid_,
      capability_.capability_,
      thread_data_.database_name_);

  // calc the length of user variables and system variables.
  ObSEArray<ObSysVarClassType, 128> sys_var_ids;
  ObSEArray<ObString, 32> user_var_names;
  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(calc_need_serialize_vars(sys_var_ids, user_var_names, ser_version))) {
    LOG_WARN("fail to calc need serialize vars", K(ret));
  } else {
    int64_t actual_ser_user_var_count = 0;
    ObSessionVariable user_var_val;
    for (int64_t i = 0; OB_SUCC(ret) && i < user_var_names.count(); ++i) {
      user_var_val.reset();
      const ObString& user_var_name = user_var_names.at(i);
      ret = user_var_val_map_.get_refactored(user_var_name, user_var_val);
      if (OB_SUCCESS != ret && OB_HASH_NOT_EXIST != ret) {
        LOG_WARN("fail to get user var from session user var map",
            K(i),
            K(user_var_name),
            K(user_var_val_map_.size()),
            K(ret));
      } else {
        if (OB_SUCCESS == ret) {
          actual_ser_user_var_count++;
          len += serialization::encoded_length(user_var_name);
          len += serialization::encoded_length(user_var_val.meta_);
          len += serialization::encoded_length(user_var_val.value_);
        }
        ret = OB_SUCCESS;
      }
    }
    len += serialization::encoded_length(actual_ser_user_var_count);
  }
  if (OB_SUCC(ret)) {
    if (ser_version == CUR_SER_VERSION) {
      OB_UNIS_ADD_LEN(sys_var_base_version_);
      OB_UNIS_ADD_LEN(sys_var_inc_info_);
    }
    len += serialization::encoded_length(sys_var_ids.count());
    for (int64_t i = 0; OB_SUCC(ret) && i < sys_var_ids.count(); ++i) {
      int64_t sys_var_idx = -1;
      ObSysVarClassType& type = sys_var_ids.at(i);
      if (OB_FAIL(ObSysVarFactory::calc_sys_var_store_idx(type, sys_var_idx))) {
        LOG_WARN("fail to calc sys var store idx", K(i), K(sys_var_idx), K(type), K(ret));
      } else if (sys_var_idx < 0 || get_sys_var_count() <= sys_var_idx) {
        ret = OB_ERR_UNEXPECTED;
        LOG_ERROR("sys var idx is invalid", K(sys_var_idx), K(get_sys_var_count()), K(ret));
      } else if (OB_ISNULL(sys_vars_[sys_var_idx])) {
        ret = OB_ERR_UNEXPECTED;
        LOG_ERROR("sys var is NULL", K(ret), K(sys_var_idx), K(get_sys_var_count()));
      } else {
        int16_t sys_var_id = static_cast<int16_t>(sys_vars_[sys_var_idx]->get_type());
        len += serialization::encoded_length(sys_var_id);
        len += sys_vars_[sys_var_idx]->get_serialize_size();
      }
    }
  }
  if (OB_SUCC(ret)) {
    bool tx_read_only = get_tx_read_only();
    len += serialization::encoded_length_bool(tx_read_only);
  }
  bool unused_literal_query = false;
  int64_t unused_inner_safe_weak_read_snapshot = safe_weak_read_snapshot_;
  bool need_serial_exec = trans_flags_.need_serial_exec();
O
obdev 已提交
3453
  uint64_t sql_scope_flags = 0;
O
oceanbase-admin 已提交
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

  LST_DO_CODE(OB_UNIS_ADD_LEN,
      sys_vars_cache_.inc_data_,
      trans_consistency_type_,
      safe_weak_read_snapshot_,
      unused_inner_safe_weak_read_snapshot,
      unused_literal_query,
      tz_info_wrap_,
      app_trace_id_,
      proxy_capability_.capability_,
      client_mode_,
      proxy_sessid_,
      nested_count_,
      thread_data_.user_name_,
      next_tx_isolation_,
      read_snapshot_version_,
      check_sys_variable_,
      weak_read_snapshot_source_,
      database_id_,
      thread_data_.user_at_host_name_,
      thread_data_.user_at_client_ip_,
      current_execution_id_,
      total_stmt_tables_,
      cur_stmt_tables_,
      is_foreign_key_cascade_,
      sys_var_in_pc_str_,
      is_foreign_key_check_exist_,
O
obdev 已提交
3481 3482 3483
      need_serial_exec,
      sql_scope_flags,
      stmt_type_);
O
oceanbase-admin 已提交
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
  return len;
}

////////////////////////////////////////////////////////////////
void ObBasicSessionInfo::reset_session_changed_info()
{
  changed_sys_vars_.reset();
  changed_user_vars_.reset();
  is_database_changed_ = false;
  changed_var_pool_.reset();
  feedback_manager_.reset();
}

bool ObBasicSessionInfo::is_already_tracked(
    const ObSysVarClassType& sys_var_id, const ObIArray<ChangedVar>& array) const
{
  bool found = false;
  for (int64_t i = 0; !found && i < array.count(); ++i) {
    if (array.at(i).id_ == sys_var_id) {
      found = true;
      break;
    }
  }
  return found;
}

bool ObBasicSessionInfo::is_already_tracked(const ObString& name, const ObIArray<ObString>& array) const
{
  bool found = false;
  for (int64_t i = 0; !found && i < array.count(); ++i) {
    if (array.at(i) == name) {
      found = true;
      break;
    }
  }
  return found;
}

int ObBasicSessionInfo::add_changed_sys_var(
    const ObSysVarClassType& sys_var_id, const ObObj& old_val, ObIArray<ChangedVar>& array)
{
  int ret = OB_SUCCESS;
  ObObj val;
  if (SYS_VAR_INVALID == sys_var_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid input value", K(sys_var_id), K(old_val), K(ret));
  } else if (OB_FAIL(deep_copy_obj(changed_var_pool_, old_val, val))) {
    LOG_WARN("failed to deep copy system var old value", K(ret), K(old_val));
  } else if (OB_FAIL(array.push_back(ChangedVar(sys_var_id, val)))) {
    LOG_WARN("fail to push back", K(sys_var_id), K(old_val), K(ret));
  }
  return ret;
}

int ObBasicSessionInfo::add_changed_user_var(const ObString& name, common::ObIArray<common::ObString>& array)
{
  return array.push_back(name);
}

int ObBasicSessionInfo::track_sys_var(const ObSysVarClassType& sys_var_id, const ObObj& old_val)
{
  int ret = OB_SUCCESS;
  if (SYS_VAR_INVALID == sys_var_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid input value", K(sys_var_id), K(ret));
  } else if (!is_already_tracked(sys_var_id, changed_sys_vars_)) {
    if (OB_FAIL(add_changed_sys_var(sys_var_id, old_val, changed_sys_vars_))) {
      LOG_WARN("fail to add changed system var", K(sys_var_id), K(old_val), K(ret));
    } else {
      LOG_DEBUG("add changed var success", K(sys_var_id), K(old_val), K(changed_sys_vars_));
    }
  }
  return ret;
}

int ObBasicSessionInfo::track_user_var(const common::ObString& user_var)
{
  int ret = OB_SUCCESS;
  if (user_var.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid input value", K(user_var), K(ret));
  } else if (!is_already_tracked(user_var, changed_user_vars_)) {
    ObString name;
    if (OB_FAIL(ob_write_string(changed_var_pool_, user_var, name))) {
      LOG_WARN("fail to write string", K(user_var), K(ret));
    } else if (OB_FAIL(add_changed_user_var(name, changed_user_vars_))) {
      LOG_WARN("fail to add changed user var", K(name), K(ret));
    }
  }
  return ret;
}

int ObBasicSessionInfo::is_sys_var_actully_changed(
    const ObSysVarClassType& sys_var_id, const ObObj& old_val, ObObj& new_val, bool& changed)
{
  int ret = OB_SUCCESS;
  changed = true;
  OZ(get_sys_variable(sys_var_id, new_val));
  if (OB_SUCC(ret)) {
    switch (sys_var_id) {
      case SYS_VAR_AUTO_INCREMENT_INCREMENT:
      case SYS_VAR_AUTO_INCREMENT_OFFSET:
      case SYS_VAR_LAST_INSERT_ID:
      case SYS_VAR_TX_READ_ONLY:
      case SYS_VAR_OB_ENABLE_PLAN_CACHE:
      case SYS_VAR_OB_ENABLE_SQL_AUDIT:
      case SYS_VAR_AUTOCOMMIT:
      case SYS_VAR_OB_ENABLE_TRACE_LOG:
      case SYS_VAR_OB_ORG_CLUSTER_ID:
      case SYS_VAR_OB_QUERY_TIMEOUT:
      case SYS_VAR_OB_TRX_TIMEOUT:
      case SYS_VAR_OB_TRX_IDLE_TIMEOUT:
      case SYS_VAR_COLLATION_CONNECTION:
      case SYS_VAR_OB_PL_BLOCK_TIMEOUT:
B
bf0 已提交
3598 3599
      case SYS_VAR_OB_COMPATIBILITY_MODE:
      case SYS_VAR__OB_PROXY_SESSION_TEMPORARY_TABLE_USED: {
O
oceanbase-admin 已提交
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 3627 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
        changed = old_val.get_meta() == new_val.get_meta() ? old_val != new_val : true;
      } break;
      default: {
        break;
      }
    }
  }
  LOG_DEBUG("is_sys_var_actully_changed", K(sys_var_id), K(changed), K(old_val), K(new_val));
  return ret;
}

int ObBasicSessionInfo::set_partition_hit(const bool is_hit)
{
  int ret = OB_SUCCESS;
  ObObj obj;
  obj.set_bool(is_hit);
  if (OB_FAIL(update_sys_variable(SYS_VAR_OB_PROXY_PARTITION_HIT, obj))) {
    LOG_WARN("fail to update_system_variable", K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::set_proxy_user_privilege(const int64_t user_priv_set)
{
  int ret = OB_SUCCESS;
  ObObj obj;
  obj.set_int(user_priv_set);
  if (OB_FAIL(update_sys_variable(SYS_VAR_OB_PROXY_USER_PRIVILEGE, obj))) {
    LOG_WARN("fail to update_system_variable", K(SYS_VAR_OB_PROXY_USER_PRIVILEGE), K(user_priv_set), K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::update_last_trace_id(const ObCurTraceId::TraceId& trace_id)
{
  int ret = OB_SUCCESS;
  const int64_t TEMP_STR_BUF_LEN = 128;
  // 128 is enough to tracd_id, or will be truncated
  char tmp_str_buf[TEMP_STR_BUF_LEN];  // no need init
  int64_t pos = trace_id.to_string(tmp_str_buf, TEMP_STR_BUF_LEN);
  ObString trace_id_str(static_cast<ObString::obstr_size_t>(pos), tmp_str_buf);

  ObObj obj;
  obj.set_string(ObVarcharType, trace_id_str);
  if (OB_FAIL(update_sys_variable(SYS_VAR_OB_STATEMENT_TRACE_ID, obj))) {
    LOG_WARN("fail to update_system_variable", K(SYS_VAR_OB_STATEMENT_TRACE_ID), K(trace_id_str), K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::set_proxy_capability(const uint64_t cap)
{
  int ret = OB_SUCCESS;
  ObObj obj;
  obj.set_int(cap);
  if (OB_FAIL(update_sys_variable(SYS_VAR_OB_CAPABILITY_FLAG, obj))) {
    LOG_WARN("fail to update_system_variable", K(SYS_VAR_OB_CAPABILITY_FLAG), K(cap), K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::set_trans_specified(const bool is_spec)
{
  int ret = OB_SUCCESS;
  ObObj obj;
  obj.set_bool(is_spec);
  if (OB_FAIL(update_sys_variable(SYS_VAR_OB_PROXY_SET_TRX_EXECUTED, obj))) {
    LOG_WARN("fail to update_system_variable", K(ret));
  } else {
  }
  return ret;
}

B
bf0 已提交
3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687
int ObBasicSessionInfo::set_session_temp_table_used(const bool is_used)
{
  int ret = OB_SUCCESS;
  ObObj obj;
  obj.set_int(is_used);
  if (OB_FAIL(update_sys_variable(SYS_VAR__OB_PROXY_SESSION_TEMPORARY_TABLE_USED, obj))) {
    LOG_WARN("fail to update_system_variable", K(ret));
  }
  return ret;
}

O
oceanbase-admin 已提交
3688 3689 3690 3691 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
int ObBasicSessionInfo::set_safe_weak_read_snapshot_variable(const int64_t safe_snapshot)
{
  int ret = OB_SUCCESS;
  ObObj obj;
  obj.set_int(safe_snapshot);
  if (OB_FAIL(update_sys_variable(SYS_VAR_OB_SAFE_WEAK_READ_SNAPSHOT, obj))) {
    LOG_WARN("fail to update_system_variable", K(SYS_VAR_OB_SAFE_WEAK_READ_SNAPSHOT), K(safe_snapshot), K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::set_init_connect(const ObString& init_sql)
{
  int ret = OB_SUCCESS;
  ObObj obj;
  obj.set_string(ObVarcharType, init_sql);
  if (OB_FAIL(update_sys_variable(SYS_VAR_INIT_CONNECT, obj))) {
    LOG_WARN("fail to update_system_variable", K(ret));
  } else {
  }
  return ret;
}

int ObBasicSessionInfo::save_trans_status()
{
  int ret = OB_SUCCESS;
  switch (trans_spec_status_) {
    case TRANS_SPEC_SET:
      ret = set_trans_specified(true);
      break;
    case TRANS_SPEC_COMMIT:
      ret = set_trans_specified(false);
      trans_spec_status_ = TRANS_SPEC_NOT_SET;
      break;
    case TRANS_SPEC_NOT_SET:
      // do nothing
      break;
    default:
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("Unknown trans specified status", K(trans_spec_status_), K(ret));
      break;
  }
  return ret;
}

int ObBasicSessionInfo::get_character_set_client(ObCharsetType& character_set_client) const
{
  return get_charset_sys_var(SYS_VAR_CHARACTER_SET_CLIENT, character_set_client);
}

int ObBasicSessionInfo::get_character_set_connection(ObCharsetType& character_set_connection) const
{
  return get_charset_sys_var(SYS_VAR_CHARACTER_SET_CONNECTION, character_set_connection);
}

int ObBasicSessionInfo::get_character_set_database(ObCharsetType& character_set_database) const
{
  return get_charset_sys_var(SYS_VAR_CHARACTER_SET_DATABASE, character_set_database);
}

int ObBasicSessionInfo::get_character_set_results(ObCharsetType& character_set_results) const
{
  if (CHARSET_INVALID == (character_set_results = sys_vars_cache_.get_character_set_results())) {
    get_charset_sys_var(SYS_VAR_CHARACTER_SET_RESULTS, character_set_results);
  }
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_character_set_server(ObCharsetType& character_set_server) const
{
  return get_charset_sys_var(SYS_VAR_CHARACTER_SET_SERVER, character_set_server);
}

int ObBasicSessionInfo::get_character_set_system(ObCharsetType& character_set_system) const
{
  return get_charset_sys_var(SYS_VAR_CHARACTER_SET_SYSTEM, character_set_system);
}

int ObBasicSessionInfo::get_character_set_filesystem(ObCharsetType& character_set_filesystem) const
{
  return get_charset_sys_var(SYS_VAR_CHARACTER_SET_FILESYSTEM, character_set_filesystem);
}

int ObBasicSessionInfo::get_collation_database(ObCollationType& collation_database) const
{
  return get_collation_sys_var(SYS_VAR_COLLATION_DATABASE, collation_database);
}

int ObBasicSessionInfo::get_collation_server(ObCollationType& collation_server) const
{
  return get_collation_sys_var(SYS_VAR_COLLATION_SERVER, collation_server);
}

int ObBasicSessionInfo::get_foreign_key_checks(int64_t& foreign_key_checks) const
{
  foreign_key_checks = sys_vars_cache_.get_foreign_key_checks();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_capture_plan_baseline(bool& v) const
{
  v = sys_vars_cache_.get_optimizer_capture_sql_plan_baselines();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_use_plan_baseline(bool& v) const
{
  v = sys_vars_cache_.get_optimizer_use_sql_plan_baselines();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_adaptive_cursor_sharing(bool& acs) const
{
  acs = sys_vars_cache_.get__optimizer_adaptive_cursor_sharing();
  return OB_SUCCESS;
}

int ObBasicSessionInfo::get_nlj_batching_enabled(bool& v) const
{
  return get_bool_sys_var(SYS_VAR__NLJ_BATCHING_ENABLED, v);
}

int ObBasicSessionInfo::get_auto_increment_cache_size(int64_t& auto_increment_cache_size)
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(get_sys_variable(SYS_VAR_AUTO_INCREMENT_CACHE_SIZE, auto_increment_cache_size))) {
    LOG_WARN("fail to get variables", K(ret));
  }
  return ret;
}

int ObBasicSessionInfo::get_enable_parallel_dml(bool& v) const
{
  return get_bool_sys_var(SYS_VAR__ENABLE_PARALLEL_DML, v);
}

int ObBasicSessionInfo::get_enable_parallel_query(bool& v) const
{
  return get_bool_sys_var(SYS_VAR__ENABLE_PARALLEL_QUERY, v);
}

int ObBasicSessionInfo::get_force_parallel_query_dop(uint64_t& v) const
{
  int ret = OB_SUCCESS;
  if (exec_min_cluster_version_ >= CLUSTER_VERSION_3100) {
    ret = get_uint64_sys_var(SYS_VAR__FORCE_PARALLEL_QUERY_DOP, v);
  } else {
    v = 1;
  }
  return ret;
}

int ObBasicSessionInfo::get_force_parallel_dml_dop(uint64_t& v) const
{
  int ret = OB_SUCCESS;
  if (exec_min_cluster_version_ >= CLUSTER_VERSION_3100) {
    ret = get_uint64_sys_var(SYS_VAR__FORCE_PARALLEL_DML_DOP, v);
  } else {
    v = 1;
  }
  return ret;
}

int ObBasicSessionInfo::get_secure_file_priv(ObString& v) const
{
  return get_string_sys_var(SYS_VAR_SECURE_FILE_PRIV, v);
}

int ObBasicSessionInfo::get_sql_safe_updates(bool& v) const
{
  return get_bool_sys_var(SYS_VAR_SQL_SAFE_UPDATES, v);
}

B
bf0 已提交
3862 3863 3864 3865
int ObBasicSessionInfo::get_session_temp_table_used(bool& is_used) const
{
  return get_bool_sys_var(SYS_VAR__OB_PROXY_SESSION_TEMPORARY_TABLE_USED, is_used);
}
O
oceanbase-admin 已提交
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 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968
void ObBasicSessionInfo::reset_tx_variable()
{
  // this function will be called in end_trans phase, and must be idempotent.
  // trans_desc will be reset in start_trans phase, not here, because end_trans may use it.
  reset_tx_isolation();
  reset_tx_read_only();
  reset_trans_flags();
  clear_trans_consistency_type();
  clear_app_trace_id();
}

int32_t ObBasicSessionInfo::get_tx_isolation() const
{
  int32_t isolation = -1;
  const int32_t TX_NOT_SET = -1;
  if (next_tx_isolation_ != TX_NOT_SET) {
    isolation = next_tx_isolation_;
  } else {
    isolation = sys_vars_cache_.get_tx_isolation();
  }
  return isolation;
}

bool ObBasicSessionInfo::is_isolation_serializable() const
{
  return ObTransIsolation::SERIALIZABLE == get_tx_isolation();
}

void ObBasicSessionInfo::set_tx_isolation(int32_t isolation)
{
  next_tx_isolation_ = isolation;
  trans_spec_status_ = TRANS_SPEC_SET;
}

void ObBasicSessionInfo::reset_tx_isolation()
{
  next_tx_isolation_ = transaction::ObTransIsolation::UNKNOWN;
  if (TRANS_SPEC_SET == trans_spec_status_) {
    trans_spec_status_ = TRANS_SPEC_COMMIT;
  }
}

void ObBasicSessionInfo::set_tx_read_only(bool read_only)
{
  next_tx_read_only_ = read_only;
  trans_spec_status_ = TRANS_SPEC_SET;
}

void ObBasicSessionInfo::reset_tx_read_only()
{
  next_tx_read_only_ = -1;
  if (TRANS_SPEC_SET == trans_spec_status_) {
    trans_spec_status_ = TRANS_SPEC_COMMIT;
  }
}

int ObBasicSessionInfo::check_tx_read_only_privilege(const ObSqlTraits& sql_traits)
{
  int ret = OB_SUCCESS;
  if (sql_traits.is_cause_implicit_commit_) {
    reset_tx_read_only();
  } else {
  }
  if (get_tx_read_only() && !sql_traits.is_readonly_stmt_) {
    ret = OB_ERR_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION;

  } else {
  }
  return ret;
}

int ObBasicSessionInfo::reset_tx_variable_if_remote_trans(const ObPhyPlanType& type)
{
  int ret = OB_SUCCESS;
  if (ObSqlTransUtil::is_remote_trans(get_local_autocommit(), trans_flags_.is_in_transaction(), type)) {
    reset_tx_variable();
  } else {
    // nothing to do
  }
  return ret;
}

bool ObBasicSessionInfo::get_tx_read_only() const
{
  bool tx_read_only = false;
  const int32_t TX_NOT_SET = -1;
  if (next_tx_read_only_ != TX_NOT_SET) {
    tx_read_only = next_tx_read_only_;
  } else {
    tx_read_only = sys_vars_cache_.get_tx_read_only();
  }
  return tx_read_only;
}

int ObBasicSessionInfo::store_query_string(const ObString& stmt)
{
  LockGuard lock_guard(thread_data_mutex_);
  return store_query_string_(stmt);
}

int ObBasicSessionInfo::store_query_string_(const ObString& stmt)
{
  int ret = OB_SUCCESS;
3969
  int64_t truncated_len = std::min(MAX_QUERY_STRING_LEN - 1, static_cast<int64_t>(stmt.length()));
O
oceanbase-admin 已提交
3970 3971 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 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 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
  if (truncated_len < 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid str length", K(ret), K(truncated_len));
  } else if (thread_data_.cur_query_buf_len_ - 1 < truncated_len) {
    if (thread_data_.cur_query_ != nullptr) {
      ob_free(thread_data_.cur_query_);
      thread_data_.cur_query_ = NULL;
      thread_data_.cur_query_buf_len_ = 0;
    }
    int64_t len = MAX(MIN_CUR_QUERY_LEN, truncated_len + 1);
    char* buf = reinterpret_cast<char*>(ob_malloc(len, ObModIds::OB_SQL_SESSION_QUERY_SQL));
    if (OB_ISNULL(buf)) {
      ret = OB_ALLOCATE_MEMORY_FAILED;
      LOG_WARN("alloc memory failed", K(ret));
    } else {
      thread_data_.cur_query_ = buf;
      thread_data_.cur_query_buf_len_ = len;
    }
  }
  if (OB_SUCC(ret)) {
    MEMCPY(thread_data_.cur_query_, stmt.ptr(), truncated_len);
    thread_data_.cur_query_[truncated_len] = '\0';
    thread_data_.cur_query_len_ = truncated_len;
  }
  return ret;
}

void ObBasicSessionInfo::reset_query_string()
{
  thread_data_.cur_query_[0] = '\0';
  thread_data_.cur_query_len_ = 0;
}

int ObBasicSessionInfo::update_session_timeout()
{
  int ret = OB_SUCCESS;
  ObObj val;
  if (thread_data_.is_interactive_) {
    val.set_int(thread_data_.interactive_timeout_);
    if (OB_FAIL(update_sys_variable(SYS_VAR_WAIT_TIMEOUT, val))) {
      LOG_WARN("failed to update session_timeout", K(ret));
    }
  }
  return ret;
}

const char* ObBasicSessionInfo::get_session_state_str() const
{
  const char* str_ret = NULL;
  switch (thread_data_.state_) {
    case SESSION_INIT:
      str_ret = "INIT";
      break;
    case SESSION_SLEEP:
      str_ret = "SLEEP";
      break;
    case QUERY_ACTIVE:
      str_ret = "ACTIVE";
      break;
    case QUERY_KILLED:
      str_ret = "QUERY_KILLED";
      break;
    case SESSION_KILLED:
      str_ret = "SESSION_KILLED";
      break;
    default:
      str_ret = "INVALID SESSION STATE";
      break;
  }
  return str_ret;
}

int ObBasicSessionInfo::is_timeout(bool& is_timeout)
{
  int ret = OB_SUCCESS;
  int64_t timeout = 0;
  is_timeout = false;
  if (SESSION_SLEEP == thread_data_.state_) {
    timeout = thread_data_.is_interactive_ ? thread_data_.interactive_timeout_ : thread_data_.wait_timeout_;
    int64_t cur_time = ::oceanbase::common::ObTimeUtility::current_time();
    if (OB_UNLIKELY(timeout < 0)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("session timeout setting is invalid", K(ret), K(timeout));
    } else if (OB_UNLIKELY(timeout > INT64_MAX / 1000 / 1000)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("session timeout setting is too larger, skip check timeout", K(ret), K(timeout));
    } else if (0 == timeout) {
      // no timeout check for timeout == 0
    } else if (SESSION_SLEEP == thread_data_.state_ &&
               thread_data_.last_active_time_ + timeout * 1000 * 1000 < cur_time) {
      const ObString& user_name = get_user_name();
      char addr_buf[common::OB_IP_PORT_STR_BUFF];
      if (OB_FAIL(get_peer_addr().ip_port_to_string(addr_buf, OB_IP_PORT_STR_BUFF))) {
        LOG_WARN("failed to get peer addr string", K(ret), K(get_peer_addr()), K(addr_buf));
      } else {
        _LOG_INFO("sessionkey %u, proxy_sessid %lu: %.*s from %s timeout: last active time=%s, cur=%s, timeout=%lds",
            sessid_,
            proxy_sessid_,
            user_name.length(),
            user_name.ptr(),
            addr_buf,
            time2str(thread_data_.last_active_time_),
            time2str(cur_time),
            timeout);
      }
      is_timeout = true;
    }
  }
  return ret;
}

int ObBasicSessionInfo::is_trx_idle_timeout(bool& timeout)
{
  int ret = OB_SUCCESS;
  int64_t cur_time = ::oceanbase::common::ObTimeUtility::current_time();
  LOG_DEBUG("check trx_idle_timeout : ",
      K_(sessid),
      K(trans_flags_.is_in_transaction()),
      K(curr_trans_last_stmt_end_time_),
      K(cur_time),
      K(get_query_start_time()));
  if (get_query_start_time() < curr_trans_last_stmt_end_time_) {
    int64_t ob_trx_idle_timeout =
        sys_vars_cache_.get_ob_trx_idle_timeout() > 0 ? sys_vars_cache_.get_ob_trx_idle_timeout() : 120 * 1000 * 1000;
    if (trans_flags_.is_in_transaction() && curr_trans_last_stmt_end_time_ > 0 &&
        curr_trans_last_stmt_end_time_ + ob_trx_idle_timeout < cur_time) {
      LOG_WARN("ob_trx_idle_timeout happen", K_(sessid), K_(curr_trans_last_stmt_end_time), K(ob_trx_idle_timeout));
      curr_trans_last_stmt_end_time_ = 0;  // flip
      timeout = true;
    }
  }
  return ret;
}

int ObBasicSessionInfo::get_pc_mem_conf(ObPCMemPctConf& pc_mem_conf)
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(get_sys_variable(SYS_VAR_OB_PLAN_CACHE_PERCENTAGE, pc_mem_conf.limit_pct_))) {
    LOG_WARN("fail to get plan cache system variables", K(pc_mem_conf.limit_pct_), K(ret));
  } else if (OB_FAIL(get_sys_variable(SYS_VAR_OB_PLAN_CACHE_EVICT_HIGH_PERCENTAGE, pc_mem_conf.high_pct_))) {
    LOG_WARN("fail to get plan cache system variables", K(pc_mem_conf.high_pct_), K(ret));
  } else if (OB_FAIL(get_sys_variable(SYS_VAR_OB_PLAN_CACHE_EVICT_LOW_PERCENTAGE, pc_mem_conf.low_pct_))) {
    LOG_WARN("fail to get plan cache system variables", K(pc_mem_conf.low_pct_), K(ret));
  }
  return ret;
}

int ObBasicSessionInfo::get_sql_audit_mem_conf(int64_t& mem_pct)
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(get_sys_variable(SYS_VAR_OB_SQL_AUDIT_PERCENTAGE, mem_pct))) {
    LOG_WARN("failed to get memory percentage for sql audit", K(ret));
  }
  return ret;
}

void ObBasicSessionInfo::set_conn(easy_connection_t* conn)
{
  LockGuard lock_guard(thread_data_mutex_);
  thread_data_.conn_ = conn;
}

void ObBasicSessionInfo::set_session_data(ObSQLSessionState state, obmysql::ObMySQLCmd mysql_cmd, int64_t thread_id)
{
  LockGuard lock_guard(thread_data_mutex_);
  set_session_state_(state);
  thread_data_.mysql_cmd_ = mysql_cmd;
  thread_id_ = thread_id;
}

int ObBasicSessionInfo::set_session_state(ObSQLSessionState state)
{
  LockGuard lock_guard(thread_data_mutex_);
  return set_session_state_(state);
}

int ObBasicSessionInfo::set_session_state_(ObSQLSessionState state)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(SESSION_KILLED == thread_data_.state_)) {
    ret = OB_ERR_SESSION_INTERRUPTED;
    LOG_WARN("session is killed", K(ret), K(sessid_), K(proxy_sessid_), K(version_), K(state));
  } else if (OB_UNLIKELY(SESS_NOT_IN_RETRY != thread_data_.is_in_retry_ && QUERY_KILLED == thread_data_.state_ &&
                         SESSION_KILLED != state)) {

    ret = OB_ERR_QUERY_INTERRUPTED;
    LOG_WARN("query is killed", K(ret), K(sessid_), K(proxy_sessid_), K(version_), K(state));
  } else {
    thread_data_.state_ = state;
    thread_data_.cur_state_start_time_ = ::oceanbase::common::ObClockGenerator::getClock();
  }
  return ret;
}

int ObBasicSessionInfo::set_session_active(
    const ObString& sql, const int64_t query_receive_ts, const int64_t last_active_time_ts, obmysql::ObMySQLCmd cmd)
{
  int ret = OB_SUCCESS;

  LockGuard lock_guard(thread_data_mutex_);
  if (OB_FAIL(store_query_string_(sql))) {
    LOG_WARN("store query string fail", K(ret));
  } else if (OB_FAIL(set_session_state_(QUERY_ACTIVE))) {
    LOG_WARN("fail to set session state", K(ret));
  } else {
    thread_data_.cur_query_start_time_ = query_receive_ts;
    thread_data_.mysql_cmd_ = cmd;
    thread_data_.last_active_time_ = last_active_time_ts;
  }
  return ret;
}

int ObBasicSessionInfo::base_save_session(BaseSavedValue& saved_value, bool skip_cur_stmt_tables)
{
  int ret = OB_SUCCESS;
  // OZ (saved_value.trans_desc_.stmt_deep_copy(trans_desc_));
  // OX (saved_value.in_transaction_ = trans_flags_.is_in_transaction());
  OX(saved_value.cur_phy_plan_ = cur_phy_plan_);
  OX(cur_phy_plan_ = NULL);
  int len = MIN(thread_data_.cur_query_len_, static_cast<int64_t>(sizeof(saved_value.cur_query_) - 1));
  if (thread_data_.cur_query_ != nullptr) {
    OX(MEMCPY(&saved_value.cur_query_, thread_data_.cur_query_, len));
    OX(thread_data_.cur_query_[0] = 0);
  }
  OX(saved_value.cur_query_len_ = len);
  OX(thread_data_.cur_query_len_ = 0);
  // OX (saved_value.cur_query_start_time_ = thread_data_.cur_query_start_time_);
  // OX (thread_data_.cur_query_start_time_ = 0);
  OZ(saved_value.total_stmt_tables_.assign(total_stmt_tables_));
  if (!skip_cur_stmt_tables) {
    OZ(append_array_no_dup(total_stmt_tables_, cur_stmt_tables_));
  }
  OZ(saved_value.cur_stmt_tables_.assign(cur_stmt_tables_));
  OX(cur_stmt_tables_.reset());
  OX(sys_vars_cache_.get_autocommit_info(
      saved_value.base_autocommit_, saved_value.inc_autocommit_, saved_value.is_inc_autocommit_));
  OX(sys_vars_cache_.set_autocommit_info(false, false, false));
  OX(saved_value.is_foreign_key_cascade_ = is_foreign_key_cascade_);
  OX(is_foreign_key_cascade_ = false);
  OX(saved_value.is_foreign_key_check_exist_ = is_foreign_key_check_exist_);
  OX(is_foreign_key_check_exist_ = false);
  OX(saved_value.need_serial_exec_ = trans_flags_.need_serial_exec());
  if (OB_NOT_NULL(saved_value.cur_phy_plan_) && saved_value.cur_phy_plan_->get_need_serial_exec()) {
    OX(trans_flags_.set_need_serial_exec(true));
  }
  return ret;
}

int ObBasicSessionInfo::stmt_save_session(StmtSavedValue& saved_value, bool skip_cur_stmt_tables)
{
  int ret = OB_SUCCESS;
  OZ(base_save_session(saved_value, skip_cur_stmt_tables));
  OZ(saved_value.trans_desc_.stmt_deep_copy(trans_desc_));
  OX(saved_value.in_transaction_ = trans_flags_.is_in_transaction());
  OX(saved_value.cur_query_start_time_ = thread_data_.cur_query_start_time_);
  OX(thread_data_.cur_query_start_time_ = 0);
  OZ(saved_value.trans_result_.assign(trans_result_));
  OX(trans_result_.reset());
  return ret;
}

int ObBasicSessionInfo::save_basic_session(StmtSavedValue& saved_value, bool skip_cur_stmt_tables)
{
  return stmt_save_session(saved_value, skip_cur_stmt_tables);
}

int ObBasicSessionInfo::begin_nested_session(StmtSavedValue& saved_value, bool skip_cur_stmt_tables)
{
  int ret = OB_SUCCESS;
  OZ(SMART_CALL(save_basic_session(saved_value, skip_cur_stmt_tables)));
  OX(trans_flags_.set_is_in_transaction(true));
  return ret;
}

int ObBasicSessionInfo::base_restore_session(BaseSavedValue& saved_value)
{
  int ret = OB_SUCCESS;
  OX(trans_flags_.set_need_serial_exec(saved_value.need_serial_exec_));
  OX(is_foreign_key_check_exist_ = saved_value.is_foreign_key_check_exist_);
  OX(is_foreign_key_cascade_ = saved_value.is_foreign_key_cascade_);
  OX(sys_vars_cache_.set_autocommit_info(
      saved_value.base_autocommit_, saved_value.inc_autocommit_, saved_value.is_inc_autocommit_));
  OZ(cur_stmt_tables_.assign(saved_value.cur_stmt_tables_));
  OZ(total_stmt_tables_.assign(saved_value.total_stmt_tables_));
  // OX (thread_data_.cur_query_start_time_ = saved_value.cur_query_start_time_);
  int64_t len = MIN(saved_value.cur_query_len_, thread_data_.cur_query_buf_len_ - 1);
  OX(thread_data_.cur_query_len_ = len);
  if (thread_data_.cur_query_ != nullptr) {
    OX(MEMCPY(thread_data_.cur_query_, saved_value.cur_query_, len));
    thread_data_.cur_query_[len] = '\0';
  }
  OX(cur_phy_plan_ = saved_value.cur_phy_plan_);
  return ret;
}

int ObBasicSessionInfo::stmt_restore_session(StmtSavedValue& saved_value)
{
  int ret = OB_SUCCESS;
  OZ(trans_result_.merge_result(saved_value.trans_result_));
  OX(thread_data_.cur_query_start_time_ = saved_value.cur_query_start_time_);
  OX(trans_flags_.set_is_in_transaction(saved_value.in_transaction_));
  OZ(trans_desc_.stmt_deep_copy(saved_value.trans_desc_));
  OZ(base_restore_session(saved_value));
  return ret;
}

int ObBasicSessionInfo::restore_basic_session(StmtSavedValue& saved_value)
{
  return stmt_restore_session(saved_value);
}

int ObBasicSessionInfo::end_nested_session(StmtSavedValue& saved_value)
{
  int ret = OB_SUCCESS;
  int64_t max_sql_no = MAX(trans_desc_.get_max_sql_no(), trans_result_.get_max_sql_no());
  OZ(SMART_CALL(restore_basic_session(saved_value)));
  OX(saved_value.reset());
  OX(trans_desc_.restore_max_sql_no(max_sql_no));
  OX(trans_result_.set_max_sql_no(max_sql_no));
  return ret;
}

int ObBasicSessionInfo::trans_save_session(TransSavedValue& saved_value)
{
  int ret = OB_SUCCESS;
  OZ(base_save_session(saved_value, false));
  OZ(saved_value.trans_desc_.trans_deep_copy(trans_desc_));
  OX(trans_desc_.reset());
  OZ(trans_desc_.init(this));
  OX(saved_value.trans_flags_ = trans_flags_);
  OX(trans_flags_.reset());
  OZ(saved_value.trans_result_.assign(trans_result_));
  OX(trans_result_.reset());
  OX(saved_value.nested_count_ = nested_count_);
  OX(nested_count_ = -1);
  return ret;
}

int ObBasicSessionInfo::trans_restore_session(TransSavedValue& saved_value)
{
  int ret = OB_SUCCESS;
  OX(nested_count_ = saved_value.nested_count_);
  OZ(trans_result_.assign(saved_value.trans_result_));
  OX(trans_flags_ = saved_value.trans_flags_);
  OZ(trans_desc_.trans_deep_copy(saved_value.trans_desc_));
  OZ(base_restore_session(saved_value));
  return ret;
}

int ObBasicSessionInfo::begin_autonomous_session(TransSavedValue& saved_value)
{
  return trans_save_session(saved_value);
}

int ObBasicSessionInfo::end_autonomous_session(TransSavedValue& saved_value)
{
  return trans_restore_session(saved_value);
}

int ObBasicSessionInfo::set_start_stmt()
{
  int ret = OB_SUCCESS;
  OV(nested_count_ == -1, OB_ERR_UNEXPECTED, nested_count_);
  OX(nested_count_ = 0);
  return ret;
}

int ObBasicSessionInfo::set_end_stmt()
{
  int ret = OB_SUCCESS;
  OV(nested_count_ == 0, OB_ERR_UNEXPECTED, nested_count_);
  OX(nested_count_ = -1);
  return ret;
}

int ObBasicSessionInfo::set_cur_stmt_tables(const ObIArray<ObPartitionKey>& stmt_partitions, stmt::StmtType stmt_type)
{
  int ret = OB_SUCCESS;
  if (!is_nested_session()) {
    // cur stmt is top stmt.
    total_stmt_tables_.reset();
    cur_stmt_tables_.reset();
  }
  if (!is_foreign_key_cascade() && !is_foreign_key_check_exist()) {
    TableStmtType table_stmt_type;
    int64_t idx = -1;
    table_stmt_type.stmt_type_ = stmt_type;
    for (int i = 0; OB_SUCC(ret) && i < stmt_partitions.count(); i++) {
      OX(table_stmt_type.table_id_ = stmt_partitions.at(i).table_id_);
      if (has_exist_in_array(total_stmt_tables_, table_stmt_type, &idx)) {
        OV(0 <= idx && idx < total_stmt_tables_.count());
        OV(table_stmt_type.is_select_stmt() && total_stmt_tables_.at(idx).is_select_stmt(),
            OB_ERR_MUTATING_TABLE_OPERATION,
            total_stmt_tables_,
            table_stmt_type,
            is_foreign_key_cascade_,
            is_foreign_key_check_exist_);
      }
      OZ(add_var_to_array_no_dup(cur_stmt_tables_, table_stmt_type), cur_stmt_tables_, table_stmt_type);
    }
  }
  return ret;
}

int ObBasicSessionInfo::set_time_zone(
    const ObString& str_val, const bool is_oralce_mode, const bool check_timezone_valid)
{
  int ret = OB_SUCCESS;
  int32_t offset = 0;
  int ret_more = OB_SUCCESS;
  if (OB_FAIL(ObTimeConverter::str_to_offset(str_val, offset, ret_more, is_oralce_mode, check_timezone_valid))) {
    if (ret != OB_ERR_UNKNOWN_TIME_ZONE) {
      LOG_WARN("fail to convert time zone", K(str_val), K(ret));
    }
  } else {
    tz_info_wrap_.set_tz_info_offset(offset);
  }

  if (OB_ERR_UNKNOWN_TIME_ZONE == ret) {
    ObTZMapWrap tz_map_wrap;
    ObTimeZoneInfoManager* tz_info_mgr = NULL;
    if (OB_FAIL(OTTZ_MGR.get_tenant_timezone(tenant_id_, tz_map_wrap, tz_info_mgr))) {
      LOG_WARN("get tenant timezone with lock failed", K(ret));
    } else if (OB_ISNULL(tz_info_mgr)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("tenant timezone mgr is null", K(tz_info_mgr));
    } else {
      const int64_t orig_version = tz_info_wrap_.get_cur_version();
      tz_info_wrap_.set_cur_version(tz_info_mgr->get_version());
      ObCollationType coll_type = ObCharset::get_default_collation(ObCharset::get_default_charset());
      int32_t no_sp_len =
          static_cast<int32_t>(ObCharset::strlen_byte_no_sp(coll_type, str_val.ptr(), str_val.length()));
      ObString val_no_sp(no_sp_len, str_val.ptr());
      if (is_oralce_mode) {
        val_no_sp = val_no_sp.trim();
      }
      int64_t start_service_time = GCTX.start_service_time_;
      if (OB_FAIL(tz_info_mgr->find_time_zone_info(val_no_sp, tz_info_wrap_.get_tz_info_pos()))) {
        LOG_WARN("fail to find time zone", K(str_val), K(val_no_sp), K(ret));
        tz_info_wrap_.set_cur_version(orig_version);
      } else {
        tz_info_wrap_.set_tz_info_position();
      }

      if (OB_ERR_UNKNOWN_TIME_ZONE == ret) {
        if (0 == start_service_time) {
          LOG_INFO(
              "ignore unknow time zone, perhaps in remote/distribute task processer when server start_time is zero",
              K(str_val));
S
st0 已提交
4419 4420 4421 4422 4423 4424
          offset = 0;
          if (OB_FAIL(ObTimeConverter::str_to_offset(ObString("+8:00"), offset, ret_more,
                                                    is_oralce_mode, check_timezone_valid))) {
            if (ret != OB_ERR_UNKNOWN_TIME_ZONE) {
              LOG_WARN("fail to convert time zone", K(str_val), K(ret));
            }
O
oceanbase-admin 已提交
4425
          } else {
S
st0 已提交
4426
            tz_info_wrap_.set_tz_info_offset(offset);
O
oceanbase-admin 已提交
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
          }
        }
      }
    }
  }
  return ret;
}

int ObBasicSessionInfo::update_timezone_info()
{
  int ret = OB_SUCCESS;
  ObTZMapWrap tz_map_wrap;
  ObTimeZoneInfoManager* tz_info_mgr = NULL;
  if (OB_FAIL(OTTZ_MGR.get_tenant_timezone(tenant_id_, tz_map_wrap, tz_info_mgr))) {
    LOG_WARN("get tenant timezone with lock failed", K(ret));
  } else if (OB_ISNULL(tz_info_mgr)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("tenant timezone mgr is null", K(tz_info_mgr));
  } else if (OB_UNLIKELY(
                 tz_info_wrap_.is_position_class() && tz_info_mgr->get_version() > tz_info_wrap_.get_cur_version())) {
    ObString tz_name;
    if (OB_UNLIKELY(!tz_info_wrap_.get_tz_info_pos().is_valid())) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("time zone info is invalid", K(tz_info_wrap_.get_tz_info_pos()), K(ret));
    } else if (OB_FAIL(tz_info_wrap_.get_tz_info_pos().get_tz_name(tz_name))) {
      LOG_WARN("fal to get time zone name", K(tz_info_wrap_.get_tz_info_pos()), K(ret));
    } else {
      int64_t orig_version = tz_info_wrap_.get_cur_version();
      tz_info_wrap_.set_cur_version(tz_info_mgr->get_version());
      if (OB_FAIL(tz_info_mgr->find_time_zone_info(tz_name, tz_info_wrap_.get_tz_info_pos()))) {
        LOG_WARN("fail to find time zone info", K(tz_name), K(ret));
        tz_info_wrap_.set_cur_version(orig_version);
      } else {
        tz_info_wrap_.get_tz_info_pos().set_error_on_overlap_time(tz_info_wrap_.is_error_on_overlap_time());
      }
    }
  }
  return ret;
}

constexpr ObSysVarClassType ObExecEnv::ExecEnvMap[MAX_ENV + 1];

void ObExecEnv::reset()
{
  sql_mode_ = DEFAULT_OCEANBASE_MODE;
  charset_client_ = CS_TYPE_INVALID;
  collation_connection_ = CS_TYPE_INVALID;
  collation_database_ = CS_TYPE_INVALID;
}

bool ObExecEnv::operator==(const ObExecEnv& other) const
{
  return sql_mode_ == other.sql_mode_ && charset_client_ == other.charset_client_ &&
         collation_connection_ == other.collation_connection_ && collation_database_ == other.collation_database_;
}

bool ObExecEnv::operator!=(const ObExecEnv& other) const
{
  return !(*this == other);
}

int ObExecEnv::gen_exec_env(const ObBasicSessionInfo& session, char* buf, int64_t len, int64_t& pos)
{
  int ret = OB_SUCCESS;
  ObObj val;
  for (int64_t i = 0; OB_SUCC(ret) && i < MAX_ENV; ++i) {
    int64_t size = 0;
    val.reset();
    if (OB_FAIL(session.get_sys_variable(ExecEnvMap[i], val))) {
      LOG_WARN("failed to get sys_variable", K(ExecEnvMap[i]), K(ret));
    } else if (OB_FAIL(val.print_plain_str_literal(buf + pos, len - pos, size))) {
      LOG_WARN("fail to encode obj", K(i), K(buf + pos), K(len), K(pos), K(val), K(ret));
    } else {
      pos += size;
      buf[pos++] = ',';
    }
  }
  return ret;
}

int ObExecEnv::get_exec_env(const ObString& exec_env, ExecEnvType type, ObString& value)
{
  int ret = OB_SUCCESS;
  if (exec_env.empty()) {
    ret = common::OB_ERR_UNEXPECTED;
    LOG_WARN("Invalid exec_env", K(exec_env), K(type), K(ret));
  } else {
    ObString result;
    ObString start = exec_env;
    const char* end = NULL;
    int64_t i = 0;
    while (!start.empty() && i++ < type) {
      start = start.after(',');
    }
    end = start.find(',');
    value.assign(start.ptr(), static_cast<ObString::obstr_size_t>(end - start.ptr()));
  }
  return ret;
}

#define SET_ENV_VALUE(env, type)                               \
  do {                                                         \
    int64_t value = 0;                                         \
    int64_t pos = 0;                                           \
    if (OB_FAIL(extract_int(value_str, 0, pos, value))) {      \
      LOG_WARN("Failed to extract int", K(value_str), K(ret)); \
    } else {                                                   \
      env = static_cast<type>(value);                          \
    }                                                          \
  } while (0)

int ObExecEnv::init(const ObString& exec_env)
{
  int ret = OB_SUCCESS;
  ObString value_str;
  for (int64_t i = 0; OB_SUCC(ret) && i < MAX_ENV; ++i) {
    value_str.reset();
    if (OB_FAIL(get_exec_env(exec_env, static_cast<ExecEnvType>(i), value_str))) {
      LOG_WARN("failed to get exec_env", K(exec_env), K(i), K(ret));
    } else {
      switch (i) {
        case SQL_MODE: {
          SET_ENV_VALUE(sql_mode_, ObSQLMode);
        } break;
        case CHARSET_CLIENT: {
          SET_ENV_VALUE(charset_client_, ObCollationType);
        } break;
        case COLLATION_CONNECTION: {
          SET_ENV_VALUE(collation_connection_, ObCollationType);
        } break;
        case COLLATION_DATABASE: {
          SET_ENV_VALUE(collation_database_, ObCollationType);
        } break;
        default: {
          ret = common::OB_ERR_UNEXPECTED;
          LOG_WARN("Invalid env type", K(exec_env), K(i), K(ret));
        } break;
      }
    }
  }
  return ret;
}

int ObExecEnv::load(ObBasicSessionInfo& session)
{
  int ret = OB_SUCCESS;
  ObObj val;
  for (int64_t i = 0; OB_SUCC(ret) && i < MAX_ENV; ++i) {
    val.reset();
    if (OB_FAIL(session.get_sys_variable(ExecEnvMap[i], val))) {
      LOG_WARN("failed to get sys_variable", K(ExecEnvMap[i]), K(ret));
    } else {
      switch (i) {
        case SQL_MODE: {
          sql_mode_ = static_cast<ObSQLMode>(val.get_int());
        } break;
        case CHARSET_CLIENT: {
          charset_client_ = static_cast<ObCollationType>(val.get_int());
        } break;
        case COLLATION_CONNECTION: {
          collation_connection_ = static_cast<ObCollationType>(val.get_int());
        } break;
        case COLLATION_DATABASE: {
          collation_database_ = static_cast<ObCollationType>(val.get_int());
        } break;
        default: {
          ret = common::OB_ERR_UNEXPECTED;
          LOG_WARN("Invalid env type", K(i), K(ret));
        } break;
      }
    }
  }
  return ret;
}

int ObExecEnv::store(ObBasicSessionInfo& session)
{
  int ret = OB_SUCCESS;
  ObObj val;
  for (int64_t i = 0; OB_SUCC(ret) && i < MAX_ENV; ++i) {
    val.reset();
    switch (i) {
      case SQL_MODE: {
        val.set_uint64(sql_mode_);
      } break;
      case CHARSET_CLIENT: {
        val.set_int(charset_client_);
      } break;
      case COLLATION_CONNECTION: {
        val.set_int(collation_connection_);
      } break;
      case COLLATION_DATABASE: {
        val.set_int(collation_database_);
      } break;
      default: {
        ret = common::OB_ERR_UNEXPECTED;
        LOG_WARN("Invalid env type", K(i), K(ret));
      } break;
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(session.update_sys_variable(ExecEnvMap[i], val))) {
        LOG_WARN("failed to get sys_variable", K(ExecEnvMap[i]), K(ret));
      }
    }
  }
  return ret;
}

4636 4637 4638 4639 4640 4641 4642 4643 4644
bool ObBasicSessionInfo::is_xa_trans()
{
  int bool_ret = false;
  if (trans_desc_.is_valid() && trans_desc_.is_xa_local_trans()) {
    bool_ret = true;
  }
  return bool_ret;
}

O
oceanbase-admin 已提交
4645 4646
}  // end of namespace sql
}  // end of namespace oceanbase