Repository: incubator-ratis Updated Branches: refs/heads/master bbc79cf46 -> 55fffd96b
RATIS-228. Handle zero size log_inprogress file. Contributed by Kit Hui Project: http://git-wip-us.apache.org/repos/asf/incubator-ratis/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ratis/commit/55fffd96 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ratis/tree/55fffd96 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ratis/diff/55fffd96 Branch: refs/heads/master Commit: 55fffd96b8b1c31e1a4b9549b26657c881eea02e Parents: bbc79cf Author: Tsz Sze <[email protected]> Authored: Tue Apr 24 11:09:06 2018 -0700 Committer: Tsz Sze <[email protected]> Committed: Tue Apr 24 11:09:06 2018 -0700 ---------------------------------------------------------------------- .../apache/ratis/server/storage/LogSegment.java | 5 ++++- .../server/storage/RaftStorageDirectory.java | 10 +++++++--- .../server/storage/TestRaftLogSegment.java | 21 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/55fffd96/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java index 61fda5c..827e2e3 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java @@ -138,7 +138,10 @@ class LogSegment implements Comparable<Long> { FileUtils.truncateFile(file, segment.getTotalSize()); } - Preconditions.assertTrue(start == segment.records.get(0).getTermIndex().getIndex()); + Preconditions.assertTrue(start == segment.getStartIndex()); + if (!segment.records.isEmpty()) { + Preconditions.assertTrue(start == segment.records.get(0).getTermIndex().getIndex()); + } if (!isOpen) { Preconditions.assertTrue(segment.getEndIndex() == end); } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/55fffd96/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java index 530f679..ded7d52 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java @@ -52,8 +52,6 @@ public class RaftStorageDirectory { static final Pattern CLOSED_SEGMENT_REGEX = Pattern.compile("log_(\\d+)-(\\d+)"); static final Pattern OPEN_SEGMENT_REGEX = Pattern.compile("log_inprogress_(\\d+)(?:\\..*)?"); - private static final List<Pattern> LOGSEGMENTS_REGEXES = - Arrays.asList(CLOSED_SEGMENT_REGEX, OPEN_SEGMENT_REGEX); enum StorageState { NON_EXISTENT, @@ -182,13 +180,19 @@ public class RaftStorageDirectory { try (DirectoryStream<Path> stream = Files.newDirectoryStream(getCurrentDir().toPath())) { for (Path path : stream) { - for (Pattern pattern : LOGSEGMENTS_REGEXES) { + for (Pattern pattern : Arrays.asList(CLOSED_SEGMENT_REGEX, OPEN_SEGMENT_REGEX)) { Matcher matcher = pattern.matcher(path.getFileName().toString()); if (matcher.matches()) { + if (pattern == OPEN_SEGMENT_REGEX && Files.size(path) == 0L) { + Files.delete(path); + LOG.info("Delete zero size file " + path); + break; + } final long startIndex = Long.parseLong(matcher.group(1)); final long endIndex = matcher.groupCount() == 2 ? Long.parseLong(matcher.group(2)) : INVALID_LOG_INDEX; list.add(new LogPathAndIndex(path, startIndex, endIndex)); + break; } } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/55fffd96/ratis-server/src/test/java/org/apache/ratis/server/storage/TestRaftLogSegment.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/storage/TestRaftLogSegment.java b/ratis-server/src/test/java/org/apache/ratis/server/storage/TestRaftLogSegment.java index 26477c2..690ad59 100644 --- a/ratis-server/src/test/java/org/apache/ratis/server/storage/TestRaftLogSegment.java +++ b/ratis-server/src/test/java/org/apache/ratis/server/storage/TestRaftLogSegment.java @@ -37,6 +37,8 @@ import org.junit.Test; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -320,4 +322,23 @@ public class TestRaftLogSegment extends BaseTest { Assert.assertEquals(totalSize, file.length()); } + + @Test + public void testZeroSizeInProgressFile() throws Exception { + final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR); + final File file = storage.getStorageDir().getOpenLogFile(0); + storage.close(); + + // create zero size in-progress file + LOG.info("file: " + file); + Assert.assertTrue(file.createNewFile()); + final Path path = file.toPath(); + Assert.assertTrue(Files.exists(path)); + Assert.assertEquals(0, Files.size(path)); + + // getLogSegmentFiles should remove it. + final List<RaftStorageDirectory.LogPathAndIndex> logs = storage.getStorageDir().getLogSegmentFiles(); + Assert.assertEquals(0, logs.size()); + Assert.assertFalse(Files.exists(path)); + } }
