ob_inner_sql_connection.cpp 64.3 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/**
 * 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 SERVER

#include "ob_inner_sql_connection.h"
#include "share/ob_worker.h"
#include "lib/stat/ob_diagnose_info.h"
#include "lib/profile/ob_trace_id.h"
#include "common/ob_timeout_ctx.h"
#include "common/ob_smart_call.h"
#include "share/ob_debug_sync.h"
#include "share/ob_time_utility2.h"
#include "share/schema/ob_multi_version_schema_service.h"
#include "share/schema/ob_schema_getter_guard.h"
#include "share/config/ob_server_config.h"
#include "sql/ob_sql.h"
#include "sql/ob_sql_partition_location_cache.h"
#include "sql/ob_sql_trans_util.h"
#include "sql/resolver/ddl/ob_ddl_stmt.h"
#include "observer/ob_server.h"
#include "observer/mysql/ob_mysql_request_manager.h"
#include "observer/ob_server_struct.h"
#include "observer/virtual_table/ob_virtual_table_iterator_factory.h"
#include "observer/ob_req_time_service.h"
#include "ob_inner_sql_connection_pool.h"
#include "ob_inner_sql_read_context.h"
#include "ob_inner_sql_result.h"
#include "storage/transaction/ob_trans_define.h"  // ObTransIsolation
#include "sql/resolver/ob_schema_checker.h"

namespace oceanbase {
using namespace common;
using namespace sql;
using namespace share;
using namespace share::schema;

namespace observer {

constexpr const char ObInnerSQLConnection::LABEL[];

class ObInnerSQLConnection::ObSqlQueryExecutor : public sqlclient::ObIExecutor {
G
gm 已提交
52
public:
O
oceanbase-admin 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  explicit ObSqlQueryExecutor(const ObString& sql) : sql_(sql)
  {}
  explicit ObSqlQueryExecutor(const char* sql) : sql_(ObString::make_string(sql))
  {}

  virtual ~ObSqlQueryExecutor()
  {}

  virtual int execute(sql::ObSql& engine, sql::ObSqlCtx& ctx, sql::ObResultSet& res)
  {
    observer::ObReqTimeGuard req_timeinfo_guard;
    res.get_session().store_query_string(sql_);
    return engine.stmt_query(sql_, ctx, res);
  }

  // process result after result open
  virtual int process_result(sql::ObResultSet&) override
  {
    return OB_SUCCESS;
  }

  INHERIT_TO_STRING_KV("ObIExecutor", ObIExecutor, K_(sql));

G
gm 已提交
76
private:
O
oceanbase-admin 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
  ObString sql_;
};

ObInnerSQLConnection::TimeoutGuard::TimeoutGuard(ObInnerSQLConnection& conn) : conn_(conn)
{
  int ret = OB_SUCCESS;
  worker_timeout_ = THIS_WORKER.get_timeout_ts();
  if (OB_FAIL(conn_.get_session().get_query_timeout(query_timeout_)) ||
      OB_FAIL(conn_.get_session().get_tx_timeout(trx_timeout_))) {
    LOG_ERROR("get timeout failed", K(ret));
  }
}

ObInnerSQLConnection::TimeoutGuard::~TimeoutGuard()
{
  int ret = OB_SUCCESS;
  if (THIS_WORKER.get_timeout_ts() != worker_timeout_) {
    THIS_WORKER.set_timeout_ts(worker_timeout_);
  }

  int64_t query_timeout = 0;
  int64_t trx_timeout = 0;
  if (OB_FAIL(conn_.get_session().get_query_timeout(query_timeout)) ||
      OB_FAIL(conn_.get_session().get_tx_timeout(trx_timeout))) {
    LOG_ERROR("get timeout failed", K(ret));
  } else {
    if (query_timeout != query_timeout_ || trx_timeout != trx_timeout_) {
      if (OB_FAIL(conn_.set_session_timeout(query_timeout_, trx_timeout_))) {
        LOG_ERROR("set session timeout failed", K(ret));
      }
    }
  }
}

ObInnerSQLConnection::ObInnerSQLConnection()
    : inited_(false),
      inner_session_(),
      extern_session_(NULL),
      is_spi_conn_(false),
      ref_cnt_(0),
      pool_(NULL),
      schema_service_(NULL),
      ob_sql_(NULL),
      vt_iter_creator_(NULL),
      ref_ctx_(NULL),
      partition_table_operator_(NULL),
      sql_modifier_(NULL),
      init_timestamp_(0),
      tid_(-1),
      bt_size_(0),
      bt_addrs_(),
      execute_start_timestamp_(0),
      execute_end_timestamp_(0),
      config_(NULL),
      associated_client_(NULL),
      primary_schema_versions_()
{}

ObInnerSQLConnection::~ObInnerSQLConnection()
{
  if (0 < ref_cnt_) {
    LOG_ERROR("connection be referenced while destruct", K_(ref_cnt));
  }
}

int ObInnerSQLConnection::init(ObInnerSQLConnectionPool* pool, ObMultiVersionSchemaService* schema_service,
    ObSql* ob_sql, ObVTIterCreator* vt_iter_creator, const share::ObPartitionTableOperator* partition_table_operator,
    common::ObServerConfig* config, sql::ObSQLSessionInfo* extern_session, /* = NULL */
    ObISQLClient* client_addr,                                             /* = NULL */
    ObRestoreSQLModifier* sql_modifier /* = NULL */)
{
  int ret = OB_SUCCESS;
  if (inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("connection init twice", K(ret));
  } else if ((NULL == pool && NULL == sql_modifier) || NULL == schema_service || NULL == ob_sql ||
             NULL == vt_iter_creator || NULL == partition_table_operator) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("schema service should not be NULL",
        K(ret),
        KP(sql_modifier),
        KP(pool),
        KP(schema_service),
        KP(ob_sql),
        KP(vt_iter_creator),
        KP(partition_table_operator));
  } else {
    pool_ = pool;
    schema_service_ = schema_service;
    ob_sql_ = ob_sql;
    vt_iter_creator_ = vt_iter_creator;
    partition_table_operator_ = partition_table_operator;
    sql_modifier_ = sql_modifier;
    init_timestamp_ = ObTimeUtility::current_time();
    tid_ = GETTID();
    if (NULL == extern_session) {
      // Only backtrace internal used connection to avoid performance problems.
      bt_size_ = backtrace(bt_addrs_, MAX_BT_SIZE);
    }
    config_ = config;
    associated_client_ = client_addr;
    primary_schema_versions_.reset();
    if (NULL != client_addr) {
      oracle_mode_ = client_addr->is_oracle_mode();
    } else if (NULL != extern_session) {
      oracle_mode_ = ORACLE_MODE == extern_session->get_compatibility_mode();
    }
    if (OB_FAIL(init_session(extern_session))) {
      LOG_WARN("init session failed", K(ret));
    } else {
      inited_ = true;
    }
  }
  return ret;
}

int ObInnerSQLConnection::destroy()
{
  int ret = OB_SUCCESS;
  // uninited connection can be destroy too
  if (inited_) {
    if (0 < ref_cnt_) {
      ret = OB_REF_NUM_NOT_ZERO;
      LOG_ERROR("connection be referenced while destroy", K(ret), K_(ref_cnt));
    }

    // continue execute while error happen.
    inited_ = false;
    ref_cnt_ = 0;
    pool_ = NULL;
    schema_service_ = NULL;
    inner_session_.destroy();
    extern_session_ = NULL;
    config_ = NULL;
    associated_client_ = NULL;
    primary_schema_versions_.reset();
    ref_ctx_ = NULL;
  }
  return ret;
}

void ObInnerSQLConnection::ref()
{
  if (!inited_) {
    LOG_WARN("not init");
  } else {
    ref_cnt_++;
    if (ref_cnt_ > TOO_MANY_REF_ALERT) {
      LOG_WARN("connection be referenced too many times, this should be rare", K_(ref_cnt));
    }
  }
}

void ObInnerSQLConnection::unref()
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret), K(lbt()));
  } else if (OB_ISNULL(pool_)) {
    LOG_ERROR("not init conn pool");
  } else {
    if (0 == --ref_cnt_) {
      if (OB_FAIL(pool_->revert(this))) {
        LOG_WARN("revert connection failed", K(ret));
      }
    } else {
      // extern_session_ = NULL;
    }
  }
}

