提供基本的ttl测试用例
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.

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