Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomize RenameFile in KeyManagedEncryptedEnv #7

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions db/db_test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ class TestKeyManager : public encryption::KeyManager {
Status LinkFile(const std::string&, const std::string&) override {
return Status::OK();
}
Status RenameFile(const std::string&, const std::string&) override {
return Status::OK();
}
};
#endif

Expand Down
27 changes: 19 additions & 8 deletions encryption/encryption.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,14 @@ Status KeyManagedEncryptedEnv::ReuseWritableFile(
"Unsupported encryption method: " +
std::to_string(static_cast<int>(file_info.method)));
}
if (s.ok()) {
s = key_manager_->RenameFile(old_fname, fname);
if (!s.ok()) {
return s;
}
s = key_manager_->LinkFile(old_fname, fname);
if (!s.ok()) {
return s;
}
s = key_manager_->DeleteFile(old_fname);
return s;
}

Expand Down Expand Up @@ -437,22 +442,28 @@ Status KeyManagedEncryptedEnv::LinkFile(const std::string& src_fname,
}
s = target()->LinkFile(src_fname, dst_fname);
if (!s.ok()) {
// Ignore error
key_manager_->DeleteFile(dst_fname);
Status delete_status __attribute__((__unused__)) =
key_manager_->DeleteFile(dst_fname);
assert(delete_status.ok());
}
return s;
}

Status KeyManagedEncryptedEnv::RenameFile(const std::string& src_fname,
const std::string& dst_fname) {
Status s = key_manager_->RenameFile(src_fname, dst_fname);
// Link(copy)File instead of RenameFile to avoid losing src_fname info when
// failed to rename the src_fname in the file system.
Status s = key_manager_->LinkFile(src_fname, dst_fname);
if (!s.ok()) {
return s;
}
s = target()->RenameFile(src_fname, dst_fname);
if (!s.ok()) {
// Ignore error
key_manager_->RenameFile(dst_fname, src_fname);
if (s.ok()) {
s = key_manager_->DeleteFile(src_fname);
} else {
Status delete_status __attribute__((__unused__)) =
key_manager_->DeleteFile(dst_fname);
assert(delete_status.ok());
}
return s;
}
Expand Down
11 changes: 0 additions & 11 deletions encryption/in_memory_key_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ class InMemoryKeyManager final : public KeyManager {
return Status::OK();
}

Status RenameFile(const std::string& src_fname,
const std::string& dst_fname) override {
MutexLock l(&mu_);
if (files_.count(src_fname) == 0) {
return Status::Corruption("File not found: " + src_fname);
}
files_[dst_fname] = files_[src_fname];
files_.erase(src_fname);
return Status::OK();
}

private:
mutable port::Mutex mu_;
Random rnd_;
Expand Down
2 changes: 0 additions & 2 deletions include/rocksdb/encryption.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class KeyManager {
virtual Status DeleteFile(const std::string& fname) = 0;
virtual Status LinkFile(const std::string& src_fname,
const std::string& dst_fname) = 0;
virtual Status RenameFile(const std::string& src_fname,
const std::string& dst_fname) = 0;
};

// An Env with underlying files being encrypted. It holds a reference to an
Expand Down
Loading