[104] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | import gridsim.gssim.SubmittedTask; |
---|
| 5 | import gssim.schedframe.scheduling.ExecTaskInterface; |
---|
| 6 | |
---|
| 7 | import java.util.ArrayList; |
---|
| 8 | import java.util.HashMap; |
---|
| 9 | import java.util.List; |
---|
| 10 | import java.util.Map; |
---|
| 11 | import java.util.Properties; |
---|
| 12 | |
---|
| 13 | import schedframe.resources.units.Memory; |
---|
| 14 | import schedframe.resources.units.ResourceUnit; |
---|
| 15 | import schedframe.scheduling.TaskInterface; |
---|
| 16 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 17 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 18 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 19 | import test.rewolucja.GSSIMJobInterface; |
---|
| 20 | import test.rewolucja.resources.ProcessingElements; |
---|
| 21 | import test.rewolucja.resources.ResourceStatus; |
---|
| 22 | import test.rewolucja.resources.ResourceType; |
---|
| 23 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
| 24 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 25 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
| 26 | import test.rewolucja.resources.physical.implementation.CPU; |
---|
| 27 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
| 28 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 29 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 30 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
[163] | 31 | import test.rewolucja.scheduling.queue.Queue; |
---|
[104] | 32 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 33 | |
---|
| 34 | public class FCFSClusterLocalPlugin extends BaseLocalPlugin { |
---|
| 35 | |
---|
| 36 | public FCFSClusterLocalPlugin() { |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 40 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
| 41 | |
---|
| 42 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 43 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
| 44 | // chose the events types to serve. |
---|
| 45 | // Different actions for different events are possible. |
---|
| 46 | switch (event.getType()) { |
---|
| 47 | case START_TASK_EXECUTION: |
---|
| 48 | case TASK_FINISHED: |
---|
| 49 | case TIMER: |
---|
| 50 | // our tasks are placed only in first queue (see |
---|
| 51 | // BaseLocalPlugin.placeTasksInQueues() method) |
---|
[163] | 52 | Queue q = queues.get(0); |
---|
[104] | 53 | // check all tasks in queue |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | for (int i = 0; i < q.size(); i++) { |
---|
| 57 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 58 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 59 | // if status of the tasks in READY |
---|
| 60 | if (task.getStatus() == Gridlet.READY) { |
---|
| 61 | |
---|
| 62 | SubmittedTask subTask = (SubmittedTask) task; |
---|
[129] | 63 | |
---|
| 64 | /****************3 ways to schedule task****************/ |
---|
| 65 | |
---|
| 66 | //1. Choosing particular resources to perform execution |
---|
| 67 | Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, subTask); |
---|
[104] | 68 | if (choosenResources != null) { |
---|
| 69 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 70 | } |
---|
[129] | 71 | |
---|
| 72 | //2. Choosing resource scheduler/provider to submit task. If the given resource doesn't contains/isn't |
---|
| 73 | //a scheduler, random resources from the given resource will be chosen in order to perform execution |
---|
| 74 | /*String provName = chooseProviderForExecution(resourceManager); |
---|
| 75 | if (provName != null) { |
---|
| 76 | addToSchedulingPlan(plan, task, provName); |
---|
| 77 | }*/ |
---|
[104] | 78 | |
---|
[129] | 79 | //3. Scheduler will choose random resources to perform execution |
---|
| 80 | //addToSchedulingPlan(plan, task); |
---|
| 81 | |
---|
[104] | 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | break; |
---|
| 86 | } |
---|
| 87 | return plan; |
---|
| 88 | } |
---|
| 89 | |
---|
[129] | 90 | private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution( |
---|
[104] | 91 | ClusterResourceManager resourceManager, ExecTaskInterface task) { |
---|
| 92 | |
---|
| 93 | HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>(); |
---|
| 94 | |
---|
| 95 | int cpuRequest; |
---|
| 96 | try { |
---|
| 97 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 98 | } catch (NoSuchFieldException e) { |
---|
| 99 | cpuRequest = 1; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if (cpuRequest != 0) { |
---|
| 103 | List<ComputingResource> choosenResources = null; |
---|
| 104 | List<CPU> processors = null; |
---|
| 105 | processors = resourceManager.getProcessors(); |
---|
| 106 | if (processors.size() < cpuRequest) { |
---|
| 107 | // log.warn("Task requires more cpus than is availiable in this resource."); |
---|
| 108 | return null; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | choosenResources = new ArrayList<ComputingResource>(); |
---|
| 112 | |
---|
| 113 | for (int i = 0; i < processors.size() && cpuRequest > 0; i++) { |
---|
| 114 | if (processors.get(i).getStatus() == ResourceStatus.FREE) { |
---|
| 115 | choosenResources.add(processors.get(i)); |
---|
| 116 | cpuRequest--; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | if (cpuRequest > 0) { |
---|
| 120 | // log.info("Task " + task.getJobId() + "_" + task.getId() + |
---|
| 121 | // " requires more cpus than is availiable in this moment."); |
---|
| 122 | return null; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getName()); |
---|
| 126 | result.addAll(choosenResources); |
---|
| 127 | map.put(ResourceParameterName.PROCESSINGELEMENTS, result); |
---|
| 128 | |
---|
| 129 | } |
---|
| 130 | int memoryRequest; |
---|
| 131 | try { |
---|
| 132 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
| 133 | } catch (NoSuchFieldException e) { |
---|
| 134 | memoryRequest = 0; |
---|
| 135 | } |
---|
| 136 | if (memoryRequest != 0) { |
---|
| 137 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 138 | |
---|
| 139 | Memory memory = null; |
---|
| 140 | for (ComputingNode node : nodes) { |
---|
| 141 | |
---|
| 142 | if (node.getFreeMemory() >= memoryRequest) { |
---|
| 143 | memory = new Memory(node.getMemory(), memoryRequest, memoryRequest); |
---|
| 144 | } else |
---|
| 145 | return null; |
---|
| 146 | } |
---|
| 147 | map.put(ResourceParameterName.MEMORY, memory); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | return map; |
---|
| 151 | } |
---|
| 152 | |
---|
[129] | 153 | private String chooseProviderForExecution(ResourceManagerInterface unitsManager) { |
---|
| 154 | List<ComputingResource> processingElements; |
---|
| 155 | Properties properties = new Properties(); |
---|
| 156 | properties.setProperty("type", ResourceType.COMPUTING_NODE.toString()); |
---|
| 157 | // properties.setProperty("status", ResourceStatus.FREE.toString()); |
---|
| 158 | processingElements = (List<ComputingResource>) unitsManager.filterResources(properties); |
---|
| 159 | return processingElements.get(0).getName(); |
---|
| 160 | } |
---|
| 161 | |
---|
[104] | 162 | public String getName() { |
---|
| 163 | return getClass().getName(); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | public void init(Properties properties) { |
---|
| 167 | // no extra initialization is expected. |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | } |
---|