diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CapacitySchedulerConfiguration.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CapacitySchedulerConfiguration.java index a88beef8c97..6f6d47f99c3 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CapacitySchedulerConfiguration.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CapacitySchedulerConfiguration.java @@ -44,6 +44,7 @@ import org.apache.hadoop.yarn.server.resourcemanager.reservation.ReservationSchedulerConfiguration; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerUtils; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.AppPriorityACLConfigurationParser.AppPriorityACLKeyType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.policy.FairQueueOrderingPolicy; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.policy.PriorityUtilizationQueueOrderingPolicy; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.policy.QueueOrderingPolicy; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.placement.MultiNodeLookupPolicy; @@ -1534,6 +1535,12 @@ public void setGlobalMaximumApplicationsPerQueue(int val) { public static final String QUEUE_PRIORITY_UTILIZATION_ORDERING_POLICY = "priority-utilization"; + /** + * Fair queue selection + */ + public static final String FAIR_ORDERING_POLICY = + "fair-queue"; + public static final String DEFAULT_QUEUE_ORDERING_POLICY = QUEUE_UTILIZATION_ORDERING_POLICY; @@ -1561,6 +1568,8 @@ public QueueOrderingPolicy getQueueOrderingPolicy(String queue, } else if (policyType.trim().equals( QUEUE_PRIORITY_UTILIZATION_ORDERING_POLICY)) { qop = new PriorityUtilizationQueueOrderingPolicy(true); + } else if (policyType.trim().equals(FAIR_ORDERING_POLICY)) { + qop = new FairQueueOrderingPolicy(); } else { String message = "Unable to construct queue ordering policy=" + policyType + " queue=" diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/policy/FairQueueOrderingPolicy.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/policy/FairQueueOrderingPolicy.java new file mode 100644 index 00000000000..97eefd309de --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/policy/FairQueueOrderingPolicy.java @@ -0,0 +1,89 @@ +/** + * 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.policy; + +import com.google.common.annotations.VisibleForTesting; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Random; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CSQueue; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; + + +/** + * Policy which picks a child queue to assign to with equal probability. + */ +public class FairQueueOrderingPolicy implements QueueOrderingPolicy { + + private List queues; + + @Override + public void setQueues(List queues) { + this.queues = queues; + } + + @Override + public Iterator getAssignmentIterator(String partition) { + return new RandomIterator(new ArrayList<>(queues)); + } + + @Override + public String getConfigName() { + return CapacitySchedulerConfiguration.FAIR_ORDERING_POLICY; + } + + static class RandomIterator implements Iterator { + private int idx; + private List q; + private Random random; + + // TODO: this is single threaded only + RandomIterator(List q) { + this.idx = q.size() - 1; + this.q = q; + this.random = new Random(); + } + + @Override + public boolean hasNext() { + return idx >= 0; + } + + @Override + public CSQueue next() { + int selected = random.nextInt(idx + 1); + CSQueue ret = q.get(selected); + Collections.swap(q, selected, idx--); + return ret; + } + + @Override + public void remove() { + throw new UnsupportedOperationException( + "Unsupported remove for " + this.getClass().getCanonicalName()); + } + + @VisibleForTesting + protected void setRandom(Random r) { + this.random = r; + } + } +} diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/policy/package-info.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/policy/package-info.java new file mode 100644 index 00000000000..4bccc5a199f --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/policy/package-info.java @@ -0,0 +1,24 @@ +/* + * 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.policy + * contains classes related to capacity scheduler queue ordering policies. + */ +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.policy; \ No newline at end of file diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/policy/TestFairQueueOrderingPolicy.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/policy/TestFairQueueOrderingPolicy.java new file mode 100644 index 00000000000..630a5c2ea1e --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/policy/TestFairQueueOrderingPolicy.java @@ -0,0 +1,64 @@ +/** + * 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.policy; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CSQueue; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.policy.FairQueueOrderingPolicy.RandomIterator; +import org.junit.Assert; +import org.junit.Test; + +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +/** + * Tests {@link FairQueueOrderingPolicy}. + */ +public class TestFairQueueOrderingPolicy { + + @Test + public void testUtilizationOrdering() { + FairQueueOrderingPolicy policy = new FairQueueOrderingPolicy(); + CSQueue q1 = mock(CSQueue.class); + CSQueue q2 = mock(CSQueue.class); + CSQueue q3 = mock(CSQueue.class); + List queues = Arrays.asList(q1, q2, q3); + policy.setQueues(queues); + RandomIterator itr = (RandomIterator) + policy.getAssignmentIterator(null); + Random r = mock(Random.class); + when(r.nextInt(eq(3))).thenReturn(1); + when(r.nextInt(eq(2))).thenReturn(0); + when(r.nextInt(eq(1))).thenReturn(0); + itr.setRandom(r); + List actual = new ArrayList<>(); + while (itr.hasNext()) { + actual.add(itr.next()); + } + Assert.assertEquals(3, actual.size()); + Assert.assertEquals(q2, actual.get(0)); + Assert.assertEquals(q1, actual.get(1)); + Assert.assertEquals(q3, actual.get(2)); + } +}