ob_store_file.h 15.5 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
/**
 * 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.
 */

#ifndef OB_STORE_FILE_H_
#define OB_STORE_FILE_H_

#include "lib/io/ob_io_manager.h"
#include "lib/atomic/ob_atomic.h"
#include "ob_block_sstable_struct.h"
#include "ob_local_file_system.h"
#include "storage/blocksstable/ob_micro_block_index_mgr.h"
#include "share/ob_unit_getter.h"
#include "share/ob_thread_mgr.h"
#include "storage/blocksstable/ob_macro_block_struct.h"

#define OB_STORE_FILE (oceanbase::blocksstable::ObStoreFile::get_instance())

namespace oceanbase {
namespace storage {
class ObPartitionService;
class ObSSTableRowScanner;
class ObMicroBlockIndexHandleMgr;
}  // namespace storage
namespace compaction {
class ObMacroBlockLoader;
}
namespace blocksstable {
class ObStoreFile;

struct ObMacroBlockInfo {
  int32_t ref_cnt_;
  int32_t write_seq_;
  bool is_free_;
  int64_t access_time_;
  ObMacroBlockInfo() : ref_cnt_(0), write_seq_(0), is_free_(false), access_time_(0)
  {}
  TO_STRING_KV(K_(ref_cnt), K_(write_seq), K_(is_free), K_(access_time));
};

struct ObMacroBlockWriteInfo {
  const char* buffer_;
  int64_t size_;
  ObFullMacroBlockMeta meta_;
  common::ObIODesc io_desc_;

