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 //
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9
10 #ifndef OS_WIN
11 #include <sys/ioctl.h>
12 #endif
13
14 #include <sys/types.h>
15
16 #include <iostream>
17 #include <unordered_set>
18 #include <atomic>
19 #include <list>
20
21 #ifdef OS_LINUX
22 #include <fcntl.h>
23 #include <linux/fs.h>
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #endif
28
29 #ifdef ROCKSDB_FALLOCATE_PRESENT
30 #include <errno.h>
31 #endif
32
33 #include "env/env_chroot.h"
34 #include "logging/log_buffer.h"
35 #include "port/malloc.h"
36 #include "port/port.h"
37 #include "rocksdb/env.h"
38 #include "test_util/sync_point.h"
39 #include "test_util/testharness.h"
40 #include "test_util/testutil.h"
41 #include "util/coding.h"
42 #include "util/mutexlock.h"
43 #include "util/string_util.h"
44
45 #ifdef OS_LINUX
46 static const size_t kPageSize = sysconf(_SC_PAGESIZE);
47 #else
48 static const size_t kPageSize = 4 * 1024;
49 #endif
50
51 namespace rocksdb {
52
53 static const int kDelayMicros = 100000;
54
55 struct Deleter {
Deleterrocksdb::Deleter56 explicit Deleter(void (*fn)(void*)) : fn_(fn) {}
57
operator ()rocksdb::Deleter58 void operator()(void* ptr) {
59 assert(fn_);
60 assert(ptr);
61 (*fn_)(ptr);
62 }
63
64 void (*fn_)(void*);
65 };
66
NewAligned(const size_t size,const char ch)67 std::unique_ptr<char, Deleter> NewAligned(const size_t size, const char ch) {
68 char* ptr = nullptr;
69 #ifdef OS_WIN
70 if (nullptr == (ptr = reinterpret_cast<char*>(_aligned_malloc(size, kPageSize)))) {
71 return std::unique_ptr<char, Deleter>(nullptr, Deleter(_aligned_free));
72 }
73 std::unique_ptr<char, Deleter> uptr(ptr, Deleter(_aligned_free));
74 #else
75 if (posix_memalign(reinterpret_cast<void**>(&ptr), kPageSize, size) != 0) {
76 return std::unique_ptr<char, Deleter>(nullptr, Deleter(free));
77 }
78 std::unique_ptr<char, Deleter> uptr(ptr, Deleter(free));
79 #endif
80 memset(uptr.get(), ch, size);
81 return uptr;
82 }
83
84 class EnvPosixTest : public testing::Test {
85 private:
86 port::Mutex mu_;
87 std::string events_;
88
89 public:
90 Env* env_;
91 bool direct_io_;
EnvPosixTest()92 EnvPosixTest() : env_(Env::Default()), direct_io_(false) {}
93 };
94
95 class EnvPosixTestWithParam
96 : public EnvPosixTest,
97 public ::testing::WithParamInterface<std::pair<Env*, bool>> {
98 public:
EnvPosixTestWithParam()99 EnvPosixTestWithParam() {
100 std::pair<Env*, bool> param_pair = GetParam();
101 env_ = param_pair.first;
102 direct_io_ = param_pair.second;
103 }
104
WaitThreadPoolsEmpty()105 void WaitThreadPoolsEmpty() {
106 // Wait until the thread pools are empty.
107 while (env_->GetThreadPoolQueueLen(Env::Priority::LOW) != 0) {
108 Env::Default()->SleepForMicroseconds(kDelayMicros);
109 }
110 while (env_->GetThreadPoolQueueLen(Env::Priority::HIGH) != 0) {
111 Env::Default()->SleepForMicroseconds(kDelayMicros);
112 }
113 }
114
~EnvPosixTestWithParam()115 ~EnvPosixTestWithParam() override { WaitThreadPoolsEmpty(); }
116 };
117
SetBool(void * ptr)118 static void SetBool(void* ptr) {
119 reinterpret_cast<std::atomic<bool>*>(ptr)->store(true);
120 }
121
TEST_F(EnvPosixTest,DISABLED_RunImmediately)122 TEST_F(EnvPosixTest, DISABLED_RunImmediately) {
123 for (int pri = Env::BOTTOM; pri < Env::TOTAL; ++pri) {
124 std::atomic<bool> called(false);
125 env_->SetBackgroundThreads(1, static_cast<Env::Priority>(pri));
126 env_->Schedule(&SetBool, &called, static_cast<Env::Priority>(pri));
127 Env::Default()->SleepForMicroseconds(kDelayMicros);
128 ASSERT_TRUE(called.load());
129 }
130 }
131
TEST_F(EnvPosixTest,RunEventually)132 TEST_F(EnvPosixTest, RunEventually) {
133 std::atomic<bool> called(false);
134 env_->StartThread(&SetBool, &called);
135 env_->WaitForJoin();
136 ASSERT_TRUE(called.load());
137 }
138
139 #ifdef OS_WIN
TEST_F(EnvPosixTest,AreFilesSame)140 TEST_F(EnvPosixTest, AreFilesSame) {
141 {
142 bool tmp;
143 if (env_->AreFilesSame("", "", &tmp).IsNotSupported()) {
144 fprintf(stderr,
145 "skipping EnvBasicTestWithParam.AreFilesSame due to "
146 "unsupported Env::AreFilesSame\n");
147 return;
148 }
149 }
150
151 const EnvOptions soptions;
152 auto* env = Env::Default();
153 std::string same_file_name = test::PerThreadDBPath(env, "same_file");
154 std::string same_file_link_name = same_file_name + "_link";
155
156 std::unique_ptr<WritableFile> same_file;
157 ASSERT_OK(env->NewWritableFile(same_file_name,
158 &same_file, soptions));
159 same_file->Append("random_data");
160 ASSERT_OK(same_file->Flush());
161 same_file.reset();
162
163 ASSERT_OK(env->LinkFile(same_file_name, same_file_link_name));
164 bool result = false;
165 ASSERT_OK(env->AreFilesSame(same_file_name, same_file_link_name, &result));
166 ASSERT_TRUE(result);
167 }
168 #endif
169
170 #ifdef OS_LINUX
TEST_F(EnvPosixTest,DISABLED_FilePermission)171 TEST_F(EnvPosixTest, DISABLED_FilePermission) {
172 // Only works for Linux environment
173 if (env_ == Env::Default()) {
174 EnvOptions soptions;
175 std::vector<std::string> fileNames{
176 test::PerThreadDBPath(env_, "testfile"),
177 test::PerThreadDBPath(env_, "testfile1")};
178 std::unique_ptr<WritableFile> wfile;
179 ASSERT_OK(env_->NewWritableFile(fileNames[0], &wfile, soptions));
180 ASSERT_OK(env_->NewWritableFile(fileNames[1], &wfile, soptions));
181 wfile.reset();
182 std::unique_ptr<RandomRWFile> rwfile;
183 ASSERT_OK(env_->NewRandomRWFile(fileNames[1], &rwfile, soptions));
184
185 struct stat sb;
186 for (const auto& filename : fileNames) {
187 if (::stat(filename.c_str(), &sb) == 0) {
188 ASSERT_EQ(sb.st_mode & 0777, 0644);
189 }
190 env_->DeleteFile(filename);
191 }
192
193 env_->SetAllowNonOwnerAccess(false);
194 ASSERT_OK(env_->NewWritableFile(fileNames[0], &wfile, soptions));
195 ASSERT_OK(env_->NewWritableFile(fileNames[1], &wfile, soptions));
196 wfile.reset();
197 ASSERT_OK(env_->NewRandomRWFile(fileNames[1], &rwfile, soptions));
198
199 for (const auto& filename : fileNames) {
200 if (::stat(filename.c_str(), &sb) == 0) {
201 ASSERT_EQ(sb.st_mode & 0777, 0600);
202 }
203 env_->DeleteFile(filename);
204 }
205 }
206 }
207 #endif
208
TEST_F(EnvPosixTest,MemoryMappedFileBuffer)209 TEST_F(EnvPosixTest, MemoryMappedFileBuffer) {
210 const int kFileBytes = 1 << 15; // 32 KB
211 std::string expected_data;
212 std::string fname = test::PerThreadDBPath(env_, "testfile");
213 {
214 std::unique_ptr<WritableFile> wfile;
215 const EnvOptions soptions;
216 ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
217
218 Random rnd(301);
219 test::RandomString(&rnd, kFileBytes, &expected_data);
220 ASSERT_OK(wfile->Append(expected_data));
221 }
222
223 std::unique_ptr<MemoryMappedFileBuffer> mmap_buffer;
224 Status status = env_->NewMemoryMappedFileBuffer(fname, &mmap_buffer);
225 // it should be supported at least on linux
226 #if !defined(OS_LINUX)
227 if (status.IsNotSupported()) {
228 fprintf(stderr,
229 "skipping EnvPosixTest.MemoryMappedFileBuffer due to "
230 "unsupported Env::NewMemoryMappedFileBuffer\n");
231 return;
232 }
233 #endif // !defined(OS_LINUX)
234
235 ASSERT_OK(status);
236 ASSERT_NE(nullptr, mmap_buffer.get());
237 ASSERT_NE(nullptr, mmap_buffer->GetBase());
238 ASSERT_EQ(kFileBytes, mmap_buffer->GetLen());
239 std::string actual_data(reinterpret_cast<const char*>(mmap_buffer->GetBase()),
240 mmap_buffer->GetLen());
241 ASSERT_EQ(expected_data, actual_data);
242 }
243
244 #ifndef ROCKSDB_NO_DYNAMIC_EXTENSION
TEST_F(EnvPosixTest,LoadRocksDBLibrary)245 TEST_F(EnvPosixTest, LoadRocksDBLibrary) {
246 std::shared_ptr<DynamicLibrary> library;
247 std::function<void*(void*, const char*)> function;
248 Status status = env_->LoadLibrary("no-such-library", "", &library);
249 ASSERT_NOK(status);
250 ASSERT_EQ(nullptr, library.get());
251 status = env_->LoadLibrary("rocksdb", "", &library);
252 if (status.ok()) { // If we have can find a rocksdb shared library
253 ASSERT_NE(nullptr, library.get());
254 ASSERT_OK(library->LoadFunction("rocksdb_create_default_env",
255 &function)); // from C definition
256 ASSERT_NE(nullptr, function);
257 ASSERT_NOK(library->LoadFunction("no-such-method", &function));
258 ASSERT_EQ(nullptr, function);
259 ASSERT_OK(env_->LoadLibrary(library->Name(), "", &library));
260 } else {
261 ASSERT_EQ(nullptr, library.get());
262 }
263 }
264 #endif // !ROCKSDB_NO_DYNAMIC_EXTENSION
265
266 #if !defined(OS_WIN) && !defined(ROCKSDB_NO_DYNAMIC_EXTENSION)
TEST_F(EnvPosixTest,LoadRocksDBLibraryWithSearchPath)267 TEST_F(EnvPosixTest, LoadRocksDBLibraryWithSearchPath) {
268 std::shared_ptr<DynamicLibrary> library;
269 std::function<void*(void*, const char*)> function;
270 ASSERT_NOK(env_->LoadLibrary("no-such-library", "/tmp", &library));
271 ASSERT_EQ(nullptr, library.get());
272 ASSERT_NOK(env_->LoadLibrary("dl", "/tmp", &library));
273 ASSERT_EQ(nullptr, library.get());
274 Status status = env_->LoadLibrary("rocksdb", "/tmp:./", &library);
275 if (status.ok()) {
276 ASSERT_NE(nullptr, library.get());
277 ASSERT_OK(env_->LoadLibrary(library->Name(), "", &library));
278 }
279 char buff[1024];
280 std::string cwd = getcwd(buff, sizeof(buff));
281
282 status = env_->LoadLibrary("rocksdb", "/tmp:" + cwd, &library);
283 if (status.ok()) {
284 ASSERT_NE(nullptr, library.get());
285 ASSERT_OK(env_->LoadLibrary(library->Name(), "", &library));
286 }
287 }
288 #endif // !OS_WIN && !ROCKSDB_NO_DYNAMIC_EXTENSION
289
TEST_P(EnvPosixTestWithParam,UnSchedule)290 TEST_P(EnvPosixTestWithParam, UnSchedule) {
291 std::atomic<bool> called(false);
292 env_->SetBackgroundThreads(1, Env::LOW);
293
294 /* Block the low priority queue */
295 test::SleepingBackgroundTask sleeping_task, sleeping_task1;
296 env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &sleeping_task,
297 Env::Priority::LOW);
298
299 /* Schedule another task */
300 env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &sleeping_task1,
301 Env::Priority::LOW, &sleeping_task1);
302
303 /* Remove it with a different tag */
304 ASSERT_EQ(0, env_->UnSchedule(&called, Env::Priority::LOW));
305
306 /* Remove it from the queue with the right tag */
307 ASSERT_EQ(1, env_->UnSchedule(&sleeping_task1, Env::Priority::LOW));
308
309 // Unblock background thread
310 sleeping_task.WakeUp();
311
312 /* Schedule another task */
313 env_->Schedule(&SetBool, &called);
314 for (int i = 0; i < kDelayMicros; i++) {
315 if (called.load()) {
316 break;
317 }
318 Env::Default()->SleepForMicroseconds(1);
319 }
320 ASSERT_TRUE(called.load());
321
322 ASSERT_TRUE(!sleeping_task.IsSleeping() && !sleeping_task1.IsSleeping());
323 WaitThreadPoolsEmpty();
324 }
325
326 // This tests assumes that the last scheduled
327 // task will run last. In fact, in the allotted
328 // sleeping time nothing may actually run or they may
329 // run in any order. The purpose of the test is unclear.
330 #ifndef OS_WIN
TEST_P(EnvPosixTestWithParam,RunMany)331 TEST_P(EnvPosixTestWithParam, RunMany) {
332 std::atomic<int> last_id(0);
333
334 struct CB {
335 std::atomic<int>* last_id_ptr; // Pointer to shared slot
336 int id; // Order# for the execution of this callback
337
338 CB(std::atomic<int>* p, int i) : last_id_ptr(p), id(i) {}
339
340 static void Run(void* v) {
341 CB* cb = reinterpret_cast<CB*>(v);
342 int cur = cb->last_id_ptr->load();
343 ASSERT_EQ(cb->id - 1, cur);
344 cb->last_id_ptr->store(cb->id);
345 }
346 };
347
348 // Schedule in different order than start time
349 CB cb1(&last_id, 1);
350 CB cb2(&last_id, 2);
351 CB cb3(&last_id, 3);
352 CB cb4(&last_id, 4);
353 env_->Schedule(&CB::Run, &cb1);
354 env_->Schedule(&CB::Run, &cb2);
355 env_->Schedule(&CB::Run, &cb3);
356 env_->Schedule(&CB::Run, &cb4);
357
358 Env::Default()->SleepForMicroseconds(kDelayMicros);
359 int cur = last_id.load(std::memory_order_acquire);
360 ASSERT_EQ(4, cur);
361 WaitThreadPoolsEmpty();
362 }
363 #endif
364
365 struct State {
366 port::Mutex mu;
367 int val;
368 int num_running;
369 };
370
ThreadBody(void * arg)371 static void ThreadBody(void* arg) {
372 State* s = reinterpret_cast<State*>(arg);
373 s->mu.Lock();
374 s->val += 1;
375 s->num_running -= 1;
376 s->mu.Unlock();
377 }
378
TEST_P(EnvPosixTestWithParam,StartThread)379 TEST_P(EnvPosixTestWithParam, StartThread) {
380 State state;
381 state.val = 0;
382 state.num_running = 3;
383 for (int i = 0; i < 3; i++) {
384 env_->StartThread(&ThreadBody, &state);
385 }
386 while (true) {
387 state.mu.Lock();
388 int num = state.num_running;
389 state.mu.Unlock();
390 if (num == 0) {
391 break;
392 }
393 Env::Default()->SleepForMicroseconds(kDelayMicros);
394 }
395 ASSERT_EQ(state.val, 3);
396 WaitThreadPoolsEmpty();
397 }
398
TEST_P(EnvPosixTestWithParam,TwoPools)399 TEST_P(EnvPosixTestWithParam, TwoPools) {
400 // Data structures to signal tasks to run.
401 port::Mutex mutex;
402 port::CondVar cv(&mutex);
403 bool should_start = false;
404
405 class CB {
406 public:
407 CB(const std::string& pool_name, int pool_size, port::Mutex* trigger_mu,
408 port::CondVar* trigger_cv, bool* _should_start)
409 : mu_(),
410 num_running_(0),
411 num_finished_(0),
412 pool_size_(pool_size),
413 pool_name_(pool_name),
414 trigger_mu_(trigger_mu),
415 trigger_cv_(trigger_cv),
416 should_start_(_should_start) {}
417
418 static void Run(void* v) {
419 CB* cb = reinterpret_cast<CB*>(v);
420 cb->Run();
421 }
422
423 void Run() {
424 {
425 MutexLock l(&mu_);
426 num_running_++;
427 // make sure we don't have more than pool_size_ jobs running.
428 ASSERT_LE(num_running_, pool_size_.load());
429 }
430
431 {
432 MutexLock l(trigger_mu_);
433 while (!(*should_start_)) {
434 trigger_cv_->Wait();
435 }
436 }
437
438 {
439 MutexLock l(&mu_);
440 num_running_--;
441 num_finished_++;
442 }
443 }
444
445 int NumFinished() {
446 MutexLock l(&mu_);
447 return num_finished_;
448 }
449
450 void Reset(int pool_size) {
451 pool_size_.store(pool_size);
452 num_finished_ = 0;
453 }
454
455 private:
456 port::Mutex mu_;
457 int num_running_;
458 int num_finished_;
459 std::atomic<int> pool_size_;
460 std::string pool_name_;
461 port::Mutex* trigger_mu_;
462 port::CondVar* trigger_cv_;
463 bool* should_start_;
464 };
465
466 const int kLowPoolSize = 2;
467 const int kHighPoolSize = 4;
468 const int kJobs = 8;
469
470 CB low_pool_job("low", kLowPoolSize, &mutex, &cv, &should_start);
471 CB high_pool_job("high", kHighPoolSize, &mutex, &cv, &should_start);
472
473 env_->SetBackgroundThreads(kLowPoolSize);
474 env_->SetBackgroundThreads(kHighPoolSize, Env::Priority::HIGH);
475
476 ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::LOW));
477 ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
478
479 // schedule same number of jobs in each pool
480 for (int i = 0; i < kJobs; i++) {
481 env_->Schedule(&CB::Run, &low_pool_job);
482 env_->Schedule(&CB::Run, &high_pool_job, Env::Priority::HIGH);
483 }
484 // Wait a short while for the jobs to be dispatched.
485 int sleep_count = 0;
486 while ((unsigned int)(kJobs - kLowPoolSize) !=
487 env_->GetThreadPoolQueueLen(Env::Priority::LOW) ||
488 (unsigned int)(kJobs - kHighPoolSize) !=
489 env_->GetThreadPoolQueueLen(Env::Priority::HIGH)) {
490 env_->SleepForMicroseconds(kDelayMicros);
491 if (++sleep_count > 100) {
492 break;
493 }
494 }
495
496 ASSERT_EQ((unsigned int)(kJobs - kLowPoolSize),
497 env_->GetThreadPoolQueueLen());
498 ASSERT_EQ((unsigned int)(kJobs - kLowPoolSize),
499 env_->GetThreadPoolQueueLen(Env::Priority::LOW));
500 ASSERT_EQ((unsigned int)(kJobs - kHighPoolSize),
501 env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
502
503 // Trigger jobs to run.
504 {
505 MutexLock l(&mutex);
506 should_start = true;
507 cv.SignalAll();
508 }
509
510 // wait for all jobs to finish
511 while (low_pool_job.NumFinished() < kJobs ||
512 high_pool_job.NumFinished() < kJobs) {
513 env_->SleepForMicroseconds(kDelayMicros);
514 }
515
516 ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::LOW));
517 ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
518
519 // Hold jobs to schedule;
520 should_start = false;
521
522 // call IncBackgroundThreadsIfNeeded to two pools. One increasing and
523 // the other decreasing
524 env_->IncBackgroundThreadsIfNeeded(kLowPoolSize - 1, Env::Priority::LOW);
525 env_->IncBackgroundThreadsIfNeeded(kHighPoolSize + 1, Env::Priority::HIGH);
526 high_pool_job.Reset(kHighPoolSize + 1);
527 low_pool_job.Reset(kLowPoolSize);
528
529 // schedule same number of jobs in each pool
530 for (int i = 0; i < kJobs; i++) {
531 env_->Schedule(&CB::Run, &low_pool_job);
532 env_->Schedule(&CB::Run, &high_pool_job, Env::Priority::HIGH);
533 }
534 // Wait a short while for the jobs to be dispatched.
535 sleep_count = 0;
536 while ((unsigned int)(kJobs - kLowPoolSize) !=
537 env_->GetThreadPoolQueueLen(Env::Priority::LOW) ||
538 (unsigned int)(kJobs - (kHighPoolSize + 1)) !=
539 env_->GetThreadPoolQueueLen(Env::Priority::HIGH)) {
540 env_->SleepForMicroseconds(kDelayMicros);
541 if (++sleep_count > 100) {
542 break;
543 }
544 }
545 ASSERT_EQ((unsigned int)(kJobs - kLowPoolSize),
546 env_->GetThreadPoolQueueLen());
547 ASSERT_EQ((unsigned int)(kJobs - kLowPoolSize),
548 env_->GetThreadPoolQueueLen(Env::Priority::LOW));
549 ASSERT_EQ((unsigned int)(kJobs - (kHighPoolSize + 1)),
550 env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
551
552 // Trigger jobs to run.
553 {
554 MutexLock l(&mutex);
555 should_start = true;
556 cv.SignalAll();
557 }
558
559 // wait for all jobs to finish
560 while (low_pool_job.NumFinished() < kJobs ||
561 high_pool_job.NumFinished() < kJobs) {
562 env_->SleepForMicroseconds(kDelayMicros);
563 }
564
565 env_->SetBackgroundThreads(kHighPoolSize, Env::Priority::HIGH);
566 WaitThreadPoolsEmpty();
567 }
568
TEST_P(EnvPosixTestWithParam,DecreaseNumBgThreads)569 TEST_P(EnvPosixTestWithParam, DecreaseNumBgThreads) {
570 std::vector<test::SleepingBackgroundTask> tasks(10);
571
572 // Set number of thread to 1 first.
573 env_->SetBackgroundThreads(1, Env::Priority::HIGH);
574 Env::Default()->SleepForMicroseconds(kDelayMicros);
575
576 // Schedule 3 tasks. 0 running; Task 1, 2 waiting.
577 for (size_t i = 0; i < 3; i++) {
578 env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[i],
579 Env::Priority::HIGH);
580 Env::Default()->SleepForMicroseconds(kDelayMicros);
581 }
582 ASSERT_EQ(2U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
583 ASSERT_TRUE(tasks[0].IsSleeping());
584 ASSERT_TRUE(!tasks[1].IsSleeping());
585 ASSERT_TRUE(!tasks[2].IsSleeping());
586
587 // Increase to 2 threads. Task 0, 1 running; 2 waiting
588 env_->SetBackgroundThreads(2, Env::Priority::HIGH);
589 Env::Default()->SleepForMicroseconds(kDelayMicros);
590 ASSERT_EQ(1U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
591 ASSERT_TRUE(tasks[0].IsSleeping());
592 ASSERT_TRUE(tasks[1].IsSleeping());
593 ASSERT_TRUE(!tasks[2].IsSleeping());
594
595 // Shrink back to 1 thread. Still task 0, 1 running, 2 waiting
596 env_->SetBackgroundThreads(1, Env::Priority::HIGH);
597 Env::Default()->SleepForMicroseconds(kDelayMicros);
598 ASSERT_EQ(1U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
599 ASSERT_TRUE(tasks[0].IsSleeping());
600 ASSERT_TRUE(tasks[1].IsSleeping());
601 ASSERT_TRUE(!tasks[2].IsSleeping());
602
603 // The last task finishes. Task 0 running, 2 waiting.
604 tasks[1].WakeUp();
605 Env::Default()->SleepForMicroseconds(kDelayMicros);
606 ASSERT_EQ(1U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
607 ASSERT_TRUE(tasks[0].IsSleeping());
608 ASSERT_TRUE(!tasks[1].IsSleeping());
609 ASSERT_TRUE(!tasks[2].IsSleeping());
610
611 // Increase to 5 threads. Task 0 and 2 running.
612 env_->SetBackgroundThreads(5, Env::Priority::HIGH);
613 Env::Default()->SleepForMicroseconds(kDelayMicros);
614 ASSERT_EQ((unsigned int)0, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
615 ASSERT_TRUE(tasks[0].IsSleeping());
616 ASSERT_TRUE(tasks[2].IsSleeping());
617
618 // Change number of threads a couple of times while there is no sufficient
619 // tasks.
620 env_->SetBackgroundThreads(7, Env::Priority::HIGH);
621 Env::Default()->SleepForMicroseconds(kDelayMicros);
622 tasks[2].WakeUp();
623 ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
624 env_->SetBackgroundThreads(3, Env::Priority::HIGH);
625 Env::Default()->SleepForMicroseconds(kDelayMicros);
626 ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
627 env_->SetBackgroundThreads(4, Env::Priority::HIGH);
628 Env::Default()->SleepForMicroseconds(kDelayMicros);
629 ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
630 env_->SetBackgroundThreads(5, Env::Priority::HIGH);
631 Env::Default()->SleepForMicroseconds(kDelayMicros);
632 ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
633 env_->SetBackgroundThreads(4, Env::Priority::HIGH);
634 Env::Default()->SleepForMicroseconds(kDelayMicros);
635 ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
636
637 Env::Default()->SleepForMicroseconds(kDelayMicros * 50);
638
639 // Enqueue 5 more tasks. Thread pool size now is 4.
640 // Task 0, 3, 4, 5 running;6, 7 waiting.
641 for (size_t i = 3; i < 8; i++) {
642 env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[i],
643 Env::Priority::HIGH);
644 }
645 Env::Default()->SleepForMicroseconds(kDelayMicros);
646 ASSERT_EQ(2U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
647 ASSERT_TRUE(tasks[3].IsSleeping());
648 ASSERT_TRUE(tasks[4].IsSleeping());
649 ASSERT_TRUE(tasks[5].IsSleeping());
650 ASSERT_TRUE(!tasks[6].IsSleeping());
651 ASSERT_TRUE(!tasks[7].IsSleeping());
652
653 // Wake up task 0, 3 and 4. Task 5, 6, 7 running.
654 tasks[0].WakeUp();
655 tasks[3].WakeUp();
656 tasks[4].WakeUp();
657
658 Env::Default()->SleepForMicroseconds(kDelayMicros);
659 ASSERT_EQ((unsigned int)0, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
660 for (size_t i = 5; i < 8; i++) {
661 ASSERT_TRUE(tasks[i].IsSleeping());
662 }
663
664 // Shrink back to 1 thread. Still task 5, 6, 7 running
665 env_->SetBackgroundThreads(1, Env::Priority::HIGH);
666 Env::Default()->SleepForMicroseconds(kDelayMicros);
667 ASSERT_TRUE(tasks[5].IsSleeping());
668 ASSERT_TRUE(tasks[6].IsSleeping());
669 ASSERT_TRUE(tasks[7].IsSleeping());
670
671 // Wake up task 6. Task 5, 7 running
672 tasks[6].WakeUp();
673 Env::Default()->SleepForMicroseconds(kDelayMicros);
674 ASSERT_TRUE(tasks[5].IsSleeping());
675 ASSERT_TRUE(!tasks[6].IsSleeping());
676 ASSERT_TRUE(tasks[7].IsSleeping());
677
678 // Wake up threads 7. Task 5 running
679 tasks[7].WakeUp();
680 Env::Default()->SleepForMicroseconds(kDelayMicros);
681 ASSERT_TRUE(!tasks[7].IsSleeping());
682
683 // Enqueue thread 8 and 9. Task 5 running; one of 8, 9 might be running.
684 env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[8],
685 Env::Priority::HIGH);
686 env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[9],
687 Env::Priority::HIGH);
688 Env::Default()->SleepForMicroseconds(kDelayMicros);
689 ASSERT_GT(env_->GetThreadPoolQueueLen(Env::Priority::HIGH), (unsigned int)0);
690 ASSERT_TRUE(!tasks[8].IsSleeping() || !tasks[9].IsSleeping());
691
692 // Increase to 4 threads. Task 5, 8, 9 running.
693 env_->SetBackgroundThreads(4, Env::Priority::HIGH);
694 Env::Default()->SleepForMicroseconds(kDelayMicros);
695 ASSERT_EQ((unsigned int)0, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
696 ASSERT_TRUE(tasks[8].IsSleeping());
697 ASSERT_TRUE(tasks[9].IsSleeping());
698
699 // Shrink to 1 thread
700 env_->SetBackgroundThreads(1, Env::Priority::HIGH);
701
702 // Wake up thread 9.
703 tasks[9].WakeUp();
704 Env::Default()->SleepForMicroseconds(kDelayMicros);
705 ASSERT_TRUE(!tasks[9].IsSleeping());
706 ASSERT_TRUE(tasks[8].IsSleeping());
707
708 // Wake up thread 8
709 tasks[8].WakeUp();
710 Env::Default()->SleepForMicroseconds(kDelayMicros);
711 ASSERT_TRUE(!tasks[8].IsSleeping());
712
713 // Wake up the last thread
714 tasks[5].WakeUp();
715
716 Env::Default()->SleepForMicroseconds(kDelayMicros);
717 ASSERT_TRUE(!tasks[5].IsSleeping());
718 WaitThreadPoolsEmpty();
719 }
720
721 #if (defined OS_LINUX || defined OS_WIN)
722 // Travis doesn't support fallocate or getting unique ID from files for whatever
723 // reason.
724 #ifndef TRAVIS
725
726 namespace {
IsSingleVarint(const std::string & s)727 bool IsSingleVarint(const std::string& s) {
728 Slice slice(s);
729
730 uint64_t v;
731 if (!GetVarint64(&slice, &v)) {
732 return false;
733 }
734
735 return slice.size() == 0;
736 }
737
IsUniqueIDValid(const std::string & s)738 bool IsUniqueIDValid(const std::string& s) {
739 return !s.empty() && !IsSingleVarint(s);
740 }
741
742 const size_t MAX_ID_SIZE = 100;
743 char temp_id[MAX_ID_SIZE];
744
745
746 } // namespace
747
748 // Determine whether we can use the FS_IOC_GETVERSION ioctl
749 // on a file in directory DIR. Create a temporary file therein,
750 // try to apply the ioctl (save that result), cleanup and
751 // return the result. Return true if it is supported, and
752 // false if anything fails.
753 // Note that this function "knows" that dir has just been created
754 // and is empty, so we create a simply-named test file: "f".
ioctl_support__FS_IOC_GETVERSION(const std::string & dir)755 bool ioctl_support__FS_IOC_GETVERSION(const std::string& dir) {
756 #ifdef OS_WIN
757 return true;
758 #else
759 const std::string file = dir + "/f";
760 int fd;
761 do {
762 fd = open(file.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
763 } while (fd < 0 && errno == EINTR);
764 long int version;
765 bool ok = (fd >= 0 && ioctl(fd, FS_IOC_GETVERSION, &version) >= 0);
766
767 close(fd);
768 unlink(file.c_str());
769
770 return ok;
771 #endif
772 }
773
774 // To ensure that Env::GetUniqueId-related tests work correctly, the files
775 // should be stored in regular storage like "hard disk" or "flash device",
776 // and not on a tmpfs file system (like /dev/shm and /tmp on some systems).
777 // Otherwise we cannot get the correct id.
778 //
779 // This function serves as the replacement for test::TmpDir(), which may be
780 // customized to be on a file system that doesn't work with GetUniqueId().
781
782 class IoctlFriendlyTmpdir {
783 public:
IoctlFriendlyTmpdir()784 explicit IoctlFriendlyTmpdir() {
785 char dir_buf[100];
786
787 const char *fmt = "%s/rocksdb.XXXXXX";
788 const char *tmp = getenv("TEST_IOCTL_FRIENDLY_TMPDIR");
789
790 #ifdef OS_WIN
791 #define rmdir _rmdir
792 if(tmp == nullptr) {
793 tmp = getenv("TMP");
794 }
795
796 snprintf(dir_buf, sizeof dir_buf, fmt, tmp);
797 auto result = _mktemp(dir_buf);
798 assert(result != nullptr);
799 BOOL ret = CreateDirectory(dir_buf, NULL);
800 assert(ret == TRUE);
801 dir_ = dir_buf;
802 #else
803 std::list<std::string> candidate_dir_list = {"/var/tmp", "/tmp"};
804
805 // If $TEST_IOCTL_FRIENDLY_TMPDIR/rocksdb.XXXXXX fits, use
806 // $TEST_IOCTL_FRIENDLY_TMPDIR; subtract 2 for the "%s", and
807 // add 1 for the trailing NUL byte.
808 if (tmp && strlen(tmp) + strlen(fmt) - 2 + 1 <= sizeof dir_buf) {
809 // use $TEST_IOCTL_FRIENDLY_TMPDIR value
810 candidate_dir_list.push_front(tmp);
811 }
812
813 for (const std::string& d : candidate_dir_list) {
814 snprintf(dir_buf, sizeof dir_buf, fmt, d.c_str());
815 if (mkdtemp(dir_buf)) {
816 if (ioctl_support__FS_IOC_GETVERSION(dir_buf)) {
817 dir_ = dir_buf;
818 return;
819 } else {
820 // Diagnose ioctl-related failure only if this is the
821 // directory specified via that envvar.
822 if (tmp && tmp == d) {
823 fprintf(stderr, "TEST_IOCTL_FRIENDLY_TMPDIR-specified directory is "
824 "not suitable: %s\n", d.c_str());
825 }
826 rmdir(dir_buf); // ignore failure
827 }
828 } else {
829 // mkdtemp failed: diagnose it, but don't give up.
830 fprintf(stderr, "mkdtemp(%s/...) failed: %s\n", d.c_str(),
831 strerror(errno));
832 }
833 }
834
835 fprintf(stderr, "failed to find an ioctl-friendly temporary directory;"
836 " specify one via the TEST_IOCTL_FRIENDLY_TMPDIR envvar\n");
837 std::abort();
838 #endif
839 }
840
~IoctlFriendlyTmpdir()841 ~IoctlFriendlyTmpdir() {
842 rmdir(dir_.c_str());
843 }
844
name() const845 const std::string& name() const {
846 return dir_;
847 }
848
849 private:
850 std::string dir_;
851 };
852
853 #ifndef ROCKSDB_LITE
TEST_F(EnvPosixTest,PositionedAppend)854 TEST_F(EnvPosixTest, PositionedAppend) {
855 std::unique_ptr<WritableFile> writable_file;
856 EnvOptions options;
857 options.use_direct_writes = true;
858 options.use_mmap_writes = false;
859 IoctlFriendlyTmpdir ift;
860 ASSERT_OK(env_->NewWritableFile(ift.name() + "/f", &writable_file, options));
861 const size_t kBlockSize = 4096;
862 const size_t kDataSize = kPageSize;
863 // Write a page worth of 'a'
864 auto data_ptr = NewAligned(kDataSize, 'a');
865 Slice data_a(data_ptr.get(), kDataSize);
866 ASSERT_OK(writable_file->PositionedAppend(data_a, 0U));
867 // Write a page worth of 'b' right after the first sector
868 data_ptr = NewAligned(kDataSize, 'b');
869 Slice data_b(data_ptr.get(), kDataSize);
870 ASSERT_OK(writable_file->PositionedAppend(data_b, kBlockSize));
871 ASSERT_OK(writable_file->Close());
872 // The file now has 1 sector worth of a followed by a page worth of b
873
874 // Verify the above
875 std::unique_ptr<SequentialFile> seq_file;
876 ASSERT_OK(env_->NewSequentialFile(ift.name() + "/f", &seq_file, options));
877 char scratch[kPageSize * 2];
878 Slice result;
879 ASSERT_OK(seq_file->Read(sizeof(scratch), &result, scratch));
880 ASSERT_EQ(kPageSize + kBlockSize, result.size());
881 ASSERT_EQ('a', result[kBlockSize - 1]);
882 ASSERT_EQ('b', result[kBlockSize]);
883 }
884 #endif // !ROCKSDB_LITE
885
886 // `GetUniqueId()` temporarily returns zero on Windows. `BlockBasedTable` can
887 // handle a return value of zero but this test case cannot.
888 #ifndef OS_WIN
TEST_P(EnvPosixTestWithParam,RandomAccessUniqueID)889 TEST_P(EnvPosixTestWithParam, RandomAccessUniqueID) {
890 // Create file.
891 if (env_ == Env::Default()) {
892 EnvOptions soptions;
893 soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
894 IoctlFriendlyTmpdir ift;
895 std::string fname = ift.name() + "/testfile";
896 std::unique_ptr<WritableFile> wfile;
897 ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
898
899 std::unique_ptr<RandomAccessFile> file;
900
901 // Get Unique ID
902 ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
903 size_t id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
904 ASSERT_TRUE(id_size > 0);
905 std::string unique_id1(temp_id, id_size);
906 ASSERT_TRUE(IsUniqueIDValid(unique_id1));
907
908 // Get Unique ID again
909 ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
910 id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
911 ASSERT_TRUE(id_size > 0);
912 std::string unique_id2(temp_id, id_size);
913 ASSERT_TRUE(IsUniqueIDValid(unique_id2));
914
915 // Get Unique ID again after waiting some time.
916 env_->SleepForMicroseconds(1000000);
917 ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
918 id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
919 ASSERT_TRUE(id_size > 0);
920 std::string unique_id3(temp_id, id_size);
921 ASSERT_TRUE(IsUniqueIDValid(unique_id3));
922
923 // Check IDs are the same.
924 ASSERT_EQ(unique_id1, unique_id2);
925 ASSERT_EQ(unique_id2, unique_id3);
926
927 // Delete the file
928 env_->DeleteFile(fname);
929 }
930 }
931 #endif // !defined(OS_WIN)
932
933 // only works in linux platforms
934 #ifdef ROCKSDB_FALLOCATE_PRESENT
TEST_P(EnvPosixTestWithParam,AllocateTest)935 TEST_P(EnvPosixTestWithParam, AllocateTest) {
936 if (env_ == Env::Default()) {
937 IoctlFriendlyTmpdir ift;
938 std::string fname = ift.name() + "/preallocate_testfile";
939
940 // Try fallocate in a file to see whether the target file system supports
941 // it.
942 // Skip the test if fallocate is not supported.
943 std::string fname_test_fallocate = ift.name() + "/preallocate_testfile_2";
944 int fd = -1;
945 do {
946 fd = open(fname_test_fallocate.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
947 } while (fd < 0 && errno == EINTR);
948 ASSERT_GT(fd, 0);
949
950 int alloc_status = fallocate(fd, 0, 0, 1);
951
952 int err_number = 0;
953 if (alloc_status != 0) {
954 err_number = errno;
955 fprintf(stderr, "Warning: fallocate() fails, %s\n", strerror(err_number));
956 }
957 close(fd);
958 ASSERT_OK(env_->DeleteFile(fname_test_fallocate));
959 if (alloc_status != 0 && err_number == EOPNOTSUPP) {
960 // The filesystem containing the file does not support fallocate
961 return;
962 }
963
964 EnvOptions soptions;
965 soptions.use_mmap_writes = false;
966 soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
967 std::unique_ptr<WritableFile> wfile;
968 ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
969
970 // allocate 100 MB
971 size_t kPreallocateSize = 100 * 1024 * 1024;
972 size_t kBlockSize = 512;
973 size_t kPageSize = 4096;
974 size_t kDataSize = 1024 * 1024;
975 auto data_ptr = NewAligned(kDataSize, 'A');
976 Slice data(data_ptr.get(), kDataSize);
977 wfile->SetPreallocationBlockSize(kPreallocateSize);
978 wfile->PrepareWrite(wfile->GetFileSize(), kDataSize);
979 ASSERT_OK(wfile->Append(data));
980 ASSERT_OK(wfile->Flush());
981
982 struct stat f_stat;
983 ASSERT_EQ(stat(fname.c_str(), &f_stat), 0);
984 ASSERT_EQ((unsigned int)kDataSize, f_stat.st_size);
985 // verify that blocks are preallocated
986 // Note here that we don't check the exact number of blocks preallocated --
987 // we only require that number of allocated blocks is at least what we
988 // expect.
989 // It looks like some FS give us more blocks that we asked for. That's fine.
990 // It might be worth investigating further.
991 ASSERT_LE((unsigned int)(kPreallocateSize / kBlockSize), f_stat.st_blocks);
992
993 // close the file, should deallocate the blocks
994 wfile.reset();
995
996 stat(fname.c_str(), &f_stat);
997 ASSERT_EQ((unsigned int)kDataSize, f_stat.st_size);
998 // verify that preallocated blocks were deallocated on file close
999 // Because the FS might give us more blocks, we add a full page to the size
1000 // and expect the number of blocks to be less or equal to that.
1001 ASSERT_GE((f_stat.st_size + kPageSize + kBlockSize - 1) / kBlockSize,
1002 (unsigned int)f_stat.st_blocks);
1003 }
1004 }
1005 #endif // ROCKSDB_FALLOCATE_PRESENT
1006
1007 // Returns true if any of the strings in ss are the prefix of another string.
HasPrefix(const std::unordered_set<std::string> & ss)1008 bool HasPrefix(const std::unordered_set<std::string>& ss) {
1009 for (const std::string& s: ss) {
1010 if (s.empty()) {
1011 return true;
1012 }
1013 for (size_t i = 1; i < s.size(); ++i) {
1014 if (ss.count(s.substr(0, i)) != 0) {
1015 return true;
1016 }
1017 }
1018 }
1019 return false;
1020 }
1021
1022 // `GetUniqueId()` temporarily returns zero on Windows. `BlockBasedTable` can
1023 // handle a return value of zero but this test case cannot.
1024 #ifndef OS_WIN
TEST_P(EnvPosixTestWithParam,RandomAccessUniqueIDConcurrent)1025 TEST_P(EnvPosixTestWithParam, RandomAccessUniqueIDConcurrent) {
1026 if (env_ == Env::Default()) {
1027 // Check whether a bunch of concurrently existing files have unique IDs.
1028 EnvOptions soptions;
1029 soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
1030
1031 // Create the files
1032 IoctlFriendlyTmpdir ift;
1033 std::vector<std::string> fnames;
1034 for (int i = 0; i < 1000; ++i) {
1035 fnames.push_back(ift.name() + "/" + "testfile" + ToString(i));
1036
1037 // Create file.
1038 std::unique_ptr<WritableFile> wfile;
1039 ASSERT_OK(env_->NewWritableFile(fnames[i], &wfile, soptions));
1040 }
1041
1042 // Collect and check whether the IDs are unique.
1043 std::unordered_set<std::string> ids;
1044 for (const std::string fname : fnames) {
1045 std::unique_ptr<RandomAccessFile> file;
1046 std::string unique_id;
1047 ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
1048 size_t id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
1049 ASSERT_TRUE(id_size > 0);
1050 unique_id = std::string(temp_id, id_size);
1051 ASSERT_TRUE(IsUniqueIDValid(unique_id));
1052
1053 ASSERT_TRUE(ids.count(unique_id) == 0);
1054 ids.insert(unique_id);
1055 }
1056
1057 // Delete the files
1058 for (const std::string fname : fnames) {
1059 ASSERT_OK(env_->DeleteFile(fname));
1060 }
1061
1062 ASSERT_TRUE(!HasPrefix(ids));
1063 }
1064 }
1065
TEST_P(EnvPosixTestWithParam,RandomAccessUniqueIDDeletes)1066 TEST_P(EnvPosixTestWithParam, RandomAccessUniqueIDDeletes) {
1067 if (env_ == Env::Default()) {
1068 EnvOptions soptions;
1069 soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
1070
1071 IoctlFriendlyTmpdir ift;
1072 std::string fname = ift.name() + "/" + "testfile";
1073
1074 // Check that after file is deleted we don't get same ID again in a new
1075 // file.
1076 std::unordered_set<std::string> ids;
1077 for (int i = 0; i < 1000; ++i) {
1078 // Create file.
1079 {
1080 std::unique_ptr<WritableFile> wfile;
1081 ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
1082 }
1083
1084 // Get Unique ID
1085 std::string unique_id;
1086 {
1087 std::unique_ptr<RandomAccessFile> file;
1088 ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
1089 size_t id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
1090 ASSERT_TRUE(id_size > 0);
1091 unique_id = std::string(temp_id, id_size);
1092 }
1093
1094 ASSERT_TRUE(IsUniqueIDValid(unique_id));
1095 ASSERT_TRUE(ids.count(unique_id) == 0);
1096 ids.insert(unique_id);
1097
1098 // Delete the file
1099 ASSERT_OK(env_->DeleteFile(fname));
1100 }
1101
1102 ASSERT_TRUE(!HasPrefix(ids));
1103 }
1104 }
1105 #endif // !defined(OS_WIN)
1106
TEST_P(EnvPosixTestWithParam,MultiRead)1107 TEST_P(EnvPosixTestWithParam, MultiRead) {
1108 EnvOptions soptions;
1109 soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
1110 std::string fname = test::PerThreadDBPath(env_, "testfile");
1111
1112 const size_t kSectorSize = 4096;
1113 const size_t kNumSectors = 8;
1114
1115 // Create file.
1116 {
1117 std::unique_ptr<WritableFile> wfile;
1118 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
1119 !defined(OS_AIX)
1120 if (soptions.use_direct_writes) {
1121 soptions.use_direct_writes = false;
1122 }
1123 #endif
1124 ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
1125 for (size_t i = 0; i < kNumSectors; ++i) {
1126 auto data = NewAligned(kSectorSize * 8, static_cast<char>(i + 1));
1127 Slice slice(data.get(), kSectorSize);
1128 ASSERT_OK(wfile->Append(slice));
1129 }
1130 ASSERT_OK(wfile->Close());
1131 }
1132
1133 // More attempts to simulate more partial result sequences.
1134 for (uint32_t attempt = 0; attempt < 20; attempt++) {
1135 // Random Read
1136 Random rnd(301 + attempt);
1137 rocksdb::SyncPoint::GetInstance()->SetCallBack(
1138 "PosixRandomAccessFile::MultiRead:io_uring_result", [&](void* arg) {
1139 if (attempt > 0) {
1140 // No failure in the first attempt.
1141 size_t& bytes_read = *static_cast<size_t*>(arg);
1142 if (rnd.OneIn(4)) {
1143 bytes_read = 0;
1144 } else if (rnd.OneIn(3)) {
1145 bytes_read = static_cast<size_t>(
1146 rnd.Uniform(static_cast<int>(bytes_read)));
1147 }
1148 }
1149 });
1150 rocksdb::SyncPoint::GetInstance()->EnableProcessing();
1151
1152 std::unique_ptr<RandomAccessFile> file;
1153 std::vector<ReadRequest> reqs(3);
1154 std::vector<std::unique_ptr<char, Deleter>> data;
1155 uint64_t offset = 0;
1156 for (size_t i = 0; i < reqs.size(); ++i) {
1157 reqs[i].offset = offset;
1158 offset += 2 * kSectorSize;
1159 reqs[i].len = kSectorSize;
1160 data.emplace_back(NewAligned(kSectorSize, 0));
1161 reqs[i].scratch = data.back().get();
1162 }
1163 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
1164 !defined(OS_AIX)
1165 if (soptions.use_direct_reads) {
1166 soptions.use_direct_reads = false;
1167 }
1168 #endif
1169 ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
1170 ASSERT_OK(file->MultiRead(reqs.data(), reqs.size()));
1171 for (size_t i = 0; i < reqs.size(); ++i) {
1172 auto buf = NewAligned(kSectorSize * 8, static_cast<char>(i * 2 + 1));
1173 ASSERT_OK(reqs[i].status);
1174 ASSERT_EQ(memcmp(reqs[i].scratch, buf.get(), kSectorSize), 0);
1175 }
1176 rocksdb::SyncPoint::GetInstance()->DisableProcessing();
1177 }
1178 }
1179
1180 // Only works in linux platforms
1181 #ifdef OS_WIN
TEST_P(EnvPosixTestWithParam,DISABLED_InvalidateCache)1182 TEST_P(EnvPosixTestWithParam, DISABLED_InvalidateCache) {
1183 #else
1184 TEST_P(EnvPosixTestWithParam, InvalidateCache) {
1185 #endif
1186 rocksdb::SyncPoint::GetInstance()->EnableProcessing();
1187 EnvOptions soptions;
1188 soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
1189 std::string fname = test::PerThreadDBPath(env_, "testfile");
1190
1191 const size_t kSectorSize = 512;
1192 auto data = NewAligned(kSectorSize, 0);
1193 Slice slice(data.get(), kSectorSize);
1194
1195 // Create file.
1196 {
1197 std::unique_ptr<WritableFile> wfile;
1198 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && !defined(OS_AIX)
1199 if (soptions.use_direct_writes) {
1200 soptions.use_direct_writes = false;
1201 }
1202 #endif
1203 ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
1204 ASSERT_OK(wfile->Append(slice));
1205 ASSERT_OK(wfile->InvalidateCache(0, 0));
1206 ASSERT_OK(wfile->Close());
1207 }
1208
1209 // Random Read
1210 {
1211 std::unique_ptr<RandomAccessFile> file;
1212 auto scratch = NewAligned(kSectorSize, 0);
1213 Slice result;
1214 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && !defined(OS_AIX)
1215 if (soptions.use_direct_reads) {
1216 soptions.use_direct_reads = false;
1217 }
1218 #endif
1219 ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
1220 ASSERT_OK(file->Read(0, kSectorSize, &result, scratch.get()));
1221 ASSERT_EQ(memcmp(scratch.get(), data.get(), kSectorSize), 0);
1222 ASSERT_OK(file->InvalidateCache(0, 11));
1223 ASSERT_OK(file->InvalidateCache(0, 0));
1224 }
1225
1226 // Sequential Read
1227 {
1228 std::unique_ptr<SequentialFile> file;
1229 auto scratch = NewAligned(kSectorSize, 0);
1230 Slice result;
1231 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && !defined(OS_AIX)
1232 if (soptions.use_direct_reads) {
1233 soptions.use_direct_reads = false;
1234 }
1235 #endif
1236 ASSERT_OK(env_->NewSequentialFile(fname, &file, soptions));
1237 if (file->use_direct_io()) {
1238 ASSERT_OK(file->PositionedRead(0, kSectorSize, &result, scratch.get()));
1239 } else {
1240 ASSERT_OK(file->Read(kSectorSize, &result, scratch.get()));
1241 }
1242 ASSERT_EQ(memcmp(scratch.get(), data.get(), kSectorSize), 0);
1243 ASSERT_OK(file->InvalidateCache(0, 11));
1244 ASSERT_OK(file->InvalidateCache(0, 0));
1245 }
1246 // Delete the file
1247 ASSERT_OK(env_->DeleteFile(fname));
1248 rocksdb::SyncPoint::GetInstance()->ClearTrace();
1249 }
1250 #endif // not TRAVIS
1251 #endif // OS_LINUX || OS_WIN
1252
1253 class TestLogger : public Logger {
1254 public:
1255 using Logger::Logv;
1256 void Logv(const char* format, va_list ap) override {
1257 log_count++;
1258
1259 char new_format[550];
1260 std::fill_n(new_format, sizeof(new_format), '2');
1261 {
1262 va_list backup_ap;
1263 va_copy(backup_ap, ap);
1264 int n = vsnprintf(new_format, sizeof(new_format) - 1, format, backup_ap);
1265 // 48 bytes for extra information + bytes allocated
1266
1267 // When we have n == -1 there is not a terminating zero expected
1268 #ifdef OS_WIN
1269 if (n < 0) {
1270 char_0_count++;
1271 }
1272 #endif
1273
1274 if (new_format[0] == '[') {
1275 // "[DEBUG] "
1276 ASSERT_TRUE(n <= 56 + (512 - static_cast<int>(sizeof(struct timeval))));
1277 } else {
1278 ASSERT_TRUE(n <= 48 + (512 - static_cast<int>(sizeof(struct timeval))));
1279 }
1280 va_end(backup_ap);
1281 }
1282
1283 for (size_t i = 0; i < sizeof(new_format); i++) {
1284 if (new_format[i] == 'x') {
1285 char_x_count++;
1286 } else if (new_format[i] == '\0') {
1287 char_0_count++;
1288 }
1289 }
1290 }
1291 int log_count;
1292 int char_x_count;
1293 int char_0_count;
1294 };
1295
1296 TEST_P(EnvPosixTestWithParam, LogBufferTest) {
1297 TestLogger test_logger;
1298 test_logger.SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
1299 test_logger.log_count = 0;
1300 test_logger.char_x_count = 0;
1301 test_logger.char_0_count = 0;
1302 LogBuffer log_buffer(InfoLogLevel::INFO_LEVEL, &test_logger);
1303 LogBuffer log_buffer_debug(DEBUG_LEVEL, &test_logger);
1304
1305 char bytes200[200];
1306 std::fill_n(bytes200, sizeof(bytes200), '1');
1307 bytes200[sizeof(bytes200) - 1] = '\0';
1308 char bytes600[600];
1309 std::fill_n(bytes600, sizeof(bytes600), '1');
1310 bytes600[sizeof(bytes600) - 1] = '\0';
1311 char bytes9000[9000];
1312 std::fill_n(bytes9000, sizeof(bytes9000), '1');
1313 bytes9000[sizeof(bytes9000) - 1] = '\0';
1314
1315 ROCKS_LOG_BUFFER(&log_buffer, "x%sx", bytes200);
1316 ROCKS_LOG_BUFFER(&log_buffer, "x%sx", bytes600);
1317 ROCKS_LOG_BUFFER(&log_buffer, "x%sx%sx%sx", bytes200, bytes200, bytes200);
1318 ROCKS_LOG_BUFFER(&log_buffer, "x%sx%sx", bytes200, bytes600);
1319 ROCKS_LOG_BUFFER(&log_buffer, "x%sx%sx", bytes600, bytes9000);
1320
1321 ROCKS_LOG_BUFFER(&log_buffer_debug, "x%sx", bytes200);
1322 test_logger.SetInfoLogLevel(DEBUG_LEVEL);
1323 ROCKS_LOG_BUFFER(&log_buffer_debug, "x%sx%sx%sx", bytes600, bytes9000,
1324 bytes200);
1325
1326 ASSERT_EQ(0, test_logger.log_count);
1327 log_buffer.FlushBufferToLog();
1328 log_buffer_debug.FlushBufferToLog();
1329 ASSERT_EQ(6, test_logger.log_count);
1330 ASSERT_EQ(6, test_logger.char_0_count);
1331 ASSERT_EQ(10, test_logger.char_x_count);
1332 }
1333
1334 class TestLogger2 : public Logger {
1335 public:
1336 explicit TestLogger2(size_t max_log_size) : max_log_size_(max_log_size) {}
1337 using Logger::Logv;
1338 void Logv(const char* format, va_list ap) override {
1339 char new_format[2000];
1340 std::fill_n(new_format, sizeof(new_format), '2');
1341 {
1342 va_list backup_ap;
1343 va_copy(backup_ap, ap);
1344 int n = vsnprintf(new_format, sizeof(new_format) - 1, format, backup_ap);
1345 // 48 bytes for extra information + bytes allocated
1346 ASSERT_TRUE(
1347 n <= 48 + static_cast<int>(max_log_size_ - sizeof(struct timeval)));
1348 ASSERT_TRUE(n > static_cast<int>(max_log_size_ - sizeof(struct timeval)));
1349 va_end(backup_ap);
1350 }
1351 }
1352 size_t max_log_size_;
1353 };
1354
1355 TEST_P(EnvPosixTestWithParam, LogBufferMaxSizeTest) {
1356 char bytes9000[9000];
1357 std::fill_n(bytes9000, sizeof(bytes9000), '1');
1358 bytes9000[sizeof(bytes9000) - 1] = '\0';
1359
1360 for (size_t max_log_size = 256; max_log_size <= 1024;
1361 max_log_size += 1024 - 256) {
1362 TestLogger2 test_logger(max_log_size);
1363 test_logger.SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
1364 LogBuffer log_buffer(InfoLogLevel::INFO_LEVEL, &test_logger);
1365 ROCKS_LOG_BUFFER_MAX_SZ(&log_buffer, max_log_size, "%s", bytes9000);
1366 log_buffer.FlushBufferToLog();
1367 }
1368 }
1369
1370 TEST_P(EnvPosixTestWithParam, Preallocation) {
1371 rocksdb::SyncPoint::GetInstance()->EnableProcessing();
1372 const std::string src = test::PerThreadDBPath(env_, "testfile");
1373 std::unique_ptr<WritableFile> srcfile;
1374 EnvOptions soptions;
1375 soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
1376 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && !defined(OS_AIX) && !defined(OS_OPENBSD) && !defined(OS_FREEBSD)
1377 if (soptions.use_direct_writes) {
1378 rocksdb::SyncPoint::GetInstance()->SetCallBack(
1379 "NewWritableFile:O_DIRECT", [&](void* arg) {
1380 int* val = static_cast<int*>(arg);
1381 *val &= ~O_DIRECT;
1382 });
1383 }
1384 #endif
1385 ASSERT_OK(env_->NewWritableFile(src, &srcfile, soptions));
1386 srcfile->SetPreallocationBlockSize(1024 * 1024);
1387
1388 // No writes should mean no preallocation
1389 size_t block_size, last_allocated_block;
1390 srcfile->GetPreallocationStatus(&block_size, &last_allocated_block);
1391 ASSERT_EQ(last_allocated_block, 0UL);
1392
1393 // Small write should preallocate one block
1394 size_t kStrSize = 4096;
1395 auto data = NewAligned(kStrSize, 'A');
1396 Slice str(data.get(), kStrSize);
1397 srcfile->PrepareWrite(srcfile->GetFileSize(), kStrSize);
1398 srcfile->Append(str);
1399 srcfile->GetPreallocationStatus(&block_size, &last_allocated_block);
1400 ASSERT_EQ(last_allocated_block, 1UL);
1401
1402 // Write an entire preallocation block, make sure we increased by two.
1403 {
1404 auto buf_ptr = NewAligned(block_size, ' ');
1405 Slice buf(buf_ptr.get(), block_size);
1406 srcfile->PrepareWrite(srcfile->GetFileSize(), block_size);
1407 srcfile->Append(buf);
1408 srcfile->GetPreallocationStatus(&block_size, &last_allocated_block);
1409 ASSERT_EQ(last_allocated_block, 2UL);
1410 }
1411
1412 // Write five more blocks at once, ensure we're where we need to be.
1413 {
1414 auto buf_ptr = NewAligned(block_size * 5, ' ');
1415 Slice buf = Slice(buf_ptr.get(), block_size * 5);
1416 srcfile->PrepareWrite(srcfile->GetFileSize(), buf.size());
1417 srcfile->Append(buf);
1418 srcfile->GetPreallocationStatus(&block_size, &last_allocated_block);
1419 ASSERT_EQ(last_allocated_block, 7UL);
1420 }
1421 rocksdb::SyncPoint::GetInstance()->ClearTrace();
1422 }
1423
1424 // Test that the two ways to get children file attributes (in bulk or
1425 // individually) behave consistently.
1426 TEST_P(EnvPosixTestWithParam, ConsistentChildrenAttributes) {
1427 rocksdb::SyncPoint::GetInstance()->EnableProcessing();
1428 EnvOptions soptions;
1429 soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
1430 const int kNumChildren = 10;
1431
1432 std::string data;
1433 for (int i = 0; i < kNumChildren; ++i) {
1434 const std::string path =
1435 test::TmpDir(env_) + "/" + "testfile_" + std::to_string(i);
1436 std::unique_ptr<WritableFile> file;
1437 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && !defined(OS_AIX) && !defined(OS_OPENBSD) && !defined(OS_FREEBSD)
1438 if (soptions.use_direct_writes) {
1439 rocksdb::SyncPoint::GetInstance()->SetCallBack(
1440 "NewWritableFile:O_DIRECT", [&](void* arg) {
1441 int* val = static_cast<int*>(arg);
1442 *val &= ~O_DIRECT;
1443 });
1444 }
1445 #endif
1446 ASSERT_OK(env_->NewWritableFile(path, &file, soptions));
1447 auto buf_ptr = NewAligned(data.size(), 'T');
1448 Slice buf(buf_ptr.get(), data.size());
1449 file->Append(buf);
1450 data.append(std::string(4096, 'T'));
1451 }
1452
1453 std::vector<Env::FileAttributes> file_attrs;
1454 ASSERT_OK(env_->GetChildrenFileAttributes(test::TmpDir(env_), &file_attrs));
1455 for (int i = 0; i < kNumChildren; ++i) {
1456 const std::string name = "testfile_" + std::to_string(i);
1457 const std::string path = test::TmpDir(env_) + "/" + name;
1458
1459 auto file_attrs_iter = std::find_if(
1460 file_attrs.begin(), file_attrs.end(),
1461 [&name](const Env::FileAttributes& fm) { return fm.name == name; });
1462 ASSERT_TRUE(file_attrs_iter != file_attrs.end());
1463 uint64_t size;
1464 ASSERT_OK(env_->GetFileSize(path, &size));
1465 ASSERT_EQ(size, 4096 * i);
1466 ASSERT_EQ(size, file_attrs_iter->size_bytes);
1467 }
1468 rocksdb::SyncPoint::GetInstance()->ClearTrace();
1469 }
1470
1471 // Test that all WritableFileWrapper forwards all calls to WritableFile.
1472 TEST_P(EnvPosixTestWithParam, WritableFileWrapper) {
1473 class Base : public WritableFile {
1474 public:
1475 mutable int *step_;
1476
1477 void inc(int x) const {
1478 EXPECT_EQ(x, (*step_)++);
1479 }
1480
1481 explicit Base(int* step) : step_(step) {
1482 inc(0);
1483 }
1484
1485 Status Append(const Slice& /*data*/) override {
1486 inc(1);
1487 return Status::OK();
1488 }
1489
1490 Status PositionedAppend(const Slice& /*data*/,
1491 uint64_t /*offset*/) override {
1492 inc(2);
1493 return Status::OK();
1494 }
1495
1496 Status Truncate(uint64_t /*size*/) override {
1497 inc(3);
1498 return Status::OK();
1499 }
1500
1501 Status Close() override {
1502 inc(4);
1503 return Status::OK();
1504 }
1505
1506 Status Flush() override {
1507 inc(5);
1508 return Status::OK();
1509 }
1510
1511 Status Sync() override {
1512 inc(6);
1513 return Status::OK();
1514 }
1515
1516 Status Fsync() override {
1517 inc(7);
1518 return Status::OK();
1519 }
1520
1521 bool IsSyncThreadSafe() const override {
1522 inc(8);
1523 return true;
1524 }
1525
1526 bool use_direct_io() const override {
1527 inc(9);
1528 return true;
1529 }
1530
1531 size_t GetRequiredBufferAlignment() const override {
1532 inc(10);
1533 return 0;
1534 }
1535
1536 void SetIOPriority(Env::IOPriority /*pri*/) override { inc(11); }
1537
1538 Env::IOPriority GetIOPriority() override {
1539 inc(12);
1540 return Env::IOPriority::IO_LOW;
1541 }
1542
1543 void SetWriteLifeTimeHint(Env::WriteLifeTimeHint /*hint*/) override {
1544 inc(13);
1545 }
1546
1547 Env::WriteLifeTimeHint GetWriteLifeTimeHint() override {
1548 inc(14);
1549 return Env::WriteLifeTimeHint::WLTH_NOT_SET;
1550 }
1551
1552 uint64_t GetFileSize() override {
1553 inc(15);
1554 return 0;
1555 }
1556
1557 void SetPreallocationBlockSize(size_t /*size*/) override { inc(16); }
1558
1559 void GetPreallocationStatus(size_t* /*block_size*/,
1560 size_t* /*last_allocated_block*/) override {
1561 inc(17);
1562 }
1563
1564 size_t GetUniqueId(char* /*id*/, size_t /*max_size*/) const override {
1565 inc(18);
1566 return 0;
1567 }
1568
1569 Status InvalidateCache(size_t /*offset*/, size_t /*length*/) override {
1570 inc(19);
1571 return Status::OK();
1572 }
1573
1574 Status RangeSync(uint64_t /*offset*/, uint64_t /*nbytes*/) override {
1575 inc(20);
1576 return Status::OK();
1577 }
1578
1579 void PrepareWrite(size_t /*offset*/, size_t /*len*/) override { inc(21); }
1580
1581 Status Allocate(uint64_t /*offset*/, uint64_t /*len*/) override {
1582 inc(22);
1583 return Status::OK();
1584 }
1585
1586 public:
1587 ~Base() override { inc(23); }
1588 };
1589
1590 class Wrapper : public WritableFileWrapper {
1591 public:
1592 explicit Wrapper(WritableFile* target) : WritableFileWrapper(target) {}
1593 };
1594
1595 int step = 0;
1596
1597 {
1598 Base b(&step);
1599 Wrapper w(&b);
1600 w.Append(Slice());
1601 w.PositionedAppend(Slice(), 0);
1602 w.Truncate(0);
1603 w.Close();
1604 w.Flush();
1605 w.Sync();
1606 w.Fsync();
1607 w.IsSyncThreadSafe();
1608 w.use_direct_io();
1609 w.GetRequiredBufferAlignment();
1610 w.SetIOPriority(Env::IOPriority::IO_HIGH);
1611 w.GetIOPriority();
1612 w.SetWriteLifeTimeHint(Env::WriteLifeTimeHint::WLTH_NOT_SET);
1613 w.GetWriteLifeTimeHint();
1614 w.GetFileSize();
1615 w.SetPreallocationBlockSize(0);
1616 w.GetPreallocationStatus(nullptr, nullptr);
1617 w.GetUniqueId(nullptr, 0);
1618 w.InvalidateCache(0, 0);
1619 w.RangeSync(0, 0);
1620 w.PrepareWrite(0, 0);
1621 w.Allocate(0, 0);
1622 }
1623
1624 EXPECT_EQ(24, step);
1625 }
1626
1627 TEST_P(EnvPosixTestWithParam, PosixRandomRWFile) {
1628 const std::string path = test::PerThreadDBPath(env_, "random_rw_file");
1629
1630 env_->DeleteFile(path);
1631
1632 std::unique_ptr<RandomRWFile> file;
1633
1634 // Cannot open non-existing file.
1635 ASSERT_NOK(env_->NewRandomRWFile(path, &file, EnvOptions()));
1636
1637 // Create the file using WriteableFile
1638 {
1639 std::unique_ptr<WritableFile> wf;
1640 ASSERT_OK(env_->NewWritableFile(path, &wf, EnvOptions()));
1641 }
1642
1643 ASSERT_OK(env_->NewRandomRWFile(path, &file, EnvOptions()));
1644
1645 char buf[10000];
1646 Slice read_res;
1647
1648 ASSERT_OK(file->Write(0, "ABCD"));
1649 ASSERT_OK(file->Read(0, 10, &read_res, buf));
1650 ASSERT_EQ(read_res.ToString(), "ABCD");
1651
1652 ASSERT_OK(file->Write(2, "XXXX"));
1653 ASSERT_OK(file->Read(0, 10, &read_res, buf));
1654 ASSERT_EQ(read_res.ToString(), "ABXXXX");
1655
1656 ASSERT_OK(file->Write(10, "ZZZ"));
1657 ASSERT_OK(file->Read(10, 10, &read_res, buf));
1658 ASSERT_EQ(read_res.ToString(), "ZZZ");
1659
1660 ASSERT_OK(file->Write(11, "Y"));
1661 ASSERT_OK(file->Read(10, 10, &read_res, buf));
1662 ASSERT_EQ(read_res.ToString(), "ZYZ");
1663
1664 ASSERT_OK(file->Write(200, "FFFFF"));
1665 ASSERT_OK(file->Read(200, 10, &read_res, buf));
1666 ASSERT_EQ(read_res.ToString(), "FFFFF");
1667
1668 ASSERT_OK(file->Write(205, "XXXX"));
1669 ASSERT_OK(file->Read(200, 10, &read_res, buf));
1670 ASSERT_EQ(read_res.ToString(), "FFFFFXXXX");
1671
1672 ASSERT_OK(file->Write(5, "QQQQ"));
1673 ASSERT_OK(file->Read(0, 9, &read_res, buf));
1674 ASSERT_EQ(read_res.ToString(), "ABXXXQQQQ");
1675
1676 ASSERT_OK(file->Read(2, 4, &read_res, buf));
1677 ASSERT_EQ(read_res.ToString(), "XXXQ");
1678
1679 // Close file and reopen it
1680 file->Close();
1681 ASSERT_OK(env_->NewRandomRWFile(path, &file, EnvOptions()));
1682
1683 ASSERT_OK(file->Read(0, 9, &read_res, buf));
1684 ASSERT_EQ(read_res.ToString(), "ABXXXQQQQ");
1685
1686 ASSERT_OK(file->Read(10, 3, &read_res, buf));
1687 ASSERT_EQ(read_res.ToString(), "ZYZ");
1688
1689 ASSERT_OK(file->Read(200, 9, &read_res, buf));
1690 ASSERT_EQ(read_res.ToString(), "FFFFFXXXX");
1691
1692 ASSERT_OK(file->Write(4, "TTTTTTTTTTTTTTTT"));
1693 ASSERT_OK(file->Read(0, 10, &read_res, buf));
1694 ASSERT_EQ(read_res.ToString(), "ABXXTTTTTT");
1695
1696 // Clean up
1697 env_->DeleteFile(path);
1698 }
1699
1700 class RandomRWFileWithMirrorString {
1701 public:
1702 explicit RandomRWFileWithMirrorString(RandomRWFile* _file) : file_(_file) {}
1703
1704 void Write(size_t offset, const std::string& data) {
1705 // Write to mirror string
1706 StringWrite(offset, data);
1707
1708 // Write to file
1709 Status s = file_->Write(offset, data);
1710 ASSERT_OK(s) << s.ToString();
1711 }
1712
1713 void Read(size_t offset = 0, size_t n = 1000000) {
1714 Slice str_res(nullptr, 0);
1715 if (offset < file_mirror_.size()) {
1716 size_t str_res_sz = std::min(file_mirror_.size() - offset, n);
1717 str_res = Slice(file_mirror_.data() + offset, str_res_sz);
1718 StopSliceAtNull(&str_res);
1719 }
1720
1721 Slice file_res;
1722 Status s = file_->Read(offset, n, &file_res, buf_);
1723 ASSERT_OK(s) << s.ToString();
1724 StopSliceAtNull(&file_res);
1725
1726 ASSERT_EQ(str_res.ToString(), file_res.ToString()) << offset << " " << n;
1727 }
1728
1729 void SetFile(RandomRWFile* _file) { file_ = _file; }
1730
1731 private:
1732 void StringWrite(size_t offset, const std::string& src) {
1733 if (offset + src.size() > file_mirror_.size()) {
1734 file_mirror_.resize(offset + src.size(), '\0');
1735 }
1736
1737 char* pos = const_cast<char*>(file_mirror_.data() + offset);
1738 memcpy(pos, src.data(), src.size());
1739 }
1740
1741 void StopSliceAtNull(Slice* slc) {
1742 for (size_t i = 0; i < slc->size(); i++) {
1743 if ((*slc)[i] == '\0') {
1744 *slc = Slice(slc->data(), i);
1745 break;
1746 }
1747 }
1748 }
1749
1750 char buf_[10000];
1751 RandomRWFile* file_;
1752 std::string file_mirror_;
1753 };
1754
1755 TEST_P(EnvPosixTestWithParam, PosixRandomRWFileRandomized) {
1756 const std::string path = test::PerThreadDBPath(env_, "random_rw_file_rand");
1757 env_->DeleteFile(path);
1758
1759 std::unique_ptr<RandomRWFile> file;
1760
1761 #ifdef OS_LINUX
1762 // Cannot open non-existing file.
1763 ASSERT_NOK(env_->NewRandomRWFile(path, &file, EnvOptions()));
1764 #endif
1765
1766 // Create the file using WriteableFile
1767 {
1768 std::unique_ptr<WritableFile> wf;
1769 ASSERT_OK(env_->NewWritableFile(path, &wf, EnvOptions()));
1770 }
1771
1772 ASSERT_OK(env_->NewRandomRWFile(path, &file, EnvOptions()));
1773 RandomRWFileWithMirrorString file_with_mirror(file.get());
1774
1775 Random rnd(301);
1776 std::string buf;
1777 for (int i = 0; i < 10000; i++) {
1778 // Genrate random data
1779 test::RandomString(&rnd, 10, &buf);
1780
1781 // Pick random offset for write
1782 size_t write_off = rnd.Next() % 1000;
1783 file_with_mirror.Write(write_off, buf);
1784
1785 // Pick random offset for read
1786 size_t read_off = rnd.Next() % 1000;
1787 size_t read_sz = rnd.Next() % 20;
1788 file_with_mirror.Read(read_off, read_sz);
1789
1790 if (i % 500 == 0) {
1791 // Reopen the file every 500 iters
1792 ASSERT_OK(env_->NewRandomRWFile(path, &file, EnvOptions()));
1793 file_with_mirror.SetFile(file.get());
1794 }
1795 }
1796
1797 // clean up
1798 env_->DeleteFile(path);
1799 }
1800
1801 class TestEnv : public EnvWrapper {
1802 public:
1803 explicit TestEnv() : EnvWrapper(Env::Default()),
1804 close_count(0) { }
1805
1806 class TestLogger : public Logger {
1807 public:
1808 using Logger::Logv;
1809 TestLogger(TestEnv* env_ptr) : Logger() { env = env_ptr; }
1810 ~TestLogger() override {
1811 if (!closed_) {
1812 CloseHelper();
1813 }
1814 }
1815 void Logv(const char* /*format*/, va_list /*ap*/) override{};
1816
1817 protected:
1818 Status CloseImpl() override { return CloseHelper(); }
1819
1820 private:
1821 Status CloseHelper() {
1822 env->CloseCountInc();;
1823 return Status::OK();
1824 }
1825 TestEnv* env;
1826 };
1827
1828 void CloseCountInc() { close_count++; }
1829
1830 int GetCloseCount() { return close_count; }
1831
1832 Status NewLogger(const std::string& /*fname*/,
1833 std::shared_ptr<Logger>* result) override {
1834 result->reset(new TestLogger(this));
1835 return Status::OK();
1836 }
1837
1838 private:
1839 int close_count;
1840 };
1841
1842 class EnvTest : public testing::Test {};
1843
1844 TEST_F(EnvTest, Close) {
1845 TestEnv* env = new TestEnv();
1846 std::shared_ptr<Logger> logger;
1847 Status s;
1848
1849 s = env->NewLogger("", &logger);
1850 ASSERT_EQ(s, Status::OK());
1851 logger.get()->Close();
1852 ASSERT_EQ(env->GetCloseCount(), 1);
1853 // Call Close() again. CloseHelper() should not be called again
1854 logger.get()->Close();
1855 ASSERT_EQ(env->GetCloseCount(), 1);
1856 logger.reset();
1857 ASSERT_EQ(env->GetCloseCount(), 1);
1858
1859 s = env->NewLogger("", &logger);
1860 ASSERT_EQ(s, Status::OK());
1861 logger.reset();
1862 ASSERT_EQ(env->GetCloseCount(), 2);
1863
1864 delete env;
1865 }
1866
1867 INSTANTIATE_TEST_CASE_P(DefaultEnvWithoutDirectIO, EnvPosixTestWithParam,
1868 ::testing::Values(std::pair<Env*, bool>(Env::Default(),
1869 false)));
1870 #if !defined(ROCKSDB_LITE)
1871 INSTANTIATE_TEST_CASE_P(DefaultEnvWithDirectIO, EnvPosixTestWithParam,
1872 ::testing::Values(std::pair<Env*, bool>(Env::Default(),
1873 true)));
1874 #endif // !defined(ROCKSDB_LITE)
1875
1876 #if !defined(ROCKSDB_LITE) && !defined(OS_WIN)
1877 static std::unique_ptr<Env> chroot_env(
1878 NewChrootEnv(Env::Default(), test::TmpDir(Env::Default())));
1879 INSTANTIATE_TEST_CASE_P(
1880 ChrootEnvWithoutDirectIO, EnvPosixTestWithParam,
1881 ::testing::Values(std::pair<Env*, bool>(chroot_env.get(), false)));
1882 INSTANTIATE_TEST_CASE_P(
1883 ChrootEnvWithDirectIO, EnvPosixTestWithParam,
1884 ::testing::Values(std::pair<Env*, bool>(chroot_env.get(), true)));
1885 #endif // !defined(ROCKSDB_LITE) && !defined(OS_WIN)
1886
1887 } // namespace rocksdb
1888
main(int argc,char ** argv)1889 int main(int argc, char** argv) {
1890 ::testing::InitGoogleTest(&argc, argv);
1891 return RUN_ALL_TESTS();
1892 }
1893