ob_table_redefinition_task.cpp 55.3 KB
Newer Older
W
wangzelin.wzl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**
 * 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 RS
#include "ob_table_redefinition_task.h"
#include "lib/rc/context.h"
#include "share/schema/ob_multi_version_schema_service.h"
#include "share/ob_ddl_error_message_table_operator.h"
#include "share/ob_autoincrement_service.h"
#include "share/ob_ddl_checksum.h" 
#include "rootserver/ddl_task/ob_ddl_scheduler.h"
#include "rootserver/ob_root_service.h"
#include "rootserver/ddl_task/ob_ddl_redefinition_task.h"
#include "storage/tablelock/ob_table_lock_service.h"
O
obdev 已提交
24
#include "observer/ob_server_event_history_table_operator.h"
25 26 27
#include "sql/engine/cmd/ob_load_data_utils.h"
#include "lib/mysqlclient/ob_mysql_proxy.h"
#include "lib/mysqlclient/ob_mysql_result.h"
W
wangzelin.wzl 已提交
28 29 30 31 32 33 34 35 36

using namespace oceanbase::lib;
using namespace oceanbase::common;
using namespace oceanbase::share;
using namespace oceanbase::share::schema;
using namespace oceanbase::rootserver;
using namespace oceanbase::obrpc;

ObTableRedefinitionTask::ObTableRedefinitionTask()
O
obdev 已提交
37 38
  : ObDDLRedefinitionTask(ObDDLType::DDL_TABLE_REDEFINITION), has_rebuild_index_(false), has_rebuild_constraint_(false), has_rebuild_foreign_key_(false), allocator_(lib::ObLabel("RedefTask")),
    is_copy_indexes_(true), is_copy_triggers_(true), is_copy_constraints_(true), is_copy_foreign_keys_(true), is_ignore_errors_(false), is_do_finish_(false)
W
wangzelin.wzl 已提交
39 40 41 42 43 44 45 46 47 48 49 50
{
}

ObTableRedefinitionTask::~ObTableRedefinitionTask()
{
}

int ObTableRedefinitionTask::init(const uint64_t tenant_id, const int64_t task_id, const share::ObDDLType &ddl_type,
    const int64_t data_table_id, const int64_t dest_table_id, const int64_t schema_version, const int64_t parallelism,
    const ObAlterTableArg &alter_table_arg, const int64_t task_status, const int64_t snapshot_version)
{
  int ret = OB_SUCCESS;
51
  uint64_t tenant_data_format_version = 0;
W
wangzelin.wzl 已提交
52 53 54 55 56 57 58 59 60 61 62
  if (OB_UNLIKELY(is_inited_)) {
    ret = OB_INIT_TWICE;
    LOG_WARN("ObTableRedefinitionTask has already been inited", K(ret));
  } else if (OB_UNLIKELY(OB_INVALID_ID == tenant_id || OB_INVALID_ID == data_table_id || OB_INVALID_ID == dest_table_id || schema_version <= 0 || task_status < ObDDLTaskStatus::PREPARE
      || task_status > ObDDLTaskStatus::SUCCESS || snapshot_version < 0 || (snapshot_version > 0 && task_status < ObDDLTaskStatus::WAIT_TRANS_END))) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid arguments", K(ret), K(tenant_id), K(task_id), K(data_table_id), K(dest_table_id), K(schema_version), K(task_status), K(snapshot_version));
  } else if (OB_FAIL(deep_copy_table_arg(allocator_, alter_table_arg, alter_table_arg_))) {
    LOG_WARN("deep copy alter table arg failed", K(ret));
  } else if (OB_FAIL(set_ddl_stmt_str(alter_table_arg_.ddl_stmt_str_))) {
    LOG_WARN("set ddl stmt str failed", K(ret));
S
simonjoylet 已提交
63
  } else if (OB_FAIL(ObShareUtil::fetch_current_data_version(*GCTX.sql_proxy_, tenant_id, tenant_data_format_version))) {
64
    LOG_WARN("get min data version failed", K(ret), K(tenant_id));
W
wangzelin.wzl 已提交
65 66 67 68 69 70 71 72 73 74 75
  } else {
    task_type_ = ddl_type;
    object_id_ = data_table_id;
    target_object_id_ = dest_table_id;
    schema_version_ = schema_version;
    task_status_ = static_cast<ObDDLTaskStatus>(task_status);
    snapshot_version_ = snapshot_version;
    tenant_id_ = tenant_id;
    task_version_ = OB_TABLE_REDEFINITION_TASK_VERSION;
    task_id_ = task_id;
    parallelism_ = parallelism;
76
    data_format_version_ = tenant_data_format_version;
W
wangzelin.wzl 已提交
77
    alter_table_arg_.exec_tenant_id_ = tenant_id_;
78
    start_time_ = ObTimeUtility::current_time();
79 80 81 82
    if (OB_FAIL(init_ddl_task_monitor_info(&alter_table_arg_.alter_table_schema_))) {
      LOG_WARN("init ddl task monitor info failed", K(ret));
    } else {
      is_inited_ = true;
O
obdev 已提交
83
      ddl_tracing_.open();
84
    }
W
wangzelin.wzl 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  }
  return ret;
}

int ObTableRedefinitionTask::init(const ObDDLTaskRecord &task_record)
{
  int ret = OB_SUCCESS;
  const uint64_t data_table_id = task_record.object_id_;
  const uint64_t dest_table_id = task_record.target_object_id_;
  const int64_t schema_version = task_record.schema_version_;
  int64_t pos = 0;
  if (OB_UNLIKELY(is_inited_)) {
    ret = OB_INIT_TWICE;
    LOG_WARN("ObTableRedefinitionTask has already been inited", K(ret));
  } else if (!task_record.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid arguments", K(ret), K(task_record));
102
  } else if (OB_FAIL(deserlize_params_from_message(task_record.tenant_id_, task_record.message_.ptr(), task_record.message_.length(), pos))) {
O
obdev 已提交
103
    LOG_WARN("deserialize params from message failed", K(ret), K(task_record.message_), K(common::lbt()));
W
wangzelin.wzl 已提交
104 105 106 107 108 109 110 111 112 113
  } else if (OB_FAIL(set_ddl_stmt_str(task_record.ddl_stmt_str_))) {
    LOG_WARN("set ddl stmt str failed", K(ret));
  } else {
    task_id_ = task_record.task_id_;
    task_type_ = task_record.ddl_type_;
    object_id_ = data_table_id;
    target_object_id_ = dest_table_id;
    schema_version_ = schema_version;
    task_status_ = static_cast<ObDDLTaskStatus>(task_record.task_status_);
    snapshot_version_ = task_record.snapshot_version_;
O
obdev 已提交
114
    execution_id_ = task_record.execution_id_;
W
wangzelin.wzl 已提交
115
    tenant_id_ = task_record.tenant_id_;
O
obdev 已提交
116
    ret_code_ = task_record.ret_code_;
W
wangzelin.wzl 已提交
117
    alter_table_arg_.exec_tenant_id_ = tenant_id_;
118
    start_time_ = ObTimeUtility::current_time();
119 120 121 122 123

    if (OB_FAIL(init_ddl_task_monitor_info(&alter_table_arg_.alter_table_schema_))) {
      LOG_WARN("init ddl task monitor info failed", K(ret));
    } else {
      is_inited_ = true;
O
obdev 已提交
124 125 126

      // set up span during recover task
      ddl_tracing_.open_for_recovery();
127
    }
W
wangzelin.wzl 已提交
128 129 130 131
  }
  return ret;
}

132 133 134
int ObTableRedefinitionTask::update_complete_sstable_job_status(const common::ObTabletID &tablet_id,
                                                                const int64_t snapshot_version,
                                                                const int64_t execution_id,
135 136
                                                                const int ret_code,
                                                                const ObDDLTaskInfo &addition_info)
W
wangzelin.wzl 已提交
137 138 139 140
{
  int ret = OB_SUCCESS;
  TCWLockGuard guard(lock_);
  UNUSED(tablet_id);
141
  UNUSED(addition_info);
W
wangzelin.wzl 已提交
142 143 144 145 146 147
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObTableRedefinitionTask has not been inited", K(ret));
  } else if (ObDDLTaskStatus::CHECK_TABLE_EMPTY == task_status_) {
    check_table_empty_job_ret_code_ = ret_code;
  } else {
O
obdev 已提交
148 149 150 151 152 153 154 155 156 157 158
    switch(task_type_) {
      case ObDDLType::DDL_DIRECT_LOAD: {
        complete_sstable_job_ret_code_ = ret_code;
        LOG_INFO("table redefinition task callback", K(complete_sstable_job_ret_code_));
        break;
      }
      default : {
        if (OB_UNLIKELY(snapshot_version_ != snapshot_version)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("error unexpected, snapshot version is not equal", K(ret), K(snapshot_version_), K(snapshot_version));
        } else if (execution_id < execution_id_) {
H
Hongqin-Li 已提交
159 160
          ret = OB_TASK_EXPIRED;
          LOG_WARN("receive a mismatch execution result, ignore", K(ret_code), K(execution_id), K(execution_id_));
O
obdev 已提交
161 162 163 164 165 166 167 168
        } else {
          complete_sstable_job_ret_code_ = ret_code;
          execution_id_ = execution_id; // update ObTableRedefinitionTask::execution_id_ from ObDDLRedefinitionSSTableBuildTask::execution_id_
          LOG_INFO("table redefinition task callback", K(complete_sstable_job_ret_code_), K(execution_id_));
        }
        break;
      }
    }
W
wangzelin.wzl 已提交
169 170 171 172 173
  }
  return ret;
}

int ObTableRedefinitionTask::send_build_replica_request()
O
obdev 已提交
174 175 176 177 178 179 180 181
{
  int ret = OB_SUCCESS;
  switch (task_type_) {
    case DDL_DIRECT_LOAD: {
      // do nothing
      break;
    }
    default: {
O
obdev 已提交
182
      if (OB_FAIL(send_build_replica_request_by_sql())) {
O
obdev 已提交
183 184 185 186 187 188 189 190 191
        LOG_WARN("failed to send build replica request", K(ret));
      }
      break;
    }
  }
  return ret;
}

int ObTableRedefinitionTask::send_build_replica_request_by_sql()
W
wangzelin.wzl 已提交
192 193 194 195 196
{
  int ret = OB_SUCCESS;
  bool modify_autoinc = false;
  bool use_heap_table_ddl_plan = false;
  ObRootService *root_service = GCTX.root_service_;
H
Hongqin-Li 已提交
197
  int64_t new_execution_id = 0;
W
wangzelin.wzl 已提交
198 199 200 201 202 203 204
  if (OB_ISNULL(root_service)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("error unexpected, root service must not be nullptr", K(ret));
  } else if (OB_FAIL(check_modify_autoinc(modify_autoinc))) {
    LOG_WARN("failed to check modify autoinc", K(ret));
  } else if (OB_FAIL(check_use_heap_table_ddl_plan(use_heap_table_ddl_plan))) {
    LOG_WARN("fail to check heap table ddl plan", K(ret));
H
Hongqin-Li 已提交
205 206
  } else if (OB_FAIL(ObDDLTask::push_execution_id(tenant_id_, task_id_, new_execution_id))) {
    LOG_WARN("failed to fetch new execution id", K(ret));
W
wangzelin.wzl 已提交
207 208 209 210 211
  } else {
    ObSQLMode sql_mode = alter_table_arg_.sql_mode_;
    if (!modify_autoinc) {
      sql_mode = sql_mode | SMO_NO_AUTO_VALUE_ON_ZERO;
    }
212 213 214 215 216 217 218
    // get execute inner sql addr
    if (OB_FAIL(ObDDLUtil::get_sys_ls_leader_addr(GCONF.cluster_id, tenant_id_, alter_table_arg_.inner_sql_exec_addr_))) {
      LOG_WARN("get sys ls leader addr fail", K(ret), K(tenant_id_));
      ret = OB_SUCCESS; // ignore ret
    } else {
      set_sql_exec_addr(alter_table_arg_.inner_sql_exec_addr_); // set to switch_status, if task cancel, we should kill session with inner_sql_exec_addr_
    }
W
wangzelin.wzl 已提交
219 220 221 222 223 224 225 226 227
    ObSchemaGetterGuard schema_guard;
    const ObTableSchema *orig_table_schema = nullptr;
    ObDDLRedefinitionSSTableBuildTask task(
        task_id_,
        tenant_id_,
        object_id_,
        target_object_id_,
        schema_version_,
        snapshot_version_,
H
Hongqin-Li 已提交
228
        new_execution_id,
W
wangzelin.wzl 已提交
229 230 231 232
        sql_mode,
        trace_id_,
        parallelism_,
        use_heap_table_ddl_plan,
233 234
        GCTX.root_service_,
        alter_table_arg_.inner_sql_exec_addr_);
W
wangzelin.wzl 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    if (OB_FAIL(root_service->get_ddl_service().get_tenant_schema_guard_with_version_in_inner_table(tenant_id_, schema_guard))) {
      LOG_WARN("get schema guard failed", K(ret));
    } else if (OB_FAIL(schema_guard.get_table_schema(tenant_id_, object_id_, orig_table_schema))) {
      LOG_WARN("failed to get orig table schema", K(ret));
    } else if (OB_FAIL(task.init(*orig_table_schema, alter_table_arg_.alter_table_schema_, alter_table_arg_.tz_info_wrap_))) {
      LOG_WARN("fail to init table redefinition sstable build task", K(ret));
    } else if (OB_FAIL(root_service->submit_ddl_single_replica_build_task(task))) {
      LOG_WARN("fail to submit ddl build single replica", K(ret));
    }
  }
  return ret;
}

int ObTableRedefinitionTask::check_build_replica_timeout()
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObTableRedefinitionTask has not been inited", K(ret));
  } else if (build_replica_request_time_ > 0) {
O
obdev 已提交
255
    const int64_t timeout = OB_MAX_DDL_SINGLE_REPLICA_BUILD_TIMEOUT;
W
wangzelin.wzl 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    const int64_t current_time = ObTimeUtility::current_time();
    if (build_replica_request_time_ + timeout < current_time) {
      ret = OB_TIMEOUT;
    }
  }
  return ret;
}

int ObTableRedefinitionTask::check_build_replica_end(bool &is_end)
{
  int ret = OB_SUCCESS;
  if (INT64_MAX == complete_sstable_job_ret_code_) {
    // not complete
  } else if (OB_SUCCESS != complete_sstable_job_ret_code_) {
    ret_code_ = complete_sstable_job_ret_code_;
    is_end = true;
    LOG_WARN("complete sstable job failed", K(ret_code_), K(object_id_), K(target_object_id_));
O
obdev 已提交
273
    if (is_replica_build_need_retry(ret_code_)) {
W
wangzelin.wzl 已提交
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
      build_replica_request_time_ = 0;
      complete_sstable_job_ret_code_ = INT64_MAX;
      ret_code_ = OB_SUCCESS;
      is_end = false;
      LOG_INFO("ddl need retry", K(*this));
    }
  } else {
    is_end = true;
    ret_code_ = complete_sstable_job_ret_code_;
  }
  return ret;
}

int ObTableRedefinitionTask::check_use_heap_table_ddl_plan(bool &use_heap_table_ddl_plan)
{
  int ret = OB_SUCCESS;
  use_heap_table_ddl_plan = false;
  ObSchemaGetterGuard schema_guard;
  const ObTableSchema *target_table_schema = nullptr;
  ObRootService *root_service = GCTX.root_service_;
  if (OB_ISNULL(root_service)) {
    ret = OB_ERR_SYS;
    LOG_WARN("error sys, root service must not be nullptr", K(ret));
  } else if (OB_FAIL(root_service->get_ddl_service()
                      .get_tenant_schema_guard_with_version_in_inner_table(tenant_id_, schema_guard))) {
    LOG_WARN("get schema guard failed", K(ret));
  } else if (OB_FAIL(schema_guard.get_table_schema(tenant_id_, target_object_id_, target_table_schema))) {
    LOG_WARN("fail to get table schema", K(ret), K(target_object_id_));
  } else if (OB_ISNULL(target_table_schema)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("error unexpected, table schema must not be nullptr", K(ret), K(target_object_id_));
  } else if (target_table_schema->is_heap_table() && 
    (DDL_ALTER_PARTITION_BY == task_type_ || DDL_DROP_PRIMARY_KEY == task_type_)) {
    use_heap_table_ddl_plan = true;
  }
  return ret;
}

