[658] | 1 | package test.article.recs.plugins.scheduling; |
---|
| 2 | |
---|
[679] | 3 | import gridsim.dcworms.DCWormsTags; |
---|
| 4 | |
---|
[708] | 5 | import java.io.FileNotFoundException; |
---|
| 6 | import java.io.IOException; |
---|
[658] | 7 | import java.util.ArrayList; |
---|
| 8 | import java.util.HashMap; |
---|
| 9 | import java.util.List; |
---|
| 10 | import java.util.Map; |
---|
[708] | 11 | import java.util.MissingResourceException; |
---|
[658] | 12 | import java.util.Random; |
---|
| 13 | |
---|
| 14 | import schedframe.events.scheduling.SchedulingEvent; |
---|
| 15 | import schedframe.resources.ResourceStatus; |
---|
| 16 | import schedframe.resources.computing.ComputingNode; |
---|
| 17 | import schedframe.resources.computing.ComputingResource; |
---|
| 18 | import schedframe.resources.computing.Core; |
---|
| 19 | import schedframe.resources.units.ProcessingElements; |
---|
| 20 | import schedframe.resources.units.ResourceUnit; |
---|
| 21 | import schedframe.resources.units.ResourceUnitName; |
---|
| 22 | import schedframe.resources.units.StandardResourceUnitName; |
---|
| 23 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
| 24 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 25 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
| 26 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 27 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
| 28 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 29 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 30 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 31 | import schedframe.scheduling.tasks.TaskInterface; |
---|
| 32 | |
---|
[679] | 33 | public class RecsExclusivenessRandomSP extends RecsSP { |
---|
[658] | 34 | |
---|
| 35 | private Random rand = new Random(5); |
---|
| 36 | |
---|
| 37 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
| 38 | ResourceManager resManager, ModuleList modules) { |
---|
| 39 | |
---|
| 40 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 41 | SchedulingPlan plan = new SchedulingPlan(); |
---|
| 42 | // choose the events types to serve. |
---|
| 43 | // Different actions for different events are possible. |
---|
| 44 | switch (event.getType()) { |
---|
| 45 | case START_TASK_EXECUTION: |
---|
| 46 | case TASK_FINISHED: |
---|
| 47 | //case TIMER: |
---|
| 48 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
| 49 | TaskQueue q = queues.get(0); |
---|
| 50 | // check all tasks in queue |
---|
| 51 | |
---|
| 52 | for (int i = 0; i < q.size(); i++) { |
---|
| 53 | TaskInterface<?> task = q.get(i); |
---|
[679] | 54 | initApplicationType(task); |
---|
| 55 | |
---|
[658] | 56 | // if status of the tasks in READY |
---|
| 57 | if (task.getStatus() == DCWormsTags.READY) { |
---|
| 58 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
| 59 | if (choosenResources != null) { |
---|
| 60 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | break; |
---|
| 66 | } |
---|
| 67 | return plan; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
| 71 | ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 72 | |
---|
| 73 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
| 74 | |
---|
| 75 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 76 | List<ComputingNode> avNodes = filterNodes(nodes, task); |
---|
| 77 | if(avNodes.size() == 0) |
---|
| 78 | return null; |
---|
| 79 | ComputingNode node = randomNode(avNodes); |
---|
| 80 | |
---|
| 81 | int cpuRequest; |
---|
| 82 | try { |
---|
| 83 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 84 | } catch (NoSuchFieldException e) { |
---|
| 85 | cpuRequest = 0; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if (cpuRequest != 0) { |
---|
| 89 | |
---|
| 90 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
| 91 | |
---|
| 92 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
[734] | 93 | for (int i = 0; i < cores.size(); i++) { |
---|
[658] | 94 | if (cores.get(i).getStatus() == ResourceStatus.FREE) { |
---|
[734] | 95 | //choosenResources.add(cores.get(i)); |
---|
[658] | 96 | cpuRequest--; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | if (cpuRequest > 0) { |
---|
[734] | 100 | //return null; |
---|
[658] | 101 | } |
---|
[734] | 102 | choosenResources.add(node); |
---|
[658] | 103 | ProcessingElements pe = new ProcessingElements(); |
---|
| 104 | pe.addAll(choosenResources); |
---|
| 105 | map.put(StandardResourceUnitName.PE, pe); |
---|
| 106 | return map; |
---|
| 107 | } |
---|
| 108 | return null; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){ |
---|
| 112 | List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>(); |
---|
| 113 | for (ComputingNode node : nodes) { |
---|
| 114 | int cpuRequest; |
---|
| 115 | try { |
---|
| 116 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 117 | } catch (NoSuchFieldException e) { |
---|
| 118 | cpuRequest = 0; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | if (cpuRequest != 0) { |
---|
| 122 | |
---|
| 123 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
| 124 | if (cores.size() < cpuRequest) { |
---|
| 125 | continue; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | int freeCores = 0; |
---|
| 129 | for(Core core: cores){ |
---|
| 130 | if(core.getStatus() == ResourceStatus.FREE) |
---|
| 131 | freeCores++; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if(freeCores != cores.size()) |
---|
| 135 | continue; |
---|
| 136 | |
---|
[708] | 137 | try { |
---|
| 138 | if(!getExecutiveness(createExecutivenessQuery(task, node))) |
---|
| 139 | continue; |
---|
| 140 | } catch (FileNotFoundException e) { |
---|
| 141 | continue; |
---|
| 142 | } catch (IOException e) { |
---|
| 143 | continue; |
---|
| 144 | } catch (MissingResourceException e){ |
---|
| 145 | continue; |
---|
| 146 | } |
---|
[658] | 147 | filteredNodes.add(node); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | return filteredNodes; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | private ComputingNode randomNode(List<ComputingNode> nodes){ |
---|
| 155 | return nodes.get(rand.nextInt(nodes.size())); |
---|
| 156 | } |
---|
| 157 | } |
---|