-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add resource stats to task framework #2089
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
Merged
dblock
merged 3 commits into
opensearch-project:main
from
sruti1312:feature/resource-stats
Mar 28, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
28 changes: 28 additions & 0 deletions
28
server/src/main/java/org/opensearch/tasks/ResourceStats.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,28 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.tasks; | ||
|
|
||
| /** | ||
| * Different resource stats are defined. | ||
| */ | ||
| public enum ResourceStats { | ||
| CPU("cpu_time_in_nanos"), | ||
| MEMORY("memory_in_bytes"); | ||
|
|
||
| private final String statsName; | ||
|
|
||
| ResourceStats(String statsName) { | ||
| this.statsName = statsName; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return statsName; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
server/src/main/java/org/opensearch/tasks/ResourceStatsType.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,32 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.tasks; | ||
|
|
||
| /** Defines the different types of resource stats. */ | ||
| public enum ResourceStatsType { | ||
| // resource stats of the worker thread reported directly from runnable. | ||
| WORKER_STATS("worker_stats", false); | ||
|
|
||
| private final String statsType; | ||
| private final boolean onlyForAnalysis; | ||
|
|
||
| ResourceStatsType(String statsType, boolean onlyForAnalysis) { | ||
| this.statsType = statsType; | ||
| this.onlyForAnalysis = onlyForAnalysis; | ||
| } | ||
|
|
||
| public boolean isOnlyForAnalysis() { | ||
| return onlyForAnalysis; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return statsType; | ||
| } | ||
| } |
108 changes: 108 additions & 0 deletions
108
server/src/main/java/org/opensearch/tasks/ResourceUsageInfo.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,108 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.tasks; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.EnumMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| /** | ||
| * Thread resource usage information for particular resource stats type. | ||
| * <p> | ||
| * It captures the resource usage information like memory, CPU about a particular execution of thread | ||
| * for a specific stats type. | ||
| */ | ||
| public class ResourceUsageInfo { | ||
| private static final Logger logger = LogManager.getLogger(ResourceUsageInfo.class); | ||
| private final EnumMap<ResourceStats, ResourceStatsInfo> statsInfo = new EnumMap<>(ResourceStats.class); | ||
|
|
||
| public ResourceUsageInfo(ResourceUsageMetric... resourceUsageMetrics) { | ||
| for (ResourceUsageMetric resourceUsageMetric : resourceUsageMetrics) { | ||
| this.statsInfo.put(resourceUsageMetric.getStats(), new ResourceStatsInfo(resourceUsageMetric.getValue())); | ||
| } | ||
| } | ||
|
|
||
| public void recordResourceUsageMetrics(ResourceUsageMetric... resourceUsageMetrics) { | ||
| for (ResourceUsageMetric resourceUsageMetric : resourceUsageMetrics) { | ||
| final ResourceStatsInfo resourceStatsInfo = statsInfo.get(resourceUsageMetric.getStats()); | ||
| if (resourceStatsInfo != null) { | ||
| updateResourceUsageInfo(resourceStatsInfo, resourceUsageMetric); | ||
| } else { | ||
| throw new IllegalStateException( | ||
| "cannot update [" | ||
| + resourceUsageMetric.getStats().toString() | ||
| + "] entry as its not present current_stats_info:" | ||
| + statsInfo | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void updateResourceUsageInfo(ResourceStatsInfo resourceStatsInfo, ResourceUsageMetric resourceUsageMetric) { | ||
| long currentEndValue; | ||
| long newEndValue; | ||
| do { | ||
| currentEndValue = resourceStatsInfo.endValue.get(); | ||
| newEndValue = resourceUsageMetric.getValue(); | ||
| if (currentEndValue > newEndValue) { | ||
sruti1312 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| logger.debug( | ||
| "dropping resource usage update as the new value is lower than current value [" | ||
| + "resource_stats=[{}], " | ||
| + "current_end_value={}, " | ||
| + "new_end_value={}]", | ||
| resourceUsageMetric.getStats(), | ||
| currentEndValue, | ||
| newEndValue | ||
| ); | ||
| return; | ||
| } | ||
| } while (!resourceStatsInfo.endValue.compareAndSet(currentEndValue, newEndValue)); | ||
| logger.debug( | ||
| "updated resource usage info [resource_stats=[{}], " + "old_end_value={}, new_end_value={}]", | ||
| resourceUsageMetric.getStats(), | ||
| currentEndValue, | ||
| newEndValue | ||
| ); | ||
| } | ||
sruti1312 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public Map<ResourceStats, ResourceStatsInfo> getStatsInfo() { | ||
| return Collections.unmodifiableMap(statsInfo); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return statsInfo.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Defines resource stats information. | ||
| */ | ||
| static class ResourceStatsInfo { | ||
| private final long startValue; | ||
| private final AtomicLong endValue; | ||
|
|
||
| private ResourceStatsInfo(long startValue) { | ||
| this.startValue = startValue; | ||
| this.endValue = new AtomicLong(startValue); | ||
| } | ||
|
|
||
| public long getTotalValue() { | ||
| return endValue.get() - startValue; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.valueOf(getTotalValue()); | ||
| } | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
server/src/main/java/org/opensearch/tasks/ResourceUsageMetric.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,27 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.tasks; | ||
|
|
||
| public class ResourceUsageMetric { | ||
| private final ResourceStats stats; | ||
| private final long value; | ||
|
|
||
| public ResourceUsageMetric(ResourceStats stats, long value) { | ||
| this.stats = stats; | ||
| this.value = value; | ||
| } | ||
|
|
||
| public ResourceStats getStats() { | ||
| return stats; | ||
| } | ||
|
|
||
| public long getValue() { | ||
| return value; | ||
| } | ||
| } |
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.