Add check for disk space usage in git directory Introduce a new check (`gitspace`) that monitors available disk space on the volume where Git repositories are stored. The check fails if the usable space falls below a configurable threshold, which defaults to 10%. This addition helps detect low-disk-space conditions early, avoiding potential service instability due to lack of storage. The threshold can be overridden in the healthcheck configuration using the `minDiskFreePercent` parameter. Bug: Issue 428666920 Change-Id: Ic4d56f8ac55f6320dd3a7d2ee9dcadb980e14829
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java index d866586..c6b5b43 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java +++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
@@ -46,6 +46,7 @@ private static final String QUERY_DEFAULT = "status:open"; private static final int LIMIT_DEFAULT = 10; private static final int ACTIVE_WORKERS_THRESHOLD_DEFAULT = 80; + private static final int GIT_SPACE_MIN_DISK_FREE_PCT_DEFAULT = 10; private static final String USERNAME_DEFAULT = "healthcheck"; private static final String PASSWORD_DEFAULT = ""; private static final String FAIL_FILE_FLAG_DEFAULT = "data/healthcheck/fail"; @@ -128,6 +129,11 @@ return repos; } + public int getMinFreeDiskPercent(String healthCheckName) { + return config.getInt( + HEALTHCHECK, healthCheckName, "minFreeDiskPercent", GIT_SPACE_MIN_DISK_FREE_PCT_DEFAULT); + } + public String getUsername(String healthCheckName) { return getStringWithFallback("username", healthCheckName, USERNAME_DEFAULT); }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java index 5d106af..9178537 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java +++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java
@@ -22,6 +22,7 @@ import com.googlesource.gerrit.plugins.healthcheck.check.BlockedThreadsCheck; import com.googlesource.gerrit.plugins.healthcheck.check.ChangesIndexHealthCheck; import com.googlesource.gerrit.plugins.healthcheck.check.DeadlockCheck; +import com.googlesource.gerrit.plugins.healthcheck.check.GitSpaceCheck; import com.googlesource.gerrit.plugins.healthcheck.check.HealthCheck; import com.googlesource.gerrit.plugins.healthcheck.check.HttpActiveWorkersCheck; import com.googlesource.gerrit.plugins.healthcheck.check.JGitHealthCheck; @@ -41,6 +42,7 @@ bindChecker(DeadlockCheck.class); bindChecker(BlockedThreadsCheck.class); bindChecker(ChangesIndexHealthCheck.class); + bindChecker(GitSpaceCheck.class); DynamicSet.bind(binder(), OnlineUpgradeListener.class).to(ChangesIndexHealthCheck.class);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/GitSpaceCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/GitSpaceCheck.java new file mode 100644 index 0000000..d8d4b59 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/GitSpaceCheck.java
@@ -0,0 +1,68 @@ +// Copyright (C) 2025 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.healthcheck.check; + +import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.GITSPACE; + +import com.google.common.flogger.FluentLogger; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.gerrit.metrics.MetricMaker; +import com.google.gerrit.server.config.GerritServerConfig; +import com.google.gerrit.server.config.SitePaths; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import com.googlesource.gerrit.plugins.healthcheck.HealthCheckConfig; +import java.io.File; +import java.nio.file.Path; +import org.eclipse.jgit.lib.Config; + +@Singleton +public class GitSpaceCheck extends AbstractHealthCheck { + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); + private final Path gitPath; + + @Inject + public GitSpaceCheck( + @GerritServerConfig Config gerritConfig, + SitePaths site, + ListeningExecutorService executor, + HealthCheckConfig config, + MetricMaker metricMaker) { + super(executor, config, GITSPACE, metricMaker); + + this.gitPath = site.resolve(gerritConfig.getString("gerrit", null, "basePath")); + } + + protected File getGitDirectory() { + return gitPath.toFile(); + } + + @Override + protected Result doCheck() throws Exception { + int minFreeDiskPercent = config.getMinFreeDiskPercent(GITSPACE); + long total = getGitDirectory().getTotalSpace(); + + if (total <= 0) { + logger.atWarning().log( + "Cannot determine total disk space for Git directory: %s", getGitDirectory()); + return Result.FAILED; + } + + long usable = getGitDirectory().getUsableSpace(); + int freePercent = (int) ((usable * 100.0) / total); + + return freePercent < minFreeDiskPercent ? Result.FAILED : Result.PASSED; + } +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java index d8f1621..26ce70e 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java +++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java
@@ -16,6 +16,7 @@ public interface HealthCheckNames { String JGIT = "jgit"; + String GITSPACE = "gitspace"; String PROJECTSLIST = "projectslist"; String QUERYCHANGES = "querychanges"; String AUTH = "auth";
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md index 1883724..c2c7dd3 100644 --- a/src/main/resources/Documentation/config.md +++ b/src/main/resources/Documentation/config.md
@@ -51,6 +51,7 @@ - `querychanges`: check the ability to query changes - `jgit` : check connectivity to the filesystem and ability to open a JGit ref and object +- `gitspace`: Checks that sufficient disk space is available on the volume where Git repositories are stored - `projectslist` : check the ability to list projects with their descriptions - `auth`: check the ability to authenticate with username and password - `activeworkers`: check the number of active worker threads and the ability to create a new one @@ -87,6 +88,11 @@ Default: All-Projects, All-Users +- `healthcheck.gitspace.minFreeDiskPercent` : Minimum acceptable percentage of free disk space + before the check fails. + + Default: 10 + - `healthcheck.auth.username` : Username to use for authentication Default: healthcheck
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/GitSpaceCheckTest.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/GitSpaceCheckTest.java new file mode 100644 index 0000000..ff21a6a --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/GitSpaceCheckTest.java
@@ -0,0 +1,121 @@ +// Copyright (C) 2025 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.healthcheck; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.*; + +import com.google.common.util.concurrent.MoreExecutors; +import com.google.gerrit.metrics.DisabledMetricMaker; +import com.google.gerrit.server.config.SitePaths; +import com.googlesource.gerrit.plugins.healthcheck.check.AbstractHealthCheck; +import com.googlesource.gerrit.plugins.healthcheck.check.GitSpaceCheck; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.eclipse.jgit.lib.Config; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class GitSpaceCheckTest { + private File mockFile; + private Path tempSitePath; + + private static Path createTempSitePath() throws IOException { + Path tmp = Files.createTempFile("gerrit_", "_site"); + Files.deleteIfExists(tmp); + return tmp; + } + + @Before + public void setUp() throws IOException { + mockFile = mock(File.class); + tempSitePath = createTempSitePath(); + } + + @After + public void tearDown() throws IOException { + if (tempSitePath != null && Files.exists(tempSitePath)) { + Files.delete(tempSitePath); + } + } + + private GitSpaceCheck createGitSpaceCheck(String configContent) throws IOException { + HealthCheckConfig config = new HealthCheckConfig(configContent); + SitePaths sitePaths = new SitePaths(tempSitePath); + return new GitSpaceCheck( + new Config(), + sitePaths, + MoreExecutors.newDirectExecutorService(), + config, + new DisabledMetricMaker()) { + @Override + protected File getGitDirectory() { + return mockFile; + } + }; + } + + private GitSpaceCheck createGitSpaceCheck() throws IOException { + Config config = new Config(); + config.setBoolean("healthcheck", "gitspace", "enabled", true); + return createGitSpaceCheck(config.toText()); + } + + @Test + public void shouldPassWhenUsableDiskAboveThreshold() throws Exception { + GitSpaceCheck gitSpaceCheck = createGitSpaceCheck(); + when(mockFile.getTotalSpace()).thenReturn(100L); + when(mockFile.getUsableSpace()).thenReturn(20L); + + assertThat(gitSpaceCheck.run().result()).isEqualTo(AbstractHealthCheck.Result.PASSED); + } + + @Test + public void shouldFailWhenUsableDiskBelowThreshold() throws Exception { + GitSpaceCheck gitSpaceCheck = createGitSpaceCheck(); + when(mockFile.getTotalSpace()).thenReturn(100L); + when(mockFile.getUsableSpace()).thenReturn(5L); + + assertThat(gitSpaceCheck.run().result()).isEqualTo(AbstractHealthCheck.Result.FAILED); + } + + @Test + public void shouldComplyWithNonDefaultThreshold() throws Exception { + Config config = new Config(); + config.setBoolean("healthcheck", "gitspace", "enabled", true); + config.setInt("healthcheck", "gitspace", "minFreeDiskPercent", 50); + config.toText(); + + GitSpaceCheck gitSpaceCheck = createGitSpaceCheck(config.toText()); + + when(mockFile.getTotalSpace()).thenReturn(100L); + when(mockFile.getUsableSpace()).thenReturn(5L); + assertThat(gitSpaceCheck.run().result()).isEqualTo(AbstractHealthCheck.Result.FAILED); + + when(mockFile.getUsableSpace()).thenReturn(60L); + assertThat(gitSpaceCheck.run().result()).isEqualTo(AbstractHealthCheck.Result.PASSED); + } + + @Test + public void shouldFailWhenTotalSpaceIsZero() throws Exception { + GitSpaceCheck gitSpaceCheck = createGitSpaceCheck(); + when(mockFile.getTotalSpace()).thenReturn(0L); + + assertThat(gitSpaceCheck.run().result()).isEqualTo(AbstractHealthCheck.Result.FAILED); + } +}