Skip to content

Commit

Permalink
Atomize RenameFile in KeyManagedEncryptedEnv (#222)
Browse files Browse the repository at this point in the history
* adjust logic in KeyManagedEncryptedEnv::RenameFile to avoid poweroff

Signed-off-by: Xintao <[email protected]>
  • Loading branch information
hunterlxt authored Dec 18, 2020
1 parent 7d209a8 commit 93e89a5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
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
31 changes: 21 additions & 10 deletions encryption/encryption.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#ifdef OPENSSL
#include "encryption/encryption.h"

#include <openssl/opensslv.h>

#include <algorithm>
#include <limits>

#include <openssl/opensslv.h>

#include "port/port.h"

namespace rocksdb {
Expand Down Expand Up @@ -372,9 +372,14 @@ Status KeyManagedEncryptedEnv::ReuseWritableFile(
s = Status::InvalidArgument("Unsupported encryption method: " +
ToString(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 @@ -426,22 +431,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 @@ -71,17 +71,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

0 comments on commit 93e89a5

Please sign in to comment.