小组成员:10215300402-朱维清 & 10222140408 谷杰
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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