YARN-9354. Resources should be created with ResourceTypesTestHelper instead of TestUtils. Contributed by Andras Gyori

This commit is contained in:
Szilard Nemeth 2020-03-10 16:44:33 +01:00
parent 61f4cf3055
commit cf9cf83a43
4 changed files with 39 additions and 45 deletions

View File

@ -57,14 +57,15 @@ public final class ResourceTypesTestHelper {
Resource resource = RECORD_FACTORY.newRecordInstance(Resource.class); Resource resource = RECORD_FACTORY.newRecordInstance(Resource.class);
resource.setMemorySize(memory); resource.setMemorySize(memory);
resource.setVirtualCores(vCores); resource.setVirtualCores(vCores);
if (customResources != null) {
for (Map.Entry<String, String> customResource : for (Map.Entry<String, String> customResource :
customResources.entrySet()) { customResources.entrySet()) {
String resourceName = customResource.getKey(); String resourceName = customResource.getKey();
ResourceInformation resourceInformation = ResourceInformation resourceInformation =
createResourceInformation(resourceName, createResourceInformation(resourceName,
customResource.getValue()); customResource.getValue());
resource.setResourceInformation(resourceName, resourceInformation); resource.setResourceInformation(resourceName, resourceInformation);
}
} }
return resource; return resource;
} }
@ -107,7 +108,12 @@ public final class ResourceTypesTestHelper {
public static Map<String, String> extractCustomResourcesAsStrings( public static Map<String, String> extractCustomResourcesAsStrings(
Resource res) { Resource res) {
Map<String, Long> resValues = extractCustomResources(res); Map<String, Long> resValues = extractCustomResources(res);
return resValues.entrySet().stream() return convertCustomResources(resValues);
}
public static Map<String, String> convertCustomResources(
Map<String, ? extends Number> customResources) {
return customResources.entrySet().stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
Map.Entry::getKey, e -> String.valueOf(e.getValue()))); Map.Entry::getKey, e -> String.valueOf(e.getValue())));
} }

View File

@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableMap;
import org.apache.hadoop.yarn.event.Dispatcher; import org.apache.hadoop.yarn.event.Dispatcher;
import org.apache.hadoop.yarn.event.DrainDispatcher; import org.apache.hadoop.yarn.event.DrainDispatcher;
import org.apache.hadoop.yarn.event.Event; import org.apache.hadoop.yarn.event.Event;
import org.apache.hadoop.yarn.resourcetypes.ResourceTypesTestHelper;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptEventType; import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptEventType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -46,7 +47,6 @@ import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptS
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.TestUtils;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fifo.FifoScheduler; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fifo.FifoScheduler;
import org.apache.hadoop.yarn.server.utils.BuilderUtils; import org.apache.hadoop.yarn.server.utils.BuilderUtils;
@ -112,8 +112,11 @@ public abstract class ApplicationMasterServiceTestBase {
private void requestResources(MockAM am, long memory, int vCores, private void requestResources(MockAM am, long memory, int vCores,
Map<String, Integer> customResources) throws Exception { Map<String, Integer> customResources) throws Exception {
Map<String, String> convertedCustomResources =
ResourceTypesTestHelper.convertCustomResources(customResources);
am.allocate(Collections.singletonList(ResourceRequest.newBuilder() am.allocate(Collections.singletonList(ResourceRequest.newBuilder()
.capability(TestUtils.createResource(memory, vCores, customResources)) .capability(ResourceTypesTestHelper.newResource(
memory, vCores, convertedCustomResources))
.numContainers(1) .numContainers(1)
.resourceName("*") .resourceName("*")
.build()), null); .build()), null);
@ -551,9 +554,11 @@ public abstract class ApplicationMasterServiceTestBase {
MockRM rm = new MockRM(yarnConf); MockRM rm = new MockRM(yarnConf);
rm.start(); rm.start();
MockNM nm1 = rm.registerNode("199.99.99.1:" + DEFAULT_PORT, TestUtils MockNM nm1 = rm.registerNode("199.99.99.1:" + DEFAULT_PORT,
.createResource(DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, ResourceTypesTestHelper.newResource(
DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES, null)); DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB,
DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES,
null));
MockRMAppSubmissionData data = MockRMAppSubmissionData data =
MockRMAppSubmissionData.Builder.createWithMemory(GB, rm) MockRMAppSubmissionData.Builder.createWithMemory(GB, rm)
@ -615,10 +620,11 @@ public abstract class ApplicationMasterServiceTestBase {
MockRM rm = new MockRM(yarnConf); MockRM rm = new MockRM(yarnConf);
rm.start(); rm.start();
MockNM nm1 = rm.registerNode("199.99.99.1:" + DEFAULT_PORT, TestUtils MockNM nm1 = rm.registerNode("199.99.99.1:" + DEFAULT_PORT,
.createResource(DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, ResourceTypesTestHelper.newResource(
DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB,
DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES, DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES,
ImmutableMap.of(CUSTOM_RES, 4))); ImmutableMap.of(CUSTOM_RES, "4")));
MockRMAppSubmissionData data = MockRMAppSubmissionData data =
MockRMAppSubmissionData.Builder.createWithMemory(GB, rm) MockRMAppSubmissionData.Builder.createWithMemory(GB, rm)

View File

@ -30,6 +30,7 @@ import org.apache.hadoop.yarn.api.records.ResourceInformation;
import org.apache.hadoop.yarn.api.records.ResourceRequest; import org.apache.hadoop.yarn.api.records.ResourceRequest;
import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
import org.apache.hadoop.yarn.resourcetypes.ResourceTypesTestHelper;
import org.apache.hadoop.yarn.server.resourcemanager.MockAM; import org.apache.hadoop.yarn.server.resourcemanager.MockAM;
import org.apache.hadoop.yarn.server.resourcemanager.MockNM; import org.apache.hadoop.yarn.server.resourcemanager.MockNM;
import org.apache.hadoop.yarn.server.resourcemanager.MockNodes; import org.apache.hadoop.yarn.server.resourcemanager.MockNodes;
@ -298,16 +299,16 @@ public class TestCapacitySchedulerWithMultiResourceTypes {
MockRM rm = new MockRM(conf); MockRM rm = new MockRM(conf);
rm.start(); rm.start();
Map<String, Integer> nameToValues = new HashMap<>(); Map<String, String> nameToValues = new HashMap<>();
nameToValues.put(ResourceInformation.GPU_URI, 4); nameToValues.put(ResourceInformation.GPU_URI, "4");
// Register NM1 with 10GB memory, 4 CPU and 4 GPU // Register NM1 with 10GB memory, 4 CPU and 4 GPU
MockNM nm1 = rm.registerNode("127.0.0.1:1234", MockNM nm1 = rm.registerNode("127.0.0.1:1234",
TestUtils.createResource(10 * GB, 4, nameToValues)); ResourceTypesTestHelper.newResource(10 * GB, 4, nameToValues));
nameToValues.clear(); nameToValues.clear();
// Register NM2 with 10GB memory, 4 CPU and 0 GPU // Register NM2 with 10GB memory, 4 CPU and 0 GPU
rm.registerNode("127.0.0.1:1235", rm.registerNode("127.0.0.1:1235",
TestUtils.createResource(10 * GB, 4, nameToValues)); ResourceTypesTestHelper.newResource(10 * GB, 4, nameToValues));
RMApp app1 = MockRMAppSubmitter.submit(rm, RMApp app1 = MockRMAppSubmitter.submit(rm,
MockRMAppSubmissionData.Builder.createWithMemory(1024, rm) MockRMAppSubmissionData.Builder.createWithMemory(1024, rm)
@ -330,9 +331,9 @@ public class TestCapacitySchedulerWithMultiResourceTypes {
Assert.assertEquals(4, report_nm1.getAvailableResource() Assert.assertEquals(4, report_nm1.getAvailableResource()
.getResourceInformation(ResourceInformation.GPU_URI).getValue()); .getResourceInformation(ResourceInformation.GPU_URI).getValue());
nameToValues.put(ResourceInformation.GPU_URI, 4); nameToValues.put(ResourceInformation.GPU_URI, "4");
Resource containerGpuResource = Resource containerGpuResource =
TestUtils.createResource(1 * GB, 1, nameToValues); ResourceTypesTestHelper.newResource(1 * GB, 1, nameToValues);
// Allocate one container which takes all 4 GPU // Allocate one container which takes all 4 GPU
am1.allocate( am1.allocate(
@ -355,7 +356,7 @@ public class TestCapacitySchedulerWithMultiResourceTypes {
nameToValues.clear(); nameToValues.clear();
Resource containerResource = Resource containerResource =
TestUtils.createResource(1 * GB, 1, nameToValues); ResourceTypesTestHelper.newResource(1 * GB, 1, nameToValues);
// Allocate one more container which doesnt need GPU // Allocate one more container which doesnt need GPU
am1.allocate( am1.allocate(
Collections.singletonList(ResourceRequest.newInstance( Collections.singletonList(ResourceRequest.newInstance(
@ -479,8 +480,8 @@ public class TestCapacitySchedulerWithMultiResourceTypes {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
fiCaApp1.updateResourceRequests(Collections.singletonList( fiCaApp1.updateResourceRequests(Collections.singletonList(
ResourceRequest.newBuilder() ResourceRequest.newBuilder()
.capability(TestUtils.createResource(1 * GB, 1, .capability(ResourceTypesTestHelper.newResource(1 * GB, 1,
ImmutableMap.of("res_1", 10))) ImmutableMap.of("res_1", "10")))
.numContainers(1) .numContainers(1)
.resourceName("*") .resourceName("*")
.build())); .build()));

View File

@ -467,23 +467,4 @@ public class TestUtils {
cs.submitResourceCommitRequest(clusterResource, cs.submitResourceCommitRequest(clusterResource,
csAssignment); csAssignment);
} }
/**
* An easy way to create resources other than memory and vcores for tests.
* @param memory memory
* @param vcores vcores
* @param nameToValues resource types other than memory and vcores.
* @return created resource
*/
public static Resource createResource(long memory, int vcores,
Map<String, Integer> nameToValues) {
Resource res = Resource.newInstance(memory, vcores);
if (nameToValues != null) {
for (Map.Entry<String, Integer> entry : nameToValues.entrySet()) {
res.setResourceInformation(entry.getKey(), ResourceInformation
.newInstance(entry.getKey(), "", entry.getValue()));
}
}
return res;
}
} }