Coverage Report

Created: 2025-10-26 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/env/mock_env.cc
Line
Count
Source
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
0
    : SystemClockWrapper(base),
76
0
      maybe_starting_time_(MaybeCurrentTime(base)),
77
0
      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
0
      : clock_(clock),
88
0
        fn_(fn),
89
0
        refs_(0),
90
0
        is_lock_file_(_is_lock_file),
91
0
        locked_(false),
92
0
        size_(0),
93
0
        modified_time_(Now()),
94
0
        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
0
      : file_(file),
258
0
        use_direct_io_(opts.use_direct_reads),
259
0
        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
0
      : file_(file),
300
0
        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
0
  IOStatus GetFileSize(uint64_t* size) override {
326
0
    *size = file_->Size();
327
0
    return IOStatus::OK();
328
0
  }
329
330
 private:
331
  MemFile* file_;
332
  bool use_direct_io_;
333
  bool use_mmap_read_;
334
};
335
336
class MockRandomRWFile : public FSRandomRWFile {
337
 public:
338
0
  explicit MockRandomRWFile(MemFile* file) : file_(file) { file_->Ref(); }
339
340
0
  ~MockRandomRWFile() override { file_->Unref(); }
341
342
  IOStatus Write(uint64_t offset, const Slice& data, const IOOptions& options,
343
0
                 IODebugContext* dbg) override {
344
0
    return file_->Write(offset, data, options, dbg);
345
0
  }
346
347
  IOStatus Read(uint64_t offset, size_t n, const IOOptions& options,
348
                Slice* result, char* scratch,
349
0
                IODebugContext* dbg) const override {
350
0
    return file_->Read(offset, n, options, result, scratch, dbg);
351
0
  }
352
353
0
  IOStatus Close(const IOOptions& options, IODebugContext* dbg) override {
354
0
    return file_->Fsync(options, dbg);
355
0
  }
356
357
  IOStatus Flush(const IOOptions& /*options*/,
358
0
                 IODebugContext* /*dbg*/) override {
359
0
    return IOStatus::OK();
360
0
  }
361
362
0
  IOStatus Sync(const IOOptions& options, IODebugContext* dbg) override {
363
0
    return file_->Fsync(options, dbg);
364
0
  }
365
366
 private:
367
  MemFile* file_;
368
};
369
370
class MockWritableFile : public FSWritableFile {
371
 public:
372
  MockWritableFile(MemFile* file, const FileOptions& opts)
373
0
      : file_(file),
374
0
        use_direct_io_(opts.use_direct_writes),
375
0
        rate_limiter_(opts.rate_limiter) {
376
0
    file_->Ref();
377
0
  }
378
379
0
  ~MockWritableFile() override { file_->Unref(); }
380
381
0
  bool use_direct_io() const override { return false && use_direct_io_; }
382
383
  using FSWritableFile::Append;
384
  IOStatus Append(const Slice& data, const IOOptions& options,
385
0
                  IODebugContext* dbg) override {
386
0
    size_t bytes_written = 0;
387
0
    while (bytes_written < data.size()) {
388
0
      auto bytes = RequestToken(data.size() - bytes_written);
389
0
      IOStatus s = file_->Append(Slice(data.data() + bytes_written, bytes),
390
0
                                 options, dbg);
391
0
      if (!s.ok()) {
392
0
        return s;
393
0
      }
394
0
      bytes_written += bytes;
395
0
    }
396
0
    return IOStatus::OK();
397
0
  }
398
399
  using FSWritableFile::PositionedAppend;
400
  IOStatus PositionedAppend(const Slice& data, uint64_t /*offset*/,
401
                            const IOOptions& options,
402
0
                            IODebugContext* dbg) override {
403
0
    assert(use_direct_io_);
404
0
    return Append(data, options, dbg);
405
0
  }
406
407
  IOStatus Truncate(uint64_t size, const IOOptions& options,
408
0
                    IODebugContext* dbg) override {
409
0
    file_->Truncate(static_cast<size_t>(size), options, dbg);
410
0
    return IOStatus::OK();
411
0
  }
412
0
  IOStatus Close(const IOOptions& options, IODebugContext* dbg) override {
413
0
    return file_->Fsync(options, dbg);
414
0
  }
415
416
  IOStatus Flush(const IOOptions& /*options*/,
417
0
                 IODebugContext* /*dbg*/) override {
418
0
    return IOStatus::OK();
419
0
  }
420
421
0
  IOStatus Sync(const IOOptions& options, IODebugContext* dbg) override {
422
0
    return file_->Fsync(options, dbg);
423
0
  }
424
425
  uint64_t GetFileSize(const IOOptions& /*options*/,
426
0
                       IODebugContext* /*dbg*/) override {
427
0
    return file_->Size();
428
0
  }
429
430
 private:
431
0
  inline size_t RequestToken(size_t bytes) {
432
0
    if (rate_limiter_ && io_priority_ < Env::IO_TOTAL) {
433
0
      bytes = std::min(
434
0
          bytes, static_cast<size_t>(rate_limiter_->GetSingleBurstBytes()));
435
0
      rate_limiter_->Request(bytes, io_priority_);
436
0
    }
437
0
    return bytes;
438
0
  }
439
440
  MemFile* file_;
441
  bool use_direct_io_;
442
  RateLimiter* rate_limiter_;
443
};
444
445
class MockEnvDirectory : public FSDirectory {
446
 public:
447
  IOStatus Fsync(const IOOptions& /*options*/,
448
0
                 IODebugContext* /*dbg*/) override {
449
0
    return IOStatus::OK();
450
0
  }
451
452
  IOStatus Close(const IOOptions& /*options*/,
453
0
                 IODebugContext* /*dbg*/) override {
454
0
    return IOStatus::OK();
455
0
  }
456
};
457
458
class MockEnvFileLock : public FileLock {
459
 public:
460
0
  explicit MockEnvFileLock(const std::string& fname) : fname_(fname) {}
461
462
0
  std::string FileName() const { return fname_; }
463
464
 private:
465
  const std::string fname_;
466
};
467
468
class TestMemLogger : public Logger {
469
 private:
470
  std::unique_ptr<FSWritableFile> file_;
471
  std::atomic_size_t log_size_;
472
  static const uint64_t flush_every_seconds_ = 5;
473
  std::atomic_uint_fast64_t last_flush_micros_;
474
  SystemClock* clock_;
475
  IOOptions options_;
476
  IODebugContext* dbg_;
477
  std::atomic<bool> flush_pending_;
478
479
 public:
480
  TestMemLogger(std::unique_ptr<FSWritableFile> f, SystemClock* clock,
481
                const IOOptions& options, IODebugContext* dbg,
482
                const InfoLogLevel log_level = InfoLogLevel::ERROR_LEVEL)
483
0
      : Logger(log_level),
484
0
        file_(std::move(f)),
485
0
        log_size_(0),
486
0
        last_flush_micros_(0),
487
0
        clock_(clock),
488
0
        options_(options),
489
0
        dbg_(dbg),
490
0
        flush_pending_(false) {}
491
0
  ~TestMemLogger() override = default;
492
493
0
  void Flush() override {
494
0
    if (flush_pending_) {
495
0
      flush_pending_ = false;
496
0
    }
497
0
    last_flush_micros_ = clock_->NowMicros();
498
0
  }
499
500
  using Logger::Logv;
501
0
  void Logv(const char* format, va_list ap) override {
502
    // We try twice: the first time with a fixed-size stack allocated buffer,
503
    // and the second time with a much larger dynamically allocated buffer.
504
0
    char buffer[500];
505
0
    for (int iter = 0; iter < 2; iter++) {
506
0
      char* base;
507
0
      int bufsize;
508
0
      if (iter == 0) {
509
0
        bufsize = sizeof(buffer);
510
0
        base = buffer;
511
0
      } else {
512
0
        bufsize = 30000;
513
0
        base = new char[bufsize];
514
0
      }
515
0
      char* p = base;
516
0
      char* limit = base + bufsize;
517
518
0
      port::TimeVal now_tv;
519
0
      port::GetTimeOfDay(&now_tv, nullptr);
520
0
      const time_t seconds = now_tv.tv_sec;
521
0
      struct tm t;
522
0
      memset(&t, 0, sizeof(t));
523
0
      struct tm* ret __attribute__((__unused__));
524
0
      ret = port::LocalTimeR(&seconds, &t);
525
0
      assert(ret);
526
0
      p += snprintf(p, limit - p, "%04d/%02d/%02d-%02d:%02d:%02d.%06d ",
527
0
                    t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour,
528
0
                    t.tm_min, t.tm_sec, static_cast<int>(now_tv.tv_usec));
529
530
      // Print the message
531
0
      if (p < limit) {
532
0
        va_list backup_ap;
533
0
        va_copy(backup_ap, ap);
534
0
        p += vsnprintf(p, limit - p, format, backup_ap);
535
0
        va_end(backup_ap);
536
0
      }
537
538
      // Truncate to available space if necessary
539
0
      if (p >= limit) {
540
0
        if (iter == 0) {
541
0
          continue;  // Try again with larger buffer
542
0
        } else {
543
0
          p = limit - 1;
544
0
        }
545
0
      }
546
547
      // Add newline if necessary
548
0
      if (p == base || p[-1] != '\n') {
549
0
        *p++ = '\n';
550
0
      }
551
552
0
      assert(p <= limit);
553
0
      const size_t write_size = p - base;
554
555
0
      Status s = file_->Append(Slice(base, write_size), options_, dbg_);
556
0
      if (s.ok()) {
557
0
        flush_pending_ = true;
558
0
        log_size_ += write_size;
559
0
      }
560
0
      uint64_t now_micros =
561
0
          static_cast<uint64_t>(now_tv.tv_sec) * 1000000 + now_tv.tv_usec;
562
0
      if (now_micros - last_flush_micros_ >= flush_every_seconds_ * 1000000) {
563
0
        flush_pending_ = false;
564
0
        last_flush_micros_ = now_micros;
565
0
      }
566
0
      if (base != buffer) {
567
0
        delete[] base;
568
0
      }
569
0
      break;
570
0
    }
571
0
  }
572
0
  size_t GetLogFileSize() const override { return log_size_; }
573
};
574
575
static std::unordered_map<std::string, OptionTypeInfo> mock_fs_type_info = {
576
    {"supports_direct_io",
577
     {0, OptionType::kBoolean, OptionVerificationType::kNormal,
578
      OptionTypeFlags::kNone}},
579
};
580
}  // namespace
581
582
MockFileSystem::MockFileSystem(const std::shared_ptr<SystemClock>& clock,
583
                               bool supports_direct_io)
584
0
    : system_clock_(clock), supports_direct_io_(supports_direct_io) {
585
0
  clock_ = system_clock_.get();
586
0
  RegisterOptions("", &supports_direct_io_, &mock_fs_type_info);
587
0
}
588
589
0
MockFileSystem::~MockFileSystem() {
590
0
  for (auto i = file_map_.begin(); i != file_map_.end(); ++i) {
591
0
    i->second->Unref();
592
0
  }
593
0
}
594
595
0
Status MockFileSystem::PrepareOptions(const ConfigOptions& options) {
596
0
  Status s = FileSystem::PrepareOptions(options);
597
0
  if (s.ok() && system_clock_ == SystemClock::Default()) {
598
0
    system_clock_ = options.env->GetSystemClock();
599
0
    clock_ = system_clock_.get();
600
0
  }
601
0
  return s;
602
0
}
603
604
IOStatus MockFileSystem::GetAbsolutePath(const std::string& db_path,
605
                                         const IOOptions& /*options*/,
606
                                         std::string* output_path,
607
0
                                         IODebugContext* /*dbg*/) {
608
0
  *output_path = NormalizeMockPath(db_path);
609
0
  if (output_path->at(0) != '/') {
610
0
    return IOStatus::NotSupported("GetAbsolutePath");
611
0
  } else {
612
0
    return IOStatus::OK();
613
0
  }
614
0
}
615
616
0
std::string MockFileSystem::NormalizeMockPath(const std::string& path) {
617
0
  std::string p = NormalizePath(path);
618
0
  if (p.back() == kFilePathSeparator && p.size() > 1) {
619
0
    p.pop_back();
620
0
  }
621
0
  return p;
622
0
}
623
624
// Partial implementation of the FileSystem interface.
625
IOStatus MockFileSystem::NewSequentialFile(
626
    const std::string& fname, const FileOptions& file_opts,
627
0
    std::unique_ptr<FSSequentialFile>* result, IODebugContext* /*dbg*/) {
628
0
  auto fn = NormalizeMockPath(fname);
629
630
0
  MutexLock lock(&mutex_);
631
0
  if (file_map_.find(fn) == file_map_.end()) {
632
0
    *result = nullptr;
633
0
    return IOStatus::PathNotFound(fn);
634
0
  }
635
0
  auto* f = file_map_[fn];
636
0
  if (f->is_lock_file()) {
637
0
    return IOStatus::InvalidArgument(fn, "Cannot open a lock file.");
638
0
  } else if (file_opts.use_direct_reads && !supports_direct_io_) {
639
0
    return IOStatus::NotSupported("Direct I/O Not Supported");
640
0
  } else {
641
0
    result->reset(new MockSequentialFile(f, file_opts));
642
0
    return IOStatus::OK();
643
0
  }
644
0
}
645
646
IOStatus MockFileSystem::NewRandomAccessFile(
647
    const std::string& fname, const FileOptions& file_opts,
648
0
    std::unique_ptr<FSRandomAccessFile>* result, IODebugContext* /*dbg*/) {
649
0
  auto fn = NormalizeMockPath(fname);
650
0
  MutexLock lock(&mutex_);
651
0
  if (file_map_.find(fn) == file_map_.end()) {
652
0
    *result = nullptr;
653
0
    return IOStatus::PathNotFound(fn);
654
0
  }
655
0
  auto* f = file_map_[fn];
656
0
  if (f->is_lock_file()) {
657
0
    return IOStatus::InvalidArgument(fn, "Cannot open a lock file.");
658
0
  } else if (file_opts.use_direct_reads && !supports_direct_io_) {
659
0
    return IOStatus::NotSupported("Direct I/O Not Supported");
660
0
  } else {
661
0
    result->reset(new MockRandomAccessFile(f, file_opts));
662
0
    return IOStatus::OK();
663
0
  }
664
0
}
665
666
IOStatus MockFileSystem::NewRandomRWFile(
667
    const std::string& fname, const FileOptions& /*file_opts*/,
668
0
    std::unique_ptr<FSRandomRWFile>* result, IODebugContext* /*dbg*/) {
669
0
  auto fn = NormalizeMockPath(fname);
670
0
  MutexLock lock(&mutex_);
671
0
  if (file_map_.find(fn) == file_map_.end()) {
672
0
    *result = nullptr;
673
0
    return IOStatus::PathNotFound(fn);
674
0
  }
675
0
  auto* f = file_map_[fn];
676
0
  if (f->is_lock_file()) {
677
0
    return IOStatus::InvalidArgument(fn, "Cannot open a lock file.");
678
0
  }
679
0
  result->reset(new MockRandomRWFile(f));
680
0
  return IOStatus::OK();
681
0
}
682
683
IOStatus MockFileSystem::ReuseWritableFile(
684
    const std::string& fname, const std::string& old_fname,
685
    const FileOptions& options, std::unique_ptr<FSWritableFile>* result,
686
0
    IODebugContext* dbg) {
687
0
  auto s = RenameFile(old_fname, fname, IOOptions(), dbg);
688
0
  if (!s.ok()) {
689
0
    return s;
690
0
  } else {
691
0
    result->reset();
692
0
    return NewWritableFile(fname, options, result, dbg);
693
0
  }
694
0
}
695
696
IOStatus MockFileSystem::NewWritableFile(
697
    const std::string& fname, const FileOptions& file_opts,
698
0
    std::unique_ptr<FSWritableFile>* result, IODebugContext* /*dbg*/) {
699
0
  auto fn = NormalizeMockPath(fname);
700
0
  MutexLock lock(&mutex_);
701
0
  if (file_map_.find(fn) != file_map_.end()) {
702
0
    DeleteFileInternal(fn);
703
0
  }
704
0
  MemFile* file = new MemFile(clock_, fn, false);
705
0
  file->Ref();
706
0
  file_map_[fn] = file;
707
0
  if (file_opts.use_direct_writes && !supports_direct_io_) {
708
0
    return IOStatus::NotSupported("Direct I/O Not Supported");
709
0
  } else {
710
0
    result->reset(new MockWritableFile(file, file_opts));
711
0
    return IOStatus::OK();
712
0
  }
713
0
}
714
715
IOStatus MockFileSystem::ReopenWritableFile(
716
    const std::string& fname, const FileOptions& file_opts,
717
0
    std::unique_ptr<FSWritableFile>* result, IODebugContext* /*dbg*/) {
718
0
  auto fn = NormalizeMockPath(fname);
719
0
  MutexLock lock(&mutex_);
720
0
  MemFile* file = nullptr;
721
0
  if (file_map_.find(fn) == file_map_.end()) {
722
0
    file = new MemFile(clock_, fn, false);
723
    // Only take a reference when we create the file objectt
724
0
    file->Ref();
725
0
    file_map_[fn] = file;
726
0
  } else {
727
0
    file = file_map_[fn];
728
0
  }
729
0
  if (file_opts.use_direct_writes && !supports_direct_io_) {
730
0
    return IOStatus::NotSupported("Direct I/O Not Supported");
731
0
  } else {
732
0
    result->reset(new MockWritableFile(file, file_opts));
733
0
    return IOStatus::OK();
734
0
  }
735
0
}
736
737
IOStatus MockFileSystem::NewDirectory(const std::string& /*name*/,
738
                                      const IOOptions& /*io_opts*/,
739
                                      std::unique_ptr<FSDirectory>* result,
740
0
                                      IODebugContext* /*dbg*/) {
741
0
  result->reset(new MockEnvDirectory());
742
0
  return IOStatus::OK();
743
0
}
744
745
IOStatus MockFileSystem::FileExists(const std::string& fname,
746
                                    const IOOptions& /*io_opts*/,
747
0
                                    IODebugContext* /*dbg*/) {
748
0
  auto fn = NormalizeMockPath(fname);
749
0
  MutexLock lock(&mutex_);
750
0
  if (file_map_.find(fn) != file_map_.end()) {
751
    // File exists
752
0
    return IOStatus::OK();
753
0
  }
754
  // Now also check if fn exists as a dir
755
0
  for (const auto& iter : file_map_) {
756
0
    const std::string& filename = iter.first;
757
0
    if (filename.size() >= fn.size() + 1 && filename[fn.size()] == '/' &&
758
0
        Slice(filename).starts_with(Slice(fn))) {
759
0
      return IOStatus::OK();
760
0
    }
761
0
  }
762
0
  return IOStatus::NotFound();
763
0
}
764
765
bool MockFileSystem::GetChildrenInternal(const std::string& dir,
766
0
                                         std::vector<std::string>* result) {
767
0
  auto d = NormalizeMockPath(dir);
768
0
  bool found_dir = false;
769
0
  result->clear();
770
0
  for (const auto& iter : file_map_) {
771
0
    const std::string& filename = iter.first;
772
773
0
    if (filename == d) {
774
0
      found_dir = true;
775
0
    } else if (filename.size() >= d.size() + 1 && filename[d.size()] == '/' &&
776
0
               Slice(filename).starts_with(Slice(d))) {
777
0
      found_dir = true;
778
0
      size_t next_slash = filename.find('/', d.size() + 1);
779
0
      if (next_slash != std::string::npos) {
780
0
        result->push_back(
781
0
            filename.substr(d.size() + 1, next_slash - d.size() - 1));
782
0
      } else {
783
0
        result->push_back(filename.substr(d.size() + 1));
784
0
      }
785
0
    }
786
0
  }
787
0
  result->erase(std::unique(result->begin(), result->end()), result->end());
788
0
  return found_dir;
789
0
}
790
791
IOStatus MockFileSystem::GetChildren(const std::string& dir,
792
                                     const IOOptions& /*options*/,
793
                                     std::vector<std::string>* result,
794
0
                                     IODebugContext* /*dbg*/) {
795
0
  MutexLock lock(&mutex_);
796
0
  bool found_dir = GetChildrenInternal(dir, result);
797
0
#ifndef __clang_analyzer__
798
0
  return found_dir ? IOStatus::OK() : IOStatus::NotFound(dir);
799
#else
800
  return found_dir ? IOStatus::OK() : IOStatus::NotFound();
801
#endif
802
0
}
803
804
0
void MockFileSystem::DeleteFileInternal(const std::string& fname) {
805
0
  assert(fname == NormalizeMockPath(fname));
806
0
  const auto& pair = file_map_.find(fname);
807
0
  if (pair != file_map_.end()) {
808
0
    pair->second->Unref();
809
0
    file_map_.erase(fname);
810
0
  }
811
0
}
812
813
IOStatus MockFileSystem::DeleteFile(const std::string& fname,
814
                                    const IOOptions& /*options*/,
815
0
                                    IODebugContext* /*dbg*/) {
816
0
  auto fn = NormalizeMockPath(fname);
817
0
  MutexLock lock(&mutex_);
818
0
  if (file_map_.find(fn) == file_map_.end()) {
819
0
    return IOStatus::PathNotFound(fn);
820
0
  }
821
822
0
  DeleteFileInternal(fn);
823
0
  return IOStatus::OK();
824
0
}
825
826
IOStatus MockFileSystem::Truncate(const std::string& fname, size_t size,
827
                                  const IOOptions& options,
828
0
                                  IODebugContext* dbg) {
829
0
  auto fn = NormalizeMockPath(fname);
830
0
  MutexLock lock(&mutex_);
831
0
  auto iter = file_map_.find(fn);
832
0
  if (iter == file_map_.end()) {
833
0
    return IOStatus::PathNotFound(fn);
834
0
  }
835
0
  iter->second->Truncate(size, options, dbg);
836
0
  return IOStatus::OK();
837
0
}
838
839
IOStatus MockFileSystem::CreateDir(const std::string& dirname,
840
                                   const IOOptions& /*options*/,
841
0
                                   IODebugContext* /*dbg*/) {
842
0
  auto dn = NormalizeMockPath(dirname);
843
0
  MutexLock lock(&mutex_);
844
0
  if (file_map_.find(dn) == file_map_.end()) {
845
0
    MemFile* file = new MemFile(clock_, dn, false);
846
0
    file->Ref();
847
0
    file_map_[dn] = file;
848
0
  } else {
849
0
    return IOStatus::IOError();
850
0
  }
851
0
  return IOStatus::OK();
852
0
}
853
854
IOStatus MockFileSystem::CreateDirIfMissing(const std::string& dirname,
855
                                            const IOOptions& options,
856
0
                                            IODebugContext* dbg) {
857
0
  CreateDir(dirname, options, dbg).PermitUncheckedError();
858
0
  return IOStatus::OK();
859
0
}
860
861
IOStatus MockFileSystem::DeleteDir(const std::string& dirname,
862
                                   const IOOptions& /*options*/,
863
0
                                   IODebugContext* /*dbg*/) {
864
0
  auto dir = NormalizeMockPath(dirname);
865
0
  MutexLock lock(&mutex_);
866
0
  if (file_map_.find(dir) == file_map_.end()) {
867
0
    return IOStatus::PathNotFound(dir);
868
0
  } else {
869
0
    std::vector<std::string> children;
870
0
    if (GetChildrenInternal(dir, &children)) {
871
0
      for (const auto& child : children) {
872
0
        DeleteFileInternal(child);
873
0
      }
874
0
    }
875
0
    DeleteFileInternal(dir);
876
0
    return IOStatus::OK();
877
0
  }
878
0
}
879
880
IOStatus MockFileSystem::GetFileSize(const std::string& fname,
881
                                     const IOOptions& /*options*/,
882
                                     uint64_t* file_size,
883
0
                                     IODebugContext* /*dbg*/) {
884
0
  auto fn = NormalizeMockPath(fname);
885
0
  TEST_SYNC_POINT_CALLBACK("MockFileSystem::GetFileSize:CheckFileType", &fn);
886
0
  MutexLock lock(&mutex_);
887
0
  auto iter = file_map_.find(fn);
888
0
  if (iter == file_map_.end()) {
889
0
    return IOStatus::PathNotFound(fn);
890
0
  }
891
892
0
  *file_size = iter->second->Size();
893
0
  return IOStatus::OK();
894
0
}
895
896
IOStatus MockFileSystem::GetFileModificationTime(const std::string& fname,
897
                                                 const IOOptions& /*options*/,
898
                                                 uint64_t* time,
899
0
                                                 IODebugContext* /*dbg*/) {
900
0
  auto fn = NormalizeMockPath(fname);
901
0
  MutexLock lock(&mutex_);
902
0
  auto iter = file_map_.find(fn);
903
0
  if (iter == file_map_.end()) {
904
0
    return IOStatus::PathNotFound(fn);
905
0
  }
906
0
  *time = iter->second->ModifiedTime();
907
0
  return IOStatus::OK();
908
0
}
909
910
bool MockFileSystem::RenameFileInternal(const std::string& src,
911
0
                                        const std::string& dest) {
912
0
  if (file_map_.find(src) == file_map_.end()) {
913
0
    return false;
914
0
  } else {
915
0
    std::vector<std::string> children;
916
0
    if (GetChildrenInternal(src, &children)) {
917
0
      for (const auto& child : children) {
918
0
        RenameFileInternal(src + "/" + child, dest + "/" + child);
919
0
      }
920
0
    }
921
0
    DeleteFileInternal(dest);
922
0
    file_map_[dest] = file_map_[src];
923
0
    file_map_.erase(src);
924
0
    return true;
925
0
  }
926
0
}
927
928
IOStatus MockFileSystem::RenameFile(const std::string& src,
929
                                    const std::string& dest,
930
                                    const IOOptions& /*options*/,
931
0
                                    IODebugContext* /*dbg*/) {
932
0
  auto s = NormalizeMockPath(src);
933
0
  auto t = NormalizeMockPath(dest);
934
0
  MutexLock lock(&mutex_);
935
0
  bool found = RenameFileInternal(s, t);
936
0
  if (!found) {
937
0
    return IOStatus::PathNotFound(s);
938
0
  } else {
939
0
    return IOStatus::OK();
940
0
  }
941
0
}
942
943
IOStatus MockFileSystem::LinkFile(const std::string& src,
944
                                  const std::string& dest,
945
                                  const IOOptions& /*options*/,
946
0
                                  IODebugContext* /*dbg*/) {
947
0
  auto s = NormalizeMockPath(src);
948
0
  auto t = NormalizeMockPath(dest);
949
0
  MutexLock lock(&mutex_);
950
0
  if (file_map_.find(s) == file_map_.end()) {
951
0
    return IOStatus::PathNotFound(s);
952
0
  }
953
954
0
  DeleteFileInternal(t);
955
0
  file_map_[t] = file_map_[s];
956
0
  file_map_[t]->Ref();  // Otherwise it might get deleted when noone uses s
957
0
  return IOStatus::OK();
958
0
}
959
960
IOStatus MockFileSystem::NewLogger(const std::string& fname,
961
                                   const IOOptions& io_opts,
962
                                   std::shared_ptr<Logger>* result,
963
0
                                   IODebugContext* dbg) {
964
0
  auto fn = NormalizeMockPath(fname);
965
0
  MutexLock lock(&mutex_);
966
0
  auto iter = file_map_.find(fn);
967
0
  MemFile* file = nullptr;
968
0
  if (iter == file_map_.end()) {
969
0
    file = new MemFile(clock_, fn, false);
970
0
    file->Ref();
971
0
    file_map_[fn] = file;
972
0
  } else {
973
0
    file = iter->second;
974
0
  }
975
0
  std::unique_ptr<FSWritableFile> f(new MockWritableFile(file, FileOptions()));
976
0
  result->reset(new TestMemLogger(std::move(f), clock_, io_opts, dbg));
977
0
  return IOStatus::OK();
978
0
}
979
980
IOStatus MockFileSystem::LockFile(const std::string& fname,
981
                                  const IOOptions& /*options*/,
982
0
                                  FileLock** flock, IODebugContext* /*dbg*/) {
983
0
  auto fn = NormalizeMockPath(fname);
984
0
  {
985
0
    MutexLock lock(&mutex_);
986
0
    if (file_map_.find(fn) != file_map_.end()) {
987
0
      if (!file_map_[fn]->is_lock_file()) {
988
0
        return IOStatus::InvalidArgument(fname, "Not a lock file.");
989
0
      }
990
0
      if (!file_map_[fn]->Lock()) {
991
0
        return IOStatus::IOError(fn, "lock is already held.");
992
0
      }
993
0
    } else {
994
0
      auto* file = new MemFile(clock_, fn, true);
995
0
      file->Ref();
996
0
      file->Lock();
997
0
      file_map_[fn] = file;
998
0
    }
999
0
  }
1000
0
  *flock = new MockEnvFileLock(fn);
1001
0
  return IOStatus::OK();
1002
0
}
1003
1004
IOStatus MockFileSystem::UnlockFile(FileLock* flock,
1005
                                    const IOOptions& /*options*/,
1006
0
                                    IODebugContext* /*dbg*/) {
1007
0
  std::string fn = static_cast_with_check<MockEnvFileLock>(flock)->FileName();
1008
0
  {
1009
0
    MutexLock lock(&mutex_);
1010
0
    if (file_map_.find(fn) != file_map_.end()) {
1011
0
      if (!file_map_[fn]->is_lock_file()) {
1012
0
        return IOStatus::InvalidArgument(fn, "Not a lock file.");
1013
0
      }
1014
0
      file_map_[fn]->Unlock();
1015
0
    }
1016
0
  }
1017
0
  delete flock;
1018
0
  return IOStatus::OK();
1019
0
}
1020
1021
IOStatus MockFileSystem::GetTestDirectory(const IOOptions& /*options*/,
1022
                                          std::string* path,
1023
0
                                          IODebugContext* /*dbg*/) {
1024
0
  *path = "/test";
1025
0
  return IOStatus::OK();
1026
0
}
1027
1028
0
Status MockFileSystem::CorruptBuffer(const std::string& fname) {
1029
0
  auto fn = NormalizeMockPath(fname);
1030
0
  MutexLock lock(&mutex_);
1031
0
  auto iter = file_map_.find(fn);
1032
0
  if (iter == file_map_.end()) {
1033
0
    return Status::IOError(fn, "File not found");
1034
0
  }
1035
0
  iter->second->CorruptBuffer();
1036
0
  return Status::OK();
1037
0
}
1038
1039
MockEnv::MockEnv(Env* env, const std::shared_ptr<FileSystem>& fs,
1040
                 const std::shared_ptr<SystemClock>& clock)
1041
0
    : CompositeEnvWrapper(env, fs, clock) {}
1042
1043
0
MockEnv* MockEnv::Create(Env* env) {
1044
0
  auto clock =
1045
0
      std::make_shared<EmulatedSystemClock>(env->GetSystemClock(), true);
1046
0
  return MockEnv::Create(env, clock);
1047
0
}
1048
1049
0
MockEnv* MockEnv::Create(Env* env, const std::shared_ptr<SystemClock>& clock) {
1050
0
  auto fs = std::make_shared<MockFileSystem>(clock);
1051
0
  return new MockEnv(env, fs, clock);
1052
0
}
1053
1054
0
Status MockEnv::CorruptBuffer(const std::string& fname) {
1055
0
  auto mock = static_cast_with_check<MockFileSystem>(GetFileSystem().get());
1056
0
  return mock->CorruptBuffer(fname);
1057
0
}
1058
1059
// This is to maintain the behavior before swithcing from InMemoryEnv to MockEnv
1060
0
Env* NewMemEnv(Env* base_env) { return MockEnv::Create(base_env); }
1061
1062
}  // namespace ROCKSDB_NAMESPACE