作者: 韩晨旭 10225101440 李畅 10225102463
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

330 rindas
8.7 KiB

  1. // Copyright (c) 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. #include "db/db_impl.h"
  5. #include "db/filename.h"
  6. #include "db/version_set.h"
  7. #include "db/write_batch_internal.h"
  8. #include "leveldb/db.h"
  9. #include "leveldb/env.h"
  10. #include "leveldb/write_batch.h"
  11. #include "util/logging.h"
  12. #include "util/testharness.h"
  13. #include "util/testutil.h"
  14. namespace leveldb {
  15. class RecoveryTest {
  16. public:
  17. RecoveryTest() : env_(Env::Default()), db_(nullptr) {
  18. dbname_ = test::TmpDir() + "/recovery_test";
  19. DestroyDB(dbname_, Options());
  20. Open();
  21. }
  22. ~RecoveryTest() {
  23. Close();
  24. DestroyDB(dbname_, Options());
  25. }
  26. DBImpl* dbfull() const { return reinterpret_cast<DBImpl*>(db_); }
  27. Env* env() const { return env_; }
  28. bool CanAppend() {
  29. WritableFile* tmp;
  30. Status s = env_->NewAppendableFile(CurrentFileName(dbname_), &tmp);
  31. delete tmp;
  32. if (s.IsNotSupportedError()) {
  33. return false;
  34. } else {
  35. return true;
  36. }
  37. }
  38. void Close() {
  39. delete db_;
  40. db_ = nullptr;
  41. }
  42. Status OpenWithStatus(Options* options = nullptr) {
  43. Close();
  44. Options opts;
  45. if (options != nullptr) {
  46. opts = *options;
  47. } else {
  48. opts.reuse_logs = true; // TODO(sanjay): test both ways
  49. opts.create_if_missing = true;
  50. }
  51. if (opts.env == nullptr) {
  52. opts.env = env_;
  53. }
  54. return DB::Open(opts, dbname_, &db_);
  55. }
  56. void Open(Options* options = nullptr) {
  57. ASSERT_OK(OpenWithStatus(options));
  58. ASSERT_EQ(1, NumLogs());
  59. }
  60. Status Put(const std::string& k, const std::string& v) {
  61. return db_->Put(WriteOptions(), k, v);
  62. }
  63. std::string Get(const std::string& k, const Snapshot* snapshot = nullptr) {
  64. std::string result;
  65. Status s = db_->Get(ReadOptions(), k, &result);
  66. if (s.IsNotFound()) {
  67. result = "NOT_FOUND";
  68. } else if (!s.ok()) {
  69. result = s.ToString();
  70. }
  71. return result;
  72. }
  73. std::string ManifestFileName() {
  74. std::string current;
  75. ASSERT_OK(ReadFileToString(env_, CurrentFileName(dbname_), &current));
  76. size_t len = current.size();
  77. if (len > 0 && current[len - 1] == '\n') {
  78. current.resize(len - 1);
  79. }
  80. return dbname_ + "/" + current;
  81. }
  82. std::string LogName(uint64_t number) { return LogFileName(dbname_, number); }
  83. size_t DeleteLogFiles() {
  84. // Linux allows unlinking open files, but Windows does not.
  85. // Closing the db allows for file deletion.
  86. Close();
  87. std::vector<uint64_t> logs = GetFiles(kLogFile);
  88. for (size_t i = 0; i < logs.size(); i++) {
  89. ASSERT_OK(env_->DeleteFile(LogName(logs[i]))) << LogName(logs[i]);
  90. }
  91. return logs.size();
  92. }
  93. void DeleteManifestFile() { ASSERT_OK(env_->DeleteFile(ManifestFileName())); }
  94. uint64_t FirstLogFile() { return GetFiles(kLogFile)[0]; }
  95. std::vector<uint64_t> GetFiles(FileType t) {
  96. std::vector<std::string> filenames;
  97. ASSERT_OK(env_->GetChildren(dbname_, &filenames));
  98. std::vector<uint64_t> result;
  99. for (size_t i = 0; i < filenames.size(); i++) {
  100. uint64_t number;
  101. FileType type;
  102. if (ParseFileName(filenames[i], &number, &type) && type == t) {
  103. result.push_back(number);
  104. }
  105. }
  106. return result;
  107. }
  108. int NumLogs() { return GetFiles(kLogFile).size(); }
  109. int NumTables() { return GetFiles(kTableFile).size(); }
  110. uint64_t FileSize(const std::string& fname) {
  111. uint64_t result;
  112. ASSERT_OK(env_->GetFileSize(fname, &result)) << fname;
  113. return result;
  114. }
  115. void CompactMemTable() { dbfull()->TEST_CompactMemTable(); }
  116. // Directly construct a log file that sets key to val.
  117. void MakeLogFile(uint64_t lognum, SequenceNumber seq, Slice key, Slice val) {
  118. std::string fname = LogFileName(dbname_, lognum);
  119. WritableFile* file;
  120. ASSERT_OK(env_->NewWritableFile(fname, &file));
  121. log::Writer writer(file);
  122. WriteBatch batch;
  123. batch.Put(key, val);
  124. WriteBatchInternal::SetSequence(&batch, seq);
  125. ASSERT_OK(writer.AddRecord(WriteBatchInternal::Contents(&batch)));
  126. ASSERT_OK(file->Flush());
  127. delete file;
  128. }
  129. private:
  130. std::string dbname_;
  131. Env* env_;
  132. DB* db_;
  133. };
  134. TEST(RecoveryTest, ManifestReused) {
  135. if (!CanAppend()) {
  136. fprintf(stderr, "skipping test because env does not support appending\n");
  137. return;
  138. }
  139. ASSERT_OK(Put("foo", "bar"));
  140. Close();
  141. std::string old_manifest = ManifestFileName();
  142. Open();
  143. ASSERT_EQ(old_manifest, ManifestFileName());
  144. ASSERT_EQ("bar", Get("foo"));
  145. Open();
  146. ASSERT_EQ(old_manifest, ManifestFileName());
  147. ASSERT_EQ("bar", Get("foo"));
  148. }
  149. TEST(RecoveryTest, LargeManifestCompacted) {
  150. if (!CanAppend()) {
  151. fprintf(stderr, "skipping test because env does not support appending\n");
  152. return;
  153. }
  154. ASSERT_OK(Put("foo", "bar"));
  155. Close();
  156. std::string old_manifest = ManifestFileName();
  157. // Pad with zeroes to make manifest file very big.
  158. {
  159. uint64_t len = FileSize(old_manifest);
  160. WritableFile* file;
  161. ASSERT_OK(env()->NewAppendableFile(old_manifest, &file));
  162. std::string zeroes(3 * 1048576 - static_cast<size_t>(len), 0);
  163. ASSERT_OK(file->Append(zeroes));
  164. ASSERT_OK(file->Flush());
  165. delete file;
  166. }
  167. Open();
  168. std::string new_manifest = ManifestFileName();
  169. ASSERT_NE(old_manifest, new_manifest);
  170. ASSERT_GT(10000, FileSize(new_manifest));
  171. ASSERT_EQ("bar", Get("foo"));
  172. Open();
  173. ASSERT_EQ(new_manifest, ManifestFileName());
  174. ASSERT_EQ("bar", Get("foo"));
  175. }
  176. TEST(RecoveryTest, NoLogFiles) {
  177. ASSERT_OK(Put("foo", "bar"));
  178. ASSERT_EQ(1, DeleteLogFiles());
  179. Open();
  180. ASSERT_EQ("NOT_FOUND", Get("foo"));
  181. Open();
  182. ASSERT_EQ("NOT_FOUND", Get("foo"));
  183. }
  184. TEST(RecoveryTest, LogFileReuse) {
  185. if (!CanAppend()) {
  186. fprintf(stderr, "skipping test because env does not support appending\n");
  187. return;
  188. }
  189. for (int i = 0; i < 2; i++) {
  190. ASSERT_OK(Put("foo", "bar"));
  191. if (i == 0) {
  192. // Compact to ensure current log is empty
  193. CompactMemTable();
  194. }
  195. Close();
  196. ASSERT_EQ(1, NumLogs());
  197. uint64_t number = FirstLogFile();
  198. if (i == 0) {
  199. ASSERT_EQ(0, FileSize(LogName(number)));
  200. } else {
  201. ASSERT_LT(0, FileSize(LogName(number)));
  202. }
  203. Open();
  204. ASSERT_EQ(1, NumLogs());
  205. ASSERT_EQ(number, FirstLogFile()) << "did not reuse log file";
  206. ASSERT_EQ("bar", Get("foo"));
  207. Open();
  208. ASSERT_EQ(1, NumLogs());
  209. ASSERT_EQ(number, FirstLogFile()) << "did not reuse log file";
  210. ASSERT_EQ("bar", Get("foo"));
  211. }
  212. }
  213. TEST(RecoveryTest, MultipleMemTables) {
  214. // Make a large log.
  215. const int kNum = 1000;
  216. for (int i = 0; i < kNum; i++) {
  217. char buf[100];
  218. snprintf(buf, sizeof(buf), "%050d", i);
  219. ASSERT_OK(Put(buf, buf));
  220. }
  221. ASSERT_EQ(0, NumTables());
  222. Close();
  223. ASSERT_EQ(0, NumTables());
  224. ASSERT_EQ(1, NumLogs());
  225. uint64_t old_log_file = FirstLogFile();
  226. // Force creation of multiple memtables by reducing the write buffer size.
  227. Options opt;
  228. opt.reuse_logs = true;
  229. opt.write_buffer_size = (kNum * 100) / 2;
  230. Open(&opt);
  231. ASSERT_LE(2, NumTables());
  232. ASSERT_EQ(1, NumLogs());
  233. ASSERT_NE(old_log_file, FirstLogFile()) << "must not reuse log";
  234. for (int i = 0; i < kNum; i++) {
  235. char buf[100];
  236. snprintf(buf, sizeof(buf), "%050d", i);
  237. ASSERT_EQ(buf, Get(buf));
  238. }
  239. }
  240. TEST(RecoveryTest, MultipleLogFiles) {
  241. ASSERT_OK(Put("foo", "bar"));
  242. Close();
  243. ASSERT_EQ(1, NumLogs());
  244. // Make a bunch of uncompacted log files.
  245. uint64_t old_log = FirstLogFile();
  246. MakeLogFile(old_log + 1, 1000, "hello", "world");
  247. MakeLogFile(old_log + 2, 1001, "hi", "there");
  248. MakeLogFile(old_log + 3, 1002, "foo", "bar2");
  249. // Recover and check that all log files were processed.
  250. Open();
  251. ASSERT_LE(1, NumTables());
  252. ASSERT_EQ(1, NumLogs());
  253. uint64_t new_log = FirstLogFile();
  254. ASSERT_LE(old_log + 3, new_log);
  255. ASSERT_EQ("bar2", Get("foo"));
  256. ASSERT_EQ("world", Get("hello"));
  257. ASSERT_EQ("there", Get("hi"));
  258. // Test that previous recovery produced recoverable state.
  259. Open();
  260. ASSERT_LE(1, NumTables());
  261. ASSERT_EQ(1, NumLogs());
  262. if (CanAppend()) {
  263. ASSERT_EQ(new_log, FirstLogFile());
  264. }
  265. ASSERT_EQ("bar2", Get("foo"));
  266. ASSERT_EQ("world", Get("hello"));
  267. ASSERT_EQ("there", Get("hi"));
  268. // Check that introducing an older log file does not cause it to be re-read.
  269. Close();
  270. MakeLogFile(old_log + 1, 2000, "hello", "stale write");
  271. Open();
  272. ASSERT_LE(1, NumTables());
  273. ASSERT_EQ(1, NumLogs());
  274. if (CanAppend()) {
  275. ASSERT_EQ(new_log, FirstLogFile());
  276. }
  277. ASSERT_EQ("bar2", Get("foo"));
  278. ASSERT_EQ("world", Get("hello"));
  279. ASSERT_EQ("there", Get("hi"));
  280. }
  281. TEST(RecoveryTest, ManifestMissing) {
  282. ASSERT_OK(Put("foo", "bar"));
  283. Close();
  284. DeleteManifestFile();
  285. Status status = OpenWithStatus();
  286. ASSERT_TRUE(status.IsCorruption());
  287. }
  288. } // namespace leveldb
  289. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }