Skip to content

Commit

Permalink
feat: adds close state checking for snapshot reader and writer (#1114)
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 authored Jun 25, 2024
1 parent 6be4448 commit 687446e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta;

/**
* Snapshot reader.
* Snapshot reader.<strong>Note: it's not thread-safe.</strong>
*
* @author boyan ([email protected])
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.google.protobuf.Message;

/**
* Snapshot writer.
* Snapshot writer. <strong>Note: it's not thread-safe.</strong>
*
* @author boyan ([email protected])
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.alipay.sofa.jraft.util.ByteBufferCollector;

/**
* Snapshot file reader
* Snapshot file reader. <strong>Note: it's not thread-safe.</strong>
*
* @author boyan ([email protected])
*
Expand Down

0 comments on commit 687446e

Please sign in to comment.