Coverage Report

Created: 2025-07-23 07:17

/src/rocksdb/file/sequence_file_reader.h
Line
Count
Source (jump to first uncovered line)
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
10
#pragma once
11
#include <atomic>
12
#include <string>
13
14
#include "env/file_system_tracer.h"
15
#include "port/port.h"
16
#include "rocksdb/env.h"
17
#include "rocksdb/file_system.h"
18
19
namespace ROCKSDB_NAMESPACE {
20
21
// SequentialFileReader is a wrapper on top of Env::SequentialFile. It handles
22
// Buffered (i.e when page cache is enabled) and Direct (with O_DIRECT / page
23
// cache disabled) reads appropriately, and also updates the IO stats.
24
class SequentialFileReader {
25
 private:
26
  void NotifyOnFileReadFinish(
27
      uint64_t offset, size_t length,
28
      const FileOperationInfo::StartTimePoint& start_ts,
29
      const FileOperationInfo::FinishTimePoint& finish_ts,
30
0
      const Status& status) const {
31
0
    FileOperationInfo info(FileOperationType::kRead, file_name_, start_ts,
32
0
                           finish_ts, status);
33
0
    info.offset = offset;
34
0
    info.length = length;
35
36
0
    for (auto& listener : listeners_) {
37
0
      listener->OnFileReadFinish(info);
38
0
    }
39
0
    info.status.PermitUncheckedError();
40
0
  }
41
42
  void AddFileIOListeners(
43
208k
      const std::vector<std::shared_ptr<EventListener>>& listeners) {
44
208k
    std::for_each(listeners.begin(), listeners.end(),
45
208k
                  [this](const std::shared_ptr<EventListener>& e) {
46
0
                    if (e->ShouldBeNotifiedOnFileIO()) {
47
0
                      listeners_.emplace_back(e);
48
0
                    }
49
0
                  });
50
208k
  }
51
52
517k
  bool ShouldNotifyListeners() const { return !listeners_.empty(); }
53
54
  std::string file_name_;
55
  FSSequentialFilePtr file_;
56
  std::atomic<size_t> offset_{0};  // read offset
57
  std::vector<std::shared_ptr<EventListener>> listeners_{};
58
  RateLimiter* rate_limiter_;
59
  bool verify_and_reconstruct_read_;
60
61
 public:
62
  explicit SequentialFileReader(
63
      std::unique_ptr<FSSequentialFile>&& _file, const std::string& _file_name,
64
      const std::shared_ptr<IOTracer>& io_tracer = nullptr,
65
      const std::vector<std::shared_ptr<EventListener>>& listeners = {},
66
      RateLimiter* rate_limiter =
67
          nullptr,  // TODO: migrate call sites to provide rate limiter
68
      bool verify_and_reconstruct_read = false)
69
0
      : file_name_(_file_name),
70
0
        file_(std::move(_file), io_tracer, _file_name),
71
0
        listeners_(),
72
0
        rate_limiter_(rate_limiter),
73
0
        verify_and_reconstruct_read_(verify_and_reconstruct_read) {
74
0
    AddFileIOListeners(listeners);
75
0
  }
76
77
  explicit SequentialFileReader(
78
      std::unique_ptr<FSSequentialFile>&& _file, const std::string& _file_name,
79
      size_t _readahead_size,
80
      const std::shared_ptr<IOTracer>& io_tracer = nullptr,
81
      const std::vector<std::shared_ptr<EventListener>>& listeners = {},
82
      RateLimiter* rate_limiter =
83
          nullptr,  // TODO: migrate call sites to provide rate limiter
84
      bool verify_and_reconstruct_read = false)
85
208k
      : file_name_(_file_name),
86
208k
        file_(NewReadaheadSequentialFile(std::move(_file), _readahead_size),
87
208k
              io_tracer, _file_name),
88
208k
        listeners_(),
89
208k
        rate_limiter_(rate_limiter),
90
208k
        verify_and_reconstruct_read_(verify_and_reconstruct_read) {
91
208k
    AddFileIOListeners(listeners);
92
208k
  }
93
  static IOStatus Create(const std::shared_ptr<FileSystem>& fs,
94
                         const std::string& fname, const FileOptions& file_opts,
95
                         std::unique_ptr<SequentialFileReader>* reader,
96
                         IODebugContext* dbg, RateLimiter* rate_limiter);
97
98
  SequentialFileReader(const SequentialFileReader&) = delete;
99
  SequentialFileReader& operator=(const SequentialFileReader&) = delete;
100
101
  // `rate_limiter_priority` is used to charge the internal rate limiter when
102
  // enabled. The special value `Env::IO_TOTAL` makes this operation bypass the
103
  // rate limiter. The amount charged to the internal rate limiter is n, even
104
  // when less than n bytes are actually read (e.g. at end of file). To avoid
105
  // overcharging the rate limiter, the caller can use file size to cap n to
106
  // read until end of file.
107
  //
108
  // TODO(hx235): accept parameter `IOOptions` containing
109
  // `rate_limiter_priority` like RandomAccessFileReader::Read()
110
  IOStatus Read(size_t n, Slice* result, char* scratch,
111
                Env::IOPriority rate_limiter_priority);
112
113
  IOStatus Skip(uint64_t n);
114
115
0
  FSSequentialFile* file() { return file_.get(); }
116
117
0
  std::string file_name() { return file_name_; }
118
119
258k
  bool use_direct_io() const { return file_->use_direct_io(); }
120
121
 private:
122
  // NewReadaheadSequentialFile provides a wrapper over SequentialFile to
123
  // always prefetch additional data with every read.
124
  static std::unique_ptr<FSSequentialFile> NewReadaheadSequentialFile(
125
      std::unique_ptr<FSSequentialFile>&& file, size_t readahead_size);
126
};
127
}  // namespace ROCKSDB_NAMESPACE