提供基本的ttl测试用例
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

591 líneas
16 KiB

This CL fixes a bug encountered when reading records from leveldb files that have been split, as in a [] input task split. Detailed description: Suppose an input split is generated between two leveldb record blocks and the preceding block ends with null padding. A reader that previously read at least 1 record within the first block (before encountering the padding) upon trying to read the next record, will successfully and correctly read the next logical record from the subsequent block, but will return a last record offset pointing to the padding in the first block. When this happened in a [], it resulted in duplicate records being handled at what appeared to be different offsets that were separated by only a few bytes. This behavior is only observed when at least 1 record was read from the first block before encountering the padding. If the initial offset for a reader was within the padding, the correct record offset would be reported, namely the offset within the second block. The tests failed to catch this scenario/bug, because each read test only read a single record with an initial offset. This CL adds an explicit test case for this scenario, and modifies the test structure to read all remaining records in the test case after an initial offset is specified. Thus an initial offset that jumps to record #3, with 5 total records in the test file, will result in reading 2 records, and validating the offset of each of them in order to pass successfully. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=115338487
hace 8 años
This CL fixes a bug encountered when reading records from leveldb files that have been split, as in a [] input task split. Detailed description: Suppose an input split is generated between two leveldb record blocks and the preceding block ends with null padding. A reader that previously read at least 1 record within the first block (before encountering the padding) upon trying to read the next record, will successfully and correctly read the next logical record from the subsequent block, but will return a last record offset pointing to the padding in the first block. When this happened in a [], it resulted in duplicate records being handled at what appeared to be different offsets that were separated by only a few bytes. This behavior is only observed when at least 1 record was read from the first block before encountering the padding. If the initial offset for a reader was within the padding, the correct record offset would be reported, namely the offset within the second block. The tests failed to catch this scenario/bug, because each read test only read a single record with an initial offset. This CL adds an explicit test case for this scenario, and modifies the test structure to read all remaining records in the test case after an initial offset is specified. Thus an initial offset that jumps to record #3, with 5 total records in the test file, will result in reading 2 records, and validating the offset of each of them in order to pass successfully. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=115338487
hace 8 años
This CL fixes a bug encountered when reading records from leveldb files that have been split, as in a [] input task split. Detailed description: Suppose an input split is generated between two leveldb record blocks and the preceding block ends with null padding. A reader that previously read at least 1 record within the first block (before encountering the padding) upon trying to read the next record, will successfully and correctly read the next logical record from the subsequent block, but will return a last record offset pointing to the padding in the first block. When this happened in a [], it resulted in duplicate records being handled at what appeared to be different offsets that were separated by only a few bytes. This behavior is only observed when at least 1 record was read from the first block before encountering the padding. If the initial offset for a reader was within the padding, the correct record offset would be reported, namely the offset within the second block. The tests failed to catch this scenario/bug, because each read test only read a single record with an initial offset. This CL adds an explicit test case for this scenario, and modifies the test structure to read all remaining records in the test case after an initial offset is specified. Thus an initial offset that jumps to record #3, with 5 total records in the test file, will result in reading 2 records, and validating the offset of each of them in order to pass successfully. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=115338487
hace 8 años
This CL fixes a bug encountered when reading records from leveldb files that have been split, as in a [] input task split. Detailed description: Suppose an input split is generated between two leveldb record blocks and the preceding block ends with null padding. A reader that previously read at least 1 record within the first block (before encountering the padding) upon trying to read the next record, will successfully and correctly read the next logical record from the subsequent block, but will return a last record offset pointing to the padding in the first block. When this happened in a [], it resulted in duplicate records being handled at what appeared to be different offsets that were separated by only a few bytes. This behavior is only observed when at least 1 record was read from the first block before encountering the padding. If the initial offset for a reader was within the padding, the correct record offset would be reported, namely the offset within the second block. The tests failed to catch this scenario/bug, because each read test only read a single record with an initial offset. This CL adds an explicit test case for this scenario, and modifies the test structure to read all remaining records in the test case after an initial offset is specified. Thus an initial offset that jumps to record #3, with 5 total records in the test file, will result in reading 2 records, and validating the offset of each of them in order to pass successfully. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=115338487
hace 8 años
This CL fixes a bug encountered when reading records from leveldb files that have been split, as in a [] input task split. Detailed description: Suppose an input split is generated between two leveldb record blocks and the preceding block ends with null padding. A reader that previously read at least 1 record within the first block (before encountering the padding) upon trying to read the next record, will successfully and correctly read the next logical record from the subsequent block, but will return a last record offset pointing to the padding in the first block. When this happened in a [], it resulted in duplicate records being handled at what appeared to be different offsets that were separated by only a few bytes. This behavior is only observed when at least 1 record was read from the first block before encountering the padding. If the initial offset for a reader was within the padding, the correct record offset would be reported, namely the offset within the second block. The tests failed to catch this scenario/bug, because each read test only read a single record with an initial offset. This CL adds an explicit test case for this scenario, and modifies the test structure to read all remaining records in the test case after an initial offset is specified. Thus an initial offset that jumps to record #3, with 5 total records in the test file, will result in reading 2 records, and validating the offset of each of them in order to pass successfully. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=115338487
hace 8 años
This CL fixes a bug encountered when reading records from leveldb files that have been split, as in a [] input task split. Detailed description: Suppose an input split is generated between two leveldb record blocks and the preceding block ends with null padding. A reader that previously read at least 1 record within the first block (before encountering the padding) upon trying to read the next record, will successfully and correctly read the next logical record from the subsequent block, but will return a last record offset pointing to the padding in the first block. When this happened in a [], it resulted in duplicate records being handled at what appeared to be different offsets that were separated by only a few bytes. This behavior is only observed when at least 1 record was read from the first block before encountering the padding. If the initial offset for a reader was within the padding, the correct record offset would be reported, namely the offset within the second block. The tests failed to catch this scenario/bug, because each read test only read a single record with an initial offset. This CL adds an explicit test case for this scenario, and modifies the test structure to read all remaining records in the test case after an initial offset is specified. Thus an initial offset that jumps to record #3, with 5 total records in the test file, will result in reading 2 records, and validating the offset of each of them in order to pass successfully. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=115338487
hace 8 años
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)
hace 10 años
This CL fixes a bug encountered when reading records from leveldb files that have been split, as in a [] input task split. Detailed description: Suppose an input split is generated between two leveldb record blocks and the preceding block ends with null padding. A reader that previously read at least 1 record within the first block (before encountering the padding) upon trying to read the next record, will successfully and correctly read the next logical record from the subsequent block, but will return a last record offset pointing to the padding in the first block. When this happened in a [], it resulted in duplicate records being handled at what appeared to be different offsets that were separated by only a few bytes. This behavior is only observed when at least 1 record was read from the first block before encountering the padding. If the initial offset for a reader was within the padding, the correct record offset would be reported, namely the offset within the second block. The tests failed to catch this scenario/bug, because each read test only read a single record with an initial offset. This CL adds an explicit test case for this scenario, and modifies the test structure to read all remaining records in the test case after an initial offset is specified. Thus an initial offset that jumps to record #3, with 5 total records in the test file, will result in reading 2 records, and validating the offset of each of them in order to pass successfully. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=115338487
hace 8 años
  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 "db/log_reader.h"
  5. #include "db/log_writer.h"
  6. #include "leveldb/env.h"
  7. #include "util/coding.h"
  8. #include "util/crc32c.h"
  9. #include "util/random.h"
  10. #include "util/testharness.h"
  11. namespace leveldb {
  12. namespace log {
  13. // Construct a string of the specified length made out of the supplied
  14. // partial string.
  15. static std::string BigString(const std::string& partial_string, size_t n) {
  16. std::string result;
  17. while (result.size() < n) {
  18. result.append(partial_string);
  19. }
  20. result.resize(n);
  21. return result;
  22. }
  23. // Construct a string from a number
  24. static std::string NumberString(int n) {
  25. char buf[50];
  26. snprintf(buf, sizeof(buf), "%d.", n);
  27. return std::string(buf);
  28. }
  29. // Return a skewed potentially long string
  30. static std::string RandomSkewedString(int i, Random* rnd) {
  31. return BigString(NumberString(i), rnd->Skewed(17));
  32. }
  33. class LogTest {
  34. private:
  35. class StringDest : public WritableFile {
  36. public:
  37. std::string contents_;
  38. virtual Status Close() { return Status::OK(); }
  39. virtual Status Flush() { return Status::OK(); }
  40. virtual Status Sync() { return Status::OK(); }
  41. virtual Status Append(const Slice& slice) {
  42. contents_.append(slice.data(), slice.size());
  43. return Status::OK();
  44. }
  45. };
  46. class StringSource : public SequentialFile {
  47. public:
  48. Slice contents_;
  49. bool force_error_;
  50. bool returned_partial_;
  51. StringSource() : force_error_(false), returned_partial_(false) { }
  52. virtual Status Read(size_t n, Slice* result, char* scratch) {
  53. ASSERT_TRUE(!returned_partial_) << "must not Read() after eof/error";
  54. if (force_error_) {
  55. force_error_ = false;
  56. returned_partial_ = true;
  57. return Status::Corruption("read error");
  58. }
  59. if (contents_.size() < n) {
  60. n = contents_.size();
  61. returned_partial_ = true;
  62. }
  63. *result = Slice(contents_.data(), n);
  64. contents_.remove_prefix(n);
  65. return Status::OK();
  66. }
  67. virtual Status Skip(uint64_t n) {
  68. if (n > contents_.size()) {
  69. contents_.clear();
  70. return Status::NotFound("in-memory file skipped past end");
  71. }
  72. contents_.remove_prefix(n);
  73. return Status::OK();
  74. }
  75. };
  76. class ReportCollector : public Reader::Reporter {
  77. public:
  78. size_t dropped_bytes_;
  79. std::string message_;
  80. ReportCollector() : dropped_bytes_(0) { }
  81. virtual void Corruption(size_t bytes, const Status& status) {
  82. dropped_bytes_ += bytes;
  83. message_.append(status.ToString());
  84. }
  85. };
  86. StringDest dest_;
  87. StringSource source_;
  88. ReportCollector report_;
  89. bool reading_;
  90. Writer* writer_;
  91. Reader* reader_;
  92. // Record metadata for testing initial offset functionality
  93. static size_t initial_offset_record_sizes_[];
  94. static uint64_t initial_offset_last_record_offsets_[];
  95. static int num_initial_offset_records_;
  96. public:
  97. LogTest() : reading_(false),
  98. writer_(new Writer(&dest_)),
  99. reader_(new Reader(&source_, &report_, true/*checksum*/,
  100. 0/*initial_offset*/)) {
  101. }
  102. ~LogTest() {
  103. delete writer_;
  104. delete reader_;
  105. }
  106. void ReopenForAppend() {
  107. delete writer_;
  108. writer_ = new Writer(&dest_, dest_.contents_.size());
  109. }
  110. void Write(const std::string& msg) {
  111. ASSERT_TRUE(!reading_) << "Write() after starting to read";
  112. writer_->AddRecord(Slice(msg));
  113. }
  114. size_t WrittenBytes() const {
  115. return dest_.contents_.size();
  116. }
  117. std::string Read() {
  118. if (!reading_) {
  119. reading_ = true;
  120. source_.contents_ = Slice(dest_.contents_);
  121. }
  122. std::string scratch;
  123. Slice record;
  124. if (reader_->ReadRecord(&record, &scratch)) {
  125. return record.ToString();
  126. } else {
  127. return "EOF";
  128. }
  129. }
  130. void IncrementByte(int offset, int delta) {
  131. dest_.contents_[offset] += delta;
  132. }
  133. void SetByte(int offset, char new_byte) {
  134. dest_.contents_[offset] = new_byte;
  135. }
  136. void ShrinkSize(int bytes) {
  137. dest_.contents_.resize(dest_.contents_.size() - bytes);
  138. }
  139. void FixChecksum(int header_offset, int len) {
  140. // Compute crc of type/len/data
  141. uint32_t crc = crc32c::Value(&dest_.contents_[header_offset+6], 1 + len);
  142. crc = crc32c::Mask(crc);
  143. EncodeFixed32(&dest_.contents_[header_offset], crc);
  144. }
  145. void ForceError() {
  146. source_.force_error_ = true;
  147. }
  148. size_t DroppedBytes() const {
  149. return report_.dropped_bytes_;
  150. }
  151. std::string ReportMessage() const {
  152. return report_.message_;
  153. }
  154. // Returns OK iff recorded error message contains "msg"
  155. std::string MatchError(const std::string& msg) const {
  156. if (report_.message_.find(msg) == std::string::npos) {
  157. return report_.message_;
  158. } else {
  159. return "OK";
  160. }
  161. }
  162. void WriteInitialOffsetLog() {
  163. for (int i = 0; i < num_initial_offset_records_; i++) {
  164. std::string record(initial_offset_record_sizes_[i],
  165. static_cast<char>('a' + i));
  166. Write(record);
  167. }
  168. }
  169. void StartReadingAt(uint64_t initial_offset) {
  170. delete reader_;
  171. reader_ = new Reader(&source_, &report_, true/*checksum*/, initial_offset);
  172. }
  173. void CheckOffsetPastEndReturnsNoRecords(uint64_t offset_past_end) {
  174. WriteInitialOffsetLog();
  175. reading_ = true;
  176. source_.contents_ = Slice(dest_.contents_);
  177. Reader* offset_reader = new Reader(&source_, &report_, true/*checksum*/,
  178. WrittenBytes() + offset_past_end);
  179. Slice record;
  180. std::string scratch;
  181. ASSERT_TRUE(!offset_reader->ReadRecord(&record, &scratch));
  182. delete offset_reader;
  183. }
  184. void CheckInitialOffsetRecord(uint64_t initial_offset,
  185. int expected_record_offset) {
  186. WriteInitialOffsetLog();
  187. reading_ = true;
  188. source_.contents_ = Slice(dest_.contents_);
  189. Reader* offset_reader = new Reader(&source_, &report_, true/*checksum*/,
  190. initial_offset);
  191. // Read all records from expected_record_offset through the last one.
  192. ASSERT_LT(expected_record_offset, num_initial_offset_records_);
  193. for (; expected_record_offset < num_initial_offset_records_;
  194. ++expected_record_offset) {
  195. Slice record;
  196. std::string scratch;
  197. ASSERT_TRUE(offset_reader->ReadRecord(&record, &scratch));
  198. ASSERT_EQ(initial_offset_record_sizes_[expected_record_offset],
  199. record.size());
  200. ASSERT_EQ(initial_offset_last_record_offsets_[expected_record_offset],
  201. offset_reader->LastRecordOffset());
  202. ASSERT_EQ((char)('a' + expected_record_offset), record.data()[0]);
  203. }
  204. delete offset_reader;
  205. }
  206. };
  207. size_t LogTest::initial_offset_record_sizes_[] =
  208. {10000, // Two sizable records in first block
  209. 10000,
  210. 2 * log::kBlockSize - 1000, // Span three blocks
  211. 1,
  212. 13716, // Consume all but two bytes of block 3.
  213. log::kBlockSize - kHeaderSize, // Consume the entirety of block 4.
  214. };
  215. uint64_t LogTest::initial_offset_last_record_offsets_[] =
  216. {0,
  217. kHeaderSize + 10000,
  218. 2 * (kHeaderSize + 10000),
  219. 2 * (kHeaderSize + 10000) +
  220. (2 * log::kBlockSize - 1000) + 3 * kHeaderSize,
  221. 2 * (kHeaderSize + 10000) +
  222. (2 * log::kBlockSize - 1000) + 3 * kHeaderSize
  223. + kHeaderSize + 1,
  224. 3 * log::kBlockSize,
  225. };
  226. // LogTest::initial_offset_last_record_offsets_ must be defined before this.
  227. int LogTest::num_initial_offset_records_ =
  228. sizeof(LogTest::initial_offset_last_record_offsets_)/sizeof(uint64_t);
  229. TEST(LogTest, Empty) {
  230. ASSERT_EQ("EOF", Read());
  231. }
  232. TEST(LogTest, ReadWrite) {
  233. Write("foo");
  234. Write("bar");
  235. Write("");
  236. Write("xxxx");
  237. ASSERT_EQ("foo", Read());
  238. ASSERT_EQ("bar", Read());
  239. ASSERT_EQ("", Read());
  240. ASSERT_EQ("xxxx", Read());
  241. ASSERT_EQ("EOF", Read());
  242. ASSERT_EQ("EOF", Read()); // Make sure reads at eof work
  243. }
  244. TEST(LogTest, ManyBlocks) {
  245. for (int i = 0; i < 100000; i++) {
  246. Write(NumberString(i));
  247. }
  248. for (int i = 0; i < 100000; i++) {
  249. ASSERT_EQ(NumberString(i), Read());
  250. }
  251. ASSERT_EQ("EOF", Read());
  252. }
  253. TEST(LogTest, Fragmentation) {
  254. Write("small");
  255. Write(BigString("medium", 50000));
  256. Write(BigString("large", 100000));
  257. ASSERT_EQ("small", Read());
  258. ASSERT_EQ(BigString("medium", 50000), Read());
  259. ASSERT_EQ(BigString("large", 100000), Read());
  260. ASSERT_EQ("EOF", Read());
  261. }
  262. TEST(LogTest, MarginalTrailer) {
  263. // Make a trailer that is exactly the same length as an empty record.
  264. const int n = kBlockSize - 2*kHeaderSize;
  265. Write(BigString("foo", n));
  266. ASSERT_EQ(kBlockSize - kHeaderSize, WrittenBytes());
  267. Write("");
  268. Write("bar");
  269. ASSERT_EQ(BigString("foo", n), Read());
  270. ASSERT_EQ("", Read());
  271. ASSERT_EQ("bar", Read());
  272. ASSERT_EQ("EOF", Read());
  273. }
  274. TEST(LogTest, MarginalTrailer2) {
  275. // Make a trailer that is exactly the same length as an empty record.
  276. const int n = kBlockSize - 2*kHeaderSize;
  277. Write(BigString("foo", n));
  278. ASSERT_EQ(kBlockSize - kHeaderSize, WrittenBytes());
  279. Write("bar");
  280. ASSERT_EQ(BigString("foo", n), Read());
  281. ASSERT_EQ("bar", Read());
  282. ASSERT_EQ("EOF", Read());
  283. ASSERT_EQ(0, DroppedBytes());
  284. ASSERT_EQ("", ReportMessage());
  285. }
  286. TEST(LogTest, ShortTrailer) {
  287. const int n = kBlockSize - 2*kHeaderSize + 4;
  288. Write(BigString("foo", n));
  289. ASSERT_EQ(kBlockSize - kHeaderSize + 4, WrittenBytes());
  290. Write("");
  291. Write("bar");
  292. ASSERT_EQ(BigString("foo", n), Read());
  293. ASSERT_EQ("", Read());
  294. ASSERT_EQ("bar", Read());
  295. ASSERT_EQ("EOF", Read());
  296. }
  297. TEST(LogTest, AlignedEof) {
  298. const int n = kBlockSize - 2*kHeaderSize + 4;
  299. Write(BigString("foo", n));
  300. ASSERT_EQ(kBlockSize - kHeaderSize + 4, WrittenBytes());
  301. ASSERT_EQ(BigString("foo", n), Read());
  302. ASSERT_EQ("EOF", Read());
  303. }
  304. TEST(LogTest, OpenForAppend) {
  305. Write("hello");
  306. ReopenForAppend();
  307. Write("world");
  308. ASSERT_EQ("hello", Read());
  309. ASSERT_EQ("world", Read());
  310. ASSERT_EQ("EOF", Read());
  311. }
  312. TEST(LogTest, RandomRead) {
  313. const int N = 500;
  314. Random write_rnd(301);
  315. for (int i = 0; i < N; i++) {
  316. Write(RandomSkewedString(i, &write_rnd));
  317. }
  318. Random read_rnd(301);
  319. for (int i = 0; i < N; i++) {
  320. ASSERT_EQ(RandomSkewedString(i, &read_rnd), Read());
  321. }
  322. ASSERT_EQ("EOF", Read());
  323. }
  324. // Tests of all the error paths in log_reader.cc follow:
  325. TEST(LogTest, ReadError) {
  326. Write("foo");
  327. ForceError();
  328. ASSERT_EQ("EOF", Read());
  329. ASSERT_EQ(kBlockSize, DroppedBytes());
  330. ASSERT_EQ("OK", MatchError("read error"));
  331. }
  332. TEST(LogTest, BadRecordType) {
  333. Write("foo");
  334. // Type is stored in header[6]
  335. IncrementByte(6, 100);
  336. FixChecksum(0, 3);
  337. ASSERT_EQ("EOF", Read());
  338. ASSERT_EQ(3, DroppedBytes());
  339. ASSERT_EQ("OK", MatchError("unknown record type"));
  340. }
  341. TEST(LogTest, TruncatedTrailingRecordIsIgnored) {
  342. Write("foo");
  343. ShrinkSize(4); // Drop all payload as well as a header byte
  344. ASSERT_EQ("EOF", Read());
  345. // Truncated last record is ignored, not treated as an error.
  346. ASSERT_EQ(0, DroppedBytes());
  347. ASSERT_EQ("", ReportMessage());
  348. }
  349. TEST(LogTest, BadLength) {
  350. const int kPayloadSize = kBlockSize - kHeaderSize;
  351. Write(BigString("bar", kPayloadSize));
  352. Write("foo");
  353. // Least significant size byte is stored in header[4].
  354. IncrementByte(4, 1);
  355. ASSERT_EQ("foo", Read());
  356. ASSERT_EQ(kBlockSize, DroppedBytes());
  357. ASSERT_EQ("OK", MatchError("bad record length"));
  358. }
  359. TEST(LogTest, BadLengthAtEndIsIgnored) {
  360. Write("foo");
  361. ShrinkSize(1);
  362. ASSERT_EQ("EOF", Read());
  363. ASSERT_EQ(0, DroppedBytes());
  364. ASSERT_EQ("", ReportMessage());
  365. }
  366. TEST(LogTest, ChecksumMismatch) {
  367. Write("foo");
  368. IncrementByte(0, 10);
  369. ASSERT_EQ("EOF", Read());
  370. ASSERT_EQ(10, DroppedBytes());
  371. ASSERT_EQ("OK", MatchError("checksum mismatch"));
  372. }
  373. TEST(LogTest, UnexpectedMiddleType) {
  374. Write("foo");
  375. SetByte(6, kMiddleType);
  376. FixChecksum(0, 3);
  377. ASSERT_EQ("EOF", Read());
  378. ASSERT_EQ(3, DroppedBytes());
  379. ASSERT_EQ("OK", MatchError("missing start"));
  380. }
  381. TEST(LogTest, UnexpectedLastType) {
  382. Write("foo");
  383. SetByte(6, kLastType);
  384. FixChecksum(0, 3);
  385. ASSERT_EQ("EOF", Read());
  386. ASSERT_EQ(3, DroppedBytes());
  387. ASSERT_EQ("OK", MatchError("missing start"));
  388. }
  389. TEST(LogTest, UnexpectedFullType) {
  390. Write("foo");
  391. Write("bar");
  392. SetByte(6, kFirstType);
  393. FixChecksum(0, 3);
  394. ASSERT_EQ("bar", Read());
  395. ASSERT_EQ("EOF", Read());
  396. ASSERT_EQ(3, DroppedBytes());
  397. ASSERT_EQ("OK", MatchError("partial record without end"));
  398. }
  399. TEST(LogTest, UnexpectedFirstType) {
  400. Write("foo");
  401. Write(BigString("bar", 100000));
  402. SetByte(6, kFirstType);
  403. FixChecksum(0, 3);
  404. ASSERT_EQ(BigString("bar", 100000), Read());
  405. ASSERT_EQ("EOF", Read());
  406. ASSERT_EQ(3, DroppedBytes());
  407. ASSERT_EQ("OK", MatchError("partial record without end"));
  408. }
  409. TEST(LogTest, MissingLastIsIgnored) {
  410. Write(BigString("bar", kBlockSize));
  411. // Remove the LAST block, including header.
  412. ShrinkSize(14);
  413. ASSERT_EQ("EOF", Read());
  414. ASSERT_EQ("", ReportMessage());
  415. ASSERT_EQ(0, DroppedBytes());
  416. }
  417. TEST(LogTest, PartialLastIsIgnored) {
  418. Write(BigString("bar", kBlockSize));
  419. // Cause a bad record length in the LAST block.
  420. ShrinkSize(1);
  421. ASSERT_EQ("EOF", Read());
  422. ASSERT_EQ("", ReportMessage());
  423. ASSERT_EQ(0, DroppedBytes());
  424. }
  425. TEST(LogTest, SkipIntoMultiRecord) {
  426. // Consider a fragmented record:
  427. // first(R1), middle(R1), last(R1), first(R2)
  428. // If initial_offset points to a record after first(R1) but before first(R2)
  429. // incomplete fragment errors are not actual errors, and must be suppressed
  430. // until a new first or full record is encountered.
  431. Write(BigString("foo", 3*kBlockSize));
  432. Write("correct");
  433. StartReadingAt(kBlockSize);
  434. ASSERT_EQ("correct", Read());
  435. ASSERT_EQ("", ReportMessage());
  436. ASSERT_EQ(0, DroppedBytes());
  437. ASSERT_EQ("EOF", Read());
  438. }
  439. TEST(LogTest, ErrorJoinsRecords) {
  440. // Consider two fragmented records:
  441. // first(R1) last(R1) first(R2) last(R2)
  442. // where the middle two fragments disappear. We do not want
  443. // first(R1),last(R2) to get joined and returned as a valid record.
  444. // Write records that span two blocks
  445. Write(BigString("foo", kBlockSize));
  446. Write(BigString("bar", kBlockSize));
  447. Write("correct");
  448. // Wipe the middle block
  449. for (int offset = kBlockSize; offset < 2*kBlockSize; offset++) {
  450. SetByte(offset, 'x');
  451. }
  452. ASSERT_EQ("correct", Read());
  453. ASSERT_EQ("EOF", Read());
  454. const size_t dropped = DroppedBytes();
  455. ASSERT_LE(dropped, 2*kBlockSize + 100);
  456. ASSERT_GE(dropped, 2*kBlockSize);
  457. }
  458. TEST(LogTest, ReadStart) {
  459. CheckInitialOffsetRecord(0, 0);
  460. }
  461. TEST(LogTest, ReadSecondOneOff) {
  462. CheckInitialOffsetRecord(1, 1);
  463. }
  464. TEST(LogTest, ReadSecondTenThousand) {
  465. CheckInitialOffsetRecord(10000, 1);
  466. }
  467. TEST(LogTest, ReadSecondStart) {
  468. CheckInitialOffsetRecord(10007, 1);
  469. }
  470. TEST(LogTest, ReadThirdOneOff) {
  471. CheckInitialOffsetRecord(10008, 2);
  472. }
  473. TEST(LogTest, ReadThirdStart) {
  474. CheckInitialOffsetRecord(20014, 2);
  475. }
  476. TEST(LogTest, ReadFourthOneOff) {
  477. CheckInitialOffsetRecord(20015, 3);
  478. }
  479. TEST(LogTest, ReadFourthFirstBlockTrailer) {
  480. CheckInitialOffsetRecord(log::kBlockSize - 4, 3);
  481. }
  482. TEST(LogTest, ReadFourthMiddleBlock) {
  483. CheckInitialOffsetRecord(log::kBlockSize + 1, 3);
  484. }
  485. TEST(LogTest, ReadFourthLastBlock) {
  486. CheckInitialOffsetRecord(2 * log::kBlockSize + 1, 3);
  487. }
  488. TEST(LogTest, ReadFourthStart) {
  489. CheckInitialOffsetRecord(
  490. 2 * (kHeaderSize + 1000) + (2 * log::kBlockSize - 1000) + 3 * kHeaderSize,
  491. 3);
  492. }
  493. TEST(LogTest, ReadInitialOffsetIntoBlockPadding) {
  494. CheckInitialOffsetRecord(3 * log::kBlockSize - 3, 5);
  495. }
  496. TEST(LogTest, ReadEnd) {
  497. CheckOffsetPastEndReturnsNoRecords(0);
  498. }
  499. TEST(LogTest, ReadPastEnd) {
  500. CheckOffsetPastEndReturnsNoRecords(5);
  501. }
  502. } // namespace log
  503. } // namespace leveldb
  504. int main(int argc, char** argv) {
  505. return leveldb::test::RunAllTests();
  506. }