random_access_file_reader_test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "file/random_access_file_reader.h"
  6. #include <algorithm>
  7. #include "file/file_util.h"
  8. #include "port/port.h"
  9. #include "port/stack_trace.h"
  10. #include "rocksdb/file_system.h"
  11. #include "test_util/sync_point.h"
  12. #include "test_util/testharness.h"
  13. #include "test_util/testutil.h"
  14. #include "util/random.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. class RandomAccessFileReaderTest : public testing::Test {
  17. public:
  18. void SetUp() override {
  19. SetupSyncPointsToMockDirectIO();
  20. env_ = Env::Default();
  21. fs_ = FileSystem::Default();
  22. test_dir_ = test::PerThreadDBPath("random_access_file_reader_test");
  23. ASSERT_OK(fs_->CreateDir(test_dir_, IOOptions(), nullptr));
  24. }
  25. void TearDown() override { EXPECT_OK(DestroyDir(env_, test_dir_)); }
  26. void Write(const std::string& fname, const std::string& content) {
  27. std::unique_ptr<FSWritableFile> f;
  28. ASSERT_OK(fs_->NewWritableFile(Path(fname), FileOptions(), &f, nullptr));
  29. ASSERT_OK(f->Append(content, IOOptions(), nullptr));
  30. ASSERT_OK(f->Close(IOOptions(), nullptr));
  31. }
  32. void Read(const std::string& fname, const FileOptions& opts,
  33. std::unique_ptr<RandomAccessFileReader>* reader) {
  34. std::string fpath = Path(fname);
  35. std::unique_ptr<FSRandomAccessFile> f;
  36. ASSERT_OK(fs_->NewRandomAccessFile(fpath, opts, &f, nullptr));
  37. reader->reset(new RandomAccessFileReader(std::move(f), fpath,
  38. env_->GetSystemClock().get()));
  39. }
  40. void AssertResult(const std::string& content,
  41. const std::vector<FSReadRequest>& reqs) {
  42. for (const auto& r : reqs) {
  43. ASSERT_OK(r.status);
  44. ASSERT_EQ(r.len, r.result.size());
  45. ASSERT_EQ(content.substr(r.offset, r.len), r.result.ToString());
  46. }
  47. }
  48. private:
  49. Env* env_;
  50. std::shared_ptr<FileSystem> fs_;
  51. std::string test_dir_;
  52. std::string Path(const std::string& fname) { return test_dir_ + "/" + fname; }
  53. };
  54. // Skip the following tests in lite mode since direct I/O is unsupported.
  55. TEST_F(RandomAccessFileReaderTest, ReadDirectIO) {
  56. std::string fname = "read-direct-io";
  57. Random rand(0);
  58. std::string content = rand.RandomString(kDefaultPageSize);
  59. Write(fname, content);
  60. FileOptions opts;
  61. opts.use_direct_reads = true;
  62. std::unique_ptr<RandomAccessFileReader> r;
  63. Read(fname, opts, &r);
  64. ASSERT_TRUE(r->use_direct_io());
  65. const size_t page_size = r->file()->GetRequiredBufferAlignment();
  66. size_t offset = page_size / 2;
  67. size_t len = page_size / 3;
  68. Slice result;
  69. AlignedBuf buf;
  70. for (Env::IOPriority rate_limiter_priority : {Env::IO_LOW, Env::IO_TOTAL}) {
  71. IOOptions io_opts;
  72. io_opts.rate_limiter_priority = rate_limiter_priority;
  73. ASSERT_OK(r->Read(io_opts, offset, len, &result, nullptr, &buf));
  74. ASSERT_EQ(result.ToString(), content.substr(offset, len));
  75. }
  76. }
  77. TEST_F(RandomAccessFileReaderTest, MultiReadDirectIO) {
  78. std::vector<FSReadRequest> aligned_reqs;
  79. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
  80. "RandomAccessFileReader::MultiRead:AlignedReqs", [&](void* reqs) {
  81. // Copy reqs, since it's allocated on stack inside MultiRead, which will
  82. // be deallocated after MultiRead returns.
  83. size_t i = 0;
  84. aligned_reqs.resize(
  85. (*reinterpret_cast<std::vector<FSReadRequest>*>(reqs)).size());
  86. for (auto& req :
  87. (*reinterpret_cast<std::vector<FSReadRequest>*>(reqs))) {
  88. aligned_reqs[i].offset = req.offset;
  89. aligned_reqs[i].len = req.len;
  90. aligned_reqs[i].result = req.result;
  91. aligned_reqs[i].status = req.status;
  92. aligned_reqs[i].scratch = req.scratch;
  93. i++;
  94. }
  95. });
  96. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  97. // Creates a file with 3 pages.
  98. std::string fname = "multi-read-direct-io";
  99. Random rand(0);
  100. std::string content = rand.RandomString(3 * kDefaultPageSize);
  101. Write(fname, content);
  102. FileOptions opts;
  103. opts.use_direct_reads = true;
  104. std::unique_ptr<RandomAccessFileReader> r;
  105. Read(fname, opts, &r);
  106. ASSERT_TRUE(r->use_direct_io());
  107. const size_t page_size = r->file()->GetRequiredBufferAlignment();
  108. {
  109. // Reads 2 blocks in the 1st page.
  110. // The results should be SharedSlices of the same underlying buffer.
  111. //
  112. // Illustration (each x is a 1/4 page)
  113. // First page: xxxx
  114. // 1st block: x
  115. // 2nd block: xx
  116. FSReadRequest r0;
  117. r0.offset = 0;
  118. r0.len = page_size / 4;
  119. r0.scratch = nullptr;
  120. FSReadRequest r1;
  121. r1.offset = page_size / 2;
  122. r1.len = page_size / 2;
  123. r1.scratch = nullptr;
  124. std::vector<FSReadRequest> reqs;
  125. reqs.push_back(std::move(r0));
  126. reqs.push_back(std::move(r1));
  127. AlignedBuf aligned_buf;
  128. IODebugContext dbg;
  129. ASSERT_OK(r->MultiRead(IOOptions(), reqs.data(), reqs.size(), &aligned_buf,
  130. &dbg));
  131. AssertResult(content, reqs);
  132. // Reads the first page internally.
  133. ASSERT_EQ(aligned_reqs.size(), 1);
  134. const FSReadRequest& aligned_r = aligned_reqs[0];
  135. ASSERT_OK(aligned_r.status);
  136. ASSERT_EQ(aligned_r.offset, 0);
  137. ASSERT_EQ(aligned_r.len, page_size);
  138. }
  139. {
  140. // Reads 3 blocks:
  141. // 1st block in the 1st page;
  142. // 2nd block from the middle of the 1st page to the middle of the 2nd page;
  143. // 3rd block in the 2nd page.
  144. // The results should be SharedSlices of the same underlying buffer.
  145. //
  146. // Illustration (each x is a 1/4 page)
  147. // 2 pages: xxxxxxxx
  148. // 1st block: x
  149. // 2nd block: xxxx
  150. // 3rd block: x
  151. FSReadRequest r0;
  152. r0.offset = 0;
  153. r0.len = page_size / 4;
  154. r0.scratch = nullptr;
  155. FSReadRequest r1;
  156. r1.offset = page_size / 2;
  157. r1.len = page_size;
  158. r1.scratch = nullptr;
  159. FSReadRequest r2;
  160. r2.offset = 2 * page_size - page_size / 4;
  161. r2.len = page_size / 4;
  162. r2.scratch = nullptr;
  163. std::vector<FSReadRequest> reqs;
  164. reqs.push_back(std::move(r0));
  165. reqs.push_back(std::move(r1));
  166. reqs.push_back(std::move(r2));
  167. AlignedBuf aligned_buf;
  168. IODebugContext dbg;
  169. ASSERT_OK(r->MultiRead(IOOptions(), reqs.data(), reqs.size(), &aligned_buf,
  170. &dbg));
  171. AssertResult(content, reqs);
  172. // Reads the first two pages in one request internally.
  173. ASSERT_EQ(aligned_reqs.size(), 1);
  174. const FSReadRequest& aligned_r = aligned_reqs[0];
  175. ASSERT_OK(aligned_r.status);
  176. ASSERT_EQ(aligned_r.offset, 0);
  177. ASSERT_EQ(aligned_r.len, 2 * page_size);
  178. }
  179. {
  180. // Reads 3 blocks:
  181. // 1st block in the middle of the 1st page;
  182. // 2nd block in the middle of the 2nd page;
  183. // 3rd block in the middle of the 3rd page.
  184. // The results should be SharedSlices of the same underlying buffer.
  185. //
  186. // Illustration (each x is a 1/4 page)
  187. // 3 pages: xxxxxxxxxxxx
  188. // 1st block: xx
  189. // 2nd block: xx
  190. // 3rd block: xx
  191. FSReadRequest r0;
  192. r0.offset = page_size / 4;
  193. r0.len = page_size / 2;
  194. r0.scratch = nullptr;
  195. FSReadRequest r1;
  196. r1.offset = page_size + page_size / 4;
  197. r1.len = page_size / 2;
  198. r1.scratch = nullptr;
  199. FSReadRequest r2;
  200. r2.offset = 2 * page_size + page_size / 4;
  201. r2.len = page_size / 2;
  202. r2.scratch = nullptr;
  203. std::vector<FSReadRequest> reqs;
  204. reqs.push_back(std::move(r0));
  205. reqs.push_back(std::move(r1));
  206. reqs.push_back(std::move(r2));
  207. AlignedBuf aligned_buf;
  208. IODebugContext dbg;
  209. ASSERT_OK(r->MultiRead(IOOptions(), reqs.data(), reqs.size(), &aligned_buf,
  210. &dbg));
  211. AssertResult(content, reqs);
  212. // Reads the first 3 pages in one request internally.
  213. ASSERT_EQ(aligned_reqs.size(), 1);
  214. const FSReadRequest& aligned_r = aligned_reqs[0];
  215. ASSERT_OK(aligned_r.status);
  216. ASSERT_EQ(aligned_r.offset, 0);
  217. ASSERT_EQ(aligned_r.len, 3 * page_size);
  218. }
  219. {
  220. // Reads 2 blocks:
  221. // 1st block in the middle of the 1st page;
  222. // 2nd block in the middle of the 3rd page.
  223. // The results are two different buffers.
  224. //
  225. // Illustration (each x is a 1/4 page)
  226. // 3 pages: xxxxxxxxxxxx
  227. // 1st block: xx
  228. // 2nd block: xx
  229. FSReadRequest r0;
  230. r0.offset = page_size / 4;
  231. r0.len = page_size / 2;
  232. r0.scratch = nullptr;
  233. FSReadRequest r1;
  234. r1.offset = 2 * page_size + page_size / 4;
  235. r1.len = page_size / 2;
  236. r1.scratch = nullptr;
  237. std::vector<FSReadRequest> reqs;
  238. reqs.push_back(std::move(r0));
  239. reqs.push_back(std::move(r1));
  240. AlignedBuf aligned_buf;
  241. IODebugContext dbg;
  242. ASSERT_OK(r->MultiRead(IOOptions(), reqs.data(), reqs.size(), &aligned_buf,
  243. &dbg));
  244. AssertResult(content, reqs);
  245. // Reads the 1st and 3rd pages in two requests internally.
  246. ASSERT_EQ(aligned_reqs.size(), 2);
  247. const FSReadRequest& aligned_r0 = aligned_reqs[0];
  248. const FSReadRequest& aligned_r1 = aligned_reqs[1];
  249. ASSERT_OK(aligned_r0.status);
  250. ASSERT_EQ(aligned_r0.offset, 0);
  251. ASSERT_EQ(aligned_r0.len, page_size);
  252. ASSERT_OK(aligned_r1.status);
  253. ASSERT_EQ(aligned_r1.offset, 2 * page_size);
  254. ASSERT_EQ(aligned_r1.len, page_size);
  255. }
  256. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  257. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
  258. }
  259. TEST(FSReadRequest, Align) {
  260. FSReadRequest r;
  261. r.offset = 2000;
  262. r.len = 2000;
  263. r.scratch = nullptr;
  264. ASSERT_OK(r.status);
  265. FSReadRequest aligned_r = Align(r, 1024);
  266. ASSERT_OK(r.status);
  267. ASSERT_OK(aligned_r.status);
  268. ASSERT_EQ(aligned_r.offset, 1024);
  269. ASSERT_EQ(aligned_r.len, 3072);
  270. }
  271. TEST(FSReadRequest, TryMerge) {
  272. // reverse means merging dest into src.
  273. for (bool reverse : {true, false}) {
  274. {
  275. // dest: [ ]
  276. // src: [ ]
  277. FSReadRequest dest;
  278. dest.offset = 0;
  279. dest.len = 10;
  280. dest.scratch = nullptr;
  281. ASSERT_OK(dest.status);
  282. FSReadRequest src;
  283. src.offset = 15;
  284. src.len = 10;
  285. src.scratch = nullptr;
  286. ASSERT_OK(src.status);
  287. if (reverse) {
  288. std::swap(dest, src);
  289. }
  290. ASSERT_FALSE(TryMerge(&dest, src));
  291. ASSERT_OK(dest.status);
  292. ASSERT_OK(src.status);
  293. }
  294. {
  295. // dest: [ ]
  296. // src: [ ]
  297. FSReadRequest dest;
  298. dest.offset = 0;
  299. dest.len = 10;
  300. dest.scratch = nullptr;
  301. ASSERT_OK(dest.status);
  302. FSReadRequest src;
  303. src.offset = 10;
  304. src.len = 10;
  305. src.scratch = nullptr;
  306. ASSERT_OK(src.status);
  307. if (reverse) {
  308. std::swap(dest, src);
  309. }
  310. ASSERT_TRUE(TryMerge(&dest, src));
  311. ASSERT_EQ(dest.offset, 0);
  312. ASSERT_EQ(dest.len, 20);
  313. ASSERT_OK(dest.status);
  314. ASSERT_OK(src.status);
  315. }
  316. {
  317. // dest: [ ]
  318. // src: [ ]
  319. FSReadRequest dest;
  320. dest.offset = 0;
  321. dest.len = 10;
  322. dest.scratch = nullptr;
  323. ASSERT_OK(dest.status);
  324. FSReadRequest src;
  325. src.offset = 5;
  326. src.len = 10;
  327. src.scratch = nullptr;
  328. ASSERT_OK(src.status);
  329. if (reverse) {
  330. std::swap(dest, src);
  331. }
  332. ASSERT_TRUE(TryMerge(&dest, src));
  333. ASSERT_EQ(dest.offset, 0);
  334. ASSERT_EQ(dest.len, 15);
  335. ASSERT_OK(dest.status);
  336. ASSERT_OK(src.status);
  337. }
  338. {
  339. // dest: [ ]
  340. // src: [ ]
  341. FSReadRequest dest;
  342. dest.offset = 0;
  343. dest.len = 10;
  344. dest.scratch = nullptr;
  345. ASSERT_OK(dest.status);
  346. FSReadRequest src;
  347. src.offset = 5;
  348. src.len = 5;
  349. src.scratch = nullptr;
  350. ASSERT_OK(src.status);
  351. if (reverse) {
  352. std::swap(dest, src);
  353. }
  354. ASSERT_TRUE(TryMerge(&dest, src));
  355. ASSERT_EQ(dest.offset, 0);
  356. ASSERT_EQ(dest.len, 10);
  357. ASSERT_OK(dest.status);
  358. ASSERT_OK(src.status);
  359. }
  360. {
  361. // dest: [ ]
  362. // src: [ ]
  363. FSReadRequest dest;
  364. dest.offset = 0;
  365. dest.len = 10;
  366. dest.scratch = nullptr;
  367. ASSERT_OK(dest.status);
  368. FSReadRequest src;
  369. src.offset = 5;
  370. src.len = 1;
  371. src.scratch = nullptr;
  372. ASSERT_OK(src.status);
  373. if (reverse) {
  374. std::swap(dest, src);
  375. }
  376. ASSERT_TRUE(TryMerge(&dest, src));
  377. ASSERT_EQ(dest.offset, 0);
  378. ASSERT_EQ(dest.len, 10);
  379. ASSERT_OK(dest.status);
  380. ASSERT_OK(src.status);
  381. }
  382. {
  383. // dest: [ ]
  384. // src: [ ]
  385. FSReadRequest dest;
  386. dest.offset = 0;
  387. dest.len = 10;
  388. dest.scratch = nullptr;
  389. ASSERT_OK(dest.status);
  390. FSReadRequest src;
  391. src.offset = 0;
  392. src.len = 10;
  393. src.scratch = nullptr;
  394. ASSERT_OK(src.status);
  395. if (reverse) {
  396. std::swap(dest, src);
  397. }
  398. ASSERT_TRUE(TryMerge(&dest, src));
  399. ASSERT_EQ(dest.offset, 0);
  400. ASSERT_EQ(dest.len, 10);
  401. ASSERT_OK(dest.status);
  402. ASSERT_OK(src.status);
  403. }
  404. {
  405. // dest: [ ]
  406. // src: [ ]
  407. FSReadRequest dest;
  408. dest.offset = 0;
  409. dest.len = 10;
  410. dest.scratch = nullptr;
  411. ASSERT_OK(dest.status);
  412. FSReadRequest src;
  413. src.offset = 0;
  414. src.len = 5;
  415. src.scratch = nullptr;
  416. ASSERT_OK(src.status);
  417. if (reverse) {
  418. std::swap(dest, src);
  419. }
  420. ASSERT_TRUE(TryMerge(&dest, src));
  421. ASSERT_EQ(dest.offset, 0);
  422. ASSERT_EQ(dest.len, 10);
  423. ASSERT_OK(dest.status);
  424. ASSERT_OK(src.status);
  425. }
  426. }
  427. }
  428. } // namespace ROCKSDB_NAMESPACE
  429. int main(int argc, char** argv) {
  430. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  431. ::testing::InitGoogleTest(&argc, argv);
  432. return RUN_ALL_TESTS();
  433. }