-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resource stats to task framework (#2089)
* Add resource stats to task framework Signed-off-by: sruti1312 <[email protected]> * Update thread resource info and add tests Signed-off-by: sruti1312 <[email protected]> (cherry picked from commit 8b997c1)
- Loading branch information
1 parent
fc5bf9a
commit 9bebecd
Showing
19 changed files
with
844 additions
and
43 deletions.
There are no files selected for viewing
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
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
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
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
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
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
28 changes: 28 additions & 0 deletions
28
server/src/main/java/org/opensearch/tasks/ResourceStats.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,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 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 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) { | ||
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 | ||
); | ||
} | ||
|
||
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 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.