ob_root_inspection.cpp 86.4 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 * 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_root_inspection.h"

#include "lib/string/ob_sql_string.h"
#include "lib/mysqlclient/ob_mysql_proxy.h"
W
wangzelin.wzl 已提交
19
#include "lib/utility/ob_tracepoint.h"
O
oceanbase-admin 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "share/inner_table/ob_inner_table_schema.h"
#include "share/schema/ob_multi_version_schema_service.h"
#include "share/schema/ob_schema_getter_guard.h"
#include "share/schema/ob_schema_utils.h"
#include "share/ob_zone_info.h"
#include "share/system_variable/ob_system_variable_factory.h"
#include "share/system_variable/ob_system_variable_init.h"
#include "rootserver/ob_root_utils.h"
#include "rootserver/ob_zone_manager.h"
#include "rootserver/ob_ddl_operator.h"
#include "rootserver/ob_root_service.h"
#include "observer/ob_server_struct.h"
#include "observer/ob_sql_client_decorator.h"
#include "share/backup/ob_backup_info_mgr.h"
#include "share/ob_primary_zone_util.h"
#include "share/ob_upgrade_utils.h"
#include "share/rc/ob_context.h"
#include "share/schema/ob_schema_mgr.h"
W
wangzelin.wzl 已提交
38
#include "share/ob_schema_status_proxy.h"//ObSchemaStatusProxy
39
#include "share/ob_global_stat_proxy.h"//ObGlobalStatProxy
40
#include "share/ob_tenant_info_proxy.h" // ObAllTenantInfoProxy
O
oceanbase-admin 已提交
41

W
wangzelin.wzl 已提交
42 43
namespace oceanbase
{
O
oceanbase-admin 已提交
44 45 46 47 48
using namespace common;
using namespace common::sqlclient;
using namespace share;
using namespace share::schema;
using namespace sql;
W
wangzelin.wzl 已提交
49 50
namespace rootserver
{
O
oceanbase-admin 已提交
51
////////////////////////////////////////////////////////////////
W
wangzelin.wzl 已提交
52
int ObTenantChecker::inspect(bool &passed, const char* &warning_info)
O
oceanbase-admin 已提交
53 54
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
55
  int tmp_ret = OB_SUCCESS;
O
oceanbase-admin 已提交
56 57
  passed = true;
  UNUSED(warning_info);
W
wangzelin.wzl 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70
  if (OB_SUCCESS != (tmp_ret = alter_tenant_primary_zone_())) {
    ret = OB_SUCC(ret) ? tmp_ret : ret;
    LOG_WARN("fail to alter tenant primary_zone", KR(ret), KR(tmp_ret));
  }

  if (OB_SUCCESS != (tmp_ret = check_create_tenant_end_())) {
    ret = OB_SUCC(ret) ? tmp_ret : ret;
    LOG_WARN("fail to check create tenant end", KR(ret), KR(tmp_ret));
  }

  if (OB_SUCCESS != (tmp_ret = check_garbage_tenant_(passed))) {
    ret = OB_SUCC(ret) ? tmp_ret : ret;
    LOG_WARN("fail to check garbage tenant", KR(ret), KR(tmp_ret));
O
oceanbase-admin 已提交
71 72 73 74 75
  }
  return ret;
}

// If the primary_zone of tenant is null, need to set to 'RANDOM'
W
wangzelin.wzl 已提交
76
int ObTenantChecker::alter_tenant_primary_zone_()
O
oceanbase-admin 已提交
77 78 79 80 81 82 83 84 85 86 87 88
{
  int ret = OB_SUCCESS;
  ObSchemaGetterGuard schema_guard;
  ObArray<uint64_t> tenant_ids;
  if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("schema service not init", K(ret));
  } else if (OB_FAIL(schema_service_->get_tenant_schema_guard(OB_SYS_TENANT_ID, schema_guard))) {
    LOG_WARN("get_schema_guard failed", K(ret));
  } else if (OB_FAIL(schema_guard.get_tenant_ids(tenant_ids))) {
    LOG_WARN("get_tenant_ids failed", K(ret));
  } else {
W
wangzelin.wzl 已提交
89
    const ObTenantSchema *tenant_schema = NULL;
O
oceanbase-admin 已提交
90
    int64_t affected_rows = 0;
W
wangzelin.wzl 已提交
91
    FOREACH_CNT_X(tenant_id, tenant_ids, OB_SUCCESS == ret) {
O
oceanbase-admin 已提交
92 93 94 95 96 97 98 99 100 101
      if (OB_ISNULL(tenant_id)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("tenant_id is null", K(ret));
      } else if (OB_FAIL(schema_guard.get_tenant_info(*tenant_id, tenant_schema))) {
        LOG_WARN("fail to get tenant info", K(ret), K(*tenant_id));
      } else if (OB_ISNULL(tenant_schema)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("tenant schema is null", K(ret), K(*tenant_id));
      } else if (tenant_schema->get_primary_zone().empty()) {
        ObSqlString sql;
W
wangzelin.wzl 已提交
102 103
        if (OB_FAIL(sql.append_fmt("ALTER TENANT %s set primary_zone = RANDOM",
                                   tenant_schema->get_tenant_name()))) {
O
oceanbase-admin 已提交
104 105 106 107 108 109 110
          LOG_WARN("fail to generate sql", K(ret), K(*tenant_id));
        } else if (OB_ISNULL(sql_proxy_)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("sql_proxy is null", K(ret));
        } else if (OB_FAIL(sql_proxy_->write(sql.ptr(), affected_rows))) {
          LOG_WARN("execute sql failed", K(ret));
        } else {
W
wangzelin.wzl 已提交
111 112 113
          ROOTSERVICE_EVENT_ADD("inspector", "alter_tenant_primary_zone",
                                "tenant_id", tenant_schema->get_tenant_id(),
                                "tenant", tenant_schema->get_tenant_name());
O
oceanbase-admin 已提交
114 115 116 117 118 119 120
        }
      }
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
121
int ObTenantChecker::check_create_tenant_end_()
O
oceanbase-admin 已提交
122 123 124 125 126 127 128 129 130 131 132
{
  int ret = OB_SUCCESS;
  ObSchemaGetterGuard schema_guard;
  ObArray<uint64_t> tenant_ids;
  if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("schema service not init", K(ret));
  } else if (!schema_service_->is_sys_full_schema()) {
    // skip
  } else if (GCTX.is_standby_cluster()) {
    // skip
W
wangzelin.wzl 已提交
133
  } else if (OB_FAIL(schema_service_->get_tenant_ids(tenant_ids))) {
O
oceanbase-admin 已提交
134 135
    LOG_WARN("get_tenant_ids failed", K(ret));
  } else {
W
wangzelin.wzl 已提交
136
    const ObSimpleTenantSchema *tenant_schema = NULL;
O
oceanbase-admin 已提交
137
    int64_t schema_version = OB_INVALID_VERSION;
O
obdev 已提交
138
    int64_t baseline_schema_version = OB_INVALID_VERSION;
W
wangzelin.wzl 已提交
139
    FOREACH_CNT(tenant_id, tenant_ids) {
O
oceanbase-admin 已提交
140
      // overwrite ret
W
wangzelin.wzl 已提交
141
      if (OB_FAIL(schema_service_->get_tenant_schema_guard(*tenant_id, schema_guard))) {
O
obdev 已提交
142
        LOG_WARN("get_schema_guard failed", KR(ret), K(*tenant_id));
W
wangzelin.wzl 已提交
143
      } else if (OB_FAIL(schema_guard.get_schema_version(*tenant_id, schema_version))) {
O
obdev 已提交
144
        LOG_WARN("fail to get tenant schema version", KR(ret), K(*tenant_id));
W
wangzelin.wzl 已提交
145 146
      } else if (!share::schema::ObSchemaService::is_formal_version(schema_version)) {
        // tenant is still in creating
O
oceanbase-admin 已提交
147
      } else if (OB_FAIL(schema_guard.get_tenant_info(*tenant_id, tenant_schema))) {
O
obdev 已提交
148
        LOG_WARN("fail to get tenant schema", KR(ret), K(*tenant_id));
O
oceanbase-admin 已提交
149 150
      } else if (OB_ISNULL(tenant_schema)) {
        ret = OB_ERR_UNEXPECTED;
O
obdev 已提交
151 152 153 154 155 156
        LOG_WARN("tenant not exist", KR(ret), K(*tenant_id));
      } else if (OB_FAIL(schema_service_->get_baseline_schema_version(*tenant_id, false/*auto update*/,
                                                                      baseline_schema_version))) {
        LOG_WARN("fail to get baseline schema_version", KR(ret), K(*tenant_id));
      } else if (OB_INVALID_VERSION == baseline_schema_version) {
        //baseline_schema_version is not valid, just skip to create this kind of tenant
O
oceanbase-admin 已提交
157 158 159 160 161
      } else if (tenant_schema->is_creating()) {
        obrpc::ObCreateTenantEndArg arg;
        arg.exec_tenant_id_ = OB_SYS_TENANT_ID;
        arg.tenant_id_ = *tenant_id;
        if (OB_FAIL(rpc_proxy_.create_tenant_end(arg))) {
O
obdev 已提交
162
          LOG_WARN("fail to execute create tenant end", KR(ret), K(*tenant_id));
O
oceanbase-admin 已提交
163
        } else {
O
obdev 已提交
164
          LOG_INFO("execute create_tenant_end", KR(ret), K(*tenant_id), K(schema_version));
W
wangzelin.wzl 已提交
165
          ROOTSERVICE_EVENT_ADD("inspector", "tenant_checker", "info", "execute create_tenant_end", "tenant_id", *tenant_id);
O
oceanbase-admin 已提交
166 167 168 169 170 171
        }
      }
    }
  }
  return ret;
}
W
wangzelin.wzl 已提交
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

int ObTenantChecker::check_garbage_tenant_(bool &passed)
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("schema service not init", KR(ret));
  } else if (!schema_service_->is_sys_full_schema()) {
    // skip
  } else if (GCTX.is_standby_cluster()) {
    //In standby cluster, As long as transaction one is successful, transaction two does not need to be executed,
    //and there is no case that tenant creation fails and drop tenant is needed.
  } else {
    obrpc::ObGetSchemaArg arg;
    obrpc::ObTenantSchemaVersions result;
    ObSchemaGetterGuard schema_guard;
    arg.ignore_fail_ = true;
    arg.exec_tenant_id_ = OB_SYS_TENANT_ID;
    // There may have multi RootService, so we won't force drop tenant here.
    if (OB_FAIL(rpc_proxy_.get_tenant_schema_versions(arg, result))) {
      LOG_WARN("fail to get tenant schema versions", KR(ret));
    } else if (OB_FAIL(schema_service_->get_tenant_schema_guard(OB_SYS_TENANT_ID, schema_guard))) {
      LOG_WARN("get_schema_guard failed", KR(ret));
    }
    for (int64_t i = 0; OB_SUCC(ret) && i < result.tenant_schema_versions_.count(); i++) {
      TenantIdAndSchemaVersion &tenant = result.tenant_schema_versions_.at(i);
      int tmp_ret = OB_SUCCESS;
      if (!ObSchemaService::is_formal_version(tenant.schema_version_)) {
        const ObSimpleTenantSchema *tenant_schema = NULL;
        uint64_t tenant_id = tenant.tenant_id_;
        if (OB_SUCCESS != (tmp_ret = schema_guard.get_tenant_info(tenant_id, tenant_schema))) {
          LOG_WARN("fail to get tenant info", KR(tmp_ret), K(tenant_id));
        } else if (OB_ISNULL(tenant_schema)) {
          tmp_ret = OB_TENANT_NOT_EXIST;
        } else if (tenant_schema->is_restore()) {
          LOG_INFO("tenant is in restore", KPC(tenant_schema));
        } else {
          LOG_ERROR("tenant maybe create failed", K(tenant_id));
210 211
          LOG_DBA_WARN(OB_ERR_ROOT_INSPECTION, "msg", "create tenant may fail",
                       K(tenant_id), "tenant_name", tenant_schema->get_tenant_name());
W
wangzelin.wzl 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224
          ROOTSERVICE_EVENT_ADD("inspector", "tenant_checker",
                                "info", "tenant maybe create failed",
                                "tenant_id", tenant_id,
                                "tenant_name", tenant_schema->get_tenant_name_str());
        }
        ret = OB_SUCC(ret) ? tmp_ret : ret;
      }
    } // end for
    passed = OB_SUCC(ret);
  }
  return ret;
}

O
oceanbase-admin 已提交
225
////////////////////////////////////////////////////////////////
W
wangzelin.wzl 已提交
226
ObTableGroupChecker::ObTableGroupChecker(share::schema::ObMultiVersionSchemaService &schema_service)
O
oceanbase-admin 已提交
227 228 229 230
    : schema_service_(schema_service),
      check_part_option_map_(),
      part_option_not_match_set_(),
      is_inited_(false)
W
wangzelin.wzl 已提交
231 232
{
}
O
oceanbase-admin 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

int ObTableGroupChecker::init()
{
  int ret = OB_SUCCESS;
  if (is_inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("init twice", K(ret));
  } else if (OB_FAIL(check_part_option_map_.create(TABLEGROUP_BUCKET_NUM, ObModIds::OB_HASH_BUCKET_TABLEGROUP_MAP))) {
    LOG_WARN("init check_part_option_map failed", K(ret));
  } else if (OB_FAIL(part_option_not_match_set_.create(TABLEGROUP_BUCKET_NUM))) {
    LOG_WARN("init part_option_not_match_set failed", K(ret));
  } else {
    is_inited_ = true;
  }
  return ret;
}

ObTableGroupChecker::~ObTableGroupChecker()
W
wangzelin.wzl 已提交
251 252
{
}
O
oceanbase-admin 已提交
253

W
wangzelin.wzl 已提交
254
int ObTableGroupChecker::inspect(bool &passed, const char* &warning_info)
O
oceanbase-admin 已提交
255 256 257
{
  int ret = OB_SUCCESS;
  passed = true;
W
wangzelin.wzl 已提交
258
  ObArray<uint64_t> tenant_ids;
O
oceanbase-admin 已提交
259 260 261
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("tablegroup checker is not init", K(ret));
W
wangzelin.wzl 已提交
262 263
  } else if (OB_FAIL(schema_service_.get_tenant_ids(tenant_ids))) {
    LOG_WARN("fail to get tenant ids", KR(ret));
O
oceanbase-admin 已提交
264
  } else {
W
wangzelin.wzl 已提交
265 266 267 268 269
    int tmp_ret = OB_SUCCESS;
    FOREACH(tenant_id, tenant_ids) { // ignore error for each tenant
      if (OB_SUCCESS != (tmp_ret = inspect_(*tenant_id, passed))) {
        LOG_WARN("inspect tablegroup options by tenant failed",
                 KR(tmp_ret), "tenant_id", *tenant_id);
O
oceanbase-admin 已提交
270
      }
W
wangzelin.wzl 已提交
271
      ret = OB_SUCC(ret) ? tmp_ret : ret;
O
oceanbase-admin 已提交
272
    }
W
wangzelin.wzl 已提交
273
    if (!passed) {
O
oceanbase-admin 已提交
274 275 276 277 278 279
      warning_info = "tablegroup has tables that have different primary_zone/locality/part_option";
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
280 281 282
int ObTableGroupChecker::inspect_(
    const uint64_t tenant_id,
    bool &passed)
O
oceanbase-admin 已提交
283 284
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
285 286 287 288
  ObSchemaGetterGuard schema_guard;
  check_part_option_map_.reuse();
  part_option_not_match_set_.reuse();
  ObArray<uint64_t> table_ids;
O
oceanbase-admin 已提交
289 290 291
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("tablegroup checker is not init", K(ret));
W
wangzelin.wzl 已提交
292 293 294 295
  } else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
    LOG_WARN("get schema guard failed", K(ret), K(tenant_id));
  } else if (OB_FAIL(schema_guard.get_table_ids_in_tenant(tenant_id, table_ids))) {
    LOG_WARN("fail to get table_ids", KR(ret), K(tenant_id));
O
oceanbase-admin 已提交
296
  } else {
W
wangzelin.wzl 已提交
297 298 299 300 301 302
    for (int64_t i = 0; OB_SUCC(ret) && i < table_ids.count(); i++) {
      const uint64_t table_id = table_ids.at(i);
      const ObTableSchema *table = NULL;
      if (OB_FAIL(schema_guard.get_table_schema(tenant_id, table_id, table))) {
        LOG_WARN("get table schema failed", K(ret), KT(table_id));
      } else if (OB_ISNULL(table)) {
O
oceanbase-admin 已提交
303
        ret = OB_ERR_UNEXPECTED;
W
wangzelin.wzl 已提交
304 305 306 307 308 309
        LOG_WARN("table not exist", K(ret), K(table_id));
      } else if (is_sys_table(table->get_table_id()) || !table->has_partition()) {
        // skip, check the partitioned user table
      } else if (OB_FAIL(check_part_option(*table, schema_guard))) {
        LOG_WARN("check part option fail", KPC(table));
      } else {}
O
oceanbase-admin 已提交
310 311
    }
  }
W
wangzelin.wzl 已提交
312 313 314 315 316
  if (OB_SUCC(ret)) {
    if (part_option_not_match_set_.size() > 0) {
      passed = false;
      LOG_WARN("tables part option in one tablegroup are not the same", K(tenant_id), K_(part_option_not_match_set));
      ROOTSERVICE_EVENT_ADD("inspector", "check_part_option", K(tenant_id), K_(part_option_not_match_set));
O
oceanbase-admin 已提交
317 318 319 320 321 322 323 324
    }
  }
  return ret;
}

// Check the partition_option of tables in the same tablegroup:
// 1. For tablegroups created before 2.0, the part_type and part_num of tables in tablegroup should be same.
// 2. For tablegroups created after 2.0:
W
wangzelin.wzl 已提交
325 326
//    1) tablegroup is nonpartition, Allow "non partitioned table" or "partitioned table with 1 number of partitions" in tablegroup.
//       in addition, the partition_num, partition_type, partition_value and number of expression vectors of tables must be same.
O
oceanbase-admin 已提交
327 328
//    2) tablegroup is partitioned, the partition_num, partition_type, partition_value and number of expression vectors
//       of tables in tablegroup should be same.
W
wangzelin.wzl 已提交
329
int ObTableGroupChecker::check_part_option(const ObTableSchema &table, ObSchemaGetterGuard &schema_guard)
O
oceanbase-admin 已提交
330 331 332
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
333
  const uint64_t tenant_id = table.get_tenant_id();
O
oceanbase-admin 已提交
334 335 336 337 338 339 340 341 342
  const uint64_t tablegroup_id = table.get_tablegroup_id();
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("tablegroup checker is not init", K(ret));
  } else if (OB_INVALID_ID == tablegroup_id) {
    // skip check while tablegroup_id is default value
  } else if (!table.is_user_table()) {
    // only check user table
  } else if (OB_HASH_NOT_EXIST != (tmp_ret = part_option_not_match_set_.exist_refactored(tablegroup_id))) {
W
wangzelin.wzl 已提交
343 344 345 346 347
   //skip check while already in part_option_not_match_set_
   if (OB_HASH_EXIST != tmp_ret) {
     ret = tmp_ret;
     LOG_WARN("fail to check if tablegroup_id exist", K(ret), K(tablegroup_id));
   }
O
oceanbase-admin 已提交
348
  } else {
W
wangzelin.wzl 已提交
349
    const ObTableSchema *table_in_map;
O
oceanbase-admin 已提交
350
    bool is_matched = true;
W
wangzelin.wzl 已提交
351
    if (is_sys_tablegroup_id(tablegroup_id)) {
O
oceanbase-admin 已提交
352
      if (OB_FAIL(check_part_option_map_.get_refactored(tablegroup_id, table_in_map))) {
W
wangzelin.wzl 已提交
353
        //set into map while not in check_part_option_map_
O
oceanbase-admin 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367
        if (OB_HASH_NOT_EXIST == ret) {
          if (OB_FAIL(check_part_option_map_.set_refactored(tablegroup_id, &table))) {
            LOG_WARN("set table_schema in hashmap fail", K(ret), K(tablegroup_id), K(table));
          }
        } else {
          LOG_WARN("check tablegroup_id in hashmap fail", K(ret), K(tablegroup_id));
        }
      } else if (OB_ISNULL(table_in_map)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("table_in_map is null ptr", K(ret));
      } else if (OB_FAIL(check_if_part_option_equal(*table_in_map, table, is_matched))) {
        LOG_WARN("check if part option equal fail", K(ret), KPC(table_in_map), K(table));
      }
    } else {
W
wangzelin.wzl 已提交
368 369 370
      const ObTablegroupSchema *tablegroup = NULL;
      if (OB_FAIL(schema_guard.get_tablegroup_schema(tenant_id, tablegroup_id, tablegroup))) {
        LOG_WARN("fail to get tablegroup schema", K(ret), K(tenant_id), KT(tablegroup_id));
O
oceanbase-admin 已提交
371 372 373 374 375 376 377 378 379
      } else if (OB_ISNULL(tablegroup)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("tablegroup schema is null", K(ret), KT(tablegroup_id));
      } else if (PARTITION_LEVEL_ZERO == tablegroup->get_part_level()) {
        if (PARTITION_LEVEL_ZERO == table.get_part_level()) {
          // no need to check partition_option of nonpartitioned table
        } else if (1 != table.get_all_part_num()) {
          is_matched = false;
        } else if (OB_FAIL(check_part_option_map_.get_refactored(tablegroup_id, table_in_map))) {
W
wangzelin.wzl 已提交
380
          //set to the map while not in check_part_option_map_
O
oceanbase-admin 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
          if (OB_HASH_NOT_EXIST == ret) {
            if (OB_FAIL(check_part_option_map_.set_refactored(tablegroup_id, &table))) {
              LOG_WARN("set table_schema in hashmap fail", K(ret), K(tablegroup_id), K(table));
            }
          } else {
            LOG_WARN("check tablegroup_id in hashmap fail", K(ret), K(tablegroup_id));
          }
        } else if (OB_FAIL(check_if_part_option_equal_v2(table, *table_in_map, is_matched))) {
          LOG_WARN("check if part option equal fail", K(ret), KPC(table_in_map), K(table));
        }
      } else {
        if (OB_FAIL(check_if_part_option_equal_v2(table, *tablegroup, is_matched))) {
          LOG_WARN("check if part option equal fail", K(ret), KPC(tablegroup), K(table));
        }
      }
    }
    if (OB_FAIL(ret) || is_matched) {
W
wangzelin.wzl 已提交
398
      //skip
O
oceanbase-admin 已提交
399 400 401 402
    } else if (OB_FAIL(part_option_not_match_set_.set_refactored(tablegroup_id))) {
      LOG_WARN("set tablegroup_id in hashset fail", K(ret));
    } else {
      LOG_INFO("tables in one tablegroup have different part/subpart option",
W
wangzelin.wzl 已提交
403
               K(tablegroup_id), "table_id", table.get_table_id());
O
oceanbase-admin 已提交
404 405 406 407 408 409
    }
  }
  return ret;
}

// For tablegroups created before 2.0, partition_type and partition_num of tables in tablegroup should be equal
W
wangzelin.wzl 已提交
410
int ObTableGroupChecker::check_if_part_option_equal(const ObTableSchema &t1, const ObTableSchema &t2, bool &is_matched)
O
oceanbase-admin 已提交
411 412 413 414 415 416 417
{
  int ret = OB_SUCCESS;
  is_matched = false;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("tablegroup checker is not init", K(ret));
  } else {
W
wangzelin.wzl 已提交
418 419 420 421 422
    is_matched = t1.get_part_level() == t2.get_part_level()
                 && t1.get_part_option().get_part_func_type() == t2.get_part_option().get_part_func_type()
                 && t1.get_part_option().get_part_num() == t2.get_part_option().get_part_num()
                 && t1.get_sub_part_option().get_part_func_type() == t2.get_sub_part_option().get_part_func_type()
                 && t1.get_sub_part_option().get_part_num() == t2.get_sub_part_option().get_part_num();
O
oceanbase-admin 已提交
423 424
    if (!is_matched) {
      LOG_ERROR("tables in one tablegroup have different part/subpart option",
W
wangzelin.wzl 已提交
425 426 427 428 429 430 431 432 433 434 435 436
                "table1_id", t1.get_table_id(),
                "table1_part_level", t1.get_part_level(),
                "table1_database_id", t1.get_database_id(),
                "table1_tenant_id", t1.get_tenant_id(),
                "table1_part_option", t1.get_part_option(),
                "table1_subpart_option", t1.get_sub_part_option(),
                "table2_id", t2.get_table_id(),
                "table2_part_level", t2.get_part_level(),
                "table2_database_id", t2.get_database_id(),
                "table2_tenant_id", t2.get_tenant_id(),
                "table2_part_option", t2.get_part_option(),
                "table2_subpart_option", t2.get_sub_part_option());
O
oceanbase-admin 已提交
437 438 439 440 441 442
    }
  }
  return ret;
}