int ObTableRedefinitionTask::table_redefinition(const ObDDLTaskStatus next_task_status)
{
  int ret = OB_SUCCESS;
  bool is_build_replica_end = false;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObTableRedefinitionTask has not been inited", K(ret));
O
obdev 已提交
319 320 321 322
  } else if (OB_UNLIKELY(snapshot_version_ <= 0)) {
    is_build_replica_end = true; // switch to fail.
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("unexpected snapshot", K(ret), KPC(this));
W
wangzelin.wzl 已提交
323 324 325
  }

  if (OB_SUCC(ret) && !is_build_replica_end && 0 == build_replica_request_time_) {
326 327 328 329 330 331 332 333
    bool need_exec_new_inner_sql = false;
    if (OB_FAIL(reap_old_replica_build_task(need_exec_new_inner_sql))) {
      if (OB_EAGAIN == ret) {
        ret = OB_SUCCESS; // retry
      } else {
        LOG_WARN("failed to reap old task", K(ret));
      }
    } else if (!need_exec_new_inner_sql) {
H
Hongqin-Li 已提交
334 335
      is_build_replica_end = true;
    } else if (OB_FAIL(send_build_replica_request())) {
W
wangzelin.wzl 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      LOG_WARN("fail to send build replica request", K(ret));
    } else {
      build_replica_request_time_ = ObTimeUtility::current_time();
    }
  }
  DEBUG_SYNC(TABLE_REDEFINITION_REPLICA_BUILD);
  if (OB_SUCC(ret) && !is_build_replica_end) {
    if (OB_FAIL(check_build_replica_end(is_build_replica_end))) {
      LOG_WARN("check build replica end failed", K(ret));
    }
  }
  if (OB_SUCC(ret) && !is_build_replica_end) {
    if (OB_FAIL(check_build_replica_timeout())) {
      LOG_WARN("fail to check build replica timeout", K(ret));
    }
  }

  // overwrite ret
  if (is_build_replica_end) {
    ret = complete_sstable_job_ret_code_;
    if (OB_SUCC(ret)) {
O
obdev 已提交
357 358
      if (OB_FAIL(replica_end_check(ret))) {
        LOG_WARN("fail to check", K(ret));
W
wangzelin.wzl 已提交
359 360
      }
    }
O
obdev 已提交
361
    if (OB_FAIL(switch_status(next_task_status, true, ret))) {
W
wangzelin.wzl 已提交
362 363 364 365 366 367
      LOG_WARN("fail to switch task status", K(ret));
    }
  }
  return ret;
}

