YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/file_reader_writer.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 the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
24
#ifndef YB_ROCKSDB_UTIL_FILE_READER_WRITER_H
25
#define YB_ROCKSDB_UTIL_FILE_READER_WRITER_H
26
27
#include <string.h>
28
29
#include <string>
30
31
#include <gflags/gflags_declare.h>
32
33
#include "yb/rocksdb/env.h"
34
#include "yb/rocksdb/port/port.h"
35
#include "yb/rocksdb/statistics.h"
36
#include "yb/rocksdb/util/aligned_buffer.h"
37
38
DECLARE_int32(rocksdb_file_starting_buffer_size);
39
40
namespace yb {
41
42
class PriorityThreadPoolSuspender;
43
44
}
45
46
namespace rocksdb {
47
48
class Statistics;
49
class HistogramImpl;
50
51
std::unique_ptr<RandomAccessFile> NewReadaheadRandomAccessFile(
52
  std::unique_ptr<RandomAccessFile>&& file, size_t readahead_size);
53
54
class SequentialFileReader {
55
 private:
56
  std::unique_ptr<SequentialFile> file_;
57
58
 public:
59
  explicit SequentialFileReader(std::unique_ptr<SequentialFile>&& _file)
60
376k
      : file_(std::move(_file)) {}
61
62
0
  SequentialFileReader(SequentialFileReader&& o) ROCKSDB_NOEXCEPT {
63
0
    *this = std::move(o);
64
0
  }
65
66
0
  SequentialFileReader& operator=(SequentialFileReader&& o) ROCKSDB_NOEXCEPT {
67
0
    file_ = std::move(o.file_);
68
0
    return *this;
69
0
  }
70
71
  SequentialFileReader(const SequentialFileReader&) = delete;
72
  SequentialFileReader& operator=(const SequentialFileReader&) = delete;
73
74
  Status Read(size_t n, Slice* result, uint8_t* scratch);
75
76
  Status Skip(uint64_t n);
77
78
26
  SequentialFile* file() { return file_.get(); }
79
};
80
81
class RandomAccessFileReader {
82
 private:
83
  std::unique_ptr<RandomAccessFile> file_;
84
  Env*            env_;
85
  Statistics*     stats_;
86
  uint32_t        hist_type_;
87
  HistogramImpl*  file_read_hist_;
88
89
 public:
90
  explicit RandomAccessFileReader(std::unique_ptr<RandomAccessFile>&& raf,
91
                                  Env* env = nullptr,
92
                                  Statistics* stats = nullptr,
93
                                  Histograms hist_type = HISTOGRAM_ENUM_MAX,
94
                                  HistogramImpl* file_read_hist = nullptr)
95
      : file_(std::move(raf)),
96
        env_(env),
97
        stats_(stats),
98
        hist_type_(hist_type),
99
169k
        file_read_hist_(file_read_hist) {}
100
101
0
  RandomAccessFileReader(RandomAccessFileReader&& o) ROCKSDB_NOEXCEPT {
102
0
    *this = std::move(o);
103
0
  }
104
105
0
  RandomAccessFileReader& operator=(RandomAccessFileReader&& o) ROCKSDB_NOEXCEPT {
106
0
    file_ = std::move(o.file_);
107
0
    env_ = std::move(o.env_);
108
0
    stats_ = std::move(o.stats_);
109
0
    hist_type_ = std::move(o.hist_type_);
110
0
    file_read_hist_ = std::move(o.file_read_hist_);
111
0
    return *this;
112
0
  }
113
114
  RandomAccessFileReader(const RandomAccessFileReader&) = delete;
115
  RandomAccessFileReader& operator=(const RandomAccessFileReader&) = delete;
116
117
  CHECKED_STATUS Read(uint64_t offset, size_t n, Slice* result, char* scratch) const;
118
  CHECKED_STATUS ReadAndValidate(
119
      uint64_t offset, size_t n, Slice* result, char* scratch, const yb::ReadValidator& validator);
120
121
365k
  RandomAccessFile* file() { return file_.get(); }
122
};
123
124
// Use posix write to write data to a file.
125
class WritableFileWriter {
126
 private:
127
  std::unique_ptr<WritableFile> writable_file_;
128
  AlignedBuffer           buf_;
129
  size_t                  max_buffer_size_;
130
  // Actually written data size can be used for truncate
131
  // not counting padding data
132
  uint64_t                filesize_;
133
  // This is necessary when we use unbuffered access
134
  // and writes must happen on aligned offsets
135
  // so we need to go back and write that page again
136
  uint64_t                next_write_offset_;
137
  bool                    pending_sync_;
138
  bool                    pending_fsync_;
139
  const bool              direct_io_;
140
  const bool              use_os_buffer_;
141
  uint64_t                last_sync_size_;
142
  uint64_t                bytes_per_sync_;
143
  RateLimiter*            rate_limiter_;
144
  // When the writer is used by the priority thread pool's task, this task could pass provided
145
  // suspender to the writer, so it will be used by writer to check whether task should be
146
  // paused after block is flushed.
147
  yb::PriorityThreadPoolSuspender* suspender_;
148
149
 public:
150
  WritableFileWriter(std::unique_ptr<WritableFile>&& file,
151
                     const EnvOptions& options,
152
                     yb::PriorityThreadPoolSuspender* suspender = nullptr)
153
      : writable_file_(std::move(file)),
154
        buf_(),
155
        max_buffer_size_(options.writable_file_max_buffer_size),
156
        filesize_(0),
157
        next_write_offset_(0),
158
        pending_sync_(false),
159
        pending_fsync_(false),
160
        direct_io_(writable_file_->UseDirectIO()),
161
        use_os_buffer_(writable_file_->UseOSBuffer()),
162
        last_sync_size_(0),
163
        bytes_per_sync_(options.bytes_per_sync),
164
        rate_limiter_(options.rate_limiter),
165
1.09M
        suspender_(suspender) {
166
167
1.09M
    buf_.Alignment(writable_file_->GetRequiredBufferAlignment());
168
1.09M
    buf_.AllocateNewBuffer(FLAGS_rocksdb_file_starting_buffer_size);
169
1.09M
  }
170
171
  WritableFileWriter(const WritableFileWriter&) = delete;
172
173
  WritableFileWriter& operator=(const WritableFileWriter&) = delete;
174
175
  ~WritableFileWriter();
176
177
  Status Append(const Slice& data);
178
179
  Status Flush();
180
181
  Status Close();
182
183
  Status Sync(bool use_fsync);
184
185
  // Sync only the data that was already Flush()ed. Safe to call concurrently
186
  // with Append() and Flush(). If !writable_file_->IsSyncThreadSafe(),
187
  // returns NotSupported status.
188
  Status SyncWithoutFlush(bool use_fsync);
189
190
48.3M
  uint64_t GetFileSize() { return filesize_; }
191
192
  Status InvalidateCache(size_t offset, size_t length);
193
194
79.1k
  WritableFile* writable_file() const { return writable_file_.get(); }
195
196
 private:
197
  // Used when os buffering is OFF and we are writing
198
  // DMA such as in Windows unbuffered mode
199
  Status WriteUnbuffered();
200
  // Normal write
201
  Status WriteBuffered(const char* data, size_t size);
202
  Status RangeSync(uint64_t offset, uint64_t nbytes);
203
  size_t RequestToken(size_t bytes, bool align);
204
  Status SyncInternal(bool use_fsync);
205
};
206
207
extern Status NewWritableFile(Env* env, const std::string& fname,
208
                              unique_ptr<WritableFile>* result,
209
                              const EnvOptions& options);
210
}  // namespace rocksdb
211
212
#endif // YB_ROCKSDB_UTIL_FILE_READER_WRITER_H