int ObInnerSQLConnection::init_session(sql::ObSQLSessionInfo* extern_session)
{
  int ret = OB_SUCCESS;
  if (NULL == extern_session) {
    // called in init(), can not check inited_ flag.
    ObArenaAllocator* allocator = NULL;
    const bool print_info_log = false;
    const bool is_sys_tenant = true;
    ObPCMemPctConf pc_mem_conf;
    inner_session_.set_inner_session();
    ObObj mysql_mode;
    ObObj oracle_mode;
    mysql_mode.set_int(0);
    oracle_mode.set_int(1);
    ObObj mysql_sql_mode;
    ObObj oracle_sql_mode;
    mysql_sql_mode.set_uint(ObUInt64Type, DEFAULT_MYSQL_MODE);
    oracle_sql_mode.set_uint(ObUInt64Type, DEFAULT_ORACLE_MODE);
    if (OB_FAIL(inner_session_.init(INNER_SQL_SESS_VERSION, INNER_SQL_SESS_ID, INNER_SQL_PROXY_SESS_ID, allocator))) {
      LOG_WARN("init session failed", K(ret));
    } else if (OB_FAIL(inner_session_.load_default_sys_variable(print_info_log, is_sys_tenant))) {
      LOG_WARN("session load default system variable failed", K(ret));
    } else if (OB_FAIL(inner_session_.update_max_packet_size())) {
      LOG_WARN("fail to update max packet size", K(ret));
    } else if (OB_FAIL(inner_session_.init_tenant(OB_SYS_TENANT_NAME, OB_SYS_TENANT_ID))) {
      LOG_WARN("fail to init tenant", K(ret));
    } else if (OB_FAIL(switch_tenant(OB_SYS_TENANT_ID))) {
      LOG_WARN("set system tenant id failed", K(ret));
    } else if (OB_FAIL(inner_session_.update_sys_variable(
                   SYS_VAR_SQL_MODE, oracle_mode_ ? oracle_sql_mode : mysql_sql_mode))) {
      LOG_WARN("update sys variables failed", K(ret));
    } else if (OB_FAIL(inner_session_.update_sys_variable(
                   SYS_VAR_OB_COMPATIBILITY_MODE, oracle_mode_ ? oracle_mode : mysql_mode))) {
      LOG_WARN("update sys variables failed", K(ret));
    } else if (OB_FAIL(inner_session_.update_sys_variable(
                   SYS_VAR_NLS_DATE_FORMAT, ObTimeConverter::COMPAT_OLD_NLS_DATE_FORMAT))) {
      LOG_WARN("update sys variables failed", K(ret));
    } else if (OB_FAIL(inner_session_.update_sys_variable(
                   SYS_VAR_NLS_TIMESTAMP_FORMAT, ObTimeConverter::COMPAT_OLD_NLS_TIMESTAMP_FORMAT))) {
      LOG_WARN("update sys variables failed", K(ret));
    } else if (OB_FAIL(inner_session_.update_sys_variable(
                   SYS_VAR_NLS_TIMESTAMP_TZ_FORMAT, ObTimeConverter::COMPAT_OLD_NLS_TIMESTAMP_TZ_FORMAT))) {
      LOG_WARN("update sys variables failed", K(ret));
    } else {
      ObString database_name(OB_SYS_DATABASE_NAME);
      if (OB_FAIL(inner_session_.set_default_database(database_name))) {
        LOG_WARN("fail to set default database", K(ret), K(database_name));
      } else if (OB_FAIL(inner_session_.get_pc_mem_conf(pc_mem_conf))) {
        LOG_WARN("fail to get pc mem conf", K(ret));
      } else {
        inner_session_.set_plan_cache_manager(ob_sql_->get_plan_cache_manager());
        inner_session_.set_database_id(combine_id(OB_SYS_TENANT_ID, OB_SYS_DATABASE_ID));
        inner_session_.set_use_static_typing_engine(false);
      }
    }
  } else {
    extern_session_ = extern_session;
  }
  return ret;
}

int ObInnerSQLConnection::init_result(ObInnerSQLResult& res, ObVirtualTableIteratorFactory* vt_iter_factory,
    int64_t retry_cnt, ObSchemaGetterGuard& schema_guard, bool is_prepare_protocol, bool is_prepare_stage,
    bool is_from_pl, bool is_dynamic_sql, bool is_dbms_sql, bool is_cursor)
{
  int ret = OB_SUCCESS;
  sql::ObResultSet& result_set = res.result_set();
  const ObGlobalContext& gctx = ObServer::get_instance().get_gctx();
  result_set.init_partition_location_cache(gctx.location_cache_, gctx.self_addr_, &schema_guard);
  result_set.get_exec_context().get_task_exec_ctx().set_min_cluster_version(GET_MIN_CLUSTER_VERSION());
  result_set.get_exec_context().get_task_exec_ctx().schema_service_ = gctx.schema_service_;
  res.sql_ctx().retry_times_ = retry_cnt;
  res.sql_ctx().merged_version_ = *(gctx.merged_version_);
  res.sql_ctx().vt_iter_factory_ = vt_iter_factory;
  res.sql_ctx().session_info_ = &get_session();
  res.sql_ctx().sql_proxy_ = gctx.sql_proxy_;
  res.sql_ctx().use_plan_cache_ = true;
  res.sql_ctx().disable_privilege_check_ = is_from_pl ? PRIV_CHECK_FLAG_IN_PL : PRIV_CHECK_FLAG_DISABLE;
  res.sql_ctx().partition_table_operator_ = partition_table_operator_;
  res.sql_ctx().partition_location_cache_ = &(result_set.get_partition_location_cache());
  res.sql_ctx().part_mgr_ = gctx.part_mgr_;
  res.sql_ctx().is_prepare_protocol_ = is_prepare_protocol;
  res.sql_ctx().is_prepare_stage_ = is_prepare_stage;
  res.sql_ctx().is_dynamic_sql_ = is_dynamic_sql;
  res.sql_ctx().is_dbms_sql_ = is_dbms_sql;
  res.sql_ctx().is_cursor_ = is_cursor;
  if (FALSE_IT(res.sql_ctx().schema_guard_ = &schema_guard)) {
    // do-nothing
  } else if (NULL == res.sql_ctx().partition_table_operator_) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("partition table operator is NULL!", K(ret));
  } else if (OB_FAIL(res.result_set().init())) {
    LOG_WARN("result set init failed", K(ret));
  } else if (is_prepare_protocol && !is_dynamic_sql) {
    result_set.set_simple_ps_protocol();
  } else { /*do nothing*/
  }
  return ret;
}

int ObInnerSQLConnection::process_retry(
    ObInnerSQLResult& res, int& last_ret, int64_t abs_timeout_us, bool& need_retry, int64_t retry_cnt, bool is_from_pl)
{
  int ret = last_ret;
  sql::ObResultSet& result_set = res.result_set();
  bool is_direct_local_plan_retry = result_set.get_exec_context().get_direct_local_plan() &&
                                    (OB_NOT_MASTER == last_ret || OB_PARTITION_NOT_EXIST == last_ret);
  ObQueryRetryInfo& retry_info = inner_session_.get_retry_info_for_update();
  const bool non_blocking_refresh = false;
  bool repeatable_stmt = (ObStmt::is_dml_stmt(result_set.get_stmt_type()) ||
                          ObStmt::is_ddl_stmt(result_set.get_stmt_type(), result_set.has_global_variable()) ||
                          ObStmt::is_dcl_stmt(result_set.get_stmt_type()));
  int64_t now = ObTimeUtility::current_time();
  if (now >= abs_timeout_us) {
    ret = OB_TIMEOUT;
    LOG_WARN("timeout, do not need retry", K(ret), K(last_ret), K(abs_timeout_us), K(now));
    if (retry_info.is_rpc_timeout() || is_transaction_rpc_timeout_err(last_ret)) {
      int err1 = result_set.refresh_location_cache(true);
      if (OB_SUCCESS != err1) {
        LOG_WARN("fail to nonblock refresh location cache", K(ret), K(last_ret), K(err1));
      }
      LOG_WARN("sql rpc timeout, or trans rpc timeout, maybe location is changed, "
               "refresh location cache non blockly",
          K(ret),
          K(last_ret),
          K(retry_info.is_rpc_timeout()));
    }
  } else if (repeatable_stmt && did_no_retry_on_rpc_error_ &&
             (retry_info.is_rpc_timeout() || is_transaction_rpc_timeout_err(last_ret) ||
                 is_server_down_error(last_ret) || is_master_changed_error(last_ret))) {
    ret = OB_TIMEOUT;
    LOG_INFO("YZFDEBUG no retry for rpc error",
        K(ret),
        K(abs_timeout_us),
        K(now),
        K(retry_cnt),
        K(last_ret),
        K(is_direct_local_plan_retry));
    if (!is_direct_local_plan_retry) {
      int err1 = result_set.refresh_location_cache(true);
      if (OB_SUCCESS != err1) {
        LOG_WARN("fail to nonblock refresh location cache", K(ret), K(last_ret), K(err1));
      }
    }
  } else if (repeatable_stmt && is_try_lock_row_err(last_ret) && is_from_pl) {
    if (retry_cnt <= 0 || !get_session().get_pl_can_retry()) {
      need_retry = true;
    } else {
      need_retry = false;
      ret = last_ret;
    }
  } else if (is_schema_error(last_ret) && result_set.is_user_sql()) {
    if ((OB_ERR_WAIT_REMOTE_SCHEMA_REFRESH == last_ret) || (OB_SCHEMA_NOT_UPTODATE == last_ret) ||
        (OB_SCHEMA_ERROR == last_ret) || (OB_SCHEMA_EAGAIN == last_ret)) {
      need_retry = true;
      LOG_WARN("schema err, need to retry", K(last_ret));
    } else {
      LOG_WARN("schema err, but not need to retry", K(last_ret));
    }
  } else if (repeatable_stmt &&
             (is_master_changed_error(last_ret) || is_partition_change_error(last_ret) ||
                 is_server_down_error(last_ret) || is_process_timeout_error(last_ret) ||
                 is_get_location_timeout_error(last_ret) || is_try_lock_row_err(last_ret) ||
                 is_has_no_readable_replica_err(last_ret) || is_select_dup_follow_replic_err(last_ret) ||
                 is_trans_stmt_need_retry_error(last_ret) || is_transaction_set_violation_err(last_ret) ||
                 is_snapshot_discarded_err(last_ret))) {
    need_retry = true;
    const uint64_t* trace_id = ObCurTraceId::get();
    bool sql_trigger_by_user_req = (NULL != trace_id && 0 != trace_id[0] && 0 != trace_id[1]);
    if (is_location_leader_not_exist_error(last_ret) || is_has_no_readable_replica_err(last_ret)) {
      // retry_info.reset();
      retry_info.clear();
    }
    if (!is_try_lock_row_err(last_ret)) {
      LOG_INFO("sql execute need retry", K(ret), K(last_ret), K(retry_cnt), K(is_direct_local_plan_retry));
      if (!is_direct_local_plan_retry && OB_FAIL(result_set.refresh_location_cache(non_blocking_refresh))) {
        LOG_WARN("refresh location cache failed", K(ret), K(last_ret));
      }
    }
    if (sql_trigger_by_user_req) {
      const int64_t step = 1000 * 10;  // 10ms
      const int64_t max = 100;         // cost constant time after max retry_cnt
      const int64_t sleep_time_us = (retry_cnt < max ? retry_cnt : max) * step;
      LOG_INFO("sql execute need retry", K(ret), K(last_ret), K(retry_cnt), K(sleep_time_us));
      THIS_WORKER.sched_wait();
      usleep(static_cast<unsigned int>(sleep_time_us));
      THIS_WORKER.sched_run();
    }
  } else if (repeatable_stmt && is_data_not_readable_err(last_ret)) {
    need_retry = true;
    const int64_t step = 1000 * 10;  // 10ms
    const int64_t max = 10;          // cost constant time after max retry_cnt, so max sleep time is 100ms
    const int64_t sleep_time_us = (retry_cnt < max ? retry_cnt : max) * step;
    usleep(static_cast<unsigned int>(sleep_time_us));

    LOG_WARN("has read not readable data, retry in local thread",
        K(need_retry),
        K(ret),
        K(last_ret),
        K(retry_cnt),
        K(sleep_time_us));
  } else if (repeatable_stmt && is_gts_not_ready_err(last_ret)) {
    const int64_t sleep_time_us = 10 * 1000;  // 10ms
    need_retry = true;
    LOG_WARN("gts is not ready, need retry", K(ret), K(last_ret), K(retry_cnt));
    THIS_WORKER.sched_wait();
    usleep(static_cast<unsigned int>(sleep_time_us));
    THIS_WORKER.sched_run();
  } else if (repeatable_stmt && OB_AUTOINC_SERVICE_BUSY == last_ret) {
    const int64_t sleep_time_us = 10 * 1000;  // 10ms
    need_retry = true;
    LOG_WARN("conncurrent update autoinc cache, need retry", K(ret), K(last_ret), K(retry_cnt));
    THIS_WORKER.sched_wait();
    usleep(static_cast<unsigned int>(sleep_time_us));
    THIS_WORKER.sched_run();
  }
  if (get_session().is_nested_session()) {
    /**
     * right now, top session will retry, bug we can do something here like refresh XXX cache.
     * in future, nested session can retry if nested transaction is supported.
     */
    need_retry = false;
    ret = last_ret;
  }
  if (need_retry) {
    LOG_WARN("need retry, set ret to OB_SUCCESS", K(ret), K(last_ret), K(retry_cnt));
    retry_info.set_last_query_retry_err(last_ret);
O
obdev 已提交
476 477 478 479
    if (OB_ISNULL(extern_session_)) {
    } else {
      extern_session_->get_retry_info_for_update().set_last_query_retry_err(last_ret);
    }
O
oceanbase-admin 已提交
480 481 482 483 484 485
    ret = OB_SUCCESS;
  }
  return ret;
}

