[Java Worker]Add a resource checker for java worker creation (#10948)

Co-authored-by: Kai Yang <kfstorm@outlook.com>
This commit is contained in:
bermaker 2020-09-22 22:00:28 +08:00 committed by GitHub
parent e3b4850224
commit 273015814d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 3 deletions

View file

@ -16,9 +16,17 @@ public abstract class BaseTaskOptions {
public BaseTaskOptions(Map<String, Double> resources) { public BaseTaskOptions(Map<String, Double> resources) {
for (Map.Entry<String, Double> entry : resources.entrySet()) { for (Map.Entry<String, Double> entry : resources.entrySet()) {
if (entry.getValue().compareTo(0.0) <= 0) { if (entry.getValue() == null || entry.getValue().compareTo(0.0) <= 0) {
throw new IllegalArgumentException(String.format("Resource capacity should be " + throw new IllegalArgumentException(String.format("Resource values should be "
"positive, but got resource %s = %f.", entry.getKey(), entry.getValue())); + "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; this.resources = resources;

View 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);
}
}