ob_backup_data_clean.cpp 311.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 19 20 21 22
/**
 * 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_backup_data_clean.h"
#include "share/backup/ob_backup_operator.h"
#include "share/backup/ob_tenant_backup_task_updater.h"
#include "share/backup/ob_extern_backup_info_mgr.h"
#include "share/config/ob_server_config.h"
#include "ob_root_backup.h"
#include "share/backup/ob_tenant_name_mgr.h"
#include "backup/ob_tenant_backup_data_clean_mgr.h"
#include "share/backup/ob_log_archive_backup_info_mgr.h"
23 24
#include "share/backup/ob_backup_operator.h"
#include "share/backup/ob_backup_backupset_operator.h"
O
oceanbase-admin 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

namespace oceanbase {

using namespace common;
using namespace obrpc;
using namespace share;
using namespace share::schema;
using namespace storage;

namespace rootserver {

int64_t ObBackupDataCleanIdling::get_idle_interval_us()
{
  const int64_t backup_check_interval = 10 * 1000 * 1000;  // 10s
  return backup_check_interval;
}

ObBackupDataClean::ObBackupDataClean()
    : is_inited_(false),
      schema_service_(NULL),
      sql_proxy_(NULL),
      idling_(stop_),
      is_prepare_flag_(false),
      inner_error_(OB_SUCCESS),
      is_working_(false),
50 51 52 53 54 55 56 57
      backup_lease_service_(nullptr),
      backup_dest_(),
      backup_dest_option_(),
      backup_backup_dest_(),
      backup_backup_dest_option_(),
      is_update_reserved_backup_timestamp_(false),
      inner_table_version_(OB_BACKUP_INNER_TABLE_VMAX),
      sys_tenant_deleted_backup_set_(),
M
mw0 已提交
58 59 60
      retry_count_(0),
      sys_tenant_deleted_backup_round_(),
      sys_tenant_deleted_backup_piece_()
O
oceanbase-admin 已提交
61 62 63 64 65
{}

ObBackupDataClean::~ObBackupDataClean()
{}

M
mw0 已提交
66 67
int ObBackupDataClean::init(share::schema::ObMultiVersionSchemaService &schema_service, ObMySQLProxy &sql_proxy,
    share::ObIBackupLeaseService &backup_lease_service)
O
oceanbase-admin 已提交
68 69 70
{
  int ret = OB_SUCCESS;
  const int root_backup_thread_cnt = 1;
71 72
  const int64_t MAX_BUCKET_NUM = 128;

O
oceanbase-admin 已提交
73 74 75 76 77
  if (is_inited_) {
    ret = OB_INIT_TWICE;
    STORAGE_LOG(WARN, "root backup init twice", K(ret));
  } else if (OB_FAIL(create(root_backup_thread_cnt, "BackupDataClean"))) {
    LOG_WARN("create thread failed", K(ret), K(root_backup_thread_cnt));
78 79
  } else if (OB_FAIL(sys_tenant_deleted_backup_set_.create(MAX_BUCKET_NUM))) {
    LOG_WARN("failed to create backup set map", K(ret));
M
mw0 已提交
80 81 82 83
  } else if (OB_FAIL(sys_tenant_deleted_backup_round_.create(MAX_BUCKET_NUM))) {
    LOG_WARN("failed to create sys tenant deleted backup round", K(ret));
  } else if (OB_FAIL(sys_tenant_deleted_backup_piece_.create(MAX_BUCKET_NUM))) {
    LOG_WARN("failed to create sys tenant deleted backup piece", K(ret));
O
oceanbase-admin 已提交
84 85 86 87
  } else {
    schema_service_ = &schema_service;
    sql_proxy_ = &sql_proxy;
    backup_lease_service_ = &backup_lease_service;
88
    inner_table_version_ = OB_BACKUP_INNER_TABLE_VMAX;
O
oceanbase-admin 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    is_inited_ = true;
    LOG_INFO("backup data clean init success");
  }
  return ret;
}

int ObBackupDataClean::idle() const
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else if (OB_FAIL(idling_.idle())) {
    LOG_WARN("idle failed", K(ret));
  } else {
    LOG_INFO("backup data clean idle", "idle_time", idling_.get_idle_interval_us());
  }
  return ret;
}

void ObBackupDataClean::wakeup()
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    idling_.wakeup();
  }
}

void ObBackupDataClean::stop()
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("not init", K(ret));
  } else {
    ObReentrantThread::stop();
    idling_.wakeup();
  }
}

int ObBackupDataClean::start()
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(ObReentrantThread::start())) {
    LOG_WARN("failed to start", K(ret));
  } else {
    is_working_ = true;
  }
  return ret;
}

void ObBackupDataClean::run3()
{
  int ret = OB_SUCCESS;
  ObArray<ObBackupDataCleanTenant> data_clean_tenants;

  while (!stop_) {
    ret = OB_SUCCESS;
    data_clean_tenants.reset();
    inner_error_ = OB_SUCCESS;
M
mw0 已提交
152 153 154
    sys_tenant_deleted_backup_set_.reuse();
    sys_tenant_deleted_backup_round_.reuse();
    sys_tenant_deleted_backup_piece_.reuse();
155 156 157 158 159 160
    ObCurTraceId::init(GCONF.self_addr_);

    if (OB_FAIL(set_current_backup_dest())) {
      LOG_WARN("failed to set current backup dest", K(ret));
    } else if (OB_FAIL(check_inner_table_version_())) {
      LOG_WARN("failed to check check_inner_table_version_", K(ret));
O
oceanbase-admin 已提交
161 162 163 164 165 166 167 168
    } else if (OB_FAIL(prepare_tenant_backup_clean())) {
      LOG_WARN("failed to prepare tenant backup clean", K(ret));
    } else if (OB_FAIL(get_need_clean_tenants(data_clean_tenants))) {
      LOG_WARN("failed to get need clean tenants", K(ret));
    } else if (OB_FAIL(do_clean_scheduler(data_clean_tenants))) {
      LOG_WARN("failed to do root scheduler", K(ret), K(data_clean_tenants));
    }

169
    set_inner_error(ret);
O
oceanbase-admin 已提交
170 171
    cleanup_prepared_infos();

172 173 174 175
    if (OB_FAIL(idle())) {
      LOG_WARN("idle failed", K(ret));
    } else {
      continue;
O
oceanbase-admin 已提交
176 177 178 179 180 181
    }
  }
  FLOG_INFO("ObBackupDataClean stop");
  is_working_ = false;
}

M
mw0 已提交
182
int ObBackupDataClean::get_need_clean_tenants(ObIArray<ObBackupDataCleanTenant> &clean_tenants)
O
oceanbase-admin 已提交
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
{
  int ret = OB_SUCCESS;
  ObArray<ObBackupDataCleanTenant> server_clean_tenants;
  hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> extern_clean_tenants_map;
  ObArray<ObSimpleBackupDataCleanTenant> deleted_clean_tenants;
  ObArray<ObSimpleBackupDataCleanTenant> new_deleted_clean_tenants;
  ObBackupCleanInfo sys_clean_info;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, sys_clean_info))) {
    if (OB_BACKUP_CLEAN_INFO_NOT_EXIST == ret) {
      ret = OB_SUCCESS;
    } else {
      LOG_WARN("failed to get clean info", K(ret));
    }
  } else if (ObBackupCleanInfoStatus::DOING != sys_clean_info.status_ &&
             ObBackupCleanInfoStatus::CANCEL != sys_clean_info.status_) {
    // do nothing
  } else if (OB_FAIL(extern_clean_tenants_map.create(MAX_BUCKET_NUM, ObModIds::BACKUP))) {
    LOG_WARN("failed to create clean tenants set", K(ret));
  } else if (OB_FAIL(get_server_clean_tenants(server_clean_tenants))) {
    LOG_WARN("failed to get server clean tenants", K(ret));
  } else if (OB_FAIL(get_deleted_clean_tenants(deleted_clean_tenants))) {
    LOG_WARN("failed to get deleted clean tenants", K(ret));
209 210
  } else if (OB_FAIL(get_clean_tenants_from_history_table(sys_clean_info, extern_clean_tenants_map))) {
    LOG_WARN("failed to get clean tenants from history table", K(ret), K(sys_clean_info));
O
oceanbase-admin 已提交
211 212
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < server_clean_tenants.count(); ++i) {
M
mw0 已提交
213
      const ObBackupDataCleanTenant &server_clean_tenant = server_clean_tenants.at(i);
O
oceanbase-admin 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      ObSimpleBackupDataCleanTenant simple_clean_tenant;
      if (OB_FAIL(extern_clean_tenants_map.get_refactored(
              server_clean_tenant.simple_clean_tenant_.tenant_id_, simple_clean_tenant))) {
        if (OB_HASH_NOT_EXIST == ret) {
          ret = OB_SUCCESS;
        } else {
          LOG_WARN("failed to get refactored from map", K(ret), K(server_clean_tenant));
        }
      } else if (OB_FAIL(
                     extern_clean_tenants_map.erase_refactored(server_clean_tenant.simple_clean_tenant_.tenant_id_))) {
        LOG_WARN("failed to erase simple clean tenant", K(ret), K(server_clean_tenant));
      }
    }

    for (int64_t i = 0; OB_SUCC(ret) && i < deleted_clean_tenants.count(); ++i) {
M
mw0 已提交
229
      const ObSimpleBackupDataCleanTenant &deleted_clean_tenant = deleted_clean_tenants.at(i);
O
oceanbase-admin 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243
      ObSimpleBackupDataCleanTenant simple_clean_tenant;
      if (OB_FAIL(extern_clean_tenants_map.get_refactored(deleted_clean_tenant.tenant_id_, simple_clean_tenant))) {
        if (OB_HASH_NOT_EXIST == ret) {
          ret = OB_SUCCESS;
        } else {
          LOG_WARN("failed to get refactored from map", K(ret), K(deleted_clean_tenant));
        }
      } else if (OB_FAIL(extern_clean_tenants_map.erase_refactored(deleted_clean_tenant.tenant_id_))) {
        LOG_WARN("failed to erase simple clean tenant", K(ret), K(deleted_clean_tenant));
      }
    }

    // add from server clean tenants
    for (int64_t i = 0; OB_SUCC(ret) && i < server_clean_tenants.count(); ++i) {
M
mw0 已提交
244
      const ObBackupDataCleanTenant &server_clean_tenant = server_clean_tenants.at(i);
O
oceanbase-admin 已提交
245 246 247 248 249 250 251
      if (OB_FAIL(clean_tenants.push_back(server_clean_tenant))) {
        LOG_WARN("failed to push server clean tenant into clean tenants", K(ret), K(server_clean_tenant));
      }
    }

    // add deleted clean tenant already in inner table
    for (int64_t i = 0; OB_SUCC(ret) && i < deleted_clean_tenants.count(); ++i) {
M
mw0 已提交
252
      const ObSimpleBackupDataCleanTenant &simple_clean_tenant = deleted_clean_tenants.at(i);
O
oceanbase-admin 已提交
253 254 255 256 257 258 259 260 261
      ObBackupDataCleanTenant clean_tenant;
      clean_tenant.simple_clean_tenant_ = simple_clean_tenant;
      if (OB_FAIL(clean_tenants.push_back(clean_tenant))) {
        LOG_WARN("failed to push server clean tenant into clean tenants", K(ret), K(clean_tenant));
      }
    }

    hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant>::iterator iter;
    for (iter = extern_clean_tenants_map.begin(); OB_SUCC(ret) && iter != extern_clean_tenants_map.end(); ++iter) {
M
mw0 已提交
262
      const ObSimpleBackupDataCleanTenant &simple_clean_tenant = iter->second;
O
oceanbase-admin 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276
      if (OB_FAIL(new_deleted_clean_tenants.push_back(simple_clean_tenant))) {
        LOG_WARN("failed to push simple clean tenant info array", K(ret), K(simple_clean_tenant));
      }
    }

    // insert new deleted tenant into inner table
    if (OB_SUCC(ret)) {
      if (OB_FAIL(schedule_deleted_clean_tenants(new_deleted_clean_tenants))) {
        LOG_WARN("failed to schedule deleted clean tenants", K(ret));
      }
    }

    // add deleted tenants
    for (int64_t i = 0; OB_SUCC(ret) && i < new_deleted_clean_tenants.count(); ++i) {
M
mw0 已提交
277
      const ObSimpleBackupDataCleanTenant &simple_clean_tenant = new_deleted_clean_tenants.at(i);
O
oceanbase-admin 已提交
278 279 280 281 282 283 284 285 286 287
      ObBackupDataCleanTenant clean_tenant;
      clean_tenant.simple_clean_tenant_ = simple_clean_tenant;
      if (OB_FAIL(clean_tenants.push_back(clean_tenant))) {
        LOG_WARN("failed to push server clean tenant into clean tenants", K(ret), K(clean_tenant));
      }
    }
  }
  return ret;
}

M
mw0 已提交
288
int ObBackupDataClean::get_server_clean_tenants(ObIArray<ObBackupDataCleanTenant> &clean_tenants)
O
oceanbase-admin 已提交
289 290 291 292
{
  int ret = OB_SUCCESS;
  ObArray<uint64_t> all_tenant_ids;
  ObBackupCleanInfo sys_clean_info;
293
  const bool for_update = false;
O
oceanbase-admin 已提交
294 295 296 297 298 299 300 301 302 303

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(get_all_tenant_ids(all_tenant_ids))) {
    LOG_WARN("failed to get all tenant ids", K(ret));
  } else if (all_tenant_ids.empty()) {
    // do nothing
  } else {
    sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
304
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, *sql_proxy_, sys_clean_info))) {
O
oceanbase-admin 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
      if (OB_BACKUP_CLEAN_INFO_NOT_EXIST == ret) {
        ret = OB_SUCCESS;
      } else {
        LOG_WARN("failed to get clean info", K(ret));
      }
    } else if (ObBackupCleanInfoStatus::PREPARE == sys_clean_info.status_) {
      // do nothing
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < all_tenant_ids.count(); ++i) {
        const uint64_t tenant_id = all_tenant_ids.at(i);
        bool need_add = false;
        if (stop_) {
          ret = OB_RS_SHUTDOWN;
          LOG_WARN("rootservice shutdown", K(ret));
        } else if (OB_SYS_TENANT_ID == tenant_id) {
          need_add = true;
        } else if (OB_FAIL(get_server_need_clean_info(tenant_id, need_add))) {
          LOG_WARN("failed to get server need clean info", K(ret), K(tenant_id));
        }

        if (OB_FAIL(ret)) {
        } else if (!need_add) {
          // do nothing
        } else {
          ObBackupDataCleanTenant clean_tenant;
          clean_tenant.simple_clean_tenant_.tenant_id_ = tenant_id;
          clean_tenant.simple_clean_tenant_.is_deleted_ = false;
          if (OB_FAIL(clean_tenants.push_back(clean_tenant))) {
            LOG_WARN("failed to push clean tenant into array", K(ret), K(clean_tenant));
          }
        }
      }
    }
  }
  return ret;
}

M
mw0 已提交
342
int ObBackupDataClean::get_all_tenant_ids(ObIArray<uint64_t> &tenant_ids)
O
oceanbase-admin 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
{
  int ret = OB_SUCCESS;
  ObSchemaGetterGuard guard;
  tenant_ids.reset();
  ObArray<uint64_t> tmp_tenant_ids;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do init", K(ret));
  } else if (OB_FAIL(schema_service_->get_tenant_schema_guard(OB_SYS_TENANT_ID, guard))) {
    LOG_WARN("failed to get tenant schema guard", K(ret));
  } else if (OB_FAIL(guard.get_tenant_ids(tmp_tenant_ids))) {
    LOG_WARN("failed to get tenant ids", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < tmp_tenant_ids.count(); ++i) {
      const uint64_t tenant_id = tmp_tenant_ids.at(i);
      bool is_restore = false;
      if (OB_FAIL(guard.check_tenant_is_restore(tenant_id, is_restore))) {
        LOG_WARN("failed to check tenant is restore", K(ret), K(tenant_id));
      } else if (is_restore) {
        // do nothing
      } else if (OB_FAIL(tenant_ids.push_back(tenant_id))) {
        LOG_WARN("failed to push tenant id into array", K(ret), K(tenant_id));
      }
    }
  }
  return ret;
}

int ObBackupDataClean::get_backup_clean_info(
M
mw0 已提交
373
    const uint64_t tenant_id, const bool for_update, ObISQLClient &sql_proxy, ObBackupCleanInfo &clean_info)
O
oceanbase-admin 已提交
374 375 376 377 378 379 380 381 382 383 384
{
  int ret = OB_SUCCESS;
  ObTenantBackupCleanInfoUpdater updater;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == clean_info.tenant_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get backup clean info get invalid argument", K(ret), K(tenant_id), K(clean_info));
  } else if (OB_FAIL(updater.init(sql_proxy))) {
    LOG_WARN("failed to init tenant backup clean info updater", K(ret));
385
  } else if (OB_FAIL(updater.get_backup_clean_info(tenant_id, for_update, clean_info))) {
O
oceanbase-admin 已提交
386 387 388 389 390 391 392
    if (OB_BACKUP_CLEAN_INFO_NOT_EXIST != ret) {
      LOG_WARN("failed to get backup clean info", K(ret), K(tenant_id), K(clean_info));
    }
  }
  return ret;
}

M
mw0 已提交
393
int ObBackupDataClean::get_backup_clean_info(const uint64_t tenant_id, ObBackupCleanInfo &clean_info)
O
oceanbase-admin 已提交
394 395 396 397
{
  int ret = OB_SUCCESS;
  ObTenantBackupCleanInfoUpdater updater;
  clean_info.tenant_id_ = tenant_id;
398
  const bool for_update = false;
O
oceanbase-admin 已提交
399 400 401 402 403 404
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get backup clean info get invalid argument", K(ret), K(tenant_id));
405
  } else if (OB_FAIL(get_backup_clean_info(tenant_id, for_update, *sql_proxy_, clean_info))) {
O
oceanbase-admin 已提交
406 407 408 409 410 411 412
    if (OB_BACKUP_CLEAN_INFO_NOT_EXIST != ret) {
      LOG_WARN("failed to get backup clean info", K(ret), K(tenant_id));
    }
  }
  return ret;
}

M
mw0 已提交
413
int ObBackupDataClean::get_server_need_clean_info(const uint64_t tenant_id, bool &need_add)
O
oceanbase-admin 已提交
414 415 416 417 418
{
  int ret = OB_SUCCESS;
  need_add = false;
  ObTenantBackupCleanInfoUpdater updater;
  ObBackupCleanInfo clean_info;
419
  const bool for_update = false;
O
oceanbase-admin 已提交
420 421 422 423 424 425

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean not init", K(ret));
  } else {
    clean_info.tenant_id_ = tenant_id;
426
    if (OB_FAIL(get_backup_clean_info(tenant_id, for_update, *sql_proxy_, clean_info))) {
O
oceanbase-admin 已提交
427 428 429 430 431 432 433 434 435
      if (OB_BACKUP_CLEAN_INFO_NOT_EXIST == ret) {
        need_add = false;
        ret = OB_SUCCESS;
      } else {
        LOG_WARN("failed to get backup clean info", K(ret), K(tenant_id));
      }
    } else {
      need_add = true;
    }
436 437 438
  }
  return ret;
}
O
oceanbase-admin 已提交
439

M
mw0 已提交
440 441
int ObBackupDataClean::get_tenant_backup_task_his_info(const share::ObBackupCleanInfo &clean_info,
    common::ObISQLClient &trans, common::ObIArray<share::ObTenantBackupTaskInfo> &tenant_infos)
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get tenant backup task his info get invalid argument", K(ret), K(clean_info));
  } else {
    if (clean_info.is_clean_copy()) {
      ObArray<ObTenantBackupBackupsetTaskInfo> backup_backupset_tasks;
      if (OB_FAIL(get_tenant_backup_backupset_task_his_info(clean_info, trans, backup_backupset_tasks))) {
        LOG_WARN("failed to get tenant backup backupset task history info", KR(ret), K(clean_info));
      } else if (OB_FAIL(convert_backup_backupset_task_to_backup_task(backup_backupset_tasks, tenant_infos))) {
        LOG_WARN("failed to convert tasks", KR(ret), K(backup_backupset_tasks), K(tenant_infos));
O
oceanbase-admin 已提交
457 458
      }
    } else {
459 460
      if (OB_FAIL(inner_get_tenant_backup_task_his_info(clean_info, trans, tenant_infos))) {
        LOG_WARN("failed to inner get tenant backup task history info", KR(ret), K(clean_info));
O
oceanbase-admin 已提交
461 462 463 464 465 466
      }
    }
  }
  return ret;
}

M
mw0 已提交
467 468
int ObBackupDataClean::inner_get_tenant_backup_task_his_info(const ObBackupCleanInfo &clean_info,
    common::ObISQLClient &trans, common::ObIArray<share::ObTenantBackupTaskInfo> &tenant_infos)
O
oceanbase-admin 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
{
  int ret = OB_SUCCESS;
  tenant_infos.reset();
  ObBackupTaskHistoryUpdater updater;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get tenant backup task his info get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(updater.init(trans))) {
    LOG_WARN("failed to init backup task history updater", K(ret));
  } else if (OB_FAIL(updater.get_tenant_full_backup_tasks(clean_info.tenant_id_, tenant_infos))) {
    LOG_WARN("failed to get tenant backup tasks", K(ret), K(clean_info));
  } else {
    LOG_INFO("succeed get tenant backup task his info", K(tenant_infos));
  }
  return ret;
}

M
mw0 已提交
490 491
int ObBackupDataClean::get_tenant_backup_task_info(const share::ObBackupCleanInfo &clean_info,
    common::ObISQLClient &trans, common::ObIArray<share::ObTenantBackupTaskInfo> &tenant_infos)
O
oceanbase-admin 已提交
492 493 494 495
{
  int ret = OB_SUCCESS;
  ObTenantBackupTaskUpdater updater;
  ObTenantBackupTaskInfo tenant_info;
496
  const bool for_update = false;
O
oceanbase-admin 已提交
497
  // can not tenant infos
498
  // TODO() need consider backup backupset clean
O
oceanbase-admin 已提交
499 500 501 502 503 504 505 506
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get tenant backup task his info get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(updater.init(trans))) {
    LOG_WARN("failed to init backup task history updater", K(ret));
507
  } else if (OB_FAIL(updater.get_tenant_backup_task(clean_info.tenant_id_, for_update, tenant_info))) {
O
oceanbase-admin 已提交
508 509 510 511 512 513 514 515 516 517 518 519
    LOG_WARN("failed to get tenant backup tasks", K(ret), K(clean_info));
  } else if (!tenant_info.is_valid() || !tenant_info.backup_type_.is_full_backup()) {
    // clean base cell is full backup set
    // do nothing
  } else if (OB_FAIL(tenant_infos.push_back(tenant_info))) {
    LOG_WARN("failed to add tenant info into array", K(ret), K(tenant_info));
  } else {
    LOG_INFO("succeed get tenant backup task info", K(tenant_info));
  }
  return ret;
}

M
mw0 已提交
520 521
int ObBackupDataClean::get_tenant_backup_backupset_task_his_info(const share::ObBackupCleanInfo &clean_info,
    common::ObISQLClient &trans, common::ObIArray<share::ObTenantBackupBackupsetTaskInfo> &tenant_infos)
O
oceanbase-admin 已提交
522 523 524 525 526 527 528 529 530
{
  int ret = OB_SUCCESS;
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get tenant backup task his info get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(ObTenantBackupBackupsetHistoryOperator::get_full_task_items(
531
                 clean_info.tenant_id_, clean_info.copy_id_, tenant_infos, trans))) {
O
oceanbase-admin 已提交
532 533 534 535 536 537
    LOG_WARN("failed to get full backupset task items", KR(ret), K(clean_info));
  }
  return ret;
}

int ObBackupDataClean::convert_backup_backupset_task_to_backup_task(
M
mw0 已提交
538 539
    const common::ObIArray<share::ObTenantBackupBackupsetTaskInfo> &backup_backupset_tasks,
    common::ObIArray<share::ObTenantBackupTaskInfo> &backup_tasks)
O
oceanbase-admin 已提交
540 541 542 543 544 545 546 547 548
{
  int ret = OB_SUCCESS;
  backup_tasks.reset();
  if (IS_NOT_INIT) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_backupset_tasks.count(); ++i) {
      ObTenantBackupTaskInfo info;
M
mw0 已提交
549
      const ObTenantBackupBackupsetTaskInfo &bb_info = backup_backupset_tasks.at(i);
O
oceanbase-admin 已提交
550 551 552 553 554 555 556 557 558 559
      if (OB_FAIL(bb_info.convert_to_backup_task_info(info))) {
        LOG_WARN("failed to convert to backup task info", KR(ret), K(bb_info));
      } else if (OB_FAIL(backup_tasks.push_back(info))) {
        LOG_WARN("failed to push back backup info", KR(ret), K(info));
      }
    }
  }
  return ret;
}

560 561
/*not use anymore*/
int ObBackupDataClean::get_log_archive_info(const int64_t copy_id, const uint64_t tenant_id,
M
mw0 已提交
562
    common::ObISQLClient &trans, common::ObIArray<share::ObLogArchiveBackupInfo> &log_archive_infos)
O
oceanbase-admin 已提交
563 564
{
  int ret = OB_SUCCESS;
565 566
  ObLogArchiveBackupInfoMgr mgr;
  const bool for_update = false;
O
oceanbase-admin 已提交
567 568 569 570 571
  ObLogArchiveBackupInfo log_archive_info;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
572
  } else if (OB_INVALID_ID == tenant_id || copy_id < 0) {
O
oceanbase-admin 已提交
573
    ret = OB_INVALID_ARGUMENT;
574 575 576 577 578
    LOG_WARN("log archive info get invalid argument", K(ret), K(tenant_id), K(copy_id));
  } else if (copy_id > 0 && OB_FAIL(mgr.set_backup_backup())) {
    LOG_WARN("failed to set copy id", K(ret), K(copy_id));
  } else if (OB_FAIL(mgr.get_log_archive_backup_info(
                 trans, for_update, tenant_id, inner_table_version_, log_archive_info))) {
O
oceanbase-admin 已提交
579 580 581 582 583 584 585 586 587 588
    LOG_WARN("failed to get log archive backup info", K(ret), K(tenant_id));
  } else if (ObLogArchiveStatus::DOING != log_archive_info.status_.status_ &&
             ObLogArchiveStatus::INTERRUPTED != log_archive_info.status_.status_) {
    // do nothing
  } else if (OB_FAIL(log_archive_infos.push_back(log_archive_info))) {
    LOG_WARN("failed to push log archive info into array", K(ret), K(log_archive_info));
  }
  return ret;
}

589 590
/*not use anymore*/
int ObBackupDataClean::get_log_archive_history_info(const int64_t copy_id, const uint64_t tenant_id,
M
mw0 已提交
591
    common::ObISQLClient &trans, common::ObIArray<share::ObLogArchiveBackupInfo> &log_archive_infos)
O
oceanbase-admin 已提交
592 593
{
  int ret = OB_SUCCESS;
594 595
  ObLogArchiveBackupInfoMgr mgr;
  const bool for_update = false;
O
oceanbase-admin 已提交
596 597 598 599

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
600
  } else if (OB_INVALID_ID == tenant_id || copy_id < 0) {
O
oceanbase-admin 已提交
601
    ret = OB_INVALID_ARGUMENT;
602 603 604 605
    LOG_WARN("log archive info get invalid argument", K(ret), K(copy_id), K(tenant_id));
  } else if (OB_FAIL(mgr.set_backup_backup())) {
    LOG_WARN("failed to set copy id", K(ret), K(copy_id));
  } else if (OB_FAIL(mgr.get_log_archive_history_infos(trans, tenant_id, for_update, log_archive_infos))) {
O
oceanbase-admin 已提交
606 607 608 609 610
    LOG_WARN("failed to get log archive history infos", K(ret), K(tenant_id));
  }
  return ret;
}

611
/*not use anymore*/
O
oceanbase-admin 已提交
612
int ObBackupDataClean::get_extern_clean_tenants(
M
mw0 已提交
613
    hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> &clean_tenants_map)
O
oceanbase-admin 已提交
614 615 616 617 618 619
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfo sys_clean_info;
  ObArray<ObTenantBackupTaskInfo> task_infos;
  ObArray<ObLogArchiveBackupInfo> log_archive_infos;
  hash::ObHashSet<ObBackupDest> backup_dest_set;
620 621 622 623
  ObSimpleBackupDataCleanTenant simple_clean_tenant;
  simple_clean_tenant.tenant_id_ = OB_SYS_TENANT_ID;
  simple_clean_tenant.is_deleted_ = false;
  const bool for_update = false;
O
oceanbase-admin 已提交
624 625 626 627 628 629 630 631

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean not init", K(ret));
  } else if (OB_FAIL(backup_dest_set.create(MAX_BUCKET_NUM))) {
    LOG_WARN("failed to create backup dest set", K(ret));
  } else {
    sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
632
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, *sql_proxy_, sys_clean_info))) {
O
oceanbase-admin 已提交
633 634 635 636 637 638 639
      if (OB_BACKUP_CLEAN_INFO_NOT_EXIST == ret) {
        ret = OB_SUCCESS;
      } else {
        LOG_WARN("failed to get backup clean info", K(ret));
      }
    } else if (ObBackupCleanInfoStatus::STOP == sys_clean_info.status_) {
      // do nothing
640 641 642
    } else if (OB_FAIL(get_all_tenant_backup_infos(
                   sys_clean_info, simple_clean_tenant, *sql_proxy_, task_infos, log_archive_infos))) {
      LOG_WARN("failed to get all tenant backup infos", K(ret), K(sys_clean_info));
O
oceanbase-admin 已提交
643 644 645 646
    }

    // get tenant info from log archive info
    for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_infos.count(); ++i) {
M
mw0 已提交
647
      const ObLogArchiveBackupInfo &info = log_archive_infos.at(i);
O
oceanbase-admin 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
      ObBackupDest backup_dest;
      if (OB_FAIL(backup_dest.set(info.backup_dest_))) {
        LOG_WARN("failed to set backup dest", K(ret), K(info));
      } else {
        int hash_ret = backup_dest_set.exist_refactored(backup_dest);
        if (OB_HASH_EXIST == hash_ret) {
          // do nothing
        } else if (OB_HASH_NOT_EXIST == hash_ret) {
          if (OB_FAIL(get_archive_clean_tenant(info, clean_tenants_map))) {
            LOG_WARN("failed to get archive clean tenant", K(ret), K(info));
          } else if (OB_FAIL(backup_dest_set.set_refactored(backup_dest))) {
            LOG_WARN("failed to set backup dest", K(ret), K(info));
          }
        } else {
          ret = OB_SUCCESS != hash_ret ? hash_ret : OB_ERR_UNEXPECTED;
          LOG_WARN("failed to check exist from hash set", K(ret), K(backup_dest_set));
        }
      }
    }

    // get tenant info from backup tenant info
    backup_dest_set.reuse();

    for (int64_t i = 0; OB_SUCC(ret) && i < task_infos.count(); ++i) {
M
mw0 已提交
672
      const ObTenantBackupTaskInfo &task_info = task_infos.at(i);
O
oceanbase-admin 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
      int hash_ret = backup_dest_set.exist_refactored(task_info.backup_dest_);
      if (OB_HASH_EXIST == hash_ret) {
        // do nothing
      } else if (OB_HASH_NOT_EXIST == hash_ret) {
        if (OB_FAIL(get_backup_clean_tenant(task_info, clean_tenants_map))) {
          LOG_WARN("failed to get extern clean tenant", K(ret), K(task_info));
        } else if (OB_FAIL(backup_dest_set.set_refactored(task_info.backup_dest_))) {
          LOG_WARN("failed to set backup dest", K(ret), K(task_info));
        }
      } else {
        ret = hash_ret != OB_SUCCESS ? hash_ret : OB_ERR_UNEXPECTED;
        LOG_WARN("can not check exist from hash set", K(ret));
      }
    }
  }
  return ret;
}

M
mw0 已提交
691 692
int ObBackupDataClean::get_archive_clean_tenant(const share::ObLogArchiveBackupInfo &log_archive_info,
    hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> &clean_tenants_map)
O
oceanbase-admin 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
{
  int ret = OB_SUCCESS;
  ObTenantNameSimpleMgr tenant_name_mgr;
  hash::ObHashSet<uint64_t> tenant_id_set;
  ObClusterBackupDest dest;
  // TODO() cluster dest
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean not init", K(ret));
  } else if (!log_archive_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get archive clean tenant get invalid argument", K(ret), K(log_archive_info));
  } else if (OB_FAIL(tenant_id_set.create(MAX_BUCKET_NUM))) {
    LOG_WARN("failed to create tenant id set", K(ret), K(log_archive_info));
  } else if (OB_FAIL(dest.set(log_archive_info.backup_dest_,
                 GCONF.cluster,
                 GCONF.cluster_id,
                 log_archive_info.status_.incarnation_))) {
    LOG_WARN("failed to set backup dest", K(ret), K(log_archive_info));
  } else if (OB_FAIL(tenant_name_mgr.init())) {
    LOG_WARN("faiuiled to init tenant_name mgr", K(ret));
  } else if (OB_FAIL(tenant_name_mgr.read_backup_file(dest))) {
    LOG_WARN("failed to read backup tenant name mgr", K(ret), K(dest));
  } else if (OB_FAIL(tenant_name_mgr.get_tenant_ids(tenant_id_set))) {
    if (OB_BACKUP_FILE_NOT_EXIST != ret) {
      LOG_WARN("failed to get tenant ids", K(ret), K(log_archive_info));
    }
  } else {
    for (hash::ObHashSet<uint64_t>::iterator iter = tenant_id_set.begin(); OB_SUCC(ret) && iter != tenant_id_set.end();
         ++iter) {
      ObSimpleBackupDataCleanTenant simple_clean_tenant;
      simple_clean_tenant.tenant_id_ = iter->first;
      simple_clean_tenant.is_deleted_ = true;
      int hash_ret = clean_tenants_map.set_refactored(simple_clean_tenant.tenant_id_, simple_clean_tenant);
      if (OB_SUCCESS == hash_ret) {
        // do nothing
      } else if (OB_HASH_EXIST == hash_ret) {
        ret = OB_SUCCESS;
      } else {
        ret = hash_ret;
        LOG_WARN("failed to set simple clean tenant info set", K(ret), K(simple_clean_tenant), K(log_archive_info));
      }
    }
  }

  return ret;
}

M
mw0 已提交
741 742
int ObBackupDataClean::get_backup_clean_tenant(const share::ObTenantBackupTaskInfo &task_info,
    hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> &clean_tenants_map)
O
oceanbase-admin 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
{
  int ret = OB_SUCCESS;
  ObExternTenantInfoMgr tenant_info_mgr;
  ObClusterBackupDest backup_dest;
  char backup_dest_str[share::OB_MAX_BACKUP_DEST_LENGTH] = "";
  ObArray<ObExternTenantInfo> extern_tenant_infos;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean not init", K(ret));
  } else if (!task_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get extern clean tenant get invalid argument", K(ret), K(task_info));
  } else if (OB_FAIL(task_info.backup_dest_.get_backup_dest_str(backup_dest_str, OB_MAX_BACKUP_DEST_LENGTH))) {
    LOG_WARN("failed to get backup dest str", K(ret), K(task_info));
  } else if (OB_FAIL(backup_dest.set(backup_dest_str, GCONF.cluster, task_info.cluster_id_, task_info.incarnation_))) {
    LOG_WARN("failed to set backup dest", K(ret), K(task_info));
  } else if (OB_FAIL(tenant_info_mgr.init(backup_dest, *backup_lease_service_))) {
    LOG_WARN("failed to init tenant info mgr", K(ret), K(backup_dest));
  } else if (OB_FAIL(tenant_info_mgr.get_extern_tenant_infos(extern_tenant_infos))) {
    LOG_WARN("failed to get extern tenant infos", K(ret), K(backup_dest));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < extern_tenant_infos.count(); ++i) {
      ObSimpleBackupDataCleanTenant simple_clean_tenant;
      simple_clean_tenant.tenant_id_ = extern_tenant_infos.at(i).tenant_id_;
      simple_clean_tenant.is_deleted_ = true;
      int hash_ret = clean_tenants_map.set_refactored(simple_clean_tenant.tenant_id_, simple_clean_tenant);
      if (OB_SUCCESS == hash_ret) {
        // do nothing
      } else if (OB_HASH_EXIST == hash_ret) {
        ret = OB_SUCCESS;
      } else {
        ret = hash_ret;
        LOG_WARN("failed to set simple clean tenant info set", K(ret), K(simple_clean_tenant), K(task_info));
      }
    }
  }
  return ret;
}

M
mw0 已提交
783
int ObBackupDataClean::do_clean_scheduler(ObIArray<ObBackupDataCleanTenant> &clean_tenants)
O
oceanbase-admin 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
{
  LOG_INFO("start do clean scheduler", K(clean_tenants));
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("root backup do not init", K(ret));
  } else if (clean_tenants.empty()) {
    // do nothing
  } else if (OB_FAIL(do_schedule_clean_tenants(clean_tenants))) {
    LOG_WARN("failed to do schedule normal tenants", K(ret));
  } else if (OB_FAIL(do_check_clean_tenants_finished(clean_tenants))) {
    LOG_WARN("failed to do schedule sys tenant", K(ret));
  }
  return ret;
}

M
mw0 已提交
800
int ObBackupDataClean::do_schedule_clean_tenants(ObIArray<ObBackupDataCleanTenant> &clean_tenants)
O
oceanbase-admin 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  LOG_INFO("start do clean scheduler", K(clean_tenants));
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("root backup do not init", K(ret));
  } else if (clean_tenants.empty()) {
    // do nothing
  } else {
    // first clean normal tenant and then clean sys tenant
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_tenants.count(); ++i) {
      ObBackupCleanInfo clean_info;
M
mw0 已提交
814
      ObBackupDataCleanTenant &clean_tenant = clean_tenants.at(i);
O
oceanbase-admin 已提交
815 816 817 818
      DEBUG_SYNC(BACKUP_DATA_CLEAN_STATUS_SCHEDULE);
      if (stop_) {
        ret = OB_RS_SHUTDOWN;
        LOG_WARN("rootservice shutdown", K(ret));
819
      } else if (OB_SUCCESS != (tmp_ret = do_tenant_clean_scheduler(clean_info, clean_tenant))) {
O
oceanbase-admin 已提交
820 821 822 823 824 825 826 827 828 829
        LOG_WARN("failed to do tenant clean scheduler", K(tmp_ret), K(clean_tenant));
      } else if (OB_SUCCESS != (tmp_ret = do_with_status(clean_info, clean_tenant))) {
        LOG_WARN("failed to do tenant backup clean status", K(tmp_ret), K(clean_info), K(clean_tenant));
      }
      set_inner_error(tmp_ret);
    }
  }
  return ret;
}

M
mw0 已提交
830
int ObBackupDataClean::do_check_clean_tenants_finished(const common::ObIArray<ObBackupDataCleanTenant> &clean_tenants)
O
oceanbase-admin 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfo sys_clean_info;
  ObArray<ObSimpleBackupDataCleanTenant> normal_clean_tenants;
  ObBackupDataCleanTenant sys_clean_tenant;
  int32_t clean_result = OB_SUCCESS;
  bool is_all_tasks_stopped = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("root backup do not init", K(ret));
  } else if (clean_tenants.empty()) {
    // do nothing
  } else if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, sys_clean_info))) {
    LOG_WARN("failed to get sys clean info", K(ret), K(sys_clean_info));
  } else if (ObBackupCleanInfoStatus::DOING != sys_clean_info.status_ &&
             ObBackupCleanInfoStatus::CANCEL != sys_clean_info.status_) {
    // do nothing
  } else if (OB_FAIL(get_clean_tenants(clean_tenants, normal_clean_tenants, sys_clean_tenant))) {
    LOG_WARN("failed to get clean tenants", K(ret), K(clean_tenants));
  } else if (OB_FAIL(do_with_failed_tenant_clean_task(normal_clean_tenants, clean_result))) {
    LOG_WARN("failed to do with failed tenant clean task", K(ret));
  } else if (OB_FAIL(check_all_tenant_clean_tasks_stopped(normal_clean_tenants, is_all_tasks_stopped))) {
    LOG_WARN("failed ot check all tennat clean tasks stopped", K(ret));
  } else if (is_all_tasks_stopped && OB_FAIL(do_with_finished_tenant_clean_task(
                                         normal_clean_tenants, sys_clean_info, sys_clean_tenant, clean_result))) {
    LOG_WARN("failed to do with finished tenant clean task", K(ret));
  } else {
    wakeup();
  }
  return ret;
}

M
mw0 已提交
864
int ObBackupDataClean::do_with_status(const share::ObBackupCleanInfo &clean_info, ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("root backup do not init", K(ret));
  } else if (stop_) {
    ret = OB_RS_SHUTDOWN;
    LOG_WARN("rootservice shutdown", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("backup info is invalid", K(ret), K(clean_info));
  } else {
    LOG_INFO("do with status", K(clean_info));
M
mw0 已提交
878
    const ObBackupCleanInfoStatus::STATUS &status = clean_info.status_;
O
oceanbase-admin 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
    switch (status) {
      case ObBackupCleanInfoStatus::STOP:
        break;
      case ObBackupCleanInfoStatus::PREPARE:
        break;
      case ObBackupCleanInfoStatus::DOING:
        if (OB_FAIL(do_tenant_backup_clean(clean_info, clean_tenant))) {
          LOG_WARN("failed to do backup", K(ret), K(clean_info));
        }
        break;
      case ObBackupCleanInfoStatus::CANCEL:
        if (OB_FAIL(do_tenant_cancel_delete_backup(clean_info, clean_tenant))) {
          LOG_WARN("failed to do cancel delete backup", K(ret), K(clean_info));
        }
        break;
      case ObBackupCleanInfoStatus::MAX:
        ret = OB_ERR_UNEXPECTED;
        LOG_ERROR("backup clean info status is invalid", K(ret), K(clean_info));
        break;
    }
    LOG_INFO("doing backup clean", K(clean_info), K(ret));
  }
  return ret;
}

int ObBackupDataClean::prepare_tenant_backup_clean()
{
  int ret = OB_SUCCESS;
  ObArray<uint64_t> tenant_ids;
  ObBackupCleanInfo sys_clean_info;
909
  // TODO(muwei.ym) use already marked info to delete, do not calculate it again when switch rs
O
oceanbase-admin 已提交
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_FAIL(get_all_tenant_ids(tenant_ids))) {
    LOG_WARN("failed to get all tenant ids", K(ret));
  } else if (tenant_ids.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("schedule get tenants backup get invalid argument", K(ret), K(tenant_ids));
  } else if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, sys_clean_info))) {
    if (OB_BACKUP_CLEAN_INFO_NOT_EXIST == ret) {
      ret = OB_SUCCESS;
    } else {
      LOG_WARN("failed to get backup clean info", K(ret));
    }
925 926
  } else if (ObBackupCleanInfoStatus::STOP == sys_clean_info.status_ ||
             ObBackupCleanInfoStatus::CANCEL == sys_clean_info.status_) {
O
oceanbase-admin 已提交
927
    // do nothing
928
  } else if (OB_FAIL(mark_sys_tenant_backup_meta_data_deleting())) {
O
oceanbase-admin 已提交
929 930 931 932 933 934 935 936
    LOG_WARN("failed to mark sys tenant backup meta data", K(ret), K(sys_clean_info));
  } else if (OB_FAIL(schedule_tenants_backup_data_clean(tenant_ids))) {
    LOG_WARN("failed to schedule tenant backup data clean", K(ret), K(tenant_ids));
  }

  return ret;
}

937
int ObBackupDataClean::mark_sys_tenant_backup_meta_data_deleting()
O
oceanbase-admin 已提交
938 939 940 941 942 943
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfo sys_clean_info;
  ObBackupDataCleanTenant sys_clean_tenant;
  ObArray<ObTenantBackupTaskInfo> task_infos;
  ObArray<ObLogArchiveBackupInfo> log_archive_infos;
944 945 946
  CompareLogArchiveBackupInfo log_archive_info_cmp;
  CompareBackupTaskInfo backup_task_cmp;
  const bool for_update = false;
O
oceanbase-admin 已提交
947 948 949 950 951 952

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("root backup do not init", K(ret));
  } else {
    sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
953
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, *sql_proxy_, sys_clean_info))) {
O
oceanbase-admin 已提交
954
      LOG_WARN("failed to get backup clean info", K(ret), K(sys_clean_info));
955
    } else if (ObBackupCleanInfoStatus::STOP == sys_clean_info.status_) {
O
oceanbase-admin 已提交
956
      // do nothing
957 958 959
    } else if (ObBackupCleanInfoStatus::DOING == sys_clean_info.status_) {
      if (OB_FAIL(prepare_delete_backup_set(sys_clean_info))) {
        LOG_WARN("failed to prepare delete backup set", K(ret), K(sys_clean_info));
M
mw0 已提交
960 961
      } else if (OB_FAIL(prepare_delete_backup_piece_and_round(sys_clean_info))) {
        LOG_WARN("failed to prepare delete backup piece and round", K(ret), K(sys_clean_info));
962
      }
O
oceanbase-admin 已提交
963 964 965
    } else {
      sys_clean_tenant.simple_clean_tenant_.tenant_id_ = OB_SYS_TENANT_ID;
      sys_clean_tenant.simple_clean_tenant_.is_deleted_ = false;
966 967 968 969 970 971

      if (OB_FAIL(get_all_tenant_backup_infos(
              sys_clean_info, sys_clean_tenant.simple_clean_tenant_, *sql_proxy_, task_infos, log_archive_infos))) {
        LOG_WARN("failed to get all tenant backup infos", K(ret), K(sys_clean_info));
      } else if (FALSE_IT(std::sort(task_infos.begin(), task_infos.end(), backup_task_cmp))) {
      } else if (FALSE_IT(std::sort(log_archive_infos.begin(), log_archive_infos.end(), log_archive_info_cmp))) {
O
oceanbase-admin 已提交
972 973 974
      } else if (OB_FAIL(get_backup_clean_elements(sys_clean_info, task_infos, log_archive_infos, sys_clean_tenant))) {
        LOG_WARN("failed to get backup clean elements", K(ret), K(sys_clean_info), K(task_infos));
      } else if (sys_clean_info.is_backup_set_clean() &&
975
                 !sys_clean_tenant.has_clean_backup_set(sys_clean_info.backup_set_id_, sys_clean_info.copy_id_)) {
O
oceanbase-admin 已提交
976
        ret = OB_BACKUP_DELETE_BACKUP_SET_NOT_ALLOWED;
W
WenJinyu 已提交
977
        LOG_WARN("can not clean backup set, because backup set is not existt", K(ret), K(sys_clean_info), K(sys_clean_tenant));
O
oceanbase-admin 已提交
978 979 980 981 982 983 984 985
      }

      if (OB_SUCC(ret)) {
        if (OB_SUCCESS != sys_clean_info.result_) {
          // do nothing
        } else if (OB_FAIL(
                       update_clog_gc_snaphost(sys_clean_info.clog_gc_snapshot_, sys_clean_info, sys_clean_tenant))) {
          LOG_WARN("failed to update clog gc snapshot", K(ret), K(sys_clean_info));
986
        } else if (OB_FAIL(mark_backup_meta_data_deleting(sys_clean_info, sys_clean_tenant))) {
O
oceanbase-admin 已提交
987
          LOG_WARN("failed to mark backup meta data deleted", K(ret), K(sys_clean_info));
M
mw0 已提交
988 989
        } else if (OB_FAIL(check_backup_dest_lifecycle(sys_clean_tenant))) {
          LOG_WARN("failed to check backup dest lifecycle", K(ret), K(sys_clean_info));
O
oceanbase-admin 已提交
990
        }
991 992 993 994
        // Forbidden check backup dest lifecycle, because oss has bug which make observer core
        // else if (OB_FAIL(check_backup_dest_lifecycle(sys_clean_tenant))) {
        //  LOG_WARN("failed to check backup dest lifecycle", K(ret), K(sys_clean_info));
        // }
O
oceanbase-admin 已提交
995 996 997 998 999 1000
      }
    }
  }
  return ret;
}

M
mw0 已提交
1001
int ObBackupDataClean::schedule_tenants_backup_data_clean(const common::ObIArray<uint64_t> &tenant_ids)
O
oceanbase-admin 已提交
1002 1003 1004 1005 1006
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfo sys_clean_info;
  ObBackupCleanInfo dest_clean_info;
  ObMySQLTransaction trans;
1007 1008
  ObTimeoutCtx timeout_ctx;
  const bool for_update = true;
O
oceanbase-admin 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (tenant_ids.empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("schedule get tenants backup get invalid argument", K(ret), K(tenant_ids));
  } else {
    DEBUG_SYNC(BACKUP_DATA_CLEAN_STATUS_PREPARE);
  }

  if (OB_FAIL(ret)) {
1021 1022
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
O
oceanbase-admin 已提交
1023 1024
  } else {
    sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
1025
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, trans, sys_clean_info))) {
O
oceanbase-admin 已提交
1026
      LOG_WARN("failed to get backup clean info", K(ret), K(sys_clean_info));
1027
    } else if (ObBackupCleanInfoStatus::STOP == sys_clean_info.status_) {
O
oceanbase-admin 已提交
1028 1029
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("sys backup info status is unexpected", K(ret), K(sys_clean_info));
1030 1031 1032
    } else if (ObBackupCleanInfoStatus::DOING == sys_clean_info.status_ ||
               ObBackupCleanInfoStatus::CANCEL == sys_clean_info.status_) {
      // do nothing
O
oceanbase-admin 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < tenant_ids.count(); ++i) {
        const uint64_t tenant_id = tenant_ids.at(i);
        if (stop_) {
          ret = OB_RS_SHUTDOWN;
          LOG_WARN("rootservice shutdown", K(ret));
        } else if (OB_SYS_TENANT_ID == tenant_id) {
          // do nothing
        } else if (OB_FAIL(schedule_tenant_backup_data_clean(tenant_id, sys_clean_info, trans))) {
          LOG_WARN("failed to schedule tenant backup", K(ret), K(tenant_id));
        }
      }
    }

    if (OB_SUCC(ret)) {
      dest_clean_info = sys_clean_info;
      dest_clean_info.status_ = ObBackupCleanInfoStatus::DOING;
      if (OB_FAIL(update_clean_info(OB_SYS_TENANT_ID, sys_clean_info, dest_clean_info, trans))) {
        LOG_WARN("failed to update clean info", K(ret), K(sys_clean_info));
      }
    }

    if (OB_SUCC(ret)) {
1056 1057
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
O
oceanbase-admin 已提交
1058 1059
      }
    } else {
1060 1061 1062 1063
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
O
oceanbase-admin 已提交
1064 1065 1066 1067 1068 1069 1070
      }
    }
  }
  return ret;
}

int ObBackupDataClean::schedule_tenant_backup_data_clean(
M
mw0 已提交
1071
    const uint64_t tenant_id, const share::ObBackupCleanInfo &sys_clean_info, ObISQLClient &sys_tenant_trans)
O
oceanbase-admin 已提交
1072 1073 1074 1075 1076 1077
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfo clean_info;
  ObBackupCleanInfo dest_clean_info;
  ObMySQLTransaction trans;
  int64_t parameter = 0;
1078 1079
  ObTimeoutCtx timeout_ctx;
  const bool for_update = true;
O
oceanbase-admin 已提交
1080 1081 1082 1083 1084 1085 1086

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup scheduler do not init", K(ret));
  } else if (OB_SYS_TENANT_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("tenant id should be sys tenant id", K(ret), K(tenant_id));
1087 1088
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
O
oceanbase-admin 已提交
1089 1090
  } else {
    clean_info.tenant_id_ = tenant_id;
1091
    if (OB_FAIL(get_backup_clean_info(tenant_id, for_update, trans, clean_info))) {
O
oceanbase-admin 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
      LOG_WARN("failed to get backup clean info", K(ret), K(tenant_id));
    } else if (!clean_info.is_valid()) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("backup clean info should not be invalid", K(ret), K(clean_info));
    } else if (ObBackupCleanInfoStatus::PREPARE == clean_info.status_) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("normal tenant backup clean info should not be PREPARE", K(ret), K(clean_info));
    } else if (ObBackupCleanInfoStatus::DOING == clean_info.status_) {
      // do nothing
    } else if (OB_FAIL(sys_clean_info.get_clean_parameter(parameter))) {
      LOG_WARN("failed to get clean parameter", K(ret), K(sys_clean_info));
    } else {
      dest_clean_info = clean_info;
      dest_clean_info.status_ = ObBackupCleanInfoStatus::DOING;
      dest_clean_info.incarnation_ = sys_clean_info.incarnation_;
      dest_clean_info.job_id_ = sys_clean_info.job_id_;
      dest_clean_info.type_ = sys_clean_info.type_;
      dest_clean_info.start_time_ = ObTimeUtil::current_time();
1110
      dest_clean_info.copy_id_ = sys_clean_info.copy_id_;
O
oceanbase-admin 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
      if (OB_FAIL(dest_clean_info.set_clean_parameter(parameter))) {
        LOG_WARN("failed to set clean parameter", K(ret), K(dest_clean_info), K(sys_clean_info));
      } else if (OB_FAIL(rootserver::ObBackupUtil::check_sys_clean_info_trans_alive(sys_tenant_trans))) {
        LOG_WARN("failed to check sys tenant trans alive", K(ret), K(clean_info));
      } else if (OB_FAIL(update_clean_info(clean_info.tenant_id_, clean_info, dest_clean_info, trans))) {
        LOG_WARN("failed to update backup clean info", K(ret), K(clean_info), K(dest_clean_info));
      }
    }

    if (OB_SUCC(ret)) {
1121 1122
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
O
oceanbase-admin 已提交
1123 1124
      }
    } else {
1125 1126 1127 1128
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
O
oceanbase-admin 已提交
1129 1130 1131 1132 1133 1134
      }
    }
  }
  return ret;
}

M
mw0 已提交
1135
int ObBackupDataClean::do_tenant_clean_scheduler(ObBackupCleanInfo &clean_info, ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
{
  int ret = OB_SUCCESS;
  clean_tenant.backup_element_array_.reset();
  ObBackupCleanInfo sys_clean_info;
  ObArray<ObTenantBackupTaskInfo> task_infos;
  ObArray<ObLogArchiveBackupInfo> log_archive_infos;
  const int64_t start_ts = ObTimeUtil::current_time();
  // TODO copy_id
  const int64_t copy_id = 0;
  CompareLogArchiveBackupInfo log_archive_info_cmp;
  CompareBackupTaskInfo backup_task_info_cmp;
  LOG_INFO("start do tenant clean scheduler", K(clean_tenant));
1148

O
oceanbase-admin 已提交
1149 1150 1151 1152 1153 1154
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_tenant.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("do tenant clean scheduler get invalid argument", K(ret), K(clean_tenant));
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
  } else if (OB_FAIL(do_scheduler_normal_tenant(clean_info, clean_tenant, task_infos, log_archive_infos))) {
    LOG_WARN("failed to do scheduler normal tenant", K(ret), K(clean_info));
  } else if (ObBackupCleanInfoStatus::STOP == clean_info.status_) {
    // do nothing
  } else if (FALSE_IT(std::sort(log_archive_infos.begin(), log_archive_infos.end(), log_archive_info_cmp))) {
  } else if (FALSE_IT(std::sort(task_infos.begin(), task_infos.end(), backup_task_info_cmp))) {
  } else if (OB_FAIL(get_backup_clean_elements(clean_info, task_infos, log_archive_infos, clean_tenant))) {
    LOG_WARN("failed to get backup clean elements", K(ret), K(clean_tenant), K(task_infos));
  } else if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, sys_clean_info))) {
    LOG_WARN("failed to get backup clean info", K(ret), K(clean_tenant));
  } else if (OB_FAIL(update_clog_gc_snaphost(sys_clean_info.clog_gc_snapshot_, clean_info, clean_tenant))) {
    LOG_WARN("failed to update clog gc snapshot", K(ret), K(sys_clean_info), K(clean_tenant));
O
oceanbase-admin 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
  }
  LOG_INFO("finish prepare tenant clean info",
      "cost",
      ObTimeUtil::current_time() - start_ts,
      K(clean_tenant),
      K(clean_info));
  return ret;
}

// ObBackupdataCleanTeannt
//   -- ObBackupDataCleanElement[0]
//   -- ObbackupDest
//   -- log_archive_round_array (ascending order)
//   -- backup_set_id_array(descending order)
M
mw0 已提交
1181 1182 1183
int ObBackupDataClean::get_backup_clean_elements(const share::ObBackupCleanInfo &clean_info,
    const common::ObIArray<share::ObTenantBackupTaskInfo> &task_infos,
    const common::ObArray<share::ObLogArchiveBackupInfo> &log_archive_infos, ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
{
  int ret = OB_SUCCESS;
  ObTenantBackupTaskInfo min_include_task_info;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_tenant.is_valid() || !clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get backup clean elements get invalid argument", K(ret), K(clean_tenant), K(clean_info));
  } else if (clean_info.is_backup_set_clean()) {
    if (OB_FAIL(
            add_delete_backup_set(clean_info, task_infos, log_archive_infos, clean_tenant, min_include_task_info))) {
      LOG_WARN("failed to add delete backup set", K(ret), K(clean_info), K(clean_tenant));
    }
1199
  } else if (clean_info.is_delete_obsolete()) {
O
oceanbase-admin 已提交
1200
    if (OB_FAIL(
1201
            add_obsolete_backup_sets(clean_info, task_infos, log_archive_infos, clean_tenant, min_include_task_info))) {
O
oceanbase-admin 已提交
1202 1203
      LOG_WARN("failed to add expired backup set", K(ret), K(clean_info), K(clean_tenant));
    }
1204 1205 1206 1207 1208 1209 1210 1211
  } else if (clean_info.is_delete_backup_piece()) {
    if (OB_FAIL(add_delete_backup_piece(clean_info, task_infos, log_archive_infos, clean_tenant))) {
      LOG_WARN("failed to add delete backup piece", K(ret), K(clean_info));
    }
  } else if (clean_info.is_delete_backup_round()) {
    if (OB_FAIL(add_delete_backup_round(clean_info, task_infos, log_archive_infos, clean_tenant))) {
      LOG_WARN("failed to add delete backup piece", K(ret), K(clean_info));
    }
O
oceanbase-admin 已提交
1212 1213 1214 1215 1216 1217 1218
  } else {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("clean info type is invalid", K(ret), K(clean_info));
  }

  if (OB_SUCC(ret)) {
    clean_tenant.clog_data_clean_point_ = min_include_task_info;
1219
    LOG_INFO("succeed get clean tenant info", K(clean_tenant), K(clean_info));
O
oceanbase-admin 已提交
1220 1221 1222 1223 1224
  }
  return ret;
}

int ObBackupDataClean::add_log_archive_infos(
M
mw0 已提交
1225
    const common::ObIArray<share::ObLogArchiveBackupInfo> &log_archive_infos, ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
1226 1227 1228 1229 1230 1231 1232
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_infos.count(); ++i) {
M
mw0 已提交
1233
      const ObLogArchiveBackupInfo &log_archive_info = log_archive_infos.at(i);
1234 1235 1236 1237 1238 1239 1240
      if (OB_FAIL(add_log_archive_info(log_archive_info, clean_tenant))) {
        LOG_WARN("failed to add log archive info", K(ret), K(log_archive_info));
      }
    }
  }
  return ret;
}
O
oceanbase-admin 已提交
1241

1242
int ObBackupDataClean::add_log_archive_info(
M
mw0 已提交
1243
    const ObLogArchiveBackupInfo &log_archive_info, ObBackupDataCleanTenant &clean_tenant)
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
  const bool for_update = false;
  const bool is_backup_backup = log_archive_info.status_.copy_id_ > 0;
  ObArray<ObBackupPieceInfo> piece_infos;
  int64_t min_copies_num = INT64_MAX;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!log_archive_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("add log archive info get invalid argument", K(ret), K(log_archive_info));
  } else if (OB_FAIL(log_archive_info_mgr.get_round_backup_piece_infos(*sql_proxy_,
                 for_update,
                 log_archive_info.status_.tenant_id_,
                 log_archive_info.status_.incarnation_,
                 log_archive_info.status_.round_,
                 is_backup_backup,
                 piece_infos))) {
    LOG_WARN("failed to get round backup piece infos", K(ret), K(log_archive_info));
  } else {
    ObBackupDest backup_dest;
    ObBackupDestOpt backup_dest_option;
    ObLogArchiveRound log_archive_round;
    ObSimplePieceInfo simple_piece_info;
    log_archive_round.log_archive_round_ = log_archive_info.status_.round_;
    log_archive_round.log_archive_status_ = log_archive_info.status_.status_;
    log_archive_round.start_ts_ = log_archive_info.status_.start_ts_;
    log_archive_round.checkpoint_ts_ = log_archive_info.status_.checkpoint_ts_;
    log_archive_round.start_piece_id_ = log_archive_info.status_.start_piece_id_;
    log_archive_round.copy_id_ = log_archive_info.status_.copy_id_;
    for (int64_t i = 0; OB_SUCC(ret) && i < piece_infos.count(); ++i) {
      simple_piece_info.reset();
M
mw0 已提交
1279
      const ObBackupPieceInfo &piece_info = piece_infos.at(i);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
      if (ObBackupFileStatus::BACKUP_FILE_DELETED == piece_info.file_status_) {
        // do nothing
      } else {
        simple_piece_info.round_id_ = piece_info.key_.round_id_;
        simple_piece_info.backup_piece_id_ = piece_info.key_.backup_piece_id_;
        simple_piece_info.checkpoint_ts_ = piece_info.checkpoint_ts_;
        simple_piece_info.create_date_ = piece_info.create_date_;
        simple_piece_info.file_status_ = piece_info.file_status_;
        simple_piece_info.max_ts_ = piece_info.max_ts_;
        simple_piece_info.start_ts_ = piece_info.start_ts_;
        simple_piece_info.status_ = piece_info.status_;
        if (0 == piece_info.key_.copy_id_) {
          if (OB_FAIL(get_backup_piece_file_copies_num(piece_info, simple_piece_info.copies_num_))) {
            LOG_WARN("failed to get backup piece file copies num", K(ret), K(piece_info));
          } else {
            min_copies_num = std::min(min_copies_num, simple_piece_info.copies_num_);
          }
        } else {
          // do nothing
        }

        if (OB_FAIL(ret)) {
M
mw0 已提交
1302 1303
        } else if (0 != STRCMP(piece_info.backup_dest_.ptr(), log_archive_info.backup_dest_)) {
          // do nothing
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
        } else if (OB_FAIL(log_archive_round.add_simpe_piece_info(simple_piece_info))) {
          LOG_WARN("failed to add simple piece info", K(ret), K(simple_piece_info));
        }
      }
    }

    if (OB_SUCC(ret)) {
      if (INT64_MAX == min_copies_num) {
        // do nothing
      } else {
        log_archive_round.copies_num_ = min_copies_num;
O
oceanbase-admin 已提交
1315 1316
      }
    }
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330

    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(backup_dest.set(log_archive_info.backup_dest_))) {
      LOG_WARN("failed to set backup dest", K(ret), K(log_archive_info));
    } else if (OB_FAIL(get_backup_dest_option(backup_dest, backup_dest_option))) {
      LOG_WARN("failed to get backup dest option", K(ret), K(backup_dest));
    } else if (OB_FAIL(clean_tenant.set_backup_clean_archive_round(GCONF.cluster_id,
                   log_archive_info.status_.incarnation_,
                   backup_dest,
                   log_archive_round,
                   backup_dest_option))) {
      LOG_WARN(
          "failed to set backup clean element", K(ret), K(log_archive_info), K(clean_tenant), K(backup_dest_option));
    }
O
oceanbase-admin 已提交
1331 1332 1333 1334
  }
  return ret;
}

M
mw0 已提交
1335 1336 1337 1338
int ObBackupDataClean::add_delete_backup_set(const share::ObBackupCleanInfo &clean_info,
    const common::ObIArray<share::ObTenantBackupTaskInfo> &task_infos,
    const common::ObArray<ObLogArchiveBackupInfo> &log_archive_infos, ObBackupDataCleanTenant &clean_tenant,
    ObTenantBackupTaskInfo &clog_data_clean_point)
O
oceanbase-admin 已提交
1339 1340 1341
{
  int ret = OB_SUCCESS;
  int64_t cluster_max_backup_set_id = 0;
1342 1343 1344
  const int64_t copy_id = clean_info.copy_id_;
  int64_t copies_num = 0;

O
oceanbase-admin 已提交
1345 1346 1347 1348 1349 1350
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("add delete backup set get invalid argument", K(ret), K(clean_info));
1351
  } else if (OB_FAIL(get_cluster_max_succeed_backup_set(copy_id, cluster_max_backup_set_id))) {
O
oceanbase-admin 已提交
1352 1353 1354 1355 1356 1357
    LOG_WARN("failed to get cluster max succeed backup set", K(ret), K(clean_info));
  } else {
    ObTenantBackupTaskInfo delete_backup_info;
    bool has_kept_last_succeed_data = false;
    bool is_continue = false;
    for (int64_t i = task_infos.count() - 1; OB_SUCC(ret) && i >= 0; --i) {
M
mw0 已提交
1358
      const ObTenantBackupTaskInfo &task_info = task_infos.at(i);
O
oceanbase-admin 已提交
1359 1360 1361 1362 1363
      is_continue = false;
      // consider can clean up
      if (ObTenantBackupTaskInfo::FINISH != task_info.status_) {
        if (task_info.backup_set_id_ == clean_info.backup_set_id_) {
          ret = OB_BACKUP_DELETE_BACKUP_SET_NOT_ALLOWED;
W
WenJinyu 已提交
1364 1365 1366 1367 1368
          LOG_WARN("backup set do not allow clean, because status of backup set is not finish",
              K(ret),
              K(clean_info),
              K(cluster_max_backup_set_id),
              K(task_info));
O
oceanbase-admin 已提交
1369 1370 1371
        }
      } else if (task_info.backup_set_id_ == clean_info.backup_set_id_) {
        delete_backup_info = task_info;
1372
      } else if (!task_info.is_result_succeed() || !task_info.backup_type_.is_full_backup()) {
O
oceanbase-admin 已提交
1373 1374 1375 1376
        // do nothing
      } else if (OB_FAIL(check_backupset_continue_with_clog_data(task_info, log_archive_infos, is_continue))) {
        LOG_WARN("failed to check backupset contine with clog data", K(ret), K(task_info));
      } else if (!is_continue) {
1377
        LOG_INFO("backup set is not continue with clog data", K(task_info), K(log_archive_infos));
O
oceanbase-admin 已提交
1378 1379 1380 1381 1382 1383 1384
      } else {
        has_kept_last_succeed_data = true;
        clog_data_clean_point = task_info;
      }
    }

    if (OB_SUCC(ret)) {
1385
      ObBackupDestOpt backup_dest_option;
O
oceanbase-admin 已提交
1386 1387
      ObBackupSetId backup_set_id;
      backup_set_id.backup_set_id_ = delete_backup_info.backup_set_id_;
1388
      backup_set_id.copy_id_ = delete_backup_info.copy_id_;
O
oceanbase-admin 已提交
1389 1390 1391 1392 1393
      if (!delete_backup_info.is_valid()) {
        // do nothing
        // The backup_set has been checked when the backup is initiated
        // Here, if keep_backup_info is invalid, it may be deleted and there will be a change in the future.
      } else {
1394 1395 1396
        if (OB_FAIL(get_backup_dest_option(delete_backup_info.backup_dest_, backup_dest_option))) {
          LOG_WARN("failed to get backup dest option", K(ret), K(delete_backup_info));
        } else if (OB_SYS_TENANT_ID != clean_info.tenant_id_) {
O
oceanbase-admin 已提交
1397
          backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;
1398 1399 1400 1401 1402 1403 1404
          clean_tenant.clog_gc_snapshot_ =
              has_kept_last_succeed_data ? clog_data_clean_point.start_replay_log_ts_ : INT64_MAX;
        } else if (!delete_backup_info.is_result_succeed()) {
          backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;
        } else if (!has_kept_last_succeed_data && delete_backup_info.backup_set_id_ >= cluster_max_backup_set_id &&
                   (delete_backup_info.backup_dest_ == backup_dest_ ||
                       delete_backup_info.backup_dest_ == backup_backup_dest_)) {
O
oceanbase-admin 已提交
1405
          ret = OB_BACKUP_DELETE_BACKUP_SET_NOT_ALLOWED;
W
WenJinyu 已提交
1406
          LOG_WARN("backup set do not allow clean, because of need to keep the last succeed backup set",
O
oceanbase-admin 已提交
1407 1408 1409 1410
              K(ret),
              K(clean_info),
              K(cluster_max_backup_set_id),
              K(delete_backup_info));
1411 1412 1413 1414 1415
        } else if (backup_dest_option.backup_copies_ > 1 &&
                   OB_FAIL(get_backup_set_file_copies_num(delete_backup_info, copies_num))) {
          LOG_WARN("failed to get backup set file copies num", K(ret), K(delete_backup_info));
        } else if (copies_num < backup_dest_option.backup_copies_) {
          ret = OB_BACKUP_DELETE_BACKUP_SET_NOT_ALLOWED;
W
WenJinyu 已提交
1416
          LOG_WARN("backup set do not allow clean, because current finish copies_num is less than backup_copies in backup_dest_option",
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
              K(ret),
              K(clean_info),
              K(cluster_max_backup_set_id),
              K(delete_backup_info),
              K(has_kept_last_succeed_data),
              K(clog_data_clean_point),
              "backup dest option copies num",
              backup_dest_option.backup_copies_,
              "already finish copies",
              copies_num);
O
oceanbase-admin 已提交
1427 1428
        } else if (has_kept_last_succeed_data) {
          backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;
1429 1430 1431 1432 1433 1434 1435 1436
          clean_tenant.clog_gc_snapshot_ = clog_data_clean_point.start_replay_log_ts_;
        } else if ((delete_backup_info.backup_dest_ != backup_dest_ &&
                       delete_backup_info.backup_dest_ != backup_backup_dest_) ||
                   clean_tenant.simple_clean_tenant_.is_deleted_) {
          // skip check with log continues
          // can delete log archive round without piece
          backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;
          clean_tenant.clog_gc_snapshot_ = INT64_MAX;
O
oceanbase-admin 已提交
1437 1438
        } else {
          ret = OB_BACKUP_DELETE_BACKUP_SET_NOT_ALLOWED;
W
WenJinyu 已提交
1439
          LOG_WARN("backup set do not allow clean, because of need to keep last succeed backup set",
O
oceanbase-admin 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
              K(ret),
              K(clean_info),
              K(cluster_max_backup_set_id),
              K(delete_backup_info),
              K(has_kept_last_succeed_data),
              K(clog_data_clean_point));
        }

        if (OB_FAIL(ret)) {
        } else if (OB_FAIL(clean_tenant.set_backup_clean_backup_set_id(delete_backup_info.cluster_id_,
                       delete_backup_info.incarnation_,
                       delete_backup_info.backup_dest_,
1452 1453
                       backup_set_id,
                       backup_dest_option))) {
O
oceanbase-admin 已提交
1454
          LOG_WARN("failed to set backup clean element", K(ret), K(delete_backup_info), K(clean_tenant));
1455 1456 1457 1458 1459 1460 1461
        } else if (delete_backup_info.backup_dest_ != backup_dest_ &&
                   delete_backup_info.backup_dest_ != backup_backup_dest_) {
          // no need delete log info, log info should delete by delete backup piece or round
        } else {
          ObBackupDest backup_dest;
          for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_infos.count(); ++i) {
            backup_dest.reset();
M
mw0 已提交
1462
            const ObLogArchiveBackupInfo &log_archive_info = log_archive_infos.at(i);
1463 1464 1465 1466 1467 1468 1469 1470
            if (OB_FAIL(backup_dest.set(log_archive_info.backup_dest_))) {
              LOG_WARN("failed to set backup dest", K(ret), K(log_archive_info));
            } else if (backup_dest != delete_backup_info.backup_dest_) {
              // do nothing
            } else if (OB_FAIL(add_log_archive_info(log_archive_info, clean_tenant))) {
              LOG_WARN("failed to add log archive info", K(ret), K(log_archive_info));
            }
          }
O
oceanbase-admin 已提交
1471 1472 1473 1474 1475 1476 1477
        }
      }
    }
  }
  return ret;
}

M
mw0 已提交
1478 1479 1480 1481
int ObBackupDataClean::add_obsolete_backup_sets(const share::ObBackupCleanInfo &clean_info,
    const common::ObIArray<share::ObTenantBackupTaskInfo> &task_infos,
    const common::ObArray<ObLogArchiveBackupInfo> &log_archive_infos, ObBackupDataCleanTenant &clean_tenant,
    ObTenantBackupTaskInfo &clog_data_clean_point)
O
oceanbase-admin 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490
{
  int ret = OB_SUCCESS;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("add delete backup set get invalid argument", K(ret), K(clean_info));
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
  } else if (OB_SYS_TENANT_ID == clean_info.tenant_id_) {
    if (OB_FAIL(add_sys_tenant_obsolete_backup_sets(
            clean_info, task_infos, log_archive_infos, clean_tenant, clog_data_clean_point))) {
      LOG_WARN("failed to add sys tenant obsolete backup sets", K(ret), K(clean_info));
    }
  } else {
    if (OB_FAIL(add_normal_tenant_obsolete_backup_sets(
            clean_info, task_infos, log_archive_infos, clean_tenant, clog_data_clean_point))) {
      LOG_WARN("failed to add normal tenant obsolete backup sets", K(ret), K(clean_info));
    }
  }
  return ret;
}

M
mw0 已提交
1505 1506 1507 1508
int ObBackupDataClean::add_normal_tenant_obsolete_backup_sets(const share::ObBackupCleanInfo &clean_info,
    const common::ObIArray<share::ObTenantBackupTaskInfo> &task_infos,
    const common::ObArray<share::ObLogArchiveBackupInfo> &log_archive_infos, ObBackupDataCleanTenant &clean_tenant,
    share::ObTenantBackupTaskInfo &clog_data_clean_point)
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
{
  int ret = OB_SUCCESS;
  bool is_continue = false;
  bool need_add_backup_set = false;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_SYS_TENANT_ID == clean_info.tenant_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("add normal tenant obsolete backup set get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(add_log_archive_infos(log_archive_infos, clean_tenant))) {
    LOG_WARN("failed to add log archive infos", K(ret), K(clean_info), K(clean_tenant));
O
oceanbase-admin 已提交
1521 1522 1523
  } else {
    for (int64_t i = task_infos.count() - 1; OB_SUCC(ret) && i >= 0; --i) {
      is_continue = false;
M
mw0 已提交
1524
      const ObTenantBackupTaskInfo &task_info = task_infos.at(i);
O
oceanbase-admin 已提交
1525 1526
      ObBackupSetId backup_set_id;
      backup_set_id.backup_set_id_ = task_info.backup_set_id_;
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
      backup_set_id.copy_id_ = task_info.copy_id_;
      backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;
      ObBackupDestOpt backup_dest_option;
      bool can_delete = false;
      need_add_backup_set = true;

      if (OB_FAIL(check_backup_set_id_can_be_deleted(clean_info.tenant_id_, backup_set_id, can_delete))) {
        LOG_WARN("failed to check backup set id can be deleted", K(ret), K(clean_info), K(backup_set_id));
      } else if (can_delete) {
        backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;
      } else if (ObTenantBackupTaskInfo::FINISH == task_info.status_ && !task_info.is_result_succeed()) {
        need_add_backup_set = false;
O
oceanbase-admin 已提交
1539
      } else {
1540 1541
        backup_set_id.clean_mode_ = ObBackupDataCleanMode::TOUCH;
        if (!task_info.is_result_succeed() || !task_info.backup_type_.is_full_backup()) {
O
oceanbase-admin 已提交
1542
          // do nothing
1543 1544 1545 1546 1547
        } else if (OB_FAIL(check_backupset_continue_with_clog_data(task_info, log_archive_infos, is_continue))) {
          LOG_WARN("failed to check backup set continue with clog data", K(ret), K(task_info));
        } else if (!is_continue) {
        } else {
          clog_data_clean_point = task_info;
O
oceanbase-admin 已提交
1548 1549 1550
        }
      }

1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
      if (OB_SUCC(ret) && need_add_backup_set) {
        if (OB_FAIL(get_backup_dest_option(task_info.backup_dest_, backup_dest_option))) {
          LOG_WARN("failed to get backup dest option", K(ret), K(task_info));
        } else if (OB_FAIL(clean_tenant.set_backup_clean_backup_set_id(task_info.cluster_id_,
                       task_info.incarnation_,
                       task_info.backup_dest_,
                       backup_set_id,
                       backup_dest_option))) {
          LOG_WARN("failed to set backup clean element", K(ret), K(task_info), K(clean_tenant));
        } else if (OB_FAIL(add_deleting_backup_set_id_into_set(clean_info.tenant_id_, backup_set_id))) {
          LOG_WARN("failed to add deleting backup set id into set", K(ret), K(backup_set_id), K(clean_info));
        }
O
oceanbase-admin 已提交
1563 1564 1565 1566 1567 1568
      }
    }
  }
  return ret;
}

M
mw0 已提交
1569 1570 1571 1572
int ObBackupDataClean::add_sys_tenant_obsolete_backup_sets(const share::ObBackupCleanInfo &clean_info,
    const common::ObIArray<share::ObTenantBackupTaskInfo> &task_infos,
    const common::ObArray<share::ObLogArchiveBackupInfo> &log_archive_infos, ObBackupDataCleanTenant &clean_tenant,
    share::ObTenantBackupTaskInfo &clog_data_clean_point)
O
oceanbase-admin 已提交
1573 1574
{
  int ret = OB_SUCCESS;
1575 1576 1577 1578 1579 1580 1581 1582
  bool has_kept_last_succeed_data = false;
  int64_t cluster_max_backup_set_id = 0;
  bool is_continue = false;
  const int64_t copy_id = clean_info.copy_id_;
  ObArray<ObBackupSetId> reverse_backup_set_ids;
  ObArray<share::ObTenantBackupTaskInfo> reverse_task_infos;
  ObBackupSetId backup_set_id;

O
oceanbase-admin 已提交
1583 1584 1585
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
1586
  } else if (OB_SYS_TENANT_ID != clean_info.tenant_id_) {
O
oceanbase-admin 已提交
1587
    ret = OB_INVALID_ARGUMENT;
1588 1589 1590 1591 1592 1593 1594 1595
    LOG_WARN("add delete backup set get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(add_log_archive_infos(log_archive_infos, clean_tenant))) {
    LOG_WARN("failed to add log archive infos", K(ret), K(clean_info), K(clean_tenant));
  } else if (OB_FAIL(get_cluster_max_succeed_backup_set(copy_id, cluster_max_backup_set_id))) {
    LOG_WARN("failed to get cluster max succeed backup set", K(ret), K(clean_info));
  } else {
    for (int64_t i = task_infos.count() - 1; OB_SUCC(ret) && i >= 0; --i) {
      is_continue = false;
M
mw0 已提交
1596
      const ObTenantBackupTaskInfo &task_info = task_infos.at(i);
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
      bool need_add_backup_set = true;
      if (task_info.snapshot_version_ <= clean_info.expired_time_) {
        if (OB_FAIL(deal_with_obsolete_backup_set(clean_info,
                task_info,
                log_archive_infos,
                cluster_max_backup_set_id,
                backup_set_id,
                has_kept_last_succeed_data,
                clog_data_clean_point))) {
          LOG_WARN("failed to deal with obsolete backup set", K(ret), K(task_info));
        }
      } else {
        // backup set is not obsolete
        if (ObTenantBackupTaskInfo::FINISH == task_info.status_ && !task_info.is_result_succeed()) {
          need_add_backup_set = false;
        } else if (OB_FAIL(deal_with_effective_backup_set(clean_info,
                       task_info,
                       log_archive_infos,
                       backup_set_id,
                       has_kept_last_succeed_data,
                       clog_data_clean_point))) {
          LOG_WARN("failed to deal with effective backup set", K(ret), K(task_info), K(clean_info));
        }
      }

      if (OB_FAIL(ret)) {
      } else if (need_add_backup_set) {
        if (OB_FAIL(reverse_task_infos.push_back(task_info))) {
          LOG_WARN("failed to push task info into array", K(ret), K(task_info));
        } else if (OB_FAIL(reverse_backup_set_ids.push_back(backup_set_id))) {
          LOG_WARN("failed to push backup set id into array", K(ret), K(backup_set_id));
        }
      }
    }

    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(add_obsolete_backup_set_with_order(
                   clean_info, reverse_task_infos, reverse_backup_set_ids, clean_tenant, clog_data_clean_point))) {
      LOG_WARN("failed to add obsolete backup set with order", K(ret), K(clean_info));
    }
  }
  return ret;
}

M
mw0 已提交
1641 1642 1643 1644
int ObBackupDataClean::deal_with_obsolete_backup_set(const share::ObBackupCleanInfo &clean_info,
    const ObTenantBackupTaskInfo &task_info, const common::ObArray<share::ObLogArchiveBackupInfo> &log_archive_infos,
    const int64_t cluster_max_backup_set_id, ObBackupSetId &backup_set_id, bool &has_kept_last_succeed_data,
    share::ObTenantBackupTaskInfo &clog_data_clean_point)
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
{
  int ret = OB_SUCCESS;
  backup_set_id.reset();
  // has_kept_last_succeed_data and clog_data_clean_point is input parameter and output parameter

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!task_info.is_valid() || task_info.snapshot_version_ > clean_info.expired_time_ ||
             cluster_max_backup_set_id < 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("deal with obsolete backup set get invalid argument",
        K(ret),
        K(task_info),
        K(clean_info),
        K(cluster_max_backup_set_id));
  } else {
    bool is_continue = false;
    backup_set_id.backup_set_id_ = task_info.backup_set_id_;
    backup_set_id.copy_id_ = task_info.copy_id_;
    backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;
    bool need_add_backup_set = true;
    ObBackupDestOpt backup_dest_option;
    int64_t copies_num = 0;
    bool can_delete = false;

    if (OB_FAIL(get_backup_dest_option(task_info.backup_dest_, backup_dest_option))) {
      LOG_WARN("failed to get backup dest option", K(ret), K(task_info));
    } else if (ObTenantBackupTaskInfo::FINISH != task_info.status_) {
      backup_set_id.clean_mode_ = ObBackupDataCleanMode::TOUCH;
    } else if (!task_info.is_result_succeed() || task_info.is_mark_deleted_) {
      backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;
    } else if (!has_kept_last_succeed_data && task_info.backup_set_id_ >= cluster_max_backup_set_id) {
      backup_set_id.clean_mode_ = ObBackupDataCleanMode::TOUCH;
      if (!task_info.backup_type_.is_full_backup()) {
        // do nothing
      } else if (OB_FAIL(check_backupset_continue_with_clog_data(task_info, log_archive_infos, is_continue))) {
        LOG_WARN("failed to check backupset continue with clog data", K(ret), K(task_info));
      } else if (!is_continue) {
        LOG_INFO("backup set is not continue with clog data", K(task_info), K(log_archive_infos));
      } else {
        has_kept_last_succeed_data = true;
        clog_data_clean_point = task_info;
      }
    } else if (backup_dest_option.backup_copies_ > 1 &&
               OB_FAIL(get_backup_set_file_copies_num(task_info, copies_num))) {
      LOG_WARN("failed to get backup set file copies num", K(ret), K(task_info));
    } else if (copies_num < backup_dest_option.backup_copies_) {
      backup_set_id.clean_mode_ = ObBackupDataCleanMode::TOUCH;
      LOG_INFO("backup copies is not reach required copies",
          K(task_info),
          "need copies",
          backup_dest_option.backup_copies_,
          "now copies",
          copies_num);
      if (!has_kept_last_succeed_data && task_info.backup_type_.is_full_backup()) {
        if (OB_FAIL(check_backupset_continue_with_clog_data(task_info, log_archive_infos, is_continue))) {
          LOG_WARN("failed to check backupset continue with clog data", K(ret), K(task_info));
        } else if (!is_continue) {
          LOG_INFO("backup set is not continue with clog data", K(task_info), K(log_archive_infos));
        } else {
          has_kept_last_succeed_data = true;
          clog_data_clean_point = task_info;
        }
      }
    } else if (has_kept_last_succeed_data) {
      backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;
    } else {
      backup_set_id.clean_mode_ = ObBackupDataCleanMode::TOUCH;
      if (!task_info.backup_type_.is_full_backup()) {
        // do nothing
        has_kept_last_succeed_data = false;
        clog_data_clean_point.reset();
      } else if (OB_FAIL(check_backupset_continue_with_clog_data(task_info, log_archive_infos, is_continue))) {
        LOG_WARN("failed to check backupset continue with clog data", K(ret), K(task_info));
      } else if (!is_continue) {
        LOG_INFO("backup set is not continue with clog data", K(task_info), K(log_archive_infos));
      } else {
        has_kept_last_succeed_data = true;
        clog_data_clean_point = task_info;
      }
    }
  }
  return ret;
}

M
mw0 已提交
1731 1732 1733 1734
int ObBackupDataClean::deal_with_effective_backup_set(const share::ObBackupCleanInfo &clean_info,
    const ObTenantBackupTaskInfo &task_info, const common::ObArray<share::ObLogArchiveBackupInfo> &log_archive_infos,
    ObBackupSetId &backup_set_id, bool &has_kept_last_succeed_data,
    share::ObTenantBackupTaskInfo &clog_data_clean_point)
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
{
  int ret = OB_SUCCESS;
  backup_set_id.reset();
  bool is_continue = false;
  // has_kept_last_succeed_data and clog_data_clean_point is input parameter and output parameter

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!task_info.is_valid() ||
             (ObTenantBackupTaskInfo::FINISH == task_info.status_ && !task_info.is_result_succeed()) ||
             task_info.snapshot_version_ <= clean_info.expired_time_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("deal with effective backup set get invalid argument", K(ret), K(task_info), K(clean_info));
  } else {
    backup_set_id.backup_set_id_ = task_info.backup_set_id_;
    backup_set_id.copy_id_ = task_info.copy_id_;
    backup_set_id.clean_mode_ = ObBackupDataCleanMode::CLEAN;

    if (ObTenantBackupTaskInfo::FINISH != task_info.status_) {
      backup_set_id.clean_mode_ = ObBackupDataCleanMode::TOUCH;
    } else {
      backup_set_id.clean_mode_ = ObBackupDataCleanMode::TOUCH;
      if (!task_info.backup_type_.is_full_backup()) {
        // do nothing
        has_kept_last_succeed_data = false;
        clog_data_clean_point.reset();
      } else if (OB_FAIL(check_backupset_continue_with_clog_data(task_info, log_archive_infos, is_continue))) {
        LOG_WARN("failed to check backupset continue with clog data", K(ret), K(task_info));
      } else if (!is_continue) {
      } else {
        // last obsolete backup need relay on clog, so cannot set has_kept_last_succeed_data
        clog_data_clean_point = task_info;
      }
    }
  }
  return ret;
}

M
mw0 已提交
1774 1775 1776 1777
int ObBackupDataClean::add_obsolete_backup_set_with_order(const share::ObBackupCleanInfo &clean_info,
    const common::ObIArray<share::ObTenantBackupTaskInfo> &reverse_task_infos,
    const common::ObIArray<ObBackupSetId> &reverse_backup_set_ids, ObBackupDataCleanTenant &clean_tenant,
    share::ObTenantBackupTaskInfo &clog_data_clean_point)
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (reverse_task_infos.count() != reverse_backup_set_ids.count()) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("reverse backup task info count not equal to reverse backup set id count",
        K(ret),
        K(reverse_task_infos),
        K(reverse_backup_set_ids));
  } else {
    bool is_deleted_set_contine = true;
    bool is_set_clog_clean_point = false;
    ObBackupSetId new_backup_set_id;
    ObBackupDestOpt backup_dest_option;
    for (int64_t i = reverse_task_infos.count() - 1; OB_SUCC(ret) && i >= 0; --i) {
      backup_dest_option.reset();
M
mw0 已提交
1796 1797
      const ObTenantBackupTaskInfo &task_info = reverse_task_infos.at(i);
      const ObBackupSetId &backup_set_id = reverse_backup_set_ids.at(i);
1798 1799
      new_backup_set_id = backup_set_id;
      if (!is_deleted_set_contine) {
M
mw0 已提交
1800 1801 1802 1803 1804 1805 1806
        if (clean_info.is_delete_obsolete_backup_backup()) {
          new_backup_set_id.clean_mode_ = ObBackupDataCleanMode::TOUCH;
          if (task_info.is_result_succeed() && !is_set_clog_clean_point) {
            clog_data_clean_point = task_info;
            is_set_clog_clean_point = true;
          }
        } else if (task_info.is_result_succeed()) {
1807 1808 1809 1810 1811
          new_backup_set_id.clean_mode_ = ObBackupDataCleanMode::TOUCH;
          if (!is_set_clog_clean_point) {
            clog_data_clean_point = task_info;
            is_set_clog_clean_point = true;
          }
M
mw0 已提交
1812 1813
        } else {
          // do nothing
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
        }
      } else if (ObBackupDataCleanMode::CLEAN == backup_set_id.clean_mode_) {
        // do nothing
      } else {
        is_deleted_set_contine = false;
      }

      if (OB_FAIL(get_backup_dest_option(task_info.backup_dest_, backup_dest_option))) {
        LOG_WARN("failed to get backup dest option", K(ret), K(task_info));
      } else if (OB_FAIL(clean_tenant.set_backup_clean_backup_set_id(task_info.cluster_id_,
                     task_info.incarnation_,
                     task_info.backup_dest_,
                     new_backup_set_id,
                     backup_dest_option))) {
        LOG_WARN("failed to set backup clean element", K(ret), K(task_info), K(clean_tenant));
      } else if (OB_FAIL(add_deleting_backup_set_id_into_set(clean_info.tenant_id_, new_backup_set_id))) {
        LOG_WARN("failed to add deleting backup set id into set", K(ret), K(backup_set_id), K(clean_info));
      }
    }
  }
  return ret;
}

M
mw0 已提交
1837 1838 1839
int ObBackupDataClean::add_delete_backup_piece(const share::ObBackupCleanInfo &clean_info,
    const common::ObIArray<share::ObTenantBackupTaskInfo> &task_infos,
    const common::ObArray<share::ObLogArchiveBackupInfo> &log_archive_infos, ObBackupDataCleanTenant &clean_tenant)
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
  const bool for_update = false;
  const int64_t tenant_id = clean_info.tenant_id_;
  const int64_t backup_piece_id = clean_info.backup_piece_id_;
  const int64_t copy_id = clean_info.copy_id_;
  ObBackupPieceInfo backup_piece_info;
  ObBackupDest backup_dest;
  bool found_log_archive_info = false;
  bool found_backup_set = false;
  ObBackupDestOpt backup_dest_option;
  int64_t copies_num = 0;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid() || !clean_info.is_delete_backup_piece()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("add delete backup piece get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(log_archive_info_mgr.get_backup_piece(
                 *sql_proxy_, for_update, tenant_id, backup_piece_id, copy_id, backup_piece_info))) {
    if (OB_ITER_END == ret) {
      ret = OB_SUCCESS;
    } else {
      LOG_WARN("failed to get backup piece info", K(ret), K(clean_info));
    }
  } else if (!backup_piece_info.is_valid() || !ObBackupUtils::can_backup_pieces_be_deleted(backup_piece_info.status_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("backup piece info is unexpcted", K(ret), K(backup_piece_info));
  } else if (OB_FAIL(backup_dest.set(backup_piece_info.backup_dest_.ptr()))) {
    LOG_WARN("failed to set backup dest", K(ret), K(backup_piece_info));
  } else {
    clean_tenant.clog_gc_snapshot_ = backup_piece_info.max_ts_;
    // 1.find log archive info
    for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_infos.count() && !found_log_archive_info; ++i) {
M
mw0 已提交
1876
      const ObLogArchiveBackupInfo &tmp_log_archive_info = log_archive_infos.at(i);
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
      ObBackupDest tmp_backup_dest;
      if (OB_FAIL(tmp_backup_dest.set(tmp_log_archive_info.backup_dest_))) {
        LOG_WARN("failed to set tmp backup dest", K(ret), K(tmp_log_archive_info));
      } else if (tmp_backup_dest == backup_dest &&
                 tmp_log_archive_info.status_.round_ == backup_piece_info.key_.round_id_ &&
                 tmp_log_archive_info.status_.copy_id_ == backup_piece_info.key_.copy_id_) {
        if (OB_FAIL(add_log_archive_info(tmp_log_archive_info, clean_tenant))) {
          LOG_WARN("failed to add log archive info", K(ret), K(tmp_log_archive_info));
        } else {
          found_log_archive_info = true;
        }
      }
    }

    if (OB_SUCC(ret)) {
      // 2. find succeed backup set witch start_replay_log_ts bigger than backup_piece_info's max_ts
      if (!found_log_archive_info) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("delete backup piece failed to find log archive info", K(ret), K(backup_piece_info), K(clean_info));
      } else if (backup_dest != backup_dest_ && backup_dest != backup_backup_dest_) {
        LOG_INFO("backup piece dest is not equal to config backup dest or backup backupset dest, "
                 "delete it directly",
            K(backup_dest),
            K(backup_dest_),
            K(backup_backup_dest_));
      } else if (OB_FAIL(get_backup_dest_option(backup_dest, backup_dest_option))) {
        LOG_WARN("failed to get backup dest option", K(ret), K(backup_dest));
      } else if (backup_dest_option.backup_copies_ > 1 &&
                 OB_FAIL(get_backup_piece_file_copies_num(backup_piece_info, copies_num))) {
        LOG_WARN("failed to get backup piece file copies num", K(ret), K(backup_piece_info));
      } else if (copies_num < backup_dest_option.backup_copies_) {
W
WenJinyu 已提交
1908 1909 1910 1911 1912 1913
        ret = OB_BACKUP_DELETE_BACKUP_PIECE_NOT_ALLOWED;
        LOG_WARN("piece is not allowed to be deleted, because current finish copies_num is less than backup_copies in backup_dest_option",
            K(ret),
            K(copies_num),
            K_(backup_dest_option.backup_copies),
            K(backup_piece_info));
1914 1915 1916
      } else {
        int64_t start_replay_log_ts = 0;
        for (int64_t i = 0; i < task_infos.count() && !found_backup_set; ++i) {
M
mw0 已提交
1917
          const ObTenantBackupTaskInfo &task_info = task_infos.at(i);
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
          if (task_info.is_result_succeed() && task_info.backup_dest_ == backup_dest &&
              task_info.backup_type_.is_full_backup()) {
            start_replay_log_ts = task_info.start_replay_log_ts_;
            found_backup_set = true;
          }
        }

        if (found_backup_set) {
          if (start_replay_log_ts >= backup_piece_info.max_ts_) {
            LOG_INFO("found suitable backup set, can delete backup piece", K(backup_piece_info));
          } else {
W
WenJinyu 已提交
1929 1930 1931 1932 1933 1934
            ret = OB_BACKUP_DELETE_BACKUP_PIECE_NOT_ALLOWED;
            LOG_WARN("piece is not allowed to be deleted, because it is found that the dependent start_replay_log_ts "
                "of backup set is less than the max_ts of piece",
                K(ret),
                K(backup_piece_info),
                K(start_replay_log_ts));
1935 1936 1937 1938 1939
          }
        } else {
          if (clean_tenant.simple_clean_tenant_.is_deleted_) {
            LOG_INFO("tenant is deleted, can delete backup piece directly", K(backup_piece_info));
          } else {
W
WenJinyu 已提交
1940 1941
            ret = OB_BACKUP_DELETE_BACKUP_PIECE_NOT_ALLOWED;
            LOG_WARN("piece is not allowed to be deleted, because no valid backup_set is found", K(ret), K(backup_piece_info));
1942 1943 1944 1945 1946 1947 1948 1949
          }
        }
      }
    }
  }
  return ret;
}

M
mw0 已提交
1950 1951 1952
int ObBackupDataClean::add_delete_backup_round(const share::ObBackupCleanInfo &clean_info,
    const common::ObIArray<share::ObTenantBackupTaskInfo> &task_infos,
    const common::ObArray<share::ObLogArchiveBackupInfo> &log_archive_infos, ObBackupDataCleanTenant &clean_tenant)
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
{
  int ret = OB_SUCCESS;
  const int64_t backup_round_id = clean_info.backup_round_id_;
  const int64_t copy_id = clean_info.copy_id_;
  ObBackupDest backup_dest;
  ObLogArchiveBackupInfo log_archive_info;
  bool found_log_archive_info = false;
  bool found_backup_set = false;
  ObBackupDestOpt backup_dest_option;
  int64_t copies_num = 0;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid() || !clean_info.is_delete_backup_round()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("add delete backup piece get invalid argument", K(ret), K(clean_info));
  } else {
    // 1.find log archive info
    for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_infos.count() && !found_log_archive_info; ++i) {
M
mw0 已提交
1973
      const ObLogArchiveBackupInfo &tmp_log_archive_info = log_archive_infos.at(i);
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
      ObBackupDest tmp_backup_dest;
      if (tmp_log_archive_info.status_.round_ == backup_round_id && tmp_log_archive_info.status_.copy_id_ == copy_id) {
        if (OB_FAIL(add_log_archive_info(tmp_log_archive_info, clean_tenant))) {
          LOG_WARN("failed to add log archive info", K(ret), K(tmp_log_archive_info));
        } else if (OB_FAIL(backup_dest.set(tmp_log_archive_info.backup_dest_))) {
          LOG_WARN("failed to set backup dest", K(ret), K(tmp_log_archive_info));
        } else {
          log_archive_info = tmp_log_archive_info;
          clean_tenant.clog_gc_snapshot_ = log_archive_info.status_.checkpoint_ts_;
          found_log_archive_info = true;
        }
      }
    }

    if (OB_SUCC(ret)) {
      // 2. find succeed backup set witch start_replay_log_ts bigger than log_archive_info's check_point_ts
      if (!found_log_archive_info) {
        // do nothing
      } else if (backup_dest != backup_dest_ && backup_dest != backup_backup_dest_) {
        LOG_INFO("backup round dest is not equal to config backup dest or backup backupset dest, "
                 "delete it directly",
            K(backup_dest),
            K(backup_dest_),
            K(backup_backup_dest_));
      } else if (OB_FAIL(get_backup_dest_option(backup_dest, backup_dest_option))) {
        LOG_WARN("failed to get backup dest option", K(ret), K(backup_dest));
      } else if (backup_dest_option.backup_copies_ > 1 &&
                 OB_FAIL(get_backup_round_copies_num(log_archive_info, copies_num))) {
        LOG_WARN("failed to get backup round copies num", K(ret), K(log_archive_info));
      } else if (copies_num < backup_dest_option.backup_copies_) {
W
WenJinyu 已提交
2004 2005 2006 2007 2008 2009
        ret = OB_BACKUP_DELETE_BACKUP_PIECE_NOT_ALLOWED;
        LOG_WARN("round is not allowed to be deleted, because current finish copies_num is less than backup_copies in backup_dest_option",
            K(ret),
            K(copies_num),
            K_(backup_dest_option.backup_copies),
            K(log_archive_info));
2010 2011 2012
      } else {
        int64_t start_replay_log_ts = 0;
        for (int64_t i = 0; i < task_infos.count() && !found_backup_set; ++i) {
M
mw0 已提交
2013
          const ObTenantBackupTaskInfo &task_info = task_infos.at(i);
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
          if (task_info.is_result_succeed() && task_info.backup_dest_ == backup_dest &&
              task_info.backup_type_.is_full_backup()) {
            start_replay_log_ts = task_info.start_replay_log_ts_;
            found_backup_set = true;
          }
        }

        if (found_backup_set) {
          if (start_replay_log_ts >= log_archive_info.status_.checkpoint_ts_) {
            LOG_INFO("found suitable backup set, can delete backup round", K(log_archive_info));
          } else {
W
WenJinyu 已提交
2025 2026 2027 2028 2029 2030
            ret = OB_BACKUP_DELETE_BACKUP_PIECE_NOT_ALLOWED;
            LOG_WARN("round is not allowed to be deleted, because it is found that the dependent start_replay_log_ts "
                "of backup set is less than the max_ts of round",
                K(ret),
                K(log_archive_info),
                K(start_replay_log_ts));
2031 2032 2033 2034 2035
          }
        } else {
          if (clean_tenant.simple_clean_tenant_.is_deleted_) {
            LOG_INFO("tenant is deleted, can delete backup round directly", K(log_archive_info));
          } else {
W
WenJinyu 已提交
2036 2037
            ret = OB_BACKUP_DELETE_BACKUP_PIECE_NOT_ALLOWED;
            LOG_WARN("round is not allowed to be deleted, because no valid backup_set is found", K(ret), K(log_archive_info));
2038 2039 2040 2041 2042 2043 2044 2045 2046
          }
        }
      }
    }
  }
  return ret;
}

int ObBackupDataClean::do_tenant_backup_clean(
M
mw0 已提交
2047
    const share::ObBackupCleanInfo &clean_info, ObBackupDataCleanTenant &clean_tenant)
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
{
  LOG_INFO("start do tenant backup clean", K(clean_info));
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_tenant.is_valid() || !clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get backup clean elements get invalid argument", K(ret), K(clean_tenant), K(clean_info));
  }

  DEBUG_SYNC(BACKUP_DATA_CLEAN_STATUS_DOING);
  if (OB_FAIL(ret)) {
  } else if (OB_SYS_TENANT_ID == clean_info.tenant_id_) {
O
oceanbase-admin 已提交
2062 2063 2064 2065 2066 2067 2068 2069 2070
    // do nothing
  } else if (OB_FAIL(do_normal_tenant_backup_clean(clean_info, clean_tenant))) {
    LOG_WARN("failed to do sys tenant backup clean", K(ret), K(clean_info), K(clean_tenant));
  }

  return ret;
}

int ObBackupDataClean::do_normal_tenant_backup_clean(
M
mw0 已提交
2071
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
{
  LOG_INFO("do normal tenant backup clean", K(clean_info), K(clean_tenant));
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtil::current_time();
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_tenant.is_valid() || !clean_info.is_valid() ||
             OB_SYS_TENANT_ID == clean_tenant.simple_clean_tenant_.tenant_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get backup clean elements get invalid argument", K(ret), K(clean_tenant), K(clean_info));
2084
  } else if (OB_FAIL(mark_backup_meta_data_deleting(clean_info, clean_tenant))) {
O
oceanbase-admin 已提交
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
    LOG_WARN("failed to mark backup data deleted", K(ret), K(clean_tenant), K(clean_info));
  } else if (OB_FAIL(delete_backup_data(clean_info, clean_tenant))) {
    LOG_WARN("failed to delete backup data", K(ret), K(clean_info), K(clean_tenant));
  } else if (OB_FAIL(delete_tenant_backup_meta_data(clean_info, clean_tenant))) {
    LOG_WARN("failed to delete backup meta data", K(ret), K(clean_tenant));
  } else if (OB_FAIL(try_clean_tenant_backup_dir(clean_tenant))) {
    LOG_WARN("failed to try clean tenant backup dir", K(ret), K(clean_info));
  }

  if (is_result_need_retry(ret)) {
    // do nothing
  } else if (OB_SUCCESS != (tmp_ret = update_normal_tenant_clean_result(clean_info, clean_tenant, ret))) {
    LOG_WARN("failed to update nomal tenant clean result", K(ret), K(clean_info), K(clean_tenant));
  }

  if (OB_SUCC(ret)) {
    ret = tmp_ret;
  }

  if (OB_SUCC(ret)) {
    wakeup();
  }

  LOG_INFO("finish do normal tenant backup clean",
      "cost_ts",
      ObTimeUtil::current_time() - start_ts,
      K(clean_tenant),
      K(ret));

  return ret;
}

int ObBackupDataClean::update_normal_tenant_clean_result(
M
mw0 已提交
2118
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant, const int32_t clean_result)
O
oceanbase-admin 已提交
2119 2120 2121 2122
{
  int ret = OB_SUCCESS;
  ObMySQLTransaction trans;
  ObBackupCleanInfo dest_clean_info;
2123
  ObTimeoutCtx timeout_ctx;
O
oceanbase-admin 已提交
2124 2125 2126 2127 2128 2129 2130
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_tenant.is_valid() || !clean_info.is_valid() ||
             OB_SYS_TENANT_ID == clean_tenant.simple_clean_tenant_.tenant_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get backup clean elements get invalid argument", K(ret), K(clean_tenant), K(clean_info));
2131 2132
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
O
oceanbase-admin 已提交
2133 2134 2135 2136 2137 2138
  } else {
    dest_clean_info = clean_info;
    dest_clean_info.result_ = clean_result;
    dest_clean_info.status_ = ObBackupCleanInfoStatus::STOP;
    dest_clean_info.end_time_ = ObTimeUtil::current_time();
    const uint64_t tenant_id = clean_tenant.simple_clean_tenant_.is_deleted_ ? OB_SYS_TENANT_ID : clean_info.tenant_id_;
2139 2140 2141 2142
    if (OB_FAIL(set_comment(dest_clean_info.comment_))) {
      LOG_WARN("failed to set comment", K(ret), K(dest_clean_info));
    } else if (OB_FAIL(set_error_msg(clean_result, dest_clean_info.error_msg_))) {
      LOG_WARN("failed to set error msg", K(ret), K(dest_clean_info));
O
oceanbase-admin 已提交
2143
    }
2144

O
oceanbase-admin 已提交
2145 2146 2147
    DEBUG_SYNC(BACKUP_DATA_NORMAL_TENANT_CLEAN_STATUS_DOING);

    if (OB_FAIL(ret)) {
2148
    } else if (OB_SUCCESS == clean_result && OB_FAIL(delete_inner_table_his_data(clean_info, clean_tenant, trans))) {
O
oceanbase-admin 已提交
2149 2150 2151 2152 2153 2154
      LOG_WARN("failed to delete inner table his data", K(ret), K(clean_info));
    } else if (OB_FAIL(update_clean_info(tenant_id, clean_info, dest_clean_info))) {
      LOG_WARN("failed to update clean info", K(ret), K(clean_info), K(dest_clean_info));
    }

    if (OB_SUCC(ret)) {
2155 2156
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
O
oceanbase-admin 已提交
2157 2158
      }
    } else {
2159 2160 2161 2162
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
O
oceanbase-admin 已提交
2163 2164 2165 2166 2167 2168
      }
    }
  }
  return ret;
}

2169
int ObBackupDataClean::mark_backup_meta_data_deleting(
M
mw0 已提交
2170
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
2171 2172 2173 2174 2175
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2176 2177 2178 2179
  } else if (OB_FAIL(mark_backup_set_infos_deleting(clean_info, clean_tenant))) {
    LOG_WARN("failed to mark backup set infos deleted", K(ret), K(clean_info));
  } else if (OB_FAIL(mark_log_archive_infos_deleting(clean_info, clean_tenant))) {
    LOG_WARN("failed to mark log archive inifos deleted", K(ret), K(clean_info));
O
oceanbase-admin 已提交
2180 2181 2182 2183 2184
  }
  return ret;
}

int ObBackupDataClean::mark_extern_backup_info_deleted(
M
mw0 已提交
2185
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanElement &clean_element)
O
oceanbase-admin 已提交
2186 2187 2188
{
  int ret = OB_SUCCESS;
  ObExternBackupInfoMgr extern_backup_info_mgr;
2189 2190 2191 2192 2193
  ObExternBackupSetFileInfoMgr extern_backup_set_file_info_mgr;
  ObClusterBackupDest cluster_backup_dest;
  ObArray<ObBackupSetIdPair> backup_set_id_pairs;
  const bool is_backup_backup = clean_info.copy_id_ > 0 || clean_info.is_delete_obsolete_backup_backup();
  ObBackupSetIdPair backup_set_id_pair;
O
oceanbase-admin 已提交
2194 2195 2196 2197

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2198
  } else if (OB_FAIL(cluster_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
O
oceanbase-admin 已提交
2199
    LOG_WARN("failed to set backup dest", K(ret), K(clean_element));
2200 2201 2202 2203 2204
  } else if (OB_FAIL(extern_backup_info_mgr.init(clean_info.tenant_id_, cluster_backup_dest, *backup_lease_service_))) {
    LOG_WARN("failed to init extern backup info mgr", K(ret), K(cluster_backup_dest), K(clean_info));
  } else if (OB_FAIL(extern_backup_set_file_info_mgr.init(
                 clean_info.tenant_id_, cluster_backup_dest, is_backup_backup, *backup_lease_service_))) {
    LOG_WARN("failed to init extern backup set file info mgr", K(ret), K(clean_info), K(cluster_backup_dest));
O
oceanbase-admin 已提交
2205 2206
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_element.backup_set_id_array_.count(); ++i) {
M
mw0 已提交
2207
      const ObBackupSetId &backup_set_id = clean_element.backup_set_id_array_.at(i);
2208
      backup_set_id_pair.reset();
O
oceanbase-admin 已提交
2209 2210 2211 2212 2213 2214
      if (!backup_set_id.is_valid()) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("backup set id is invalid ", K(ret), K(backup_set_id));
      } else if (ObBackupDataCleanMode::TOUCH == backup_set_id.clean_mode_) {
        // do nothing
      } else {
2215 2216 2217 2218
        backup_set_id_pair.backup_set_id_ = backup_set_id.backup_set_id_;
        backup_set_id_pair.copy_id_ = backup_set_id.copy_id_;
        if (OB_FAIL(backup_set_id_pairs.push_back(backup_set_id_pair))) {
          LOG_WARN("failed to push backup set id into array", K(ret), K(backup_set_id_pair));
O
oceanbase-admin 已提交
2219 2220 2221 2222 2223
        }
      }
    }

    if (OB_SUCC(ret)) {
2224 2225 2226 2227
      if (OB_FAIL(extern_backup_info_mgr.mark_backup_info_deleted(backup_set_id_pairs))) {
        LOG_WARN("failed to mark backup info deleted", K(ret), K(backup_set_id_pairs));
      } else if (OB_FAIL(extern_backup_set_file_info_mgr.mark_backup_set_file_deleting(backup_set_id_pairs))) {
        LOG_WARN("failed to mark backup set file info deleting", K(ret));
O
oceanbase-admin 已提交
2228 2229 2230 2231 2232 2233 2234
      }
    }
  }
  return ret;
}

int ObBackupDataClean::mark_inner_table_his_data_deleted(
M
mw0 已提交
2235
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
2236 2237 2238 2239
{
  int ret = OB_SUCCESS;
  ObMySQLTransaction trans;
  const int64_t clog_gc_snapshot = clean_info.clog_gc_snapshot_;
2240
  ObTimeoutCtx timeout_ctx;
O
oceanbase-admin 已提交
2241 2242 2243 2244

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2245 2246
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
O
oceanbase-admin 已提交
2247 2248
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_tenant.backup_element_array_.count(); ++i) {
M
mw0 已提交
2249
      const ObBackupDataCleanElement &backup_clean_element = clean_tenant.backup_element_array_.at(i);
O
oceanbase-admin 已提交
2250 2251
      if (OB_FAIL(mark_backup_task_his_data_deleted(clean_info, backup_clean_element, trans))) {
        LOG_WARN("failed to mark backup task his data deleted", K(ret), K(clean_info));
2252 2253
      } else if (OB_FAIL(mark_log_archive_stauts_his_data_deleted(
                     clean_info, backup_clean_element, clog_gc_snapshot, trans))) {
O
oceanbase-admin 已提交
2254 2255 2256 2257 2258
        LOG_WARN("failed to mark log archive status his data deleted", K(ret), K(clean_info));
      }
    }

    if (OB_SUCC(ret)) {
2259 2260
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
O
oceanbase-admin 已提交
2261 2262
      }
    } else {
2263 2264 2265 2266
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
O
oceanbase-admin 已提交
2267 2268 2269 2270 2271 2272
      }
    }
  }
  return ret;
}

M
mw0 已提交
2273 2274
int ObBackupDataClean::mark_backup_task_his_data_deleted(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, common::ObISQLClient &trans)
O
oceanbase-admin 已提交
2275 2276 2277 2278
{
  int ret = OB_SUCCESS;
  const uint64_t tenant_id = clean_info.tenant_id_;
  const int64_t incarnation = clean_element.incarnation_;
M
mw0 已提交
2279
  const ObBackupDest &backup_dest = clean_element.backup_dest_;
O
oceanbase-admin 已提交
2280 2281 2282 2283 2284 2285

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_element.backup_set_id_array_.count(); ++i) {
M
mw0 已提交
2286
      const ObBackupSetId &backup_set_id = clean_element.backup_set_id_array_.at(i);
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
      if (ObBackupDataCleanMode::CLEAN != backup_set_id.clean_mode_) {
        // do nothing
      } else {
        if (0 == backup_set_id.copy_id_) {
          if (OB_FAIL(inner_mark_backup_task_his_data_deleted(
                  tenant_id, incarnation, backup_set_id.backup_set_id_, backup_dest, trans))) {
            LOG_WARN("failed to do inner mark backup task his data deleted", K(ret), K(backup_set_id), K(clean_info));
          }
        } else {
          if (OB_FAIL(inner_mark_backup_backup_task_his_data_deleted(
                  tenant_id, incarnation, backup_set_id.backup_set_id_, backup_set_id.copy_id_, backup_dest, trans))) {
            LOG_WARN("failed to do inner mark backup backupset task his data deleted",
                K(ret),
                K(backup_set_id),
                K(clean_info));
          }
        }

        if (OB_FAIL(ret)) {
        } else if (OB_FAIL(inner_mark_backup_set_file_data_deleted(
                       tenant_id, incarnation, backup_set_id.backup_set_id_, backup_set_id.copy_id_, trans))) {
          LOG_WARN("failed to inner mark backup set file data deleted", K(ret), K(backup_set_id), K(clean_info));
        }
      }
    }
O
oceanbase-admin 已提交
2312 2313 2314 2315 2316
  }
  return ret;
}

int ObBackupDataClean::inner_mark_backup_task_his_data_deleted(const uint64_t tenant_id, const int64_t incarnation,
M
mw0 已提交
2317
    const int64_t backup_set_id, const ObBackupDest &backup_dest, common::ObISQLClient &trans)
O
oceanbase-admin 已提交
2318 2319 2320 2321
{
  int ret = OB_SUCCESS;
  ObBackupTaskHistoryUpdater updater;
  ObArray<ObTenantBackupTaskInfo> backup_tasks;
2322
  const bool for_update = true;
O
oceanbase-admin 已提交
2323 2324 2325 2326

  if (OB_FAIL(updater.init(trans))) {
    LOG_WARN("failed to init backup task history updater", K(ret));
  } else if (OB_FAIL(updater.get_need_mark_deleted_backup_tasks(
2327
                 tenant_id, backup_set_id, incarnation, backup_dest, for_update, backup_tasks))) {
O
oceanbase-admin 已提交
2328 2329 2330
    LOG_WARN("failed to get need mark deleted backup tasks", K(ret), K(tenant_id), K(backup_set_id), K(backup_dest));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_tasks.count(); ++i) {
M
mw0 已提交
2331
      const ObTenantBackupTaskInfo &backup_task_info = backup_tasks.at(i);
O
oceanbase-admin 已提交
2332 2333 2334 2335 2336 2337 2338 2339 2340
      if (OB_FAIL(updater.mark_backup_task_deleted(
              backup_task_info.tenant_id_, backup_task_info.incarnation_, backup_task_info.backup_set_id_))) {
        LOG_WARN("failed to mark backup task deleted", K(ret), K(backup_task_info));
      }
    }
  }
  return ret;
}

2341 2342
int ObBackupDataClean::inner_mark_backup_backup_task_his_data_deleted(const uint64_t tenant_id,
    const int64_t incarnation, const int64_t backup_set_id, const int64_t copy_id,
M
mw0 已提交
2343
    const share::ObBackupDest &backup_dest, common::ObISQLClient &trans)
O
oceanbase-admin 已提交
2344 2345
{
  int ret = OB_SUCCESS;
2346 2347
  ObArray<ObTenantBackupBackupsetTaskInfo> backup_backupset_tasks;
  UNUSED(incarnation);
O
oceanbase-admin 已提交
2348 2349 2350
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2351 2352 2353
  } else if (OB_FAIL(ObTenantBackupBackupsetHistoryOperator::get_need_mark_deleted_tasks_items(
                 tenant_id, copy_id, backup_set_id, backup_dest, backup_backupset_tasks, trans))) {
    LOG_WARN("failed to get need mark deleted task items", KR(ret), K(tenant_id));
O
oceanbase-admin 已提交
2354
  } else {
2355
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_backupset_tasks.count(); ++i) {
M
mw0 已提交
2356
      const ObTenantBackupBackupsetTaskInfo &info = backup_backupset_tasks.at(i);
2357 2358 2359
      if (OB_FAIL(ObTenantBackupBackupsetHistoryOperator::mark_task_item_deleted(
              info.tenant_id_, info.incarnation_, info.copy_id_, info.backup_set_id_, trans))) {
        LOG_WARN("failed to mark task item deleted", KR(ret), K(tenant_id), K(copy_id));
O
oceanbase-admin 已提交
2360 2361 2362
      }
    }
  }
2363

O
oceanbase-admin 已提交
2364 2365 2366
  return ret;
}

2367
int ObBackupDataClean::inner_mark_backup_set_file_data_deleting(const uint64_t tenant_id, const int64_t incarnation,
M
mw0 已提交
2368
    const int64_t backup_set_id, const int64_t copy_id, common::ObISQLClient &trans)
O
oceanbase-admin 已提交
2369 2370
{
  int ret = OB_SUCCESS;
2371 2372 2373
  ObArray<ObBackupSetFileInfo> backup_set_file_infos;
  const bool for_update = true;
  ObBackupSetFileInfo dest_backup_set_file;
O
oceanbase-admin 已提交
2374

2375 2376 2377 2378 2379 2380 2381 2382
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(ObBackupSetFilesOperator::get_tenant_backup_set_file_infos(
                 tenant_id, incarnation, backup_set_id, copy_id, for_update, trans, backup_set_file_infos))) {
    LOG_WARN("failed to get tenant backup set file infos", K(ret), K(tenant_id), K(incarnation), K(backup_set_id));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_file_infos.count(); ++i) {
M
mw0 已提交
2383
      const ObBackupSetFileInfo &src_backup_set_file = backup_set_file_infos.at(i);
2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
      dest_backup_set_file = src_backup_set_file;
      if (ObBackupSetFileInfo::DOING == src_backup_set_file.status_) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("backup set file status is unexpected, can not clean", K(ret), K(src_backup_set_file));
      } else if (ObBackupFileStatus::BACKUP_FILE_DELETING == src_backup_set_file.file_status_) {
        // do nothing
      } else if (FALSE_IT(dest_backup_set_file.file_status_ = ObBackupFileStatus::BACKUP_FILE_DELETING)) {
      } else if (OB_FAIL(ObBackupSetFilesOperator::update_tenant_backup_set_file(
                     src_backup_set_file, dest_backup_set_file, trans))) {
        LOG_WARN("failed to update tenant backup set file", K(ret), K(src_backup_set_file), K(dest_backup_set_file));
      }
    }
  }
O
oceanbase-admin 已提交
2397 2398 2399
  return ret;
}

2400
int ObBackupDataClean::inner_mark_backup_set_file_data_deleted(const uint64_t tenant_id, const int64_t incarnation,
M
mw0 已提交
2401
    const int64_t backup_set_id, const int64_t copy_id, common::ObISQLClient &trans)
O
oceanbase-admin 已提交
2402 2403
{
  int ret = OB_SUCCESS;
2404 2405 2406 2407
  ObArray<ObBackupSetFileInfo> backup_set_file_infos;
  const bool for_update = true;
  ObBackupSetFileInfo dest_backup_set_file;

O
oceanbase-admin 已提交
2408 2409 2410
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2411 2412 2413 2414 2415
  } else if (OB_FAIL(ObBackupSetFilesOperator::get_tenant_backup_set_file_infos(
                 tenant_id, incarnation, backup_set_id, copy_id, for_update, trans, backup_set_file_infos))) {
    LOG_WARN("failed to get tenant backup set file infos", K(ret), K(tenant_id), K(incarnation), K(backup_set_id));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_file_infos.count(); ++i) {
M
mw0 已提交
2416
      const ObBackupSetFileInfo &src_backup_set_file = backup_set_file_infos.at(i);
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
      dest_backup_set_file = src_backup_set_file;
      if (ObBackupSetFileInfo::DOING == src_backup_set_file.status_) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("backup set file status is unexpected, can not clean", K(ret), K(src_backup_set_file));
      } else if (ObBackupFileStatus::BACKUP_FILE_DELETED == src_backup_set_file.file_status_) {
        // do nothing
      } else if (FALSE_IT(dest_backup_set_file.file_status_ = ObBackupFileStatus::BACKUP_FILE_DELETED)) {
      } else if (OB_FAIL(ObBackupSetFilesOperator::update_tenant_backup_set_file(
                     src_backup_set_file, dest_backup_set_file, trans))) {
        LOG_WARN("failed to update tenant backup set file", K(ret), K(src_backup_set_file), K(dest_backup_set_file));
      }
    }
O
oceanbase-admin 已提交
2429 2430 2431 2432
  }
  return ret;
}

M
mw0 已提交
2433 2434
int ObBackupDataClean::mark_log_archive_stauts_his_data_deleted(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, const int64_t clog_gc_snapshot, common::ObISQLClient &trans)
O
oceanbase-admin 已提交
2435 2436 2437 2438 2439 2440
{
  int ret = OB_SUCCESS;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
  } else if (clean_info.is_delete_backup_piece()) {
    const bool for_update = true;
    ObLogArchiveBackupInfoMgr log_archive_backup_info_mgr;
    ObBackupPieceInfo backup_piece_info;
    if (OB_FAIL(log_archive_backup_info_mgr.get_backup_piece(trans,
            for_update,
            clean_info.tenant_id_,
            clean_info.backup_piece_id_,
            clean_info.copy_id_,
            backup_piece_info))) {
      LOG_WARN("failed to get backup piece info", K(ret), K(clean_info));
    } else if (OB_FAIL(mark_log_archive_piece_data_deleting(clean_info, backup_piece_info, clog_gc_snapshot, trans))) {
      LOG_WARN("failed to mark log archive piece data deleted", K(clean_info), K(backup_piece_info));
    }
  } else if (clean_info.is_delete_backup_round()) {
    const int64_t start_piece_id = 0;
    if (OB_FAIL(mark_log_archive_round_data_deleted(
            clean_info, clean_info.backup_round_id_, clean_info.copy_id_, start_piece_id, clog_gc_snapshot, trans))) {
      LOG_WARN("failed to mark delete log archive round data deleted", K(ret), K(clean_info));
    }
O
oceanbase-admin 已提交
2461
  } else {
2462
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_element.log_archive_round_array_.count(); ++i) {
M
mw0 已提交
2463
      const ObLogArchiveRound &log_archive_round = clean_element.log_archive_round_array_.at(i);
2464 2465 2466 2467 2468 2469 2470
      if (OB_FAIL(mark_log_archive_round_data_deleted(clean_info,
              log_archive_round.log_archive_round_,
              log_archive_round.copy_id_,
              log_archive_round.start_piece_id_,
              clog_gc_snapshot,
              trans))) {
        LOG_WARN("failed to mark log archive round data deleted", K(ret), K(clean_info), K(log_archive_round));
O
oceanbase-admin 已提交
2471 2472 2473 2474 2475 2476
      }
    }
  }
  return ret;
}

M
mw0 已提交
2477
int ObBackupDataClean::mark_log_archive_round_data_deleted(const share::ObBackupCleanInfo &clean_info,
2478
    const int64_t log_archive_round, const int64_t copy_id, const int64_t start_piece_id,
M
mw0 已提交
2479
    const int64_t clog_gc_snapshot, common::ObISQLClient &trans)
O
oceanbase-admin 已提交
2480 2481
{
  int ret = OB_SUCCESS;
2482 2483 2484 2485 2486
  const bool is_backup_backup = copy_id > 0;
  ObLogArchiveBackupInfo log_archive_backup_info;
  ObLogArchiveBackupInfoMgr log_archive_backup_info_mgr;
  ObArray<ObBackupPieceInfo> backup_piece_infos;
  const bool for_update = true;
O
oceanbase-admin 已提交
2487 2488 2489 2490

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2491 2492 2493 2494 2495 2496 2497 2498
  } else if (is_backup_backup && OB_FAIL(log_archive_backup_info_mgr.set_backup_backup())) {
    LOG_WARN("failed to set copy id", K(ret), K(clean_info));
  } else if (OB_FAIL(log_archive_backup_info_mgr.get_log_archive_history_info(
                 trans, clean_info.tenant_id_, log_archive_round, copy_id, for_update, log_archive_backup_info))) {
    if (OB_ENTRY_NOT_EXIST == ret && is_backup_backup && start_piece_id > 0) {
      ret = OB_SUCCESS;
    } else {
      LOG_WARN("failed to get log archive history info", K(ret), K(clean_info));
O
oceanbase-admin 已提交
2499
    }
2500 2501 2502 2503 2504 2505
  } else if (ObLogArchiveStatus::STOP != log_archive_backup_info.status_.status_ ||
             log_archive_backup_info.status_.checkpoint_ts_ < clog_gc_snapshot) {
    // do nothing
  } else if (OB_FAIL(
                 log_archive_backup_info_mgr.mark_log_archive_history_info_deleted(log_archive_backup_info, trans))) {
    LOG_WARN("failed to mark log archive history info deleted", K(ret), K(clean_info), K(log_archive_backup_info));
O
oceanbase-admin 已提交
2506 2507 2508
  }

  if (OB_FAIL(ret)) {
2509 2510 2511 2512 2513 2514 2515 2516
  } else if (OB_FAIL(log_archive_backup_info_mgr.get_round_backup_piece_infos(trans,
                 for_update,
                 clean_info.tenant_id_,
                 log_archive_backup_info.status_.incarnation_,
                 clean_info.backup_round_id_,
                 is_backup_backup,
                 backup_piece_infos))) {
    LOG_WARN("failed to get round backup piece infos", K(ret), K(clean_info), K(log_archive_backup_info));
O
oceanbase-admin 已提交
2517
  } else {
2518 2519 2520 2521 2522
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_piece_infos.count(); ++i) {
      ObBackupPieceInfo backup_piece_info = backup_piece_infos.at(i);
      if (OB_FAIL(mark_log_archive_piece_data_deleting(clean_info, backup_piece_info, clog_gc_snapshot, trans))) {
        LOG_WARN("failed to mark log archive piece data deleted", K(ret), K(clean_info));
      }
O
oceanbase-admin 已提交
2523 2524 2525 2526 2527
    }
  }
  return ret;
}

M
mw0 已提交
2528 2529
int ObBackupDataClean::mark_log_archive_piece_data_deleting(const share::ObBackupCleanInfo &clean_info,
    const ObBackupPieceInfo &backup_piece_info, const int64_t clog_gc_snapshot, common::ObISQLClient &trans)
O
oceanbase-admin 已提交
2530 2531
{
  int ret = OB_SUCCESS;
2532
  ObLogArchiveBackupInfoMgr log_archive_backup_info_mgr;
O
oceanbase-admin 已提交
2533 2534 2535 2536 2537

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
    if (!ObBackupUtils::can_backup_pieces_be_deleted(backup_piece_info.status_) ||
        backup_piece_info.max_ts_ > clog_gc_snapshot) {
      if (clean_info.is_delete_backup_piece() || clean_info.is_delete_backup_round()) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("backup piece info status is unexpected, can not delete", K(ret), K(backup_piece_info));
      } else {
        // do nothing
      }
    } else if (ObBackupFileStatus::BACKUP_FILE_DELETING == backup_piece_info.file_status_) {
      // do nothing
    } else {
      ObBackupPieceInfo dest_backup_piece_info = backup_piece_info;
      dest_backup_piece_info.file_status_ = ObBackupFileStatus::BACKUP_FILE_DELETING;
      if (OB_FAIL(log_archive_backup_info_mgr.update_backup_piece(trans, dest_backup_piece_info))) {
        LOG_WARN("failed to update backup piece", K(ret), K(dest_backup_piece_info), K(backup_piece_info));
      }
O
oceanbase-admin 已提交
2554 2555
    }
  }
2556 2557
  return ret;
}
O
oceanbase-admin 已提交
2558

2559
int ObBackupDataClean::mark_backup_set_infos_deleting(
M
mw0 已提交
2560
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
2561 2562 2563 2564 2565 2566 2567
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_tenant.backup_element_array_.count(); ++i) {
M
mw0 已提交
2568
      const ObBackupDataCleanElement &clean_element = clean_tenant.backup_element_array_.at(i);
2569 2570 2571 2572 2573
      if (OB_FAIL(mark_backup_set_info_deleting(clean_info, clean_element))) {
        LOG_WARN("failed to mark backup set info deleted", K(ret), K(clean_info));
      }
    }
  }
O
oceanbase-admin 已提交
2574 2575 2576
  return ret;
}

2577
int ObBackupDataClean::mark_backup_set_info_deleting(
M
mw0 已提交
2578
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanElement &clean_element)
O
oceanbase-admin 已提交
2579 2580
{
  int ret = OB_SUCCESS;
2581
  ObArray<ObBackupSetId> backup_set_ids;
O
oceanbase-admin 已提交
2582 2583 2584 2585

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2586 2587 2588 2589 2590 2591
  } else if (OB_FAIL(get_need_delete_backup_set_ids(clean_element, backup_set_ids))) {
    LOG_WARN("failed to get need delete backup set ids", K(ret), K(clean_info));
  } else if (OB_FAIL(mark_backup_set_info_inner_table_deleting(clean_info, clean_element, backup_set_ids))) {
    LOG_WARN("failed to mark backup set info inner table deleted", K(ret), K(clean_info));
  } else if (OB_FAIL(mark_extern_backup_set_info_deleting(clean_info, clean_element, backup_set_ids))) {
    LOG_WARN("failed to mark extern backup set info deleted", K(ret), K(clean_info));
O
oceanbase-admin 已提交
2592 2593 2594 2595
  }
  return ret;
}

M
mw0 已提交
2596 2597
int ObBackupDataClean::mark_backup_set_info_inner_table_deleting(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, const common::ObIArray<ObBackupSetId> &backup_set_ids)
O
oceanbase-admin 已提交
2598 2599
{
  int ret = OB_SUCCESS;
2600 2601 2602
  const uint64_t tenant_id = clean_info.tenant_id_;
  ObTimeoutCtx timeout_ctx;
  ObMySQLTransaction trans;
O
oceanbase-admin 已提交
2603
  ObBackupTaskHistoryUpdater updater;
2604
  UNUSED(clean_element);
O
oceanbase-admin 已提交
2605 2606 2607 2608

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2609 2610
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
O
oceanbase-admin 已提交
2611
  } else {
2612 2613 2614 2615
    if (OB_FAIL(updater.init(trans))) {
      LOG_WARN("failed to init history updater", K(ret));
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_ids.count(); ++i) {
M
mw0 已提交
2616
        const ObBackupSetId &backup_set_id = backup_set_ids.at(i);
2617 2618 2619 2620 2621 2622 2623
        if (ObBackupDataCleanMode::CLEAN != backup_set_id.clean_mode_) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("can not mark backup set deleted", K(ret), K(backup_set_id));
        } else if (OB_FAIL(updater.mark_backup_set_info_deleting(
                       tenant_id, backup_set_id.backup_set_id_, backup_set_id.copy_id_))) {
          LOG_WARN("failed to mark backup set info deleting", K(ret), K(backup_set_id));
        }
O
oceanbase-admin 已提交
2624 2625 2626
      }
    }

2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
    if (OB_SUCC(ret)) {
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
      }
    } else {
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
      }
    }
  }
O
oceanbase-admin 已提交
2639 2640 2641
  return ret;
}

2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741
int ObBackupDataClean::get_source_backup_set_file_info(const uint64_t tenant_id, const int64_t incarnation,
    const ObBackupSetId &backup_set_id, ObBackupSetFileInfo &backup_set_file_info, bool &is_need_modify)
{
  int ret = OB_SUCCESS;
  int64_t src_copy_id = 0;  // src backup dest copy_id=0
  bool for_update = false;
  char backup_dest_str[OB_MAX_BACKUP_DEST_LENGTH] = "";
  is_need_modify = false;
  backup_set_file_info.reset();
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(ObBackupSetFilesOperator::get_tenant_backup_set_file_info(tenant_id,
                 backup_set_id.backup_set_id_,
                 incarnation,
                 src_copy_id,
                 for_update,
                 *sql_proxy_,
                 backup_set_file_info))) {
    LOG_WARN("failed to get tenant backup set file info", K(ret), K(backup_set_id));
  } else if (OB_FAIL(GCONF.backup_dest.copy(backup_dest_str, sizeof(backup_dest_str)))) {
    LOG_WARN("failed to get configure backup dest", K(ret));
  } else if (0 != strcmp(backup_dest_str, backup_set_file_info.backup_dest_.ptr())) {
    LOG_INFO("the source backup destination of the backup backup are not equal to the configured backup destination");
  } else {
    is_need_modify = true;
  }

  return ret;
}

int ObBackupDataClean::get_source_backup_dest_from_piece_file(const common::ObIArray<ObBackupPieceInfoKey> &piece_keys,
    ObClusterBackupDest &cluster_backup_dest, bool &is_need_modify)
{
  int ret = OB_SUCCESS;
  bool for_update = false;
  int64_t src_copy_id = 0;
  char backup_dest_str[OB_MAX_BACKUP_DEST_LENGTH] = "";
  is_need_modify = false;
  cluster_backup_dest.reset();
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (piece_keys.empty()) {
    // do nothing
  } else {
    ObBackupPieceInfo piece_info;
    ObBackupDest backup_dest;
    ObLogArchiveBackupInfoMgr log_archive_info_mgr;
    const share::ObBackupPieceInfoKey &piece_key = piece_keys.at(0);
    if (OB_FAIL(log_archive_info_mgr.get_backup_piece(
            *sql_proxy_, for_update, piece_key.tenant_id_, piece_key.backup_piece_id_, src_copy_id, piece_info))) {
      LOG_WARN("failed to get piece file info", K(ret), K(piece_key));
    } else if (OB_FAIL(GCONF.backup_dest.copy(backup_dest_str, sizeof(backup_dest_str)))) {
      LOG_WARN("failed to get backup dest", K(ret));
    } else if (0 != strcmp(backup_dest_str, piece_info.backup_dest_.ptr())) {
      LOG_INFO("backup_dest of backup_set is not current backup dest");
    } else if (OB_FAIL(backup_dest.set(backup_dest_str))) {
      LOG_WARN("failed to set backup dest", K(ret));
    } else if (OB_FAIL(cluster_backup_dest.set(backup_dest, piece_key.incarnation_))) {
      LOG_WARN("failed to set cluster backup dest", K(ret));
    } else {
      is_need_modify = true;
    }
  }

  return ret;
}

// mark the status of the external file at the source destination of the backup backup as deleting
int ObBackupDataClean::mark_extern_source_backup_set_info_of_backup_backup(const uint64_t tenant_id,
    const int64_t incarnation, const ObBackupSetFileInfo &backup_set_file_info,
    const ObArray<ObBackupSetIdPair> &backup_set_id_pairs, const bool is_deleting)
{
  int ret = OB_SUCCESS;
  ObClusterBackupDest src_cluster_backup_dest;
  ObBackupDest src_backup_dest;
  ObExternBackupSetFileInfoMgr src_extern_backup_set_file_info_mgr;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(src_backup_dest.set(backup_set_file_info.backup_dest_.ptr()))) {
    LOG_WARN("failed to set backup dest", K(ret));
  } else if (OB_FAIL(src_cluster_backup_dest.set(src_backup_dest, incarnation))) {
    LOG_WARN("failed to set cluster backup dest", K(ret), K(src_backup_dest));
  } else if (OB_FAIL(src_extern_backup_set_file_info_mgr.init(
                 tenant_id, src_cluster_backup_dest, true /* is_backup_backup */, *backup_lease_service_))) {
    LOG_WARN("failed to init extern backup set file info", K(ret), K(src_cluster_backup_dest));
  } else {
    if (is_deleting &&
        OB_FAIL(src_extern_backup_set_file_info_mgr.mark_backup_set_file_deleting(backup_set_id_pairs))) {
      LOG_WARN("failed to mark backup set file deleting", K(ret));
    } else if (!is_deleting &&
               OB_FAIL(src_extern_backup_set_file_info_mgr.mark_backup_set_file_deleted(backup_set_id_pairs))) {
      LOG_WARN("failed to mark backup set file deleted", K(ret));
    }
  }
  return ret;
}

M
mw0 已提交
2742 2743
int ObBackupDataClean::mark_extern_backup_set_info_deleting(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, const common::ObIArray<ObBackupSetId> &backup_set_ids)
O
oceanbase-admin 已提交
2744 2745
{
  int ret = OB_SUCCESS;
2746 2747
  const uint64_t tenant_id = clean_info.tenant_id_;
  const int64_t incarnation = clean_element.incarnation_;
M
mw0 已提交
2748
  const ObBackupDest &backup_dest = clean_element.backup_dest_;
2749 2750 2751 2752 2753
  ObArray<ObBackupSetIdPair> backup_set_id_pairs;
  ObExternBackupInfoMgr extern_backup_info_mgr;
  ObExternBackupSetFileInfoMgr extern_backup_set_file_info_mgr;
  ObClusterBackupDest cluster_backup_dest;
  ObBackupSetIdPair backup_set_id_pair;
2754 2755 2756 2757
  const bool is_backup_backup = clean_info.copy_id_ > 0 || clean_info.is_delete_obsolete_backup_backup();
  bool src_backup_dest_unknown = true;
  bool is_need_modify = false;  // the status of the external file at the source destination of the backup backup
  ObBackupSetFileInfo backup_set_file_info;
2758 2759 2760 2761 2762

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (backup_set_ids.empty()) {
2763
    // do nothing
2764 2765 2766 2767
  } else if (OB_FAIL(cluster_backup_dest.set(backup_dest, incarnation))) {
    LOG_WARN("failed to set cluster backup dest", K(ret), K(backup_dest));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_ids.count(); ++i) {
M
mw0 已提交
2768
      const ObBackupSetId &backup_set_id = backup_set_ids.at(i);
2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779
      backup_set_id_pair.reset();
      if (ObBackupDataCleanMode::CLEAN != backup_set_id.clean_mode_) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("can not mark backup set deleted", K(ret), K(backup_set_id));
      } else {
        backup_set_id_pair.backup_set_id_ = backup_set_id.backup_set_id_;
        backup_set_id_pair.copy_id_ = backup_set_id.copy_id_;
        if (OB_FAIL(backup_set_id_pairs.push_back(backup_set_id_pair))) {
          LOG_WARN("failed to push backup set id into array", K(ret), K(backup_set_id_pair));
        }
      }
2780 2781 2782 2783 2784 2785 2786 2787 2788

      if (src_backup_dest_unknown && OB_SUCC(ret) && is_backup_backup) {
        if (OB_FAIL(get_source_backup_set_file_info(
                tenant_id, incarnation, backup_set_id, backup_set_file_info, is_need_modify))) {
          LOG_WARN("failed to get source backup set file info", K(ret), K(backup_set_id));
        } else {
          src_backup_dest_unknown = false;
        }
      }
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(extern_backup_info_mgr.init(tenant_id, cluster_backup_dest, *backup_lease_service_))) {
        LOG_WARN("failed to init extern backup info mgr", K(ret), K(cluster_backup_dest));
      } else if (OB_FAIL(extern_backup_info_mgr.mark_backup_info_deleted(backup_set_id_pairs))) {
        LOG_WARN("failed to mark backup info deleted", K(ret), K(clean_info));
      }
    }

2799 2800 2801 2802
    if (OB_SUCC(ret) && is_backup_backup && is_need_modify) {
      if (OB_FAIL(mark_extern_source_backup_set_info_of_backup_backup(
              tenant_id, incarnation, backup_set_file_info, backup_set_id_pairs, true /* is_deleting*/))) {
        LOG_WARN("failed to mark source backup set file info deleting", K(ret), K(clean_info));
2803 2804
      }
    }
2805 2806 2807 2808 2809 2810 2811 2812

    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(extern_backup_set_file_info_mgr.init(
                   tenant_id, cluster_backup_dest, is_backup_backup, *backup_lease_service_))) {
      LOG_WARN("failed to init extern backup set file info", K(ret), K(clean_info));
    } else if (OB_FAIL(extern_backup_set_file_info_mgr.mark_backup_set_file_deleting(backup_set_id_pairs))) {
      LOG_WARN("failed to mark backup set file deleting", K(ret), K(clean_info));
    }
O
oceanbase-admin 已提交
2813 2814 2815 2816
  }
  return ret;
}

2817
int ObBackupDataClean::mark_log_archive_infos_deleting(
M
mw0 已提交
2818
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
2819 2820 2821 2822 2823
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2824 2825
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_tenant.backup_element_array_.count(); ++i) {
M
mw0 已提交
2826
      const ObBackupDataCleanElement &clean_element = clean_tenant.backup_element_array_.at(i);
2827 2828 2829 2830
      if (OB_FAIL(mark_log_archive_info_deleting(clean_info, clean_element))) {
        LOG_WARN("failed to mark log archive inifo deleted", K(ret), K(clean_info));
      }
    }
O
oceanbase-admin 已提交
2831 2832 2833 2834
  }
  return ret;
}

2835
int ObBackupDataClean::mark_log_archive_info_deleting(
M
mw0 已提交
2836
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanElement &clean_element)
O
oceanbase-admin 已提交
2837 2838
{
  int ret = OB_SUCCESS;
2839 2840 2841
  ObArray<ObBackupPieceInfoKey> backup_piece_keys;
  ObArray<ObLogArchiveRound> log_archive_rounds;

O
oceanbase-admin 已提交
2842 2843 2844
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
2845 2846 2847 2848 2849 2850 2851 2852
  } else if (OB_FAIL(get_need_delete_clog_round_and_piece(
                 clean_info, clean_element, log_archive_rounds, backup_piece_keys))) {
    LOG_WARN("failed to get need delete clog round and piece", K(ret), K(clean_info));
  } else if (OB_FAIL(mark_log_archive_info_inner_table_deleting(clean_info, backup_piece_keys, log_archive_rounds))) {
    LOG_WARN("failed to mark backup set info inner table deleted", K(clean_info));
  } else if (OB_FAIL(mark_extern_log_archive_info_deleting(
                 clean_info, clean_element, backup_piece_keys, log_archive_rounds))) {
    LOG_WARN("failed to mark extern backup set info deleted", K(ret), K(clean_info));
O
oceanbase-admin 已提交
2853
  }
2854

O
oceanbase-admin 已提交
2855 2856 2857
  return ret;
}

M
mw0 已提交
2858 2859 2860
int ObBackupDataClean::mark_log_archive_info_inner_table_deleting(const share::ObBackupCleanInfo &clean_info,
    const common::ObIArray<ObBackupPieceInfoKey> &backup_piece_keys,
    const common::ObIArray<ObLogArchiveRound> &log_archive_rounds)
O
oceanbase-admin 已提交
2861 2862
{
  int ret = OB_SUCCESS;
2863
  ObLogArchiveBackupInfoMgr log_info_mgr;
O
oceanbase-admin 已提交
2864
  ObMySQLTransaction trans;
2865 2866 2867 2868 2869
  const bool for_update = true;
  ObLogArchiveBackupInfo info;
  ObBackupPieceInfo piece_info;
  const ObBackupFileStatus::STATUS dest_file_status = ObBackupFileStatus::BACKUP_FILE_DELETING;
  ObTimeoutCtx timeout_ctx;
O
oceanbase-admin 已提交
2870 2871 2872

  if (!is_inited_) {
    ret = OB_NOT_INIT;
2873 2874 2875
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
O
oceanbase-admin 已提交
2876
  } else {
2877
    for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_rounds.count(); ++i) {
M
mw0 已提交
2878
      const ObLogArchiveRound &log_archive_round = log_archive_rounds.at(i);
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893
      ObLogArchiveBackupInfoMgr log_info_mgr;
      info.reset();
      if (log_archive_round.copy_id_ > 0 && OB_FAIL(log_info_mgr.set_backup_backup())) {
        LOG_WARN("failed to set log info mgr copy id", K(ret), K(log_archive_round));
      } else if (OB_FAIL(log_info_mgr.get_log_archive_history_info(trans,
                     clean_info.tenant_id_,
                     log_archive_round.log_archive_round_,
                     log_archive_round.copy_id_,
                     for_update,
                     info))) {
        if (OB_ENTRY_NOT_EXIST == ret && log_archive_round.copy_id_ > 0) {
          // backup backuplog may not generate backup round info, skip it
          ret = OB_SUCCESS;
        } else {
          LOG_WARN("failed to get log archive history info", K(ret), K(clean_info), K(log_archive_round));
O
oceanbase-admin 已提交
2894
        }
2895 2896
      } else if (OB_FAIL(log_info_mgr.mark_log_archive_history_info_deleted(info, trans))) {
        LOG_WARN("failed to mark log archive history info deleted", K(ret), K(info));
O
oceanbase-admin 已提交
2897
      }
2898
    }
O
oceanbase-admin 已提交
2899

2900
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_piece_keys.count(); ++i) {
M
mw0 已提交
2901
      const ObBackupPieceInfoKey &piece_key = backup_piece_keys.at(i);
2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
      piece_info.reset();
      ObLogArchiveBackupInfoMgr log_info_mgr;
      if (piece_key.copy_id_ > 0 && OB_FAIL(log_info_mgr.set_backup_backup())) {
        LOG_WARN("failed to set log info mgr copy id", K(ret), K(piece_key));
      } else if (OB_FAIL(log_info_mgr.get_backup_piece(trans, for_update, piece_key, piece_info))) {
        LOG_WARN("failed to get backup piece", K(ret), K(piece_key));
      } else if (ObBackupFileStatus::BACKUP_FILE_DELETING == piece_info.file_status_ ||
                 ObBackupFileStatus::BACKUP_FILE_DELETED == piece_info.file_status_) {
        // do nothing
      } else if (OB_FAIL(ObBackupFileStatus::check_can_change_status(piece_info.file_status_, dest_file_status))) {
        LOG_WARN("failed to check can change status", K(ret), K(piece_info));
      } else if (FALSE_IT(piece_info.file_status_ = ObBackupFileStatus::BACKUP_FILE_DELETING)) {
      } else if (OB_FAIL(log_info_mgr.update_backup_piece(trans, piece_info))) {
        LOG_WARN("failed to update backup piece", K(ret), K(piece_info));
O
oceanbase-admin 已提交
2916
      }
2917
    }
O
oceanbase-admin 已提交
2918 2919

    if (OB_SUCC(ret)) {
2920 2921
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
O
oceanbase-admin 已提交
2922 2923
      }
    } else {
2924 2925 2926 2927
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
O
oceanbase-admin 已提交
2928 2929 2930
      }
    }
  }
2931
  return ret;
O
oceanbase-admin 已提交
2932 2933
}

M
mw0 已提交
2934 2935 2936
int ObBackupDataClean::mark_extern_log_archive_info_deleting(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, const common::ObIArray<ObBackupPieceInfoKey> &backup_piece_keys,
    const common::ObIArray<ObLogArchiveRound> &log_archive_rounds)
O
oceanbase-admin 已提交
2937 2938
{
  int ret = OB_SUCCESS;
2939 2940 2941 2942
  ObArray<int64_t> round_ids;
  ObLogArchiveBackupInfoMgr log_info_mgr;
  const uint64_t tenant_id = clean_info.tenant_id_;
  const int64_t incarnation = clean_element.incarnation_;
M
mw0 已提交
2943
  const ObBackupDest &backup_dest = clean_element.backup_dest_;
2944
  ObClusterBackupDest cluster_backup_dest;
2945 2946 2947
  const bool is_backup_backup = clean_info.copy_id_ > 0 || clean_info.is_delete_obsolete_backup_backup();
  ObClusterBackupDest src_cluster_backup_dest;
  bool is_need_modify = false;  // the status of the external file at the source destination of the backup backup
O
oceanbase-admin 已提交
2948 2949 2950

  if (!is_inited_) {
    ret = OB_NOT_INIT;
2951 2952 2953 2954 2955
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(cluster_backup_dest.set(backup_dest, incarnation))) {
    LOG_WARN("failed to set cluster backup dest", K(ret), K(backup_dest), K(clean_info));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_rounds.count(); ++i) {
M
mw0 已提交
2956
      const ObLogArchiveRound &log_archive_round = log_archive_rounds.at(i);
2957 2958 2959 2960 2961
      if (OB_FAIL(round_ids.push_back(log_archive_round.log_archive_round_))) {
        LOG_WARN("failed to push log archive round into array", K(ret), K(log_archive_round));
      }
    }

2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974
    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(log_info_mgr.mark_extern_log_archive_backup_info_deleted(
                   cluster_backup_dest, tenant_id, round_ids, *backup_lease_service_))) {
      LOG_WARN("failed to mark extern log archive backup info deleted", K(ret), K(cluster_backup_dest));
    } else if (is_backup_backup) {
      if (OB_FAIL(get_source_backup_dest_from_piece_file(backup_piece_keys, src_cluster_backup_dest, is_need_modify))) {
        LOG_WARN("failed to get source backup piece file info", K(ret));
      } else if (is_need_modify && OB_FAIL(log_info_mgr.mark_extern_backup_piece_deleting(src_cluster_backup_dest,
                                       tenant_id,
                                       backup_piece_keys,
                                       is_backup_backup,
                                       *backup_lease_service_))) {
        LOG_WARN("failed to mark source extern backup piece deleting", K(ret));
2975 2976
      }
    }
2977 2978 2979 2980 2981 2982

    if (OB_SUCC(ret) &&
        OB_FAIL(log_info_mgr.mark_extern_backup_piece_deleting(
            cluster_backup_dest, tenant_id, backup_piece_keys, is_backup_backup, *backup_lease_service_))) {
      LOG_WARN("failed to mark extern backup piece deleting", K(ret), K(cluster_backup_dest));
    }
O
oceanbase-admin 已提交
2983 2984 2985 2986
  }
  return ret;
}

2987
int ObBackupDataClean::get_need_delete_backup_set_ids(
M
mw0 已提交
2988
    const ObBackupDataCleanElement &clean_element, common::ObIArray<ObBackupSetId> &backup_set_ids)
O
oceanbase-admin 已提交
2989 2990
{
  int ret = OB_SUCCESS;
2991
  backup_set_ids.reset();
O
oceanbase-admin 已提交
2992 2993 2994 2995
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
2996
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_element.backup_set_id_array_.count(); ++i) {
M
mw0 已提交
2997
      const ObBackupSetId &backup_set_id = clean_element.backup_set_id_array_.at(i);
2998 2999 3000 3001
      if (ObBackupDataCleanMode::TOUCH == backup_set_id.clean_mode_) {
        // do nothing
      } else if (OB_FAIL(backup_set_ids.push_back(backup_set_id))) {
        LOG_WARN("failed to push backup set id into array", K(ret), K(backup_set_id));
O
oceanbase-admin 已提交
3002 3003 3004 3005 3006 3007
      }
    }
  }
  return ret;
}

M
mw0 已提交
3008 3009 3010
int ObBackupDataClean::get_need_delete_clog_round_and_piece(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, common::ObIArray<ObLogArchiveRound> &log_archive_rounds,
    common::ObIArray<ObBackupPieceInfoKey> &backup_piece_keys)
O
oceanbase-admin 已提交
3011 3012
{
  int ret = OB_SUCCESS;
3013 3014
  log_archive_rounds.reset();
  backup_piece_keys.reset();
O
oceanbase-admin 已提交
3015 3016 3017 3018

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
3019 3020 3021 3022 3023
  } else if (clean_info.is_delete_backup_piece()) {
    ObBackupPieceInfoKey piece_key;
    if (clean_element.log_archive_round_array_.empty() || clean_element.log_archive_round_array_.count() > 1) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("delete backup piece get unexpected round infos", K(ret), K(clean_element));
O
oceanbase-admin 已提交
3024
    } else {
3025 3026 3027 3028 3029 3030 3031
      piece_key.backup_piece_id_ = clean_info.backup_piece_id_;
      piece_key.copy_id_ = clean_element.log_archive_round_array_.at(0).copy_id_;
      piece_key.incarnation_ = clean_element.incarnation_;
      piece_key.round_id_ = clean_element.log_archive_round_array_.at(0).log_archive_round_;
      piece_key.tenant_id_ = clean_info.tenant_id_;
      if (OB_FAIL(backup_piece_keys.push_back(piece_key))) {
        LOG_WARN("failed to push backup piece key into array", K(ret), K(piece_key));
O
oceanbase-admin 已提交
3032
      }
3033
    }
M
mw0 已提交
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
  } else if (OB_SYS_TENANT_ID == clean_info.tenant_id_) {
    if (OB_FAIL(get_sys_tenant_delete_clog_round_and_piece(
            clean_info, clean_element, log_archive_rounds, backup_piece_keys))) {
      LOG_WARN("failed to get sys tenant delete clog round and piece", K(ret), K(clean_info));
    }
  } else {
    if (OB_FAIL(get_normal_tenant_delete_clog_round_and_piece(
            clean_info, clean_element, log_archive_rounds, backup_piece_keys))) {
      LOG_WARN("failed to get normal tenant delete clog round and piece", K(ret), K(clean_info));
    }
  }
  return ret;
}

int ObBackupDataClean::get_sys_tenant_delete_clog_round_and_piece(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, common::ObIArray<ObLogArchiveRound> &log_archive_rounds,
    common::ObIArray<ObBackupPieceInfoKey> &backup_piece_keys)
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_SYS_TENANT_ID != clean_info.tenant_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get sys tenant delete clog round and piece get invalid argument", K(ret), K(clean_info));
  } else if (ObBackupCleanInfoStatus::PREPARE == clean_info.status_) {
    if (OB_FAIL(get_sys_tenant_prepare_clog_round_and_piece(
            clean_info, clean_element, log_archive_rounds, backup_piece_keys))) {
      LOG_WARN("failed to get sys tenant prepare delete clog round and piece", K(ret), K(clean_info));
    }
  } else if (ObBackupCleanInfoStatus::DOING == clean_info.status_) {
    if (OB_FAIL(get_sys_tenant_doing_clog_round_and_piece(
            clean_info, clean_element, log_archive_rounds, backup_piece_keys))) {
      LOG_WARN("failed to get sys tenant prepare delete clog round and piece", K(ret), K(clean_info));
    }
  } else {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("clean info status is unexpected", K(ret), K(clean_info));
  }
  return ret;
}

int ObBackupDataClean::get_sys_tenant_prepare_clog_round_and_piece(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, common::ObIArray<ObLogArchiveRound> &log_archive_rounds,
    common::ObIArray<ObBackupPieceInfoKey> &backup_piece_keys)
{
  int ret = OB_SUCCESS;
  log_archive_rounds.reset();
  backup_piece_keys.reset();
  const int64_t clog_gc_snapshot = clean_info.clog_gc_snapshot_;
  ObLogArchiveBackupInfoMgr log_info_mgr;
  const uint64_t tenant_id = clean_info.tenant_id_;
  const int64_t incarnation = clean_element.incarnation_;
  const int64_t backup_copies = clean_element.backup_dest_option_.backup_copies_;
  ObSimpleArchiveRound simple_archive_round;
  ObSimplePieceKey simple_piece_key;
  const bool overwrite_key = true;
  bool is_delete_inorder = true;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid() || OB_SYS_TENANT_ID != clean_info.tenant_id_ ||
             ObBackupCleanInfoStatus::PREPARE != clean_info.status_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get sys tenant delete clog round and piece get invalid argument", K(ret), K(clean_info));
3100 3101
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_element.log_archive_round_array_.count(); ++i) {
M
mw0 已提交
3102 3103 3104 3105
      const ObLogArchiveRound &log_archive_round = clean_element.log_archive_round_array_.at(i);
      if (!is_delete_inorder) {
        break;
      } else if (clean_info.is_delete_backup_set() && 0 != log_archive_round.start_piece_id_) {
3106
        // do nothing
O
oceanbase-admin 已提交
3107
      } else {
M
mw0 已提交
3108 3109
        for (int64_t j = 0; OB_SUCC(ret) && is_delete_inorder && j < log_archive_round.piece_infos_.count(); ++j) {
          const ObSimplePieceInfo &simple_piece_info = log_archive_round.piece_infos_.at(j);
3110 3111
          if (simple_piece_info.max_ts_ > clog_gc_snapshot ||
              ObBackupPieceStatus::BACKUP_PIECE_FROZEN != simple_piece_info.status_ ||
3112
              ObBackupFileStatus::BACKUP_FILE_COPYING == simple_piece_info.file_status_ ||
3113
              simple_piece_info.copies_num_ < backup_copies) {
M
mw0 已提交
3114
            is_delete_inorder = false;
3115 3116 3117 3118 3119 3120 3121 3122 3123
          } else if (ObBackupFileStatus::BACKUP_FILE_DELETED == simple_piece_info.file_status_) {
            // do nothing
          } else {
            ObBackupPieceInfoKey piece_info_key;
            piece_info_key.backup_piece_id_ = simple_piece_info.backup_piece_id_;
            piece_info_key.copy_id_ = log_archive_round.copy_id_;
            piece_info_key.incarnation_ = incarnation;
            piece_info_key.round_id_ = log_archive_round.log_archive_round_;
            piece_info_key.tenant_id_ = tenant_id;
M
mw0 已提交
3124 3125 3126 3127 3128 3129

            simple_piece_key.reset();
            simple_piece_key.incarnation_ = piece_info_key.incarnation_;
            simple_piece_key.round_id_ = piece_info_key.round_id_;
            simple_piece_key.backup_piece_id_ = piece_info_key.backup_piece_id_;
            simple_piece_key.copy_id_ = piece_info_key.copy_id_;
3130 3131
            if (OB_FAIL(backup_piece_keys.push_back(piece_info_key))) {
              LOG_WARN("failed to push piece info key into array", K(ret), K(simple_piece_info), K(piece_info_key));
M
mw0 已提交
3132 3133
            } else if (OB_FAIL(sys_tenant_deleted_backup_piece_.set_refactored_1(simple_piece_key, overwrite_key))) {
              LOG_WARN("failed to set simple piece key", K(ret), K(simple_piece_key));
3134 3135
            }
          }
O
oceanbase-admin 已提交
3136 3137
        }
      }
3138 3139 3140 3141 3142

      if (OB_FAIL(ret)) {
      } else if (ObLogArchiveStatus::STOP != log_archive_round.log_archive_status_) {
        // do nothing
      } else if (log_archive_round.checkpoint_ts_ > clog_gc_snapshot || log_archive_round.copies_num_ < backup_copies) {
M
mw0 已提交
3143
        is_delete_inorder = false;
3144 3145
      } else if (OB_FAIL(log_archive_rounds.push_back(log_archive_round))) {
        LOG_WARN("failed to push log archive round into array", K(ret), K(log_archive_round));
M
mw0 已提交
3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271
      } else {
        simple_archive_round.reset();
        simple_archive_round.incarnation_ = clean_element.incarnation_;
        simple_archive_round.round_id_ = log_archive_round.log_archive_round_;
        simple_archive_round.copy_id_ = log_archive_round.copy_id_;
        if (OB_FAIL(sys_tenant_deleted_backup_round_.set_refactored_1(simple_archive_round, overwrite_key))) {
          LOG_WARN("failed to set sys tenant deleted backup round", K(ret), K(simple_archive_round));
        }
      }
    }
  }
  return ret;
}

int ObBackupDataClean::get_sys_tenant_doing_clog_round_and_piece(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, common::ObIArray<ObLogArchiveRound> &log_archive_rounds,
    common::ObIArray<ObBackupPieceInfoKey> &backup_piece_keys)
{
  int ret = OB_SUCCESS;
  ObBackupPieceInfoKey backup_piece_key;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (ObBackupCleanInfoStatus::DOING != clean_info.status_) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("backup clean info status is unexpected", K(ret));
  } else {
    hash::ObHashSet<ObSimplePieceKey>::const_iterator piece_iter;
    for (piece_iter = sys_tenant_deleted_backup_piece_.begin();
         OB_SUCC(ret) && piece_iter != sys_tenant_deleted_backup_piece_.end();
         ++piece_iter) {
      backup_piece_key.reset();
      const ObSimplePieceKey &simple_piece_key = piece_iter->first;
      backup_piece_key.backup_piece_id_ = simple_piece_key.backup_piece_id_;
      backup_piece_key.copy_id_ = simple_piece_key.copy_id_;
      backup_piece_key.incarnation_ = simple_piece_key.incarnation_;
      backup_piece_key.round_id_ = simple_piece_key.round_id_;
      backup_piece_key.tenant_id_ = clean_info.tenant_id_;
      if (OB_FAIL(backup_piece_keys.push_back(backup_piece_key))) {
        LOG_WARN("failed to push backup piece key into array", K(ret), K(backup_piece_key), K(simple_piece_key));
      }
    }

    hash::ObHashSet<ObSimpleArchiveRound>::const_iterator round_iter;
    for (round_iter = sys_tenant_deleted_backup_round_.begin();
         OB_SUCC(ret) && round_iter != sys_tenant_deleted_backup_round_.end();
         ++round_iter) {
      const ObSimpleArchiveRound &simple_archive_round = round_iter->first;
      for (int64_t i = 0; OB_SUCC(ret) && i < clean_element.log_archive_round_array_.count(); ++i) {
        const ObLogArchiveRound &tmp_archive_round = clean_element.log_archive_round_array_.at(i);
        if (tmp_archive_round.log_archive_round_ == simple_archive_round.round_id_ &&
            tmp_archive_round.copy_id_ == simple_archive_round.copy_id_) {
          if (OB_FAIL(log_archive_rounds.push_back(tmp_archive_round))) {
            LOG_WARN("failed to push log archive round into array", K(ret), K(tmp_archive_round));
          } else {
            break;
          }
        }
      }
    }
  }
  return ret;
}

int ObBackupDataClean::get_normal_tenant_delete_clog_round_and_piece(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, common::ObIArray<ObLogArchiveRound> &log_archive_rounds,
    common::ObIArray<ObBackupPieceInfoKey> &backup_piece_keys)
{
  int ret = OB_SUCCESS;
  log_archive_rounds.reset();
  backup_piece_keys.reset();
  const uint64_t tenant_id = clean_info.tenant_id_;
  const int64_t incarnation = clean_element.incarnation_;
  ObSimpleArchiveRound simple_archive_round;
  ObSimplePieceKey simple_piece_key;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid() || OB_SYS_TENANT_ID == clean_info.tenant_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get normal tenant delete clog round and piece get invalid argument", K(ret), K(clean_info));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_element.log_archive_round_array_.count(); ++i) {
      const ObLogArchiveRound &log_archive_round = clean_element.log_archive_round_array_.at(i);
      for (int64_t j = 0; OB_SUCC(ret) && j < log_archive_round.piece_infos_.count(); ++j) {
        const ObSimplePieceInfo &simple_piece_info = log_archive_round.piece_infos_.at(j);
        simple_piece_key.reset();
        simple_piece_key.incarnation_ = incarnation;
        simple_piece_key.round_id_ = simple_piece_info.round_id_;
        simple_piece_key.backup_piece_id_ = simple_piece_info.backup_piece_id_;
        simple_piece_key.copy_id_ = log_archive_round.copy_id_;
        const int hash_ret = sys_tenant_deleted_backup_piece_.exist_refactored(simple_piece_key);
        if (OB_HASH_NOT_EXIST == hash_ret) {
          // do nothing
        } else if (OB_HASH_EXIST != hash_ret) {
          ret = hash_ret;
          LOG_WARN("failed to check piece key exist", K(ret), K(simple_piece_key));
        } else {
          ObBackupPieceInfoKey piece_info_key;
          piece_info_key.backup_piece_id_ = simple_piece_info.backup_piece_id_;
          piece_info_key.copy_id_ = log_archive_round.copy_id_;
          piece_info_key.incarnation_ = incarnation;
          piece_info_key.round_id_ = log_archive_round.log_archive_round_;
          piece_info_key.tenant_id_ = tenant_id;
          if (OB_FAIL(backup_piece_keys.push_back(piece_info_key))) {
            LOG_WARN("failed to push piece info key into array", K(ret), K(simple_piece_info), K(piece_info_key));
          }
        }
      }

      if (OB_FAIL(ret)) {
      } else {
        simple_archive_round.reset();
        simple_archive_round.incarnation_ = clean_element.incarnation_;
        simple_archive_round.round_id_ = log_archive_round.log_archive_round_;
        simple_archive_round.copy_id_ = log_archive_round.copy_id_;
        const int hash_ret = sys_tenant_deleted_backup_round_.exist_refactored(simple_archive_round);
        if (OB_HASH_NOT_EXIST == hash_ret) {
          // do nothing
        } else if (OB_HASH_EXIST != hash_ret) {
          ret = hash_ret;
          LOG_WARN("failed to check round exist", K(ret), K(simple_piece_key));
        } else if (OB_FAIL(log_archive_rounds.push_back(log_archive_round))) {
          LOG_WARN("failed to push log archive round into array", K(ret), K(log_archive_round));
        }
3272
      }
O
oceanbase-admin 已提交
3273 3274
    }
  }
3275 3276
  return ret;
}
O
oceanbase-admin 已提交
3277

3278
int ObBackupDataClean::delete_backup_data(
M
mw0 已提交
3279
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
3280 3281 3282 3283 3284 3285 3286
{
  int ret = OB_SUCCESS;
  ObTenantBackupDataCleanMgr tenant_backup_data_clean_mgr;
  if (OB_FAIL(tenant_backup_data_clean_mgr.init(clean_info, clean_tenant, this, sql_proxy_))) {
    LOG_WARN("failed to init tenant backup data clean mgr", K(ret), K(clean_info), K(clean_tenant));
  } else if (OB_FAIL(tenant_backup_data_clean_mgr.do_clean())) {
    LOG_WARN("failed to do clean", K(ret), K(clean_info), K(clean_tenant));
O
oceanbase-admin 已提交
3287
  }
3288

O
oceanbase-admin 已提交
3289 3290 3291
  return ret;
}

3292
int ObBackupDataClean::delete_tenant_backup_meta_data(
M
mw0 已提交
3293
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
3294 3295 3296 3297 3298
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
3299 3300 3301 3302 3303
  } else if (!clean_info.is_valid() || OB_SYS_TENANT_ID == clean_info.tenant_id_ || !clean_tenant.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("delete tenant backup meta data get invalid argument", K(ret), K(clean_info), K(clean_tenant));
  } else if (OB_FAIL(delete_backup_extern_infos(clean_info, clean_tenant))) {
    LOG_WARN("failed to mark extern backup infos deleted", K(ret), K(clean_info));
O
oceanbase-admin 已提交
3304 3305 3306 3307
  }
  return ret;
}

3308
int ObBackupDataClean::delete_backup_extern_infos(
M
mw0 已提交
3309
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
3310 3311
{
  int ret = OB_SUCCESS;
3312 3313 3314 3315
  ObArray<ObBackupSetId> backup_set_ids;
  ObArray<ObLogArchiveRound> log_archive_rounds;
  ObArray<ObBackupPieceInfoKey> backup_piece_keys;

O
oceanbase-admin 已提交
3316 3317 3318
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
3319 3320 3321
  } else if (!clean_info.is_valid() || !clean_tenant.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("mark_extern_backup_infos_deleted get invalid argument", K(ret), K(clean_info), K(clean_tenant));
O
oceanbase-admin 已提交
3322
  } else {
M
mw0 已提交
3323
    const ObIArray<ObBackupDataCleanElement> &backup_element_array = clean_tenant.backup_element_array_;
3324
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_element_array.count(); ++i) {
M
mw0 已提交
3325
      const ObBackupDataCleanElement &clean_element = backup_element_array.at(i);
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340
      if (OB_FAIL(get_need_delete_backup_set_ids(clean_element, backup_set_ids))) {
        LOG_WARN("failed to get need delete backup set ids", K(ret), K(clean_info));
      } else if (OB_FAIL(get_need_delete_clog_round_and_piece(
                     clean_info, clean_element, log_archive_rounds, backup_piece_keys))) {
        LOG_WARN("failed to get need delete clog round and piece", K(ret), K(clean_info));
      } else if (OB_FAIL(delete_extern_tmp_files(clean_info, clean_element))) {
        LOG_WARN("failed to delete extern tmp files", K(ret), K(clean_info));
      } else if (OB_FAIL(delete_extern_backup_info_deleted(clean_info, clean_element, backup_set_ids))) {
        LOG_WARN("failed to makr extern backup info deleted", K(ret), K(clean_info), K(clean_element));
      } else if (OB_FAIL(delete_extern_clog_info_deleted(clean_info, clean_element, log_archive_rounds))) {
        LOG_WARN("failed to mark extern clog info deleted", K(ret), K(clean_info), K(clean_element));
      } else if (OB_FAIL(mark_extern_backup_set_file_info_deleted(clean_info, clean_element, backup_set_ids))) {
        LOG_WARN("failed to mark extern backup set file info deleted", K(ret), K(clean_info));
      } else if (OB_FAIL(mark_extern_backup_piece_file_info_deleted(clean_info, clean_element, backup_piece_keys))) {
        LOG_WARN("failed to mark extern backup piece file info deleted", K(ret), K(clean_info));
O
oceanbase-admin 已提交
3341 3342 3343 3344 3345 3346
      }
    }
  }
  return ret;
}

M
mw0 已提交
3347 3348
int ObBackupDataClean::delete_extern_backup_info_deleted(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, const common::ObIArray<ObBackupSetId> &backup_set_ids)
O
oceanbase-admin 已提交
3349 3350
{
  int ret = OB_SUCCESS;
3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361
  ObExternBackupInfoMgr extern_backup_info_mgr;
  ObClusterBackupDest current_backup_dest;
  ObArray<int64_t> set_ids;
  const bool is_backup_backup = clean_info.copy_id_ > 0 || clean_info.is_delete_obsolete_backup_backup();
  ObClusterBackupDest conf_backup_dest;
  ObClusterBackupDest conf_backup_backup_dest;
  const bool is_update_timestamp = clean_element.backup_dest_option_.auto_touch_reserved_backup_;
  bool can_delete_file = false;
  bool is_empty = false;
  ObBackupPath path;

O
oceanbase-admin 已提交
3362 3363 3364
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
3365 3366 3367 3368 3369 3370 3371 3372 3373 3374
  } else if (OB_FAIL(current_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
    LOG_WARN("failed to set backup dest", K(ret), K(clean_element));
  } else if (backup_dest_.is_valid() && OB_FAIL(conf_backup_dest.set(backup_dest_, clean_element.incarnation_))) {
    LOG_WARN("failed to set cluster backup dest", K(ret), K(backup_dest_));
  } else if (is_backup_backup) {
    if (OB_FAIL(backup_backup_dest_.is_valid() &&
                conf_backup_backup_dest.set(backup_backup_dest_, clean_element.incarnation_))) {
      LOG_WARN("failed to set original backup dest", K(ret), K(backup_backup_dest_));
    }
  }
O
oceanbase-admin 已提交
3375

3376 3377
  if (OB_SUCC(ret)) {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_ids.count(); ++i) {
M
mw0 已提交
3378
      const ObBackupSetId &backup_set_id = backup_set_ids.at(i);
3379 3380 3381 3382 3383
      if (ObBackupDataCleanMode::CLEAN != backup_set_id.clean_mode_) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("backup set clean mode is unexpected", K(ret), K(backup_set_id));
      } else if (OB_FAIL(set_ids.push_back(backup_set_id.backup_set_id_))) {
        LOG_WARN("failed to push set id into array", K(ret), K(backup_set_id));
O
oceanbase-admin 已提交
3384 3385 3386
      }
    }
  }
3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417

  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(extern_backup_info_mgr.init(clean_info.tenant_id_, current_backup_dest, *backup_lease_service_))) {
    LOG_WARN("failed to init extern backup info mgr", K(ret), K(current_backup_dest), K(clean_info));
  } else if (OB_FAIL(extern_backup_info_mgr.delete_marked_backup_info(set_ids, is_empty))) {
    LOG_WARN("failed to delete marked backup info", K(ret), K(clean_element));
  } else if (!is_empty) {
    can_delete_file = false;
  } else if (!conf_backup_dest.is_valid()) {
    can_delete_file = true;
  } else if (OB_FAIL(extern_backup_info_mgr.get_backup_path(conf_backup_dest, path))) {
    LOG_WARN("failed to get backup path", K(ret), K(conf_backup_dest));
  } else if (OB_FAIL(check_can_delete_extern_info_file(
                 clean_info.tenant_id_, current_backup_dest, is_backup_backup, path, can_delete_file))) {
    LOG_WARN("failed to check can delete extern info file", K(ret), K(clean_info));
  }

  if (OB_FAIL(ret)) {
  } else if (!can_delete_file) {
    if (!is_update_timestamp) {
      // do nothing
    } else if (OB_FAIL(extern_backup_info_mgr.update_backup_info_file_timestamp())) {
      LOG_WARN("failed to update backup info file timestamp", K(ret));
    }
  } else {
    if (is_update_timestamp) {
      // do nothing
    } else if (OB_FAIL(extern_backup_info_mgr.delete_backup_info_file())) {
      LOG_WARN("failed to delete backup info file", K(ret));
    }
  }
O
oceanbase-admin 已提交
3418 3419 3420
  return ret;
}

M
mw0 已提交
3421 3422
int ObBackupDataClean::delete_extern_clog_info_deleted(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, const ObIArray<ObLogArchiveRound> &log_archive_rounds)
O
oceanbase-admin 已提交
3423 3424
{
  int ret = OB_SUCCESS;
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
  ObLogArchiveBackupInfoMgr mgr;
  ObClusterBackupDest current_backup_dest;
  ObArray<int64_t> round_ids;
  const bool is_backup_backup = clean_info.copy_id_ > 0 || clean_info.is_delete_obsolete_backup_backup();
  ObClusterBackupDest conf_backup_dest;
  ObClusterBackupDest conf_backup_backup_dest;
  const int64_t tenant_id = clean_info.tenant_id_;
  const bool is_update_timestamp = clean_element.backup_dest_option_.auto_touch_reserved_backup_;
  bool is_empty = false;
  bool can_delete_file = false;
  ObBackupPath path;
O
oceanbase-admin 已提交
3436 3437 3438 3439

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
3440 3441
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_rounds.count(); ++i) {
M
mw0 已提交
3442
      const ObLogArchiveRound &log_archive_round = log_archive_rounds.at(i);
3443 3444 3445
      if (OB_FAIL(round_ids.push_back(log_archive_round.log_archive_round_))) {
        LOG_WARN("failed to push log archive round id into array", K(ret), K(log_archive_round));
      }
O
oceanbase-admin 已提交
3446 3447 3448 3449
    }
  }

  if (OB_FAIL(ret)) {
3450 3451 3452 3453 3454 3455 3456 3457
  } else if (OB_FAIL(current_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
    LOG_WARN("failed to set backup dest", K(ret), K(clean_element));
  } else if (backup_dest_.is_valid() && OB_FAIL(conf_backup_dest.set(backup_dest_, clean_element.incarnation_))) {
    LOG_WARN("failed to set cluster backup dest", K(ret), K(backup_dest_));
  } else if (is_backup_backup) {
    if (backup_backup_dest_.is_valid() &&
        OB_FAIL(conf_backup_backup_dest.set(backup_backup_dest_, clean_element.incarnation_))) {
      LOG_WARN("failed to set original backup dest", K(ret), K(backup_backup_dest_));
O
oceanbase-admin 已提交
3458 3459 3460
    }
  }

3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481
  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(mgr.delete_marked_extern_log_archive_backup_info(
                 current_backup_dest, tenant_id, round_ids, is_empty, *backup_lease_service_))) {
    LOG_WARN("failed to delete marked extern log archive backup info", K(current_backup_dest), K(clean_info));
  } else if (!is_empty) {
    can_delete_file = false;
  } else if (!conf_backup_dest.is_valid()) {
    can_delete_file = true;
  } else if (OB_FAIL(mgr.get_extern_backup_info_path(conf_backup_dest, clean_info.tenant_id_, path))) {
    LOG_WARN("failed to get backup path", K(ret), K(conf_backup_dest));
  } else if (OB_FAIL(check_can_delete_extern_info_file(
                 clean_info.tenant_id_, current_backup_dest, is_backup_backup, path, can_delete_file))) {
    LOG_WARN("failed to check can delete extern info file", K(ret), K(clean_info));
  }

  if (OB_FAIL(ret)) {
  } else if (!can_delete_file) {
    if (!is_update_timestamp) {
      // do nothing
    } else if (OB_FAIL(mgr.update_extern_backup_info_file_timestamp(current_backup_dest, tenant_id))) {
      LOG_WARN("failed to update backup info file timestamp", K(ret));
O
oceanbase-admin 已提交
3482 3483
    }
  } else {
3484 3485 3486 3487
    if (is_update_timestamp) {
      // do nothing
    } else if (OB_FAIL(mgr.delete_extern_backup_info_file(current_backup_dest, tenant_id))) {
      LOG_WARN("failed to delete backup info file", K(ret));
O
oceanbase-admin 已提交
3488 3489 3490 3491 3492
    }
  }
  return ret;
}

3493
int ObBackupDataClean::delete_extern_tmp_files(
M
mw0 已提交
3494
    const share::ObBackupCleanInfo &clean_info, const ObBackupDataCleanElement &clean_element)
O
oceanbase-admin 已提交
3495 3496
{
  int ret = OB_SUCCESS;
3497 3498 3499 3500
  ObClusterBackupDest cluster_backup_dest;
  ObBackupPath cluster_prefix_path;
  ObBackupPath data_prefix_path;
  ObBackupPath clog_prefix_path;
O
oceanbase-admin 已提交
3501 3502 3503
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
3504 3505 3506 3507 3508 3509 3510 3511 3512
  } else if (OB_FAIL(cluster_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
    LOG_WARN("failed to set backup dest", K(ret), K(clean_element));
  } else if (OB_SYS_TENANT_ID == clean_info.tenant_id_) {
    if (OB_FAIL(ObBackupPathUtil::get_cluster_prefix_path(cluster_backup_dest, cluster_prefix_path))) {
      LOG_WARN("failed to get cluster prefix path", K(ret), K(cluster_backup_dest), K(clean_info));
    } else if (OB_FAIL(ObBackupDataCleanUtil::delete_tmp_files(
                   cluster_prefix_path, cluster_backup_dest.get_storage_info()))) {
      LOG_WARN("failed to delete tmp files", K(ret), K(cluster_prefix_path));
    }
O
oceanbase-admin 已提交
3513
  } else {
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525
    if (OB_FAIL(ObBackupPathUtil::get_tenant_backup_data_path(
            cluster_backup_dest, clean_info.tenant_id_, data_prefix_path))) {
      LOG_WARN("failed to get tenant prefix path", K(ret), K(cluster_backup_dest), K(clean_info));
    } else if (OB_FAIL(ObBackupPathUtil::get_tenant_clog_path(
                   cluster_backup_dest, clean_info.tenant_id_, clog_prefix_path))) {
      LOG_WARN("failed to get tenant prefix path", K(ret), K(cluster_backup_dest), K(clean_info));
    } else if (OB_FAIL(
                   ObBackupDataCleanUtil::delete_tmp_files(data_prefix_path, cluster_backup_dest.get_storage_info()))) {
      LOG_WARN("failed to delete tmp files", K(ret), K(cluster_prefix_path));
    } else if (OB_FAIL(
                   ObBackupDataCleanUtil::delete_tmp_files(clog_prefix_path, cluster_backup_dest.get_storage_info()))) {
      LOG_WARN("failed to delete tmp files", K(ret), K(cluster_prefix_path));
O
oceanbase-admin 已提交
3526 3527 3528 3529 3530
    }
  }
  return ret;
}

M
mw0 已提交
3531 3532
int ObBackupDataClean::mark_extern_backup_set_file_info_deleted(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, const common::ObIArray<ObBackupSetId> &backup_set_ids)
O
oceanbase-admin 已提交
3533 3534
{
  int ret = OB_SUCCESS;
3535 3536 3537 3538 3539 3540
  ObExternBackupSetFileInfoMgr extern_backup_set_file_info_mgr;
  ObClusterBackupDest current_backup_dest;
  const bool is_backup_backup = clean_info.copy_id_ > 0 || clean_info.is_delete_obsolete_backup_backup();
  ObArray<ObBackupSetIdPair> backup_set_id_pairs;
  ObBackupPath path;
  ObBackupSetIdPair backup_set_id_pair;
3541 3542 3543
  bool src_backup_dest_unknown = true;
  bool is_need_modify = false;  // the status of the external file at the source destination of the backup backup
  ObBackupSetFileInfo backup_set_file_info;
O
oceanbase-admin 已提交
3544 3545 3546 3547

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
3548 3549
  } else if (OB_FAIL(current_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
    LOG_WARN("failed to set backup dest", K(ret), K(clean_element));
3550
  } else {
3551
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_ids.count(); ++i) {
M
mw0 已提交
3552
      const ObBackupSetId &backup_set_id = backup_set_ids.at(i);
3553 3554
      backup_set_id_pair.reset();
      if (ObBackupDataCleanMode::CLEAN != backup_set_id.clean_mode_) {
O
oceanbase-admin 已提交
3555
        ret = OB_ERR_UNEXPECTED;
3556
        LOG_WARN("backup set clean mode is unexpected", K(ret), K(backup_set_id));
O
oceanbase-admin 已提交
3557
      } else {
3558 3559 3560 3561
        backup_set_id_pair.backup_set_id_ = backup_set_id.backup_set_id_;
        backup_set_id_pair.copy_id_ = backup_set_id.copy_id_;
        if (OB_FAIL(backup_set_id_pairs.push_back(backup_set_id_pair))) {
          LOG_WARN("failed to push backup set id pair into array", K(ret), K(backup_set_id_pair));
O
oceanbase-admin 已提交
3562 3563
        }
      }
3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585

      if (src_backup_dest_unknown && OB_SUCC(ret) && is_backup_backup) {
        if (OB_FAIL(get_source_backup_set_file_info(clean_info.tenant_id_,
                clean_element.incarnation_,
                backup_set_id,
                backup_set_file_info,
                is_need_modify))) {
          LOG_WARN("failed to get source backup set file info", K(ret), K(backup_set_id));
        } else {
          src_backup_dest_unknown = false;
        }
      }
    }
  }

  if (OB_SUCC(ret) && is_backup_backup && is_need_modify) {
    if (OB_FAIL(mark_extern_source_backup_set_info_of_backup_backup(clean_info.tenant_id_,
            clean_element.incarnation_,
            backup_set_file_info,
            backup_set_id_pairs,
            false /* is_deleting*/))) {
      LOG_WARN("failed to mark source backup set file info deleted", K(ret), K(clean_info));
O
oceanbase-admin 已提交
3586 3587
    }
  }
3588 3589 3590 3591 3592

  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(extern_backup_set_file_info_mgr.init(
                 clean_info.tenant_id_, current_backup_dest, is_backup_backup, *backup_lease_service_))) {
    LOG_WARN("failed to init extern backup info mgr", K(ret), K(current_backup_dest), K(clean_info));
3593
  } else if (OB_FAIL(extern_backup_set_file_info_mgr.mark_backup_set_file_deleted(backup_set_id_pairs))) {
3594 3595
    LOG_WARN("failed to delete marked backup info", K(ret), K(clean_element));
  }
O
oceanbase-admin 已提交
3596 3597 3598
  return ret;
}

M
mw0 已提交
3599 3600
int ObBackupDataClean::mark_extern_backup_piece_file_info_deleted(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, const common::ObIArray<ObBackupPieceInfoKey> &backup_piece_keys)
O
oceanbase-admin 已提交
3601 3602
{
  int ret = OB_SUCCESS;
3603 3604
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
  ObClusterBackupDest current_backup_dest;
3605
  ObClusterBackupDest src_cluster_backup_dest;
3606
  const bool is_backup_backup = clean_info.copy_id_ > 0 || clean_info.is_delete_obsolete_backup_backup();
3607
  bool is_need_modify = false;  // the status of the external file at the source destination of the backup backup
O
oceanbase-admin 已提交
3608 3609 3610 3611

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
3612 3613 3614
  } else if (OB_FAIL(current_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
    LOG_WARN("failed to set backup dest", K(ret), K(clean_element));
  } else if (is_backup_backup) {
3615 3616 3617 3618 3619 3620 3621 3622
    if (OB_FAIL(get_source_backup_dest_from_piece_file(backup_piece_keys, src_cluster_backup_dest, is_need_modify))) {
      LOG_WARN("failed to get source backup piece file info", K(ret));
    } else if (is_need_modify && OB_FAIL(log_archive_info_mgr.mark_extern_backup_piece_deleted(src_cluster_backup_dest,
                                     clean_info.tenant_id_,
                                     backup_piece_keys,
                                     is_backup_backup,
                                     *backup_lease_service_))) {
      LOG_WARN("failed to mark source extern backup piece deleted", K(ret), K(clean_info), K(src_cluster_backup_dest));
3623 3624
    }
  }
O
oceanbase-admin 已提交
3625

3626 3627 3628
  if (OB_SUCC(ret) &&
      OB_FAIL(log_archive_info_mgr.mark_extern_backup_piece_deleted(
          current_backup_dest, clean_info.tenant_id_, backup_piece_keys, is_backup_backup, *backup_lease_service_))) {
3629 3630
    LOG_WARN("failed to mark extern backup piece deleted", K(ret), K(clean_info), K(current_backup_dest));
  }
O
oceanbase-admin 已提交
3631 3632 3633 3634

  return ret;
}

3635
int ObBackupDataClean::delete_inner_table_his_data(
M
mw0 已提交
3636
    const ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant, common::ObISQLClient &trans)
O
oceanbase-admin 已提交
3637 3638 3639 3640 3641 3642
{
  int ret = OB_SUCCESS;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
3643 3644
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_tenant.backup_element_array_.count(); ++i) {
M
mw0 已提交
3645
      const ObBackupDataCleanElement &clean_element = clean_tenant.backup_element_array_.at(i);
3646 3647 3648 3649 3650 3651 3652 3653 3654 3655
      if (OB_FAIL(delete_marked_backup_task_his_data(clean_info, clean_element, trans))) {
        LOG_WARN("failed to delete marked backup task his data", K(ret), K(clean_info));
      } else if (OB_FAIL(delete_marked_log_archive_status_his_data(clean_info, clean_element, trans))) {
        LOG_WARN("failed to delete marked log archive stauts his data", K(ret), K(clean_info));
      }
    }
  }
  return ret;
}

M
mw0 已提交
3656 3657
int ObBackupDataClean::delete_marked_backup_task_his_data(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, common::ObISQLClient &trans)
3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
{
  int ret = OB_SUCCESS;
  ObArray<ObBackupSetId> backup_set_ids;
  ObBackupTaskHistoryUpdater updater;
  const int64_t tenant_id = clean_info.tenant_id_;
  const bool for_update = true;
  ObArray<ObTenantBackupTaskInfo> tenant_backup_tasks;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(get_need_delete_backup_set_ids(clean_element, backup_set_ids))) {
    LOG_WARN("failed to get need delete backup set ids", K(ret), K(clean_info));
  } else if (OB_FAIL(updater.init(trans))) {
    LOG_WARN("faile to init backup task history updater", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_ids.count(); ++i) {
      tenant_backup_tasks.reset();
M
mw0 已提交
3676
      const ObBackupSetId &backup_set_id = backup_set_ids.at(i);
3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707
      ObTenantBackupTaskInfo backup_task_info;
      if (ObBackupDataCleanMode::CLEAN != backup_set_id.clean_mode_) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("backup set id clean mode is unexpected", K(ret), K(backup_set_id));
      } else if (OB_FAIL(updater.get_tenant_backup_task(tenant_id,
                     backup_set_id.backup_set_id_,
                     backup_set_id.copy_id_,
                     for_update,
                     tenant_backup_tasks))) {
        LOG_WARN("failed to get tenant backup task", K(ret), K(tenant_id), K(backup_set_id));
      } else if (tenant_backup_tasks.empty()) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("tenant backup tasks should not be empty", K(ret), K(backup_set_id), K(clean_info));
      } else if (FALSE_IT(backup_task_info = tenant_backup_tasks.at(tenant_backup_tasks.count() - 1))) {
        // 1.Delete backupset history table only has one task info
        // 2.Delete backup backupset history table may has multi task infos, but last info's status is SUCCESS OR FAILED
        // We use THE LAST task info to check should delete. We use backup set file info's file status to mutex
        // clean thread and backup backup thread
      } else if (OB_FAIL(ObBackupTaskCleanHistoryOpertor::insert_task_info(
                     clean_info.job_id_, backup_set_id.copy_id_, backup_task_info, trans))) {
        LOG_WARN("failed to insert task into clean history", K(ret), K(backup_task_info));
      } else if (OB_FAIL(
                     updater.delete_backup_set_info(tenant_id, backup_set_id.backup_set_id_, backup_set_id.copy_id_))) {
        LOG_WARN("failed to delete backup set info", K(ret));
      }
    }
  }
  return ret;
}

int ObBackupDataClean::inner_delete_marked_backup_backup_task_his_data(const uint64_t tenant_id, const int64_t job_id,
M
mw0 已提交
3708
    const int64_t copy_id, const int64_t backup_set_id, common::ObISQLClient &trans)
3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731
{
  int ret = OB_SUCCESS;
  ObTenantBackupTaskInfo tenant_backup_task;
  ObTenantBackupBackupsetTaskInfo backup_backupset_task;
  ObArray<ObTenantBackupBackupsetTaskInfo> task_list;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(ObTenantBackupBackupsetHistoryOperator::get_task_item(
                 tenant_id, job_id, backup_set_id, copy_id, task_list, trans))) {
    LOG_WARN("failed to get task item", K(ret), K(backup_set_id), K(copy_id));
  } else if (FALSE_IT(backup_backupset_task = task_list.at(0))) {
    // TODO(muwei.ym): deal with array list
  } else if (OB_FAIL(backup_backupset_task.convert_to_backup_task_info(tenant_backup_task))) {
    LOG_WARN("failed to convert to backup task info", K(ret), K(backup_backupset_task));
  } else if (OB_FAIL(ObBackupTaskCleanHistoryOpertor::insert_task_info(job_id, copy_id, tenant_backup_task, trans))) {
    LOG_WARN("failed to insert task into clean history", K(ret), K(tenant_backup_task));
  } else if (OB_FAIL(ObTenantBackupBackupsetHistoryOperator::delete_task_item(backup_backupset_task, trans))) {
    LOG_WARN("failed to delete task item", KR(ret), K(tenant_id), K(job_id));
  }
  return ret;
}

M
mw0 已提交
3732 3733
int ObBackupDataClean::delete_marked_log_archive_status_his_data(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanElement &clean_element, common::ObISQLClient &trans)
3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750
{
  int ret = OB_SUCCESS;
  ObArray<ObLogArchiveRound> log_archive_rounds;
  ObArray<ObBackupPieceInfoKey> backup_piece_keys;
  ObLogArchiveBackupInfo log_archive_info;
  ObBackupPieceInfo backup_piece_info;
  const bool for_update = true;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(get_need_delete_clog_round_and_piece(
                 clean_info, clean_element, log_archive_rounds, backup_piece_keys))) {
    LOG_WARN("failed to get need delete clog round and piece", K(ret), K(clean_info));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_rounds.count(); ++i) {
      log_archive_info.reset();
      ObLogArchiveBackupInfoMgr log_archive_backup_info_mgr;
M
mw0 已提交
3751
      const ObLogArchiveRound &log_archive_round = log_archive_rounds.at(i);
3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773
      if (log_archive_round.copy_id_ > 0 && OB_FAIL(log_archive_backup_info_mgr.set_backup_backup())) {
        LOG_WARN("failed to set copy id", K(ret), K(log_archive_round));
      } else if (OB_FAIL(log_archive_backup_info_mgr.get_log_archive_history_info(trans,
                     clean_info.tenant_id_,
                     log_archive_round.log_archive_round_,
                     log_archive_round.copy_id_,
                     for_update,
                     log_archive_info))) {
        if (OB_ENTRY_NOT_EXIST == ret && log_archive_round.copy_id_ > 0) {
          // backup backuplog may not generate backup round info, skip it
          ret = OB_SUCCESS;
        } else {
          LOG_WARN("failed to get log archive history info", K(ret), K(clean_info), K(log_archive_round));
        }
      } else if (OB_FAIL(log_archive_backup_info_mgr.delete_log_archive_info(log_archive_info, trans))) {
        LOG_WARN("failed to delete clog archive info", K(ret), K(clean_info));
      }
    }

    for (int64_t i = 0; OB_SUCC(ret) && i < backup_piece_keys.count(); ++i) {
      backup_piece_info.reset();
      ObLogArchiveBackupInfoMgr log_archive_backup_info_mgr;
M
mw0 已提交
3774
      const ObBackupPieceInfoKey &piece_info_key = backup_piece_keys.at(i);
3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788
      if (piece_info_key.copy_id_ > 0 && OB_FAIL(log_archive_backup_info_mgr.set_backup_backup())) {
        LOG_WARN("failed to set copy id", K(ret), K(piece_info_key));
      } else if (OB_FAIL(log_archive_backup_info_mgr.get_backup_piece(
                     trans, for_update, piece_info_key, backup_piece_info))) {
        LOG_WARN("failed to get backup piece", K(ret), K(piece_info_key));
      } else if (FALSE_IT(backup_piece_info.file_status_ = ObBackupFileStatus::BACKUP_FILE_DELETED)) {
      } else if (OB_FAIL(log_archive_backup_info_mgr.update_backup_piece(trans, backup_piece_info))) {
        LOG_WARN("failed to update backup piece", K(ret), K(backup_piece_info));
      }
    }
  }
  return ret;
}

M
mw0 已提交
3789 3790
int ObBackupDataClean::update_clean_info(const uint64_t tenant_id, const share::ObBackupCleanInfo &src_clean_info,
    const share::ObBackupCleanInfo &dest_clean_info)
3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(update_clean_info(tenant_id, src_clean_info, dest_clean_info, *sql_proxy_))) {
    LOG_WARN("failed to update clean info", K(ret), K(src_clean_info), K(dest_clean_info));
  }
  return ret;
}

M
mw0 已提交
3802 3803
int ObBackupDataClean::update_clean_info(const uint64_t tenant_id, const share::ObBackupCleanInfo &src_clean_info,
    const share::ObBackupCleanInfo &dest_clean_info, common::ObISQLClient &trans)
3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853
{
  int ret = OB_SUCCESS;
  ObTenantBackupCleanInfoUpdater updater;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!src_clean_info.is_valid() || !dest_clean_info.is_valid() || OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("update clean info get invalid argument", K(ret), K(src_clean_info), K(dest_clean_info), K(tenant_id));
  } else if (OB_FAIL(updater.init(trans))) {
    LOG_WARN("failed to init tenant backup clean info updater", K(ret), K(src_clean_info));
  } else if (OB_FAIL(updater.update_backup_clean_info(tenant_id, src_clean_info, dest_clean_info))) {
    LOG_WARN("failed to update backup clean info", K(ret), K(src_clean_info), K(dest_clean_info));
  }
  return ret;
}

void ObBackupDataClean::cleanup_prepared_infos()
{
  int ret = OB_SUCCESS;
  ObArray<uint64_t> tenant_ids;
  bool need_clean = false;
  const int64_t EXECUTE_TIMEOUT_US = 100L * 100 * 1000;      // 100ms
  const int64_t MIN_EXECUTE_TIMEOUT_US = 30L * 1000 * 1000;  // 30s
  ObTimeoutCtx timeout_ctx;
  int64_t stmt_timeout = EXECUTE_TIMEOUT_US;
  int64_t calc_stmt_timeout = EXECUTE_TIMEOUT_US;
  ObBackupCleanInfo sys_clean_info;
  ObBackupCleanInfo dest_sys_clean_info;
  ObMySQLTransaction trans;
  ObArray<ObSimpleBackupDataCleanTenant> normal_clean_tenants;
  const bool for_update = true;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("root backup do not init", K(ret));
  } else if (OB_FAIL(get_all_tenant_ids(tenant_ids))) {
    LOG_WARN("failed to get all tenant ids", K(ret));
  } else if (tenant_ids.empty()) {
    // do nothing
  } else if (OB_SYS_TENANT_ID != tenant_ids.at(0)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("first tenant id should be sys tenant", K(ret), K(tenant_ids));
  } else if (FALSE_IT(calc_stmt_timeout = tenant_ids.count() * EXECUTE_TIMEOUT_US)) {
  } else if (FALSE_IT(stmt_timeout =
                          (calc_stmt_timeout > MIN_EXECUTE_TIMEOUT_US ? calc_stmt_timeout : MIN_EXECUTE_TIMEOUT_US))) {
  } else if (OB_FAIL(timeout_ctx.set_trx_timeout_us(stmt_timeout))) {
    LOG_WARN("failed to set trx timeout us", K(ret), K(stmt_timeout));
  } else if (OB_FAIL(timeout_ctx.set_timeout(stmt_timeout))) {
    LOG_WARN("set timeout context failed", K(ret), K(stmt_timeout));
  } else if (OB_FAIL(trans.start(sql_proxy_))) {
O
oceanbase-admin 已提交
3854 3855
    LOG_WARN("fail to start trans", K(ret));
  } else {
3856 3857 3858 3859 3860
    sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, trans, sys_clean_info))) {
      if (OB_BACKUP_CLEAN_INFO_NOT_EXIST == ret) {
        need_clean = false;
        ret = OB_SUCCESS;
O
oceanbase-admin 已提交
3861
      } else {
3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914
        LOG_WARN("faield to get backup clean info", K(ret));
      }
    } else if (OB_FAIL(check_need_cleanup_prepared_infos(sys_clean_info, need_clean))) {
      LOG_WARN("failed to check need clean prepared infos", K(ret));
    } else if (!need_clean) {
      // do nothing
    } else {
      // normal tenants, skip sys tenant
      for (int64_t i = 0; OB_SUCC(ret) && i < tenant_ids.count(); ++i) {
        const uint64_t tenant_id = tenant_ids.at(i);
        if (OB_SYS_TENANT_ID == tenant_id) {
          // do nothing
        } else if (OB_FAIL(cleanup_tenant_prepared_infos(tenant_id, trans))) {
          LOG_WARN("failed to cleanup stoppped tenant infos", K(ret), K(tenant_id));
        }
      }

      for (int64_t i = 0; OB_SUCC(ret) && i < tenant_ids.count(); ++i) {
        const uint64_t tenant_id = tenant_ids.at(i);
        if (OB_SYS_TENANT_ID == tenant_id) {
          // do nothing
        } else {
          ObSimpleBackupDataCleanTenant simple_clean_tenant;
          simple_clean_tenant.tenant_id_ = tenant_id;
          simple_clean_tenant.is_deleted_ = false;
          if (OB_FAIL(normal_clean_tenants.push_back(simple_clean_tenant))) {
            LOG_WARN("failed to push simple clean tenant into array", K(ret), K(simple_clean_tenant));
          }
        }
      }

      if (OB_SUCC(ret)) {
        if (OB_FAIL(insert_clean_infos_into_history(normal_clean_tenants, trans))) {
          LOG_WARN("failed to insert clean infos into history", K(ret));
        } else if (OB_FAIL(reset_backup_clean_infos(normal_clean_tenants, trans))) {
          LOG_WARN("failed to reset backup clean infos", K(ret));
        } else {
          // result
          if (OB_SUCCESS != sys_clean_info.result_) {
            // do nothing
          } else if (OB_SUCCESS != inner_error_) {
            sys_clean_info.result_ = inner_error_;
            LOG_INFO("set sys clean info result failed", K(sys_clean_info), K(inner_error_));
          } else {
            sys_clean_info.result_ = OB_CANCELED;
            LOG_INFO("set sys clean info result cancel", K(ret), K(sys_clean_info));
          }
        }

        if (OB_FAIL(ret)) {
        } else if (OB_FAIL(set_sys_clean_info_stop(sys_clean_info, trans))) {
          LOG_WARN("failed to set sys clean info stop", K(ret), K(sys_clean_info));
        }
O
oceanbase-admin 已提交
3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(trans.end(true /*commit*/))) {
        OB_LOG(WARN, "failed to commit", K(ret));
      }
    } else {
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = trans.end(false /* commit*/))) {
        OB_LOG(WARN, "failed to rollback trans", K(tmp_ret));
      }
    }
3928 3929 3930
  }
}

M
mw0 已提交
3931
int ObBackupDataClean::check_need_cleanup_prepared_infos(const ObBackupCleanInfo &sys_clean_info, bool &need_clean)
3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947
{
  int ret = OB_SUCCESS;
  need_clean = false;
  bool is_prepare_flag = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data cleando not init", K(ret));
  } else if (FALSE_IT(is_prepare_flag = get_prepare_flag())) {
  } else if (ObBackupCleanInfoStatus::PREPARE == sys_clean_info.status_ && !is_prepare_flag) {
    need_clean = true;
    LOG_INFO("sys tenant is in prepare status, need cleanup", K(ret), K(sys_clean_info), K(is_prepare_flag));
  }
  return ret;
}

M
mw0 已提交
3948
int ObBackupDataClean::cleanup_tenant_prepared_infos(const uint64_t tenant_id, ObISQLClient &sys_tenant_trans)
3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010
{
  int ret = OB_SUCCESS;
  const int64_t EXECUTE_TIMEOUT_US = 30L * 1000 * 1000;  // 30s
  ObTimeoutCtx timeout_ctx;
  int64_t stmt_timeout = EXECUTE_TIMEOUT_US;
  ObBackupCleanInfo clean_info;
  ObBackupCleanInfo dest_clean_info;
  ObMySQLTransaction trans;
  const bool for_update = true;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (stop_) {
    ret = OB_RS_SHUTDOWN;
    LOG_WARN("rootservice shutdown", K(ret));
  } else if (OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("tenant is is invalid", K(ret), K(tenant_id));
  } else if (OB_FAIL(timeout_ctx.set_trx_timeout_us(stmt_timeout))) {
    LOG_WARN("failed to set trx timeout", K(ret), K(stmt_timeout));
  } else if (OB_FAIL(timeout_ctx.set_timeout(stmt_timeout))) {
    LOG_WARN("set timeout context failed", K(ret));
  } else if (OB_FAIL(trans.start(sql_proxy_))) {
    LOG_WARN("fail to start trans", K(ret));
  } else {
    clean_info.tenant_id_ = tenant_id;
    if (OB_FAIL(get_backup_clean_info(tenant_id, for_update, trans, clean_info))) {
      LOG_WARN("failed to get backup clean info", K(ret), K(tenant_id));
    } else if (ObBackupCleanInfoStatus::STOP != clean_info.status_ &&
               ObBackupCleanInfoStatus::DOING != clean_info.status_) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("info status is unexpected", K(ret), K(clean_info));
    } else if (ObBackupCleanInfoStatus::STOP == clean_info.status_) {
      // do nothing
    } else {
      dest_clean_info = clean_info;
      dest_clean_info.result_ = OB_CANCELED;
      dest_clean_info.status_ = ObBackupCleanInfoStatus::STOP;
      if (OB_FAIL(dest_clean_info.error_msg_.assign(ob_strerror(OB_CANCELED)))) {
        LOG_WARN("failed to assign error msg", K(ret), K(dest_clean_info));
      } else if (OB_FAIL(ObBackupUtil::check_sys_clean_info_trans_alive(sys_tenant_trans))) {
        LOG_WARN("failed to check sys tenant trans alive", K(ret), K(clean_info));
      } else if (OB_FAIL(update_clean_info(clean_info.tenant_id_, clean_info, dest_clean_info, trans))) {
        LOG_WARN("failed to update tenant backup info", K(ret), K(clean_info), K(dest_clean_info));
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(trans.end(true /*commit*/))) {
        OB_LOG(WARN, "failed to commit", K(ret));
      }
    } else {
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = trans.end(false /* commit*/))) {
        OB_LOG(WARN, "failed to rollback trans", K(tmp_ret));
      }
    }
  }
  return ret;
}

int ObBackupDataClean::insert_tenant_backup_clean_info_history(
M
mw0 已提交
4011
    const ObSimpleBackupDataCleanTenant &simple_clean_tenant, common::ObISQLClient &sys_trans)
4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfo clean_info;
  clean_info.tenant_id_ = simple_clean_tenant.tenant_id_;
  ObBackupCleanInfoHistoryUpdater updater;
  const bool for_update = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (simple_clean_tenant.is_deleted_) {
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, sys_trans, clean_info))) {
      LOG_WARN("failed to get backup data clean info", K(ret));
    }
  } else {
    if (OB_FAIL(get_backup_clean_info(simple_clean_tenant.tenant_id_, for_update, *sql_proxy_, clean_info))) {
      LOG_WARN("failed to get backup data clean info", K(ret));
    }
  }

  if (OB_FAIL(ret)) {
  } else if (ObBackupCleanInfoStatus::STOP != clean_info.status_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("clean info status is unexpected, can not insert into task", K(ret), K(clean_info));
  } else if (clean_info.is_empty_clean_type()) {
    // do nothing
  } else if (OB_FAIL(updater.init(sys_trans))) {
    LOG_WARN("failed to init clean info history updater", K(ret));
  } else if (OB_FAIL(updater.insert_backup_clean_info(clean_info))) {
    LOG_WARN("failed to insert backup clean info into history", K(ret), K(clean_info));
  }
  return ret;
}

int ObBackupDataClean::do_with_failed_tenant_clean_task(
M
mw0 已提交
4047
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants, int32_t &clean_result)
4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094
{
  int ret = OB_SUCCESS;
  ObMySQLTransaction trans;
  ObBackupCleanInfo sys_clean_info;
  sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
  clean_result = OB_SUCCESS;
  ObTimeoutCtx timeout_ctx;
  const bool for_update = true;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
  } else {
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, trans, sys_clean_info))) {
      LOG_WARN("failed to get backup data clean info", K(ret));
    } else if (OB_FAIL(
                   check_tenant_backup_clean_task_failed(normal_clean_tenants, sys_clean_info, trans, clean_result))) {
      LOG_WARN("failed to check tenant backup clean task failed", K(ret), K(sys_clean_info));
    } else if (OB_FAIL(update_tenant_backup_clean_task_failed(normal_clean_tenants, trans, clean_result))) {
      LOG_WARN("failed to update tenant backup clean task failed", K(ret));
    } else if (OB_SUCCESS == clean_result) {
      // do nothing
    } else if (OB_SUCCESS == sys_clean_info.result_) {
      ObBackupCleanInfo dest_clean_info = sys_clean_info;
      dest_clean_info.result_ = clean_result;
      if (OB_FAIL(update_clean_info(OB_SYS_TENANT_ID, sys_clean_info, dest_clean_info, trans))) {
        LOG_WARN("failed to update clean info", K(ret), K(sys_clean_info), K(dest_clean_info));
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
      }
    } else {
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
      }
    }
  }
  return ret;
}

int ObBackupDataClean::check_tenant_backup_clean_task_failed(
M
mw0 已提交
4095 4096
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants,
    const ObBackupCleanInfo &sys_clean_info, common::ObISQLClient &sys_tenant_trans, int32_t &result)
4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109
{
  int ret = OB_SUCCESS;
  result = OB_SUCCESS;
  const bool for_update = false;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_SUCCESS != sys_clean_info.result_) {
    result = sys_clean_info.result_;
  } else if (OB_SUCCESS != inner_error_) {
    result = inner_error_;
  } else {
    for (int64_t i = 0; OB_SUCCESS == result && OB_SUCC(ret) && i < normal_clean_tenants.count(); ++i) {
M
mw0 已提交
4110
      const ObSimpleBackupDataCleanTenant &simple_clean_tenant = normal_clean_tenants.at(i);
4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141
      ObBackupCleanInfo tenant_clean_info;
      tenant_clean_info.tenant_id_ = simple_clean_tenant.tenant_id_;
      if (OB_SYS_TENANT_ID == simple_clean_tenant.tenant_id_) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("normal clean tenant should not be sys tenant", K(ret), K(simple_clean_tenant));
      } else if (simple_clean_tenant.is_deleted_) {
        if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, sys_tenant_trans, tenant_clean_info))) {
          LOG_WARN("failed to push tenant clean info into array", K(ret), K(tenant_clean_info));
        } else if (ObBackupCleanInfoStatus::STOP != tenant_clean_info.status_) {
          // do nothing
        } else if (OB_SUCCESS != tenant_clean_info.result_) {
          result = tenant_clean_info.result_;
        }
      } else {
        if (OB_FAIL(
                get_backup_clean_info(simple_clean_tenant.tenant_id_, for_update, *sql_proxy_, tenant_clean_info))) {
          LOG_WARN("failed to push tenant clean info into array", K(ret), K(simple_clean_tenant));
        } else if (OB_FAIL(rootserver::ObBackupUtil::check_sys_clean_info_trans_alive(sys_tenant_trans))) {
          LOG_WARN("failed to check sys tenant trans alive", K(ret), K(tenant_clean_info));
        } else if (ObBackupCleanInfoStatus::STOP != tenant_clean_info.status_) {
          // do nothing
        } else if (OB_SUCCESS != tenant_clean_info.result_) {
          result = tenant_clean_info.result_;
        }
      }
    }
  }
  return ret;
}

int ObBackupDataClean::update_tenant_backup_clean_task_failed(
M
mw0 已提交
4142
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants, common::ObISQLClient &sys_tenant_trans,
4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160
    const int32_t result)
{
  int ret = OB_SUCCESS;
  const int64_t MIN_EXECUTE_TIMEOUT_US = 30L * 1000 * 1000;  // 30s
  ObTimeoutCtx timeout_ctx;
  const int64_t stmt_timeout = MIN_EXECUTE_TIMEOUT_US;
  const bool for_update = true;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_SUCCESS == result) {
    // do nothing
  } else if (OB_FAIL(timeout_ctx.set_trx_timeout_us(stmt_timeout))) {
    LOG_WARN("fail to set trx timeout", K(ret));
  } else if (OB_FAIL(timeout_ctx.set_timeout(stmt_timeout))) {
    LOG_WARN("set timeout context failed", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < normal_clean_tenants.count(); ++i) {
M
mw0 已提交
4161
      const ObSimpleBackupDataCleanTenant &simple_clean_tenant = normal_clean_tenants.at(i);
4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218
      ObBackupCleanInfo tenant_clean_info;
      ObMySQLTransaction trans;
      tenant_clean_info.tenant_id_ = simple_clean_tenant.tenant_id_;
      if (OB_SYS_TENANT_ID == simple_clean_tenant.tenant_id_) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("clean tenant should not be sys clean tenant", K(ret), K(simple_clean_tenant));
      } else if (simple_clean_tenant.is_deleted_) {
        if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, sys_tenant_trans, tenant_clean_info))) {
          LOG_WARN("failed to push tenant clean info into array", K(ret), K(simple_clean_tenant));
        } else if (ObBackupCleanInfoStatus::DOING != tenant_clean_info.status_) {
          // do nothing
        } else {
          ObBackupCleanInfo dest_clean_info = tenant_clean_info;
          dest_clean_info.result_ = result;
          dest_clean_info.status_ = ObBackupCleanInfoStatus::STOP;
          if (OB_FAIL(update_clean_info(OB_SYS_TENANT_ID, tenant_clean_info, dest_clean_info, sys_tenant_trans))) {
            LOG_WARN("failed to update clean info", K(ret), K(tenant_clean_info), K(dest_clean_info));
          }
        }
      } else {
        if (OB_FAIL(trans.start(sql_proxy_))) {
          LOG_WARN("fail to start trans", K(ret));
        } else {
          if (OB_FAIL(get_backup_clean_info(simple_clean_tenant.tenant_id_, for_update, trans, tenant_clean_info))) {
            LOG_WARN("failed to push tenant clean info into array", K(ret), K(simple_clean_tenant));
          } else if (ObBackupCleanInfoStatus::DOING != tenant_clean_info.status_) {
            // do nothing
          } else {
            ObBackupCleanInfo dest_clean_info = tenant_clean_info;
            dest_clean_info.result_ = result;
            dest_clean_info.status_ = ObBackupCleanInfoStatus::STOP;
            if (OB_FAIL(ObBackupUtil::check_sys_clean_info_trans_alive(sys_tenant_trans))) {
              LOG_WARN("failed to check sys tenant trans alive", K(ret), K(tenant_clean_info));
            } else if (OB_FAIL(update_clean_info(
                           simple_clean_tenant.tenant_id_, tenant_clean_info, dest_clean_info, trans))) {
              LOG_WARN("failed to update clean info", K(ret), K(tenant_clean_info), K(dest_clean_info));
            }
          }

          if (OB_SUCC(ret)) {
            if (OB_FAIL(trans.end(true /*commit*/))) {
              OB_LOG(WARN, "failed to commit", K(ret));
            }
          } else {
            int tmp_ret = OB_SUCCESS;
            if (OB_SUCCESS != (tmp_ret = trans.end(false /* commit*/))) {
              OB_LOG(WARN, "failed to rollback trans", K(tmp_ret));
            }
          }
        }
      }
    }
  }
  return ret;
}

int ObBackupDataClean::do_with_finished_tenant_clean_task(
M
mw0 已提交
4219 4220
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants,
    const share::ObBackupCleanInfo &sys_clean_info, const ObBackupDataCleanTenant &sys_clean_tenant,
4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290
    const int32_t clean_result)
{
  int ret = OB_SUCCESS;
  ObTimeoutCtx timeout_ctx;
  ObBackupCleanInfo dest_sys_clean_info;
  ObMySQLTransaction trans;
  ObBackupInfoManager backup_info_manager;
  const bool for_update = true;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_SUCCESS == clean_result) {
    if (OB_FAIL(delete_cluster_backup_meta_data(sys_clean_info, sys_clean_tenant, normal_clean_tenants))) {
      LOG_WARN("failed to delete backup meta data", K(ret), K(sys_clean_info), K(sys_clean_tenant));
    }
  }

  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
  } else {
    // push sys clean info into array
    dest_sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, trans, dest_sys_clean_info))) {
      LOG_WARN("failed to get backup clean info", K(ret), K(sys_clean_info));
    } else if (OB_FAIL(insert_clean_infos_into_history(normal_clean_tenants, trans))) {
      LOG_WARN("failed to insert clean infos into history", K(ret), K(normal_clean_tenants));
    } else if (OB_FAIL(reset_backup_clean_infos(normal_clean_tenants, trans))) {
      LOG_WARN("failed to reset backup clean infos", K(ret), K(normal_clean_tenants));
    } else if (OB_FAIL(backup_info_manager.init(OB_SYS_TENANT_ID, *sql_proxy_))) {
      LOG_WARN("failed to init backup info manager", K(ret));
    } else if (OB_SUCCESS == clean_result && sys_clean_info.is_delete_obsolete()) {
      const ObBackupManageArg::Type type = sys_clean_info.is_delete_obsolete_backup()
                                               ? ObBackupManageArg::DELETE_OBSOLETE_BACKUP
                                               : ObBackupManageArg::DELETE_OBSOLETE_BACKUP_BACKUP;
      if (OB_FAIL(backup_info_manager.update_delete_obsolete_snapshot(
              sys_clean_info.tenant_id_, type, sys_clean_info.start_time_, trans))) {
        LOG_WARN("failed to update last delete obsolete snapshot", K(ret), K(sys_clean_info));
      }
    }
    DEBUG_SYNC(BACKUP_DATA_SYS_CLEAN_STATUS_DOING);
    if (OB_FAIL(ret)) {
    } else if (OB_SUCCESS == clean_result &&
               OB_FAIL(delete_inner_table_his_data(dest_sys_clean_info, sys_clean_tenant, trans))) {
      LOG_WARN("failed to delete inner table his data", K(ret), K(sys_clean_info));
    } else if (OB_FAIL(set_sys_clean_info_stop(dest_sys_clean_info, trans))) {
      LOG_WARN("failed to set sys clean info stop", K(ret), K(sys_clean_info));
    }
  }

  if (OB_SUCC(ret)) {
    if (OB_FAIL(commit_trans(trans))) {
      LOG_WARN("failed to commit trans", K(ret));
    }
  } else {
    int tmp_ret = trans.end(false /*commit*/);
    if (OB_SUCCESS != tmp_ret) {
      LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
      ret = OB_SUCCESS == ret ? tmp_ret : ret;
    }
  }

  if (OB_SUCC(ret)) {
    retry_count_ = 0;
  }
  return ret;
}

int ObBackupDataClean::insert_clean_infos_into_history(
M
mw0 已提交
4291
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants, common::ObISQLClient &sys_tenant_trans)
4292 4293 4294 4295 4296 4297 4298
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < normal_clean_tenants.count(); ++i) {
M
mw0 已提交
4299
      const ObSimpleBackupDataCleanTenant &simple_clean_tennat = normal_clean_tenants.at(i);
4300 4301 4302 4303 4304 4305 4306 4307 4308
      if (OB_FAIL(insert_tenant_backup_clean_info_history(simple_clean_tennat, sys_tenant_trans))) {
        LOG_WARN("failed to insert tenant backup clean info", K(ret), K(simple_clean_tennat));
      }
    }
  }
  return ret;
}

int ObBackupDataClean::reset_backup_clean_infos(
M
mw0 已提交
4309
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants, common::ObISQLClient &sys_trans)
4310 4311 4312 4313 4314 4315 4316 4317
{
  int ret = OB_SUCCESS;
  const bool for_update = true;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < normal_clean_tenants.count(); ++i) {
M
mw0 已提交
4318
      const ObSimpleBackupDataCleanTenant &simple_clean_tenant = normal_clean_tenants.at(i);
4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
      ObMySQLTransaction trans;
      ObBackupCleanInfo dest_clean_info;
      ObBackupCleanInfo clean_info;
      ObTimeoutCtx timeout_ctx;
      clean_info.tenant_id_ = simple_clean_tenant.tenant_id_;
      if (OB_SYS_TENANT_ID == simple_clean_tenant.tenant_id_) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("normal clean tenants should not has sys clean tenant", K(ret), K(simple_clean_tenant));
      } else if (simple_clean_tenant.is_deleted_) {
        if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, sys_trans, clean_info))) {
          LOG_WARN("failed to get backup clean info", K(ret));
        } else if (OB_FAIL(delete_backup_clean_info(OB_SYS_TENANT_ID, clean_info, sys_trans))) {
          LOG_WARN("failed to delete backup clean info", K(ret), K(clean_info));
        }
      } else {
        if (OB_FAIL(start_trans(timeout_ctx, trans))) {
          LOG_WARN("failed to start trans", K(ret));
        } else {
          if (OB_FAIL(get_backup_clean_info(simple_clean_tenant.tenant_id_, for_update, trans, clean_info))) {
            LOG_WARN("failed to get backup clean info", K(ret), K(simple_clean_tenant));
          } else {
            dest_clean_info = clean_info;
            dest_clean_info.start_time_ = 0;
            dest_clean_info.end_time_ = 0;
            dest_clean_info.error_msg_.reset();
            dest_clean_info.comment_.reset();
            dest_clean_info.type_ = ObBackupCleanType::EMPTY_TYPE;
            dest_clean_info.clog_gc_snapshot_ = 0;
            dest_clean_info.result_ = OB_SUCCESS;
            dest_clean_info.copy_id_ = 0;

            if (OB_FAIL(update_clean_info(simple_clean_tenant.tenant_id_, clean_info, dest_clean_info, trans))) {
              LOG_WARN("failed to update clean info", K(ret), K(clean_info), K(dest_clean_info));
            }
          }

          if (OB_SUCC(ret)) {
            if (OB_FAIL(commit_trans(trans))) {
              LOG_WARN("failed to commit trans", K(ret));
            }
          } else {
            int tmp_ret = trans.end(false /*commit*/);
            if (OB_SUCCESS != tmp_ret) {
              LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
              ret = OB_SUCCESS == ret ? tmp_ret : ret;
            }
          }
        }
      }
    }
  }
  return ret;
}

int ObBackupDataClean::check_all_tenant_clean_tasks_stopped(
M
mw0 已提交
4374
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants, bool &is_all_tasks_stopped)
4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfo dest_clean_info;
  is_all_tasks_stopped = true;
  const bool for_update = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
    for (int64_t i = 0; is_all_tasks_stopped && OB_SUCC(ret) && i < normal_clean_tenants.count(); ++i) {
M
mw0 已提交
4386
      const ObSimpleBackupDataCleanTenant &simple_clean_tenant = normal_clean_tenants.at(i);
4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413
      ObBackupCleanInfo tenant_clean_info;
      tenant_clean_info.tenant_id_ = simple_clean_tenant.tenant_id_;
      if (OB_SYS_TENANT_ID == simple_clean_tenant.tenant_id_) {
        // do nothing
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("normal clean tenant should not has sys clean tenant", K(ret), K(simple_clean_tenant));
      } else if (simple_clean_tenant.is_deleted_) {
        if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, *sql_proxy_, tenant_clean_info))) {
          LOG_WARN("failed to get backup clean info", K(ret), K(simple_clean_tenant));
        }
      } else {
        if (OB_FAIL(
                get_backup_clean_info(simple_clean_tenant.tenant_id_, for_update, *sql_proxy_, tenant_clean_info))) {
          LOG_WARN("failed to get backup clean info", K(ret), K(simple_clean_tenant));
        }
      }

      if (OB_FAIL(ret)) {
      } else if (ObBackupCleanInfoStatus::STOP != tenant_clean_info.status_) {
        is_all_tasks_stopped = false;
      }
    }
  }
  return ret;
}

int ObBackupDataClean::update_clog_gc_snaphost(
M
mw0 已提交
4414
    const int64_t cluster_clog_gc_snapshot, ObBackupCleanInfo &clean_info, ObBackupDataCleanTenant &clean_tenant)
4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470
{
  int ret = OB_SUCCESS;
  int64_t clog_gc_snapshot = 0;
  ObBackupCleanInfo backup_clean_info;
  ObBackupCleanInfo dest_clean_info;
  ObMySQLTransaction trans;
  const bool for_update = true;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (cluster_clog_gc_snapshot < 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get clog gc snapshot get invalid argument", K(ret), K(cluster_clog_gc_snapshot));
  } else if (OB_FAIL(get_clog_gc_snapshot(clean_info, clean_tenant, cluster_clog_gc_snapshot, clog_gc_snapshot))) {
    LOG_WARN("failed to get clog gc snapshot", K(ret), K(clean_tenant));
  } else if (clean_tenant.simple_clean_tenant_.is_deleted_) {
    clean_tenant.clog_gc_snapshot_ = clog_gc_snapshot;
    clean_info.clog_gc_snapshot_ = clog_gc_snapshot;
  } else if (OB_FAIL(trans.start(sql_proxy_))) {
    LOG_WARN("fail to start trans", K(ret));
  } else {
    const uint64_t tenant_id =
        clean_tenant.simple_clean_tenant_.is_deleted_ ? OB_SYS_TENANT_ID : clean_tenant.simple_clean_tenant_.tenant_id_;
    backup_clean_info.tenant_id_ = clean_tenant.simple_clean_tenant_.tenant_id_;
    if (OB_FAIL(get_backup_clean_info(tenant_id, for_update, trans, backup_clean_info))) {
      LOG_WARN("failed to get backup clean info", K(ret), K(clean_tenant));
    } else if (ObBackupCleanInfoStatus::STOP == backup_clean_info.status_) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("update clog gc snapshot get invalid argument", K(ret), K(backup_clean_info));
    } else {
      dest_clean_info = backup_clean_info;
      dest_clean_info.clog_gc_snapshot_ = clog_gc_snapshot;
      if (OB_FAIL(update_clean_info(tenant_id, backup_clean_info, dest_clean_info, trans))) {
        LOG_WARN("failed to update clean info", K(ret), K(backup_clean_info), K(dest_clean_info));
      } else {
        clean_tenant.clog_gc_snapshot_ = clog_gc_snapshot;
        clean_info.clog_gc_snapshot_ = clog_gc_snapshot;
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(trans.end(true /*commit*/))) {
        OB_LOG(WARN, "failed to commit", K(ret));
      }
    } else {
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = trans.end(false /* commit*/))) {
        OB_LOG(WARN, "failed to rollback trans", K(tmp_ret));
      }
    }
  }

  return ret;
}

M
mw0 已提交
4471 4472
int ObBackupDataClean::get_clog_gc_snapshot(const ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanTenant &clean_tenant, const int64_t cluster_clog_gc_snapshot, int64_t &clog_gc_snapshot)
4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509
{
  int ret = OB_SUCCESS;
  clog_gc_snapshot = INT64_MAX;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (cluster_clog_gc_snapshot < 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get clog gc snapshot get invalid argument", K(ret), K(cluster_clog_gc_snapshot));
  } else if (clean_info.is_delete_backup_set()) {
    // delete failed backup set, so clog gc snapshot may be 0
    clog_gc_snapshot = clean_tenant.clog_gc_snapshot_;
  } else if (clean_info.is_delete_backup_piece() || clean_info.is_delete_backup_round()) {
    if (clean_tenant.clog_gc_snapshot_ <= 0 && !clean_tenant.backup_element_array_.empty()) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("clean info is delete backup set or piece but clog gc snapshot is invalid",
          K(ret),
          K(clean_info),
          K(clean_tenant));
    } else {
      clog_gc_snapshot = clean_tenant.clog_gc_snapshot_;
    }
  } else if (!clean_tenant.clog_data_clean_point_.is_valid() ||
             0 == clean_tenant.clog_data_clean_point_.start_replay_log_ts_) {
    // do nothing
    clog_gc_snapshot = cluster_clog_gc_snapshot;
  } else if (OB_SYS_TENANT_ID == clean_info.tenant_id_) {
    clog_gc_snapshot = clean_tenant.clog_data_clean_point_.start_replay_log_ts_;
  } else {
    clog_gc_snapshot = std::min(clean_tenant.clog_data_clean_point_.start_replay_log_ts_, cluster_clog_gc_snapshot);
  }

  LOG_INFO(
      "get tenant backup clog gc snapshot", K(ret), K(clog_gc_snapshot), K(cluster_clog_gc_snapshot), K(clean_tenant));
  return ret;
}

M
mw0 已提交
4510
int ObBackupDataClean::get_deleted_clean_tenants(ObIArray<ObSimpleBackupDataCleanTenant> &deleted_tenants)
4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532
{
  int ret = OB_SUCCESS;
  deleted_tenants.reset();
  ObMySQLTransaction trans;
  ObBackupCleanInfo sys_clean_info;
  ObArray<ObBackupCleanInfo> deleted_tenant_clean_infos;
  const bool for_update = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(trans.start(sql_proxy_))) {
    LOG_WARN("fail to start trans", K(ret));
  } else {
    sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, trans, sys_clean_info))) {
      LOG_WARN("failed to get clean info", K(ret));
    } else if (OB_FAIL(get_deleted_tenant_clean_infos(trans, deleted_tenant_clean_infos))) {
      LOG_WARN("failed to get deleted tenant clean infos", K(ret), K(sys_clean_info));
    } else {
      ObSimpleBackupDataCleanTenant deleted_clean_tenant;
      for (int64_t i = 0; OB_SUCC(ret) && i < deleted_tenant_clean_infos.count(); ++i) {
M
mw0 已提交
4533
        const ObBackupCleanInfo &backup_clean_info = deleted_tenant_clean_infos.at(i);
4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557
        deleted_clean_tenant.reset();
        deleted_clean_tenant.tenant_id_ = backup_clean_info.tenant_id_;
        deleted_clean_tenant.is_deleted_ = true;
        if (OB_FAIL(deleted_tenants.push_back(deleted_clean_tenant))) {
          LOG_WARN("failed to push deleted clean tenant into array", K(ret), K(deleted_clean_tenant));
        }
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(trans.end(true /*commit*/))) {
        OB_LOG(WARN, "failed to commit", K(ret));
      }
    } else {
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = trans.end(false /* commit*/))) {
        OB_LOG(WARN, "failed to rollback trans", K(tmp_ret));
      }
    }
  }
  return ret;
}

int ObBackupDataClean::get_deleted_tenant_clean_infos(
M
mw0 已提交
4558
    ObISQLClient &trans, common::ObIArray<ObBackupCleanInfo> &deleted_tenant_clean_infos)
4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573
{
  int ret = OB_SUCCESS;
  ObTenantBackupCleanInfoUpdater updater;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_FAIL(updater.init(trans))) {
    LOG_WARN("failed to init tenant backup clean info updater", K(ret));
  } else if (OB_FAIL(updater.get_deleted_tenant_clean_infos(deleted_tenant_clean_infos))) {
    LOG_WARN("failed to get backup clean info", K(ret));
  }
  return ret;
}

int ObBackupDataClean::schedule_deleted_clean_tenants(
M
mw0 已提交
4574
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &deleted_clean_tenants)
4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600
{
  int ret = OB_SUCCESS;
  ObTenantBackupCleanInfoUpdater updater;
  ObMySQLTransaction trans;
  ObBackupCleanInfo sys_clean_info;
  const bool for_update = true;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (deleted_clean_tenants.empty()) {
    // do nothing
  } else if (OB_FAIL(trans.start(sql_proxy_))) {
    LOG_WARN("fail to start trans", K(ret));
  } else {
    sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
    if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, trans, sys_clean_info))) {
      LOG_WARN("failed to get clean info", K(ret));
    } else if (ObBackupCleanInfoStatus::DOING != sys_clean_info.status_ &&
               ObBackupCleanInfoStatus::CANCEL != sys_clean_info.status_) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("schedule deleted clean tenants get unexpected status", K(ret), K(sys_clean_info));
    } else if (updater.init(trans)) {
      LOG_WARN("failed to init tenant backup clean info updater", K(ret));
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < deleted_clean_tenants.count(); ++i) {
M
mw0 已提交
4601
        const ObSimpleBackupDataCleanTenant &simple_clean_tenant = deleted_clean_tenants.at(i);
4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623
        ObBackupCleanInfo clean_info = sys_clean_info;
        clean_info.tenant_id_ = simple_clean_tenant.tenant_id_;
        if (OB_FAIL(updater.insert_backup_clean_info(OB_SYS_TENANT_ID, clean_info))) {
          LOG_WARN("failed to insert backup clean info", K(ret), K(clean_info));
        }
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(trans.end(true /*commit*/))) {
        OB_LOG(WARN, "failed to commit", K(ret));
      }
    } else {
      int tmp_ret = OB_SUCCESS;
      if (OB_SUCCESS != (tmp_ret = trans.end(false /* commit*/))) {
        OB_LOG(WARN, "failed to rollback trans", K(tmp_ret));
      }
    }
  }
  return ret;
}

M
mw0 已提交
4624 4625
int ObBackupDataClean::get_clean_tenants(const common::ObIArray<ObBackupDataCleanTenant> &clean_tenants,
    common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants, ObBackupDataCleanTenant &sys_clean_tenant)
4626 4627 4628 4629 4630 4631 4632
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_tenants.count(); ++i) {
M
mw0 已提交
4633 4634
      const ObBackupDataCleanTenant &clean_tenant = clean_tenants.at(i);
      const ObSimpleBackupDataCleanTenant &simple_clean_tenant = clean_tenant.simple_clean_tenant_;
4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645
      if (OB_SYS_TENANT_ID == clean_tenant.simple_clean_tenant_.tenant_id_) {
        sys_clean_tenant = clean_tenant;
      } else if (OB_FAIL(normal_clean_tenants.push_back(simple_clean_tenant))) {
        LOG_WARN("failed to push clean tenant into array", K(ret), K(simple_clean_tenant));
      }
    }
  }
  return ret;
}

int ObBackupDataClean::delete_backup_clean_info(
M
mw0 已提交
4646
    const uint64_t tenant_id, const ObBackupCleanInfo &clean_info, ObISQLClient &trans)
4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661
{
  int ret = OB_SUCCESS;
  ObTenantBackupCleanInfoUpdater updater;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_FAIL(updater.init(trans))) {
    LOG_WARN("failed to init updater", K(ret));
  } else if (OB_FAIL(updater.remove_clean_info(tenant_id, clean_info))) {
    LOG_WARN("failedto remove clean info", K(ret), K(tenant_id), K(clean_info));
  }
  return ret;
}

M
mw0 已提交
4662
int ObBackupDataClean::set_sys_clean_info_stop(const ObBackupCleanInfo &backup_clean_info, ObISQLClient &trans)
4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfoHistoryUpdater updater;
  ObBackupCleanInfo dest_clean_info;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (!backup_clean_info.is_valid() || OB_SYS_TENANT_ID != backup_clean_info.tenant_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("set sys clean info stop get invalid argument", K(ret), K(backup_clean_info));
  } else if (ObBackupCleanInfoStatus::STOP == backup_clean_info.status_) {
    // do nothing
  } else {
    dest_clean_info = backup_clean_info;
    dest_clean_info.status_ = ObBackupCleanInfoStatus::STOP;
    dest_clean_info.end_time_ = ObTimeUtil::current_time();
    const int32_t result = dest_clean_info.result_;
    if (OB_FAIL(set_comment(dest_clean_info.comment_))) {
      LOG_WARN("failed to set comment", K(ret), K(dest_clean_info));
    } else if (OB_FAIL(set_error_msg(result, dest_clean_info.error_msg_))) {
      LOG_WARN("failed to set error msg", K(ret), K(dest_clean_info));
    } else if (OB_FAIL(updater.init(trans))) {
      LOG_WARN("failed to init updater", K(ret));
    } else if (OB_FAIL(updater.insert_backup_clean_info(dest_clean_info))) {
      LOG_WARN("failed to insert backup clean info into history", K(ret), K(dest_clean_info));
    } else {
      dest_clean_info.reset();
      dest_clean_info = backup_clean_info;
      dest_clean_info.start_time_ = 0;
      dest_clean_info.end_time_ = 0;
      dest_clean_info.error_msg_.reset();
      dest_clean_info.comment_.reset();
      dest_clean_info.type_ = ObBackupCleanType::EMPTY_TYPE;
      dest_clean_info.status_ = ObBackupCleanInfoStatus::STOP;
      dest_clean_info.result_ = OB_SUCCESS;
      dest_clean_info.clog_gc_snapshot_ = 0;
      dest_clean_info.copy_id_ = 0;

      if (OB_FAIL(update_clean_info(OB_SYS_TENANT_ID, backup_clean_info, dest_clean_info, trans))) {
        LOG_WARN("failed to update clean info", K(ret), K(backup_clean_info), K(dest_clean_info));
      }
    }
  }
  return ret;
}

M
mw0 已提交
4709
int ObBackupDataClean::try_clean_tenant_backup_dir(const ObBackupDataCleanTenant &clean_tenant)
4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720
{
  int ret = OB_SUCCESS;
  const uint64_t tenant_id = clean_tenant.simple_clean_tenant_.tenant_id_;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_SYS_TENANT_ID == tenant_id || !clean_tenant.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("try clean normal tenant backup dir get invalid agument", K(ret), K(clean_tenant));
  } else {
M
mw0 已提交
4721
    const ObIArray<ObBackupDataCleanElement> &backup_element_array = clean_tenant.backup_element_array_;
4722
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_element_array.count(); ++i) {
M
mw0 已提交
4723
      const ObBackupDataCleanElement &clean_element = backup_element_array.at(i);
4724 4725 4726 4727 4728 4729 4730 4731
      if (OB_FAIL(clean_tenant_backup_dir(tenant_id, clean_element))) {
        LOG_WARN("failed to clean tenant backup dir", K(ret), K(tenant_id), K(clean_element));
      }
    }
  }
  return ret;
}

M
mw0 已提交
4732
int ObBackupDataClean::clean_tenant_backup_dir(const uint64_t tenant_id, const ObBackupDataCleanElement &clean_element)
4733 4734 4735 4736
{
  int ret = OB_SUCCESS;
  ObClusterBackupDest cluster_backup_dest;
  ObBackupPath path;
M
mw0 已提交
4737 4738
  const char *storage_info = clean_element.backup_dest_.storage_info_;
  const ObStorageType &device_type = clean_element.backup_dest_.device_type_;
4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_FAIL(cluster_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
    LOG_WARN("failed to set cluster backup dest", K(ret), K(clean_element));
  } else if (OB_FAIL(ObBackupPathUtil::get_tenant_backup_data_path(cluster_backup_dest, tenant_id, path))) {
    LOG_WARN("failed to get tenant backup data path", K(ret), K(cluster_backup_dest), K(tenant_id));
  } else if (OB_FAIL(ObBackupDataCleanUtil::delete_backup_dir(path, storage_info, device_type))) {
    LOG_WARN("failed to delete backup dir", K(ret), K(path), K(storage_info));
  } else if (FALSE_IT(path.reset())) {
  } else if (OB_FAIL(ObBackupPathUtil::get_tenant_clog_path(cluster_backup_dest, tenant_id, path))) {
    LOG_WARN("failed to get tenant clog path", K(ret), K(cluster_backup_dest), K(tenant_id));
  } else if (OB_FAIL(ObBackupDataCleanUtil::delete_backup_dir(path, storage_info, device_type))) {
    LOG_WARN("failed to delete backup dir", K(ret), K(path), K(storage_info));
  } else if (FALSE_IT(path.reset())) {
  } else if (OB_FAIL(ObBackupPathUtil::get_cluster_prefix_path(cluster_backup_dest, path))) {
    LOG_WARN("failed to get tenant data backup info path", K(ret), K(cluster_backup_dest), K(tenant_id));
  } else if (OB_FAIL(path.join(tenant_id))) {
    LOG_WARN("failed to join backup path", K(ret), K(path));
  } else if (OB_FAIL(ObBackupDataCleanUtil::delete_backup_dir(path, storage_info, device_type))) {
    LOG_WARN("failed to delete backup dir", K(ret), K(path), K(storage_info));
  }
  return ret;
}

M
mw0 已提交
4765 4766
int ObBackupDataClean::clean_backup_tenant_info(const ObBackupDataCleanTenant &sys_clean_tenant,
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants)
4767 4768 4769 4770 4771 4772 4773 4774 4775 4776
{
  int ret = OB_SUCCESS;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (!sys_clean_tenant.is_valid() || OB_SYS_TENANT_ID != sys_clean_tenant.simple_clean_tenant_.tenant_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("try clean backup tenant get invalid argument", K(ret), K(sys_clean_tenant));
  } else {
M
mw0 已提交
4777
    const ObIArray<ObBackupDataCleanElement> &backup_element_array = sys_clean_tenant.backup_element_array_;
4778
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_element_array.count(); ++i) {
M
mw0 已提交
4779
      const ObBackupDataCleanElement &clean_element = backup_element_array.at(i);
4780 4781 4782 4783 4784 4785 4786 4787
      if (OB_FAIL(inner_clean_backup_tenant_info(clean_element, normal_clean_tenants))) {
        LOG_WARN("failed to clean backup tenant info", K(ret), K(clean_element));
      }
    }
  }
  return ret;
}

M
mw0 已提交
4788 4789
int ObBackupDataClean::inner_clean_backup_tenant_info(const ObBackupDataCleanElement &clean_element,
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants)
4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808
{
  int ret = OB_SUCCESS;
  const ObStorageType device_type = clean_element.backup_dest_.device_type_;
  ObExternTenantInfoMgr tenant_info_mgr;
  ObClusterBackupDest cluster_backup_dest;
  ObStorageUtil util(false /*need retry*/);
  ObBackupPath path;
  ObArenaAllocator allocator;
  ObArray<ObString> file_name_array;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_FAIL(cluster_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
    LOG_WARN("failed to set cluster backup dest", K(ret), K(clean_element));
  } else if (OB_FAIL(tenant_info_mgr.init(cluster_backup_dest, *backup_lease_service_))) {
    LOG_WARN("failed to init tenant info mgr", K(ret), K(cluster_backup_dest));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < normal_clean_tenants.count(); ++i) {
M
mw0 已提交
4809
      const ObSimpleBackupDataCleanTenant &normal_clean_tenant = normal_clean_tenants.at(i);
4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850
      if (normal_clean_tenant.is_deleted_) {
        path.reset();
        file_name_array.reset();
        allocator.reset();
        if (OB_FAIL(ObBackupPathUtil::get_tenant_path(cluster_backup_dest, normal_clean_tenant.tenant_id_, path))) {
          LOG_WARN("failed to get tenant path", K(ret), K(cluster_backup_dest));
        } else if (OB_FAIL(util.list_files(
                       path.get_ptr(), cluster_backup_dest.get_storage_info(), allocator, file_name_array))) {
          LOG_WARN("failed to list files", K(ret), K(path));
        } else if (!file_name_array.empty()) {
          // do nothing
        } else if (OB_FAIL(tenant_info_mgr.delete_tenant_info(normal_clean_tenant.tenant_id_))) {
          LOG_WARN("failed to delete tenant info", K(ret), K(normal_clean_tenant));
        }
      }
    }

    if (OB_SUCC(ret)) {
      path.reset();
      if (OB_FAIL(ObBackupPathUtil::get_tenant_info_path(cluster_backup_dest, path))) {
        LOG_WARN("failed to get tenatn info path", K(ret), K(cluster_backup_dest));
      } else if (tenant_info_mgr.is_empty()) {
        if (OB_FAIL(
                ObBackupDataCleanUtil::delete_backup_file(path, cluster_backup_dest.get_storage_info(), device_type))) {
          LOG_WARN("failed to delete backup file", K(ret), K(path), K(clean_element));
        }
      } else if (!tenant_info_mgr.is_extern_tenant_infos_modified()) {
        if (OB_FAIL(
                ObBackupDataCleanUtil::touch_backup_file(path, cluster_backup_dest.get_storage_info(), device_type))) {
          LOG_WARN("failed to touch backup file", K(ret), K(path), K(clean_element));
        }
      } else {
        if (OB_FAIL(tenant_info_mgr.upload_tenant_infos())) {
          LOG_WARN("failed to upload tenant infos", K(ret), K(cluster_backup_dest));
        }
      }
    }
  }
  return ret;
}

M
mw0 已提交
4851
int ObBackupDataClean::touch_extern_tenant_name(const ObBackupDataCleanTenant &clean_tenant)
4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868
{
  int ret = OB_SUCCESS;
  ObExternTenantInfoMgr tenant_info_mgr;
  ObClusterBackupDest cluster_backup_dest;
  ObStorageUtil util(false /*need retry*/);
  ObBackupPath path;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (!clean_tenant.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("touch extern tenant name get invalid argument", K(ret), K(clean_tenant));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_tenant.backup_element_array_.count(); ++i) {
      path.reset();
      cluster_backup_dest.reset();
M
mw0 已提交
4869
      const ObBackupDataCleanElement &clean_element = clean_tenant.backup_element_array_.at(i);
4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883
      const ObStorageType device_type = clean_element.backup_dest_.device_type_;
      if (OB_FAIL(cluster_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
        LOG_WARN("failed to set cluster backup dest", K(ret), K(clean_element));
      } else if (OB_FAIL(ObBackupPathUtil::get_tenant_name_info_path(cluster_backup_dest, path))) {
        LOG_WARN("failed to get tenant path", K(ret), K(cluster_backup_dest));
      } else if (OB_FAIL(ObBackupDataCleanUtil::touch_backup_file(
                     path, cluster_backup_dest.get_storage_info(), device_type))) {
        LOG_WARN("failed to touch backup file", K(ret), K(path), K(clean_element));
      }
    }
  }
  return ret;
}

M
mw0 已提交
4884
int ObBackupDataClean::touch_extern_clog_info(const ObBackupDataCleanTenant &clean_tenant)
4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900
{
  int ret = OB_SUCCESS;
  ObBackupPath path;
  ObClusterBackupDest cluster_backup_dest;
  ObBackupDataCleanStatics clean_statics;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (!clean_tenant.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("touch extern tenant name get invalid argument", K(ret), K(clean_tenant));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_tenant.backup_element_array_.count(); ++i) {
      path.reset();
      cluster_backup_dest.reset();
M
mw0 已提交
4901
      const ObBackupDataCleanElement &clean_element = clean_tenant.backup_element_array_.at(i);
4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918
      const ObStorageType device_type = clean_element.backup_dest_.device_type_;
      if (OB_FAIL(cluster_backup_dest.set(clean_element.backup_dest_, clean_element.incarnation_))) {
        LOG_WARN("failed to set cluster backup dest", K(ret), K(clean_element));
      } else if (OB_FAIL(ObBackupPathUtil::get_cluster_clog_info(cluster_backup_dest, path))) {
        LOG_WARN("failed to get tenant path", K(ret), K(cluster_backup_dest));
      } else if (OB_FAIL(ObBackupDataCleanUtil::touch_backup_dir_files(path,
                     cluster_backup_dest.get_storage_info(),
                     device_type,
                     clean_statics,
                     *backup_lease_service_))) {
        LOG_WARN("failed to touch backup file", K(ret), K(path), K(clean_element));
      }
    }
  }
  return ret;
}

M
mw0 已提交
4919 4920 4921
int ObBackupDataClean::delete_cluster_backup_meta_data(const share::ObBackupCleanInfo &clean_info,
    const ObBackupDataCleanTenant &clean_tenant,
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &normal_clean_tenants)
4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (!clean_info.is_valid() || OB_SYS_TENANT_ID != clean_info.tenant_id_ || !clean_tenant.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("delete cluster backup meta data get invalid argument", K(ret), K(clean_info), K(clean_tenant));
  } else if (OB_FAIL(delete_backup_extern_infos(clean_info, clean_tenant))) {
    LOG_WARN("failed to delete extern backup infos deleted", K(ret), K(clean_info));
  } else if (OB_FAIL(clean_backup_tenant_info(clean_tenant, normal_clean_tenants))) {
    LOG_WARN("failed to clean backup tenant info", K(ret), K(clean_tenant));
  } else if (OB_FAIL(touch_extern_tenant_name(clean_tenant))) {
    LOG_WARN("failed to touch extern tenant name", K(ret), K(clean_tenant));
  } else if (OB_FAIL(touch_extern_clog_info(clean_tenant))) {
    LOG_WARN("failed to touch extern clog info", K(ret), K(clean_tenant));
  }
  return ret;
}

M
mw0 已提交
4942
int ObBackupDataClean::get_cluster_max_succeed_backup_set(const int64_t copy_id, int64_t &backup_set_id)
4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965
{
  int ret = OB_SUCCESS;
  backup_set_id = 0;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (copy_id < 0) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get cluster max succeed backup set get invalid argument", K(ret), K(copy_id));
  } else {
    if (0 == copy_id) {
      if (OB_FAIL(inner_get_cluster_max_succeed_backup_set(backup_set_id))) {
        LOG_WARN("failed to get cluster max succeed backup set", KR(ret));
      }
    } else {
      if (OB_FAIL(inner_get_cluster_max_succeed_backup_backup_set(copy_id, backup_set_id))) {
        LOG_WARN("failed to get cluster max succeed backup set", KR(ret));
      }
    }
  }
  return ret;
}

M
mw0 已提交
4966
int ObBackupDataClean::inner_get_cluster_max_succeed_backup_set(int64_t &backup_set_id)
4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990
{
  int ret = OB_SUCCESS;
  backup_set_id = 0;
  ObBackupTaskHistoryUpdater updater;
  ObTenantBackupTaskInfo task_info;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_FAIL(updater.init(*sql_proxy_))) {
    LOG_WARN("failed to init backup task history updater", K(ret));
  } else if (OB_FAIL(updater.get_tenant_max_succeed_backup_task(OB_SYS_TENANT_ID, task_info))) {
    if (OB_INVALID_BACKUP_SET_ID == ret) {
      backup_set_id = INT64_MAX;
      ret = OB_SUCCESS;
    } else {
      LOG_WARN("failed to get tenant max succeed backup task", K(ret));
    }
  } else {
    backup_set_id = task_info.backup_set_id_;
  }
  return ret;
}

M
mw0 已提交
4991
int ObBackupDataClean::inner_get_cluster_max_succeed_backup_backup_set(const int64_t copy_id, int64_t &backup_set_id)
4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012
{
  int ret = OB_SUCCESS;
  ObTenantBackupBackupsetTaskInfo task_info;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (OB_FAIL(ObTenantBackupBackupsetHistoryOperator::get_max_succeed_task(
                 OB_SYS_TENANT_ID, copy_id, task_info, *sql_proxy_))) {
    if (OB_INVALID_BACKUP_SET_ID == ret) {
      backup_set_id = 0;
      ret = OB_SUCCESS;
    } else {
      LOG_WARN("failed to get tenant max succeed backup task", K(ret));
    }
  } else {
    backup_set_id = task_info.backup_set_id_;
  }
  return ret;
}

int ObBackupDataClean::get_log_archive_info(
M
mw0 已提交
5013
    const int64_t snapshot_version, const ObArray<ObLogArchiveBackupInfo> &log_infos, ObLogArchiveBackupInfo &log_info)
5014 5015 5016 5017 5018 5019 5020 5021 5022 5023
{
  int ret = OB_SUCCESS;
  log_info.reset();
  typedef ObArray<ObLogArchiveBackupInfo>::const_iterator ArrayIter;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (log_infos.empty()) {
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < log_infos.count() - 1; ++i) {
M
mw0 已提交
5024
      if (log_infos.at(i).status_.start_ts_ > log_infos.at(i + 1).status_.start_ts_) {
5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("log infos is not sorted by start ts", K(ret), K(log_infos));
      }
    }
    if (OB_SUCC(ret)) {
      CompareLogArchiveSnapshotVersion cmp;
      ArrayIter iter = std::lower_bound(log_infos.begin(), log_infos.end(), snapshot_version, cmp);
      if (iter == log_infos.end()) {
        --iter;
      } else if (iter != log_infos.begin() && iter->status_.start_ts_ > snapshot_version) {
        --iter;
      }
      log_info = *iter;
    }
  }
  return ret;
}

M
mw0 已提交
5043 5044
int ObBackupDataClean::check_backupset_continue_with_clog_data(const ObTenantBackupTaskInfo &backup_task_info,
    const common::ObArray<ObLogArchiveBackupInfo> &log_archive_infos, bool &is_continue)
5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121
{
  int ret = OB_SUCCESS;
  ObLogArchiveBackupInfo log_archive_info;
  is_continue = false;

  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean scheduler do not init", K(ret));
  } else if (!backup_task_info.is_valid() || !backup_task_info.is_result_succeed()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("check backupset contine with clog data get invalid argument", K(ret), K(backup_task_info));
  } else if (OB_FAIL(get_log_archive_info(backup_task_info.snapshot_version_, log_archive_infos, log_archive_info))) {
    LOG_WARN("failed to get log archive info", K(ret), K(backup_task_info));
  } else if (log_archive_info.status_.checkpoint_ts_ > backup_task_info.snapshot_version_ &&
             log_archive_info.status_.start_ts_ < backup_task_info.snapshot_version_) {
    is_continue = true;
  }
  return ret;
}

int ObBackupDataClean::check_inner_table_version_()
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfo sys_clean_info;
  sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (stop_) {
    ret = OB_SERVER_IS_STOPPING;
    LOG_WARN("rs is stopping", K(ret));
  } else if (OB_FAIL(backup_lease_service_->check_lease())) {
    LOG_WARN("failed to check can backup", K(ret));
  } else if (is_valid_backup_inner_table_version(inner_table_version_) &&
             inner_table_version_ >= OB_BACKUP_INNER_TABLE_V3) {
    // inner table version is new enough
  } else if (OB_FAIL(ObBackupInfoOperator::get_inner_table_version(*sql_proxy_, inner_table_version_))) {
    LOG_WARN("Failed to get inner table version", K(ret));
  } else if (inner_table_version_ >= OB_BACKUP_INNER_TABLE_V1 && inner_table_version_ < OB_BACKUP_INNER_TABLE_V2) {
    ret = OB_EAGAIN;
    LOG_INFO("inner table version is too old, waiting backup inner table upgrade", K(ret), K(inner_table_version_));
  } else if (OB_FAIL(prepare_deleted_tenant_backup_infos())) {
    LOG_WARN("failed to prepare deleted tenant backup infos", K(ret));
  }

  return ret;
}

int ObBackupDataClean::check_can_do_task()
{
  // TODO (muwei.ym) consider tenant cancel delete backup
  int ret = OB_SUCCESS;
  ObBackupCleanInfo sys_clean_info;
  sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
  const bool for_update = false;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (stop_) {
    ret = OB_SERVER_IS_STOPPING;
    LOG_WARN("rs is stopping", K(ret));
  } else if (OB_FAIL(backup_lease_service_->check_lease())) {
    LOG_WARN("failed to check can backup", K(ret));
  } else if (OB_FAIL(get_backup_clean_info(OB_SYS_TENANT_ID, for_update, *sql_proxy_, sys_clean_info))) {
    if (OB_BACKUP_CLEAN_INFO_NOT_EXIST == ret) {
      ret = OB_SUCCESS;
    } else {
      LOG_WARN("failed to get backup clean info", K(ret));
    }
  } else if (ObBackupCleanInfoStatus::CANCEL == sys_clean_info.status_) {
    ret = OB_CANCELED;
    LOG_WARN("backup data clean has been canceled", K(ret), K(sys_clean_info));
  }
  return ret;
}

int ObBackupDataClean::do_tenant_cancel_delete_backup(
M
mw0 已提交
5122
    const ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
    if (OB_SYS_TENANT_ID == clean_info.tenant_id_) {
      if (OB_FAIL(do_sys_tenant_cancel_delete_backup(clean_info))) {
        LOG_WARN("failed to do sys tenant cancel delete backup", K(ret), K(clean_info));
      }
    } else {
      if (OB_FAIL(do_normal_tenant_cancel_delete_backup(clean_info, clean_tenant))) {
        LOG_WARN("failedto do normal tenant cancel delete backup", K(ret), K(clean_info));
      }
    }
  }
  return ret;
}

M
mw0 已提交
5142
int ObBackupDataClean::do_sys_tenant_cancel_delete_backup(const ObBackupCleanInfo &clean_info)
5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159
{
  int ret = OB_SUCCESS;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("do_sys_tenant cancel delete backup get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(set_tenant_clean_info_cancel(clean_info))) {
    LOG_WARN("failed to set tenant clean info cancel", K(ret), K(clean_info));
  } else {
    wakeup();
  }
  return ret;
}

M
mw0 已提交
5160
int ObBackupDataClean::set_tenant_clean_info_cancel(const ObBackupCleanInfo &clean_info)
5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201
{
  int ret = OB_SUCCESS;
  ObArray<uint64_t> all_tenant_ids;
  ObMySQLTransaction trans;
  ObTimeoutCtx timeout_ctx;
  ObBackupCleanInfo sys_clean_info;
  sys_clean_info.tenant_id_ = OB_SYS_TENANT_ID;
  const bool for_update = true;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_SYS_TENANT_ID != clean_info.tenant_id_ || ObBackupCleanInfoStatus::CANCEL != clean_info.status_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("do sys tenant cancel delete backup get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(get_all_tenant_ids(all_tenant_ids))) {
    LOG_WARN("failed to get all tenant ids", K(ret), K(clean_info));
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
  } else {
    if (OB_FAIL(get_backup_clean_info(clean_info.tenant_id_, for_update, trans, sys_clean_info))) {
      LOG_WARN("failed to get backup clean info", K(ret), K(clean_info));
    } else if (OB_FAIL(clean_info.check_backup_clean_info_match(sys_clean_info))) {
      LOG_WARN("failed to check backup clean info match", K(ret), K(clean_info), K(sys_clean_info));
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < all_tenant_ids.count(); ++i) {
        const uint64_t tenant_id = all_tenant_ids.at(i);
        if (OB_SYS_TENANT_ID == tenant_id) {
          // do nothing
        } else if (OB_FAIL(set_normal_tenant_cancel(tenant_id, trans))) {
          LOG_WARN("failed to set normal tenant cancel", K(ret), K(tenant_id));
        }
      }
    }

    if (OB_SUCC(ret)) {
      ObArray<ObBackupCleanInfo> deleted_tenant_clean_infos;
      if (OB_FAIL(get_deleted_tenant_clean_infos(trans, deleted_tenant_clean_infos))) {
        LOG_WARN("failed to get deleted tenant clean infos", K(ret));
      } else {
        for (int64_t i = 0; OB_SUCC(ret) && i < deleted_tenant_clean_infos.count(); ++i) {
M
mw0 已提交
5202
          const ObBackupCleanInfo &delete_tenant_clean_info = deleted_tenant_clean_infos.at(i);
5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224
          if (OB_FAIL(set_deleted_tenant_cancel(delete_tenant_clean_info, trans))) {
            LOG_WARN("failed to set deleted tenant cancel", K(ret), K(delete_tenant_clean_info));
          }
        }
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
      }
    } else {
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
      }
    }
  }
  return ret;
}

M
mw0 已提交
5225
int ObBackupDataClean::set_normal_tenant_cancel(const uint64_t tenant_id, common::ObISQLClient &sys_tenant_trans)
5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274
{
  int ret = OB_SUCCESS;
  ObMySQLTransaction trans;
  ObBackupCleanInfo clean_info;
  clean_info.tenant_id_ = tenant_id;
  ObBackupCleanInfo dest_clean_info;
  ObTimeoutCtx timeout_ctx;
  const bool for_update = true;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_INVALID_ID == tenant_id) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("set normal tenant cancel get invalid argument", K(ret), K(tenant_id));
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
  } else {
    if (OB_FAIL(get_backup_clean_info(tenant_id, for_update, trans, clean_info))) {
      LOG_WARN("failed to get backup clean info", K(ret), K(clean_info));
    } else if (ObBackupCleanInfoStatus::STOP == clean_info.status_ ||
               ObBackupCleanInfoStatus::CANCEL == clean_info.status_) {
      // do nothing
    } else if (OB_FAIL(ObBackupUtil::check_sys_clean_info_trans_alive(sys_tenant_trans))) {
      LOG_WARN("failed to check sys clean info trans alive", K(ret), K(tenant_id));
    } else {
      dest_clean_info = clean_info;
      dest_clean_info.status_ = ObBackupCleanInfoStatus::CANCEL;
      if (OB_FAIL(update_clean_info(tenant_id, clean_info, dest_clean_info, trans))) {
        LOG_WARN("failed to update clean info", K(ret), K(clean_info), K(dest_clean_info));
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
      }
    } else {
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
      }
    }
  }
  return ret;
}

int ObBackupDataClean::set_deleted_tenant_cancel(
M
mw0 已提交
5275
    const ObBackupCleanInfo &clean_info, common::ObISQLClient &sys_tenant_trans)
5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299
{
  int ret = OB_SUCCESS;
  ObBackupCleanInfo dest_clean_info;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("set deleted tenant cancel get invalid argument", K(ret), K(clean_info));
  } else if (ObBackupCleanInfoStatus::STOP == clean_info.status_ ||
             ObBackupCleanInfoStatus::CANCEL == clean_info.status_) {
    // do nothing
  } else {
    dest_clean_info = clean_info;
    dest_clean_info.status_ = ObBackupCleanInfoStatus::CANCEL;
    if (OB_FAIL(update_clean_info(OB_SYS_TENANT_ID, clean_info, dest_clean_info, sys_tenant_trans))) {
      LOG_WARN("failed to update clean info", K(ret), K(clean_info), K(dest_clean_info));
    }
  }
  return ret;
}

int ObBackupDataClean::do_normal_tenant_cancel_delete_backup(
M
mw0 已提交
5300
    const ObBackupCleanInfo &clean_info, const ObBackupDataCleanTenant &clean_tenant)
5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317
{
  int ret = OB_SUCCESS;
  const int32_t result = clean_info.result_ == OB_SUCCESS ? OB_CANCELED : clean_info.result_;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_INVALID_ID == clean_info.tenant_id_ || OB_SYS_TENANT_ID == clean_info.tenant_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("do normal tenant cancel delete backup get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(update_normal_tenant_clean_result(clean_info, clean_tenant, result))) {
    LOG_WARN("failed to update normal tenant clean result", K(ret), K(clean_info), K(clean_tenant));
  }
  return ret;
}

/*not use anymore*/
M
mw0 已提交
5318
int ObBackupDataClean::get_sys_tenant_backup_dest(hash::ObHashSet<ObClusterBackupDest> &cluster_backup_dest_set)
5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367
{
  int ret = OB_SUCCESS;
  ObArray<ObTenantBackupTaskInfo> task_infos;
  ObArray<ObLogArchiveBackupInfo> log_archive_infos;
  ObMySQLTransaction trans;
  ObTimeoutCtx timeout_ctx;
  const uint64_t tenant_id = OB_SYS_TENANT_ID;
  ObBackupCleanInfo sys_clean_info;
  sys_clean_info.tenant_id_ = tenant_id;
  const bool for_update = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    LOG_WARN("failed to start trans", K(ret));
  } else {
    if (OB_FAIL(get_backup_clean_info(tenant_id, for_update, trans, sys_clean_info))) {
      LOG_WARN("failed to get backup clean info", K(ret), K(sys_clean_info));
    } else if (ObBackupCleanInfoStatus::STOP == sys_clean_info.status_ ||
               ObBackupCleanInfoStatus::PREPARE == sys_clean_info.status_) {
      // do nothing
    } else if (OB_FAIL(get_tenant_backup_task_his_info(sys_clean_info, trans, task_infos))) {
      LOG_WARN("failed to get backup task his info", K(ret), K(sys_clean_info));
    } else if (OB_FAIL(get_tenant_backup_task_info(sys_clean_info, trans, task_infos))) {
      LOG_WARN("failed to get archive info", K(ret), K(sys_clean_info));
    } else if (OB_FAIL(get_log_archive_history_info(sys_clean_info.copy_id_, tenant_id, trans, log_archive_infos))) {
      LOG_WARN("failed to get log archive history info", K(ret), K(sys_clean_info));
    } else if (OB_FAIL(get_log_archive_info(sys_clean_info.copy_id_, tenant_id, trans, log_archive_infos))) {
      LOG_WARN("failed to get archive info", K(ret), K(sys_clean_info));
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
      }
    } else {
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
      }
    }

    if (OB_SUCC(ret)) {
      // add task info backup dest
      ObClusterBackupDest cluster_backup_dest;
      for (int64_t i = 0; OB_SUCC(ret) && i < task_infos.count(); ++i) {
        cluster_backup_dest.reset();
M
mw0 已提交
5368
        const ObTenantBackupTaskInfo &task_info = task_infos.at(i);
5369 5370 5371 5372 5373 5374 5375 5376 5377 5378
        if (OB_FAIL(cluster_backup_dest.set(task_info.backup_dest_, task_info.incarnation_))) {
          LOG_WARN("failed to set cluster backup dest", K(ret), K(task_info));
        } else if (OB_FAIL(cluster_backup_dest_set.set_refactored(cluster_backup_dest))) {
          LOG_WARN("failed to set cluster backup dest", K(ret), K(cluster_backup_dest));
        }
      }

      // add log archive dest
      for (int64_t i = 0; OB_SUCC(ret) && i < log_archive_infos.count(); ++i) {
        cluster_backup_dest.reset();
M
mw0 已提交
5379
        const ObLogArchiveBackupInfo &archive_info = log_archive_infos.at(i);
5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390
        if (OB_FAIL(cluster_backup_dest.set(archive_info.backup_dest_, archive_info.status_.incarnation_))) {
          LOG_WARN("failed to set cluster backup dest", K(ret), K(archive_info));
        } else if (OB_FAIL(cluster_backup_dest_set.set_refactored(cluster_backup_dest))) {
          LOG_WARN("failed to set cluster backup dest", K(ret), K(cluster_backup_dest));
        }
      }
    }
  }
  return ret;
}

M
mw0 已提交
5391 5392 5393
int ObBackupDataClean::do_scheduler_normal_tenant(share::ObBackupCleanInfo &clean_info,
    ObBackupDataCleanTenant &clean_tenant, common::ObIArray<ObTenantBackupTaskInfo> &task_infos,
    common::ObIArray<ObLogArchiveBackupInfo> &log_archive_infos)
5394 5395 5396 5397 5398
{
  int ret = OB_SUCCESS;
  clean_tenant.backup_element_array_.reset();
  ObBackupCleanInfo sys_clean_info;
  clean_info.reset();
M
mw0 已提交
5399
  ObSimpleBackupDataCleanTenant &simple_clean_tenant = clean_tenant.simple_clean_tenant_;
5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423

  const bool for_update = false;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_tenant.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("do tenant clean scheduler get invalid argument", K(ret), K(clean_tenant));
  } else {
    uint64_t tenant_id = simple_clean_tenant.is_deleted_ ? OB_SYS_TENANT_ID : simple_clean_tenant.tenant_id_;
    clean_info.tenant_id_ = simple_clean_tenant.tenant_id_;
    if (OB_FAIL(get_backup_clean_info(tenant_id, for_update, *sql_proxy_, clean_info))) {
      LOG_WARN("failed to get backup clean info", K(ret), K(clean_tenant));
    } else if (ObBackupCleanInfoStatus::STOP == clean_info.status_ ||
               ObBackupCleanInfoStatus::PREPARE == clean_info.status_) {
      // do nothing
    } else if (OB_FAIL(get_all_tenant_backup_infos(
                   clean_info, clean_tenant.simple_clean_tenant_, *sql_proxy_, task_infos, log_archive_infos))) {
      LOG_WARN("failed to get all tenant backup infos", K(ret), K(clean_info));
    }
  }
  return ret;
}

M
mw0 已提交
5424 5425 5426
int ObBackupDataClean::do_scheduler_deleted_tenant(share::ObBackupCleanInfo &clean_info,
    ObBackupDataCleanTenant &clean_tenant, common::ObIArray<ObTenantBackupTaskInfo> &task_infos,
    common::ObIArray<ObLogArchiveBackupInfo> &log_archive_infos)
5427 5428 5429 5430 5431
{
  int ret = OB_SUCCESS;
  clean_tenant.backup_element_array_.reset();
  ObBackupCleanInfo sys_clean_info;
  clean_info.reset();
M
mw0 已提交
5432
  ObSimpleBackupDataCleanTenant &simple_clean_tenant = clean_tenant.simple_clean_tenant_;
5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456
  const bool for_update = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_tenant.is_valid() || !clean_tenant.simple_clean_tenant_.is_deleted_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("do tenant clean scheduler get invalid argument", K(ret), K(clean_tenant));
  } else {
    const uint64_t tenant_id = OB_SYS_TENANT_ID;
    clean_info.tenant_id_ = simple_clean_tenant.tenant_id_;
    if (OB_FAIL(get_backup_clean_info(tenant_id, for_update, *sql_proxy_, clean_info))) {
      LOG_WARN("failed to get backup clean info", K(ret), K(clean_tenant));
    } else if (ObBackupCleanInfoStatus::STOP == clean_info.status_ ||
               ObBackupCleanInfoStatus::PREPARE == clean_info.status_) {
      // do nothing
    } else if (OB_FAIL(get_all_tenant_backup_infos(
                   clean_info, clean_tenant.simple_clean_tenant_, *sql_proxy_, task_infos, log_archive_infos))) {
      LOG_WARN("failed to get all tenant backup infos", K(ret), K(clean_info));
    }
  }
  return ret;
}

M
mw0 已提交
5457 5458 5459
int ObBackupDataClean::do_inner_scheduler_delete_tenant(const ObClusterBackupDest &cluster_backup_dest,
    ObBackupDataCleanTenant &clean_tenant, common::ObIArray<ObTenantBackupTaskInfo> &task_infos,
    common::ObIArray<ObLogArchiveBackupInfo> &log_archive_infos)
5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485
{
  int ret = OB_SUCCESS;
  ObExternBackupInfoMgr extern_backup_info_mgr;
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
  ObArray<ObExternBackupInfo> extern_backup_infos;
  ObExternLogArchiveBackupInfo archive_backup_info;
  ObArray<ObTenantLogArchiveStatus> status_array;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!cluster_backup_dest.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("do inner scheduler delete tenant get invalid argument", K(ret), K(cluster_backup_dest));
  } else if (OB_FAIL(extern_backup_info_mgr.init(
                 clean_tenant.simple_clean_tenant_.tenant_id_, cluster_backup_dest, *backup_lease_service_))) {
    LOG_WARN("failed to init extern backup info mgr", K(ret), K(cluster_backup_dest), K(clean_tenant));
  } else if (OB_FAIL(extern_backup_info_mgr.get_extern_full_backup_infos(extern_backup_infos))) {
    LOG_WARN("failed to get extern full backup infos", K(ret), K(clean_tenant));
  } else if (OB_FAIL(log_archive_info_mgr.read_extern_log_archive_backup_info(
                 cluster_backup_dest, clean_tenant.simple_clean_tenant_.tenant_id_, archive_backup_info))) {
    LOG_WARN("failed to read extern log archive backup info", K(ret), K(cluster_backup_dest));
  } else if (OB_FAIL(archive_backup_info.get_log_archive_status(status_array))) {
    LOG_WARN("failed to get log archive status", K(ret), K(clean_tenant));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < extern_backup_infos.count(); ++i) {
M
mw0 已提交
5486
      const ObExternBackupInfo &extern_backup_info = extern_backup_infos.at(i);
5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506
      ObTenantBackupTaskInfo task_info;
      task_info.cluster_id_ = cluster_backup_dest.cluster_id_;
      task_info.tenant_id_ = clean_tenant.simple_clean_tenant_.tenant_id_;
      task_info.backup_set_id_ = extern_backup_info.full_backup_set_id_;
      task_info.incarnation_ = cluster_backup_dest.incarnation_;
      task_info.snapshot_version_ = extern_backup_info.backup_snapshot_version_;
      task_info.prev_full_backup_set_id_ = extern_backup_info.prev_full_backup_set_id_;
      task_info.prev_inc_backup_set_id_ = extern_backup_info.prev_inc_backup_set_id_;
      task_info.backup_type_.type_ = extern_backup_info.backup_type_;
      task_info.status_ = ObTenantBackupTaskInfo::FINISH;
      task_info.device_type_ = cluster_backup_dest.dest_.device_type_;
      task_info.backup_dest_ = cluster_backup_dest.dest_;
      task_info.encryption_mode_ = extern_backup_info.encryption_mode_;
      task_info.date_ = extern_backup_info.date_;
      if (OB_FAIL(task_infos.push_back(task_info))) {
        LOG_WARN("failed to push task info into array", K(ret), K(task_info));
      }
    }

    for (int64_t i = 0; OB_SUCC(ret) && i < status_array.count(); ++i) {
M
mw0 已提交
5507
      ObTenantLogArchiveStatus &status = status_array.at(i);
5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518
      // set delete tenant archive status stop
      status.status_ = ObLogArchiveStatus::STOP;
      ObLogArchiveBackupInfo archive_info;
      archive_info.status_ = status;
      if (OB_FAIL(
              cluster_backup_dest.dest_.get_backup_dest_str(archive_info.backup_dest_, OB_MAX_BACKUP_DEST_LENGTH))) {
        LOG_WARN("failed to get backup dest str", K(ret), K(cluster_backup_dest));
      } else if (OB_FAIL(log_archive_infos.push_back(archive_info))) {
        LOG_WARN("failed to push archive info into array", K(ret), K(archive_info));
      }
    }
O
oceanbase-admin 已提交
5519

5520 5521 5522 5523
    if (OB_SUCC(ret)) {
      LOG_INFO("succeed get task info and log archive info", K(task_infos), K(log_archive_infos));
    }
  }
O
oceanbase-admin 已提交
5524 5525 5526
  return ret;
}

M
mw0 已提交
5527
int ObBackupDataClean::check_backup_dest_lifecycle(const ObBackupDataCleanTenant &clean_tenant)
O
oceanbase-admin 已提交
5528 5529
{
  int ret = OB_SUCCESS;
5530 5531 5532 5533
  int tmp_ret = OB_SUCCESS;
  ObBackupDest current_backup_dest;
  char backup_dest_str[OB_MAX_BACKUP_DEST_LENGTH] = "";

O
oceanbase-admin 已提交
5534 5535 5536
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
5537 5538 5539
  } else if (OB_FAIL(GCONF.backup_dest.copy(backup_dest_str, sizeof(backup_dest_str)))) {
    LOG_WARN("failed to copy backup dest", K(ret));
  } else if (0 == strlen(backup_dest_str)) {
O
oceanbase-admin 已提交
5540
    // do nothing
5541 5542
  } else if (OB_FAIL(current_backup_dest.set(backup_dest_str))) {
    LOG_WARN("failed to set current backup dest", K(ret), K(backup_dest_str));
O
oceanbase-admin 已提交
5543
  } else {
5544
    for (int64_t i = 0; OB_SUCC(ret) && i < clean_tenant.backup_element_array_.count(); ++i) {
M
mw0 已提交
5545 5546
      const ObBackupDataCleanElement &data_clean_element = clean_tenant.backup_element_array_.at(i);
      const ObBackupDest &backup_dest = data_clean_element.backup_dest_;
5547 5548 5549 5550 5551
      if (current_backup_dest == backup_dest) {
        if (OB_SUCCESS !=
            (tmp_ret = ObBackupUtil::check_backup_dest_lifecycle(backup_dest, is_update_reserved_backup_timestamp_))) {
          LOG_WARN("failed to check backup dest lifecycle", K(ret), K(backup_dest));
        }
O
oceanbase-admin 已提交
5552 5553 5554
      }
    }
  }
5555 5556
  return ret;
}
O
oceanbase-admin 已提交
5557

M
mw0 已提交
5558 5559 5560 5561
int ObBackupDataClean::get_all_tenant_backup_infos(const share::ObBackupCleanInfo &clean_info,
    const ObSimpleBackupDataCleanTenant &simple_clean_tenant, common::ObISQLClient &trans,
    common::ObIArray<share::ObTenantBackupTaskInfo> &tenant_backup_infos,
    common::ObIArray<share::ObLogArchiveBackupInfo> &tenant_backup_log_infos)
5562 5563
{
  int ret = OB_SUCCESS;
O
oceanbase-admin 已提交
5564

5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get all tenant backup task infos get invalid argument", K(ret), K(clean_info));
  } else if (clean_info.is_backup_set_clean() || clean_info.is_delete_backup_piece() ||
             clean_info.is_delete_backup_round()) {
    if (OB_FAIL(get_delete_backup_infos(
            clean_info, simple_clean_tenant, trans, tenant_backup_infos, tenant_backup_log_infos))) {
      LOG_WARN("failed to get delete backupset task infos", K(ret), K(clean_info));
O
oceanbase-admin 已提交
5576
    }
5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590
  } else if (clean_info.is_delete_obsolete_backup()) {
    if (OB_FAIL(get_delete_obsolete_backup_set_infos(
            clean_info, simple_clean_tenant, trans, tenant_backup_infos, tenant_backup_log_infos))) {
      LOG_WARN("failed to get delete obsolete backup set infos", K(ret), K(clean_info));
    }
  } else if (clean_info.is_delete_obsolete_backup_backup()) {
    if (OB_FAIL(get_delete_obsolete_backup_backupset_infos(
            clean_info, simple_clean_tenant, trans, tenant_backup_infos, tenant_backup_log_infos))) {
      LOG_WARN("failed to get delete obsolete backup backupset infos", K(ret), K(clean_info));
    }
  } else {
    // TODO(muwei.ym) add delete backup piece && auto delete
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("backup clean type is unexpected", K(clean_info));
O
oceanbase-admin 已提交
5591 5592 5593 5594
  }
  return ret;
}

M
mw0 已提交
5595 5596 5597 5598
int ObBackupDataClean::get_delete_backup_infos(const share::ObBackupCleanInfo &clean_info,
    const ObSimpleBackupDataCleanTenant &simple_clean_tenant, common::ObISQLClient &trans,
    common::ObIArray<share::ObTenantBackupTaskInfo> &tenant_backup_infos,
    common::ObIArray<share::ObLogArchiveBackupInfo> &tenant_backup_log_infos)
O
oceanbase-admin 已提交
5599 5600
{
  int ret = OB_SUCCESS;
5601 5602
  tenant_backup_infos.reset();
  tenant_backup_log_infos.reset();
O
oceanbase-admin 已提交
5603 5604 5605 5606

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
5607 5608 5609 5610 5611 5612 5613 5614 5615
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get delete backupset task infos get invalid argument", K(ret), K(clean_info));
  } else if (0 == clean_info.copy_id_) {
    if (OB_FAIL(get_tenant_backup_task_infos(clean_info, simple_clean_tenant, trans, tenant_backup_infos))) {
      LOG_WARN("failed to get tenant backup task infos", K(ret), K(clean_info));
    } else if (OB_FAIL(get_tenant_backup_log_infos(clean_info, simple_clean_tenant, trans, tenant_backup_log_infos))) {
      LOG_WARN("failed to get tenant backup piece infos", K(ret), K(clean_info));
    }
O
oceanbase-admin 已提交
5616
  } else {
5617 5618 5619 5620 5621
    if (OB_FAIL(get_tenant_backup_backupset_task_infos(clean_info, simple_clean_tenant, trans, tenant_backup_infos))) {
      LOG_WARN("failed to get tenant backup backupset task infos", K(ret), K(clean_info));
    } else if (OB_FAIL(get_tenant_backup_backuplog_infos(
                   clean_info, simple_clean_tenant, trans, tenant_backup_log_infos))) {
      LOG_WARN("failed to get tenant backup backuppiece infos", K(ret), K(clean_info));
O
oceanbase-admin 已提交
5622
    }
5623 5624 5625
  }
  return ret;
}
O
oceanbase-admin 已提交
5626

M
mw0 已提交
5627 5628 5629 5630
int ObBackupDataClean::get_delete_obsolete_backup_set_infos(const share::ObBackupCleanInfo &clean_info,
    const ObSimpleBackupDataCleanTenant &simple_clean_tenant, common::ObISQLClient &trans,
    common::ObIArray<share::ObTenantBackupTaskInfo> &tenant_backup_infos,
    common::ObIArray<share::ObLogArchiveBackupInfo> &tenant_backup_log_infos)
5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645
{
  int ret = OB_SUCCESS;
  tenant_backup_infos.reset();
  tenant_backup_log_infos.reset();

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret), K(clean_info));
  } else if (!clean_info.is_valid() || !clean_info.is_delete_obsolete_backup()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get delete backupset task infos get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(get_tenant_backup_task_infos(clean_info, simple_clean_tenant, trans, tenant_backup_infos))) {
    LOG_WARN("failed to get tenant backup task infos", K(ret), K(clean_info));
  } else if (OB_FAIL(get_tenant_backup_log_infos(clean_info, simple_clean_tenant, trans, tenant_backup_log_infos))) {
    LOG_WARN("failed to get tenant backup piece infos", K(ret), K(clean_info));
O
oceanbase-admin 已提交
5646 5647 5648 5649
  }
  return ret;
}

M
mw0 已提交
5650 5651 5652 5653
int ObBackupDataClean::get_delete_obsolete_backup_backupset_infos(const share::ObBackupCleanInfo &clean_info,
    const ObSimpleBackupDataCleanTenant &simple_clean_tenant, common::ObISQLClient &trans,
    common::ObIArray<share::ObTenantBackupTaskInfo> &tenant_backup_infos,
    common::ObIArray<share::ObLogArchiveBackupInfo> &tenant_backup_log_infos)
O
oceanbase-admin 已提交
5654 5655
{
  int ret = OB_SUCCESS;
5656 5657 5658
  tenant_backup_infos.reset();
  tenant_backup_log_infos.reset();

O
oceanbase-admin 已提交
5659 5660
  if (!is_inited_) {
    ret = OB_NOT_INIT;
5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674
    LOG_WARN("backup data clean do not init", K(ret), K(clean_info));
  } else if (!clean_info.is_valid() || !clean_info.is_delete_obsolete_backup_backup()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get delete backupset task infos get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(
                 get_tenant_backup_backupset_task_infos(clean_info, simple_clean_tenant, trans, tenant_backup_infos))) {
    LOG_WARN("failed to get tenant backup task infos", K(ret), K(clean_info));
  } else if (OB_FAIL(
                 get_tenant_backup_backuplog_infos(clean_info, simple_clean_tenant, trans, tenant_backup_log_infos))) {
    LOG_WARN("failed to get tenant backup piece infos", K(ret), K(clean_info));
  }
  return ret;
}

M
mw0 已提交
5675 5676 5677
int ObBackupDataClean::get_tenant_backup_task_infos(const share::ObBackupCleanInfo &clean_info,
    const ObSimpleBackupDataCleanTenant &simple_clean_tenant, common::ObISQLClient &trans,
    common::ObIArray<share::ObTenantBackupTaskInfo> &tenant_infos)
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694
{
  int ret = OB_SUCCESS;
  ObTenantBackupTaskUpdater updater;
  ObBackupTaskHistoryUpdater his_updater;
  ObArray<share::ObTenantBackupTaskInfo> tmp_tenant_infos;
  ObTenantBackupTaskInfo tenant_info;
  const bool is_backup_backup = false;
  const bool for_update = false;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get tenant backup task his info get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(his_updater.init(trans))) {
    LOG_WARN("failed to init backup task history updater", K(ret));
O
oceanbase-admin 已提交
5695
  } else if (OB_FAIL(updater.init(trans))) {
5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709
    LOG_WARN("failed to init backup task updater", K(ret));
  } else if (OB_FAIL(his_updater.get_tenant_backup_tasks(clean_info.tenant_id_, is_backup_backup, tmp_tenant_infos))) {
    LOG_WARN("failed to get tenant full backup tasks", K(ret), K(clean_info));
  } else if (!simple_clean_tenant.is_deleted_) {
    if (OB_FAIL(updater.get_tenant_backup_task(clean_info.tenant_id_, for_update, tenant_info))) {
      LOG_WARN("failed to get tenant backup tasks", K(ret), K(clean_info));
    } else if (tenant_info.is_valid() && OB_FAIL(tmp_tenant_infos.push_back(tenant_info))) {
      LOG_WARN("failed to push tenant info into array", K(ret), K(tenant_info));
    }
  }

  if (OB_FAIL(ret)) {
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < tmp_tenant_infos.count(); ++i) {
M
mw0 已提交
5710
      const ObTenantBackupTaskInfo &tenant_task_info = tmp_tenant_infos.at(i);
5711 5712 5713 5714 5715 5716 5717 5718 5719
      if (clean_info.is_delete_obsolete_backup() && tenant_task_info.backup_dest_ != backup_dest_) {
        // do nothing
      } else if (OB_FAIL(tenant_infos.push_back(tenant_task_info))) {
        LOG_WARN("failed to push tenant task info into array", K(ret), K(tenant_task_info));
      }
    }
  }
  if (OB_SUCC(ret)) {
    LOG_INFO("succeed get tenant backup task his info", K(tenant_infos));
O
oceanbase-admin 已提交
5720 5721 5722 5723
  }
  return ret;
}

M
mw0 已提交
5724 5725 5726
int ObBackupDataClean::get_tenant_backup_backupset_task_infos(const share::ObBackupCleanInfo &clean_info,
    const ObSimpleBackupDataCleanTenant &simple_clean_tenant, common::ObISQLClient &trans,
    common::ObIArray<share::ObTenantBackupTaskInfo> &tenant_infos)
O
oceanbase-admin 已提交
5727 5728
{
  int ret = OB_SUCCESS;
5729 5730 5731 5732 5733 5734 5735 5736 5737
  ObArray<share::ObTenantBackupBackupsetTaskInfo> tenant_backupset_infos;
  ObArray<share::ObTenantBackupTaskInfo> his_tenant_backupset_infos;
  SimpleBackupBackupsetTenant simple_tenant;
  simple_tenant.tenant_id_ = simple_clean_tenant.tenant_id_;
  simple_tenant.is_dropped_ = simple_clean_tenant.is_deleted_;
  const bool is_backup_backup = true;
  ObBackupTaskHistoryUpdater his_updater;
  ObTenantBackupTaskInfo prev_task_info;
  ObTenantBackupTaskInfo tenant_info;
O
oceanbase-admin 已提交
5738 5739 5740

  if (!is_inited_) {
    ret = OB_NOT_INIT;
5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get tenant backup task his info get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(his_updater.init(trans))) {
    LOG_WARN("failed to init his updater", K(ret));
  } else if (OB_FAIL(his_updater.get_tenant_backup_tasks(
                 clean_info.tenant_id_, is_backup_backup, his_tenant_backupset_infos))) {
    LOG_WARN("failed to get tenant backup tasks", K(ret), K(clean_info));
  } else if (OB_FAIL(ObTenantBackupBackupsetOperator::get_task_items(simple_tenant, tenant_backupset_infos, trans))) {
    LOG_WARN("failed to get full backupset task items", KR(ret), K(clean_info));
O
oceanbase-admin 已提交
5752
  } else {
5753
    for (int64_t i = 0; OB_SUCC(ret) && i < his_tenant_backupset_infos.count(); ++i) {
M
mw0 已提交
5754
      const ObTenantBackupTaskInfo &backup_task_info = his_tenant_backupset_infos.at(i);
5755 5756 5757 5758 5759
      if ((clean_info.is_delete_obsolete_backup_backup() && backup_task_info.backup_dest_ != backup_backup_dest_) ||
          (clean_info.copy_id_ > 0 && backup_task_info.copy_id_ != clean_info.copy_id_)) {
        // do nothing
      } else if (OB_FAIL(tenant_infos.push_back(backup_task_info))) {
        LOG_WARN("failed to push back backup info", KR(ret), K(tenant_info));
O
oceanbase-admin 已提交
5760 5761 5762
      }
    }

5763 5764
    for (int64_t i = 0; OB_SUCC(ret) && i < tenant_backupset_infos.count(); ++i) {
      tenant_info.reset();
M
mw0 已提交
5765
      const ObTenantBackupBackupsetTaskInfo &backupset_info = tenant_backupset_infos.at(i);
5766 5767 5768 5769 5770 5771 5772
      if (OB_FAIL(backupset_info.convert_to_backup_task_info(tenant_info))) {
        LOG_WARN("failed to convert to backup task info", KR(ret), K(backupset_info));
      } else if ((clean_info.is_delete_obsolete_backup_backup() && tenant_info.backup_dest_ != backup_backup_dest_) ||
                 (clean_info.copy_id_ > 0 && tenant_info.copy_id_ != clean_info.copy_id_)) {
        // do nothing
      } else if (OB_FAIL(tenant_infos.push_back(tenant_info))) {
        LOG_WARN("failed to push back backup info", KR(ret), K(tenant_info));
O
oceanbase-admin 已提交
5773
      }
5774 5775
    }

M
mw0 已提交
5776 5777 5778
    if (OB_SUCC(ret)) {
      if (OB_FAIL(duplicate_task_info(tenant_infos))) {
        LOG_WARN("failed to duplicate task infos", K(ret));
O
oceanbase-admin 已提交
5779 5780 5781 5782 5783 5784
      }
    }
  }
  return ret;
}

M
mw0 已提交
5785 5786 5787
int ObBackupDataClean::get_tenant_backup_log_infos(const share::ObBackupCleanInfo &clean_info,
    const ObSimpleBackupDataCleanTenant &simple_clean_tenant, common::ObISQLClient &trans,
    common::ObIArray<ObLogArchiveBackupInfo> &tenant_backup_log_infos)
O
oceanbase-admin 已提交
5788 5789
{
  int ret = OB_SUCCESS;
5790 5791 5792 5793 5794
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
  const bool for_update = false;
  ObArray<ObLogArchiveBackupInfo> tmp_log_archive_infos;
  ObLogArchiveBackupInfo tmp_log_archive_info;

O
oceanbase-admin 已提交
5795 5796
  if (!is_inited_) {
    ret = OB_NOT_INIT;
5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819
    LOG_WARN("backup data clean do not init", K(ret), K(clean_info));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get delete backupset task infos get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(log_archive_info_mgr.get_log_archive_history_infos(
                 trans, clean_info.tenant_id_, for_update, tmp_log_archive_infos))) {
    LOG_WARN("failed to get log arhive history infos", K(ret), K(clean_info));
  } else if (!simple_clean_tenant.is_deleted_) {
    if (OB_FAIL(log_archive_info_mgr.get_log_archive_backup_info(
            trans, for_update, clean_info.tenant_id_, inner_table_version_, tmp_log_archive_info))) {
      if (OB_ENTRY_NOT_EXIST == ret) {
        ret = OB_SUCCESS;
      } else {
        LOG_WARN("failed to get log archive backup info", K(ret), K(clean_info));
      }
    } else if (ObLogArchiveStatus::STOP == tmp_log_archive_info.status_.status_) {
      // do nothing
    } else if (OB_FAIL(tmp_log_archive_infos.push_back(tmp_log_archive_info))) {
      LOG_WARN("failed to push log archive info into array", K(ret), K(tmp_log_archive_info));
    }
  }

  if (OB_FAIL(ret)) {
O
oceanbase-admin 已提交
5820
  } else {
5821
    for (int64_t i = 0; OB_SUCC(ret) && i < tmp_log_archive_infos.count(); ++i) {
M
mw0 已提交
5822
      const ObLogArchiveBackupInfo &log_archive_info = tmp_log_archive_infos.at(i);
5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833
      ObBackupDest backup_dest;
      if (ObLogArchiveStatus::DOING != log_archive_info.status_.status_ &&
          ObLogArchiveStatus::INTERRUPTED != log_archive_info.status_.status_ &&
          ObLogArchiveStatus::STOP != log_archive_info.status_.status_) {
        // do nothing
      } else if (OB_FAIL(backup_dest.set(log_archive_info.backup_dest_))) {
        LOG_WARN("failed to set backup dest", K(ret), K(log_archive_info));
      } else if (clean_info.is_delete_obsolete_backup() && backup_dest_ != backup_dest) {
        // do nothing
      } else if (OB_FAIL(tenant_backup_log_infos.push_back(log_archive_info))) {
        LOG_WARN("failed to push log archive info into array", K(ret), K(log_archive_info));
O
oceanbase-admin 已提交
5834 5835 5836 5837 5838 5839
      }
    }
  }
  return ret;
}

5840 5841 5842 5843 5844 5845
// step1 get backup backup log info from piece info if exist
// step2 get log archive info from log_archive_history if exist
// step3 get log archive info from backup log table if exist
// NOTE:step1 get log archive info from piece because if piece_interval > 0,
// backup backup log will not record log archive info into history, so we use piece infos to get log archive info

M
mw0 已提交
5846 5847 5848
int ObBackupDataClean::get_tenant_backup_backuplog_infos(const share::ObBackupCleanInfo &clean_info,
    const ObSimpleBackupDataCleanTenant &simple_clean_tenant, common::ObISQLClient &trans,
    common::ObIArray<ObLogArchiveBackupInfo> &tenant_backup_log_infos)
O
oceanbase-admin 已提交
5849 5850
{
  int ret = OB_SUCCESS;
5851 5852 5853 5854
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
  const bool for_update = false;
  ObArray<ObLogArchiveBackupInfo> tmp_log_archive_infos;
  const int64_t GCONF_BACKUP_COPY_ID = 10001;
O
oceanbase-admin 已提交
5855 5856 5857

  if (!is_inited_) {
    ret = OB_NOT_INIT;
5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889
    LOG_WARN("backup data clean do not init", K(ret), K(clean_info));
  } else if (!clean_info.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get delete backupset task infos get invalid argument", K(ret), K(clean_info));
  } else if (clean_info.is_delete_backup_round()) {
    ret = OB_NOT_SUPPORTED;
    LOG_WARN("clean info is delete backup backup round, not support now", K(ret), K(clean_info));
  } else if (clean_info.is_delete_backup_set() && clean_info.copy_id_ > GCONF_BACKUP_COPY_ID) {
    // do nothing
  } else if (clean_info.is_delete_backup_piece()) {
    ObLogArchiveBackupInfo tmp_log_archive_info;
    if (OB_FAIL(log_archive_info_mgr.get_backup_log_archive_info_from_piece_info(trans,
            simple_clean_tenant.tenant_id_,
            clean_info.backup_piece_id_,
            clean_info.copy_id_,
            for_update,
            tmp_log_archive_info))) {
      if (OB_ITER_END == ret) {
        ret = OB_SUCCESS;
      } else {
        LOG_WARN("failed to get backup log archive info from piece info", K(ret), K(clean_info));
      }
    } else if (OB_FAIL(tenant_backup_log_infos.push_back(tmp_log_archive_info))) {
      LOG_WARN("failed to push log archive info into array", K(ret), K(clean_info), K(tmp_log_archive_info));
    }
  } else {
    // clean info delete obsolete backup backup
    if (OB_FAIL(log_archive_info_mgr.get_backup_log_archive_info_from_original_piece_infos(
            trans, simple_clean_tenant.tenant_id_, for_update, tmp_log_archive_infos))) {
      LOG_WARN("failed to get backup log archive info from piece infos", K(ret), K(clean_info));
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < tmp_log_archive_infos.count(); ++i) {
M
mw0 已提交
5890
        const ObLogArchiveBackupInfo &log_archive_info = tmp_log_archive_infos.at(i);
5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904
        ObBackupDest backup_dest;
        if (ObLogArchiveStatus::DOING != log_archive_info.status_.status_ &&
            ObLogArchiveStatus::INTERRUPTED != log_archive_info.status_.status_ &&
            ObLogArchiveStatus::STOP != log_archive_info.status_.status_) {
          // do nothing
        } else if (OB_FAIL(backup_dest.set(log_archive_info.backup_dest_))) {
          LOG_WARN("failed to set backup dest", K(ret), K(log_archive_info));
        } else if (clean_info.is_delete_obsolete_backup_backup() && backup_backup_dest_ != backup_dest) {
          // do nothing
        } else if (OB_FAIL(tenant_backup_log_infos.push_back(log_archive_info))) {
          LOG_WARN("failed to push log archive info into array", K(ret), K(log_archive_info));
        }
      }
    }
O
oceanbase-admin 已提交
5905
  }
5906

O
oceanbase-admin 已提交
5907 5908 5909
  return ret;
}

M
mw0 已提交
5910
int ObBackupDataClean::get_backup_dest_option(const ObBackupDest &backup_dest, ObBackupDestOpt &backup_dest_option)
O
oceanbase-admin 已提交
5911 5912
{
  int ret = OB_SUCCESS;
5913
  backup_dest_option.reset();
O
oceanbase-admin 已提交
5914 5915
  if (!is_inited_) {
    ret = OB_NOT_INIT;
5916 5917
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!backup_dest.is_valid()) {
O
oceanbase-admin 已提交
5918
    ret = OB_INVALID_ARGUMENT;
5919 5920 5921 5922 5923
    LOG_WARN("get backup dest option get invalid argument", K(ret), K(backup_dest));
  } else if (backup_dest == backup_dest_) {
    backup_dest_option = backup_dest_option_;
  } else if (backup_dest == backup_backup_dest_) {
    backup_dest_option = backup_backup_dest_option_;
O
oceanbase-admin 已提交
5924
  } else {
5925 5926 5927 5928 5929
    backup_dest_option.auto_delete_obsolete_backup_ = true;
    backup_dest_option.is_valid_ = true;
  }
  return ret;
}
O
oceanbase-admin 已提交
5930

5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951
int ObBackupDataClean::set_current_backup_dest()
{
  int ret = OB_SUCCESS;
  const int64_t backup_dest_buf_len = OB_MAX_BACKUP_DEST_LENGTH;
  char backup_dest_buf[backup_dest_buf_len] = "";
  char backup_backup_dest_buf[backup_dest_buf_len] = "";

  backup_dest_.reset();
  backup_backup_dest_.reset();
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_FAIL(GCONF.backup_dest.copy(backup_dest_buf, backup_dest_buf_len))) {
    LOG_WARN("failed to copy backup dest", K(ret));
  } else if (0 == strlen(backup_dest_buf)) {
    // do nothing
  } else if (OB_FAIL(backup_dest_.set(backup_dest_buf))) {
    LOG_WARN("failed to set backup dest", K(ret), K(backup_dest_buf));
  } else if (OB_FAIL(backup_dest_option_.init(false /*is_backup_backup*/))) {
    LOG_WARN("failed to init backup dest option", K(ret));
  }
O
oceanbase-admin 已提交
5952

5953 5954 5955 5956 5957 5958 5959 5960 5961
  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(GCONF.backup_backup_dest.copy(backup_backup_dest_buf, backup_dest_buf_len))) {
    LOG_WARN("failed to copy backup dest", K(ret));
  } else if (0 == strlen(backup_backup_dest_buf)) {
    // do nothing
  } else if (OB_FAIL(backup_backup_dest_.set(backup_backup_dest_buf))) {
    LOG_WARN("failed to set backup dest", K(ret), K(backup_dest_buf));
  } else if (OB_FAIL(backup_backup_dest_option_.init(true /*is_backup_backup*/))) {
    LOG_WARN("failed to init backup dest option", K(ret));
O
oceanbase-admin 已提交
5962
  }
5963

O
oceanbase-admin 已提交
5964 5965 5966
  return ret;
}

5967
int ObBackupDataClean::get_clean_tenants_from_history_table(
M
mw0 已提交
5968
    const ObBackupCleanInfo &clean_info, hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> &clean_tenants_map)
O
oceanbase-admin 已提交
5969 5970
{
  int ret = OB_SUCCESS;
5971
  ObArray<ObTenantBackupTaskInfo> tenant_backup_tasks;
O
oceanbase-admin 已提交
5972 5973 5974

  if (!is_inited_) {
    ret = OB_NOT_INIT;
5975 5976
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid()) {
O
oceanbase-admin 已提交
5977
    ret = OB_INVALID_ARGUMENT;
5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990
    LOG_WARN("get clean tenants from history table get invalid argument", K(ret), K(clean_info));
  } else if (clean_info.is_delete_backup_set()) {
    if (OB_FAIL(get_delete_backup_set_tenants_from_history_table(clean_info, clean_tenants_map))) {
      LOG_WARN("failed to get delete backup set tenants from history table", K(ret), K(clean_info));
    }
  } else if (clean_info.is_delete_backup_piece()) {
    if (OB_FAIL(get_delete_backup_piece_tenants_from_history_table(clean_info, clean_tenants_map))) {
      LOG_WARN("failed to get delete backup piece tenants from history table", K(ret), K(clean_info));
    }
  } else if (clean_info.is_delete_backup_round()) {
    if (get_delete_backup_round_tenants_from_history_table(clean_info, clean_tenants_map)) {
      LOG_WARN("failed to get delete backup round tenants from history table", K(ret), K(clean_info));
    }
O
oceanbase-admin 已提交
5991
  } else {
5992 5993
    if (OB_FAIL(get_delete_obsolete_backup_tenants_from_history_table(clean_info, clean_tenants_map))) {
      LOG_WARN("failed to get delete obsolete backup tenants from history table", K(ret), K(clean_info));
O
oceanbase-admin 已提交
5994 5995 5996 5997 5998
    }
  }
  return ret;
}

5999
int ObBackupDataClean::get_delete_backup_set_tenants_from_history_table(
M
mw0 已提交
6000
    const ObBackupCleanInfo &clean_info, hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> &clean_tenants_map)
O
oceanbase-admin 已提交
6001 6002
{
  int ret = OB_SUCCESS;
6003 6004 6005
  const bool for_update = false;
  ObArray<uint64_t> tenant_ids;
  ObBackupTaskHistoryUpdater updater;
O
oceanbase-admin 已提交
6006 6007 6008

  if (!is_inited_) {
    ret = OB_NOT_INIT;
6009 6010 6011 6012 6013 6014 6015 6016 6017 6018
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid() || !clean_info.is_backup_set_clean()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get delete backup set tenants from history table get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(updater.init(*sql_proxy_))) {
    LOG_WARN("failed to init task history updater", K(ret));
  } else if (OB_FAIL(updater.get_tenant_ids_with_backup_set_id(clean_info.backup_set_id_, for_update, tenant_ids))) {
    LOG_WARN("failed to get tenant backup tasks", K(ret), K(clean_info));
  } else if (OB_FAIL(set_history_tenant_info_into_map(tenant_ids, clean_tenants_map))) {
    LOG_WARN("failed to set history tenant info into map", K(ret), K(clean_info));
O
oceanbase-admin 已提交
6019 6020 6021 6022
  }
  return ret;
}

6023
int ObBackupDataClean::get_delete_backup_piece_tenants_from_history_table(
M
mw0 已提交
6024
    const ObBackupCleanInfo &clean_info, hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> &clean_tenants_map)
O
oceanbase-admin 已提交
6025 6026
{
  int ret = OB_SUCCESS;
6027 6028
  ObArray<uint64_t> tenant_ids;
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
O
oceanbase-admin 已提交
6029 6030 6031

  if (!is_inited_) {
    ret = OB_NOT_INIT;
6032 6033
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid() || !clean_info.is_delete_backup_piece()) {
O
oceanbase-admin 已提交
6034
    ret = OB_INVALID_ARGUMENT;
6035 6036 6037 6038 6039 6040
    LOG_WARN("get delete backup set tenants from history table get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(log_archive_info_mgr.get_tenant_ids_with_piece_id(
                 *sql_proxy_, clean_info.backup_piece_id_, clean_info.copy_id_, tenant_ids))) {
    LOG_WARN("failed to get tenant ids with piece id", K(ret), K(clean_info));
  } else if (OB_FAIL(set_history_tenant_info_into_map(tenant_ids, clean_tenants_map))) {
    LOG_WARN("failed to set history tenant info into map", K(ret), K(clean_info));
O
oceanbase-admin 已提交
6041 6042 6043 6044
  }
  return ret;
}

6045
int ObBackupDataClean::get_delete_backup_round_tenants_from_history_table(
M
mw0 已提交
6046
    const ObBackupCleanInfo &clean_info, hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> &clean_tenants_map)
O
oceanbase-admin 已提交
6047 6048
{
  int ret = OB_SUCCESS;
6049 6050 6051
  const bool for_update = false;
  ObArray<uint64_t> tenant_ids;
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
O
oceanbase-admin 已提交
6052 6053
  if (!is_inited_) {
    ret = OB_NOT_INIT;
6054 6055 6056 6057 6058 6059 6060 6061 6062
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid() || !clean_info.is_delete_backup_round()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get delete backup set tenants from history table get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(log_archive_info_mgr.get_tenant_ids_with_round_id(
                 *sql_proxy_, for_update, clean_info.backup_round_id_, clean_info.copy_id_, tenant_ids))) {
    LOG_WARN("failed to get tenant ids with round id", K(ret), K(clean_info));
  } else if (OB_FAIL(set_history_tenant_info_into_map(tenant_ids, clean_tenants_map))) {
    LOG_WARN("failed to set history tenant info into map", K(ret), K(clean_info));
O
oceanbase-admin 已提交
6063 6064 6065 6066
  }
  return ret;
}

6067
int ObBackupDataClean::get_delete_obsolete_backup_tenants_from_history_table(
M
mw0 已提交
6068
    const ObBackupCleanInfo &clean_info, hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> &clean_tenants_map)
O
oceanbase-admin 已提交
6069 6070
{
  int ret = OB_SUCCESS;
6071 6072 6073 6074 6075
  ObArray<uint64_t> log_archive_tenant_ids;
  ObArray<uint64_t> backup_set_tenant_ids;
  ObBackupTaskHistoryUpdater updater;
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
  const bool is_backup_backup = clean_info.is_delete_obsolete_backup_backup();
O
oceanbase-admin 已提交
6076 6077 6078

  if (!is_inited_) {
    ret = OB_NOT_INIT;
6079 6080
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!clean_info.is_valid() || !clean_info.is_delete_obsolete()) {
O
oceanbase-admin 已提交
6081
    ret = OB_INVALID_ARGUMENT;
6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094
    LOG_WARN("get delete obsolete backup tenants from history table get invalid argument", K(ret), K(clean_info));
  } else if (OB_FAIL(log_archive_info_mgr.get_backup_tenant_ids_with_snapshot(
                 *sql_proxy_, clean_info.expired_time_, is_backup_backup, log_archive_tenant_ids))) {
    LOG_WARN("failed to get tenant ids with snapshot version", K(ret), K(clean_info));
  } else if (OB_FAIL(updater.init(*sql_proxy_))) {
    LOG_WARN("failed to init backup task history updater", K(ret), K(clean_info));
  } else if (OB_FAIL(updater.get_tenant_ids_with_snapshot_version(
                 clean_info.expired_time_, is_backup_backup, backup_set_tenant_ids))) {
    LOG_WARN("failed to get tenant ids with snapshot version", K(ret), K(clean_info));
  } else if (OB_FAIL(set_history_tenant_info_into_map(log_archive_tenant_ids, clean_tenants_map))) {
    LOG_WARN("failed to set history tenant info into map", K(ret), K(clean_info));
  } else if (OB_FAIL(set_history_tenant_info_into_map(backup_set_tenant_ids, clean_tenants_map))) {
    LOG_WARN("failed to set history tenant info into map", K(ret), K(clean_info));
O
oceanbase-admin 已提交
6095 6096 6097 6098
  }
  return ret;
}

M
mw0 已提交
6099 6100
int ObBackupDataClean::set_history_tenant_info_into_map(const common::ObIArray<uint64_t> &tenant_ids,
    hash::ObHashMap<uint64_t, ObSimpleBackupDataCleanTenant> &clean_tenants_map)
O
oceanbase-admin 已提交
6101 6102 6103 6104
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
6105
    LOG_WARN("backup data clean do not init", K(ret));
O
oceanbase-admin 已提交
6106
  } else {
6107 6108 6109 6110 6111 6112 6113 6114 6115
    ObSimpleBackupDataCleanTenant tmp_simple_clean_tenant;
    const int overwrite = 1;
    for (int64_t i = 0; OB_SUCC(ret) && i < tenant_ids.count(); ++i) {
      const uint64_t tenant_id = tenant_ids.at(i);
      tmp_simple_clean_tenant.reset();
      tmp_simple_clean_tenant.tenant_id_ = tenant_id;
      tmp_simple_clean_tenant.is_deleted_ = true;
      if (OB_FAIL(clean_tenants_map.set_refactored(tenant_id, tmp_simple_clean_tenant, overwrite))) {
        LOG_WARN("failed to set simple clean tenant into map", K(ret), K(tmp_simple_clean_tenant));
O
oceanbase-admin 已提交
6116 6117 6118 6119 6120 6121
      }
    }
  }
  return ret;
}

6122 6123 6124 6125 6126 6127
bool ObBackupDataClean::is_result_need_retry(const int32_t result)
{
  bool b_ret = false;
  const int64_t MAX_RETRY_COUNT = 64;
  if (OB_NOT_INIT != result && OB_INVALID_ARGUMENT != result && OB_ERR_SYS != result && OB_INIT_TWICE != result &&
      OB_ERR_UNEXPECTED != result && OB_TENANT_HAS_BEEN_DROPPED != result && OB_NOT_SUPPORTED != result &&
W
WenJinyu 已提交
6128 6129
      OB_BACKUP_DELETE_BACKUP_SET_NOT_ALLOWED != result && OB_BACKUP_DELETE_BACKUP_PIECE_NOT_ALLOWED != result && 
      OB_SUCCESS != result && OB_CANCELED != result &&
6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165
      retry_count_ < MAX_RETRY_COUNT) {
    // do nothing
    // TODO() Need to consider the limit of the number of retry
    // When the upper limit is reached, it needs to actively fail to
    // The scenario where the backup medium is not used is not considered here for the time being
    retry_count_++;
    b_ret = true;
  }
  return b_ret;
}

void ObBackupDataClean::set_inner_error(const int32_t result)
{
  if (OB_SUCCESS != result) {
    if (is_result_need_retry(result)) {
      wakeup();
    } else {
      inner_error_ = OB_SUCCESS == inner_error_ ? result : inner_error_;
    }
  }
}

bool ObBackupDataClean::get_prepare_flag() const
{
  const bool prepare_flag = ATOMIC_LOAD(&is_prepare_flag_);
  return prepare_flag;
}

void ObBackupDataClean::update_prepare_flag(const bool is_prepare_flag)
{
  const bool old_prepare_flag = get_prepare_flag();
  if (old_prepare_flag != is_prepare_flag) {
    ATOMIC_VCAS(&is_prepare_flag_, old_prepare_flag, is_prepare_flag);
  }
}

M
mw0 已提交
6166
int ObBackupDataClean::commit_trans(ObMySQLTransaction &trans)
O
oceanbase-admin 已提交
6167 6168
{
  int ret = OB_SUCCESS;
6169 6170 6171 6172 6173 6174 6175 6176 6177 6178
  int tmp_ret = OB_SUCCESS;

  if (OB_FAIL(backup_lease_service_->check_lease())) {
    LOG_WARN("failed to check can backup", K(ret));
  }

  tmp_ret = trans.end(OB_SUCC(ret));
  if (OB_SUCCESS != tmp_ret) {
    LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
    ret = OB_SUCCESS == ret ? tmp_ret : ret;
O
oceanbase-admin 已提交
6179 6180 6181 6182
  }
  return ret;
}

M
mw0 已提交
6183
int ObBackupDataClean::start_trans(ObTimeoutCtx &timeout_ctx, ObMySQLTransaction &trans)
O
oceanbase-admin 已提交
6184 6185
{
  int ret = OB_SUCCESS;
6186 6187 6188 6189 6190 6191 6192 6193
  const int64_t MAX_EXECUTE_TIMEOUT_US = 600L * 1000 * 1000;  // 600s
  int64_t stmt_timeout = MAX_EXECUTE_TIMEOUT_US;
  if (OB_FAIL(timeout_ctx.set_trx_timeout_us(stmt_timeout))) {
    LOG_WARN("fail to set trx timeout", K(ret), K(stmt_timeout));
  } else if (OB_FAIL(timeout_ctx.set_timeout(stmt_timeout))) {
    LOG_WARN("set timeout context failed", K(ret));
  } else if (OB_FAIL(trans.start(sql_proxy_))) {
    LOG_WARN("failed to start trans", K(ret));
O
oceanbase-admin 已提交
6194 6195 6196 6197
  }
  return ret;
}

6198
int ObBackupDataClean::check_can_delete_extern_info_file(const uint64_t tenant_id,
M
mw0 已提交
6199 6200
    const ObClusterBackupDest &current_backup_dest, const bool is_backup_backup, const ObBackupPath &path,
    bool &can_delete_file)
O
oceanbase-admin 已提交
6201 6202
{
  int ret = OB_SUCCESS;
6203 6204 6205 6206 6207 6208
  can_delete_file = false;
  bool is_dropped = false;
  bool is_exist = false;
  ObStorageUtil util(false /*need retry*/);

  if (!is_inited_) {
O
oceanbase-admin 已提交
6209
    ret = OB_NOT_INIT;
6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!current_backup_dest.is_valid() || path.is_empty()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("check can delete exten info file get invalid argument", K(ret), K(path), K(current_backup_dest));
  } else if (!is_backup_backup) {
    ObClusterBackupDest tmp_backup_dest;
    if (!backup_dest_.is_valid()) {
      can_delete_file = true;
    } else if (OB_FAIL(tmp_backup_dest.set(backup_dest_, current_backup_dest.incarnation_))) {
      LOG_WARN("failed to set cluster backup dest", K(ret), K(current_backup_dest));
    } else if (current_backup_dest.is_same(tmp_backup_dest)) {
      can_delete_file = false;
    }
O
oceanbase-admin 已提交
6223
  } else {
6224 6225 6226 6227 6228 6229 6230
    ObClusterBackupDest tmp_backup_dest;
    if (!backup_backup_dest_.is_valid()) {
      can_delete_file = true;
    } else if (OB_FAIL(tmp_backup_dest.set(backup_backup_dest_, current_backup_dest.incarnation_))) {
      LOG_WARN("failed to set cluster backup dest", K(ret), K(current_backup_dest));
    } else if (current_backup_dest.is_same(tmp_backup_dest)) {
      can_delete_file = false;
O
oceanbase-admin 已提交
6231
    }
6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251
  }

  if (OB_FAIL(ret)) {
    // do nothing
  } else if (can_delete_file) {
    // do nothing
  } else {
    if (OB_FAIL(schema_service_->check_if_tenant_has_been_dropped(tenant_id, is_dropped))) {
      LOG_WARN("failed to check if tenant has been dropped", K(ret), K(tenant_id));
    } else {
      can_delete_file = is_dropped;
    }

    if (OB_SUCC(ret) && can_delete_file) {
      if (!is_backup_backup) {
        // do nothing
      } else if (OB_FAIL(util.is_exist(path.get_ptr(), backup_dest_.storage_info_, is_exist))) {
        LOG_WARN("failed to check file exist", K(ret), K(path));
      } else {
        can_delete_file = !is_exist;
O
oceanbase-admin 已提交
6252 6253 6254 6255 6256 6257
      }
    }
  }
  return ret;
}

M
mw0 已提交
6258
int ObBackupDataClean::get_backup_set_file_copies_num(const ObTenantBackupTaskInfo &task_info, int64_t &copies_num)
O
oceanbase-admin 已提交
6259 6260
{
  int ret = OB_SUCCESS;
6261 6262 6263 6264
  ObBackupTaskHistoryUpdater updater;
  ObArray<ObBackupSetFileInfo> backup_set_file_infos;
  copies_num = 0;
  if (!is_inited_) {
O
oceanbase-admin 已提交
6265
    ret = OB_NOT_INIT;
6266 6267
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (!task_info.is_valid()) {
O
oceanbase-admin 已提交
6268
    ret = OB_INVALID_ARGUMENT;
6269 6270 6271 6272 6273 6274 6275 6276
    LOG_WARN("get backup set file copies num get invalid argument", K(ret), K(task_info));
  } else if (OB_FAIL(updater.init(*sql_proxy_))) {
    LOG_WARN("failed to init history updater", K(ret));
  } else if (OB_FAIL(updater.get_backup_set_file_info_copies(
                 task_info.tenant_id_, task_info.incarnation_, task_info.backup_set_id_, backup_set_file_infos))) {
    LOG_WARN("failed to get backup set file info copies", K(ret), K(task_info));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_file_infos.count(); ++i) {
M
mw0 已提交
6277
      const ObBackupSetFileInfo &backup_set_file_info = backup_set_file_infos.at(i);
6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296
      if (0 == backup_set_file_info.copy_id_) {
        if (ObBackupFileStatus::BACKUP_FILE_DELETED == backup_set_file_info.file_status_ ||
            OB_SUCCESS != backup_set_file_info.result_) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("backup set file status is unexpected", K(ret), K(backup_set_file_info));
        } else {
          copies_num++;
        }
      } else {
        if (ObBackupFileStatus::BACKUP_FILE_COPYING == backup_set_file_info.file_status_ ||
            ObBackupFileStatus::BACKUP_FILE_INCOMPLETE == backup_set_file_info.file_status_ ||
            OB_SUCCESS != backup_set_file_info.result_ ||
            backup_set_file_info.copy_id_ >= OB_TENANT_GCONF_DEST_START_COPY_ID) {
          // do nothing
        } else {
          copies_num++;
        }
      }
    }
O
oceanbase-admin 已提交
6297 6298 6299 6300
  }
  return ret;
}

M
mw0 已提交
6301
int ObBackupDataClean::get_backup_piece_file_copies_num(const ObBackupPieceInfo &backup_piece_info, int64_t &copies_num)
O
oceanbase-admin 已提交
6302 6303
{
  int ret = OB_SUCCESS;
6304 6305 6306
  ObLogArchiveBackupInfoMgr archive_backup_info_mgr;
  ObArray<ObBackupPieceInfo> backup_piece_infos;
  copies_num = 0;
O
oceanbase-admin 已提交
6307 6308 6309
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321
  } else if (!backup_piece_info.is_valid() || 0 != backup_piece_info.key_.copy_id_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("get backup piece file copies num get invalid argument", K(ret), K(backup_piece_info));
  } else if (OB_FAIL(archive_backup_info_mgr.get_tenant_backup_piece_infos(*sql_proxy_,
                 backup_piece_info.key_.incarnation_,
                 backup_piece_info.key_.tenant_id_,
                 backup_piece_info.key_.round_id_,
                 backup_piece_info.key_.backup_piece_id_,
                 backup_piece_infos))) {
    LOG_WARN("failed to get backup piece copy list", K(ret), K(backup_piece_info));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_piece_infos.count(); ++i) {
M
mw0 已提交
6322
      const ObBackupPieceInfo &tmp_piece_info = backup_piece_infos.at(i);
6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339
      if (0 == tmp_piece_info.key_.copy_id_) {
        if (ObBackupFileStatus::BACKUP_FILE_DELETED == tmp_piece_info.file_status_) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("backup piece info is unexpected", K(ret), K(tmp_piece_info));
        } else {
          copies_num++;
        }
      } else {
        if (ObBackupFileStatus::BACKUP_FILE_COPYING == tmp_piece_info.file_status_ ||
            ObBackupFileStatus::BACKUP_FILE_INCOMPLETE == tmp_piece_info.file_status_ ||
            tmp_piece_info.key_.copy_id_ >= OB_TENANT_GCONF_DEST_START_COPY_ID ||
            tmp_piece_info.checkpoint_ts_ < backup_piece_info.checkpoint_ts_) {
          // do nothing
        } else {
          copies_num++;
        }
      }
O
oceanbase-admin 已提交
6340 6341 6342 6343 6344
    }
  }
  return ret;
}

6345
int ObBackupDataClean::prepare_deleted_tenant_backup_infos()
O
oceanbase-admin 已提交
6346 6347
{
  int ret = OB_SUCCESS;
6348 6349 6350 6351 6352 6353 6354
  ObArray<uint64_t> tenant_ids;
  ObTenantNameSimpleMgr tenant_name_mgr;
  hash::ObHashSet<uint64_t> tenant_id_set;
  ObClusterBackupDest cluster_backup_dest;
  const int64_t MAX_BUCKET = 1024;
  ObArray<ObSimpleBackupDataCleanTenant> simple_tenants;

O
oceanbase-admin 已提交
6355 6356 6357
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375
  } else if (is_valid_backup_inner_table_version(inner_table_version_) &&
             inner_table_version_ >= OB_BACKUP_INNER_TABLE_V3) {
    // inner table version is new enough
    // do nothing
  } else if (!backup_dest_.is_valid()) {
    // do nothing
  } else if (OB_FAIL(get_all_tenant_ids(tenant_ids))) {
    LOG_WARN("failed to get all tenant ids", K(ret));
  } else if (OB_FAIL(cluster_backup_dest.set(backup_dest_, OB_START_INCARNATION))) {
    LOG_WARN("failed to set cluster backup dest", K(ret), K(backup_dest_));
  } else if (OB_FAIL(tenant_id_set.create(MAX_BUCKET))) {
    LOG_WARN("failed to create tenant is set", K(ret));
  } else if (OB_FAIL(tenant_name_mgr.init())) {
    LOG_WARN("failed to init tenant name mgr", K(ret));
  } else if (OB_FAIL(tenant_name_mgr.read_backup_file(cluster_backup_dest))) {
    LOG_WARN("failed to read backup file", K(ret));
  } else if (OB_FAIL(tenant_name_mgr.get_tenant_ids(tenant_id_set))) {
    LOG_WARN("failed to get tenant ids", K(ret));
O
oceanbase-admin 已提交
6376
  } else {
6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387
    for (int64_t i = 0; OB_SUCC(ret) && i < tenant_ids.count(); ++i) {
      const uint64_t tenant_id = tenant_ids.at(i);
      int hash_ret = tenant_id_set.exist_refactored(tenant_id);
      if (OB_HASH_EXIST == hash_ret) {
        if (OB_FAIL(tenant_id_set.erase_refactored(tenant_id))) {
          LOG_WARN("failed to erase tenant id", K(ret), K(tenant_id));
        }
      } else if (OB_HASH_NOT_EXIST == hash_ret) {
        // do nothing
      } else {
        ret = hash_ret;
O
oceanbase-admin 已提交
6388
      }
6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424
      if (OB_SUCC(ret)) {
        ObSimpleBackupDataCleanTenant simple_tenant;
        simple_tenant.tenant_id_ = tenant_id;
        simple_tenant.is_deleted_ = false;
        if (OB_FAIL(simple_tenants.push_back(simple_tenant))) {
          LOG_WARN("failed to push simple tenant into array", K(ret), K(simple_tenant));
        }
      }
    }

    if (OB_SUCC(ret)) {
      // add delete backup tenant
      for (hash::ObHashSet<uint64_t>::iterator iter = tenant_id_set.begin();
           OB_SUCC(ret) && iter != tenant_id_set.end();
           ++iter) {
        const uint64_t tenant_id = iter->first;
        ObSimpleBackupDataCleanTenant simple_tenant;
        simple_tenant.tenant_id_ = tenant_id;
        simple_tenant.is_deleted_ = true;
        if (OB_FAIL(simple_tenants.push_back(simple_tenant))) {
          LOG_WARN("failed to push simple tenant into array", K(ret), K(simple_tenant));
        }
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(add_backup_infos_for_compatible(cluster_backup_dest, simple_tenants))) {
        LOG_WARN("failed to add backup infos for compatible", K(ret), K(cluster_backup_dest));
      } else if (OB_FAIL(add_log_archive_infos_for_compatible(cluster_backup_dest, simple_tenants))) {
        LOG_WARN("failed to add log archive infos for compatible", K(ret), K(cluster_backup_dest));
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(upgrade_backup_info())) {
        LOG_WARN("failed to upgrade backup info", K(ret));
O
oceanbase-admin 已提交
6425 6426 6427 6428 6429 6430
      }
    }
  }
  return ret;
}

M
mw0 已提交
6431 6432
int ObBackupDataClean::add_backup_infos_for_compatible(const ObClusterBackupDest &cluster_backup_dest,
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &simple_tenants)
O
oceanbase-admin 已提交
6433 6434
{
  int ret = OB_SUCCESS;
6435 6436 6437
  hash::ObHashMap<int64_t, int64_t> min_backup_set_log_ts;
  ObBackupTaskHistoryUpdater updater;
  ObArray<ObTenantBackupTaskInfo> backup_task_infos;
O
oceanbase-admin 已提交
6438 6439 6440 6441

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6442
  } else if (!cluster_backup_dest.is_valid() || simple_tenants.empty()) {
O
oceanbase-admin 已提交
6443
    ret = OB_INVALID_ARGUMENT;
6444 6445 6446 6447 6448
    LOG_WARN("add backup infos for compatible get invalid argument", K(ret), K(cluster_backup_dest));
  } else if (OB_FAIL(min_backup_set_log_ts.create(simple_tenants.count(), ObModIds::BACKUP))) {
    LOG_WARN("failed to create min backup set log ts map", K(ret));
  } else if (OB_FAIL(updater.init(*sql_proxy_))) {
    LOG_WARN("failed to init updater", K(ret));
O
oceanbase-admin 已提交
6449
  } else {
6450
    for (int64_t i = 0; OB_SUCC(ret) && i < simple_tenants.count(); ++i) {
M
mw0 已提交
6451
      const ObSimpleBackupDataCleanTenant &simple_tenant = simple_tenants.at(i);
6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487
      if (OB_SYS_TENANT_ID == simple_tenant.tenant_id_) {
        // do nothing
      } else if (OB_FAIL(add_backup_infos_for_compatible_(cluster_backup_dest, simple_tenant, min_backup_set_log_ts))) {
        LOG_WARN("failed to add backup infos for compatible", K(ret), K(simple_tenant));
      }

      if (OB_FAIL(ret)) {
      } else if (OB_FAIL(remove_delete_expired_data_snapshot_(simple_tenant))) {
        LOG_WARN("failed to remvoe delete expired data snapshot", K(ret), K(simple_tenant));
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_FAIL(updater.get_tenant_backup_tasks(OB_SYS_TENANT_ID, false /*is_backup_backup*/, backup_task_infos))) {
        LOG_WARN("failed to get tenant backup tasks", K(ret));
      } else {
        for (int64_t i = 0; OB_SUCC(ret) && i < backup_task_infos.count(); ++i) {
          ObTenantBackupTaskInfo backup_task_info = backup_task_infos.at(i);
          ObBackupSetFileInfo set_file_info;
          int64_t min_replay_log_ts = 0;
          int hash_ret = min_backup_set_log_ts.get_refactored(backup_task_info.backup_set_id_, min_replay_log_ts);
          if (OB_SUCCESS != hash_ret && OB_HASH_NOT_EXIST != hash_ret) {
            ret = hash_ret;
            LOG_WARN("failed to get min replay log ts", K(ret), K(backup_task_info));
          } else if (FALSE_IT(backup_task_info.start_replay_log_ts_ = min_replay_log_ts)) {
          } else if (OB_FAIL(set_file_info.extract_from_backup_task_info(backup_task_info))) {
            LOG_WARN("failed to extract from backup task info", K(ret), K(backup_task_info));
          } else if (OB_FAIL(
                         ObBackupSetFilesOperator::insert_tenant_backup_set_file_info(set_file_info, *sql_proxy_))) {
            LOG_WARN("failed to insert tenant backup set file info", K(ret), K(set_file_info));
          } else if (OB_FAIL(updater.insert_tenant_backup_task(backup_task_info))) {
            LOG_WARN("failedto insert tenant backup task", K(ret), K(backup_task_info));
          }
        }
      }
    }
O
oceanbase-admin 已提交
6488 6489 6490 6491
  }
  return ret;
}

M
mw0 已提交
6492 6493
int ObBackupDataClean::add_backup_infos_for_compatible_(const ObClusterBackupDest &cluster_backup_dest,
    const ObSimpleBackupDataCleanTenant &simple_tenant, hash::ObHashMap<int64_t, int64_t> &min_backup_set_log_ts)
O
oceanbase-admin 已提交
6494 6495
{
  int ret = OB_SUCCESS;
6496 6497 6498 6499 6500 6501
  ObExternBackupInfoMgr backup_info_mgr;
  ObArray<ObExternBackupInfo> extern_backup_infos;
  ObTenantBackupTaskInfo backup_task_info;
  ObArray<ObTenantBackupTaskInfo> backup_task_infos;
  ObBackupTaskHistoryUpdater updater;
  ObBackupSetFileInfo set_file_info;
O
oceanbase-admin 已提交
6502 6503 6504 6505

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6506
  } else if (!cluster_backup_dest.is_valid() || !simple_tenant.is_valid()) {
O
oceanbase-admin 已提交
6507
    ret = OB_INVALID_ARGUMENT;
6508 6509 6510 6511 6512 6513 6514
    LOG_WARN("add backup infos for compatible get invalid argument", K(ret), K(cluster_backup_dest), K(simple_tenant));
  } else if (OB_FAIL(updater.init(*sql_proxy_))) {
    LOG_WARN("failed to init updater", K(ret));
  } else if (OB_FAIL(backup_info_mgr.init(simple_tenant.tenant_id_, cluster_backup_dest, *backup_lease_service_))) {
    LOG_WARN("failed to init backup info mgr", K(ret), K(simple_tenant), K(cluster_backup_dest));
  } else if (OB_FAIL(backup_info_mgr.get_extern_backup_infos(extern_backup_infos))) {
    LOG_WARN("failed to get extern backup infos", K(ret));
O
oceanbase-admin 已提交
6515
  } else {
6516
    for (int64_t i = 0; OB_SUCC(ret) && i < extern_backup_infos.count(); ++i) {
M
mw0 已提交
6517
      const ObExternBackupInfo &extern_backup_info = extern_backup_infos.at(i);
6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528
      ObTenantBackupTaskInfo tmp_backup_task_info;
      if (OB_FAIL(check_can_do_task())) {
        LOG_WARN("failed to check can do task", K(ret));
      } else if (!simple_tenant.is_deleted_ && ObExternBackupInfo::DOING == extern_backup_info.status_) {
        // do nothing
      } else if (OB_FAIL(get_backup_task_info_from_extern_info(
                     simple_tenant.tenant_id_, cluster_backup_dest, extern_backup_info, backup_task_info))) {
        LOG_WARN(
            "failed to get backup task info from extern info", K(ret), K(cluster_backup_dest), K(extern_backup_info));
      } else if (OB_FAIL(backup_task_infos.push_back(backup_task_info))) {
        LOG_WARN("failed to push backup task info into array", K(ret), K(backup_task_info));
O
oceanbase-admin 已提交
6529 6530 6531 6532
      }
    }

    if (OB_SUCC(ret)) {
6533 6534
      // add inner table
      for (int64_t i = 0; OB_SUCC(ret) && i < backup_task_infos.count(); ++i) {
M
mw0 已提交
6535
        ObTenantBackupTaskInfo &task_info = backup_task_infos.at(i);
6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565
        set_file_info.reset();
        int64_t start_replay_log_ts = 0;
        if (OB_FAIL(check_can_do_task())) {
          LOG_WARN("failed to check can do task", K(ret));
        } else if (ObBackupCompatibleVersion::OB_BACKUP_COMPATIBLE_VERSION_V1 == task_info.compatible_ &&
                   0 == task_info.start_replay_log_ts_ && OB_SUCCESS == task_info.result_) {
          if (OB_FAIL(ObRootBackup::calculate_tenant_start_replay_log_ts(
                  task_info, *backup_lease_service_, start_replay_log_ts))) {
            LOG_WARN("failed to calculate tenant start replay log ts", K(ret), K(task_info));
          } else {
            task_info.start_replay_log_ts_ = start_replay_log_ts;
            int64_t tmp_replay_log_ts = 0;
            int64_t min_relay_log_ts = 0;
            const int64_t overwrite = 1;
            int hash_ret = min_backup_set_log_ts.get_refactored(task_info.backup_set_id_, tmp_replay_log_ts);
            if (OB_SUCCESS == hash_ret) {
              min_relay_log_ts = std::min(task_info.start_replay_log_ts_, tmp_replay_log_ts);
              if (OB_FAIL(
                      min_backup_set_log_ts.set_refactored(task_info.backup_set_id_, min_relay_log_ts, overwrite))) {
                LOG_WARN("failed to set replay log ts into map", K(ret), K(task_info));
              }
            } else if (OB_HASH_NOT_EXIST == hash_ret) {
              if (OB_FAIL(min_backup_set_log_ts.set_refactored(
                      task_info.backup_set_id_, task_info.start_replay_log_ts_, overwrite))) {
                LOG_WARN("failed to set replay log ts into map", K(ret), K(task_info));
              }
            } else {
              ret = hash_ret;
              LOG_WARN("failed to get min backup set log ts", K(ret), K(hash_ret), K(task_info));
            }
O
oceanbase-admin 已提交
6566 6567 6568
          }
        }

6569 6570 6571 6572 6573 6574 6575 6576
        if (OB_FAIL(ret)) {
        } else if (OB_FAIL(set_file_info.extract_from_backup_task_info(task_info))) {
          LOG_WARN("failed to extract from backup task info", K(ret), K(task_info));
        } else if (OB_FAIL(ObBackupSetFilesOperator::insert_tenant_backup_set_file_info(set_file_info, *sql_proxy_))) {
          LOG_WARN("failed to insert tenant backup set file info", K(ret), K(set_file_info));
        } else if (OB_FAIL(updater.insert_tenant_backup_task(task_info))) {
          LOG_WARN("failedto insert tenant backup task", K(ret), K(task_info));
        }
O
oceanbase-admin 已提交
6577 6578 6579 6580 6581 6582
      }
    }
  }
  return ret;
}

6583
int ObBackupDataClean::get_backup_task_info_from_extern_info(const uint64_t tenant_id,
M
mw0 已提交
6584 6585
    const ObClusterBackupDest &cluster_backup_dest, const ObExternBackupInfo &extern_backup_info,
    ObTenantBackupTaskInfo &backup_task_info)
O
oceanbase-admin 已提交
6586 6587
{
  int ret = OB_SUCCESS;
6588
  backup_task_info.reset();
O
oceanbase-admin 已提交
6589 6590 6591
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6592
  } else if (!cluster_backup_dest.is_valid() || !extern_backup_info.is_valid()) {
O
oceanbase-admin 已提交
6593
    ret = OB_INVALID_ARGUMENT;
6594 6595 6596 6597
    LOG_WARN("get backup task info from extern info get invalid argument",
        K(ret),
        K(cluster_backup_dest),
        K(extern_backup_info));
O
oceanbase-admin 已提交
6598
  } else {
6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624
    backup_task_info.snapshot_version_ = extern_backup_info.backup_snapshot_version_;
    backup_task_info.tenant_id_ = tenant_id;
    backup_task_info.backup_data_version_ = extern_backup_info.backup_data_version_;
    backup_task_info.backup_dest_ = cluster_backup_dest.dest_;
    backup_task_info.backup_schema_version_ = extern_backup_info.backup_schema_version_;
    backup_task_info.backup_set_id_ = extern_backup_info.inc_backup_set_id_;
    backup_task_info.backup_type_.type_ = extern_backup_info.backup_type_;
    backup_task_info.cluster_id_ = GCONF.cluster_id;
    backup_task_info.cluster_version_ = extern_backup_info.cluster_version_;
    backup_task_info.compatible_ = extern_backup_info.compatible_;
    backup_task_info.copy_id_ = 0;
    backup_task_info.date_ = extern_backup_info.date_;
    backup_task_info.device_type_ = cluster_backup_dest.dest_.device_type_;
    backup_task_info.encryption_mode_ = extern_backup_info.encryption_mode_;
    backup_task_info.end_time_ = 0;
    backup_task_info.incarnation_ = cluster_backup_dest.incarnation_;
    backup_task_info.is_mark_deleted_ = extern_backup_info.is_mark_deleted_;
    backup_task_info.macro_block_count_ = 0;
    backup_task_info.passwd_ = extern_backup_info.passwd_;
    backup_task_info.prev_backup_data_version_ = extern_backup_info.prev_backup_data_version_;
    backup_task_info.prev_full_backup_set_id_ = extern_backup_info.prev_full_backup_set_id_;
    backup_task_info.prev_inc_backup_set_id_ = extern_backup_info.prev_inc_backup_set_id_;
    backup_task_info.status_ = ObTenantBackupTaskInfo::FINISH;
    if (ObExternBackupInfo::DOING == extern_backup_info.status_ ||
        ObExternBackupInfo::FAILED == extern_backup_info.status_) {
      backup_task_info.result_ = OB_TENANT_HAS_BEEN_DROPPED;
O
oceanbase-admin 已提交
6625
    } else {
6626
      backup_task_info.result_ = OB_SUCCESS;
O
oceanbase-admin 已提交
6627
    }
6628 6629 6630 6631
  }
  return ret;
}

M
mw0 已提交
6632 6633
int ObBackupDataClean::add_log_archive_infos_for_compatible(const ObClusterBackupDest &cluster_backup_dest,
    const common::ObIArray<ObSimpleBackupDataCleanTenant> &simple_tenants)
6634 6635 6636 6637 6638 6639 6640
{
  int ret = OB_SUCCESS;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < simple_tenants.count(); ++i) {
M
mw0 已提交
6641
      const ObSimpleBackupDataCleanTenant &simple_tenant = simple_tenants.at(i);
6642 6643 6644 6645
      if (!simple_tenant.is_deleted_) {
        // do nothing
      } else if (OB_FAIL(add_log_archive_infos_for_compatible_(cluster_backup_dest, simple_tenant))) {
        LOG_WARN("failed to add log archive infos for compatible", K(ret), K(cluster_backup_dest));
O
oceanbase-admin 已提交
6646 6647 6648 6649 6650
      }
    }
  }
  return ret;
}
6651 6652

int ObBackupDataClean::add_log_archive_infos_for_compatible_(
M
mw0 已提交
6653
    const ObClusterBackupDest &cluster_backup_dest, const ObSimpleBackupDataCleanTenant &simple_tenant)
O
oceanbase-admin 已提交
6654 6655
{
  int ret = OB_SUCCESS;
6656 6657 6658 6659 6660
  ObLogArchiveBackupInfoMgr mgr;
  ObLogArchiveBackupInfo archive_info;
  ObBackupPieceInfo piece;
  ObExternLogArchiveBackupInfo extern_info;
  ObArray<ObTenantLogArchiveStatus> archive_status_array;
O
oceanbase-admin 已提交
6661 6662 6663 6664

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6665
  } else if (!cluster_backup_dest.is_valid() || !simple_tenant.is_valid()) {
O
oceanbase-admin 已提交
6666
    ret = OB_INVALID_ARGUMENT;
6667 6668 6669 6670 6671 6672
    LOG_WARN("add backup infos for compatible get invalid argument", K(ret), K(cluster_backup_dest), K(simple_tenant));
  } else if (OB_FAIL(
                 mgr.read_extern_log_archive_backup_info(cluster_backup_dest, simple_tenant.tenant_id_, extern_info))) {
    LOG_WARN("failed to read extern log archive backup info", K(ret), K(simple_tenant), K(cluster_backup_dest));
  } else if (OB_FAIL(extern_info.get_log_archive_status(archive_status_array))) {
    LOG_WARN("failed to get log archive status", K(ret), K(simple_tenant));
O
oceanbase-admin 已提交
6673
  } else {
6674 6675 6676
    for (int64_t i = 0; OB_SUCC(ret) && i < archive_status_array.count(); ++i) {
      archive_info.reset();
      piece.reset();
M
mw0 已提交
6677
      ObTenantLogArchiveStatus &archive_status = archive_status_array.at(i);
6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709
      archive_status.status_ = ObLogArchiveStatus::STOP;
      archive_info.status_ = archive_status;

      if (OB_FAIL(check_can_do_task())) {
        LOG_WARN("failed to check can do task", K(ret));
      } else if (OB_FAIL(cluster_backup_dest.dest_.get_backup_dest_str(
                     archive_info.backup_dest_, OB_MAX_BACKUP_DEST_LENGTH))) {
        LOG_WARN("failed to get backup destr", K(ret), K(cluster_backup_dest));
      } else if (OB_FAIL(archive_info.get_piece_key(piece.key_))) {
        LOG_WARN("failed to get piece key", K(ret), K(archive_info));
      } else if (OB_FAIL(piece.backup_dest_.assign(archive_info.backup_dest_))) {
        LOG_WARN("failed to copy backup dest", K(ret), K(archive_info));
      } else {
        piece.create_date_ = 0;
        piece.status_ = ObBackupPieceStatus::BACKUP_PIECE_FROZEN;
        if (archive_info.status_.is_mark_deleted_) {
          piece.file_status_ = ObBackupFileStatus::BACKUP_FILE_DELETING;
        } else {
          piece.file_status_ = ObBackupFileStatus::BACKUP_FILE_AVAILABLE;
        }
        piece.start_ts_ = archive_info.status_.start_ts_;
        piece.checkpoint_ts_ = archive_info.status_.checkpoint_ts_;
        piece.max_ts_ = archive_info.status_.checkpoint_ts_;
        piece.compatible_ = ObTenantLogArchiveStatus::NONE;
        piece.start_piece_id_ = 0;
        if (OB_FAIL(mgr.update_backup_piece(*sql_proxy_, piece))) {
          LOG_WARN("failed to update backup piece", K(ret), K(piece));
        } else if (OB_FAIL(mgr.update_log_archive_status_history(
                       *sql_proxy_, archive_info, inner_table_version_, *backup_lease_service_))) {
          LOG_WARN("failed to upload log archive status history", K(ret), K(archive_info));
        }
      }
O
oceanbase-admin 已提交
6710 6711 6712 6713 6714
    }
  }
  return ret;
}

6715
int ObBackupDataClean::get_backup_round_copies_num(
M
mw0 已提交
6716
    const ObLogArchiveBackupInfo &archive_backup_info, int64_t &copies_num)
O
oceanbase-admin 已提交
6717 6718
{
  int ret = OB_SUCCESS;
6719 6720 6721 6722 6723
  ObLogArchiveBackupInfoMgr archive_backup_info_mgr;
  const bool for_update = false;
  copies_num = INT64_MAX;
  const bool is_backup_backup = false;
  ObArray<ObBackupPieceInfo> backup_piece_infos;
O
oceanbase-admin 已提交
6724 6725 6726 6727

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6728
  } else if (!archive_backup_info.is_valid() || 0 != archive_backup_info.status_.copy_id_) {
O
oceanbase-admin 已提交
6729
    ret = OB_INVALID_ARGUMENT;
6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740
    LOG_WARN("get backup piece file copies num get invalid argument", K(ret), K(archive_backup_info));
  } else if (OB_FAIL(archive_backup_info_mgr.get_round_backup_piece_infos(*sql_proxy_,
                 for_update,
                 archive_backup_info.status_.tenant_id_,
                 archive_backup_info.status_.incarnation_,
                 archive_backup_info.status_.round_,
                 is_backup_backup,
                 backup_piece_infos))) {
    LOG_WARN("failed to get round backup piece infos", K(ret), K(archive_backup_info));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_piece_infos.count(); ++i) {
M
mw0 已提交
6741
      const ObBackupPieceInfo &piece_info = backup_piece_infos.at(i);
6742 6743 6744 6745 6746 6747 6748
      int64_t tmp_copies_num = 0;
      if (OB_FAIL(get_backup_piece_file_copies_num(piece_info, tmp_copies_num))) {
        LOG_WARN("failed to get backup piece file copies num", K(ret), K(piece_info));
      } else {
        copies_num = std::min(tmp_copies_num, copies_num);
      }
    }
O
oceanbase-admin 已提交
6749 6750 6751 6752
  }
  return ret;
}

6753
int ObBackupDataClean::upgrade_backup_info()
O
oceanbase-admin 已提交
6754 6755 6756
{
  int ret = OB_SUCCESS;
  ObTimeoutCtx timeout_ctx;
6757 6758 6759 6760
  ObMySQLTransaction trans;
  ObBackupInfoManager info_manager;
  common::ObAddr scheduler_leader_addr;
  int64_t backup_schema_version = 0;
O
oceanbase-admin 已提交
6761 6762 6763

  if (!is_inited_) {
    ret = OB_NOT_INIT;
6764 6765 6766
    LOG_ERROR("not inited", K(ret));
  } else if (OB_FAIL(start_trans(timeout_ctx, trans))) {
    OB_LOG(WARN, "fail to start trans", K(ret));
O
oceanbase-admin 已提交
6767
  } else {
6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778
    if (OB_FAIL(info_manager.init(OB_SYS_TENANT_ID, *sql_proxy_))) {
      LOG_WARN("failed to init info manager", K(ret), K(OB_SYS_TENANT_ID));
    } else if (OB_FAIL(check_can_do_task())) {
      LOG_WARN("failed to check can backup", K(ret));
    } else if (OB_FAIL(
                   ObTenantBackupInfoOperation::get_tenant_name_backup_schema_version(trans, backup_schema_version))) {
      LOG_WARN("failed to get tenant name backup schema version", K(ret));
    } else if (OB_FAIL(ObBackupInfoOperator::set_inner_table_version(trans, OB_BACKUP_INNER_TABLE_V3))) {
      LOG_WARN("failed to set_inner_table_version", K(ret));
    } else if (OB_FAIL(ObBackupInfoOperator::set_tenant_name_backup_schema_version(trans, backup_schema_version))) {
      LOG_WARN("failed to set tenant name backup schema version", K(ret));
O
oceanbase-admin 已提交
6779 6780 6781
    }

    if (OB_SUCC(ret)) {
6782 6783 6784 6785 6786
      if (OB_FAIL(commit_trans(trans))) {
        LOG_WARN("failed to commit trans", K(ret));
      } else {
        inner_table_version_ = OB_BACKUP_INNER_TABLE_V3;
        FLOG_INFO("finish upgrade_backup_info", K(ret), K(inner_table_version_));
O
oceanbase-admin 已提交
6787 6788
      }
    } else {
6789 6790 6791 6792
      int tmp_ret = trans.end(false /*commit*/);
      if (OB_SUCCESS != tmp_ret) {
        LOG_WARN("end transaction failed", K(tmp_ret), K(ret));
        ret = OB_SUCCESS == ret ? tmp_ret : ret;
O
oceanbase-admin 已提交
6793 6794 6795 6796 6797 6798
      }
    }
  }
  return ret;
}

6799
int ObBackupDataClean::check_backup_set_id_can_be_deleted(
M
mw0 已提交
6800
    const uint64_t tenant_id, const ObBackupSetId &backup_set_id, bool &can_deleted)
O
oceanbase-admin 已提交
6801 6802
{
  int ret = OB_SUCCESS;
6803
  can_deleted = false;
O
oceanbase-admin 已提交
6804 6805 6806
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6807
  } else if (!backup_set_id.is_valid() || ObBackupDataCleanMode::CLEAN != backup_set_id.clean_mode_) {
O
oceanbase-admin 已提交
6808
    ret = OB_INVALID_ARGUMENT;
6809 6810 6811
    LOG_WARN("check backup set id can be deleted get invalid argument", K(ret), K(tenant_id), K(backup_set_id));
  } else if (OB_SYS_TENANT_ID == tenant_id) {
    can_deleted = true;
O
oceanbase-admin 已提交
6812
  } else {
6813 6814 6815 6816 6817
    int hash_ret = sys_tenant_deleted_backup_set_.exist_refactored(backup_set_id.backup_set_id_);
    if (OB_HASH_EXIST == hash_ret) {
      can_deleted = true;
    } else if (OB_HASH_NOT_EXIST == hash_ret) {
      can_deleted = false;
O
oceanbase-admin 已提交
6818
    } else {
6819 6820
      ret = hash_ret;
      LOG_WARN("failed to check backup set id can be deleted", K(ret), K(tenant_id), K(backup_set_id));
O
oceanbase-admin 已提交
6821 6822 6823 6824 6825
    }
  }
  return ret;
}

M
mw0 已提交
6826
int ObBackupDataClean::add_deleting_backup_set_id_into_set(const uint64_t tenant_id, const ObBackupSetId &backup_set_id)
O
oceanbase-admin 已提交
6827 6828
{
  int ret = OB_SUCCESS;
6829
  const bool overwirte_key = true;
O
oceanbase-admin 已提交
6830 6831 6832
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6833
  } else if (!backup_set_id.is_valid()) {
O
oceanbase-admin 已提交
6834
    ret = OB_INVALID_ARGUMENT;
6835 6836 6837 6838 6839
    LOG_WARN("add deleting set id into set get invalid argument", K(ret));
  } else if (tenant_id != OB_SYS_TENANT_ID || ObBackupDataCleanMode::CLEAN != backup_set_id.clean_mode_) {
    // do nothing
  } else if (OB_FAIL(sys_tenant_deleted_backup_set_.set_refactored_1(backup_set_id.backup_set_id_, overwirte_key))) {
    LOG_WARN("failed to set backup set id into set", K(ret), K(backup_set_id), K(tenant_id));
O
oceanbase-admin 已提交
6840 6841 6842 6843
  }
  return ret;
}

M
mw0 已提交
6844
int ObBackupDataClean::remove_delete_expired_data_snapshot_(const ObSimpleBackupDataCleanTenant &simple_tenant)
O
oceanbase-admin 已提交
6845 6846
{
  int ret = OB_SUCCESS;
6847
  ObBackupInfoManager info_manager;
O
oceanbase-admin 已提交
6848 6849 6850 6851

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
6852
  } else if (!simple_tenant.is_valid()) {
O
oceanbase-admin 已提交
6853
    ret = OB_INVALID_ARGUMENT;
6854 6855 6856 6857 6858 6859 6860
    LOG_WARN("remove delete expired data snapshot get invalid argument", K(ret), K(simple_tenant));
  } else if (simple_tenant.is_deleted_) {
    // do nothing
  } else if (OB_FAIL(info_manager.init(simple_tenant.tenant_id_, *sql_proxy_))) {
    LOG_WARN("failed to init backup info manager", K(ret), K(simple_tenant));
  } else if (OB_FAIL(info_manager.delete_last_delete_epxired_data_snapshot(simple_tenant.tenant_id_, *sql_proxy_))) {
    LOG_WARN("failed to delete last delete epxired data snapshot", K(ret), K(simple_tenant));
O
oceanbase-admin 已提交
6861 6862 6863 6864
  }
  return ret;
}

M
mw0 已提交
6865
int ObBackupDataClean::set_comment(ObBackupCleanInfo::Comment &comment)
O
oceanbase-admin 已提交
6866
{
6867 6868 6869 6870 6871 6872 6873
  int ret = OB_SUCCESS;
  char ip[common::OB_MAX_SERVER_ADDR_SIZE] = "";
  char trace_id[common::OB_MAX_TRACE_ID_BUFFER_SIZE] = "";
  char comment_str[common::MAX_TABLE_COMMENT_LENGTH] = "";
  int64_t pos = 0;
  comment.reset();
  int trace_length = 0;
O
oceanbase-admin 已提交
6874

6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (GCONF.self_addr_.ip_port_to_string(ip, sizeof(ip))) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("fail to convert ip to string", K(ret));
  } else if (FALSE_IT(trace_length =
                          ObCurTraceId::get_trace_id()->to_string(trace_id, common::OB_MAX_TRACE_ID_BUFFER_SIZE))) {
    LOG_WARN("failed to get trace id", K(ret), K(*ObCurTraceId::get_trace_id()));
  } else if (trace_length > OB_MAX_TRACE_ID_BUFFER_SIZE) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("failed to get trace id", K(ret), K(*ObCurTraceId::get_trace_id()));
  } else if (OB_FAIL(databuff_printf(
                 comment_str, MAX_ROOTSERVICE_EVENT_VALUE_LENGTH, pos, "server:%s, trace_id:%s", ip, trace_id))) {
    LOG_WARN("failed to set comment", K(ret), K(ip), K(trace_id));
  } else if (OB_FAIL(comment.assign(comment_str))) {
    LOG_WARN("failed to assign comment", K(ret), K(comment_str));
O
oceanbase-admin 已提交
6892
  }
6893
  return ret;
O
oceanbase-admin 已提交
6894 6895
}

M
mw0 已提交
6896
int ObBackupDataClean::set_error_msg(const int32_t result, ObBackupCleanInfo::ErrorMsg &error_msg)
O
oceanbase-admin 已提交
6897
{
6898 6899 6900 6901 6902 6903 6904 6905 6906
  int ret = OB_SUCCESS;
  error_msg.reset();
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (OB_SUCCESS != result && OB_FAIL(error_msg.assign(ob_strerror(result)))) {
    LOG_WARN("failed to set comment", K(ret), K(result));
  }
  return ret;
O
oceanbase-admin 已提交
6907 6908
}

M
mw0 已提交
6909
int ObBackupDataClean::prepare_delete_backup_set(const ObBackupCleanInfo &sys_clean_info)
O
oceanbase-admin 已提交
6910
{
6911 6912 6913 6914 6915
  int ret = OB_SUCCESS;
  const ObBackupFileStatus::STATUS file_status = ObBackupFileStatus::BACKUP_FILE_DELETING;
  ObArray<ObBackupSetFileInfo> backup_set_file_infos;
  sys_tenant_deleted_backup_set_.reuse();
  const bool is_backup_backup = sys_clean_info.is_delete_obsolete_backup_backup();
M
mw0 已提交
6916
  const ObBackupDest *backup_dest_ptr = is_backup_backup ? &backup_backup_dest_ : &backup_dest_;
6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939
  const bool overwirte_key = true;
  ObBackupDest backup_dest;
  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (ObBackupCleanInfoStatus::DOING != sys_clean_info.status_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("prepare delete backup set get invalid argument", K(ret), K(sys_clean_info));
  } else if (!sys_clean_info.is_delete_obsolete()) {
    // do nothing
  } else if (OB_FAIL(ObBackupSetFilesOperator::get_backup_set_info_with_file_status(sys_clean_info.tenant_id_,
                 OB_START_INCARNATION,
                 false /*for_update*/,
                 file_status,
                 *sql_proxy_,
                 backup_set_file_infos))) {
    LOG_WARN("failed to get backup set info with file status", KR(ret), K(sys_clean_info), K(file_status));
  } else {
    backup_dest_ptr = is_backup_backup ? &backup_backup_dest_ : &backup_dest_;
    if (!backup_dest_ptr->is_valid()) {
      // do nothing
    } else {
      for (int64_t i = 0; OB_SUCC(ret) && i < backup_set_file_infos.count(); ++i) {
M
mw0 已提交
6940
        const ObBackupSetFileInfo &backup_set_file_info = backup_set_file_infos.at(i);
6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954
        backup_dest.reset();
        if (OB_FAIL(backup_dest.set(backup_set_file_info.backup_dest_.ptr()))) {
          LOG_WARN("failed to set backup dest", K(ret), K(backup_set_file_info));
        } else if (backup_dest != *backup_dest_ptr) {
          // do nothing
        } else if ((backup_set_file_info.copy_id_ == 0 && !is_backup_backup) ||
                   (backup_set_file_info.copy_id_ > 0 && is_backup_backup)) {
          if (OB_FAIL(sys_tenant_deleted_backup_set_.set_refactored_1(
                  backup_set_file_info.backup_set_id_, overwirte_key))) {
            LOG_WARN("failed to set backup set id into set", K(ret), K(backup_set_file_info));
          }
        }
      }
    }
O
oceanbase-admin 已提交
6955
  }
6956
  return ret;
O
oceanbase-admin 已提交
6957 6958
}

M
mw0 已提交
6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079
int ObBackupDataClean::prepare_delete_backup_piece_and_round(const ObBackupCleanInfo &sys_clean_info)
{
  int ret = OB_SUCCESS;
  const ObBackupFileStatus::STATUS file_status = ObBackupFileStatus::BACKUP_FILE_DELETING;
  ObLogArchiveBackupInfoMgr log_archive_info_mgr;
  ObArray<ObBackupPieceInfo> backup_piece_infos;
  ObArray<ObLogArchiveBackupInfo> archive_infos;
  const bool is_backup_backup = sys_clean_info.is_delete_obsolete_backup_backup();
  const ObBackupDest *backup_dest_ptr = is_backup_backup ? &backup_backup_dest_ : &backup_dest_;
  const bool overwrite_key = true;
  const bool for_update = false;
  ObBackupDest backup_dest;
  ObSimplePieceKey simple_piece_key;
  ObSimpleArchiveRound simple_archive_round;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (ObBackupCleanInfoStatus::DOING != sys_clean_info.status_) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("prepare delete backup set get invalid argument", K(ret), K(sys_clean_info));
  } else if (!sys_clean_info.is_delete_obsolete() || !backup_dest_ptr->is_valid()) {
    // do nothing
  } else if (is_backup_backup && OB_FAIL(log_archive_info_mgr.set_backup_backup())) {
    LOG_WARN("failed to set backup backup", K(ret));
  } else if (OB_FAIL(log_archive_info_mgr.get_tenant_backup_piece_infos_with_file_status(*sql_proxy_,
                 OB_START_INCARNATION,
                 sys_clean_info.tenant_id_,
                 file_status,
                 is_backup_backup,
                 backup_piece_infos))) {
    LOG_WARN("failed to get tenant backup piece infos with file stauts", K(ret), K(sys_clean_info));
  } else if (OB_FAIL(log_archive_info_mgr.get_backup_log_archive_history_infos(
                 *sql_proxy_, sys_clean_info.tenant_id_, for_update, archive_infos))) {
    LOG_WARN("failed to get backup log archive history infos", K(ret), K(sys_clean_info));
  } else {
    for (int64_t i = 0; OB_SUCC(ret) && i < backup_piece_infos.count(); ++i) {
      const ObBackupPieceInfo &backup_piece_info = backup_piece_infos.at(i);
      backup_dest.reset();
      simple_piece_key.reset();
      if (OB_FAIL(backup_dest.set(backup_piece_info.backup_dest_.ptr()))) {
        LOG_WARN("failed to set backup dest", K(ret), K(backup_piece_info));
      } else if (backup_dest != *backup_dest_ptr) {
        // do nothing
      } else if ((backup_piece_info.key_.copy_id_ == 0 && !is_backup_backup) ||
                 (backup_piece_info.key_.copy_id_ > 0 && is_backup_backup)) {
        simple_piece_key.backup_piece_id_ = backup_piece_info.key_.backup_piece_id_;
        simple_piece_key.copy_id_ = backup_piece_info.key_.copy_id_;
        simple_piece_key.incarnation_ = backup_piece_info.key_.incarnation_;
        simple_piece_key.round_id_ = backup_piece_info.key_.round_id_;
        if (OB_FAIL(sys_tenant_deleted_backup_piece_.set_refactored_1(simple_piece_key, overwrite_key))) {
          LOG_WARN("failed to set tenant deleted backup piece", K(ret), K(simple_piece_key));
        }
      }
    }

    for (int64_t i = 0; OB_SUCC(ret) && i < archive_infos.count(); ++i) {
      const ObLogArchiveBackupInfo &archive_info = archive_infos.at(i);
      backup_dest.reset();
      simple_archive_round.reset();
      if (OB_FAIL(backup_dest.set(archive_info.backup_dest_))) {
        LOG_WARN("failed to set backup dest", K(ret), K(archive_info));
      } else if (backup_dest != *backup_dest_ptr || !archive_info.status_.is_mark_deleted_) {
        // do nothing
      } else if ((archive_info.status_.copy_id_ == 0 && !is_backup_backup) ||
                 (archive_info.status_.copy_id_ > 0 && is_backup_backup)) {
        simple_archive_round.copy_id_ = archive_info.status_.copy_id_;
        simple_archive_round.incarnation_ = archive_info.status_.incarnation_;
        simple_archive_round.round_id_ = archive_info.status_.round_;
        if (OB_FAIL(sys_tenant_deleted_backup_round_.set_refactored_1(simple_archive_round, overwrite_key))) {
          LOG_WARN("failed to set tenant deleted backup round", K(ret), K(simple_archive_round));
        }
      }
    }
  }
  return ret;
}

int ObBackupDataClean::duplicate_task_info(common::ObIArray<share::ObTenantBackupTaskInfo> &task_infos)
{
  int ret = OB_SUCCESS;
  int64_t step = 0;
  ObHashSet<ObTenantBackupTaskItem> task_infos_set;
  ObArray<ObTenantBackupTaskItem> tmp_task_infos;

  if (!is_inited_) {
    ret = OB_NOT_INIT;
    LOG_WARN("backup data clean do not init", K(ret));
  } else if (task_infos.empty()) {
    // do nothing
  } else if (OB_FAIL(task_infos_set.create(task_infos.count()))) {
    LOG_WARN("failed to create task info set", K(ret));
  } else {
    // remove same tasks
    for (int64_t i = task_infos.count() - 1; OB_SUCC(ret) && i >= 0; --i) {
      const ObTenantBackupTaskInfo &tmp_info = task_infos.at(i);
      int hash_ret = task_infos_set.exist_refactored(tmp_info);
      if (OB_HASH_NOT_EXIST == hash_ret) {
        if (OB_FAIL(task_infos_set.set_refactored(tmp_info))) {
          LOG_WARN("failed to set task info into set", K(ret), K(tmp_info));
        } else if (OB_FAIL(tmp_task_infos.push_back(tmp_info))) {
          LOG_WARN("failed to push tmp info into array", K(ret), K(tmp_info));
        }
      } else if (OB_HASH_EXIST == hash_ret) {
        // do nothing
      } else {
        ret = OB_SUCCESS == hash_ret ? OB_ERR_UNEXPECTED : hash_ret;
      }
    }

    if (OB_SUCC(ret)) {
      CompareBackupTaskInfo backup_task_cmp;
      std::sort(tmp_task_infos.begin(), tmp_task_infos.end(), backup_task_cmp);
      if (OB_FAIL(task_infos.assign(tmp_task_infos))) {
        LOG_WARN("failed to assign task infos", K(ret), K(tmp_task_infos));
      }
    }
  }
  return ret;
}

O
oceanbase-admin 已提交
7080 7081
}  // namespace rootserver
}  // namespace oceanbase