小组成员:姚凯文(kevinyao0901),姜嘉琪
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

561 line
16 KiB

  1. // Copyright 2014 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. // This test uses a custom Env to keep track of the state of a filesystem as of
  5. // the last "sync". It then checks for data loss errors by purposely dropping
  6. // file data (or entire files) not protected by a "sync".
  7. #include <map>
  8. #include <set>
  9. #include "leveldb/db.h"
  10. #include "db/db_impl.h"
  11. #include "db/filename.h"
  12. #include "db/log_format.h"
  13. #include "db/version_set.h"
  14. #include "leveldb/cache.h"
  15. #include "leveldb/env.h"
  16. #include "leveldb/table.h"
  17. #include "leveldb/write_batch.h"
  18. #include "port/port.h"
  19. #include "port/thread_annotations.h"
  20. #include "util/logging.h"
  21. #include "util/mutexlock.h"
  22. #include "util/testharness.h"
  23. #include "util/testutil.h"
  24. namespace leveldb {
  25. static const int kValueSize = 1000;
  26. static const int kMaxNumValues = 2000;
  27. static const size_t kNumIterations = 3;
  28. class FaultInjectionTestEnv;
  29. namespace {
  30. // Assume a filename, and not a directory name like "/foo/bar/"
  31. static std::string GetDirName(const std::string& filename) {
  32. size_t found = filename.find_last_of("/\\");
  33. if (found == std::string::npos) {
  34. return "";
  35. } else {
  36. return filename.substr(0, found);
  37. }
  38. }
  39. Status SyncDir(const std::string& dir) {
  40. // As this is a test it isn't required to *actually* sync this directory.
  41. return Status::OK();
  42. }
  43. // A basic file truncation function suitable for this test.
  44. Status Truncate(const std::string& filename, uint64_t length) {
  45. leveldb::Env* env = leveldb::Env::Default();
  46. SequentialFile* orig_file;
  47. Status s = env->NewSequentialFile(filename, &orig_file);
  48. if (!s.ok())
  49. return s;
  50. char* scratch = new char[length];
  51. leveldb::Slice result;
  52. s = orig_file->Read(length, &result, scratch);
  53. delete orig_file;
  54. if (s.ok()) {
  55. std::string tmp_name = GetDirName(filename) + "/truncate.tmp";
  56. WritableFile* tmp_file;
  57. s = env->NewWritableFile(tmp_name, &tmp_file);
  58. if (s.ok()) {
  59. s = tmp_file->Append(result);
  60. delete tmp_file;
  61. if (s.ok()) {
  62. s = env->RenameFile(tmp_name, filename);
  63. } else {
  64. env->DeleteFile(tmp_name);
  65. }
  66. }
  67. }
  68. delete[] scratch;
  69. return s;
  70. }
  71. struct FileState {
  72. std::string filename_;
  73. int64_t pos_;
  74. int64_t pos_at_last_sync_;
  75. int64_t pos_at_last_flush_;
  76. FileState(const std::string& filename)
  77. : filename_(filename),
  78. pos_(-1),
  79. pos_at_last_sync_(-1),
  80. pos_at_last_flush_(-1) { }
  81. FileState() : pos_(-1), pos_at_last_sync_(-1), pos_at_last_flush_(-1) {}
  82. bool IsFullySynced() const { return pos_ <= 0 || pos_ == pos_at_last_sync_; }
  83. Status DropUnsyncedData() const;
  84. };
  85. } // anonymous namespace
  86. // A wrapper around WritableFile which informs another Env whenever this file
  87. // is written to or sync'ed.
  88. class TestWritableFile : public WritableFile {
  89. public:
  90. TestWritableFile(const FileState& state,
  91. WritableFile* f,
  92. FaultInjectionTestEnv* env);
  93. virtual ~TestWritableFile();
  94. virtual Status Append(const Slice& data);
  95. virtual Status Close();
  96. virtual Status Flush();
  97. virtual Status Sync();
  98. private:
  99. FileState state_;
  100. WritableFile* target_;
  101. bool writable_file_opened_;
  102. FaultInjectionTestEnv* env_;
  103. Status SyncParent();
  104. };
  105. class FaultInjectionTestEnv : public EnvWrapper {
  106. public:
  107. FaultInjectionTestEnv()
  108. : EnvWrapper(Env::Default()), filesystem_active_(true) {}
  109. virtual ~FaultInjectionTestEnv() { }
  110. virtual Status NewWritableFile(const std::string& fname,
  111. WritableFile** result);
  112. virtual Status NewAppendableFile(const std::string& fname,
  113. WritableFile** result);
  114. virtual Status DeleteFile(const std::string& f);
  115. virtual Status RenameFile(const std::string& s, const std::string& t);
  116. void WritableFileClosed(const FileState& state);
  117. Status DropUnsyncedFileData();
  118. Status DeleteFilesCreatedAfterLastDirSync();
  119. void DirWasSynced();
  120. bool IsFileCreatedSinceLastDirSync(const std::string& filename);
  121. void ResetState();
  122. void UntrackFile(const std::string& f);
  123. // Setting the filesystem to inactive is the test equivalent to simulating a
  124. // system reset. Setting to inactive will freeze our saved filesystem state so
  125. // that it will stop being recorded. It can then be reset back to the state at
  126. // the time of the reset.
  127. bool IsFilesystemActive() LOCKS_EXCLUDED(mutex_) {
  128. MutexLock l(&mutex_);
  129. return filesystem_active_;
  130. }
  131. void SetFilesystemActive(bool active) LOCKS_EXCLUDED(mutex_) {
  132. MutexLock l(&mutex_);
  133. filesystem_active_ = active;
  134. }
  135. private:
  136. port::Mutex mutex_;
  137. std::map<std::string, FileState> db_file_state_ GUARDED_BY(mutex_);
  138. std::set<std::string> new_files_since_last_dir_sync_ GUARDED_BY(mutex_);
  139. bool filesystem_active_ GUARDED_BY(mutex_); // Record flushes, syncs, writes
  140. };
  141. TestWritableFile::TestWritableFile(const FileState& state,
  142. WritableFile* f,
  143. FaultInjectionTestEnv* env)
  144. : state_(state),
  145. target_(f),
  146. writable_file_opened_(true),
  147. env_(env) {
  148. assert(f != nullptr);
  149. }
  150. TestWritableFile::~TestWritableFile() {
  151. if (writable_file_opened_) {
  152. Close();
  153. }
  154. delete target_;
  155. }
  156. Status TestWritableFile::Append(const Slice& data) {
  157. Status s = target_->Append(data);
  158. if (s.ok() && env_->IsFilesystemActive()) {
  159. state_.pos_ += data.size();
  160. }
  161. return s;
  162. }
  163. Status TestWritableFile::Close() {
  164. writable_file_opened_ = false;
  165. Status s = target_->Close();
  166. if (s.ok()) {
  167. env_->WritableFileClosed(state_);
  168. }
  169. return s;
  170. }
  171. Status TestWritableFile::Flush() {
  172. Status s = target_->Flush();
  173. if (s.ok() && env_->IsFilesystemActive()) {
  174. state_.pos_at_last_flush_ = state_.pos_;
  175. }
  176. return s;
  177. }
  178. Status TestWritableFile::SyncParent() {
  179. Status s = SyncDir(GetDirName(state_.filename_));
  180. if (s.ok()) {
  181. env_->DirWasSynced();
  182. }
  183. return s;
  184. }
  185. Status TestWritableFile::Sync() {
  186. if (!env_->IsFilesystemActive()) {
  187. return Status::OK();
  188. }
  189. // Ensure new files referred to by the manifest are in the filesystem.
  190. Status s = target_->Sync();
  191. if (s.ok()) {
  192. state_.pos_at_last_sync_ = state_.pos_;
  193. }
  194. if (env_->IsFileCreatedSinceLastDirSync(state_.filename_)) {
  195. Status ps = SyncParent();
  196. if (s.ok() && !ps.ok()) {
  197. s = ps;
  198. }
  199. }
  200. return s;
  201. }
  202. Status FaultInjectionTestEnv::NewWritableFile(const std::string& fname,
  203. WritableFile** result) {
  204. WritableFile* actual_writable_file;
  205. Status s = target()->NewWritableFile(fname, &actual_writable_file);
  206. if (s.ok()) {
  207. FileState state(fname);
  208. state.pos_ = 0;
  209. *result = new TestWritableFile(state, actual_writable_file, this);
  210. // NewWritableFile doesn't append to files, so if the same file is
  211. // opened again then it will be truncated - so forget our saved
  212. // state.
  213. UntrackFile(fname);
  214. MutexLock l(&mutex_);
  215. new_files_since_last_dir_sync_.insert(fname);
  216. }
  217. return s;
  218. }
  219. Status FaultInjectionTestEnv::NewAppendableFile(const std::string& fname,
  220. WritableFile** result) {
  221. WritableFile* actual_writable_file;
  222. Status s = target()->NewAppendableFile(fname, &actual_writable_file);
  223. if (s.ok()) {
  224. FileState state(fname);
  225. state.pos_ = 0;
  226. {
  227. MutexLock l(&mutex_);
  228. if (db_file_state_.count(fname) == 0) {
  229. new_files_since_last_dir_sync_.insert(fname);
  230. } else {
  231. state = db_file_state_[fname];
  232. }
  233. }
  234. *result = new TestWritableFile(state, actual_writable_file, this);
  235. }
  236. return s;
  237. }
  238. Status FaultInjectionTestEnv::DropUnsyncedFileData() {
  239. Status s;
  240. MutexLock l(&mutex_);
  241. for (std::map<std::string, FileState>::const_iterator it =
  242. db_file_state_.begin();
  243. s.ok() && it != db_file_state_.end(); ++it) {
  244. const FileState& state = it->second;
  245. if (!state.IsFullySynced()) {
  246. s = state.DropUnsyncedData();
  247. }
  248. }
  249. return s;
  250. }
  251. void FaultInjectionTestEnv::DirWasSynced() {
  252. MutexLock l(&mutex_);
  253. new_files_since_last_dir_sync_.clear();
  254. }
  255. bool FaultInjectionTestEnv::IsFileCreatedSinceLastDirSync(
  256. const std::string& filename) {
  257. MutexLock l(&mutex_);
  258. return new_files_since_last_dir_sync_.find(filename) !=
  259. new_files_since_last_dir_sync_.end();
  260. }
  261. void FaultInjectionTestEnv::UntrackFile(const std::string& f) {
  262. MutexLock l(&mutex_);
  263. db_file_state_.erase(f);
  264. new_files_since_last_dir_sync_.erase(f);
  265. }
  266. Status FaultInjectionTestEnv::DeleteFile(const std::string& f) {
  267. Status s = EnvWrapper::DeleteFile(f);
  268. ASSERT_OK(s);
  269. if (s.ok()) {
  270. UntrackFile(f);
  271. }
  272. return s;
  273. }
  274. Status FaultInjectionTestEnv::RenameFile(const std::string& s,
  275. const std::string& t) {
  276. Status ret = EnvWrapper::RenameFile(s, t);
  277. if (ret.ok()) {
  278. MutexLock l(&mutex_);
  279. if (db_file_state_.find(s) != db_file_state_.end()) {
  280. db_file_state_[t] = db_file_state_[s];
  281. db_file_state_.erase(s);
  282. }
  283. if (new_files_since_last_dir_sync_.erase(s) != 0) {
  284. assert(new_files_since_last_dir_sync_.find(t) ==
  285. new_files_since_last_dir_sync_.end());
  286. new_files_since_last_dir_sync_.insert(t);
  287. }
  288. }
  289. return ret;
  290. }
  291. void FaultInjectionTestEnv::ResetState() {
  292. // Since we are not destroying the database, the existing files
  293. // should keep their recorded synced/flushed state. Therefore
  294. // we do not reset db_file_state_ and new_files_since_last_dir_sync_.
  295. SetFilesystemActive(true);
  296. }
  297. Status FaultInjectionTestEnv::DeleteFilesCreatedAfterLastDirSync() {
  298. // Because DeleteFile access this container make a copy to avoid deadlock
  299. mutex_.Lock();
  300. std::set<std::string> new_files(new_files_since_last_dir_sync_.begin(),
  301. new_files_since_last_dir_sync_.end());
  302. mutex_.Unlock();
  303. Status s;
  304. std::set<std::string>::const_iterator it;
  305. for (it = new_files.begin(); s.ok() && it != new_files.end(); ++it) {
  306. s = DeleteFile(*it);
  307. }
  308. return s;
  309. }
  310. void FaultInjectionTestEnv::WritableFileClosed(const FileState& state) {
  311. MutexLock l(&mutex_);
  312. db_file_state_[state.filename_] = state;
  313. }
  314. Status FileState::DropUnsyncedData() const {
  315. int64_t sync_pos = pos_at_last_sync_ == -1 ? 0 : pos_at_last_sync_;
  316. return Truncate(filename_, sync_pos);
  317. }
  318. class FaultInjectionTest {
  319. public:
  320. enum ExpectedVerifResult { VAL_EXPECT_NO_ERROR, VAL_EXPECT_ERROR };
  321. enum ResetMethod { RESET_DROP_UNSYNCED_DATA, RESET_DELETE_UNSYNCED_FILES };
  322. FaultInjectionTestEnv* env_;
  323. std::string dbname_;
  324. Cache* tiny_cache_;
  325. Options options_;
  326. DB* db_;
  327. FaultInjectionTest()
  328. : env_(new FaultInjectionTestEnv),
  329. tiny_cache_(NewLRUCache(100)),
  330. db_(nullptr) {
  331. dbname_ = test::TmpDir() + "/fault_test";
  332. DestroyDB(dbname_, Options()); // Destroy any db from earlier run
  333. options_.reuse_logs = true;
  334. options_.env = env_;
  335. options_.paranoid_checks = true;
  336. options_.block_cache = tiny_cache_;
  337. options_.create_if_missing = true;
  338. }
  339. ~FaultInjectionTest() {
  340. CloseDB();
  341. DestroyDB(dbname_, Options());
  342. delete tiny_cache_;
  343. delete env_;
  344. }
  345. void ReuseLogs(bool reuse) {
  346. options_.reuse_logs = reuse;
  347. }
  348. void Build(int start_idx, int num_vals) {
  349. std::string key_space, value_space;
  350. WriteBatch batch;
  351. for (int i = start_idx; i < start_idx + num_vals; i++) {
  352. Slice key = Key(i, &key_space);
  353. batch.Clear();
  354. batch.Put(key, Value(i, &value_space));
  355. WriteOptions options;
  356. ASSERT_OK(db_->Write(options, &batch));
  357. }
  358. }
  359. Status ReadValue(int i, std::string* val) const {
  360. std::string key_space, value_space;
  361. Slice key = Key(i, &key_space);
  362. Value(i, &value_space);
  363. ReadOptions options;
  364. return db_->Get(options, key, val);
  365. }
  366. Status Verify(int start_idx, int num_vals,
  367. ExpectedVerifResult expected) const {
  368. std::string val;
  369. std::string value_space;
  370. Status s;
  371. for (int i = start_idx; i < start_idx + num_vals && s.ok(); i++) {
  372. Value(i, &value_space);
  373. s = ReadValue(i, &val);
  374. if (expected == VAL_EXPECT_NO_ERROR) {
  375. if (s.ok()) {
  376. ASSERT_EQ(value_space, val);
  377. }
  378. } else if (s.ok()) {
  379. fprintf(stderr, "Expected an error at %d, but was OK\n", i);
  380. s = Status::IOError(dbname_, "Expected value error:");
  381. } else {
  382. s = Status::OK(); // An expected error
  383. }
  384. }
  385. return s;
  386. }
  387. // Return the ith key
  388. Slice Key(int i, std::string* storage) const {
  389. char buf[100];
  390. snprintf(buf, sizeof(buf), "%016d", i);
  391. storage->assign(buf, strlen(buf));
  392. return Slice(*storage);
  393. }
  394. // Return the value to associate with the specified key
  395. Slice Value(int k, std::string* storage) const {
  396. Random r(k);
  397. return test::RandomString(&r, kValueSize, storage);
  398. }
  399. Status OpenDB() {
  400. delete db_;
  401. db_ = nullptr;
  402. env_->ResetState();
  403. return DB::Open(options_, dbname_, &db_);
  404. }
  405. void CloseDB() {
  406. delete db_;
  407. db_ = nullptr;
  408. }
  409. void DeleteAllData() {
  410. Iterator* iter = db_->NewIterator(ReadOptions());
  411. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  412. ASSERT_OK(db_->Delete(WriteOptions(), iter->key()));
  413. }
  414. delete iter;
  415. }
  416. void ResetDBState(ResetMethod reset_method) {
  417. switch (reset_method) {
  418. case RESET_DROP_UNSYNCED_DATA:
  419. ASSERT_OK(env_->DropUnsyncedFileData());
  420. break;
  421. case RESET_DELETE_UNSYNCED_FILES:
  422. ASSERT_OK(env_->DeleteFilesCreatedAfterLastDirSync());
  423. break;
  424. default:
  425. assert(false);
  426. }
  427. }
  428. void PartialCompactTestPreFault(int num_pre_sync, int num_post_sync) {
  429. DeleteAllData();
  430. Build(0, num_pre_sync);
  431. db_->CompactRange(nullptr, nullptr);
  432. Build(num_pre_sync, num_post_sync);
  433. }
  434. void PartialCompactTestReopenWithFault(ResetMethod reset_method,
  435. int num_pre_sync,
  436. int num_post_sync) {
  437. env_->SetFilesystemActive(false);
  438. CloseDB();
  439. ResetDBState(reset_method);
  440. ASSERT_OK(OpenDB());
  441. ASSERT_OK(Verify(0, num_pre_sync, FaultInjectionTest::VAL_EXPECT_NO_ERROR));
  442. ASSERT_OK(Verify(num_pre_sync, num_post_sync, FaultInjectionTest::VAL_EXPECT_ERROR));
  443. }
  444. void NoWriteTestPreFault() {
  445. }
  446. void NoWriteTestReopenWithFault(ResetMethod reset_method) {
  447. CloseDB();
  448. ResetDBState(reset_method);
  449. ASSERT_OK(OpenDB());
  450. }
  451. void DoTest() {
  452. Random rnd(0);
  453. ASSERT_OK(OpenDB());
  454. for (size_t idx = 0; idx < kNumIterations; idx++) {
  455. int num_pre_sync = rnd.Uniform(kMaxNumValues);
  456. int num_post_sync = rnd.Uniform(kMaxNumValues);
  457. PartialCompactTestPreFault(num_pre_sync, num_post_sync);
  458. PartialCompactTestReopenWithFault(RESET_DROP_UNSYNCED_DATA,
  459. num_pre_sync,
  460. num_post_sync);
  461. NoWriteTestPreFault();
  462. NoWriteTestReopenWithFault(RESET_DROP_UNSYNCED_DATA);
  463. PartialCompactTestPreFault(num_pre_sync, num_post_sync);
  464. // No new files created so we expect all values since no files will be
  465. // dropped.
  466. PartialCompactTestReopenWithFault(RESET_DELETE_UNSYNCED_FILES,
  467. num_pre_sync + num_post_sync,
  468. 0);
  469. NoWriteTestPreFault();
  470. NoWriteTestReopenWithFault(RESET_DELETE_UNSYNCED_FILES);
  471. }
  472. }
  473. };
  474. TEST(FaultInjectionTest, FaultTestNoLogReuse) {
  475. ReuseLogs(false);
  476. DoTest();
  477. }
  478. TEST(FaultInjectionTest, FaultTestWithLogReuse) {
  479. ReuseLogs(true);
  480. DoTest();
  481. }
  482. } // namespace leveldb
  483. int main(int argc, char** argv) {
  484. return leveldb::test::RunAllTests();
  485. }