ob_storage.cpp 42.5 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * 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.
 */

#include "ob_storage.h"
#include "lib/restore/ob_i_storage.h"
#include "lib/utility/ob_tracepoint.h"
16
#include "lib/stat/ob_diagnose_info.h"
O
oceanbase-admin 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

namespace oceanbase {
namespace common {

const char* OB_STORAGE_TYPES_STR[] = {"OSS", "FILE", "COS"};

void print_access_storage_log(
    const char* msg, const common::ObString& uri, const int64_t start_ts, const int64_t size, bool* is_slow)
{
  if (NULL != is_slow) {
    *is_slow = false;
  }

  if (NULL != msg) {
    int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
    const int64_t warn_cost_time = 1000 * 1000;
    double speed = 0;
    if (cost_ts > warn_cost_time) {
      if (NULL != is_slow) {
        *is_slow = true;
      }
      speed = 1.0 * (double)size / 1024 / 1024 * 1000 * 1000 / (double)cost_ts;
      _STORAGE_LOG(WARN,
          "access storage op=%s uri=%.*s size=%ld Byte cost_ts=%ld us speed=%.2f MB/s",
          msg,
          uri.length(),
          uri.ptr(),
          size,
          cost_ts,
          speed);
    }
  }
}

int get_storage_type_from_path(const common::ObString& uri, ObStorageType& type)
{
  int ret = OB_SUCCESS;
  type = OB_STORAGE_MAX_TYPE;

  if (uri.prefix_match(OB_OSS_PREFIX)) {
    type = OB_STORAGE_OSS;
  } else if (uri.prefix_match(OB_FILE_PREFIX)) {
    type = OB_STORAGE_FILE;
  } else if (uri.prefix_match(OB_COS_PREFIX)) {
    type = OB_STORAGE_COS;
  } else {
    ret = OB_INVALID_BACKUP_DEST;
    STORAGE_LOG(ERROR, "invlaid backup uri", K(ret), K(uri));
  }
  return ret;
}

const char* get_storage_type_str(const ObStorageType& type)
{
  const char* str = "UNKNOWN";
  STATIC_ASSERT(
      static_cast<int64_t>(OB_STORAGE_MAX_TYPE) == ARRAYSIZEOF(OB_STORAGE_TYPES_STR), "ObStorageType count mismatch");
  if (type >= 0 && type < OB_STORAGE_MAX_TYPE) {
    str = OB_STORAGE_TYPES_STR[type];
  }
  return str;
}

80 81 82 83 84
bool is_io_error(const int result)
{
  return OB_IO_ERROR == result || OB_OSS_ERROR == result;
}

O
oceanbase-admin 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
int get_storage_type_from_name(const char* type_str, ObStorageType& type)
{
  int ret = OB_SUCCESS;
  type = ObStorageType::OB_STORAGE_MAX_TYPE;
  const int64_t count = ARRAYSIZEOF(OB_STORAGE_TYPES_STR);
  STATIC_ASSERT(static_cast<int64_t>(ObStorageType::OB_STORAGE_MAX_TYPE) == count, "status count mismatch");
  for (int64_t i = 0; i < count; ++i) {
    if (0 == strcmp(type_str, OB_STORAGE_TYPES_STR[i])) {
      type = static_cast<ObStorageType>(i);
      break;
    }
  }
  return ret;
}

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
/**
 * ------------------------------ObStorageGlobalIns---------------------
 */
int ObStorageGlobalIns::init()
{
  int ret = OB_SUCCESS;

  io_prohibited_ = false;

  return ret;
}

void ObStorageGlobalIns::fin()
{}

void ObStorageGlobalIns::set_io_prohibited(bool prohibited)
{
  STORAGE_LOG(WARN, "set_io_prohibited", K_(io_prohibited), K(prohibited));
  io_prohibited_ = prohibited;
}

bool ObStorageGlobalIns::is_io_prohibited() const
{
  return io_prohibited_;
}

O
oceanbase-admin 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
ObStorageUtil::ObStorageUtil(const bool need_retry, const int64_t max_retry_duraion_us, const uint32_t retry_sleep_us)
    : file_util_(), max_retry_duraion_us_(max_retry_duraion_us), retry_sleep_us_(retry_sleep_us)
{
  if (!need_retry) {
    max_retry_duraion_us_ = 0;
  }
}

int ObStorageUtil::is_exist(const common::ObString& uri, const common::ObString& storage_info, bool& exist)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  exist = false;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_IS_EXIST) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
145 146 147
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else if (OB_FAIL(util->is_exist(uri, storage_info, exist))) {
    STORAGE_LOG(WARN, "failed to check is exist", K(ret), K(uri));
  }

  print_access_storage_log("is_exist", uri, start_ts);
  return ret;
}

int ObStorageUtil::get_file_length(
    const common::ObString& uri, const common::ObString& storage_info, int64_t& file_length)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  file_length = -1;
  int64_t retry_times = 0;
  bool need_retry = true;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_GET_FILE_LENGTH) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
