ob_all_disk_stat.cpp 4.3 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 80 81 82 83 84 85 86 87 88 89
/**
 * 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 "observer/virtual_table/ob_all_disk_stat.h"
#include "observer/ob_server.h"
#include "storage/blocksstable/ob_store_file.h"

using namespace oceanbase::blocksstable;
using namespace oceanbase::common;

namespace oceanbase {
namespace observer {
using namespace storage;
ObInfoSchemaDiskStatTable::ObInfoSchemaDiskStatTable()
    : ObVirtualTableScannerIterator(), addr_(NULL), ipstr_(), port_(0), is_end_(false)
{}

ObInfoSchemaDiskStatTable::~ObInfoSchemaDiskStatTable()
{
  reset();
}

void ObInfoSchemaDiskStatTable::reset()
{
  ObVirtualTableScannerIterator::reset();
  addr_ = NULL;
  port_ = 0;
  ipstr_.reset();
  is_end_ = false;
}

int ObInfoSchemaDiskStatTable::set_ip(common::ObAddr* addr)
{
  int ret = OB_SUCCESS;
  char ipbuf[common::OB_IP_STR_BUFF];
  if (NULL == addr) {
    ret = OB_ENTRY_NOT_EXIST;
  } else if (!addr_->ip_to_string(ipbuf, sizeof(ipbuf))) {
    SERVER_LOG(ERROR, "ip to string failed");
    ret = OB_ERR_UNEXPECTED;
  } else {
    ipstr_ = ObString::make_string(ipbuf);
    if (OB_FAIL(ob_write_string(*allocator_, ipstr_, ipstr_))) {
      SERVER_LOG(WARN, "failed to write string", K(ret));
    }
    port_ = addr_->get_port();
  }
  return ret;
}

int ObInfoSchemaDiskStatTable::inner_get_next_row(ObNewRow*& row)
{
  int ret = OB_SUCCESS;
  ObObj* cells = cur_row_.cells_;
  if (OB_ISNULL(allocator_) || OB_ISNULL(cells)) {
    ret = OB_NOT_INIT;
    SERVER_LOG(WARN, "allocator is NULL", KP(allocator_), KP(cells), K(ret));
  } else {
    const int64_t col_count = output_column_ids_.count();

    if (OB_SUCCESS != (ret = set_ip(addr_))) {
      SERVER_LOG(WARN, "can't get ip", K(ret));
    } else if (is_end_) {
      ret = OB_ITER_END;
    }
    if (OB_SUCC(ret)) {
      for (int64_t cell_idx = 0; OB_SUCC(ret) && cell_idx < col_count; ++cell_idx) {
        uint64_t col_id = output_column_ids_.at(cell_idx);
        switch (col_id) {
          case SVR_IP: {
            cells[cell_idx].set_varchar(ipstr_);
            cells[cell_idx].set_collation_type(ObCharset::get_default_collation(ObCharset::get_default_charset()));
            break;
          }
          case SVR_PORT: {
            cells[cell_idx].set_int(port_);
            break;
          }
          case TOTAL_SIZE: {
            cells[cell_idx].set_int(
90
                OB_FILE_SYSTEM.get_total_macro_block_max_count() * OB_FILE_SYSTEM.get_macro_block_size());
O
oceanbase-admin 已提交
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
            break;
          }
          case USED_SIZE: {
            cells[cell_idx].set_int(
                OB_FILE_SYSTEM.get_used_macro_block_count() * OB_FILE_SYSTEM.get_macro_block_size());
            break;
          }
          case FREE_SIZE: {
            cells[cell_idx].set_int(
                OB_FILE_SYSTEM.get_free_macro_block_count() * OB_FILE_SYSTEM.get_macro_block_size());
            break;
          }
          case IS_DISK_VALID: {
            bool disk_error = false;
            int temp_ret = OB_SUCCESS;
            if (OB_SUCCESS != (temp_ret = ObIOManager::get_instance().is_disk_error(disk_error))) {
              SERVER_LOG(WARN, "is_disk_error failed", K(ret));
              // if is_disk_error failed, set to null
              cells[cell_idx].set_null();
            } else {
              cells[cell_idx].set_int(disk_error ? 0 : 1);
            }
            break;
          }
          case DISK_ERROR_BEGIN_TS: {
            const int64_t disk_error_begin_ts = 0;
            cells[cell_idx].set_int(disk_error_begin_ts);
            break;
          }
          default: {
            ret = OB_ERR_UNEXPECTED;
            SERVER_LOG(WARN, "invalid column id", K(ret), K(cell_idx), K(output_column_ids_), K(col_id));
            break;
          }
        }
      }
    }

    if (OB_SUCC(ret)) {
      row = &cur_row_;
      is_end_ = true;
    }
  }
  return ret;
}
}  // namespace observer
}  // namespace oceanbase