-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fixing backward incompatibility check Signed-off-by: Ankit Jain <[email protected]> * Fixing spotless violations Signed-off-by: Ankit Jain <[email protected]> * Adding missing javadocs Signed-off-by: Ankit Jain <[email protected]> --------- Signed-off-by: Ankit Jain <[email protected]>
- Loading branch information
1 parent
d7e7d94
commit 461e2b3
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
server/src/main/java/org/opensearch/search/ResourceType.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,66 @@ | ||
/* | ||
* 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.search; | ||
|
||
import org.opensearch.common.annotation.PublicApi; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.tasks.resourcetracker.ResourceStats; | ||
import org.opensearch.tasks.Task; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Enum to hold the resource type | ||
*/ | ||
@PublicApi(since = "2.x") | ||
public enum ResourceType { | ||
CPU("cpu", task -> task.getTotalResourceUtilization(ResourceStats.CPU)), | ||
MEMORY("memory", task -> task.getTotalResourceUtilization(ResourceStats.MEMORY)); | ||
|
||
private final String name; | ||
private final Function<Task, Long> getResourceUsage; | ||
|
||
ResourceType(String name, Function<Task, Long> getResourceUsage) { | ||
this.name = name; | ||
this.getResourceUsage = getResourceUsage; | ||
} | ||
|
||
/** | ||
* The string match here is case-sensitive | ||
* @param s name matching the resource type name | ||
* @return a {@link ResourceType} | ||
*/ | ||
public static ResourceType fromName(String s) { | ||
for (ResourceType resourceType : values()) { | ||
if (resourceType.getName().equals(s)) { | ||
return resourceType; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unknown resource type: [" + s + "]"); | ||
} | ||
|
||
public static void writeTo(StreamOutput out, ResourceType resourceType) throws IOException { | ||
out.writeString(resourceType.getName()); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Gets the resource usage for a given resource type and task. | ||
* | ||
* @param task the task for which to calculate resource usage | ||
* @return the resource usage | ||
*/ | ||
public long getResourceUsage(Task task) { | ||
return getResourceUsage.apply(task); | ||
} | ||
} |