Coverage Report

Created: 2024-07-27 06:53

/src/rocksdb/env/mock_env.cc
Line
Count
Source (jump to first uncovered line)
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
#include "env/mock_env.h"
11
12
#include <algorithm>
13
#include <chrono>
14
15
#include "env/emulated_clock.h"
16
#include "file/filename.h"
17
#include "port/sys_time.h"
18
#include "rocksdb/file_system.h"
19
#include "rocksdb/utilities/options_type.h"
20
#include "test_util/sync_point.h"
21
#include "util/cast_util.h"
22
#include "util/hash.h"
23
#include "util/random.h"
24
#include "util/rate_limiter_impl.h"
25
#include "util/string_util.h"
26
27
namespace ROCKSDB_NAMESPACE {
28
namespace {
29
0
int64_t MaybeCurrentTime(const std::shared_ptr<SystemClock>& clock) {
30
0
  int64_t time = 1337346000;  // arbitrary fallback default
31
0
  clock->GetCurrentTime(&time).PermitUncheckedError();
32
0
  return time;
33
0
}
34
35
static std::unordered_map<std::string, OptionTypeInfo> time_elapse_type_info = {
36
    {"time_elapse_only_sleep",
37
     {0, OptionType::kBoolean, OptionVerificationType::kNormal,
38
      OptionTypeFlags::kCompareNever,
39
      [](const ConfigOptions& /*opts*/, const std::string& /*name*/,
40
0
         const std::string& value, void* addr) {
41
0
        auto clock = static_cast<EmulatedSystemClock*>(addr);
42
0
        clock->SetTimeElapseOnlySleep(ParseBoolean("", value));
43
0
        return Status::OK();
44
0
      },
45
      [](const ConfigOptions& /*opts*/, const std::string& /*name*/,
46
0
         const void* addr, std::string* value) {
47
0
        const auto clock = static_cast<const EmulatedSystemClock*>(addr);
48
0
        *value = clock->IsTimeElapseOnlySleep() ? "true" : "false";
49
0
        return Status::OK();
50
0
      },
51
      nullptr}},
52
};
53
static std::unordered_map<std::string, OptionTypeInfo> mock_sleep_type_info = {
54
    {"mock_sleep",
55
     {0, OptionType::kBoolean, OptionVerificationType::kNormal,
56
      OptionTypeFlags::kCompareNever,
57
      [](const ConfigOptions& /*opts*/, const std::string& /*name*/,
58
0
         const std::string& value, void* addr) {
59
0
        auto clock = static_cast<EmulatedSystemClock*>(addr);
60
0
        clock->SetMockSleep(ParseBoolean("", value));
61
0
        return Status::OK();
62
0
      },
63
      [](const ConfigOptions& /*opts*/, const std::string& /*name*/,
64
0
         const void* addr, std::string* value) {
65
0
        const auto clock = static_cast<const EmulatedSystemClock*>(addr);
66
0
        *value = clock->IsMockSleepEnabled() ? "true" : "false";
67
0
        return Status::OK();
68
0
      },
69
      nullptr}},
70
};
71
}  // namespace
72
73
EmulatedSystemClock::EmulatedSystemClock(
74
    const std::shared_ptr<SystemClock>& base, bool time_elapse_only_sleep)
75
    : SystemClockWrapper(base),
76
      maybe_starting_time_(MaybeCurrentTime(base)),
77
      time_elapse_only_sleep_(time_elapse_only_sleep),
78
0
      no_slowdown_(time_elapse_only_sleep) {
79
0
  RegisterOptions("", this, &time_elapse_type_info);
80
0
  RegisterOptions("", this, &mock_sleep_type_info);
81
0
}
82
83
class MemFile {
84
 public:
85
  explicit MemFile(SystemClock* clock, const std::string& fn,
86
                   bool _is_lock_file = false)
87
      : clock_(clock),
88
        fn_(fn),
89
        refs_(0),
90
        is_lock_file_(_is_lock_file),
91
        locked_(false),
92
        size_(0),
93
        modified_time_(Now()),
94
        rnd_(Lower32of64(GetSliceNPHash64(fn))),
95
0
        fsynced_bytes_(0) {}
96
  // No copying allowed.
97
  MemFile(const MemFile&) = delete;
98
  void operator=(const MemFile&) = delete;
99
100
0
  void Ref() {
101
0
    MutexLock lock(&mutex_);
102
0
    ++refs_;
103
0
  }
104
105
0
  bool is_lock_file() const { return is_lock_file_; }
106
107
0
  bool Lock() {
108
0
    assert(is_lock_file_);
109
0
    MutexLock lock(&mutex_);
110
0
    if (locked_) {
111
0
      return false;
112
0
    } else {
113
0
      locked_ = true;
114
0
      return true;
115
0
    }
116
0
  }
117
118
0
  void Unlock() {
119
0
    assert(is_lock_file_);
120
0
    MutexLock lock(&mutex_);
121
0
    locked_ = false;
122
0
  }
123
124
0
  void Unref() {
125
0
    bool do_delete = false;
126
0
    {
127
0
      MutexLock lock(&mutex_);
128
0
      --refs_;
129
0
      assert(refs_ >= 0);
130
0
      if (refs_ <= 0) {
131
0
        do_delete = true;
132
0
      }
133
0
    }
134
135
0
    if (do_delete) {
136
0
      delete this;
137
0
    }
138
0
  }
139
140
0
  uint64_t Size() const { return size_; }
141
142
  void Truncate(size_t size, const IOOptions& /*options*/,
143
0
                IODebugContext* /*dbg*/) {
144
0
    MutexLock lock(&mutex_);
145
0
    if (size < size_) {
146
0
      data_.resize(size);
147
0
      size_ = size;
148
0
    }
149
0
  }
150
151
0
  void CorruptBuffer() {
152
0
    if (fsynced_bytes_ >= size_) {
153
0
      return;
154
0
    }
155
0
    uint64_t buffered_bytes = size_ - fsynced_bytes_;
156
0
    uint64_t start =
157
0
        fsynced_bytes_ + rnd_.Uniform(static_cast<int>(buffered_bytes));
158
0
    uint64_t end = std::min(start + 512, size_.load());
159
0
    MutexLock lock(&mutex_);
160
0
    for (uint64_t pos = start; pos < end; ++pos) {
161
0
      data_[static_cast<size_t>(pos)] = static_cast<char>(rnd_.Uniform(256));
162
0
    }
163
0
  }
164
165
  IOStatus Read(uint64_t offset, size_t n, const IOOptions& /*options*/,
166
0
                Slice* result, char* scratch, IODebugContext* /*dbg*/) const {
167
0
    {
168
0
      IOStatus s;
169
0
      TEST_SYNC_POINT_CALLBACK("MemFile::Read:IOStatus", &s);
170
0
      if (!s.ok()) {
171
        // with sync point only
172
0
        *result = Slice();
173
0
        return s;
174
0
      }
175
0
    }
176
0
    MutexLock lock(&mutex_);
177
0
    const uint64_t available = Size() - std::min(Size(), offset);
178
0
    size_t offset_ = static_cast<size_t>(offset);
179
0
    if (n > available) {
180
0
      n = static_cast<size_t>(available);
181
0
    }
182
0
    if (n == 0) {
183
0
      *result = Slice();
184
0
      return IOStatus::OK();
185
0
    }
186
0
    if (scratch) {
187
0
      memcpy(scratch, &(data_[offset_]), n);
188
0
      *result = Slice(scratch, n);
189
0
    } else {
190
0
      *result = Slice(&(data_[offset_]), n);
191
0
    }
192
0
    return IOStatus::OK();
193
0
  }
194
195
  IOStatus Write(uint64_t offset, const Slice& data,
196
0
                 const IOOptions& /*options*/, IODebugContext* /*dbg*/) {
197
0
    MutexLock lock(&mutex_);
198
0
    size_t offset_ = static_cast<size_t>(offset);
199
0
    if (offset + data.size() > data_.size()) {
200
0
      data_.resize(offset_ + data.size());
201
0
    }
202
0
    data_.replace(offset_, data.size(), data.data(), data.size());
203
0
    size_ = data_.size();
204
0
    modified_time_ = Now();
205
0
    return IOStatus::OK();
206
0
  }
207
208
  IOStatus Append(const Slice& data, const IOOptions& /*options*/,
209
0
                  IODebugContext* /*dbg*/) {
210
0
    MutexLock lock(&mutex_);
211
0
    data_.append(data.data(), data.size());
212
0
    size_ = data_.size();
213
0
    modified_time_ = Now();
214
0
    return IOStatus::OK();
215
0
  }
216
217
0
  IOStatus Fsync(const IOOptions& /*options*/, IODebugContext* /*dbg*/) {
218
0
    fsynced_bytes_ = size_.load();
219
0
    return IOStatus::OK();
220
0
  }
221
222
0
  uint64_t ModifiedTime() const { return modified_time_; }
223
224
 private:
225
0
  uint64_t Now() {
226
0
    int64_t unix_time = 0;
227
0
    auto s = clock_->GetCurrentTime(&unix_time);
228
0
    assert(s.ok());
229
0
    return static_cast<uint64_t>(unix_time);
230
0
  }
231
232
  // Private since only Unref() should be used to delete it.
233
0
  ~MemFile() { assert(refs_ == 0); }
234
235
  SystemClock* clock_;
236
  const std::string fn_;
237
  mutable port::Mutex mutex_;
238
  int refs_;
239
  bool is_lock_file_;
240
  bool locked_;
241
242
  // Data written into this file, all bytes before fsynced_bytes are
243
  // persistent.
244
  std::string data_;
245
  std::atomic<uint64_t> size_;
246
  std::atomic<uint64_t> modified_time_;
247
248
  Random rnd_;
249
  std::atomic<uint64_t> fsynced_bytes_;
250
};
251
252
namespace {
253
254
class MockSequentialFile : public FSSequentialFile {
255
 public:
256
  explicit MockSequentialFile(MemFile* file, const FileOptions& opts)
257
      : file_(file),
258
        use_direct_io_(opts.use_direct_reads),
259
        use_mmap_read_(opts.use_mmap_reads),
260
0
        pos_(0) {
261
0
    file_->Ref();
262
0
  }
263
264
0
  ~MockSequentialFile() override { file_->Unref(); }
265
266
  IOStatus Read(size_t n, const IOOptions& options, Slice* result,
267
0
                char* scratch, IODebugContext* dbg) override {
268
0
    IOStatus s = file_->Read(pos_, n, options, result,
269
0
                             (use_mmap_read_) ? nullptr : scratch, dbg);
270
0
    if (s.ok()) {
271
0
      pos_ += result->size();
272
0
    }
273
0
    return s;
274
0
  }
275
276
0
  bool use_direct_io() const override { return use_direct_io_; }
277
0
  IOStatus Skip(uint64_t n) override {
278
0
    if (pos_ > file_->Size()) {
279
0
      return IOStatus::IOError("pos_ > file_->Size()");
280
0
    }
281
0
    const uint64_t available = file_->Size() - pos_;
282
0
    if (n > available) {
283
0
      n = available;
284
0
    }
285
0
    pos_ += static_cast<size_t>(n);
286
0
    return IOStatus::OK();
287
0
  }
288
289
 private:
290
  MemFile* file_;
291
  bool use_direct_io_;
292
  bool use_mmap_read_;
293
  size_t pos_;
294
};
295
296
class MockRandomAccessFile : public FSRandomAccessFile {
297
 public:
298
  explicit MockRandomAccessFile(MemFile* file, const FileOptions& opts)
299
      : file_(file),
300
        use_direct_io_(opts.use_direct_reads),
301
0
        use_mmap_read_(opts.use_mmap_reads) {
302
0
    file_->Ref();
303
0
  }
304
305
0
  ~MockRandomAccessFile() override { file_->Unref(); }
306
307
0
  bool use_direct_io() const override { return use_direct_io_; }
308
309
  IOStatus Prefetch(uint64_t /*offset*/, size_t /*n*/,
310
                    const IOOptions& /*options*/,
311
0
                    IODebugContext* /*dbg*/) override {
312
0
    return IOStatus::OK();
313
0
  }
314
315
  IOStatus Read(uint64_t offset, size_t n, const IOOptions& options,
316
                Slice* result, char* scratch,
317
0
                IODebugContext* dbg) const override {
318
0
    if (use_mmap_read_) {
319
0
      return file_->Read(offset, n, options, result, nullptr, dbg);
320
0
    } else {
321
0
      return file_->Read(offset, n, options, result, scratch, dbg);
322
0
    }
323
0
  }
324
325
 private:
326
  MemFile* file_;
327
  bool use_direct_io_;
328
  bool use_mmap_read_;
329
};
330
331
class MockRandomRWFile : public FSRandomRWFile {
332
 public:
333
0
  explicit MockRandomRWFile(MemFile* file) : file_(file) { file_->Ref(); }
334
335
0
  ~MockRandomRWFile() override { file_->Unref(); }
336
337
  IOStatus Write(uint64_t offset, const Slice& data, const IOOptions& options,
338
0
                 IODebugContext* dbg) override {
339
0
    return file_->Write(offset, data, options, dbg);
340
0
  }
341
342
  IOStatus Read(uint64_t offset, size_t n, const IOOptions& options,
343
                Slice* result, char* scratch,
344
0
                IODebugContext* dbg) const override {
345
0
    return file_->Read(offset, n, options, result, scratch, dbg);
346
0
  }
347
348
0
  IOStatus Close(const IOOptions& options, IODebugContext* dbg) override {
349
0
    return file_->Fsync(options, dbg);
350
0
  }
351
352
  IOStatus Flush(const IOOptions& /*options*/,
353
0
                 IODebugContext* /*dbg*/) override {
354
0
    return IOStatus::OK();
355
0
  }
356
357
0
  IOStatus Sync(const IOOptions& options, IODebugContext* dbg) override {
358
0
    return file_->Fsync(options, dbg);
359
0
  }
360
361
 private:
362
  MemFile* file_;
363
};
364
365
class MockWritableFile : public FSWritableFile {
366
 public:
367
  MockWritableFile(MemFile* file, const FileOptions& opts)
368
      : file_(file),
369
        use_direct_io_(opts.use_direct_writes),
370
0
        rate_limiter_(opts.rate_limiter) {
371
0
    file_->Ref();
372
0
  }
373
374
0
  ~MockWritableFile() override { file_->Unref(); }
375
376
0
  bool use_direct_io() const override { return false && use_direct_io_; }
377
378
  using FSWritableFile::Append;
379
  IOStatus Append(const Slice& data, const IOOptions& options,
380
0
                  IODebugContext* dbg) override {
381
0
    size_t bytes_written = 0;
382
0
    while (bytes_written < data.size()) {
383
0
      auto bytes = RequestToken(data.size() - bytes_written);
384
0
      IOStatus s = file_->Append(Slice(data.data() + bytes_written, bytes),
385
0
                                 options, dbg);
386
0
      if (!s.ok()) {
387
0
        return s;
388
0
      }
389
0
      bytes_written += bytes;
390
0
    }
391
0
    return IOStatus::OK();
392
0
  }
393
394
  using FSWritableFile::PositionedAppend;
395
  IOStatus PositionedAppend(const Slice& data, uint64_t /*offset*/,
396
                            const IOOptions& options,
397
0
                            IODebugContext* dbg) override {
398
0
    assert(use_direct_io_);
399
0
    return Append(data, options, dbg);
400
0
  }
401
402
  IOStatus Truncate(uint64_t size, const IOOptions& options,
403
0
                    IODebugContext* dbg) override {
404
0
    file_->Truncate(static_cast<size_t>(size), options, dbg);
405
0
    return IOStatus::OK();
406
0
  }
407
0
  IOStatus Close(const IOOptions& options, IODebugContext* dbg) override {
408
0
    return file_->Fsync(options, dbg);
409
0
  }
410
411
  IOStatus Flush(const IOOptions& /*options*/,
412
0
                 IODebugContext* /*dbg*/) override {
413
0
    return IOStatus::OK();
414
0
  }
415
416
0
  IOStatus Sync(const IOOptions& options, IODebugContext* dbg) override {
417
0
    return file_->Fsync(options, dbg);
418
0
  }
419
420
  uint64_t GetFileSize(const IOOptions& /*options*/,
421
0
                       IODebugContext* /*dbg*/) override {
422
0
    return file_->Size();
423
0
  }
424
425
 private:
426
0
  inline size_t RequestToken(size_t bytes) {
427
0
    if (rate_limiter_ && io_priority_ < Env::IO_TOTAL) {
428
0
      bytes = std::min(
429
0
          bytes, static_cast<size_t>(rate_limiter_->GetSingleBurstBytes()));
430
0
      rate_limiter_->Request(bytes, io_priority_);
431
0
    }
432
0
    return bytes;
433
0
  }
434
435
  MemFile* file_;
436
  bool use_direct_io_;
437
  RateLimiter* rate_limiter_;
438
};
439
440
class MockEnvDirectory : public FSDirectory {
441
 public:
442
  IOStatus Fsync(const IOOptions& /*options*/,
443
0
                 IODebugContext* /*dbg*/) override {
444
0
    return IOStatus::OK();
445
0
  }
446
447
  IOStatus Close(const IOOptions& /*options*/,
448
0
                 IODebugContext* /*dbg*/) override {
449
0
    return IOStatus::OK();
450
0
  }
451
};
452
453
class MockEnvFileLock : public FileLock {
454
 public:
455
0
  explicit MockEnvFileLock(const std::string& fname) : fname_(fname) {}
456
457
0
  std::string FileName() const { return fname_; }
458
459
 private:
460
  const std::string fname_;
461
};
462
463
class TestMemLogger : public Logger {
464
 private:
465
  std::unique_ptr<FSWritableFile> file_;
466
  std::atomic_size_t log_size_;
467
  static const uint64_t flush_every_seconds_ = 5;
468
  std::atomic_uint_fast64_t last_flush_micros_;
469
  SystemClock* clock_;
470
  IOOptions options_;
471
  IODebugContext* dbg_;
472
  std::atomic<bool> flush_pending_;
473
474
 public:
475
  TestMemLogger(std::unique_ptr<FSWritableFile> f, SystemClock* clock,
476
                const IOOptions& options, IODebugContext* dbg,
477
                const InfoLogLevel log_level = InfoLogLevel::ERROR_LEVEL)
478
      : Logger(log_level),
479
        file_(std::move(f)),
480
        log_size_(0),
481
        last_flush_micros_(0),
482
        clock_(clock),
483
        options_(options),
484
        dbg_(dbg),
485
0
        flush_pending_(false) {}
486
0
  ~TestMemLogger() override = default;
487
488
0
  void Flush() override {
489
0
    if (flush_pending_) {
490
0
      flush_pending_ = false;
491
0
    }
492
0
    last_flush_micros_ = clock_->NowMicros();
493
0
  }
494
495
  using Logger::Logv;
496
0
  void Logv(const char* format, va_list ap) override {
497
    // We try twice: the first time with a fixed-size stack allocated buffer,
498
    // and the second time with a much larger dynamically allocated buffer.
499
0
    char buffer[500];
500
0
    for (int iter = 0; iter < 2; iter++) {
501
0
      char* base;
502
0
      int bufsize;
503
0
      if (iter == 0) {
504
0
        bufsize = sizeof(buffer);
505
0
        base = buffer;
506
0
      } else {
507
0
        bufsize = 30000;
508
0
        base = new char[bufsize];
509
0
      }
510
0
      char* p = base;
511
0
      char* limit = base + bufsize;
512
513
0
      port::TimeVal now_tv;
514
0
      port::GetTimeOfDay(&now_tv, nullptr);
515
0
      const time_t seconds = now_tv.tv_sec;
516
0
      struct tm t;
517
0
      memset(&t, 0, sizeof(t));
518
0
      struct tm* ret __attribute__((__unused__));
519
0
      ret = port::LocalTimeR(&seconds, &t);
520
0
      assert(ret);
521
0
      p += snprintf(p, limit - p, "%04d/%02d/%02d-%02d:%02d:%02d.%06d ",
522
0
                    t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour,
523
0
                    t.tm_min, t.tm_sec, static_cast<int>(now_tv.tv_usec));
524
525
      // Print the message
526
0
      if (p < limit) {
527
0
        va_list backup_ap;
528
0
        va_copy(backup_ap, ap);
529
0
        p += vsnprintf(p, limit - p, format, backup_ap);
530
0
        va_end(backup_ap);
531
0
      }
532
533
      // Truncate to available space if necessary
534
0
      if (p >= limit) {
535
0
        if (iter == 0) {
536
0
          continue;  // Try again with larger buffer
537
0
        } else {
538
0
          p = limit - 1;
539
0
        }
540
0
      }
541
542
      // Add newline if necessary
543
0
      if (p == base || p[-1] != '\n') {
544
0
        *p++ = '\n';
545
0
      }
546
547
0
      assert(p <= limit);
548
0
      const size_t write_size = p - base;
549
550
0
      Status s = file_->Append(Slice(base, write_size), options_, dbg_);
551
0
      if (s.ok()) {
552
0
        flush_pending_ = true;
553
0
        log_size_ += write_size;
554
0
      }
555
0
      uint64_t now_micros =
556
0
          static_cast<uint64_t>(now_tv.tv_sec) * 1000000 + now_tv.tv_usec;
557
0
      if (now_micros - last_flush_micros_ >= flush_every_seconds_ * 1000000) {
558
0
        flush_pending_ = false;
559
0
        last_flush_micros_ = now_micros;
560
0
      }
561
0
      if (base != buffer) {
562
0
        delete[] base;
563
0
      }
564
0
      break;
565
0
    }
566
0
  }
567
0
  size_t GetLogFileSize() const override { return log_size_; }
568
};
569
570
static std::unordered_map<std::string, OptionTypeInfo> mock_fs_type_info = {
571
    {"supports_direct_io",
572
     {0, OptionType::kBoolean, OptionVerificationType::kNormal,
573
      OptionTypeFlags::kNone}},
574
};
575
}  // namespace
576
577
MockFileSystem::MockFileSystem(const std::shared_ptr<SystemClock>& clock,
578
                               bool supports_direct_io)
579
0
    : system_clock_(clock), supports_direct_io_(supports_direct_io) {
580
0
  clock_ = system_clock_.get();
581
0
  RegisterOptions("", &supports_direct_io_, &mock_fs_type_info);
582
0
}
583
584
0
MockFileSystem::~MockFileSystem() {
585
0
  for (auto i = file_map_.begin(); i != file_map_.end(); ++i) {
586
0
    i->second->Unref();
587
0
  }
588
0
}
589
590
0
Status MockFileSystem::PrepareOptions(const ConfigOptions& options) {
591
0
  Status s = FileSystem::PrepareOptions(options);
592
0
  if (s.ok() && system_clock_ == SystemClock::Default()) {
593
0
    system_clock_ = options.env->GetSystemClock();
594
0
    clock_ = system_clock_.get();
595
0
  }
596
0
  return s;
597
0
}
598
599
IOStatus MockFileSystem::GetAbsolutePath(const std::string& db_path,
600
                                         const IOOptions& /*options*/,
601
                                         std::string* output_path,
602
0
                                         IODebugContext* /*dbg*/) {
603
0
  *output_path = NormalizeMockPath(db_path);
604
0
  if (output_path->at(0) != '/') {
605
0
    return IOStatus::NotSupported("GetAbsolutePath");
606
0
  } else {
607
0
    return IOStatus::OK();
608
0
  }
609
0
}
610
611
0
std::string MockFileSystem::NormalizeMockPath(const std::string& path) {
612
0
  std::string p = NormalizePath(path);
613
0
  if (p.back() == kFilePathSeparator && p.size() > 1) {
614
0
    p.pop_back();
615
0
  }
616
0
  return p;
617
0
}
618
619
// Partial implementation of the FileSystem interface.
620
IOStatus MockFileSystem::NewSequentialFile(
621
    const std::string& fname, const FileOptions& file_opts,
622
0
    std::unique_ptr<FSSequentialFile>* result, IODebugContext* /*dbg*/) {
623
0
  auto fn = NormalizeMockPath(fname);
624
625
0
  MutexLock lock(&mutex_);
626
0
  if (file_map_.find(fn) == file_map_.end()) {
627
0
    *result = nullptr;
628
0
    return IOStatus::PathNotFound(fn);
629
0
  }
630
0
  auto* f = file_map_[fn];
631
0
  if (f->is_lock_file()) {
632
0
    return IOStatus::InvalidArgument(fn, "Cannot open a lock file.");
633
0
  } else if (file_opts.use_direct_reads && !supports_direct_io_) {
634
0
    return IOStatus::NotSupported("Direct I/O Not Supported");
635
0
  } else {
636
0
    result->reset(new MockSequentialFile(f, file_opts));
637
0
    return IOStatus::OK();
638
0
  }
639
0
}
640
641
IOStatus MockFileSystem::NewRandomAccessFile(
642
    const std::string& fname, const FileOptions& file_opts,
643
0
    std::unique_ptr<FSRandomAccessFile>* result, IODebugContext* /*dbg*/) {
644
0
  auto fn = NormalizeMockPath(fname);
645
0
  MutexLock lock(&mutex_);
646
0
  if (file_map_.find(fn) == file_map_.end()) {
647
0
    *result = nullptr;
648
0
    return IOStatus::PathNotFound(fn);
649
0
  }
650
0
  auto* f = file_map_[fn];
651
0
  if (f->is_lock_file()) {
652
0
    return IOStatus::InvalidArgument(fn, "Cannot open a lock file.");
653
0
  } else if (file_opts.use_direct_reads && !supports_direct_io_) {
654
0
    return IOStatus::NotSupported("Direct I/O Not Supported");
655
0
  } else {
656
0
    result->reset(new MockRandomAccessFile(f, file_opts));
657
0
    return IOStatus::OK();
658
0
  }
659
0
}
660
661
IOStatus MockFileSystem::NewRandomRWFile(
662
    const std::string& fname, const FileOptions& /*file_opts*/,
663
0
    std::unique_ptr<FSRandomRWFile>* result, IODebugContext* /*dbg*/) {
664
0
  auto fn = NormalizeMockPath(fname);
665
0
  MutexLock lock(&mutex_);
666
0
  if (file_map_.find(fn) == file_map_.end()) {
667
0
    *result = nullptr;
668
0
    return IOStatus::PathNotFound(fn);
669
0
  }
670
0
  auto* f = file_map_[fn];
671
0
  if (f->is_lock_file()) {
672
0
    return IOStatus::InvalidArgument(fn, "Cannot open a lock file.");
673
0
  }
674
0
  result->reset(new MockRandomRWFile(f));
675
0
  return IOStatus::OK();
676
0
}
677
678
IOStatus MockFileSystem::ReuseWritableFile(
679
    const std::string& fname, const std::string& old_fname,
680
    const FileOptions& options, std::unique_ptr<FSWritableFile>* result,
681
0
    IODebugContext* dbg) {
682
0
  auto s = RenameFile(old_fname, fname, IOOptions(), dbg);
683
0
  if (!s.ok()) {
684
0
    return s;
685
0
  } else {
686
0
    result->reset();
687
0
    return NewWritableFile(fname, options, result, dbg);
688
0
  }
689
0
}
690
691
IOStatus MockFileSystem::NewWritableFile(
692
    const std::string& fname, const FileOptions& file_opts,
693
0
    std::unique_ptr<FSWritableFile>* result, IODebugContext* /*dbg*/) {
694
0
  auto fn = NormalizeMockPath(fname);
695
0
  MutexLock lock(&mutex_);
696
0
  if (file_map_.find(fn) != file_map_.end()) {
697
0
    DeleteFileInternal(fn);
698
0
  }
699
0
  MemFile* file = new MemFile(clock_, fn, false);
700
0
  file->Ref();
701
0
  file_map_[fn] = file;
702
0
  if (file_opts.use_direct_writes && !supports_direct_io_) {
703
0
    return IOStatus::NotSupported("Direct I/O Not Supported");
704
0
  } else {
705
0
    result->reset(new MockWritableFile(file, file_opts));
706
0
    return IOStatus::OK();
707
0
  }
708
0
}
709
710
IOStatus MockFileSystem::ReopenWritableFile(
711
    const std::string& fname, const FileOptions& file_opts,
712
0
    std::unique_ptr<FSWritableFile>* result, IODebugContext* /*dbg*/) {
713
0
  auto fn = NormalizeMockPath(fname);
714
0
  MutexLock lock(&mutex_);
715
0
  MemFile* file = nullptr;
716
0
  if (file_map_.find(fn) == file_map_.end()) {
717
0
    file = new MemFile(clock_, fn, false);
718
    // Only take a reference when we create the file objectt
719
0
    file->Ref();
720
0
    file_map_[fn] = file;
721
0
  } else {
722
0
    file = file_map_[fn];
723
0
  }
724
0
  if (file_opts.use_direct_writes && !supports_direct_io_) {
725
0
    return IOStatus::NotSupported("Direct I/O Not Supported");
726
0
  } else {
727
0
    result->reset(new MockWritableFile(file, file_opts));
728
0
    return IOStatus::OK();
729
0
  }
730
0
}
731
732
IOStatus MockFileSystem::NewDirectory(const std::string& /*name*/,
733
                                      const IOOptions& /*io_opts*/,
734
                                      std::unique_ptr<FSDirectory>* result,
735
0
                                      IODebugContext* /*dbg*/) {
736
0
  result->reset(new MockEnvDirectory());
737
0
  return IOStatus::OK();
738
0
}
739
740
IOStatus MockFileSystem::FileExists(const std::string& fname,
741
                                    const IOOptions& /*io_opts*/,
742
0
                                    IODebugContext* /*dbg*/) {
743
0
  auto fn = NormalizeMockPath(fname);
744
0
  MutexLock lock(&mutex_);
745
0
  if (file_map_.find(fn) != file_map_.end()) {
746
    // File exists
747
0
    return IOStatus::OK();
748
0
  }
749
  // Now also check if fn exists as a dir
750
0
  for (const auto& iter : file_map_) {
751
0
    const std::string& filename = iter.first;
752
0
    if (filename.size() >= fn.size() + 1 && filename[fn.size()] == '/' &&
753
0
        Slice(filename).starts_with(Slice(fn))) {
754
0
      return IOStatus::OK();
755
0
    }
756
0
  }
757
0
  return IOStatus::NotFound();
758
0
}
759
760
bool MockFileSystem::GetChildrenInternal(const std::string& dir,
761
0
                                         std::vector<std::string>* result) {
762
0
  auto d = NormalizeMockPath(dir);
763
0
  bool found_dir = false;
764
0
  result->clear();
765
0
  for (const auto& iter : file_map_) {
766
0
    const std::string& filename = iter.first;
767
768
0
    if (filename == d) {
769
0
      found_dir = true;
770
0
    } else if (filename.size() >= d.size() + 1 && filename[d.size()] == '/' &&
771
0
               Slice(filename).starts_with(Slice(d))) {
772
0
      found_dir = true;
773
0
      size_t next_slash = filename.find('/', d.size() + 1);
774
0
      if (next_slash != std::string::npos) {
775
0
        result->push_back(
776
0
            filename.substr(d.size() + 1, next_slash - d.size() - 1));
777
0
      } else {
778
0
        result->push_back(filename.substr(d.size() + 1));
779
0
      }
780
0
    }
781
0
  }
782
0
  result->erase(std::unique(result->begin(), result->end()), result->end());
783
0
  return found_dir;
784
0
}
785
786
IOStatus MockFileSystem::GetChildren(const std::string& dir,
787
                                     const IOOptions& /*options*/,
788
                                     std::vector<std::string>* result,
789
0
                                     IODebugContext* /*dbg*/) {
790
0
  MutexLock lock(&mutex_);
791
0
  bool found_dir = GetChildrenInternal(dir, result);
792
0
#ifndef __clang_analyzer__
793
0
  return found_dir ? IOStatus::OK() : IOStatus::NotFound(dir);
794
#else
795
  return found_dir ? IOStatus::OK() : IOStatus::NotFound();
796
#endif
797
0
}
798
799
0
void MockFileSystem::DeleteFileInternal(const std::string& fname) {
800
0
  assert(fname == NormalizeMockPath(fname));
801
0
  const auto& pair = file_map_.find(fname);
802
0
  if (pair != file_map_.end()) {
803
0
    pair->second->Unref();
804
0
    file_map_.erase(fname);
805
0
  }
806
0
}
807
808
IOStatus MockFileSystem::DeleteFile(const std::string& fname,
809
                                    const IOOptions& /*options*/,
810
0
                                    IODebugContext* /*dbg*/) {
811
0
  auto fn = NormalizeMockPath(fname);
812
0
  MutexLock lock(&mutex_);
813
0
  if (file_map_.find(fn) == file_map_.end()) {
814
0
    return IOStatus::PathNotFound(fn);
815
0
  }
816
817
0
  DeleteFileInternal(fn);
818
0
  return IOStatus::OK();
819
0
}
820
821
IOStatus MockFileSystem::Truncate(const std::string& fname, size_t size,
822
                                  const IOOptions& options,
823
0
                                  IODebugContext* dbg) {
824
0
  auto fn = NormalizeMockPath(fname);
825
0
  MutexLock lock(&mutex_);
826
0
  auto iter = file_map_.find(fn);
827
0
  if (iter == file_map_.end()) {
828
0
    return IOStatus::PathNotFound(fn);
829
0
  }
830
0
  iter->second->Truncate(size, options, dbg);
831
0
  return IOStatus::OK();
832
0
}
833
834
IOStatus MockFileSystem::CreateDir(const std::string& dirname,
835
                                   const IOOptions& /*options*/,
836
0
                                   IODebugContext* /*dbg*/) {
837
0
  auto dn = NormalizeMockPath(dirname);
838
0
  MutexLock lock(&mutex_);
839
0
  if (file_map_.find(dn) == file_map_.end()) {
840
0
    MemFile* file = new MemFile(clock_, dn, false);
841
0
    file->Ref();
842
0
    file_map_[dn] = file;
843
0
  } else {
844
0
    return IOStatus::IOError();
845
0
  }
846
0
  return IOStatus::OK();
847
0
}
848
849
IOStatus MockFileSystem::CreateDirIfMissing(const std::string& dirname,
850
                                            const IOOptions& options,
851
0
                                            IODebugContext* dbg) {
852
0
  CreateDir(dirname, options, dbg).PermitUncheckedError();
853
0
  return IOStatus::OK();
854
0
}
855
856
IOStatus MockFileSystem::DeleteDir(const std::string& dirname,
857
                                   const IOOptions& /*options*/,
858
0
                                   IODebugContext* /*dbg*/) {
859
0
  auto dir = NormalizeMockPath(dirname);
860
0
  MutexLock lock(&mutex_);
861
0
  if (file_map_.find(dir) == file_map_.end()) {
862
0
    return IOStatus::PathNotFound(dir);
863
0
  } else {
864
0
    std::vector<std::string> children;
865
0
    if (GetChildrenInternal(dir, &children)) {
866
0
      for (const auto& child : children) {
867
0
        DeleteFileInternal(child);
868
0
      }
869
0
    }
870
0
    DeleteFileInternal(dir);
871
0
    return IOStatus::OK();
872
0
  }
873
0
}
874
875
IOStatus MockFileSystem::GetFileSize(const std::string& fname,
876
                                     const IOOptions& /*options*/,
877
                                     uint64_t* file_size,
878
0
                                     IODebugContext* /*dbg*/) {
879
0
  auto fn = NormalizeMockPath(fname);
880
0
  TEST_SYNC_POINT_CALLBACK("MockFileSystem::GetFileSize:CheckFileType", &fn);
881
0
  MutexLock lock(&mutex_);
882
0
  auto iter = file_map_.find(fn);
883
0
  if (iter == file_map_.end()) {
884
0
    return IOStatus::PathNotFound(fn);
885
0
  }
886
887
0
  *file_size = iter->second->Size();
888
0
  return IOStatus::OK();
889
0
}
890
891
IOStatus MockFileSystem::GetFileModificationTime(const std::string& fname,
892
                                                 const IOOptions& /*options*/,
893
                                                 uint64_t* time,
894
0
                                                 IODebugContext* /*dbg*/) {
895
0
  auto fn = NormalizeMockPath(fname);
896
0
  MutexLock lock(&mutex_);
897
0
  auto iter = file_map_.find(fn);
898
0
  if (iter == file_map_.end()) {
899
0
    return IOStatus::PathNotFound(fn);
900
0
  }
901
0
  *time = iter->second->ModifiedTime();
902
0
  return IOStatus::OK();
903
0
}
904
905
bool MockFileSystem::RenameFileInternal(const std::string& src,
906
0
                                        const std::string& dest) {
907
0
  if (file_map_.find(src) == file_map_.end()) {
908
0
    return false;
909
0
  } else {
910
0
    std::vector<std::string> children;
911
0
    if (GetChildrenInternal(src, &children)) {
912
0
      for (const auto& child : children) {
913
0
        RenameFileInternal(src + "/" + child, dest + "/" + child);
914
0
      }
915
0
    }
916
0
    DeleteFileInternal(dest);
917
0
    file_map_[dest] = file_map_[src];
918
0
    file_map_.erase(src);
919
0
    return true;
920
0
  }
921
0
}
922
923
IOStatus MockFileSystem::RenameFile(const std::string& src,
924
                                    const std::string& dest,
925
                                    const IOOptions& /*options*/,
926
0
                                    IODebugContext* /*dbg*/) {
927
0
  auto s = NormalizeMockPath(src);
928
0
  auto t = NormalizeMockPath(dest);
929
0
  MutexLock lock(&mutex_);
930
0
  bool found = RenameFileInternal(s, t);
931
0
  if (!found) {
932
0
    return IOStatus::PathNotFound(s);
933
0
  } else {
934
0
    return IOStatus::OK();
935
0
  }
936
0
}
937
938
IOStatus MockFileSystem::LinkFile(const std::string& src,
939
                                  const std::string& dest,
940
                                  const IOOptions& /*options*/,
941
0
                                  IODebugContext* /*dbg*/) {
942
0
  auto s = NormalizeMockPath(src);
943
0
  auto t = NormalizeMockPath(dest);
944
0
  MutexLock lock(&mutex_);
945
0
  if (file_map_.find(s) == file_map_.end()) {
946
0
    return IOStatus::PathNotFound(s);
947
0
  }
948
949
0
  DeleteFileInternal(t);
950
0
  file_map_[t] = file_map_[s];
951
0
  file_map_[t]->Ref();  // Otherwise it might get deleted when noone uses s
952
0
  return IOStatus::OK();
953
0
}
954
955
IOStatus MockFileSystem::NewLogger(const std::string& fname,
956
                                   const IOOptions& io_opts,
957
                                   std::shared_ptr<Logger>* result,
958
0
                                   IODebugContext* dbg) {
959
0
  auto fn = NormalizeMockPath(fname);
960
0
  MutexLock lock(&mutex_);
961
0
  auto iter = file_map_.find(fn);
962
0
  MemFile* file = nullptr;
963
0
  if (iter == file_map_.end()) {
964
0
    file = new MemFile(clock_, fn, false);
965
0
    file->Ref();
966
0
    file_map_[fn] = file;
967
0
  } else {
968
0
    file = iter->second;
969
0
  }
970
0
  std::unique_ptr<FSWritableFile> f(new MockWritableFile(file, FileOptions()));
971
0
  result->reset(new TestMemLogger(std::move(f), clock_, io_opts, dbg));
972
0
  return IOStatus::OK();
973
0
}
974
975
IOStatus MockFileSystem::LockFile(const std::string& fname,
976
                                  const IOOptions& /*options*/,
977
0
                                  FileLock** flock, IODebugContext* /*dbg*/) {
978
0
  auto fn = NormalizeMockPath(fname);
979
0
  {
980
0
    MutexLock lock(&mutex_);
981
0
    if (file_map_.find(fn) != file_map_.end()) {
982
0
      if (!file_map_[fn]->is_lock_file()) {
983
0
        return IOStatus::InvalidArgument(fname, "Not a lock file.");
984
0
      }
985
0
      if (!file_map_[fn]->Lock()) {
986
0
        return IOStatus::IOError(fn, "lock is already held.");
987
0
      }
988
0
    } else {
989
0
      auto* file = new MemFile(clock_, fn, true);
990
0
      file->Ref();
991
0
      file->Lock();
992
0
      file_map_[fn] = file;
993
0
    }
994
0
  }
995
0
  *flock = new MockEnvFileLock(fn);
996
0
  return IOStatus::OK();
997
0
}
998
999
IOStatus MockFileSystem::UnlockFile(FileLock* flock,
1000
                                    const IOOptions& /*options*/,
1001
0
                                    IODebugContext* /*dbg*/) {
1002
0
  std::string fn = static_cast_with_check<MockEnvFileLock>(flock)->FileName();
1003
0
  {
1004
0
    MutexLock lock(&mutex_);
1005
0
    if (file_map_.find(fn) != file_map_.end()) {
1006
0
      if (!file_map_[fn]->is_lock_file()) {
1007
0
        return IOStatus::InvalidArgument(fn, "Not a lock file.");
1008
0
      }
1009
0
      file_map_[fn]->Unlock();
1010
0
    }
1011
0
  }
1012
0
  delete flock;
1013
0
  return IOStatus::OK();
1014
0
}
1015
1016
IOStatus MockFileSystem::GetTestDirectory(const IOOptions& /*options*/,
1017
                                          std::string* path,
1018
0
                                          IODebugContext* /*dbg*/) {
1019
0
  *path = "/test";
1020
0
  return IOStatus::OK();
1021
0
}
1022
1023
0
Status MockFileSystem::CorruptBuffer(const std::string& fname) {
1024
0
  auto fn = NormalizeMockPath(fname);
1025
0
  MutexLock lock(&mutex_);
1026
0
  auto iter = file_map_.find(fn);
1027
0
  if (iter == file_map_.end()) {
1028
0
    return Status::IOError(fn, "File not found");
1029
0
  }
1030
0
  iter->second->CorruptBuffer();
1031
0
  return Status::OK();
1032
0
}
1033
1034
MockEnv::MockEnv(Env* env, const std::shared_ptr<FileSystem>& fs,
1035
                 const std::shared_ptr<SystemClock>& clock)
1036
0
    : CompositeEnvWrapper(env, fs, clock) {}
1037
1038
0
MockEnv* MockEnv::Create(Env* env) {
1039
0
  auto clock =
1040
0
      std::make_shared<EmulatedSystemClock>(env->GetSystemClock(), true);
1041
0
  return MockEnv::Create(env, clock);
1042
0
}
1043
1044
0
MockEnv* MockEnv::Create(Env* env, const std::shared_ptr<SystemClock>& clock) {
1045
0
  auto fs = std::make_shared<MockFileSystem>(clock);
1046
0
  return new MockEnv(env, fs, clock);
1047
0
}
1048
1049
0
Status MockEnv::CorruptBuffer(const std::string& fname) {
1050
0
  auto mock = static_cast_with_check<MockFileSystem>(GetFileSystem().get());
1051
0
  return mock->CorruptBuffer(fname);
1052
0
}
1053
1054
// This is to maintain the behavior before swithcing from InMemoryEnv to MockEnv
1055
0
Env* NewMemEnv(Env* base_env) { return MockEnv::Create(base_env); }
1056
1057
1058
}  // namespace ROCKSDB_NAMESPACE