小组成员:10215300402-朱维清 & 10222140408 谷杰
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.

2305 lines
68 KiB

Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
преди 4 години
Release 1.18 Changes are: * Update version number to 1.18 * Replace the basic fprintf call with a call to fwrite in order to work around the apparent compiler optimization/rewrite failure that we are seeing with the new toolchain/iOS SDKs provided with Xcode6 and iOS8. * Fix ALL the header guards. * Createed a README.md with the LevelDB project description. * A new CONTRIBUTING file. * Don't implicitly convert uint64_t to size_t or int. Either preserve it as uint64_t, or explicitly cast. This fixes MSVC warnings about possible value truncation when compiling this code in Chromium. * Added a DumpFile() library function that encapsulates the guts of the "leveldbutil dump" command. This will allow clients to dump data to their log files instead of stdout. It will also allow clients to supply their own environment. * leveldb: Remove unused function 'ConsumeChar'. * leveldbutil: Remove unused member variables from WriteBatchItemPrinter. * OpenBSD, NetBSD and DragonflyBSD have _LITTLE_ENDIAN, so define PLATFORM_IS_LITTLE_ENDIAN like on FreeBSD. This fixes: * issue #143 * issue #198 * issue #249 * Switch from <cstdatomic> to <atomic>. The former never made it into the standard and doesn't exist in modern gcc versions at all. The later contains everything that leveldb was using from the former. This problem was noticed when porting to Portable Native Client where no memory barrier is defined. The fact that <cstdatomic> is missing normally goes unnoticed since memory barriers are defined for most architectures. * Make Hash() treat its input as unsigned. Before this change LevelDB files from platforms with different signedness of char were not compatible. This change fixes: issue #243 * Verify checksums of index/meta/filter blocks when paranoid_checks set. * Invoke all tools for iOS with xcrun. (This was causing problems with the new XCode 5.1.1 image on pulse.) * include <sys/stat.h> only once, and fix the following linter warning: "Found C system header after C++ system header" * When encountering a corrupted table file, return Status::Corruption instead of Status::InvalidArgument. * Support cygwin as build platform, patch is from https://code.google.com/p/leveldb/issues/detail?id=188 * Fix typo, merge patch from https://code.google.com/p/leveldb/issues/detail?id=159 * Fix typos and comments, and address the following two issues: * issue #166 * issue #241 * Add missing db synchronize after "fillseq" in the benchmark. * Removed unused variable in SeekRandom: value (issue #201)
преди 10 години
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
преди 4 години
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
преди 4 години
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
преди 4 години
  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 <atomic>
  6. #include <string>
  7. #include "gtest/gtest.h"
  8. #include "db/db_impl.h"
  9. #include "db/filename.h"
  10. #include "db/version_set.h"
  11. #include "db/write_batch_internal.h"
  12. #include "leveldb/cache.h"
  13. #include "leveldb/env.h"
  14. #include "leveldb/filter_policy.h"
  15. #include "leveldb/table.h"
  16. #include "port/port.h"
  17. #include "port/thread_annotations.h"
  18. #include "util/hash.h"
  19. #include "util/logging.h"
  20. #include "util/mutexlock.h"
  21. #include "util/testutil.h"
  22. namespace leveldb {
  23. static std::string RandomString(Random* rnd, int len) {
  24. std::string r;
  25. test::RandomString(rnd, len, &r);
  26. return r;
  27. }
  28. static std::string RandomKey(Random* rnd) {
  29. int len =
  30. (rnd->OneIn(3) ? 1 // Short sometimes to encourage collisions
  31. : (rnd->OneIn(100) ? rnd->Skewed(10) : rnd->Uniform(10)));
  32. return test::RandomKey(rnd, len);
  33. }
  34. namespace {
  35. class AtomicCounter {
  36. public:
  37. AtomicCounter() : count_(0) {}
  38. void Increment() { IncrementBy(1); }
  39. void IncrementBy(int count) LOCKS_EXCLUDED(mu_) {
  40. MutexLock l(&mu_);
  41. count_ += count;
  42. }
  43. int Read() LOCKS_EXCLUDED(mu_) {
  44. MutexLock l(&mu_);
  45. return count_;
  46. }
  47. void Reset() LOCKS_EXCLUDED(mu_) {
  48. MutexLock l(&mu_);
  49. count_ = 0;
  50. }
  51. private:
  52. port::Mutex mu_;
  53. int count_ GUARDED_BY(mu_);
  54. };
  55. void DelayMilliseconds(int millis) {
  56. Env::Default()->SleepForMicroseconds(millis * 1000);
  57. }
  58. } // namespace
  59. // Test Env to override default Env behavior for testing.
  60. class TestEnv : public EnvWrapper {
  61. public:
  62. explicit TestEnv(Env* base) : EnvWrapper(base), ignore_dot_files_(false) {}
  63. void SetIgnoreDotFiles(bool ignored) { ignore_dot_files_ = ignored; }
  64. Status GetChildren(const std::string& dir,
  65. std::vector<std::string>* result) override {
  66. Status s = target()->GetChildren(dir, result);
  67. if (!s.ok() || !ignore_dot_files_) {
  68. return s;
  69. }
  70. std::vector<std::string>::iterator it = result->begin();
  71. while (it != result->end()) {
  72. if ((*it == ".") || (*it == "..")) {
  73. it = result->erase(it);
  74. } else {
  75. ++it;
  76. }
  77. }
  78. return s;
  79. }
  80. private:
  81. bool ignore_dot_files_;
  82. };
  83. // Special Env used to delay background operations.
  84. class SpecialEnv : public EnvWrapper {
  85. public:
  86. // sstable/log Sync() calls are blocked while this pointer is non-null.
  87. std::atomic<bool> delay_data_sync_;
  88. // sstable/log Sync() calls return an error.
  89. std::atomic<bool> data_sync_error_;
  90. // Simulate no-space errors while this pointer is non-null.
  91. std::atomic<bool> no_space_;
  92. // Simulate non-writable file system while this pointer is non-null.
  93. std::atomic<bool> non_writable_;
  94. // Force sync of manifest files to fail while this pointer is non-null.
  95. std::atomic<bool> manifest_sync_error_;
  96. // Force write to manifest files to fail while this pointer is non-null.
  97. std::atomic<bool> manifest_write_error_;
  98. bool count_random_reads_;
  99. AtomicCounter random_read_counter_;
  100. explicit SpecialEnv(Env* base)
  101. : EnvWrapper(base),
  102. delay_data_sync_(false),
  103. data_sync_error_(false),
  104. no_space_(false),
  105. non_writable_(false),
  106. manifest_sync_error_(false),
  107. manifest_write_error_(false),
  108. count_random_reads_(false) {}
  109. Status NewWritableFile(const std::string& f, WritableFile** r) {
  110. class DataFile : public WritableFile {
  111. private:
  112. SpecialEnv* const env_;
  113. WritableFile* const base_;
  114. public:
  115. DataFile(SpecialEnv* env, WritableFile* base) : env_(env), base_(base) {}
  116. ~DataFile() { delete base_; }
  117. Status Append(const Slice& data) {
  118. if (env_->no_space_.load(std::memory_order_acquire)) {
  119. // Drop writes on the floor
  120. return Status::OK();
  121. } else {
  122. return base_->Append(data);
  123. }
  124. }
  125. Status Close() { return base_->Close(); }
  126. Status Flush() { return base_->Flush(); }
  127. Status Sync() {
  128. if (env_->data_sync_error_.load(std::memory_order_acquire)) {
  129. return Status::IOError("simulated data sync error");
  130. }
  131. while (env_->delay_data_sync_.load(std::memory_order_acquire)) {
  132. DelayMilliseconds(100);
  133. }
  134. return base_->Sync();
  135. }
  136. };
  137. class ManifestFile : public WritableFile {
  138. private:
  139. SpecialEnv* env_;
  140. WritableFile* base_;
  141. public:
  142. ManifestFile(SpecialEnv* env, WritableFile* b) : env_(env), base_(b) {}
  143. ~ManifestFile() { delete base_; }
  144. Status Append(const Slice& data) {
  145. if (env_->manifest_write_error_.load(std::memory_order_acquire)) {
  146. return Status::IOError("simulated writer error");
  147. } else {
  148. return base_->Append(data);
  149. }
  150. }
  151. Status Close() { return base_->Close(); }
  152. Status Flush() { return base_->Flush(); }
  153. Status Sync() {
  154. if (env_->manifest_sync_error_.load(std::memory_order_acquire)) {
  155. return Status::IOError("simulated sync error");
  156. } else {
  157. return base_->Sync();
  158. }
  159. }
  160. };
  161. if (non_writable_.load(std::memory_order_acquire)) {
  162. return Status::IOError("simulated write error");
  163. }
  164. Status s = target()->NewWritableFile(f, r);
  165. if (s.ok()) {
  166. if (strstr(f.c_str(), ".ldb") != nullptr ||
  167. strstr(f.c_str(), ".log") != nullptr) {
  168. *r = new DataFile(this, *r);
  169. } else if (strstr(f.c_str(), "MANIFEST") != nullptr) {
  170. *r = new ManifestFile(this, *r);
  171. }
  172. }
  173. return s;
  174. }
  175. Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
  176. class CountingFile : public RandomAccessFile {
  177. private:
  178. RandomAccessFile* target_;
  179. AtomicCounter* counter_;
  180. public:
  181. CountingFile(RandomAccessFile* target, AtomicCounter* counter)
  182. : target_(target), counter_(counter) {}
  183. ~CountingFile() override { delete target_; }
  184. Status Read(uint64_t offset, size_t n, Slice* result,
  185. char* scratch) const override {
  186. counter_->Increment();
  187. return target_->Read(offset, n, result, scratch);
  188. }
  189. };
  190. Status s = target()->NewRandomAccessFile(f, r);
  191. if (s.ok() && count_random_reads_) {
  192. *r = new CountingFile(*r, &random_read_counter_);
  193. }
  194. return s;
  195. }
  196. };
  197. class DBTest : public testing::Test {
  198. public:
  199. std::string dbname_;
  200. SpecialEnv* env_;
  201. DB* db_;
  202. Options last_options_;
  203. DBTest() : env_(new SpecialEnv(Env::Default())), option_config_(kDefault) {
  204. filter_policy_ = NewBloomFilterPolicy(10);
  205. dbname_ = testing::TempDir() + "db_test";
  206. DestroyDB(dbname_, Options());
  207. db_ = nullptr;
  208. Reopen();
  209. }
  210. ~DBTest() {
  211. delete db_;
  212. DestroyDB(dbname_, Options());
  213. delete env_;
  214. delete filter_policy_;
  215. }
  216. // Switch to a fresh database with the next option configuration to
  217. // test. Return false if there are no more configurations to test.
  218. bool ChangeOptions() {
  219. option_config_++;
  220. if (option_config_ >= kEnd) {
  221. return false;
  222. } else {
  223. DestroyAndReopen();
  224. return true;
  225. }
  226. }
  227. // Return the current option configuration.
  228. Options CurrentOptions() {
  229. Options options;
  230. options.reuse_logs = false;
  231. switch (option_config_) {
  232. case kReuse:
  233. options.reuse_logs = true;
  234. break;
  235. case kFilter:
  236. options.filter_policy = filter_policy_;
  237. break;
  238. case kUncompressed:
  239. options.compression = kNoCompression;
  240. break;
  241. default:
  242. break;
  243. }
  244. return options;
  245. }
  246. DBImpl* dbfull() { return reinterpret_cast<DBImpl*>(db_); }
  247. void Reopen(Options* options = nullptr) {
  248. ASSERT_LEVELDB_OK(TryReopen(options));
  249. }
  250. void Close() {
  251. delete db_;
  252. db_ = nullptr;
  253. }
  254. void DestroyAndReopen(Options* options = nullptr) {
  255. delete db_;
  256. db_ = nullptr;
  257. DestroyDB(dbname_, Options());
  258. ASSERT_LEVELDB_OK(TryReopen(options));
  259. }
  260. Status TryReopen(Options* options) {
  261. delete db_;
  262. db_ = nullptr;
  263. Options opts;
  264. if (options != nullptr) {
  265. opts = *options;
  266. } else {
  267. opts = CurrentOptions();
  268. opts.create_if_missing = true;
  269. }
  270. last_options_ = opts;
  271. return DB::Open(opts, dbname_, &db_);
  272. }
  273. Status Put(const std::string& k, const std::string& v) {
  274. return db_->Put(WriteOptions(), k, v);
  275. }
  276. Status Delete(const std::string& k) { return db_->Delete(WriteOptions(), k); }
  277. std::string Get(const std::string& k, const Snapshot* snapshot = nullptr) {
  278. ReadOptions options;
  279. options.snapshot = snapshot;
  280. std::string result;
  281. Status s = db_->Get(options, k, &result);
  282. if (s.IsNotFound()) {
  283. result = "NOT_FOUND";
  284. } else if (!s.ok()) {
  285. result = s.ToString();
  286. }
  287. return result;
  288. }
  289. // Return a string that contains all key,value pairs in order,
  290. // formatted like "(k1->v1)(k2->v2)".
  291. std::string Contents() {
  292. std::vector<std::string> forward;
  293. std::string result;
  294. Iterator* iter = db_->NewIterator(ReadOptions());
  295. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  296. std::string s = IterStatus(iter);
  297. result.push_back('(');
  298. result.append(s);
  299. result.push_back(')');
  300. forward.push_back(s);
  301. }
  302. // Check reverse iteration results are the reverse of forward results
  303. size_t matched = 0;
  304. for (iter->SeekToLast(); iter->Valid(); iter->Prev()) {
  305. EXPECT_LT(matched, forward.size());
  306. EXPECT_EQ(IterStatus(iter), forward[forward.size() - matched - 1]);
  307. matched++;
  308. }
  309. EXPECT_EQ(matched, forward.size());
  310. delete iter;
  311. return result;
  312. }
  313. std::string AllEntriesFor(const Slice& user_key) {
  314. Iterator* iter = dbfull()->TEST_NewInternalIterator();
  315. InternalKey target(user_key, kMaxSequenceNumber, kTypeValue);
  316. iter->Seek(target.Encode());
  317. std::string result;
  318. if (!iter->status().ok()) {
  319. result = iter->status().ToString();
  320. } else {
  321. result = "[ ";
  322. bool first = true;
  323. while (iter->Valid()) {
  324. ParsedInternalKey ikey;
  325. if (!ParseInternalKey(iter->key(), &ikey)) {
  326. result += "CORRUPTED";
  327. } else {
  328. if (last_options_.comparator->Compare(ikey.user_key, user_key) != 0) {
  329. break;
  330. }
  331. if (!first) {
  332. result += ", ";
  333. }
  334. first = false;
  335. switch (ikey.type) {
  336. case kTypeValue:
  337. result += iter->value().ToString();
  338. break;
  339. case kTypeDeletion:
  340. result += "DEL";
  341. break;
  342. }
  343. }
  344. iter->Next();
  345. }
  346. if (!first) {
  347. result += " ";
  348. }
  349. result += "]";
  350. }
  351. delete iter;
  352. return result;
  353. }
  354. int NumTableFilesAtLevel(int level) {
  355. std::string property;
  356. EXPECT_TRUE(db_->GetProperty(
  357. "leveldb.num-files-at-level" + NumberToString(level), &property));
  358. return std::stoi(property);
  359. }
  360. int TotalTableFiles() {
  361. int result = 0;
  362. for (int level = 0; level < config::kNumLevels; level++) {
  363. result += NumTableFilesAtLevel(level);
  364. }
  365. return result;
  366. }
  367. // Return spread of files per level
  368. std::string FilesPerLevel() {
  369. std::string result;
  370. int last_non_zero_offset = 0;
  371. for (int level = 0; level < config::kNumLevels; level++) {
  372. int f = NumTableFilesAtLevel(level);
  373. char buf[100];
  374. snprintf(buf, sizeof(buf), "%s%d", (level ? "," : ""), f);
  375. result += buf;
  376. if (f > 0) {
  377. last_non_zero_offset = result.size();
  378. }
  379. }
  380. result.resize(last_non_zero_offset);
  381. return result;
  382. }
  383. int CountFiles() {
  384. std::vector<std::string> files;
  385. env_->GetChildren(dbname_, &files);
  386. return static_cast<int>(files.size());
  387. }
  388. uint64_t Size(const Slice& start, const Slice& limit) {
  389. Range r(start, limit);
  390. uint64_t size;
  391. db_->GetApproximateSizes(&r, 1, &size);
  392. return size;
  393. }
  394. void Compact(const Slice& start, const Slice& limit) {
  395. db_->CompactRange(&start, &limit);
  396. }
  397. // Do n memtable compactions, each of which produces an sstable
  398. // covering the range [small_key,large_key].
  399. void MakeTables(int n, const std::string& small_key,
  400. const std::string& large_key) {
  401. for (int i = 0; i < n; i++) {
  402. Put(small_key, "begin");
  403. Put(large_key, "end");
  404. dbfull()->TEST_CompactMemTable();
  405. }
  406. }
  407. // Prevent pushing of new sstables into deeper levels by adding
  408. // tables that cover a specified range to all levels.
  409. void FillLevels(const std::string& smallest, const std::string& largest) {
  410. MakeTables(config::kNumLevels, smallest, largest);
  411. }
  412. void DumpFileCounts(const char* label) {
  413. fprintf(stderr, "---\n%s:\n", label);
  414. fprintf(
  415. stderr, "maxoverlap: %lld\n",
  416. static_cast<long long>(dbfull()->TEST_MaxNextLevelOverlappingBytes()));
  417. for (int level = 0; level < config::kNumLevels; level++) {
  418. int num = NumTableFilesAtLevel(level);
  419. if (num > 0) {
  420. fprintf(stderr, " level %3d : %d files\n", level, num);
  421. }
  422. }
  423. }
  424. std::string DumpSSTableList() {
  425. std::string property;
  426. db_->GetProperty("leveldb.sstables", &property);
  427. return property;
  428. }
  429. std::string IterStatus(Iterator* iter) {
  430. std::string result;
  431. if (iter->Valid()) {
  432. result = iter->key().ToString() + "->" + iter->value().ToString();
  433. } else {
  434. result = "(invalid)";
  435. }
  436. return result;
  437. }
  438. bool DeleteAnSSTFile() {
  439. std::vector<std::string> filenames;
  440. EXPECT_LEVELDB_OK(env_->GetChildren(dbname_, &filenames));
  441. uint64_t number;
  442. FileType type;
  443. for (size_t i = 0; i < filenames.size(); i++) {
  444. if (ParseFileName(filenames[i], &number, &type) && type == kTableFile) {
  445. EXPECT_LEVELDB_OK(env_->RemoveFile(TableFileName(dbname_, number)));
  446. return true;
  447. }
  448. }
  449. return false;
  450. }
  451. // Returns number of files renamed.
  452. int RenameLDBToSST() {
  453. std::vector<std::string> filenames;
  454. EXPECT_LEVELDB_OK(env_->GetChildren(dbname_, &filenames));
  455. uint64_t number;
  456. FileType type;
  457. int files_renamed = 0;
  458. for (size_t i = 0; i < filenames.size(); i++) {
  459. if (ParseFileName(filenames[i], &number, &type) && type == kTableFile) {
  460. const std::string from = TableFileName(dbname_, number);
  461. const std::string to = SSTTableFileName(dbname_, number);
  462. EXPECT_LEVELDB_OK(env_->RenameFile(from, to));
  463. files_renamed++;
  464. }
  465. }
  466. return files_renamed;
  467. }
  468. private:
  469. // Sequence of option configurations to try
  470. enum OptionConfig { kDefault, kReuse, kFilter, kUncompressed, kEnd };
  471. const FilterPolicy* filter_policy_;
  472. int option_config_;
  473. };
  474. TEST_F(DBTest, Empty) {
  475. do {
  476. ASSERT_TRUE(db_ != nullptr);
  477. ASSERT_EQ("NOT_FOUND", Get("foo"));
  478. } while (ChangeOptions());
  479. }
  480. TEST_F(DBTest, EmptyKey) {
  481. do {
  482. ASSERT_LEVELDB_OK(Put("", "v1"));
  483. ASSERT_EQ("v1", Get(""));
  484. ASSERT_LEVELDB_OK(Put("", "v2"));
  485. ASSERT_EQ("v2", Get(""));
  486. } while (ChangeOptions());
  487. }
  488. TEST_F(DBTest, EmptyValue) {
  489. do {
  490. ASSERT_LEVELDB_OK(Put("key", "v1"));
  491. ASSERT_EQ("v1", Get("key"));
  492. ASSERT_LEVELDB_OK(Put("key", ""));
  493. ASSERT_EQ("", Get("key"));
  494. ASSERT_LEVELDB_OK(Put("key", "v2"));
  495. ASSERT_EQ("v2", Get("key"));
  496. } while (ChangeOptions());
  497. }
  498. TEST_F(DBTest, ReadWrite) {
  499. do {
  500. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  501. ASSERT_EQ("v1", Get("foo"));
  502. ASSERT_LEVELDB_OK(Put("bar", "v2"));
  503. ASSERT_LEVELDB_OK(Put("foo", "v3"));
  504. ASSERT_EQ("v3", Get("foo"));
  505. ASSERT_EQ("v2", Get("bar"));
  506. } while (ChangeOptions());
  507. }
  508. TEST_F(DBTest, PutDeleteGet) {
  509. do {
  510. ASSERT_LEVELDB_OK(db_->Put(WriteOptions(), "foo", "v1"));
  511. ASSERT_EQ("v1", Get("foo"));
  512. ASSERT_LEVELDB_OK(db_->Put(WriteOptions(), "foo", "v2"));
  513. ASSERT_EQ("v2", Get("foo"));
  514. ASSERT_LEVELDB_OK(db_->Delete(WriteOptions(), "foo"));
  515. ASSERT_EQ("NOT_FOUND", Get("foo"));
  516. } while (ChangeOptions());
  517. }
  518. TEST_F(DBTest, GetFromImmutableLayer) {
  519. do {
  520. Options options = CurrentOptions();
  521. options.env = env_;
  522. options.write_buffer_size = 100000; // Small write buffer
  523. Reopen(&options);
  524. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  525. ASSERT_EQ("v1", Get("foo"));
  526. // Block sync calls.
  527. env_->delay_data_sync_.store(true, std::memory_order_release);
  528. Put("k1", std::string(100000, 'x')); // Fill memtable.
  529. Put("k2", std::string(100000, 'y')); // Trigger compaction.
  530. ASSERT_EQ("v1", Get("foo"));
  531. // Release sync calls.
  532. env_->delay_data_sync_.store(false, std::memory_order_release);
  533. } while (ChangeOptions());
  534. }
  535. TEST_F(DBTest, GetFromVersions) {
  536. do {
  537. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  538. dbfull()->TEST_CompactMemTable();
  539. ASSERT_EQ("v1", Get("foo"));
  540. } while (ChangeOptions());
  541. }
  542. TEST_F(DBTest, GetMemUsage) {
  543. do {
  544. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  545. std::string val;
  546. ASSERT_TRUE(db_->GetProperty("leveldb.approximate-memory-usage", &val));
  547. int mem_usage = std::stoi(val);
  548. ASSERT_GT(mem_usage, 0);
  549. ASSERT_LT(mem_usage, 5 * 1024 * 1024);
  550. } while (ChangeOptions());
  551. }
  552. TEST_F(DBTest, GetSnapshot) {
  553. do {
  554. // Try with both a short key and a long key
  555. for (int i = 0; i < 2; i++) {
  556. std::string key = (i == 0) ? std::string("foo") : std::string(200, 'x');
  557. ASSERT_LEVELDB_OK(Put(key, "v1"));
  558. const Snapshot* s1 = db_->GetSnapshot();
  559. ASSERT_LEVELDB_OK(Put(key, "v2"));
  560. ASSERT_EQ("v2", Get(key));
  561. ASSERT_EQ("v1", Get(key, s1));
  562. dbfull()->TEST_CompactMemTable();
  563. ASSERT_EQ("v2", Get(key));
  564. ASSERT_EQ("v1", Get(key, s1));
  565. db_->ReleaseSnapshot(s1);
  566. }
  567. } while (ChangeOptions());
  568. }
  569. TEST_F(DBTest, GetIdenticalSnapshots) {
  570. do {
  571. // Try with both a short key and a long key
  572. for (int i = 0; i < 2; i++) {
  573. std::string key = (i == 0) ? std::string("foo") : std::string(200, 'x');
  574. ASSERT_LEVELDB_OK(Put(key, "v1"));
  575. const Snapshot* s1 = db_->GetSnapshot();
  576. const Snapshot* s2 = db_->GetSnapshot();
  577. const Snapshot* s3 = db_->GetSnapshot();
  578. ASSERT_LEVELDB_OK(Put(key, "v2"));
  579. ASSERT_EQ("v2", Get(key));
  580. ASSERT_EQ("v1", Get(key, s1));
  581. ASSERT_EQ("v1", Get(key, s2));
  582. ASSERT_EQ("v1", Get(key, s3));
  583. db_->ReleaseSnapshot(s1);
  584. dbfull()->TEST_CompactMemTable();
  585. ASSERT_EQ("v2", Get(key));
  586. ASSERT_EQ("v1", Get(key, s2));
  587. db_->ReleaseSnapshot(s2);
  588. ASSERT_EQ("v1", Get(key, s3));
  589. db_->ReleaseSnapshot(s3);
  590. }
  591. } while (ChangeOptions());
  592. }
  593. TEST_F(DBTest, IterateOverEmptySnapshot) {
  594. do {
  595. const Snapshot* snapshot = db_->GetSnapshot();
  596. ReadOptions read_options;
  597. read_options.snapshot = snapshot;
  598. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  599. ASSERT_LEVELDB_OK(Put("foo", "v2"));
  600. Iterator* iterator1 = db_->NewIterator(read_options);
  601. iterator1->SeekToFirst();
  602. ASSERT_TRUE(!iterator1->Valid());
  603. delete iterator1;
  604. dbfull()->TEST_CompactMemTable();
  605. Iterator* iterator2 = db_->NewIterator(read_options);
  606. iterator2->SeekToFirst();
  607. ASSERT_TRUE(!iterator2->Valid());
  608. delete iterator2;
  609. db_->ReleaseSnapshot(snapshot);
  610. } while (ChangeOptions());
  611. }
  612. TEST_F(DBTest, GetLevel0Ordering) {
  613. do {
  614. // Check that we process level-0 files in correct order. The code
  615. // below generates two level-0 files where the earlier one comes
  616. // before the later one in the level-0 file list since the earlier
  617. // one has a smaller "smallest" key.
  618. ASSERT_LEVELDB_OK(Put("bar", "b"));
  619. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  620. dbfull()->TEST_CompactMemTable();
  621. ASSERT_LEVELDB_OK(Put("foo", "v2"));
  622. dbfull()->TEST_CompactMemTable();
  623. ASSERT_EQ("v2", Get("foo"));
  624. } while (ChangeOptions());
  625. }
  626. TEST_F(DBTest, GetOrderedByLevels) {
  627. do {
  628. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  629. Compact("a", "z");
  630. ASSERT_EQ("v1", Get("foo"));
  631. ASSERT_LEVELDB_OK(Put("foo", "v2"));
  632. ASSERT_EQ("v2", Get("foo"));
  633. dbfull()->TEST_CompactMemTable();
  634. ASSERT_EQ("v2", Get("foo"));
  635. } while (ChangeOptions());
  636. }
  637. TEST_F(DBTest, GetPicksCorrectFile) {
  638. do {
  639. // Arrange to have multiple files in a non-level-0 level.
  640. ASSERT_LEVELDB_OK(Put("a", "va"));
  641. Compact("a", "b");
  642. ASSERT_LEVELDB_OK(Put("x", "vx"));
  643. Compact("x", "y");
  644. ASSERT_LEVELDB_OK(Put("f", "vf"));
  645. Compact("f", "g");
  646. ASSERT_EQ("va", Get("a"));
  647. ASSERT_EQ("vf", Get("f"));
  648. ASSERT_EQ("vx", Get("x"));
  649. } while (ChangeOptions());
  650. }
  651. TEST_F(DBTest, GetEncountersEmptyLevel) {
  652. do {
  653. // Arrange for the following to happen:
  654. // * sstable A in level 0
  655. // * nothing in level 1
  656. // * sstable B in level 2
  657. // Then do enough Get() calls to arrange for an automatic compaction
  658. // of sstable A. A bug would cause the compaction to be marked as
  659. // occurring at level 1 (instead of the correct level 0).
  660. // Step 1: First place sstables in levels 0 and 2
  661. int compaction_count = 0;
  662. while (NumTableFilesAtLevel(0) == 0 || NumTableFilesAtLevel(2) == 0) {
  663. ASSERT_LE(compaction_count, 100) << "could not fill levels 0 and 2";
  664. compaction_count++;
  665. Put("a", "begin");
  666. Put("z", "end");
  667. dbfull()->TEST_CompactMemTable();
  668. }
  669. // Step 2: clear level 1 if necessary.
  670. dbfull()->TEST_CompactRange(1, nullptr, nullptr);
  671. ASSERT_EQ(NumTableFilesAtLevel(0), 1);
  672. ASSERT_EQ(NumTableFilesAtLevel(1), 0);
  673. ASSERT_EQ(NumTableFilesAtLevel(2), 1);
  674. // Step 3: read a bunch of times
  675. for (int i = 0; i < 1000; i++) {
  676. ASSERT_EQ("NOT_FOUND", Get("missing"));
  677. }
  678. // Step 4: Wait for compaction to finish
  679. DelayMilliseconds(1000);
  680. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  681. } while (ChangeOptions());
  682. }
  683. TEST_F(DBTest, IterEmpty) {
  684. Iterator* iter = db_->NewIterator(ReadOptions());
  685. iter->SeekToFirst();
  686. ASSERT_EQ(IterStatus(iter), "(invalid)");
  687. iter->SeekToLast();
  688. ASSERT_EQ(IterStatus(iter), "(invalid)");
  689. iter->Seek("foo");
  690. ASSERT_EQ(IterStatus(iter), "(invalid)");
  691. delete iter;
  692. }
  693. TEST_F(DBTest, IterSingle) {
  694. ASSERT_LEVELDB_OK(Put("a", "va"));
  695. Iterator* iter = db_->NewIterator(ReadOptions());
  696. iter->SeekToFirst();
  697. ASSERT_EQ(IterStatus(iter), "a->va");
  698. iter->Next();
  699. ASSERT_EQ(IterStatus(iter), "(invalid)");
  700. iter->SeekToFirst();
  701. ASSERT_EQ(IterStatus(iter), "a->va");
  702. iter->Prev();
  703. ASSERT_EQ(IterStatus(iter), "(invalid)");
  704. iter->SeekToLast();
  705. ASSERT_EQ(IterStatus(iter), "a->va");
  706. iter->Next();
  707. ASSERT_EQ(IterStatus(iter), "(invalid)");
  708. iter->SeekToLast();
  709. ASSERT_EQ(IterStatus(iter), "a->va");
  710. iter->Prev();
  711. ASSERT_EQ(IterStatus(iter), "(invalid)");
  712. iter->Seek("");
  713. ASSERT_EQ(IterStatus(iter), "a->va");
  714. iter->Next();
  715. ASSERT_EQ(IterStatus(iter), "(invalid)");
  716. iter->Seek("a");
  717. ASSERT_EQ(IterStatus(iter), "a->va");
  718. iter->Next();
  719. ASSERT_EQ(IterStatus(iter), "(invalid)");
  720. iter->Seek("b");
  721. ASSERT_EQ(IterStatus(iter), "(invalid)");
  722. delete iter;
  723. }
  724. TEST_F(DBTest, IterMulti) {
  725. ASSERT_LEVELDB_OK(Put("a", "va"));
  726. ASSERT_LEVELDB_OK(Put("b", "vb"));
  727. ASSERT_LEVELDB_OK(Put("c", "vc"));
  728. Iterator* iter = db_->NewIterator(ReadOptions());
  729. iter->SeekToFirst();
  730. ASSERT_EQ(IterStatus(iter), "a->va");
  731. iter->Next();
  732. ASSERT_EQ(IterStatus(iter), "b->vb");
  733. iter->Next();
  734. ASSERT_EQ(IterStatus(iter), "c->vc");
  735. iter->Next();
  736. ASSERT_EQ(IterStatus(iter), "(invalid)");
  737. iter->SeekToFirst();
  738. ASSERT_EQ(IterStatus(iter), "a->va");
  739. iter->Prev();
  740. ASSERT_EQ(IterStatus(iter), "(invalid)");
  741. iter->SeekToLast();
  742. ASSERT_EQ(IterStatus(iter), "c->vc");
  743. iter->Prev();
  744. ASSERT_EQ(IterStatus(iter), "b->vb");
  745. iter->Prev();
  746. ASSERT_EQ(IterStatus(iter), "a->va");
  747. iter->Prev();
  748. ASSERT_EQ(IterStatus(iter), "(invalid)");
  749. iter->SeekToLast();
  750. ASSERT_EQ(IterStatus(iter), "c->vc");
  751. iter->Next();
  752. ASSERT_EQ(IterStatus(iter), "(invalid)");
  753. iter->Seek("");
  754. ASSERT_EQ(IterStatus(iter), "a->va");
  755. iter->Seek("a");
  756. ASSERT_EQ(IterStatus(iter), "a->va");
  757. iter->Seek("ax");
  758. ASSERT_EQ(IterStatus(iter), "b->vb");
  759. iter->Seek("b");
  760. ASSERT_EQ(IterStatus(iter), "b->vb");
  761. iter->Seek("z");
  762. ASSERT_EQ(IterStatus(iter), "(invalid)");
  763. // Switch from reverse to forward
  764. iter->SeekToLast();
  765. iter->Prev();
  766. iter->Prev();
  767. iter->Next();
  768. ASSERT_EQ(IterStatus(iter), "b->vb");
  769. // Switch from forward to reverse
  770. iter->SeekToFirst();
  771. iter->Next();
  772. iter->Next();
  773. iter->Prev();
  774. ASSERT_EQ(IterStatus(iter), "b->vb");
  775. // Make sure iter stays at snapshot
  776. ASSERT_LEVELDB_OK(Put("a", "va2"));
  777. ASSERT_LEVELDB_OK(Put("a2", "va3"));
  778. ASSERT_LEVELDB_OK(Put("b", "vb2"));
  779. ASSERT_LEVELDB_OK(Put("c", "vc2"));
  780. ASSERT_LEVELDB_OK(Delete("b"));
  781. iter->SeekToFirst();
  782. ASSERT_EQ(IterStatus(iter), "a->va");
  783. iter->Next();
  784. ASSERT_EQ(IterStatus(iter), "b->vb");
  785. iter->Next();
  786. ASSERT_EQ(IterStatus(iter), "c->vc");
  787. iter->Next();
  788. ASSERT_EQ(IterStatus(iter), "(invalid)");
  789. iter->SeekToLast();
  790. ASSERT_EQ(IterStatus(iter), "c->vc");
  791. iter->Prev();
  792. ASSERT_EQ(IterStatus(iter), "b->vb");
  793. iter->Prev();
  794. ASSERT_EQ(IterStatus(iter), "a->va");
  795. iter->Prev();
  796. ASSERT_EQ(IterStatus(iter), "(invalid)");
  797. delete iter;
  798. }
  799. TEST_F(DBTest, IterSmallAndLargeMix) {
  800. ASSERT_LEVELDB_OK(Put("a", "va"));
  801. ASSERT_LEVELDB_OK(Put("b", std::string(100000, 'b')));
  802. ASSERT_LEVELDB_OK(Put("c", "vc"));
  803. ASSERT_LEVELDB_OK(Put("d", std::string(100000, 'd')));
  804. ASSERT_LEVELDB_OK(Put("e", std::string(100000, 'e')));
  805. Iterator* iter = db_->NewIterator(ReadOptions());
  806. iter->SeekToFirst();
  807. ASSERT_EQ(IterStatus(iter), "a->va");
  808. iter->Next();
  809. ASSERT_EQ(IterStatus(iter), "b->" + std::string(100000, 'b'));
  810. iter->Next();
  811. ASSERT_EQ(IterStatus(iter), "c->vc");
  812. iter->Next();
  813. ASSERT_EQ(IterStatus(iter), "d->" + std::string(100000, 'd'));
  814. iter->Next();
  815. ASSERT_EQ(IterStatus(iter), "e->" + std::string(100000, 'e'));
  816. iter->Next();
  817. ASSERT_EQ(IterStatus(iter), "(invalid)");
  818. iter->SeekToLast();
  819. ASSERT_EQ(IterStatus(iter), "e->" + std::string(100000, 'e'));
  820. iter->Prev();
  821. ASSERT_EQ(IterStatus(iter), "d->" + std::string(100000, 'd'));
  822. iter->Prev();
  823. ASSERT_EQ(IterStatus(iter), "c->vc");
  824. iter->Prev();
  825. ASSERT_EQ(IterStatus(iter), "b->" + std::string(100000, 'b'));
  826. iter->Prev();
  827. ASSERT_EQ(IterStatus(iter), "a->va");
  828. iter->Prev();
  829. ASSERT_EQ(IterStatus(iter), "(invalid)");
  830. delete iter;
  831. }
  832. TEST_F(DBTest, IterMultiWithDelete) {
  833. do {
  834. ASSERT_LEVELDB_OK(Put("a", "va"));
  835. ASSERT_LEVELDB_OK(Put("b", "vb"));
  836. ASSERT_LEVELDB_OK(Put("c", "vc"));
  837. ASSERT_LEVELDB_OK(Delete("b"));
  838. ASSERT_EQ("NOT_FOUND", Get("b"));
  839. Iterator* iter = db_->NewIterator(ReadOptions());
  840. iter->Seek("c");
  841. ASSERT_EQ(IterStatus(iter), "c->vc");
  842. iter->Prev();
  843. ASSERT_EQ(IterStatus(iter), "a->va");
  844. delete iter;
  845. } while (ChangeOptions());
  846. }
  847. TEST_F(DBTest, Recover) {
  848. do {
  849. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  850. ASSERT_LEVELDB_OK(Put("baz", "v5"));
  851. Reopen();
  852. ASSERT_EQ("v1", Get("foo"));
  853. ASSERT_EQ("v1", Get("foo"));
  854. ASSERT_EQ("v5", Get("baz"));
  855. ASSERT_LEVELDB_OK(Put("bar", "v2"));
  856. ASSERT_LEVELDB_OK(Put("foo", "v3"));
  857. Reopen();
  858. ASSERT_EQ("v3", Get("foo"));
  859. ASSERT_LEVELDB_OK(Put("foo", "v4"));
  860. ASSERT_EQ("v4", Get("foo"));
  861. ASSERT_EQ("v2", Get("bar"));
  862. ASSERT_EQ("v5", Get("baz"));
  863. } while (ChangeOptions());
  864. }
  865. TEST_F(DBTest, RecoveryWithEmptyLog) {
  866. do {
  867. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  868. ASSERT_LEVELDB_OK(Put("foo", "v2"));
  869. Reopen();
  870. Reopen();
  871. ASSERT_LEVELDB_OK(Put("foo", "v3"));
  872. Reopen();
  873. ASSERT_EQ("v3", Get("foo"));
  874. } while (ChangeOptions());
  875. }
  876. // Check that writes done during a memtable compaction are recovered
  877. // if the database is shutdown during the memtable compaction.
  878. TEST_F(DBTest, RecoverDuringMemtableCompaction) {
  879. do {
  880. Options options = CurrentOptions();
  881. options.env = env_;
  882. options.write_buffer_size = 1000000;
  883. Reopen(&options);
  884. // Trigger a long memtable compaction and reopen the database during it
  885. ASSERT_LEVELDB_OK(Put("foo", "v1")); // Goes to 1st log file
  886. ASSERT_LEVELDB_OK(
  887. Put("big1", std::string(10000000, 'x'))); // Fills memtable
  888. ASSERT_LEVELDB_OK(
  889. Put("big2", std::string(1000, 'y'))); // Triggers compaction
  890. ASSERT_LEVELDB_OK(Put("bar", "v2")); // Goes to new log file
  891. Reopen(&options);
  892. ASSERT_EQ("v1", Get("foo"));
  893. ASSERT_EQ("v2", Get("bar"));
  894. ASSERT_EQ(std::string(10000000, 'x'), Get("big1"));
  895. ASSERT_EQ(std::string(1000, 'y'), Get("big2"));
  896. } while (ChangeOptions());
  897. }
  898. static std::string Key(int i) {
  899. char buf[100];
  900. snprintf(buf, sizeof(buf), "key%06d", i);
  901. return std::string(buf);
  902. }
  903. TEST_F(DBTest, MinorCompactionsHappen) {
  904. Options options = CurrentOptions();
  905. options.write_buffer_size = 10000;
  906. Reopen(&options);
  907. const int N = 500;
  908. int starting_num_tables = TotalTableFiles();
  909. for (int i = 0; i < N; i++) {
  910. ASSERT_LEVELDB_OK(Put(Key(i), Key(i) + std::string(1000, 'v')));
  911. }
  912. int ending_num_tables = TotalTableFiles();
  913. ASSERT_GT(ending_num_tables, starting_num_tables);
  914. for (int i = 0; i < N; i++) {
  915. ASSERT_EQ(Key(i) + std::string(1000, 'v'), Get(Key(i)));
  916. }
  917. Reopen();
  918. for (int i = 0; i < N; i++) {
  919. ASSERT_EQ(Key(i) + std::string(1000, 'v'), Get(Key(i)));
  920. }
  921. }
  922. TEST_F(DBTest, RecoverWithLargeLog) {
  923. {
  924. Options options = CurrentOptions();
  925. Reopen(&options);
  926. ASSERT_LEVELDB_OK(Put("big1", std::string(200000, '1')));
  927. ASSERT_LEVELDB_OK(Put("big2", std::string(200000, '2')));
  928. ASSERT_LEVELDB_OK(Put("small3", std::string(10, '3')));
  929. ASSERT_LEVELDB_OK(Put("small4", std::string(10, '4')));
  930. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  931. }
  932. // Make sure that if we re-open with a small write buffer size that
  933. // we flush table files in the middle of a large log file.
  934. Options options = CurrentOptions();
  935. options.write_buffer_size = 100000;
  936. Reopen(&options);
  937. ASSERT_EQ(NumTableFilesAtLevel(0), 3);
  938. ASSERT_EQ(std::string(200000, '1'), Get("big1"));
  939. ASSERT_EQ(std::string(200000, '2'), Get("big2"));
  940. ASSERT_EQ(std::string(10, '3'), Get("small3"));
  941. ASSERT_EQ(std::string(10, '4'), Get("small4"));
  942. ASSERT_GT(NumTableFilesAtLevel(0), 1);
  943. }
  944. TEST_F(DBTest, CompactionsGenerateMultipleFiles) {
  945. Options options = CurrentOptions();
  946. options.write_buffer_size = 100000000; // Large write buffer
  947. Reopen(&options);
  948. Random rnd(301);
  949. // Write 8MB (80 values, each 100K)
  950. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  951. std::vector<std::string> values;
  952. for (int i = 0; i < 80; i++) {
  953. values.push_back(RandomString(&rnd, 100000));
  954. ASSERT_LEVELDB_OK(Put(Key(i), values[i]));
  955. }
  956. // Reopening moves updates to level-0
  957. Reopen(&options);
  958. dbfull()->TEST_CompactRange(0, nullptr, nullptr);
  959. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  960. ASSERT_GT(NumTableFilesAtLevel(1), 1);
  961. for (int i = 0; i < 80; i++) {
  962. ASSERT_EQ(Get(Key(i)), values[i]);
  963. }
  964. }
  965. TEST_F(DBTest, RepeatedWritesToSameKey) {
  966. Options options = CurrentOptions();
  967. options.env = env_;
  968. options.write_buffer_size = 100000; // Small write buffer
  969. Reopen(&options);
  970. // We must have at most one file per level except for level-0,
  971. // which may have up to kL0_StopWritesTrigger files.
  972. const int kMaxFiles = config::kNumLevels + config::kL0_StopWritesTrigger;
  973. Random rnd(301);
  974. std::string value = RandomString(&rnd, 2 * options.write_buffer_size);
  975. for (int i = 0; i < 5 * kMaxFiles; i++) {
  976. Put("key", value);
  977. ASSERT_LE(TotalTableFiles(), kMaxFiles);
  978. fprintf(stderr, "after %d: %d files\n", i + 1, TotalTableFiles());
  979. }
  980. }
  981. TEST_F(DBTest, SparseMerge) {
  982. Options options = CurrentOptions();
  983. options.compression = kNoCompression;
  984. Reopen(&options);
  985. FillLevels("A", "Z");
  986. // Suppose there is:
  987. // small amount of data with prefix A
  988. // large amount of data with prefix B
  989. // small amount of data with prefix C
  990. // and that recent updates have made small changes to all three prefixes.
  991. // Check that we do not do a compaction that merges all of B in one shot.
  992. const std::string value(1000, 'x');
  993. Put("A", "va");
  994. // Write approximately 100MB of "B" values
  995. for (int i = 0; i < 100000; i++) {
  996. char key[100];
  997. snprintf(key, sizeof(key), "B%010d", i);
  998. Put(key, value);
  999. }
  1000. Put("C", "vc");
  1001. dbfull()->TEST_CompactMemTable();
  1002. dbfull()->TEST_CompactRange(0, nullptr, nullptr);
  1003. // Make sparse update
  1004. Put("A", "va2");
  1005. Put("B100", "bvalue2");
  1006. Put("C", "vc2");
  1007. dbfull()->TEST_CompactMemTable();
  1008. // Compactions should not cause us to create a situation where
  1009. // a file overlaps too much data at the next level.
  1010. ASSERT_LE(dbfull()->TEST_MaxNextLevelOverlappingBytes(), 20 * 1048576);
  1011. dbfull()->TEST_CompactRange(0, nullptr, nullptr);
  1012. ASSERT_LE(dbfull()->TEST_MaxNextLevelOverlappingBytes(), 20 * 1048576);
  1013. dbfull()->TEST_CompactRange(1, nullptr, nullptr);
  1014. ASSERT_LE(dbfull()->TEST_MaxNextLevelOverlappingBytes(), 20 * 1048576);
  1015. }
  1016. static bool Between(uint64_t val, uint64_t low, uint64_t high) {
  1017. bool result = (val >= low) && (val <= high);
  1018. if (!result) {
  1019. fprintf(stderr, "Value %llu is not in range [%llu, %llu]\n",
  1020. (unsigned long long)(val), (unsigned long long)(low),
  1021. (unsigned long long)(high));
  1022. }
  1023. return result;
  1024. }
  1025. TEST_F(DBTest, ApproximateSizes) {
  1026. do {
  1027. Options options = CurrentOptions();
  1028. options.write_buffer_size = 100000000; // Large write buffer
  1029. options.compression = kNoCompression;
  1030. DestroyAndReopen();
  1031. ASSERT_TRUE(Between(Size("", "xyz"), 0, 0));
  1032. Reopen(&options);
  1033. ASSERT_TRUE(Between(Size("", "xyz"), 0, 0));
  1034. // Write 8MB (80 values, each 100K)
  1035. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  1036. const int N = 80;
  1037. static const int S1 = 100000;
  1038. static const int S2 = 105000; // Allow some expansion from metadata
  1039. Random rnd(301);
  1040. for (int i = 0; i < N; i++) {
  1041. ASSERT_LEVELDB_OK(Put(Key(i), RandomString(&rnd, S1)));
  1042. }
  1043. // 0 because GetApproximateSizes() does not account for memtable space
  1044. ASSERT_TRUE(Between(Size("", Key(50)), 0, 0));
  1045. if (options.reuse_logs) {
  1046. // Recovery will reuse memtable, and GetApproximateSizes() does not
  1047. // account for memtable usage;
  1048. Reopen(&options);
  1049. ASSERT_TRUE(Between(Size("", Key(50)), 0, 0));
  1050. continue;
  1051. }
  1052. // Check sizes across recovery by reopening a few times
  1053. for (int run = 0; run < 3; run++) {
  1054. Reopen(&options);
  1055. for (int compact_start = 0; compact_start < N; compact_start += 10) {
  1056. for (int i = 0; i < N; i += 10) {
  1057. ASSERT_TRUE(Between(Size("", Key(i)), S1 * i, S2 * i));
  1058. ASSERT_TRUE(Between(Size("", Key(i) + ".suffix"), S1 * (i + 1),
  1059. S2 * (i + 1)));
  1060. ASSERT_TRUE(Between(Size(Key(i), Key(i + 10)), S1 * 10, S2 * 10));
  1061. }
  1062. ASSERT_TRUE(Between(Size("", Key(50)), S1 * 50, S2 * 50));
  1063. ASSERT_TRUE(Between(Size("", Key(50) + ".suffix"), S1 * 50, S2 * 50));
  1064. std::string cstart_str = Key(compact_start);
  1065. std::string cend_str = Key(compact_start + 9);
  1066. Slice cstart = cstart_str;
  1067. Slice cend = cend_str;
  1068. dbfull()->TEST_CompactRange(0, &cstart, &cend);
  1069. }
  1070. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  1071. ASSERT_GT(NumTableFilesAtLevel(1), 0);
  1072. }
  1073. } while (ChangeOptions());
  1074. }
  1075. TEST_F(DBTest, ApproximateSizes_MixOfSmallAndLarge) {
  1076. do {
  1077. Options options = CurrentOptions();
  1078. options.compression = kNoCompression;
  1079. Reopen();
  1080. Random rnd(301);
  1081. std::string big1 = RandomString(&rnd, 100000);
  1082. ASSERT_LEVELDB_OK(Put(Key(0), RandomString(&rnd, 10000)));
  1083. ASSERT_LEVELDB_OK(Put(Key(1), RandomString(&rnd, 10000)));
  1084. ASSERT_LEVELDB_OK(Put(Key(2), big1));
  1085. ASSERT_LEVELDB_OK(Put(Key(3), RandomString(&rnd, 10000)));
  1086. ASSERT_LEVELDB_OK(Put(Key(4), big1));
  1087. ASSERT_LEVELDB_OK(Put(Key(5), RandomString(&rnd, 10000)));
  1088. ASSERT_LEVELDB_OK(Put(Key(6), RandomString(&rnd, 300000)));
  1089. ASSERT_LEVELDB_OK(Put(Key(7), RandomString(&rnd, 10000)));
  1090. if (options.reuse_logs) {
  1091. // Need to force a memtable compaction since recovery does not do so.
  1092. ASSERT_LEVELDB_OK(dbfull()->TEST_CompactMemTable());
  1093. }
  1094. // Check sizes across recovery by reopening a few times
  1095. for (int run = 0; run < 3; run++) {
  1096. Reopen(&options);
  1097. ASSERT_TRUE(Between(Size("", Key(0)), 0, 0));
  1098. ASSERT_TRUE(Between(Size("", Key(1)), 10000, 11000));
  1099. ASSERT_TRUE(Between(Size("", Key(2)), 20000, 21000));
  1100. ASSERT_TRUE(Between(Size("", Key(3)), 120000, 121000));
  1101. ASSERT_TRUE(Between(Size("", Key(4)), 130000, 131000));
  1102. ASSERT_TRUE(Between(Size("", Key(5)), 230000, 231000));
  1103. ASSERT_TRUE(Between(Size("", Key(6)), 240000, 241000));
  1104. ASSERT_TRUE(Between(Size("", Key(7)), 540000, 541000));
  1105. ASSERT_TRUE(Between(Size("", Key(8)), 550000, 560000));
  1106. ASSERT_TRUE(Between(Size(Key(3), Key(5)), 110000, 111000));
  1107. dbfull()->TEST_CompactRange(0, nullptr, nullptr);
  1108. }
  1109. } while (ChangeOptions());
  1110. }
  1111. TEST_F(DBTest, IteratorPinsRef) {
  1112. Put("foo", "hello");
  1113. // Get iterator that will yield the current contents of the DB.
  1114. Iterator* iter = db_->NewIterator(ReadOptions());
  1115. // Write to force compactions
  1116. Put("foo", "newvalue1");
  1117. for (int i = 0; i < 100; i++) {
  1118. ASSERT_LEVELDB_OK(
  1119. Put(Key(i), Key(i) + std::string(100000, 'v'))); // 100K values
  1120. }
  1121. Put("foo", "newvalue2");
  1122. iter->SeekToFirst();
  1123. ASSERT_TRUE(iter->Valid());
  1124. ASSERT_EQ("foo", iter->key().ToString());
  1125. ASSERT_EQ("hello", iter->value().ToString());
  1126. iter->Next();
  1127. ASSERT_TRUE(!iter->Valid());
  1128. delete iter;
  1129. }
  1130. TEST_F(DBTest, Snapshot) {
  1131. do {
  1132. Put("foo", "v1");
  1133. const Snapshot* s1 = db_->GetSnapshot();
  1134. Put("foo", "v2");
  1135. const Snapshot* s2 = db_->GetSnapshot();
  1136. Put("foo", "v3");
  1137. const Snapshot* s3 = db_->GetSnapshot();
  1138. Put("foo", "v4");
  1139. ASSERT_EQ("v1", Get("foo", s1));
  1140. ASSERT_EQ("v2", Get("foo", s2));
  1141. ASSERT_EQ("v3", Get("foo", s3));
  1142. ASSERT_EQ("v4", Get("foo"));
  1143. db_->ReleaseSnapshot(s3);
  1144. ASSERT_EQ("v1", Get("foo", s1));
  1145. ASSERT_EQ("v2", Get("foo", s2));
  1146. ASSERT_EQ("v4", Get("foo"));
  1147. db_->ReleaseSnapshot(s1);
  1148. ASSERT_EQ("v2", Get("foo", s2));
  1149. ASSERT_EQ("v4", Get("foo"));
  1150. db_->ReleaseSnapshot(s2);
  1151. ASSERT_EQ("v4", Get("foo"));
  1152. } while (ChangeOptions());
  1153. }
  1154. TEST_F(DBTest, HiddenValuesAreRemoved) {
  1155. do {
  1156. Random rnd(301);
  1157. FillLevels("a", "z");
  1158. std::string big = RandomString(&rnd, 50000);
  1159. Put("foo", big);
  1160. Put("pastfoo", "v");
  1161. const Snapshot* snapshot = db_->GetSnapshot();
  1162. Put("foo", "tiny");
  1163. Put("pastfoo2", "v2"); // Advance sequence number one more
  1164. ASSERT_LEVELDB_OK(dbfull()->TEST_CompactMemTable());
  1165. ASSERT_GT(NumTableFilesAtLevel(0), 0);
  1166. ASSERT_EQ(big, Get("foo", snapshot));
  1167. ASSERT_TRUE(Between(Size("", "pastfoo"), 50000, 60000));
  1168. db_->ReleaseSnapshot(snapshot);
  1169. ASSERT_EQ(AllEntriesFor("foo"), "[ tiny, " + big + " ]");
  1170. Slice x("x");
  1171. dbfull()->TEST_CompactRange(0, nullptr, &x);
  1172. ASSERT_EQ(AllEntriesFor("foo"), "[ tiny ]");
  1173. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  1174. ASSERT_GE(NumTableFilesAtLevel(1), 1);
  1175. dbfull()->TEST_CompactRange(1, nullptr, &x);
  1176. ASSERT_EQ(AllEntriesFor("foo"), "[ tiny ]");
  1177. ASSERT_TRUE(Between(Size("", "pastfoo"), 0, 1000));
  1178. } while (ChangeOptions());
  1179. }
  1180. TEST_F(DBTest, DeletionMarkers1) {
  1181. Put("foo", "v1");
  1182. ASSERT_LEVELDB_OK(dbfull()->TEST_CompactMemTable());
  1183. const int last = config::kMaxMemCompactLevel;
  1184. ASSERT_EQ(NumTableFilesAtLevel(last), 1); // foo => v1 is now in last level
  1185. // Place a table at level last-1 to prevent merging with preceding mutation
  1186. Put("a", "begin");
  1187. Put("z", "end");
  1188. dbfull()->TEST_CompactMemTable();
  1189. ASSERT_EQ(NumTableFilesAtLevel(last), 1);
  1190. ASSERT_EQ(NumTableFilesAtLevel(last - 1), 1);
  1191. Delete("foo");
  1192. Put("foo", "v2");
  1193. ASSERT_EQ(AllEntriesFor("foo"), "[ v2, DEL, v1 ]");
  1194. ASSERT_LEVELDB_OK(dbfull()->TEST_CompactMemTable()); // Moves to level last-2
  1195. ASSERT_EQ(AllEntriesFor("foo"), "[ v2, DEL, v1 ]");
  1196. Slice z("z");
  1197. dbfull()->TEST_CompactRange(last - 2, nullptr, &z);
  1198. // DEL eliminated, but v1 remains because we aren't compacting that level
  1199. // (DEL can be eliminated because v2 hides v1).
  1200. ASSERT_EQ(AllEntriesFor("foo"), "[ v2, v1 ]");
  1201. dbfull()->TEST_CompactRange(last - 1, nullptr, nullptr);
  1202. // Merging last-1 w/ last, so we are the base level for "foo", so
  1203. // DEL is removed. (as is v1).
  1204. ASSERT_EQ(AllEntriesFor("foo"), "[ v2 ]");
  1205. }
  1206. TEST_F(DBTest, DeletionMarkers2) {
  1207. Put("foo", "v1");
  1208. ASSERT_LEVELDB_OK(dbfull()->TEST_CompactMemTable());
  1209. const int last = config::kMaxMemCompactLevel;
  1210. ASSERT_EQ(NumTableFilesAtLevel(last), 1); // foo => v1 is now in last level
  1211. // Place a table at level last-1 to prevent merging with preceding mutation
  1212. Put("a", "begin");
  1213. Put("z", "end");
  1214. dbfull()->TEST_CompactMemTable();
  1215. ASSERT_EQ(NumTableFilesAtLevel(last), 1);
  1216. ASSERT_EQ(NumTableFilesAtLevel(last - 1), 1);
  1217. Delete("foo");
  1218. ASSERT_EQ(AllEntriesFor("foo"), "[ DEL, v1 ]");
  1219. ASSERT_LEVELDB_OK(dbfull()->TEST_CompactMemTable()); // Moves to level last-2
  1220. ASSERT_EQ(AllEntriesFor("foo"), "[ DEL, v1 ]");
  1221. dbfull()->TEST_CompactRange(last - 2, nullptr, nullptr);
  1222. // DEL kept: "last" file overlaps
  1223. ASSERT_EQ(AllEntriesFor("foo"), "[ DEL, v1 ]");
  1224. dbfull()->TEST_CompactRange(last - 1, nullptr, nullptr);
  1225. // Merging last-1 w/ last, so we are the base level for "foo", so
  1226. // DEL is removed. (as is v1).
  1227. ASSERT_EQ(AllEntriesFor("foo"), "[ ]");
  1228. }
  1229. TEST_F(DBTest, OverlapInLevel0) {
  1230. do {
  1231. ASSERT_EQ(config::kMaxMemCompactLevel, 2) << "Fix test to match config";
  1232. // Fill levels 1 and 2 to disable the pushing of new memtables to levels >
  1233. // 0.
  1234. ASSERT_LEVELDB_OK(Put("100", "v100"));
  1235. ASSERT_LEVELDB_OK(Put("999", "v999"));
  1236. dbfull()->TEST_CompactMemTable();
  1237. ASSERT_LEVELDB_OK(Delete("100"));
  1238. ASSERT_LEVELDB_OK(Delete("999"));
  1239. dbfull()->TEST_CompactMemTable();
  1240. ASSERT_EQ("0,1,1", FilesPerLevel());
  1241. // Make files spanning the following ranges in level-0:
  1242. // files[0] 200 .. 900
  1243. // files[1] 300 .. 500
  1244. // Note that files are sorted by smallest key.
  1245. ASSERT_LEVELDB_OK(Put("300", "v300"));
  1246. ASSERT_LEVELDB_OK(Put("500", "v500"));
  1247. dbfull()->TEST_CompactMemTable();
  1248. ASSERT_LEVELDB_OK(Put("200", "v200"));
  1249. ASSERT_LEVELDB_OK(Put("600", "v600"));
  1250. ASSERT_LEVELDB_OK(Put("900", "v900"));
  1251. dbfull()->TEST_CompactMemTable();
  1252. ASSERT_EQ("2,1,1", FilesPerLevel());
  1253. // Compact away the placeholder files we created initially
  1254. dbfull()->TEST_CompactRange(1, nullptr, nullptr);
  1255. dbfull()->TEST_CompactRange(2, nullptr, nullptr);
  1256. ASSERT_EQ("2", FilesPerLevel());
  1257. // Do a memtable compaction. Before bug-fix, the compaction would
  1258. // not detect the overlap with level-0 files and would incorrectly place
  1259. // the deletion in a deeper level.
  1260. ASSERT_LEVELDB_OK(Delete("600"));
  1261. dbfull()->TEST_CompactMemTable();
  1262. ASSERT_EQ("3", FilesPerLevel());
  1263. ASSERT_EQ("NOT_FOUND", Get("600"));
  1264. } while (ChangeOptions());
  1265. }
  1266. TEST_F(DBTest, L0_CompactionBug_Issue44_a) {
  1267. Reopen();
  1268. ASSERT_LEVELDB_OK(Put("b", "v"));
  1269. Reopen();
  1270. ASSERT_LEVELDB_OK(Delete("b"));
  1271. ASSERT_LEVELDB_OK(Delete("a"));
  1272. Reopen();
  1273. ASSERT_LEVELDB_OK(Delete("a"));
  1274. Reopen();
  1275. ASSERT_LEVELDB_OK(Put("a", "v"));
  1276. Reopen();
  1277. Reopen();
  1278. ASSERT_EQ("(a->v)", Contents());
  1279. DelayMilliseconds(1000); // Wait for compaction to finish
  1280. ASSERT_EQ("(a->v)", Contents());
  1281. }
  1282. TEST_F(DBTest, L0_CompactionBug_Issue44_b) {
  1283. Reopen();
  1284. Put("", "");
  1285. Reopen();
  1286. Delete("e");
  1287. Put("", "");
  1288. Reopen();
  1289. Put("c", "cv");
  1290. Reopen();
  1291. Put("", "");
  1292. Reopen();
  1293. Put("", "");
  1294. DelayMilliseconds(1000); // Wait for compaction to finish
  1295. Reopen();
  1296. Put("d", "dv");
  1297. Reopen();
  1298. Put("", "");
  1299. Reopen();
  1300. Delete("d");
  1301. Delete("b");
  1302. Reopen();
  1303. ASSERT_EQ("(->)(c->cv)", Contents());
  1304. DelayMilliseconds(1000); // Wait for compaction to finish
  1305. ASSERT_EQ("(->)(c->cv)", Contents());
  1306. }
  1307. TEST_F(DBTest, Fflush_Issue474) {
  1308. static const int kNum = 100000;
  1309. Random rnd(test::RandomSeed());
  1310. for (int i = 0; i < kNum; i++) {
  1311. fflush(nullptr);
  1312. ASSERT_LEVELDB_OK(Put(RandomKey(&rnd), RandomString(&rnd, 100)));
  1313. }
  1314. }
  1315. TEST_F(DBTest, ComparatorCheck) {
  1316. class NewComparator : public Comparator {
  1317. public:
  1318. const char* Name() const override { return "leveldb.NewComparator"; }
  1319. int Compare(const Slice& a, const Slice& b) const override {
  1320. return BytewiseComparator()->Compare(a, b);
  1321. }
  1322. void FindShortestSeparator(std::string* s, const Slice& l) const override {
  1323. BytewiseComparator()->FindShortestSeparator(s, l);
  1324. }
  1325. void FindShortSuccessor(std::string* key) const override {
  1326. BytewiseComparator()->FindShortSuccessor(key);
  1327. }
  1328. };
  1329. NewComparator cmp;
  1330. Options new_options = CurrentOptions();
  1331. new_options.comparator = &cmp;
  1332. Status s = TryReopen(&new_options);
  1333. ASSERT_TRUE(!s.ok());
  1334. ASSERT_TRUE(s.ToString().find("comparator") != std::string::npos)
  1335. << s.ToString();
  1336. }
  1337. TEST_F(DBTest, CustomComparator) {
  1338. class NumberComparator : public Comparator {
  1339. public:
  1340. const char* Name() const override { return "test.NumberComparator"; }
  1341. int Compare(const Slice& a, const Slice& b) const override {
  1342. return ToNumber(a) - ToNumber(b);
  1343. }
  1344. void FindShortestSeparator(std::string* s, const Slice& l) const override {
  1345. ToNumber(*s); // Check format
  1346. ToNumber(l); // Check format
  1347. }
  1348. void FindShortSuccessor(std::string* key) const override {
  1349. ToNumber(*key); // Check format
  1350. }
  1351. private:
  1352. static int ToNumber(const Slice& x) {
  1353. // Check that there are no extra characters.
  1354. EXPECT_TRUE(x.size() >= 2 && x[0] == '[' && x[x.size() - 1] == ']')
  1355. << EscapeString(x);
  1356. int val;
  1357. char ignored;
  1358. EXPECT_TRUE(sscanf(x.ToString().c_str(), "[%i]%c", &val, &ignored) == 1)
  1359. << EscapeString(x);
  1360. return val;
  1361. }
  1362. };
  1363. NumberComparator cmp;
  1364. Options new_options = CurrentOptions();
  1365. new_options.create_if_missing = true;
  1366. new_options.comparator = &cmp;
  1367. new_options.filter_policy = nullptr; // Cannot use bloom filters
  1368. new_options.write_buffer_size = 1000; // Compact more often
  1369. DestroyAndReopen(&new_options);
  1370. ASSERT_LEVELDB_OK(Put("[10]", "ten"));
  1371. ASSERT_LEVELDB_OK(Put("[0x14]", "twenty"));
  1372. for (int i = 0; i < 2; i++) {
  1373. ASSERT_EQ("ten", Get("[10]"));
  1374. ASSERT_EQ("ten", Get("[0xa]"));
  1375. ASSERT_EQ("twenty", Get("[20]"));
  1376. ASSERT_EQ("twenty", Get("[0x14]"));
  1377. ASSERT_EQ("NOT_FOUND", Get("[15]"));
  1378. ASSERT_EQ("NOT_FOUND", Get("[0xf]"));
  1379. Compact("[0]", "[9999]");
  1380. }
  1381. for (int run = 0; run < 2; run++) {
  1382. for (int i = 0; i < 1000; i++) {
  1383. char buf[100];
  1384. snprintf(buf, sizeof(buf), "[%d]", i * 10);
  1385. ASSERT_LEVELDB_OK(Put(buf, buf));
  1386. }
  1387. Compact("[0]", "[1000000]");
  1388. }
  1389. }
  1390. TEST_F(DBTest, ManualCompaction) {
  1391. ASSERT_EQ(config::kMaxMemCompactLevel, 2)
  1392. << "Need to update this test to match kMaxMemCompactLevel";
  1393. MakeTables(3, "p", "q");
  1394. ASSERT_EQ("1,1,1", FilesPerLevel());
  1395. // Compaction range falls before files
  1396. Compact("", "c");
  1397. ASSERT_EQ("1,1,1", FilesPerLevel());
  1398. // Compaction range falls after files
  1399. Compact("r", "z");
  1400. ASSERT_EQ("1,1,1", FilesPerLevel());
  1401. // Compaction range overlaps files
  1402. Compact("p1", "p9");
  1403. ASSERT_EQ("0,0,1", FilesPerLevel());
  1404. // Populate a different range
  1405. MakeTables(3, "c", "e");
  1406. ASSERT_EQ("1,1,2", FilesPerLevel());
  1407. // Compact just the new range
  1408. Compact("b", "f");
  1409. ASSERT_EQ("0,0,2", FilesPerLevel());
  1410. // Compact all
  1411. MakeTables(1, "a", "z");
  1412. ASSERT_EQ("0,1,2", FilesPerLevel());
  1413. db_->CompactRange(nullptr, nullptr);
  1414. ASSERT_EQ("0,0,1", FilesPerLevel());
  1415. }
  1416. TEST_F(DBTest, DBOpen_Options) {
  1417. std::string dbname = testing::TempDir() + "db_options_test";
  1418. DestroyDB(dbname, Options());
  1419. // Does not exist, and create_if_missing == false: error
  1420. DB* db = nullptr;
  1421. Options opts;
  1422. opts.create_if_missing = false;
  1423. Status s = DB::Open(opts, dbname, &db);
  1424. ASSERT_TRUE(strstr(s.ToString().c_str(), "does not exist") != nullptr);
  1425. ASSERT_TRUE(db == nullptr);
  1426. // Does not exist, and create_if_missing == true: OK
  1427. opts.create_if_missing = true;
  1428. s = DB::Open(opts, dbname, &db);
  1429. ASSERT_LEVELDB_OK(s);
  1430. ASSERT_TRUE(db != nullptr);
  1431. delete db;
  1432. db = nullptr;
  1433. // Does exist, and error_if_exists == true: error
  1434. opts.create_if_missing = false;
  1435. opts.error_if_exists = true;
  1436. s = DB::Open(opts, dbname, &db);
  1437. ASSERT_TRUE(strstr(s.ToString().c_str(), "exists") != nullptr);
  1438. ASSERT_TRUE(db == nullptr);
  1439. // Does exist, and error_if_exists == false: OK
  1440. opts.create_if_missing = true;
  1441. opts.error_if_exists = false;
  1442. s = DB::Open(opts, dbname, &db);
  1443. ASSERT_LEVELDB_OK(s);
  1444. ASSERT_TRUE(db != nullptr);
  1445. delete db;
  1446. db = nullptr;
  1447. }
  1448. TEST_F(DBTest, DestroyEmptyDir) {
  1449. std::string dbname = testing::TempDir() + "db_empty_dir";
  1450. TestEnv env(Env::Default());
  1451. env.RemoveDir(dbname);
  1452. ASSERT_TRUE(!env.FileExists(dbname));
  1453. Options opts;
  1454. opts.env = &env;
  1455. ASSERT_LEVELDB_OK(env.CreateDir(dbname));
  1456. ASSERT_TRUE(env.FileExists(dbname));
  1457. std::vector<std::string> children;
  1458. ASSERT_LEVELDB_OK(env.GetChildren(dbname, &children));
  1459. // The stock Env's do not filter out '.' and '..' special files.
  1460. ASSERT_EQ(2, children.size());
  1461. ASSERT_LEVELDB_OK(DestroyDB(dbname, opts));
  1462. ASSERT_TRUE(!env.FileExists(dbname));
  1463. // Should also be destroyed if Env is filtering out dot files.
  1464. env.SetIgnoreDotFiles(true);
  1465. ASSERT_LEVELDB_OK(env.CreateDir(dbname));
  1466. ASSERT_TRUE(env.FileExists(dbname));
  1467. ASSERT_LEVELDB_OK(env.GetChildren(dbname, &children));
  1468. ASSERT_EQ(0, children.size());
  1469. ASSERT_LEVELDB_OK(DestroyDB(dbname, opts));
  1470. ASSERT_TRUE(!env.FileExists(dbname));
  1471. }
  1472. TEST_F(DBTest, DestroyOpenDB) {
  1473. std::string dbname = testing::TempDir() + "open_db_dir";
  1474. env_->RemoveDir(dbname);
  1475. ASSERT_TRUE(!env_->FileExists(dbname));
  1476. Options opts;
  1477. opts.create_if_missing = true;
  1478. DB* db = nullptr;
  1479. ASSERT_LEVELDB_OK(DB::Open(opts, dbname, &db));
  1480. ASSERT_TRUE(db != nullptr);
  1481. // Must fail to destroy an open db.
  1482. ASSERT_TRUE(env_->FileExists(dbname));
  1483. ASSERT_TRUE(!DestroyDB(dbname, Options()).ok());
  1484. ASSERT_TRUE(env_->FileExists(dbname));
  1485. delete db;
  1486. db = nullptr;
  1487. // Should succeed destroying a closed db.
  1488. ASSERT_LEVELDB_OK(DestroyDB(dbname, Options()));
  1489. ASSERT_TRUE(!env_->FileExists(dbname));
  1490. }
  1491. TEST_F(DBTest, Locking) {
  1492. DB* db2 = nullptr;
  1493. Status s = DB::Open(CurrentOptions(), dbname_, &db2);
  1494. ASSERT_TRUE(!s.ok()) << "Locking did not prevent re-opening db";
  1495. }
  1496. // Check that number of files does not grow when we are out of space
  1497. TEST_F(DBTest, NoSpace) {
  1498. Options options = CurrentOptions();
  1499. options.env = env_;
  1500. Reopen(&options);
  1501. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  1502. ASSERT_EQ("v1", Get("foo"));
  1503. Compact("a", "z");
  1504. const int num_files = CountFiles();
  1505. // Force out-of-space errors.
  1506. env_->no_space_.store(true, std::memory_order_release);
  1507. for (int i = 0; i < 10; i++) {
  1508. for (int level = 0; level < config::kNumLevels - 1; level++) {
  1509. dbfull()->TEST_CompactRange(level, nullptr, nullptr);
  1510. }
  1511. }
  1512. env_->no_space_.store(false, std::memory_order_release);
  1513. ASSERT_LT(CountFiles(), num_files + 3);
  1514. }
  1515. TEST_F(DBTest, NonWritableFileSystem) {
  1516. Options options = CurrentOptions();
  1517. options.write_buffer_size = 1000;
  1518. options.env = env_;
  1519. Reopen(&options);
  1520. ASSERT_LEVELDB_OK(Put("foo", "v1"));
  1521. // Force errors for new files.
  1522. env_->non_writable_.store(true, std::memory_order_release);
  1523. std::string big(100000, 'x');
  1524. int errors = 0;
  1525. for (int i = 0; i < 20; i++) {
  1526. fprintf(stderr, "iter %d; errors %d\n", i, errors);
  1527. if (!Put("foo", big).ok()) {
  1528. errors++;
  1529. DelayMilliseconds(100);
  1530. }
  1531. }
  1532. ASSERT_GT(errors, 0);
  1533. env_->non_writable_.store(false, std::memory_order_release);
  1534. }
  1535. TEST_F(DBTest, WriteSyncError) {
  1536. // Check that log sync errors cause the DB to disallow future writes.
  1537. // (a) Cause log sync calls to fail
  1538. Options options = CurrentOptions();
  1539. options.env = env_;
  1540. Reopen(&options);
  1541. env_->data_sync_error_.store(true, std::memory_order_release);
  1542. // (b) Normal write should succeed
  1543. WriteOptions w;
  1544. ASSERT_LEVELDB_OK(db_->Put(w, "k1", "v1"));
  1545. ASSERT_EQ("v1", Get("k1"));
  1546. // (c) Do a sync write; should fail
  1547. w.sync = true;
  1548. ASSERT_TRUE(!db_->Put(w, "k2", "v2").ok());
  1549. ASSERT_EQ("v1", Get("k1"));
  1550. ASSERT_EQ("NOT_FOUND", Get("k2"));
  1551. // (d) make sync behave normally
  1552. env_->data_sync_error_.store(false, std::memory_order_release);
  1553. // (e) Do a non-sync write; should fail
  1554. w.sync = false;
  1555. ASSERT_TRUE(!db_->Put(w, "k3", "v3").ok());
  1556. ASSERT_EQ("v1", Get("k1"));
  1557. ASSERT_EQ("NOT_FOUND", Get("k2"));
  1558. ASSERT_EQ("NOT_FOUND", Get("k3"));
  1559. }
  1560. TEST_F(DBTest, ManifestWriteError) {
  1561. // Test for the following problem:
  1562. // (a) Compaction produces file F
  1563. // (b) Log record containing F is written to MANIFEST file, but Sync() fails
  1564. // (c) GC deletes F
  1565. // (d) After reopening DB, reads fail since deleted F is named in log record
  1566. // We iterate twice. In the second iteration, everything is the
  1567. // same except the log record never makes it to the MANIFEST file.
  1568. for (int iter = 0; iter < 2; iter++) {
  1569. std::atomic<bool>* error_type = (iter == 0) ? &env_->manifest_sync_error_
  1570. : &env_->manifest_write_error_;
  1571. // Insert foo=>bar mapping
  1572. Options options = CurrentOptions();
  1573. options.env = env_;
  1574. options.create_if_missing = true;
  1575. options.error_if_exists = false;
  1576. DestroyAndReopen(&options);
  1577. ASSERT_LEVELDB_OK(Put("foo", "bar"));
  1578. ASSERT_EQ("bar", Get("foo"));
  1579. // Memtable compaction (will succeed)
  1580. dbfull()->TEST_CompactMemTable();
  1581. ASSERT_EQ("bar", Get("foo"));
  1582. const int last = config::kMaxMemCompactLevel;
  1583. ASSERT_EQ(NumTableFilesAtLevel(last), 1); // foo=>bar is now in last level
  1584. // Merging compaction (will fail)
  1585. error_type->store(true, std::memory_order_release);
  1586. dbfull()->TEST_CompactRange(last, nullptr, nullptr); // Should fail
  1587. ASSERT_EQ("bar", Get("foo"));
  1588. // Recovery: should not lose data
  1589. error_type->store(false, std::memory_order_release);
  1590. Reopen(&options);
  1591. ASSERT_EQ("bar", Get("foo"));
  1592. }
  1593. }
  1594. TEST_F(DBTest, MissingSSTFile) {
  1595. ASSERT_LEVELDB_OK(Put("foo", "bar"));
  1596. ASSERT_EQ("bar", Get("foo"));
  1597. // Dump the memtable to disk.
  1598. dbfull()->TEST_CompactMemTable();
  1599. ASSERT_EQ("bar", Get("foo"));
  1600. Close();
  1601. ASSERT_TRUE(DeleteAnSSTFile());
  1602. Options options = CurrentOptions();
  1603. options.paranoid_checks = true;
  1604. Status s = TryReopen(&options);
  1605. ASSERT_TRUE(!s.ok());
  1606. ASSERT_TRUE(s.ToString().find("issing") != std::string::npos) << s.ToString();
  1607. }
  1608. TEST_F(DBTest, StillReadSST) {
  1609. ASSERT_LEVELDB_OK(Put("foo", "bar"));
  1610. ASSERT_EQ("bar", Get("foo"));
  1611. // Dump the memtable to disk.
  1612. dbfull()->TEST_CompactMemTable();
  1613. ASSERT_EQ("bar", Get("foo"));
  1614. Close();
  1615. ASSERT_GT(RenameLDBToSST(), 0);
  1616. Options options = CurrentOptions();
  1617. options.paranoid_checks = true;
  1618. Status s = TryReopen(&options);
  1619. ASSERT_TRUE(s.ok());
  1620. ASSERT_EQ("bar", Get("foo"));
  1621. }
  1622. TEST_F(DBTest, FilesDeletedAfterCompaction) {
  1623. ASSERT_LEVELDB_OK(Put("foo", "v2"));
  1624. Compact("a", "z");
  1625. const int num_files = CountFiles();
  1626. for (int i = 0; i < 10; i++) {
  1627. ASSERT_LEVELDB_OK(Put("foo", "v2"));
  1628. Compact("a", "z");
  1629. }
  1630. ASSERT_EQ(CountFiles(), num_files);
  1631. }
  1632. TEST_F(DBTest, BloomFilter) {
  1633. env_->count_random_reads_ = true;
  1634. Options options = CurrentOptions();
  1635. options.env = env_;
  1636. options.block_cache = NewLRUCache(0); // Prevent cache hits
  1637. options.filter_policy = NewBloomFilterPolicy(10);
  1638. Reopen(&options);
  1639. // Populate multiple layers
  1640. const int N = 10000;
  1641. for (int i = 0; i < N; i++) {
  1642. ASSERT_LEVELDB_OK(Put(Key(i), Key(i)));
  1643. }
  1644. Compact("a", "z");
  1645. for (int i = 0; i < N; i += 100) {
  1646. ASSERT_LEVELDB_OK(Put(Key(i), Key(i)));
  1647. }
  1648. dbfull()->TEST_CompactMemTable();
  1649. // Prevent auto compactions triggered by seeks
  1650. env_->delay_data_sync_.store(true, std::memory_order_release);
  1651. // Lookup present keys. Should rarely read from small sstable.
  1652. env_->random_read_counter_.Reset();
  1653. for (int i = 0; i < N; i++) {
  1654. ASSERT_EQ(Key(i), Get(Key(i)));
  1655. }
  1656. int reads = env_->random_read_counter_.Read();
  1657. fprintf(stderr, "%d present => %d reads\n", N, reads);
  1658. ASSERT_GE(reads, N);
  1659. ASSERT_LE(reads, N + 2 * N / 100);
  1660. // Lookup present keys. Should rarely read from either sstable.
  1661. env_->random_read_counter_.Reset();
  1662. for (int i = 0; i < N; i++) {
  1663. ASSERT_EQ("NOT_FOUND", Get(Key(i) + ".missing"));
  1664. }
  1665. reads = env_->random_read_counter_.Read();
  1666. fprintf(stderr, "%d missing => %d reads\n", N, reads);
  1667. ASSERT_LE(reads, 3 * N / 100);
  1668. env_->delay_data_sync_.store(false, std::memory_order_release);
  1669. Close();
  1670. delete options.block_cache;
  1671. delete options.filter_policy;
  1672. }
  1673. // Multi-threaded test:
  1674. namespace {
  1675. static const int kNumThreads = 4;
  1676. static const int kTestSeconds = 10;
  1677. static const int kNumKeys = 1000;
  1678. struct MTState {
  1679. DBTest* test;
  1680. std::atomic<bool> stop;
  1681. std::atomic<int> counter[kNumThreads];
  1682. std::atomic<bool> thread_done[kNumThreads];
  1683. };
  1684. struct MTThread {
  1685. MTState* state;
  1686. int id;
  1687. };
  1688. static void MTThreadBody(void* arg) {
  1689. MTThread* t = reinterpret_cast<MTThread*>(arg);
  1690. int id = t->id;
  1691. DB* db = t->state->test->db_;
  1692. int counter = 0;
  1693. fprintf(stderr, "... starting thread %d\n", id);
  1694. Random rnd(1000 + id);
  1695. std::string value;
  1696. char valbuf[1500];
  1697. while (!t->state->stop.load(std::memory_order_acquire)) {
  1698. t->state->counter[id].store(counter, std::memory_order_release);
  1699. int key = rnd.Uniform(kNumKeys);
  1700. char keybuf[20];
  1701. snprintf(keybuf, sizeof(keybuf), "%016d", key);
  1702. if (rnd.OneIn(2)) {
  1703. // Write values of the form <key, my id, counter>.
  1704. // We add some padding for force compactions.
  1705. snprintf(valbuf, sizeof(valbuf), "%d.%d.%-1000d", key, id,
  1706. static_cast<int>(counter));
  1707. ASSERT_LEVELDB_OK(db->Put(WriteOptions(), Slice(keybuf), Slice(valbuf)));
  1708. } else {
  1709. // Read a value and verify that it matches the pattern written above.
  1710. Status s = db->Get(ReadOptions(), Slice(keybuf), &value);
  1711. if (s.IsNotFound()) {
  1712. // Key has not yet been written
  1713. } else {
  1714. // Check that the writer thread counter is >= the counter in the value
  1715. ASSERT_LEVELDB_OK(s);
  1716. int k, w, c;
  1717. ASSERT_EQ(3, sscanf(value.c_str(), "%d.%d.%d", &k, &w, &c)) << value;
  1718. ASSERT_EQ(k, key);
  1719. ASSERT_GE(w, 0);
  1720. ASSERT_LT(w, kNumThreads);
  1721. ASSERT_LE(c, t->state->counter[w].load(std::memory_order_acquire));
  1722. }
  1723. }
  1724. counter++;
  1725. }
  1726. t->state->thread_done[id].store(true, std::memory_order_release);
  1727. fprintf(stderr, "... stopping thread %d after %d ops\n", id, counter);
  1728. }
  1729. } // namespace
  1730. TEST_F(DBTest, MultiThreaded) {
  1731. do {
  1732. // Initialize state
  1733. MTState mt;
  1734. mt.test = this;
  1735. mt.stop.store(false, std::memory_order_release);
  1736. for (int id = 0; id < kNumThreads; id++) {
  1737. mt.counter[id].store(false, std::memory_order_release);
  1738. mt.thread_done[id].store(false, std::memory_order_release);
  1739. }
  1740. // Start threads
  1741. MTThread thread[kNumThreads];
  1742. for (int id = 0; id < kNumThreads; id++) {
  1743. thread[id].state = &mt;
  1744. thread[id].id = id;
  1745. env_->StartThread(MTThreadBody, &thread[id]);
  1746. }
  1747. // Let them run for a while
  1748. DelayMilliseconds(kTestSeconds * 1000);
  1749. // Stop the threads and wait for them to finish
  1750. mt.stop.store(true, std::memory_order_release);
  1751. for (int id = 0; id < kNumThreads; id++) {
  1752. while (!mt.thread_done[id].load(std::memory_order_acquire)) {
  1753. DelayMilliseconds(100);
  1754. }
  1755. }
  1756. } while (ChangeOptions());
  1757. }
  1758. namespace {
  1759. typedef std::map<std::string, std::string> KVMap;
  1760. }
  1761. class ModelDB : public DB {
  1762. public:
  1763. class ModelSnapshot : public Snapshot {
  1764. public:
  1765. KVMap map_;
  1766. };
  1767. explicit ModelDB(const Options& options) : options_(options) {}
  1768. ~ModelDB() override = default;
  1769. Status Put(const WriteOptions& o, const Slice& k, const Slice& v) override {
  1770. return DB::Put(o, k, v);
  1771. }
  1772. Status Delete(const WriteOptions& o, const Slice& key) override {
  1773. return DB::Delete(o, key);
  1774. }
  1775. Status Get(const ReadOptions& options, const Slice& key,
  1776. std::string* value) override {
  1777. assert(false); // Not implemented
  1778. return Status::NotFound(key);
  1779. }
  1780. Iterator* NewIterator(const ReadOptions& options) override {
  1781. if (options.snapshot == nullptr) {
  1782. KVMap* saved = new KVMap;
  1783. *saved = map_;
  1784. return new ModelIter(saved, true);
  1785. } else {
  1786. const KVMap* snapshot_state =
  1787. &(reinterpret_cast<const ModelSnapshot*>(options.snapshot)->map_);
  1788. return new ModelIter(snapshot_state, false);
  1789. }
  1790. }
  1791. const Snapshot* GetSnapshot() override {
  1792. ModelSnapshot* snapshot = new ModelSnapshot;
  1793. snapshot->map_ = map_;
  1794. return snapshot;
  1795. }
  1796. void ReleaseSnapshot(const Snapshot* snapshot) override {
  1797. delete reinterpret_cast<const ModelSnapshot*>(snapshot);
  1798. }
  1799. Status Write(const WriteOptions& options, WriteBatch* batch) override {
  1800. class Handler : public WriteBatch::Handler {
  1801. public:
  1802. KVMap* map_;
  1803. void Put(const Slice& key, const Slice& value) override {
  1804. (*map_)[key.ToString()] = value.ToString();
  1805. }
  1806. void Delete(const Slice& key) override { map_->erase(key.ToString()); }
  1807. };
  1808. Handler handler;
  1809. handler.map_ = &map_;
  1810. return batch->Iterate(&handler);
  1811. }
  1812. bool GetProperty(const Slice& property, std::string* value) override {
  1813. return false;
  1814. }
  1815. void GetApproximateSizes(const Range* r, int n, uint64_t* sizes) override {
  1816. for (int i = 0; i < n; i++) {
  1817. sizes[i] = 0;
  1818. }
  1819. }
  1820. void CompactRange(const Slice* start, const Slice* end) override {}
  1821. private:
  1822. class ModelIter : public Iterator {
  1823. public:
  1824. ModelIter(const KVMap* map, bool owned)
  1825. : map_(map), owned_(owned), iter_(map_->end()) {}
  1826. ~ModelIter() override {
  1827. if (owned_) delete map_;
  1828. }
  1829. bool Valid() const override { return iter_ != map_->end(); }
  1830. void SeekToFirst() override { iter_ = map_->begin(); }
  1831. void SeekToLast() override {
  1832. if (map_->empty()) {
  1833. iter_ = map_->end();
  1834. } else {
  1835. iter_ = map_->find(map_->rbegin()->first);
  1836. }
  1837. }
  1838. void Seek(const Slice& k) override {
  1839. iter_ = map_->lower_bound(k.ToString());
  1840. }
  1841. void Next() override { ++iter_; }
  1842. void Prev() override { --iter_; }
  1843. Slice key() const override { return iter_->first; }
  1844. Slice value() const override { return iter_->second; }
  1845. Status status() const override { return Status::OK(); }
  1846. private:
  1847. const KVMap* const map_;
  1848. const bool owned_; // Do we own map_
  1849. KVMap::const_iterator iter_;
  1850. };
  1851. const Options options_;
  1852. KVMap map_;
  1853. };
  1854. static bool CompareIterators(int step, DB* model, DB* db,
  1855. const Snapshot* model_snap,
  1856. const Snapshot* db_snap) {
  1857. ReadOptions options;
  1858. options.snapshot = model_snap;
  1859. Iterator* miter = model->NewIterator(options);
  1860. options.snapshot = db_snap;
  1861. Iterator* dbiter = db->NewIterator(options);
  1862. bool ok = true;
  1863. int count = 0;
  1864. for (miter->SeekToFirst(), dbiter->SeekToFirst();
  1865. ok && miter->Valid() && dbiter->Valid(); miter->Next(), dbiter->Next()) {
  1866. count++;
  1867. if (miter->key().compare(dbiter->key()) != 0) {
  1868. fprintf(stderr, "step %d: Key mismatch: '%s' vs. '%s'\n", step,
  1869. EscapeString(miter->key()).c_str(),
  1870. EscapeString(dbiter->key()).c_str());
  1871. ok = false;
  1872. break;
  1873. }
  1874. if (miter->value().compare(dbiter->value()) != 0) {
  1875. fprintf(stderr, "step %d: Value mismatch for key '%s': '%s' vs. '%s'\n",
  1876. step, EscapeString(miter->key()).c_str(),
  1877. EscapeString(miter->value()).c_str(),
  1878. EscapeString(miter->value()).c_str());
  1879. ok = false;
  1880. }
  1881. }
  1882. if (ok) {
  1883. if (miter->Valid() != dbiter->Valid()) {
  1884. fprintf(stderr, "step %d: Mismatch at end of iterators: %d vs. %d\n",
  1885. step, miter->Valid(), dbiter->Valid());
  1886. ok = false;
  1887. }
  1888. }
  1889. fprintf(stderr, "%d entries compared: ok=%d\n", count, ok);
  1890. delete miter;
  1891. delete dbiter;
  1892. return ok;
  1893. }
  1894. TEST_F(DBTest, Randomized) {
  1895. Random rnd(test::RandomSeed());
  1896. do {
  1897. ModelDB model(CurrentOptions());
  1898. const int N = 10000;
  1899. const Snapshot* model_snap = nullptr;
  1900. const Snapshot* db_snap = nullptr;
  1901. std::string k, v;
  1902. for (int step = 0; step < N; step++) {
  1903. if (step % 100 == 0) {
  1904. fprintf(stderr, "Step %d of %d\n", step, N);
  1905. }
  1906. // TODO(sanjay): Test Get() works
  1907. int p = rnd.Uniform(100);
  1908. if (p < 45) { // Put
  1909. k = RandomKey(&rnd);
  1910. v = RandomString(
  1911. &rnd, rnd.OneIn(20) ? 100 + rnd.Uniform(100) : rnd.Uniform(8));
  1912. ASSERT_LEVELDB_OK(model.Put(WriteOptions(), k, v));
  1913. ASSERT_LEVELDB_OK(db_->Put(WriteOptions(), k, v));
  1914. } else if (p < 90) { // Delete
  1915. k = RandomKey(&rnd);
  1916. ASSERT_LEVELDB_OK(model.Delete(WriteOptions(), k));
  1917. ASSERT_LEVELDB_OK(db_->Delete(WriteOptions(), k));
  1918. } else { // Multi-element batch
  1919. WriteBatch b;
  1920. const int num = rnd.Uniform(8);
  1921. for (int i = 0; i < num; i++) {
  1922. if (i == 0 || !rnd.OneIn(10)) {
  1923. k = RandomKey(&rnd);
  1924. } else {
  1925. // Periodically re-use the same key from the previous iter, so
  1926. // we have multiple entries in the write batch for the same key
  1927. }
  1928. if (rnd.OneIn(2)) {
  1929. v = RandomString(&rnd, rnd.Uniform(10));
  1930. b.Put(k, v);
  1931. } else {
  1932. b.Delete(k);
  1933. }
  1934. }
  1935. ASSERT_LEVELDB_OK(model.Write(WriteOptions(), &b));
  1936. ASSERT_LEVELDB_OK(db_->Write(WriteOptions(), &b));
  1937. }
  1938. if ((step % 100) == 0) {
  1939. ASSERT_TRUE(CompareIterators(step, &model, db_, nullptr, nullptr));
  1940. ASSERT_TRUE(CompareIterators(step, &model, db_, model_snap, db_snap));
  1941. // Save a snapshot from each DB this time that we'll use next
  1942. // time we compare things, to make sure the current state is
  1943. // preserved with the snapshot
  1944. if (model_snap != nullptr) model.ReleaseSnapshot(model_snap);
  1945. if (db_snap != nullptr) db_->ReleaseSnapshot(db_snap);
  1946. Reopen();
  1947. ASSERT_TRUE(CompareIterators(step, &model, db_, nullptr, nullptr));
  1948. model_snap = model.GetSnapshot();
  1949. db_snap = db_->GetSnapshot();
  1950. }
  1951. }
  1952. if (model_snap != nullptr) model.ReleaseSnapshot(model_snap);
  1953. if (db_snap != nullptr) db_->ReleaseSnapshot(db_snap);
  1954. } while (ChangeOptions());
  1955. }
  1956. std::string MakeKey(unsigned int num) {
  1957. char buf[30];
  1958. snprintf(buf, sizeof(buf), "%016u", num);
  1959. return std::string(buf);
  1960. }
  1961. void BM_LogAndApply(int iters, int num_base_files) {
  1962. std::string dbname = testing::TempDir() + "leveldb_test_benchmark";
  1963. DestroyDB(dbname, Options());
  1964. DB* db = nullptr;
  1965. Options opts;
  1966. opts.create_if_missing = true;
  1967. Status s = DB::Open(opts, dbname, &db);
  1968. ASSERT_LEVELDB_OK(s);
  1969. ASSERT_TRUE(db != nullptr);
  1970. delete db;
  1971. db = nullptr;
  1972. Env* env = Env::Default();
  1973. port::Mutex mu;
  1974. MutexLock l(&mu);
  1975. InternalKeyComparator cmp(BytewiseComparator());
  1976. Options options;
  1977. VersionSet vset(dbname, &options, nullptr, &cmp);
  1978. bool save_manifest;
  1979. ASSERT_LEVELDB_OK(vset.Recover(&save_manifest));
  1980. VersionEdit vbase;
  1981. uint64_t fnum = 1;
  1982. for (int i = 0; i < num_base_files; i++) {
  1983. InternalKey start(MakeKey(2 * fnum), 1, kTypeValue);
  1984. InternalKey limit(MakeKey(2 * fnum + 1), 1, kTypeDeletion);
  1985. vbase.AddFile(2, fnum++, 1 /* file size */, start, limit);
  1986. }
  1987. ASSERT_LEVELDB_OK(vset.LogAndApply(&vbase, &mu));
  1988. uint64_t start_micros = env->NowMicros();
  1989. for (int i = 0; i < iters; i++) {
  1990. VersionEdit vedit;
  1991. vedit.RemoveFile(2, fnum);
  1992. InternalKey start(MakeKey(2 * fnum), 1, kTypeValue);
  1993. InternalKey limit(MakeKey(2 * fnum + 1), 1, kTypeDeletion);
  1994. vedit.AddFile(2, fnum++, 1 /* file size */, start, limit);
  1995. vset.LogAndApply(&vedit, &mu);
  1996. }
  1997. uint64_t stop_micros = env->NowMicros();
  1998. unsigned int us = stop_micros - start_micros;
  1999. char buf[16];
  2000. snprintf(buf, sizeof(buf), "%d", num_base_files);
  2001. fprintf(stderr,
  2002. "BM_LogAndApply/%-6s %8d iters : %9u us (%7.0f us / iter)\n", buf,
  2003. iters, us, ((float)us) / iters);
  2004. }
  2005. } // namespace leveldb
  2006. int main(int argc, char** argv) {
  2007. if (argc > 1 && std::string(argv[1]) == "--benchmark") {
  2008. leveldb::BM_LogAndApply(1000, 1);
  2009. leveldb::BM_LogAndApply(1000, 100);
  2010. leveldb::BM_LogAndApply(1000, 10000);
  2011. leveldb::BM_LogAndApply(100, 100000);
  2012. return 0;
  2013. }
  2014. testing::InitGoogleTest(&argc, argv);
  2015. return RUN_ALL_TESTS();
  2016. }