Skip to content

Commit f31c92d

Browse files
committed
Reworked health check for repo storage to avoid race conditions
1 parent 71cc10f commit f31c92d

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/main/java/edu/kit/datamanager/repo/service/impl/ContentInformationService.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@
4747
import edu.kit.datamanager.util.PatchUtil;
4848
import java.io.InputStream;
4949
import java.net.URI;
50-
import java.net.URISyntaxException;
5150
import java.net.URL;
52-
import java.nio.file.Files;
53-
import java.nio.file.Path;
54-
import java.nio.file.Paths;
5551
import java.time.Instant;
5652
import java.util.ArrayList;
5753
import java.util.Collection;
@@ -64,6 +60,12 @@
6460
import java.util.Optional;
6561
import java.util.Set;
6662
import jakarta.servlet.http.HttpServletResponse;
63+
import java.io.File;
64+
import java.util.concurrent.ExecutorService;
65+
import java.util.concurrent.Executors;
66+
import java.util.concurrent.Future;
67+
import java.util.concurrent.TimeUnit;
68+
import java.util.concurrent.TimeoutException;
6769
import org.slf4j.Logger;
6870
import org.slf4j.LoggerFactory;
6971
import org.springframework.beans.factory.annotation.Autowired;
@@ -545,7 +547,29 @@ public Health health() {
545547
LOGGER.trace("Obtaining health information.");
546548
boolean repositoryPathAvailable = true;
547549
URL basePath = applicationProperties.getBasepath();
550+
ExecutorService executor = Executors.newSingleThreadExecutor();
551+
552+
Future<Boolean> future = executor.submit(() -> {
553+
File mount = new File(basePath.toURI());
554+
return mount.exists() && mount.isDirectory() && mount.canRead() && mount.list() != null;
555+
});
556+
548557
try {
558+
LOGGER.trace("Checking repository path at {}.", basePath);
559+
Boolean accessible = future.get(5, TimeUnit.SECONDS);
560+
LOGGER.trace("Repository path is " + (accessible ? "accessible" : "not accessible"));
561+
repositoryPathAvailable = accessible;
562+
} catch (TimeoutException e) {
563+
LOGGER.error("Timeout while checking repository path.");
564+
repositoryPathAvailable = false;
565+
} catch (Exception e) {
566+
LOGGER.error("Error while checking repository path.", e);
567+
repositoryPathAvailable = false;
568+
} finally {
569+
executor.shutdownNow();
570+
}
571+
572+
/*try {
549573
Path basePathAsPath = Paths.get(basePath.toURI());
550574
Path probe = Paths.get(basePathAsPath.toString(), "probe.txt");
551575
try {
@@ -563,7 +587,7 @@ public Health health() {
563587
} catch (URISyntaxException ex) {
564588
LOGGER.error("Invalid base path uri of " + basePath + ".", ex);
565589
repositoryPathAvailable = false;
566-
}
590+
}*/
567591
if (repositoryPathAvailable) {
568592
return Health.up().withDetail("ContentInformation", dao.count()).build();
569593
} else {

0 commit comments

Comments
 (0)