// For tablegroup create after 2.0, the check of partition_option is more strict.
W
wangzelin.wzl 已提交
443
// You need to check the partition mode, the number of partitions, the number of expression vectors and the value of the partition point
O
oceanbase-admin 已提交
444
// FIXME:Support non templated subpartition
W
wangzelin.wzl 已提交
445 446
template<typename SCHEMA>
int ObTableGroupChecker::check_if_part_option_equal_v2(const ObTableSchema &table, const SCHEMA &schema, bool &is_matched)
O
oceanbase-admin 已提交
447 448 449
{
  int ret = OB_SUCCESS;
  is_matched = false;
W
wangzelin.wzl 已提交
450 451
  bool is_oracle_mode = false;
  bool schema_is_oracle_mode = false;
O
oceanbase-admin 已提交
452 453 454
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("tablegroup checker is not init", K(ret));
W
wangzelin.wzl 已提交
455 456 457 458
  } else if (OB_FAIL(table.check_if_oracle_compat_mode(is_oracle_mode))) {
    LOG_WARN("fail to check oracle mode", KR(ret), K(table));
  } else if (OB_FAIL(schema.check_if_oracle_compat_mode(schema_is_oracle_mode))) {
    LOG_WARN("fail to check oracle mode", KR(ret), K(schema));
O
oceanbase-admin 已提交
459
  } else {
W
wangzelin.wzl 已提交
460 461 462 463 464 465 466 467
    is_matched = is_oracle_mode == schema_is_oracle_mode
                 && table.get_part_level() == schema.get_part_level()
                 && (table.get_part_option().get_part_func_type() == schema.get_part_option().get_part_func_type()
                     || (is_key_part(table.get_part_option().get_part_func_type()) && is_key_part(schema.get_part_option().get_part_func_type())))
                 && table.get_part_option().get_part_num() == schema.get_part_option().get_part_num()
                 && (table.get_sub_part_option().get_part_func_type() == schema.get_sub_part_option().get_part_func_type()
                     || (is_key_part(table.get_sub_part_option().get_part_func_type()) && is_key_part(schema.get_sub_part_option().get_part_func_type())))
                 && table.get_sub_part_option().get_part_num() == schema.get_sub_part_option().get_part_num();
O
oceanbase-admin 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480
    // check expr value
    if (!is_matched) {
      // skip
    } else if (PARTITION_LEVEL_ONE <= table.get_part_level()) {
      if (table.is_key_part()) {
        int64_t table_expr_num = OB_INVALID_INDEX;
        int64_t schema_expr_num = OB_INVALID_INDEX;
        if (OB_FAIL(table.calc_part_func_expr_num(table_expr_num))) {
          LOG_WARN("fail to get table part_func_expr_num", K(ret));
        } else if (OB_FAIL(schema.calc_part_func_expr_num(schema_expr_num))) {
          LOG_WARN("fail to get schema part_func_expr_num", K(ret));
        }
        is_matched = (table_expr_num == schema_expr_num);
W
wangzelin.wzl 已提交
481 482 483 484
      } else if (table.is_range_part()
                 || table.is_list_part()) {
        if (OB_ISNULL(table.get_part_array())
            || OB_ISNULL(schema.get_part_array())) {
O
oceanbase-admin 已提交
485 486 487 488 489 490 491 492 493
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("partition_array is null", K(ret), K(schema), K(table));
        } else {
          is_matched = true;
          const int64_t table_part_num = table.get_part_option().get_part_num();
          const int64_t schema_part_num = schema.get_part_option().get_part_num();
          const ObPartitionFuncType part_func_type = table.get_part_option().get_part_func_type();
          for (int64_t i = 0; OB_SUCC(ret) && is_matched && i < table_part_num; i++) {
            is_matched = false;
W
wangzelin.wzl 已提交
494
            ObPartition *table_part = table.get_part_array()[i];
O
oceanbase-admin 已提交
495
            for (int64_t j = 0; OB_SUCC(ret) && !is_matched && j < schema_part_num; j++) {
W
wangzelin.wzl 已提交
496
              ObPartition *schema_part = schema.get_part_array()[j];
O
oceanbase-admin 已提交
497 498 499 500
              if (OB_ISNULL(schema_part) || OB_ISNULL(table_part)) {
                ret = OB_ERR_UNEXPECTED;
                LOG_WARN("partition is null", K(ret), KPC(schema_part), KPC(table_part));
              } else if (OB_FAIL(ObPartitionUtils::check_partition_value(
W
wangzelin.wzl 已提交
501
                         is_oracle_mode, *schema_part, *table_part, part_func_type, is_matched))) {
O
oceanbase-admin 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
                LOG_WARN("fail to check partition value", KPC(schema_part), KPC(table_part), K(part_func_type));
              }
            }
          }
        }
      }
    }
    if (OB_SUCC(ret) || !is_matched) {
      // skip
    } else if (PARTITION_LEVEL_TWO == table.get_part_level()) {
      if (table.is_key_subpart()) {
        int64_t table_expr_num = OB_INVALID_INDEX;
        int64_t schema_expr_num = OB_INVALID_INDEX;
        if (OB_FAIL(table.calc_subpart_func_expr_num(table_expr_num))) {
          LOG_WARN("fail to get table subpart_func_expr_num", K(ret));
        } else if (OB_FAIL(schema.calc_subpart_func_expr_num(schema_expr_num))) {
          LOG_WARN("fail to get schema subpart_func_expr_num", K(ret));
        }
        is_matched = (table_expr_num == schema_expr_num);
W
wangzelin.wzl 已提交
521 522 523 524
      } else if (table.is_range_subpart()
                 || table.is_list_subpart()) {
        if (OB_ISNULL(table.get_def_subpart_array())
            || OB_ISNULL(schema.get_def_subpart_array())) {
O
oceanbase-admin 已提交
525 526 527 528 529 530 531 532 533
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("partition_array is null", K(ret), K(schema), K(table));
        } else {
          is_matched = true;
          const int64_t table_sub_part_num = table.get_sub_part_option().get_part_num();
          const int64_t schema_sub_part_num = schema.get_sub_part_option().get_part_num();
          const ObPartitionFuncType part_func_type = table.get_sub_part_option().get_part_func_type();
          for (int64_t i = 0; OB_SUCC(ret) && is_matched && i < table_sub_part_num; i++) {
            is_matched = false;
W
wangzelin.wzl 已提交
534 535 536 537 538 539 540 541 542 543
            ObSubPartition *table_part = table.get_def_subpart_array()[i];
            for (int64_t j = 0; OB_SUCC(ret) && !is_matched && j < schema_sub_part_num; j++) {
              ObSubPartition *schema_part = schema.get_def_subpart_array()[j];
              if (OB_ISNULL(schema_part) || OB_ISNULL(table_part)) {
                ret = OB_ERR_UNEXPECTED;
                LOG_WARN("partition is null", K(ret), KPC(schema_part), KPC(table_part));
              } else if (OB_FAIL(ObPartitionUtils::check_partition_value(
                         is_oracle_mode, *schema_part, *table_part, part_func_type, is_matched))) {
                LOG_WARN("fail to check partition value", KPC(schema_part), KPC(table_part), K(part_func_type));
              }
O
oceanbase-admin 已提交
544 545 546 547 548 549
            }
          }
        }
      }
    }
  }
W
wangzelin.wzl 已提交
550 551
  if (!is_matched) {
      LOG_ERROR("table & tablegroup partition option not match", K(ret), K(table), K(schema));
O
oceanbase-admin 已提交
552 553 554 555
  }
  return ret;
}
////////////////////////////////////////////////////////////////
W
wangzelin.wzl 已提交
556 557 558
ObInspector::ObInspector(ObRootService &rs)
    :ObAsyncTimerTask(rs.get_inspect_task_queue()),
    rs_(rs)
O
oceanbase-admin 已提交
559 560 561 562 563 564 565 566 567
{}

int ObInspector::process()
{
  // @todo ObTaskController::get().switch_task(share::ObTaskType::ROOT_SERVICE);
  int ret = OB_SUCCESS;
  ObTableGroupChecker tablegroup_checker(rs_.get_schema_service());
  ObRootInspection system_schema_checker;
  ObTenantChecker tenant_checker(rs_.get_schema_service(), rs_.get_sql_proxy(), rs_.get_common_rpc_proxy());
W
wangzelin.wzl 已提交
568

O
obdev 已提交
569
  ret = OB_E(EventTable::EN_STOP_ROOT_INSPECTION) OB_SUCCESS;
O
oceanbase-admin 已提交
570 571 572
  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(tablegroup_checker.init())) {
    LOG_WARN("init tablegroup_checker failed", K(ret));
W
wangzelin.wzl 已提交
573 574
  } else if (OB_FAIL(system_schema_checker.init(rs_.get_schema_service(), rs_.get_zone_mgr(),
                                         rs_.get_sql_proxy()))) {
O
oceanbase-admin 已提交
575 576
    LOG_WARN("init root inspection failed", K(ret));
  } else {
W
wangzelin.wzl 已提交
577 578 579 580 581
    ObInspectionTask *inspection_tasks[] = {
      &tablegroup_checker,
      &system_schema_checker,
      &tenant_checker
    };
O
oceanbase-admin 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594
    bool passed = true;
    const char* warning_info = NULL;
    int N = ARRAYSIZEOF(inspection_tasks);
    for (int i = 0; i < N; ++i) {
      passed = true;
      warning_info = NULL;
      int tmp_ret = inspection_tasks[i]->inspect(passed, warning_info);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("inpection task failed", K(tmp_ret), K(i), "task", inspection_tasks[i]->get_task_name());
      } else if (passed) {
        LOG_INFO("inspection task succ", K(i), "task", inspection_tasks[i]->get_task_name());
      } else {
        LOG_ERROR(warning_info);
W
wangzelin.wzl 已提交
595 596
        ROOTSERVICE_EVENT_ADD("inspector", inspection_tasks[i]->get_task_name(),
                              "info", (warning_info == NULL ? "": warning_info));
O
oceanbase-admin 已提交
597 598 599 600 601 602
      }
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
603
ObAsyncTask *ObInspector::deep_copy(char *buf, const int64_t buf_size) const
O
oceanbase-admin 已提交
604
{
W
wangzelin.wzl 已提交
605
  ObInspector *task = NULL;
O
oceanbase-admin 已提交
606
  if (NULL == buf || buf_size < static_cast<int64_t>(sizeof(*this))) {
607
    LOG_WARN_RET(OB_BUF_NOT_ENOUGH, "buffer not large enough", K(buf_size));
O
oceanbase-admin 已提交
608
  } else {
W
wangzelin.wzl 已提交
609
    task = new(buf) ObInspector(rs_);
O
oceanbase-admin 已提交
610 611 612 613 614
  }
  return task;
}

////////////////////////////////////////////////////////////////
W
wangzelin.wzl 已提交
615 616 617
ObPurgeRecyclebinTask::ObPurgeRecyclebinTask(ObRootService &rs)
    :ObAsyncTimerTask(rs.get_inspect_task_queue()),
    root_service_(rs)
O
oceanbase-admin 已提交
618 619 620 621 622 623 624 625 626 627 628
{}

int ObPurgeRecyclebinTask::process()
{
  LOG_INFO("purge recyclebin task begin");
  int ret = OB_SUCCESS;
  const int64_t PURGE_EACH_TIME = 1000;
  int64_t delay = 1 * 60 * 1000 * 1000;
  int64_t expire_time = GCONF.recyclebin_object_expire_time;
  int64_t purge_interval = GCONF._recyclebin_object_purge_frequency;
  if (expire_time > 0 && purge_interval > 0) {
W
wangzelin.wzl 已提交
629
   if (OB_FAIL(root_service_.purge_recyclebin_objects(PURGE_EACH_TIME))) {
O
oceanbase-admin 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
      LOG_WARN("fail to purge recyclebin objects", KR(ret));
    }
    delay = purge_interval;
  }
  // the error code is only for outputtion log, the function will return success.
  // the task no need retry, because it will be triggered periodically.
  if (OB_FAIL(root_service_.schedule_recyclebin_task(delay))) {
    LOG_WARN("schedule purge recyclebin task failed", KR(ret), K(delay));
  } else {
    LOG_INFO("submit purge recyclebin task success", K(delay));
  }
  LOG_INFO("purge recyclebin task end", K(delay));
  return OB_SUCCESS;
}

W
wangzelin.wzl 已提交
645
ObAsyncTask *ObPurgeRecyclebinTask::deep_copy(char *buf, const int64_t buf_size) const
O
oceanbase-admin 已提交
646
{
W
wangzelin.wzl 已提交
647
  ObPurgeRecyclebinTask *task = NULL;
O
oceanbase-admin 已提交
648
  if (NULL == buf || buf_size < static_cast<int64_t>(sizeof(*this))) {
649
    LOG_WARN_RET(OB_BUF_NOT_ENOUGH, "buffer not large enough", K(buf_size));
O
oceanbase-admin 已提交
650
  } else {
W
wangzelin.wzl 已提交
651
    task = new(buf) ObPurgeRecyclebinTask(root_service_);
O
oceanbase-admin 已提交
652 653 654 655
  }
  return task;
}

W
wangzelin.wzl 已提交
656 657 658
ObRootInspection::ObRootInspection()
  : inited_(false), stopped_(false), zone_passed_(false),
    sys_param_passed_(false), sys_stat_passed_(false),
659 660 661 662
    sys_table_schema_passed_(false), data_version_passed_(false),
    all_checked_(false), all_passed_(false), can_retry_(false),
    sql_proxy_(NULL), rpc_proxy_(NULL), schema_service_(NULL),
    zone_mgr_(NULL)
O
oceanbase-admin 已提交
663 664 665
{
}

W
wangzelin.wzl 已提交
666
ObRootInspection::~ObRootInspection()
O
oceanbase-admin 已提交
667 668 669
{
}

W
wangzelin.wzl 已提交
670 671 672 673
int ObRootInspection::init(ObMultiVersionSchemaService &schema_service,
                           ObZoneManager &zone_mgr,
                           ObMySQLProxy &sql_proxy,
                           obrpc::ObCommonRpcProxy *rpc_proxy)
O
oceanbase-admin 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687
{
  int ret = OB_SUCCESS;
  if (inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("init twice", K(ret));
  } else {
    schema_service_ = &schema_service;
    zone_mgr_ = &zone_mgr;
    sql_proxy_ = &sql_proxy;
    stopped_ = false;
    zone_passed_ = false;
    sys_param_passed_ = false;
    sys_stat_passed_ = false;
    sys_table_schema_passed_ = false;
688
    data_version_passed_ = false;
O
oceanbase-admin 已提交
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
    all_checked_ = false;
    all_passed_ = false;
    can_retry_ = false;
    rpc_proxy_ = rpc_proxy;
    inited_ = true;
  }
  return ret;
}

int ObRootInspection::check_all()
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", K(ret));
  } else if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("schema_service is null", K(ret));
  } else if (!schema_service_->is_tenant_full_schema(OB_SYS_TENANT_ID)) {
    ret = OB_EAGAIN;
    LOG_WARN("schema is not ready, try again", K(ret));
  } else {
    can_retry_ = false;
    int tmp = OB_SUCCESS;

    // check __all_zone
    if (OB_SUCCESS != (tmp = check_zone())) {
      LOG_WARN("check_zone failed", K(tmp));
      ret = (OB_SUCCESS == ret) ? tmp : ret;
    }
    zone_passed_ = (OB_SUCCESS == tmp);

    // check sys stat
    tmp = OB_SUCCESS;
725
    if (OB_SUCCESS != (tmp = check_sys_stat_())) {
O
oceanbase-admin 已提交
726 727 728 729 730 731 732
      LOG_WARN("check_sys_stat failed", K(tmp));
      ret = (OB_SUCCESS == ret) ? tmp : ret;
    }
    sys_stat_passed_ = (OB_SUCCESS == tmp);

    // check sys param
    tmp = OB_SUCCESS;
733
    if (OB_SUCCESS != (tmp = check_sys_param_())) {
O
oceanbase-admin 已提交
734 735 736 737 738 739 740
      LOG_WARN("check_sys_param failed", K(tmp));
      ret = (OB_SUCCESS == ret) ? tmp : ret;
    }
    sys_param_passed_ = (OB_SUCCESS == tmp);

    // check sys schema
    tmp = OB_SUCCESS;
741
    if (OB_SUCCESS != (tmp = check_sys_table_schemas_())) {
O
oceanbase-admin 已提交
742 743 744 745 746
      LOG_WARN("check_sys_table_schemas failed", K(tmp));
      ret = (OB_SUCCESS == ret) ? tmp : ret;
    }
    sys_table_schema_passed_ = (OB_SUCCESS == tmp);

747 748 749 750 751 752 753 754
    // check tenant's data version
    tmp = OB_SUCCESS;
    if (OB_SUCCESS != (tmp = check_data_version_())) {
      LOG_WARN("check_data_version failed", K(tmp));
      ret = (OB_SUCCESS == ret) ? tmp : ret;
    }
    data_version_passed_ = (OB_SUCCESS == tmp);

O
oceanbase-admin 已提交
755 756 757 758 759 760 761 762 763 764 765 766
    // upgrade job may still running, in order to avoid the upgrade process error stuck,
    // ignore the 4674 error
    for (int64_t i = 0; i < UPGRADE_JOB_TYPE_COUNT; i++) {
      tmp = OB_SUCCESS;
      ObRsJobType job_type = upgrade_job_type_array[i];
      if (job_type > JOB_TYPE_INVALID && job_type < JOB_TYPE_MAX) {
        if (OB_SUCCESS != (tmp = ObUpgradeUtils::check_upgrade_job_passed(job_type))) {
          LOG_WARN("fail to check upgrade job passed", K(tmp), K(job_type));
          if (OB_RUN_JOB_NOT_SUCCESS != tmp) {
            ret = (OB_SUCCESS == ret) ? tmp : ret;
          } else {
            LOG_WARN("upgrade job may still running, check with __all_virtual_uprade_inspection",
W
wangzelin.wzl 已提交
767
                     K(ret), K(tmp), "job_type", ObRsJobTableOperator::get_job_type_str(job_type));
O
oceanbase-admin 已提交
768 769 770 771 772 773 774 775 776 777 778
          }
        }
      }
    }

    all_checked_ = true;
    all_passed_ = OB_SUCC(ret);
  }
  return ret;
}

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
int ObRootInspection::check_tenant(const uint64_t tenant_id)
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", KR(ret));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", KR(ret));
  } else {
    int tmp_ret = OB_SUCCESS;
    if (OB_TMP_FAIL(check_sys_stat_(tenant_id))) {
      LOG_WARN("fail to check sys stat", KR(tmp_ret), K(tenant_id));
      ret = OB_SUCC(ret) ? tmp_ret : ret;
    }
    if (OB_TMP_FAIL(check_sys_param_(tenant_id))) {
      LOG_WARN("fail to check param", KR(tmp_ret), K(tenant_id));
      ret = OB_SUCC(ret) ? tmp_ret : ret;
    }
    if (OB_TMP_FAIL(check_sys_table_schemas_(tenant_id))) {
      LOG_WARN("fail to check sys table", KR(tmp_ret), K(tenant_id));
      ret = OB_SUCC(ret) ? tmp_ret : ret;
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
805
int ObRootInspection::inspect(bool &passed, const char* &warning_info)
O
oceanbase-admin 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819
{
  int ret = OB_SUCCESS;
  if (!GCONF.in_upgrade_mode()) {
    ret = check_all();
    if (OB_SUCC(ret)) {
      passed = all_passed_;
      warning_info = "system metadata error";
    }
  } else {
    passed = true;
  }
  return ret;
}

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

// standby tenant may stay at lower data version,
// root_inspection won't check standby tenant's schema.
int ObRootInspection::construct_tenant_ids_(
    common::ObIArray<uint64_t> &tenant_ids)
{
  int ret = OB_SUCCESS;
  tenant_ids.reset();
  ObArray<uint64_t> standby_tenants;
  ObArray<uint64_t> tmp_tenants;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", KR(ret));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", KR(ret));
  } else if (OB_FAIL(ObTenantUtils::get_tenant_ids(schema_service_, tmp_tenants))) {
    LOG_WARN("get_tenant_ids failed", KR(ret));
  } else {
T
tino247 已提交
838
    bool is_standby = false;
839 840
    for (int64_t i = 0; OB_SUCC(ret) && i < tmp_tenants.count(); i++) {
      const uint64_t tenant_id = tmp_tenants.at(i);
T
tino247 已提交
841 842 843
      if (OB_FAIL(ObAllTenantInfoProxy::is_standby_tenant(sql_proxy_, tenant_id, is_standby))) {
        LOG_WARN("fail to check is standby tenant", KR(ret), K(tenant_id));
      } else if (is_standby) {
844 845 846 847 848 849 850 851 852
        // skip
      } else if (OB_FAIL(tenant_ids.push_back(tenant_id))) {
        LOG_WARN("fail to push back tenant_id", KR(ret), K(tenant_id));
      }
    } // end for
  }
  return ret;
}

O
oceanbase-admin 已提交
853 854 855 856
int ObRootInspection::check_zone()
{
  int ret = OB_SUCCESS;
  ObSqlString extra_cond;
W
wangzelin.wzl 已提交
857
  HEAP_VAR(ObGlobalInfo, global_zone_info) {
O
oceanbase-admin 已提交
858
    ObArray<ObZoneInfo> zone_infos;
W
wangzelin.wzl 已提交
859
    ObArray<const char *> global_zone_item_names;
O
oceanbase-admin 已提交
860 861 862 863 864 865 866 867 868 869
    if (!inited_) {
      ret = OB_NOT_INIT;
      LOG_WARN("not init", K(ret));
    } else if (OB_FAIL(check_cancel())) {
      LOG_WARN("check_cancel failed", K(ret));
    } else if (OB_FAIL(extra_cond.assign_fmt("zone = '%s'", global_zone_info.zone_.ptr()))) {
      LOG_WARN("extra_cond assign_fmt failed", K(ret));
    } else if (OB_FAIL(get_names(global_zone_info.list_, global_zone_item_names))) {
      LOG_WARN("get global zone item names failed", K(ret));
    } else if (OB_FAIL(check_names(OB_SYS_TENANT_ID, OB_ALL_ZONE_TNAME, global_zone_item_names, extra_cond))) {
W
wangzelin.wzl 已提交
870 871
      LOG_WARN("check global zone item names failed", "table_name", OB_ALL_ZONE_TNAME,
          K(global_zone_item_names), K(extra_cond), K(ret));
O
oceanbase-admin 已提交
872 873 874
    } else if (OB_FAIL(zone_mgr_->get_zone(zone_infos))) {
      LOG_WARN("zone manager get_zone failed", K(ret));
    } else {
W
wangzelin.wzl 已提交
875 876
      ObArray<const char *> zone_item_names;
      FOREACH_CNT_X(zone_info, zone_infos, OB_SUCCESS == ret) {
O
oceanbase-admin 已提交
877 878 879 880 881 882 883 884 885
        zone_item_names.reuse();
        extra_cond.reset();
        if (OB_FAIL(check_cancel())) {
          LOG_WARN("check_cancel failed", K(ret));
        } else if (OB_FAIL(extra_cond.assign_fmt("zone = '%s'", zone_info->zone_.ptr()))) {
          LOG_WARN("extra_cond assign_fmt failed", K(ret));
        } else if (OB_FAIL(get_names(zone_info->list_, zone_item_names))) {
          LOG_WARN("get zone item names failed", K(ret));
        } else if (OB_FAIL(check_names(OB_SYS_TENANT_ID, OB_ALL_ZONE_TNAME, zone_item_names, extra_cond))) {
W
wangzelin.wzl 已提交
886 887
          LOG_WARN("check zone item names failed", "table_name", OB_ALL_ZONE_TNAME,
              K(zone_item_names), "zone_info", *zone_info, K(extra_cond), K(ret));
O
oceanbase-admin 已提交
888 889 890 891 892 893 894
        }
      }
    }
  }
  return ret;
}

895
int ObRootInspection::check_sys_stat_()
O
oceanbase-admin 已提交
896 897 898 899 900
{
  int ret = OB_SUCCESS;
  ObArray<uint64_t> tenant_ids;
  if (!inited_) {
    ret = OB_NOT_INIT;
901
    LOG_WARN("not init", KR(ret));
O
oceanbase-admin 已提交
902 903
  } else if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
904
    LOG_WARN("schema_service is null", KR(ret));
O
oceanbase-admin 已提交
905
  } else if (OB_FAIL(check_cancel())) {
906
    LOG_WARN("check_cancel failed", KR(ret));
907
  } else if (OB_FAIL(construct_tenant_ids_(tenant_ids))) {
908
    LOG_WARN("get_tenant_ids failed", KR(ret));
O
oceanbase-admin 已提交
909
  } else {
910 911 912
    int backup_ret = OB_SUCCESS;
    int tmp_ret = OB_SUCCESS;
    FOREACH_CNT_X(tenant_id, tenant_ids, OB_SUCC(ret)) {
O
oceanbase-admin 已提交
913
      if (OB_FAIL(check_cancel())) {
914 915 916 917
        LOG_WARN("check_cancel failed", KR(ret));
      } else if (OB_TMP_FAIL(check_sys_stat_(*tenant_id))) {
        LOG_WARN("fail to check sys stat", KR(tmp_ret), K(*tenant_id));
        backup_ret = OB_SUCCESS == backup_ret ? tmp_ret : backup_ret;
O
oceanbase-admin 已提交
918
      }
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
    } // end foreach
    ret = OB_SUCC(ret) ? backup_ret : ret;
  }
  return ret;
}

int ObRootInspection::check_sys_stat_(const uint64_t tenant_id)
{
  int ret = OB_SUCCESS;
  ObArray<const char *> sys_stat_names;
  ObSqlString extra_cond;
  ObSysStat sys_stat;
  const uint64_t exec_tenant_id = ObSchemaUtils::get_exec_tenant_id(tenant_id);
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", KR(ret));
  } else if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("schema_service is null", KR(ret));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", KR(ret));
T
tino247 已提交
940 941
  } else if (OB_FAIL(check_tenant_status_(tenant_id))) {
    LOG_WARN("fail to check tenant status", KR(ret), K(tenant_id));
942 943 944 945 946 947 948 949 950 951
  } else if (OB_FAIL(sys_stat.set_initial_values(tenant_id))) {
    LOG_WARN("set initial values failed", KR(ret), K(tenant_id));
  } else if (OB_FAIL(extra_cond.assign_fmt("tenant_id = %lu",
             ObSchemaUtils::get_extract_tenant_id(exec_tenant_id, tenant_id)))) {
    LOG_WARN("extra_cond assign_fmt failed", KR(ret), K(tenant_id));
  } else if (OB_FAIL(get_names(sys_stat.item_list_, sys_stat_names))) {
    LOG_WARN("get sys stat names failed", KR(ret), K(tenant_id));
  } else if (OB_FAIL(check_names(tenant_id, OB_ALL_SYS_STAT_TNAME, sys_stat_names, extra_cond))) {
    LOG_WARN("check all sys stat names failed", K(ret), K(tenant_id),
             "table_name", OB_ALL_SYS_STAT_TNAME, K(sys_stat_names));
O
oceanbase-admin 已提交
952 953 954 955
  }
  return ret;
}

956
int ObRootInspection::check_sys_param_()
O
oceanbase-admin 已提交
957 958 959 960 961
{
  int ret = OB_SUCCESS;
  ObArray<uint64_t> tenant_ids;
  if (!inited_) {
    ret = OB_NOT_INIT;
962
    LOG_WARN("not init", KR(ret));
O
oceanbase-admin 已提交
963 964
  } else if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
965
    LOG_WARN("schema_service is null", KR(ret));
O
oceanbase-admin 已提交
966
  } else if (OB_FAIL(check_cancel())) {
967
    LOG_WARN("check_cancel failed", KR(ret));
968
  } else if (OB_FAIL(construct_tenant_ids_(tenant_ids))) {
969
    LOG_WARN("get_tenant_ids failed", KR(ret));
O
oceanbase-admin 已提交
970
  } else {
971 972 973
    int backup_ret = OB_SUCCESS;
    int tmp_ret = OB_SUCCESS;
    FOREACH_CNT_X(tenant_id, tenant_ids, OB_SUCC(ret)) {
O
oceanbase-admin 已提交
974 975
      if (OB_FAIL(check_cancel())) {
        LOG_WARN("check_cancel failed", K(ret));
976 977 978
      } else if (OB_TMP_FAIL(check_sys_param_(*tenant_id))) {
        LOG_WARN("fail to check sys param", KR(tmp_ret), K(*tenant_id));
        backup_ret = OB_SUCCESS == backup_ret ? tmp_ret : backup_ret;
O
oceanbase-admin 已提交
979
      }
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    } // end foreach
    ret = OB_SUCC(ret) ? backup_ret : ret;
  }
  return ret;
}

int ObRootInspection::check_sys_param_(const uint64_t tenant_id)
{
  int ret = OB_SUCCESS;
  ObArray<uint64_t> tenant_ids;
  ObArray<const char *> sys_param_names;
  ObSqlString extra_cond;
  const uint64_t exec_tenant_id = ObSchemaUtils::get_exec_tenant_id(tenant_id);
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", KR(ret));
  } else if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("schema_service is null", KR(ret));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", KR(ret));
T
tino247 已提交
1001 1002
  } else if (OB_FAIL(check_tenant_status_(tenant_id))) {
    LOG_WARN("fail to check tenant status", KR(ret), K(tenant_id));
1003 1004 1005 1006 1007 1008 1009 1010 1011
  } else if (OB_FAIL(extra_cond.assign_fmt("tenant_id = %lu",
             ObSchemaUtils::get_extract_tenant_id(exec_tenant_id, tenant_id)))) {
    LOG_WARN("extra_cond assign_fmt failed", KR(ret), K(tenant_id));
  } else if (OB_FAIL(get_sys_param_names(sys_param_names))) {
    LOG_WARN("get sys param names failed", KR(ret), K(tenant_id));
  } else if (OB_FAIL(check_names(tenant_id, OB_ALL_SYS_VARIABLE_TNAME,
             sys_param_names, extra_cond))) {
    LOG_WARN("check all sys params names failed", KR(ret), K(tenant_id),
             "table_name", OB_ALL_SYS_VARIABLE_TNAME, K(sys_param_names), K(extra_cond));
O
oceanbase-admin 已提交
1012 1013 1014
  }
  if (OB_SCHEMA_ERROR != ret) {
  } else if (GCONF.in_upgrade_mode()) {
1015
    LOG_WARN("check sys_variable failed", KR(ret));
O
oceanbase-admin 已提交
1016
  } else {
1017
    LOG_DBA_ERROR(OB_ERR_ROOT_INSPECTION, "msg", "system variables are unmatched", KR(ret));
O
oceanbase-admin 已提交
1018 1019 1020 1021
  }
  return ret;
}

W
wangzelin.wzl 已提交
1022 1023
template<typename Item>
int ObRootInspection::get_names(const ObDList<Item> &list, ObIArray<const char*> &names)
O
oceanbase-admin 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (list.get_size() <= 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("list is empty", K(ret));
  } else {
W
wangzelin.wzl 已提交
1033
    const Item *it = list.get_first();
O
oceanbase-admin 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
    while (OB_SUCCESS == ret && it != list.get_header()) {
      if (OB_FAIL(names.push_back(it->name_))) {
        LOG_WARN("push_back failed", K(ret));
      } else {
        it = it->get_next();
      }
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
1045
int ObRootInspection::get_sys_param_names(ObIArray<const char *> &names)
O
oceanbase-admin 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    const int64_t param_count = ObSysVariables::get_amount();
    for (int64_t i = 0; OB_SUCC(ret) && i < param_count; ++i) {
      if (OB_FAIL(names.push_back(ObSysVariables::get_name(i).ptr()))) {
        LOG_WARN("push_back failed", K(ret));
      }
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
1062 1063 1064 1065
int ObRootInspection::check_names(const uint64_t tenant_id,
                                  const char *table_name,
                                  const ObIArray<const char *> &names,
                                  const ObSqlString &extra_cond)
O
oceanbase-admin 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", K(ret));
  } else if (NULL == table_name || names.count() <= 0) {
    // extra_cond can be empty, so wo don't check it here
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("table_name is null or names is empty", KP(table_name), K(names), K(ret));
  } else {
W
wangzelin.wzl 已提交
1078 1079 1080 1081 1082
    ObArray<Name> fetch_names; // Get the data of the internal table
    ObArray<Name> extra_names; // data inner table more than hard code
    ObArray<Name> miss_names; // data inner table less than hard code
    if (OB_FAIL(calc_diff_names(tenant_id, table_name, names, extra_cond,
        fetch_names, extra_names, miss_names))) {
O
oceanbase-admin 已提交
1083 1084 1085 1086
      LOG_WARN("fail to calc diff names", K(ret), KP(table_name), K(names), K(extra_cond));
    } else {
      if (fetch_names.count() <= 0) {
        // don't need to set ret
W
wangzelin.wzl 已提交
1087
        LOG_WARN("maybe tenant or zone has been deleted, ignore it",
1088
                 K(tenant_id), K(table_name), K(extra_cond));
O
oceanbase-admin 已提交
1089 1090 1091
      } else {
        if (extra_names.count() > 0) {
          // don't need to set ret
W
wangzelin.wzl 已提交
1092
          LOG_WARN("some item exist in table, but not hard coded",
1093
                   K(tenant_id), K(table_name), K(extra_names));
O
oceanbase-admin 已提交
1094 1095 1096
        }
        if (miss_names.count() > 0) {
          ret = OB_ENTRY_NOT_EXIST;
W
wangzelin.wzl 已提交
1097
          LOG_WARN("some item exist in hard code, but not exist in inner table",
1098
                   K(ret), K(tenant_id), K(table_name), K(miss_names));
O
oceanbase-admin 已提交
1099 1100 1101 1102 1103 1104 1105
        }
      }
    }
  }
  return ret;
}

W
wangzelin.wzl 已提交
1106 1107 1108 1109 1110 1111 1112
int ObRootInspection::calc_diff_names(const uint64_t tenant_id,
                                      const char *table_name,
                                      const ObIArray<const char *> &names,
                                      const ObSqlString &extra_cond,
                                      ObIArray<Name> &fetch_names, /* data reading from inner table*/
                                      ObIArray<Name> &extra_names, /* data inner table more than hard code*/
                                      ObIArray<Name> &miss_names /* data inner table less than hard code*/)
O
oceanbase-admin 已提交
1113 1114
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
1115 1116 1117
  ObRefreshSchemaStatus schema_status;
  schema_status.tenant_id_ = tenant_id;
  fetch_names.reset();
O
oceanbase-admin 已提交
1118 1119 1120
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
W
wangzelin.wzl 已提交
1121
  } else if (OB_INVALID_TENANT_ID == tenant_id) {
O
oceanbase-admin 已提交
1122
    ret = OB_INVALID_ARGUMENT;
W
wangzelin.wzl 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
    LOG_WARN("invalid tenant_id", KR(ret), K(tenant_id));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", KR(ret), K(tenant_id));
  } else if (NULL == table_name || names.count() <= 0) {
    // extra_cond can be empty, don't need to check it
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("table_name is null or names is empty",
             KR(ret), K(tenant_id), KP(table_name), K(names));
  } else if (GCTX.is_standby_cluster() && is_user_tenant(tenant_id)) {
    if (OB_ISNULL(GCTX.schema_status_proxy_)) {
O
oceanbase-admin 已提交
1133 1134 1135
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("schema status proxy is null", K(ret));
    } else if (OB_FAIL(GCTX.schema_status_proxy_->get_refresh_schema_status(tenant_id, schema_status))) {
W
wangzelin.wzl 已提交
1136
      LOG_WARN("fail to get schema status", KR(ret), K(tenant_id));
O
oceanbase-admin 已提交
1137 1138 1139
    }
  }

W
wangzelin.wzl 已提交
1140 1141
  if (OB_SUCC(ret)) {
    const uint64_t exec_tenant_id = schema_status.tenant_id_;
O
oceanbase-admin 已提交
1142 1143
    int64_t snapshot_timestamp = schema_status.snapshot_timestamp_;
    ObSqlString sql;
W
wangzelin.wzl 已提交
1144 1145 1146 1147 1148
    ObSQLClientRetryWeak sql_client_retry_weak(sql_proxy_,
                                               snapshot_timestamp);
    if (OB_FAIL(sql.append_fmt("SELECT name FROM %s%s%s", table_name,
        (extra_cond.empty()) ? "" : " WHERE ", extra_cond.ptr()))) {
      LOG_WARN("append_fmt failed", KR(ret), K(tenant_id), K(table_name), K(extra_cond));
O
oceanbase-admin 已提交
1149
    } else {
W
wangzelin.wzl 已提交
1150 1151
      SMART_VAR(ObMySQLProxy::MySQLResult, res) {
        ObMySQLResult *result = NULL;
O
oceanbase-admin 已提交
1152
        if (OB_FAIL(sql_client_retry_weak.read(res, exec_tenant_id, sql.ptr()))) {
W
wangzelin.wzl 已提交
1153
          LOG_WARN("execute sql failed", KR(ret), K(tenant_id), K(sql));
O
oceanbase-admin 已提交
1154
          can_retry_ = true;
W
wangzelin.wzl 已提交
1155
        } else if (OB_ISNULL(result = res.get_result())) {
O
oceanbase-admin 已提交
1156
          ret = OB_ERR_UNEXPECTED;
W
wangzelin.wzl 已提交
1157 1158
          LOG_WARN("result is not expected to be NULL",
                   KR(ret), K(tenant_id), "result", OB_P(result));
O
oceanbase-admin 已提交
1159
        } else {
W
wangzelin.wzl 已提交
1160 1161
          //only for filling the out parameter,
          //Ensure that there is no '\ 0' character in the middle of the corresponding string
O
oceanbase-admin 已提交
1162 1163 1164 1165 1166 1167 1168 1169
          int64_t tmp_real_str_len = 0;
          Name name;
          while (OB_SUCC(ret)) {
            if (OB_FAIL(result->next())) {
              if (OB_ITER_END == ret) {
                ret = OB_SUCCESS;
                break;
              } else {
W
wangzelin.wzl 已提交
1170
                LOG_WARN("get next result failed", KR(ret), K(tenant_id));
O
oceanbase-admin 已提交
1171 1172
              }
            } else {
W
wangzelin.wzl 已提交
1173 1174 1175
              EXTRACT_STRBUF_FIELD_MYSQL(*result, "name", name.ptr(),
                  static_cast<int64_t>(NAME_BUF_LEN), tmp_real_str_len);
              (void) tmp_real_str_len; // make compiler happy
O
oceanbase-admin 已提交
1176
              if (OB_FAIL(fetch_names.push_back(name))) {
W
wangzelin.wzl 已提交
1177
                LOG_WARN("push_back failed", KR(ret), K(tenant_id));
O
oceanbase-admin 已提交
1178 1179 1180 1181 1182 1183 1184 1185
              }
            }
          }
        }
      }
    }
    if (OB_SUCC(ret)) {
      if (fetch_names.count() <= 0) {
W
wangzelin.wzl 已提交
1186 1187
        LOG_WARN("maybe tenant or zone has been deleted, ignore it",
                 KR(ret), K(schema_status), K(table_name), K(extra_cond));
O
oceanbase-admin 已提交
1188 1189 1190
      } else {
        extra_names.reset();
        miss_names.reset();
W
wangzelin.wzl 已提交
1191
        FOREACH_CNT_X(fetch_name, fetch_names, OB_SUCCESS == ret) {
O
oceanbase-admin 已提交
1192
          bool found = false;
W
wangzelin.wzl 已提交
1193
          FOREACH_CNT_X(name, names, OB_SUCC(ret)) {
O
oceanbase-admin 已提交
1194 1195 1196 1197 1198 1199 1200
            if (Name(*name) == *fetch_name) {
              found = true;
              break;
            }
          }
          if (!found) {
            if (OB_FAIL(extra_names.push_back(*fetch_name))) {
W
wangzelin.wzl 已提交
1201 1202
              LOG_WARN("fail to push name into fetch_names",
                       KR(ret), K(tenant_id), K(*fetch_name), K(fetch_names));
O
oceanbase-admin 已提交
1203 1204 1205
            }
          }
        }
W
wangzelin.wzl 已提交
1206
        FOREACH_CNT_X(name, names, OB_SUCCESS == ret) {
O
oceanbase-admin 已提交
1207
          bool found = false;
W
wangzelin.wzl 已提交
1208
          FOREACH_CNT_X(fetch_name, fetch_names, OB_SUCCESS == ret) {
O
oceanbase-admin 已提交
1209 1210 1211 1212 1213 1214 1215
            if (Name(*name) == *fetch_name) {
              found = true;
              break;
            }
          }
          if (!found) {
            if (OB_FAIL(miss_names.push_back(Name(*name)))) {
W
wangzelin.wzl 已提交
1216 1217
              LOG_WARN("fail to push name into miss_names",
                       KR(ret), K(tenant_id), K(*name), K(miss_names));
O
oceanbase-admin 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226
            }
          }
        }
      }
    }
  }
  return ret;
}

1227
int ObRootInspection::check_sys_table_schemas_()
O
oceanbase-admin 已提交
1228 1229
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
1230 1231
  ObArray<uint64_t> tenant_ids;
  if (OB_UNLIKELY(!inited_)) {
O
oceanbase-admin 已提交
1232
    ret = OB_NOT_INIT;
W
wangzelin.wzl 已提交
1233
    LOG_WARN("not init", KR(ret));
O
oceanbase-admin 已提交
1234
  } else if (OB_FAIL(check_cancel())) {
W
wangzelin.wzl 已提交
1235 1236 1237 1238
    LOG_WARN("check_cancel failed", KR(ret));
  } else if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("schema_service is null", KR(ret));
1239
  } else if (OB_FAIL(construct_tenant_ids_(tenant_ids))) {
W
wangzelin.wzl 已提交
1240 1241
    LOG_WARN("get_tenant_ids failed", KR(ret));
  } else {
1242 1243 1244 1245 1246 1247
    int backup_ret = OB_SUCCESS;
    int tmp_ret = OB_SUCCESS;
    FOREACH_X(tenant_id, tenant_ids, OB_SUCC(ret)) {
      if (OB_FAIL(check_cancel())) {
        LOG_WARN("check_cancel failed", KR(ret));
      } else if (OB_TMP_FAIL(check_sys_table_schemas_(*tenant_id))) {
W
wangzelin.wzl 已提交
1248
        LOG_WARN("fail to check sys table schemas by tenant", KR(tmp_ret), K(*tenant_id));
1249
        backup_ret = OB_SUCCESS == backup_ret ? tmp_ret : backup_ret;
W
wangzelin.wzl 已提交
1250
      }
1251 1252
    } // end foreach
    ret = OB_SUCC(ret) ? backup_ret : ret;
W
wangzelin.wzl 已提交
1253 1254 1255 1256 1257 1258 1259 1260
  }
  return ret;
}

int ObRootInspection::check_sys_table_schemas_(
    const uint64_t tenant_id)
{
  int ret = OB_SUCCESS;
1261
  int64_t schema_version = OB_INVALID_VERSION;
W
wangzelin.wzl 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
  if (OB_UNLIKELY(!inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", KR(ret));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", KR(ret));
  } else if (OB_UNLIKELY(
             is_virtual_tenant_id(tenant_id)
             || OB_INVALID_TENANT_ID == tenant_id)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid tenant_id", KR(ret), K(tenant_id));
O
oceanbase-admin 已提交
1272
  } else {
W
wangzelin.wzl 已提交
1273 1274 1275 1276 1277
    const schema_create_func *creator_ptr_array[] = {
      share::all_core_table_schema_creator,
      share::core_table_schema_creators,
      share::sys_table_schema_creators,
      share::virtual_table_schema_creators,
O
obdev 已提交
1278
      share::sys_view_schema_creators,
O
obdev 已提交
1279 1280
      share::core_index_table_schema_creators,
      share::sys_index_table_schema_creators,
O
obdev 已提交
1281
      NULL };
O
oceanbase-admin 已提交
1282 1283

    int back_ret = OB_SUCCESS;
1284
    int tmp_ret = OB_SUCCESS;
O
oceanbase-admin 已提交
1285
    ObTableSchema table_schema;
W
wangzelin.wzl 已提交
1286 1287 1288 1289 1290
    bool exist = false;
    for (const schema_create_func **creator_ptr_ptr = creator_ptr_array;
         OB_SUCC(ret) && OB_NOT_NULL(*creator_ptr_ptr); ++creator_ptr_ptr) {
      for (const schema_create_func *creator_ptr = *creator_ptr_ptr;
           OB_SUCC(ret) && OB_NOT_NULL(*creator_ptr); ++creator_ptr) {
O
oceanbase-admin 已提交
1291 1292
        table_schema.reset();
        if (OB_FAIL(check_cancel())) {
W
wangzelin.wzl 已提交
1293
          LOG_WARN("check_cancel failed", KR(ret));
T
tino247 已提交
1294 1295
        } else if (OB_FAIL(check_tenant_status_(tenant_id))) {
          LOG_WARN("fail to check tenant status", KR(ret), K(tenant_id));
O
oceanbase-admin 已提交
1296
        } else if (OB_FAIL((*creator_ptr)(table_schema))) {
W
wangzelin.wzl 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
          LOG_WARN("create table schema failed", KR(ret));
        } else if (!is_sys_tenant(tenant_id)
                   && OB_FAIL(ObSchemaUtils::construct_tenant_space_full_table(
                              tenant_id, table_schema))) {
          LOG_WARN("fail to construct tenant space table", KR(ret), K(tenant_id));
        } else if (OB_FAIL(ObSysTableChecker::is_inner_table_exist(
                   tenant_id, table_schema, exist))) {
          LOG_WARN("fail to check inner table exist",
                   KR(ret), K(tenant_id), K(table_schema));
        } else if (!exist) {
          // skip
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
        } else {
          if (OB_TMP_FAIL(check_table_schema(tenant_id, table_schema))) {
            // don't print table_schema, otherwise log will be too much
            LOG_WARN("check table schema failed", KR(tmp_ret), K(tenant_id),
                     "table_id", table_schema.get_table_id(), "table_name", table_schema.get_table_name());
            back_ret = OB_SUCCESS == back_ret ? tmp_ret : back_ret;
          }

          if (OB_TMP_FAIL(check_sys_view_(tenant_id, table_schema))) {
            LOG_WARN("check sys view failed", KR(tmp_ret), K(tenant_id),
                     "table_id", table_schema.get_table_id(), "table_name", table_schema.get_table_name());
            back_ret = OB_SUCCESS == back_ret ? tmp_ret : back_ret;
T
tino247 已提交
1320 1321 1322 1323
            // sql may has occur other error except OB_SCHEMA_ERROR, we should not continue is such situation.
            if (OB_SCHEMA_ERROR != tmp_ret) {
              ret = OB_SUCC(ret) ? back_ret : tmp_ret;
            }
1324
          }
O
oceanbase-admin 已提交
1325
        }
1326 1327 1328
      } // end for
    } // end for
    ret = OB_SUCC(ret) ? back_ret : ret;
O
oceanbase-admin 已提交
1329 1330 1331
  }
  if (OB_SCHEMA_ERROR != ret) {
  } else if (GCONF.in_upgrade_mode()) {
W
wangzelin.wzl 已提交
1332
    LOG_WARN("check sys table schema failed", KR(ret), K(tenant_id));
O
oceanbase-admin 已提交
1333
  } else {
W
wangzelin.wzl 已提交
1334
    LOG_ERROR("check sys table schema failed", KR(ret), K(tenant_id));
1335
    LOG_DBA_ERROR(OB_ERR_ROOT_INSPECTION, "msg", "inner tables are unmatched", KR(ret), K(tenant_id));
O
oceanbase-admin 已提交
1336 1337 1338 1339
  }
  return ret;
}

