diff --git a/db_stress_tool/db_stress_listener.cc b/db_stress_tool/db_stress_listener.cc index 9968f546541..3be642111ef 100644 --- a/db_stress_tool/db_stress_listener.cc +++ b/db_stress_tool/db_stress_listener.cc @@ -17,7 +17,7 @@ namespace ROCKSDB_NAMESPACE { // TODO: consider using expected_values_dir instead, but this is more // convenient for now. -UniqueIdVerifier::UniqueIdVerifier(const std::string& db_name) +UniqueIdVerifier::UniqueIdVerifier(const std::string& db_name, Env* env) : path_(db_name + "/.unique_ids") { // We expect such a small number of files generated during this test // (thousands?), checking full 192-bit IDs for uniqueness is a very @@ -27,14 +27,14 @@ UniqueIdVerifier::UniqueIdVerifier(const std::string& db_name) // very good probability for the quantities in this test. offset_ = Random::GetTLSInstance()->Uniform(17); // 0 to 16 - // Use default FileSystem to avoid fault injection, etc. - FileSystem& fs = *FileSystem::Default(); + // Use the FileSystem from the environment + const std::shared_ptrfs = env->GetFileSystem(); IOOptions opts; { std::unique_ptr reader; Status s = - fs.NewSequentialFile(path_, FileOptions(), &reader, /*dbg*/ nullptr); + fs->NewSequentialFile(path_, FileOptions(), &reader, /*dbg*/ nullptr); if (s.ok()) { // Load from file std::string id(24U, '\0'); @@ -54,7 +54,7 @@ UniqueIdVerifier::UniqueIdVerifier(const std::string& db_name) fprintf(stdout, "Warning: clearing corrupt unique id file\n"); id_set_.clear(); reader.reset(); - s = fs.DeleteFile(path_, opts, /*dbg*/ nullptr); + s = fs->DeleteFile(path_, opts, /*dbg*/ nullptr); assert(s.ok()); } break; @@ -65,16 +65,20 @@ UniqueIdVerifier::UniqueIdVerifier(const std::string& db_name) // Newly created is ok. // But FileSystem doesn't tell us whether non-existence was the cause of // the failure. (Issue #9021) - Status s2 = fs.FileExists(path_, opts, /*dbg*/ nullptr); - if (!s2.IsNotFound()) { - fprintf(stderr, "Error opening unique id file: %s\n", - s.ToString().c_str()); - assert(false); + Status s2 = fs->FileExists(path_, opts, /*dbg*/ nullptr); + if (s2.IsNotFound()) { + std::unique_ptr wf; + s = fs->NewWritableFile(path_, FileOptions(), &wf, /*dbg*/ nullptr); + if (!s.ok()) { + fprintf(stderr, "Error creating unique id file: %s\n", + s.ToString().c_str()); + assert(false); + } } } } fprintf(stdout, "(Re-)verified %zu unique IDs\n", id_set_.size()); - Status s = fs.ReopenWritableFile(path_, FileOptions(), &data_file_writer_, + Status s = fs->ReopenWritableFile(path_, FileOptions(), &data_file_writer_, /*dbg*/ nullptr); if (!s.ok()) { fprintf(stderr, "Error opening unique id file for append: %s\n", @@ -84,7 +88,8 @@ UniqueIdVerifier::UniqueIdVerifier(const std::string& db_name) } UniqueIdVerifier::~UniqueIdVerifier() { - data_file_writer_->Close(IOOptions(), /*dbg*/ nullptr); + if (data_file_writer_) + data_file_writer_->Close(IOOptions(), /*dbg*/ nullptr); } void UniqueIdVerifier::VerifyNoWrite(const std::string& id) { diff --git a/db_stress_tool/db_stress_listener.h b/db_stress_tool/db_stress_listener.h index 8ba7eda6584..2ebf532cd90 100644 --- a/db_stress_tool/db_stress_listener.h +++ b/db_stress_tool/db_stress_listener.h @@ -12,6 +12,7 @@ #include "file/filename.h" #include "rocksdb/db.h" #include "rocksdb/file_system.h" +#include "rocksdb/env.h" #include "rocksdb/listener.h" #include "rocksdb/table_properties.h" #include "rocksdb/unique_id.h" @@ -26,7 +27,7 @@ namespace ROCKSDB_NAMESPACE { // Verify across process executions that all seen IDs are unique class UniqueIdVerifier { public: - explicit UniqueIdVerifier(const std::string& db_name); + explicit UniqueIdVerifier(const std::string& db_name, Env* env); ~UniqueIdVerifier(); void Verify(const std::string& id); @@ -49,12 +50,13 @@ class DbStressListener : public EventListener { public: DbStressListener(const std::string& db_name, const std::vector& db_paths, - const std::vector& column_families) + const std::vector& column_families, + Env* env) : db_name_(db_name), db_paths_(db_paths), column_families_(column_families), num_pending_file_creations_(0), - unique_ids_(db_name) {} + unique_ids_(db_name, env) {} const char* Name() const override { return kClassName(); } static const char* kClassName() { return "DBStressListener"; } diff --git a/db_stress_tool/db_stress_test_base.cc b/db_stress_tool/db_stress_test_base.cc index d16fcd326e0..96a8fa36351 100644 --- a/db_stress_tool/db_stress_test_base.cc +++ b/db_stress_tool/db_stress_test_base.cc @@ -2497,7 +2497,8 @@ void StressTest::Open() { options_.listeners.clear(); #ifndef ROCKSDB_LITE options_.listeners.emplace_back( - new DbStressListener(FLAGS_db, options_.db_paths, cf_descriptors)); + new DbStressListener(FLAGS_db, options_.db_paths, + cf_descriptors, db_stress_env)); #endif // !ROCKSDB_LITE options_.create_missing_column_families = true; if (!FLAGS_use_txn) {