db_sanity_test.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <memory>
  8. #include <vector>
  9. #include "port/port.h"
  10. #include "rocksdb/comparator.h"
  11. #include "rocksdb/db.h"
  12. #include "rocksdb/env.h"
  13. #include "rocksdb/filter_policy.h"
  14. #include "rocksdb/options.h"
  15. #include "rocksdb/slice.h"
  16. #include "rocksdb/slice_transform.h"
  17. #include "rocksdb/status.h"
  18. #include "rocksdb/table.h"
  19. #include "util/string_util.h"
  20. namespace ROCKSDB_NAMESPACE {
  21. class SanityTest {
  22. public:
  23. explicit SanityTest(const std::string& path)
  24. : env_(Env::Default()), path_(path) {
  25. env_->CreateDirIfMissing(path);
  26. }
  27. virtual ~SanityTest() {}
  28. virtual std::string Name() const = 0;
  29. virtual Options GetOptions() const = 0;
  30. Status Create() {
  31. Options options = GetOptions();
  32. options.create_if_missing = true;
  33. std::string dbname = path_ + Name();
  34. Status s = DestroyDB(dbname, options);
  35. if (!s.ok()) {
  36. return s;
  37. }
  38. DB* db = nullptr;
  39. s = DB::Open(options, dbname, &db);
  40. std::unique_ptr<DB> db_guard(db);
  41. if (!s.ok()) {
  42. return s;
  43. }
  44. for (int i = 0; i < 1000000; ++i) {
  45. std::string k = "key" + std::to_string(i);
  46. std::string v = "value" + std::to_string(i);
  47. s = db->Put(WriteOptions(), Slice(k), Slice(v));
  48. if (!s.ok()) {
  49. return s;
  50. }
  51. }
  52. return db->Flush(FlushOptions());
  53. }
  54. Status Verify() {
  55. DB* db = nullptr;
  56. std::string dbname = path_ + Name();
  57. Status s = DB::Open(GetOptions(), dbname, &db);
  58. std::unique_ptr<DB> db_guard(db);
  59. if (!s.ok()) {
  60. return s;
  61. }
  62. for (int i = 0; i < 1000000; ++i) {
  63. std::string k = "key" + std::to_string(i);
  64. std::string v = "value" + std::to_string(i);
  65. std::string result;
  66. s = db->Get(ReadOptions(), Slice(k), &result);
  67. if (!s.ok()) {
  68. return s;
  69. }
  70. if (result != v) {
  71. return Status::Corruption("Unexpected value for key " + k);
  72. }
  73. }
  74. return Status::OK();
  75. }
  76. private:
  77. Env* env_;
  78. std::string const path_;
  79. };
  80. class SanityTestBasic : public SanityTest {
  81. public:
  82. explicit SanityTestBasic(const std::string& path) : SanityTest(path) {}
  83. Options GetOptions() const override {
  84. Options options;
  85. options.create_if_missing = true;
  86. return options;
  87. }
  88. std::string Name() const override { return "Basic"; }
  89. };
  90. class SanityTestSpecialComparator : public SanityTest {
  91. public:
  92. explicit SanityTestSpecialComparator(const std::string& path)
  93. : SanityTest(path) {
  94. options_.comparator = new NewComparator();
  95. }
  96. ~SanityTestSpecialComparator() { delete options_.comparator; }
  97. Options GetOptions() const override { return options_; }
  98. std::string Name() const override { return "SpecialComparator"; }
  99. private:
  100. class NewComparator : public Comparator {
  101. public:
  102. const char* Name() const override { return "rocksdb.NewComparator"; }
  103. int Compare(const Slice& a, const Slice& b) const override {
  104. return BytewiseComparator()->Compare(a, b);
  105. }
  106. void FindShortestSeparator(std::string* s, const Slice& l) const override {
  107. BytewiseComparator()->FindShortestSeparator(s, l);
  108. }
  109. void FindShortSuccessor(std::string* key) const override {
  110. BytewiseComparator()->FindShortSuccessor(key);
  111. }
  112. };
  113. Options options_;
  114. };
  115. class SanityTestZlibCompression : public SanityTest {
  116. public:
  117. explicit SanityTestZlibCompression(const std::string& path)
  118. : SanityTest(path) {
  119. options_.compression = kZlibCompression;
  120. }
  121. Options GetOptions() const override { return options_; }
  122. std::string Name() const override { return "ZlibCompression"; }
  123. private:
  124. Options options_;
  125. };
  126. class SanityTestZlibCompressionVersion2 : public SanityTest {
  127. public:
  128. explicit SanityTestZlibCompressionVersion2(const std::string& path)
  129. : SanityTest(path) {
  130. options_.compression = kZlibCompression;
  131. BlockBasedTableOptions table_options;
  132. #if ROCKSDB_MAJOR > 3 || (ROCKSDB_MAJOR == 3 && ROCKSDB_MINOR >= 10)
  133. table_options.format_version = 2;
  134. #endif
  135. options_.table_factory.reset(NewBlockBasedTableFactory(table_options));
  136. }
  137. Options GetOptions() const override { return options_; }
  138. std::string Name() const override { return "ZlibCompressionVersion2"; }
  139. private:
  140. Options options_;
  141. };
  142. class SanityTestLZ4Compression : public SanityTest {
  143. public:
  144. explicit SanityTestLZ4Compression(const std::string& path)
  145. : SanityTest(path) {
  146. options_.compression = kLZ4Compression;
  147. }
  148. Options GetOptions() const override { return options_; }
  149. std::string Name() const override { return "LZ4Compression"; }
  150. private:
  151. Options options_;
  152. };
  153. class SanityTestLZ4HCCompression : public SanityTest {
  154. public:
  155. explicit SanityTestLZ4HCCompression(const std::string& path)
  156. : SanityTest(path) {
  157. options_.compression = kLZ4HCCompression;
  158. }
  159. Options GetOptions() const override { return options_; }
  160. std::string Name() const override { return "LZ4HCCompression"; }
  161. private:
  162. Options options_;
  163. };
  164. class SanityTestZSTDCompression : public SanityTest {
  165. public:
  166. explicit SanityTestZSTDCompression(const std::string& path)
  167. : SanityTest(path) {
  168. options_.compression = kZSTD;
  169. }
  170. Options GetOptions() const override { return options_; }
  171. std::string Name() const override { return "ZSTDCompression"; }
  172. private:
  173. Options options_;
  174. };
  175. class SanityTestPlainTableFactory : public SanityTest {
  176. public:
  177. explicit SanityTestPlainTableFactory(const std::string& path)
  178. : SanityTest(path) {
  179. options_.table_factory.reset(NewPlainTableFactory());
  180. options_.prefix_extractor.reset(NewFixedPrefixTransform(2));
  181. options_.allow_mmap_reads = true;
  182. }
  183. ~SanityTestPlainTableFactory() {}
  184. Options GetOptions() const override { return options_; }
  185. std::string Name() const override { return "PlainTable"; }
  186. private:
  187. Options options_;
  188. };
  189. class SanityTestBloomFilter : public SanityTest {
  190. public:
  191. explicit SanityTestBloomFilter(const std::string& path) : SanityTest(path) {
  192. BlockBasedTableOptions table_options;
  193. table_options.filter_policy.reset(NewBloomFilterPolicy(10));
  194. options_.table_factory.reset(NewBlockBasedTableFactory(table_options));
  195. }
  196. ~SanityTestBloomFilter() {}
  197. Options GetOptions() const override { return options_; }
  198. std::string Name() const override { return "BloomFilter"; }
  199. private:
  200. Options options_;
  201. };
  202. namespace {
  203. bool RunSanityTests(const std::string& command, const std::string& path) {
  204. bool result = true;
  205. // Suppress false positive clang static anaylzer warnings.
  206. #ifndef __clang_analyzer__
  207. std::vector<SanityTest*> sanity_tests = {
  208. new SanityTestBasic(path),
  209. new SanityTestSpecialComparator(path),
  210. new SanityTestZlibCompression(path),
  211. new SanityTestZlibCompressionVersion2(path),
  212. new SanityTestLZ4Compression(path),
  213. new SanityTestLZ4HCCompression(path),
  214. new SanityTestZSTDCompression(path),
  215. new SanityTestPlainTableFactory(path),
  216. new SanityTestBloomFilter(path)};
  217. if (command == "create") {
  218. fprintf(stderr, "Creating...\n");
  219. } else {
  220. fprintf(stderr, "Verifying...\n");
  221. }
  222. for (auto sanity_test : sanity_tests) {
  223. Status s;
  224. fprintf(stderr, "%s -- ", sanity_test->Name().c_str());
  225. if (command == "create") {
  226. s = sanity_test->Create();
  227. } else {
  228. assert(command == "verify");
  229. s = sanity_test->Verify();
  230. }
  231. fprintf(stderr, "%s\n", s.ToString().c_str());
  232. if (!s.ok()) {
  233. fprintf(stderr, "FAIL\n");
  234. result = false;
  235. }
  236. delete sanity_test;
  237. }
  238. #endif // __clang_analyzer__
  239. return result;
  240. }
  241. } // namespace
  242. } // namespace ROCKSDB_NAMESPACE
  243. int main(int argc, char** argv) {
  244. std::string path, command;
  245. bool ok = (argc == 3);
  246. if (ok) {
  247. path = std::string(argv[1]);
  248. command = std::string(argv[2]);
  249. ok = (command == "create" || command == "verify");
  250. }
  251. if (!ok) {
  252. fprintf(stderr, "Usage: %s <path> [create|verify] \n", argv[0]);
  253. exit(1);
  254. }
  255. if (path.back() != '/') {
  256. path += "/";
  257. }
  258. bool sanity_ok = ROCKSDB_NAMESPACE::RunSanityTests(command, path);
  259. return sanity_ok ? 0 : 1;
  260. }