  ObMacroBlocksWriteCtx* block_write_ctx_;
  // reuse_block_ctx_ are only used when reuse old block id
  const ObMacroBlockCtx* reuse_block_ctx_;
  ObMacroBlockWriteInfo();
  OB_INLINE bool is_valid() const;
  OB_INLINE bool is_reuse_macro_block() const
  {
    return NULL != reuse_block_ctx_;
  }
  TO_STRING_KV(KP_(buffer), K_(meta), K_(size), K_(io_desc), K_(block_write_ctx), K_(reuse_block_ctx));
};

struct ObMacroBlockReadInfo {
  const ObMacroBlockCtx* macro_block_ctx_;
  int64_t offset_;
  int64_t size_;
  common::ObIODesc io_desc_;
  common::ObIOCallback* io_callback_;
  ObMacroBlockReadInfo() : macro_block_ctx_(NULL), offset_(), size_(), io_desc_(), io_callback_(NULL)
  {}
  OB_INLINE bool is_valid() const;
  TO_STRING_KV(K_(offset), K_(size), K_(io_desc), KP_(io_callback), K_(macro_block_ctx));
};

class ObMacroBlocksHandle {
G
gm 已提交
80
public:
O
oceanbase-admin 已提交
81 82 83 84 85 86 87 88 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
  ObMacroBlocksHandle();
  virtual ~ObMacroBlocksHandle();
  int add(const MacroBlockId& macro_id);
  int assign(const common::ObIArray<MacroBlockId>& list);
  int64_t count() const
  {
    return macro_id_list_.count();
  }
  MacroBlockId at(const int64_t i) const
  {
    return macro_id_list_.at(i);
  }
  const common::ObArray<MacroBlockId>& get_macro_id_list() const
  {
    return macro_id_list_;
  }
  common::ObArray<MacroBlockId>& get_macro_id_list()
  {
    return macro_id_list_;
  }
  void reset();
  int reserve(const int64_t block_cnt);
  void set_storage_file(ObStorageFile* file)
  {
    file_ = file;
  }
  ObStorageFile* get_storage_file()
  {
    return file_;
  }
  TO_STRING_KV(K_(macro_id_list), "tenant_id", nullptr == file_ ? 0 : file_->get_tenant_id(), "file_id",
      nullptr == file_ ? 0 : file_->get_file_id());

G
gm 已提交
114
private:
O
oceanbase-admin 已提交
115 116 117 118 119 120
  common::ObArray<MacroBlockId> macro_id_list_;
  DISALLOW_COPY_AND_ASSIGN(ObMacroBlocksHandle);
  ObStorageFile* file_;
};

class ObMacroBlockHandle {
G
gm 已提交
121
public:
O
oceanbase-admin 已提交
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 152 153 154 155 156 157 158 159 160 161 162 163 164
  ObMacroBlockHandle() : block_write_ctx_(NULL), file_(NULL)
  {}
  virtual ~ObMacroBlockHandle();
  ObMacroBlockHandle(const ObMacroBlockHandle& other);
  void set_file(ObStorageFile* file)
  {
    file_ = file;
  }
  ObMacroBlockHandle& operator=(const ObMacroBlockHandle& other);
  void reset();
  void reuse();
  OB_INLINE MacroBlockId get_macro_id() const
  {
    return macro_id_;
  }
  OB_INLINE int64_t get_data_size()
  {
    return io_handle_.get_data_size();
  }
  OB_INLINE bool is_valid()
  {
    return io_handle_.is_valid();
  }
  OB_INLINE bool is_empty()
  {
    return io_handle_.is_empty();
  }
  int wait(const int64_t timeout_ms);
  OB_INLINE const char* get_buffer()
  {
    return io_handle_.get_buffer();
  }
  int set_macro_block_id(const MacroBlockId& macro_block_id);
  common::ObIOHandle& get_io_handle()
  {
    return io_handle_;
  }
  OB_INLINE void set_block_write_ctx(ObMacroBlocksWriteCtx* block_write_ctx)
  {
    block_write_ctx_ = block_write_ctx;
  }
  TO_STRING_KV(K_(macro_id), K_(io_handle), KP_(block_write_ctx));

G
gm 已提交
165
private:
O
oceanbase-admin 已提交
166 167 168 169 170 171 172
  MacroBlockId macro_id_;
  common::ObIOHandle io_handle_;
  ObMacroBlocksWriteCtx* block_write_ctx_;
  ObStorageFile* file_;
};

class ObMacroBlockHandleV1 {
G
gm 已提交
173
public:
O
oceanbase-admin 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  ObMacroBlockHandleV1();
  virtual ~ObMacroBlockHandleV1();
  ObMacroBlockHandleV1(const ObMacroBlockHandleV1& other);
  ObMacroBlockHandleV1& operator=(const ObMacroBlockHandleV1& other);
  void reset();
  OB_INLINE MacroBlockId get_macro_id() const
  {
    return macro_id_;
  }
  OB_INLINE int64_t get_data_size()
  {
    return io_handle_.get_data_size();
  }
  OB_INLINE bool is_valid()
  {
    return io_handle_.is_valid();
  }
  OB_INLINE bool is_empty()
  {
    return io_handle_.is_empty();
  }
  int wait(const int64_t timeout_ms);
  OB_INLINE const char* get_buffer()
  {
    return io_handle_.get_buffer();
  }
  int set_macro_block_id(const MacroBlockId& macro_block_id);
  common::ObIOHandle& get_io_handle()
  {
    return io_handle_;
  }
  TO_STRING_KV(K_(macro_id), K_(io_handle));

G
gm 已提交
207
private:
O
oceanbase-admin 已提交
208 209 210 211
  MacroBlockId macro_id_;
  common::ObIOHandle io_handle_;
};

O
obdev 已提交
212 213 214 215 216 217 218
class ObStoreFileAutoExtendTask : public common::ObTimerTask {
public:
  ObStoreFileAutoExtendTask();
  virtual ~ObStoreFileAutoExtendTask();
  virtual void runTimerTask();
};

O
oceanbase-admin 已提交
219
class ObStoreFileGCTask : public common::ObTimerTask {
G
gm 已提交
220
public:
O
oceanbase-admin 已提交
221 222 223 224 225 226
  ObStoreFileGCTask();
  virtual ~ObStoreFileGCTask();
  virtual void runTimerTask();
};

struct ObBadBlockInfo {
G
gm 已提交
227
public:
O
oceanbase-admin 已提交
228 229 230 231 232 233 234 235 236 237 238 239
  ObBadBlockInfo()
  {
    reset();
  }
  ~ObBadBlockInfo()
  {}
  OB_INLINE void reset()
  {
    MEMSET(this, 0, sizeof(*this));
  }
  TO_STRING_KV(K(disk_id_), K(macro_block_id_), K(error_type_), K(store_file_path_), K(error_msg_), K(check_time_));

G
gm 已提交
240
public:
O
oceanbase-admin 已提交
241 242 243 244 245 246 247 248 249
  int64_t disk_id_;
  MacroBlockId macro_block_id_;
  int64_t error_type_;
  char store_file_path_[MAX_PATH_SIZE];
  char error_msg_[common::OB_MAX_ERROR_MSG_LEN];
  int64_t check_time_;
};

class ObAllMacroIdIterator : public ObIMacroIdIterator {
G
gm 已提交
250
public:
O
oceanbase-admin 已提交
251 252 253 254
  ObAllMacroIdIterator();
  virtual ~ObAllMacroIdIterator();
  virtual int get_next_macro_id(MacroBlockId& block_id);

G
gm 已提交
255
private:
O
oceanbase-admin 已提交
256 257 258 259 260
  uint32_t cur_pos_;
};

template <typename T>
class ObSegmentArray final {
G
gm 已提交
261
public:
O
oceanbase-admin 已提交
262 263 264 265 266 267 268
  ObSegmentArray();
  ~ObSegmentArray();
  int reserve(const int64_t size);
  const T& operator[](const int64_t index) const;
  T& operator[](const int64_t index);
  void reset();

G
gm 已提交
269
private:
O
oceanbase-admin 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  static const int64_t ELEMENTS_PER_SEGMENT = 1638400;
  static const int64_t MAX_SEGMENT_CNT = 1024;  // max supported file 1600T
  common::ObArray<T> segments_[MAX_SEGMENT_CNT];
  int64_t segment_cnt_;            // segment cnt
  int64_t last_segment_free_cnt_;  // last postive segment element count
};
template <typename T>
ObSegmentArray<T>::ObSegmentArray() : segments_(), segment_cnt_(0), last_segment_free_cnt_(0)
{}
template <typename T>
ObSegmentArray<T>::~ObSegmentArray()
{}
template <typename T>
void ObSegmentArray<T>::reset()
{
  for (int64_t i = 0; i < segment_cnt_; ++i) {
    segments_[i].reset();
  }
  segment_cnt_ = 0;
  last_segment_free_cnt_ = 0;
}
template <typename T>
int ObSegmentArray<T>::reserve(const int64_t size)
{
  int ret = common::OB_SUCCESS;
  const int64_t delta_size = size - (segment_cnt_ * ELEMENTS_PER_SEGMENT - last_segment_free_cnt_);
  if (delta_size > last_segment_free_cnt_) {
    const int64_t new_delta_segment_cnt = (delta_size - last_segment_free_cnt_) / ELEMENTS_PER_SEGMENT + 1;
    const int64_t new_segment_cnt = segment_cnt_ + new_delta_segment_cnt;
    const int64_t new_last_segment_free_cnt = new_segment_cnt * ELEMENTS_PER_SEGMENT - size;
    if (new_segment_cnt > MAX_SEGMENT_CNT) {
      ret = common::OB_NOT_SUPPORTED;
      STORAGE_LOG(WARN, "file size is too large, not supported", K(ret), K(size));
    }
    for (int64_t i = 0; OB_SUCC(ret) && i < new_delta_segment_cnt; ++i) {
      if (OB_FAIL(segments_[segment_cnt_ + i].prepare_allocate(ELEMENTS_PER_SEGMENT))) {
        STORAGE_LOG(WARN, "fail to reserve segment", K(ret));
      }
    }
    if (OB_SUCC(ret)) {
      segment_cnt_ = new_segment_cnt;
      last_segment_free_cnt_ = new_last_segment_free_cnt;
    }
  } else {
    last_segment_free_cnt_ -= delta_size;
  }
  return ret;
}
template <typename T>
T& ObSegmentArray<T>::operator[](const int64_t index)
{
  const int64_t segment_idx = index / ELEMENTS_PER_SEGMENT;
  const int64_t element_idx = index % ELEMENTS_PER_SEGMENT;
  return segments_[segment_idx][element_idx];
}
template <typename T>
const T& ObSegmentArray<T>::operator[](const int64_t index) const
{
  const int64_t segment_idx = index / ELEMENTS_PER_SEGMENT;
  const int64_t element_idx = index % ELEMENTS_PER_SEGMENT;
  return segments_[segment_idx][element_idx];
}

class ObStoreFile {
G
gm 已提交
334
public:
O
oceanbase-admin 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  static ObStoreFile& get_instance();
  int init(const ObStorageEnv& storage_env, ObStoreFileSystem* store_file_system);
  int open(const bool is_physical_flashback = false);
  void destroy();
  void stop();
  void wait();
  int write_block(const ObMacroBlockWriteInfo& write_info, ObMacroBlockHandle& macro_handle);
  int async_write_block(const ObMacroBlockWriteInfo& write_info, ObMacroBlockHandle& macro_handle);
  int async_write_block_v1(const ObMacroBlockWriteInfo& write_info, ObMacroBlockHandleV1& macro_handle);
  int read_block(const ObMacroBlockReadInfo& read_info, ObMacroBlockHandle& macro_handle);
  int async_read_block(const ObMacroBlockReadInfo& read_info, ObMacroBlockHandle& macro_handle);
  int async_read_block_v1(const ObMacroBlockReadInfo& read_info, ObMacroBlockHandleV1& macro_handle);
  int fsync();
  void inc_ref(const MacroBlockId macro_id);
  void dec_ref(const MacroBlockId macro_id);
  void inc_ref(const common::ObIArray<MacroBlockId>& macro_id_list);
  void dec_ref(const common::ObIArray<MacroBlockId>& macro_id_list);
  int check_disk_full(const int64_t required_size) const;
  bool is_disk_full() const;
  int get_store_status(ObMacroBlockMarkerStatus& status);
  int get_macro_block_info(const int64_t block_index, ObMacroBlockInfo& macro_block_info);
  int report_bad_block(const MacroBlockId& macro_block_id, const int64_t error_type, const char* error_msg);
  int get_bad_block_infos(common::ObArray<ObBadBlockInfo>& bad_block_infos);
O
obdev 已提交
358 359 360
  OB_INLINE const char* get_store_file_path() { return sstable_dir_; }
  OB_INLINE int64_t get_free_macro_block_count() const { return free_block_cnt_; }
  
O
oceanbase-admin 已提交
361 362 363 364 365
  int add_disk(
      const common::ObString& diskgroup_name, const common::ObString& disk_path, const common::ObString& alias_name);
  int drop_disk(const common::ObString& diskgroup_name, const common::ObString& alias_name);
  int is_free_block(const int64_t block_index, bool& is_free);
  int resize_file(const int64_t new_data_file_size, const int64_t new_data_file_disk_percentage);
O
obdev 已提交
366 367
  int validate_datafile_param(const ObString& name, const char* config_data_file_param);
  int refresh_block_meta();
O
oceanbase-admin 已提交
368

G
gm 已提交
369
private:
O
oceanbase-admin 已提交
370 371 372
  friend class ObStoreFileGCTask;
  friend class ObFileSystemInspectBadBlockTask;
  friend class ObAllMacroIdIterator;
O
obdev 已提交
373
  friend class ObStoreFileAutoExtendTask;
O
oceanbase-admin 已提交
374 375
  ObStoreFile();
  virtual ~ObStoreFile();
O
obdev 已提交
376 377 378 379
  int  alloc_block(ObMacroBlockHandle& macro_handle);
  int  mark_macro_blocks();
  int  extend_file_size_task();
  int  auto_extend_file_size();
O
oceanbase-admin 已提交
380 381
  void free_block(const uint32_t block_idx, bool& is_freed);
  void mark_and_sweep();
O
obdev 已提交
382
  void ssblock_check_and_extend();
O
oceanbase-admin 已提交
383 384 385 386 387
  OB_INLINE bool is_valid(const MacroBlockId macro_id);
  OB_INLINE void bitmap_set(const int64_t block_idx);
  OB_INLINE bool bitmap_test(const int64_t block_idx);
  bool is_bad_block(const MacroBlockId& macro_block_id);
  int read_checkpoint_and_replay_log(bool& is_replay_old);
O
obdev 已提交
388 389 390 391 392 393 394 395 396 397

