sequence_file_reader.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include <atomic>
  11. #include <string>
  12. #include "env/file_system_tracer.h"
  13. #include "port/port.h"
  14. #include "rocksdb/env.h"
  15. #include "rocksdb/file_system.h"
  16. namespace ROCKSDB_NAMESPACE {
  17. // SequentialFileReader is a wrapper on top of Env::SequentialFile. It handles
  18. // Buffered (i.e when page cache is enabled) and Direct (with O_DIRECT / page
  19. // cache disabled) reads appropriately, and also updates the IO stats.
  20. class SequentialFileReader {
  21. private:
  22. void NotifyOnFileReadFinish(
  23. uint64_t offset, size_t length,
  24. const FileOperationInfo::StartTimePoint& start_ts,
  25. const FileOperationInfo::FinishTimePoint& finish_ts,
  26. const Status& status) const {
  27. FileOperationInfo info(FileOperationType::kRead, file_name_, start_ts,
  28. finish_ts, status);
  29. info.offset = offset;
  30. info.length = length;
  31. for (auto& listener : listeners_) {
  32. listener->OnFileReadFinish(info);
  33. }
  34. info.status.PermitUncheckedError();
  35. }
  36. void AddFileIOListeners(
  37. const std::vector<std::shared_ptr<EventListener>>& listeners) {
  38. std::for_each(listeners.begin(), listeners.end(),
  39. [this](const std::shared_ptr<EventListener>& e) {
  40. if (e->ShouldBeNotifiedOnFileIO()) {
  41. listeners_.emplace_back(e);
  42. }
  43. });
  44. }
  45. bool ShouldNotifyListeners() const { return !listeners_.empty(); }
  46. std::string file_name_;
  47. FSSequentialFilePtr file_;
  48. std::atomic<size_t> offset_{0}; // read offset
  49. std::vector<std::shared_ptr<EventListener>> listeners_{};
  50. RateLimiter* rate_limiter_;
  51. bool verify_and_reconstruct_read_;
  52. public:
  53. explicit SequentialFileReader(
  54. std::unique_ptr<FSSequentialFile>&& _file, const std::string& _file_name,
  55. const std::shared_ptr<IOTracer>& io_tracer = nullptr,
  56. const std::vector<std::shared_ptr<EventListener>>& listeners = {},
  57. RateLimiter* rate_limiter =
  58. nullptr, // TODO: migrate call sites to provide rate limiter
  59. bool verify_and_reconstruct_read = false)
  60. : file_name_(_file_name),
  61. file_(std::move(_file), io_tracer, _file_name),
  62. listeners_(),
  63. rate_limiter_(rate_limiter),
  64. verify_and_reconstruct_read_(verify_and_reconstruct_read) {
  65. AddFileIOListeners(listeners);
  66. }
  67. explicit SequentialFileReader(
  68. std::unique_ptr<FSSequentialFile>&& _file, const std::string& _file_name,
  69. size_t _readahead_size,
  70. const std::shared_ptr<IOTracer>& io_tracer = nullptr,
  71. const std::vector<std::shared_ptr<EventListener>>& listeners = {},
  72. RateLimiter* rate_limiter =
  73. nullptr, // TODO: migrate call sites to provide rate limiter
  74. bool verify_and_reconstruct_read = false)
  75. : file_name_(_file_name),
  76. file_(NewReadaheadSequentialFile(std::move(_file), _readahead_size),
  77. io_tracer, _file_name),
  78. listeners_(),
  79. rate_limiter_(rate_limiter),
  80. verify_and_reconstruct_read_(verify_and_reconstruct_read) {
  81. AddFileIOListeners(listeners);
  82. }
  83. static IOStatus Create(const std::shared_ptr<FileSystem>& fs,
  84. const std::string& fname, const FileOptions& file_opts,
  85. std::unique_ptr<SequentialFileReader>* reader,
  86. IODebugContext* dbg, RateLimiter* rate_limiter);
  87. SequentialFileReader(const SequentialFileReader&) = delete;
  88. SequentialFileReader& operator=(const SequentialFileReader&) = delete;
  89. // `rate_limiter_priority` is used to charge the internal rate limiter when
  90. // enabled. The special value `Env::IO_TOTAL` makes this operation bypass the
  91. // rate limiter. The amount charged to the internal rate limiter is n, even
  92. // when less than n bytes are actually read (e.g. at end of file). To avoid
  93. // overcharging the rate limiter, the caller can use file size to cap n to
  94. // read until end of file.
  95. //
  96. // TODO(hx235): accept parameter `IOOptions` containing
  97. // `rate_limiter_priority` like RandomAccessFileReader::Read()
  98. IOStatus Read(size_t n, Slice* result, char* scratch,
  99. Env::IOPriority rate_limiter_priority);
  100. IOStatus Skip(uint64_t n);
  101. FSSequentialFile* file() { return file_.get(); }
  102. std::string file_name() { return file_name_; }
  103. bool use_direct_io() const { return file_->use_direct_io(); }
  104. private:
  105. // NewReadaheadSequentialFile provides a wrapper over SequentialFile to
  106. // always prefetch additional data with every read.
  107. static std::unique_ptr<FSSequentialFile> NewReadaheadSequentialFile(
  108. std::unique_ptr<FSSequentialFile>&& file, size_t readahead_size);
  109. };
  110. } // namespace ROCKSDB_NAMESPACE