作者: 谢瑞阳 10225101483 徐翔宇 10225101535
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.

374 regels
9.9 KiB

  1. // Copyright (c) 2011 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 "leveldb/db.h"
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include "leveldb/cache.h"
  10. #include "leveldb/env.h"
  11. #include "leveldb/table.h"
  12. #include "leveldb/write_batch.h"
  13. #include "db/db_impl.h"
  14. #include "db/filename.h"
  15. #include "db/log_format.h"
  16. #include "db/version_set.h"
  17. #include "util/logging.h"
  18. #include "util/testharness.h"
  19. #include "util/testutil.h"
  20. namespace leveldb {
  21. static const int kValueSize = 1000;
  22. class CorruptionTest {
  23. public:
  24. test::ErrorEnv env_;
  25. std::string dbname_;
  26. Cache* tiny_cache_;
  27. Options options_;
  28. DB* db_;
  29. CorruptionTest() {
  30. tiny_cache_ = NewLRUCache(100);
  31. options_.env = &env_;
  32. options_.block_cache = tiny_cache_;
  33. dbname_ = test::TmpDir() + "/corruption_test";
  34. DestroyDB(dbname_, options_);
  35. db_ = NULL;
  36. options_.create_if_missing = true;
  37. Reopen();
  38. options_.create_if_missing = false;
  39. }
  40. ~CorruptionTest() {
  41. delete db_;
  42. DestroyDB(dbname_, Options());
  43. delete tiny_cache_;
  44. }
  45. Status TryReopen() {
  46. delete db_;
  47. db_ = NULL;
  48. return DB::Open(options_, dbname_, &db_);
  49. }
  50. void Reopen() {
  51. ASSERT_OK(TryReopen());
  52. }
  53. void RepairDB() {
  54. delete db_;
  55. db_ = NULL;
  56. ASSERT_OK(::leveldb::RepairDB(dbname_, options_));
  57. }
  58. void Build(int n) {
  59. std::string key_space, value_space;
  60. WriteBatch batch;
  61. for (int i = 0; i < n; i++) {
  62. //if ((i % 100) == 0) fprintf(stderr, "@ %d of %d\n", i, n);
  63. Slice key = Key(i, &key_space);
  64. batch.Clear();
  65. batch.Put(key, Value(i, &value_space));
  66. WriteOptions options;
  67. // Corrupt() doesn't work without this sync on windows; stat reports 0 for
  68. // the file size.
  69. if (i == n - 1) {
  70. options.sync = true;
  71. }
  72. ASSERT_OK(db_->Write(options, &batch));
  73. }
  74. }
  75. void Check(int min_expected, int max_expected) {
  76. int next_expected = 0;
  77. int missed = 0;
  78. int bad_keys = 0;
  79. int bad_values = 0;
  80. int correct = 0;
  81. std::string value_space;
  82. Iterator* iter = db_->NewIterator(ReadOptions());
  83. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  84. uint64_t key;
  85. Slice in(iter->key());
  86. if (in == "" || in == "~") {
  87. // Ignore boundary keys.
  88. continue;
  89. }
  90. if (!ConsumeDecimalNumber(&in, &key) ||
  91. !in.empty() ||
  92. key < next_expected) {
  93. bad_keys++;
  94. continue;
  95. }
  96. missed += (key - next_expected);
  97. next_expected = key + 1;
  98. if (iter->value() != Value(key, &value_space)) {
  99. bad_values++;
  100. } else {
  101. correct++;
  102. }
  103. }
  104. delete iter;
  105. fprintf(stderr,
  106. "expected=%d..%d; got=%d; bad_keys=%d; bad_values=%d; missed=%d\n",
  107. min_expected, max_expected, correct, bad_keys, bad_values, missed);
  108. ASSERT_LE(min_expected, correct);
  109. ASSERT_GE(max_expected, correct);
  110. }
  111. void Corrupt(FileType filetype, int offset, int bytes_to_corrupt) {
  112. // Pick file to corrupt
  113. std::vector<std::string> filenames;
  114. ASSERT_OK(env_.GetChildren(dbname_, &filenames));
  115. uint64_t number;
  116. FileType type;
  117. std::string fname;
  118. int picked_number = -1;
  119. for (size_t i = 0; i < filenames.size(); i++) {
  120. if (ParseFileName(filenames[i], &number, &type) &&
  121. type == filetype &&
  122. int(number) > picked_number) { // Pick latest file
  123. fname = dbname_ + "/" + filenames[i];
  124. picked_number = number;
  125. }
  126. }
  127. ASSERT_TRUE(!fname.empty()) << filetype;
  128. struct stat sbuf;
  129. if (stat(fname.c_str(), &sbuf) != 0) {
  130. const char* msg = strerror(errno);
  131. ASSERT_TRUE(false) << fname << ": " << msg;
  132. }
  133. if (offset < 0) {
  134. // Relative to end of file; make it absolute
  135. if (-offset > sbuf.st_size) {
  136. offset = 0;
  137. } else {
  138. offset = sbuf.st_size + offset;
  139. }
  140. }
  141. if (offset > sbuf.st_size) {
  142. offset = sbuf.st_size;
  143. }
  144. if (offset + bytes_to_corrupt > sbuf.st_size) {
  145. bytes_to_corrupt = sbuf.st_size - offset;
  146. }
  147. // Do it
  148. std::string contents;
  149. Status s = ReadFileToString(Env::Default(), fname, &contents);
  150. ASSERT_TRUE(s.ok()) << s.ToString();
  151. for (int i = 0; i < bytes_to_corrupt; i++) {
  152. contents[i + offset] ^= 0x80;
  153. }
  154. s = WriteStringToFile(Env::Default(), contents, fname);
  155. ASSERT_TRUE(s.ok()) << s.ToString();
  156. }
  157. int Property(const std::string& name) {
  158. std::string property;
  159. int result;
  160. if (db_->GetProperty(name, &property) &&
  161. sscanf(property.c_str(), "%d", &result) == 1) {
  162. return result;
  163. } else {
  164. return -1;
  165. }
  166. }
  167. // Return the ith key
  168. Slice Key(int i, std::string* storage) {
  169. char buf[100];
  170. snprintf(buf, sizeof(buf), "%016d", i);
  171. storage->assign(buf, strlen(buf));
  172. return Slice(*storage);
  173. }
  174. // Return the value to associate with the specified key
  175. Slice Value(int k, std::string* storage) {
  176. Random r(k);
  177. return test::RandomString(&r, kValueSize, storage);
  178. }
  179. };
  180. TEST(CorruptionTest, Recovery) {
  181. Build(100);
  182. Check(100, 100);
  183. Corrupt(kLogFile, 19, 1); // WriteBatch tag for first record
  184. Corrupt(kLogFile, log::kBlockSize + 1000, 1); // Somewhere in second block
  185. Reopen();
  186. // The 64 records in the first two log blocks are completely lost.
  187. Check(36, 36);
  188. }
  189. TEST(CorruptionTest, RecoverWriteError) {
  190. env_.writable_file_error_ = true;
  191. Status s = TryReopen();
  192. ASSERT_TRUE(!s.ok());
  193. }
  194. TEST(CorruptionTest, NewFileErrorDuringWrite) {
  195. // Do enough writing to force minor compaction
  196. env_.writable_file_error_ = true;
  197. const int num = 3 + (Options().write_buffer_size / kValueSize);
  198. std::string value_storage;
  199. Status s;
  200. for (int i = 0; s.ok() && i < num; i++) {
  201. WriteBatch batch;
  202. batch.Put("a", Value(100, &value_storage));
  203. s = db_->Write(WriteOptions(), &batch);
  204. }
  205. ASSERT_TRUE(!s.ok());
  206. ASSERT_GE(env_.num_writable_file_errors_, 1);
  207. env_.writable_file_error_ = false;
  208. Reopen();
  209. }
  210. TEST(CorruptionTest, TableFile) {
  211. Build(100);
  212. DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
  213. dbi->TEST_CompactMemTable();
  214. dbi->TEST_CompactRange(0, NULL, NULL);
  215. dbi->TEST_CompactRange(1, NULL, NULL);
  216. Corrupt(kTableFile, 100, 1);
  217. Check(90, 99);
  218. }
  219. TEST(CorruptionTest, TableFileRepair) {
  220. options_.block_size = 2 * kValueSize; // Limit scope of corruption
  221. options_.paranoid_checks = true;
  222. Reopen();
  223. Build(100);
  224. DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
  225. dbi->TEST_CompactMemTable();
  226. dbi->TEST_CompactRange(0, NULL, NULL);
  227. dbi->TEST_CompactRange(1, NULL, NULL);
  228. Corrupt(kTableFile, 100, 1);
  229. RepairDB();
  230. Reopen();
  231. Check(95, 99);
  232. }
  233. TEST(CorruptionTest, TableFileIndexData) {
  234. Build(10000); // Enough to build multiple Tables
  235. DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
  236. dbi->TEST_CompactMemTable();
  237. Corrupt(kTableFile, -2000, 500);
  238. Reopen();
  239. Check(5000, 9999);
  240. }
  241. TEST(CorruptionTest, MissingDescriptor) {
  242. Build(1000);
  243. RepairDB();
  244. Reopen();
  245. Check(1000, 1000);
  246. }
  247. TEST(CorruptionTest, SequenceNumberRecovery) {
  248. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v1"));
  249. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v2"));
  250. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v3"));
  251. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v4"));
  252. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v5"));
  253. RepairDB();
  254. Reopen();
  255. std::string v;
  256. ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
  257. ASSERT_EQ("v5", v);
  258. // Write something. If sequence number was not recovered properly,
  259. // it will be hidden by an earlier write.
  260. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v6"));
  261. ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
  262. ASSERT_EQ("v6", v);
  263. Reopen();
  264. ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
  265. ASSERT_EQ("v6", v);
  266. }
  267. TEST(CorruptionTest, CorruptedDescriptor) {
  268. ASSERT_OK(db_->Put(WriteOptions(), "foo", "hello"));
  269. DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
  270. dbi->TEST_CompactMemTable();
  271. dbi->TEST_CompactRange(0, NULL, NULL);
  272. Corrupt(kDescriptorFile, 0, 1000);
  273. Status s = TryReopen();
  274. ASSERT_TRUE(!s.ok());
  275. RepairDB();
  276. Reopen();
  277. std::string v;
  278. ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
  279. ASSERT_EQ("hello", v);
  280. }
  281. TEST(CorruptionTest, CompactionInputError) {
  282. Build(10);
  283. DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
  284. dbi->TEST_CompactMemTable();
  285. const int last = config::kMaxMemCompactLevel;
  286. ASSERT_EQ(1, Property("leveldb.num-files-at-level" + NumberToString(last)));
  287. Corrupt(kTableFile, 100, 1);
  288. Check(5, 9);
  289. // Force compactions by writing lots of values
  290. Build(10000);
  291. Check(10000, 10000);
  292. }
  293. TEST(CorruptionTest, CompactionInputErrorParanoid) {
  294. options_.paranoid_checks = true;
  295. options_.write_buffer_size = 512 << 10;
  296. Reopen();
  297. DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
  298. // Make multiple inputs so we need to compact.
  299. for (int i = 0; i < 2; i++) {
  300. Build(10);
  301. dbi->TEST_CompactMemTable();
  302. Corrupt(kTableFile, 100, 1);
  303. env_.SleepForMicroseconds(100000);
  304. }
  305. dbi->CompactRange(NULL, NULL);
  306. // Write must fail because of corrupted table
  307. std::string tmp1, tmp2;
  308. Status s = db_->Put(WriteOptions(), Key(5, &tmp1), Value(5, &tmp2));
  309. ASSERT_TRUE(!s.ok()) << "write did not fail in corrupted paranoid db";
  310. }
  311. TEST(CorruptionTest, UnrelatedKeys) {
  312. Build(10);
  313. DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
  314. dbi->TEST_CompactMemTable();
  315. Corrupt(kTableFile, 100, 1);
  316. std::string tmp1, tmp2;
  317. ASSERT_OK(db_->Put(WriteOptions(), Key(1000, &tmp1), Value(1000, &tmp2)));
  318. std::string v;
  319. ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
  320. ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
  321. dbi->TEST_CompactMemTable();
  322. ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
  323. ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
  324. }
  325. } // namespace leveldb
  326. int main(int argc, char** argv) {
  327. return leveldb::test::RunAllTests();
  328. }