From 4927b4e662b85609af4c1de3071866919a6d05d8 Mon Sep 17 00:00:00 2001 From: Maysam Yabandeh Date: Fri, 26 Jan 2018 12:50:48 -0800 Subject: [PATCH] Rounddown in FilePrefetchBuffer::Prefetch Summary: FilePrefetchBuffer::Prefetch is currently rounds the offset up which does not fit its new use cases in prefetching index/filter blocks, as it would skips over some the offsets that were requested to be prefetched. This patch rounds down instead. Fixes #3180 Closes https://github.com/facebook/rocksdb/pull/3413 Differential Revision: D6816392 Pulled By: maysamyabandeh fbshipit-source-id: 3aaeaf59c55d72b61dacfae6d4a8e65eccb3c553 --- util/aligned_buffer.h | 2 ++ util/file_reader_writer.cc | 13 ++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/util/aligned_buffer.h b/util/aligned_buffer.h index e93f4b5c6..8d4a0be58 100644 --- a/util/aligned_buffer.h +++ b/util/aligned_buffer.h @@ -23,6 +23,8 @@ inline size_t Roundup(size_t x, size_t y) { return ((x + y - 1) / y) * y; } +inline size_t Rounddown(size_t x, size_t y) { return (x / y) * y; } + // This class is to manage an aligned user // allocated buffer for direct I/O purposes // though can be used for any purpose. diff --git a/util/file_reader_writer.cc b/util/file_reader_writer.cc index f46b78fa0..cf18004d1 100644 --- a/util/file_reader_writer.cc +++ b/util/file_reader_writer.cc @@ -606,16 +606,19 @@ class ReadaheadRandomAccessFile : public RandomAccessFile { Status FilePrefetchBuffer::Prefetch(RandomAccessFileReader* reader, uint64_t offset, size_t n) { size_t alignment = reader->file()->GetRequiredBufferAlignment(); - uint64_t roundup_offset = Roundup(offset, alignment); - uint64_t roundup_len = Roundup(n, alignment); + uint64_t rounddown_offset = Rounddown(offset, alignment); + uint64_t roundup_end = Roundup(offset + n, alignment); + uint64_t roundup_len = roundup_end - rounddown_offset; + assert(roundup_len >= alignment); + assert(roundup_len % alignment == 0); buffer_.Alignment(alignment); buffer_.AllocateNewBuffer(roundup_len); Slice result; - Status s = - reader->Read(roundup_offset, roundup_len, &result, buffer_.BufferStart()); + Status s = reader->Read(rounddown_offset, roundup_len, &result, + buffer_.BufferStart()); if (s.ok()) { - buffer_offset_ = roundup_offset; + buffer_offset_ = rounddown_offset; buffer_len_ = result.size(); } return s;