2019-09-16 17:31:27 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <atomic>
|
|
|
|
#include <string>
|
2020-08-18 23:19:22 +00:00
|
|
|
|
|
|
|
#include "env/file_system_tracer.h"
|
2019-09-16 17:31:27 +00:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "rocksdb/env.h"
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
#include "rocksdb/file_system.h"
|
2019-09-16 17:31:27 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2019-09-16 17:31:27 +00:00
|
|
|
|
|
|
|
// SequentialFileReader is a wrapper on top of Env::SequentialFile. It handles
|
|
|
|
// Buffered (i.e when page cache is enabled) and Direct (with O_DIRECT / page
|
|
|
|
// cache disabled) reads appropriately, and also updates the IO stats.
|
|
|
|
class SequentialFileReader {
|
|
|
|
private:
|
2021-10-05 17:50:27 +00:00
|
|
|
void NotifyOnFileReadFinish(
|
|
|
|
uint64_t offset, size_t length,
|
|
|
|
const FileOperationInfo::StartTimePoint& start_ts,
|
|
|
|
const FileOperationInfo::FinishTimePoint& finish_ts,
|
|
|
|
const Status& status) const {
|
|
|
|
FileOperationInfo info(FileOperationType::kRead, file_name_, start_ts,
|
|
|
|
finish_ts, status);
|
|
|
|
info.offset = offset;
|
|
|
|
info.length = length;
|
|
|
|
|
|
|
|
for (auto& listener : listeners_) {
|
|
|
|
listener->OnFileReadFinish(info);
|
|
|
|
}
|
2021-11-19 01:09:54 +00:00
|
|
|
info.status.PermitUncheckedError();
|
2021-10-05 17:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddFileIOListeners(
|
|
|
|
const std::vector<std::shared_ptr<EventListener>>& listeners) {
|
|
|
|
std::for_each(listeners.begin(), listeners.end(),
|
|
|
|
[this](const std::shared_ptr<EventListener>& e) {
|
|
|
|
if (e->ShouldBeNotifiedOnFileIO()) {
|
|
|
|
listeners_.emplace_back(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldNotifyListeners() const { return !listeners_.empty(); }
|
|
|
|
|
2019-09-16 17:31:27 +00:00
|
|
|
std::string file_name_;
|
2020-08-18 23:19:22 +00:00
|
|
|
FSSequentialFilePtr file_;
|
2019-09-16 17:31:27 +00:00
|
|
|
std::atomic<size_t> offset_{0}; // read offset
|
2021-10-05 17:50:27 +00:00
|
|
|
std::vector<std::shared_ptr<EventListener>> listeners_{};
|
2022-05-24 17:28:57 +00:00
|
|
|
RateLimiter* rate_limiter_;
|
2024-04-19 00:36:33 +00:00
|
|
|
bool verify_and_reconstruct_read_;
|
2019-09-16 17:31:27 +00:00
|
|
|
|
|
|
|
public:
|
2020-08-18 23:19:22 +00:00
|
|
|
explicit SequentialFileReader(
|
|
|
|
std::unique_ptr<FSSequentialFile>&& _file, const std::string& _file_name,
|
2021-10-05 17:50:27 +00:00
|
|
|
const std::shared_ptr<IOTracer>& io_tracer = nullptr,
|
2022-05-24 17:28:57 +00:00
|
|
|
const std::vector<std::shared_ptr<EventListener>>& listeners = {},
|
|
|
|
RateLimiter* rate_limiter =
|
2024-04-19 00:36:33 +00:00
|
|
|
nullptr, // TODO: migrate call sites to provide rate limiter
|
|
|
|
bool verify_and_reconstruct_read = false)
|
2021-01-25 22:35:45 +00:00
|
|
|
: file_name_(_file_name),
|
2021-10-05 17:50:27 +00:00
|
|
|
file_(std::move(_file), io_tracer, _file_name),
|
2022-05-24 17:28:57 +00:00
|
|
|
listeners_(),
|
2024-04-19 00:36:33 +00:00
|
|
|
rate_limiter_(rate_limiter),
|
|
|
|
verify_and_reconstruct_read_(verify_and_reconstruct_read) {
|
2021-10-05 17:50:27 +00:00
|
|
|
AddFileIOListeners(listeners);
|
|
|
|
}
|
2019-09-16 17:31:27 +00:00
|
|
|
|
2020-08-18 23:19:22 +00:00
|
|
|
explicit SequentialFileReader(
|
|
|
|
std::unique_ptr<FSSequentialFile>&& _file, const std::string& _file_name,
|
|
|
|
size_t _readahead_size,
|
2021-10-05 17:50:27 +00:00
|
|
|
const std::shared_ptr<IOTracer>& io_tracer = nullptr,
|
2022-05-24 17:28:57 +00:00
|
|
|
const std::vector<std::shared_ptr<EventListener>>& listeners = {},
|
|
|
|
RateLimiter* rate_limiter =
|
2024-04-19 00:36:33 +00:00
|
|
|
nullptr, // TODO: migrate call sites to provide rate limiter
|
|
|
|
bool verify_and_reconstruct_read = false)
|
2020-08-18 23:19:22 +00:00
|
|
|
: file_name_(_file_name),
|
|
|
|
file_(NewReadaheadSequentialFile(std::move(_file), _readahead_size),
|
2021-10-05 17:50:27 +00:00
|
|
|
io_tracer, _file_name),
|
2022-05-24 17:28:57 +00:00
|
|
|
listeners_(),
|
2024-04-19 00:36:33 +00:00
|
|
|
rate_limiter_(rate_limiter),
|
|
|
|
verify_and_reconstruct_read_(verify_and_reconstruct_read) {
|
2021-10-05 17:50:27 +00:00
|
|
|
AddFileIOListeners(listeners);
|
|
|
|
}
|
2021-09-15 22:08:39 +00:00
|
|
|
static IOStatus Create(const std::shared_ptr<FileSystem>& fs,
|
|
|
|
const std::string& fname, const FileOptions& file_opts,
|
|
|
|
std::unique_ptr<SequentialFileReader>* reader,
|
2022-05-24 17:28:57 +00:00
|
|
|
IODebugContext* dbg, RateLimiter* rate_limiter);
|
2019-09-16 17:31:27 +00:00
|
|
|
|
|
|
|
SequentialFileReader(const SequentialFileReader&) = delete;
|
|
|
|
SequentialFileReader& operator=(const SequentialFileReader&) = delete;
|
|
|
|
|
2022-05-24 17:28:57 +00:00
|
|
|
// `rate_limiter_priority` is used to charge the internal rate limiter when
|
|
|
|
// enabled. The special value `Env::IO_TOTAL` makes this operation bypass the
|
|
|
|
// rate limiter. The amount charged to the internal rate limiter is n, even
|
|
|
|
// when less than n bytes are actually read (e.g. at end of file). To avoid
|
|
|
|
// overcharging the rate limiter, the caller can use file size to cap n to
|
|
|
|
// read until end of file.
|
Group rocksdb.sst.read.micros stat by different user read IOActivity + misc (#11444)
Summary:
**Context/Summary:**
- Similar to https://github.com/facebook/rocksdb/pull/11288 but for user read such as `Get(), MultiGet(), DBIterator::XXX(), Verify(File)Checksum()`.
- For this, I refactored some user-facing `MultiGet` calls in `TransactionBase` and various types of `DB` so that it does not call a user-facing `Get()` but `GetImpl()` for passing the `ReadOptions::io_activity` check (see PR conversation)
- New user read stats breakdown are guarded by `kExceptDetailedTimers` since measurement shows they have 4-5% regression to the upstream/main.
- Misc
- More refactoring: with https://github.com/facebook/rocksdb/pull/11288, we complete passing `ReadOptions/IOOptions` to FS level. So we can now replace the previously [added](https://github.com/facebook/rocksdb/pull/9424) `rate_limiter_priority` parameter in `RandomAccessFileReader`'s `Read/MultiRead/Prefetch()` with `IOOptions::rate_limiter_priority`
- Also, `ReadAsync()` call time is measured in `SST_READ_MICRO` now
Pull Request resolved: https://github.com/facebook/rocksdb/pull/11444
Test Plan:
- CI fake db crash/stress test
- Microbenchmarking
**Build** `make clean && ROCKSDB_NO_FBCODE=1 DEBUG_LEVEL=0 make -jN db_basic_bench`
- google benchmark version: https://github.com/google/benchmark/commit/604f6fd3f4b34a84ec4eb4db81d842fa4db829cd
- db_basic_bench_base: upstream
- db_basic_bench_pr: db_basic_bench_base + this PR
- asyncread_db_basic_bench_base: upstream + [db basic bench patch for IteratorNext](https://github.com/facebook/rocksdb/compare/main...hx235:rocksdb:micro_bench_async_read)
- asyncread_db_basic_bench_pr: asyncread_db_basic_bench_base + this PR
**Test**
Get
```
TEST_TMPDIR=/dev/shm ./db_basic_bench_{null_stat|base|pr} --benchmark_filter=DBGet/comp_style:0/max_data:134217728/per_key_size:256/enable_statistics:1/negative_query:0/enable_filter:0/mmap:1/threads:1 --benchmark_repetitions=1000
```
Result
```
Coming soon
```
AsyncRead
```
TEST_TMPDIR=/dev/shm ./asyncread_db_basic_bench_{base|pr} --benchmark_filter=IteratorNext/comp_style:0/max_data:134217728/per_key_size:256/enable_statistics:1/async_io:1/include_detailed_timers:0 --benchmark_repetitions=1000 > syncread_db_basic_bench_{base|pr}.out
```
Result
```
Base:
1956,1956,1968,1977,1979,1986,1988,1988,1988,1990,1991,1991,1993,1993,1993,1993,1994,1996,1997,1997,1997,1998,1999,2001,2001,2002,2004,2007,2007,2008,
PR (2.3% regression, due to measuring `SST_READ_MICRO` that wasn't measured before):
1993,2014,2016,2022,2024,2027,2027,2028,2028,2030,2031,2031,2032,2032,2038,2039,2042,2044,2044,2047,2047,2047,2048,2049,2050,2052,2052,2052,2053,2053,
```
Reviewed By: ajkr
Differential Revision: D45918925
Pulled By: hx235
fbshipit-source-id: 58a54560d9ebeb3a59b6d807639692614dad058a
2023-08-09 00:26:50 +00:00
|
|
|
//
|
|
|
|
// TODO(hx235): accept parameter `IOOptions` containing
|
|
|
|
// `rate_limiter_priority` like RandomAccessFileReader::Read()
|
2022-05-24 17:28:57 +00:00
|
|
|
IOStatus Read(size_t n, Slice* result, char* scratch,
|
|
|
|
Env::IOPriority rate_limiter_priority);
|
2019-09-16 17:31:27 +00:00
|
|
|
|
2021-09-15 22:08:39 +00:00
|
|
|
IOStatus Skip(uint64_t n);
|
2019-09-16 17:31:27 +00:00
|
|
|
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
FSSequentialFile* file() { return file_.get(); }
|
2019-09-16 17:31:27 +00:00
|
|
|
|
|
|
|
std::string file_name() { return file_name_; }
|
|
|
|
|
|
|
|
bool use_direct_io() const { return file_->use_direct_io(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// NewReadaheadSequentialFile provides a wrapper over SequentialFile to
|
|
|
|
// always prefetch additional data with every read.
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
static std::unique_ptr<FSSequentialFile> NewReadaheadSequentialFile(
|
|
|
|
std::unique_ptr<FSSequentialFile>&& file, size_t readahead_size);
|
2019-09-16 17:31:27 +00:00
|
|
|
};
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|