Skip to content

RATIS-1654. Throw exceptions instead of returning a StorageState on failure - #1701

Open
Elia-Renzoni wants to merge 3 commits into
apache:masterfrom
Elia-Renzoni:new-storage-state
Open

Elia-Renzoni wants to merge 3 commits into
apache:masterfrom
Elia-Renzoni:new-storage-state

Conversation

@Elia-Renzoni

@Elia-Renzoni Elia-Renzoni commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

With this PR analyzeStorage throws 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

Signed-off-by: Elia Renzoni <elia.renzoni03@gmail.com>

@szetszwo szetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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()) {

@Elia-Renzoni Elia-Renzoni changed the title RATIS-1654. Add a StorageState for insufficient privilege RATIS-1654. Throw exceptions instead of returning a StorageState on failure Nov 19, 2026
@Elia-Renzoni

Copy link
Copy Markdown
Contributor Author

@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()) {

Thanks for the suggestions! I've pushed the changes here: 422172f

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants