Elia-Renzoni commented on PR #1701:
URL: https://github.com/apache/ratis/pull/1701#issuecomment-7001983579
> @Elia-Renzoni , thanks for working on this!
>
> Found that we didn't need StorageState for error cases -- just throw an
exception; see below.
>
> ```diff
> 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
> 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:
422172f6b80d8c12a1147e9017a520e081839b42
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]