1 | package schedframe.scheduling.policy.local; |
---|
2 | |
---|
3 | import dcworms.schedframe.scheduling.ExecTask; |
---|
4 | import dcworms.schedframe.scheduling.Executable; |
---|
5 | import eduni.simjava.Sim_event; |
---|
6 | import eduni.simjava.Sim_system; |
---|
7 | import gridsim.Accumulator; |
---|
8 | import gridsim.ResourceCalendar; |
---|
9 | import gridsim.dcworms.DCWormsTags; |
---|
10 | import gridsim.dcworms.filter.ExecTaskFilter; |
---|
11 | |
---|
12 | import java.util.ArrayList; |
---|
13 | import java.util.HashMap; |
---|
14 | import java.util.Iterator; |
---|
15 | import java.util.List; |
---|
16 | import java.util.Map; |
---|
17 | |
---|
18 | import org.apache.commons.lang.ArrayUtils; |
---|
19 | import org.apache.commons.logging.Log; |
---|
20 | import org.apache.commons.logging.LogFactory; |
---|
21 | import org.joda.time.DateTime; |
---|
22 | import org.joda.time.DateTimeUtilsExt; |
---|
23 | import org.qcg.broker.schemas.schedulingplan.types.AllocationStatus; |
---|
24 | |
---|
25 | import qcg.shared.constants.BrokerConstants; |
---|
26 | import schedframe.ResourceController; |
---|
27 | import schedframe.events.scheduling.SchedulingEvent; |
---|
28 | import schedframe.events.scheduling.SchedulingEventType; |
---|
29 | import schedframe.events.scheduling.StartTaskExecutionEvent; |
---|
30 | import schedframe.events.scheduling.TaskFinishedEvent; |
---|
31 | import schedframe.events.scheduling.TaskRequestedTimeExpiredEvent; |
---|
32 | import schedframe.exceptions.ResourceException; |
---|
33 | import schedframe.resources.computing.ComputingResource; |
---|
34 | import schedframe.resources.computing.profiles.energy.EnergyEvent; |
---|
35 | import schedframe.resources.computing.profiles.energy.EnergyEventType; |
---|
36 | import schedframe.resources.units.PEUnit; |
---|
37 | import schedframe.resources.units.ProcessingElements; |
---|
38 | import schedframe.resources.units.ResourceUnit; |
---|
39 | import schedframe.resources.units.ResourceUnitName; |
---|
40 | import schedframe.resources.units.StandardResourceUnitName; |
---|
41 | import schedframe.scheduling.ResourceHistoryItem; |
---|
42 | import schedframe.scheduling.Scheduler; |
---|
43 | import schedframe.scheduling.TaskList; |
---|
44 | import schedframe.scheduling.TaskListImpl; |
---|
45 | import schedframe.scheduling.UsedResourcesList; |
---|
46 | import schedframe.scheduling.WorkloadUnitHandler; |
---|
47 | import schedframe.scheduling.manager.resources.LocalResourceManager; |
---|
48 | import schedframe.scheduling.manager.resources.ManagedResources; |
---|
49 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
50 | import schedframe.scheduling.plan.AllocationInterface; |
---|
51 | import schedframe.scheduling.plan.ScheduledTaskInterface; |
---|
52 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
53 | import schedframe.scheduling.plugin.SchedulingPlugin; |
---|
54 | import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin; |
---|
55 | import schedframe.scheduling.plugin.grid.ModuleListImpl; |
---|
56 | import schedframe.scheduling.plugin.grid.ModuleType; |
---|
57 | import schedframe.scheduling.policy.AbstractManagementSystem; |
---|
58 | import schedframe.scheduling.queue.TaskQueueList; |
---|
59 | import schedframe.scheduling.tasks.AbstractProcesses; |
---|
60 | import schedframe.scheduling.tasks.Job; |
---|
61 | import schedframe.scheduling.tasks.JobInterface; |
---|
62 | import schedframe.scheduling.tasks.Task; |
---|
63 | import schedframe.scheduling.tasks.TaskInterface; |
---|
64 | import schedframe.scheduling.tasks.WorkloadUnit; |
---|
65 | import simulator.DCWormsConstants; |
---|
66 | import simulator.utils.DoubleMath; |
---|
67 | |
---|
68 | public class LocalManagementSystem extends AbstractManagementSystem { |
---|
69 | |
---|
70 | private Log log = LogFactory.getLog(LocalManagementSystem.class); |
---|
71 | |
---|
72 | protected double lastUpdateTime; |
---|
73 | |
---|
74 | protected Accumulator accTotalLoad; |
---|
75 | |
---|
76 | public LocalManagementSystem(String providerId, String entityName, SchedulingPlugin schedPlugin, |
---|
77 | ExecutionTimeEstimationPlugin execTimeEstimationPlugin, TaskQueueList queues) |
---|
78 | throws Exception { |
---|
79 | |
---|
80 | super(providerId, entityName, execTimeEstimationPlugin, queues); |
---|
81 | |
---|
82 | if (schedPlugin == null) { |
---|
83 | throw new Exception("Can not create local scheduling plugin instance."); |
---|
84 | } |
---|
85 | this.schedulingPlugin = schedPlugin; |
---|
86 | this.moduleList = new ModuleListImpl(1); |
---|
87 | |
---|
88 | this.accTotalLoad = new Accumulator(); |
---|
89 | } |
---|
90 | |
---|
91 | public void init(Scheduler sched, ManagedResources managedResources) { |
---|
92 | super.init(sched, managedResources); |
---|
93 | double load = 0; |
---|
94 | accTotalLoad.add(load); |
---|
95 | } |
---|
96 | |
---|
97 | public void processEvent(Sim_event ev) { |
---|
98 | |
---|
99 | updateProcessingProgress(); |
---|
100 | |
---|
101 | int tag = ev.get_tag(); |
---|
102 | Object obj; |
---|
103 | |
---|
104 | switch (tag) { |
---|
105 | |
---|
106 | case DCWormsTags.TIMER: |
---|
107 | if (pluginSupportsEvent(tag)) { |
---|
108 | SchedulingEvent event = new SchedulingEvent(SchedulingEventType.TIMER); |
---|
109 | SchedulingPlanInterface<?> decision = schedulingPlugin.schedule(event, |
---|
110 | queues, getJobRegistry(), getResourceManager(), moduleList); |
---|
111 | executeSchedulingPlan(decision); |
---|
112 | } |
---|
113 | sendTimerEvent(); |
---|
114 | break; |
---|
115 | |
---|
116 | case DCWormsTags.TASK_READY_FOR_EXECUTION: |
---|
117 | |
---|
118 | ExecTask execTask = (ExecTask) ev.get_data(); |
---|
119 | try { |
---|
120 | execTask.setStatus(DCWormsTags.READY); |
---|
121 | if (pluginSupportsEvent(tag)) { |
---|
122 | SchedulingEvent event = new StartTaskExecutionEvent(execTask.getJobId(), execTask.getId()); |
---|
123 | SchedulingPlanInterface<?> decision = schedulingPlugin.schedule(event, |
---|
124 | queues, getJobRegistry(), getResourceManager(), moduleList); |
---|
125 | executeSchedulingPlan(decision); |
---|
126 | } |
---|
127 | } catch (Exception e) { |
---|
128 | e.printStackTrace(); |
---|
129 | } |
---|
130 | break; |
---|
131 | |
---|
132 | case DCWormsTags.TASK_EXECUTION_FINISHED: |
---|
133 | obj = ev.get_data(); |
---|
134 | execTask = (ExecTask) obj; |
---|
135 | if (execTask.getStatus() == DCWormsTags.INEXEC) { |
---|
136 | |
---|
137 | finalizeExecutable(execTask); |
---|
138 | sendFinishedWorkloadUnit(execTask); |
---|
139 | log.debug(execTask.getJobId() + "_" + execTask.getId() + " finished execution on " + new DateTime()); |
---|
140 | log.info(DCWormsConstants.USAGE_MEASURE_NAME + ": " + calculateTotalLoad(jobRegistry.getRunningTasks().size())); |
---|
141 | if (pluginSupportsEvent(tag)) { |
---|
142 | SchedulingEvent event = new TaskFinishedEvent(execTask.getJobId(), execTask.getId()); |
---|
143 | SchedulingPlanInterface<?> decision = schedulingPlugin.schedule(event, |
---|
144 | queues, getJobRegistry(), getResourceManager(), moduleList); |
---|
145 | executeSchedulingPlan(decision); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | Job job = jobRegistry.getJob(execTask.getJobId()); |
---|
150 | if(!job.isFinished()){ |
---|
151 | getWorkloadUnitHandler().handleJob(job); |
---|
152 | } |
---|
153 | break; |
---|
154 | |
---|
155 | case DCWormsTags.TASK_REQUESTED_TIME_EXPIRED: |
---|
156 | obj = ev.get_data(); |
---|
157 | execTask = (Executable) obj; |
---|
158 | if (pluginSupportsEvent(tag)) { |
---|
159 | SchedulingEvent event = new TaskRequestedTimeExpiredEvent(execTask.getJobId(), execTask.getId()); |
---|
160 | SchedulingPlanInterface<?> decision = schedulingPlugin.schedule(event, |
---|
161 | queues, getJobRegistry(), getResourceManager(), moduleList); |
---|
162 | executeSchedulingPlan(decision); |
---|
163 | } |
---|
164 | break; |
---|
165 | |
---|
166 | case DCWormsTags.UPDATE: |
---|
167 | updateProcessingTimes(ev); |
---|
168 | break; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | public void notifyReturnedWorkloadUnit(WorkloadUnit wu) { |
---|
173 | if (pluginSupportsEvent(DCWormsTags.TASK_EXECUTION_FINISHED)) { |
---|
174 | SchedulingEvent event = new SchedulingEvent(SchedulingEventType.TASK_FINISHED); |
---|
175 | SchedulingPlanInterface<?> decision = schedulingPlugin.schedule(event, |
---|
176 | queues, getJobRegistry(), getResourceManager(), moduleList); |
---|
177 | executeSchedulingPlan(decision); |
---|
178 | } |
---|
179 | //if(scheduler.getParent() != null){ |
---|
180 | sendFinishedWorkloadUnit(wu); |
---|
181 | //} |
---|
182 | } |
---|
183 | |
---|
184 | protected void executeSchedulingPlan(SchedulingPlanInterface<?> decision) { |
---|
185 | |
---|
186 | ArrayList<ScheduledTaskInterface<?>> taskSchedulingDecisions = decision.getTasks(); |
---|
187 | for (int i = 0; i < taskSchedulingDecisions.size(); i++) { |
---|
188 | ScheduledTaskInterface<?> taskDecision = taskSchedulingDecisions.get(i); |
---|
189 | |
---|
190 | if (taskDecision.getStatus() == AllocationStatus.REJECTED) { |
---|
191 | continue; |
---|
192 | } |
---|
193 | |
---|
194 | ArrayList<AllocationInterface<?>> allocations = taskDecision.getAllocations(); |
---|
195 | |
---|
196 | TaskInterface<?> task = taskDecision.getTask(); |
---|
197 | for (int j = 0; j < allocations.size(); j++) { |
---|
198 | |
---|
199 | AllocationInterface<?> allocation = allocations.get(j); |
---|
200 | if (allocation.isProcessing()) { |
---|
201 | ExecTask exec = (ExecTask) task; |
---|
202 | executeTask(exec, allocation.getRequestedResources()); |
---|
203 | } else if(resourceManager.getSchedulerName(allocation.getProviderName()) != null){ |
---|
204 | allocation.setProviderName(resourceManager.getSchedulerName(allocation.getProviderName())); |
---|
205 | submitTask(task, allocation); |
---|
206 | } else { |
---|
207 | ExecTask exec = (ExecTask) task; |
---|
208 | executeTask(exec, chooseResourcesForExecution(allocation.getProviderName(), exec)); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | protected void executeTask(ExecTask task, Map<ResourceUnitName, ResourceUnit> choosenResources) { |
---|
216 | |
---|
217 | Executable exec = (Executable)task; |
---|
218 | boolean allocationStatus = getAllocationManager().allocateResources(choosenResources); |
---|
219 | if(allocationStatus == false) |
---|
220 | return; |
---|
221 | removeFromQueue(task); |
---|
222 | |
---|
223 | SchedulingEvent event = new SchedulingEvent(SchedulingEventType.START_TASK_EXECUTION); |
---|
224 | int time = Double.valueOf( |
---|
225 | execTimeEstimationPlugin.execTimeEstimation(event, task, choosenResources, exec.getCompletionPercentage())).intValue(); |
---|
226 | log.debug(task.getJobId() + "_" + task.getId() + " starts executing on " + new DateTime() |
---|
227 | + " will finish after " + time); |
---|
228 | |
---|
229 | if (time < 0.0) |
---|
230 | return; |
---|
231 | |
---|
232 | exec.setEstimatedDuration(time); |
---|
233 | DateTime currentTime = new DateTime(); |
---|
234 | ResourceHistoryItem resHistItem = new ResourceHistoryItem(choosenResources, currentTime); |
---|
235 | exec.addUsedResources(resHistItem); |
---|
236 | try { |
---|
237 | exec.setStatus(DCWormsTags.INEXEC); |
---|
238 | } catch (Exception e) { |
---|
239 | // TODO Auto-generated catch block |
---|
240 | e.printStackTrace(); |
---|
241 | } |
---|
242 | scheduler.sendInternal(time, DCWormsTags.TASK_EXECUTION_FINISHED, exec); |
---|
243 | |
---|
244 | try { |
---|
245 | long expectedDuration = exec.getExpectedDuration().getMillis() / 1000; |
---|
246 | scheduler.sendInternal(expectedDuration, DCWormsTags.TASK_REQUESTED_TIME_EXPIRED, exec); |
---|
247 | } catch (NoSuchFieldException e) { |
---|
248 | double t = exec.getEstimatedDuration(); |
---|
249 | scheduler.sendInternal(t, DCWormsTags.TASK_REQUESTED_TIME_EXPIRED, exec); |
---|
250 | } |
---|
251 | |
---|
252 | log.info(DCWormsConstants.USAGE_MEASURE_NAME + ": " + calculateTotalLoad(jobRegistry.getRunningTasks().size())); |
---|
253 | |
---|
254 | PEUnit peUnit = (PEUnit)choosenResources.get(StandardResourceUnitName.PE); |
---|
255 | |
---|
256 | notifyComputingResources(peUnit, EnergyEventType.TASK_STARTED, exec); |
---|
257 | |
---|
258 | /*if(peUnit instanceof ProcessingElements){ |
---|
259 | ProcessingElements pes = (ProcessingElements) peUnit; |
---|
260 | for (ComputingResource resource : pes) { |
---|
261 | resource.handleEvent(new EnergyEvent(EnergyEventType.TASK_STARTED, exec)); |
---|
262 | } |
---|
263 | } else { |
---|
264 | ComputingResource resource = null; |
---|
265 | try { |
---|
266 | resource = ResourceController.getComputingResourceByName(peUnit.getResourceId()); |
---|
267 | } catch (ResourceException e) { |
---|
268 | return; |
---|
269 | } |
---|
270 | resource.handleEvent(new EnergyEvent(EnergyEventType.TASK_STARTED, exec)); |
---|
271 | } |
---|
272 | */ |
---|
273 | |
---|
274 | /*for(ExecTaskInterface etask : jobRegistry.getRunningTasks()){ |
---|
275 | System.out.println(etask.getJobId()); |
---|
276 | for(String taskId: etask.getVisitedResources()) |
---|
277 | System.out.println("====="+taskId); |
---|
278 | }*/ |
---|
279 | } |
---|
280 | |
---|
281 | public void finalizeExecutable(ExecTask execTask){ |
---|
282 | |
---|
283 | Executable exec = (Executable)execTask; |
---|
284 | exec.finalizeExecutable(); |
---|
285 | |
---|
286 | ExecTaskFilter filter = new ExecTaskFilter(exec.getUniqueId(), DCWormsTags.TASK_REQUESTED_TIME_EXPIRED); |
---|
287 | scheduler.sim_cancel(filter, null); |
---|
288 | |
---|
289 | Task task; |
---|
290 | Job job = jobRegistry.getJob(exec.getJobId()); |
---|
291 | try { |
---|
292 | task = job.getTask(exec.getTaskId()); |
---|
293 | } catch (NoSuchFieldException e) { |
---|
294 | return; |
---|
295 | } |
---|
296 | if(exec.getProcessesId() == null){ |
---|
297 | try { |
---|
298 | task.setStatus(exec.getStatus()); |
---|
299 | } catch (Exception e) { |
---|
300 | e.printStackTrace(); |
---|
301 | } |
---|
302 | } else { |
---|
303 | List<AbstractProcesses> processesList = task.getProcesses(); |
---|
304 | for(int i = 0; i < processesList.size(); i++){ |
---|
305 | AbstractProcesses processes = processesList.get(i); |
---|
306 | if(processes.getId().equals(exec.getProcessesId())){ |
---|
307 | processes.setStatus(exec.getStatus()); |
---|
308 | break; |
---|
309 | } |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | UsedResourcesList lastUsedList = exec.getUsedResources(); |
---|
314 | Map<ResourceUnitName, ResourceUnit> lastUsed = lastUsedList.getLast() |
---|
315 | .getResourceUnits(); |
---|
316 | getAllocationManager().freeResources(lastUsed); |
---|
317 | |
---|
318 | PEUnit peUnit = (PEUnit)lastUsed.get(StandardResourceUnitName.PE); |
---|
319 | notifyComputingResources(peUnit, EnergyEventType.TASK_FINISHED, exec); |
---|
320 | /*if(peUnit instanceof ProcessingElements){ |
---|
321 | ProcessingElements pes = (ProcessingElements) peUnit; |
---|
322 | for (ComputingResource resource : pes) { |
---|
323 | resource.handleEvent(new EnergyEvent(EnergyEventType.TASK_FINISHED, exec)); |
---|
324 | } |
---|
325 | } else { |
---|
326 | ComputingResource resource = null; |
---|
327 | try { |
---|
328 | resource = ResourceController.getComputingResourceByName(peUnit.getResourceId()); |
---|
329 | } catch (ResourceException e) { |
---|
330 | return; |
---|
331 | } |
---|
332 | resource.handleEvent(new EnergyEvent(EnergyEventType.TASK_FINISHED, exec)); |
---|
333 | }*/ |
---|
334 | |
---|
335 | //sendFinishedWorkloadUnit(executable); |
---|
336 | } |
---|
337 | |
---|
338 | protected void updateProcessingProgress() { |
---|
339 | double timeSpan = DoubleMath.subtract(Sim_system.clock(), lastUpdateTime); |
---|
340 | if (timeSpan <= 0.0) { |
---|
341 | // don't update when nothing changed |
---|
342 | return; |
---|
343 | } |
---|
344 | lastUpdateTime = Sim_system.clock(); |
---|
345 | Iterator<ExecTask> iter = jobRegistry.getRunningTasks().iterator(); |
---|
346 | while (iter.hasNext()) { |
---|
347 | ExecTask task = iter.next(); |
---|
348 | Executable exec = (Executable)task; |
---|
349 | UsedResourcesList usedResourcesList = exec.getUsedResources(); |
---|
350 | PEUnit peUnit = (PEUnit)usedResourcesList.getLast().getResourceUnits() |
---|
351 | .get(StandardResourceUnitName.PE); |
---|
352 | |
---|
353 | double load = getMIShare(timeSpan, peUnit); |
---|
354 | exec.setCompletionPercentage(100 * timeSpan/exec.getEstimatedDuration()); |
---|
355 | addTotalLoad(load); |
---|
356 | } |
---|
357 | } |
---|
358 | private void notifyComputingResources(PEUnit peUnit, EnergyEventType eventType, Object obj){ |
---|
359 | |
---|
360 | if(peUnit instanceof ProcessingElements){ |
---|
361 | ProcessingElements pes = (ProcessingElements) peUnit; |
---|
362 | for (ComputingResource resource : pes) { |
---|
363 | resource.handleEvent(new EnergyEvent(eventType, obj)); |
---|
364 | } |
---|
365 | } else { |
---|
366 | ComputingResource resource = null; |
---|
367 | try { |
---|
368 | resource = ResourceController.getComputingResourceByName(peUnit.getResourceId()); |
---|
369 | } catch (ResourceException e) { |
---|
370 | return; |
---|
371 | } |
---|
372 | resource.handleEvent(new EnergyEvent(eventType, obj)); |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | private double getMIShare(double timeSpan, PEUnit pes) { |
---|
377 | double localLoad; |
---|
378 | ResourceCalendar resCalendar = (ResourceCalendar) moduleList.getModule(ModuleType.RESOURCE_CALENDAR); |
---|
379 | if (resCalendar == null) |
---|
380 | localLoad = 0; |
---|
381 | else |
---|
382 | // 1 - localLoad_ = available MI share percentage |
---|
383 | localLoad = resCalendar.getCurrentLoad(); |
---|
384 | |
---|
385 | int speed = pes.getSpeed(); |
---|
386 | int cnt = pes.getAmount(); |
---|
387 | |
---|
388 | double totalMI = speed * cnt * timeSpan * (1 - localLoad); |
---|
389 | return totalMI; |
---|
390 | } |
---|
391 | |
---|
392 | protected void updateProcessingTimes(Sim_event ev) { |
---|
393 | updateProcessingProgress(); |
---|
394 | for (ExecTask execTask : jobRegistry.getRunningTasks()) { |
---|
395 | Executable exec = (Executable)execTask; |
---|
396 | List<String> visitedResource = exec.getVisitedResources(); |
---|
397 | String originResource = ev.get_data().toString(); |
---|
398 | if(!ArrayUtils.contains(visitedResource.toArray(new String[visitedResource.size()]), originResource)){ |
---|
399 | continue; |
---|
400 | } |
---|
401 | |
---|
402 | Map<ResourceUnitName, ResourceUnit> choosenResources = exec.getUsedResources().getLast().getResourceUnits(); |
---|
403 | double time = execTimeEstimationPlugin.execTimeEstimation(new SchedulingEvent(SchedulingEventType.RESOURCE_STATE_CHANGED), |
---|
404 | execTask, choosenResources, exec.getCompletionPercentage()); |
---|
405 | |
---|
406 | //check if the new estimated end time is equal to the previous one; if yes the continue without update |
---|
407 | if( DoubleMath.subtract((exec.getExecStartTime() + exec.getEstimatedDuration()), (new DateTime().getMillis()/1000 + time)) == 0.0){ |
---|
408 | continue; |
---|
409 | } |
---|
410 | ExecTaskFilter filter = new ExecTaskFilter(exec.getUniqueId(), DCWormsTags.TASK_EXECUTION_FINISHED); |
---|
411 | scheduler.sim_cancel(filter, null); |
---|
412 | scheduler.sendInternal(time, DCWormsTags.TASK_EXECUTION_FINISHED, execTask); |
---|
413 | } |
---|
414 | } |
---|
415 | |
---|
416 | public double calculateTotalLoad(int size) { |
---|
417 | // background load, defined during initialization |
---|
418 | double load; |
---|
419 | ResourceCalendar resCalendar = (ResourceCalendar) moduleList.getModule(ModuleType.RESOURCE_CALENDAR); |
---|
420 | if (resCalendar == null) |
---|
421 | load = 0; |
---|
422 | else |
---|
423 | load = resCalendar.getCurrentLoad(); |
---|
424 | |
---|
425 | int numberOfPE = 0; |
---|
426 | try { |
---|
427 | for(ResourceUnit resUnit : getResourceManager().getPE()){ |
---|
428 | numberOfPE = numberOfPE + resUnit.getAmount(); |
---|
429 | } |
---|
430 | } catch (Exception e) { |
---|
431 | numberOfPE = 1; |
---|
432 | } |
---|
433 | double tasksPerPE = (double) size / numberOfPE; |
---|
434 | load += Math.min(1.0 - load, tasksPerPE); |
---|
435 | |
---|
436 | return load; |
---|
437 | } |
---|
438 | |
---|
439 | public Accumulator getTotalLoad() { |
---|
440 | return accTotalLoad; |
---|
441 | } |
---|
442 | |
---|
443 | protected void addTotalLoad(double load) { |
---|
444 | accTotalLoad.add(load); |
---|
445 | } |
---|
446 | |
---|
447 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(String resourceName, |
---|
448 | ExecTask task) { |
---|
449 | |
---|
450 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
451 | ResourceManager resourceManager = this.resourceManager; |
---|
452 | if(resourceName != null){ |
---|
453 | ComputingResource resource = null; |
---|
454 | try { |
---|
455 | resource = resourceManager.getResourceByName(resourceName); |
---|
456 | } catch (ResourceException e) { |
---|
457 | return null; |
---|
458 | } |
---|
459 | resourceManager = new LocalResourceManager(resource); |
---|
460 | } |
---|
461 | |
---|
462 | int cpuRequest; |
---|
463 | try { |
---|
464 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
465 | } catch (NoSuchFieldException e) { |
---|
466 | cpuRequest = 1; |
---|
467 | } |
---|
468 | |
---|
469 | if (cpuRequest != 0) { |
---|
470 | |
---|
471 | List<ResourceUnit> availableUnits = null; |
---|
472 | try { |
---|
473 | availableUnits = getResourceManager().getPE(); |
---|
474 | } catch (ResourceException e) { |
---|
475 | return null; |
---|
476 | } |
---|
477 | |
---|
478 | List<ResourceUnit> choosenPEUnits = new ArrayList<ResourceUnit>(); |
---|
479 | for (int i = 0; i < availableUnits.size() && cpuRequest > 0; i++) { |
---|
480 | PEUnit peUnit = (PEUnit) availableUnits .get(i); |
---|
481 | if(peUnit.getFreeAmount() > 0){ |
---|
482 | int allocPE = Math.min(peUnit.getFreeAmount(), cpuRequest); |
---|
483 | cpuRequest = cpuRequest - allocPE; |
---|
484 | choosenPEUnits.add(peUnit.replicate(allocPE)); |
---|
485 | } |
---|
486 | } |
---|
487 | |
---|
488 | if(cpuRequest > 0){ |
---|
489 | return null; |
---|
490 | } |
---|
491 | map.put(StandardResourceUnitName.PE, choosenPEUnits.get(0)); |
---|
492 | } |
---|
493 | |
---|
494 | return map; |
---|
495 | } |
---|
496 | |
---|
497 | public void notifySubmittedWorkloadUnit(WorkloadUnit wu, boolean ack) { |
---|
498 | updateProcessingProgress(); |
---|
499 | registerWorkloadUnit(wu); |
---|
500 | } |
---|
501 | |
---|
502 | private void registerWorkloadUnit(WorkloadUnit wu){ |
---|
503 | if(!wu.isRegistered()){ |
---|
504 | wu.register(jobRegistry); |
---|
505 | } |
---|
506 | wu.accept(getWorkloadUnitHandler()); |
---|
507 | } |
---|
508 | |
---|
509 | class LocalWorkloadUnitHandler implements WorkloadUnitHandler{ |
---|
510 | |
---|
511 | public void handleJob(JobInterface<?> job){ |
---|
512 | |
---|
513 | if (log.isInfoEnabled()) |
---|
514 | log.info("Received job " + job.getId() + " at " + new DateTime(DateTimeUtilsExt.currentTimeMillis())); |
---|
515 | |
---|
516 | List<JobInterface<?>> jobsList = new ArrayList<JobInterface<?>>(); |
---|
517 | jobsList.add(job); |
---|
518 | TaskListImpl availableTasks = new TaskListImpl(); |
---|
519 | for(Task task: jobRegistry.getAvailableTasks(jobsList)){ |
---|
520 | task.setStatus((int)BrokerConstants.TASK_STATUS_QUEUED); |
---|
521 | availableTasks.add(task); |
---|
522 | } |
---|
523 | |
---|
524 | for(TaskInterface<?> task: availableTasks){ |
---|
525 | registerWorkloadUnit(task); |
---|
526 | } |
---|
527 | } |
---|
528 | |
---|
529 | public void handleTask(TaskInterface<?> t){ |
---|
530 | Task task = (Task)t; |
---|
531 | List<AbstractProcesses> processes = task.getProcesses(); |
---|
532 | |
---|
533 | if(processes == null || processes.size() == 0){ |
---|
534 | Executable exec = new Executable(task); |
---|
535 | registerWorkloadUnit(exec); |
---|
536 | } else { |
---|
537 | for(int j = 0; j < processes.size(); j++){ |
---|
538 | AbstractProcesses procesesSet = processes.get(j); |
---|
539 | Executable exec = new Executable(task, procesesSet); |
---|
540 | registerWorkloadUnit(exec); |
---|
541 | } |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | public void handleExecutable(ExecTask task){ |
---|
546 | |
---|
547 | Executable exec = (Executable) task; |
---|
548 | jobRegistry.addExecTask(exec); |
---|
549 | |
---|
550 | exec.trackResource(scheduler.get_name()); |
---|
551 | Scheduler parentScheduler = scheduler.getParent(); |
---|
552 | List<String> visitedResource = exec.getVisitedResources(); |
---|
553 | String [] visitedResourcesArray = visitedResource.toArray(new String[visitedResource.size()]); |
---|
554 | while (parentScheduler != null && !ArrayUtils.contains(visitedResourcesArray, parentScheduler.get_name())) { |
---|
555 | exec.trackResource(parentScheduler.get_name()); |
---|
556 | parentScheduler = parentScheduler.getParent(); |
---|
557 | } |
---|
558 | exec.setSchedulerName(scheduler.get_id()); |
---|
559 | |
---|
560 | TaskList newTasks = new TaskListImpl(); |
---|
561 | newTasks.add(exec); |
---|
562 | |
---|
563 | schedulingPlugin.placeTasksInQueues(newTasks, queues, getResourceManager(), moduleList); |
---|
564 | |
---|
565 | if (exec.getStatus() == DCWormsTags.QUEUED) { |
---|
566 | sendExecutableReadyEvent(exec); |
---|
567 | } |
---|
568 | } |
---|
569 | } |
---|
570 | |
---|
571 | public WorkloadUnitHandler getWorkloadUnitHandler() { |
---|
572 | return new LocalWorkloadUnitHandler(); |
---|
573 | } |
---|
574 | |
---|
575 | |
---|
576 | public LocalResourceManager getResourceManager() { |
---|
577 | if (resourceManager instanceof ResourceManager) |
---|
578 | return (LocalResourceManager) resourceManager; |
---|
579 | else |
---|
580 | return null; |
---|
581 | } |
---|
582 | |
---|
583 | } |
---|