Skip to content

Commit 269fc6c

Browse files
committed
Release LevelDB 1.16
- Make Log::Reader not report a corruption when the last record in a log file is truncated. - Fix issue 224: variable created but not utilized. - Remove comment that referenced a removed feature.
1 parent 0cfb990 commit 269fc6c

File tree

6 files changed

+52
-17
lines changed

6 files changed

+52
-17
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ SHARED = $(SHARED1)
7272
else
7373
# Update db.h if you change these.
7474
SHARED_MAJOR = 1
75-
SHARED_MINOR = 15
75+
SHARED_MINOR = 16
7676
SHARED1 = libleveldb.$(PLATFORM_SHARED_EXT)
7777
SHARED2 = $(SHARED1).$(SHARED_MAJOR)
7878
SHARED3 = $(SHARED1).$(SHARED_MAJOR).$(SHARED_MINOR)

db/log_reader.cc

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ bool Reader::ReadRecord(Slice* record, std::string* scratch) {
133133

134134
case kEof:
135135
if (in_fragmented_record) {
136-
ReportCorruption(scratch->size(), "partial record without end(3)");
136+
// This can be caused by the writer dying immediately after
137+
// writing a physical record but before completing the next; don't
138+
// treat it as a corruption, just ignore the entire logical record.
137139
scratch->clear();
138140
}
139141
return false;
@@ -193,13 +195,12 @@ unsigned int Reader::ReadPhysicalRecord(Slice* result) {
193195
eof_ = true;
194196
}
195197
continue;
196-
} else if (buffer_.size() == 0) {
197-
// End of file
198-
return kEof;
199198
} else {
200-
size_t drop_size = buffer_.size();
199+
// Note that if buffer_ is non-empty, we have a truncated header at the
200+
// end of the file, which can be caused by the writer crashing in the
201+
// middle of writing the header. Instead of considering this an error,
202+
// just report EOF.
201203
buffer_.clear();
202-
ReportCorruption(drop_size, "truncated record at end of file");
203204
return kEof;
204205
}
205206
}
@@ -213,8 +214,14 @@ unsigned int Reader::ReadPhysicalRecord(Slice* result) {
213214
if (kHeaderSize + length > buffer_.size()) {
214215
size_t drop_size = buffer_.size();
215216
buffer_.clear();
216-
ReportCorruption(drop_size, "bad record length");
217-
return kBadRecord;
217+
if (!eof_) {
218+
ReportCorruption(drop_size, "bad record length");
219+
return kBadRecord;
220+
}
221+
// If the end of the file has been reached without reading |length| bytes
222+
// of payload, assume the writer died in the middle of writing the record.
223+
// Don't report a corruption.
224+
return kEof;
218225
}
219226

220227
if (type == kZeroType && length == 0) {

db/log_test.cc

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,20 +351,32 @@ TEST(LogTest, BadRecordType) {
351351
ASSERT_EQ("OK", MatchError("unknown record type"));
352352
}
353353

354-
TEST(LogTest, TruncatedTrailingRecord) {
354+
TEST(LogTest, TruncatedTrailingRecordIsIgnored) {
355355
Write("foo");
356356
ShrinkSize(4); // Drop all payload as well as a header byte
357357
ASSERT_EQ("EOF", Read());
358-
ASSERT_EQ(kHeaderSize - 1, DroppedBytes());
359-
ASSERT_EQ("OK", MatchError("truncated record at end of file"));
358+
// Truncated last record is ignored, not treated as an error.
359+
ASSERT_EQ(0, DroppedBytes());
360+
ASSERT_EQ("", ReportMessage());
360361
}
361362

362363
TEST(LogTest, BadLength) {
364+
const int kPayloadSize = kBlockSize - kHeaderSize;
365+
Write(BigString("bar", kPayloadSize));
366+
Write("foo");
367+
// Least significant size byte is stored in header[4].
368+
IncrementByte(4, 1);
369+
ASSERT_EQ("foo", Read());
370+
ASSERT_EQ(kBlockSize, DroppedBytes());
371+
ASSERT_EQ("OK", MatchError("bad record length"));
372+
}
373+
374+
TEST(LogTest, BadLengthAtEndIsIgnored) {
363375
Write("foo");
364376
ShrinkSize(1);
365377
ASSERT_EQ("EOF", Read());
366-
ASSERT_EQ(kHeaderSize + 2, DroppedBytes());
367-
ASSERT_EQ("OK", MatchError("bad record length"));
378+
ASSERT_EQ(0, DroppedBytes());
379+
ASSERT_EQ("", ReportMessage());
368380
}
369381

370382
TEST(LogTest, ChecksumMismatch) {
@@ -415,6 +427,24 @@ TEST(LogTest, UnexpectedFirstType) {
415427
ASSERT_EQ("OK", MatchError("partial record without end"));
416428
}
417429

430+
TEST(LogTest, MissingLastIsIgnored) {
431+
Write(BigString("bar", kBlockSize));
432+
// Remove the LAST block, including header.
433+
ShrinkSize(14);
434+
ASSERT_EQ("EOF", Read());
435+
ASSERT_EQ("", ReportMessage());
436+
ASSERT_EQ(0, DroppedBytes());
437+
}
438+
439+
TEST(LogTest, PartialLastIsIgnored) {
440+
Write(BigString("bar", kBlockSize));
441+
// Cause a bad record length in the LAST block.
442+
ShrinkSize(1);
443+
ASSERT_EQ("EOF", Read());
444+
ASSERT_EQ("", ReportMessage());
445+
ASSERT_EQ(0, DroppedBytes());
446+
}
447+
418448
TEST(LogTest, ErrorJoinsRecords) {
419449
// Consider two fragmented records:
420450
// first(R1) last(R1) first(R2) last(R2)

db/repair.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ class Repairer {
242242
}
243243

244244
void ExtractMetaData() {
245-
std::vector<TableInfo> kept;
246245
for (size_t i = 0; i < table_numbers_.size(); i++) {
247246
ScanTable(table_numbers_[i]);
248247
}

include/leveldb/c.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
Does not support:
1010
. getters for the option types
1111
. custom comparators that implement key shortening
12-
. capturing post-write-snapshot
1312
. custom iter, db, env, cache implementations using just the C bindings
1413
1514
Some conventions:

include/leveldb/db.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace leveldb {
1414

1515
// Update Makefile if you change these
1616
static const int kMajorVersion = 1;
17-
static const int kMinorVersion = 15;
17+
static const int kMinorVersion = 16;
1818

1919
struct Options;
2020
struct ReadOptions;

0 commit comments

Comments
 (0)