mirror of
https://github.com/vale981/ray
synced 2025-03-06 02:21:39 -05:00
[Java Worker]Add a resource checker for java worker creation (#10948)
Co-authored-by: Kai Yang <kfstorm@outlook.com>
This commit is contained in:
parent
e3b4850224
commit
273015814d
2 changed files with 60 additions and 3 deletions
|
@ -16,9 +16,17 @@ public abstract class BaseTaskOptions {
|
|||
|
||||
public BaseTaskOptions(Map<String, Double> resources) {
|
||||
for (Map.Entry<String, Double> entry : resources.entrySet()) {
|
||||
if (entry.getValue().compareTo(0.0) <= 0) {
|
||||
throw new IllegalArgumentException(String.format("Resource capacity should be " +
|
||||
"positive, but got resource %s = %f.", entry.getKey(), entry.getValue()));
|
||||
if (entry.getValue() == null || entry.getValue().compareTo(0.0) <= 0) {
|
||||
throw new IllegalArgumentException(String.format("Resource values should be "
|
||||
+ "positive. Specified resource: %s = %s.", entry.getKey(), entry.getValue()));
|
||||
}
|
||||
// Note: A resource value should be an integer if it is greater than 1.0.
|
||||
// e.g. 3.0 is a valid resource value, but 3.5 is not.
|
||||
if (entry.getValue().compareTo(1.0) >= 0
|
||||
&& entry.getValue().compareTo(Math.floor(entry.getValue())) != 0) {
|
||||
throw new IllegalArgumentException(String.format("A resource value should be "
|
||||
+ "an integer if it is greater than 1.0. Specified resource: %s = %s.",
|
||||
entry.getKey(), entry.getValue()));
|
||||
}
|
||||
}
|
||||
this.resources = resources;
|
||||
|
|
49
java/test/src/main/java/io/ray/test/BaseTaskOptionsTest.java
Normal file
49
java/test/src/main/java/io/ray/test/BaseTaskOptionsTest.java
Normal file
|
@ -0,0 +1,49 @@
|
|||
package io.ray.test;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.ray.api.options.BaseTaskOptions;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class BaseTaskOptionsTest {
|
||||
|
||||
public static class MockActorCreationOptions extends BaseTaskOptions {
|
||||
public MockActorCreationOptions(Map<String, Double> resources) {
|
||||
super(resources);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLegalResources() {
|
||||
Map<String, Double> resources = ImmutableMap.of(
|
||||
"CPU", 0.5, "GPU", 3.0, "memory", 1024.0, "A", 4294967296.0);
|
||||
new MockActorCreationOptions(resources);
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = {IllegalArgumentException.class})
|
||||
public void testIllegalResourcesWithNullValue() {
|
||||
Map<String, Double> resources = new HashMap<>();
|
||||
resources.put("CPU", null);
|
||||
new MockActorCreationOptions(resources);
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = {IllegalArgumentException.class})
|
||||
public void testIllegalResourcesWithZeroValue() {
|
||||
Map<String, Double> resources = ImmutableMap.of("CPU", 0.0);
|
||||
new MockActorCreationOptions(resources);
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = {IllegalArgumentException.class})
|
||||
public void testIllegalResourcesWithNegativeValue() {
|
||||
Map<String, Double> resources = ImmutableMap.of("CPU", -1.0);
|
||||
new MockActorCreationOptions(resources);
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = {IllegalArgumentException.class})
|
||||
public void testIllegalResourcesWithNonIntegerValue() {
|
||||
Map<String, Double> resources = ImmutableMap.of("CPU", 3.5);
|
||||
new MockActorCreationOptions(resources);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue