Refactor SnapshotInfo and its implementations.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ratis/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ratis/commit/f58b5ef8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ratis/tree/f58b5ef8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ratis/diff/f58b5ef8 Branch: refs/heads/master Commit: f58b5ef8f8f96ad640ff1b6b2d707f21712ec87a Parents: 5946058 Author: Tsz-Wo Nicholas Sze <[email protected]> Authored: Fri Jan 6 16:03:30 2017 +0800 Committer: Tsz-Wo Nicholas Sze <[email protected]> Committed: Fri Jan 6 16:03:30 2017 +0800 ---------------------------------------------------------------------- .../arithmetic/ArithmeticStateMachine.java | 1 - .../apache/raft/server/storage/FileInfo.java | 11 +++- .../raft/statemachine/FileListSnapshotInfo.java | 64 ++++++++++++++++++ .../statemachine/SimpleStateMachineStorage.java | 18 +----- .../statemachine/SingleFileSnapshotInfo.java | 38 +++++++++++ .../apache/raft/statemachine/SnapshotInfo.java | 7 -- .../raft/statemachine/SnapshotInfoImpl.java | 68 -------------------- .../SimpleStateMachine4Testing.java | 1 - 8 files changed, 113 insertions(+), 95 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f58b5ef8/raft-examples/src/main/java/org/apache/raft/examples/arithmetic/ArithmeticStateMachine.java ---------------------------------------------------------------------- diff --git a/raft-examples/src/main/java/org/apache/raft/examples/arithmetic/ArithmeticStateMachine.java b/raft-examples/src/main/java/org/apache/raft/examples/arithmetic/ArithmeticStateMachine.java index 53b6122..5112e14 100644 --- a/raft-examples/src/main/java/org/apache/raft/examples/arithmetic/ArithmeticStateMachine.java +++ b/raft-examples/src/main/java/org/apache/raft/examples/arithmetic/ArithmeticStateMachine.java @@ -28,7 +28,6 @@ import org.apache.raft.server.protocol.TermIndex; import org.apache.raft.server.storage.RaftStorage; import org.apache.raft.shaded.proto.RaftProtos.LogEntryProto; import org.apache.raft.statemachine.*; -import org.apache.raft.statemachine.SimpleStateMachineStorage.SingleFileSnapshotInfo; import org.apache.raft.util.AutoCloseableLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f58b5ef8/raft-server/src/main/java/org/apache/raft/server/storage/FileInfo.java ---------------------------------------------------------------------- diff --git a/raft-server/src/main/java/org/apache/raft/server/storage/FileInfo.java b/raft-server/src/main/java/org/apache/raft/server/storage/FileInfo.java index 78e0301..cdeb622 100644 --- a/raft-server/src/main/java/org/apache/raft/server/storage/FileInfo.java +++ b/raft-server/src/main/java/org/apache/raft/server/storage/FileInfo.java @@ -23,11 +23,13 @@ import java.nio.file.Path; /** * Metadata about a file. + * + * The objects of this class are immutable. */ public class FileInfo { - private Path path; - private MD5Hash fileDigest; - private long fileSize; + private final Path path; + private final MD5Hash fileDigest; + private final long fileSize; public FileInfo(Path path, MD5Hash fileDigest) { this.path = path; @@ -40,14 +42,17 @@ public class FileInfo { return path.toString(); } + /** @return the path of the file. */ public Path getPath() { return path; } + /** @return the MD5 file digest of the file. */ public MD5Hash getFileDigest() { return fileDigest; } + /** @return the size of the file. */ public long getFileSize() { return fileSize; } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f58b5ef8/raft-server/src/main/java/org/apache/raft/statemachine/FileListSnapshotInfo.java ---------------------------------------------------------------------- diff --git a/raft-server/src/main/java/org/apache/raft/statemachine/FileListSnapshotInfo.java b/raft-server/src/main/java/org/apache/raft/statemachine/FileListSnapshotInfo.java new file mode 100644 index 0000000..b65fc13 --- /dev/null +++ b/raft-server/src/main/java/org/apache/raft/statemachine/FileListSnapshotInfo.java @@ -0,0 +1,64 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.raft.statemachine; + +import org.apache.raft.server.protocol.TermIndex; +import org.apache.raft.server.storage.FileInfo; + +import java.util.Collections; +import java.util.List; + +/** + * Each snapshot has a list of files. + * + * The objects of this class are immutable. + */ +public class FileListSnapshotInfo implements SnapshotInfo { + private final TermIndex termIndex; + private final List<FileInfo> files; + + public FileListSnapshotInfo(List<FileInfo> files, long term, long index) { + this.termIndex = TermIndex.newTermIndex(term, index); + this.files = Collections.unmodifiableList(files); + } + + @Override + public TermIndex getTermIndex() { + return termIndex; + } + + @Override + public long getTerm() { + return termIndex.getTerm(); + } + + @Override + public long getIndex() { + return termIndex.getIndex(); + } + + @Override + public List<FileInfo> getFiles() { + return files; + } + + @Override + public String toString() { + return termIndex + ":" + files; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f58b5ef8/raft-server/src/main/java/org/apache/raft/statemachine/SimpleStateMachineStorage.java ---------------------------------------------------------------------- diff --git a/raft-server/src/main/java/org/apache/raft/statemachine/SimpleStateMachineStorage.java b/raft-server/src/main/java/org/apache/raft/statemachine/SimpleStateMachineStorage.java index c317eb8..a779f98 100644 --- a/raft-server/src/main/java/org/apache/raft/statemachine/SimpleStateMachineStorage.java +++ b/raft-server/src/main/java/org/apache/raft/statemachine/SimpleStateMachineStorage.java @@ -19,9 +19,7 @@ package org.apache.raft.statemachine; import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Lists; import org.apache.raft.io.MD5Hash; -import org.apache.raft.server.impl.RaftConfiguration; import org.apache.raft.server.protocol.TermIndex; import org.apache.raft.server.storage.FileInfo; import org.apache.raft.server.storage.RaftStorage; @@ -56,17 +54,6 @@ public class SimpleStateMachineStorage implements StateMachineStorage { private volatile SingleFileSnapshotInfo currentSnapshot = null; - public static class SingleFileSnapshotInfo extends SnapshotInfoImpl { - protected SingleFileSnapshotInfo(RaftConfiguration raftConfiguration, - Path path, MD5Hash fileDigest, long term, long endIndex) { - super(raftConfiguration, Lists.newArrayList(new FileInfo(path, fileDigest)), term, endIndex); - } - - public FileInfo getFile() { - return getFiles().get(0); - } - } - public void init(RaftStorage raftStorage) throws IOException { this.raftStorage = raftStorage; this.smDir = raftStorage.getStorageDir().getStateMachineDir(); @@ -120,10 +107,11 @@ public class SimpleStateMachineStorage implements StateMachineStorage { Matcher matcher = SNAPSHOT_REGEX.matcher(path.getFileName().toString()); if (matcher.matches()) { final long endIndex = Long.parseLong(matcher.group(2)); - if (latest == null || endIndex > latest.termIndex.getIndex()) { + if (latest == null || endIndex > latest.getIndex()) { final long term = Long.parseLong(matcher.group(1)); MD5Hash fileDigest = MD5FileUtil.readStoredMd5ForFile(path.toFile()); - latest = new SingleFileSnapshotInfo(null, path, fileDigest, term, endIndex); + final FileInfo fileInfo = new FileInfo(path, fileDigest); + latest = new SingleFileSnapshotInfo(fileInfo, term, endIndex); } } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f58b5ef8/raft-server/src/main/java/org/apache/raft/statemachine/SingleFileSnapshotInfo.java ---------------------------------------------------------------------- diff --git a/raft-server/src/main/java/org/apache/raft/statemachine/SingleFileSnapshotInfo.java b/raft-server/src/main/java/org/apache/raft/statemachine/SingleFileSnapshotInfo.java new file mode 100644 index 0000000..6b01e17 --- /dev/null +++ b/raft-server/src/main/java/org/apache/raft/statemachine/SingleFileSnapshotInfo.java @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.raft.statemachine; + +import org.apache.raft.server.storage.FileInfo; + +import java.util.Arrays; + +/** + * Each snapshot only has a single file. + * + * The objects of this class are immutable. + */ +public class SingleFileSnapshotInfo extends FileListSnapshotInfo { + public SingleFileSnapshotInfo(FileInfo fileInfo, long term, long endIndex) { + super(Arrays.asList(fileInfo), term, endIndex); + } + + /** @return the file associated with the snapshot. */ + public FileInfo getFile() { + return getFiles().get(0); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f58b5ef8/raft-server/src/main/java/org/apache/raft/statemachine/SnapshotInfo.java ---------------------------------------------------------------------- diff --git a/raft-server/src/main/java/org/apache/raft/statemachine/SnapshotInfo.java b/raft-server/src/main/java/org/apache/raft/statemachine/SnapshotInfo.java index b7deb9a..0fdcbc3 100644 --- a/raft-server/src/main/java/org/apache/raft/statemachine/SnapshotInfo.java +++ b/raft-server/src/main/java/org/apache/raft/statemachine/SnapshotInfo.java @@ -17,7 +17,6 @@ */ package org.apache.raft.statemachine; -import org.apache.raft.server.impl.RaftConfiguration; import org.apache.raft.server.protocol.TermIndex; import org.apache.raft.server.storage.FileInfo; @@ -56,10 +55,4 @@ public interface SnapshotInfo { * @return a list of Files corresponding to the this snapshot. */ List<FileInfo> getFiles(); - - /** - * Returns the RaftConfiguration corresponding to this snapshot. - * @return the RaftConfiguration corresponding to this snapshot. - */ - RaftConfiguration getRaftConfiguration(); } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f58b5ef8/raft-server/src/main/java/org/apache/raft/statemachine/SnapshotInfoImpl.java ---------------------------------------------------------------------- diff --git a/raft-server/src/main/java/org/apache/raft/statemachine/SnapshotInfoImpl.java b/raft-server/src/main/java/org/apache/raft/statemachine/SnapshotInfoImpl.java deleted file mode 100644 index 1929614..0000000 --- a/raft-server/src/main/java/org/apache/raft/statemachine/SnapshotInfoImpl.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.raft.statemachine; - -import org.apache.raft.server.impl.RaftConfiguration; -import org.apache.raft.server.protocol.TermIndex; -import org.apache.raft.server.storage.FileInfo; - -import java.util.List; - -public class SnapshotInfoImpl implements SnapshotInfo { - - protected final RaftConfiguration raftConfiguration; - protected final List<FileInfo> files; - protected final TermIndex termIndex; - - public SnapshotInfoImpl(RaftConfiguration raftConfiguration, - List<FileInfo> files, long term, long index) { - this.raftConfiguration = raftConfiguration; - this.files = files; - this.termIndex = TermIndex.newTermIndex(term, index); - } - - @Override - public TermIndex getTermIndex() { - return termIndex; - } - - @Override - public long getTerm() { - return termIndex.getTerm(); - } - - @Override - public long getIndex() { - return termIndex.getIndex(); - } - - @Override - public List<FileInfo> getFiles() { - return files; - } - - @Override - public RaftConfiguration getRaftConfiguration() { - return raftConfiguration; - } - - @Override - public String toString() { - return raftConfiguration + "." + files + "." + termIndex; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f58b5ef8/raft-server/src/test/java/org/apache/raft/statemachine/SimpleStateMachine4Testing.java ---------------------------------------------------------------------- diff --git a/raft-server/src/test/java/org/apache/raft/statemachine/SimpleStateMachine4Testing.java b/raft-server/src/test/java/org/apache/raft/statemachine/SimpleStateMachine4Testing.java index 0709d22..b8dd3f3 100644 --- a/raft-server/src/test/java/org/apache/raft/statemachine/SimpleStateMachine4Testing.java +++ b/raft-server/src/test/java/org/apache/raft/statemachine/SimpleStateMachine4Testing.java @@ -34,7 +34,6 @@ import org.apache.raft.server.storage.LogOutputStream; import org.apache.raft.server.storage.RaftStorage; import org.apache.raft.shaded.proto.RaftProtos.LogEntryProto; import org.apache.raft.shaded.proto.RaftProtos.SMLogEntryProto; -import org.apache.raft.statemachine.SimpleStateMachineStorage.SingleFileSnapshotInfo; import org.apache.raft.util.Daemon; import org.apache.raft.util.LifeCycle; import org.apache.raft.util.MD5FileUtil;