  void disable_mark_sweep() { ATOMIC_SET(&is_mark_sweep_enabled_, false);}
  void enable_mark_sweep() { ATOMIC_SET(&is_mark_sweep_enabled_, true);}
  bool is_mark_sweep_enabled() { return ATOMIC_LOAD(&is_mark_sweep_enabled_);}

  void finish_doing_disk_extend() { ATOMIC_SET(&is_doing_disk_extend_, false);}
  void start_doing_disk_extend() { ATOMIC_SET(&is_doing_disk_extend_, true);}
  bool is_doing_disk_extend() { return ATOMIC_LOAD(&is_doing_disk_extend_);}
  int64_t get_macro_bitmap_array_cnt(const int64_t macro_block_cnt) { return macro_block_cnt / 64 + 1; }

O
oceanbase-admin 已提交
398 399 400
  int wait_mark_sweep_finish();
  void set_mark_sweep_doing();
  void set_mark_sweep_done();
O
obdev 已提交
401

O
oceanbase-admin 已提交
402 403 404
  int alloc_memory(const int64_t total_macro_block_cnt, uint32_t*& free_block_array, uint64_t*& macro_block_bitmap,
      ObSegmentArray<ObMacroBlockInfo>& macro_block_info_array);

G
gm 已提交
405
private:
O
obdev 已提交
406 407 408 409 410 411 412 413
  static const int64_t RECYCLE_DELAY_US     = 5 * 1000 * 1000;  // 5s
  static const int64_t INSPECT_DELAY_US     = 1 * 1000 * 1000;  // 1s
  static const int64_t AUTOEXTEND_DELAY_US  = 1 * 1000 * 1000;  // 1s
  static const int64_t DATAFILE_NEXT_MIN    = 1 * 1024 * 1024 * 1024; // 1G
  static const int64_t FREE_BLOCK_LEFT_PERCENTAGE = 5;
  static const int64_t AUTO_EXTEND_LEAST_FREE_BLOCK_CNT = 512; // 1G 
  static const int64_t MARK_BLOCK_INFO_TIMEOUT = RECYCLE_DELAY_US;

O
oceanbase-admin 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  bool is_inited_;
  bool is_opened_;
  char sstable_dir_[common::OB_MAX_FILE_NAME_LENGTH];
  common::ObLfFIFOAllocator allocator_;
  lib::ObMutex block_lock_;
  int64_t free_block_push_pos_;
  int64_t free_block_pop_pos_;
  uint32_t free_block_cnt_;
  uint32_t* free_block_array_;
  uint64_t* macro_block_bitmap_;
  ObSegmentArray<ObMacroBlockInfo> macro_block_info_;
  lib::ObMutex ckpt_lock_;
  lib::ObMutex meta_array_lock_;
  int64_t cur_meta_array_pos_;
  common::ObArray<MacroBlockId> meta_block_ids_[2];
  ObStoreFileGCTask gc_task_;
O
obdev 已提交
430
  ObStoreFileAutoExtendTask ssblock_auto_extend_task_;
O
oceanbase-admin 已提交
431 432 433 434 435 436 437 438 439 440 441 442
  ObFileSystemInspectBadBlockTask inspect_bad_block_task_;
  char* print_buffer_;
  int64_t print_buffer_size_;
  int64_t mark_cost_time_;
  int64_t sweep_cost_time_;
  int64_t hold_macro_cnt_;
  int64_t used_macro_cnt_[ObMacroBlockCommonHeader::MaxMacroType];
  lib::ObMutex bad_block_lock_;
  common::ObArray<ObBadBlockInfo> bad_block_infos_;
  ObStoreFileSystem* store_file_system_;
  bool is_mark_sweep_enabled_;
  bool is_doing_mark_sweep_;
O
obdev 已提交
443
  bool is_doing_disk_extend_;
O
oceanbase-admin 已提交
444
  ObThreadCond cond_;  // for mark sweep
O
obdev 已提交
445 446
  bool is_fs_support_punch_hole_;
  int block_file_fd_;
O
obdev 已提交
447
  lib::ObMutex alloc_lock_;
448
  lib::ObMutex resize_file_lock_;
O
oceanbase-admin 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
};

OB_INLINE bool ObStoreFile::is_valid(const MacroBlockId macro_id)
{
  return macro_id.block_index() < store_file_system_->get_total_macro_block_count();
}

OB_INLINE void ObStoreFile::bitmap_set(const int64_t block_idx)
{
  uint64_t mask = 1;
  macro_block_bitmap_[block_idx / 64] |= (mask << (block_idx % 64));
}

OB_INLINE bool ObStoreFile::bitmap_test(const int64_t block_idx)
{
  uint64_t mask = 1;
  return 0 != (macro_block_bitmap_[block_idx / 64] & (mask << (block_idx % 64)));
}

bool ObMacroBlockWriteInfo::is_valid() const
{
  bool bool_ret = true;

  if (!io_desc_.is_valid() || NULL == block_write_ctx_) {
    bool_ret = false;
  } else if (NULL != reuse_block_ctx_) {  // reuse block
    if (NULL != buffer_ || 0 != size_ || !meta_.is_valid()) {
      bool_ret = false;
    }
  } else {  // not reuse block
    if (NULL == buffer_ || 0 >= size_ || !meta_.is_valid()) {
      bool_ret = false;
    }
  }
  return bool_ret;
}

bool ObMacroBlockReadInfo::is_valid() const
{
  return OB_NOT_NULL(macro_block_ctx_) && offset_ >= 0 && size_ > 0 && io_desc_.is_valid();
}

}  // namespace blocksstable
}  // namespace oceanbase

#endif /* OB_STORE_FILE_H_ */