diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/SnapshotReader.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/SnapshotReader.java index b04d3128b..6df42be9b 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/SnapshotReader.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/SnapshotReader.java @@ -22,7 +22,7 @@ import com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta; /** - * Snapshot reader. + * Snapshot reader.Note: it's not thread-safe. * * @author boyan (boyan@alibaba-inc.com) * diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/SnapshotWriter.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/SnapshotWriter.java index b9c8220f9..14acf9905 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/SnapshotWriter.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/SnapshotWriter.java @@ -24,7 +24,7 @@ import com.google.protobuf.Message; /** - * Snapshot writer. + * Snapshot writer. Note: it's not thread-safe. * * @author boyan (boyan@alibaba-inc.com) * diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/LocalSnapshotReader.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/LocalSnapshotReader.java index 0879123a7..c4e21ac87 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/LocalSnapshotReader.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/LocalSnapshotReader.java @@ -53,11 +53,21 @@ public class LocalSnapshotReader extends SnapshotReader { private final String path; private final LocalSnapshotStorage snapshotStorage; private final SnapshotThrottle snapshotThrottle; + private volatile boolean closed; @Override public void close() throws IOException { + checkState(); snapshotStorage.unref(this.getSnapshotIndex()); + this.destroyReaderInFileService(); + this.closed = true; + } + + private void checkState() { + if (this.closed) { + throw new IllegalStateException("Reader was closed"); + } } public LocalSnapshotReader(LocalSnapshotStorage snapshotStorage, SnapshotThrottle snapshotThrottle, Endpoint addr, @@ -68,6 +78,7 @@ public LocalSnapshotReader(LocalSnapshotStorage snapshotStorage, SnapshotThrottl this.addr = addr; this.path = path; this.readerId = 0; + this.closed = false; this.metaTable = new LocalSnapshotMetaTable(raftOptions); } @@ -118,6 +129,7 @@ public SnapshotMeta load() { @Override public String generateURIForCopy() { + checkState(); if (this.addr == null || this.addr.equals(new Endpoint(Utils.IP_ANY, 0))) { LOG.error("Address is not specified"); return null; diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/LocalSnapshotWriter.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/LocalSnapshotWriter.java index c9a9617a5..c2b9b1120 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/LocalSnapshotWriter.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/LocalSnapshotWriter.java @@ -47,12 +47,14 @@ public class LocalSnapshotWriter extends SnapshotWriter { private final LocalSnapshotMetaTable metaTable; private final String path; private final LocalSnapshotStorage snapshotStorage; + private volatile boolean closed; public LocalSnapshotWriter(String path, LocalSnapshotStorage snapshotStorage, RaftOptions raftOptions) { super(); this.snapshotStorage = snapshotStorage; this.path = path; this.metaTable = new LocalSnapshotMetaTable(raftOptions); + this.closed = false; } @Override @@ -95,21 +97,32 @@ public void close() throws IOException { @Override public void close(final boolean keepDataOnError) throws IOException { + checkState(); this.snapshotStorage.close(this, keepDataOnError); + this.closed = true; + } + + private void checkState() { + if (this.closed) { + throw new IllegalStateException("Writer was closed"); + } } @Override public boolean saveMeta(final SnapshotMeta meta) { + checkState(); this.metaTable.setMeta(meta); return true; } public boolean sync() throws IOException { + checkState(); return this.metaTable.saveToFile(this.path + File.separator + JRAFT_SNAPSHOT_META_FILE); } @Override public boolean addFile(final String fileName, final Message fileMeta) { + checkState(); final Builder metaBuilder = LocalFileMeta.newBuilder(); if (fileMeta != null) { metaBuilder.mergeFrom(fileMeta); @@ -120,6 +133,7 @@ public boolean addFile(final String fileName, final Message fileMeta) { @Override public boolean removeFile(final String fileName) { + checkState(); return this.metaTable.removeFile(fileName); } diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/SnapshotFileReader.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/SnapshotFileReader.java index b4ed3afa0..50535d7a5 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/SnapshotFileReader.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/snapshot/local/SnapshotFileReader.java @@ -30,7 +30,7 @@ import com.alipay.sofa.jraft.util.ByteBufferCollector; /** - * Snapshot file reader + * Snapshot file reader. Note: it's not thread-safe. * * @author boyan (boyan@alibaba-inc.com) *