172 173 174
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->get_file_length(uri, storage_info, file_length))) {
        if (OB_BACKUP_FILE_NOT_EXIST == ret) {
          need_retry = false;
          STORAGE_LOG(INFO, "cannot get file length for not exist file", K(ret), K(uri));
        } else {
          const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
          STORAGE_LOG(
              WARN, "failed to get_file_length", K(ret), K(cost_ts), K(retry_times), K_(max_retry_duraion_us), K(uri));
          if (cost_ts < max_retry_duraion_us_) {
            usleep(retry_sleep_us_);
            ++retry_times;
            need_retry = true;
            ret = OB_SUCCESS;
          }
        }
      }
    }
  }

  if (file_length <= 0) {
    STORAGE_LOG(INFO, "this file is empty", K(ret), K(uri), K(storage_info), K(file_length));
  }
  print_access_storage_log("get_file_length", uri, start_ts);
  return ret;
}

int ObStorageUtil::del_file(const common::ObString& uri, const common::ObString& storage_info)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_BEFORE_DEL_FILE) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
216 217 218
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else {
    const int max_try_cnt = 5;
    int try_cnt = 0;
    while (try_cnt < max_try_cnt) {
      if (OB_FAIL(util->del_file(uri, storage_info))) {
        try_cnt++;
        OB_LOG(WARN, "failed to delete file", K(ret), K(try_cnt), K(uri), K(storage_info));
      } else {
        break;
      }
    }

    if (OB_SUCC(ret)) {
      OB_LOG(DEBUG, "succeed to delete file", K(ret), K(uri), K(storage_info));
    } else {
      OB_LOG(WARN, "failed to delete file", K(ret), K(uri), K(storage_info));
    }
  }

#ifdef ERRSIM
  if (OB_SUCC(ret)) {
    ret = E(EventTable::EN_BACKUP_IO_AFTER_DEL_FILE) OB_SUCCESS;
  }
#endif
  print_access_storage_log("del_file", uri, start_ts);
246 247 248 249 250 251

  if (OB_FAIL(ret)) {
    EVENT_INC(ObStatEventIds::BACKUP_IO_DEL_FAIL_COUNT);
  }
  EVENT_INC(ObStatEventIds::BACKUP_DELETE_COUNT);
  EVENT_ADD(ObStatEventIds::BACKUP_DELETE_DELAY, ObTimeUtility::current_time() - start_ts);
O
oceanbase-admin 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265
  return ret;
}

int ObStorageUtil::mkdir(const common::ObString& uri, const common::ObString& storage_info)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;

  STORAGE_LOG(DEBUG, "mkdir", K(uri));
#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_BEFORE_MKDIR) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
266 267 268
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else if (OB_FAIL(util->mkdir(uri, storage_info))) {
    STORAGE_LOG(WARN, "failed to mkdir", K(ret), K(uri), K(storage_info));
  }
#ifdef ERRSIM
  if (OB_SUCC(ret)) {
    ret = E(EventTable::EN_BACKUP_IO_AFTER_MKDIR) OB_SUCCESS;
  }
#endif
  print_access_storage_log("mkdir", uri, start_ts);
  return ret;
}

int ObStorageUtil::mk_parent_dir(const common::ObString& uri, const common::ObString& storage_info)
{
  int ret = OB_SUCCESS;
  char path[OB_MAX_URI_LENGTH];

  STORAGE_LOG(INFO, "mk_parent_dir", K(uri));
  if (uri.empty()) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "invlaid args", K(ret), K(uri));
292 293 294
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  } else if (OB_FAIL(databuff_printf(path, sizeof(path), "%.*s", uri.length(), uri.ptr()))) {
    STORAGE_LOG(WARN, "failed to fill path", K(ret), K(path));
  } else if (path[strlen(path) - 1] == '/') {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "cannot mk parent dir for dir", K(ret), K(path));
  } else {
    bool found = false;
    for (int64_t i = strlen(path) - 1; i >= 0; --i) {
      if (path[i] == '/') {
        path[i] = '\0';
        found = true;
        STORAGE_LOG(INFO, "found parent dir", K(i), K(path));
        break;
      }
    }

    if (!found) {
      ret = OB_INVALID_ARGUMENT;
      STORAGE_LOG(WARN, "no dir found in uri", K(ret), K(uri));
    } else if (OB_FAIL(mkdir(path, storage_info))) {
      STORAGE_LOG(WARN, "failed to mkdir", K(ret), K(uri));
    }
  }
  return ret;
}

int ObStorageUtil::read_single_file(const common::ObString& uri, const common::ObString& storage_info, char* buf,
    const int64_t buf_size, int64_t& read_size)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  int64_t retry_times = 0;
  bool need_retry = true;

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(do_read_single_file(uri, storage_info, buf, buf_size, read_size))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(WARN,
            "failed to do_read_single_file",
            K(ret),
            K(cost_ts),
            K(retry_times),
            K_(max_retry_duraion_us),
            K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
O
oceanbase-admin 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
      }
    }
  }

  return ret;
}

int ObStorageUtil::do_read_single_file(const common::ObString& uri, const common::ObString& storage_info, char* buf,
    const int64_t buf_size, int64_t& read_size)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  ObStorageReader reader;

  read_size = -1;

  if (OB_FAIL(reader.open(uri, storage_info))) {
    STORAGE_LOG(WARN, "failed to open reader", K(ret), K(uri));
  } else {
    if (OB_FAIL(reader.pread(buf, buf_size, 0, read_size))) {
      STORAGE_LOG(WARN, "failed to read", K(ret));
    } else if (read_size != reader.get_length()) {
      ret = OB_BUF_NOT_ENOUGH;
      STORAGE_LOG(
          WARN, "not whole file read, maybe buf not enough", K(ret), K(read_size), K(reader.get_length()), K(uri));
    }

    if (OB_SUCCESS != (tmp_ret = reader.close())) {
      STORAGE_LOG(WARN, "failed to close reader", K(tmp_ret), K(uri));
    }
  }

  return ret;
}

// has '\0' in the end
int ObStorageUtil::read_single_text_file(
    const common::ObString& uri, const common::ObString& storage_info, char* buf, const int64_t buf_size)
{
  int ret = OB_SUCCESS;
  int64_t read_size = -1;

392 393 394 395
  if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
  } else if (OB_FAIL(ObStorageUtil::read_single_file(uri, storage_info, buf, buf_size, read_size))) {
O
oceanbase-admin 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    STORAGE_LOG(WARN, "failed to read_single_object", K(ret), K(uri));
  } else if (read_size < 0 || read_size >= buf_size) {
    ret = OB_BUF_NOT_ENOUGH;
    STORAGE_LOG(WARN, "buf not enough", K(ret), K(read_size), K(buf_size));
  } else {
    buf[read_size] = '\0';
  }

  return ret;
}

int ObStorageUtil::update_file_modify_time(const common::ObString& uri, const common::ObString& storage_info)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  int64_t retry_times = 0;
  bool need_retry = true;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_UPDATE_FILE_MODIFY_TIME) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
419 420 421
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->update_file_modify_time(uri, storage_info))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(WARN,
            "failed to update_file_modify_time",
            K(ret),
            K(cost_ts),
            K(retry_times),
            K_(max_retry_duraion_us),
            K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
      }
    }
  }
  print_access_storage_log("update_file_modify_time", uri, start_ts, 0);
  return ret;
}

int ObStorageUtil::list_files(const common::ObString& uri, const common::ObString& storage_info,
    common::ObIAllocator& allocator, common::ObIArray<common::ObString>& file_names)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  int64_t retry_times = 0;
  bool need_retry = true;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_LIST_FILE) OB_SUCCESS;
#endif

  if (OB_FAIL(ret)) {
463 464 465
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->list_files(uri, storage_info, allocator, file_names))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(WARN, "failed to list_files", K(ret), K(cost_ts), K(retry_times), K_(max_retry_duraion_us), K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
      }
    }
  }
  print_access_storage_log("list_files", uri, start_ts, 0);
484 485 486 487 488 489 490

  if (OB_FAIL(ret)) {
    EVENT_INC(ObStatEventIds::BACKUP_IO_LS_FAIL_COUNT);
  }

  EVENT_INC(ObStatEventIds::BACKUP_IO_LS_COUNT);

O
oceanbase-admin 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  return ret;
}

int ObStorageUtil::write_single_file(
    const common::ObString& uri, const common::ObString& storage_info, const char* buf, const int64_t size)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  int64_t retry_times = 0;
  bool need_retry = true;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_BEFORE_WRITE_SINGLE_FILE) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
507 508 509
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->write_single_file(uri, storage_info, buf, size))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(
            WARN, "failed to write single file", K(ret), K(cost_ts), K(retry_times), K_(max_retry_duraion_us), K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
525 526
      } else {
        EVENT_ADD(ObStatEventIds::BACKUP_IO_WRITE_BYTES, size);
O
oceanbase-admin 已提交
527 528 529 530 531 532 533 534 535
      }
    }
  }
#ifdef ERRSIM
  if (OB_SUCC(ret)) {
    ret = E(EventTable::EN_BACKUP_IO_AFTER_WRITE_SINGLE_FILE) OB_SUCCESS;
  }
#endif
  print_access_storage_log("write_single_file", uri, start_ts, size);
536 537 538 539 540 541 542 543

  if (OB_FAIL(ret)) {
    EVENT_INC(ObStatEventIds::BACKUP_IO_WRITE_FAIL_COUNT);
  }

  EVENT_INC(ObStatEventIds::BACKUP_IO_WRITE_COUNT);
  EVENT_ADD(ObStatEventIds::BACKUP_IO_WRITE_DELAY, ObTimeUtility::current_time() - start_ts);

O
oceanbase-admin 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
  return ret;
}

int ObStorageUtil::get_util(const common::ObString& uri, ObIStorageUtil*& util)
{
  int ret = OB_SUCCESS;
  ObStorageType type = OB_STORAGE_MAX_TYPE;
  util = NULL;

  if (OB_FAIL(get_storage_type_from_path(uri, type))) {
    STORAGE_LOG(WARN, "failed to get storage type", K(ret));
  }
#ifdef _WITH_OSS
  else if (OB_STORAGE_OSS == type) {
    util = &oss_util_;
  }
#endif
  else if (OB_STORAGE_FILE == type) {
    util = &file_util_;
  } else {
    ret = OB_ERR_SYS;
    STORAGE_LOG(ERROR, "unknown storage type", K(ret), K(uri));
  }

  return ret;
}

int ObStorageUtil::read_part_file(const common::ObString& uri, const common::ObString& storage_info, char* buf,
    const int64_t buf_size, const int64_t offset, int64_t& read_size)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  int64_t retry_times = 0;
  bool need_retry = true;
  read_size = 0;

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
  if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(do_read_part_file(uri, storage_info, buf, buf_size, offset, read_size))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(
            WARN, "failed to do_dump_file", K(ret), K(cost_ts), K(retry_times), K_(max_retry_duraion_us), K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
O
oceanbase-admin 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
      }
    }
  }

  return ret;
}

int ObStorageUtil::do_read_part_file(const common::ObString& uri, const common::ObString& storage_info, char* buf,
    const int64_t buf_size, const int64_t offset, int64_t& read_size)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  ObStorageReader reader;

  read_size = -1;

  if (OB_FAIL(reader.open(uri, storage_info))) {
    STORAGE_LOG(WARN, "failed to open reader", K(ret), K(uri));
  } else {
    if (OB_FAIL(reader.pread(buf, buf_size, offset, read_size))) {
      STORAGE_LOG(WARN, "failed to read", K(ret));
    }
    if (OB_SUCCESS != (tmp_ret = reader.close())) {
      STORAGE_LOG(WARN, "failed to close reader", K(tmp_ret), K(uri));
    }
  }

  return ret;
}

int ObStorageUtil::del_dir(const common::ObString& uri, const common::ObString& storage_info)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
631 632
  int64_t retry_times = 0;
  bool need_retry = true;
O
oceanbase-admin 已提交
633 634

  if (OB_FAIL(ret)) {
635 636 637
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
638 639
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
640 641 642 643 644 645 646 647 648 649 650 651 652 653
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->del_dir(uri, storage_info))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(WARN, "failed to del_dir", K(ret), K(cost_ts), K(retry_times), K_(max_retry_duraion_us), K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
      }
    }
O
oceanbase-admin 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
  }
  print_access_storage_log("del_file", uri, start_ts);
  return ret;
}

int ObStorageUtil::get_pkeys_from_dir(
    const common::ObString& uri, const common::ObString& storage_info, common::ObIArray<common::ObPartitionKey>& pkeys)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  int64_t retry_times = 0;
  bool need_retry = true;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_LIST_FILE) OB_SUCCESS;
#endif

  if (OB_FAIL(ret)) {
673 674 675
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->get_pkeys_from_dir(uri, storage_info, pkeys))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(WARN, "failed to list_files", K(ret), K(cost_ts), K(retry_times), K_(max_retry_duraion_us), K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
      }
    }
  }

  print_access_storage_log("get_pkeys_from_dir", uri, start_ts, 0);
  return ret;
}

int ObStorageUtil::delete_tmp_files(const common::ObString& uri, const common::ObString& storage_info)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  int64_t retry_times = 0;
  bool need_retry = true;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_LIST_FILE) OB_SUCCESS;
#endif

  if (OB_FAIL(ret)) {
  } else if (uri.empty()) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "delete tmp files get invalid argument", K(ret), K(uri));
714 715 716
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->delete_tmp_files(uri, storage_info))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(
            WARN, "failed to delete tmp files", K(ret), K(cost_ts), K(retry_times), K_(max_retry_duraion_us), K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
      }
    }
  }

  print_access_storage_log("delete_tmp_files", uri, start_ts, 0);
  return ret;
}

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
int ObStorageUtil::check_backup_dest_lifecycle(
    const common::ObString& path, const common::ObString& storage_info, bool& is_set_lifecycle)
{

  int ret = OB_NOT_SUPPORTED;
  UNUSED(path);
  UNUSED(storage_info);
  is_set_lifecycle = false;
  // Forbidden check backup dest lifecycle, because oss has bug which make observer core
  /*
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  int64_t retry_times = 0;
  bool need_retry = true;
  is_set_lifecycle = false;

  if (OB_FAIL(ret)) {
  } else if (path.empty()) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "delete tmp files get invalid argument", K(ret), K(path));
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(path));
  } else if (OB_FAIL(get_util(path, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(path));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->check_backup_dest_lifecycle(path, storage_info, is_set_lifecycle))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(
            WARN, "failed to delete tmp files", K(ret), K(cost_ts), K(retry_times), K_(max_retry_duraion_us), K(path));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
      }
    }
  }

  print_access_storage_log("check_bucket_lifecycle", path, start_ts, 0);
  */
  return ret;
}

int ObStorageUtil::is_empty_directory(
    const common::ObString& uri, const common::ObString& storage_info, bool& is_empty_directory)
{
  int ret = OB_SUCCESS;
  is_empty_directory = false;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  int64_t retry_times = 0;
  bool need_retry = true;

  if (OB_FAIL(ret)) {
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->is_empty_directory(uri, storage_info, is_empty_directory))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(WARN, "failed to check is empty directory", K(ret), K(cost_ts), K(retry_times), K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
      }
    }
  }
  print_access_storage_log("is_empty_directory", uri, start_ts);
  return ret;
}