O
obdev 已提交
368 369 370 371 372 373 374 375
int ObTableRedefinitionTask::replica_end_check(const int ret_code)
{
  int ret = OB_SUCCESS;
  switch(task_type_) {
    case DDL_DIRECT_LOAD : {
      break;
    }
    default : {
H
Hongqin-Li 已提交
376
      if (OB_FAIL(check_data_dest_tables_columns_checksum(get_execution_id()))) {
O
obdev 已提交
377 378 379 380 381 382 383 384
        LOG_WARN("fail to check the columns checksum of data table and destination table", K(ret));
      }
      break;
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
int ObTableRedefinitionTask::copy_table_indexes()
{
  int ret = OB_SUCCESS;
  ObRootService *root_service = GCTX.root_service_;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObTableRedefinitionTask has not been inited", K(ret));
  } else if (OB_ISNULL(root_service)) {
    ret = OB_ERR_SYS;
    LOG_WARN("error sys, root service must not be nullptr", K(ret));
  } else {
    // check if has rebuild index
    if (has_rebuild_index_) {
    } else {
      ObSchemaGetterGuard schema_guard;
      const ObTableSchema *table_schema = nullptr;
      ObSArray<uint64_t> index_ids;
      alter_table_arg_.ddl_task_type_ = share::REBUILD_INDEX_TASK;
      alter_table_arg_.table_id_ = object_id_;
      alter_table_arg_.hidden_table_id_ = target_object_id_;
405
      alter_table_arg_.alter_table_schema_.set_tenant_id(tenant_id_);
W
wangzelin.wzl 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
      if (OB_FAIL(root_service->get_ddl_service().get_tenant_schema_guard_with_version_in_inner_table(tenant_id_, schema_guard))) {
        LOG_WARN("get schema guard failed", K(ret));
      } else if (OB_FAIL(schema_guard.get_table_schema(tenant_id_, target_object_id_, table_schema))) {
        LOG_WARN("get table schema failed", K(ret), K(tenant_id_), K(target_object_id_));
      } else if (OB_ISNULL(table_schema)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("error unexpected, table schema must not be nullptr", K(ret), K(target_object_id_));
      } else {
        const common::ObIArray<ObAuxTableMetaInfo> &index_infos = table_schema->get_simple_index_infos();
        if (index_infos.count() > 0) {
          // if there is indexes in new tables, if so, the indexes is already rebuilt in new table
          for (int64_t i = 0; OB_SUCC(ret) && i < index_infos.count(); ++i) {
            if (OB_FAIL(index_ids.push_back(index_infos.at(i).table_id_))) {
              LOG_WARN("push back index id failed", K(ret));
            }
          }
          LOG_INFO("indexes schema are already built", K(index_ids));
        } else {
          // if there is no indexes in new tables, we need to rebuild indexes in new table
O
obdev 已提交
425 426 427 428 429 430 431
          int64_t ddl_rpc_timeout = 0;
          int64_t all_tablet_count = 0;
          if (OB_FAIL(get_orig_all_index_tablet_count(schema_guard, all_tablet_count))) {
            LOG_WARN("get all tablet count failed", K(ret));
          } else if (OB_FAIL(ObDDLUtil::get_ddl_rpc_timeout(all_tablet_count, ddl_rpc_timeout))) {
            LOG_WARN("get ddl rpc timeout failed", K(ret));
          } else if (OB_FAIL(root_service->get_ddl_service().get_common_rpc()->to(obrpc::ObRpcProxy::myaddr_).timeout(ddl_rpc_timeout).
W
wangzelin.wzl 已提交
432
                execute_ddl_task(alter_table_arg_, index_ids))) {
O
obdev 已提交
433
            LOG_WARN("rebuild hidden table index failed", K(ret), K(ddl_rpc_timeout));
W
wangzelin.wzl 已提交
434 435 436 437 438
          }
        }
      }
      DEBUG_SYNC(TABLE_REDEFINITION_COPY_TABLE_INDEXES);
      if (OB_SUCC(ret) && index_ids.count() > 0) {
O
obdev 已提交
439 440 441 442
        ObSchemaGetterGuard new_schema_guard;
        if (OB_FAIL(root_service->get_ddl_service().get_tenant_schema_guard_with_version_in_inner_table(tenant_id_, new_schema_guard))) {
          LOG_WARN("failed to refresh schema guard", K(ret));
        }
W
wangzelin.wzl 已提交
443 444 445 446 447 448 449 450 451 452
        for (int64_t i = 0; OB_SUCC(ret) && i < index_ids.count(); ++i) {
          const uint64_t index_id = index_ids.at(i);	
          const ObTableSchema *index_schema = nullptr;
          ObDDLTaskRecord task_record;
          bool need_rebuild_index = true;
          SMART_VAR(ObCreateIndexArg, create_index_arg) {
            // this create index arg is not valid, only has nls format
            create_index_arg.nls_date_format_ = alter_table_arg_.nls_formats_[0];
            create_index_arg.nls_timestamp_format_ = alter_table_arg_.nls_formats_[1];
            create_index_arg.nls_timestamp_tz_format_ = alter_table_arg_.nls_formats_[2];
O
obdev 已提交
453
            if (OB_FAIL(new_schema_guard.get_table_schema(tenant_id_, index_ids.at(i), index_schema))) {
W
wangzelin.wzl 已提交
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
              LOG_WARN("get table schema failed", K(ret), K(tenant_id_), K(index_ids.at(i)));
            } else if (OB_ISNULL(index_schema)) {
              ret = OB_ERR_SYS;
              LOG_WARN("error sys, index schema must not be nullptr", K(ret), K(index_ids.at(i)));
            } else if (index_schema->can_read_index()) {
              // index is already built
              need_rebuild_index = false;
            } else {
              ObCreateDDLTaskParam param(tenant_id_,
                                         ObDDLType::DDL_CREATE_INDEX,
                                         table_schema,
                                         index_schema,
                                         0/*object_id*/,
                                         index_schema->get_schema_version(),
                                         parallelism_ / index_ids.count()/*parallelism*/,
                                         &allocator_,
                                         &create_index_arg,
                                         task_id_);
              if (OB_FAIL(GCTX.root_service_->get_ddl_task_scheduler().create_ddl_task(param, *GCTX.sql_proxy_, task_record))) {
                if (OB_ENTRY_EXIST == ret) {
                  ret = OB_SUCCESS;
                } else {
                  LOG_WARN("submit ddl task failed", K(ret));
                }
              } else if (OB_FAIL(GCTX.root_service_->get_ddl_task_scheduler().schedule_ddl_task(task_record))) {
                LOG_WARN("fail to schedule ddl task", K(ret), K(task_record));
              }
            }
            if (OB_SUCC(ret) && need_rebuild_index) {
O
obdev 已提交
483
              const uint64_t task_key = index_ids.at(i);
W
wangzelin.wzl 已提交
484 485
              DependTaskStatus status;
              status.task_id_ = task_record.task_id_;
486
              TCWLockGuard guard(lock_);
O
obdev 已提交
487 488 489 490 491 492
              if (OB_FAIL(dependent_task_result_map_.set_refactored(task_key, status))) {
                if (OB_HASH_EXIST == ret) {
                  ret = OB_SUCCESS;
                } else {
                  LOG_WARN("set dependent task map failed", K(ret), K(task_key));
                }
W
wangzelin.wzl 已提交
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
              } else {
                LOG_INFO("add build index task", K(task_key));
              }
            }
          }
        }
      }
      if (OB_SUCC(ret)) {
        has_rebuild_index_ = true;
      }
    }
  }
  return ret;
}

int ObTableRedefinitionTask::copy_table_constraints()
{
  int ret = OB_SUCCESS;
  ObRootService *root_service = GCTX.root_service_;
  const ObTableSchema *table_schema = nullptr;
  ObSchemaGetterGuard schema_guard;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObTableRedefinitionTask has not been inited", K(ret));
  } else if (OB_ISNULL(root_service)) {
    ret = OB_ERR_SYS;
    LOG_WARN("error sys, root service must not be nullptr", K(ret));
  } else {
    if (has_rebuild_constraint_) {
      // do nothing
    } else {
      ObSArray<uint64_t> constraint_ids;
      ObSArray<uint64_t> new_constraint_ids;
O
obdev 已提交
526
      bool need_rebuild_constraint = true;
W
wangzelin.wzl 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
      if (OB_FAIL(root_service->get_ddl_service().get_tenant_schema_guard_with_version_in_inner_table(tenant_id_, schema_guard))) {
        LOG_WARN("get schema guard failed", K(ret));
      } else if (OB_FAIL(schema_guard.get_table_schema(tenant_id_, target_object_id_, table_schema))) {
        LOG_WARN("get table schema failed", K(ret), K(tenant_id_), K(target_object_id_));
      } else if (OB_ISNULL(table_schema)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("error unexpected, table schema must not be nullptr", K(ret), K(target_object_id_));
      } else if (OB_FAIL(check_need_rebuild_constraint(*table_schema,
                                                       new_constraint_ids,
                                                       need_rebuild_constraint))) {
        LOG_WARN("failed to check need rebuild constraint", K(ret));
      } else if (need_rebuild_constraint) {
        alter_table_arg_.ddl_task_type_ = share::REBUILD_CONSTRAINT_TASK;
        alter_table_arg_.table_id_ = object_id_;
        alter_table_arg_.hidden_table_id_ = target_object_id_;
542
        alter_table_arg_.alter_table_schema_.set_tenant_id(tenant_id_);
O
obdev 已提交
543 544 545 546
        int64_t ddl_rpc_timeout = 0;
        if (OB_FAIL(ObDDLUtil::get_ddl_rpc_timeout(tenant_id_, target_object_id_, ddl_rpc_timeout))) {
          LOG_WARN("get ddl rpc timeout fail", K(ret));
        } else if (OB_FAIL(root_service->get_ddl_service().get_common_rpc()->to(obrpc::ObRpcProxy::myaddr_).timeout(ddl_rpc_timeout).
W
wangzelin.wzl 已提交
547
              execute_ddl_task(alter_table_arg_, constraint_ids))) {
O
obdev 已提交
548
          LOG_WARN("rebuild hidden table constraint failed", K(ret), K(ddl_rpc_timeout));
W
wangzelin.wzl 已提交
549 550 551 552 553 554 555
        }
      } else {
        LOG_INFO("constraint has already been built");
      }
      DEBUG_SYNC(TABLE_REDEFINITION_COPY_TABLE_CONSTRAINTS);
      if (OB_SUCC(ret) && constraint_ids.count() > 0) {
        for (int64_t i = 0; OB_SUCC(ret) && i < constraint_ids.count(); ++i) {
O
obdev 已提交
556
          if (OB_FAIL(add_constraint_ddl_task(constraint_ids.at(i)))) {
W
wangzelin.wzl 已提交
557 558 559 560 561 562
            LOG_WARN("add constraint ddl task failed", K(ret));
          }
        }
      }
      if (OB_SUCC(ret) && new_constraint_ids.count() > 0) {
        for (int64_t i = 0; OB_SUCC(ret) && i < new_constraint_ids.count(); ++i) {
O
obdev 已提交
563
          if (OB_FAIL(add_constraint_ddl_task(new_constraint_ids.at(i)))) {
W
wangzelin.wzl 已提交
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
            LOG_WARN("add constraint ddl task failed", K(ret));
          }
        }
      }
      if (OB_SUCC(ret)) {
        has_rebuild_constraint_ = true;
      }
    }
  }
  return ret;
}

int ObTableRedefinitionTask::copy_table_foreign_keys()
{
  int ret = OB_SUCCESS;
  ObRootService *root_service = GCTX.root_service_;
  const ObSimpleTableSchemaV2 *table_schema = nullptr;
  ObSchemaGetterGuard schema_guard;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObTableRedefinitionTask has not been inited", K(ret));
  } else if (OB_ISNULL(root_service)) {
    ret = OB_ERR_SYS;
    LOG_WARN("error sys, root service must not be nullptr", K(ret));
  } else {
    if (has_rebuild_foreign_key_) {
      // do nothing
    } else {
      if (OB_FAIL(root_service->get_ddl_service().get_tenant_schema_guard_with_version_in_inner_table(tenant_id_, schema_guard))) {
        LOG_WARN("get schema guard failed", K(ret));
      } else if (OB_FAIL(schema_guard.get_simple_table_schema(tenant_id_, target_object_id_, table_schema))) {
        LOG_WARN("get table schema failed", K(ret), K(tenant_id_), K(target_object_id_));
      } else if (OB_ISNULL(table_schema)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("error unexpected, table schema must not be nullptr", K(ret), K(target_object_id_));
      } else {
        const ObIArray<ObSimpleForeignKeyInfo> &fk_infos = table_schema->get_simple_foreign_key_info_array();
        ObSArray<uint64_t> fk_ids;
        LOG_INFO("get current fk infos", K(fk_infos));
        if (fk_infos.count() > 0) {
          for (int64_t i = 0; OB_SUCC(ret) && i < fk_infos.count(); ++i) {
            if (OB_FAIL(fk_ids.push_back(fk_infos.at(i).foreign_key_id_))) {
              LOG_WARN("push back fk id failed", K(ret));
            }
          }
          LOG_INFO("foreign key is already built", K(fk_infos));
        } else {
          alter_table_arg_.ddl_task_type_ = share::REBUILD_FOREIGN_KEY_TASK;
          alter_table_arg_.table_id_ = object_id_;
          alter_table_arg_.hidden_table_id_ = target_object_id_;
614
          alter_table_arg_.alter_table_schema_.set_tenant_id(tenant_id_);
O
obdev 已提交
615 616 617 618
          int64_t ddl_rpc_timeout = 0;
          if (OB_FAIL(ObDDLUtil::get_ddl_rpc_timeout(tenant_id_, target_object_id_, ddl_rpc_timeout))) {
            LOG_WARN("get ddl rpc timeout fail", K(ret));
          } else if (OB_FAIL(root_service->get_ddl_service().get_common_rpc()->to(obrpc::ObRpcProxy::myaddr_).timeout(ddl_rpc_timeout).
W
wangzelin.wzl 已提交
619
                execute_ddl_task(alter_table_arg_, fk_ids))) {
O
obdev 已提交
620
            LOG_WARN("rebuild hidden table constraint failed", K(ret), K(ddl_rpc_timeout));
W
wangzelin.wzl 已提交
621 622 623 624 625
          }
        }
        DEBUG_SYNC(TABLE_REDEFINITION_COPY_TABLE_FOREIGN_KEYS);
        if (OB_SUCC(ret) && fk_ids.count() > 0) {
          for (int64_t i = 0; OB_SUCC(ret) && i < fk_ids.count(); ++i) {
O
obdev 已提交
626
            if (OB_FAIL(add_fk_ddl_task(fk_ids.at(i)))) {
W
wangzelin.wzl 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
              LOG_WARN("add foreign key ddl task failed", K(ret));
            }
          }
        }
        if (OB_SUCC(ret)) {
          has_rebuild_foreign_key_ = true;
        }
      }
    }
  }
  return ret;
}

int ObTableRedefinitionTask::copy_table_dependent_objects(const ObDDLTaskStatus next_task_status)
{
  int ret = OB_SUCCESS;
  ObRootService *root_service = GCTX.root_service_;
  int64_t finished_task_cnt = 0;
O
obdev 已提交
645
  bool state_finish = false;
W
wangzelin.wzl 已提交
646 647 648 649 650 651 652 653 654
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObTableRedefinitionTask has not been inited", K(ret));
  } else if (OB_ISNULL(root_service)) {
    ret = OB_ERR_SYS;
    LOG_WARN("error sys, root service must not be nullptr", K(ret));
  } else if (!dependent_task_result_map_.created() && OB_FAIL(dependent_task_result_map_.create(MAX_DEPEND_OBJECT_COUNT, lib::ObLabel("DepTasMap")))) {
    LOG_WARN("create dependent task map failed", K(ret));
  } else {
O
obdev 已提交
655
    if (get_is_copy_indexes() && OB_FAIL(copy_table_indexes())) {
W
wangzelin.wzl 已提交
656
      LOG_WARN("copy table indexes failed", K(ret));
O
obdev 已提交
657
    } else if (get_is_copy_constraints() && OB_FAIL(copy_table_constraints())) {
W
wangzelin.wzl 已提交
658
      LOG_WARN("copy table constraints failed", K(ret));
O
obdev 已提交
659
    } else if (get_is_copy_foreign_keys() && OB_FAIL(copy_table_foreign_keys())) {
W
wangzelin.wzl 已提交
660
      LOG_WARN("copy table foreign keys failed", K(ret));
O
obdev 已提交
661 662
    } else {
      // copy triggers(at current, not supported, skip it)
O
obdev 已提交
663 664
    }
  }
