Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds close state checking for snapshot reader and writer #1114

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (boyan@alibaba-inc.com)
*
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 (boyan@alibaba-inc.com)
*
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;
fengjiachun marked this conversation as resolved.
Show resolved Hide resolved
}

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 (boyan@alibaba-inc.com)
*
Expand Down
Loading