W
wangzelin.wzl 已提交
1340 1341 1342
int ObRootInspection::check_table_schema(
    const uint64_t tenant_id,
    const ObTableSchema &hard_code_table)
O
oceanbase-admin 已提交
1343 1344
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
1345
  const ObTableSchema *table = NULL;
O
oceanbase-admin 已提交
1346
  ObSchemaGetterGuard schema_guard;
W
wangzelin.wzl 已提交
1347
  if (OB_UNLIKELY(!inited_)) {
O
oceanbase-admin 已提交
1348
    ret = OB_NOT_INIT;
W
wangzelin.wzl 已提交
1349 1350 1351 1352
    LOG_WARN("not init", KR(ret));
  } else if (OB_UNLIKELY(
             is_virtual_tenant_id(tenant_id)
             || OB_INVALID_TENANT_ID == tenant_id)) {
O
oceanbase-admin 已提交
1353
    ret = OB_INVALID_ARGUMENT;
W
wangzelin.wzl 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
    LOG_WARN("invalid tenant_id", KR(ret), K(tenant_id));
  } else if (OB_UNLIKELY(!hard_code_table.is_valid())) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid table_schema", KR(ret), K(tenant_id), K(hard_code_table));
  } else if (OB_FAIL(schema_service_->get_tenant_schema_guard(tenant_id, schema_guard))) {
    LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
  } else if (OB_FAIL(schema_guard.get_table_schema(
             tenant_id, hard_code_table.get_table_id(), table))) {
    LOG_WARN("get_table_schema failed", KR(ret), K(tenant_id),
             "table_id", hard_code_table.get_table_id(),
             "table_name", hard_code_table.get_table_name());
O
oceanbase-admin 已提交
1365 1366
    // fail may cause by load table schema sql, set retry flag.
    can_retry_ = true;
W
wangzelin.wzl 已提交
1367
  } else if (OB_ISNULL(table)) {
O
oceanbase-admin 已提交
1368
    ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1369 1370 1371
    LOG_WARN("table should not be null", KR(ret), K(tenant_id),
             "table_id", hard_code_table.get_table_id(),
             "table_name", hard_code_table.get_table_name());
O
oceanbase-admin 已提交
1372 1373
    can_retry_ = true;
  } else if (OB_FAIL(check_table_schema(hard_code_table, *table))) {
W
wangzelin.wzl 已提交
1374
    LOG_WARN("fail to check table schema", KR(ret), K(tenant_id), K(hard_code_table), KPC(table));
O
oceanbase-admin 已提交
1375 1376 1377 1378
  }
  return ret;
}

W
wangzelin.wzl 已提交
1379 1380
int ObRootInspection::check_table_schema(const ObTableSchema &hard_code_table,
                                         const ObTableSchema &inner_table)
O
oceanbase-admin 已提交
1381 1382
{
  int ret = OB_SUCCESS;
W
wangzelin.wzl 已提交
1383 1384
  if (!hard_code_table.is_valid()
      || !inner_table.is_valid()) {
O
oceanbase-admin 已提交
1385 1386
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid table_schema", K(hard_code_table), K(inner_table), K(ret));
1387
  } else if (OB_FAIL(check_table_options_(inner_table, hard_code_table))) {
O
oceanbase-admin 已提交
1388
    LOG_WARN("check_table_options failed", "table_id", hard_code_table.get_table_id(), K(ret));
O
obdev 已提交
1389
  } else if (!inner_table.is_view_table()) { //view table do not check column info
O
oceanbase-admin 已提交
1390 1391
    if (hard_code_table.get_column_count() != inner_table.get_column_count()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1392 1393 1394
      LOG_WARN("column count mismatch", "table_id", inner_table.get_table_id(),
          "table_name",inner_table.get_table_name(), "table_column_cnt",inner_table.get_column_count(),
          "hard_code_table_column_cnt", hard_code_table.get_column_count(), K(ret));
O
oceanbase-admin 已提交
1395 1396 1397
    } else {
      int back_ret = OB_SUCCESS;
      for (int64_t i = 0; OB_SUCC(ret) && i < hard_code_table.get_column_count(); ++i) {
W
wangzelin.wzl 已提交
1398 1399
        const ObColumnSchemaV2 *hard_code_column = hard_code_table.get_column_schema_by_idx(i);
        const ObColumnSchemaV2 *column = NULL;
O
oceanbase-admin 已提交
1400 1401 1402
        if (NULL == hard_code_column) {
          ret = OB_SCHEMA_ERROR;
          LOG_WARN("hard_code_column is null", "hard_code_column", OB_P(hard_code_column), K(ret));
W
wangzelin.wzl 已提交
1403 1404
        } else if (NULL == (column = inner_table.get_column_schema(
            hard_code_column->get_column_name()))) {
O
oceanbase-admin 已提交
1405
          ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1406 1407 1408
          LOG_WARN("hard code column not found", "table_id", hard_code_table.get_table_id(),
              "table_name", hard_code_table.get_table_name(), "column",
              hard_code_column->get_column_name(), K(ret));
O
oceanbase-admin 已提交
1409 1410
        } else {
          const bool ignore_column_id = is_virtual_table(hard_code_table.get_table_id());
1411
          if (OB_FAIL(check_column_schema_(hard_code_table.get_table_name(),
W
wangzelin.wzl 已提交
1412
              *column, *hard_code_column, ignore_column_id))) {
O
oceanbase-admin 已提交
1413
            LOG_WARN("column schema mismatch with hard code column schema",
W
wangzelin.wzl 已提交
1414 1415
                "table_name",inner_table.get_table_name(), "column", *column,
                "hard_code_column", *hard_code_column, K(ret));
O
oceanbase-admin 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
          }
        }
        back_ret = OB_SUCCESS == back_ret ? ret : back_ret;
        ret = OB_SUCCESS;
      }
      ret = back_ret;
    }
  }
  return ret;
}

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
int ObRootInspection::check_and_get_system_table_column_diff(
    const share::schema::ObTableSchema &table_schema,
    const share::schema::ObTableSchema &hard_code_schema,
    common::ObIArray<uint64_t> &add_column_ids,
    common::ObIArray<uint64_t> &alter_column_ids)
{
  int ret = OB_SUCCESS;
  add_column_ids.reset();
  alter_column_ids.reset();
  if (!table_schema.is_valid() || !hard_code_schema.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid table_schema", KR(ret), K(table_schema), K(hard_code_schema));
  } else if (table_schema.get_tenant_id() != hard_code_schema.get_tenant_id()
             || table_schema.get_table_id() != hard_code_schema.get_table_id()
             || 0 != table_schema.get_table_name_str().compare(hard_code_schema.get_table_name_str())
             || !is_system_table(table_schema.get_table_id())) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid table_schema", KR(ret),
             "tenant_id", table_schema.get_tenant_id(),
             "table_id", table_schema.get_table_id(),
             "table_name", table_schema.get_table_name(),
             "hard_code_tenant_id", hard_code_schema.get_tenant_id(),
             "hard_code_table_id", hard_code_schema.get_table_id(),
             "hard_code_table_name", hard_code_schema.get_table_name());
  } else {
    const uint64_t tenant_id = table_schema.get_tenant_id();
    const uint64_t table_id = table_schema.get_table_id();
    const ObColumnSchemaV2 *column = NULL;
    const ObColumnSchemaV2 *hard_code_column = NULL;
    ObColumnSchemaV2 tmp_column; // check_column_can_be_altered_online() may change dst_column, is ugly.
    bool ignore_column_id = false;

    // case 1. check if columns should be dropped.
    // case 2. check if column can be altered online.
    for (int64_t i = 0; OB_SUCC(ret) && i < table_schema.get_column_count(); i++) {
      column = table_schema.get_column_schema_by_idx(i);
      if (OB_ISNULL(column)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("column schema is null", KR(ret), K(tenant_id), K(table_id), K(i));
      } else if (OB_ISNULL(hard_code_column = hard_code_schema.get_column_schema(column->get_column_id()))) {
        ret = OB_NOT_SUPPORTED; // case 1
        LOG_WARN("can't drop system table's column", KR(ret),
                 K(tenant_id), K(table_id),
                 "table_name", table_schema.get_table_name(),
                 "column_id", column->get_column_id(),
                 "column_name", column->get_column_name());
      } else {
        // case 2
        int tmp_ret = check_column_schema_(table_schema.get_table_name_str(),
                                           *column,
                                           *hard_code_column,
                                           ignore_column_id);
        if (OB_SUCCESS == tmp_ret) {
          // not changed
        } else if (OB_SCHEMA_ERROR != tmp_ret) {
          ret = tmp_ret;
          LOG_WARN("fail to check column schema", KR(ret),
                   K(tenant_id), K(table_id), KPC(column), KPC(hard_code_column));
        } else if (OB_FAIL(tmp_column.assign(*hard_code_column))) {
          LOG_WARN("fail to assign hard code column schema", KR(ret),
                   K(tenant_id), K(table_id),  "column_id", hard_code_column->get_column_id());
        } else if (OB_FAIL(table_schema.check_column_can_be_altered_online(column, &tmp_column))) {
          LOG_WARN("fail to check alter column online", KR(ret),
                   K(tenant_id), K(table_id),
                   "table_name", table_schema.get_table_name(),
                   "column_id", column->get_column_id(),
                   "column_name", column->get_column_name());
        } else if (OB_FAIL(alter_column_ids.push_back(column->get_column_id()))) {
          LOG_WARN("fail to push back column_id", KR(ret), K(tenant_id), K(table_id),
                   "column_id", column->get_column_id());
        }
      }
    } // end for

    // case 3: check if columns should be added.
    for (int64_t i = 0; OB_SUCC(ret) && i < hard_code_schema.get_column_count(); i++) {
      hard_code_column = hard_code_schema.get_column_schema_by_idx(i);
      if (OB_ISNULL(hard_code_column)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("column schema is null", KR(ret), K(tenant_id), K(table_id), K(i));
      } else if (OB_NOT_NULL(column = table_schema.get_column_schema(hard_code_column->get_column_id()))) {
        // column exist, just skip
      } else {
        const uint64_t hard_code_column_id = hard_code_column->get_column_id();
        const ObColumnSchemaV2 *last_column = NULL;
        if (table_schema.get_column_count() <= 0
            || OB_ISNULL(last_column = table_schema.get_column_schema_by_idx(
                         table_schema.get_column_count() - 1))) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("invalid column count or column", KR(ret), K(table_schema));
        } else if (table_schema.get_max_used_column_id() >= hard_code_column_id
                  || last_column->get_column_id() >= hard_code_column_id) {
          ret = OB_NOT_SUPPORTED;
          LOG_WARN("column should be added at last", KR(ret), KPC(hard_code_column), K(table_schema));
        } else if (OB_FAIL(add_column_ids.push_back(hard_code_column_id))) {
          LOG_WARN("fail to push back column_id", KR(ret), K(tenant_id), K(table_id),
                   "column_id", hard_code_column_id);
        }
      }
    } // end for
  }
  return ret;
}

W
wangzelin.wzl 已提交
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
bool ObRootInspection::check_str_with_lower_case_(const ObString &str)
{
  bool bret = false;
  if (str.length() > 0) {
    for (int64_t i = 0; !bret && i < str.length(); i++) {
      if (str.ptr()[i] >= 'a' && str.ptr()[i] <= 'z') {
        bret = true;
      }
    }
  }
  return bret;
}

