49a10feb21
Summary: In FilePrefetchBuffer if reads are sequential, after prefetching call ReadAsync API to prefetch data asynchronously so that in next prefetching data will be available. Data prefetched asynchronously will be readahead_size/2. It uses two buffers, one for synchronous prefetching and one for asynchronous. In case, the data is overlapping, the data is copied from both buffers to third buffer to make it continuous. This feature is under ReadOptions::async_io and is under experimental. Pull Request resolved: https://github.com/facebook/rocksdb/pull/9674 Test Plan: 1. Add new unit tests 2. Run **db_stress** to make sure nothing crashes. - Normal prefetch without `async_io` ran successfully: ``` export CRASH_TEST_EXT_ARGS=" --async_io=0" make crash_test -j ``` 3. **Run Regressions**. i) Main branch without any change for normal prefetching with async_io disabled: ``` ./db_bench -db=/tmp/prefix_scan_prefetch_main -benchmarks="fillseq" -key_size=32 -value_size=512 -num=5000000 - use_direct_io_for_flush_and_compaction=true -target_file_size_base=16777216 ``` ``` ./db_bench -use_existing_db=true -db=/tmp/prefix_scan_prefetch_main -benchmarks="seekrandom" -key_size=32 -value_size=512 -num=5000000 -use_direct_reads=true -seek_nexts=327680 -duration=120 -ops_between_duration_checks=1 Initializing RocksDB Options from the specified file Initializing RocksDB Options from command-line flags RocksDB: version 7.0 Date: Thu Mar 17 13:11:34 2022 CPU: 24 * Intel Core Processor (Broadwell) CPUCache: 16384 KB Keys: 32 bytes each (+ 0 bytes user-defined timestamp) Values: 512 bytes each (256 bytes after compression) Entries: 5000000 Prefix: 0 bytes Keys per prefix: 0 RawSize: 2594.0 MB (estimated) FileSize: 1373.3 MB (estimated) Write rate: 0 bytes/second Read rate: 0 ops/second Compression: Snappy Compression sampling rate: 0 Memtablerep: SkipListFactory Perf Level: 1 ------------------------------------------------ DB path: [/tmp/prefix_scan_prefetch_main] seekrandom : 483618.390 micros/op 2 ops/sec; 338.9 MB/s (249 of 249 found) ``` ii) normal prefetching after changes with async_io disable: ``` ./db_bench -use_existing_db=true -db=/tmp/prefix_scan_prefetch_withchange -benchmarks="seekrandom" -key_size=32 -value_size=512 -num=5000000 -use_direct_reads=true -seek_nexts=327680 -duration=120 -ops_between_duration_checks=1 Initializing RocksDB Options from the specified file Initializing RocksDB Options from command-line flags RocksDB: version 7.0 Date: Thu Mar 17 14:11:31 2022 CPU: 24 * Intel Core Processor (Broadwell) CPUCache: 16384 KB Keys: 32 bytes each (+ 0 bytes user-defined timestamp) Values: 512 bytes each (256 bytes after compression) Entries: 5000000 Prefix: 0 bytes Keys per prefix: 0 RawSize: 2594.0 MB (estimated) FileSize: 1373.3 MB (estimated) Write rate: 0 bytes/second Read rate: 0 ops/second Compression: Snappy Compression sampling rate: 0 Memtablerep: SkipListFactory Perf Level: 1 ------------------------------------------------ DB path: [/tmp/prefix_scan_prefetch_withchange] seekrandom : 471347.227 micros/op 2 ops/sec; 348.1 MB/s (255 of 255 found) ``` Reviewed By: anand1976 Differential Revision: D34731543 Pulled By: akankshamahajan15 fbshipit-source-id: 8e23aa93453d5fe3c672b9231ad582f60207937f
101 lines
3.6 KiB
C++
101 lines
3.6 KiB
C++
// 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.
|
|
#include "table/block_based/block_prefetcher.h"
|
|
|
|
#include "table/block_based/block_based_table_reader.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
void BlockPrefetcher::PrefetchIfNeeded(const BlockBasedTable::Rep* rep,
|
|
const BlockHandle& handle,
|
|
size_t readahead_size,
|
|
bool is_for_compaction, bool async_io) {
|
|
if (is_for_compaction) {
|
|
rep->CreateFilePrefetchBufferIfNotExists(
|
|
compaction_readahead_size_, compaction_readahead_size_,
|
|
&prefetch_buffer_, false, async_io);
|
|
return;
|
|
}
|
|
|
|
// Explicit user requested readahead.
|
|
if (readahead_size > 0) {
|
|
rep->CreateFilePrefetchBufferIfNotExists(
|
|
readahead_size, readahead_size, &prefetch_buffer_, false, async_io);
|
|
return;
|
|
}
|
|
|
|
// Implicit readahead.
|
|
|
|
// If max_auto_readahead_size is set to be 0 by user, no data will be
|
|
// prefetched.
|
|
size_t max_auto_readahead_size = rep->table_options.max_auto_readahead_size;
|
|
if (max_auto_readahead_size == 0) {
|
|
return;
|
|
}
|
|
|
|
size_t len = BlockBasedTable::BlockSizeWithTrailer(handle);
|
|
size_t offset = handle.offset();
|
|
|
|
// If FS supports prefetching (readahead_limit_ will be non zero in that case)
|
|
// and current block exists in prefetch buffer then return.
|
|
if (offset + len <= readahead_limit_) {
|
|
UpdateReadPattern(offset, len);
|
|
return;
|
|
}
|
|
|
|
if (!IsBlockSequential(offset)) {
|
|
UpdateReadPattern(offset, len);
|
|
ResetValues();
|
|
return;
|
|
}
|
|
UpdateReadPattern(offset, len);
|
|
|
|
// Implicit auto readahead, which will be enabled if the number of reads
|
|
// reached `kMinNumFileReadsToStartAutoReadahead` (default: 2) and scans are
|
|
// sequential.
|
|
num_file_reads_++;
|
|
if (num_file_reads_ <=
|
|
BlockBasedTable::kMinNumFileReadsToStartAutoReadahead) {
|
|
return;
|
|
}
|
|
|
|
if (initial_auto_readahead_size_ > max_auto_readahead_size) {
|
|
initial_auto_readahead_size_ = max_auto_readahead_size;
|
|
}
|
|
|
|
if (rep->file->use_direct_io()) {
|
|
rep->CreateFilePrefetchBufferIfNotExists(initial_auto_readahead_size_,
|
|
max_auto_readahead_size,
|
|
&prefetch_buffer_, true, async_io);
|
|
return;
|
|
}
|
|
|
|
if (readahead_size_ > max_auto_readahead_size) {
|
|
readahead_size_ = max_auto_readahead_size;
|
|
}
|
|
|
|
// If prefetch is not supported, fall back to use internal prefetch buffer.
|
|
// Discarding other return status of Prefetch calls intentionally, as
|
|
// we can fallback to reading from disk if Prefetch fails.
|
|
Status s = rep->file->Prefetch(
|
|
handle.offset(),
|
|
BlockBasedTable::BlockSizeWithTrailer(handle) + readahead_size_);
|
|
if (s.IsNotSupported()) {
|
|
rep->CreateFilePrefetchBufferIfNotExists(initial_auto_readahead_size_,
|
|
max_auto_readahead_size,
|
|
&prefetch_buffer_, true, async_io);
|
|
return;
|
|
}
|
|
|
|
readahead_limit_ = offset + len + readahead_size_;
|
|
// Keep exponentially increasing readahead size until
|
|
// max_auto_readahead_size.
|
|
readahead_size_ = std::min(max_auto_readahead_size, readahead_size_ * 2);
|
|
}
|
|
} // namespace ROCKSDB_NAMESPACE
|