665

O
obdev 已提交
666 667 668 669 670
  if (OB_FAIL(ret)) {
    state_finish = true;
  } else {
    // wait copy dependent objects to be finished
    ObAddr unused_addr;
671
    TCRLockGuard guard(lock_);
O
obdev 已提交
672
    for (common::hash::ObHashMap<uint64_t, DependTaskStatus>::const_iterator iter = dependent_task_result_map_.begin();
O
obdev 已提交
673
        iter != dependent_task_result_map_.end(); ++iter) {
O
obdev 已提交
674
      const uint64_t task_key = iter->first;
O
obdev 已提交
675 676 677 678 679 680 681 682 683 684
      const int64_t target_object_id = -1;
      const int64_t child_task_id = iter->second.task_id_;
      if (iter->second.ret_code_ == INT64_MAX) {
        // maybe ddl already finish when switching rs
        HEAP_VAR(ObDDLErrorMessageTableOperator::ObBuildDDLErrorMessage, error_message) {
          int64_t unused_user_msg_len = 0;
          if (OB_FAIL(ObDDLErrorMessageTableOperator::get_ddl_error_message(tenant_id_, child_task_id, target_object_id,
                  unused_addr, false /* is_ddl_retry_task */, *GCTX.sql_proxy_, error_message, unused_user_msg_len))) {
            if (OB_ENTRY_NOT_EXIST == ret) {
              ret = OB_SUCCESS;
O
obdev 已提交
685
              LOG_INFO("ddl task not finish", K(task_key), K(child_task_id), K(target_object_id));
W
wangzelin.wzl 已提交
686
            } else {
O
obdev 已提交
687
              LOG_WARN("fail to get ddl error message", K(ret), K(task_key), K(child_task_id), K(target_object_id));
O
obdev 已提交
688 689 690 691 692
            }
          } else {
            finished_task_cnt++;
            if (error_message.ret_code_ != OB_SUCCESS) {
              ret = error_message.ret_code_;
O
obdev 已提交
693 694 695
              if (get_is_ignore_errors()) {
                ret = OB_SUCCESS;
              }
W
wangzelin.wzl 已提交
696
            }
O
obdev 已提交
697
          }
W
wangzelin.wzl 已提交
698
        }
O
obdev 已提交
699 700 701 702
      } else {
        finished_task_cnt++;
        if (iter->second.ret_code_ != OB_SUCCESS) {
          ret = iter->second.ret_code_;
W
wangzelin.wzl 已提交
703 704 705
        }
      }
    }
O
obdev 已提交
706 707 708 709 710
    if (finished_task_cnt == dependent_task_result_map_.size()) {
      state_finish = true;
    }
  }
  if (state_finish) {
O
obdev 已提交
711
    if (OB_FAIL(switch_status(next_task_status, true, ret))) {
O
obdev 已提交
712 713
      LOG_WARN("fail to switch status", K(ret));
    }
714
  }
W
wangzelin.wzl 已提交
715 716 717 718 719 720
  return ret;
}

int ObTableRedefinitionTask::take_effect(const ObDDLTaskStatus next_task_status)
{
  int ret = OB_SUCCESS;
O
obdev 已提交
721 722 723 724 725 726 727
#ifdef ERRSIM
  SERVER_EVENT_ADD("ddl_task", "before_table_redefinition_task_effect",
                   "tenant_id", tenant_id_,
                   "object_id", object_id_,
                   "target_object_id", target_object_id_);
  DEBUG_SYNC(BEFORE_TABLE_REDEFINITION_TASK_EFFECT);
#endif
W
wangzelin.wzl 已提交
728
  ObSArray<uint64_t> objs;
O
obdev 已提交
729
  int64_t ddl_rpc_timeout = 0;
W
wangzelin.wzl 已提交
730 731 732 733 734
  alter_table_arg_.ddl_task_type_ = share::MAKE_DDL_TAKE_EFFECT_TASK;
  alter_table_arg_.table_id_ = object_id_;
  alter_table_arg_.hidden_table_id_ = target_object_id_;
  // offline ddl is allowed on table with trigger(enable/disable).
  alter_table_arg_.need_rebuild_trigger_ = true;
735
  alter_table_arg_.alter_table_schema_.set_tenant_id(tenant_id_);
W
wangzelin.wzl 已提交
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
  ObRootService *root_service = GCTX.root_service_;
  ObSchemaGetterGuard schema_guard;
  const ObTableSchema *table_schema = nullptr;
  bool use_heap_table_ddl_plan = false;
  ObDDLTaskStatus new_status = next_task_status;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObTableRedefinitionTask has not been inited", K(ret));
  } else if (OB_ISNULL(root_service)) {
    ret = OB_ERR_SYS;
    LOG_WARN("error sys, root service must not be nullptr", K(ret));
  } else if (OB_FAIL(ObMultiVersionSchemaService::get_instance().get_tenant_schema_guard(tenant_id_, schema_guard))) {
    LOG_WARN("get tenant schema guard failed", K(ret));
  } else if (OB_FAIL(schema_guard.get_table_schema(tenant_id_, target_object_id_, table_schema))) {
    LOG_WARN("get table schema failed", K(ret), K(tenant_id_));
  } else if (OB_ISNULL(table_schema)) {
    ret = OB_TABLE_NOT_EXIST;
    LOG_WARN("table schema not exist", K(ret), K(target_object_id_));
  } else if (!table_schema->is_user_hidden_table()) {
    LOG_INFO("target schema took effect", K(target_object_id_));
  } else if (OB_FAIL(check_use_heap_table_ddl_plan(use_heap_table_ddl_plan))) {
    LOG_WARN("fail to check heap table ddl plan", K(ret));
  } else if (table_schema->is_heap_table() && !use_heap_table_ddl_plan && OB_FAIL(sync_tablet_autoinc_seq())) {
    if (OB_TIMEOUT == ret || OB_NOT_MASTER == ret) {
      ret = OB_SUCCESS;
      new_status = ObDDLTaskStatus::TAKE_EFFECT;
    } else {
      LOG_WARN("fail to sync tablet autoinc seq", K(ret));
    }
  } else if (OB_FAIL(sync_auto_increment_position())) {
    if (OB_NOT_MASTER == ret) {
      ret = OB_SUCCESS;
      new_status = ObDDLTaskStatus::TAKE_EFFECT;
    } else {
      LOG_WARN("sync auto increment position failed", K(ret), K(object_id_), K(target_object_id_));
    }
  } else if (OB_FAIL(sync_stats_info())) {
    LOG_WARN("fail to sync stats info", K(ret), K(object_id_), K(target_object_id_));
O
obdev 已提交
774 775 776
  } else if (OB_FAIL(ObDDLUtil::get_ddl_rpc_timeout(tenant_id_, target_object_id_, ddl_rpc_timeout))) {
            LOG_WARN("get ddl rpc timeout fail", K(ret));
  } else if (OB_FAIL(root_service->get_ddl_service().get_common_rpc()->to(obrpc::ObRpcProxy::myaddr_).timeout(ddl_rpc_timeout).
W
wangzelin.wzl 已提交
777 778 779 780 781 782 783 784 785
      execute_ddl_task(alter_table_arg_, objs))) {
    LOG_WARN("fail to swap original and hidden table state", K(ret));
    if (OB_TIMEOUT == ret) {
      ret = OB_SUCCESS;
      new_status = ObDDLTaskStatus::TAKE_EFFECT;
    }
  }
  DEBUG_SYNC(TABLE_REDEFINITION_TAKE_EFFECT);
  if (new_status == next_task_status || OB_FAIL(ret)) {
O
obdev 已提交
786
    if (OB_FAIL(switch_status(next_task_status, true, ret))) {
W
wangzelin.wzl 已提交
787 788 789 790 791 792
      LOG_WARN("fail to switch status", K(ret));
    }
  }
  return ret;
}

O
obdev 已提交
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
int ObTableRedefinitionTask::repending(const share::ObDDLTaskStatus next_task_status)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObDDLRedefinitionTask has not been inited", K(ret));
  } else {
    switch (task_type_) {
      case DDL_DIRECT_LOAD:
        if (get_is_do_finish()) {
          if (OB_FAIL(switch_status(next_task_status, true, ret))) {
            LOG_WARN("fail to switch status", K(ret));
          }
        }
        break;
      default:
        if (OB_FAIL(switch_status(next_task_status, true, ret))) {
          LOG_WARN("fail to switch status", K(ret));
        }
        break;
    }
  }
  return ret;
}

