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.
5 #include "leveldb/db.h"
10 #include <sys/types.h>
11 #include "leveldb/cache.h"
12 #include "leveldb/env.h"
13 #include "leveldb/table.h"
14 #include "leveldb/write_batch.h"
15 #include "db/db_impl.h"
16 #include "db/filename.h"
17 #include "db/log_format.h"
18 #include "db/version_set.h"
19 #include "util/logging.h"
20 #include "util/testharness.h"
21 #include "util/testutil.h"
25 static const int kValueSize = 1000;
27 class CorruptionTest {
36 tiny_cache_ = NewLRUCache(100);
38 options_.block_cache = tiny_cache_;
39 dbname_ = test::TmpDir() + "/db_test";
40 DestroyDB(dbname_, options_);
43 options_.create_if_missing = true;
45 options_.create_if_missing = false;
50 DestroyDB(dbname_, Options());
57 return DB::Open(options_, dbname_, &db_);
61 ASSERT_OK(TryReopen());
67 ASSERT_OK(::leveldb::RepairDB(dbname_, options_));
71 std::string key_space, value_space;
73 for (int i = 0; i < n; i++) {
74 //if ((i % 100) == 0) fprintf(stderr, "@ %d of %d\n", i, n);
75 Slice key = Key(i, &key_space);
77 batch.Put(key, Value(i, &value_space));
79 // Corrupt() doesn't work without this sync on windows; stat reports 0 for
84 ASSERT_OK(db_->Write(options, &batch));
88 void Check(int min_expected, int max_expected) {
89 int next_expected = 0;
94 std::string value_space;
95 Iterator* iter = db_->NewIterator(ReadOptions());
96 for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
98 Slice in(iter->key());
99 if (in == "" || in == "~") {
100 // Ignore boundary keys.
103 if (!ConsumeDecimalNumber(&in, &key) ||
105 key < next_expected) {
109 missed += (key - next_expected);
110 next_expected = key + 1;
111 if (iter->value() != Value(key, &value_space)) {
120 "expected=%d..%d; got=%d; bad_keys=%d; bad_values=%d; missed=%d\n",
121 min_expected, max_expected, correct, bad_keys, bad_values, missed);
122 ASSERT_LE(min_expected, correct);
123 ASSERT_GE(max_expected, correct);
126 void Corrupt(FileType filetype, int offset, int bytes_to_corrupt) {
127 // Pick file to corrupt
128 std::vector<std::string> filenames;
129 ASSERT_OK(env_.GetChildren(dbname_, &filenames));
133 int picked_number = -1;
134 for (size_t i = 0; i < filenames.size(); i++) {
135 if (ParseFileName(filenames[i], &number, &type) &&
137 int(number) > picked_number) { // Pick latest file
138 fname = dbname_ + "/" + filenames[i];
139 picked_number = number;
142 ASSERT_TRUE(!fname.empty()) << filetype;
145 if (stat(fname.c_str(), &sbuf) != 0) {
146 const char* msg = strerror(errno);
147 ASSERT_TRUE(false) << fname << ": " << msg;
151 // Relative to end of file; make it absolute
152 if (-offset > sbuf.st_size) {
155 offset = sbuf.st_size + offset;
158 if (offset > sbuf.st_size) {
159 offset = sbuf.st_size;
161 if (offset + bytes_to_corrupt > sbuf.st_size) {
162 bytes_to_corrupt = sbuf.st_size - offset;
166 std::string contents;
167 Status s = ReadFileToString(Env::Default(), fname, &contents);
168 ASSERT_TRUE(s.ok()) << s.ToString();
169 for (int i = 0; i < bytes_to_corrupt; i++) {
170 contents[i + offset] ^= 0x80;
172 s = WriteStringToFile(Env::Default(), contents, fname);
173 ASSERT_TRUE(s.ok()) << s.ToString();
176 int Property(const std::string& name) {
177 std::string property;
179 if (db_->GetProperty(name, &property) &&
180 sscanf(property.c_str(), "%d", &result) == 1) {
187 // Return the ith key
188 Slice Key(int i, std::string* storage) {
190 snprintf(buf, sizeof(buf), "%016d", i);
191 storage->assign(buf, strlen(buf));
192 return Slice(*storage);
195 // Return the value to associate with the specified key
196 Slice Value(int k, std::string* storage) {
198 return test::RandomString(&r, kValueSize, storage);
202 TEST(CorruptionTest, Recovery) {
205 Corrupt(kLogFile, 19, 1); // WriteBatch tag for first record
206 Corrupt(kLogFile, log::kBlockSize + 1000, 1); // Somewhere in second block
209 // The 64 records in the first two log blocks are completely lost.
213 TEST(CorruptionTest, RecoverWriteError) {
214 env_.writable_file_error_ = true;
215 Status s = TryReopen();
216 ASSERT_TRUE(!s.ok());
219 TEST(CorruptionTest, NewFileErrorDuringWrite) {
220 // Do enough writing to force minor compaction
221 env_.writable_file_error_ = true;
222 const int num = 3 + (Options().write_buffer_size / kValueSize);
223 std::string value_storage;
225 for (int i = 0; s.ok() && i < num; i++) {
227 batch.Put("a", Value(100, &value_storage));
228 s = db_->Write(WriteOptions(), &batch);
230 ASSERT_TRUE(!s.ok());
231 ASSERT_GE(env_.num_writable_file_errors_, 1);
232 env_.writable_file_error_ = false;
236 TEST(CorruptionTest, TableFile) {
238 DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
239 dbi->TEST_CompactMemTable();
240 dbi->TEST_CompactRange(0, NULL, NULL);
241 dbi->TEST_CompactRange(1, NULL, NULL);
243 Corrupt(kTableFile, 100, 1);
247 TEST(CorruptionTest, TableFileRepair) {
248 options_.block_size = 2 * kValueSize; // Limit scope of corruption
249 options_.paranoid_checks = true;
252 DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
253 dbi->TEST_CompactMemTable();
254 dbi->TEST_CompactRange(0, NULL, NULL);
255 dbi->TEST_CompactRange(1, NULL, NULL);
257 Corrupt(kTableFile, 100, 1);
263 TEST(CorruptionTest, TableFileIndexData) {
264 Build(10000); // Enough to build multiple Tables
265 DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
266 dbi->TEST_CompactMemTable();
268 Corrupt(kTableFile, -2000, 500);
273 TEST(CorruptionTest, MissingDescriptor) {
280 TEST(CorruptionTest, SequenceNumberRecovery) {
281 ASSERT_OK(db_->Put(WriteOptions(), "foo", "v1"));
282 ASSERT_OK(db_->Put(WriteOptions(), "foo", "v2"));
283 ASSERT_OK(db_->Put(WriteOptions(), "foo", "v3"));
284 ASSERT_OK(db_->Put(WriteOptions(), "foo", "v4"));
285 ASSERT_OK(db_->Put(WriteOptions(), "foo", "v5"));
289 ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
291 // Write something. If sequence number was not recovered properly,
292 // it will be hidden by an earlier write.
293 ASSERT_OK(db_->Put(WriteOptions(), "foo", "v6"));
294 ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
297 ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
301 TEST(CorruptionTest, CorruptedDescriptor) {
302 ASSERT_OK(db_->Put(WriteOptions(), "foo", "hello"));
303 DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
304 dbi->TEST_CompactMemTable();
305 dbi->TEST_CompactRange(0, NULL, NULL);
307 Corrupt(kDescriptorFile, 0, 1000);
308 Status s = TryReopen();
309 ASSERT_TRUE(!s.ok());
314 ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
315 ASSERT_EQ("hello", v);
318 TEST(CorruptionTest, CompactionInputError) {
320 DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
321 dbi->TEST_CompactMemTable();
322 const int last = config::kMaxMemCompactLevel;
323 ASSERT_EQ(1, Property("leveldb.num-files-at-level" + NumberToString(last)));
325 Corrupt(kTableFile, 100, 1);
328 // Force compactions by writing lots of values
333 TEST(CorruptionTest, CompactionInputErrorParanoid) {
334 options_.paranoid_checks = true;
335 options_.write_buffer_size = 512 << 10;
337 DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
339 // Make multiple inputs so we need to compact.
340 for (int i = 0; i < 2; i++) {
342 dbi->TEST_CompactMemTable();
343 Corrupt(kTableFile, 100, 1);
344 env_.SleepForMicroseconds(100000);
346 dbi->CompactRange(NULL, NULL);
348 // Write must fail because of corrupted table
349 std::string tmp1, tmp2;
350 Status s = db_->Put(WriteOptions(), Key(5, &tmp1), Value(5, &tmp2));
351 ASSERT_TRUE(!s.ok()) << "write did not fail in corrupted paranoid db";
354 TEST(CorruptionTest, UnrelatedKeys) {
356 DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
357 dbi->TEST_CompactMemTable();
358 Corrupt(kTableFile, 100, 1);
360 std::string tmp1, tmp2;
361 ASSERT_OK(db_->Put(WriteOptions(), Key(1000, &tmp1), Value(1000, &tmp2)));
363 ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
364 ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
365 dbi->TEST_CompactMemTable();
366 ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
367 ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
370 } // namespace leveldb
372 int main(int argc, char** argv) {
373 return leveldb::test::RunAllTests();