1 | package example.gridplugin; |
---|
2 | |
---|
3 | |
---|
4 | import java.util.List; |
---|
5 | import java.util.Properties; |
---|
6 | |
---|
7 | import org.apache.commons.logging.Log; |
---|
8 | import org.apache.commons.logging.LogFactory; |
---|
9 | |
---|
10 | import schedframe.resources.ResourceDescription; |
---|
11 | import schedframe.scheduling.TaskInterface; |
---|
12 | import schedframe.scheduling.events.SchedulingEvent; |
---|
13 | import schedframe.scheduling.plugin.SchedulingPluginConfiguration; |
---|
14 | import schedframe.scheduling.plugin.configuration.DefaultConfiguration; |
---|
15 | import schedframe.scheduling.plugin.grid.Module; |
---|
16 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
17 | import schedframe.scheduling.plugin.grid.ResourceDiscovery; |
---|
18 | import test.rewolucja.GSSIMJobInterface; |
---|
19 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
20 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
21 | import test.rewolucja.scheduling.plan.AllocationNew; |
---|
22 | import test.rewolucja.scheduling.plan.ScheduledTaskNew; |
---|
23 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
24 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
25 | import test.rewolucja.scheduling.queue.GSSIMQueue; |
---|
26 | import test.rewolucja.scheduling.queue.QueueList; |
---|
27 | |
---|
28 | public 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 | } |
---|