int ObStorageUtil::list_directories(const common::ObString& uri, const common::ObString& storage_info,
    common::ObIAllocator& allocator, common::ObIArray<common::ObString>& directory_names)
{
  int ret = OB_SUCCESS;
  const int64_t start_ts = ObTimeUtility::current_time();
  ObIStorageUtil* util = NULL;
  int64_t retry_times = 0;
  bool need_retry = true;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_LIST_FILE) OB_SUCCESS;
#endif

  if (OB_FAIL(ret)) {
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
  } else if (OB_FAIL(get_util(uri, util))) {
    STORAGE_LOG(WARN, "failed to get util", K(ret), K(uri));
  } else {
    while (OB_SUCC(ret) && need_retry) {
      need_retry = false;
      if (OB_FAIL(util->list_directories(uri, storage_info, allocator, directory_names))) {
        const int64_t cost_ts = ObTimeUtility::current_time() - start_ts;
        STORAGE_LOG(WARN, "failed to list_files", K(ret), K(cost_ts), K(retry_times), K_(max_retry_duraion_us), K(uri));
        if (cost_ts < max_retry_duraion_us_) {
          usleep(retry_sleep_us_);
          ++retry_times;
          need_retry = true;
          ret = OB_SUCCESS;
        }
      }
    }
  }
  print_access_storage_log("list_files", uri, start_ts, 0);
  return ret;
}

ObStorageReader::ObStorageReader()
    : file_length_(-1), reader_(NULL), start_ts_(0)
O
oceanbase-admin 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
{
  uri_[0] = '\0';
}

ObStorageReader::~ObStorageReader()
{
  if (NULL != reader_) {
    STORAGE_LOG(ERROR, "reader not closed", K(uri_));
  }
}

int ObStorageReader::open(const common::ObString& uri, const common::ObString& storage_info)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  ObStorageType type = OB_STORAGE_MAX_TYPE;
  start_ts_ = ObTimeUtility::current_time();

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_READER_OPEN) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
884 885 886
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
  } else if (NULL != reader_) {
    ret = OB_INIT_TWICE;
    STORAGE_LOG(WARN, "cannot open twice", K(ret), K(uri));
  } else if (OB_FAIL(databuff_printf(uri_, sizeof(uri_), "%.*s", uri.length(), uri.ptr()))) {
    STORAGE_LOG(WARN, "failed to fill uri", K(ret), K(uri));
  } else if (OB_FAIL(get_storage_type_from_path(uri, type))) {
    STORAGE_LOG(WARN, "failed to get type", K(ret), K(uri));
  }
#ifdef _WITH_OSS
  else if (OB_STORAGE_OSS == type) {
    reader_ = &oss_reader_;
  }
#endif
  else if (OB_STORAGE_FILE == type) {
    reader_ = &file_reader_;
  } else {
    ret = OB_ERR_SYS;
    STORAGE_LOG(ERROR, "unkown storage type", K(ret), K(uri));
  }

  if (OB_SUCC(ret)) {
    if (OB_FAIL(reader_->open(uri, storage_info))) {
      STORAGE_LOG(WARN, "failed to open reader", K(ret), K(uri));
    } else {
      file_length_ = reader_->get_length();
    }
  }

  if (OB_FAIL(ret)) {
    if (OB_SUCCESS != (tmp_ret = close())) {
      STORAGE_LOG(WARN, "failed to close read file", K(ret), K(tmp_ret), K(uri));
    }
  }

  return ret;
}

int ObStorageReader::pread(char* buf, const int64_t buf_size, int64_t offset, int64_t& read_size)
{
  int ret = OB_SUCCESS;
  read_size = 0;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_READER_PREAD) OB_SUCCESS;
#endif
932 933

  const int64_t start_ts = ObTimeUtility::current_time();
O
oceanbase-admin 已提交
934
  if (OB_FAIL(ret)) {
935 936 937
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(offset), K(buf_size));
O
oceanbase-admin 已提交
938 939 940 941 942 943 944
  } else if (OB_ISNULL(reader_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "not opened", K(ret));
  } else if (OB_ISNULL(buf) || offset < 0 || offset > file_length_) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "invalid args", K(ret), KP(buf), K(offset), K(file_length_));
  } else if (OB_FAIL(reader_->pread(buf, buf_size, offset, read_size))) {
945
    EVENT_INC(ObStatEventIds::BACKUP_IO_READ_FAIL_COUNT);
O
oceanbase-admin 已提交
946
    STORAGE_LOG(WARN, "failed to read file", K(ret));
947 948
  } else {
    EVENT_ADD(ObStatEventIds::BACKUP_IO_READ_BYTES, read_size);
O
oceanbase-admin 已提交
949
  }
950 951 952 953

  EVENT_INC(ObStatEventIds::BACKUP_IO_READ_COUNT);
  EVENT_ADD(ObStatEventIds::BACKUP_IO_READ_DELAY, ObTimeUtility::current_time() - start_ts);

O
oceanbase-admin 已提交
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
  return ret;
}

