提供基本的ttl测试用例
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

336 строки
9.0 KiB

  1. // Copyright (c) 2014 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "gtest/gtest.h"
  5. #include "db/db_impl.h"
  6. #include "db/filename.h"
  7. #include "db/version_set.h"
  8. #include "db/write_batch_internal.h"
  9. #include "leveldb/db.h"
  10. #include "leveldb/env.h"
  11. #include "leveldb/write_batch.h"
  12. #include "util/logging.h"
  13. #include "util/testutil.h"
  14. namespace leveldb {
  15. class RecoveryTest : public testing::Test {
  16. public:
  17. RecoveryTest() : env_(Env::Default()), db_(nullptr) {
  18. dbname_ = testing::TempDir() + "/recovery_test";
  19. DestroyDB(dbname_, Options());
  20. Open();
  21. }
  22. ~RecoveryTest() {
  23. Close();
  24. DestroyDB(dbname_, Options());
  25. }
  26. DBImpl* dbfull() const { return reinterpret_cast<DBImpl*>(db_); }
  27. Env* env() const { return env_; }
  28. bool CanAppend() {
  29. WritableFile* tmp;
  30. Status s = env_->NewAppendableFile(CurrentFileName(dbname_), &tmp);
  31. delete tmp;
  32. if (s.IsNotSupportedError()) {
  33. return false;
  34. } else {
  35. return true;
  36. }
  37. }
  38. void Close() {
  39. delete db_;
  40. db_ = nullptr;
  41. }
  42. Status OpenWithStatus(Options* options = nullptr) {
  43. Close();
  44. Options opts;
  45. if (options != nullptr) {
  46. opts = *options;
  47. } else {
  48. opts.reuse_logs = true; // TODO(sanjay): test both ways
  49. opts.create_if_missing = true;
  50. }
  51. if (opts.env == nullptr) {
  52. opts.env = env_;
  53. }
  54. return DB::Open(opts, dbname_, &db_);
  55. }
  56. void Open(Options* options = nullptr) {
  57. ASSERT_LEVELDB_OK(OpenWithStatus(options));
  58. ASSERT_EQ(1, NumLogs());
  59. }
  60. Status Put(const std::string& k, const std::string& v) {
  61. return db_->Put(WriteOptions(), k, v);
  62. }
  63. std::string Get(const std::string& k, const Snapshot* snapshot = nullptr) {
  64. std::string result;
  65. Status s = db_->Get(ReadOptions(), k, &result);
  66. if (s.IsNotFound()) {
  67. result = "NOT_FOUND";
  68. } else if (!s.ok()) {
  69. result = s.ToString();
  70. }
  71. return result;
  72. }
  73. std::string ManifestFileName() {
  74. std::string current;
  75. EXPECT_LEVELDB_OK(
  76. ReadFileToString(env_, CurrentFileName(dbname_), &current));
  77. size_t len = current.size();
  78. if (len > 0 && current[len - 1] == '\n') {
  79. current.resize(len - 1);
  80. }
  81. return dbname_ + "/" + current;
  82. }
  83. std::string LogName(uint64_t number) { return LogFileName(dbname_, number); }
  84. size_t DeleteLogFiles() {
  85. // Linux allows unlinking open files, but Windows does not.
  86. // Closing the db allows for file deletion.
  87. Close();
  88. std::vector<uint64_t> logs = GetFiles(kLogFile);
  89. for (size_t i = 0; i < logs.size(); i++) {
  90. EXPECT_LEVELDB_OK(env_->DeleteFile(LogName(logs[i]))) << LogName(logs[i]);
  91. }
  92. return logs.size();
  93. }
  94. void DeleteManifestFile() {
  95. ASSERT_LEVELDB_OK(env_->DeleteFile(ManifestFileName()));
  96. }
  97. uint64_t FirstLogFile() { return GetFiles(kLogFile)[0]; }
  98. std::vector<uint64_t> GetFiles(FileType t) {
  99. std::vector<std::string> filenames;
  100. EXPECT_LEVELDB_OK(env_->GetChildren(dbname_, &filenames));
  101. std::vector<uint64_t> result;
  102. for (size_t i = 0; i < filenames.size(); i++) {
  103. uint64_t number;
  104. FileType type;
  105. if (ParseFileName(filenames[i], &number, &type) && type == t) {
  106. result.push_back(number);
  107. }
  108. }
  109. return result;
  110. }
  111. int NumLogs() { return GetFiles(kLogFile).size(); }
  112. int NumTables() { return GetFiles(kTableFile).size(); }
  113. uint64_t FileSize(const std::string& fname) {
  114. uint64_t result;
  115. EXPECT_LEVELDB_OK(env_->GetFileSize(fname, &result)) << fname;
  116. return result;
  117. }
  118. void CompactMemTable() { dbfull()->TEST_CompactMemTable(); }
  119. // Directly construct a log file that sets key to val.
  120. void MakeLogFile(uint64_t lognum, SequenceNumber seq, Slice key, Slice val) {
  121. std::string fname = LogFileName(dbname_, lognum);
  122. WritableFile* file;
  123. ASSERT_LEVELDB_OK(env_->NewWritableFile(fname, &file));
  124. log::Writer writer(file);
  125. WriteBatch batch;
  126. batch.Put(key, val);
  127. WriteBatchInternal::SetSequence(&batch, seq);
  128. ASSERT_LEVELDB_OK(writer.AddRecord(WriteBatchInternal::Contents(&batch)));
  129. ASSERT_LEVELDB_OK(file->Flush());
  130. delete file;
  131. }
  132. private:
  133. std::string dbname_;
  134. Env* env_;
  135. DB* db_;
  136. };
  137. TEST_F(RecoveryTest, ManifestReused) {
  138. if (!CanAppend()) {
  139. fprintf(stderr, "skipping test because env does not support appending\n");
  140. return;
  141. }
  142. ASSERT_LEVELDB_OK(Put("foo", "bar"));
  143. Close();
  144. std::string old_manifest = ManifestFileName();
  145. Open();
  146. ASSERT_EQ(old_manifest, ManifestFileName());
  147. ASSERT_EQ("bar", Get("foo"));
  148. Open();
  149. ASSERT_EQ(old_manifest, ManifestFileName());
  150. ASSERT_EQ("bar", Get("foo"));
  151. }
  152. TEST_F(RecoveryTest, LargeManifestCompacted) {
  153. if (!CanAppend()) {
  154. fprintf(stderr, "skipping test because env does not support appending\n");
  155. return;
  156. }
  157. ASSERT_LEVELDB_OK(Put("foo", "bar"));
  158. Close();
  159. std::string old_manifest = ManifestFileName();
  160. // Pad with zeroes to make manifest file very big.
  161. {
  162. uint64_t len = FileSize(old_manifest);
  163. WritableFile* file;
  164. ASSERT_LEVELDB_OK(env()->NewAppendableFile(old_manifest, &file));
  165. std::string zeroes(3 * 1048576 - static_cast<size_t>(len), 0);
  166. ASSERT_LEVELDB_OK(file->Append(zeroes));
  167. ASSERT_LEVELDB_OK(file->Flush());
  168. delete file;
  169. }
  170. Open();
  171. std::string new_manifest = ManifestFileName();
  172. ASSERT_NE(old_manifest, new_manifest);
  173. ASSERT_GT(10000, FileSize(new_manifest));
  174. ASSERT_EQ("bar", Get("foo"));
  175. Open();
  176. ASSERT_EQ(new_manifest, ManifestFileName());
  177. ASSERT_EQ("bar", Get("foo"));
  178. }
  179. TEST_F(RecoveryTest, NoLogFiles) {
  180. ASSERT_LEVELDB_OK(Put("foo", "bar"));
  181. ASSERT_EQ(1, DeleteLogFiles());
  182. Open();
  183. ASSERT_EQ("NOT_FOUND", Get("foo"));
  184. Open();
  185. ASSERT_EQ("NOT_FOUND", Get("foo"));
  186. }
  187. TEST_F(RecoveryTest, LogFileReuse) {
  188. if (!CanAppend()) {
  189. fprintf(stderr, "skipping test because env does not support appending\n");
  190. return;
  191. }
  192. for (int i = 0; i < 2; i++) {
  193. ASSERT_LEVELDB_OK(Put("foo", "bar"));
  194. if (i == 0) {
  195. // Compact to ensure current log is empty
  196. CompactMemTable();
  197. }
  198. Close();
  199. ASSERT_EQ(1, NumLogs());
  200. uint64_t number = FirstLogFile();
  201. if (i == 0) {
  202. ASSERT_EQ(0, FileSize(LogName(number)));
  203. } else {
  204. ASSERT_LT(0, FileSize(LogName(number)));
  205. }
  206. Open();
  207. ASSERT_EQ(1, NumLogs());
  208. ASSERT_EQ(number, FirstLogFile()) << "did not reuse log file";
  209. ASSERT_EQ("bar", Get("foo"));
  210. Open();
  211. ASSERT_EQ(1, NumLogs());
  212. ASSERT_EQ(number, FirstLogFile()) << "did not reuse log file";
  213. ASSERT_EQ("bar", Get("foo"));
  214. }
  215. }
  216. TEST_F(RecoveryTest, MultipleMemTables) {
  217. // Make a large log.
  218. const int kNum = 1000;
  219. for (int i = 0; i < kNum; i++) {
  220. char buf[100];
  221. snprintf(buf, sizeof(buf), "%050d", i);
  222. ASSERT_LEVELDB_OK(Put(buf, buf));
  223. }
  224. ASSERT_EQ(0, NumTables());
  225. Close();
  226. ASSERT_EQ(0, NumTables());
  227. ASSERT_EQ(1, NumLogs());
  228. uint64_t old_log_file = FirstLogFile();
  229. // Force creation of multiple memtables by reducing the write buffer size.
  230. Options opt;
  231. opt.reuse_logs = true;
  232. opt.write_buffer_size = (kNum * 100) / 2;
  233. Open(&opt);
  234. ASSERT_LE(2, NumTables());
  235. ASSERT_EQ(1, NumLogs());
  236. ASSERT_NE(old_log_file, FirstLogFile()) << "must not reuse log";
  237. for (int i = 0; i < kNum; i++) {
  238. char buf[100];
  239. snprintf(buf, sizeof(buf), "%050d", i);
  240. ASSERT_EQ(buf, Get(buf));
  241. }
  242. }
  243. TEST_F(RecoveryTest, MultipleLogFiles) {
  244. ASSERT_LEVELDB_OK(Put("foo", "bar"));
  245. Close();
  246. ASSERT_EQ(1, NumLogs());
  247. // Make a bunch of uncompacted log files.
  248. uint64_t old_log = FirstLogFile();
  249. MakeLogFile(old_log + 1, 1000, "hello", "world");
  250. MakeLogFile(old_log + 2, 1001, "hi", "there");
  251. MakeLogFile(old_log + 3, 1002, "foo", "bar2");
  252. // Recover and check that all log files were processed.
  253. Open();
  254. ASSERT_LE(1, NumTables());
  255. ASSERT_EQ(1, NumLogs());
  256. uint64_t new_log = FirstLogFile();
  257. ASSERT_LE(old_log + 3, new_log);
  258. ASSERT_EQ("bar2", Get("foo"));
  259. ASSERT_EQ("world", Get("hello"));
  260. ASSERT_EQ("there", Get("hi"));
  261. // Test that previous recovery produced recoverable state.
  262. Open();
  263. ASSERT_LE(1, NumTables());
  264. ASSERT_EQ(1, NumLogs());
  265. if (CanAppend()) {
  266. ASSERT_EQ(new_log, FirstLogFile());
  267. }
  268. ASSERT_EQ("bar2", Get("foo"));
  269. ASSERT_EQ("world", Get("hello"));
  270. ASSERT_EQ("there", Get("hi"));
  271. // Check that introducing an older log file does not cause it to be re-read.
  272. Close();
  273. MakeLogFile(old_log + 1, 2000, "hello", "stale write");
  274. Open();
  275. ASSERT_LE(1, NumTables());
  276. ASSERT_EQ(1, NumLogs());
  277. if (CanAppend()) {
  278. ASSERT_EQ(new_log, FirstLogFile());
  279. }
  280. ASSERT_EQ("bar2", Get("foo"));
  281. ASSERT_EQ("world", Get("hello"));
  282. ASSERT_EQ("there", Get("hi"));
  283. }
  284. TEST_F(RecoveryTest, ManifestMissing) {
  285. ASSERT_LEVELDB_OK(Put("foo", "bar"));
  286. Close();
  287. DeleteManifestFile();
  288. Status status = OpenWithStatus();
  289. ASSERT_TRUE(status.IsCorruption());
  290. }
  291. } // namespace leveldb
  292. int main(int argc, char** argv) {
  293. testing::InitGoogleTest(&argc, argv);
  294. return RUN_ALL_TESTS();
  295. }