source: DCWoRMS/branches/coolemall/src/example/globalplugin/GridFCFSLoadBalancingPlugin.java @ 1396

Revision 1396, 3.1 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package example.globalplugin;
2
3
4import java.util.List;
5
6import org.apache.commons.logging.Log;
7import org.apache.commons.logging.LogFactory;
8
9import schedframe.events.scheduling.SchedulingEvent;
10import schedframe.scheduling.SchedulerDescription;
11import schedframe.scheduling.manager.resources.ResourceManager;
12import schedframe.scheduling.manager.tasks.JobRegistry;
13import schedframe.scheduling.plan.SchedulingPlanInterface;
14import schedframe.scheduling.plan.impl.Allocation;
15import schedframe.scheduling.plan.impl.ScheduledTask;
16import schedframe.scheduling.plan.impl.SchedulingPlan;
17import schedframe.scheduling.plugin.Module;
18import schedframe.scheduling.plugin.ModuleList;
19import schedframe.scheduling.plugin.grid.ResourceDiscovery;
20import schedframe.scheduling.queue.QueueDescription;
21import schedframe.scheduling.queue.TaskQueue;
22import schedframe.scheduling.queue.TaskQueueList;
23import schedframe.scheduling.tasks.TaskInterface;
24import schedframe.scheduling.tasks.WorkloadUnit;
25
26public class GridFCFSLoadBalancingPlugin extends BaseGlobalPlugin {
27
28        private Log log = LogFactory.getLog(GridFCFSLoadBalancingPlugin.class);
29       
30        public SchedulingPlanInterface<?> schedule(SchedulingEvent event,
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++) {
54                        WorkloadUnit job = q.remove(0);
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;
78               
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}
Note: See TracBrowser for help on using the repository browser.