class ObInnerSQLTimeRecord : public ObITimeRecord {
G
gm 已提交
486
public:
O
oceanbase-admin 已提交
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
  ObInnerSQLTimeRecord(sql::ObSQLSessionInfo& session)
      : session_(session), execute_start_timestamp_(0), execute_end_timestamp_(0)
  {}

  int64_t get_send_timestamp() const
  {
    return session_.get_query_start_time();
  }
  int64_t get_receive_timestamp() const
  {
    return session_.get_query_start_time();
  }
  int64_t get_enqueue_timestamp() const
  {
    return session_.get_query_start_time();
  }
  int64_t get_run_timestamp() const
  {
    return session_.get_query_start_time();
  }
  int64_t get_process_timestamp() const
  {
    return session_.get_query_start_time();
  }
  int64_t get_single_process_timestamp() const
  {
    return session_.get_query_start_time();
  }
  int64_t get_exec_start_timestamp() const
  {
    return execute_start_timestamp_;
  }
  int64_t get_exec_end_timestamp() const
  {
    return execute_end_timestamp_;
  }

  void set_execute_start_timestamp(int64_t v)
  {
    execute_start_timestamp_ = v;
  }
  void set_execute_end_timestamp(int64_t v)
  {
    execute_end_timestamp_ = v;
  }

G
gm 已提交
533
private:
O
oceanbase-admin 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
  sql::ObSQLSessionInfo& session_;
  int64_t execute_start_timestamp_;
  int64_t execute_end_timestamp_;
};

int ObInnerSQLConnection::process_record(ObInnerSQLResult& res, sql::ObSQLSessionInfo& session,
    ObITimeRecord& time_record, int last_ret, int64_t execution_id, int64_t ps_stmt_id, int64_t routine_id,
    ObWaitEventDesc& max_wait_desc, ObWaitEventStat& total_wait_desc, ObExecRecord& exec_record,
    ObExecTimestamp& exec_timestamp, bool is_from_pl)
{
  int ret = OB_SUCCESS;

  UNUSED(routine_id);
  sql::ObResultSet& result_set = res.result_set();
  ObAuditRecordData& audit_record = session.get_audit_record();
  ObPhysicalPlan* plan = res.result_set().get_physical_plan();
  audit_record.seq_ = 0;  // don't use now
  audit_record.status_ = (0 == last_ret || OB_ITER_END == last_ret) ? obmysql::REQUEST_SUCC : last_ret;

  audit_record.client_addr_ = session.get_peer_addr();
  audit_record.user_client_addr_ = session.get_user_client_addr();
  audit_record.user_group_ = THIS_WORKER.get_group_id();
  audit_record.execution_id_ = execution_id;
  audit_record.ps_stmt_id_ = ps_stmt_id;
  MEMCPY(audit_record.sql_id_, res.sql_ctx().sql_id_, (int32_t)sizeof(audit_record.sql_id_));
  audit_record.affected_rows_ = res.result_set().get_affected_rows();
  audit_record.return_rows_ = res.result_set().get_return_rows();
  if (NULL != res.result_set().get_exec_context().get_task_executor_ctx()) {
    audit_record.partition_cnt_ = res.result_set().get_exec_context().get_task_executor_ctx()->get_related_part_cnt();
  }

  exec_record.max_wait_event_ = max_wait_desc;
  exec_record.wait_time_end_ = total_wait_desc.time_waited_;
  exec_record.wait_count_end_ = total_wait_desc.total_waits_;

  if (NULL != res.result_set().get_physical_plan()) {
    audit_record.plan_type_ = res.result_set().get_physical_plan()->get_plan_type();
    audit_record.table_scan_ = res.result_set().get_physical_plan()->contain_table_scan();
    audit_record.plan_id_ = res.result_set().get_physical_plan()->get_plan_id();
    audit_record.plan_hash_ = res.result_set().get_physical_plan()->get_plan_hash_value();
  }

  audit_record.is_executor_rpc_ = false;
  audit_record.is_inner_sql_ = true;
  audit_record.is_hit_plan_cache_ = res.result_set().get_is_from_plan_cache();
  audit_record.is_multi_stmt_ = false;  // if multi sql
  audit_record.trans_hash_ = session.get_trans_desc().get_trans_id().hash();

  bool first_record = (0 == audit_record.try_cnt_);
  ObExecStatUtils::record_exec_timestamp(time_record, first_record, exec_timestamp);
  audit_record.exec_timestamp_ = exec_timestamp;
  audit_record.exec_record_ = exec_record;

  ObIArray<ObTableRowCount>* table_row_count_list = NULL;
  ObPhysicalPlanCtx* plan_ctx = GET_PHY_PLAN_CTX(res.result_set().get_exec_context());
  if (NULL != plan_ctx) {
    audit_record.consistency_level_ = plan_ctx->get_consistency_level();
    audit_record.table_scan_stat_ = plan_ctx->get_table_scan_stat();
    table_row_count_list = &(plan_ctx->get_table_row_count_list());
  }

  audit_record.update_stage_stat();

  // update v$sql statistics
  if (OB_SUCC(last_ret) && session.get_local_ob_enable_plan_cache()) {
    if (NULL != plan) {
      if (!(res.sql_ctx().self_add_plan_) && res.sql_ctx().plan_cache_hit_) {
        plan->update_plan_stat(audit_record,
            false,  // false mean not first update plan stat
            res.result_set().get_exec_context().get_is_evolution(),
            table_row_count_list);
      } else if (res.sql_ctx().self_add_plan_ && !res.sql_ctx().plan_cache_hit_) {
        plan->update_plan_stat(
            audit_record, true, res.result_set().get_exec_context().get_is_evolution(), table_row_count_list);
      }
    }
  }

  record_stat(session, result_set.get_stmt_type(), is_from_pl);
  ObSQLUtils::handle_audit_record(false, EXECUTE_INNER, session, res.result_set().get_exec_context());
  return ret;
}