int ObStorageReader::close()
{
  int ret = OB_SUCCESS;

  if (NULL != reader_) {
    print_access_storage_log("storage reader", uri_, start_ts_, file_length_);
  }

  if (OB_ISNULL(reader_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "not opened", K(ret));
  } else if (OB_FAIL(reader_->close())) {
    STORAGE_LOG(WARN, "failed to close reader", K(ret));
  }
  reader_ = NULL;
  file_length_ = -1;
  start_ts_ = 0;
  uri_[0] = '\0';
  return ret;
}

ObStorageWriter::ObStorageWriter() : writer_(NULL), file_writer_(), start_ts_(0)
{
  uri_[0] = '\0';
}

ObStorageWriter::~ObStorageWriter()
{
  if (NULL != writer_) {
    STORAGE_LOG(ERROR, "writer not close");
  }
}

int ObStorageWriter::open(const common::ObString& uri, const common::ObString& storage_info)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  ObStorageType type = OB_STORAGE_MAX_TYPE;
  start_ts_ = ObTimeUtility::current_time();

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_WRITE_OPEN) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
1001 1002 1003
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
  } else if (NULL != writer_) {
    ret = OB_INIT_TWICE;
    STORAGE_LOG(WARN, "cannot open twice", K(ret), K(uri));
  } else if (OB_FAIL(databuff_printf(uri_, sizeof(uri_), "%.*s", uri.length(), uri.ptr()))) {
    STORAGE_LOG(WARN, "failed to fill uri", K(ret), K(uri));
  } else if (OB_FAIL(get_storage_type_from_path(uri, type))) {
    STORAGE_LOG(WARN, "failed to get type", K(ret), K(uri));
  }
#ifdef _WITH_OSS
  else if (OB_STORAGE_OSS == type) {
    writer_ = &oss_writer_;
  }
#endif
  else if (OB_STORAGE_FILE == type) {
    writer_ = &file_writer_;
  } else {
    ret = OB_ERR_SYS;
    STORAGE_LOG(ERROR, "unkown storage type", K(ret), K(uri));
  }

  if (OB_SUCC(ret)) {
    if (OB_FAIL(writer_->open(uri, storage_info))) {
      STORAGE_LOG(WARN, "failed to open writer", K(ret), K(uri), K(storage_info));
    }
  }

  if (OB_FAIL(ret)) {
    if (OB_SUCCESS != (tmp_ret = close())) {
      STORAGE_LOG(WARN, "failed close write file", K(ret), K(tmp_ret), K(uri));
    }
  }
  return ret;
}

int ObStorageWriter::write(const char* buf, const int64_t size)
{
  int ret = OB_SUCCESS;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_WRITE_WRITE) OB_SUCCESS;
#endif
1045 1046

  const int64_t start_ts = ObTimeUtility::current_time();
O
oceanbase-admin 已提交
1047
  if (OB_FAIL(ret)) {
1048 1049 1050
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(size));
O
oceanbase-admin 已提交
1051 1052 1053 1054
  } else if (OB_ISNULL(writer_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "not opened", K(ret));
  } else if (OB_FAIL(writer_->write(buf, size))) {
1055
    EVENT_INC(ObStatEventIds::BACKUP_IO_WRITE_FAIL_COUNT);
O
oceanbase-admin 已提交
1056
    STORAGE_LOG(WARN, "failed to write", K(ret));
1057 1058
  } else {
    EVENT_ADD(ObStatEventIds::BACKUP_IO_WRITE_BYTES, size);
O
oceanbase-admin 已提交
1059
  }
1060 1061 1062 1063

  EVENT_INC(ObStatEventIds::BACKUP_IO_WRITE_COUNT);
  EVENT_ADD(ObStatEventIds::BACKUP_IO_WRITE_DELAY, ObTimeUtility::current_time() - start_ts);

O
oceanbase-admin 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
  return ret;
}

int ObStorageWriter::close()
{
  int ret = OB_SUCCESS;

  if (NULL != writer_) {
    print_access_storage_log("storage writer", uri_, start_ts_, writer_->get_length());
  }

  if (OB_ISNULL(writer_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "not opened", K(ret));
  } else if (OB_FAIL(writer_->close())) {
    STORAGE_LOG(WARN, "failed to close writer", K(ret));
  }
  writer_ = NULL;
  start_ts_ = 0;
  uri_[0] = '\0';
  return ret;
}

ObStorageAppender::ObStorageAppender() : appender_(NULL), file_appender_(), start_ts_(0), is_opened_(false)
{
  uri_[0] = '\0';
}

ObStorageAppender::ObStorageAppender(StorageOpenMode mode)
    : appender_(NULL), file_appender_(mode), start_ts_(0), is_opened_(false)
{
  uri_[0] = '\0';
}

ObStorageAppender::~ObStorageAppender()
{
  if (is_opened_ && NULL != appender_) {
    STORAGE_LOG(ERROR, "appender not close");
  }
}

