Repository navigation
RATIS-1654. Throw exceptions instead of returning a StorageState on failure - #1701
Open
Elia-Renzoni wants to merge 3 commits into
Open
Elia-Renzoni wants to merge 3 commits into
Elia-Renzoni wants to merge 3 commits into
Conversation
Signed-off-by: Elia Renzoni <elia.renzoni03@gmail.com>
szetszwo
reviewed
Nov 18, 2026
szetszwo
left a comment
Contributor
There was a problem hiding this comment.
@Elia-Renzoni , thanks for working on this!
Found that we didn't need StorageState for error cases -- just throw an exception; see below.
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java
index 5647e89d3..e1af81816 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java
@@ -24,8 +24,10 @@ import org.apache.ratis.util.SizeInBytes;
import java.io.File;
import java.io.IOException;
+import java.nio.file.AccessDeniedException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
+import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
import java.util.Objects;
@@ -39,7 +41,6 @@ class RaftStorageDirectoryImpl implements RaftStorageDirectory {
enum StorageState {
UNINITIALIZED,
- NON_EXISTENT,
NOT_FORMATTED,
NO_SPACE,
NORMAL
@@ -139,16 +140,15 @@ class RaftStorageDirectoryImpl implements RaftStorageDirectory {
}
// or is inaccessible
if (!root.isDirectory()) {
- LOG.warn("{} is not a directory", rootPath);
- return StorageState.NON_EXISTENT;
+ throw new NotDirectoryException(rootPath);
}
if (!Files.isWritable(root.toPath())) {
- LOG.warn("The storage directory {} is not writable.", rootPath);
- return StorageState.NON_EXISTENT;
+ throw new AccessDeniedException(rootPath, null, "not writable");
}
} catch(SecurityException ex) {
- LOG.warn("Cannot access storage directory " + rootPath, ex);
- return StorageState.NON_EXISTENT;
+ final AccessDeniedException ade = new AccessDeniedException(rootPath);
+ ade.initCause(ex);
+ throw ade;
}
if (toLock) {diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java
index 33dc333bb..44ea3c31f 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java
@@ -57,9 +57,7 @@ public class RaftStorageImpl implements RaftStorage {
public void initialize() throws IOException {
try {
if (startupOption == StartupOption.FORMAT) {
- if (storageDir.analyzeStorage(false) == StorageState.NON_EXISTENT) {
- throw new IOException("Cannot format " + storageDir);
- }
+ storageDir.analyzeStorage(false);
storageDir.lock();
format();
state = storageDir.analyzeStorage(false);
@@ -108,9 +106,7 @@ public class RaftStorageImpl implements RaftStorage {
StorageState storageState = storageDir.analyzeStorage(toLock);
// Existence of raft-meta.tmp means the change of votedFor/term has not
// been committed. Thus we should delete the tmp file.
- if (storageState != StorageState.NON_EXISTENT) {
- cleanMetaTmpFile();
- }
+ cleanMetaTmpFile();
if (storageState == StorageState.NORMAL) {
final File f = storageDir.getMetaFile();
if (!f.exists()) {Signed-off-by: Elia Renzoni <elia.renzoni03@gmail.com>
Contributor
Author
Thanks for the suggestions! I've pushed the changes here: 422172f |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
With this PR
analyzeStoragethrows exceptions instead of returning a StorageState on failure. The goal is to avoid confusion when this type of failure occurs.What is the link to the Apache JIRA
https://issues.apache.org/jira/browse/RATIS-1654
How was this patch tested?
unit test