作者: 韩晨旭 10225101440 李畅 10225102463
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.

279 lines
8.7 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
8 years ago
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 years ago
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 years ago
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 years ago
  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 <stdio.h>
  6. #include "leveldb/env.h"
  7. #include "util/coding.h"
  8. #include "util/crc32c.h"
  9. namespace leveldb {
  10. namespace log {
  11. Reader::Reporter::~Reporter() {
  12. }
  13. Reader::Reader(SequentialFile* file, Reporter* reporter, bool checksum,
  14. uint64_t initial_offset)
  15. : file_(file),
  16. reporter_(reporter),
  17. checksum_(checksum),
  18. backing_store_(new char[kBlockSize]),
  19. buffer_(),
  20. eof_(false),
  21. last_record_offset_(0),
  22. end_of_buffer_offset_(0),
  23. initial_offset_(initial_offset),
  24. resyncing_(initial_offset > 0) {
  25. }
  26. Reader::~Reader() {
  27. delete[] backing_store_;
  28. }
  29. bool Reader::SkipToInitialBlock() {
  30. const size_t offset_in_block = initial_offset_ % kBlockSize;
  31. uint64_t block_start_location = initial_offset_ - offset_in_block;
  32. // Don't search a block if we'd be in the trailer
  33. if (offset_in_block > kBlockSize - 6) {
  34. block_start_location += kBlockSize;
  35. }
  36. end_of_buffer_offset_ = block_start_location;
  37. // Skip to start of first block that can contain the initial record
  38. if (block_start_location > 0) {
  39. Status skip_status = file_->Skip(block_start_location);
  40. if (!skip_status.ok()) {
  41. ReportDrop(block_start_location, skip_status);
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. bool Reader::ReadRecord(Slice* record, std::string* scratch) {
  48. if (last_record_offset_ < initial_offset_) {
  49. if (!SkipToInitialBlock()) {
  50. return false;
  51. }
  52. }
  53. scratch->clear();
  54. record->clear();
  55. bool in_fragmented_record = false;
  56. // Record offset of the logical record that we're reading
  57. // 0 is a dummy value to make compilers happy
  58. uint64_t prospective_record_offset = 0;
  59. Slice fragment;
  60. while (true) {
  61. const unsigned int record_type = ReadPhysicalRecord(&fragment);
  62. // ReadPhysicalRecord may have only had an empty trailer remaining in its
  63. // internal buffer. Calculate the offset of the next physical record now
  64. // that it has returned, properly accounting for its header size.
  65. uint64_t physical_record_offset =
  66. end_of_buffer_offset_ - buffer_.size() - kHeaderSize - fragment.size();
  67. if (resyncing_) {
  68. if (record_type == kMiddleType) {
  69. continue;
  70. } else if (record_type == kLastType) {
  71. resyncing_ = false;
  72. continue;
  73. } else {
  74. resyncing_ = false;
  75. }
  76. }
  77. switch (record_type) {
  78. case kFullType:
  79. if (in_fragmented_record) {
  80. // Handle bug in earlier versions of log::Writer where
  81. // it could emit an empty kFirstType record at the tail end
  82. // of a block followed by a kFullType or kFirstType record
  83. // at the beginning of the next block.
  84. if (!scratch->empty()) {
  85. ReportCorruption(scratch->size(), "partial record without end(1)");
  86. }
  87. }
  88. prospective_record_offset = physical_record_offset;
  89. scratch->clear();
  90. *record = fragment;
  91. last_record_offset_ = prospective_record_offset;
  92. return true;
  93. case kFirstType:
  94. if (in_fragmented_record) {
  95. // Handle bug in earlier versions of log::Writer where
  96. // it could emit an empty kFirstType record at the tail end
  97. // of a block followed by a kFullType or kFirstType record
  98. // at the beginning of the next block.
  99. if (!scratch->empty()) {
  100. ReportCorruption(scratch->size(), "partial record without end(2)");
  101. }
  102. }
  103. prospective_record_offset = physical_record_offset;
  104. scratch->assign(fragment.data(), fragment.size());
  105. in_fragmented_record = true;
  106. break;
  107. case kMiddleType:
  108. if (!in_fragmented_record) {
  109. ReportCorruption(fragment.size(),
  110. "missing start of fragmented record(1)");
  111. } else {
  112. scratch->append(fragment.data(), fragment.size());
  113. }
  114. break;
  115. case kLastType:
  116. if (!in_fragmented_record) {
  117. ReportCorruption(fragment.size(),
  118. "missing start of fragmented record(2)");
  119. } else {
  120. scratch->append(fragment.data(), fragment.size());
  121. *record = Slice(*scratch);
  122. last_record_offset_ = prospective_record_offset;
  123. return true;
  124. }
  125. break;
  126. case kEof:
  127. if (in_fragmented_record) {
  128. // This can be caused by the writer dying immediately after
  129. // writing a physical record but before completing the next; don't
  130. // treat it as a corruption, just ignore the entire logical record.
  131. scratch->clear();
  132. }
  133. return false;
  134. case kBadRecord:
  135. if (in_fragmented_record) {
  136. ReportCorruption(scratch->size(), "error in middle of record");
  137. in_fragmented_record = false;
  138. scratch->clear();
  139. }
  140. break;
  141. default: {
  142. char buf[40];
  143. snprintf(buf, sizeof(buf), "unknown record type %u", record_type);
  144. ReportCorruption(
  145. (fragment.size() + (in_fragmented_record ? scratch->size() : 0)),
  146. buf);
  147. in_fragmented_record = false;
  148. scratch->clear();
  149. break;
  150. }
  151. }
  152. }
  153. return false;
  154. }
  155. uint64_t Reader::LastRecordOffset() {
  156. return last_record_offset_;
  157. }
  158. void Reader::ReportCorruption(uint64_t bytes, const char* reason) {
  159. ReportDrop(bytes, Status::Corruption(reason));
  160. }
  161. void Reader::ReportDrop(uint64_t bytes, const Status& reason) {
  162. if (reporter_ != NULL &&
  163. end_of_buffer_offset_ - buffer_.size() - bytes >= initial_offset_) {
  164. reporter_->Corruption(static_cast<size_t>(bytes), reason);
  165. }
  166. }
  167. unsigned int Reader::ReadPhysicalRecord(Slice* result) {
  168. while (true) {
  169. if (buffer_.size() < kHeaderSize) {
  170. if (!eof_) {
  171. // Last read was a full read, so this is a trailer to skip
  172. buffer_.clear();
  173. Status status = file_->Read(kBlockSize, &buffer_, backing_store_);
  174. end_of_buffer_offset_ += buffer_.size();
  175. if (!status.ok()) {
  176. buffer_.clear();
  177. ReportDrop(kBlockSize, status);
  178. eof_ = true;
  179. return kEof;
  180. } else if (buffer_.size() < kBlockSize) {
  181. eof_ = true;
  182. }
  183. continue;
  184. } else {
  185. // Note that if buffer_ is non-empty, we have a truncated header at the
  186. // end of the file, which can be caused by the writer crashing in the
  187. // middle of writing the header. Instead of considering this an error,
  188. // just report EOF.
  189. buffer_.clear();
  190. return kEof;
  191. }
  192. }
  193. // Parse the header
  194. const char* header = buffer_.data();
  195. const uint32_t a = static_cast<uint32_t>(header[4]) & 0xff;
  196. const uint32_t b = static_cast<uint32_t>(header[5]) & 0xff;
  197. const unsigned int type = header[6];
  198. const uint32_t length = a | (b << 8);
  199. if (kHeaderSize + length > buffer_.size()) {
  200. size_t drop_size = buffer_.size();
  201. buffer_.clear();
  202. if (!eof_) {
  203. ReportCorruption(drop_size, "bad record length");
  204. return kBadRecord;
  205. }
  206. // If the end of the file has been reached without reading |length| bytes
  207. // of payload, assume the writer died in the middle of writing the record.
  208. // Don't report a corruption.
  209. return kEof;
  210. }
  211. if (type == kZeroType && length == 0) {
  212. // Skip zero length record without reporting any drops since
  213. // such records are produced by the mmap based writing code in
  214. // env_posix.cc that preallocates file regions.
  215. buffer_.clear();
  216. return kBadRecord;
  217. }
  218. // Check crc
  219. if (checksum_) {
  220. uint32_t expected_crc = crc32c::Unmask(DecodeFixed32(header));
  221. uint32_t actual_crc = crc32c::Value(header + 6, 1 + length);
  222. if (actual_crc != expected_crc) {
  223. // Drop the rest of the buffer since "length" itself may have
  224. // been corrupted and if we trust it, we could find some
  225. // fragment of a real log record that just happens to look
  226. // like a valid log record.
  227. size_t drop_size = buffer_.size();
  228. buffer_.clear();
  229. ReportCorruption(drop_size, "checksum mismatch");
  230. return kBadRecord;
  231. }
  232. }
  233. buffer_.remove_prefix(kHeaderSize + length);
  234. // Skip physical record that started before initial_offset_
  235. if (end_of_buffer_offset_ - buffer_.size() - kHeaderSize - length <
  236. initial_offset_) {
  237. result->clear();
  238. return kBadRecord;
  239. }
  240. *result = Slice(header + kHeaderSize, length);
  241. return type;
  242. }
  243. }
  244. } // namespace log
  245. } // namespace leveldb