Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
issue #191: log cabin cannot start if open segment is full of 0s
  • Loading branch information
Nate Hardt committed Aug 25, 2016
1 parent e6defc3 commit 6e5a061
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Storage/SegmentedLog.cc
Expand Up @@ -992,10 +992,19 @@ SegmentedLog::loadOpenSegment(Segment& segment, uint64_t logStartIndex)
uint8_t version = *reader.get<uint8_t>(0, 1);
offset += 1;
if (version != 1) {
PANIC("Segment version read from %s was %u, but this code can "
"only read version 1",
segment.filename.c_str(),
version);
uint64_t remainingBytes = reader.getFileLength() - offset;
if (version == 0 &&
isAllZeros(reader.get(
offset, remainingBytes), remainingBytes)) {
// move the offset to the end of the file. allow the
// existing cleanup mechanism to remove this file.
offset = reader.getFileLength();
} else {
PANIC("Segment version read from %s was %u, "
"but this code can only read version 1",
segment.filename.c_str(),
version);
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions Storage/SegmentedLogTest.cc
Expand Up @@ -1069,6 +1069,23 @@ TEST_F(StorageSegmentedLogTest, loadOpenSegment_removeUnneeded)
O_RDONLY).fd);
}

TEST_F(StorageSegmentedLogTest, loadOpenSegment_removeFileOfAllZeros)
{
FS::File file = FS::openFile(log->dir,
openSegment.filename,
O_CREAT|O_WRONLY);
FS::truncate(file, 8 * 1024 * 1024);
LogCabin::Core::Debug::setLogPolicy({ // expect warnings
{"Storage/SegmentedLog", "ERROR"}
});
EXPECT_FALSE(log->loadOpenSegment(openSegment, 1));
LogCabin::Core::Debug::setLogPolicy({
{"", "WARNING"}
});
EXPECT_EQ(-1, FS::tryOpenFile(log->dir, openSegment.filename,
O_RDONLY).fd);
}

TEST_F(StorageSegmentedLogTest, loadOpenSegment_corruptDelete)
{
log->truncatePrefix(3);
Expand Down

0 comments on commit 6e5a061

Please sign in to comment.