Skip to content

Commit

Permalink
Fix result slice's address for direct io read (facebook#6672)
Browse files Browse the repository at this point in the history
Summary:
When aligned_buf is provided, the result slice's starting address should take offset advance into account.
Pull Request resolved: facebook#6672

Test Plan: make check

Reviewed By: anand1976

Differential Revision: D20934198

Pulled By: cheng-chang

fbshipit-source-id: c3475c9c132b92c50d8c7c399fca2e9e76870803
  • Loading branch information
Cheng Chang authored and facebook-github-bot committed Apr 9, 2020
1 parent 83fc90b commit 31759a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion file/random_access_file_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Status RandomAccessFileReader::Read(uint64_t offset, size_t n, Slice* result,
if (aligned_buf == nullptr) {
buf.Read(scratch, offset_advance, res_len);
} else {
scratch = buf.BufferStart();
scratch = buf.BufferStart() + offset_advance;
aligned_buf->reset(buf.Release());
}
}
Expand Down
28 changes: 28 additions & 0 deletions file/random_access_file_reader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ class RandomAccessFileReaderTest : public testing::Test {
}
};

TEST_F(RandomAccessFileReaderTest, ReadDirectIO) {
if (!IsDirectIOSupported()) {
printf("Direct IO is not supported, skip this test\n");
return;
}

std::string fname = "read-direct-io";
Random rand(0);
std::string content;
test::RandomString(&rand, static_cast<int>(alignment()), &content);
Write(fname, content);

FileOptions opts;
opts.use_direct_reads = true;
std::unique_ptr<RandomAccessFileReader> r;
Read(fname, opts, &r);
ASSERT_TRUE(r->use_direct_io());

size_t offset = alignment() / 2;
size_t len = alignment() / 3;
Slice result;
AlignedBuf buf;
for (bool for_compaction : {true, false}) {
ASSERT_OK(r->Read(offset, len, &result, nullptr, &buf, for_compaction));
ASSERT_EQ(result.ToString(), content.substr(offset, len));
}
}

TEST_F(RandomAccessFileReaderTest, MultiReadDirectIO) {
if (!IsDirectIOSupported()) {
printf("Direct IO is not supported, skip this test\n");
Expand Down

0 comments on commit 31759a7

Please sign in to comment.