template <typename T>
int ObInnerSQLConnection::process_final(const T& sql, ObInnerSQLResult& res, int last_ret)
{
  int ret = OB_SUCCESS;
  UNUSED(res);
  if (lib::is_diagnose_info_enabled()) {
    int64_t process_time = ObTimeUtility::current_time() - get_session().get_query_start_time();
    if (OB_SUCC(last_ret)) {
      const int64_t now = ObTimeUtility::current_time();
      EVENT_INC(INNER_SQL_CONNECTION_EXECUTE_COUNT);
      EVENT_ADD(INNER_SQL_CONNECTION_EXECUTE_TIME, now - get_session().get_query_start_time());
    }

    if (process_time > 1L * 1000 * 1000) {
      LOG_INFO("slow inner sql", K(last_ret), K(sql), K(process_time));
    }
  }
  return ret;
}

int ObInnerSQLConnection::do_query(sqlclient::ObIExecutor& executor, ObInnerSQLResult& res)
{
  int ret = OB_SUCCESS;
  WITH_CONTEXT(res.mem_context_)
  {
    bool is_restore = NULL != sql_modifier_;
    res.sql_ctx().is_restore_ = is_restore;
    res.sql_ctx().is_ddl_from_primary_ = is_ddl_from_primary();
    if (!inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("not init", K(ret));
    } else if (OB_ISNULL(ob_sql_)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("ob_sql_ is NULL", K(ret));
    } else if (OB_FAIL(executor.execute(*ob_sql_, res.sql_ctx(), res.result_set()))) {
      LOG_WARN("executor execute failed", K(ret));
    } else {
      ObSQLSessionInfo& session = res.result_set().get_session();
      if (OB_ISNULL(res.sql_ctx().schema_guard_)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("schema guard is null");
      } else if (OB_FAIL(session.update_query_sensitive_system_variable(*(res.sql_ctx().schema_guard_)))) {
        LOG_WARN("update query affacted system variable failed", K(ret));
      } else if (OB_UNLIKELY(is_restore) && OB_FAIL(sql_modifier_->modify(res.result_set()))) {
        LOG_WARN("fail modify sql", K(res.result_set().get_statement_name()), K(ret));
      } else if (0 < primary_schema_versions_.count() && OB_FAIL(process_schema_version(res.result_set()))) {
        LOG_WARN("failed to procsee schema version", K(ret), K(primary_schema_versions_));
      } else if (OB_FAIL(res.open())) {
        LOG_WARN("result set open failed", K(ret), K(executor));
      }
    }
  }

  return ret;
}

int ObInnerSQLConnection::process_schema_version(sql::ObResultSet& result_set)
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (0 >= primary_schema_versions_.count()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("schema version is invalid", K(ret), K(primary_schema_versions_));
  } else {
    ObICmd* cmd = const_cast<ObICmd*>(result_set.get_cmd());
    if (OB_ISNULL(cmd)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("cmd is null", K(ret), K(result_set));
    } else {
      sql::ObDDLStmt* ddl_stmt = static_cast<sql::ObDDLStmt*>(cmd);
      if (OB_ISNULL(ddl_stmt)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("type error", K(ret), K(cmd));
      } else if (OB_FAIL(ddl_stmt->get_ddl_arg().primary_schema_versions_.assign(primary_schema_versions_))) {
        LOG_WARN("failed to assign ddl_stmt", K(ret), K(primary_schema_versions_), K(ddl_stmt));
      }
    }
  }
  return ret;
}

int ObInnerSQLConnection::query(sqlclient::ObIExecutor& executor, ObInnerSQLResult& res,
    ObVirtualTableIteratorFactory* vt_iter_factory, bool is_from_pl)
{
  int ret = OB_SUCCESS;
  share::CompatModeGuard g(get_compat_mode());
  ObExecRecord exec_record;
  ObExecTimestamp exec_timestamp;

  exec_timestamp.exec_type_ = sql::InnerSql;
  const ObGlobalContext& gctx = ObServer::get_instance().get_gctx();
  int64_t start_time = ObTimeUtility::current_time();
  if (!is_from_pl) {
    get_session().set_query_start_time(start_time);
  }
  get_session().set_trans_type(transaction::ObTransType::TRANS_SYSTEM);
  int64_t abs_timeout_us = 0;
  int64_t execution_id = 0;
  bool is_trace_id_init = true;
  ObQueryRetryInfo& retry_info = inner_session_.get_retry_info_for_update();
  ObAddr host_addr = ObCurTraceId::get_addr();
  if (!host_addr.is_valid()) {
    is_trace_id_init = false;
    common::ObCurTraceId::init(observer::ObServer::get_instance().get_self());
  }

  // backup && restore worker/session timeout.
  TimeoutGuard timeout_guard(*this);

  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (NULL != ref_ctx_) {
    ret = OB_REF_NUM_NOT_ZERO;
    LOG_ERROR("connection still be referred by previous sql result, can not execute sql now", K(ret), K(executor));
  } else if (OB_FAIL(set_timeout(abs_timeout_us, is_from_pl))) {
    LOG_WARN("set timeout failed", K(ret));
  } else if (OB_ISNULL(ob_sql_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid sql engine", K(ret), K(ob_sql_));
  } else if (OB_UNLIKELY(retry_info.is_inited())) {
    if (is_inner_session()) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("retry info is inited", K(ret), K(retry_info), K(executor));
    }
  } else if (OB_FAIL(retry_info.init())) {
    LOG_WARN("fail to init retry info", K(ret), K(retry_info), K(executor));
  }
  if (OB_SUCC(ret)) {
    execution_id = ob_sql_->get_execution_id();
    bool need_retry = true;
    for (int64_t retry_cnt = 0; need_retry; ++retry_cnt) {
      need_retry = false;
      retry_info.clear_state_before_each_retry();
      if (retry_cnt > 0) {  // reset result set
        bool is_user_sql = res.result_set().is_user_sql();
        res.~ObInnerSQLResult();
        new (&res) ObInnerSQLResult(get_session());
        if (OB_FAIL(res.init())) {
          LOG_WARN("fail to init result set", K(ret));
        } else {
          res.result_set().set_user_sql(is_user_sql);
        }
      }

      int64_t local_tenant_schema_version = -1;
      int64_t local_sys_schema_version = -1;
      ObWaitEventDesc max_wait_desc;
      ObWaitEventStat total_wait_desc;
      const bool enable_perf_event = lib::is_diagnose_info_enabled();
      const bool enable_sql_audit =
          GCONF.enable_sql_audit && get_session().get_local_ob_enable_sql_audit() && !is_from_pl;
      {
        ObMaxWaitGuard max_wait_guard(enable_perf_event ? &max_wait_desc : NULL);
        ObTotalWaitGuard total_wait_guard(enable_perf_event ? &total_wait_desc : NULL);

        if (enable_sql_audit) {
          exec_record.record_start();
        }

        const uint64_t tenant_id = get_session().get_effective_tenant_id();
        if (OB_FAIL(ret)) {
          // do nothing
        } else if (OB_FAIL(gctx.schema_service_->get_tenant_schema_guard(tenant_id, res.schema_guard_))) {
          LOG_WARN("get schema guard failed", K(ret));
        } else if (OB_FAIL(init_result(res, vt_iter_factory, retry_cnt, res.schema_guard_, false, false, is_from_pl))) {
          LOG_WARN("failed to init result", K(ret));
        } else if (OB_FAIL(res.schema_guard_.get_schema_version(tenant_id, local_tenant_schema_version))) {
          LOG_WARN("get tenant schema version failed", K(ret), K(ob_sql_));
        } else if (OB_FAIL(res.schema_guard_.get_schema_version(OB_SYS_TENANT_ID, local_sys_schema_version))) {
          LOG_WARN("get sys tenant schema version failed", K(ret), K(ob_sql_));
        } else if (OB_UNLIKELY(is_extern_session())) {
          res.result_set().get_exec_context().get_task_exec_ctx().set_query_tenant_begin_schema_version(
              local_tenant_schema_version);
          res.result_set().get_exec_context().get_task_exec_ctx().set_query_sys_begin_schema_version(
              local_sys_schema_version);
        }

797
        int ret_code = OB_SUCCESS;
O
oceanbase-admin 已提交
798 799 800
        if (OB_FAIL(ret)) {
          // do nothing
        } else if (OB_FAIL(SMART_CALL(do_query(executor, res)))) {
801
          ret_code = ret;
O
oceanbase-admin 已提交
802 803 804 805 806 807 808 809 810 811 812 813
          LOG_WARN("execute failed", K(ret), K(executor), K(retry_cnt));
          int tmp_ret = process_retry(res, ret, abs_timeout_us, need_retry, retry_cnt, is_from_pl);
          if (OB_SUCCESS != tmp_ret) {
            LOG_WARN("failed to process retry", K(tmp_ret), K(ret), K(executor), K(retry_cnt));
          }
          ret = tmp_ret;
          // moved here from ObInnerSQLConnection::do_query() -> ObInnerSQLResult::open().
          int close_ret = res.force_close(need_retry);
          if (OB_SUCCESS != close_ret) {
            LOG_WARN("failed to close result", K(close_ret), K(ret));
          }
        }
814
        get_session().set_session_in_retry(need_retry, ret_code);
O
oceanbase-admin 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 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 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
        execute_start_timestamp_ = res.get_execute_start_ts();
        execute_end_timestamp_ = res.get_execute_end_ts();

        if (enable_sql_audit) {
          exec_record.record_end();
        }
      }

      if (enable_sql_audit && res.is_inited()) {
        ObInnerSQLTimeRecord time_record(get_session());
        time_record.set_execute_start_timestamp(execute_start_timestamp_);
        time_record.set_execute_end_timestamp(execute_end_timestamp_);
        int record_ret = process_record(res,
            get_session(),
            time_record,
            ret,
            execution_id,
            OB_INVALID_ID,
            OB_INVALID_ID,
            max_wait_desc,
            total_wait_desc,
            exec_record,
            exec_timestamp);
        if (OB_SUCCESS != record_ret) {
          LOG_WARN("failed to process record", K(executor), K(record_ret), K(ret));
        }
      }

      if (res.is_inited()) {
        if (get_session().get_in_transaction()) {
          if (ObStmt::is_dml_write_stmt(res.result_set().get_stmt_type())) {
            get_session().set_has_inner_dml_write(true);
          }
        }
      }
    }
  }
  if (res.is_inited()) {
    int aret = process_final(executor, res, ret);
    if (OB_SUCCESS != aret) {
      LOG_WARN("failed to process final", K(executor), K(aret), K(ret));
    }
  }

  if (false == is_trace_id_init) {
    common::ObCurTraceId::reset();
  }

  retry_info.reset();
  return ret;
}

