generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
Improve resource monitor errors #5129
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
Open
Swiddis
wants to merge
6
commits into
opensearch-project:main
Choose a base branch
from
Swiddis:fix/resource-monitor-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df48210
generic improvement: resource monitor errors
Swiddis dc2a32c
Remove 'current'
Swiddis aa89c9d
Address CodeRabbit PR feedback: fix comment, add tests, handle edge case
Swiddis 3b369f2
more coderabbit comments
Swiddis 6a28cff
Fix retry policy for resource monitors, and don't hardcode settings
Swiddis 4539700
rename tests
Swiddis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
116 changes: 116 additions & 0 deletions
116
core/src/main/java/org/opensearch/sql/monitor/ResourceStatus.java
This file contains hidden or 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,116 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.monitor; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| /** | ||
| * Represents the health status of a resource with detailed context information. This wrapper allows | ||
| * error messages to include actionable details about resource exhaustion instead of just boolean | ||
| * health checks. | ||
| */ | ||
| @Getter | ||
| @Builder | ||
| public class ResourceStatus { | ||
| /** Type of resource being monitored. */ | ||
| public enum ResourceType { | ||
| MEMORY, | ||
| CPU, | ||
| DISK, | ||
| OTHER | ||
| } | ||
|
|
||
| /** Whether the resource is healthy (within limits). */ | ||
| private final boolean healthy; | ||
|
|
||
| /** Type of resource (memory, CPU, etc.). */ | ||
| private final ResourceType type; | ||
|
|
||
| /** Human-readable description of resource state. */ | ||
| private final String description; | ||
|
|
||
| /** Current resource usage value (optional, for metrics). */ | ||
| private final Long currentUsage; | ||
|
|
||
| /** Maximum allowed resource value (optional, for metrics). */ | ||
| private final Long maxLimit; | ||
|
|
||
| /** Additional contextual information (optional). */ | ||
| private final String additionalContext; | ||
|
|
||
| /** | ||
| * Creates a healthy status with minimal information. | ||
| * | ||
| * @param type Resource type | ||
| * @return Healthy ResourceStatus | ||
| */ | ||
| public static ResourceStatus healthy(ResourceType type) { | ||
| return ResourceStatus.builder() | ||
| .healthy(true) | ||
| .type(type) | ||
| .description(type + " resources are healthy") | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an unhealthy status with detailed context. | ||
| * | ||
| * @param type Resource type | ||
| * @param currentUsage Current usage value | ||
| * @param maxLimit Maximum allowed limit | ||
| * @param description Human-readable description | ||
| * @return Unhealthy ResourceStatus with context | ||
| */ | ||
| public static ResourceStatus unhealthy( | ||
| ResourceType type, long currentUsage, long maxLimit, String description) { | ||
| return ResourceStatus.builder() | ||
| .healthy(false) | ||
| .type(type) | ||
| .currentUsage(currentUsage) | ||
| .maxLimit(maxLimit) | ||
| .description(description) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets a formatted description including usage metrics if available. | ||
| * | ||
| * @return Formatted description string | ||
| */ | ||
| public String getFormattedDescription() { | ||
| if (currentUsage != null && maxLimit != null) { | ||
| if (maxLimit <= 0) { | ||
| // Treat invalid limit as 0, don't compute percentage | ||
| return String.format( | ||
| "%s (current: %s, limit: %s)", description, formatBytes(currentUsage), formatBytes(0)); | ||
| } | ||
| double percentage = (double) currentUsage / maxLimit * 100; | ||
| return String.format( | ||
| "%s (current: %s, limit: %s, usage: %.1f%%)", | ||
| description, formatBytes(currentUsage), formatBytes(maxLimit), percentage); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return description; | ||
| } | ||
Swiddis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Formats byte values into human-readable format (KB, MB, GB). | ||
| * | ||
| * @param bytes Byte value | ||
| * @return Formatted string | ||
| */ | ||
| private String formatBytes(long bytes) { | ||
| if (bytes < 1024) { | ||
| return bytes + "B"; | ||
| } else if (bytes < 1024 * 1024) { | ||
| return String.format("%.1fKB", bytes / 1024.0); | ||
| } else if (bytes < 1024 * 1024 * 1024) { | ||
| return String.format("%.1fMB", bytes / (1024.0 * 1024)); | ||
| } else { | ||
| return String.format("%.1fGB", bytes / (1024.0 * 1024 * 1024)); | ||
| } | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
core/src/test/java/org/opensearch/sql/monitor/ResourceMonitorTest.java
This file contains hidden or 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,24 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.monitor; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class ResourceMonitorTest { | ||
|
|
||
| @Test | ||
| void testDefaultImplementationThrowsException() { | ||
| // Create a minimal subclass that doesn't override getStatus() or isHealthyImpl() | ||
| ResourceMonitor monitor = new ResourceMonitor() { | ||
| // Intentionally empty - doesn't override anything | ||
| }; | ||
|
|
||
| // Attempting to use the default path should throw UnsupportedOperationException | ||
| assertThrows(UnsupportedOperationException.class, monitor::isHealthy); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.