818 819
bool ObTableRedefinitionTask::check_task_status_before_pending(const share::ObDDLTaskStatus task_status)
{
820 821
  return task_status == ObDDLTaskStatus::PREPARE || task_status == ObDDLTaskStatus::WAIT_TRANS_END
         || task_status == ObDDLTaskStatus::LOCK_TABLE || task_status == ObDDLTaskStatus::CHECK_TABLE_EMPTY;
822 823
}

W
wangzelin.wzl 已提交
824 825 826 827 828 829 830 831 832
int ObTableRedefinitionTask::process()
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("ObTableRedefinitionTask has not been inited", K(ret));
  } else if (OB_FAIL(check_health())) {
    LOG_WARN("check task health failed", K(ret));
  } else {
O
obdev 已提交
833
    ddl_tracing_.restore_span_hierarchy();
W
wangzelin.wzl 已提交
834 835 836 837 838 839 840 841 842 843 844 845
    switch(task_status_) {
      case ObDDLTaskStatus::PREPARE:
        if (OB_FAIL(prepare(ObDDLTaskStatus::WAIT_TRANS_END))) {
          LOG_WARN("fail to prepare table redefinition task", K(ret));
        }
        break;
      case ObDDLTaskStatus::WAIT_TRANS_END:
        if (OB_FAIL(wait_trans_end(wait_trans_ctx_, ObDDLTaskStatus::LOCK_TABLE))) {
          LOG_WARN("fail to wait trans end", K(ret));
        }
        break;
      case ObDDLTaskStatus::LOCK_TABLE:
846
        if (OB_FAIL(lock_table(ObDDLTaskStatus::CHECK_TABLE_EMPTY))) {
W
wangzelin.wzl 已提交
847 848 849 850
          LOG_WARN("fail to lock table", K(ret));
        }
        break;
      case ObDDLTaskStatus::CHECK_TABLE_EMPTY:
851
        if (OB_FAIL(check_table_empty(ObDDLTaskStatus::REPENDING))) {
W
wangzelin.wzl 已提交
852 853 854
          LOG_WARN("fail to check table empty", K(ret));
        }
        break;
855 856 857 858 859
      case ObDDLTaskStatus::REPENDING:
        if (OB_FAIL(repending(ObDDLTaskStatus::REDEFINITION))) {
          LOG_WARN("fail to repending", K(ret));
        }
        break;
W
wangzelin.wzl 已提交
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
      case ObDDLTaskStatus::REDEFINITION:
        if (OB_FAIL(table_redefinition(ObDDLTaskStatus::COPY_TABLE_DEPENDENT_OBJECTS))) {
          LOG_WARN("fail to do table redefinition", K(ret));
        }
        break;
      case ObDDLTaskStatus::COPY_TABLE_DEPENDENT_OBJECTS:
        if (OB_FAIL(copy_table_dependent_objects(ObDDLTaskStatus::MODIFY_AUTOINC))) {
          LOG_WARN("fail to copy table dependent objects", K(ret));
        }
        break;
      case ObDDLTaskStatus::MODIFY_AUTOINC:
        if (OB_FAIL(modify_autoinc(ObDDLTaskStatus::TAKE_EFFECT))) {
          LOG_WARN("fail to modify autoinc", K(ret));
        }
        break;
      case ObDDLTaskStatus::TAKE_EFFECT:
        if (OB_FAIL(take_effect(ObDDLTaskStatus::SUCCESS))) {
          LOG_WARN("fail to take effect", K(ret));
        }
        break;
      case ObDDLTaskStatus::FAIL:
        if (OB_FAIL(fail())) {
          LOG_WARN("fail to do clean up", K(ret));
        }
        break;
      case ObDDLTaskStatus::SUCCESS:
        if (OB_FAIL(success())) {
          LOG_WARN("fail to success", K(ret));
        }
        break;
      default:
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("unexpected table redefinition task state", K(task_status_));
        break;
    }
O
obdev 已提交
895
    ddl_tracing_.release_span_hierarchy();
W
wangzelin.wzl 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
  }
  return ret;
}

int ObTableRedefinitionTask::check_modify_autoinc(bool &modify_autoinc)
{
  int ret = OB_SUCCESS;
  modify_autoinc = false;
  AlterTableSchema &alter_table_schema = alter_table_arg_.alter_table_schema_;
  ObTableSchema::const_column_iterator iter = alter_table_schema.column_begin();
  ObTableSchema::const_column_iterator iter_end = alter_table_schema.column_end();
  AlterColumnSchema *alter_column_schema = nullptr;
  for(; OB_SUCC(ret) && iter != iter_end; iter++) {
    if (OB_ISNULL(alter_column_schema = static_cast<AlterColumnSchema *>(*iter))) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("iter is NULL", K(ret));
    } else if (alter_column_schema->is_autoincrement()) {
      modify_autoinc = true;
    }
  }
  return ret;
}
918

O
obdev 已提交
919 920 921 922 923 924 925 926 927
int64_t ObTableRedefinitionTask::get_serialize_param_size() const
{
  int8_t copy_indexes = static_cast<int8_t>(is_copy_indexes_);
  int8_t copy_triggers = static_cast<int8_t>(is_copy_triggers_);
  int8_t copy_constraints = static_cast<int8_t>(is_copy_constraints_);
  int8_t copy_foreign_keys = static_cast<int8_t>(is_copy_foreign_keys_);
  int8_t ignore_errors = static_cast<int8_t>(is_ignore_errors_);
  int8_t do_finish = static_cast<int8_t>(is_do_finish_);
  return alter_table_arg_.get_serialize_size() + serialization::encoded_length_i64(task_version_)
928
         + serialization::encoded_length_i64(parallelism_) + serialization::encoded_length_i64(data_format_version_)
O
obdev 已提交
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
         + serialization::encoded_length_i8(copy_indexes) + serialization::encoded_length_i8(copy_triggers)
         + serialization::encoded_length_i8(copy_constraints) + serialization::encoded_length_i8(copy_foreign_keys)
         + serialization::encoded_length_i8(ignore_errors) + serialization::encoded_length_i8(do_finish);
}

int ObTableRedefinitionTask::serialize_params_to_message(char *buf, const int64_t buf_len, int64_t &pos) const
{
  int ret = OB_SUCCESS;
  int8_t copy_indexes = static_cast<int8_t>(is_copy_indexes_);
  int8_t copy_triggers = static_cast<int8_t>(is_copy_triggers_);
  int8_t copy_constraints = static_cast<int8_t>(is_copy_constraints_);
  int8_t copy_foreign_keys = static_cast<int8_t>(is_copy_foreign_keys_);
  int8_t ignore_errors = static_cast<int8_t>(is_ignore_errors_);
  int8_t do_finish = static_cast<int8_t>(is_do_finish_);
  if (OB_UNLIKELY(nullptr == buf || buf_len <= 0)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid arguments", K(ret), KP(buf), K(buf_len));
  } else if (OB_FAIL(serialization::encode_i64(buf, buf_len, pos, task_version_))) {
    LOG_WARN("fail to serialize task version", K(ret), K(task_version_));
  } else if (OB_FAIL(alter_table_arg_.serialize(buf, buf_len, pos))) {
    LOG_WARN("serialize table arg failed", K(ret));
  } else if (OB_FAIL(serialization::encode_i64(buf, buf_len, pos, parallelism_))) {
    LOG_WARN("fail to serialize parallelism_", K(ret));
952
  } else if (OB_FAIL(serialization::encode_i64(buf, buf_len, pos, data_format_version_))) {
O
obdev 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966
    LOG_WARN("fail to serialize parallelism_", K(ret));
  } else if (OB_FAIL(serialization::encode_i8(buf, buf_len, pos, copy_indexes))) {
    LOG_WARN("fail to serialize is_copy_indexes", K(ret));
  } else if (OB_FAIL(serialization::encode_i8(buf, buf_len, pos, copy_triggers))) {
    LOG_WARN("fail to serialize is_copy_triggers", K(ret));
  } else if (OB_FAIL(serialization::encode_i8(buf, buf_len, pos, copy_constraints))) {
    LOG_WARN("fail to serialize is_copy_constraints", K(ret));
  } else if (OB_FAIL(serialization::encode_i8(buf, buf_len, pos, copy_foreign_keys))) {
    LOG_WARN("fail to serialize is_copy_foreign_keys", K(ret));
  } else if (OB_FAIL(serialization::encode_i8(buf, buf_len, pos, ignore_errors))) {
    LOG_WARN("fail to serialize is_ignore_errors", K(ret));
  } else if (OB_FAIL(serialization::encode_i8(buf, buf_len, pos, do_finish))) {
    LOG_WARN("fail to serialize is_do_finish");
  }
S
simonjoylet 已提交
967 968
  FLOG_INFO("serialize message for table redefinition", K(ret),
      K(copy_indexes), K(copy_triggers), K(copy_constraints), K(copy_foreign_keys), K(ignore_errors), K(do_finish), K(*this));
O
obdev 已提交
969 970 971
  return ret;
}