int ObInnerSQLConnection::do_prepare(const common::ObString& sql, ObInnerSQLResult& res)
{
  int ret = OB_SUCCESS;
  WITH_CONTEXT(res.mem_context_)
  {
    if (!inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("not init", K(ret));
    } else if (sql.empty()) {
      ret = OB_INVALID_ARGUMENT;
      LOG_WARN("invalid argument", K(ret), K(sql));
    } else if (OB_FAIL(ob_sql_->stmt_prepare(sql, res.sql_ctx(), res.result_set()))) {
      LOG_WARN("sql execute failed", K(ret), K(sql));
    }
  }
  return ret;
}

int ObInnerSQLConnection::prepare(const ObString& sql, bool is_dynamic_sql, bool is_dbms_sql, bool is_cursor,
    ObInnerSQLResult& res, ObVirtualTableIteratorFactory* vt_iter_factory)
{
  int ret = OB_SUCCESS;
  ObExecRecord exec_record;
  ObExecTimestamp exec_timestamp;
  int64_t execution_id = 0;
  exec_timestamp.exec_type_ = sql::InnerSql;
  const ObGlobalContext& gctx = ObServer::get_instance().get_gctx();
  int64_t old_query_start_time = get_session().get_query_start_time();
  get_session().set_query_start_time(ObTimeUtility::current_time());
  get_session().set_trans_type(transaction::ObTransType::TRANS_SYSTEM);
  // get_session().store_query_string(sql);
  int64_t abs_timeout_us = 0;
  const uint64_t* trace_id_val = ObCurTraceId::get();
  bool is_trace_id_init = true;
  ObQueryRetryInfo& retry_info = inner_session_.get_retry_info_for_update();
  if (0 == trace_id_val[0]) {
    is_trace_id_init = false;
    common::ObCurTraceId::init(observer::ObServer::get_instance().get_self());
  }

  // backup && restore worker/session timeout.
  TimeoutGuard timeout_guard(*this);

  // %vt_iter_factory may be NULL
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (sql.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(sql));
  } else if (NULL != ref_ctx_) {
    ret = OB_REF_NUM_NOT_ZERO;
    LOG_ERROR("connection still be referred by previous sql result, can not execute sql now", K(ret), K(sql));
  } else if (OB_FAIL(set_timeout(abs_timeout_us, true))) {
    LOG_WARN("set timeout failed", K(ret));
  } else if (OB_ISNULL(ob_sql_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid sql engine", K(ret), K(ob_sql_));
  } else if (OB_UNLIKELY(retry_info.is_inited())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("retry info is inited", K(ret), K(retry_info), K(sql));
  } else if (OB_FAIL(retry_info.init())) {
    LOG_WARN("fail to init retry info", K(ret), K(retry_info), K(sql));
  } else {
    execution_id = ob_sql_->get_execution_id();
  }
  if (OB_SUCC(ret)) {
    bool need_retry = true;
    for (int64_t retry_cnt = 0; need_retry; ++retry_cnt) {
      need_retry = false;
      retry_info.clear_state_before_each_retry();
      if (retry_cnt > 0) {  // reset result set
        res.~ObInnerSQLResult();
        new (&res) ObInnerSQLResult(get_session());
        ret = res.init();
      }
      const uint64_t tenant_id = get_session().get_effective_tenant_id();
      if (OB_FAIL(gctx.schema_service_->get_tenant_schema_guard(tenant_id, res.schema_guard_))) {
        LOG_WARN("get schema guard failed", K(ret));
      } else if (OB_FAIL(init_result(res,
                     vt_iter_factory,
                     retry_cnt,
                     res.schema_guard_,
                     true,
                     true,
                     true,
                     is_dynamic_sql,
                     is_dbms_sql,
                     is_cursor))) {
        LOG_WARN("failed to init result", K(ret));
      } else if (OB_FAIL(do_prepare(sql, res))) {
        LOG_WARN("execute sql failed", K(ret), K(sql), K(retry_cnt));
        int tmp_ret = process_retry(res, ret, abs_timeout_us, need_retry, retry_cnt, true);
        if (OB_SUCCESS != tmp_ret) {
          LOG_WARN("failed to process retry", K(tmp_ret), K(ret), K(sql), K(retry_cnt));
        }
        ret = tmp_ret;
      }
    }
  }
  if (res.is_inited()) {
    int aret = process_final(sql, res, ret);
    if (OB_SUCCESS != aret) {
      LOG_WARN("failed to process final", K(sql), K(aret), K(ret));
    }
  }
  if (false == is_trace_id_init) {
    common::ObCurTraceId::reset();
  }
  retry_info.reset();
  get_session().set_query_start_time(old_query_start_time);
  return ret;
}

int ObInnerSQLConnection::do_execute(const ParamStore& params, ObInnerSQLResult& res)
{
  int ret = OB_SUCCESS;
  WITH_CONTEXT(res.mem_context_)
  {
    if (!inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("not init", K(ret));
    } else if (OB_ISNULL(ob_sql_)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_ERROR("ob_sql_ is NULL", K(ret));
    } else if (OB_FAIL(ob_sql_->stmt_execute(res.result_set().get_statement_id(),
                   res.result_set().get_stmt_type(),
                   params,
                   res.sql_ctx(),
                   res.result_set(),
                   true /* is_inner_sql */))) {
      LOG_WARN("sql execute failed", K(res.result_set().get_statement_id()), K(ret));
    } else {
      ObSQLSessionInfo& session = res.result_set().get_session();
      if (OB_ISNULL(res.sql_ctx().schema_guard_)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("schema guard is null");
      } else if (OB_FAIL(session.update_query_sensitive_system_variable(*(res.sql_ctx().schema_guard_)))) {
        LOG_WARN("update query affacted system variable failed", K(ret));
      } else if (OB_UNLIKELY(NULL != sql_modifier_) && OB_FAIL(sql_modifier_->modify(res.result_set()))) {
        LOG_WARN("fail modify sql", K(res.result_set().get_statement_name()), K(ret));
      } else if (OB_FAIL(res.open())) {
        LOG_WARN("result set open failed", K(res.result_set().get_statement_id()), K(ret));
      } else { /*do nothing*/
      }
    }
  }
  return ret;
}

