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