source: xssim/src/example/gridplugin/GridFCFSLoadBalancingPlugin.java @ 104

Revision 104, 3.4 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package example.gridplugin;
2
3
4import java.util.List;
5import java.util.Properties;
6
7import org.apache.commons.logging.Log;
8import org.apache.commons.logging.LogFactory;
9
10import schedframe.resources.ResourceDescription;
11import schedframe.scheduling.TaskInterface;
12import schedframe.scheduling.events.SchedulingEvent;
13import schedframe.scheduling.plugin.SchedulingPluginConfiguration;
14import schedframe.scheduling.plugin.configuration.DefaultConfiguration;
15import schedframe.scheduling.plugin.grid.Module;
16import schedframe.scheduling.plugin.grid.ModuleList;
17import schedframe.scheduling.plugin.grid.ResourceDiscovery;
18import test.rewolucja.GSSIMJobInterface;
19import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface;
20import test.rewolucja.scheduling.JobRegistryInterface;
21import test.rewolucja.scheduling.plan.AllocationNew;
22import test.rewolucja.scheduling.plan.ScheduledTaskNew;
23import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
24import test.rewolucja.scheduling.plan.SchedulingPlanNew;
25import test.rewolucja.scheduling.queue.GSSIMQueue;
26import test.rewolucja.scheduling.queue.QueueList;
27
28public class GridFCFSLoadBalancingPlugin  extends BaseGlobalPlugin {
29
30        private Log log = LogFactory.getLog(GridFCFSLoadBalancingPlugin.class);
31       
32        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event,
33                        QueueList queues,
34                        JobRegistryInterface jobRegistry,
35                        ResourceManagerInterface resourceManager, ModuleList modules)  {
36       
37                ResourceDiscovery resources = null;
38                for(int i = 0; i < modules.size(); i++){
39                        Module m = modules.get(i);
40                        switch(m.getType()){
41                                case RESOURCE_DISCOVERY: resources = (ResourceDiscovery) m;
42                                        break;
43                        }
44                }
45               
46                SchedulingPlanNew plan = new SchedulingPlanNew();
47               
48                GSSIMQueue q = queues.get(0);
49                int size = q.size();
50               
51                // order of the resources on this list is not determined
52                List<ResourceDescription> availableResources = resources.getResources(null);
53                int resourceIdx = -1;
54               
55                for(int i = 0; i < size; i++) {
56                        GSSIMJobInterface<?> job = q.remove(0);
57                        TaskInterface<?> task = (TaskInterface<?>)job;
58
59                        resourceIdx = findLeastLoadedResourceIdx(availableResources);
60                        ResourceDescription rd = availableResources.get(resourceIdx);
61                       
62                        AllocationNew allocation = new AllocationNew();
63                        allocation.setProcessesCount(1);
64                        allocation.setProviderName(rd.getProvider().getProviderId());
65                       
66                        ScheduledTaskNew scheduledTask = new ScheduledTaskNew(task);
67                        scheduledTask.setTaskId(task.getId());
68                        scheduledTask.setJobId(task.getJobId());
69                        scheduledTask.addAllocation(allocation);               
70                       
71                        plan.addTask(scheduledTask);
72                }
73                return plan;
74               
75        }
76
77        public SchedulingPluginConfiguration getConfiguration() {
78                return DefaultConfiguration.forGridPlugin();
79        }
80
81        public String getName() {
82                return getClass().getName();
83        }
84
85        public void init(Properties properties) {
86                // no extra initialization is expected.
87        }
88       
89        private int findLeastLoadedResourceIdx(List<ResourceDescription> availableResources){
90               
91                int resourceIdx = -1;
92                long maxLoad = Long.MAX_VALUE;
93                for(int i = 0; i < availableResources.size(); i++){
94                        ResourceDescription rd = availableResources.get(i);
95                        try {
96                                if(rd.getQueueLoad(null) < maxLoad){
97                                        resourceIdx = i;
98                                        maxLoad = rd.getQueueLoad(null);
99                                }
100                        } catch (NoSuchFieldException e) {
101                                log.warn(e.getMessage());
102                        }
103                }
104                return resourceIdx;
105        }
106
107}
Note: See TracBrowser for help on using the repository browser.