szilard-nemeth commented on a change in pull request #3358: URL: https://github.com/apache/hadoop/pull/3358#discussion_r732879744
########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/TestQueueCapacityConfigParser.java ########## @@ -0,0 +1,208 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityConfigParser { + + private static final String QUEUE = "root.test"; + private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; + private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; + private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; + private static final String RESOURCE_TYPES = "yarn.io/gpu"; + + private final QueueCapacityConfigParser capacityConfigParser + = new QueueCapacityConfigParser(); + + @Test + public void testPercentageCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setCapacity(QUEUE, 50); + + QueueCapacityVector percentageCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(percentageCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(0).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(1).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(1).getResourceValue(), 1e-6); + + QueueCapacityVector rootCapacityVector = capacityConfigParser.parse(conf, + CapacitySchedulerConfiguration.ROOT, ""); + List<QueueCapacityVectorEntry> rootResources = + Lists.newArrayList(rootCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(0).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(1).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(1).getResourceValue(), 1e-6); Review comment: can you also add the resource types (memory, vcores) assertions here as well? ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/TestQueueCapacityConfigParser.java ########## @@ -0,0 +1,208 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityConfigParser { + + private static final String QUEUE = "root.test"; + private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; + private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; + private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; + private static final String RESOURCE_TYPES = "yarn.io/gpu"; + + private final QueueCapacityConfigParser capacityConfigParser + = new QueueCapacityConfigParser(); + + @Test + public void testPercentageCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setCapacity(QUEUE, 50); + + QueueCapacityVector percentageCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(percentageCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(0).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(1).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(1).getResourceValue(), 1e-6); + + QueueCapacityVector rootCapacityVector = capacityConfigParser.parse(conf, + CapacitySchedulerConfiguration.ROOT, ""); + List<QueueCapacityVectorEntry> rootResources = + Lists.newArrayList(rootCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(0).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(1).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testWeightCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setNonLabeledQueueWeight(QUEUE, 6); + + QueueCapacityVector weightCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(weightCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(0).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(1).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testAbsoluteCapacityVectorConfig() { Review comment: Please use constants for "memory-mb" & "yarn.io/gpu" from class: ResourceInformation. ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestResourceVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestResourceVector { + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void testCreation() { + ResourceVector zeroResourceVector = ResourceVector.newInstance(); + Assert.assertEquals(0, zeroResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("custom"), 1e-6); + + ResourceVector uniformResourceVector = ResourceVector.of(10); + Assert.assertEquals(10, uniformResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("custom"), 1e-6); + + Map<String, Long> customResources = new HashMap<>(); + customResources.put("custom", 2L); + Resource resource = Resource.newInstance(10, 5, customResources); + ResourceVector resourceVectorFromResource = ResourceVector.of(resource); + Assert.assertEquals(10, resourceVectorFromResource.getValue("memory-mb"), 1e-6); + Assert.assertEquals(5, resourceVectorFromResource.getValue("vcores"), 1e-6); + Assert.assertEquals(2, resourceVectorFromResource.getValue("custom"), 1e-6); + } + + @Test + public void subtract() { Review comment: all testcase names should start with "test" by JUnit convention ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestResourceVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestResourceVector { + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void testCreation() { + ResourceVector zeroResourceVector = ResourceVector.newInstance(); + Assert.assertEquals(0, zeroResourceVector.getValue("memory-mb"), 1e-6); Review comment: Pls extract the precision into a constant. ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestResourceVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestResourceVector { + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void testCreation() { + ResourceVector zeroResourceVector = ResourceVector.newInstance(); + Assert.assertEquals(0, zeroResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("custom"), 1e-6); + + ResourceVector uniformResourceVector = ResourceVector.of(10); + Assert.assertEquals(10, uniformResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("custom"), 1e-6); + + Map<String, Long> customResources = new HashMap<>(); + customResources.put("custom", 2L); + Resource resource = Resource.newInstance(10, 5, customResources); + ResourceVector resourceVectorFromResource = ResourceVector.of(resource); + Assert.assertEquals(10, resourceVectorFromResource.getValue("memory-mb"), 1e-6); + Assert.assertEquals(5, resourceVectorFromResource.getValue("vcores"), 1e-6); + Assert.assertEquals(2, resourceVectorFromResource.getValue("custom"), 1e-6); + } + + @Test + public void subtract() { Review comment: Can you also add a subtract case that goes to negative, e.g. 10-25? ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/TestQueueCapacityConfigParser.java ########## @@ -0,0 +1,208 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityConfigParser { + + private static final String QUEUE = "root.test"; + private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; + private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; + private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; + private static final String RESOURCE_TYPES = "yarn.io/gpu"; + + private final QueueCapacityConfigParser capacityConfigParser + = new QueueCapacityConfigParser(); + + @Test + public void testPercentageCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setCapacity(QUEUE, 50); + + QueueCapacityVector percentageCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(percentageCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(0).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(1).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(1).getResourceValue(), 1e-6); + + QueueCapacityVector rootCapacityVector = capacityConfigParser.parse(conf, + CapacitySchedulerConfiguration.ROOT, ""); + List<QueueCapacityVectorEntry> rootResources = + Lists.newArrayList(rootCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(0).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(1).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(1).getResourceValue(), 1e-6); Review comment: Note: Applies to all other testcases. ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/TestQueueCapacityConfigParser.java ########## @@ -0,0 +1,208 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityConfigParser { + + private static final String QUEUE = "root.test"; + private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; + private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; + private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; + private static final String RESOURCE_TYPES = "yarn.io/gpu"; + + private final QueueCapacityConfigParser capacityConfigParser + = new QueueCapacityConfigParser(); + + @Test + public void testPercentageCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setCapacity(QUEUE, 50); + + QueueCapacityVector percentageCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(percentageCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(0).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(1).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(1).getResourceValue(), 1e-6); + + QueueCapacityVector rootCapacityVector = capacityConfigParser.parse(conf, + CapacitySchedulerConfiguration.ROOT, ""); + List<QueueCapacityVectorEntry> rootResources = + Lists.newArrayList(rootCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(0).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(1).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testWeightCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setNonLabeledQueueWeight(QUEUE, 6); + + QueueCapacityVector weightCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(weightCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(0).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(1).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testAbsoluteCapacityVectorConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + CapacitySchedulerConfiguration.CAPACITY, ABSOLUTE_RESOURCE); + conf.set(YarnConfiguration.RESOURCE_TYPES, RESOURCE_TYPES); + ResourceUtils.resetResourceTypes(conf); + + QueueCapacityVector absoluteCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(12 * 1024, absoluteCapacityVector.getResource("memory-mb").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("vcores").getVectorResourceType()); + Assert.assertEquals(6f, absoluteCapacityVector.getResource("vcores").getResourceValue(), 1e-6); Review comment: Can you extract the values of vcores, gpu and memory (6, 10 and 12, respectively) to some constants? I can see they are coming from here: ``` private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; ``` I think it would greatly improve readability to extract the numbers to constants and use them in these 3 constants + the expectations as well. ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/TestQueueCapacityConfigParser.java ########## @@ -0,0 +1,208 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityConfigParser { + + private static final String QUEUE = "root.test"; + private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; + private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; + private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; + private static final String RESOURCE_TYPES = "yarn.io/gpu"; + + private final QueueCapacityConfigParser capacityConfigParser + = new QueueCapacityConfigParser(); + + @Test + public void testPercentageCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setCapacity(QUEUE, 50); + + QueueCapacityVector percentageCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(percentageCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(0).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(1).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(1).getResourceValue(), 1e-6); + + QueueCapacityVector rootCapacityVector = capacityConfigParser.parse(conf, + CapacitySchedulerConfiguration.ROOT, ""); + List<QueueCapacityVectorEntry> rootResources = + Lists.newArrayList(rootCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(0).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(1).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testWeightCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setNonLabeledQueueWeight(QUEUE, 6); + + QueueCapacityVector weightCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(weightCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(0).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(1).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testAbsoluteCapacityVectorConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + CapacitySchedulerConfiguration.CAPACITY, ABSOLUTE_RESOURCE); + conf.set(YarnConfiguration.RESOURCE_TYPES, RESOURCE_TYPES); + ResourceUtils.resetResourceTypes(conf); + + QueueCapacityVector absoluteCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("memory-mb").getVectorResourceType()); Review comment: Could you extract the precision to a constant? (1e-6) ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/TestQueueCapacityConfigParser.java ########## @@ -0,0 +1,208 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityConfigParser { + + private static final String QUEUE = "root.test"; + private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; + private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; + private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; + private static final String RESOURCE_TYPES = "yarn.io/gpu"; + + private final QueueCapacityConfigParser capacityConfigParser + = new QueueCapacityConfigParser(); + + @Test + public void testPercentageCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setCapacity(QUEUE, 50); + + QueueCapacityVector percentageCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(percentageCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(0).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(1).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(1).getResourceValue(), 1e-6); Review comment: Can you add testing the resource names (memory, vcores I assume) as well? I suppose both the memory and vcores should be set to 50, but it's not too straightforward from the test code. ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestResourceVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestResourceVector { + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void testCreation() { + ResourceVector zeroResourceVector = ResourceVector.newInstance(); + Assert.assertEquals(0, zeroResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("custom"), 1e-6); + + ResourceVector uniformResourceVector = ResourceVector.of(10); + Assert.assertEquals(10, uniformResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("custom"), 1e-6); + + Map<String, Long> customResources = new HashMap<>(); + customResources.put("custom", 2L); + Resource resource = Resource.newInstance(10, 5, customResources); + ResourceVector resourceVectorFromResource = ResourceVector.of(resource); + Assert.assertEquals(10, resourceVectorFromResource.getValue("memory-mb"), 1e-6); + Assert.assertEquals(5, resourceVectorFromResource.getValue("vcores"), 1e-6); + Assert.assertEquals(2, resourceVectorFromResource.getValue("custom"), 1e-6); + } + + @Test + public void subtract() { + ResourceVector lhsResourceVector = ResourceVector.of(13); + ResourceVector rhsResourceVector = ResourceVector.of(5); + lhsResourceVector.subtract(rhsResourceVector); + + Assert.assertEquals(8, lhsResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(8, lhsResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(8, lhsResourceVector.getValue("custom"), 1e-6); + } + + @Test + public void increment() { + ResourceVector resourceVector = ResourceVector.of(13); + resourceVector.increment("memory-mb", 5); + + Assert.assertEquals(18, resourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(13, resourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(13, resourceVector.getValue("custom"), 1e-6); + + // Check whether overflow causes any issues + ResourceVector maxFloatResourceVector = ResourceVector.of(Float.MAX_VALUE); + maxFloatResourceVector.increment("memory-mb", 100); + Assert.assertEquals(Float.MAX_VALUE, maxFloatResourceVector.getValue("memory-mb"), 1e-6); + } + + @Test + public void testEquals() { + ResourceVector resourceVector = ResourceVector.of(13); + ResourceVector resourceVectorOther = ResourceVector.of(14); + Resource resource = Resource.newInstance(13, 13); + + Assert.assertFalse(resourceVector.equals(null)); + Assert.assertFalse(resourceVector.equals(resourceVectorOther)); + Assert.assertFalse(resourceVector.equals(resource)); Review comment: IntelliJ complains with: "'equals' between objects of inconvertible types 'ResourceVector' and 'Resource' " ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/TestQueueCapacityConfigParser.java ########## @@ -0,0 +1,208 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityConfigParser { + + private static final String QUEUE = "root.test"; + private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; + private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; + private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; + private static final String RESOURCE_TYPES = "yarn.io/gpu"; + + private final QueueCapacityConfigParser capacityConfigParser + = new QueueCapacityConfigParser(); + + @Test + public void testPercentageCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setCapacity(QUEUE, 50); + + QueueCapacityVector percentageCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(percentageCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(0).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(1).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(1).getResourceValue(), 1e-6); + + QueueCapacityVector rootCapacityVector = capacityConfigParser.parse(conf, + CapacitySchedulerConfiguration.ROOT, ""); + List<QueueCapacityVectorEntry> rootResources = + Lists.newArrayList(rootCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(0).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(1).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testWeightCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setNonLabeledQueueWeight(QUEUE, 6); + + QueueCapacityVector weightCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(weightCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(0).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(1).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testAbsoluteCapacityVectorConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + CapacitySchedulerConfiguration.CAPACITY, ABSOLUTE_RESOURCE); + conf.set(YarnConfiguration.RESOURCE_TYPES, RESOURCE_TYPES); + ResourceUtils.resetResourceTypes(conf); + + QueueCapacityVector absoluteCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(12 * 1024, absoluteCapacityVector.getResource("memory-mb").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("vcores").getVectorResourceType()); + Assert.assertEquals(6f, absoluteCapacityVector.getResource("vcores").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("yarn.io/gpu").getVectorResourceType()); + Assert.assertEquals(10f, absoluteCapacityVector.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + CapacitySchedulerConfiguration.CAPACITY, ABSOLUTE_RESOURCE_MEMORY_VCORE); + QueueCapacityVector withoutGpuVector = capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(3, withoutGpuVector.getResourceCount()); + Assert.assertEquals(0f, withoutGpuVector.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + } + + @Test + public void testMixedCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, MIXED_RESOURCE); + conf.set(YarnConfiguration.RESOURCE_TYPES, RESOURCE_TYPES); + ResourceUtils.resetResourceTypes(conf); + + QueueCapacityVector mixedCapacityVector = + capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, + mixedCapacityVector.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(1024, mixedCapacityVector.getResource("memory-mb").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + mixedCapacityVector.getResource("vcores").getVectorResourceType()); + Assert.assertEquals(50f, + mixedCapacityVector.getResource("vcores").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.WEIGHT, + mixedCapacityVector.getResource("yarn.io/gpu").getVectorResourceType()); + Assert.assertEquals(6f, + mixedCapacityVector.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + + // Test undefined capacity type default value + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, "[memory-mb=12Gi,vcores=6]"); + + QueueCapacityVector mixedCapacityVectorWithGpuUndefined = + capacityConfigParser.parse(conf, QUEUE, ""); + Assert.assertEquals(QueueCapacityType.ABSOLUTE, + mixedCapacityVectorWithGpuUndefined.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(0, mixedCapacityVectorWithGpuUndefined.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + + } + + @Test + public void testInvalidCapacityConfigs() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, "50nonexistingsuffix"); + QueueCapacityVector capacityVectorWithInvalidSuffix = + capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> entriesWithInvalidSuffix = + Lists.newArrayList(capacityVectorWithInvalidSuffix.iterator()); + Assert.assertEquals(0, entriesWithInvalidSuffix.size()); Review comment: Shouldn't this be an erroneous case instead? ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/TestQueueCapacityConfigParser.java ########## @@ -0,0 +1,208 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityConfigParser { + + private static final String QUEUE = "root.test"; + private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; + private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; + private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; + private static final String RESOURCE_TYPES = "yarn.io/gpu"; + + private final QueueCapacityConfigParser capacityConfigParser + = new QueueCapacityConfigParser(); + + @Test + public void testPercentageCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setCapacity(QUEUE, 50); + + QueueCapacityVector percentageCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(percentageCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(0).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(1).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(1).getResourceValue(), 1e-6); + + QueueCapacityVector rootCapacityVector = capacityConfigParser.parse(conf, + CapacitySchedulerConfiguration.ROOT, ""); + List<QueueCapacityVectorEntry> rootResources = + Lists.newArrayList(rootCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(0).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(1).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testWeightCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setNonLabeledQueueWeight(QUEUE, 6); + + QueueCapacityVector weightCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(weightCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(0).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(1).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testAbsoluteCapacityVectorConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + CapacitySchedulerConfiguration.CAPACITY, ABSOLUTE_RESOURCE); + conf.set(YarnConfiguration.RESOURCE_TYPES, RESOURCE_TYPES); + ResourceUtils.resetResourceTypes(conf); + + QueueCapacityVector absoluteCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(12 * 1024, absoluteCapacityVector.getResource("memory-mb").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("vcores").getVectorResourceType()); + Assert.assertEquals(6f, absoluteCapacityVector.getResource("vcores").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("yarn.io/gpu").getVectorResourceType()); + Assert.assertEquals(10f, absoluteCapacityVector.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + CapacitySchedulerConfiguration.CAPACITY, ABSOLUTE_RESOURCE_MEMORY_VCORE); + QueueCapacityVector withoutGpuVector = capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(3, withoutGpuVector.getResourceCount()); + Assert.assertEquals(0f, withoutGpuVector.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + } + + @Test + public void testMixedCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, MIXED_RESOURCE); + conf.set(YarnConfiguration.RESOURCE_TYPES, RESOURCE_TYPES); + ResourceUtils.resetResourceTypes(conf); + + QueueCapacityVector mixedCapacityVector = + capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, + mixedCapacityVector.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(1024, mixedCapacityVector.getResource("memory-mb").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + mixedCapacityVector.getResource("vcores").getVectorResourceType()); + Assert.assertEquals(50f, + mixedCapacityVector.getResource("vcores").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.WEIGHT, + mixedCapacityVector.getResource("yarn.io/gpu").getVectorResourceType()); + Assert.assertEquals(6f, + mixedCapacityVector.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + + // Test undefined capacity type default value + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, "[memory-mb=12Gi,vcores=6]"); + + QueueCapacityVector mixedCapacityVectorWithGpuUndefined = + capacityConfigParser.parse(conf, QUEUE, ""); + Assert.assertEquals(QueueCapacityType.ABSOLUTE, + mixedCapacityVectorWithGpuUndefined.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(0, mixedCapacityVectorWithGpuUndefined.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + + } + + @Test + public void testInvalidCapacityConfigs() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, "50nonexistingsuffix"); + QueueCapacityVector capacityVectorWithInvalidSuffix = + capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> entriesWithInvalidSuffix = + Lists.newArrayList(capacityVectorWithInvalidSuffix.iterator()); + Assert.assertEquals(0, entriesWithInvalidSuffix.size()); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, "[memory-100,vcores-60]"); + QueueCapacityVector invalidDelimiterCapacityVector = + capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> invalidDelimiterEntries = + Lists.newArrayList(invalidDelimiterCapacityVector.iterator()); + Assert.assertEquals(0, invalidDelimiterEntries.size()); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, "[invalid]"); + QueueCapacityVector invalidCapacityVector = + capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = + Lists.newArrayList(invalidCapacityVector.iterator()); + Assert.assertEquals(0, resources.size()); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, "[]"); + QueueCapacityVector emptyBracketCapacityVector = + capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> emptyEntries = + Lists.newArrayList(emptyBracketCapacityVector.iterator()); + Assert.assertEquals(0, resources.size()); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, ""); + QueueCapacityVector emptyCapacity = + capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> emptyResources = + Lists.newArrayList(emptyCapacity.iterator()); + Assert.assertEquals(emptyResources.size(), 0); + + conf.unset(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY); + QueueCapacityVector nonSetCapacity = + capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> nonSetResources = + Lists.newArrayList(nonSetCapacity.iterator()); + Assert.assertEquals(nonSetResources.size(), 0); + } + + @Test + public void testResourceVector() { Review comment: why this is empty? ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestResourceVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestResourceVector { + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void testCreation() { + ResourceVector zeroResourceVector = ResourceVector.newInstance(); + Assert.assertEquals(0, zeroResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("vcores"), 1e-6); Review comment: Pls use resource type name constants from ResourceInformation. ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestResourceVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestResourceVector { + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void testCreation() { + ResourceVector zeroResourceVector = ResourceVector.newInstance(); + Assert.assertEquals(0, zeroResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("custom"), 1e-6); + + ResourceVector uniformResourceVector = ResourceVector.of(10); + Assert.assertEquals(10, uniformResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("custom"), 1e-6); + + Map<String, Long> customResources = new HashMap<>(); + customResources.put("custom", 2L); + Resource resource = Resource.newInstance(10, 5, customResources); + ResourceVector resourceVectorFromResource = ResourceVector.of(resource); + Assert.assertEquals(10, resourceVectorFromResource.getValue("memory-mb"), 1e-6); + Assert.assertEquals(5, resourceVectorFromResource.getValue("vcores"), 1e-6); + Assert.assertEquals(2, resourceVectorFromResource.getValue("custom"), 1e-6); + } + + @Test + public void subtract() { + ResourceVector lhsResourceVector = ResourceVector.of(13); + ResourceVector rhsResourceVector = ResourceVector.of(5); + lhsResourceVector.subtract(rhsResourceVector); + + Assert.assertEquals(8, lhsResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(8, lhsResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(8, lhsResourceVector.getValue("custom"), 1e-6); + } + + @Test + public void increment() { Review comment: Can you also add an increment case that goes from negative to positive, e.g. -10+25? ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestQueueCapacityVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityVector { + + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void getResourceNamesByCapacityType() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + + capacityVector.setResource("memory-mb", 10, QueueCapacityType.PERCENTAGE); Review comment: Pls use resource type name constants from ResourceInformation. ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestResourceVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestResourceVector { + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void testCreation() { + ResourceVector zeroResourceVector = ResourceVector.newInstance(); + Assert.assertEquals(0, zeroResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("custom"), 1e-6); + + ResourceVector uniformResourceVector = ResourceVector.of(10); + Assert.assertEquals(10, uniformResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("custom"), 1e-6); + + Map<String, Long> customResources = new HashMap<>(); + customResources.put("custom", 2L); + Resource resource = Resource.newInstance(10, 5, customResources); + ResourceVector resourceVectorFromResource = ResourceVector.of(resource); + Assert.assertEquals(10, resourceVectorFromResource.getValue("memory-mb"), 1e-6); + Assert.assertEquals(5, resourceVectorFromResource.getValue("vcores"), 1e-6); + Assert.assertEquals(2, resourceVectorFromResource.getValue("custom"), 1e-6); + } + + @Test + public void subtract() { + ResourceVector lhsResourceVector = ResourceVector.of(13); + ResourceVector rhsResourceVector = ResourceVector.of(5); + lhsResourceVector.subtract(rhsResourceVector); + + Assert.assertEquals(8, lhsResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(8, lhsResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(8, lhsResourceVector.getValue("custom"), 1e-6); + } + + @Test + public void increment() { + ResourceVector resourceVector = ResourceVector.of(13); + resourceVector.increment("memory-mb", 5); + + Assert.assertEquals(18, resourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(13, resourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(13, resourceVector.getValue("custom"), 1e-6); + + // Check whether overflow causes any issues + ResourceVector maxFloatResourceVector = ResourceVector.of(Float.MAX_VALUE); + maxFloatResourceVector.increment("memory-mb", 100); + Assert.assertEquals(Float.MAX_VALUE, maxFloatResourceVector.getValue("memory-mb"), 1e-6); + } + + @Test + public void testEquals() { + ResourceVector resourceVector = ResourceVector.of(13); + ResourceVector resourceVectorOther = ResourceVector.of(14); + Resource resource = Resource.newInstance(13, 13); + + Assert.assertFalse(resourceVector.equals(null)); Review comment: Use this: `Assert.assertNotEquals(null, resourceVector);` ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestResourceVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestResourceVector { + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void testCreation() { + ResourceVector zeroResourceVector = ResourceVector.newInstance(); + Assert.assertEquals(0, zeroResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(0, zeroResourceVector.getValue("custom"), 1e-6); + + ResourceVector uniformResourceVector = ResourceVector.of(10); + Assert.assertEquals(10, uniformResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(10, uniformResourceVector.getValue("custom"), 1e-6); + + Map<String, Long> customResources = new HashMap<>(); + customResources.put("custom", 2L); + Resource resource = Resource.newInstance(10, 5, customResources); + ResourceVector resourceVectorFromResource = ResourceVector.of(resource); + Assert.assertEquals(10, resourceVectorFromResource.getValue("memory-mb"), 1e-6); + Assert.assertEquals(5, resourceVectorFromResource.getValue("vcores"), 1e-6); + Assert.assertEquals(2, resourceVectorFromResource.getValue("custom"), 1e-6); + } + + @Test + public void subtract() { + ResourceVector lhsResourceVector = ResourceVector.of(13); + ResourceVector rhsResourceVector = ResourceVector.of(5); + lhsResourceVector.subtract(rhsResourceVector); + + Assert.assertEquals(8, lhsResourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(8, lhsResourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(8, lhsResourceVector.getValue("custom"), 1e-6); + } + + @Test + public void increment() { + ResourceVector resourceVector = ResourceVector.of(13); + resourceVector.increment("memory-mb", 5); + + Assert.assertEquals(18, resourceVector.getValue("memory-mb"), 1e-6); + Assert.assertEquals(13, resourceVector.getValue("vcores"), 1e-6); + Assert.assertEquals(13, resourceVector.getValue("custom"), 1e-6); + + // Check whether overflow causes any issues + ResourceVector maxFloatResourceVector = ResourceVector.of(Float.MAX_VALUE); + maxFloatResourceVector.increment("memory-mb", 100); + Assert.assertEquals(Float.MAX_VALUE, maxFloatResourceVector.getValue("memory-mb"), 1e-6); + } + + @Test + public void testEquals() { + ResourceVector resourceVector = ResourceVector.of(13); + ResourceVector resourceVectorOther = ResourceVector.of(14); + Resource resource = Resource.newInstance(13, 13); + + Assert.assertFalse(resourceVector.equals(null)); + Assert.assertFalse(resourceVector.equals(resourceVectorOther)); + Assert.assertFalse(resourceVector.equals(resource)); + + ResourceVector resourceVectorOne = ResourceVector.of(1); + resourceVectorOther.subtract(resourceVectorOne); + + Assert.assertTrue(resourceVector.equals(resourceVectorOther)); Review comment: Use this: `Assert.assertEquals(resourceVector, resourceVectorOther)` ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestQueueCapacityVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityVector { + + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void getResourceNamesByCapacityType() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + + capacityVector.setResource("memory-mb", 10, QueueCapacityType.PERCENTAGE); + capacityVector.setResource("vcores", 6, QueueCapacityType.PERCENTAGE); + + // custom is not set, defaults to 0 + Assert.assertEquals(1, capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.ABSOLUTE).size()); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.ABSOLUTE).contains("custom")); + + Assert.assertEquals(2, capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.PERCENTAGE).size()); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.PERCENTAGE).contains("vcores")); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.PERCENTAGE).contains("memory-mb")); + } + + @Test + public void isResourceOfType() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + + capacityVector.setResource("memory-mb", 10, QueueCapacityType.WEIGHT); + capacityVector.setResource("vcores", 6, QueueCapacityType.PERCENTAGE); + capacityVector.setResource("custom", 3, QueueCapacityType.ABSOLUTE); + + Assert.assertTrue(capacityVector.isResourceOfType("memory-mb", QueueCapacityType.WEIGHT)); + Assert.assertTrue(capacityVector.isResourceOfType("vcores", QueueCapacityType.PERCENTAGE)); + Assert.assertTrue(capacityVector.isResourceOfType("custom", QueueCapacityType.ABSOLUTE)); + } + + @Test + public void iterator() { Review comment: All testcase names should start with "test" ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestQueueCapacityVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityVector { + + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void getResourceNamesByCapacityType() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + + capacityVector.setResource("memory-mb", 10, QueueCapacityType.PERCENTAGE); + capacityVector.setResource("vcores", 6, QueueCapacityType.PERCENTAGE); + + // custom is not set, defaults to 0 + Assert.assertEquals(1, capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.ABSOLUTE).size()); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.ABSOLUTE).contains("custom")); + + Assert.assertEquals(2, capacityVector.getResourceNamesByCapacityType( Review comment: You are not testing here that memory=10 and vcores=6 ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestQueueCapacityVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityVector { + + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void getResourceNamesByCapacityType() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + + capacityVector.setResource("memory-mb", 10, QueueCapacityType.PERCENTAGE); + capacityVector.setResource("vcores", 6, QueueCapacityType.PERCENTAGE); + + // custom is not set, defaults to 0 + Assert.assertEquals(1, capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.ABSOLUTE).size()); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.ABSOLUTE).contains("custom")); + + Assert.assertEquals(2, capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.PERCENTAGE).size()); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.PERCENTAGE).contains("vcores")); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.PERCENTAGE).contains("memory-mb")); + } + + @Test + public void isResourceOfType() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + + capacityVector.setResource("memory-mb", 10, QueueCapacityType.WEIGHT); + capacityVector.setResource("vcores", 6, QueueCapacityType.PERCENTAGE); + capacityVector.setResource("custom", 3, QueueCapacityType.ABSOLUTE); + + Assert.assertTrue(capacityVector.isResourceOfType("memory-mb", QueueCapacityType.WEIGHT)); + Assert.assertTrue(capacityVector.isResourceOfType("vcores", QueueCapacityType.PERCENTAGE)); + Assert.assertTrue(capacityVector.isResourceOfType("custom", QueueCapacityType.ABSOLUTE)); + } + + @Test + public void iterator() { Review comment: The name of the testcase is misleading: I can't see any real testing for the iterator logic here. ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/TestQueueCapacityConfigParser.java ########## @@ -0,0 +1,208 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityConfigParser { + + private static final String QUEUE = "root.test"; + private static final String ABSOLUTE_RESOURCE = "[memory-mb=12Gi, vcores=6, yarn.io/gpu=10]"; + private static final String ABSOLUTE_RESOURCE_MEMORY_VCORE = "[memory-mb=12Gi, vcores=6]"; + private static final String MIXED_RESOURCE = "[memory-mb=1024, vcores=50%, yarn.io/gpu=6w]"; + private static final String RESOURCE_TYPES = "yarn.io/gpu"; + + private final QueueCapacityConfigParser capacityConfigParser + = new QueueCapacityConfigParser(); + + @Test + public void testPercentageCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setCapacity(QUEUE, 50); + + QueueCapacityVector percentageCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(percentageCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(0).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, resources.get(1).getVectorResourceType()); + Assert.assertEquals(50f, resources.get(1).getResourceValue(), 1e-6); + + QueueCapacityVector rootCapacityVector = capacityConfigParser.parse(conf, + CapacitySchedulerConfiguration.ROOT, ""); + List<QueueCapacityVectorEntry> rootResources = + Lists.newArrayList(rootCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(0).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + rootResources.get(1).getVectorResourceType()); + Assert.assertEquals(100f, + rootResources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testWeightCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.setNonLabeledQueueWeight(QUEUE, 6); + + QueueCapacityVector weightCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> resources = Lists.newArrayList(weightCapacityVector.iterator()); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(0).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(0).getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.WEIGHT, resources.get(1).getVectorResourceType()); + Assert.assertEquals(6f, resources.get(1).getResourceValue(), 1e-6); + } + + @Test + public void testAbsoluteCapacityVectorConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + CapacitySchedulerConfiguration.CAPACITY, ABSOLUTE_RESOURCE); + conf.set(YarnConfiguration.RESOURCE_TYPES, RESOURCE_TYPES); + ResourceUtils.resetResourceTypes(conf); + + QueueCapacityVector absoluteCapacityVector = capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(12 * 1024, absoluteCapacityVector.getResource("memory-mb").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("vcores").getVectorResourceType()); + Assert.assertEquals(6f, absoluteCapacityVector.getResource("vcores").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, absoluteCapacityVector.getResource("yarn.io/gpu").getVectorResourceType()); + Assert.assertEquals(10f, absoluteCapacityVector.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + CapacitySchedulerConfiguration.CAPACITY, ABSOLUTE_RESOURCE_MEMORY_VCORE); + QueueCapacityVector withoutGpuVector = capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(3, withoutGpuVector.getResourceCount()); + Assert.assertEquals(0f, withoutGpuVector.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + } + + @Test + public void testMixedCapacityConfig() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, MIXED_RESOURCE); + conf.set(YarnConfiguration.RESOURCE_TYPES, RESOURCE_TYPES); + ResourceUtils.resetResourceTypes(conf); + + QueueCapacityVector mixedCapacityVector = + capacityConfigParser.parse(conf, QUEUE, ""); + + Assert.assertEquals(QueueCapacityType.ABSOLUTE, + mixedCapacityVector.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(1024, mixedCapacityVector.getResource("memory-mb").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.PERCENTAGE, + mixedCapacityVector.getResource("vcores").getVectorResourceType()); + Assert.assertEquals(50f, + mixedCapacityVector.getResource("vcores").getResourceValue(), 1e-6); + + Assert.assertEquals(QueueCapacityType.WEIGHT, + mixedCapacityVector.getResource("yarn.io/gpu").getVectorResourceType()); + Assert.assertEquals(6f, + mixedCapacityVector.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + + // Test undefined capacity type default value + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, "[memory-mb=12Gi,vcores=6]"); + + QueueCapacityVector mixedCapacityVectorWithGpuUndefined = + capacityConfigParser.parse(conf, QUEUE, ""); + Assert.assertEquals(QueueCapacityType.ABSOLUTE, + mixedCapacityVectorWithGpuUndefined.getResource("memory-mb").getVectorResourceType()); + Assert.assertEquals(0, mixedCapacityVectorWithGpuUndefined.getResource("yarn.io/gpu").getResourceValue(), 1e-6); + + } + + @Test + public void testInvalidCapacityConfigs() { + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(QUEUE) + + CapacitySchedulerConfiguration.CAPACITY, "50nonexistingsuffix"); + QueueCapacityVector capacityVectorWithInvalidSuffix = + capacityConfigParser.parse(conf, QUEUE, ""); + List<QueueCapacityVectorEntry> entriesWithInvalidSuffix = + Lists.newArrayList(capacityVectorWithInvalidSuffix.iterator()); + Assert.assertEquals(0, entriesWithInvalidSuffix.size()); Review comment: TBH, most of these cases should be an error case. I would also prefer if you could add all of these cases to a separate testcase if possible. ########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestQueueCapacityVector.java ########## @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.util.Lists; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityVectorEntry; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class TestQueueCapacityVector { + + private final YarnConfiguration conf = new YarnConfiguration(); + + @Before + public void setUp() { + conf.set(YarnConfiguration.RESOURCE_TYPES, "custom"); + ResourceUtils.resetResourceTypes(conf); + } + + @Test + public void getResourceNamesByCapacityType() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + + capacityVector.setResource("memory-mb", 10, QueueCapacityType.PERCENTAGE); + capacityVector.setResource("vcores", 6, QueueCapacityType.PERCENTAGE); + + // custom is not set, defaults to 0 + Assert.assertEquals(1, capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.ABSOLUTE).size()); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.ABSOLUTE).contains("custom")); + + Assert.assertEquals(2, capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.PERCENTAGE).size()); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.PERCENTAGE).contains("vcores")); + Assert.assertTrue(capacityVector.getResourceNamesByCapacityType( + QueueCapacityType.PERCENTAGE).contains("memory-mb")); + } + + @Test + public void isResourceOfType() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + + capacityVector.setResource("memory-mb", 10, QueueCapacityType.WEIGHT); + capacityVector.setResource("vcores", 6, QueueCapacityType.PERCENTAGE); + capacityVector.setResource("custom", 3, QueueCapacityType.ABSOLUTE); + + Assert.assertTrue(capacityVector.isResourceOfType("memory-mb", QueueCapacityType.WEIGHT)); + Assert.assertTrue(capacityVector.isResourceOfType("vcores", QueueCapacityType.PERCENTAGE)); + Assert.assertTrue(capacityVector.isResourceOfType("custom", QueueCapacityType.ABSOLUTE)); + } + + @Test + public void iterator() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + List<QueueCapacityVectorEntry> entries = Lists.newArrayList(capacityVector); + + Assert.assertEquals(3, entries.size()); + + QueueCapacityVector emptyCapacityVector = new QueueCapacityVector(); + List<QueueCapacityVectorEntry> emptyEntries = Lists.newArrayList(emptyCapacityVector); + + Assert.assertEquals(0, emptyEntries.size()); + } + + @Test + public void testToString() { + QueueCapacityVector capacityVector = QueueCapacityVector.newInstance(); + + capacityVector.setResource("memory-mb", 10, QueueCapacityType.WEIGHT); + capacityVector.setResource("vcores", 6, QueueCapacityType.PERCENTAGE); + capacityVector.setResource("custom", 3, QueueCapacityType.ABSOLUTE); + + Assert.assertEquals("[custom=3.0,memory-mb=10.0w,vcores=6.0%]", capacityVector.toString()); Review comment: Is it guaranteed that the custom resource always goes first? Is this intentionally passing so that the resource names sorted by their names or something else is happening? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