int ObStorageAppender::open(
    const common::ObString& uri, const common::ObString& storage_info, const AppenderParam& param)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  ObStorageType type = OB_STORAGE_MAX_TYPE;
  start_ts_ = ObTimeUtility::current_time();

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_APPENDER_OPEN) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
1117 1118 1119
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
  } else if (NULL != appender_) {
    ret = OB_INIT_TWICE;
    STORAGE_LOG(WARN, "cannot open twice", K(ret), K(uri));
  } else if (OB_FAIL(databuff_printf(uri_, sizeof(uri_), "%.*s", uri.length(), uri.ptr()))) {
    STORAGE_LOG(WARN, "failed to fill uri", K(ret), K(uri));
  } else if (OB_FAIL(get_storage_type_from_path(uri, type))) {
    STORAGE_LOG(WARN, "failed to get type", K(ret), K(uri));
  }
#ifdef _WITH_OSS
  else if (OB_STORAGE_OSS == type) {
    appender_ = &oss_appender_;
  }
#endif
  else if (OB_STORAGE_FILE == type) {
    appender_ = &file_appender_;
  } else {
    ret = OB_ERR_SYS;
    STORAGE_LOG(ERROR, "unkown storage type", K(ret), K(uri));
  }

  if (OB_SUCC(ret)) {
    if (OB_FAIL(appender_->open(uri, storage_info))) {
      STORAGE_LOG(WARN, "failed to open writer", K(ret), K(uri), K(storage_info));
    } else {
      is_opened_ = true;
    }
  }

  if (OB_FAIL(ret)) {
    if (OB_SUCCESS != (tmp_ret = close())) {
      STORAGE_LOG(WARN, "failed close write file", K(ret), K(tmp_ret), K(uri));
    }
  }

  UNUSED(param);

  return ret;
}

int ObStorageAppender::open_deprecated(const common::ObString& uri, const common::ObString& storage_info)
{
  int ret = OB_SUCCESS;
  int tmp_ret = OB_SUCCESS;
  ObStorageType type = OB_STORAGE_MAX_TYPE;
  start_ts_ = ObTimeUtility::current_time();

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_APPENDER_OPEN) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
1170 1171 1172
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
O
oceanbase-admin 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
  } else if (NULL != appender_) {
    ret = OB_INIT_TWICE;
    STORAGE_LOG(WARN, "cannot open twice", K(ret), K(uri));
  } else if (OB_FAIL(databuff_printf(uri_, sizeof(uri_), "%.*s", uri.length(), uri.ptr()))) {
    STORAGE_LOG(WARN, "failed to fill uri", K(ret), K(uri));
  } else if (OB_FAIL(get_storage_type_from_path(uri, type))) {
    STORAGE_LOG(WARN, "failed to get type", K(ret), K(uri));
  }
#ifdef _WITH_OSS
  else if (OB_STORAGE_OSS == type) {
    appender_ = &oss_appender_;
  }
#endif
  else if (OB_STORAGE_FILE == type) {
    appender_ = &file_appender_;
  } else {
    ret = OB_ERR_SYS;
    STORAGE_LOG(ERROR, "unkown storage type", K(ret), K(uri));
  }

  if (OB_SUCC(ret)) {
    if (OB_FAIL(appender_->open(uri, storage_info))) {
      STORAGE_LOG(WARN, "failed to open writer", K(ret), K(uri), K(storage_info));
    } else {
      is_opened_ = true;
    }
  }

  if (OB_FAIL(ret)) {
    if (OB_SUCCESS != (tmp_ret = close())) {
      STORAGE_LOG(WARN, "failed close write file", K(ret), K(tmp_ret), K(uri));
    }
  }
  return ret;
}

int ObStorageAppender::write(const char* buf, const int64_t size)
{
  int ret = OB_SUCCESS;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_APPENDER_WRITE) OB_SUCCESS;
#endif
1216 1217

  const int64_t start_ts = ObTimeUtility::current_time();
O
oceanbase-admin 已提交
1218
  if (OB_FAIL(ret)) {
1219 1220 1221
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(size));
O
oceanbase-admin 已提交
1222 1223 1224 1225
  } else if (OB_ISNULL(appender_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "not opened", K(ret));
  } else if (OB_FAIL(appender_->write(buf, size))) {
1226
    EVENT_INC(ObStatEventIds::BACKUP_IO_WRITE_FAIL_COUNT);
O
oceanbase-admin 已提交
1227
    STORAGE_LOG(WARN, "failed to write", K(ret));
1228 1229
  } else {
    EVENT_ADD(ObStatEventIds::BACKUP_IO_WRITE_BYTES, size);
O
oceanbase-admin 已提交
1230
  }
1231 1232 1233 1234

  EVENT_INC(ObStatEventIds::BACKUP_IO_WRITE_COUNT);
  EVENT_ADD(ObStatEventIds::BACKUP_IO_WRITE_DELAY, ObTimeUtility::current_time() - start_ts);

O
oceanbase-admin 已提交
1235 1236 1237
  return ret;
}

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
int ObStorageAppender::pwrite(const char* buf, const int64_t size, const int64_t offset)
{
  int ret = OB_SUCCESS;

#ifdef ERRSIM
  ret = E(EventTable::EN_BACKUP_IO_APPENDER_WRITE) OB_SUCCESS;
#endif
  if (OB_FAIL(ret)) {
  } else if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(offset), K(size));
  } else if (OB_ISNULL(appender_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "not opened", K(ret));
  } else if (OB_FAIL(appender_->pwrite(buf, size, offset))) {
    STORAGE_LOG(WARN, "failed to write", K(ret));
  }
  return ret;
}

O
oceanbase-admin 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
int64_t ObStorageAppender::get_length()
{
  int64_t ret_int = -1;

  if (OB_ISNULL(appender_)) {
    STORAGE_LOG(WARN, "appender not opened");
  } else {
    ret_int = appender_->get_length();
  }
  return ret_int;
}

int ObStorageAppender::close()
{
  int ret = OB_SUCCESS;

  if (NULL != appender_) {
    print_access_storage_log("storage appender_", uri_, start_ts_, appender_->get_length());
  }

  if (OB_ISNULL(appender_) || !is_opened_) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "not opened", K(ret));
  } else if (OB_FAIL(appender_->close())) {
    STORAGE_LOG(WARN, "failed to close writer", K(ret));
  }
  appender_ = NULL;
  start_ts_ = 0;
  uri_[0] = '\0';
  is_opened_ = false;
  return ret;
}

ObStorageMetaWrapper::ObStorageMetaWrapper() : file_meta_()
{}

ObStorageMetaWrapper::~ObStorageMetaWrapper()
{}

int ObStorageMetaWrapper::get(const common::ObString& uri, const common::ObString& storage_info, char* buf,
    const int64_t buf_size, int64_t& read_size)
{
  int ret = OB_SUCCESS;
  ObIStorageMetaWrapper* meta = NULL;

1303
  const int64_t start_ts = ObTimeUtility::current_time();
1304 1305 1306 1307
  if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
  } else if (OB_FAIL(get_meta(uri, meta))) {
O
oceanbase-admin 已提交
1308 1309
    STORAGE_LOG(WARN, "failed to get meta", K(ret), K(uri));
  } else if (OB_FAIL(meta->get(uri, storage_info, buf, buf_size, read_size))) {
1310
    EVENT_INC(ObStatEventIds::BACKUP_IO_READ_FAIL_COUNT);
O
oceanbase-admin 已提交
1311
    STORAGE_LOG(WARN, "failed to meta get", K(ret), K(uri));
1312 1313
  } else {
    EVENT_ADD(ObStatEventIds::BACKUP_IO_READ_BYTES, read_size);
O
oceanbase-admin 已提交
1314
  }
1315 1316 1317 1318

  EVENT_INC(ObStatEventIds::BACKUP_IO_READ_COUNT);
  EVENT_ADD(ObStatEventIds::BACKUP_IO_READ_DELAY, ObTimeUtility::current_time() - start_ts);

O
oceanbase-admin 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327
  return ret;
}

int ObStorageMetaWrapper::set(
    const common::ObString& uri, const common::ObString& storage_info, const char* buf, const int64_t size)
{
  int ret = OB_SUCCESS;
  ObIStorageMetaWrapper* meta = NULL;

1328
  const int64_t start_ts = ObTimeUtility::current_time();
1329 1330 1331 1332
  if (ObStorageGlobalIns::get_instance().is_io_prohibited()) {
    ret = OB_BACKUP_IO_PROHIBITED;
    STORAGE_LOG(WARN, "current observer backup io is prohibited", K(ret), K(uri));
  } else if (OB_FAIL(get_meta(uri, meta))) {
O
oceanbase-admin 已提交
1333 1334
    STORAGE_LOG(WARN, "failed to get meta", K(ret), K(uri));
  } else if (OB_FAIL(meta->set(uri, storage_info, buf, size))) {
1335
    EVENT_INC(ObStatEventIds::BACKUP_IO_WRITE_FAIL_COUNT);
O
oceanbase-admin 已提交
1336
    STORAGE_LOG(WARN, "failed to meta set", K(ret), K(uri));
1337 1338
  } else {
    EVENT_ADD(ObStatEventIds::BACKUP_IO_WRITE_BYTES, size);
O
oceanbase-admin 已提交
1339
  }
1340 1341 1342 1343

  EVENT_INC(ObStatEventIds::BACKUP_IO_WRITE_COUNT);
  EVENT_ADD(ObStatEventIds::BACKUP_IO_WRITE_DELAY, ObTimeUtility::current_time() - start_ts);

O
oceanbase-admin 已提交
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
  return ret;
}

int ObStorageMetaWrapper::get_meta(const common::ObString& uri, ObIStorageMetaWrapper*& meta)
{
  int ret = OB_SUCCESS;
  ObStorageType type = OB_STORAGE_MAX_TYPE;
  meta = NULL;

  if (OB_FAIL(get_storage_type_from_path(uri, type))) {
    STORAGE_LOG(WARN, "failed to get storage type", K(ret));
  }
#ifdef _WITH_OSS
  else if (OB_STORAGE_OSS == type) {
    meta = &oss_meta_;
  }
#endif
  else if (OB_STORAGE_FILE == type) {
    meta = &file_meta_;
  } else {
    ret = OB_ERR_SYS;
    STORAGE_LOG(ERROR, "unkown storage type", K(ret), K(uri));
  }

  return ret;
}

}  // namespace common
}  // namespace oceanbase