int ObRootInspection::check_sys_view_(
    const uint64_t tenant_id,
    const ObTableSchema &hard_code_table)
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(GCTX.root_service_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", KR(ret));
  } else if (hard_code_table.is_view_table()) {
    // check view definition
    const ObString &table_name = hard_code_table.get_table_name();
    const uint64_t table_id = hard_code_table.get_table_id();
    const uint64_t database_id = hard_code_table.get_database_id();
    bool is_oracle = is_oracle_sys_database_id(database_id);
    bool check_lower_case = !is_mysql_database_id(database_id);
    SMART_VAR(ObMySQLProxy::MySQLResult, res) {
      common::sqlclient::ObMySQLResult *result = NULL;
      ObSqlString sql;
      // case 0: check expansion of sys view definition
      if (is_oracle) {
        if (OB_FAIL(sql.assign_fmt("SELECT FIELD FROM \"%s\".\"%s\" WHERE TABLE_ID = %lu",
                                   OB_ORA_SYS_SCHEMA_NAME,
                                   OB_TENANT_VIRTUAL_TABLE_COLUMN_ORA_TNAME,
                                   table_id))) {
          LOG_WARN("failed to assign sql", KR(ret), K(sql));
        } else if (OB_FAIL(GCTX.root_service_->get_oracle_sql_proxy().read(res, tenant_id, sql.ptr()))) {
          LOG_WARN("execute sql failed", KR(ret), K(tenant_id), K(table_name), K(sql));
        }
      } else {
        if (OB_FAIL(sql.assign_fmt("SELECT FIELD FROM `%s`.`%s` WHERE TABLE_ID = %lu",
                                   OB_SYS_DATABASE_NAME,
                                   OB_TENANT_VIRTUAL_TABLE_COLUMN_TNAME,
                                   table_id))) {
          LOG_WARN("failed to assign sql", KR(ret), K(sql));
        } else if (!is_oracle && OB_FAIL(GCTX.root_service_->get_sql_proxy().read(res, tenant_id, sql.ptr()))) {
          LOG_WARN("execute sql failed", KR(ret), K(tenant_id), K(table_name), K(sql));
        }
      }
      if (OB_FAIL(ret)) {
T
tino247 已提交
1583 1584 1585 1586 1587 1588
        if (OB_ERR_VIEW_INVALID == ret) {
          ret = OB_SCHEMA_ERROR;
          LOG_ERROR("check sys view: expand failed", KR(ret), K(tenant_id), K(table_name));
        } else {
          LOG_WARN("check sys view: expand failed", KR(ret), K(tenant_id), K(table_name));
        }
W
wangzelin.wzl 已提交
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
      } else if (OB_ISNULL(result = res.get_result())) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("failed to get sql result", KR(ret), K(tenant_id));
      } else if (check_lower_case) {
        // case 1: check column name with lower case
        ObString col_name;
        while (OB_SUCC(ret) && OB_SUCC(result->next())) {
          if (OB_FAIL(result->get_varchar(0L, col_name))) {
            LOG_WARN("fail to get filed", KR(ret), K(tenant_id), K(table_name));
          } else if (check_str_with_lower_case_(col_name)) {
            ret = OB_SCHEMA_ERROR;
            LOG_ERROR("check sys view: column name should be uppercase",
                      KR(ret), K(tenant_id), K(table_name), K(col_name));
          }
        } // end while
        if (OB_ITER_END == ret) {
          ret = OB_SUCCESS;
        } else {
          ret = OB_SUCC(ret) ? OB_ERR_UNEXPECTED : ret;
          LOG_WARN("iterate failed", KR(ret));
        }
        // case 2: check view name with lower case
        if (OB_SUCC(ret) && check_str_with_lower_case_(hard_code_table.get_table_name())) {
          ret = OB_SCHEMA_ERROR;
          LOG_ERROR("check sys view: table name should be uppercase", KR(ret), K(tenant_id), K(table_name));
        }
      }
    }
  }
  return ret;
}

1621 1622
int ObRootInspection::check_table_options_(const ObTableSchema &table,
                                           const ObTableSchema &hard_code_table)
O
oceanbase-admin 已提交
1623 1624 1625 1626 1627 1628 1629
{
  int ret = OB_SUCCESS;
  if (!table.is_valid() || !hard_code_table.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid table or invalid hard_code_table", K(table), K(hard_code_table), K(ret));
  } else if (table.get_table_id() != hard_code_table.get_table_id()) {
    ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1630 1631
    LOG_WARN("table id not match", "table_id", table.get_table_id(),
        "hard_code table_id", hard_code_table.get_table_id(), K(ret));
O
oceanbase-admin 已提交
1632 1633 1634
  } else if (table.get_table_name_str() != hard_code_table.get_table_name_str()) {
    ret = OB_SCHEMA_ERROR;
    LOG_WARN("table name mismatch with hard code table",
W
wangzelin.wzl 已提交
1635 1636
        "table_id", table.get_table_id(), "table_name", table.get_table_name(),
        "hard_code_table name", hard_code_table.get_table_name(), K(ret));
O
oceanbase-admin 已提交
1637
  } else {
W
wangzelin.wzl 已提交
1638
    const ObString &table_name = table.get_table_name_str();
O
oceanbase-admin 已提交
1639 1640 1641

    if (table.get_tenant_id() != hard_code_table.get_tenant_id()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1642 1643
      LOG_WARN("tenant_id mismatch", K(table_name), "in_memory", table.get_tenant_id(),
          "hard_code", hard_code_table.get_tenant_id(), K(ret));
O
oceanbase-admin 已提交
1644 1645
    } else if (table.get_database_id() != hard_code_table.get_database_id()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1646 1647
      LOG_WARN("database_id mismatch", K(table_name), "in_memory", table.get_database_id(),
          "hard_code", hard_code_table.get_database_id(), K(ret));
O
oceanbase-admin 已提交
1648 1649
    } else if (table.get_tablegroup_id() != hard_code_table.get_tablegroup_id()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1650 1651
      LOG_WARN("tablegroup_id mismatch", K(table_name), "in_memory", table.get_tablegroup_id(),
          "hard_code", hard_code_table.get_tablegroup_id(), K(ret));
O
oceanbase-admin 已提交
1652 1653
    } else if (table.get_auto_increment() != hard_code_table.get_auto_increment()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1654 1655
      LOG_WARN("auto_increment mismatch", K(table_name), "in_memory", table.get_auto_increment(),
          "hard_code", hard_code_table.get_auto_increment(), K(ret));
O
oceanbase-admin 已提交
1656 1657
    } else if (table.is_read_only() != hard_code_table.is_read_only()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1658 1659
      LOG_WARN("read_only mismatch", K(table_name), "in_memory", table.is_read_only(),
          "hard code", hard_code_table.is_read_only(), K(ret));
O
oceanbase-admin 已提交
1660 1661
    } else if (table.get_load_type() != hard_code_table.get_load_type()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1662 1663
      LOG_WARN("load_type mismatch", K(table_name), "in_memory", table.get_load_type(),
          "hard_code", hard_code_table.get_load_type(), K(ret));
O
oceanbase-admin 已提交
1664 1665
    } else if (table.get_table_type() != hard_code_table.get_table_type()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1666 1667
      LOG_WARN("table_type mismatch", K(table_name), "in_memory", table.get_table_type(),
          "hard_code", hard_code_table.get_table_type(), K(ret));
O
oceanbase-admin 已提交
1668 1669
    } else if (table.get_index_type() != hard_code_table.get_index_type()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1670 1671
      LOG_WARN("index_type mismatch", K(table_name), "in_memory", table.get_index_type(),
          "hard_code", hard_code_table.get_index_type(), K(ret));
O
oceanbase-admin 已提交
1672 1673
    } else if (table.get_index_using_type() != hard_code_table.get_index_using_type()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1674 1675
      LOG_WARN("index_using_type mismatch", K(table_name), "in_memory", table.get_index_using_type(),
          "hard_code", hard_code_table.get_index_using_type(), K(ret));
O
oceanbase-admin 已提交
1676 1677
    } else if (table.get_def_type() != hard_code_table.get_def_type()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1678 1679
      LOG_WARN("def_type mismatch", K(table_name), "in_memory", table.get_def_type(),
          "hard_code", hard_code_table.get_def_type(), K(ret));
O
oceanbase-admin 已提交
1680 1681
    } else if (table.get_data_table_id() != hard_code_table.get_data_table_id()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1682 1683
      LOG_WARN("data_table_id mismatch", K(table_name), "in_memory", table.get_data_table_id(),
          "hard_code", hard_code_table.get_data_table_id(), K(ret));
O
oceanbase-admin 已提交
1684 1685
    } else if (table.get_tablegroup_name() != hard_code_table.get_tablegroup_name()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1686 1687
      LOG_WARN("tablegroup_name mismatch", K(table_name), "in_memory", table.get_tablegroup_name(),
          "hard_code", hard_code_table.get_tablegroup_name(), K(ret));
O
oceanbase-admin 已提交
1688 1689
    } else if (table.get_view_schema() != hard_code_table.get_view_schema()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1690 1691
      LOG_WARN("view_schema mismatch", K(table_name), "in_memory", table.get_view_schema(),
          "hard_code", hard_code_table.get_view_schema(), K(ret));
O
oceanbase-admin 已提交
1692 1693
    } else if (table.get_part_level() != hard_code_table.get_part_level()) {
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1694 1695 1696 1697 1698 1699
      LOG_WARN("part_level mismatch", K(table_name), "in_memory", table.get_part_level(),
          "hard_code", hard_code_table.get_part_level(), K(ret));
    } else if ((table.get_part_option().get_part_func_expr_str()
        != hard_code_table.get_part_option().get_part_func_expr_str())
        || (table.get_part_option().get_part_func_type()
        != hard_code_table.get_part_option().get_part_func_type())) {
O
oceanbase-admin 已提交
1700
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1701 1702 1703 1704 1705 1706
      LOG_WARN("part_expr mismatch", K(table_name), "in_memory",
          table.get_part_option(), "hard_code", hard_code_table.get_part_option(), K(ret));
    } else if ((table.get_sub_part_option().get_part_func_expr_str()
        != hard_code_table.get_sub_part_option().get_part_func_expr_str())
        || (table.get_sub_part_option().get_part_func_type()
        != hard_code_table.get_sub_part_option().get_part_func_type())) {
O
oceanbase-admin 已提交
1707
      ret = OB_SCHEMA_ERROR;
W
wangzelin.wzl 已提交
1708 1709
      LOG_WARN("sub_part_expr mismatch", K(table_name), "in_memory",
          table.get_sub_part_option(), "hard_code", hard_code_table.get_sub_part_option(), K(ret));
O
obdev 已提交
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
    } else if (table.is_view_table()) {
      // view table do not check column info
    } else if (table.get_max_used_column_id() < hard_code_table.get_max_used_column_id()) {
      ret = OB_SCHEMA_ERROR;
      LOG_WARN("max_used_column_id mismatch", K(table_name), "in_memory",
          table.get_max_used_column_id(), "hard_code",
          hard_code_table.get_max_used_column_id(), K(ret));
    } else if (table.get_rowkey_column_num() != hard_code_table.get_rowkey_column_num()) {
      ret = OB_SCHEMA_ERROR;
      LOG_WARN("rowkey_column_num mismatch", K(table_name), "in_memory",
          table.get_rowkey_column_num(), "hard_code",
          hard_code_table.get_rowkey_column_num(), K(ret));
    } else if (table.get_index_column_num() != hard_code_table.get_index_column_num()) {
      ret = OB_SCHEMA_ERROR;
      LOG_WARN("index_column_num mismatch", K(table_name), "in_memory",
          table.get_index_column_num(), "hard_code",
          hard_code_table.get_index_column_num(), K(ret));
    } else if (table.get_rowkey_split_pos() != hard_code_table.get_rowkey_split_pos()) {
      ret = OB_SCHEMA_ERROR;
      LOG_WARN("rowkey_split_pos mismatch", K(table_name), "in_memory",
          table.get_rowkey_split_pos(), "hard_code",
          hard_code_table.get_rowkey_split_pos(), K(ret));
    } else if (table.get_partition_key_column_num()
        != hard_code_table.get_partition_key_column_num()) {
      ret = OB_SCHEMA_ERROR;
      LOG_WARN("partition_key_column_num mismatch", K(table_name), "in_memory",
          table.get_partition_key_column_num(), "hard_code",
          hard_code_table.get_partition_key_column_num(), K(ret));
    } else if (table.get_subpartition_key_column_num()
        != hard_code_table.get_subpartition_key_column_num()) {
      ret = OB_SCHEMA_ERROR;
      LOG_WARN("partition_key_column_num mismatch", K(table_name), "in_memory",
          table.get_subpartition_key_column_num(), "hard_code",
          hard_code_table.get_subpartition_key_column_num(), K(ret));
    } else if (table.get_autoinc_column_id() != hard_code_table.get_autoinc_column_id()) {
      ret = OB_SCHEMA_ERROR;
      LOG_WARN("autoinc_column_id mismatch", K(table_name), "in_memory",
          table.get_autoinc_column_id(), "hard_code",
          hard_code_table.get_autoinc_column_id(), K(ret));
O
oceanbase-admin 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
    }

    // options may be different between different ob instance, don't check
    // block_size
    // is_user_bloomfilter
    // progressive_merge_num
    // replica_num
    // index_status
    // name_case_mode
    // charset_type
    // collation_type
    // schema_version
    // comment
    // compress_func_name
    // expire_info
    // zone_list
    // primary_zone
    // part_expr.part_num_
    // sub_part_expr.part_num_
    // store_format
    // row_store_type
    // progressive_merge_round
    // storage_format_version
  }
  return ret;
}

1776 1777 1778 1779
int ObRootInspection::check_column_schema_(const ObString &table_name,
                                           const ObColumnSchemaV2 &column,
                                           const ObColumnSchemaV2 &hard_code_column,
                                           const bool ignore_column_id)
