-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: create disk memory health checker #5781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 2024 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you 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: | ||
| * | ||
| * https://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.linecorp.armeria.server.healthcheck; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import java.io.File; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionStage; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * A {@link HealthChecker} that reports as unhealthy | ||
| * when the target free disk space exceeds a file disk usable space. | ||
| * For example: | ||
| * <pre>{@code | ||
| * final DiskMemoryHealthChecker diskMemoryHealthChecker = HealthChecker.ofDisk(0.8, new File("/tmp")); | ||
| * | ||
| * // Returns false if a file disk usable memory space is less than 80%, | ||
| * // or true if a file disk usable memory space is greater than or equal to 80%. | ||
| * final boolean healthy = diskMemoryHealthChecker.isHealthy(); | ||
| * }</pre> | ||
| */ | ||
| // Forked from <a href="https://github.com/micrometer-metrics/micrometer/blob/8339d57bef8689beb8d7a18b429a166f6595f2af/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/system/DiskSpaceMetrics.java">DiskSpaceMetrics.java</a> in the micrometer core. | ||
| final class DiskMemoryHealthChecker implements Supplier<CompletionStage<HealthCheckStatus>> { | ||
|
|
||
| private final double freeDiskSpacePercentage; | ||
|
|
||
| private final File path; | ||
|
|
||
| DiskMemoryHealthChecker(double freeDiskSpacePercentage, File path) { | ||
| checkArgument(freeDiskSpacePercentage >= 0 && freeDiskSpacePercentage <= 1, | ||
| "freeDiskSpacePercentage: %s (expected >= 0 and expected <= 1)", freeDiskSpacePercentage); | ||
| requireNonNull(path); | ||
| this.freeDiskSpacePercentage = freeDiskSpacePercentage; | ||
| this.path = path; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the file usable space is greater or equal than the target space. | ||
| * @return boolean | ||
| */ | ||
| public boolean isHealthy() { | ||
| return freeDiskSpacePercentage * path.getTotalSpace() <= path.getUsableSpace(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a {@link CompletionStage} that represents | ||
| * the health of the file disk usable space asynchronously. | ||
| * @return CompletionStage | ||
| */ | ||
| @Override | ||
| public CompletionStage<HealthCheckStatus> get() { | ||
| return CompletableFuture.supplyAsync(() -> new HealthCheckStatus(isHealthy(), 500)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of adding the method variant of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's good. I personally want to make this method relating to |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright 2024 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you 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: | ||
| * | ||
| * https://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.linecorp.armeria.server.healthcheck; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class DiskMemoryCheckerTest { | ||
|
|
||
| @Test | ||
| void throwExceptionWhenTargetFreeDiskSpaceLowerThanZero() { | ||
| final double targetFreeDiskSpace = -1.0; | ||
| assertThatThrownBy(() -> HealthChecker.ofDisk(targetFreeDiskSpace, new File("/tmp"))) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("freeDiskSpacePercentage: %.1f (expected >= 0 and expected <= 1)", | ||
| targetFreeDiskSpace); | ||
| } | ||
|
|
||
| @Test | ||
| void throwExceptionWhenPathIsNull() { | ||
| final double targetFreeDiskSpace = 1.0; | ||
| assertThatThrownBy(() -> HealthChecker.ofDisk(targetFreeDiskSpace, null)) | ||
| .isInstanceOf(NullPointerException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnTrueWhenDiskSpaceIsHealthy() { | ||
| final DiskMemoryHealthChecker diskMemoryHealthChecker = new DiskMemoryHealthChecker(0.8, new File("")); | ||
| assertThat(diskMemoryHealthChecker.isHealthy()).isTrue(); | ||
|
AnneMayor marked this conversation as resolved.
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.