Adding another NewFlashcacheAwareEnv function to support pre-opened fd

Summary:
There are some cases when flachcache file descriptor was
already allocated (i.e. fb-MySQL). Then NewFlashcacheAwareEnv returns an
error at open() because fd was already assigned. This diff adds another
function to instantiate FlashcacheAwareEnv, with pre-allocated fd cachedev_fd.

Test Plan: Tested with MyRocks using this function, then worked

Reviewers: sdong, igor

Reviewed By: igor

Subscribers: dhruba, MarkCallaghan, rven

Differential Revision: https://reviews.facebook.net/D36447
main
Yoshinori Matsunobu 10 years ago
parent 5e067a7b19
commit 824e646341
  1. 12
      db/db_bench.cc
  2. 5
      include/rocksdb/utilities/flashcache.h
  3. 11
      utilities/flashcache/flashcache.cc

@ -24,6 +24,7 @@ int main() {
#include <numaif.h> #include <numaif.h>
#endif #endif
#include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <cstddef> #include <cstddef>
#include <sys/types.h> #include <sys/types.h>
@ -1189,6 +1190,7 @@ class Benchmark {
int64_t readwrites_; int64_t readwrites_;
int64_t merge_keys_; int64_t merge_keys_;
bool report_file_operations_; bool report_file_operations_;
int cachedev_fd_;
bool SanityCheck() { bool SanityCheck() {
if (FLAGS_compression_ratio > 1) { if (FLAGS_compression_ratio > 1) {
@ -1456,6 +1458,9 @@ class Benchmark {
// this will leak, but we're shutting down so nobody cares // this will leak, but we're shutting down so nobody cares
cache_->DisownData(); cache_->DisownData();
} }
if (FLAGS_disable_flashcache_for_background_threads && cachedev_fd_ != -1) {
close(cachedev_fd_);
}
} }
Slice AllocateKey(std::unique_ptr<const char[]>* key_guard) { Slice AllocateKey(std::unique_ptr<const char[]>* key_guard) {
@ -2026,8 +2031,13 @@ class Benchmark {
FLAGS_env->LowerThreadPoolIOPriority(Env::HIGH); FLAGS_env->LowerThreadPoolIOPriority(Env::HIGH);
} }
if (FLAGS_disable_flashcache_for_background_threads) { if (FLAGS_disable_flashcache_for_background_threads) {
cachedev_fd_ = open(FLAGS_flashcache_dev.c_str(), O_RDONLY);
if (cachedev_fd_ < 0) {
fprintf(stderr, "Open flash device failed\n");
exit(1);
}
flashcache_aware_env_ = flashcache_aware_env_ =
std::move(NewFlashcacheAwareEnv(FLAGS_env, FLAGS_flashcache_dev)); std::move(NewFlashcacheAwareEnv(FLAGS_env, cachedev_fd_));
if (flashcache_aware_env_.get() == nullptr) { if (flashcache_aware_env_.get() == nullptr) {
fprintf(stderr, "Failed to open flashcahce device at %s\n", fprintf(stderr, "Failed to open flashcahce device at %s\n",
FLAGS_flashcache_dev.c_str()); FLAGS_flashcache_dev.c_str());

@ -17,8 +17,9 @@ namespace rocksdb {
// reads. Reads from compaction thread don't need to be cached because they are // reads. Reads from compaction thread don't need to be cached because they are
// going to be soon made obsolete (due to nature of compaction) // going to be soon made obsolete (due to nature of compaction)
// Usually you would pass Env::Default() as base. // Usually you would pass Env::Default() as base.
// flashcache_dev is a path to the flashcache device // cachedev_fd is a file descriptor of the flashcache device. Caller has to
// open flashcache device before calling this API.
extern std::unique_ptr<Env> NewFlashcacheAwareEnv( extern std::unique_ptr<Env> NewFlashcacheAwareEnv(
Env* base, const std::string& flashcache_dev); Env* base, const int cachedev_fd);
} // namespace rocksdb } // namespace rocksdb

@ -29,7 +29,6 @@ class FlashcacheAwareEnv : public EnvWrapper {
pid_t pid = getpid(); pid_t pid = getpid();
/* cleanup previous whitelistings */ /* cleanup previous whitelistings */
if (ioctl(cachedev_fd_, FLASHCACHEDELALLWHITELIST, &pid) < 0) { if (ioctl(cachedev_fd_, FLASHCACHEDELALLWHITELIST, &pid) < 0) {
close(cachedev_fd_);
cachedev_fd_ = -1; cachedev_fd_ = -1;
fprintf(stderr, "ioctl del-all-whitelist for flashcache failed\n"); fprintf(stderr, "ioctl del-all-whitelist for flashcache failed\n");
return; return;
@ -46,7 +45,6 @@ class FlashcacheAwareEnv : public EnvWrapper {
if (ioctl(cachedev_fd_, FLASHCACHEDELWHITELIST, &pid) < 0) { if (ioctl(cachedev_fd_, FLASHCACHEDELWHITELIST, &pid) < 0) {
fprintf(stderr, "ioctl del-whitelist for flashcache failed\n"); fprintf(stderr, "ioctl del-whitelist for flashcache failed\n");
} }
close(cachedev_fd_);
} }
} }
@ -103,14 +101,7 @@ class FlashcacheAwareEnv : public EnvWrapper {
}; };
std::unique_ptr<Env> NewFlashcacheAwareEnv(Env* base, std::unique_ptr<Env> NewFlashcacheAwareEnv(Env* base,
const std::string& flashcache_dev) { const int cachedev_fd) {
// Cachedev should remain open or ioctl will be lost
int cachedev_fd = open(flashcache_dev.c_str(), O_RDONLY);
if (cachedev_fd < 0) {
fprintf(stderr, "Open flash device failed\n");
return nullptr;
}
std::unique_ptr<Env> ret(new FlashcacheAwareEnv(base, cachedev_fd)); std::unique_ptr<Env> ret(new FlashcacheAwareEnv(base, cachedev_fd));
return std::move(ret); return std::move(ret);
} }

Loading…
Cancel
Save