int ObInnerSQLConnection::execute(ParamStore& params, ObInnerSQLResult& res,
    ObVirtualTableIteratorFactory* vt_iter_factory, bool is_from_pl, bool is_dynamic)
{
  int ret = OB_SUCCESS;
  ObExecRecord exec_record;
  ObExecTimestamp exec_timestamp;
  exec_timestamp.exec_type_ = sql::InnerSql;
  const ObGlobalContext& gctx = ObServer::get_instance().get_gctx();
  const ObString& sql = res.result_set().get_statement_name();
  int64_t start_time = ObTimeUtility::current_time();
  if (!is_from_pl) {
    get_session().set_query_start_time(start_time);
  }
  get_session().set_trans_type(transaction::ObTransType::TRANS_SYSTEM);
  int64_t abs_timeout_us = 0;
  int64_t execution_id = 0;
  uint64_t stmt_id = res.result_set().get_statement_id();
  sql::stmt::StmtType stmt_type = res.result_set().get_stmt_type();
  const uint64_t* trace_id_val = ObCurTraceId::get();
  bool is_trace_id_init = true;
  ObQueryRetryInfo& retry_info = inner_session_.get_retry_info_for_update();
  if (0 == trace_id_val[0]) {
    is_trace_id_init = false;
    common::ObCurTraceId::init(observer::ObServer::get_instance().get_self());
  }

  // backup && restore worker/session timeout.
  TimeoutGuard timeout_guard(*this);

  // %vt_iter_factory may be NULL
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (NULL != ref_ctx_) {
    ret = OB_REF_NUM_NOT_ZERO;
    LOG_ERROR("connection still be referred by previous sql result, can not execute sql now", K(ret), K(sql));
  } else if (OB_FAIL(set_timeout(abs_timeout_us, is_from_pl))) {
    LOG_WARN("set timeout failed", K(ret));
  } else if (OB_ISNULL(ob_sql_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid sql engine", K(ret), K(ob_sql_));
  } else if (OB_UNLIKELY(retry_info.is_inited())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_ERROR("retry info is inited", K(ret), K(retry_info), K(sql));
  } else if (OB_FAIL(retry_info.init())) {
    LOG_WARN("fail to init retry info", K(ret), K(retry_info), K(sql));
  } else {
    execution_id = ob_sql_->get_execution_id();
    bool need_retry = true;
    for (int64_t retry_cnt = 0; need_retry; ++retry_cnt) {
      need_retry = false;
      retry_info.clear_state_before_each_retry();
      if (retry_cnt > 0) {  // reset result set
        res.~ObInnerSQLResult();
        new (&res) ObInnerSQLResult(get_session());
        ret = res.init();
        if (OB_SUCC(ret)) {
          res.result_set().set_ps_protocol();
          res.result_set().set_statement_id(stmt_id);
          res.result_set().set_stmt_type(stmt_type);
        }
      }

      ObWaitEventDesc max_wait_desc;
      ObWaitEventStat total_wait_desc;
      const bool enable_perf_event = lib::is_diagnose_info_enabled();
      const bool enable_sql_audit =
          GCONF.enable_sql_audit && get_session().get_local_ob_enable_sql_audit() && !is_from_pl;
      {
        ObMaxWaitGuard max_wait_guard(enable_perf_event ? &max_wait_desc : NULL);
        ObTotalWaitGuard total_wait_guard(enable_perf_event ? &total_wait_desc : NULL);

        if (enable_sql_audit) {
          exec_record.record_start();
        }

        const uint64_t tenant_id = get_session().get_effective_tenant_id();
        int ret_code = OB_SUCCESS;
        if (OB_FAIL(gctx.schema_service_->get_tenant_schema_guard(tenant_id, res.schema_guard_))) {
          LOG_WARN("get schema guard failed", K(ret));
        } else if (OB_FAIL(init_result(
                       res, vt_iter_factory, retry_cnt, res.schema_guard_, false, false, is_from_pl, is_dynamic))) {
          LOG_WARN("failed to init result", K(ret));
        } else if (OB_FAIL(do_execute(params, res))) {
          LOG_WARN("execute sql failed", K(ret), K(sql), K(retry_cnt));
          ret_code = ret;
          int tmp_ret = process_retry(res, ret, abs_timeout_us, need_retry, retry_cnt, is_from_pl);
          if (OB_SUCCESS != tmp_ret) {
            LOG_WARN("failed to process retry", K(tmp_ret), K(ret), K(sql), K(retry_cnt));
          }
          ret = tmp_ret;
          // moved here from ObInnerSQLConnection::do_execute() -> ObInnerSQLResult::open().
          int close_ret = res.force_close(need_retry);
          if (OB_SUCCESS != close_ret) {
            LOG_WARN("failed to close result", K(close_ret), K(ret), K(sql));
          }
        }
        ObSQLSessionInfo& session = res.result_set().get_session();
        session.set_session_in_retry(need_retry, ret_code);
        LOG_DEBUG("after process_retry",
            K(retry_cnt),
            K(ret),
            K(need_retry),
            K(inner_session_),
            KP(&inner_session_),
            K(inner_session_.get_is_in_retry()),
            K(session.get_is_in_retry_for_dup_tbl()),
            K(session),
            K(&session),
            K(session.get_is_in_retry()));

        execute_start_timestamp_ = res.get_execute_start_ts();
        execute_end_timestamp_ = res.get_execute_end_ts();
        if (enable_sql_audit) {
          exec_record.record_end();
        }
      }

      if (enable_sql_audit && res.is_inited()) {
        ObInnerSQLTimeRecord time_record(get_session());
        time_record.set_execute_start_timestamp(execute_start_timestamp_);
        time_record.set_execute_end_timestamp(execute_end_timestamp_);
        int record_ret = process_record(res,
            get_session(),
            time_record,
            ret,
            execution_id,
            OB_INVALID_ID,
            OB_INVALID_ID,
            max_wait_desc,
            total_wait_desc,
            exec_record,
            exec_timestamp);
        if (OB_SUCCESS != record_ret) {
          LOG_WARN("failed to process record", K(sql), K(record_ret), K(ret));
        }
      }

      if (get_session().get_in_transaction()) {
        if (ObStmt::is_dml_write_stmt(stmt_type)) {
          get_session().set_has_inner_dml_write(true);
        }
      }
    }
  }
  if (res.is_inited()) {
    int aret = process_final(sql, res, ret);
    if (OB_SUCCESS != aret) {
      LOG_WARN("failed to process final", K(sql), K(aret), K(ret));
    }
  }

  if (false == is_trace_id_init) {
    common::ObCurTraceId::reset();
  }

  retry_info.reset();

  return ret;
}

int ObInnerSQLConnection::start_transaction(bool with_snap_shot /* = false */)
{
  int ret = OB_SUCCESS;
  ObString sql;
  observer::ObReqTimeGuard req_timeinfo_guard;
  if (with_snap_shot) {
    sql = ObString::make_string("START TRANSACTION WITH CONSISTENT SNAPSHOT");
  } else {
    sql = ObString::make_string("START TRANSACTION");
  }
  ObSqlQueryExecutor executor(sql);
  SMART_VAR(ObInnerSQLResult, res, get_session())
  {
    if (OB_FAIL(res.init())) {
      LOG_WARN("init result set", K(ret));
    } else if (!inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("connection not inited", K(ret));
    } else if (OB_FAIL(query(executor, res))) {
      LOG_WARN("start transaction failed", K(ret), K(with_snap_shot));
    } else if (OB_FAIL(res.close())) {
      LOG_WARN("close result set failed", K(ret));
    }
  }

  return ret;
}

int ObInnerSQLConnection::rollback()
{
  int ret = OB_SUCCESS;
  observer::ObReqTimeGuard req_timeinfo_guard;
  ObSqlQueryExecutor executor("ROLLBACK");
  SMART_VAR(ObInnerSQLResult, res, get_session())
  {
    if (OB_FAIL(res.init())) {
      LOG_WARN("init result set", K(ret));
    } else if (!inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("connection not inited", K(ret));
    } else if (OB_FAIL(query(executor, res))) {
      LOG_WARN("rollback failed", K(ret));
    } else if (OB_FAIL(res.close())) {
      LOG_WARN("close result set failed", K(ret));
    }
  }

  return ret;
}

int ObInnerSQLConnection::commit()
{
  int ret = OB_SUCCESS;
  DEBUG_SYNC(BEFORE_INNER_SQL_COMMIT);
  observer::ObReqTimeGuard req_timeinfo_guard;
  ObSqlQueryExecutor executor("COMMIT");
  SMART_VAR(ObInnerSQLResult, res, get_session())
  {
    if (OB_FAIL(res.init())) {
      LOG_WARN("init result set", K(ret));
    } else if (!inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("connection not inited", K(ret));
    } else if (OB_FAIL(query(executor, res))) {
      LOG_WARN("commit failed", K(ret));
    } else if (OB_FAIL(res.close())) {
      LOG_WARN("close result set failed", K(ret));
    }
  }

  return ret;
}

int ObInnerSQLConnection::execute_write(
    const uint64_t tenant_id, const char* sql, int64_t& affected_rows, bool is_user_sql)
{
  int ret = OB_SUCCESS;
  observer::ObReqTimeGuard req_timeinfo_guard;
  ObSqlQueryExecutor executor(sql);
  SMART_VAR(ObInnerSQLResult, res, get_session())
  {
    if (OB_FAIL(res.init())) {
      LOG_WARN("init result set", K(ret));
    } else if (!inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("connection not inited", K(ret));
    } else if (NULL == sql || '\0' == *sql || OB_INVALID_ID == tenant_id) {
      ret = OB_INVALID_ARGUMENT;
      LOG_WARN("invalid argument", K(ret), K(sql), K(tenant_id));
    } else if (OB_FAIL(switch_tenant(tenant_id))) {
      LOG_WARN("switch tenant_id failed", K(ret), K(tenant_id));
    } else if (FALSE_IT(res.result_set().set_user_sql(is_user_sql))) {
    } else if (OB_FAIL(query(executor, res))) {
      LOG_WARN("execute sql failed", K(ret), K(tenant_id), K(sql));
    } else if (FALSE_IT(affected_rows = res.result_set().get_affected_rows())) {
    } else if (OB_FAIL(res.close())) {
      LOG_WARN("close result set failed", K(ret), K(tenant_id), K(sql));
    }
    primary_schema_versions_.reset();
    if (tenant_id < OB_MAX_RESERVED_TENANT_ID) {  // only print log for sys table
      LOG_INFO("execute write sql", K(ret), K(tenant_id), K(affected_rows), K(sql));
    }
  }
  return ret;
}

int ObInnerSQLConnection::execute_read(
    const uint64_t tenant_id, const char* sql, ObISQLClient::ReadResult& res, bool is_user_sql, bool is_from_pl)
{
  int ret = OB_SUCCESS;
  ObInnerSQLReadContext* read_ctx = NULL;
  observer::ObReqTimeGuard req_timeinfo_guard;
  const static int64_t ctx_size = sizeof(ObInnerSQLReadContext);
  static_assert(ctx_size <= ObISQLClient::ReadResult::BUF_SIZE, "buffer not enough");
  ObSqlQueryExecutor executor(sql);
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("connection not inited", K(ret));
  } else if (NULL == sql || '\0' == *sql || OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(sql), K(tenant_id));
  } else if (OB_FAIL(switch_tenant(tenant_id))) {
    LOG_WARN("switch tenant_id failed", K(ret), K(tenant_id));
  } else if (OB_FAIL(res.create_handler(read_ctx, *this))) {
    LOG_WARN("create result handler failed", K(ret));
  } else if (OB_FAIL(read_ctx->get_result().init())) {
    LOG_WARN("init result set", K(ret));
  } else if (FALSE_IT(read_ctx->get_result().result_set().set_user_sql(is_user_sql))) {
    // nothing.
  } else if (OB_FAIL(query(executor, read_ctx->get_result(), &read_ctx->get_vt_iter_factory(), is_from_pl))) {
    LOG_WARN("execute sql failed", K(ret), K(tenant_id), K(sql));
  }
  if (OB_SUCC(ret)) {
    ref_ctx_ = read_ctx;
  }
  return ret;
}

int ObInnerSQLConnection::execute(const uint64_t tenant_id, sqlclient::ObIExecutor& executor)
{
  int ret = OB_SUCCESS;
  observer::ObReqTimeGuard req_timeinfo_guard;
  SMART_VAR(ObInnerSQLResult, res, get_session())
  {
    if (OB_FAIL(res.init())) {
      LOG_WARN("init result set", K(ret));
    } else if (!inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("connection not inited", K(ret));
    } else if (OB_INVALID_ID == tenant_id) {
      ret = OB_INVALID_ARGUMENT;
      LOG_WARN("invalid argument", K(ret), K(tenant_id), K(executor));
    } else if (OB_FAIL(switch_tenant(tenant_id))) {
      LOG_WARN("switch tenant_id failed", K(ret), K(tenant_id));
    } else if (OB_FAIL(query(executor, res))) {
      LOG_WARN("executor execute failed", K(ret), K(tenant_id), K(executor));
    } else {
      share::CompatModeGuard g(get_compat_mode());
      WITH_CONTEXT(res.mem_context_)
      {
        if (OB_FAIL(executor.process_result(res.result_set()))) {
          LOG_WARN("process result failed", K(ret));
        } else {
          if (OB_FAIL(res.close())) {
            LOG_WARN("close result set failed", K(ret), K(tenant_id), K(executor));
          }
        }
      }
    }
    LOG_INFO("execute executor", K(ret), K(tenant_id), K(executor));
  }
  return ret;
}

int ObInnerSQLConnection::prepare(const uint64_t tenant_id, const ObString& sql, bool is_dynamic_sql, bool is_dbms_sql,
    bool is_cursor, ObISQLClient::ReadResult& res)
{
  int ret = OB_SUCCESS;
  ObInnerSQLReadContext* read_ctx = NULL;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("connection not inited", K(ret));
  } else if (NULL == sql || '\0' == *sql || OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(sql), K(tenant_id));
  } else if (OB_FAIL(switch_tenant(tenant_id))) {
    LOG_WARN("switch tenant_id failed", K(ret), K(tenant_id));
  } else if (OB_FAIL(res.create_handler(read_ctx, *this))) {
    LOG_WARN("create result handler failed", K(ret));
  } else if (OB_FAIL(read_ctx->get_result().init())) {
    LOG_WARN("init result set", K(ret));
  } else if (OB_FAIL(prepare(sql,
                 is_dynamic_sql,
                 is_dbms_sql,
                 is_cursor,
                 read_ctx->get_result(),
                 &read_ctx->get_vt_iter_factory()))) {
    LOG_WARN("execute sql failed", K(ret), K(tenant_id), K(sql));
  }
  if (OB_SUCC(ret)) {
    ref_ctx_ = read_ctx;
  }
  return ret;
}

int ObInnerSQLConnection::execute(const uint64_t tenant_id, const ObPsStmtId stmt_id, const stmt::StmtType stmt_type,
    ParamStore& params, ObISQLClient::ReadResult& res, bool is_from_pl, bool is_dynamic)
{
  int ret = OB_SUCCESS;
  ObInnerSQLReadContext* read_ctx = NULL;
  observer::ObReqTimeGuard req_timeinfo_guard;
  ObPsStmtInfoGuard ps_guard;
  ObPsStmtInfo* ps_info = NULL;
  ObPsCache* ps_cache = get_session().get_ps_cache();
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("connection not inited", K(ret));
  } else if (OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(stmt_id), K(tenant_id));
  } else if (OB_FAIL(switch_tenant(tenant_id))) {
    LOG_WARN("switch tenant_id failed", K(ret), K(tenant_id));
  } else if (OB_FAIL(res.create_handler(read_ctx, *this))) {
    LOG_WARN("create result handler failed", K(ret));
  } else if (OB_FAIL(read_ctx->get_result().init())) {
    LOG_WARN("init result set", K(ret));
  } else if (OB_ISNULL(ps_cache)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("ps cache is null", K(ret), K(ps_cache));
  } else if (OB_FAIL(ps_cache->get_stmt_info_guard(stmt_id, ps_guard))) {
    LOG_WARN("get stmt info guard failed", K(ret), K(stmt_id));
  } else if (OB_ISNULL(ps_info = ps_guard.get_stmt_info())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("get stmt info is null", K(ret), K(ps_info), K(stmt_id));
  } else {
    read_ctx->get_result().result_set().set_ps_protocol();
    read_ctx->get_result().result_set().set_statement_id(stmt_id);
    read_ctx->get_result().result_set().set_stmt_type(stmt_type);
    get_session().store_query_string(ps_info->get_ps_sql());

    if (OB_FAIL(execute(params, read_ctx->get_result(), &read_ctx->get_vt_iter_factory(), is_from_pl, is_dynamic))) {
      LOG_WARN("execute sql failed", K(ret), K(tenant_id), K(stmt_id));
    }
  }
  if (OB_SUCC(ret)) {
    ref_ctx_ = read_ctx;
  }
  return ret;
}

int ObInnerSQLConnection::switch_tenant(const uint64_t tenant_id)
{
  int ret = OB_SUCCESS;
  // called in init(), can not check inited_ flag.
  if (OB_INVALID_ID == tenant_id) {
    LOG_WARN("invalid argument", K(ret), K(tenant_id));
  } else if (is_inner_session()) {
    if (OB_FAIL(get_session().switch_tenant(tenant_id))) {
      LOG_WARN("Init sys tenant in session error", K(ret));
    } else if (OB_FAIL(
                   get_session().set_user(OB_SYS_USER_NAME, OB_SYS_HOST_NAME, combine_id(tenant_id, OB_SYS_USER_ID)))) {
      LOG_WARN("Set sys user in session error", K(ret));
    } else {
      get_session().set_user_priv_set(OB_PRIV_ALL | OB_PRIV_GRANT);
      get_session().set_database_id(combine_id(tenant_id, OB_SYS_DATABASE_ID));
    }
  } else { /*do nothing*/
  }
  return ret;
}

int ObInnerSQLConnection::set_timeout(int64_t& abs_timeout_us, bool is_from_pl)
{
  int ret = OB_SUCCESS;
  const ObTimeoutCtx& ctx = ObTimeoutCtx::get_ctx();
  const int64_t now = ObTimeUtility::current_time();
  int64_t timeout = 0;
  int64_t trx_timeout = 0;
  abs_timeout_us = 0;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  }

  if (OB_SUCC(ret)) {
    if (THIS_WORKER.is_timeout()) {
      ret = OB_TIMEOUT;
      LOG_WARN("already timeout", K(ret), K(abs_timeout_us), K(THIS_WORKER.get_timeout_ts()));
    } else {
      if (THIS_WORKER.get_timeout_remain() < OB_MAX_USER_SPECIFIED_TIMEOUT) {
        timeout = THIS_WORKER.get_timeout_remain();
        abs_timeout_us = THIS_WORKER.get_timeout_ts();
        LOG_DEBUG("set timeout by worker", K(timeout), K(abs_timeout_us));
        trx_timeout = timeout;
        LOG_DEBUG("set timeout according to THIS_WORKER", K(timeout), K(trx_timeout), K(abs_timeout_us));
      }
    }
  }

  if (OB_SUCC(ret)) {
    if (ctx.is_timeout_set()) {
      if (ctx.get_abs_timeout() < abs_timeout_us || 0 == abs_timeout_us) {
        abs_timeout_us = ctx.get_abs_timeout();
        timeout = ctx.get_timeout();
        trx_timeout = timeout;
      }
      if (ctx.is_trx_timeout_set()) {
        trx_timeout = ctx.get_trx_timeout_us();
      }
      if (timeout <= 0) {
        ret = OB_TIMEOUT;
        LOG_WARN("already timeout", K(ret), K(ctx), K(abs_timeout_us));
      }
    }
#if !defined(NDEBUG)
    LOG_DEBUG("set timeout according to time_ctx", K(timeout), K(trx_timeout), K(abs_timeout_us));
#endif
  }

  if (OB_SUCC(ret)) {
    if (0 == abs_timeout_us) {
      timeout = GCONF.internal_sql_execute_timeout;
      trx_timeout = timeout;
      abs_timeout_us = now + timeout;
    }
  }

  // no need to set session timeout for outer session if no timeout ctx
  if (OB_SUCC(ret) && (is_inner_session() || ctx.is_timeout_set() || ctx.is_trx_timeout_set() || is_from_pl)) {
    if (!is_from_pl) {
      if (OB_FAIL(set_session_timeout(timeout, trx_timeout))) {
        LOG_WARN("set session timeout failed", K(timeout), K(trx_timeout), K(ret));
      } else {
        THIS_WORKER.set_timeout_ts(get_session().get_query_start_time() + timeout);
      }
    } else {
      int64_t query_timeout;
      OZ(get_session().get_query_timeout(query_timeout));
      OX(abs_timeout_us = get_session().get_query_start_time() > 0
                              ? get_session().get_query_start_time() + query_timeout
                              : ObTimeUtility::current_time() + query_timeout);
      if (OB_SUCC(ret) && THIS_WORKER.get_timeout_ts() > abs_timeout_us) {
        OX(THIS_WORKER.set_timeout_ts(abs_timeout_us));
      }
    }
  }
  return ret;
}

