2017-05-10 21:54:35 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// 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).
|
2017-05-10 21:54:35 +00:00
|
|
|
//
|
|
|
|
|
2020-10-08 00:46:50 +00:00
|
|
|
#include "db/blob/blob_log_sequential_reader.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2019-09-16 17:31:27 +00:00
|
|
|
#include "file/random_access_file_reader.h"
|
2023-05-17 18:27:09 +00:00
|
|
|
#include "monitoring/statistics_impl.h"
|
2017-11-28 19:42:28 +00:00
|
|
|
#include "util/stop_watch.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-10-08 00:46:50 +00:00
|
|
|
BlobLogSequentialReader::BlobLogSequentialReader(
|
2021-03-15 11:32:24 +00:00
|
|
|
std::unique_ptr<RandomAccessFileReader>&& file_reader, SystemClock* clock,
|
|
|
|
Statistics* statistics)
|
2017-11-28 19:42:28 +00:00
|
|
|
: file_(std::move(file_reader)),
|
2021-01-26 06:07:26 +00:00
|
|
|
clock_(clock),
|
2017-11-28 19:42:28 +00:00
|
|
|
statistics_(statistics),
|
|
|
|
next_byte_(0) {}
|
2017-10-27 20:14:34 +00:00
|
|
|
|
2020-10-08 00:46:50 +00:00
|
|
|
BlobLogSequentialReader::~BlobLogSequentialReader() = default;
|
|
|
|
|
|
|
|
Status BlobLogSequentialReader::ReadSlice(uint64_t size, Slice* slice,
|
|
|
|
char* buf) {
|
|
|
|
assert(slice);
|
2020-08-27 18:54:43 +00:00
|
|
|
assert(file_);
|
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
StopWatch read_sw(clock_, statistics_, BLOB_DB_BLOB_FILE_READ_MICROS);
|
2022-02-17 07:17:03 +00:00
|
|
|
// TODO: rate limit `BlobLogSequentialReader` reads (it appears unused?)
|
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
|
|
|
Status s = file_->Read(IOOptions(), next_byte_, static_cast<size_t>(size),
|
|
|
|
slice, buf, nullptr);
|
2017-10-27 20:14:34 +00:00
|
|
|
next_byte_ += size;
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2017-11-28 19:42:28 +00:00
|
|
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_READ, slice->size());
|
2017-10-27 20:14:34 +00:00
|
|
|
if (slice->size() != size) {
|
|
|
|
return Status::Corruption("EOF reached while reading record");
|
|
|
|
}
|
|
|
|
return s;
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 00:46:50 +00:00
|
|
|
Status BlobLogSequentialReader::ReadHeader(BlobLogHeader* header) {
|
|
|
|
assert(header);
|
2017-05-10 21:54:35 +00:00
|
|
|
assert(next_byte_ == 0);
|
2020-08-27 18:54:43 +00:00
|
|
|
|
|
|
|
static_assert(BlobLogHeader::kSize <= sizeof(header_buf_),
|
|
|
|
"Buffer is smaller than BlobLogHeader::kSize");
|
|
|
|
|
2018-11-13 20:46:35 +00:00
|
|
|
Status s = ReadSlice(BlobLogHeader::kSize, &buffer_, header_buf_);
|
2017-10-27 20:14:34 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
if (buffer_.size() != BlobLogHeader::kSize) {
|
|
|
|
return Status::Corruption("EOF reached before file header");
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
return header->DecodeFrom(buffer_);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 00:46:50 +00:00
|
|
|
Status BlobLogSequentialReader::ReadRecord(BlobLogRecord* record,
|
|
|
|
ReadLevel level,
|
|
|
|
uint64_t* blob_offset) {
|
|
|
|
assert(record);
|
2020-08-27 18:54:43 +00:00
|
|
|
static_assert(BlobLogRecord::kHeaderSize <= sizeof(header_buf_),
|
|
|
|
"Buffer is smaller than BlobLogRecord::kHeaderSize");
|
|
|
|
|
2018-11-13 20:46:35 +00:00
|
|
|
Status s = ReadSlice(BlobLogRecord::kHeaderSize, &buffer_, header_buf_);
|
2017-10-27 20:14:34 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
if (buffer_.size() != BlobLogRecord::kHeaderSize) {
|
2017-10-27 20:14:34 +00:00
|
|
|
return Status::Corruption("EOF reached before record header");
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
s = record->DecodeHeaderFrom(buffer_);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
2017-05-23 17:30:04 +00:00
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
uint64_t kb_size = record->key_size + record->value_size;
|
2017-08-11 19:30:02 +00:00
|
|
|
if (blob_offset != nullptr) {
|
2017-10-27 20:14:34 +00:00
|
|
|
*blob_offset = next_byte_ + record->key_size;
|
2017-08-11 19:30:02 +00:00
|
|
|
}
|
2017-10-27 20:14:34 +00:00
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
switch (level) {
|
2017-10-17 19:11:52 +00:00
|
|
|
case kReadHeader:
|
2017-05-10 21:54:35 +00:00
|
|
|
next_byte_ += kb_size;
|
2017-10-27 20:14:34 +00:00
|
|
|
break;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-10-17 19:11:52 +00:00
|
|
|
case kReadHeaderKey:
|
2018-11-13 20:46:35 +00:00
|
|
|
record->key_buf.reset(new char[record->key_size]);
|
|
|
|
s = ReadSlice(record->key_size, &record->key, record->key_buf.get());
|
2017-10-27 20:14:34 +00:00
|
|
|
next_byte_ += record->value_size;
|
|
|
|
break;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-10-17 19:11:52 +00:00
|
|
|
case kReadHeaderKeyBlob:
|
2018-11-13 20:46:35 +00:00
|
|
|
record->key_buf.reset(new char[record->key_size]);
|
|
|
|
s = ReadSlice(record->key_size, &record->key, record->key_buf.get());
|
2017-10-27 20:14:34 +00:00
|
|
|
if (s.ok()) {
|
2018-11-13 20:46:35 +00:00
|
|
|
record->value_buf.reset(new char[record->value_size]);
|
|
|
|
s = ReadSlice(record->value_size, &record->value,
|
|
|
|
record->value_buf.get());
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-10-27 20:14:34 +00:00
|
|
|
if (s.ok()) {
|
|
|
|
s = record->CheckBlobCRC();
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-10-27 20:14:34 +00:00
|
|
|
break;
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-10-27 20:14:34 +00:00
|
|
|
return s;
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 00:46:50 +00:00
|
|
|
Status BlobLogSequentialReader::ReadFooter(BlobLogFooter* footer) {
|
|
|
|
assert(footer);
|
2020-08-27 18:54:43 +00:00
|
|
|
static_assert(BlobLogFooter::kSize <= sizeof(header_buf_),
|
|
|
|
"Buffer is smaller than BlobLogFooter::kSize");
|
|
|
|
|
|
|
|
Status s = ReadSlice(BlobLogFooter::kSize, &buffer_, header_buf_);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_.size() != BlobLogFooter::kSize) {
|
|
|
|
return Status::Corruption("EOF reached before file footer");
|
|
|
|
}
|
|
|
|
|
|
|
|
return footer->DecodeFrom(buffer_);
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|