972
int ObTableRedefinitionTask::deserlize_params_from_message(const uint64_t tenant_id, const char *buf, const int64_t data_len, int64_t &pos)
O
obdev 已提交
973 974 975 976 977 978 979 980 981
{
  int ret = OB_SUCCESS;
  int8_t copy_indexes = 0;
  int8_t copy_triggers = 0;
  int8_t copy_constraints = 0;
  int8_t copy_foreign_keys = 0;
  int8_t ignore_errors = 0;
  int8_t do_finish = 0;
  obrpc::ObAlterTableArg tmp_arg;
982
  if (OB_UNLIKELY(!is_valid_tenant_id(tenant_id) || nullptr == buf || data_len <= 0)) {
O
obdev 已提交
983
    ret = OB_INVALID_ARGUMENT;
984
    LOG_WARN("invalid arguments", K(ret), K(tenant_id), KP(buf), K(data_len));
O
obdev 已提交
985 986 987 988
  } else if (OB_FAIL(serialization::decode_i64(buf, data_len, pos, &task_version_))) {
    LOG_WARN("fail to deserialize task version", K(ret));
  } else if (OB_FAIL(tmp_arg.deserialize(buf, data_len, pos))) {
    LOG_WARN("serialize table failed", K(ret));
989 990
  } else if (OB_FAIL(ObDDLUtil::replace_user_tenant_id(tenant_id, tmp_arg))) {
    LOG_WARN("replace user tenant id failed", K(ret), K(tenant_id), K(tmp_arg));
O
obdev 已提交
991 992 993 994
  } else if (OB_FAIL(deep_copy_table_arg(allocator_, tmp_arg, alter_table_arg_))) {
    LOG_WARN("deep copy table arg failed", K(ret));
  } else if (OB_FAIL(serialization::decode_i64(buf, data_len, pos, &parallelism_))) {
    LOG_WARN("fail to deserialize parallelism", K(ret));
995 996
  } else if (OB_FAIL(serialization::decode_i64(buf, data_len, pos, &data_format_version_))) {
    LOG_WARN("fail to deserialize data format version", K(ret));
O
obdev 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
  } else if (pos < data_len) {
    if (OB_FAIL(serialization::decode_i8(buf, data_len, pos, &copy_indexes))) {
      LOG_WARN("fail to deserialize is_copy_indexes_", K(ret));
    } else if (OB_FAIL(serialization::decode_i8(buf, data_len, pos, &copy_triggers))) {
      LOG_WARN("fail to deserialize is_copy_triggers_", K(ret));
    } else if (OB_FAIL(serialization::decode_i8(buf, data_len, pos, &copy_constraints))) {
      LOG_WARN("fail to deserialize is_copy_constraints_", K(ret));
    } else if (OB_FAIL(serialization::decode_i8(buf, data_len, pos, &copy_foreign_keys))) {
      LOG_WARN("fail to deserialize is_copy_foreign_keys_", K(ret));
    } else if (OB_FAIL(serialization::decode_i8(buf, data_len, pos, &ignore_errors))) {
      LOG_WARN("fail to deserialize is_ignore_errors_", K(ret));
    } else if (OB_FAIL(serialization::decode_i8(buf, data_len, pos, &do_finish))) {
      LOG_WARN("fail to deserialize is_do_finish_", K(ret));
    } else {
      is_copy_indexes_ = static_cast<bool>(copy_indexes);
      is_copy_triggers_ = static_cast<bool>(copy_triggers);
      is_copy_constraints_ = static_cast<bool>(copy_constraints);
      is_copy_foreign_keys_ = static_cast<bool>(copy_foreign_keys);
      is_ignore_errors_ = static_cast<bool>(ignore_errors);
      is_do_finish_ = static_cast<bool>(do_finish);
    }
  }
S
simonjoylet 已提交
1019 1020
  FLOG_INFO("deserialize message for table redefinition", K(ret),
      K(copy_indexes), K(copy_triggers), K(copy_constraints), K(copy_foreign_keys), K(ignore_errors), K(do_finish), K(*this));
O
obdev 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
  return ret;
}

int ObTableRedefinitionTask::assign(const ObTableRedefinitionTask *table_redef_task)
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(deep_copy_table_arg(allocator_, table_redef_task->alter_table_arg_, alter_table_arg_))) {
    LOG_WARN("assign alter_table_arg failed", K(ret));
  } else {
    task_version_ = table_redef_task->task_version_;
    parallelism_ = table_redef_task->parallelism_;
    set_is_copy_indexes(table_redef_task->get_is_copy_indexes());
    set_is_copy_triggers(table_redef_task->get_is_copy_triggers());
    set_is_copy_constraints(table_redef_task->get_is_copy_constraints());
    set_is_copy_foreign_keys(table_redef_task->get_is_copy_foreign_keys());
    set_is_ignore_errors(table_redef_task->get_is_ignore_errors());
    set_is_do_finish(table_redef_task->get_is_do_finish());
  }
  return ret;
}

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
int ObTableRedefinitionTask::collect_longops_stat(ObLongopsValue &value)
{
  int ret = OB_SUCCESS;
  int64_t pos = 0;
  const ObDDLTaskStatus status = static_cast<ObDDLTaskStatus>(task_status_);
  databuff_printf(stat_info_.message_, MAX_LONG_OPS_MESSAGE_LENGTH, pos, "TENANT_ID: %ld, TASK_ID: %ld, ", tenant_id_, task_id_);
  switch (status) {
    case ObDDLTaskStatus::PREPARE: {
      if (OB_FAIL(databuff_printf(stat_info_.message_,
                                  MAX_LONG_OPS_MESSAGE_LENGTH,
                                  "STATUS: PREPARE"))) {
        LOG_WARN("failed to print", K(ret));
      }
      break;
    }
    case ObDDLTaskStatus::WAIT_TRANS_END: {
      if (snapshot_version_ > 0) {
        if (OB_FAIL(databuff_printf(stat_info_.message_,
                                    MAX_LONG_OPS_MESSAGE_LENGTH,
                                    pos,
                                    "STATUS: ACQUIRE SNAPSHOT, SNAPSHOT_VERSION: %ld",
                                    snapshot_version_))) {
          LOG_WARN("failed to print", K(ret));
        }
      } else {
        if (OB_FAIL(databuff_printf(stat_info_.message_,
                                    MAX_LONG_OPS_MESSAGE_LENGTH,
                                    pos,
                                    "STATUS: WAIT TRANS END, PENDING_TX_ID: %ld",
                                    wait_trans_ctx_.get_pending_tx_id().get_id()))) {
          LOG_WARN("failed to print", K(ret));
        }
      }
      break;
    }
    case ObDDLTaskStatus::LOCK_TABLE: {
      if (OB_FAIL(databuff_printf(stat_info_.message_,
                                  MAX_LONG_OPS_MESSAGE_LENGTH,
                                  pos,
                                  "STATUS: ACQUIRE TABLE LOCK"))) {
        LOG_WARN("failed to print", K(ret));
      }
      break;
    }
    case ObDDLTaskStatus::CHECK_TABLE_EMPTY: {
      if (OB_FAIL(databuff_printf(stat_info_.message_,
                                  MAX_LONG_OPS_MESSAGE_LENGTH,
                                  pos,
                                  "STATUS: CHECK TABLE EMPTY"))) {
        LOG_WARN("failed to print", K(ret));
      }
      break;
    }
    case ObDDLTaskStatus::REDEFINITION: {
      int64_t row_scanned = 0;
      int64_t row_sorted = 0;
      int64_t row_inserted = 0;
      if (OB_FAIL(gather_redefinition_stats(tenant_id_, task_id_, *GCTX.sql_proxy_, row_scanned, row_sorted, row_inserted))) {
        LOG_WARN("failed to gather redefinition stats", K(ret));
      } else if (OB_FAIL(databuff_printf(stat_info_.message_,
                                  MAX_LONG_OPS_MESSAGE_LENGTH,
                                  pos,
                                  "STATUS: REPLICA BUILD, ROW_SCANNED: %ld, ROW_SORTED: %ld, ROW_INSERTED: %ld",
                                  row_scanned,
                                  row_sorted,
                                  row_inserted))) {
        LOG_WARN("failed to print", K(ret));
      }
      break;
    }
    case ObDDLTaskStatus::COPY_TABLE_DEPENDENT_OBJECTS: {
      char child_task_ids[MAX_LONG_OPS_MESSAGE_LENGTH];
      if (OB_FAIL(get_child_task_ids(child_task_ids, MAX_LONG_OPS_MESSAGE_LENGTH))) {
        if (ret == OB_SIZE_OVERFLOW) {
          ret = OB_SUCCESS;
        } else {
          LOG_WARN("failed to get all child task ids", K(ret));
        }
      } else if (OB_FAIL(databuff_printf(stat_info_.message_,
                                         MAX_LONG_OPS_MESSAGE_LENGTH,
                                         pos,
                                         "STATUS: COPY DEPENDENT OBJECTS, CHILD TASK IDS: %s",
                                         child_task_ids))) {
        if (ret == OB_SIZE_OVERFLOW) {
          ret = OB_SUCCESS;
        } else {
          LOG_WARN("failed to print", K(ret));
        }
      }
      break;
    }
    case ObDDLTaskStatus::MODIFY_AUTOINC: {
      if (OB_FAIL(databuff_printf(stat_info_.message_,
                                  MAX_LONG_OPS_MESSAGE_LENGTH,
                                  pos,
                                  "STATUS: MODIFY AUTOINC"))) {
        LOG_WARN("failed to print", K(ret));
      }
      break;
    }
    case ObDDLTaskStatus::TAKE_EFFECT: {
      if (OB_FAIL(databuff_printf(stat_info_.message_,
                                  MAX_LONG_OPS_MESSAGE_LENGTH,
                                  pos,
                                  "STATUS: TAKE EFFECT"))) {
        LOG_WARN("failed to print", K(ret));
      }
      break;
    }
1151 1152 1153 1154 1155 1156 1157 1158 1159
    case ObDDLTaskStatus::REPENDING: {
      if (OB_FAIL(databuff_printf(stat_info_.message_,
                                  MAX_LONG_OPS_MESSAGE_LENGTH,
                                  pos,
                                  "STATUS: REPENDING"))) {
        LOG_WARN("failed to print", K(ret));
      }
      break;
    }
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    case ObDDLTaskStatus::FAIL: {
      if (OB_FAIL(databuff_printf(stat_info_.message_,
                                  MAX_LONG_OPS_MESSAGE_LENGTH,
                                  "STATUS: CLEAN ON FAIL"))) {
        LOG_WARN("failed to print", K(ret));
      }
      break;
    }
    case ObDDLTaskStatus::SUCCESS: {
      if (OB_FAIL(databuff_printf(stat_info_.message_,
                                  MAX_LONG_OPS_MESSAGE_LENGTH,
                                  pos,
                                  "STATUS: CLEAN ON SUCCESS"))) {
        LOG_WARN("failed to print", K(ret));
      }
      break;
    }
    default: {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("not expected status", K(ret), K(status), K(*this));
      break;
    }
  }
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

  // append direct load information to the message
  if (OB_SUCC(ret) && (ObDDLType::DDL_DIRECT_LOAD == get_task_type())) {
    common::ObArenaAllocator allocator(lib::ObLabel("RedefTask"));
    sql::ObLoadDataStat job_stat;
    if (OB_FAIL(get_direct_load_job_stat(allocator, job_stat))) {
      LOG_WARN("failed to get direct load job_stat", KR(ret));
    } else if (job_stat.job_id_ > 0) {
      databuff_printf(stat_info_.message_, MAX_LONG_OPS_MESSAGE_LENGTH, pos,
          ", TABLE_ID: %ld, BATCH_SIZE: %ld, PARALLEL: %ld, MAX_ALLOWED_ERROR_ROWS: %ld"
          ", DETECTED_ERROR_ROWS: %ld, PROCESSED_ROWS: %ld, LOAD_STATUS: %.*s",
          job_stat.job_id_,
          job_stat.batch_size_,
          job_stat.parallel_,
          job_stat.max_allowed_error_rows_,
          job_stat.detected_error_rows_,
          job_stat.coordinator.received_rows_,
          job_stat.coordinator.status_.length(),
          job_stat.coordinator.status_.ptr());
    }
  }

1205 1206 1207 1208 1209 1210
  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(copy_longops_stat(value))) {
    LOG_WARN("failed to collect common longops stat", K(ret));
  }
  return ret;
}
O
obdev 已提交
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

void ObTableRedefinitionTask::flt_set_task_span_tag() const
{
  FLT_SET_TAG(ddl_task_id, task_id_, ddl_parent_task_id, parent_task_id_,
              ddl_data_table_id, object_id_, ddl_schema_version, schema_version_,
              ddl_ret_code, ret_code_);
}

void ObTableRedefinitionTask::flt_set_status_span_tag() const
{
  switch (task_status_) {
  case ObDDLTaskStatus::PREPARE: {
    FLT_SET_TAG(ddl_ret_code, ret_code_);
    break;
  }
  case ObDDLTaskStatus::WAIT_TRANS_END: {
    FLT_SET_TAG(ddl_data_table_id, object_id_, ddl_ret_code, ret_code_);
    break;
  }
  case ObDDLTaskStatus::LOCK_TABLE: {
    FLT_SET_TAG(ddl_ret_code, ret_code_);
    break;
  }
  case ObDDLTaskStatus::CHECK_TABLE_EMPTY: {
    FLT_SET_TAG(ddl_ret_code, ret_code_);
    break;
  }
  case ObDDLTaskStatus::REDEFINITION: {
    FLT_SET_TAG(ddl_ret_code, ret_code_);
    break;
  }
  case ObDDLTaskStatus::COPY_TABLE_DEPENDENT_OBJECTS: {
    FLT_SET_TAG(ddl_ret_code, ret_code_);
    break;
  }
  case ObDDLTaskStatus::MODIFY_AUTOINC: {
    FLT_SET_TAG(ddl_ret_code, ret_code_);
    break;
  }
  case ObDDLTaskStatus::TAKE_EFFECT: {
    FLT_SET_TAG(ddl_ret_code, ret_code_);
    break;
  }
  case ObDDLTaskStatus::FAIL: {
    FLT_SET_TAG(ddl_ret_code, ret_code_);
    break;
  }
  case ObDDLTaskStatus::SUCCESS: {
    FLT_SET_TAG(ddl_ret_code, ret_code_);
    break;
  }
  default: {
    break;
  }
  }
}
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

int ObTableRedefinitionTask::get_direct_load_job_stat(common::ObArenaAllocator &allocator,
                                                      sql::ObLoadDataStat &job_stat)
{
  int ret = OB_SUCCESS;
  ObMySQLProxy &sql_proxy = *GCTX.sql_proxy_;
  ObSqlString select_sql;
  sqlclient::ObMySQLResult *select_result = NULL;
  SMART_VAR(ObMySQLProxy::MySQLResult, select_res) {
    if (OB_FAIL(select_sql.assign_fmt(
        "SELECT JOB_ID, BATCH_SIZE, PARALLEL, MAX_ALLOWED_ERROR_ROWS, DETECTED_ERROR_ROWS, "
        "COORDINATOR_RECEIVED_ROWS, COORDINATOR_STATUS FROM %s WHERE TENANT_ID=%lu "
        "AND JOB_ID=%ld AND JOB_TYPE='direct' AND COORDINATOR_STATUS!='none'",
        OB_ALL_VIRTUAL_LOAD_DATA_STAT_TNAME, tenant_id_, object_id_))) {
      LOG_WARN("failed to assign sql", KR(ret));
    } else if (OB_FAIL(sql_proxy.read(select_res, OB_SYS_TENANT_ID, select_sql.ptr()))) {
      LOG_WARN("fail to execute sql", KR(ret), K(select_sql));
    } else if (OB_ISNULL(select_result = select_res.get_result())) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("error unexpected, query result must not be NULL", KR(ret));
    }
    while (OB_SUCC(ret)) {
      if (OB_FAIL(select_result->next())) {
        if (OB_ITER_END == ret) {
          ret = OB_SUCCESS;
          break;
        } else {
          LOG_WARN("failed to get next row", KR(ret));
        }
      } else {
        ObString load_status;
        EXTRACT_INT_FIELD_MYSQL(*select_result, "JOB_ID", job_stat.job_id_, int64_t);
        EXTRACT_INT_FIELD_MYSQL(*select_result, "BATCH_SIZE", job_stat.batch_size_, int64_t);
        EXTRACT_INT_FIELD_MYSQL(*select_result, "PARALLEL", job_stat.parallel_, int64_t);
        EXTRACT_INT_FIELD_MYSQL(*select_result, "MAX_ALLOWED_ERROR_ROWS", job_stat.max_allowed_error_rows_, int64_t);
        EXTRACT_INT_FIELD_MYSQL(*select_result, "DETECTED_ERROR_ROWS", job_stat.detected_error_rows_, int64_t);
        EXTRACT_INT_FIELD_MYSQL(*select_result, "COORDINATOR_RECEIVED_ROWS", job_stat.coordinator.received_rows_, int64_t);
        EXTRACT_VARCHAR_FIELD_MYSQL(*select_result, "COORDINATOR_STATUS", load_status);
        if (OB_SUCC(ret)
            && OB_FAIL(ob_write_string(allocator, load_status, job_stat.coordinator.status_))) {
          LOG_WARN("failed to write string", KR(ret));
        }
      }
    }
  }
  return ret;
}