-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
core/src/main/java/com/linecorp/armeria/server/healthcheck/DiskMemoryHealthChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* 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(100, new File("/tmp")); | ||
* | ||
* // Returns false if a file disk usable memory space is less than 100 bytes, | ||
* // or true if a file disk usable memory space is greater than or equal to 100 bytes. | ||
* 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 HealthChecker { | ||
|
||
private final double targetFreeDiskSpace; | ||
|
||
private final File path; | ||
|
||
DiskMemoryHealthChecker(double targetFreeDiskSpace, File path) { | ||
checkArgument(targetFreeDiskSpace >= 0, "freeDiskSpace: %s (expected: >= 0)", targetFreeDiskSpace); | ||
requireNonNull(path); | ||
this.targetFreeDiskSpace = targetFreeDiskSpace; | ||
this.path = path; | ||
} | ||
|
||
/** | ||
* Returns true if the file usable space is greater or equal than the target space. | ||
* @return boolean | ||
*/ | ||
@Override | ||
public boolean isHealthy() { | ||
return path.getUsableSpace() >= targetFreeDiskSpace; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
core/src/test/java/com/linecorp/armeria/server/healthcheck/DiskMemoryCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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("freeDiskSpace: %.1f (expected: >= 0)", targetFreeDiskSpace); | ||
} | ||
|
||
@Test | ||
void throwExceptionWhenPathIsNull() { | ||
final double targetFreeDiskSpace = 1.0; | ||
assertThatThrownBy(() -> HealthChecker.ofDisk(targetFreeDiskSpace, null)) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
void shouldReturnTrueWhenDiskSpaceIsHealthy() { | ||
final DiskMemoryHealthChecker diskMemoryHealthChecker = | ||
(DiskMemoryHealthChecker) HealthChecker.ofDisk( | ||
1.0, new File(".") | ||
); | ||
assertThat(diskMemoryHealthChecker.isHealthy()).isTrue(); | ||
} | ||
} |