作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

341 righe
8.6 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) {
  83. return LogFileName(dbname_, number);
  84. }
  85. size_t DeleteLogFiles() {
  86. std::vector<uint64_t> logs = GetFiles(kLogFile);
  87. for (size_t i = 0; i < logs.size(); i++) {
  88. ASSERT_OK(env_->DeleteFile(LogName(logs[i]))) << LogName(logs[i]);
  89. }
  90. return logs.size();
  91. }
  92. void DeleteManifestFile() {
  93. ASSERT_OK(env_->DeleteFile(ManifestFileName()));
  94. }
  95. uint64_t FirstLogFile() {
  96. return GetFiles(kLogFile)[0];
  97. }
  98. std::vector<uint64_t> GetFiles(FileType t) {
  99. std::vector<std::string> filenames;
  100. ASSERT_OK(env_->GetChildren(dbname_, &filenames));
  101. std::vector<uint64_t> result;
  102. for (size_t i = 0; i < filenames.size(); i++) {
  103. uint64_t number;
  104. FileType type;
  105. if (ParseFileName(filenames[i], &number, &type) && type == t) {
  106. result.push_back(number);
  107. }
  108. }
  109. return result;
  110. }
  111. int NumLogs() {
  112. return GetFiles(kLogFile).size();
  113. }
  114. int NumTables() {
  115. return GetFiles(kTableFile).size();
  116. }
  117. uint64_t FileSize(const std::string& fname) {
  118. uint64_t result;
  119. ASSERT_OK(env_->GetFileSize(fname, &result)) << fname;
  120. return result;
  121. }
  122. void CompactMemTable() {
  123. dbfull()->TEST_CompactMemTable();
  124. }
  125. // Directly construct a log file that sets key to val.
  126. void MakeLogFile(uint64_t lognum, SequenceNumber seq, Slice key, Slice val) {
  127. std::string fname = LogFileName(dbname_, lognum);
  128. WritableFile* file;
  129. ASSERT_OK(env_->NewWritableFile(fname, &file));
  130. log::Writer writer(file);
  131. WriteBatch batch;
  132. batch.Put(key, val);
  133. WriteBatchInternal::SetSequence(&batch, seq);
  134. ASSERT_OK(writer.AddRecord(WriteBatchInternal::Contents(&batch)));
  135. ASSERT_OK(file->Flush());
  136. delete file;
  137. }
  138. private:
  139. std::string dbname_;
  140. Env* env_;
  141. DB* db_;
  142. };
  143. TEST(RecoveryTest, ManifestReused) {
  144. if (!CanAppend()) {
  145. fprintf(stderr, "skipping test because env does not support appending\n");
  146. return;
  147. }
  148. ASSERT_OK(Put("foo", "bar"));
  149. Close();
  150. std::string old_manifest = ManifestFileName();
  151. Open();
  152. ASSERT_EQ(old_manifest, ManifestFileName());
  153. ASSERT_EQ("bar", Get("foo"));
  154. Open();
  155. ASSERT_EQ(old_manifest, ManifestFileName());
  156. ASSERT_EQ("bar", Get("foo"));
  157. }
  158. TEST(RecoveryTest, LargeManifestCompacted) {
  159. if (!CanAppend()) {
  160. fprintf(stderr, "skipping test because env does not support appending\n");
  161. return;
  162. }
  163. ASSERT_OK(Put("foo", "bar"));
  164. Close();
  165. std::string old_manifest = ManifestFileName();
  166. // Pad with zeroes to make manifest file very big.
  167. {
  168. uint64_t len = FileSize(old_manifest);
  169. WritableFile* file;
  170. ASSERT_OK(env()->NewAppendableFile(old_manifest, &file));
  171. std::string zeroes(3*1048576 - static_cast<size_t>(len), 0);
  172. ASSERT_OK(file->Append(zeroes));
  173. ASSERT_OK(file->Flush());
  174. delete file;
  175. }
  176. Open();
  177. std::string new_manifest = ManifestFileName();
  178. ASSERT_NE(old_manifest, new_manifest);
  179. ASSERT_GT(10000, FileSize(new_manifest));
  180. ASSERT_EQ("bar", Get("foo"));
  181. Open();
  182. ASSERT_EQ(new_manifest, ManifestFileName());
  183. ASSERT_EQ("bar", Get("foo"));
  184. }
  185. TEST(RecoveryTest, NoLogFiles) {
  186. ASSERT_OK(Put("foo", "bar"));
  187. ASSERT_EQ(1, DeleteLogFiles());
  188. Open();
  189. ASSERT_EQ("NOT_FOUND", Get("foo"));
  190. Open();
  191. ASSERT_EQ("NOT_FOUND", Get("foo"));
  192. }
  193. TEST(RecoveryTest, LogFileReuse) {
  194. if (!CanAppend()) {
  195. fprintf(stderr, "skipping test because env does not support appending\n");
  196. return;
  197. }
  198. for (int i = 0; i < 2; i++) {
  199. ASSERT_OK(Put("foo", "bar"));
  200. if (i == 0) {
  201. // Compact to ensure current log is empty
  202. CompactMemTable();
  203. }
  204. Close();
  205. ASSERT_EQ(1, NumLogs());
  206. uint64_t number = FirstLogFile();
  207. if (i == 0) {
  208. ASSERT_EQ(0, FileSize(LogName(number)));
  209. } else {
  210. ASSERT_LT(0, FileSize(LogName(number)));
  211. }
  212. Open();
  213. ASSERT_EQ(1, NumLogs());
  214. ASSERT_EQ(number, FirstLogFile()) << "did not reuse log file";
  215. ASSERT_EQ("bar", Get("foo"));
  216. Open();
  217. ASSERT_EQ(1, NumLogs());
  218. ASSERT_EQ(number, FirstLogFile()) << "did not reuse log file";
  219. ASSERT_EQ("bar", Get("foo"));
  220. }
  221. }
  222. TEST(RecoveryTest, MultipleMemTables) {
  223. // Make a large log.
  224. const int kNum = 1000;
  225. for (int i = 0; i < kNum; i++) {
  226. char buf[100];
  227. snprintf(buf, sizeof(buf), "%050d", i);
  228. ASSERT_OK(Put(buf, buf));
  229. }
  230. ASSERT_EQ(0, NumTables());
  231. Close();
  232. ASSERT_EQ(0, NumTables());
  233. ASSERT_EQ(1, NumLogs());
  234. uint64_t old_log_file = FirstLogFile();
  235. // Force creation of multiple memtables by reducing the write buffer size.
  236. Options opt;
  237. opt.reuse_logs = true;
  238. opt.write_buffer_size = (kNum*100) / 2;
  239. Open(&opt);
  240. ASSERT_LE(2, NumTables());
  241. ASSERT_EQ(1, NumLogs());
  242. ASSERT_NE(old_log_file, FirstLogFile()) << "must not reuse log";
  243. for (int i = 0; i < kNum; i++) {
  244. char buf[100];
  245. snprintf(buf, sizeof(buf), "%050d", i);
  246. ASSERT_EQ(buf, Get(buf));
  247. }
  248. }
  249. TEST(RecoveryTest, MultipleLogFiles) {
  250. ASSERT_OK(Put("foo", "bar"));
  251. Close();
  252. ASSERT_EQ(1, NumLogs());
  253. // Make a bunch of uncompacted log files.
  254. uint64_t old_log = FirstLogFile();
  255. MakeLogFile(old_log+1, 1000, "hello", "world");
  256. MakeLogFile(old_log+2, 1001, "hi", "there");
  257. MakeLogFile(old_log+3, 1002, "foo", "bar2");
  258. // Recover and check that all log files were processed.
  259. Open();
  260. ASSERT_LE(1, NumTables());
  261. ASSERT_EQ(1, NumLogs());
  262. uint64_t new_log = FirstLogFile();
  263. ASSERT_LE(old_log+3, new_log);
  264. ASSERT_EQ("bar2", Get("foo"));
  265. ASSERT_EQ("world", Get("hello"));
  266. ASSERT_EQ("there", Get("hi"));
  267. // Test that previous recovery produced recoverable state.
  268. Open();
  269. ASSERT_LE(1, NumTables());
  270. ASSERT_EQ(1, NumLogs());
  271. if (CanAppend()) {
  272. ASSERT_EQ(new_log, FirstLogFile());
  273. }
  274. ASSERT_EQ("bar2", Get("foo"));
  275. ASSERT_EQ("world", Get("hello"));
  276. ASSERT_EQ("there", Get("hi"));
  277. // Check that introducing an older log file does not cause it to be re-read.
  278. Close();
  279. MakeLogFile(old_log+1, 2000, "hello", "stale write");
  280. Open();
  281. ASSERT_LE(1, NumTables());
  282. ASSERT_EQ(1, NumLogs());
  283. if (CanAppend()) {
  284. ASSERT_EQ(new_log, FirstLogFile());
  285. }
  286. ASSERT_EQ("bar2", Get("foo"));
  287. ASSERT_EQ("world", Get("hello"));
  288. ASSERT_EQ("there", Get("hi"));
  289. }
  290. TEST(RecoveryTest, ManifestMissing) {
  291. ASSERT_OK(Put("foo", "bar"));
  292. Close();
  293. DeleteManifestFile();
  294. Status status = OpenWithStatus();
  295. ASSERT_TRUE(status.IsCorruption());
  296. }
  297. } // namespace leveldb
  298. int main(int argc, char** argv) {
  299. return leveldb::test::RunAllTests();
  300. }