[477] | 1 | package example.globalplugin; |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | import java.util.List; |
---|
| 5 | |
---|
| 6 | import org.apache.commons.logging.Log; |
---|
| 7 | import org.apache.commons.logging.LogFactory; |
---|
| 8 | |
---|
| 9 | import schedframe.events.scheduling.SchedulingEvent; |
---|
| 10 | import schedframe.scheduling.SchedulerDescription; |
---|
| 11 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 12 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
| 13 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 14 | import schedframe.scheduling.plan.impl.Allocation; |
---|
| 15 | import schedframe.scheduling.plan.impl.ScheduledTask; |
---|
| 16 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
[1396] | 17 | import schedframe.scheduling.plugin.Module; |
---|
| 18 | import schedframe.scheduling.plugin.ModuleList; |
---|
[477] | 19 | import schedframe.scheduling.plugin.grid.ResourceDiscovery; |
---|
[481] | 20 | import schedframe.scheduling.queue.QueueDescription; |
---|
[477] | 21 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 22 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 23 | import schedframe.scheduling.tasks.TaskInterface; |
---|
| 24 | import schedframe.scheduling.tasks.WorkloadUnit; |
---|
| 25 | |
---|
| 26 | public class GridFCFSLoadBalancingPlugin extends BaseGlobalPlugin { |
---|
| 27 | |
---|
| 28 | private Log log = LogFactory.getLog(GridFCFSLoadBalancingPlugin.class); |
---|
| 29 | |
---|
[481] | 30 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, |
---|
[477] | 31 | TaskQueueList queues, |
---|
| 32 | JobRegistry jobRegistry, |
---|
| 33 | ResourceManager resourceManager, ModuleList modules) { |
---|
| 34 | |
---|
| 35 | ResourceDiscovery resources = null; |
---|
| 36 | for(int i = 0; i < modules.size(); i++){ |
---|
| 37 | Module m = modules.get(i); |
---|
| 38 | switch(m.getType()){ |
---|
| 39 | case RESOURCE_DISCOVERY: resources = (ResourceDiscovery) m; |
---|
| 40 | break; |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | SchedulingPlan plan = new SchedulingPlan(); |
---|
| 45 | |
---|
| 46 | TaskQueue q = queues.get(0); |
---|
| 47 | int size = q.size(); |
---|
| 48 | |
---|
| 49 | // order of the resources on this list is not determined |
---|
| 50 | List<SchedulerDescription> availableResources = resources.getResources(); |
---|
| 51 | int resourceIdx = -1; |
---|
| 52 | |
---|
| 53 | for(int i = 0; i < size; i++) { |
---|
[478] | 54 | WorkloadUnit job = q.remove(0); |
---|
[477] | 55 | TaskInterface<?> task = (TaskInterface<?>)job; |
---|
| 56 | |
---|
| 57 | resourceIdx = findLeastLoadedResourceIdx(availableResources); |
---|
| 58 | SchedulerDescription sd = availableResources.get(resourceIdx); |
---|
| 59 | |
---|
| 60 | Allocation allocation = new Allocation(); |
---|
| 61 | allocation.setProcessesCount(1); |
---|
| 62 | allocation.setProviderName(sd.getProvider().getProviderId()); |
---|
| 63 | |
---|
| 64 | ScheduledTask scheduledTask = new ScheduledTask(task); |
---|
| 65 | scheduledTask.setTaskId(task.getId()); |
---|
| 66 | scheduledTask.setJobId(task.getJobId()); |
---|
| 67 | scheduledTask.addAllocation(allocation); |
---|
| 68 | |
---|
| 69 | plan.addTask(scheduledTask); |
---|
| 70 | } |
---|
| 71 | return plan; |
---|
| 72 | |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | private int findLeastLoadedResourceIdx(List<SchedulerDescription> availableResources){ |
---|
| 76 | int resourceIdx = -1; |
---|
| 77 | long minLoad = Long.MAX_VALUE; |
---|
[481] | 78 | |
---|
[477] | 79 | for(int i = 0; i < availableResources.size(); i++){ |
---|
| 80 | SchedulerDescription sd = availableResources.get(i); |
---|
| 81 | long totalLoad = getQueueLoad(sd.getAvailableQueues()); |
---|
| 82 | if(totalLoad < minLoad){ |
---|
| 83 | resourceIdx = i; |
---|
| 84 | minLoad = totalLoad; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | return resourceIdx; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | private long getQueueLoad(List<QueueDescription> queues){ |
---|
| 91 | long load = 0; |
---|
| 92 | for(QueueDescription queue : queues){ |
---|
| 93 | load = load + queue.getLoad(); |
---|
| 94 | } |
---|
| 95 | return load; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | } |
---|