1 | package example.localplugin; |
---|
2 | |
---|
3 | import gridsim.dcworms.DCWormsTags; |
---|
4 | |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.HashMap; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Map; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import schedframe.events.scheduling.SchedulingEvent; |
---|
12 | import schedframe.resources.ResourceStatus; |
---|
13 | import schedframe.resources.StandardResourceType; |
---|
14 | import schedframe.resources.computing.ComputingNode; |
---|
15 | import schedframe.resources.computing.ComputingResource; |
---|
16 | import schedframe.resources.computing.Processor; |
---|
17 | import schedframe.resources.units.Memory; |
---|
18 | import schedframe.resources.units.ProcessingElements; |
---|
19 | import schedframe.resources.units.ResourceUnit; |
---|
20 | import schedframe.resources.units.ResourceUnitName; |
---|
21 | import schedframe.resources.units.StandardResourceUnitName; |
---|
22 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
23 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
24 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
25 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
26 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
27 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
28 | import schedframe.scheduling.queue.TaskQueue; |
---|
29 | import schedframe.scheduling.queue.TaskQueueList; |
---|
30 | import schedframe.scheduling.tasks.TaskInterface; |
---|
31 | import schedframe.scheduling.tasks.WorkloadUnit; |
---|
32 | |
---|
33 | public class FCFSClusterLocalPlugin extends BaseLocalSchedulingPlugin { |
---|
34 | |
---|
35 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
36 | ResourceManager resManager, ModuleList modules) { |
---|
37 | |
---|
38 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
39 | SchedulingPlan plan = new SchedulingPlan(); |
---|
40 | // chose the events types to serve. |
---|
41 | // Different actions for different events are possible. |
---|
42 | switch (event.getType()) { |
---|
43 | case START_TASK_EXECUTION: |
---|
44 | case TASK_FINISHED: |
---|
45 | //case TIMER: |
---|
46 | // our tasks are placed only in first queue (see BaseLocalPlugin.placeJobsInQueues() method) |
---|
47 | TaskQueue q = queues.get(0); |
---|
48 | // check all tasks in queue |
---|
49 | |
---|
50 | for (int i = 0; i < q.size(); i++) { |
---|
51 | WorkloadUnit job = q.get(i); |
---|
52 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
53 | // if status of the tasks in READY |
---|
54 | if (task.getStatus() == DCWormsTags.READY) { |
---|
55 | |
---|
56 | /****************3 ways to schedule task****************/ |
---|
57 | |
---|
58 | /****************1. Choosing particular resources to perform execution****************/ |
---|
59 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution2(resourceManager, task); |
---|
60 | if (choosenResources != null) { |
---|
61 | addToSchedulingPlan(plan, task, choosenResources); |
---|
62 | } |
---|
63 | |
---|
64 | /****************2. Choosing resource scheduler/provider to submit task. If the given resource doesn't contains/isn't |
---|
65 | a scheduler, random resources from the given resource will be chosen in order to perform execution****************/ |
---|
66 | /*String provName = chooseProviderForExecution(resourceManager); |
---|
67 | if (provName != null) { |
---|
68 | addToSchedulingPlan(plan, task, provName); |
---|
69 | }*/ |
---|
70 | |
---|
71 | /****************3. Scheduler will choose random resources to perform execution****************/ |
---|
72 | //addToSchedulingPlan(plan, task); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | break; |
---|
77 | } |
---|
78 | return plan; |
---|
79 | } |
---|
80 | |
---|
81 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
82 | ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
83 | |
---|
84 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
85 | |
---|
86 | int cpuRequest; |
---|
87 | try { |
---|
88 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
89 | } catch (NoSuchFieldException e) { |
---|
90 | cpuRequest = 1; |
---|
91 | } |
---|
92 | |
---|
93 | if (cpuRequest != 0) { |
---|
94 | List<ComputingResource> choosenResources = null; |
---|
95 | List<Processor> processors = null; |
---|
96 | processors = resourceManager.getProcessors(); |
---|
97 | if (processors.size() < cpuRequest) { |
---|
98 | // log.warn("Task requires more cpus than is availiable in this resource."); |
---|
99 | return null; |
---|
100 | } |
---|
101 | |
---|
102 | choosenResources = new ArrayList<ComputingResource>(); |
---|
103 | |
---|
104 | for (int i = 0; i < processors.size() && cpuRequest > 0; i++) { |
---|
105 | if (processors.get(i).getStatus() == ResourceStatus.FREE) { |
---|
106 | choosenResources.add(processors.get(i)); |
---|
107 | cpuRequest--; |
---|
108 | } |
---|
109 | } |
---|
110 | if (cpuRequest > 0) { |
---|
111 | // log.info("Task " + task.getJobId() + "_" + task.getId() + |
---|
112 | // " requires more cpus than is availiable in this moment."); |
---|
113 | return null; |
---|
114 | } |
---|
115 | |
---|
116 | ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getName()); |
---|
117 | result.addAll(choosenResources); |
---|
118 | map.put(StandardResourceUnitName.PE, result); |
---|
119 | |
---|
120 | } |
---|
121 | int memoryRequest; |
---|
122 | try { |
---|
123 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
124 | } catch (NoSuchFieldException e) { |
---|
125 | memoryRequest = 0; |
---|
126 | } |
---|
127 | if (memoryRequest != 0) { |
---|
128 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
129 | |
---|
130 | Memory memory = null; |
---|
131 | for (ComputingNode node : nodes) { |
---|
132 | try{ |
---|
133 | if (node.getFreeMemory() >= memoryRequest) { |
---|
134 | memory = new Memory(node.getMemory(), memoryRequest, memoryRequest); |
---|
135 | } |
---|
136 | } catch(NoSuchFieldException e){ |
---|
137 | memory = null; |
---|
138 | } |
---|
139 | } |
---|
140 | if(memory != null) |
---|
141 | map.put(StandardResourceUnitName.MEMORY, memory); |
---|
142 | else return null; |
---|
143 | } |
---|
144 | |
---|
145 | return map; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution2( |
---|
150 | ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
151 | |
---|
152 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
153 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
154 | for (ComputingNode node : nodes) { |
---|
155 | int cpuRequest; |
---|
156 | try { |
---|
157 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
158 | } catch (NoSuchFieldException e) { |
---|
159 | cpuRequest = 1; |
---|
160 | } |
---|
161 | |
---|
162 | if (cpuRequest != 0) { |
---|
163 | |
---|
164 | List<Processor> processors = node.getProcessors(); |
---|
165 | if (processors.size() < cpuRequest) { |
---|
166 | continue; |
---|
167 | } |
---|
168 | |
---|
169 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
170 | for (int i = 0; i < processors.size() && cpuRequest > 0; i++) { |
---|
171 | if (processors.get(i).getStatus() == ResourceStatus.FREE) { |
---|
172 | choosenResources.add(processors.get(i)); |
---|
173 | cpuRequest--; |
---|
174 | } |
---|
175 | } |
---|
176 | if (cpuRequest > 0) { |
---|
177 | continue; |
---|
178 | } |
---|
179 | |
---|
180 | ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getName()); |
---|
181 | result.addAll(choosenResources); |
---|
182 | map.put(StandardResourceUnitName.PE, result); |
---|
183 | |
---|
184 | int memoryRequest; |
---|
185 | try { |
---|
186 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
187 | } catch (NoSuchFieldException e) { |
---|
188 | memoryRequest = 0; |
---|
189 | } |
---|
190 | if (memoryRequest != 0) { |
---|
191 | |
---|
192 | Memory memory = null; |
---|
193 | try{ |
---|
194 | if (node.getFreeMemory() >= memoryRequest) { |
---|
195 | memory = new Memory(node.getMemory(), memoryRequest, memoryRequest); |
---|
196 | } |
---|
197 | } catch(NoSuchFieldException e){ |
---|
198 | continue; |
---|
199 | } |
---|
200 | if(memory == null) |
---|
201 | continue; |
---|
202 | else { |
---|
203 | map.put(StandardResourceUnitName.MEMORY, memory); |
---|
204 | return map; |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | return null; |
---|
211 | } |
---|
212 | |
---|
213 | @SuppressWarnings("unchecked") |
---|
214 | private String chooseProviderForExecution(ResourceManager unitsManager) { |
---|
215 | List<ComputingResource> processingElements; |
---|
216 | Properties properties = new Properties(); |
---|
217 | properties.setProperty("type", StandardResourceType.ComputingNode.toString()); |
---|
218 | // properties.setProperty("status", ResourceStatus.FREE.toString()); |
---|
219 | processingElements = (List<ComputingResource>) unitsManager.filterResources(properties); |
---|
220 | return processingElements.get(0).getName(); |
---|
221 | } |
---|
222 | |
---|
223 | public String getName() { |
---|
224 | return getClass().getName(); |
---|
225 | } |
---|
226 | |
---|
227 | } |
---|