O
oceanbase-admin 已提交
1780 1781 1782 1783 1784
{
  int ret = OB_SUCCESS;
  if (table_name.empty() || !column.is_valid() || !hard_code_column.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("table_name is empty or invalid column or invalid hard_code_column",
1785
             KR(ret), K(table_name), K(column), K(hard_code_column));
O
oceanbase-admin 已提交
1786
  } else {
W
wangzelin.wzl 已提交
1787 1788
#define CMP_COLUMN_ATTR(attr) \
  if (OB_SUCC(ret)) { \
O
oceanbase-admin 已提交
1789
    if (column.get_##attr() != hard_code_column.get_##attr()) { \
W
wangzelin.wzl 已提交
1790
      ret = OB_SCHEMA_ERROR; \
1791 1792
      LOG_WARN(#attr " mismatch", KR(ret), K(table_name), "column_name", column.get_column_name(), \
               "in_memory", column.get_##attr(), "hard_code", hard_code_column.get_##attr()); \
W
wangzelin.wzl 已提交
1793
    } \
O
oceanbase-admin 已提交
1794 1795
  }

W
wangzelin.wzl 已提交
1796 1797
#define CMP_COLUMN_IS_ATTR(attr) \
  if (OB_SUCC(ret)) { \
O
oceanbase-admin 已提交
1798
    if (column.is_##attr() != hard_code_column.is_##attr()) { \
W
wangzelin.wzl 已提交
1799
      ret = OB_SCHEMA_ERROR; \
1800 1801
      LOG_WARN(#attr " mismatch", KR(ret), K(table_name), "column_name", column.get_column_name(), \
               "in_memory", column.is_##attr(), "hard_code", hard_code_column.is_##attr()); \
W
wangzelin.wzl 已提交
1802
    } \
O
oceanbase-admin 已提交
1803
  }
1804 1805 1806 1807 1808 1809 1810 1811
    if (OB_SUCC(ret)) {
      if (column.get_column_name_str() != hard_code_column.get_column_name_str()) {
        ret = OB_SCHEMA_ERROR;
        LOG_WARN("column_name mismatch", KR(ret), K(table_name),
                 "in_memory", column.get_column_name(),
                 "hard_code", hard_code_column.get_column_name());
      }
    }
O
oceanbase-admin 已提交
1812 1813

    if (!ignore_column_id) {
O
obdev 已提交
1814
      CMP_COLUMN_ATTR(column_id);
O
oceanbase-admin 已提交
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
    }
    CMP_COLUMN_ATTR(tenant_id);
    CMP_COLUMN_ATTR(table_id);
    // don't need to check schema version
    CMP_COLUMN_ATTR(rowkey_position);
    CMP_COLUMN_ATTR(index_position);
    CMP_COLUMN_ATTR(order_in_rowkey);
    CMP_COLUMN_ATTR(tbl_part_key_pos);
    CMP_COLUMN_ATTR(meta_type);
    CMP_COLUMN_ATTR(accuracy);
    CMP_COLUMN_ATTR(data_length);
    CMP_COLUMN_IS_ATTR(nullable);
    CMP_COLUMN_IS_ATTR(zero_fill);
    CMP_COLUMN_IS_ATTR(autoincrement);
    CMP_COLUMN_IS_ATTR(hidden);
    CMP_COLUMN_IS_ATTR(on_update_current_timestamp);
    CMP_COLUMN_ATTR(charset_type);
    // don't need to check orig default value
W
wangzelin.wzl 已提交
1833 1834
    if (ObString("row_store_type") == column.get_column_name()
        && (ObString("__all_table") == table_name || ObString("__all_table_history") == table_name)) {
O
oceanbase-admin 已提交
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
      // row_store_type may have two possible default values
    } else {
      CMP_COLUMN_ATTR(cur_default_value);
    }
    CMP_COLUMN_ATTR(comment);

  }

#undef CMP_COLUMN_IS_ATTR
#undef CMP_COLUMN_INT_ATTR
  return ret;
}

1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
int ObRootInspection::check_data_version_()
{
  int ret = OB_SUCCESS;
  ObArray<uint64_t> tenant_ids;
  if (OB_UNLIKELY(!inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", KR(ret));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", KR(ret));
  } else if (OB_ISNULL(schema_service_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("schema_service is null", KR(ret));
1860
  } else if (OB_FAIL(construct_tenant_ids_(tenant_ids))) {
1861 1862 1863 1864 1865 1866 1867
    LOG_WARN("get_tenant_ids failed", KR(ret));
  } else {
    int backup_ret = OB_SUCCESS;
    int tmp_ret = OB_SUCCESS;
    FOREACH_X(tenant_id, tenant_ids, OB_SUCC(ret)) {
      if (OB_FAIL(check_cancel())) {
        LOG_WARN("check_cancel failed", KR(ret));
T
tino247 已提交
1868 1869
      } else if (OB_FAIL(check_tenant_status_(*tenant_id))) {
        LOG_WARN("fail to check tenant status", KR(ret), K(*tenant_id));
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
      } else if (OB_TMP_FAIL(check_data_version_(*tenant_id))) {
        LOG_WARN("fail to check data version by tenant", KR(tmp_ret), K(*tenant_id));
        backup_ret = OB_SUCCESS == backup_ret ? tmp_ret : backup_ret;
      }
    } // end foreach
    ret = OB_SUCC(ret) ? backup_ret : ret;
  }
  return ret;
}

int ObRootInspection::check_data_version_(
    const uint64_t tenant_id)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", KR(ret));
  } else if (OB_FAIL(check_cancel())) {
    LOG_WARN("check_cancel failed", KR(ret));
  } else {
    share::ObGlobalStatProxy proxy(*sql_proxy_, tenant_id);
    uint64_t target_data_version = 0;
    uint64_t current_data_version = 0;
    uint64_t compatible_version = 0;
    bool for_update = false;
    if (OB_FAIL(proxy.get_target_data_version(for_update, target_data_version))) {
      LOG_WARN("fail to get target data version", KR(ret), K(tenant_id));
    } else if (OB_FAIL(proxy.get_current_data_version(current_data_version))) {
      LOG_WARN("fail to get current data version", KR(ret), K(tenant_id));
    } else if (OB_FAIL(GET_MIN_DATA_VERSION(tenant_id, compatible_version))) {
      LOG_WARN("fail to get min data version", KR(ret), K(tenant_id));
    } else if (target_data_version != current_data_version
               || target_data_version != compatible_version
               || target_data_version != DATA_CURRENT_VERSION) {
      ret = OB_STATE_NOT_MATCH;
      LOG_WARN("data_version not match, upgrade process should be run",
               KR(ret), K(tenant_id), K(target_data_version),
               K(current_data_version), K(compatible_version));
    }
  }
  return ret;
}

O
oceanbase-admin 已提交
1913 1914 1915 1916 1917 1918 1919 1920 1921
int ObRootInspection::check_cancel()
{
  int ret = OB_SUCCESS;
  if (stopped_) {
    ret = OB_CANCELED;
  }
  return ret;
}

T
tino247 已提交
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
int ObRootInspection::check_tenant_status_(const uint64_t tenant_id)
{
  int ret = OB_SUCCESS;
  ObSchemaGetterGuard guard;
  const ObSimpleTenantSchema *tenant = NULL;
  int64_t schema_version = OB_INVALID_VERSION;
  if (OB_ISNULL(schema_service_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("schema service is null", KR(ret));
  } else if (OB_FAIL(schema_service_->get_tenant_schema_guard(OB_SYS_TENANT_ID, guard))) {
    LOG_WARN("fail to get schema guard", KR(ret));
  } else if (OB_FAIL(guard.get_tenant_info(tenant_id, tenant))) {
    LOG_WARN("fail to get tenant schema", KR(ret), K(tenant_id));
  } else if (OB_ISNULL(tenant)) {
    // tenant may has been dropped;
    ret = OB_EAGAIN;
    LOG_WARN("tenant may be dropped, don't continue", KR(ret), K(tenant_id));
  } else if (!tenant->is_normal()) {
    ret = OB_EAGAIN;
    LOG_WARN("tenant status is not noraml, should check next round", KR(ret), K(tenant_id));
  } else if (OB_FAIL(schema_service_->get_tenant_refreshed_schema_version(tenant_id, schema_version))) {
    LOG_WARN("fail to get tenant schema version", KR(ret), K(tenant_id));
  } else if (!ObSchemaService::is_formal_version(schema_version)) {
    ret = OB_EAGAIN;
    LOG_WARN("schema version is not formal, observer may be restarting or inner table schema changed, "
             "should check next round", KR(ret), K(tenant_id), K(schema_version));
  }
  return ret;
}

W
wangzelin.wzl 已提交
1952 1953 1954 1955
ObUpgradeInspection::ObUpgradeInspection()
  : inited_(false), schema_service_(NULL), root_inspection_(NULL)
{
}
O
oceanbase-admin 已提交
1956 1957

ObUpgradeInspection::~ObUpgradeInspection()
W
wangzelin.wzl 已提交
1958 1959
{
}
O
oceanbase-admin 已提交
1960

W
wangzelin.wzl 已提交
1961 1962
int ObUpgradeInspection::init(ObMultiVersionSchemaService &schema_service,
                              ObRootInspection &root_inspection)
O
oceanbase-admin 已提交
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
{
  int ret = OB_SUCCESS;
  if (inited_) {
    ret = OB_INIT_TWICE;
    LOG_WARN("init twice", K(ret));
  } else {
    schema_service_ = &schema_service;
    root_inspection_ = &root_inspection;
    inited_ = true;
  }
  return ret;
}

W
wangzelin.wzl 已提交
1976
int ObUpgradeInspection::inner_get_next_row(common::ObNewRow *&row)
O
oceanbase-admin 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
{
  int ret = OB_SUCCESS;
  ObSchemaGetterGuard schema_guard;
  if (NULL == allocator_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init, allocator is null", K(ret));
  } else if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_FAIL(schema_service_->get_tenant_schema_guard(OB_SYS_TENANT_ID, schema_guard))) {
    LOG_WARN("get schema guard error", K(ret));
  } else if (!start_to_read_) {
W
wangzelin.wzl 已提交
1989 1990 1991
    const ObTableSchema *table_schema = NULL;
    const uint64_t table_id = OB_ALL_VIRTUAL_UPGRADE_INSPECTION_TID;
    if (OB_FAIL(schema_guard.get_table_schema(OB_SYS_TENANT_ID, table_id, table_schema))) {
O
oceanbase-admin 已提交
1992 1993 1994 1995 1996 1997 1998
      LOG_WARN("get_table_schema failed", K(table_id), K(ret));
    } else if (NULL == table_schema) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("table_schema is null", KP(table_schema), K(ret));
    } else {
      ObArray<Column> columns;

W
wangzelin.wzl 已提交
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
#define ADD_ROW(name, info) \
  do { \
    columns.reuse(); \
    if (OB_FAIL(ret)) { \
    } else if (OB_FAIL(get_full_row(table_schema, name, info, columns))) { \
      LOG_WARN("get_full_row failed", "table_schema", *table_schema, \
          K(name), K(info), K(ret)); \
    } else if (OB_FAIL(project_row(columns, cur_row_))) { \
      LOG_WARN("project_row failed", K(columns), K(ret)); \
    } else if (OB_FAIL(scanner_.add_row(cur_row_))) { \
      LOG_WARN("add_row failed", K(cur_row_), K(ret)); \
    } \
O
oceanbase-admin 已提交
2011 2012 2013 2014
  } while (false)

#define CHECK_RESULT(checked, value) (checked ? (value ? "succeed" : "failed") : "checking")

W
wangzelin.wzl 已提交
2015 2016 2017 2018 2019 2020 2021 2022
      ADD_ROW("zone_check", CHECK_RESULT(root_inspection_->is_all_checked(),
          root_inspection_->is_zone_passed()));
      ADD_ROW("sys_stat_check", CHECK_RESULT(root_inspection_->is_all_checked(),
          root_inspection_->is_sys_stat_passed()));
      ADD_ROW("sys_param_check", CHECK_RESULT(root_inspection_->is_all_checked(),
          root_inspection_->is_sys_param_passed()));
      ADD_ROW("sys_table_schema_check", CHECK_RESULT(root_inspection_->is_all_checked(),
          root_inspection_->is_sys_table_schema_passed()));
2023 2024
      ADD_ROW("data_version_check", CHECK_RESULT(root_inspection_->is_all_checked(),
          root_inspection_->is_data_version_passed()));
O
oceanbase-admin 已提交
2025 2026

      bool upgrade_job_passed = true;
2027
      for (int64_t i = 0; OB_SUCC(ret) && i < UPGRADE_JOB_TYPE_COUNT; i++) {
O
oceanbase-admin 已提交
2028 2029 2030 2031 2032 2033 2034 2035
        int tmp = OB_SUCCESS;
        ObRsJobType job_type = upgrade_job_type_array[i];
        if (job_type > JOB_TYPE_INVALID && job_type < JOB_TYPE_MAX) {
          if (OB_SUCCESS != (tmp = ObUpgradeUtils::check_upgrade_job_passed(job_type))) {
            LOG_WARN("fail to check upgrade job passed", K(tmp), K(job_type));
            upgrade_job_passed = false;
          }
          ADD_ROW(ObRsJobTableOperator::get_job_type_str(job_type),
W
wangzelin.wzl 已提交
2036
                  CHECK_RESULT(root_inspection_->is_all_checked(), (OB_SUCCESS == tmp)));
O
oceanbase-admin 已提交
2037 2038 2039
        }
      }

W
wangzelin.wzl 已提交
2040 2041
      ADD_ROW("all_check", CHECK_RESULT(root_inspection_->is_all_checked(),
              (root_inspection_->is_all_passed() && upgrade_job_passed)));
O
oceanbase-admin 已提交
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060

#undef CHECK_RESULT
#undef ADD_ROW
    }
    if (OB_SUCC(ret)) {
      scanner_it_ = scanner_.begin();
      start_to_read_ = true;
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(scanner_it_.get_next_row(cur_row_))) {
      if (OB_ITER_END != ret) {
        LOG_WARN("get_next_row failed", K(ret));
      }
    } else {
      row = &cur_row_;
    }
  }
  return ret;
W
wangzelin.wzl 已提交
2061

O
oceanbase-admin 已提交
2062 2063
}

W
wangzelin.wzl 已提交
2064 2065 2066
int ObUpgradeInspection::get_full_row(const share::schema::ObTableSchema *table,
                                      const char *name, const char *info,
                                      ObIArray<Column> &columns)
O
oceanbase-admin 已提交
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
{
  int ret = OB_SUCCESS;
  if (!inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (NULL == table || NULL == name || NULL == info) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", KP(table), KP(name), KP(info), K(ret));
  } else {
    ADD_COLUMN(set_varchar, table, "name", name, columns);
    ADD_COLUMN(set_varchar, table, "info", info, columns);
  }

  return ret;
}

W
wangzelin.wzl 已提交
2083 2084
}//end namespace rootserver
}//end namespace oceanbase