int ObInnerSQLConnection::set_session_timeout(int64_t query_timeout, int64_t trx_timeout)
{
  int ret = OB_SUCCESS;
  if (OB_SUCC(ret)) {
    LOG_DEBUG("set query timeout", K(query_timeout));
    ObObj val;
    val.set_int(query_timeout);
    if (OB_FAIL(get_session().update_sys_variable(SYS_VAR_OB_QUERY_TIMEOUT, val))) {
      LOG_WARN("set sys variable failed", K(ret), K(OB_SV_QUERY_TIMEOUT), K(val));
    }
  }
  if (OB_SUCC(ret)) {
    LOG_DEBUG("set trx timeout", K(trx_timeout));
    ObObj val;
    val.set_int(trx_timeout);
    if (OB_FAIL(get_session().update_sys_variable(SYS_VAR_OB_TRX_TIMEOUT, val))) {
      LOG_WARN("set sys variable failed", K(ret), K(OB_SV_TRX_TIMEOUT), K(val));
    }
  }

  return ret;
}

void ObInnerSQLConnection::dump_conn_bt_info()
{
  const int64_t BUF_SIZE = (1LL << 10);
  char buf_bt[BUF_SIZE];
  buf_bt[0] = '\0';
  char buf_time[OB_MAX_TIMESTAMP_LENGTH];
  int64_t pos = 0;
  (void)ObTimeUtility2::usec_to_str(init_timestamp_, buf_time, OB_MAX_TIMESTAMP_LENGTH, pos);
  pos = 0;
  for (int i = 0; i < bt_size_; ++i) {
    if (OB_UNLIKELY(pos + 1 > BUF_SIZE)) {
      LOG_WARN("buf is not large enough", K(pos), K(BUF_SIZE));
    } else {
      (void)databuff_printf(buf_bt, BUF_SIZE, pos, "%p ", bt_addrs_[i]);
    }
  }
  LOG_WARN("dump inner sql connection backtrace", "tid", tid_, "init time", buf_time, "backtrace", buf_bt);
}

void ObInnerSQLConnection::record_stat(sql::ObSQLSessionInfo& session, const stmt::StmtType type, bool is_from_pl)
{
#define ADD_STMT_STAT(type)                        \
  if (is_from_pl) {                                \
    EVENT_INC(SQL_##type##_COUNT);                 \
    EVENT_ADD(SQL_##type##_TIME, time_cost);       \
  } else {                                         \
    EVENT_INC(SQL_INNER_##type##_COUNT);           \
    EVENT_ADD(SQL_INNER_##type##_TIME, time_cost); \
  }

#define ADD_CASE(type)   \
  case stmt::T_##type:   \
    ADD_STMT_STAT(type); \
    break

  if (lib::is_diagnose_info_enabled()) {
    const int64_t now = ObTimeUtility::current_time();
    const int64_t time_cost = now - session.get_query_start_time();
    switch (type) {
      ADD_CASE(SELECT);
      ADD_CASE(INSERT);
      ADD_CASE(REPLACE);
      ADD_CASE(UPDATE);
      ADD_CASE(DELETE);
      default: {
        ADD_STMT_STAT(OTHER);
      }
    }
  }
#undef ADD_STMT_STAT
#undef ADD_CASE
}

int ObInnerSQLConnection::get_session_variable(const ObString& name, int64_t& val)
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (0 == name.case_compare("ob_read_snapshot_version")) {  // fake system variable
    val = get_session().get_read_snapshot_version();
  } else if (0 == name.case_compare("tx_isolation")) {
    ObObj obj;
    if (OB_FAIL(get_session().get_sys_variable_by_name(name, obj))) {
      LOG_WARN("get tx_isolation system variable value fail", K(ret), K(name));
    } else {
      val = transaction::ObTransIsolation::get_level(obj.get_string());
    }
  } else {
    ret = get_session().get_sys_variable_by_name(name, val);
  }
  return ret;
}

int ObInnerSQLConnection::set_session_variable(const ObString& name, int64_t val)
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (0 == name.case_compare("ob_read_snapshot_version")) {  // fake system variable
    if (val > 0) {
      LOG_INFO("inner sql with read snapshot version", K(name), K(val));
    }
    (void)get_session().set_read_snapshot_version(val);
  } else if (0 == name.case_compare("ob_check_sys_variable")) {  // fake system variable
    if (0 == val) {
      LOG_INFO("disable inner sql check sys variable");
    }
    (void)get_session().set_check_sys_variable(0 != val);
  } else if (0 == name.case_compare("tx_isolation")) {
    ObObj obj;
    obj.set_varchar(transaction::ObTransIsolation::get_name(val));
    if (OB_FAIL(get_session().update_sys_variable_by_name(name, obj))) {
      LOG_WARN("update sys variable by name fail", K(ret), K(name), K(obj), K(name), K(val));
    }
  } else if (OB_FAIL(get_session().update_sys_variable_by_name(name, val))) {
    LOG_WARN("failed to update sys variable", K(ret), K(name), K(val));
  } else if (0 == name.case_compare("ob_read_consistency")) {
    LOG_INFO("YZFDEBUG inner session use weak consitency", K(val), "inner_connection_p", this);
  }
  return ret;
}

ObWorker::CompatMode ObInnerSQLConnection::get_compat_mode() const
{
  ObWorker::CompatMode mode;
  if (is_oracle_compat_mode()) {
    mode = ObWorker::CompatMode::ORACLE;
  } else {
    mode = ObWorker::CompatMode::MYSQL;
  }
  return mode;
}

// nested session and sql execute for foreign key.

int ObInnerSQLConnection::begin_nested_session(
    ObSQLSessionInfo::StmtSavedValue& saved_session, SavedValue& saved_conn, bool skip_cur_stmt_tables)
{
  int ret = OB_SUCCESS;
  if (!is_extern_session()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("connection is not extern session", K(ret));
  } else if (OB_FAIL(extern_session_->begin_nested_session(saved_session, skip_cur_stmt_tables))) {
    LOG_WARN("failed to begin nested session", K(ret));
  } else {
    saved_conn.ref_ctx_ = ref_ctx_;
    saved_conn.execute_start_timestamp_ = execute_start_timestamp_;
    saved_conn.execute_end_timestamp_ = execute_end_timestamp_;
  }
  return ret;
}

int ObInnerSQLConnection::end_nested_session(ObSQLSessionInfo::StmtSavedValue& saved_session, SavedValue& saved_conn)
{
  int ret = OB_SUCCESS;
  if (!is_extern_session()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("connection is not extern session", K(ret));
  } else if (OB_FAIL(extern_session_->end_nested_session(saved_session))) {
    LOG_WARN("failed to end nested session", K(ret));
  } else {
    ref_ctx_ = saved_conn.ref_ctx_;
    execute_start_timestamp_ = saved_conn.execute_start_timestamp_;
    execute_end_timestamp_ = saved_conn.execute_end_timestamp_;
    saved_conn.reset();
  }
  return ret;
}

int ObInnerSQLConnection::set_foreign_key_cascade(bool is_cascade)
{
  int ret = OB_SUCCESS;
  OV(is_extern_session());
  OX(extern_session_->set_foreign_key_casecade(is_cascade));
  return ret;
}

int ObInnerSQLConnection::get_foreign_key_cascade(bool& is_cascade) const
{
  int ret = OB_SUCCESS;
  OV(is_extern_session());
  OX(is_cascade = extern_session_->is_foreign_key_cascade());
  return ret;
}

int ObInnerSQLConnection::set_foreign_key_check_exist(bool is_check_exist)
{
  int ret = OB_SUCCESS;
  OV(is_extern_session());
  OX(extern_session_->set_foreign_key_check_exist(is_check_exist));
  return ret;
}

int ObInnerSQLConnection::get_foreign_key_check_exist(bool& is_check_exist) const
{
  int ret = OB_SUCCESS;
  OV(is_extern_session());
  OX(is_check_exist = extern_session_->is_foreign_key_check_exist());
  return ret;
}

int ObInnerSQLConnection::set_primary_schema_version(const common::ObIArray<int64_t>& primary_schema_versions)
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(primary_schema_versions_.assign(primary_schema_versions))) {
    LOG_WARN("failed to copy assign schema version", K(ret), K(primary_schema_versions), K(primary_schema_versions_));
  }
  return ret;
}
}  // end of namespace observer
}  // end of namespace oceanbase