Skip to content

Commit

Permalink
Introduce file inspector and add FileSystemInspectedEnv (#216)
Browse files Browse the repository at this point in the history
Introduce `FileSystemInspector` interface and `FileSystemInspectedEnv`. With
`FileSystemInspectedEnv`, each file operation must first consult
`FileSystemInspector` before reaching actual IO logic.

Signed-off-by: tabokie <xy.tao@outlook.com>
  • Loading branch information
tabokie committed May 12, 2022
1 parent 3a238bf commit 7749860
Show file tree
Hide file tree
Showing 6 changed files with 503 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ set(SOURCES
env/env_chroot.cc
env/env_encryption.cc
env/env_hdfs.cc
env/env_inspected.cc
env/file_system.cc
env/file_system_tracer.cc
env/fs_remap.cc
Expand Down
2 changes: 2 additions & 0 deletions TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ cpp_library(
"env/env_chroot.cc",
"env/env_encryption.cc",
"env/env_hdfs.cc",
"env/env_inspected.cc",
"env/env_posix.cc",
"env/file_system.cc",
"env/file_system_tracer.cc",
Expand Down Expand Up @@ -562,6 +563,7 @@ cpp_library(
"env/env_chroot.cc",
"env/env_encryption.cc",
"env/env_hdfs.cc",
"env/env_inspected.cc",
"env/env_posix.cc",
"env/file_system.cc",
"env/file_system_tracer.cc",
Expand Down
39 changes: 39 additions & 0 deletions env/env_basic_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "rocksdb/convenience.h"
#include "rocksdb/env.h"
#include "rocksdb/env_encryption.h"
#include "rocksdb/env_inspected.h"
#include "test_util/testharness.h"

namespace ROCKSDB_NAMESPACE {
Expand Down Expand Up @@ -81,6 +82,41 @@ static Env* GetTestFS() {
EXPECT_NE(fs_env, nullptr);
return fs_env;
}

class DummyFileSystemInspector : public FileSystemInspector {
public:
DummyFileSystemInspector(size_t refill_bytes = 0)
: refill_bytes_(refill_bytes) {}

Status Read(size_t len, size_t* allowed) override {
assert(allowed);
if (refill_bytes_ == 0) {
*allowed = len;
} else {
*allowed = std::min(refill_bytes_, len);
}
return Status::OK();
}

Status Write(size_t len, size_t* allowed) override {
assert(allowed);
if (refill_bytes_ == 0) {
*allowed = len;
} else {
*allowed = std::min(refill_bytes_, len);
}
return Status::OK();
}

private:
size_t refill_bytes_;
};

static Env* GetInspectedEnv() {
static std::unique_ptr<Env> inspected_env(NewFileSystemInspectedEnv(
Env::Default(), std::make_shared<DummyFileSystemInspector>(1)));
return inspected_env.get();
}
#endif // ROCKSDB_LITE

} // namespace
Expand Down Expand Up @@ -121,6 +157,9 @@ INSTANTIATE_TEST_CASE_P(EncryptedEnv, EnvMoreTestWithParam,
INSTANTIATE_TEST_CASE_P(MemEnv, EnvBasicTestWithParam,
::testing::Values(&GetMemoryEnv));

INSTANTIATE_TEST_CASE_P(InspectedEnv, EnvBasicTestWithParam,
::testing::Values(&GetInspectedEnv));

namespace {

// Returns a vector of 0 or 1 Env*, depending whether an Env is registered for
Expand Down
Loading

0 comments on commit 7749860

Please sign in to comment.