diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/CompoundOrderingPolicy.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/CompoundOrderingPolicy.java new file mode 100644 index 0000000..5cb4694 --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/CompoundOrderingPolicy.java @@ -0,0 +1,108 @@ +/** + * 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.policy; + +import java.util.*; +import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.RMContainer; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.*; + +/** + * An OrderingPolicy which supports combining other + * OrderingPolicies together to implement overall logic. + * Comparison occurs in the order of the schedulingComparators in the policies + * member, with the first non-0 value returned + * (0 if all results are 0) + */ +public class CompoundOrderingPolicy + implements OrderingPolicy { + + private class CompoundComparator implements Comparator { + @Override + public int compare(SchedulableProcess r1, SchedulableProcess r2) { + for (OrderingPolicy policy : policies) { + int result = policy.getSchedulingComparator().compare(r1, r2); + if (result != 0) return result; + } + return 0; + } + } + + private List> policies = + new ArrayList>(); + + private CompoundComparator compoundComparator = new CompoundComparator(); + + @Override + public Comparator getSchedulingComparator() { + return compoundComparator; + } + + @Override + public boolean reorderForContainerAllocation( + SchedulableProcess schedulableProcess, RMContainer r) { + for (OrderingPolicy policy : policies) { + boolean result = policy.reorderForContainerAllocation( + schedulableProcess, r); + if (result) return result; + } + return false; + } + + @Override + public boolean reorderForContainerRelease( + SchedulableProcess schedulableProcess, RMContainer r) { + for (OrderingPolicy policy : policies) { + boolean result = policy.reorderForContainerRelease( + schedulableProcess, r); + if (result) return result; + } + return false; + } + + public void setPolicies( + List> policies) { + this.policies = policies; + } + + public List> getPolicies() { + return policies; + } + + @Override + public void configure(String conf) { + + } + + @Override + public String getInfo() { + StringBuilder info = new StringBuilder(); + info.append("CompoundOrderingPolicy ( "); + for (Iterator> ic = policies.iterator(); + ic.hasNext();) { + OrderingPolicy c = ic.next(); + info.append(c.getInfo()); + if (ic.hasNext()) { + info.append(", "); + } + } + info.append(" )"); + return info.toString(); + } + +} diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/FifoOrderingPolicy.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/FifoOrderingPolicy.java new file mode 100644 index 0000000..9ce6657 --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/FifoOrderingPolicy.java @@ -0,0 +1,73 @@ +/** + * 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.policy; + +import java.util.*; +import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.RMContainer; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.*; + +/** + * A comparator which orders SchedulableProcesses by submission order, earlier + * submission is lesser + */ +public class FifoOrderingPolicy implements OrderingPolicy { + + private class FifoComparator implements Comparator { + @Override + public int compare(SchedulableProcess r1, SchedulableProcess r2) { + int res = r1.compareInputOrderTo(r2); + if (res == 0) { + //cannot return equality for different processses, will result in + //"loss". process must always have unique ids to use as a fallback + res = r1.getId().compareTo(r2.getId()); + } + return res; + } + } + + private FifoComparator fifoComparator = new FifoComparator(); + + @Override + public Comparator getSchedulingComparator() { + return fifoComparator; + } + + @Override + public boolean reorderForContainerAllocation( + SchedulableProcess schedulableProcess, RMContainer r) { + return false; + } + + @Override + public boolean reorderForContainerRelease( + SchedulableProcess schedulableProcess, RMContainer r) { + return false; + } + + @Override + public void configure(String conf) { + + } + + @Override + public String getInfo() { + return "FifoOrderingPolicy"; + } + +} diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/OrderingPolicy.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/OrderingPolicy.java new file mode 100644 index 0000000..b96a8b9 --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/OrderingPolicy.java @@ -0,0 +1,66 @@ +/** + * 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.policy; + +import java.util.*; +import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.RMContainer; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.*; + +/** + * An OrderingPolicy manages the ordering of SchedulableProcesses for allocation + * and preemption for a queue + */ +public interface OrderingPolicy { + + /** + * Should the passed in schedulableProcess be reordered for allocation and + * preemption based on the allocation of the passed container (which has + * just occurred) + */ + public boolean reorderForContainerAllocation( + SchedulableProcess schedulableProcess, RMContainer r); + + /** + * Should the passed in schedulableProcess be reordered for allocation and + * preemption based on the release of the passed container (which has + * just occurred) + */ + public boolean reorderForContainerRelease( + SchedulableProcess schedulableProcess, RMContainer r); + + /** + * Get the scheduling comparator. The SchedulingComparator is the comparator + * which is used to order SchedulableProcesses for Allocation and Preemption. + * They are allocated in ascending order and preempted in descending order. + */ + public Comparator getSchedulingComparator(); + + /** + * If the Policy supports configuration it can be passed here + * @param conf The configuration information + */ + public void configure(String conf); + + /** + * Information about the policy, should include it's type & potentially + * configuration options & status + */ + public String getInfo(); + +} diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/SchedulableProcess.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/SchedulableProcess.java new file mode 100644 index 0000000..b472caf --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/SchedulableProcess.java @@ -0,0 +1,67 @@ +/** + * 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.policy; + +import java.util.*; + +import org.apache.hadoop.yarn.api.records.Priority; +import org.apache.hadoop.yarn.api.records.Resource; + + +/** + * A SchedulableProcess is a process to be scheduled, + * for example, an application / application attempt + */ +public interface SchedulableProcess { + + /** + * Compare the passed SchedulableProcess to this one for input order. + * Input order is implementation defined and should reflect the + * correct ordering for first-in first-out processing, which is to say, the + * order of creation or appearance in a naturally ordered list or hierarchy of + * like items + */ + public int compareInputOrderTo(SchedulableProcess other); + + /** + * Id - each SchedulableProcess must have a unique id + */ + public String getId(); + + /** + * View of Resources consumed by the process who's mutation is managed for + * the scheduler through updateSchedulingState. + */ + public Resource getSchedulingConsumption(); + + /** + * View of Resources wanted by the process who's mutation is managed for + * the scheduler through updateSchedulingState. + * Is the total of current consumption and outstanding resource requests. + */ + public Resource getSchedulingDemand(); + + /** + * Use to manage SchedulingConsumption, SchedulingDemand, and any other + * mutable values to insure ordering consistency. Aforementioned properties + * should not change except when this method is called + */ + public void updateSchedulingState(); + +} diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/SchedulingOrder.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/SchedulingOrder.java new file mode 100644 index 0000000..8a52ea9 --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/SchedulingOrder.java @@ -0,0 +1,210 @@ +/** + * 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.policy; + +import java.util.*; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.RMContainer; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.*; + + +/** + * SchedulingOrder is used by the scheduler to order SchedulableProcesses for + * container assignment and preemption + */ +public class SchedulingOrder { + /* + * Note: SchedulingOrder depends upon external + * synchronization of all use of the SchedulableProcess Collection and + * Iterators for correctness and to avoid concurrent modification issues + */ + + private static final Log LOG = LogFactory.getLog(SchedulingOrder.class); + + private TreeSet schedulableProcesses; + private OrderingPolicy policy; + + public SchedulingOrder() { } + + public SchedulingOrder(OrderingPolicy + policy) { + setOrderingPolicy(policy); + } + + /** + * Get the collection of SchedulableProcesses which are managed by this + * SchedulingOrder - should include processes returned by the Assignment and + * Preemption iterator with no guarantees regarding order + */ + public Collection getSchedulableProcesses() { + return schedulableProcesses; + } + + /** + * Return an iterator over the collection of SchedulableProcesses which orders + * them for container assignment + */ + public Iterator getAssignmentIterator() { + return schedulableProcesses.iterator(); + } + + /** + * Return an iterator over the collection of SchedulableProcesses which orders + * them for preemption + */ + public Iterator getPreemptionIterator() { + return schedulableProcesses.descendingIterator(); + } + + private void reorderSchedulableProcess(S schedulableProcess) { + //remove, update comparable data, and reinsert to update position in order + schedulableProcesses.remove(schedulableProcess); + schedulableProcess.updateSchedulingState(); + schedulableProcesses.add(schedulableProcess); + } + + /** + * The passed SchedulableProcess has been allocated the passed Container, + * take appropriate action (depending on policy, a reordering of the + * SchedulableProcess may be required) + */ + public void containerAllocated(S schedulableProcess, + RMContainer r) { + if (policy.reorderForContainerAllocation( + schedulableProcess, r)) { + reorderSchedulableProcess(schedulableProcess); + } + } + + /** + * The passed SchedulableProcess has released the passed Container, + * take appropriate action (depending on policy, a reordering of the + * SchedulableProcess may be required) + */ + public void containerReleased(S schedulableProcess, + RMContainer r) { + if (policy.reorderForContainerRelease( + schedulableProcess, r)) { + reorderSchedulableProcess(schedulableProcess); + } + } + + public void setOrderingPolicy(OrderingPolicy policy) { + this.policy = policy; + TreeSet schedulableProcesses = new TreeSet(policy.getSchedulingComparator()); + if (this.schedulableProcesses != null) { + schedulableProcesses.addAll(this.schedulableProcesses); + } + this.schedulableProcesses = schedulableProcesses; + } + + public OrderingPolicy getOrderingPolicy() { + return policy; + } + + /** + * Setup policy configuration, typically based on details provided in the + * scheduler configuration + */ + public void configurePolicy(String policyConfiguration) { + String[] policyDefs = policyConfiguration.split( + java.util.regex.Pattern.quote("+")); + if (policyDefs.length == 1) { + setOrderingPolicy(getPolicyInstance(policyDefs[0])); + } else { + CompoundOrderingPolicy compoundPolicy = new CompoundOrderingPolicy(); + for (int i = 0;i < policyDefs.length;i++) { + compoundPolicy.getPolicies().add( + getPolicyInstance(policyDefs[i]) + ); + } + setOrderingPolicy(compoundPolicy); + } + } + + public static OrderingPolicy getPolicyInstance( + String policyDef) { + + policyDef = policyDef.trim(); + String[] defComponents = policyDef.split(":"); + + String policyClass = defComponents[0]; + + if (policyClass.equals("fifo")) { + policyClass = FifoOrderingPolicy.class.getName(); + } + if (policyClass.equals("compound")) { + policyClass = CompoundOrderingPolicy.class.getName(); + } + OrderingPolicy policy; + try { + policy = (OrderingPolicy) + Class.forName(policyClass).newInstance(); + } catch (Exception e) { + String message = "Unable to construct scheduler policy, " + e.getMessage(); + throw new RuntimeException(message, e); + } + if (defComponents.length > 1) { + policy.configure(defComponents[1]); + } + return policy; + } + + /** + * Display policy type & information regarding configuration & status + */ + public String getInfo() { + String info = "OrderingPolicy: " + policy.getInfo(); + return info; + } + + /** + * Add a SchedulableProcess to be managed for allocation and preemption + * ordering + */ + public void addSchedulableProcess(S s) { + schedulableProcesses.add(s); + } + + /** + * Remove a SchedulableProcess from management for allocation and preemption + * ordering + */ + public boolean removeSchedulableProcess(S s) { + return schedulableProcesses.remove(s); + } + + /** + * Add a collection of SchedulableProcesses to be managed for allocation + * and preemption ordering + */ + public void addAllSchedulableProcesses(Collection sc) { + schedulableProcesses.addAll(sc); + } + + /** + * Get the number of SchedulableProcesses managed for allocation and + * preemption ordering + */ + public int getNumSchedulableProcesses() { + return schedulableProcesses.size(); + } + +} diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/MockSchedulableProcess.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/MockSchedulableProcess.java new file mode 100644 index 0000000..e4d1c25 --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/MockSchedulableProcess.java @@ -0,0 +1,98 @@ +/** + * 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.policy; + +import java.util.*; + +import org.apache.hadoop.yarn.api.records.Priority; +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.util.resource.Resources; + + +public class MockSchedulableProcess implements SchedulableProcess { + + private long serialEpoch = 0; + private long serial = 0; + private String id = ""; + private Resource consumption = Resources.createResource(0); + private Resource demand = Resources.createResource(0); + private Resource cachedConsumption = Resources.createResource(0); + private Resource cachedDemand = Resources.createResource(0); + + public MockSchedulableProcess() { } + + public void setSerialEpoch(long serialEpoch) { + this.serialEpoch = serialEpoch; + } + + private long getSerialEpoch() { + return serialEpoch; + } + + public void setSerial(long serial) { + this.serial = serial; + } + + private long getSerial() { + return serial; + } + + public void setId(String id) { + this.id = id; + } + + public String getId() { + return id; + } + + public void setConsumption(Resource consumption) { + this.consumption = consumption; + } + + public Resource getSchedulingConsumption() { + return cachedConsumption; + } + + public void setDemand(Resource demand) { + this.demand = demand; + } + + public Resource getSchedulingDemand() { + return cachedDemand; + } + + public void updateSchedulingState() { + cachedConsumption = consumption; + cachedDemand = demand; + } + + @Override + public int compareInputOrderTo(SchedulableProcess other) { + if (other instanceof MockSchedulableProcess) { + MockSchedulableProcess r2 = (MockSchedulableProcess) other; + int res = (int) Math.signum(getSerialEpoch() - r2.getSerialEpoch()); + if (res == 0) { + res = (int) Math.signum(getSerial() - r2.getSerial()); + } + return res; + } + return 1;//let other types go before this, if any + } + +} diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/TestFifoOrderingPolicy.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/TestFifoOrderingPolicy.java new file mode 100644 index 0000000..0d9d343 --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/policy/TestFifoOrderingPolicy.java @@ -0,0 +1,88 @@ +/** + * 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.policy; + +import java.util.*; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.hadoop.yarn.api.records.Priority; +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.util.resource.Resources; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; + +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.common.fica.FiCaSchedulerApp; + +public class TestFifoOrderingPolicy { + + @Test + public void testFifoOrderingPolicy() { + FifoOrderingPolicy policy = new FifoOrderingPolicy(); + MockSchedulableProcess r1 = new MockSchedulableProcess(); + MockSchedulableProcess r2 = new MockSchedulableProcess(); + + Assert.assertEquals(policy.getSchedulingComparator().compare(r1, r2), 0); + + //serial + r1.setSerial(1); + Assert.assertEquals(policy.getSchedulingComparator().compare(r1, r2), 1); + + //serial epoch + r2.setSerialEpoch(1); + Assert.assertEquals(policy.getSchedulingComparator().compare(r1, r2), -1); + } + + @Test + public void testIterators() { + SchedulingOrder schedOrder = + new SchedulingOrder(new FifoOrderingPolicy()); + + MockSchedulableProcess msp1 = new MockSchedulableProcess(); + MockSchedulableProcess msp2 = new MockSchedulableProcess(); + MockSchedulableProcess msp3 = new MockSchedulableProcess(); + + msp1.setSerial(3); + msp2.setSerial(2); + msp3.setSerial(1); + + msp1.setId("3"); + msp2.setId("2"); + msp3.setId("1"); + + schedOrder.addSchedulableProcess(msp1); + schedOrder.addSchedulableProcess(msp2); + schedOrder.addSchedulableProcess(msp3); + + //Assignment, oldest to youngest + checkIds(schedOrder.getAssignmentIterator(), new String[]{"1", "2", "3"}); + + //Preemption, youngest to oldest + checkIds(schedOrder.getPreemptionIterator(), new String[]{"3", "2", "1"}); + } + + public void checkIds(Iterator si, + String[] ids) { + for (int i = 0;i < ids.length;i++) { + Assert.assertEquals(si.next().getId(), + ids[i]); + } + } + +}