From 6422356a270329c740304c2b6209be897249cd9d Mon Sep 17 00:00:00 2001 From: Chinmay Kamat Date: Fri, 12 Oct 2018 10:40:06 -0700 Subject: [PATCH] Acquire lock on DB LOCK file before starting repair. (#4435) Summary: This commit adds code to acquire lock on the DB LOCK file before starting the repair process. This will prevent multiple processes from performing repair on the same DB simultaneously. Fixes repair_test to work with this change. Pull Request resolved: https://github.com/facebook/rocksdb/pull/4435 Differential Revision: D10361499 Pulled By: riversand963 fbshipit-source-id: 3c512c48b7193d383b2279ccecabdb660ac1cf22 --- db/repair.cc | 12 +++++++++++- db/repair_test.cc | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/db/repair.cc b/db/repair.cc index 195689418..a856de0eb 100644 --- a/db/repair.cc +++ b/db/repair.cc @@ -163,11 +163,18 @@ class Repairer { } ~Repairer() { + if (db_lock_ != nullptr) { + env_->UnlockFile(db_lock_); + } delete table_cache_; } Status Run() { - Status status = FindFiles(); + Status status = env_->LockFile(LockFileName(dbname_), &db_lock_); + if (!status.ok()) { + return status; + } + status = FindFiles(); if (status.ok()) { // Discard older manifests and start a fresh one for (size_t i = 0; i < manifests_.size(); i++) { @@ -245,6 +252,9 @@ class Repairer { std::vector logs_; std::vector tables_; uint64_t next_file_number_; + // Lock over the persistent DB state. Non-nullptr iff successfully + // acquired. + FileLock* db_lock_; Status FindFiles() { std::vector filenames; diff --git a/db/repair_test.cc b/db/repair_test.cc index 72abd62a5..3422532da 100644 --- a/db/repair_test.cc +++ b/db/repair_test.cc @@ -313,6 +313,7 @@ TEST_F(RepairTest, RepairColumnFamilyOptions) { ASSERT_EQ(comparator_name, fname_and_props.second->comparator_name); } + Close(); // Also check comparator when it's provided via "unknown" CF options ASSERT_OK(RepairDB(dbname_, opts, {{"default", opts}},